Files
deepseek-harness/docs/rfc/implemented/architecture/2026-06-11-dev-invariants-over-deep-readonly.md
T
Tianyi Cui e6fad266a6 docs(rfc): define and enforce a uniform RFC format; adopt it across the corpus
Define the in-file RFC contract in docs/rfc/README.md § The file format:
the header block (`# RFC: <title>` plus a dateless Status enum
cross-checked against the lifecycle folder), the per-lifecycle body
skeleton (a Problem opener everywhere; Proposal/Alternatives considered/
Acceptance criteria/Risks in proposed/; present-tense Decision/
Consequences with proposal-era headings banned in implemented/; the
frozen proposal shape in rejected/), and a mandatory Alternatives
considered section with a date-fenced grandfather comment for pre-format
RFCs whose alternatives are not reconstructible from the record.

Enforce it with a new doc-sync gate, scripts/verify-rfc-format.ts, and
normalize all 112 RFCs to it: ~15 Status-line spellings collapse to the
enum, 29 Context openers become Problem, the 39 legacy-format XXX debt
markers are resolved and banned from reappearing, proposal-era sections
in implemented RFCs are rewritten to shipped reality (including the
web/fs/subagent seam RFCs' migration plans and test checklists, closing
the doc-tiers deferred-work item on the web seam), every RFC gains an
Alternatives considered section or the grandfather comment, and the
bilingual pair is re-mirrored and re-recorded.

Move the generated index tables out of README.md into a fully generated
docs/rfc/INDEX.md — gen-rfc-index now writes the whole file, and
verify-rfc-classification checks its freshness and rejects index-shaped
rows in the curated README — which makes room for the format contract to
live in the README front door instead of a separate FORMAT.md.

The decision record, and the first RFC written in the new format, is
docs/rfc/implemented/process/2026-07-05-uniform-rfc-format.md.
2026-07-05 22:58:25 +08:00

3.2 KiB

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

Status: implemented

Problem

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 tool-execution pipeline step ends the turn), and both idle→disposed and running→disposed are legal.

Alternatives considered

The pervasive DeepReadonly<T> type flip (the rejected proposal) — compile-time only (a plugin casts straight through it), high type-noise across every log/message consumer and adapter, and it 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.