SpuntoSpunto/@spunto/build

Agent events

The normalized vocabulary an agent session is stored in — TASK_EVENT_TYPES, the NormalizedEvent shape, the agent contract (agent-v1) a runner speaks, and how its lines become events.

A delegated task runs under an agent app: Claude Code, Codex, Mistral Vibe, or one you wrote. The app's runner drives its harness in the worker, and to show the session while it runs, the control plane reads the runner's stdout and stores what it says. The harness's own JSON is never what gets stored: an output format is not an API. Persisting a CLI's shape would date the history the first time the vendor reshuffles a field.

Instead, every runner prints a small vocabulary of our own, one JSON object per line: the Spunto agent contract, agent-v1. Translating from its harness's dialect is the runner's job, done in the machine before the line leaves it, so the platform has one strict reader and no adapter of anyone's. That vocabulary is what gets stored. Events written months ago are read by today's UI, so it is a format like the others in this tab. Module reference: agent-stream. The reader, its JSON Schema (@spunto/build/agent-contract/schema.json) and a conformance suite (runConformance) live in @spunto/build/agent-contract.

Two rules hold for all of it:

  1. Anything the reader cannot place becomes raw, rather than being dropped. The schemas are closed: an unknown type, a field the type does not have, or a field of the wrong shape keeps the whole line as raw, with the reason, instead of half-reading it into the timeline. The UI keeps raw events behind a toggle.
  2. A malformed line is never fatal. Garbage in a line costs that line, not the task.

Protocols

export const AGENT_PROTOCOLS = ["none", "agent-v1"] as const
ProtocolThe session speaksNotes
noneanythingNothing to read: a task with no session, or one whose session has ended and whose history is already stored.
agent-v1The agent contract: our vocabulary, nativelyInteractive: the runner stays alive between turns. Its stdin takes five verbs, the same for every runner: {"type":"user","text"} (a turn, userTurnLine), {"type":"interrupt","id"} (stop the turn without killing the session, interruptLine), {"type":"title","id","description"} (name the conversation, sessionTitleRequestLine), {"type":"answer","callId","answers"} (answer a question, answerLine) and {"type":"mcp.reload","id","servers"?} (reconnect the MCP servers now, mcpReloadLine). Closing stdin ends the session once its turn is over.

The harnesses' own dialects (claude-json, claude-stream, jsonl, claude-runner, codex-runner) and applyProtocol were removed in 0.31.0: what a harness prints is its runner's business.

The vocabulary

export const TASK_EVENT_TYPES = [
  "session.started",
  "session.title",
  "message",
  "thinking",
  "tool.call",
  "tool.result",
  "plan",
  "usage",
  "session.ended",
  "session.background",
  "subagent",
  "context.compacted",
  "skill.loaded",
  "question",
  "raw",
] as const

export type NormalizedEvent = {
  type: TaskEventType
  payload: Record<string, unknown>
}

It is deliberately short. It has to be translatable from several harnesses, so anything specific to one of them does not belong in it. On a line of the contract, type sits next to the payload's fields, plus an optional source (see below). These are the payloads:

TypePayload
session.startedharness (required, such as "claude-code" or "codex"), model, sessionId (what a resume takes back), cwd, tools (string array), contextWindow (the model's window, when the runner could tell)
session.titletitle: the name the harness gave the conversation
messagerole ("assistant" or "user"), text, truncated, and limit on the message that carries a usage/rate limit
thinkingtext, truncated
tool.callcallId, name, input (a string: the call's arguments as the harness wrote them, JSON or not), truncated
tool.resultcallId, isError, output (string), truncated
plansteps: an array of { text, status }
usagescope ("call" or "session"), model, inputTokens, outputTokens, cacheReadTokens, cacheCreationTokens, contextTokens, contextWindow, costUsd, turns, durationMs
session.endedstatus ("succeeded" or "failed"), stopReason, summary, and limit ({ kind, status, message, resetsAt }) when the turn ended on a usage/rate limit or an overloaded API
session.backgroundtasks: the whole current list of { id, kind, description } still running in the background
subagentan agent the session started through one of its own tool calls (Claude Code's Agent), restated whole on every change: callId (that call), status (running, completed, failed or stopped), description, kind, background, activity (what it is doing now), lastTool, toolUses, tokens, durationMs, summary (its report, once it ended)
context.compactedtrigger ("auto" or "manual"), preTokens, postTokens, durationMs, summary (what the agent carries on with), truncated, and parentCallId when a subagent's context was compacted. Printed by the Claude runner only, today
skill.loadedcallId (the Skill call that asked for it, or null), name, path (the skill's directory), text (its SKILL.md, without the header line), truncated, and parentCallId when a subagent loaded it. Printed by the Claude runner only, today
questionthe agent asks the human to choose, and its turn waits for the answer (Claude Code's AskUserQuestion), restated whole as it changes: callId, status (pending, answered, or dismissed when the human replied in words or the turn was interrupted), questions ({ question, header, multiSelect, options: [{ label, description, preview }] }) and, once answered, answers ({ question, selected, text }). Answered with the runner's answer verb (answerLine). Printed by the Claude runner only (agent-v1), when SPUNTO_AGENT_QUESTIONS=1
mcp.reloadedwhere the session's MCP servers stand: id (the mcp.reload it answers, or null for the snapshot a runner prints when the session opens), servers ({ name, status, tools, error } — status in the harness's words, or deferred for a server with a call in flight, reconnected once the call is over; tools, how many it lists) and error when the harness cannot reload at all. The Claude runner puts the SDK's own status in source ({"type":"mcp_status","servers":[…with each tool's name, description and annotations]})
rawtext (a non-JSON line) or json (an object the reader could not place, with invalid: why)

message, thinking, tool.call, tool.result, plan, usage, context.compacted and skill.loaded may carry parentCallId: the event happened inside a subagent, the one the call with that callId started. A reader that draws the session's own thread leaves those out, and nests them under that call instead.

limit is a TurnLimit (kind one of TURN_LIMIT_KINDS: usage, rate or overloaded). Which of its harness's messages is a limit is the runner's reading; the platform then waits for resetsAt instead of treating the turn as finished work.

An optional field left out of a line is unknown; the nullable ones (model, callId, the token counters…) also take null for the same thing. Long text is clipped to 8,000 characters on the way in: message and thinking text, tool.call input, tool.result output, context.compacted summary, skill.loaded text, the summary of session.ended and subagent, and raw text. A single tool.result can be a 2 MB file read, and the timeline needs enough of it to be readable, not all of it. Where the payload has a truncated flag, it is set, so the UI can say so instead of quietly misrepresenting what the agent saw.

message.delta is not an event

A runner whose harness streams tokens may print the text of a message while it is being written, as {"type":"message.delta","text":"…","start":true} lines (start opens a fresh block, otherwise the text is appended). Those lines are never stored: parseLines reads them as nothing, and foldDraft folds them into the one transient draft a UI shows until the finished message lands. A runner only prints them when the platform sets SPUNTO_AGENT_STREAM=1 (STREAM_ENV).

Two kinds of usage

  • scope: "call" describes what one model call consumed. Summed over a session, these events give what the session has spent, and they exist during a turn. The latest one alone gives the context: its contextTokens is the prompt that was just sent. A call a subagent made (parentCallId) counts in the tokens, but its prompt is not the session's context.
  • scope: "session" is the harness's own running total, cumulative over the whole process. Every turn may restate the session so far, so readers take the latest one and never add them up.

summarizeUsage folds these into the recap a UI shows. It sums tokens from calls (and falls back to the session total), and it takes cost, turns and duration from the harness only, because a platform that invented a rate card would quote a number it cannot stand behind. The context window is the one session.started announced; the platform never guesses it from a model's name.

session.title answers the title verb

The platform asks a session to name its conversation with {"type":"title","id","description"} (description being the human's prompt), and the runner answers with a {"type":"session.title","title":"…"} line, if its harness can name a conversation at all. A runner may also print one on its own. cleanTitle gives a title the shape it is kept in: one line, at most 200 characters.

From a line to events

import { parseStreamLine, parseLines, splitCompleteLines } from "@spunto/build/agent-stream"

const { lines, consumed } = splitCompleteLines(bytes) // whole lines only; advance the cursor by `consumed`
const events = parseLines("agent-v1", lines, budget) // SourcedEvent[]: each event + what it was read from

parseStreamLine(protocol, line) returns zero or more events. Zero is a normal answer: a blank line, a message.delta, or any line of a session whose protocol is none. parseLines attaches a source to each event, which is what makes normalization reversible: the harness's own message, which the runner puts on the line as "source": {…}, or the trimmed line itself when the runner gave none. If our reading is wrong, what it was read from is still there to read again. parseLines also stops at budget events, and it skips a line the reader throws on.

splitCompleteLines only consumes up to the last \n. A 3 KB event split across two reads is parsed once, whole, instead of twice as two invalid halves.

Examples

@spunto/build/agent-contract/fake-agent.mjs is a whole runner, with no SDK, that passes the conformance suite. It is the shortest way to see what a session of the contract prints.

Compatibility

  • Adding an event type is safe for stored history. Older events never carry it, and an older reader that meets it should treat it like raw.
  • Renaming or removing a type, or a payload key, makes stored events unreadable by the UI that reads them. Add a new key instead, and read the old one as a fallback.
  • The contract's version (AGENT_CONTRACT_VERSION, 1) is bumped only for a change an existing runner would not survive. Additive changes keep it.
  • A change in a harness's output is not a format change on our side. The fix belongs in that app's runner.
Edit on GitHub

On this page