@@ -51,58 +51,57 @@ export function applyUsageLimitsUpdate(input: {
5151 readonly previous : ServerProviderUsageLimits | undefined ;
5252 readonly update : ProviderUsageLimitsUpdate ;
5353 readonly checkedAt : string ;
54- /**
55- * The update is the account's whole state: windows it leaves out are
56- * dropped, and a reset time it omits is cleared rather than kept.
57- */
54+ /** Replace the whole window set, including omitted resets and windows. */
5855 readonly replace ?: boolean ;
5956} ) : ServerProviderUsageLimits | undefined {
6057 const { previous, update } = input ;
61- const replace = input . replace === true ;
62- if ( update . windows . length === 0 || previous ?. unavailable ?. reason === "unsupported" ) {
58+ if ( previous ?. unavailable ?. reason === "unsupported" ) return previous ;
59+ if ( input . replace ) {
60+ const next = makeUsageLimits ( {
61+ checkedAt : input . checkedAt ,
62+ windows : update . windows . map ( ( window ) => ( {
63+ ...window ,
64+ usedPercent : clampPercent ( window . usedPercent ) ,
65+ } ) ) ,
66+ } ) ;
67+ if (
68+ previous &&
69+ ! previous . unavailable &&
70+ previous . checkedAt === next . checkedAt &&
71+ previous . windows . length === next . windows . length &&
72+ previous . windows . every ( ( window , index ) => usageWindowEquals ( window , next . windows [ index ] ! ) )
73+ )
74+ return previous ;
75+ return { ...next , ...( previous ?. resetCredits ? { resetCredits : previous . resetCredits } : { } ) } ;
76+ }
77+ if ( update . windows . length === 0 ) {
6378 return previous ;
6479 }
65- const published = new Map ( previous ?. windows . map ( ( window ) => [ window . id , window ] as const ) ) ;
66- const merged = replace
67- ? new Map < ServerProviderUsageWindow [ "id" ] , ServerProviderUsageWindow > ( )
68- : new Map ( published ) ;
69- // Decide "nothing changed" per window so a no-op never allocates a new
70- // snapshot. A whole-state report that only moves the time is still news:
71- // Limits picks the freshest report per account by `checkedAt`.
72- let changed =
73- replace &&
74- ( update . windows . length !== published . size ||
75- input . checkedAt !== previous ?. checkedAt ||
76- previous ?. unavailable !== undefined ) ;
80+ const merged = new Map ( previous ?. windows . map ( ( window ) => [ window . id , window ] as const ) ) ;
81+ // Codex repeats unchanged limits on token ticks; avoid allocating snapshots for no-ops.
82+ let changed = false ;
7783 for ( const window of update . windows ) {
78- const existing = published . get ( window . id ) ;
84+ const existing = merged . get ( window . id ) ;
7985 const next : ServerProviderUsageWindow = {
8086 ...window ,
8187 usedPercent : clampPercent ( window . usedPercent ) ,
82- ...( ! replace && window . resetsAt === undefined && existing ?. resetsAt !== undefined
88+ ...( window . resetsAt === undefined && existing ?. resetsAt !== undefined
8389 ? { resetsAt : existing . resetsAt }
8490 : { } ) ,
85- ...( ! replace &&
86- window . windowDurationMins === undefined &&
87- existing ?. windowDurationMins !== undefined
91+ ...( window . windowDurationMins === undefined && existing ?. windowDurationMins !== undefined
8892 ? { windowDurationMins : existing . windowDurationMins }
8993 : { } ) ,
9094 } ;
9195 if ( existing === undefined || ! usageWindowEquals ( existing , next ) ) {
9296 merged . set ( window . id , next ) ;
9397 changed = true ;
94- } else if ( replace ) {
95- merged . set ( window . id , existing ) ;
9698 }
9799 }
98100 if ( ! changed && previous !== undefined ) {
99101 return previous ;
100102 }
101- const unavailable = replace
102- ? undefined
103- : previous === undefined
104- ? { reason : "probeFailed" as const }
105- : previous . unavailable ;
103+ const unavailable =
104+ previous === undefined ? { reason : "probeFailed" as const } : previous . unavailable ;
106105 return {
107106 ...makeUsageLimits ( { checkedAt : input . checkedAt , windows : merged . values ( ) } ) ,
108107 ...( previous ?. resetCredits !== undefined ? { resetCredits : previous . resetCredits } : { } ) ,
0 commit comments