ADR 0021doc/{zh,en}/adr/0021-session-state-and-content-version.md

ADR 0021: Runtime Session State and Session Content Version

Status: accepted. Part 1 was built in slice 4 of the context refactor and part 2 in slice 7 (2026-12-03).

Status: accepted. Part 1 was built in slice 4 of the context refactor and part 2 in slice 7 (2026-12-03).

Context

The context refactor has the agent only append to its transcript across turns. The transcript belongs to the agent, but the agent is a plugin process that keeps no memory across runs, so the Host must store it on the agent's behalf. Today the Host can store a Plan and each round's Data for one goal (ADR 0014); there is no channel to store runtime state per session.

Removal (RemoveMessage) and forgetting (Forget) empty an entry's body and mark it with a tombstone (internal/host/sessions/remove.go:45-52, forget.go), but record no version. A parked run that resumes restores its messages from its own checkpoint (plugins/agent/agent.go:109-123), so removed content is still sent to the model and may be carried out by a later summary, commit or outbound send. session-contracts.md says "running and parked runs check content_version before every model call"; the code has neither the field nor the check. The transcript widens this gap: the transcript is itself a copy of the session's content.

Decision

Part 1: Opaque Session State

  1. RunInput.SessionState (bytes, may be empty) and Result.SessionState (bytes, may be empty).
  2. The Host keeps one value per (session, runtime instance) in a session_states table: when a run ends successfully and returns a non-empty value, it is replaced in the same transaction that commits the run's terminal state; a failed, cancelled or superseded run does not change it.
  3. The Host does not parse the content and only limits its size (8 MiB by default; above that it is not stored and session.state_too_large is recorded); the value is deleted together with the session's entries (/new, idle reset, tenant data deletion, forgetting a person).
  4. A session runs only one run at a time (sessions are already serial), so no concurrent merge is needed.

Part 2: Session Content Version

  1. sessions.content_version (integer, initially 0). RemoveMessage and Forget increment it, for every session whose entries they blank, in the same transaction that empties the body.
  2. At the start of each attempt the Host puts the current version in RunInput.ContentVersion.
  3. A runtime that rebuilds its model context declares rebuilds: true in its manifest (the agent does). Every model request of such a runtime names the version its messages were built from (ChatRequest.ContentVersion), and the Host refuses one built from another version with error code session.content_changed, whose data.content_version is the current version. The agent then builds its earlier turns and its start again from the entries, where the removed content is already empty, keeps the run's own steps so no committed tool call runs again, and goes on with the new version. It saves the rebuilt run once, with its steps, so a pause or a restart cannot resume without them; a resumed run rebuilds with the arguments of the skills the person invoked (built 2026-12-05, review #264 B7 and H20). A run resumed after content was removed while it waited, parked or across a restart, rebuilds before anything else: it does not return the answer it committed before, nor make the calls it had not made, which get a not-run result; the model is asked again (built 2026-12-05, review #264 H17). Steps already compacted in the run may hold what was removed: the agent then returns the refusal and the run ends superseded.
  4. A removal supersedes, in its own transaction, each run in flight in the session that the removed message started, and every run in flight there on a runtime without rebuilds (the owner, 2026-12-03). A superseded run ends in the new terminal state superseded (已作废): it sends a one-line notice that a message it used was removed, never its reply; its committed tool ledger stays, and its pending question is closed, so the person's next message starts a new run.
  5. The Host also checks, against the version of the run's last accepted model call (or its start): the delivery of its final reply, which is superseded instead when content was removed after it (checked again in the transaction that records the run, with a stop asked for meanwhile, and before a goal judges the round: built 2026-12-05, review #264 B4 and H17); a commit that writes a summary (compaction or digest); and a message the runtime sends itself (Send). A refused summary or send makes the agent rebuild first, the summary its start writes included; a summary not yet stored is dropped, and the rebuilt start makes its own (built 2026-12-05, review #264 H20).
  6. What a run produced goes with the message that started it: tool_result(res:<run>/…) of that run is refused with tool.result_removed, the files sent with that message can no longer be opened, and each workspace provider the run used deletes what it kept of the run's calls, such as a long result's whole output (plugin.OutputForgetter; the owner, 2026-12-03). The history the agent builds from the entries keeps that run's reply, which people saw, but not its reasoning, its calls' arguments or its results; a run that rebuilds keeps its own steps but not their reasoning, which may quote what was removed. Files the agent wrote into the workspace stay. Each workspace provider also deletes its copies of the files sent with the message (plugin.AttachmentForgetter, added 2026-12-05 after review #264).
  7. /recall [N] removes the sender's message N back in the conversation (1, the default, is the latest), as the platform's own recall does, also while a question waits (the owner, 2026-12-03).

Consequences

  • ◆New table session_states, new column sessions.content_version, new terminal state superseded: one migration each; superseded reaches every consumer of run states (CLI, TUI, Web, trace, statistics), with Chinese and English text updated together.
  • ◆The new RunInput and Result fields are backward compatible: a runtime that returns no SessionState behaves as before. External runtimes (Claude Code, Codex) do not use it.
  • ◆One write of the value per turn: about 2 MB at a 419k-token compaction trigger, at most 8 MiB; acceptable at today's turn rate in private chats.
  • ◆Thread copies that external runtimes keep locally (Codex and Claude Code session files) are outside the version check; after a removal the Host no longer reuses the old external thread (built 2026-12-05: the external runtime keys its thread by the content version, review #264 B5); cleaning up the local copies is decided together with version binding for external runtimes (next section).
  • ◆New manifest field rebuilds, directive /recall and optional workspace provider interfaces OutputForgetter and AttachmentForgetter (the latter added 2026-12-05).
  • ◆Not built: a memory a run saved from a message that is later recalled stays (a recall does not tell plugins, as a forget's user.forgotten does); a session_scope: sender session that reads a shared group log does not record the group session's version; progress updates are not checked (a superseded run's notice replaces its progress message); a reply enqueued before a removal and retried after it is sent.
  • ◆This is the twenty-first ADR, past the v1 guideline of about ten; it is the only Host kernel change in the refactor.

Not decided here

Per-viewer transcripts for shared group sessions (the owner chose one shared history instead on 2026-12-05: sessions, section on visibility); version binding of external runtimes' threads and cleanup of their local copies; authorization for reading back tool screenshots across runs.