@@ -121,6 +121,54 @@ describe("readSubagentOutput", () => {
121121 expect ( deps . callGateway ) . toHaveBeenCalledOnce ( ) ;
122122 } ) ;
123123
124+ it . each ( [
125+ {
126+ shape : "OpenAI top-level snake_case function call" ,
127+ assistant : {
128+ role : "assistant" ,
129+ content : "Waiting for child completion." ,
130+ tool_calls : [ { type : "function" , function : { name : "sessions_yield" } } ] ,
131+ } ,
132+ } ,
133+ {
134+ shape : "top-level camelCase tool call" ,
135+ assistant : {
136+ role : "assistant" ,
137+ content : "Waiting for child completion." ,
138+ toolCalls : [ { name : "sessions_yield" } ] ,
139+ } ,
140+ } ,
141+ {
142+ shape : "nested content function call" ,
143+ assistant : {
144+ role : "assistant" ,
145+ content : [
146+ { type : "text" , text : "Waiting for child completion." } ,
147+ { type : "function_call" , function : { name : "sessions_yield" } } ,
148+ ] ,
149+ } ,
150+ } ,
151+ ] ) ( "does not expose a $shape yield turn as completion output" , async ( { assistant } ) => {
152+ installOutputDeps ( {
153+ messages : [ assistant , { role : "tool" , content : '{"status":"yielded"}' } ] ,
154+ } ) ;
155+
156+ await expect ( readSubagentOutput ( "agent:main:subagent:child" ) ) . resolves . toBeUndefined ( ) ;
157+ } ) ;
158+
159+ it . each ( [ "toolUse" , "functionCall" , "function_call" ] ) (
160+ "reports visible tool activity for provider-specific %s transcript blocks" ,
161+ async ( type ) => {
162+ installOutputDeps ( {
163+ messages : [ { role : "assistant" , content : [ { type, name : "read" } ] } ] ,
164+ } ) ;
165+
166+ await expect ( readSubagentOutput ( "agent:main:subagent:child" ) ) . resolves . toBe (
167+ "1 tool call(s) made without visible output." ,
168+ ) ;
169+ } ,
170+ ) ;
171+
124172 it ( "returns final assistant output that arrives after a sessions_yield wait turn" , async ( ) => {
125173 installOutputDeps ( {
126174 messages : [
@@ -321,6 +369,106 @@ describe("readSubagentOutput", () => {
321369} ) ;
322370
323371describe ( "buildChildCompletionFindings" , ( ) => {
372+ it ( "hard-bounds each child result and the aggregate parent prompt" , ( ) => {
373+ const findings = buildChildCompletionFindings (
374+ Array . from ( { length : 8 } , ( _ , index ) => ( {
375+ childSessionKey : `agent:main:subagent:${ index } ` ,
376+ task : `worker ${ index } ` ,
377+ createdAt : index ,
378+ completion : { resultText : "🚀" . repeat ( 60_000 ) } ,
379+ outcome : { status : "ok" as const } ,
380+ } ) ) ,
381+ ) ;
382+
383+ expect ( findings ) . toBeDefined ( ) ;
384+ expect ( findings ! . length ) . toBeLessThanOrEqual ( 4_096 ) ;
385+ expect ( findings ) . toContain ( "status: ok" ) ;
386+ expect ( findings ) . toContain ( "[child result truncated]" ) ;
387+ expect ( findings ) . toContain ( "additional child completion result" ) ;
388+ expect ( findings ) . toContain ( "</prompt-data>" ) ;
389+ for ( const character of findings ?? "" ) {
390+ const code = character . charCodeAt ( 0 ) ;
391+ expect ( character . length > 1 || code < 0xd800 || code > 0xdfff ) . toBe ( true ) ;
392+ }
393+ } ) ;
394+
395+ it ( "retains a later actionable failure when an earlier child exceeds the remaining budget" , ( ) => {
396+ const findings = buildChildCompletionFindings ( [
397+ {
398+ childSessionKey : "agent:main:subagent:first" ,
399+ task : "first large result" ,
400+ createdAt : 1 ,
401+ completion : { resultText : "<" . repeat ( 100_000 ) } ,
402+ outcome : { status : "ok" } ,
403+ } ,
404+ {
405+ childSessionKey : "agent:main:subagent:second" ,
406+ task : "second large result" ,
407+ createdAt : 2 ,
408+ completion : { resultText : "<" . repeat ( 100_000 ) } ,
409+ outcome : { status : "ok" } ,
410+ } ,
411+ {
412+ childSessionKey : "agent:main:subagent:failure" ,
413+ task : "later actionable failure" ,
414+ createdAt : 3 ,
415+ completion : { resultText : "Permission required." } ,
416+ outcome : { status : "error" , error : "Writable session authorization required." } ,
417+ } ,
418+ ] ) ;
419+
420+ expect ( findings ! . length ) . toBeLessThanOrEqual ( 4_096 ) ;
421+ expect ( findings ) . toContain ( "first large result" ) ;
422+ expect ( findings ) . toContain ( "later actionable failure" ) ;
423+ expect ( findings ) . toContain ( "status: error: Writable session authorization required." ) ;
424+ expect ( findings ) . toContain ( "[1 additional child completion result omitted" ) ;
425+ } ) ;
426+
427+ it ( "prioritizes an oversized failed completion over an earlier oversized success" , ( ) => {
428+ const findings = buildChildCompletionFindings ( [
429+ {
430+ childSessionKey : "agent:main:subagent:success" ,
431+ task : "earlier oversized success" ,
432+ createdAt : 1 ,
433+ completion : { resultText : "<" . repeat ( 100_000 ) } ,
434+ outcome : { status : "ok" } ,
435+ } ,
436+ {
437+ childSessionKey : "agent:main:subagent:failure" ,
438+ task : "later oversized failure" ,
439+ createdAt : 2 ,
440+ completion : { resultText : "<" . repeat ( 100_000 ) } ,
441+ outcome : { status : "error" , error : "Writable session authorization required." } ,
442+ } ,
443+ ] ) ;
444+
445+ expect ( findings ! . length ) . toBeLessThanOrEqual ( 4_096 ) ;
446+ expect ( findings ) . toContain ( "later oversized failure" ) ;
447+ expect ( findings ) . toContain ( "status: error: Writable session authorization required." ) ;
448+ expect ( findings ) . not . toContain ( "earlier oversized success" ) ;
449+ expect ( findings ) . toContain ( "[1 additional child completion result omitted" ) ;
450+ } ) ;
451+
452+ it ( "keeps escaped child data and oversized failure metadata inside the same hard cap" , ( ) => {
453+ const findings = buildChildCompletionFindings ( [
454+ {
455+ childSessionKey : "agent:main:subagent:child" ,
456+ label : "L" . repeat ( 20_000 ) ,
457+ task : "child task" ,
458+ createdAt : 1 ,
459+ completion : { resultText : "<" . repeat ( 100_000 ) } ,
460+ outcome : { status : "error" , error : "E" . repeat ( 20_000 ) } ,
461+ } ,
462+ ] ) ;
463+
464+ expect ( findings ) . toBeDefined ( ) ;
465+ expect ( findings ! . length ) . toBeLessThanOrEqual ( 4_096 ) ;
466+ expect ( findings ) . toContain ( "status: error:" ) ;
467+ expect ( findings ) . toContain ( "<" ) ;
468+ expect ( findings ) . toContain ( "[child result truncated]" ) ;
469+ expect ( findings ) . toContain ( "</prompt-data>" ) ;
470+ } ) ;
471+
324472 it ( "does not convert ANNOUNCE_SKIP child completions into no-output findings" , ( ) => {
325473 const findings = buildChildCompletionFindings ( [
326474 {
0 commit comments