@@ -15,6 +15,8 @@ import * as NodeOS from "node:os";
1515
1616import {
1717 USAGE_CONTRACT_VERSION ,
18+ type ClaudeSettings ,
19+ type CodexSettings ,
1820 type UsageProviderKind ,
1921 type UsageSource ,
2022 type UsageSummary ,
@@ -63,6 +65,7 @@ import {
6365import type { UsageRecord } from "./usageTranscripts.ts" ;
6466import {
6567 awaitSubscriptionLimits ,
68+ makeSubscriptionLimitsCacheKey ,
6669 makeSubscriptionLimitsCacheEntry ,
6770 makeSubscriptionLimitsDevFixture ,
6871 normalizeClaudeSubscriptionLimits ,
@@ -141,6 +144,20 @@ export const layerTest = Layer.succeed(
141144 } ) ,
142145) ;
143146
147+ interface SubscriptionLimitsContext {
148+ readonly claude : {
149+ readonly enabled : boolean ;
150+ readonly cacheKey : string ;
151+ readonly settings : ClaudeSettings ;
152+ } ;
153+ readonly codex : {
154+ readonly enabled : boolean ;
155+ readonly cacheKey : string ;
156+ readonly settings : CodexSettings ;
157+ readonly canUseTranscriptSnapshot : boolean ;
158+ } ;
159+ }
160+
144161export const make = Effect . gen ( function * ( ) {
145162 const fileSystem = yield * FileSystem . FileSystem ;
146163 const path = yield * Path . Path ;
@@ -161,10 +178,10 @@ export const make = Effect.gen(function* () {
161178 let rates : RateTable = new Map ( ) ;
162179 let ratesFetchedAtMs : number | null = null ;
163180 let ratesStatus : UsageSummary [ "pricing" ] [ "status" ] = "unavailable" ;
164- const subscriptionLimitsCache = new Map < UsageProviderKind , SubscriptionLimitsCacheEntry > ( ) ;
181+ const subscriptionLimitsCache = new Map < string , SubscriptionLimitsCacheEntry > ( ) ;
165182
166183 const readCurrentSubscriptionLimits = Effect . fn ( "UsageService.readCurrentSubscriptionLimits" ) (
167- function * ( ) {
184+ function * ( context : SubscriptionLimitsContext ) {
168185 const now = yield * Clock . currentTimeMillis ;
169186 const fixture = makeSubscriptionLimitsDevFixture (
170187 config . devUrl !== undefined ,
@@ -173,20 +190,11 @@ export const make = Effect.gen(function* () {
173190 ) ;
174191 if ( fixture !== null ) return fixture ;
175192
176- const settings = yield * settingsService . getSettings . pipe (
177- Effect . catchCause ( ( ) => Effect . succeed ( null ) ) ,
178- ) ;
179- const providers : readonly UsageProviderKind [ ] =
180- settings === null
181- ? [ "codex" , "claude" ]
182- : [
183- ...( settings . providers . codex . enabled ? ( [ "codex" ] as const ) : [ ] ) ,
184- ...( settings . providers . claudeAgent . enabled ? ( [ "claude" ] as const ) : [ ] ) ,
185- ] ;
186-
187- return providers . flatMap ( ( provider ) => {
193+ const providers = [ context . codex , context . claude ] . filter ( ( { enabled } ) => enabled ) ;
194+
195+ return providers . flatMap ( ( { cacheKey } ) => {
188196 const outcome = readSubscriptionLimitsCacheEntry (
189- subscriptionLimitsCache . get ( provider ) ,
197+ subscriptionLimitsCache . get ( cacheKey ) ,
190198 now ,
191199 ) ;
192200 return outcome ?. _tag === "Success" && outcome . limits !== null ? [ outcome . limits ] : [ ] ;
@@ -195,20 +203,21 @@ export const make = Effect.gen(function* () {
195203 ) ;
196204
197205 const cacheSubscriptionLimitsProbe = Effect . fn ( "UsageService.cacheSubscriptionLimitsProbe" ) (
198- function * ( provider : UsageProviderKind , outcome : SubscriptionLimitsProbeOutcome ) {
206+ function * ( cacheKey : string , outcome : SubscriptionLimitsProbeOutcome ) {
199207 const fetchedAtMs = yield * Clock . currentTimeMillis ;
200208 subscriptionLimitsCache . set (
201- provider ,
209+ cacheKey ,
202210 makeSubscriptionLimitsCacheEntry (
203211 outcome ,
204212 fetchedAtMs ,
205- subscriptionLimitsCache . get ( provider ) ,
213+ subscriptionLimitsCache . get ( cacheKey ) ,
206214 ) ,
207215 ) ;
208216 } ,
209217 ) ;
210218
211219 const refreshSubscriptionLimits = Effect . fn ( "UsageService.refreshSubscriptionLimits" ) ( function * (
220+ context : SubscriptionLimitsContext ,
212221 codexSnapshot : CodexTranscriptRateLimitsSnapshot | null ,
213222 ) {
214223 const now = yield * Clock . currentTimeMillis ;
@@ -223,18 +232,20 @@ export const make = Effect.gen(function* () {
223232 // it before taking the probe lock so a slow Claude refresh cannot hold it
224233 // past the page response budget or its own freshness deadline.
225234 if (
235+ context . codex . enabled &&
236+ context . codex . canUseTranscriptSnapshot &&
226237 codexSnapshot !== null &&
227- shouldReadCodexTranscriptSnapshot ( subscriptionLimitsCache . get ( " codex" ) , now )
238+ shouldReadCodexTranscriptSnapshot ( subscriptionLimitsCache . get ( context . codex . cacheKey ) , now )
228239 ) {
229240 subscriptionLimitsCache . set (
230- " codex" ,
241+ context . codex . cacheKey ,
231242 makeSubscriptionLimitsCacheEntry (
232243 {
233244 _tag : "Success" ,
234245 limits : normalizeCodexSubscriptionLimits ( codexSnapshot . response ) ,
235246 } ,
236247 now ,
237- subscriptionLimitsCache . get ( " codex" ) ,
248+ subscriptionLimitsCache . get ( context . codex . cacheKey ) ,
238249 codexSnapshot . observedAtMs ,
239250 ) ,
240251 ) ;
@@ -244,55 +255,47 @@ export const make = Effect.gen(function* () {
244255 Effect . gen ( function * ( ) {
245256 const probeStartedAtMs = yield * Clock . currentTimeMillis ;
246257
247- const cachedOutcomes = new Map < UsageProviderKind , SubscriptionLimitsProbeOutcome > ( ) ;
248- for ( const [ provider , cached ] of subscriptionLimitsCache ) {
249- const outcome = readSubscriptionLimitsCacheEntry ( cached , probeStartedAtMs ) ;
250- if ( outcome !== undefined ) cachedOutcomes . set ( provider , outcome ) ;
251- }
252-
253- const settings = yield * settingsService . getSettings . pipe (
254- Effect . catchCause ( ( ) => Effect . succeed ( null ) ) ,
255- ) ;
256- if ( settings === null ) return ;
257-
258- const cachedClaude = settings . providers . claudeAgent . enabled
259- ? cachedOutcomes . get ( "claude" )
258+ const cachedClaude = context . claude . enabled
259+ ? readSubscriptionLimitsCacheEntry (
260+ subscriptionLimitsCache . get ( context . claude . cacheKey ) ,
261+ probeStartedAtMs ,
262+ )
260263 : undefined ;
261- const cachedCodex = settings . providers . codex . enabled
262- ? cachedOutcomes . get ( "codex" )
264+ const cachedCodex = context . codex . enabled
265+ ? readSubscriptionLimitsCacheEntry (
266+ subscriptionLimitsCache . get ( context . codex . cacheKey ) ,
267+ probeStartedAtMs ,
268+ )
263269 : undefined ;
264- const codexHomeLayout = yield * resolveCodexHomeLayout ( settings . providers . codex ) . pipe (
265- Effect . provideService ( Path . Path , path ) ,
266- ) ;
267- const codexProbeSettings = {
268- ...settings . providers . codex ,
269- homePath : codexHomeLayout . effectiveHomePath ?? "" ,
270- } ;
271270 yield * Effect . all (
272271 [
273- settings . providers . claudeAgent . enabled && cachedClaude === undefined
272+ context . claude . enabled && cachedClaude === undefined
274273 ? runSubscriptionLimitsProbe (
275- probeClaudeUsage (
276- settings . providers . claudeAgent ,
277- hostEnvironment ,
278- config . cwd ,
279- ) . pipe (
274+ probeClaudeUsage ( context . claude . settings , hostEnvironment , config . cwd ) . pipe (
280275 Effect . provideService ( FileSystem . FileSystem , fileSystem ) ,
281276 Effect . provideService ( Path . Path , path ) ,
282277 ) ,
283278 normalizeClaudeSubscriptionLimits ,
284- ) . pipe ( Effect . flatMap ( ( outcome ) => cacheSubscriptionLimitsProbe ( "claude" , outcome ) ) )
279+ ) . pipe (
280+ Effect . flatMap ( ( outcome ) =>
281+ cacheSubscriptionLimitsProbe ( context . claude . cacheKey , outcome ) ,
282+ ) ,
283+ )
285284 : Effect . void ,
286- settings . providers . codex . enabled && cachedCodex === undefined
285+ context . codex . enabled && cachedCodex === undefined
287286 ? runSubscriptionLimitsProbe (
288- probeCodexRateLimits ( codexProbeSettings , hostEnvironment , config . cwd ) . pipe (
287+ probeCodexRateLimits ( context . codex . settings , hostEnvironment , config . cwd ) . pipe (
289288 Effect . provideService (
290289 ChildProcessSpawner . ChildProcessSpawner ,
291290 childProcessSpawner ,
292291 ) ,
293292 ) ,
294293 normalizeCodexSubscriptionLimits ,
295- ) . pipe ( Effect . flatMap ( ( outcome ) => cacheSubscriptionLimitsProbe ( "codex" , outcome ) ) )
294+ ) . pipe (
295+ Effect . flatMap ( ( outcome ) =>
296+ cacheSubscriptionLimitsProbe ( context . codex . cacheKey , outcome ) ,
297+ ) ,
298+ )
296299 : Effect . void ,
297300 ] ,
298301 { concurrency : "unbounded" } ,
@@ -394,15 +397,46 @@ export const make = Effect.gen(function* () {
394397 ? path . resolve ( expandHomePath ( grokHomeEnv ) )
395398 : path . join ( NodeOS . homedir ( ) , ".grok" ) ;
396399
397- return [
398- { provider : "claude" as const , dir : claudeDir } ,
399- { provider : "codex" as const , dir : path . join ( codexLayout . sharedHomePath , "sessions" ) } ,
400- {
401- provider : "grok" as const ,
402- dir : path . join ( grokHome , "sessions" ) ,
403- fileName : "updates.jsonl" ,
404- } ,
405- ] ;
400+ const codexProbeSettings = {
401+ ...settings . providers . codex ,
402+ homePath : codexLayout . effectiveHomePath ?? "" ,
403+ } ;
404+
405+ return {
406+ dirs : [
407+ { provider : "claude" as const , dir : claudeDir } ,
408+ { provider : "codex" as const , dir : path . join ( codexLayout . sharedHomePath , "sessions" ) } ,
409+ {
410+ provider : "grok" as const ,
411+ dir : path . join ( grokHome , "sessions" ) ,
412+ fileName : "updates.jsonl" ,
413+ } ,
414+ ] ,
415+ subscriptionLimits : {
416+ claude : {
417+ enabled : settings . providers . claudeAgent . enabled ,
418+ cacheKey : makeSubscriptionLimitsCacheKey ( {
419+ provider : "claude" ,
420+ binaryPath : settings . providers . claudeAgent . binaryPath ,
421+ homePath : claudeHome ,
422+ } ) ,
423+ settings : settings . providers . claudeAgent ,
424+ } ,
425+ codex : {
426+ enabled : settings . providers . codex . enabled ,
427+ cacheKey : makeSubscriptionLimitsCacheKey ( {
428+ provider : "codex" ,
429+ binaryPath : settings . providers . codex . binaryPath ,
430+ homePath : codexLayout . effectiveHomePath ?? codexLayout . sharedHomePath ,
431+ launchArgs : settings . providers . codex . launchArgs ,
432+ } ) ,
433+ settings : codexProbeSettings ,
434+ // Shadow homes share transcripts while keeping credentials separate,
435+ // so a transcript snapshot cannot identify the active account there.
436+ canUseTranscriptSnapshot : codexLayout . mode === "direct" ,
437+ } ,
438+ } satisfies SubscriptionLimitsContext ,
439+ } ;
406440 } ) ;
407441
408442 /**
@@ -505,7 +539,10 @@ export const make = Effect.gen(function* () {
505539 const startedAtMs = yield * Clock . currentTimeMillis ;
506540 // The home resolvers ask for `Path` themselves; satisfy them from the
507541 // instance we already hold so `readSummary` stays context-free.
508- const dirs = yield * resolveTranscriptDirs ( ) . pipe ( Effect . provideService ( Path . Path , path ) ) ;
542+ const resolvedUsage = yield * resolveTranscriptDirs ( ) . pipe (
543+ Effect . provideService ( Path . Path , path ) ,
544+ ) ;
545+ const { dirs, subscriptionLimits : subscriptionLimitsContext } = resolvedUsage ;
509546 const windowStart = DateTime . make ( `${ input . sinceDay } T00:00:00Z` ) ;
510547 if ( Option . isNone ( windowStart ) ) {
511548 return yield * new UsageReadError ( {
@@ -522,19 +559,23 @@ export const make = Effect.gen(function* () {
522559 ? [ ]
523560 : yield * Effect . promise ( ( ) => listTranscriptFiles ( codexDir , windowStartMs ) ) ;
524561 if ( codexDir !== undefined ) prefetchedFiles . set ( codexDir , codexFiles ) ;
525- const codexSnapshot = shouldReadCodexTranscriptSnapshot (
526- subscriptionLimitsCache . get ( "codex" ) ,
527- startedAtMs ,
528- )
529- ? yield * Effect . promise ( ( ) =>
530- readFreshCodexRateLimitsSnapshot (
531- codexFiles ,
532- startedAtMs - SUBSCRIPTION_LIMITS_SUCCESS_TTL_MS ,
533- startedAtMs ,
534- ) ,
535- )
536- : null ;
537- const subscriptionLimitsFiber = yield * refreshSubscriptionLimits ( codexSnapshot ) . pipe (
562+ const codexSnapshot =
563+ shouldReadCodexTranscriptSnapshot (
564+ subscriptionLimitsCache . get ( subscriptionLimitsContext . codex . cacheKey ) ,
565+ startedAtMs ,
566+ ) && subscriptionLimitsContext . codex . canUseTranscriptSnapshot
567+ ? yield * Effect . promise ( ( ) =>
568+ readFreshCodexRateLimitsSnapshot (
569+ codexFiles ,
570+ startedAtMs - SUBSCRIPTION_LIMITS_SUCCESS_TTL_MS ,
571+ startedAtMs ,
572+ ) ,
573+ )
574+ : null ;
575+ const subscriptionLimitsFiber = yield * refreshSubscriptionLimits (
576+ subscriptionLimitsContext ,
577+ codexSnapshot ,
578+ ) . pipe (
538579 // Subscription meters are optional. Provider payload drift must not make
539580 // transcript usage unavailable.
540581 Effect . catchCause ( ( ) => Effect . void ) ,
@@ -636,7 +677,7 @@ export const make = Effect.gen(function* () {
636677 const finishedAtMs = yield * Clock . currentTimeMillis ;
637678 const subscriptionLimits = yield * awaitSubscriptionLimits (
638679 subscriptionLimitsFiber ,
639- readCurrentSubscriptionLimits ( ) ,
680+ readCurrentSubscriptionLimits ( subscriptionLimitsContext ) ,
640681 ) ;
641682
642683 return {
0 commit comments