# Sub-agent row

A spawned agent appears once in the turn: its step row in the [execution timeline](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/chat/chat-turn-visual-spec.html). Opening the row shows that agent's conversation. No separate card repeats it, as in Claude's "Ran an agent" row and Codex's sub-agent activity list.

## Frame routing

1. **Job turn** branch of the same session, same session_id
2. **Driver sink** adds data.job_id to chat_response only
3. **Sub-agent store** frame has job_id; without it the frame goes to the conversation

The runtime bridge (`wsHandleChatResponse`) ignores frames with `job_id` too.

| Item | Rule |
| --- | --- |
| Admission ids | `<job>_user`, `<job>_user_reply` |
| Follow-up Job | `job_status.parent_msg_id` is the previous `<job>_user_reply`, which maps it to the agent's first Job |
| Store key | `<session_id>#sub-agent:<first job id>`. Older spawns without a Job id: their branch head. |
| Prompt row | Added before the reply streams, from the spawn card's prompt or `job_status.prompt_summary` |
| Rows | The key is the view's own: a row id it shares with the conversation, as on a checked-out agent branch, is a separate row. A conversation checked out on the branch applies the Job's frames to its own copy while that reply is open. |
| Opening the row | `load_sub_agent_transcript` answers with the agent's whole branch as turns and a window, through the transcript reader under this key: tools, thinking, function trees, terminal states and later turns. It reads the branch to its tip and never moves HEAD. |

## The row

| Part | Behaviour |
| --- | --- |
| Folded group | "Ran sub-agent scout · done", "· failed", "· cancelled"; "Running sub-agent scout…" while it works, even after the turn ended; "Ran 3 sub-agents · 2 done, 1 running" |
| Step row | Ghost glyph, "Sub-agent: *name*", status. Cancel while running (`execution.cancel`). The title opens the call's detail in the right panel. |
| Opened body | The agent's conversation, at most `min(520px, 60vh)`. Collapsed rows load nothing. Running agents open at the newest output and follow it until the reader scrolls up; finished agents open at the task. |
| Prompt | Raised mini card, *Task* or *Message*. Drops the `[task from …]` / `[message from …]` receipt line. Clamped to 3 lines until clicked. |
| Reply | `AssistantBubble` in `embedded` mode: no header, no action footer |
| Open ↗ | Checks out the agent's newest reply in the main view: the tip of the loaded transcript (loaded on click if needed), else the spawn card's `head_id`. While the conversation runs the server answers `run_active`, so it opens the row instead. |

## Transcript load

`load_sub_agent_transcript {session_id, job_id | head_id}` replies `sub_agent_transcript {view, head_id, turns, window}` and leaves the session head alone.

1. With only `job_id`: resolve the head from the Job's execution input.
2. Follow the newest user or assistant child to the branch tip, which reaches later messages to the agent.
3. Return user and assistant rows in branch order, not storage order: a reply placeholder can be written before its prompt.

The client replaces the agent's rows, keeps live rows the load did not include, and reloads when a Job of the agent reaches a terminal status.

**Where the reply id comes from**

A finished Job records its reply as `head_id` on the Job and its spawn card. The canonical driver reads it from `TurnResult.assistant_msg_id`. A turn that raised has no result and uses the reply id the Job was admitted with.

## The attach row

| Moment | What is written |
| --- | --- |
| Job accepted | From `on_accepted`: `status=running`, `job_id`, `tool_call_id` and, for the `agent` tool, `delivery=tool_result`. `head_id` is null until the end. The Job holds the row's id as `attach_pointer_id`. Each write reaches the caller's open chat as a `transcript_update` of that card; no stream event announces it. |
| Spawn returns | A patch of the same row. A failure or a result with no head patches it to `errored`. |
| Placeholder write failed | The end write appends the full row. |
| Worker startup | A running row whose Job is missing or finished is closed. |

## The reference card

An agent that runs in another conversation, and a branch attached by hand, are not steps of this turn. They keep a card (`rowKind: external_reference`), one row until it is opened.

| Part | Behaviour |
| --- | --- |
| Row | The agent's character and name, a state badge, the first line of its task, then time and cost: counted from the card's time while it runs with the Job's subtree cost, and `attach.usage` once it ended. A cost with an unknown price is left out. The row is a button; Enter or Space opens it. |
| State | Needs approval or Needs input while a question of its execution is open (it comes before Running), Waiting while it is queued, Running, Cancelling, Failed, Stopped, Done; Attached for a reference nobody ran. The states the Activity roster also names wear its badge and its words, and cost is written the same way. Approval and failure show on the closed row. |
| Hover tip | What kind of reference it is (Spawned, Attached, Imported from) and where from, why a limit or the person ended it, tokens and tool calls |
| Opened | The final reply, the files it changed with their branch (`attach.files`, only for an agent with its own worktree), the automatic follow-up, then the actions: **Switch** (checks out the agent's branch; **Open** for another conversation, *current* when already there), **Cancel execution** while it runs, and the time. |
| Live | The card is patched in place: an open card stays open when its agent ends |
| Find | A match inside a closed card opens it (`onFindReveal`) |

## The way back

On an agent's branch the sub-agent branch banner shows the agent's name and **Back to main conversation**, leading to the newest turn below the spawning reply. The reply itself would hide later turns. The Spawned-from card stays out while that branch is on screen.

**`session_loaded.spawned_branch`**

| Item | Rule |
| --- | --- |
| Shape | `{root_id, caller_id, back_target, first_reply, label}`, or `null` off a spawn branch. A bounded load ships no graph. |
| Branch root | User row with `source=agent_spawn` and no predecessor; its `caller` is the spawning reply |
| Label | Spawn card with the matching `job_id`. A run older than the placeholder, stopped mid-call, has no card: the `description` of the spawning reply's `agent` call whose prompt opened the branch. |
| `spawned_from` | Found the same way; needs no pointer head |

**Code**

- `apps/web/components/chat/messages/execution/execution-strip.tsx` (`SubAgentStep`), `tool-group-summary.ts`
- `apps/web/components/chat/messages/cards/sub-agent-transcript.tsx`
- `apps/web/components/chat/messages/cards/attach-card.tsx`, `agent-event-row.tsx`, `apps/web/lib/chat/transcript/agent-card.ts`
- `apps/web/lib/chat/transcript/sub-agent-transcripts.ts`
- `apps/web/lib/runtime-bridge/session/transcript-updates.ts` (`applyTranscriptUpdate`)
- `openprogram/runtime/conversation/production_driver/execution.py` (`_turn_event_sink`)
- `openprogram/interfaces/server/ws_actions/sub_agent.py`
