@@ -22,6 +22,7 @@ import {
2222} from "@t3tools/contracts" ;
2323import * as Context from "effect/Context" ;
2424import * as DateTime from "effect/DateTime" ;
25+ import * as Deferred from "effect/Deferred" ;
2526import * as Duration from "effect/Duration" ;
2627import * as Effect from "effect/Effect" ;
2728import * as FileSystem from "effect/FileSystem" ;
@@ -301,17 +302,30 @@ function observedAtMs(window: UsageLimitWindow): number {
301302 return Number . isNaN ( ms ) ? Number . NEGATIVE_INFINITY : ms ;
302303}
303304
305+ /** The newest instant among a reading's windows; oldest possible when it has none. */
306+ function latestObservedMs ( limits : UsageProviderLimits | undefined ) : number {
307+ let latest = Number . NEGATIVE_INFINITY ;
308+ for ( const window of limits ?. windows ?? [ ] ) latest = Math . max ( latest , observedAtMs ( window ) ) ;
309+ return latest ;
310+ }
311+
304312/**
305313 * Folds a new observation into what is already known for the provider. Each
306- * window is replaced by its newer reading; windows the update did not mention
307- * are kept, which is what turns Claude's one-window events into a full set.
314+ * window is replaced by its newer reading. A partial update keeps the windows
315+ * it did not mention, which is what turns Claude's one-window events into a
316+ * full set; a complete reading (Codex's on-demand read) replaces the set, so a
317+ * bucket the account stopped reporting disappears.
308318 */
309319export function mergeProviderLimits (
310320 previous : UsageProviderLimits | undefined ,
311321 next : UsageProviderLimits ,
322+ options : { readonly complete ?: boolean } = { } ,
312323) : UsageProviderLimits {
313324 const byId = new Map < string , UsageLimitWindow > ( ) ;
314- for ( const window of previous ?. windows ?? [ ] ) byId . set ( window . id , window ) ;
325+ if ( ! options . complete ) {
326+ for ( const window of previous ?. windows ?? [ ] ) byId . set ( window . id , window ) ;
327+ }
328+ const nextIsNewest = latestObservedMs ( next ) >= latestObservedMs ( previous ) ;
315329 for ( const window of next . windows ) {
316330 const existing = byId . get ( window . id ) ;
317331 if ( existing === undefined || observedAtMs ( existing ) <= observedAtMs ( window ) ) {
@@ -326,7 +340,9 @@ export function mergeProviderLimits(
326340 ( right . durationMinutes ?? Number . MAX_SAFE_INTEGER ) ||
327341 left . id . localeCompare ( right . id ) ,
328342 ) ;
329- return { provider : next . provider , plan : next . plan ?? previous ?. plan ?? null , windows } ;
343+ // A delayed older observation must not relabel the plan either.
344+ const plan = next . plan !== null && nextIsNewest ? next . plan : ( previous ?. plan ?? null ) ;
345+ return { provider : next . provider , plan, windows } ;
330346}
331347
332348function sameWindow ( left : UsageLimitWindow , right : UsageLimitWindow ) : boolean {
@@ -354,12 +370,19 @@ function sameLimits(left: UsageProviderLimits, right: UsageProviderLimits): bool
354370export class UsageLimitsStore extends Context . Service <
355371 UsageLimitsStore ,
356372 {
357- /** Folds one observation into the store. */
358- readonly record : ( limits : UsageProviderLimits ) => Effect . Effect < void > ;
373+ /**
374+ * Folds one observation into the store. A `complete` reading replaces the
375+ * provider's window set instead of merging into it.
376+ */
377+ readonly record : (
378+ limits : UsageProviderLimits ,
379+ options ?: { readonly complete ?: boolean } ,
380+ ) => Effect . Effect < void > ;
359381 /**
360382 * Asks providers that can answer on demand (Codex) for a live reading.
361383 * Never fails: a provider that cannot answer leaves its last reading in
362- * place.
384+ * place. Concurrent callers share one in-flight read rather than each
385+ * launching a CLI.
363386 */
364387 readonly refresh : Effect . Effect < void > ;
365388 readonly read : Effect . Effect < readonly UsageProviderLimits [ ] > ;
@@ -410,11 +433,11 @@ export const make = Effect.gen(function* () {
410433 Effect . catchCause ( ( ) => Effect . void ) ,
411434 ) ;
412435
413- const record : UsageLimitsStore [ "Service" ] [ "record" ] = ( limits ) =>
436+ const record : UsageLimitsStore [ "Service" ] [ "record" ] = ( limits , options ) =>
414437 recordSemaphore . withPermits ( 1 ) (
415438 Effect . suspend ( ( ) => {
416439 const previous = byProvider . get ( limits . provider ) ;
417- const merged = mergeProviderLimits ( previous , limits ) ;
440+ const merged = mergeProviderLimits ( previous , limits , options ) ;
418441 // Claude repeats the same reading on every request inside a turn; only
419442 // a changed figure is worth a write.
420443 if ( previous !== undefined && sameLimits ( previous , merged ) ) return Effect . void ;
@@ -423,7 +446,7 @@ export const make = Effect.gen(function* () {
423446 } ) ,
424447 ) ;
425448
426- const refresh : UsageLimitsStore [ "Service" ] [ "refresh" ] = Effect . gen ( function * ( ) {
449+ const refreshOnce = Effect . gen ( function * ( ) {
427450 const settings = yield * settingsService . getSettings . pipe (
428451 Effect . catchCause ( ( ) => Effect . succeed ( null ) ) ,
429452 ) ;
@@ -446,7 +469,24 @@ export const make = Effect.gen(function* () {
446469 ) ;
447470 if ( response === null ) return ;
448471 const limits = parseCodexRateLimitsRead ( response , observedAt ) ;
449- if ( limits !== null ) yield * record ( limits ) ;
472+ if ( limits !== null ) yield * record ( limits , { complete : true } ) ;
473+ } ) ;
474+
475+ // Single-flight: every usage request asks for a refresh, and each one is a
476+ // CLI process. Callers that arrive while one is running wait for it.
477+ let inflightRefresh : Deferred . Deferred < void > | null = null ;
478+ const refresh : UsageLimitsStore [ "Service" ] [ "refresh" ] = Effect . suspend ( ( ) => {
479+ const existing = inflightRefresh ;
480+ if ( existing !== null ) return Deferred . await ( existing ) ;
481+ const created = Deferred . makeUnsafe < void > ( ) ;
482+ inflightRefresh = created ;
483+ return refreshOnce . pipe (
484+ Effect . ensuring (
485+ Effect . sync ( ( ) => {
486+ inflightRefresh = null ;
487+ } ) . pipe ( Effect . andThen ( Deferred . succeed ( created , undefined ) ) ) ,
488+ ) ,
489+ ) ;
450490 } ) ;
451491
452492 const read : UsageLimitsStore [ "Service" ] [ "read" ] = Effect . sync ( ( ) => [ ...byProvider . values ( ) ] ) ;
0 commit comments