{"version":3,"file":"result-intercom.d.ts","sourceRoot":"","sources":["../../../src/intercom/result-intercom.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,KAAK,OAAO,EACZ,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,wBAAwB,EAC7B,KAAK,sBAAsB,EAK3B,KAAK,2BAA2B,EAChC,KAAK,6BAA6B,EAClC,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,MAAM,oBAAoB,CAAC;AAE5B,wBAAgB,2BAA2B,CAAC,KAAK,EAAE;IAClD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC7B,GAAG,oBAAoB,CAWvB;AA4HD,wBAAgB,2BAA2B,CAC1C,QAAQ,EAAE,KAAK,CAAC,gBAAgB,GAAG,sBAAsB,CAAC,GAAG,SAAS,GACpE,sBAAsB,EAAE,GAAG,SAAS,CAGtC;AAED,wBAAgB,oCAAoC,CACnD,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,2BAA2B,EAAE,EACvC,cAAc,EAAE,gBAAgB,EAAE,GAAG,SAAS,GAC5C,2BAA2B,EAAE,CAuB/B;AAwBD,UAAU,iCAAiC;IAC1C,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC;IAC/B,QAAQ,EAAE,2BAA2B,EAAE,CAAC;IACxC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,wBAAwB,CAAC;CAC3C;AAmFD,wBAAgB,kCAAkC,CACjD,KAAK,EAAE,iCAAiC,GACtC,6BAA6B,CA8B/B;AAED,wBAAsB,kCAAkC,CACvD,MAAM,EAAE,gBAAgB,EACxB,OAAO,EAAE,6BAA6B,EACtC,SAAS,SAAM,GACb,OAAO,CAAC,OAAO,CAAC,CAQlB;AAED,wBAAsB,mCAAmC,CACxD,MAAM,EAAE,gBAAgB,EACxB,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,MAAM,EACf,SAAS,SAAM,EACf,KAAK,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACjC,OAAO,CAAC,OAAO,CAAC,CA2BlB;AAWD,wBAAgB,qCAAqC,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAK/E;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE;IAClD,IAAI,EAAE,eAAe,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,6BAA6B,CAAC;CACvC,GAAG,MAAM,CA0CT","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport * as fs from \"node:fs\";\nimport { isUnexplainedProcessSignal } from \"../runs/shared/process-signal.ts\";\nimport {\n\ttype Details,\n\ttype IntercomEventBus,\n\ttype NestedRunSummary,\n\ttype ParallelHandoffReference,\n\ttype PublicNestedRunSummary,\n\ttype SingleResult,\n\tSUBAGENT_RESULT_INTERCOM_DELIVERY_EVENT,\n\tSUBAGENT_RESULT_INTERCOM_EVENT,\n\ttype SubagentOutputState,\n\ttype SubagentResultIntercomChild,\n\ttype SubagentResultIntercomPayload,\n\ttype SubagentResultStatus,\n\ttype SubagentRunMode,\n} from \"../shared/types.ts\";\n\nexport function resolveSubagentResultStatus(input: {\n\texitCode?: number;\n\tsuccess?: boolean;\n\tstate?: string;\n\tinterrupted?: boolean;\n\tdetached?: boolean;\n\tprocessSignal?: string | null;\n\ttimedOut?: boolean;\n\tstopped?: boolean;\n\tturnBudgetExceeded?: boolean;\n}): SubagentResultStatus {\n\tif (input.detached) return \"detached\";\n\tif (input.stopped || input.state === \"stopped\") return \"stopped\";\n\tif (input.interrupted || input.state === \"paused\") return \"paused\";\n\tif (input.success === true) return \"completed\";\n\tif (isUnexplainedProcessSignal(input) && input.exitCode !== 0) return \"stopped\";\n\tif (input.success === false) return \"failed\";\n\tif (input.state === \"complete\") return \"completed\";\n\tif (input.state === \"failed\") return \"failed\";\n\tif (typeof input.exitCode === \"number\") return input.exitCode === 0 ? \"completed\" : \"failed\";\n\treturn \"failed\";\n}\n\nfunction countStatuses(children: SubagentResultIntercomChild[]): Record<SubagentResultStatus, number> {\n\tconst counts: Record<SubagentResultStatus, number> = {\n\t\tcompleted: 0,\n\t\tfailed: 0,\n\t\tpaused: 0,\n\t\tstopped: 0,\n\t\tdetached: 0,\n\t};\n\tfor (const child of children) {\n\t\tcounts[child.status] += 1;\n\t}\n\treturn counts;\n}\n\nfunction formatStatusCounts(counts: Record<SubagentResultStatus, number>): string {\n\tconst parts = [\n\t\tcounts.completed ? `${counts.completed} completed` : undefined,\n\t\tcounts.failed ? `${counts.failed} failed` : undefined,\n\t\tcounts.stopped ? `${counts.stopped} stopped` : undefined,\n\t\tcounts.paused ? `${counts.paused} paused` : undefined,\n\t\tcounts.detached ? `${counts.detached} detached` : undefined,\n\t].filter((part): part is string => Boolean(part));\n\treturn parts.length ? parts.join(\", \") : \"0 results\";\n}\n\nfunction countOutputStates(children: SubagentResultIntercomChild[]): Record<SubagentOutputState, number> {\n\tconst counts: Record<SubagentOutputState, number> = { present: 0, absent: 0, unknown: 0 };\n\tfor (const child of children) counts[child.outputState ?? \"unknown\"] += 1;\n\treturn counts;\n}\n\nfunction formatOutputCounts(counts: Record<SubagentOutputState, number>): string {\n\tconst parts = [\n\t\tcounts.present ? `${counts.present} present` : undefined,\n\t\tcounts.absent ? `${counts.absent} absent` : undefined,\n\t\tcounts.unknown ? `${counts.unknown} unknown` : undefined,\n\t].filter((part): part is string => Boolean(part));\n\treturn parts.length ? parts.join(\", \") : \"0 outputs\";\n}\n\nfunction resolveGroupedStatus(children: SubagentResultIntercomChild[]): SubagentResultStatus {\n\tconst counts = countStatuses(children);\n\tif (counts.failed > 0) return \"failed\";\n\tif (counts.stopped > 0) return \"stopped\";\n\tif (counts.paused > 0) return \"paused\";\n\tif (counts.completed > 0) return \"completed\";\n\tif (counts.detached > 0) return \"detached\";\n\treturn \"failed\";\n}\n\nfunction compactNestedRun(run: NestedRunSummary | PublicNestedRunSummary, depth = 0): PublicNestedRunSummary {\n\treturn {\n\t\tid: run.id,\n\t\tparentRunId: run.parentRunId,\n\t\t...(run.parentStepIndex !== undefined ? { parentStepIndex: run.parentStepIndex } : {}),\n\t\t...(run.parentAgent ? { parentAgent: run.parentAgent } : {}),\n\t\tdepth: run.depth,\n\t\tpath: run.path.slice(0, 4).map((part) => ({\n\t\t\trunId: part.runId,\n\t\t\t...(part.stepIndex !== undefined ? { stepIndex: part.stepIndex } : {}),\n\t\t\t...(part.agent ? { agent: part.agent } : {}),\n\t\t})),\n\t\t...(run.asyncDir ? { asyncDir: run.asyncDir } : {}),\n\t\t...(run.sessionId ? { sessionId: run.sessionId } : {}),\n\t\t...(run.sessionFile ? { sessionFile: run.sessionFile } : {}),\n\t\t...(run.intercomTarget ? { intercomTarget: run.intercomTarget } : {}),\n\t\t...(run.ownerIntercomTarget ? { ownerIntercomTarget: run.ownerIntercomTarget } : {}),\n\t\t...(run.leafIntercomTarget ? { leafIntercomTarget: run.leafIntercomTarget } : {}),\n\t\t...(run.ownerState ? { ownerState: run.ownerState } : {}),\n\t\t...(run.mode ? { mode: run.mode } : {}),\n\t\tstate: run.state,\n\t\t...(run.agent ? { agent: run.agent } : {}),\n\t\t...(run.agents?.length ? { agents: run.agents.slice(0, 12) } : {}),\n\t\t...(run.model ? { model: run.model } : {}),\n\t\t...(run.thinking ? { thinking: run.thinking } : {}),\n\t\t...(run.currentStep !== undefined ? { currentStep: run.currentStep } : {}),\n\t\t...(run.chainStepCount !== undefined ? { chainStepCount: run.chainStepCount } : {}),\n\t\t...(run.parallelGroups?.length ? { parallelGroups: run.parallelGroups.slice(0, 8) } : {}),\n\t\t...(run.activityState ? { activityState: run.activityState } : {}),\n\t\t...(run.lastActivityAt !== undefined ? { lastActivityAt: run.lastActivityAt } : {}),\n\t\t...(run.currentTool ? { currentTool: run.currentTool } : {}),\n\t\t...(run.currentToolStartedAt !== undefined ? { currentToolStartedAt: run.currentToolStartedAt } : {}),\n\t\t...(run.currentPath ? { currentPath: run.currentPath } : {}),\n\t\t...(run.turnCount !== undefined ? { turnCount: run.turnCount } : {}),\n\t\t...(run.toolCount !== undefined ? { toolCount: run.toolCount } : {}),\n\t\t...(run.totalTokens ? { totalTokens: run.totalTokens } : {}),\n\t\t...(run.startedAt !== undefined ? { startedAt: run.startedAt } : {}),\n\t\t...(run.endedAt !== undefined ? { endedAt: run.endedAt } : {}),\n\t\t...(run.lastUpdate !== undefined ? { lastUpdate: run.lastUpdate } : {}),\n\t\t...(run.error ? { error: run.error } : {}),\n\t\t...(run.steps?.length\n\t\t\t? {\n\t\t\t\t\tsteps: run.steps.slice(0, 12).map((step) => ({\n\t\t\t\t\t\tagent: step.agent,\n\t\t\t\t\t\tstatus: step.status,\n\t\t\t\t\t\t...(step.model ? { model: step.model } : {}),\n\t\t\t\t\t\t...(step.thinking ? { thinking: step.thinking } : {}),\n\t\t\t\t\t\t...(step.sessionFile ? { sessionFile: step.sessionFile } : {}),\n\t\t\t\t\t\t...(step.activityState ? { activityState: step.activityState } : {}),\n\t\t\t\t\t\t...(step.lastActivityAt !== undefined ? { lastActivityAt: step.lastActivityAt } : {}),\n\t\t\t\t\t\t...(step.currentTool ? { currentTool: step.currentTool } : {}),\n\t\t\t\t\t\t...(step.currentToolStartedAt !== undefined\n\t\t\t\t\t\t\t? { currentToolStartedAt: step.currentToolStartedAt }\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t\t...(step.currentPath ? { currentPath: step.currentPath } : {}),\n\t\t\t\t\t\t...(step.turnCount !== undefined ? { turnCount: step.turnCount } : {}),\n\t\t\t\t\t\t...(step.toolCount !== undefined ? { toolCount: step.toolCount } : {}),\n\t\t\t\t\t\t...(step.startedAt !== undefined ? { startedAt: step.startedAt } : {}),\n\t\t\t\t\t\t...(step.endedAt !== undefined ? { endedAt: step.endedAt } : {}),\n\t\t\t\t\t\t...(step.error ? { error: step.error } : {}),\n\t\t\t\t\t\t...(depth < 2 && step.children?.length\n\t\t\t\t\t\t\t? { children: step.children.slice(0, 8).map((child) => compactNestedRun(child, depth + 1)) }\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t})),\n\t\t\t\t}\n\t\t\t: {}),\n\t\t...(depth < 2 && run.children?.length\n\t\t\t? { children: run.children.slice(0, 8).map((child) => compactNestedRun(child, depth + 1)) }\n\t\t\t: {}),\n\t};\n}\n\nexport function compactNestedResultChildren(\n\tchildren: Array<NestedRunSummary | PublicNestedRunSummary> | undefined,\n): PublicNestedRunSummary[] | undefined {\n\tif (!children?.length) return undefined;\n\treturn children.slice(0, 16).map((child) => compactNestedRun(child));\n}\n\nexport function attachNestedChildrenToResultChildren(\n\trunId: string,\n\tchildren: SubagentResultIntercomChild[],\n\tnestedChildren: NestedRunSummary[] | undefined,\n): SubagentResultIntercomChild[] {\n\tconst compact = compactNestedResultChildren(nestedChildren);\n\tif (!compact?.length)\n\t\treturn children.map((child) => ({ ...child, children: compactNestedResultChildren(child.children) }));\n\treturn children.map((child, index) => {\n\t\tconst childIndex = child.index ?? index;\n\t\tconst alreadyAttachedIds = new Set(child.children?.map((nested) => nested.id) ?? []);\n\t\tconst attached = compact.filter(\n\t\t\t(nested) =>\n\t\t\t\tnested.parentRunId === runId && nested.parentStepIndex === childIndex && !alreadyAttachedIds.has(nested.id),\n\t\t);\n\t\tconst fallbackAttached =\n\t\t\tchildren.length === 1\n\t\t\t\t? compact.filter(\n\t\t\t\t\t\t(nested) =>\n\t\t\t\t\t\t\tnested.parentRunId === runId &&\n\t\t\t\t\t\t\tnested.parentStepIndex === undefined &&\n\t\t\t\t\t\t\t!alreadyAttachedIds.has(nested.id),\n\t\t\t\t\t)\n\t\t\t\t: [];\n\t\tconst merged = compactNestedResultChildren([...(child.children ?? []), ...attached, ...fallbackAttached]);\n\t\treturn merged?.length ? { ...child, children: merged } : { ...child, children: undefined };\n\t});\n}\n\nfunction formatNestedResultLines(children: PublicNestedRunSummary[] | undefined): string[] {\n\tif (!children?.length) return [];\n\tconst lines = [\"Nested subagents:\"];\n\tlet remaining = 10;\n\tconst append = (runs: PublicNestedRunSummary[] | undefined, indent: string): void => {\n\t\tfor (const run of runs ?? []) {\n\t\t\tif (remaining <= 0) {\n\t\t\t\tlines.push(`${indent}↳ +more nested runs; inspect status for full tree`);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tremaining--;\n\t\t\tconst label = run.agent ?? run.agents?.join(\"+\") ?? run.id;\n\t\t\tlines.push(`${indent}↳ ${label} — ${run.state} [${run.id}]`);\n\t\t\tif (run.sessionFile) lines.push(`${indent}  Session: ${run.sessionFile}`);\n\t\t\tappend(run.children, `${indent}  `);\n\t\t\tfor (const step of run.steps ?? []) append(step.children, `${indent}    `);\n\t\t}\n\t};\n\tappend(children, \"\");\n\treturn lines;\n}\n\ninterface GroupedResultIntercomMessageInput {\n\tto: string;\n\trunId: string;\n\tmode: SubagentRunMode;\n\tsource: \"foreground\" | \"async\";\n\tchildren: SubagentResultIntercomChild[];\n\tasyncId?: string;\n\tasyncDir?: string;\n\tchainSteps?: number;\n\tparallelHandoff?: ParallelHandoffReference;\n}\n\nfunction asyncResumeGuidance(input: {\n\tsource: \"foreground\" | \"async\";\n\tchildren: SubagentResultIntercomChild[];\n\tasyncId?: string;\n}): string | undefined {\n\tif (input.source !== \"async\" || !input.asyncId) return undefined;\n\tconst resumable = input.children.filter(\n\t\t(child) => typeof child.sessionPath === \"string\" && fs.existsSync(child.sessionPath),\n\t);\n\tif (input.children.length === 1 && resumable.length === 1) {\n\t\treturn `Revive: subagent({ action: \"resume\", id: \"${input.asyncId}\", message: \"...\" })`;\n\t}\n\tif (resumable.length > 0) {\n\t\tconst firstIndex = resumable[0]?.index ?? input.children.indexOf(resumable[0]!);\n\t\treturn `Revive child: subagent({ action: \"resume\", id: \"${input.asyncId}\", index: ${firstIndex}, message: \"...\" })`;\n\t}\n\treturn \"Resume: unavailable; no child session file was persisted.\";\n}\n\nfunction formatSubagentResultIntercomMessage(input: {\n\trunId: string;\n\tmode: SubagentRunMode;\n\tstatus: SubagentResultStatus;\n\tsource: \"foreground\" | \"async\";\n\tchildren: SubagentResultIntercomChild[];\n\tasyncId?: string;\n\tasyncDir?: string;\n\tchainSteps?: number;\n\tparallelHandoff?: ParallelHandoffReference;\n}): string {\n\tconst counts = countStatuses(input.children);\n\tconst outputCounts = countOutputStates(input.children);\n\tconst lines: string[] = [\n\t\t\"subagent results\",\n\t\t\"\",\n\t\t`Run: ${input.runId}`,\n\t\t`Mode: ${input.mode}`,\n\t\t`Process status: ${input.status}`,\n\t\t`Children: ${formatStatusCounts(counts)}`,\n\t\t`Outputs: ${formatOutputCounts(outputCounts)} (semantic adequacy unassessed)`,\n\t];\n\tif (input.children.some((child) => child.status === \"failed\" && child.outputState === \"present\")) {\n\t\tlines.push(\n\t\t\t\"Recovery: At least one failed process produced output. Inspect that output before retrying; output presence does not establish task completion.\",\n\t\t);\n\t}\n\tif (input.mode === \"chain\" && typeof input.chainSteps === \"number\") {\n\t\tlines.push(`Chain steps: ${input.chainSteps}`);\n\t}\n\tif (input.asyncId) lines.push(`Async id: ${input.asyncId}`);\n\tif (input.asyncDir) lines.push(`Async dir: ${input.asyncDir}`);\n\tif (input.parallelHandoff) lines.push(`Parallel handoff: ${input.parallelHandoff.path}`);\n\tconst resumeGuidance = asyncResumeGuidance(input);\n\tif (resumeGuidance) lines.push(resumeGuidance);\n\tif (input.children.some((child) => child.intercomTarget)) {\n\t\tlines.push(\"\");\n\t\tlines.push(\n\t\t\tinput.source === \"async\"\n\t\t\t\t? \"Previous intercom targets below identify child sessions used while they were running. Inspect artifacts or session logs if resume is unavailable.\"\n\t\t\t\t: \"Intercom targets below identify child sessions used while they were running; completed child sessions may no longer be reachable. Inspect artifacts or session logs for follow-up.\",\n\t\t);\n\t}\n\n\tfor (let index = 0; index < input.children.length; index++) {\n\t\tconst child = input.children[index]!;\n\t\tlines.push(\"\");\n\t\tlines.push(`${index + 1}. ${child.agent} — process ${child.status} · output ${child.outputState ?? \"unknown\"}`);\n\t\tif (child.intercomTarget)\n\t\t\tlines.push(\n\t\t\t\t`${input.source === \"async\" ? \"Previous intercom target\" : \"Run intercom target\"}: ${child.intercomTarget}`,\n\t\t\t);\n\t\tif (child.artifactPath) lines.push(`Output artifact: ${child.artifactPath}`);\n\t\tif (child.sessionPath) lines.push(`Session: ${child.sessionPath}`);\n\t\tlines.push(...formatNestedResultLines(child.children));\n\t\tlines.push(\"Summary:\");\n\t\tlines.push(child.summary);\n\t}\n\n\treturn lines.join(\"\\n\");\n}\n\nexport function buildSubagentResultIntercomPayload(\n\tinput: GroupedResultIntercomMessageInput,\n): SubagentResultIntercomPayload {\n\tconst children = input.children.map((child) => ({\n\t\t...child,\n\t\toutputState: child.outputState ?? \"unknown\",\n\t\tsummary: child.summary.trim() || \"(no output)\",\n\t\tchildren: compactNestedResultChildren(child.children),\n\t}));\n\tconst status = resolveGroupedStatus(children);\n\tconst summary = formatStatusCounts(countStatuses(children));\n\tconst firstChild = children[0];\n\tconst payload: SubagentResultIntercomPayload = {\n\t\tto: input.to,\n\t\trunId: input.runId,\n\t\tmode: input.mode,\n\t\tstatus,\n\t\tsummary,\n\t\tsource: input.source,\n\t\tchildren,\n\t\t...(input.asyncId ? { asyncId: input.asyncId } : {}),\n\t\t...(input.asyncDir ? { asyncDir: input.asyncDir } : {}),\n\t\t...(typeof input.chainSteps === \"number\" ? { chainSteps: input.chainSteps } : {}),\n\t\t...(input.parallelHandoff ? { parallelHandoff: input.parallelHandoff } : {}),\n\t\t...(firstChild?.agent ? { agent: firstChild.agent } : {}),\n\t\t...(firstChild?.index !== undefined ? { index: firstChild.index } : {}),\n\t\t...(firstChild?.artifactPath ? { artifactPath: firstChild.artifactPath } : {}),\n\t\t...(firstChild?.sessionPath ? { sessionPath: firstChild.sessionPath } : {}),\n\t\tmessage: \"\",\n\t};\n\tpayload.message = formatSubagentResultIntercomMessage(payload);\n\treturn payload;\n}\n\nexport async function deliverSubagentResultIntercomEvent(\n\tevents: IntercomEventBus,\n\tpayload: SubagentResultIntercomPayload,\n\ttimeoutMs = 500,\n): Promise<boolean> {\n\treturn deliverSubagentIntercomMessageEvent(\n\t\tevents,\n\t\tpayload.to,\n\t\tpayload.message,\n\t\ttimeoutMs,\n\t\tpayload as unknown as Record<string, unknown>,\n\t);\n}\n\nexport async function deliverSubagentIntercomMessageEvent(\n\tevents: IntercomEventBus,\n\tto: string,\n\tmessage: string,\n\ttimeoutMs = 500,\n\textra: Record<string, unknown> = {},\n): Promise<boolean> {\n\tif (typeof events.on !== \"function\" || typeof events.emit !== \"function\") return false;\n\tconst requestId = typeof extra.requestId === \"string\" ? extra.requestId : randomUUID();\n\treturn new Promise((resolve) => {\n\t\tlet settled = false;\n\t\tlet unsubscribe: (() => void) | undefined;\n\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\t\tconst finish = (delivered: boolean) => {\n\t\t\tif (settled) return;\n\t\t\tsettled = true;\n\t\t\tif (timer) clearTimeout(timer);\n\t\t\tunsubscribe?.();\n\t\t\tresolve(delivered);\n\t\t};\n\t\tunsubscribe = events.on(SUBAGENT_RESULT_INTERCOM_DELIVERY_EVENT, (data) => {\n\t\t\tif (!data || typeof data !== \"object\") return;\n\t\t\tconst delivery = data as { requestId?: unknown; delivered?: unknown };\n\t\t\tif (delivery.requestId !== requestId) return;\n\t\t\tfinish(delivery.delivered === true);\n\t\t});\n\t\ttimer = setTimeout(() => finish(false), timeoutMs);\n\t\ttry {\n\t\t\tevents.emit(SUBAGENT_RESULT_INTERCOM_EVENT, { ...extra, to, message, requestId });\n\t\t} catch {\n\t\t\tfinish(false);\n\t\t}\n\t});\n}\n\nfunction stripSingleResultOutputs(result: SingleResult): SingleResult {\n\treturn {\n\t\t...result,\n\t\tmessages: undefined,\n\t\tfinalOutput: undefined,\n\t\ttruncation: undefined,\n\t};\n}\n\nexport function stripDetailsOutputsForIntercomReceipt(details: Details): Details {\n\treturn {\n\t\t...details,\n\t\tresults: details.results.map(stripSingleResultOutputs),\n\t};\n}\n\nexport function formatSubagentResultReceipt(input: {\n\tmode: SubagentRunMode;\n\trunId: string;\n\tpayload: SubagentResultIntercomPayload;\n}): string {\n\tconst counts = countStatuses(input.payload.children);\n\tconst modeLabel =\n\t\tinput.mode === \"single\"\n\t\t\t? \"single subagent result\"\n\t\t\t: input.mode === \"parallel\"\n\t\t\t\t? \"parallel subagent results\"\n\t\t\t\t: \"chain subagent results\";\n\tconst lines = [\n\t\t`Delivered ${modeLabel} via intercom.`,\n\t\t`Run: ${input.runId}`,\n\t\t`Children: ${formatStatusCounts(counts)}`,\n\t];\n\n\tif (input.payload.parallelHandoff) lines.push(`Parallel handoff: ${input.payload.parallelHandoff.path}`);\n\n\tconst artifacts = input.payload.children.filter((child) => typeof child.artifactPath === \"string\");\n\tif (artifacts.length > 0) {\n\t\tlines.push(\"Artifacts:\");\n\t\tfor (const child of artifacts) {\n\t\t\tlines.push(`- ${child.agent} [${child.status}]: ${child.artifactPath}`);\n\t\t}\n\t}\n\n\tconst intercomTargets = input.payload.children.filter((child) => typeof child.intercomTarget === \"string\");\n\tif (intercomTargets.length > 0) {\n\t\tlines.push(\"Run intercom targets (may be inactive after completion):\");\n\t\tfor (const child of intercomTargets) {\n\t\t\tlines.push(`- ${child.agent} [${child.status}]: ${child.intercomTarget}`);\n\t\t}\n\t}\n\n\tconst sessions = input.payload.children.filter((child) => typeof child.sessionPath === \"string\");\n\tif (sessions.length > 0) {\n\t\tlines.push(\"Sessions:\");\n\t\tfor (const child of sessions) {\n\t\t\tlines.push(`- ${child.agent} [${child.status}]: ${child.sessionPath}`);\n\t\t}\n\t}\n\n\tlines.push(\"Full grouped output was sent over intercom.\");\n\treturn lines.join(\"\\n\");\n}\n"]}