(
sessionId: string,
input: AgentOutputInput,
)
| 5139 | } |
| 5140 | |
| 5141 | private async findChildRunForOutput( |
| 5142 | sessionId: string, |
| 5143 | input: AgentOutputInput, |
| 5144 | ): Promise<{ |
| 5145 | header: AgentRunHeader; |
| 5146 | execution: SubagentExecutionRef; |
| 5147 | graph?: NonNullable<SubagentSessionParent['graph']>; |
| 5148 | }> { |
| 5149 | if (Number(!!input.execution) + Number(!!input.runId) + Number(!!input.turnId) !== 1) { |
| 5150 | throw new Error('agent_output requires exactly one execution, runId, or turnId locator'); |
| 5151 | } |
| 5152 | if (input.execution?.kind === 'child_session') { |
| 5153 | const execution = input.execution; |
| 5154 | const child = await this.deps.store.readHeader(execution.sessionId).catch((error) => { |
| 5155 | if (isNotFoundError(error)) return undefined; |
| 5156 | throw error; |
| 5157 | }); |
| 5158 | if ( |
| 5159 | !child || |
| 5160 | child.subagentParent?.kind !== 'subagent' || |
| 5161 | child.subagentParent.parentSessionId !== sessionId |
| 5162 | ) { |
| 5163 | throw new Error('agent_output could not find the requested child session'); |
| 5164 | } |
| 5165 | const runs = await this.deps.runStore?.listSessionRuns(child.id); |
| 5166 | const selected = execution.currentRunId |
| 5167 | ? runs?.find((run) => run.runId === execution.currentRunId) |
| 5168 | : runs |
| 5169 | ?.slice() |
| 5170 | .sort( |
| 5171 | (left, right) => |
| 5172 | right.createdAt - left.createdAt || |
| 5173 | right.updatedAt - left.updatedAt || |
| 5174 | right.runId.localeCompare(left.runId), |
| 5175 | )[0]; |
| 5176 | if (!selected || !isSessionInlineRun(selected)) { |
| 5177 | throw new Error('agent_output could not find the requested child session run'); |
| 5178 | } |
| 5179 | const header = await this.effectiveRunHeaderFromRuntimeLedger(selected); |
| 5180 | return { |
| 5181 | header, |
| 5182 | execution: { |
| 5183 | kind: 'child_session', |
| 5184 | sessionId: child.id, |
| 5185 | currentRunId: header.runId, |
| 5186 | }, |
| 5187 | ...(child.subagentParent.graph ? { graph: child.subagentParent.graph } : {}), |
| 5188 | }; |
| 5189 | } |
| 5190 | |
| 5191 | const legacyExecution = |
| 5192 | input.execution?.kind === 'legacy_child_run' ? input.execution : undefined; |
| 5193 | if (legacyExecution && legacyExecution.sessionId !== sessionId) { |
| 5194 | throw new Error('agent_output could not find the requested legacy child run'); |
| 5195 | } |
| 5196 | const runs = await this.deps.runStore?.listSessionRuns(sessionId); |
| 5197 | const header = runs?.find((run) => |
| 5198 | legacyExecution |
no test coverage detected