@@ -183,6 +183,85 @@ export function buildSubagentSystemPrompt(params: {
183183 return lines . join ( "\n" ) ;
184184}
185185
186+ export type SubagentRunOutcome = {
187+ status : "ok" | "error" | "timeout" | "unknown" ;
188+ error ?: string ;
189+ } ;
190+
191+ const ANNOUNCE_SECTION_RE = / ^ \s * [ - * ] ? \s * (?: \* \* ) ? ( s t a t u s | r e s u l t | n o t e s ) (?: \* \* ) ? \s * : \s * ( .* ) $ / i;
192+
193+ function parseAnnounceSections ( announce : string ) {
194+ const sections = {
195+ status : [ ] as string [ ] ,
196+ result : [ ] as string [ ] ,
197+ notes : [ ] as string [ ] ,
198+ } ;
199+ let current : keyof typeof sections | null = null ;
200+ let sawSection = false ;
201+
202+ for ( const line of announce . split ( / \r ? \n / ) ) {
203+ const match = line . match ( ANNOUNCE_SECTION_RE ) ;
204+ if ( match ) {
205+ const key = match [ 1 ] ?. toLowerCase ( ) as keyof typeof sections ;
206+ current = key ;
207+ sawSection = true ;
208+ const rest = match [ 2 ] ?. trim ( ) ;
209+ if ( rest ) sections [ key ] . push ( rest ) ;
210+ continue ;
211+ }
212+ if ( current ) sections [ current ] . push ( line ) ;
213+ }
214+
215+ const normalize = ( lines : string [ ] ) => {
216+ const joined = lines . join ( "\n" ) . trim ( ) ;
217+ return joined . length > 0 ? joined : undefined ;
218+ } ;
219+
220+ return {
221+ sawSection,
222+ status : normalize ( sections . status ) ,
223+ result : normalize ( sections . result ) ,
224+ notes : normalize ( sections . notes ) ,
225+ } ;
226+ }
227+
228+ function normalizeAnnounceBody ( params : {
229+ outcome : SubagentRunOutcome ;
230+ announceReply : string ;
231+ statsLine ?: string ;
232+ } ) {
233+ const announce = params . announceReply . trim ( ) ;
234+ const statsLine = params . statsLine ?. trim ( ) ;
235+
236+ const statusLabel =
237+ params . outcome . status === "ok"
238+ ? "success"
239+ : params . outcome . status === "timeout"
240+ ? "timeout"
241+ : params . outcome . status === "unknown"
242+ ? "unknown"
243+ : "error" ;
244+
245+ const parsed = parseAnnounceSections ( announce ) ;
246+ const resultText = parsed . result ?? ( announce || "(not available)" ) ;
247+ const notesParts : string [ ] = [ ] ;
248+ if ( parsed . notes ) notesParts . push ( parsed . notes ) ;
249+ if ( params . outcome . error ) notesParts . push ( `- Error: ${ params . outcome . error } ` ) ;
250+ const notesBlock = notesParts . length ? notesParts . join ( "\n" ) : "- (none)" ;
251+
252+ const message = [
253+ `Status: ${ statusLabel } ` ,
254+ "" ,
255+ "Result:" ,
256+ resultText ,
257+ "" ,
258+ "Notes:" ,
259+ notesBlock ,
260+ ] . join ( "\n" ) ;
261+
262+ return statsLine ? `${ message } \n\n${ statsLine } ` : message ;
263+ }
264+
186265function buildSubagentAnnouncePrompt ( params : {
187266 requesterSessionKey ?: string ;
188267 requesterChannel ?: string ;
@@ -202,6 +281,10 @@ function buildSubagentAnnouncePrompt(params: {
202281 "" ,
203282 "**You MUST announce your result.** The requester is waiting for your response." ,
204283 "Provide a brief, useful summary of what you accomplished." ,
284+ "Reply with Result and Notes only (no Status line; status is added by the system)." ,
285+ "Format:" ,
286+ "Result: <summary>" ,
287+ "Notes: <extra context>" ,
205288 'Only reply "ANNOUNCE_SKIP" if the task completely failed with no useful output.' ,
206289 "Your reply will be posted to the requester chat." ,
207290 ] . filter ( Boolean ) ;
@@ -222,10 +305,12 @@ export async function runSubagentAnnounceFlow(params: {
222305 startedAt ?: number ;
223306 endedAt ?: number ;
224307 label ?: string ;
308+ outcome ?: SubagentRunOutcome ;
225309} ) : Promise < boolean > {
226310 let didAnnounce = false ;
227311 try {
228312 let reply = params . roundOneReply ;
313+ let outcome : SubagentRunOutcome | undefined = params . outcome ;
229314 if ( ! reply && params . waitForCompletion !== false ) {
230315 const waitMs = Math . min ( params . timeoutMs , 60_000 ) ;
231316 const wait = ( await callGateway ( {
@@ -235,8 +320,30 @@ export async function runSubagentAnnounceFlow(params: {
235320 timeoutMs : waitMs ,
236321 } ,
237322 timeoutMs : waitMs + 2000 ,
238- } ) ) as { status ?: string } ;
239- if ( wait ?. status !== "ok" ) return false ;
323+ } ) ) as {
324+ status ?: string ;
325+ error ?: string ;
326+ startedAt ?: number ;
327+ endedAt ?: number ;
328+ } ;
329+ if ( wait ?. status === "timeout" ) {
330+ outcome = { status : "timeout" } ;
331+ } else if ( wait ?. status === "error" ) {
332+ outcome = { status : "error" , error : wait . error } ;
333+ } else if ( wait ?. status === "ok" ) {
334+ outcome = { status : "ok" } ;
335+ }
336+ if ( typeof wait ?. startedAt === "number" && ! params . startedAt ) {
337+ params . startedAt = wait . startedAt ;
338+ }
339+ if ( typeof wait ?. endedAt === "number" && ! params . endedAt ) {
340+ params . endedAt = wait . endedAt ;
341+ }
342+ if ( wait ?. status === "timeout" ) {
343+ // No lifecycle end seen before timeout. Still attempt an announce so
344+ // requesters are not left hanging.
345+ if ( ! outcome ) outcome = { status : "timeout" } ;
346+ }
240347 reply = await readLatestAssistantReply ( {
241348 sessionKey : params . childSessionKey ,
242349 } ) ;
@@ -248,6 +355,8 @@ export async function runSubagentAnnounceFlow(params: {
248355 } ) ;
249356 }
250357
358+ if ( ! outcome ) outcome = { status : "unknown" } ;
359+
251360 const announceTarget = await resolveAnnounceTarget ( {
252361 sessionKey : params . requesterSessionKey ,
253362 displayKey : params . requesterDisplayKey ,
@@ -278,7 +387,11 @@ export async function runSubagentAnnounceFlow(params: {
278387 startedAt : params . startedAt ,
279388 endedAt : params . endedAt ,
280389 } ) ;
281- const message = statsLine ? `${ announceReply . trim ( ) } \n\n${ statsLine } ` : announceReply . trim ( ) ;
390+ const message = normalizeAnnounceBody ( {
391+ outcome,
392+ announceReply,
393+ statsLine,
394+ } ) ;
282395
283396 await callGateway ( {
284397 method : "send" ,
0 commit comments