Agents: chat rows and run cards
How src/agents/model.ts turns session events into transcript rows and run records into run cards, including plan ownership, pinned runs and truncation.

src/agents/model.ts is the presentation model of the agents package. It is pure: wire records in, flat display records out, no network and no clock. Two functions do most of the work: chatRows builds the transcript and runRow builds a run card. Both are called from sources and streams.

Types

Type

Meaning

Wire

Record<string, any>: a protocol record, kept untyped until converted

SessionEvent

{id, seq, createdAt, event}: a durable event ordered by the server's seq

ChatRow

One transcript row. Matches shape AgentChatRow in agents.contract

ChildRun

A descendant run shown under a plan step or under the card

RunRow

One run card. Matches shape AgentRun

Every ChatRow has all 14 fields set (strings default to ""), because a Contract shape has no optional fields.

chatRows

chatRows(events, triggerContext?) makes one pass to index tool_result and tool_spawn events by toolCallId and to collect known call ids, then a second pass in seq order. The kind field decides how the session screen draws a row:

Kind

Produced from

message

A text part or message text. mine is true when the actor is user

system

Text whose actor is system

tool

A tool_call event, a tool part inside a message, a legacy toolCalls entry, an orphaned tool_spawn, or an orphaned tool_result

trigger

The session's trigger context, placed first with at: 0

continuation

A system message starting with <session_continuation>

error

An error event

message-error

A message's errorMessage

raw

Anything else: unknown parts and event types, structured blocks, object content, contextLines, attachments

Rules worth knowing:

    Row ids. The first part of a message keeps the event id; later parts are <id>-part-<i>; legacy tool calls are <id>-tool-<i>. Contract tests rely on these ids (message-mock-session-17, tool-mock-session-20).

    Tool rows. tool renames execute_code to execute, and drops plan and status tools from the chat (inferred reason: the plan is shown on the run card). Status is Failed when the result has an error or isError is true, Done when a result or output exists, else Running. Title, label, link target and source chip come from tool-summary. A spawn attaches childSessionId and childTitle.

    Continuations. textRow parses <predecessor session="..." title="..."> and the <handoff> body, shows the first non-heading line, and links to /s/<predecessor>.

    Trigger framing is hidden. A user message containing <trigger_context> is cut at that tag, <trigger_data_warning> is removed, and <trigger_instructions> is moved once into the trigger row's detail.

    Nothing is dropped silently. Unknown content becomes a raw row with its JSON in detail.

    Linear time. Results are joined through maps. A test builds 40 000 events and requires chatRows to finish in under 150 ms, named for the 100 ms turn budget.

upsertEvent replaces by id and sorts by seq, so "Replay is idempotent and out-of-order delivery cannot scramble the transcript".

Truncation limit

detail(value) pretty-prints JSON with two spaces and cuts it at 4 000 characters, appending a line that says it was truncated. It is used for toolInput, toolOutput and every raw row. This is the only size limit in the module. The row list itself is not capped.

runRow

runRow(run, sessionPlan?, tree = []) converts one run record:

Field

Rule

label

runLabel: queued Queued, claimed Starting, running Working, waiting Waiting, succeeded Done, failed Failed, canceled Canceled

live

Status is queued, claimed, running or waiting

waiting, signal

waiting status; signal is wait.answerWith, or resume for a budget-pause

note

run.error.message, else a sentence for the wait reason: budget pause, children with a count, timer, or wait.label

steps

The plan's steps. When the run is no longer live, a running step shows done and a pending step shows skipped. automatic is resolvedBy === 'runtime'

children

Descendants not attached to any step

startedAt, endedAt, wakeAt

For the live duration and the "Waking in" countdown

tokens, usage

formatTokens(run.usage.total): 999, 1.2k, 12k, 1.2M

Plan ownership

A session has one plan record, but several runs. The plan shown on a card is run.plan if present; otherwise the session plan when its ownerRunId equals this run; otherwise, when the session plan names no owner, only a live run shows it. A finished run therefore never displays a later run's plan.

Child runs

Descendants are the records in tree whose rootRunId matches. A child attaches to a step by planStepId when it has one, else by stepLabel equal to the step's label. Unattached children stay visible under the card.

Pinned runs

pinned is live and (status is waiting or the plan is not frozen). A plan is frozen when it has settledAt and every step is done, failed or skipped. The session screen places pinned cards after the last message, and unpinned cards in the timeline at at, which is the frozen time, else finishedAt, else updatedAt. So a working run stays at the bottom, a waiting run stays reachable, and a completed plan settles into place before the closing reply.

Tests

    agents.test.ts, group transcript and run presentation, six tests: replay and tool joins, pending and failed tools with hidden planning tools, frozen plans and the owning run, continuations, settled plans and pinned waiting runs, token thresholds.

    parity.test.ts: child runs by step id then label, tool spawns, trigger context, structured parts, legacy embedded tools, inspectable structured content.

    review.test.ts: the linear-time test.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime

Comments