@@ -2497,32 +2497,106 @@ describe("runCliAgent reliability", () => {
24972497 expect ( result . meta . agentMeta ?. contextTokens ) . toBe ( 150_000 ) ;
24982498 } ) ;
24992499
2500- it ( "marks CLI runs as paused after sessions_yield" , async ( ) => {
2500+ it ( "returns accepted CLI session spawns when sessions_yield pauses the requester" , async ( ) => {
2501+ const requesterTurnRunId = "run-cli-yield" ;
2502+ const childRunId = "run-cli-child" ;
2503+ const childSessionKey = "agent:main:subagent:cli-child" ;
25012504 supervisorSpawnMock . mockImplementationOnce ( async ( ...args : unknown [ ] ) => {
25022505 const input = args [ 0 ] as Parameters < ReturnType < typeof getProcessSupervisor > [ "spawn" ] > [ 0 ] ;
2506+ const spawnCapture = markMcpLoopbackToolCallStarted ( {
2507+ captureKey : input . env ?. OPENCLAW_MCP_CLI_CAPTURE_KEY ,
2508+ toolName : "sessions_spawn" ,
2509+ args : { task : "review" } ,
2510+ } ) ;
2511+ if ( ! spawnCapture ) {
2512+ throw new Error ( "missing sessions_spawn capture" ) ;
2513+ }
2514+ recordMcpLoopbackToolCallResult ( {
2515+ captureHandle : spawnCapture ,
2516+ toolName : "sessions_spawn" ,
2517+ args : { task : "review" } ,
2518+ outcome : "completed" ,
2519+ result : {
2520+ details : {
2521+ status : "accepted" ,
2522+ runId : childRunId ,
2523+ childSessionKey,
2524+ expectsCompletionMessage : true ,
2525+ } ,
2526+ } ,
2527+ } ) ;
2528+ markMcpLoopbackToolCallFinished ( spawnCapture ) ;
25032529 const captureHandle = markMcpLoopbackRequestStarted ( input . env ?. OPENCLAW_MCP_CLI_CAPTURE_KEY ) ;
25042530 await resolveMcpLoopbackYieldContext ( captureHandle ) ?. onYield ( "waiting on subagents" ) ;
25052531 markMcpLoopbackRequestFinished ( captureHandle ) ;
25062532 input . onStdout ?.( "yield acknowledged" ) ;
25072533 return makeManagedRun ( ) ;
25082534 } ) ;
2509- const context = buildPreparedContext ( ) ;
2535+ const context = buildPreparedContext ( {
2536+ sessionKey : "agent:main:main" ,
2537+ runId : requesterTurnRunId ,
2538+ } ) ;
25102539 context . mcpDeliveryCapture = true ;
25112540
25122541 const result = await runPreparedCliAgent ( context ) ;
25132542
2514- expect ( result . meta ) . toMatchObject ( {
2515- yielded : true ,
2516- livenessState : "paused" ,
2517- stopReason : "end_turn" ,
2518- completion : {
2519- finishReason : "end_turn" ,
2543+ expect ( result ) . toMatchObject ( {
2544+ acceptedSessionSpawns : [
2545+ { runId : childRunId , childSessionKey, expectsCompletionMessage : true } ,
2546+ ] ,
2547+ meta : {
2548+ yielded : true ,
2549+ livenessState : "paused" ,
25202550 stopReason : "end_turn" ,
2521- refusal : false ,
2551+ completion : {
2552+ finishReason : "end_turn" ,
2553+ stopReason : "end_turn" ,
2554+ refusal : false ,
2555+ } ,
25222556 } ,
25232557 } ) ;
25242558 } ) ;
25252559
2560+ it . each ( [
2561+ [ "rejected" , { details : { status : "rejected" , error : "not accepted" } } ] ,
2562+ [ "malformed accepted" , { details : { status : "accepted" , runId : "run-without-session" } } ] ,
2563+ ] as const ) ( "does not retain a %s CLI session spawn" , async ( _label , spawnResult ) => {
2564+ supervisorSpawnMock . mockImplementationOnce ( async ( ...args : unknown [ ] ) => {
2565+ const input = args [ 0 ] as Parameters < ReturnType < typeof getProcessSupervisor > [ "spawn" ] > [ 0 ] ;
2566+ const spawnCapture = markMcpLoopbackToolCallStarted ( {
2567+ captureKey : input . env ?. OPENCLAW_MCP_CLI_CAPTURE_KEY ,
2568+ toolName : "sessions_spawn" ,
2569+ args : { task : "review" } ,
2570+ } ) ;
2571+ if ( ! spawnCapture ) {
2572+ throw new Error ( "missing sessions_spawn capture" ) ;
2573+ }
2574+ recordMcpLoopbackToolCallResult ( {
2575+ captureHandle : spawnCapture ,
2576+ toolName : "sessions_spawn" ,
2577+ args : { task : "review" } ,
2578+ outcome : "completed" ,
2579+ result : spawnResult ,
2580+ } ) ;
2581+ markMcpLoopbackToolCallFinished ( spawnCapture ) ;
2582+ const yieldCapture = markMcpLoopbackRequestStarted ( input . env ?. OPENCLAW_MCP_CLI_CAPTURE_KEY ) ;
2583+ await resolveMcpLoopbackYieldContext ( yieldCapture ) ?. onYield ( "waiting on subagents" ) ;
2584+ markMcpLoopbackRequestFinished ( yieldCapture ) ;
2585+ input . onStdout ?.( "yield acknowledged" ) ;
2586+ return makeManagedRun ( ) ;
2587+ } ) ;
2588+ const context = buildPreparedContext ( {
2589+ sessionKey : "agent:main:main" ,
2590+ runId : "run-cli-unproven-spawn" ,
2591+ } ) ;
2592+ context . mcpDeliveryCapture = true ;
2593+
2594+ const result = await runPreparedCliAgent ( context ) ;
2595+
2596+ expect ( result . acceptedSessionSpawns ) . toBeUndefined ( ) ;
2597+ expect ( result . meta ) . toMatchObject ( { yielded : true , livenessState : "paused" } ) ;
2598+ } ) ;
2599+
25262600 it ( "seeds fresh CLI sessions from the OpenClaw transcript" , async ( ) => {
25272601 supervisorSpawnMock . mockResolvedValueOnce ( makeManagedRun ( { stdout : "hello from cli" } ) ) ;
25282602
0 commit comments