Files
deepseek-harness/docs/rfc/implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md
T
Tianyi Cui 605587e79c docs(rfc): classify RFCs by kind via path-encoded subdirectories
Add a second axis to every RFC — its class (feature, bug-fix,
simplification, architecture, process, testing) — encoded in the path
as docs/rfc/{lifecycle}/{class}/file.md. The folder is the label, so
the closed set is enforced by structure rather than a parsed field.

Two new doc-sync gates back it:
- verify-rfc-classification: every RFC sits in a valid class folder and
  the README index lists it under the matching lifecycle→class heading.
- verify-doc-refs: every docs/*.md path cited in a packages|examples TS
  comment resolves — closes a drift class verify-md-links can't see, and
  catches the four comment refs this reorg moved.

The README gains a Classification section explaining the taxonomy and
per-class index sub-sections. A self-referential process RFC records why
the scheme is path-encoded and gated.
2026-06-20 22:29:45 +08:00

3.2 KiB

RFC: Dev-mode invariants over compile-time deep-readonly

Status: implemented (accepted 2026-06-13)

Context

The session log is append-only by contract, but the types don't enforce it: session.events returns readonly SessionEvent[] whose elements are mutable, and deriveMessages() handed the logged content arrays/blocks out by reference. The loop then passes those derived messages into the agent/request waterfall and on to adapters, where mutating the request is sanctioned — so a request middleware could reach back and rewrite history, silently breaking replay equivalence and the derived-history guarantee. Separately, the event taxonomy (turn/step nesting, seq monotonicity, tool-call/result pairing, legal status transitions) was asserted only where individual tests happened to look.

Two ways to defend the log: make immutability part of the type (DeepReadonly<SessionEvent> on the way out), or catch corruption at runtime in dev. The runtime-validation proposal took the runtime route; the deep-readonly proposal took the type route.

Decision

Reject the pervasive DeepReadonly<T> type flip. Instead:

  1. Always-on: deriveMessages() deep-clones the content it emits (one structuredClone per derived message). In-flight mutation of a request can no longer reach the log — this is the real fix, and it costs nothing meaningful next to a model call.
  2. Dev-mode: a new dsh-invariants plugin (pure listeners, off in production, on in tests and demos) asserts the event contract and Object.freezes logged event data so any other code that mutates a logged event throws instead of corrupting silently. Seeded sessions are frozen and checked on session/created (the constructor copies the seed without emitting session/event).

The invariants encode the real contract, not an idealized one: a tool/call may have no tool/result (a thrown tools/execute waterfall ends the step), and both idle→disposed and running→disposed are legal.

DeepReadonly was rejected because it is compile-time only (a plugin casts straight through it), high type-noise across every log/message consumer and adapter, and would force readonly types through code where mutation is the sanctioned API. The clone draws the mutable/immutable boundary exactly at "logged vs in-flight" without any of that noise.

Consequences

  • History corruption is caught loudly in tests and demos, at zero production cost and zero type noise. The trade-off is that the guarantee is dynamic (a dev-mode tripwire) rather than static.
  • The invariants plugin doubles as executable documentation of the event taxonomy — the assertions are the contract.
  • Session.events keeps its readonly SessionEvent[] type; no consumer churn.
  • This folds in the deep-readonly proposal — there is no separate deep-readonly record; this records the decision to not pursue that approach. InvariantError is a plain Error with a code for now; a later taxonomy change can promote it.