@@ -9,11 +9,9 @@ import { withSavepoint } from '../store/savepoint.js';
99import { runSqliteWrite } from '../store/sqlite-write.js' ;
1010import type { Store } from '../store/store.js' ;
1111import { KeyQuotaResets , type StageQuota } from './key-quota-resets.js' ;
12+ import { KeySettlementRecovery } from './key-settlement-recovery.js' ;
1213import type { AccountKeyPolicy } from './key-types.js' ;
1314import type { KeyUsage } from './key-usage.js' ;
14- import { keySettlements } from './key-settlement-candidates.js' ;
15-
16- const RECOVERY_PAGE = 64 ;
1715
1816type AccountingWitness = {
1917 pendingOwner : string | null ;
@@ -45,13 +43,10 @@ type HeldAdmission = {
4543} ;
4644
4745export class KeyAccounting {
48- private timer ?: NodeJS . Timeout ;
49- private warned = false ;
50- private recoveryAfter = '0' ;
51- private retrySweep = false ;
5246 private readonly held = new Map < string , HeldSettlement > ( ) ;
5347 private readonly admissions = new Map < string , HeldAdmission > ( ) ;
5448 private readonly resets : KeyQuotaResets ;
49+ private readonly recovery : KeySettlementRecovery ;
5550 constructor (
5651 private store : Store ,
5752 private usage : KeyUsage ,
@@ -63,7 +58,23 @@ export class KeyAccounting {
6358 usage ,
6459 ( owner , key ) => this . assertResettable ( owner , key ) ,
6560 ( owner , key , run , total , status ) => this . settle ( key , owner , run , total , status ) ,
66- ( ) => this . schedule ( 5000 ) ,
61+ ( ) => this . recovery . request ( ) ,
62+ ) ;
63+ this . recovery = new KeySettlementRecovery (
64+ store ,
65+ [
66+ { size : ( ) => this . resets . waitingReadbacks , next : ( ) => this . resets . recoverOne ( ) } ,
67+ { size : ( ) => this . admissions . size , next : ( ) => this . recoverAdmission ( ) } ,
68+ { size : ( ) => this . held . size , next : ( ) => this . recoverHeld ( ) } ,
69+ ] ,
70+ ( r ) =>
71+ this . settle (
72+ String ( r . key_id ) ,
73+ String ( r . owner ) ,
74+ String ( r . run_id ) ,
75+ Number ( r . total_tokens ) ,
76+ r . status as RunStatus ,
77+ ) ,
6778 ) ;
6879 }
6980
@@ -114,7 +125,7 @@ export class KeyAccounting {
114125 ) : void {
115126 this . admissions . set ( keyId , { owner, issue, publication } ) ;
116127 this . store . accountingReadbacks . add ( issue . runId ) ;
117- this . schedule ( 5000 ) ;
128+ this . recovery . request ( ) ;
118129 }
119130 private inspectAdmission ( keyId : string , { owner, issue } : HeldAdmission ) {
120131 const seen = issue . inspect ( ) ;
@@ -129,18 +140,16 @@ export class KeyAccounting {
129140 }
130141 return 'unknown' ;
131142 }
132- private recoverAdmissions ( ) : void {
133- for ( let n = Math . min ( this . admissions . size , RECOVERY_PAGE ) ; n > 0 ; n -- ) {
134- const [ key , ticket ] = this . admissions . entries ( ) . next ( ) . value ! ;
135- const seen = this . inspectAdmission ( key , ticket ) ;
136- this . admissions . delete ( key ) ;
137- if ( seen === 'unknown' ) this . admissions . set ( key , ticket ) ;
138- else {
139- this . store . accountingReadbacks . delete ( ticket . issue . runId ) ;
140- // Publish only accounting, never rerun submission or enqueue from a readback timer.
141- if ( seen === 'committed' ) ticket . publication ( ) ;
142- else ticket . publication . discard ( ) ;
143- }
143+ private recoverAdmission ( ) : void {
144+ const [ key , ticket ] = this . admissions . entries ( ) . next ( ) . value ! ;
145+ const seen = this . inspectAdmission ( key , ticket ) ;
146+ this . admissions . delete ( key ) ;
147+ if ( seen === 'unknown' ) this . admissions . set ( key , ticket ) ;
148+ else {
149+ this . store . accountingReadbacks . delete ( ticket . issue . runId ) ;
150+ // Publish only accounting, never rerun submission or enqueue from a readback timer.
151+ if ( seen === 'committed' ) ticket . publication ( ) ;
152+ else ticket . publication . discard ( ) ;
144153 }
145154 }
146155 begin ( ...args : Parameters < KeyAccounting [ 'stage' ] > ) : void {
@@ -188,11 +197,11 @@ export class KeyAccounting {
188197 . get ( run , owner , keyId ) ;
189198 } catch {
190199 // The durable admission journal can be retried once storage is readable.
191- this . requestSweep ( ) ;
200+ this . recovery . request ( ) ;
192201 return ;
193202 }
194203 if ( ! pending ) throw error ;
195- this . requestSweep ( ) ;
204+ this . recovery . request ( ) ;
196205 }
197206 }
198207 private requireSettled ( keyId : string , owner ?: string ) {
@@ -321,60 +330,28 @@ export class KeyAccounting {
321330 const issue = new UnconfirmedRunCommit ( run ) ;
322331 this . held . set ( keyId , { ...ticket , issue } ) ;
323332 this . store . accountingReadbacks . add ( run ) ;
324- this . schedule ( 5000 ) ;
333+ this . recovery . request ( ) ;
325334 throw issue ;
326335 }
327336 publish ! ( ) ;
328337 }
329338 recover ( ) : void {
330- let pendingResets = false ;
331- try {
332- pendingResets = this . resets . recover ( ) ;
333- this . recoverAdmissions ( ) ;
334- for ( let n = Math . min ( this . held . size , RECOVERY_PAGE ) ; n > 0 ; n -- ) {
335- const [ key , ticket ] = this . held . entries ( ) . next ( ) . value ! ;
336- const seen = this . inspect ( ticket ) ;
337- this . held . delete ( key ) ;
338- if ( seen === 'absent' ) this . usage . discardStage ( key , ticket . run ) ;
339- if ( seen === 'unknown' ) this . held . set ( key , ticket ) ;
340- else if ( seen === 'absent' && ticket . prior . pendingKey === null ) {
341- try {
342- this . settle ( key , ticket . owner , ticket . run , ticket . total , ticket . status ) ;
343- } catch {
344- if ( ! this . held . has ( key ) ) this . held . set ( key , ticket ) ;
345- }
346- }
347- if ( ! this . held . has ( key ) ) this . store . accountingReadbacks . delete ( ticket . run ) ;
348- }
349- // Only admissions in the journal are recoverable. Never estimate old, possibly reset usage.
350- const rows = keySettlements ( this . store , this . recoveryAfter , RECOVERY_PAGE ) ;
351- for ( const r of rows ) {
352- try {
353- this . settle (
354- String ( r . key_id ) ,
355- String ( r . owner ) ,
356- String ( r . run_id ) ,
357- Number ( r . total_tokens ) ,
358- r . status as RunStatus ,
359- ) ;
360- } catch {
361- this . retrySweep = true ;
362- }
363- }
364- // Visit later rows before retrying a page that could not settle.
365- if ( rows . length === RECOVERY_PAGE ) {
366- this . recoveryAfter = String ( rows . at ( - 1 ) ! . recovery_ordinal ) ;
367- this . schedule ( 0 ) ;
368- return ;
339+ this . recovery . recover ( ) ;
340+ }
341+ private recoverHeld ( ) : void {
342+ const [ key , ticket ] = this . held . entries ( ) . next ( ) . value ! ;
343+ const seen = this . inspect ( ticket ) ;
344+ this . held . delete ( key ) ;
345+ if ( seen === 'absent' ) this . usage . discardStage ( key , ticket . run ) ;
346+ if ( seen === 'unknown' ) this . held . set ( key , ticket ) ;
347+ else if ( seen === 'absent' && ticket . prior . pendingKey === null ) {
348+ try {
349+ this . settle ( key , ticket . owner , ticket . run , ticket . total , ticket . status ) ;
350+ } catch {
351+ if ( ! this . held . has ( key ) ) this . held . set ( key , ticket ) ;
369352 }
370- } catch {
371- this . retrySweep = true ;
372353 }
373- this . recoveryAfter = '0' ;
374- if ( this . retrySweep || this . held . size || this . admissions . size || pendingResets )
375- this . schedule ( 5000 ) ;
376- else this . warned = false ;
377- this . retrySweep = false ;
354+ if ( ! this . held . has ( key ) ) this . store . accountingReadbacks . delete ( ticket . run ) ;
378355 }
379356 assertResettable ( owner : string , keyId : string ) : void {
380357 this . requireSettled ( keyId , owner ) ;
@@ -389,29 +366,9 @@ export class KeyAccounting {
389366 stopResets ( ) : void {
390367 this . resets . stop ( ) ;
391368 }
392- private requestSweep ( ) {
393- // A newly terminal run may be behind a page cursor while another sweep is already queued.
394- if ( this . recoveryAfter !== '0' ) this . retrySweep = true ;
395- this . schedule ( 5000 ) ;
396- }
397- private schedule ( delay : number ) {
398- if ( this . timer ) return ;
399- if ( delay && ! this . warned ) {
400- this . warned = true ;
401- process . emitWarning (
402- 'Run accounting could not be confirmed; pending writes will be checked again.' ,
403- 'OAASettlement' ,
404- ) ;
405- }
406- this . timer = setTimeout ( ( ) => {
407- this . timer = undefined ;
408- this . recover ( ) ;
409- } , delay ) . unref ( ) ;
410- }
411369 close ( ) {
412370 this . stopResets ( ) ;
413- clearTimeout ( this . timer ) ;
414- this . timer = undefined ;
371+ this . recovery . close ( ) ;
415372 for ( const ticket of this . held . values ( ) ) this . store . accountingReadbacks . delete ( ticket . run ) ;
416373 this . held . clear ( ) ;
417374 for ( const ticket of this . admissions . values ( ) )
0 commit comments