Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(turnrewind): skip dead snapshot refs when picking the undo target

yingtianlandc537170d6ad
diff --git a/plugins/dsh-tauri-turnrewind/README.md b/plugins/dsh-tauri-turnrewind/README.md
index 5c590336..0d36e7b1 100644
--- a/plugins/dsh-tauri-turnrewind/README.md
+++ b/plugins/dsh-tauri-turnrewind/README.md
@@ -12,7 +12,7 @@
- 将快照映射记录到 `$DSH_HOME/ledger.sqlite`;
- **git 全程异步执行**:快照、diff、恢复都不阻塞 Host 事件循环;同一会话的捕获/结算按 FIFO 串行,不同会话并行;
- **git 可用性探测**:系统没有 git 时,turn 显式记为 `skipped`(原因 `TURNREWIND_GIT_UNAVAILABLE`),而不是静默失败;
-- **快照链自愈**:私有快照仓库被删/损坏后,下一次捕获自动降级重建基线(日志有一条 warning),后续 turn 照常可撤销;
+- **快照链自愈**:私有快照仓库被删/损坏后,下一次捕获自动降级重建基线(日志有一条 warning),后续 turn 照常可撤销;被清空前留下来的旧 turn 会在 `/undo` 选目标时自动识别为死快照并标记跳过(`snapshot ref missing`),不会甩出 git 原始报错;
- **工作区资格守卫**:家目录、家目录的祖先、盘根目录,以及超过快照预算(文件数 / 总大小 / 单文件大小)的目录不做快照,turn 记为 `skipped` 并向 `/undo` 说明原因(见「工作区资格与快照预算」);
- **不可用弹窗**:客户端半(`lib/client.js`)通过 `turnrewind` 会话投影检测到不可用提示时,在 Web UI 内弹出模态对话框(中英双语、每个浏览器只弹一次);
- 注册人类命令 `/undo`;
diff --git a/plugins/dsh-tauri-turnrewind/lib/core/ledger.js b/plugins/dsh-tauri-turnrewind/lib/core/ledger.js
index f6f1235d..2a7b7919 100644
--- a/plugins/dsh-tauri-turnrewind/lib/core/ledger.js
+++ b/plugins/dsh-tauri-turnrewind/lib/core/ledger.js
@@ -124,6 +124,23 @@ export function skipTurn(db, turn, reason) {
`).run(turn.turnId, turn.sessionId, turn.workspaceKey, turn.startedAt, reason)
}
+export function listReversibleTurns(db, sessionId, workspaceKey) {
+ return db.prepare(`
+ SELECT * FROM turns
+ WHERE session_id = ? AND workspace_key = ?
+ AND reversible = 1
+ AND status IN ('settled', 'interrupted')
+ ORDER BY started_at DESC
+ `).all(sessionId, workspaceKey)
+}
+
+export function markTurnSnapshotMissing(db, turnId) {
+ db.prepare(`
+ UPDATE turns SET reversible = 0, error = 'snapshot ref missing (snapshot repository was wiped)'
+ WHERE turn_id = ?
+ `).run(turnId)
+}
+
export function recordSkippedTurn(db, turn, reason) {
db.exec('BEGIN')
try {
diff --git a/plugins/dsh-tauri-turnrewind/lib/index.js b/plugins/dsh-tauri-turnrewind/lib/index.js
index 8af83331..1ba56a98 100644
--- a/plugins/dsh-tauri-turnrewind/lib/index.js
+++ b/plugins/dsh-tauri-turnrewind/lib/index.js
@@ -3,9 +3,9 @@ import { homedir } from 'node:os'
import { join, resolve } from 'node:path'
import process from 'node:process'
import { createDialogProjection } from './core/dialog-projection.js'
-import { captureSnapshot, createSnapshotStore, currentState, gitAvailable, restorePath, snapshotDiff, stateAt } from './core/git-snapshot.js'
+import { captureSnapshot, createSnapshotStore, currentState, gitAvailable, gitRef, restorePath, snapshotDiff, stateAt } from './core/git-snapshot.js'
import { assessWorkspace } from './core/guard.js'
-import { claimRewindNotices, completeUndoWithNotice, createOperation, failTurn, getLatestSnapshotRef, getLatestTurn, getLatestTurnSummary, getTurn, insertTurn, openLedger, recordSkippedTurn, registerWorkspace, settleInterruptedTurn, settleNoopTurn, settleOperation, settleTurn, skipTurn } from './core/ledger.js'
+import { claimRewindNotices, completeUndoWithNotice, createOperation, failTurn, getLatestSnapshotRef, getLatestTurnSummary, getTurn, insertTurn, listReversibleTurns, markTurnSnapshotMissing, openLedger, recordSkippedTurn, registerWorkspace, settleInterruptedTurn, settleNoopTurn, settleOperation, settleTurn, skipTurn } from './core/ledger.js'
import { classifyUndo } from './core/planner.js'
const name = 'dsh-tauri-turnrewind'
@@ -154,6 +154,15 @@ async function settleSessionTurns(ledger, active, sessionId, exceptKey, reason)
}
}
+async function turnRefsExist(store, turn) {
+ if (!turn.before_ref || !turn.after_ref)
+ return false
+ return Boolean(
+ await gitRef(store.repoDir, store.workspaceDir, turn.before_ref)
+ && await gitRef(store.repoDir, store.workspaceDir, turn.after_ref),
+ )
+}
+
async function applyUndo(runtime, active, invocation) {
const parsed = parseUndoInput(invocation.rawInput)
if (parsed.error)
@@ -169,9 +178,27 @@ async function applyUndo(runtime, active, invocation) {
if (runtime.undoing)
return { kind: 'error', text: 'Another undo operation is already running in this workspace.' }
- const target = parsed.turnId
- ? getTurn(runtime.db, parsed.turnId)
- : getLatestTurn(runtime.db, invocation.agent.session.id, workspaceKey)
+ let target
+ if (parsed.turnId) {
+ target = getTurn(runtime.db, parsed.turnId)
+ if (target && !await turnRefsExist(runtime.store, target)) {
+ return {
+ kind: 'error',
+ text: `The snapshot data for turn ${parsed.turnId} no longer exists (the snapshot repository was previously wiped); its changes can no longer be undone.`,
+ }
+ }
+ }
+ else {
+ // Walk newest-first and skip turns whose snapshot refs died with a wiped
+ // snapshot repository, marking them so later /undo runs never re-check.
+ for (const candidate of listReversibleTurns(runtime.db, invocation.agent.session.id, workspaceKey)) {
+ if (await turnRefsExist(runtime.store, candidate)) {
+ target = candidate
+ break
+ }
+ markTurnSnapshotMissing(runtime.db, candidate.turn_id)
+ }
+ }
if (!target) {
const latest = getLatestTurnSummary(runtime.db, invocation.agent.session.id)
const detail = latest
diff --git a/plugins/dsh-tauri-turnrewind/test/ledger.test.js b/plugins/dsh-tauri-turnrewind/test/ledger.test.js
index c8f59e46..02e4c0e2 100644
--- a/plugins/dsh-tauri-turnrewind/test/ledger.test.js
+++ b/plugins/dsh-tauri-turnrewind/test/ledger.test.js
@@ -3,7 +3,7 @@ import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { it } from 'vitest'
-import { claimRewindNotices, completeUndoWithNotice, getLatestSnapshotRef, getLatestTurn, getTurn, insertTurn, openLedger, queueRewindNotice, recordSkippedTurn, settleInterruptedTurn, settleNoopTurn, settleTurn } from '../lib/core/ledger.js'
+import { claimRewindNotices, completeUndoWithNotice, getLatestSnapshotRef, getLatestTurn, getTurn, insertTurn, listReversibleTurns, markTurnSnapshotMissing, openLedger, queueRewindNotice, recordSkippedTurn, settleInterruptedTurn, settleNoopTurn, settleTurn } from '../lib/core/ledger.js'
it('persists turn lifecycle and resumes from the latest durable snapshot', async () => {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-ledger-test-'))
@@ -243,6 +243,45 @@ it('selects an interrupted turn after a later turn is undone', async () => {
}
})
+it('lists reversible turns newest-first and marks dead snapshots skipped', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-deadref-test-'))
+ const db = openLedger(root)
+ try {
+ for (const [turnId, time] of [
+ ['session:1', '2026-01-01T00:00:00.000Z'],
+ ['session:2', '2026-01-01T00:01:00.000Z'],
+ ['session:3', '2026-01-01T00:02:00.000Z'],
+ ]) {
+ insertTurn(db, {
+ turnId,
+ sessionId: 'session',
+ workspaceKey: 'workspace',
+ startedAt: time,
+ beforeRef: `refs/turnrewind/turn-${turnId}-before`,
+ })
+ settleTurn(db, turnId, `refs/turnrewind/turn-${turnId}-after`)
+ }
+ completeUndoWithNotice(db, 'session:3', {
+ noticeId: 'notice-3',
+ sessionId: 'session',
+ workspaceKey: 'workspace',
+ targetTurnId: 'session:3',
+ paths: ['a.txt'],
+ createdAt: '2026-01-01T00:03:00.000Z',
+ })
+ const candidates = listReversibleTurns(db, 'session', 'workspace')
+ assert.deepEqual(candidates.map(turn => turn.turn_id), ['session:2', 'session:1'])
+ markTurnSnapshotMissing(db, 'session:2')
+ assert.equal(getTurn(db, 'session:2').reversible, 0)
+ assert.match(getTurn(db, 'session:2').error, /snapshot repository was wiped/)
+ assert.deepEqual(listReversibleTurns(db, 'session', 'workspace').map(turn => turn.turn_id), ['session:1'])
+ }
+ finally {
+ db.close()
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
it('marks an interrupted active turn abandoned on reopen', async () => {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-recovery-test-'))
const first = openLedger(root)