feat(core): persist file history snapshots for cross-session /rewind (T2.1) (#4897)

* feat(core): persist file history snapshots to JSONL for cross-session /rewind (T2.1)

File history snapshots were purely in-memory — lost on process exit, making
/rewind unusable after session resume. This adds JSONL persistence so restored
sessions can rewind to any pre-resume turn.

Key changes:
- Serialize/deserialize FileHistorySnapshot to/from JSONL system records
- Record each snapshot after makeSnapshot succeeds (incremental per turn)
- Re-record surviving snapshots after rewind (full batch on active branch)
- Parse file_history_snapshot records in sessionService.loadSession()
- Restore snapshot chain in config.getFileHistoryService() on resume
- Copy backup files on session fork (hard link with copy fallback)
- Add session_resume capability tag (stable alias for unstable_session_resume)
- validateRestoredSnapshots with dedup + batched parallel stat

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix: address Copilot review — last-wins dedup + isEnabled guard

- Change snapshot dedup from first-wins to last-wins so rewind batch
  records (which contain the most up-to-date snapshot state) override
  earlier incremental records for the same promptId.
- Guard validateRestoredSnapshots behind isEnabled() to skip I/O
  when file checkpointing is disabled.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix: address wenshao review — ghost snapshots + test assertion

- Session.ts: slice snapshots to targetTurnIndex+1 to exclude
  turns being discarded (fixes ghost-snapshot persistence)
- AppContainer.tsx: only pass snapshots when file restore succeeded
  (avoids writing un-truncated snapshots for conversation-only rewind)
- Session.test.ts: update rewindRecording assertion to expect 3 args

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix: address wenshao R2 — slice snapshots in AppContainer + add deserialize warning

- AppContainer.tsx: use .slice(0, targetTurnIndex + 1) to match
  Session.ts behavior (prevents ghost snapshots for conversation-only rewind)
- sessionService.ts: log warning instead of silent continue on malformed
  file_history_snapshot deserialization

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix: enable daemon file checkpointing + seed TUI promptCount on resume

Two fixes from wenshao's local verification:

1. ACP daemon sessions had fileCheckpointingEnabled=false because
   stdin is a pipe (non-TTY). Add enableFileCheckpointing() to Config
   and call it in acpAgent.newSessionConfig so daemon /rewind works.

2. TUI prompt counter restarted at 0 on --resume, colliding with
   restored snapshot promptIds and corrupting the chain via last-wins
   dedup. Seed promptCount from the resumed conversation's user turn
   count so new promptIds don't overlap.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix: add makeSnapshot + recordFileHistorySnapshot to ACP prompt path

ACP sessions drive the chat through Session.prompt → GeminiChat,
bypassing GeminiClient.sendMessageStream where makeSnapshot lives.
This meant daemon-created sessions never produced file history
snapshots, leaving /rewind non-functional.

Add makeSnapshot + recordFileHistorySnapshot at the start of each
ACP prompt turn (mirroring client.ts:1488), using the existing
sessionId########turn promptId format.

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix: add session_resume to integration test capability assertion

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix: add debug logging to ACP makeSnapshot catch blocks

🤖 Generated with [Qwen Code](https://github.com/QwenLM/qwen-code)

* fix(acp): move makeSnapshot after slash-command and hook checks

Locally handled slash commands (/help, /memory, etc.) previously
created file history snapshots even though no model turn was added.
This caused the snapshot index to drift from the real user turn count,
breaking rewind in web-shell sessions that use slash commands frequently.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: address qwen-code-ci-bot review round 5

- Remove stale line number from comment (Session.ts)
- Single cast instead of double cast for systemPayload (sessionService.ts)
- Guard mkdir in copyFileHistoryBackups to prevent fork failure (sessionService.ts)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: address wenshao review — mock, ordering guard, JSDoc placement

- Add makeSnapshot/rewind to FileHistoryService mock in Session.test.ts
- Invalidate cached FileHistoryService on enableFileCheckpointing() to
  prevent stale enabled=false if service was lazily created first
- Move copyFileHistoryBackups above class JSDoc to fix association

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: truncate in-memory snapshots on ACP rewind + deduplicate recordFileHistorySnapshot

- Call restoreFromSnapshots(survivingSnapshots) in ACP rewindToTurn to
  prevent phantom snapshots from accumulating in the in-memory array
  after conversation-only rewind
- Simplify recordFileHistorySnapshot to delegate to the batch variant

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: add restoreFromSnapshots to FileHistoryService mock

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
jinye 2026-06-12 13:50:16 +08:00 committed by GitHub
parent 963fc543d1
commit 246a0a1fc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 375 additions and 19 deletions

View file

@ -193,6 +193,7 @@ describe('qwen serve — capabilities envelope', () => {
'session_create',
'session_scope_override',
'session_load',
'session_resume',
'unstable_session_resume',
'session_list',
'session_prompt',