@@ -7,6 +7,7 @@ import crypto from "node:crypto";
77import fs from "node:fs" ;
88import os from "node:os" ;
99import path from "node:path" ;
10+ import { setImmediate } from "node:timers/promises" ;
1011import { afterEach , beforeAll , beforeEach , describe , expect , it , vi , type Mock } from "vitest" ;
1112import { onAgentEvent } from "../infra/agent-events.js" ;
1213import {
@@ -395,6 +396,19 @@ function requireApprovalFollowupInput(
395396 return call [ 0 ] ;
396397}
397398
399+ function captureProcessUnhandledRejections ( ) {
400+ const reasons : unknown [ ] = [ ] ;
401+ const originalProcessEmit = process . emit . bind ( process ) ;
402+ const processEmit = vi . spyOn ( process , "emit" ) . mockImplementation ( ( event , ...args ) => {
403+ if ( event === "unhandledRejection" ) {
404+ reasons . push ( args [ 0 ] ) ;
405+ return true ;
406+ }
407+ return originalProcessEmit ( event , ...args ) ;
408+ } ) ;
409+ return { reasons, restore : ( ) => processEmit . mockRestore ( ) } ;
410+ }
411+
398412function captureSecurityEvents ( ) : {
399413 events : DiagnosticSecurityEvent [ ] ;
400414 stop : ( ) => void ;
@@ -522,11 +536,15 @@ describe("processGatewayAllowlist", () => {
522536 outcome : ExecApprovalFollowupOutcome ;
523537 sessionId ?: string ;
524538 } ) {
525- resolveApprovalDecisionOrUndefinedMock . mockResolvedValue ( "allow-once" ) ;
526- createExecApprovalDecisionStateMock . mockReturnValue ( {
527- baseDecision : { timedOut : false } ,
528- approvedByAsk : true ,
529- deniedReason : null ,
539+ resolveExecApprovalWaitOutcomeMock . mockResolvedValueOnce ( {
540+ kind : "resolved" ,
541+ decision : "allow-once" ,
542+ state : {
543+ baseDecision : { timedOut : false } ,
544+ approvedByAsk : true ,
545+ deniedReason : null ,
546+ timeoutContext : undefined ,
547+ } ,
530548 } ) ;
531549 runExecProcessMock . mockResolvedValue ( {
532550 session : { id : params . sessionId ?? "sess-1" } ,
@@ -2201,6 +2219,94 @@ EOF`,
22012219 expect ( requireSentFollowupText ( 0 ) ) . toContain ( "done" ) ;
22022220 } ) ;
22032221
2222+ it ( "keeps a completed detached outcome terminal when agent follow-up registration fails" , async ( ) => {
2223+ const unhandledRejections = captureProcessUnhandledRejections ( ) ;
2224+ const completedOutcome = {
2225+ status : "completed" as const ,
2226+ exitCode : 0 ,
2227+ timedOut : false ,
2228+ aggregated : "completed output" ,
2229+ } ;
2230+ const approvalFollowup = vi . fn < ExecApprovalFollowupFactory > ( ( ) => undefined ) ;
2231+ mockApprovedDetachedExec ( { outcome : completedOutcome } ) ;
2232+ sendExecApprovalFollowupResultMock . mockRejectedValueOnce (
2233+ new Error ( "synchronous runtime-handoff registration failure" ) ,
2234+ ) ;
2235+
2236+ try {
2237+ const result = await runGatewayAllowlist ( {
2238+ command : "side-effecting-command" ,
2239+ approvalFollowupMode : "agent" ,
2240+ approvalFollowup,
2241+ turnSourceChannel : "webchat" ,
2242+ } ) ;
2243+
2244+ expect ( result . pendingResult ?. details . status ) . toBe ( "approval-pending" ) ;
2245+ await vi . waitFor ( ( ) => expect ( approvalFollowup ) . toHaveBeenCalledOnce ( ) ) ;
2246+ await setImmediate ( ) ;
2247+
2248+ expect ( requireApprovalFollowupInput ( approvalFollowup , 0 ) . outcome ) . toEqual ( completedOutcome ) ;
2249+ expect ( unhandledRejections . reasons ) . toEqual ( [ ] ) ;
2250+ expect ( sendExecApprovalFollowupResultMock ) . toHaveBeenCalledOnce ( ) ;
2251+ expect ( requireSentFollowupText ( 0 ) ) . toContain ( "completed output" ) ;
2252+ expect ( requireSentFollowupText ( 0 ) ) . not . toContain ( "Exec denied" ) ;
2253+ } finally {
2254+ unhandledRejections . restore ( ) ;
2255+ }
2256+ } ) ;
2257+
2258+ it . each ( [
2259+ {
2260+ name : "request failure" ,
2261+ outcome : { kind : "request-failed" as const } ,
2262+ firstFollowupReason : "approval-request-failed" ,
2263+ } ,
2264+ {
2265+ name : "denial" ,
2266+ outcome : {
2267+ kind : "resolved" as const ,
2268+ decision : "deny" ,
2269+ state : {
2270+ baseDecision : { timedOut : false } ,
2271+ approvedByAsk : false ,
2272+ deniedReason : "user-denied" ,
2273+ timeoutContext : undefined ,
2274+ } ,
2275+ } ,
2276+ firstFollowupReason : "user-denied" ,
2277+ } ,
2278+ ] ) ( "consumes rejected detached pre-dispatch $name and fallback follow-ups" , async ( scenario ) => {
2279+ const unhandledRejections = captureProcessUnhandledRejections ( ) ;
2280+ resolveExecApprovalWaitOutcomeMock . mockResolvedValueOnce ( scenario . outcome ) ;
2281+ buildExecApprovalFollowupTargetMock . mockImplementation ( ( value ) => value ) ;
2282+ sendExecApprovalFollowupResultMock . mockRejectedValue (
2283+ new Error ( "pre-dispatch denial follow-up failed" ) ,
2284+ ) ;
2285+
2286+ try {
2287+ const result = await runGatewayAllowlist ( {
2288+ command : "side-effecting-command" ,
2289+ approvalFollowupMode : "agent" ,
2290+ turnSourceChannel : "webchat" ,
2291+ } ) ;
2292+
2293+ expect ( result . pendingResult ?. details . status ) . toBe ( "approval-pending" ) ;
2294+ await vi . waitFor ( ( ) => expect ( sendExecApprovalFollowupResultMock ) . toHaveBeenCalledTimes ( 2 ) ) ;
2295+ await setImmediate ( ) ;
2296+
2297+ expect ( unhandledRejections . reasons ) . toEqual ( [ ] ) ;
2298+ expect ( requireSentFollowupText ( 0 ) ) . toBe (
2299+ `Exec denied (gateway id=req-1, ${ scenario . firstFollowupReason } ): side-effecting-command` ,
2300+ ) ;
2301+ expect ( requireSentFollowupText ( 1 ) ) . toBe (
2302+ "Exec denied (gateway id=req-1, approval-request-failed): side-effecting-command" ,
2303+ ) ;
2304+ expect ( runExecProcessMock ) . not . toHaveBeenCalled ( ) ;
2305+ } finally {
2306+ unhandledRejections . restore ( ) ;
2307+ }
2308+ } ) ;
2309+
22042310 it ( "keeps multiline gateway approval follow-up output intact" , async ( ) => {
22052311 resolveExecHostApprovalContextMock . mockReturnValue ( {
22062312 approvals : { allowlist : [ ] , file : { version : 1 , agents : { } } } ,
0 commit comments