Skip to content

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

feat(turnrewind): archive expired plans instead of deleting them

yingtianlan510ca8292230
diff --git a/packages/dsh-tauri-turnrewind-ts/README.md b/packages/dsh-tauri-turnrewind-ts/README.md
index 589f15ef..77caab62 100644
--- a/packages/dsh-tauri-turnrewind-ts/README.md
+++ b/packages/dsh-tauri-turnrewind-ts/README.md
@@ -18,7 +18,7 @@
- **alternates 失效自愈**:私有 repo 通过 alternates 借用源仓库对象,而源仓库 `git gc --prune=now`(amend/rebase 的日常残留)可能删掉被借用且不可达的对象;每次 capture 后做连通性检查(`git rev-list --objects --missing=print`),发现缺对象即降级为自包含存储(不再借用、不再复制源 index)并重建基线,旧 turn 走死快照跳过路径;
- **工作区资格守卫(Git 目录模式)**:会话 cwd 必须位于 Git worktree(子目录自动归并到 worktree 根,共享同一快照域);家目录、家目录祖先、盘根等系统目录直接拒绝;非 Git 目录记为 `TURNREWIND_GIT_REQUIRED`,不再做全目录预算扫描(见「工作区资格」);
- **不可用弹窗**:客户端半(`src/client/`)通过 `turnrewind` 会话投影检测到不可用提示时,在 Web UI 内弹出模态对话框(中英双语、跟随应用主题、每个浏览器只弹一次);
-- **两阶段 `/undo`**:先出预览卡(红绿 diff + `+x -y` 徽标 + 文件清单),卡内 ✓/✗ 按钮确认执行或取消——不看预览就不会误执行;计划 5 分钟过期,确认时二次校验磁盘;
+- **两阶段 `/undo`**:先出预览卡(红绿 diff + `+x -y` 徽标 + 文件清单),卡内 ✓/✗ 按钮确认执行或取消——不看预览就不会误执行;计划 5 分钟过期,过期/取消的 plan **永久留档**:卡片保留文件清单与 diff 供随时回看(过期只锁执行、不抹记录),确认时二次校验磁盘与预览绑定;
- 恢复前比较当前文件与 turn 完成时的快照,发现变化则拒绝覆盖,并给出「turn 产物 → 当前磁盘」的冲突 diff;
- 冲突可用 `--skip-conflicts`(只恢复无冲突文件)或 `--force`(强制覆盖)直接执行;
- `/undo --redo`(重做最近一次已应用的 undo)**已禁用**:入口在解析层直接拒绝,底层恢复路径加固代码保留但未开放(见「已禁用:redo」);
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/components/command-view.ts b/packages/dsh-tauri-turnrewind-ts/src/client/components/command-view.ts
index bb58b0d8..3c65f8d2 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/components/command-view.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/components/command-view.ts
@@ -203,7 +203,9 @@ export function UndoCommandView(props: CommandViewProps): React.ReactElement {
}
}, [state, parsed.planId, ownerSessionId])
- const collapsed = planStatus === 'cancelled' || planStatus === 'gone'
+ // 只有「真不存在」(旧数据被清理/工作区被 purge)才折叠为细行;
+ // cancelled/expired 的 plan 行已永久留档,卡片保留文件清单与 diff 供随时回看。
+ const collapsed = planStatus === 'gone'
const showBody = expanded && !collapsed && (hasDiff || parsed.files.length > 0)
const actionable = parsed.planId !== undefined && state === 'ok' && !collapsed && (planStatus === null || planStatus === 'pending')
// ref 防抖:React 状态更新慢一拍,双击会绕过 state-only 检查发两次请求。
@@ -252,13 +254,13 @@ export function UndoCommandView(props: CommandViewProps): React.ReactElement {
? `执行确认失败:${submitError}`
: resultText || (planStatus === 'applied' || pendingWait
? '已提交,等待执行结果…'
- : planStatus === 'cancelled' || submitted === 'cancel' ? '已取消' : planStatus === 'gone' ? '该计划已过期,重新执行 /undo 可生成新预览' : '执行将恢复下方文件到本轮改动前')
- // 提交后的结果(成功/失败/等待中)靠左展示;预览提示贴 footer 右缘。
+ : planStatus === 'expired' || planStatus === 'gone' ? '该计划已过期,重新执行 /undo 可生成新预览' : planStatus === 'cancelled' || submitted === 'cancel' ? '已取消' : '执行将恢复下方文件到本轮改动前')
+ // 提交后的结果(成功/失败/等待中)靠左展示;预览/过期/取消提示贴 footer 右缘。
const hintLeft = Boolean(resultText || submitError || submitted !== null || planStatus === 'applied')
const hintCls = `${TURNREWIND_CLASS_PREFIX}-card-hint${submitError
? ` ${TURNREWIND_CLASS_PREFIX}-card-hint-error`
: resultText || planStatus === 'applied' ? ` ${TURNREWIND_CLASS_PREFIX}-card-hint-ok` : ''}${hintLeft ? '' : ` ${TURNREWIND_CLASS_PREFIX}-card-hint-right`}`
- const showFooter = actionable || submitting || resultText !== null || submitError !== null || submitted !== null || planStatus === 'applied'
+ const showFooter = actionable || submitting || resultText !== null || submitError !== null || submitted !== null || planStatus === 'applied' || planStatus === 'expired' || planStatus === 'cancelled'
// 取消/过期折叠为无边框细行。
if (collapsed) {
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/types/index.ts b/packages/dsh-tauri-turnrewind-ts/src/client/types/index.ts
index dd3102b4..cf32e04f 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/types/index.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/types/index.ts
@@ -24,7 +24,7 @@ export interface ParsedUndoOutput {
/** plan 状态轮询结果。 */
export interface PlanStatusResolution {
- status: 'pending' | 'applied' | 'cancelled' | 'gone' | 'error' | null
+ status: 'pending' | 'applied' | 'cancelled' | 'expired' | 'gone' | 'error' | null
stop: boolean
resultText: string | null
}
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/utils/parse.ts b/packages/dsh-tauri-turnrewind-ts/src/client/utils/parse.ts
index e9c694a9..b633af49 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/utils/parse.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/utils/parse.ts
@@ -76,8 +76,8 @@ export function parseUndoOutput(raw: string | undefined): ParsedUndoOutput {
return result
}
-/** plan 状态轮询的结局判定(gone 即停;applied/cancelled/error 带 resultText)。 */
-export function resolvePlanStatus(res: { ok: boolean, status: number }, payload: { status?: string, resultText?: string | null } | null): { status: 'pending' | 'applied' | 'cancelled' | 'gone' | 'error' | null, stop: boolean, resultText: string | null } {
+/** plan 状态轮询的结局判定(expired/gone/applied/cancelled/error 终态;非 404 失败继续轮询)。 */
+export function resolvePlanStatus(res: { ok: boolean, status: number }, payload: { status?: string, resultText?: string | null } | null): { status: 'pending' | 'applied' | 'cancelled' | 'expired' | 'gone' | 'error' | null, stop: boolean, resultText: string | null } {
// Non-404 failures must not settle the card: the plan may still land on a
// later poll, so keep polling without flipping to a terminal state.
if (!res.ok)
@@ -85,6 +85,9 @@ export function resolvePlanStatus(res: { ok: boolean, status: number }, payload:
const status = payload?.status
if (status === 'gone')
return { status: 'gone', stop: true, resultText: null }
+ // expired 是终态但不是 gone:plan 行已留档,卡片保留留档视图、仅不可执行。
+ if (status === 'expired')
+ return { status: 'expired', stop: true, resultText: payload?.resultText ?? null }
if (status === 'applied' || status === 'cancelled' || status === 'error')
return { status, stop: true, resultText: payload?.resultText ?? null }
return { status: 'pending', stop: false, resultText: null }
diff --git a/packages/dsh-tauri-turnrewind-ts/src/host/apply.ts b/packages/dsh-tauri-turnrewind-ts/src/host/apply.ts
index 7d7f2fcd..29fe6f99 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/host/apply.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/host/apply.ts
@@ -435,9 +435,12 @@ export function apply(ctx: HostApplyContext): void {
return [400, { error: 'planId and sessionId are required' }]
const previewRow = getPendingPlanRow(ledger, planId)
if (previewRow === undefined)
- return [404, { error: 'plan expired or already applied — run /undo again' }]
+ return [404, { error: 'plan not found — run /undo again' }]
if (previewRow.session_id !== sessionId)
return [403, { error: 'the plan belongs to another session' }]
+ // 过期 plan 已转 expired 留档:明确告知过期(可查看,不可执行)。
+ if (previewRow.status === 'expired')
+ return [409, { error: 'this plan has expired — run /undo again to preview a fresh plan' }]
if (previewRow.status !== 'pending')
return [409, { error: 'this plan was already applied or cancelled — run /undo again' }]
const planRuntime = workspaceStores.get(previewRow.workspace_key)
diff --git a/packages/dsh-tauri-turnrewind-ts/src/host/service/ledger.ts b/packages/dsh-tauri-turnrewind-ts/src/host/service/ledger.ts
index 8ea3eea3..f8f416c5 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/host/service/ledger.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/host/service/ledger.ts
@@ -165,14 +165,15 @@ export function openLedger(rootDir: string): Ledger {
}
/**
- * 失效 plan 清扫:只删过期的 pending 行。
+ * 失效 plan 清扫:过期的 pending 行转为 `expired` 永久留档,不删除。
*
- * settled 行(applied/cancelled)**永远保留**:undo 的执行结果必须可追溯
- * (刷新页面、跨天查看、审计),结果行就是持久记录——删了它卡片就变成
- * "gone",用户看不到已经执行了什么。
+ * 过期只锁执行(confirm 路径按状态拒绝),不抹掉审计记录:卡片里的文件
+ * 清单与 diff 来自命令输出文本(对话内永久存在),plan 行保留后状态轮询
+ * 返回 expired,用户随时可以回看「当时预览了什么」。settled 行
+ * (applied/cancelled)同样永远保留。
*/
export function prunePendingPlans(db: Ledger): void {
- db.prepare('DELETE FROM pending_plans WHERE status = \'pending\' AND expires_at < ?')
+ db.prepare('UPDATE pending_plans SET status = \'expired\' WHERE status = \'pending\' AND expires_at < ?')
.run(new Date().toISOString())
}
@@ -301,7 +302,8 @@ export function createPendingPlan(db: Ledger, plan: PendingPlan): string {
db.exec('BEGIN')
try {
// One live plan per session+workspace: a newer preview replaces the older.
- db.prepare('DELETE FROM pending_plans WHERE session_id = ? AND workspace_key = ?')
+ // 被替换的旧 plan 转 expired 留档(审计可见),不再物理删除。
+ db.prepare('UPDATE pending_plans SET status = \'expired\' WHERE session_id = ? AND workspace_key = ? AND status = \'pending\'')
.run(plan.sessionId, plan.workspaceKey)
const planId = randomUUID()
const createdAt = new Date().toISOString()
@@ -348,14 +350,17 @@ export interface PendingPlan {
export function claimPendingPlan(db: Ledger, planId: string, sessionId: string): { ok: true, row: PendingPlanRow & { paths: string[] } } | { ok: false, code: number, error: string } {
const row = db.prepare('SELECT * FROM pending_plans WHERE plan_id = ?').get(planId) as unknown as PendingPlanRow | undefined
if (row === undefined)
- return { ok: false, code: 404, error: 'plan expired or already applied — run /undo again' }
+ return { ok: false, code: 404, error: 'plan not found — run /undo again' }
if (row.session_id !== sessionId)
return { ok: false, code: 403, error: 'the plan belongs to another session' }
+ // 过期 plan 转 expired 留档但拒绝执行:过期锁的是执行,不是查看。
+ if (row.status === 'expired')
+ return { ok: false, code: 410, error: 'this plan has expired — run /undo again to preview a fresh plan' }
if (row.status !== 'pending')
return { ok: false, code: 409, error: 'this plan was already applied, cancelled, or is being applied — run /undo again' }
if (row.expires_at < new Date().toISOString()) {
- db.prepare('DELETE FROM pending_plans WHERE plan_id = ? AND status = \'pending\'').run(planId)
- return { ok: false, code: 404, error: 'plan expired or already applied — run /undo again' }
+ db.prepare('UPDATE pending_plans SET status = \'expired\' WHERE plan_id = ? AND status = \'pending\'').run(planId)
+ return { ok: false, code: 410, error: 'this plan has expired — run /undo again to preview a fresh plan' }
}
const claimed = db.prepare(`
UPDATE pending_plans SET status = 'applying'
@@ -383,9 +388,11 @@ export function getPendingPlanRow(db: Ledger, planId: string): (PendingPlanRow &
const row = db.prepare('SELECT * FROM pending_plans WHERE plan_id = ?').get(planId) as unknown as PendingPlanRow | undefined
if (row === undefined)
return undefined
+ // 过期 pending 转 expired 留档并原样返回(status = 'expired'),
+ // 由调用方按状态拒绝执行;行本身保留供审计查看。
if (row.status === 'pending' && row.expires_at < new Date().toISOString()) {
- db.prepare('DELETE FROM pending_plans WHERE plan_id = ? AND status = \'pending\'').run(planId)
- return undefined
+ db.prepare('UPDATE pending_plans SET status = \'expired\' WHERE plan_id = ? AND status = \'pending\'').run(planId)
+ row.status = 'expired'
}
return { ...row, paths: JSON.parse(row.paths_json) }
}
@@ -408,8 +415,15 @@ export function markPendingPlanApplied(db: Ledger, planId: string, sessionId: st
}
export function getPendingPlanStatus(db: Ledger, planId: string, sessionId: string): { status: string, resultText: string | null } | undefined {
- const row = db.prepare('SELECT status, result_text FROM pending_plans WHERE plan_id = ? AND session_id = ?').get(planId, sessionId) as unknown as { status: string, result_text: string | null } | undefined
- return row === undefined ? undefined : { status: row.status, resultText: row.result_text }
+ const row = db.prepare('SELECT plan_id, status, result_text, expires_at FROM pending_plans WHERE plan_id = ? AND session_id = ?').get(planId, sessionId) as unknown as { plan_id: string, status: string, result_text: string | null, expires_at: string } | undefined
+ if (row === undefined)
+ return undefined
+ // 过期 pending 转 expired 留档:状态轮询如实回报,卡片保留留档视图但不再可执行。
+ if (row.status === 'pending' && row.expires_at < new Date().toISOString()) {
+ db.prepare('UPDATE pending_plans SET status = \'expired\' WHERE plan_id = ? AND status = \'pending\'').run(row.plan_id)
+ row.status = 'expired'
+ }
+ return { status: row.status, resultText: row.result_text }
}
export function settleTurn(db: Ledger, turnId: string, afterRef: string): void {
diff --git a/packages/dsh-tauri-turnrewind-ts/test/client-view.test.js b/packages/dsh-tauri-turnrewind-ts/test/client-view.test.js
index 011db284..b4264127 100644
--- a/packages/dsh-tauri-turnrewind-ts/test/client-view.test.js
+++ b/packages/dsh-tauri-turnrewind-ts/test/client-view.test.js
@@ -19,6 +19,13 @@ it('resolvePlanStatus settles an expired plan as gone and stops polling', () =>
assert.deepEqual(explicit, { status: 'gone', stop: true, resultText: null })
})
+it('resolvePlanStatus treats expired as a terminal archived state (not gone)', () => {
+ // Expired plans stay in the ledger: the card keeps its archived view and
+ // only execution is refused — it must NOT collapse to gone.
+ const expired = client.resolvePlanStatus({ ok: true, status: 200 }, { status: 'expired' })
+ assert.deepEqual(expired, { status: 'expired', stop: true, resultText: null })
+})
+
it('resolvePlanStatus stops polling once the plan settles', () => {
const applied = client.resolvePlanStatus({ ok: true, status: 200 }, { status: 'applied', resultText: 'Undid turn x' })
assert.deepEqual(applied, { status: 'applied', stop: true, resultText: 'Undid turn x' })
diff --git a/packages/dsh-tauri-turnrewind-ts/test/ledger.test.js b/packages/dsh-tauri-turnrewind-ts/test/ledger.test.js
index 8d8277c7..c0c9122b 100644
--- a/packages/dsh-tauri-turnrewind-ts/test/ledger.test.js
+++ b/packages/dsh-tauri-turnrewind-ts/test/ledger.test.js
@@ -3,7 +3,7 @@ import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'pathe'
import { it } from 'vitest'
-import { claimPendingPlan, claimRewindNotices, completeRedoTransaction, completeUndoTransaction, createOperation, createPendingPlan, getLatestAppliedUndo, getLatestSnapshotRef, getLatestTurn, getPendingPlanStatus, getTurn, insertTurn, listNeedsRecoveryWorkspaces, listReversibleTurns, markPendingPlanApplied, markPendingPlanCancelled, markTurnSnapshotMissing, openLedger, planPathsDigest, pruneConsumedNotices, queueRewindNotice, recordSkippedTurn, releasePendingPlanClaim, settleInterruptedTurn, settleNoopTurn, settleOperation, settleTurn } from '../src/host/service/ledger'
+import { claimPendingPlan, claimRewindNotices, completeRedoTransaction, completeUndoTransaction, createOperation, createPendingPlan, getLatestAppliedUndo, getLatestSnapshotRef, getLatestTurn, getPendingPlanRow, getPendingPlanStatus, getTurn, insertTurn, listNeedsRecoveryWorkspaces, listReversibleTurns, markPendingPlanApplied, markPendingPlanCancelled, markTurnSnapshotMissing, openLedger, planPathsDigest, pruneConsumedNotices, prunePendingPlans, queueRewindNotice, recordSkippedTurn, releasePendingPlanClaim, settleInterruptedTurn, settleNoopTurn, settleOperation, settleTurn } from '../src/host/service/ledger'
it('persists turn lifecycle and resumes from the latest durable snapshot', async () => {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-ledger-test-'))
@@ -473,6 +473,58 @@ it('claims a pending plan exactly once and serializes cancel/apply outcomes', as
}
})
+it('archives expired plans as expired instead of deleting them', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-plan-expire-'))
+ const db = openLedger(root)
+ try {
+ const planId = createPendingPlan(db, {
+ sessionId: 'session',
+ workspaceKey: 'workspace',
+ turnId: 'session:1',
+ paths: ['a.txt'],
+ beforeRef: 'refs/turnrewind/b',
+ afterRef: 'refs/turnrewind/a',
+ })
+ // Force the plan past its TTL.
+ db.prepare('UPDATE pending_plans SET expires_at = \'2020-01-01T00:00:00.000Z\' WHERE plan_id = ?').run(planId)
+
+ // The status poll converts it to expired and reports it (NOT gone):
+ // the card keeps its archived view, only execution is refused.
+ assert.deepEqual(getPendingPlanStatus(db, planId, 'session'), { status: 'expired', resultText: null })
+ assert.equal(db.prepare('SELECT status FROM pending_plans WHERE plan_id = ?').get(planId).status, 'expired')
+
+ // The row lookup for the confirm route also reports expired (not undefined).
+ const row = getPendingPlanRow(db, planId)
+ assert.equal(row.status, 'expired')
+
+ // Claiming an expired plan is refused with a precise error; the row stays.
+ const claim = claimPendingPlan(db, planId, 'session')
+ assert.equal(claim.ok, false)
+ assert.equal(claim.ok ? 200 : claim.code, 410)
+ assert.match(claim.ok ? '' : claim.error, /has expired/u)
+ assert.equal(db.prepare('SELECT status FROM pending_plans WHERE plan_id = ?').get(planId).status, 'expired')
+
+ // A newer preview expires the replaced plan (kept as archive) instead of deleting it.
+ const secondId = createPendingPlan(db, {
+ sessionId: 'session',
+ workspaceKey: 'workspace',
+ turnId: 'session:2',
+ paths: ['b.txt'],
+ beforeRef: 'refs/turnrewind/b2',
+ afterRef: 'refs/turnrewind/a2',
+ })
+ db.prepare('UPDATE pending_plans SET expires_at = \'2020-01-01T00:00:00.000Z\' WHERE plan_id = ?').run(secondId)
+ prunePendingPlans(db)
+ const statuses = db.prepare('SELECT plan_id, status FROM pending_plans WHERE session_id = ? ORDER BY plan_id').all('session')
+ assert.equal(statuses.length, 2)
+ assert.deepEqual(statuses.map(row2 => row2.status), ['expired', 'expired'])
+ }
+ finally {
+ db.close()
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
it('fences applying operations as recovery-required on reopen', async () => {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-operation-recovery-test-'))
const first = openLedger(root)