Files
deepseek-harness/docs/rfc/implemented/2026-06-11-dev-invariants-over-deep-readonly.md
T
Tianyi Cui 7c400e9c02 docs: unify ADR/RFC trees into one lifecycle-organized RFC tree
Collapse docs/adr/ and docs/rfc/ into a single docs/rfc/ with proposed/,
implemented/, and rejected/ subfolders. Every file is renamed to
yyyy-mm-dd-topic-title.md, where the date is when the topic was first
proposed (from git history). ADRs and RFCs that covered exactly the same
topic are merged (property-based testing, session persistence); the
umbrella RFC 005 stays split across its three implemented decisions, and
RFC 006's deferred part-3 (API extractor reports) splits into its own
proposed RFC. All cross-references become machine-checkable relative
links instead of bare "ADR NNNN" / "RFC NNN" prose.

Add a verify-md-links doc-sync gate (scripts/verify-md-links.ts) that
checks every relative Markdown cross-link resolves, wired into doc-sync
alongside verify-md-wrap. This makes the reorganization self-verifying:
the same change that rewrote ~forty inter-doc links adds the check that
proves none dangle. Document the cross-link convention in a new
docs/AGENTS.md and record the gate as an implemented RFC.

doc-sync, typecheck, lint, and the full test suite (667) all pass.
2026-06-18 02:18:24 +08:00

3.1 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.