Skip to content

Read a session as a conversation: GET /sessions/{id}/transcript - #251

Merged
fylorn merged 1 commit into
mainfrom
session-transcript
Oct 2, 2026
Merged

fylorn merged 1 commit into
mainfrom
session-transcript

Conversation

@fylorn

@fylorn fylorn commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

What this changes

New control-plane endpoint GET /sessions/{id}/transcript (Transcript). It reads a session's stored request and response bodies into a conversation: for every turn, the messages that are new in that request, the answer decoded from the stored response, and what could not be shown. Control-plane protocol 32.

type Transcript = { session: string; system: string | null; turns: Array<TranscriptTurn> };
type TranscriptTurn = { id: string; restart: boolean; system_changed: string | null;
  input: Array<TranscriptMessage>; output: Array<TranscriptPart>; gaps: Array<TranscriptGap> };
type TranscriptMessage = { role: "user" | "assistant" | "tool" | "system"; parts: Array<TranscriptPart> };
type TranscriptPart =
  | { kind: "text"; text: string } | { kind: "thinking"; text: string }
  | { kind: "tool_call"; id: string; name: string; input: string }
  | { kind: "tool_result"; call_id: string; text: string; is_error: boolean }
  | { kind: "image"; media_type: string | null; bytes: number | null }
  | { kind: "other"; label: string };
type TranscriptGap = "request_missing" | "request_truncated" | "response_missing" | "response_truncated" | "response_unreadable";

Why

The session view shows each turn's tokens and cost (SessionDetail) but not what was said. The bodies are on disk, but clients resend the whole history on every turn, so showing them raw repeats everything hundreds of times.

How a turn is read

  • Same turns as the session detail. Db::session_requests uses the same filter and order as Db::turns. id is the request id as a decimal string.
  • Formats are worked out the same way content search does it: the client format from the path (search::text::client_dialect), the upstream format from the row's translated.to, otherwise the client's. Calls that do not generate an answer (count_tokens, /responses/compact) are empty turns and are not compared with their neighbours.
  • New input. If the previous readable request's messages are a prefix of this one's, the rest is new, minus one leading assistant message (the previous answer, already in that turn's output). Messages are compared by a 16-byte fingerprint of a normalized form: cache_control, reasoning signatures and key order are ignored, and so is reasoning text, because some clients drop earlier reasoning from the history. No prefix, or an unreadable request in between, gives restart: true and the whole history.
    • The leading assistant message is kept when the previous turn's answer could not be read in full (response_* gap). It is then the only record of what was said.
    • Tool-call ids in the previous answer are replaced with the ids the client recorded, when the names line up one by one. Translated answers otherwise carry the upstream's ids (Gemini sends none), and the next turn's results would not pair with their calls.
  • Roles. Messages made only of tool results are tool (Anthropic tool_result-only user messages, Chat tool, Responses *_call_output). Consecutive assistant items (a Responses answer is several items) and consecutive tool results merge into one message; user messages do not. Mid-conversation system and developer messages are system. Leading ones in Chat and Responses are part of the system prompt, after instructions.
  • Requests are read from the client's JSON, not from tw-dialect's IR. The IR keeps only what maps across formats: it folds mid-conversation system messages into the system prompt, gives tool results the user role, and drops server-tool blocks, documents and audio. Those are what a transcript has to show. Field choices follow tw-dialect's decoders. Blocks without a counterpart become other with their type name (document, server_tool_use, web_search_call, compaction, input_audio…). Images give only the media type and the decoded size.
  • Answers.
    • Whole JSON answers are read with the same message readers (an answer has the same shape as an assistant message); Bedrock's goes through bedrock::decode_response.
    • Streams go through tw-dialect's per-format stream parsers frame by frame, including Bedrock's SSE-transcoded stream and Gemini's JSON-array stream.
    • Blocks the parsers skip (Anthropic server tools, Responses hosted tools, Gemini images and code execution) are recognized on the same frame and kept in place.
    • Freeform calls that another format wrapped in {"input": …} are unwrapped, as the gateway does.
  • Gaps. request_missing; request_truncated (a .len sidecar longer than the stored body, or JSON that does not parse); response_missing (2xx with bytes received but no body file); response_truncated (decoded as far as it goes); response_unreadable (a body that is not this upstream's answer). A failed request (non-2xx) has no output and no gap, since the failure is already in TurnView. A request cancelled before its first byte has neither.
  • Masking. Every string goes through tw_secret::mask_body after decoding, at the end. That is stricter than masking the raw JSON: a key right after a \n escape is masked too. Image data and reasoning signatures never leave.
  • Unknown session: 404 with control.session_not_found, the same code /sessions/{id} uses. No new message codes.
  • Locking. Rows are read under the recorder lock. Bodies are read without it on spawn_blocking, as in content search.

How it was verified

  • cargo fmt --all -- --check: clean
  • cargo clippy --workspace --all-targets -- -D warnings: clean
  • env -u HTTP_PROXY -u HTTPS_PROXY -u http_proxy -u https_proxy cargo test --workspace --no-fail-fast: 2064 passed, 0 failed, 6 ignored (1 new: the cost test below)
  • TypeScript, the CI steps: cargo clippy -p tw-api --all-targets --features ts -- -D warnings, cargo test -p tw-api --features ts (a new test pins the exported declarations), export_ts, then tsc --noEmit --strict --target es2022: clean
  • scripts/smoke.sh: 69 passed, 0 failed, plus the existing memory warning. The new check reads every session the run creates on the real binary: 3 sessions, 6 turns. Turn ids match /sessions/{id}, no secret appears, and the Bedrock eventstream answer comes back as bedrock-stream text.
  • New tests in tw-store (17) and tw-control (3):
    • formats: a Claude Code session (cache markers moving each turn, signed reasoning, a later request with a changed signature and one with reasoning stripped, tool_use/tool_result, a screenshot inside a tool result); OpenAI Chat with tool_calls/tool and a mid-conversation system message, streamed and whole; Codex on Responses (instructions plus developer message, reasoning items, function_call/function_call_output, custom_tool_call apply_patch, a hosted web_search_call); Gemini functionCall/functionResponse without ids
    • chain: restart after compaction and after an edited history; system prompt change; a count_tokens call in the middle; a failed turn and its retry
    • gaps: missing and truncated requests, with the restart after them; missing, truncated and unreadable answers; a client that left before the first byte
    • masking: keys in a tool result, a tool input, the system prompt and the answer
    • cross-format: client Anthropic answered by Chat and by Responses, including call-id repair; Codex answered by Anthropic (freeform unwrap); Bedrock streamed and whole; a Gemini JSON-array answer, whole and cut
    • endpoint: the exact JSON for every part kind, the 404, a session whose bodies are gone
  • Cost (cargo test --release -p tw-store transcript_cost -- --ignored --nocapture): a 300-turn Claude Code-like session, 367 MB of bodies (1.2 MB a request on average), builds in 0.69–0.74 s, about 2.3 ms a request. The JSON is 1.5 MB. Each body is parsed once and dropped after its turn; only per-message fingerprints and the system prompt carry over.

Notes for review

  • TranscriptTurn.id is a string, as the desktop app asked, while TurnView.id is a number.
  • Sub-agents get their own sessions, so a sub-agent's work is not in the parent's transcript. Sessions are keyed on the system prompt and first user message, and a sub-agent's differ.
  • A side request that shares the fingerprint but does not extend the history (same system prompt and first message, different tail) makes that turn and the next restart. Only the previous readable request is compared, as specified.
  • Long requests. A request over the 4 MiB body cap is request_truncated, because its new messages are at the end that was cut. Answers over the current 256 KiB response cap are response_truncated and show what fits.

🤖 Generated with Claude Code

GET /sessions/{id}/transcript turns a session's stored request and
response bodies into the conversation the desktop app shows: per turn,
the messages new in that request, the answer decoded from the stored
response, and what could not be shown (gaps). Control-plane protocol 32.

Clients resend the whole history every turn, so a request's new messages
are what is left after the previous readable request's messages,
compared by fingerprint with cache_control, signatures, key order and
reasoning ignored (some clients drop earlier reasoning from the
history). The first leftover assistant message is the previous turn's
answer and is dropped, unless that answer could not be read in full.
No prefix match, or an unreadable request before it, restarts with the
whole history.

Requests are read from the client's own JSON instead of tw-dialect's IR:
the IR drops exactly what a transcript needs (where a system message
sits, tool-only messages, server-tool blocks, files). Answers reuse
tw-dialect's per-format stream parsers, and the blocks those skip are
recognized on the same frame. Every string is masked with mask_body on
the way out; images carry only their type and size.

300 turns at about 1.2 MB a request build in about 0.7 s (release).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit dc0f88e into main Oct 2, 2026
4 checks passed
@fylorn
fylorn deleted the session-transcript branch October 2, 2026 08:43
fylorn added a commit that referenced this pull request Oct 3, 2026
Release for #251, #252, #263, #265, #268 and #272, with the pre-release
fixes in #273.

- Bump the workspace version to 0.58.0
- release-notes/0.58.0.md

The control-plane protocol (34) and the request store schema (25) are
already at their release values on main and do not change here.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant