@@ -15,6 +15,7 @@ import {
1515import { adaptAnthropicToolCallIds } from "./mock-anthropic-wire.js" ;
1616import {
1717 buildAssistantText ,
18+ readForkedContextCompletion ,
1819 isCanonicalCompactionRetryWriteResult ,
1920 QA_COMPACTION_RETRY_FINAL_MARKER ,
2021} from "./mock-openai-assistant-text.js" ;
@@ -158,6 +159,8 @@ import {
158159import {
159160 extractLastUserText ,
160161 extractLastMatchingUserTurn ,
162+ extractMockSubagentContext ,
163+ splitMockConversationContext ,
161164 hasToolOutput ,
162165 extractToolOutput ,
163166 extractToolOutputValue ,
@@ -2392,17 +2395,47 @@ async function buildResponsesPayload(
23922395 if ( explicitSessionsSpawnArgs && canCallSessionsSpawn && ! hasCompletedToolOutput ) {
23932396 return buildToolCallEventsWithArgs ( "sessions_spawn" , explicitSessionsSpawnArgs ) ;
23942397 }
2398+ const forkTask = extractMockSubagentContext ( input ) ;
2399+ if ( forkTask && / ^ R e p o r t t h e v i s i b l e c o d e f r o m t h e r e q u e s t e r t r a n s c r i p t \. / i. test ( forkTask . task ) ) {
2400+ return buildAssistantEvents ( buildAssistantText ( input , body ) ) ;
2401+ }
2402+ const forkCompletion = readForkedContextCompletion ( input ) ;
23952403 if (
2396- canCallSessionsSpawn &&
2397- / f o r k e d s u b a g e n t c o n t e x t q a c h e c k / i. test ( prompt ) &&
2398- ! hasCompletedToolOutput
2404+ / f o r k e d s u b a g e n t c o n t e x t q a c h e c k / i. test ( splitMockConversationContext ( prompt ) . current ) ||
2405+ forkCompletion
23992406 ) {
2400- return buildToolCallEventsWithArgs ( "sessions_spawn" , {
2401- task : "Report the visible code from the requester transcript." ,
2402- label : "qa-fork-context" ,
2403- mode : "run" ,
2404- context : "fork" ,
2405- } ) ;
2407+ if ( forkCompletion ) {
2408+ // Completion must be delivered by the parent, not synthesized from its
2409+ // kickoff or spawn receipt. Never replay the inherited spawn instruction.
2410+ if ( completedToolName === "message" ) {
2411+ return buildAssistantEvents ( "NO_REPLY" ) ;
2412+ }
2413+ return hasToolDefinition ( toolDeclarationBody , "message" ) || hasCallableCodeMode
2414+ ? buildToolCallEventsWithArgs ( "message" , {
2415+ action : "send" ,
2416+ message : forkCompletion ,
2417+ final : true ,
2418+ } )
2419+ : buildAssistantEvents ( forkCompletion ) ;
2420+ }
2421+ if ( ! hasCompletedToolOutput && canCallSessionsSpawn ) {
2422+ return buildToolCallEventsWithArgs ( "sessions_spawn" , {
2423+ task : "Report the visible code from the requester transcript." ,
2424+ label : "qa-fork-context" ,
2425+ mode : "run" ,
2426+ context : "fork" ,
2427+ } ) ;
2428+ }
2429+ if (
2430+ hasCompletedToolOutput &&
2431+ canCallSessionsYield &&
2432+ ! hasToolErrorOutput ( toolJson , toolOutput )
2433+ ) {
2434+ return buildToolCallEventsWithArgs ( "sessions_yield" , {
2435+ message : "Waiting for the forked child to recover the visible code." ,
2436+ } ) ;
2437+ }
2438+ return buildAssistantEvents ( buildAssistantText ( input , body ) ) ;
24062439 }
24072440 if ( / t o o l c o n t i n u i t y c h e c k / i. test ( prompt ) && ! hasCompletedToolOutput ) {
24082441 return buildToolCallEventsWithArgs ( "read" , { path : "QA_KICKOFF_TASK.md" } ) ;
0 commit comments