Skip to content

fix(desktop): acknowledge revision roots without replaying old messages - #1922

Merged
vastsa merged 4 commits into
vastsa:mainfrom
Seraph-x:fix/revision-root-replay-1870
Oct 28, 2026
Merged

vastsa merged 4 commits into
vastsa:mainfrom
Seraph-x:fix/revision-root-replay-1870

Conversation

@Seraph-x

@Seraph-x Seraph-x commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

After regenerating an older prompt, later turn completion can append that prompt after the latest reply when its root is outside the renderer's loaded history window. session.saveActiveRevision returns the most recent revision-bearing root, and Main currently broadcasts it as a new message_end; the renderer upserts an unknown ID at the tail.

Send the existing user_message_persisted acknowledgement with the root's current message ID instead. It updates the pager only where that exact user row is already present in the active transcript, retained pane, or session cache. An unloaded or subsequently deleted root stays absent. If a background session has no projection at all, the acknowledgement leaves both its cache and live marker absent so later prefetch still reads the Host. Normal user-message delivery still appends, and revision families and stored branches remain intact.

The reported 3,484 − 2,829 = 655 arithmetic is reproduced by the live branch's suffix length; current main does not use message_count to infer a root. This change fixes the reproduced event-to-projection failure without deleting historical revisions or filtering messages by timestamp.

The review identified a cache edge case in the existing acknowledgement handler: an unloaded background session could gain an empty cache entry and skip later prefetch. A new red test confirmed it. The handler now reconciles only when an active, retained, or cached projection exists; both the regression test and real-Host E2E assert that subsequent prefetch actually reads durable history.

Validation

  • The original code fails 6 of the initial 8 projection regression cases; the real-Host scenario also reproduces the stale root append before the fix.
  • The 90 targeted Desktop tests pass in three consecutive runs after the review correction.
  • The real-Host E2E passes in three consecutive runs: 3,484 initial messages, root at seq 655, a 2,829-message branch, newest 100-message window, foreground/background projections, a fully unloaded background session followed by a real Host prefetch, three follow-up turns, restart, and complete revision 1 → 2 restoration (3,490 final messages).
  • Desktop production build and typecheck, repository lint, architecture, agent-policy, documentation, and diff checks pass.
  • Full Desktop suite after the review correction: 4,430 passed, 3 skipped, 0 failed (run from apps/desktop).
  • All 9 new regression cases also pass in Electron's bundled Node 24.20.0 runtime.

Local validation uses macOS arm64 and Node 26.12.1. The E2E drives real Host RPC, Main persistence, and renderer projection code using synthetic messages in a temporary profile; it does not call a provider or run the full Desktop GUI. The Windows release installer has not been tested locally.

CI status

All six checks pass on current head 678a05fc854abe4b3e093928e9b4a7201ef15d93:

  • JS build / typecheck / lint / architecture / tests.
  • Rust host-core format / lint / tests.
  • Windows host-core / transcript cache E2E.
  • Trusted extensions Electron E2E, including the provider optional client-certificate and transcript rendering/disclosure scenarios.
  • Documentation checks.
  • Head contains latest base.

Runs: CI, Docs, PR base. GitHub merge candidate 77ac79ca1fccbd8267b647541b7f24fd59107740 has the same complete Git tree as the tested head.

Historical failure: on the initial commit, the Electron certificate fixture hung and was killed after 65 seconds. Its complete build source graph (191 project files) was identical to origin/main; no certificate or proxy code was changed for this PR. The current-head Electron job completes successfully. No failed check is counted as a pass.

Refs #1870

Copilot AI lite review requested due to automatic review settings October 27, 2026 12:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new acknowledgement can create an empty cache entry for unloaded background sessions, causing prefetch to skip loading their transcripts and leaving the session state incorrect.

1 open issue
What changed in this PR

This PR prevents regenerated historical user prompts from being appended to the end of the renderer transcript. It uses an exact-row persistence acknowledgement while preserving revision history, normal message delivery, and bounded-history behavior.

Changes:

  • Replaces archive-triggered message_end events with user_message_persisted acknowledgements.
  • Adds projection regression tests and a real-Host end-to-end scenario.
  • Documents the loaded-history semantics and records the fix in unreleased changes.
  • Review note: Background sessions with no retained or cached transcript may still create an empty live cache entry through the acknowledgement handler, causing later prefetching to skip loading that session. The handler should reconcile only existing projections or avoid caching when all projections are absent.
File Description
scripts/​e2e-regenerate-root.mjs Exercises the real Host, persistence, and renderer projection flow with a large synthetic transcript.
docs/​spec/​06-delivery/​04-e2e-test-plan.md Documents the bounded-history regression scenario and validation command.
docs/​spec/​03-runtime/​04-data-storage.md Defines acknowledgement and loaded-history boundary semantics.
docs/​project/​unreleased.md Records the user-visible stale-root projection fix.
apps/​desktop/​test/​regenerate-root-projection.test.mjs Covers active, background, cached, unloaded, deleted, and ordinary message projection behavior.
apps/​desktop/​electron/​main/​runtime/​event-persistence.ts Emits in-place persistence acknowledgements for archived revision roots.

Review effort: Lite

sessionId: envelope.sessionId,
ts: Date.now(),
event: { type: "message_end", message: root },
event: { type: "user_message_persisted", optimisticMessageId: root.id, message: root },

@muzimu217 muzimu217 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event swap is the right fix, and it is right for a reason worth naming: message_end is a terminal append for a row the renderer is expected to have just streamed, so its handler has to materialize an unknown id somewhere — and for a revision root outside the loaded window, "somewhere" is the tail. user_message_persisted is a pager stamp, and reconcilePersistedUserMessage returns the array untouched unless the exact optimistic id is already a user row (lib/session-transcript.ts:148-157), so an unloaded or deleted root stays absent by construction rather than by a new special case.

The early return is a second fix and I would call it out in the description: previously a session with no projection fell through to reconcile([]) and got an empty entry cached, which makes a background session look loaded and suppresses the later prefetch that would have read the Host. Returning before touching the cache keeps "no projection" meaning no projection.

I also checked that skipping liveSessionTranscripts.add on that early return does not strand a background session: the set is also populated from the running-state transition (session-runtime.ts:209) and from the other event paths (events-slice.ts:138, :309), so a live turn still marks its session live before any revision acknowledgement arrives.

Driving the real event-persistence and events-slice with only the host RPC stubbed is the correct boundary for this bug — the projection logic is what was wrong, and a mocked store would have hidden exactly the tail-upsert behavior being fixed.

@vastsa
vastsa merged commit 47f38b7 into vastsa:main Oct 28, 2026
2 checks passed
@Seraph-x
Seraph-x deleted the fix/revision-root-replay-1870 branch October 28, 2026 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants