@@ -7,9 +7,11 @@ import { Fault } from '../core/util.js';
77import type { Store } from '../store/store.js' ;
88import { WebhookCaptureUnavailable , isWriteUnavailable } from '../store/checkpoint.js' ;
99import { PendingTerminalCommit } from '../store/terminal-publication.js' ;
10+ import { parkedDecisions } from '../store/parked-decisions.js' ;
11+ import { ParkedWakes } from './parked-wakes.js' ;
1012import type { NodeHub } from '../fabric/network.js' ;
1113import { responseObject } from './response-object.js' ;
12- import * as clientTools from './client-tools.js' ;
14+ import { parkedFailure } from './client-tools.js' ;
1315import { estimateStart , type Sample } from './queue-estimate.js' ;
1416import { terminalEventType } from './run-state.js' ;
1517import type { KernelLimits , QueueInsight } from './types.js' ;
@@ -52,7 +54,31 @@ export class Admission {
5254 /** Stored-duration fallback per owner+agent, refreshed at most every 5 s. */
5355 private storedDurations = new Map < string , { at : number ; sample : number [ ] } > ( ) ;
5456 private runKeys = new Map < string , string > ( ) ;
55- constructor ( private readonly rt : AdmissionRuntime ) { }
57+ readonly wakes : ParkedWakes ;
58+ constructor ( private readonly rt : AdmissionRuntime ) {
59+ this . wakes = new ParkedWakes ( rt . store , {
60+ closing : ( ) => rt . closing ( ) ,
61+ parked : ( id ) => this . parked . has ( id ) ,
62+ queued : ( id ) => {
63+ this . enqueue ( id ) ;
64+ setImmediate ( ( ) => {
65+ if ( ! rt . closing ( ) && this . parked . has ( id ) ) {
66+ this . scheduleParked ( id ) ;
67+ this . pump ( ) ;
68+ }
69+ } ) ;
70+ } ,
71+ retry : ( id , immediate ) => this . retryParked ( id , immediate ? 0 : 1000 ) ,
72+ cancelLocked : ( run ) =>
73+ this . finishParked (
74+ run ,
75+ 'cancelled' ,
76+ 'cancelled' ,
77+ 'Execution was cancelled while awaiting approval.' ,
78+ 'owned' ,
79+ ) ,
80+ } ) ;
81+ }
5682 /** 运行槽计数的不变量检查:违反时记数并发出进程警告(测试通过 stats().breaches 断言)。 */
5783 private checkSlots ( ) : void {
5884 if ( this . active >= 0 && this . active <= this . rt . limits . maxActiveRuns ) return ;
@@ -262,6 +288,7 @@ export class Admission {
262288 status : 'failed' | 'cancelled' | 'interrupted' | 'incomplete' ,
263289 code ?: string ,
264290 message ?: string ,
291+ writer ?: 'owned' | 'try' ,
265292 ) : void {
266293 run . status = status ;
267294 if ( code && message ) run . error = { code, message } ;
@@ -279,17 +306,18 @@ export class Admission {
279306 this . queue = this . queue . filter ( ( id ) => id !== run . id ) ;
280307 } ,
281308 ( ) => this . rt . releaseRun ( run ) ,
309+ writer ,
282310 ) ;
283311 }
284312 scheduleParked ( runId : string ) : boolean {
285313 if ( this . rt . closing ( ) || this . tasks . has ( runId ) || ! this . parked . has ( runId ) ) return true ;
286- if ( this . retrying . get ( runId ) ) return false ;
314+ if ( this . retrying . get ( runId ) || this . wakes . has ( runId ) ) return false ;
287315 try {
288316 const pending = this . rt . store . terminalPublications . issue ( runId ) ;
289317 if ( pending ) throw pending ;
290318 this . checkParked ( runId ) ;
291- this . retrying . delete ( runId ) ;
292- return true ;
319+ if ( this . retrying . get ( runId ) === false ) this . retrying . delete ( runId ) ;
320+ return ! this . wakes . has ( runId ) && ! this . retrying . get ( runId ) ;
293321 } catch ( error ) {
294322 if ( error instanceof PendingTerminalCommit ) {
295323 this . retrying . set ( runId , true ) ;
@@ -316,72 +344,67 @@ export class Admission {
316344 'A parked decision could not be checkpointed; scheduling will retry.' ,
317345 'OAAParkedCheckpoint' ,
318346 ) ;
319- this . retrying . set ( runId , true ) ;
320- clearTimeout ( this . parked . get ( runId ) ) ;
321- this . parked . set (
322- runId ,
323- setTimeout ( ( ) => {
324- this . retrying . set ( runId , false ) ;
325- this . scheduleParked ( runId ) ;
326- this . pump ( ) ;
327- } , 1000 ) . unref ( ) ,
328- ) ;
347+ this . retryParked ( runId , 1000 ) ;
329348 return false ;
330349 }
331350 }
351+ private retryParked ( runId : string , ms : number ) {
352+ if ( this . rt . closing ( ) || ! this . parked . has ( runId ) ) return ;
353+ clearTimeout ( this . parked . get ( runId ) ) ;
354+ this . retrying . set ( runId , true ) ;
355+ this . parked . set (
356+ runId ,
357+ setTimeout ( ( ) => {
358+ this . retrying . set ( runId , false ) ;
359+ this . scheduleParked ( runId ) ;
360+ this . pump ( ) ;
361+ } , ms ) . unref ( ) ,
362+ ) ;
363+ }
332364 private checkParked ( runId : string ) : void {
333- const run = this . rt . store . run ( runId ) ;
334- const deadline = run . checkpoint . deadlineAt ?? Infinity ;
335- const now = Date . now ( ) ;
336- if ( ! [ 'requires_action' , 'queued' ] . includes ( run . status ) ) {
365+ const run = this . rt . store . parkedRun ( runId ) ;
366+ if ( ! run ) {
367+ this . rt . store . terminalPublications . requireConfirmed ( runId ) ;
337368 this . clearParked ( runId ) ;
338369 return ;
339370 }
371+ const deadline = run . checkpoint . deadlineAt ?? Infinity ;
372+ const now = Date . now ( ) ;
340373 if ( now >= deadline ) {
341374 this . finishParked (
342375 run ,
343376 'incomplete' ,
344377 'run_timeout' ,
345378 'The run deadline expired while waiting.' ,
379+ 'try' ,
346380 ) ;
347381 return ;
348382 }
349383 let wakeAt = deadline ;
350384 if ( run . checkpoint . suspension ) {
351- const actions = run . checkpoint . suspension . approvalIds . map ( ( id ) =>
352- this . rt . store . getApproval ( id , run . owner ) ,
353- ) ;
354- if ( actions . some ( ( a ) => a . status === 'denied' ) ) {
355- this . finishParked (
356- run ,
357- 'failed' ,
358- 'permission_denied' ,
359- 'The API owner denied a pending tool call.' ,
360- ) ;
361- return ;
362- }
363- if (
364- actions . some (
365- ( a ) => a . status === 'expired' || ( a . status === 'pending' && now >= a . expiresAt ) ,
366- )
367- ) {
368- this . finishParked ( run , 'failed' , ...clientTools . parkedExpiry ( actions ) ) ;
385+ const decisions = parkedDecisions ( this . rt . store , run ) ,
386+ actions = decisions . actions ;
387+ const failure = parkedFailure ( actions , now ) ;
388+ if ( failure ) {
389+ this . finishParked ( run , 'failed' , ...failure , 'try' ) ;
369390 return ;
370391 }
371392 if ( run . status === 'requires_action' && actions . some ( ( a ) => a . status === 'approved' ) ) {
372- run . status = 'queued' ;
373- this . rt . store . checkpoint ( run , [
374- { type : 'run.queued' , data : { reason : 'approval_resolved' } } ,
375- ] ) ;
376- if ( ! this . parked . has ( runId ) ) return ;
377- this . enqueue ( run . id ) ;
393+ clearTimeout ( this . parked . get ( runId ) ) ;
394+ this . parked . set ( runId , undefined ) ;
395+ this . wakes . begin ( run , decisions ) ;
396+ return ;
378397 }
379398 wakeAt = Math . min (
380399 deadline ,
381400 ...actions . filter ( ( a ) => a . status === 'pending' ) . map ( ( a ) => a . expiresAt ) ,
382401 ) ;
383402 }
384403 clearTimeout ( this . parked . get ( runId ) ) ;
404+ if ( ! Number . isFinite ( wakeAt ) ) {
405+ this . parked . set ( runId , undefined ) ;
406+ return ;
407+ }
385408 this . parked . set (
386409 runId ,
387410 setTimeout (
0 commit comments