Repository navigation
fix(storage): append review state without rewriting history - #1956
Conversation
Persist review rollback state as a small transcript mutation instead of materializing and replacing the complete session and message index. Cache proven message identities with the layout so historical card lookup reads only its selected record. Invalidate unknown generations, preserve message positions, and replay state across windows and session mutations. Refs vastsa#1836
There was a problem hiding this comment.
🔵 Needs a closer look
Independent Windows validation is still needed for append recovery, change-time generation, restart behavior, file timestamps, and cache invalidation because the supplied large-session evidence is macOS-only.
No open issues 🎉
What changed in this PR
This pull request changes review-state rollback from rewriting the full transcript to appending targeted tool_review_state records, preserving message positions, counts, and turn ownership while reducing large-session I/O.
Changes:
- Adds append-only review-state persistence keyed by
messageIdandsnapshotId, with replay across full, paginated, and nested-tool reads. - Upgrades transcript layout caching to v3 with message identities, state offsets, generation validation, and recovery handling.
- Adds Rust regression tests, Host E2E coverage, storage specifications, an ADR, and updated test-plan documentation.
| File | Description |
|---|---|
scripts/e2e-transcript-cache.mjs |
Updates cache-format assertions for layout cache v3. |
scripts/e2e-review-state-persistence.mjs |
Adds large-transcript rollback, restart, prefix-integrity, and duplicate-append E2E coverage. |
package.json |
Registers the review-state persistence E2E command. |
docs/spec/06-delivery/04-e2e-test-plan.md |
Documents review-state persistence scenarios and acceptance criteria. |
docs/spec/03-runtime/04-data-storage.md |
Specifies append-only review-state records and layout-cache v3 behavior. |
docs/adr/README.md |
Adds the append-only review-state ADR to the index. |
docs/adr/append-only-review-state.md |
Records the design decision, cache strategy, compatibility, and consequences. |
crates/host-core/src/transcripts/review_state_tests.rs |
Tests state replay, cache behavior, torn tails, duplicate IDs, and rewrite handling. |
crates/host-core/src/transcripts/layout.rs |
Adds layout-cache v3 identities, state offsets, generation validation, and incremental append handling. |
crates/host-core/src/transcripts/binary_integrity.rs |
Exposes Windows change-time retrieval for generation tracking. |
crates/host-core/src/transcripts.rs |
Implements state-record serialization, append logic, replay, pagination support, and nested-tool integration. |
crates/host-core/src/sessions.rs |
Replaces full review-message rewrites with targeted append operations and adds lifecycle regression tests. |
This pull request requires changes.
Review effort: Lite
muzimu217
left a comment
There was a problem hiding this comment.
Verified the premise on current main: update_tool_review_state (crates/host-core/src/sessions.rs:3054) reads the transcript and calls replace_messages (around :3090), which is the same full-rewrite path regenerate uses. So a single rollback toggle on a large session rewrites the whole body and rebuilds every index — this is the same class of cost as #1836, and removing it is worth doing on its own merits, not only for the toggle.
The append design is the right shape: a (message_id, snapshot_id) keyed record, latest wins on read (apply_tool_review_states), message position/count/turn attribution untouched, and repeated rollback not appending duplicates. Keeping the original row authoritative and layering state on top is what makes the rewrite unnecessary without changing what readers see.
Three things I would want pinned in the description or the tests, because they are where append-only state usually breaks:
-
What happens to the state records on the rewrite paths that still exist. Regenerate and truncate still call
rewrite_transcript; if that path does not carry the appended records forward, a rollback silently reverts after the next regenerate. The test nametool_review_state_survives_fork_revision_activation_and_deletionsuggests this is covered — naming the exact function that preserves them would make it reviewable without reading the test. -
Ordering under a same-key repeat.
latestis built from the file order, so this relies on the append being strictly ordered on disk. If two toggles land within the same timestamp granularity, is the later record still last in the file (not sorted by timestamp)? A line in the ADR saying "file order is the order of record" would settle it. -
Cache generation with equal-length appends. The description says an equal-length edit followed by an append forces a rebuild, which is the hard case; a test that appends a record whose serialized length happens to match the previous one, then reads back, would pin it. If that case is already in
review_state_tests.rs, ignore this.
The layout cache v3 carrying record offsets rather than re-parsing large messages is the part that makes this a real win rather than a smaller rewrite, and validating file identity plus generation before reusing it is the right guard.
Document the exact read-overlay and serialization path that preserves review state through transcript rewrites and revision archives. Cover retained-prefix truncation and equal-sized state appends through warm, cold, and sequential reads without relying on timestamps. Refs vastsa#1836
|
@muzimu217 Addressed the three review points in the ADR, PR description, and regression coverage:
The follow-up changes only tests and documentation. Both targeted suites pass: 84 transcript tests and 131 session tests. Formatting, diff checks, and all 596 documentation-page checks pass. |
vastsa
left a comment
There was a problem hiding this comment.
Confirmed the review-state full-rewrite trigger in #1836 and verified this PR removes that path while preserving message positions, replay, and rewrite folding. On the current main integration tree: 84 transcript tests, 132 session tests, formatting, the 384 MiB Host rollback/restart E2E, and the transcript-cache E2E passed. The large E2E retained the original prefix and appended 129 bytes. Clippy completed with one existing filter(...).next_back() warning. The untested Windows-specific file-generation behavior remains a platform limitation of this local review. Thanks for the careful fix and regression coverage.
Rolling back a historical Write/Edit review card loaded the entire session, rewrote its transcript, and rebuilt every message index row just to persist
details.review.state. The regression test reproduces that full-rewrite path on the original implementation.Persist the change as a small append-only
tool_review_staterecord keyed by(messageId, snapshotId). Full, paginated, and nested-tool reads apply the latest state without moving the original message or changing its count or turn ownership. Repeated rollback acknowledgements append nothing.Layout cache v3 stores message identities and state offsets, so historical-card lookup reads only the selected message after the layout is built. Reuse requires matching file identity and modification/change-time generation; only a Host-owned append from the proven generation advances the cached prefix. Unknown mutations and lagging disk caches are rebuilt.
Review invariants
truncate_fromcallsget_session, whose full read reachesread_transcript_windowandapply_tool_review_statesbefore the cut.rewrite_transcript_bodythen usesrecords_and_texts/ui_to_recordandwrite_transcript_with_compactionsto fold the effective state into surviving canonical messages. Discarded messages reacharchive_discarded_regenerate_branchandsave_message_revision/refresh_message_revisionwith that state already applied.truncate_from_folds_review_state_into_the_retained_prefixverifies that the state log disappears while the surviving tool record retainsrolledBack. The existingtool_review_state_survives_fork_revision_activation_and_deletioncovers the archived branch and subsequent lifecycle.append_linewrites and syncs each complete line, and readers apply state in increasing byte-offset order. The last record for a key wins independently of clock precision. The ADR now states this explicitly.equal_length_state_appends_replay_in_file_order_across_cached_readspads valid JSON whitespace soactiveandrolledBackrecords have exactly the same serialized length. It verifies that each append grows the file by that length, adds a distinct state offset, and returns the latest state through warm, cold, and sequential reads. This complements the existing equal-length in-place-edit-then-append invalidation regression.Validation
cargo test -p host-core transcripts::: 84 passed.cargo test -p host-core sessions::: 131 passed.node docs/scripts/check-docs.mjs: passed (596 pages).scripts/e2e-review-state-persistence.mjs --largeandscripts/e2e-transcript-cache.mjscover rollback, original-prefix integrity, restart/retry, paging, legacy cache recovery, navigation, Task rewrites, and invalid UTF-8.chunks_exactsuggestions intools/hashline/tag.rsand the transcript'sfilter(...).next_back()suggestion. Consequently local-D warningsremains failing; no suppression rules were added.Scope and compatibility
This fixes the review-state full-rewrite trigger in #1836 and its historical-message lookup cost. Deliberate history replacement, cold-start storage-integrity verification, and RPC fairness retain their existing behavior. Windows CI is separate from replaying the reported native WM_NULL/UI timeout, which has not been measured here.
No SQLite or RPC schema change is required. Older apps that ignore the additive state line may display the previous card state; snapshot metadata still prevents repeated destructive rollback. Intentional rewrites fold the effective state into the canonical message payload. The storage spec, ADR, and E2E scenario document these rules.
Refs #1836