@@ -33,6 +33,13 @@ const MAX_CHILD_COMPLETION_RESULT_CHARS = 512;
3333const MAX_CHILD_COMPLETION_FIELD_CHARS = 256 ;
3434const MAX_CHILD_COMPLETION_FINDINGS_CHARS = 4_096 ;
3535const CHILD_RESULT_TRUNCATION_NOTICE = "\n[child result truncated]" ;
36+ const ASSISTANT_TOOL_CALL_BLOCK_TYPES = new Set ( [
37+ "toolCall" ,
38+ "tool_use" ,
39+ "toolUse" ,
40+ "functionCall" ,
41+ "function_call" ,
42+ ] ) ;
3643type SubagentAnnounceOutputDeps = {
3744 callGateway : typeof callGateway ;
3845 getRuntimeConfig : typeof getRuntimeConfig ;
@@ -60,6 +67,7 @@ function isFastTestMode() {
6067type SubagentOutputSnapshot = {
6168 latestAssistantText ?: string ;
6269 latestSilentText ?: string ;
70+ latestToolCallCount ?: number ;
6371 waitingForContinuation ?: boolean ;
6472} ;
6573
@@ -121,6 +129,25 @@ function extractSubagentAssistantText(message: unknown): string {
121129 return extractAssistantText ( message ) ?? "" ;
122130}
123131
132+ function countAssistantToolCalls ( message : unknown ) : number {
133+ if ( ! message || typeof message !== "object" ) {
134+ return 0 ;
135+ }
136+ const content = ( message as { content ?: unknown } ) . content ;
137+ const contentToolCalls = Array . isArray ( content )
138+ ? content . filter (
139+ ( block ) =>
140+ block &&
141+ typeof block === "object" &&
142+ ASSISTANT_TOOL_CALL_BLOCK_TYPES . has ( ( block as { type ?: string } ) . type ?? "" ) ,
143+ ) . length
144+ : 0 ;
145+ const toolCalls =
146+ ( message as { toolCalls ?: unknown ; tool_calls ?: unknown } ) . toolCalls ??
147+ ( message as { tool_calls ?: unknown } ) . tool_calls ;
148+ return contentToolCalls + ( Array . isArray ( toolCalls ) ? toolCalls . length : 0 ) ;
149+ }
150+
124151function summarizeSubagentOutputHistory ( messages : Array < unknown > ) : SubagentOutputSnapshot {
125152 const snapshot : SubagentOutputSnapshot = { } ;
126153 let previousAssistantCalledYield = false ;
@@ -141,6 +168,7 @@ function summarizeSubagentOutputHistory(messages: Array<unknown>): SubagentOutpu
141168 // when the current run fails or completes without visible output.
142169 snapshot . latestAssistantText = undefined ;
143170 snapshot . latestSilentText = undefined ;
171+ snapshot . latestToolCallCount = undefined ;
144172 snapshot . waitingForContinuation = false ;
145173 previousAssistantCalledYield = false ;
146174 continue ;
@@ -155,6 +183,8 @@ function summarizeSubagentOutputHistory(messages: Array<unknown>): SubagentOutpu
155183 }
156184 const text = extractSubagentAssistantText ( message ) . trim ( ) ;
157185 if ( ! text ) {
186+ snapshot . latestToolCallCount =
187+ ( snapshot . latestToolCallCount ?? 0 ) + countAssistantToolCalls ( message ) ;
158188 snapshot . waitingForContinuation = false ;
159189 previousAssistantCalledYield = false ;
160190 continue ;
@@ -184,7 +214,10 @@ function summarizeSubagentOutputHistory(messages: Array<unknown>): SubagentOutpu
184214 return snapshot ;
185215}
186216
187- function selectSubagentOutputText ( snapshot : SubagentOutputSnapshot ) : string | undefined {
217+ function selectSubagentOutputText (
218+ snapshot : SubagentOutputSnapshot ,
219+ outcome ?: SubagentRunOutcome ,
220+ ) : string | undefined {
188221 if ( snapshot . waitingForContinuation ) {
189222 return undefined ;
190223 }
@@ -194,12 +227,21 @@ function selectSubagentOutputText(snapshot: SubagentOutputSnapshot): string | un
194227 if ( snapshot . latestAssistantText ) {
195228 return snapshot . latestAssistantText ;
196229 }
230+ // Tool activity is partial-progress evidence only for a timed-out run. It is
231+ // not authoritative completion output when producer terminal facts are absent.
232+ if (
233+ outcome ?. status === "timeout" &&
234+ snapshot . latestToolCallCount &&
235+ snapshot . latestToolCallCount > 0
236+ ) {
237+ return `${ snapshot . latestToolCallCount } tool call(s) made without visible output.` ;
238+ }
197239 return undefined ;
198240}
199241
200242export async function readSubagentOutput (
201243 sessionKey : string ,
202- _outcome ?: SubagentRunOutcome ,
244+ outcome ?: SubagentRunOutcome ,
203245 options ?: { sessionTarget ?: SessionTranscriptRuntimeTarget } ,
204246) : Promise < string | undefined > {
205247 let messages : unknown [ ] | undefined ;
@@ -223,7 +265,7 @@ export async function readSubagentOutput(
223265 : undefined ;
224266 const sourceMessages = messages ?? ( Array . isArray ( history ?. messages ) ? history . messages : [ ] ) ;
225267 const snapshot = summarizeSubagentOutputHistory ( sourceMessages ) ;
226- const selected = selectSubagentOutputText ( snapshot ) ;
268+ const selected = selectSubagentOutputText ( snapshot , outcome ) ;
227269 if ( selected ?. trim ( ) ) {
228270 return selected ;
229271 }
@@ -244,6 +286,20 @@ export async function readLatestSubagentOutputWithRetry(params: {
244286 } ) ;
245287}
246288
289+ export async function readSubagentTimeoutProgress (
290+ sessionKey : string ,
291+ maxWaitMs : number ,
292+ outcome : SubagentRunOutcome ,
293+ ) : Promise < string | undefined > {
294+ const initial = await readSubagentOutput ( sessionKey , outcome ) ;
295+ const progress = initial ?. trim ( )
296+ ? initial
297+ : await readLatestSubagentOutputWithRetry ( { sessionKey, maxWaitMs, outcome } ) ;
298+ return progress && ! isAnnounceSkip ( progress ) && ! isSilentReplyText ( progress , SILENT_REPLY_TOKEN )
299+ ? progress
300+ : undefined ;
301+ }
302+
247303export async function waitForSubagentRunOutcome (
248304 runId : string ,
249305 timeoutMs : number ,
0 commit comments