@@ -5494,6 +5494,109 @@ describe("createReactor — approval resume error result", () => {
54945494 await waitForEvent ( events , ( e ) => e . type === "connector.reply" ) ;
54955495 expect ( parkedCallResultTexts ( cell . turns ) ) . toHaveLength ( 1 ) ;
54965496 } ) ;
5497+
5498+ // The loop must be busy when the decision is accepted, or it acts on it
5499+ // on the next turn of the loop before an abort can be queued. A second
5500+ // message's held inference keeps it busy; the abort queued behind that
5501+ // inference is dequeued ahead of the accepted decision.
5502+ async function decideThenAbortBehindHeldInference (
5503+ contextStore : ContextStore ,
5504+ beforeAbort : ( ) => void = ( ) => { } ,
5505+ decision : ( correlationId : string ) => InboundMessage = rejection ,
5506+ ) {
5507+ const held = heldContinuationRunner ( ) ;
5508+ const handle = createTestReactor ( {
5509+ contextStore,
5510+ director : createDefaultDirector ( "test agent" , [ ] ) ,
5511+ inferenceRunner : held . runner ,
5512+ beforeToolExtensions : [ askExtension ( ) ] ,
5513+ } ) ;
5514+ const { reactor, events, waitFor } = handle ;
5515+
5516+ reactor . start ( ) ;
5517+ reactor . deliver ( makeInboundMessage ( ) ) ;
5518+ const blocked = await waitFor ( "reactor.gate.blocked" ) ;
5519+ if ( blocked . type !== "reactor.gate.blocked" ) throw new Error ( "unreachable" ) ;
5520+ const correlationId = blocked . data . correlationId ;
5521+ if ( correlationId === undefined ) throw new Error ( "expected correlationId" ) ;
5522+
5523+ reactor . deliver ( makeInboundMessage ( ) ) ;
5524+ await held . continuationStarted ;
5525+ reactor . deliver ( decision ( correlationId ) ) ;
5526+ await waitForEvent ( events , ( e ) => e . type === "message.correlated" ) ;
5527+
5528+ beforeAbort ( ) ;
5529+ reactor . abort ( "user_disconnect" ) ;
5530+ held . release ( ) ;
5531+ await waitFor ( "reactor.done" ) ;
5532+ return handle ;
5533+ }
5534+
5535+ test ( "an accepted rejection still queued at shutdown reaches the store" , async ( ) => {
5536+ const cell : PersistedCell = {
5537+ turns : [ ] ,
5538+ pendingOperations : [ ] ,
5539+ tokenUsage : emptyUsage ( ) ,
5540+ } ;
5541+
5542+ await decideThenAbortBehindHeldInference ( makePersistingContextStore ( cell ) ) ;
5543+
5544+ expect ( parkedCallResultTexts ( cell . turns ) ) . toEqual ( [
5545+ "denied by approver: too risky" ,
5546+ ] ) ;
5547+ } ) ;
5548+
5549+ test ( "an accepted approval still queued at shutdown stays parked with no result" , async ( ) => {
5550+ const cell : PersistedCell = {
5551+ turns : [ ] ,
5552+ pendingOperations : [ ] ,
5553+ tokenUsage : emptyUsage ( ) ,
5554+ } ;
5555+
5556+ await decideThenAbortBehindHeldInference (
5557+ makePersistingContextStore ( cell ) ,
5558+ undefined ,
5559+ ( correlationId ) => makeApprovalMessage ( correlationId ) ,
5560+ ) ;
5561+
5562+ // The host handed the approval over and the reactor accepted it, but the
5563+ // call never ran: only a rebuilt agent's host can settle it now.
5564+ expect ( parkedCallResultTexts ( cell . turns ) ) . toEqual ( [ ] ) ;
5565+ expect ( cell . pendingOperations . map ( ( op ) => op . suspendedCall ?. id ) ) . toEqual ( [
5566+ "call-ask" ,
5567+ ] ) ;
5568+ } ) ;
5569+
5570+ test ( "a store that fails the shutdown write is reported without blocking shutdown" , async ( ) => {
5571+ const cell : PersistedCell = {
5572+ turns : [ ] ,
5573+ pendingOperations : [ ] ,
5574+ tokenUsage : emptyUsage ( ) ,
5575+ } ;
5576+ const persisting = makePersistingContextStore ( cell ) ;
5577+ let failWrites = false ;
5578+ const contextStore : ContextStore = {
5579+ ...persisting ,
5580+ async writeTurns ( turns ) {
5581+ if ( failWrites ) throw new Error ( "disk full" ) ;
5582+ await persisting . writeTurns ( turns ) ;
5583+ } ,
5584+ } ;
5585+
5586+ const { events } = await decideThenAbortBehindHeldInference (
5587+ contextStore ,
5588+ ( ) => {
5589+ failWrites = true ;
5590+ } ,
5591+ ) ;
5592+
5593+ const errors = events . flatMap ( ( e ) =>
5594+ e . type === "reactor.error" ? [ e . data ] : [ ] ,
5595+ ) ;
5596+ expect ( errors ) . toHaveLength ( 1 ) ;
5597+ expect ( errors [ 0 ] ?. fatal ) . toBe ( false ) ;
5598+ expect ( errors [ 0 ] ?. error ) . toContain ( "disk full" ) ;
5599+ } ) ;
54975600} ) ;
54985601
54995602// ---------------------------------------------------------------------------
0 commit comments