Skip to content

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

feat(turnrewind): self-heal when alternates lose borrowed objects

yingtianlan462595128168
diff --git a/docs/TURN_REWIND_GIT_DIR_PROGRESS.md b/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
index ac22f76a..136d73f2 100644
--- a/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
+++ b/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
@@ -50,7 +50,8 @@ DSH_HOME
- snapshot capture 复用 source workspace 的 `.gitignore` 和 global Git ignore 语义;
- 自定义敏感文件规则已收窄为 Git 元数据和 turnrewind 临时文件,避免把 `token.ts`、`credentials.module.ts` 等合法源码静默排除;
- snapshot refs 仅接受 `refs/turnrewind/` 前缀,并拒绝明显的路径穿越形式;
-- Git 路径 diff 继续使用 NUL 分隔。
+- Git 路径 diff 继续使用 NUL 分隔;
+- **alternates 失效自愈**:每次 capture 后用 `git rev-list --objects --missing=print` 做连通性检查——源仓库 `gc --prune=now`(amend/rebase 的日常残留)可能删除被 snapshot 链借用且不可达的对象;检测到缺对象时自动降级:删除私有 snapshot repo、以**自包含模式**(不再写 alternates、不再复制 source index)重建全新基线。旧 turn 成为死快照走既有的 planner 跳过路径,后续 turn 永久不再借用。检查失败(极老 git/瞬时错误)只跳过自愈并告警,绝不误伤健康链。已在磁盘上的文件会在下次 capture 时因 read-tree 的 stat-less index 被重新哈希而自动补写本地 blob,因此永久性断裂只发生在“文件已删 + blob 是借用 + 源 gc”的组合上,检测正覆盖这一路径。
### 测试
@@ -67,11 +68,12 @@ DSH_HOME
- 新增 `test/git-worktree.test.js`:linked worktree 隔离——同一仓库两个 worktree 得到两个独立 snapshot repo 与 refs(互不泄漏、共享同一 common objects alternates)、源仓库 HEAD/branch/status/refs 逐字节不变、worktree 子目录 cwd 归并到该 worktree 的 store;以及打包对象(`git gc --prune=now`)经 alternates 的 capture/restore 读写路径;
- `maintenance.test.js` 新增 purge 安全测试:purge 只删除该 workspace 的 snapshot repo 与账本行,用户 `.git` 目录、HEAD、porcelain status、工作区文件以及其他 workspace 的 snapshot repo 和账本行全部完好;
- `test/git-test-utils.js` 为每个测试仓库显式设置 `core.autocrlf=false`:Git for Windows 系统级默认 `core.autocrlf=true` 会让 worktree checkout/add 发生行尾转换,导致 fixture 依赖机器配置(本机已实测踩到);
+- 新增 `test/git-gc.test.js`:alternates 失效自愈——构造“文件已删除 + blob 从源仓库借用(`hash-object -w` 不可达对象)+ 源 `gc --prune=now`”的真实断裂场景,验证读取路径断(`stateAt` reject)、下一次 capture 检测到父链缺对象并自愈为自包含基线(alternates 消失、对象物理落本地、后续 capture 独立可读);
- 当前已执行的全量插件回归:
```text
-Test Files: 13 passed
-Tests: 68 passed
+Test Files: 14 passed
+Tests: 69 passed
Failed: 0
```
@@ -81,7 +83,7 @@ Failed: 0
### 必须完成
-1. alternates 剩余风险:源仓库 `git gc`/`git prune` 可能删除仅被 snapshot 链引用(经 alternates 复用、源侧已不可达)的对象;需要失效检测或回退独立对象存储。常规路径(打包对象读写、source index 初始化、dirty 仓库)已有测试钉住;
+1. 连通性检查的性能优化(可选):当前每次 capture 走全链 `rev-list`(O(链上对象总数))。优化方向:常态走 `--not <parent>` 的增量检查 + 源对象库 mtime 变化时触发全链检查。大仓库上需先实测 capture 耗时;
2. 更新插件 README、`docs/TURN_REWIND.md` 和生产审计报告,明确本模式是实验分支、Git-only workspace 和仍未解决的风险(README 中的预算守卫/`TURNREWIND_MAX_*` 章节已过时);
3. 完整插件回归、lint、Node import smoke 已全部通过(见上节),剩余必要的真实 DSH Host 真机验证。
@@ -105,7 +107,7 @@ Failed: 0
| 项目 | 当前取舍 |
| --- | --- |
| 快照存储 | 仍为 DSH_HOME 下的独立 Git repo,不污染用户 `.git` |
-| Git 对象复用 | 通过 alternates 尝试复用 source objects;失败时应能回退到独立对象 |
+| Git 对象复用 | 通过 alternates 借用 source objects;capture 后连通性检查发现被源 gc 削掉的对象时自愈为自包含存储(不再借用)。检查成本:每次 capture 全链 rev-list(优化 TODO) |
| Ignore 规则 | 以 source Git ignore 为主,turnrewind 临时文件额外排除 |
| 非 Git workspace | 不支持,明确记录 `TURNREWIND_GIT_REQUIRED` |
| 大 workspace | 不再做重复的全目录预算扫描;Git ignore 可减少范围,但快照容量和性能风险仍存在 |
diff --git a/plugins/dsh-tauri-turnrewind/lib/core/git-snapshot.js b/plugins/dsh-tauri-turnrewind/lib/core/git-snapshot.js
index 9efc50dd..1d652e88 100644
--- a/plugins/dsh-tauri-turnrewind/lib/core/git-snapshot.js
+++ b/plugins/dsh-tauri-turnrewind/lib/core/git-snapshot.js
@@ -119,12 +119,16 @@ async function ensureRepository(store) {
await runGitText(repoDir, workspaceDir, ['config', 'core.longpaths', 'true'])
// Reuse committed source objects, as OpenCode does, while new snapshot
- // objects stay in this private repository.
- const sourceObjects = join(sourceCommonDir, 'objects')
- if (existsSync(sourceObjects)) {
- const alternates = join(repoDir, 'objects', 'info', 'alternates')
- mkdirSync(dirname(alternates), { recursive: true })
- writeFileSync(alternates, `${sourceObjects}\n`)
+ // objects stay in this private repository. A healed store stays
+ // self-contained: alternates are never written again, so source-side
+ // gc/prune can no longer break snapshot chains for this workspace.
+ if (store.selfContained !== true) {
+ const sourceObjects = join(sourceCommonDir, 'objects')
+ if (existsSync(sourceObjects)) {
+ const alternates = join(repoDir, 'objects', 'info', 'alternates')
+ mkdirSync(dirname(alternates), { recursive: true })
+ writeFileSync(alternates, `${sourceObjects}\n`)
+ }
}
}
if (sourceInfoExclude && existsSync(sourceInfoExclude)) {
@@ -225,11 +229,48 @@ export function probeWorkspace(workspaceDir) {
}
/**
- * Capture a complete allowed-path tree, incrementally reusing the parent tree.
- * A missing parent (wiped snapshot directory, moved DSH_HOME) degrades to a
- * fresh baseline instead of failing every future turn.
+ * Capture a snapshot, then verify every referenced object is still readable
+ * through the snapshot repository (including its alternates). The source
+ * repository can prune exactly the unreachable objects we borrowed —
+ * `git gc --prune=now` after an amend/rebase is the everyday case — so a
+ * capture that reuses a parent chain may silently reference deleted blobs.
+ * When that happens, rebuild the store as a self-contained repository (no
+ * alternates, no source-index seeding) and take a fresh baseline: old turns
+ * become dead snapshots the planner already skips, and future turns never
+ * borrow again.
*/
export async function captureSnapshot(store, refName, message, parentRef) {
+ let snapshot = await captureInto(store, refName, message, parentRef)
+ if (await snapshotHasMissingObjects(store, snapshot.commit)) {
+ console.warn(`turnrewind: snapshot objects for ${store.workspaceDir} disappeared from the source repository (gc/prune); rebuilding a self-contained baseline`)
+ rmSync(store.repoDir, { recursive: true, force: true })
+ store.selfContained = true
+ snapshot = await captureInto(store, refName, message, undefined)
+ if (await snapshotHasMissingObjects(store, snapshot.commit))
+ throw new Error(`TURNREWIND_SNAPSHOT_INCOMPLETE: ${store.workspaceDir} baseline still misses objects after a self-contained rebuild`)
+ }
+ return snapshot
+}
+
+/**
+ * `git rev-list --objects --missing=print` walks every object reachable from
+ * the commit (through alternates) and prints missing ones as `?<oid>` lines
+ * while exiting 0, so detection stays non-fatal on healthy stores.
+ */
+async function snapshotHasMissingObjects(store, commit) {
+ try {
+ const output = await runGit(store.repoDir, store.workspaceDir, ['rev-list', '--objects', '--missing=print', commit])
+ return output.toString('utf8').split('\n').some(line => line.startsWith('?'))
+ }
+ catch (error) {
+ // Detection must never take healthy snapshots down: an unusable probe
+ // (very old git, transient failure) only skips the self-heal path.
+ console.warn(`turnrewind: snapshot connectivity check failed for ${store.workspaceDir}: ${error.message}`)
+ return false
+ }
+}
+
+async function captureInto(store, refName, message, parentRef) {
const { repoDir, workspaceDir, sourceIndexPath } = store
await ensureRepository(store)
let parent
@@ -243,7 +284,7 @@ export async function captureSnapshot(store, refName, message, parentRef) {
const env = { GIT_INDEX_FILE: indexPath }
if (parent)
await runGit(repoDir, workspaceDir, ['read-tree', parent], env)
- else if (existsSync(sourceIndexPath))
+ else if (store.selfContained !== true && existsSync(sourceIndexPath))
copyFileSync(sourceIndexPath, indexPath)
// Git reads .gitignore and global excludes from the source worktree while
// GIT_INDEX_FILE isolates the snapshot from the user's real index.
diff --git a/plugins/dsh-tauri-turnrewind/test/git-gc.test.js b/plugins/dsh-tauri-turnrewind/test/git-gc.test.js
new file mode 100644
index 00000000..1dfab5be
--- /dev/null
+++ b/plugins/dsh-tauri-turnrewind/test/git-gc.test.js
@@ -0,0 +1,66 @@
+import assert from 'node:assert/strict'
+import { existsSync } from 'node:fs'
+import { mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { it } from 'vitest'
+import { captureSnapshot, createSnapshotStore, restorePath, stateAt } from '../lib/core/git-snapshot.js'
+import { commitAll, gitOutput, initGitWorkspace, runGit } from './git-test-utils.js'
+
+it('detects source-pruned objects and rebuilds a self-contained baseline', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-gc-test-'))
+ const workspace = join(root, 'workspace')
+ try {
+ await initGitWorkspace(workspace)
+ await writeFile(join(workspace, 'committed.txt'), 'committed\n')
+ await commitAll(workspace, 'initial')
+ await writeFile(join(workspace, 'borrowed.txt'), 'borrowed content\n')
+ // An unreachable source object: present in the source object store but
+ // referenced by no ref, exactly like the leftovers of an amend or rebase.
+ const borrowedBlob = (await gitOutput(workspace, ['hash-object', '-w', 'borrowed.txt'])).trim()
+
+ const store = createSnapshotStore(join(root, 'data'), workspace)
+ const first = await captureSnapshot(store, 'refs/turnrewind/gc-before', 'before')
+ // The blob was borrowed through alternates instead of being copied.
+ assert.equal((await stateAt(store, first.commit, 'borrowed.txt')).kind, 'file')
+ assert.equal(existsSync(join(store.repoDir, 'objects', 'info', 'alternates')), true)
+ await gitOutput(workspace, ['--git-dir', store.repoDir, 'cat-file', '-e', `${first.commit}:borrowed.txt`])
+
+ // The turn: rewrite one file and delete the borrowed one.
+ await writeFile(join(workspace, 'committed.txt'), 'changed\n')
+ await rm(join(workspace, 'borrowed.txt'))
+ // The source repository prunes its unreachable objects. Files still on
+ // disk get their blobs re-written locally on the next capture (read-tree
+ // seeds a stat-less index, so git add re-hashes everything), but a
+ // deleted file's borrowed blob is never re-written: only the parent
+ // chain still references it.
+ await runGit(workspace, ['gc', '--quiet', '--prune=now'])
+ await assert.rejects(() => gitOutput(workspace, ['cat-file', '-t', borrowedBlob]))
+ // Reads of the old snapshot now break: the borrowed blob is gone everywhere.
+ await assert.rejects(() => stateAt(store, first.commit, 'borrowed.txt'))
+
+ // The next capture detects the hole through the parent chain and heals
+ // into a self-contained store.
+ const second = await captureSnapshot(store, 'refs/turnrewind/gc-after', 'after', 'refs/turnrewind/gc-before')
+ assert.notEqual(second.commit, first.commit)
+ assert.equal(existsSync(join(store.repoDir, 'objects', 'info', 'alternates')), false)
+ assert.equal((await stateAt(store, second.commit, 'committed.txt')).kind, 'file')
+ assert.equal((await stateAt(store, second.commit, 'borrowed.txt')).kind, 'absent')
+
+ // Later captures stay self-contained and keep reading without the source.
+ const third = await captureSnapshot(store, 'refs/turnrewind/gc-third', 'third', 'refs/turnrewind/gc-after')
+ assert.equal((await stateAt(store, third.commit, 'committed.txt')).kind, 'file')
+ await restorePath(store, third.commit, 'committed.txt')
+ assert.equal(await readFile(join(workspace, 'committed.txt'), 'utf8'), 'changed\n')
+
+ // The healed store physically owns its objects: with no alternates file
+ // the snapshot chain no longer depends on the source repository's
+ // object store at all.
+ const objects = await readdir(join(store.repoDir, 'objects'), { withFileTypes: true })
+ assert.equal(existsSync(join(store.repoDir, 'objects', 'info', 'alternates')), false)
+ assert.ok(objects.some(entry => entry.isDirectory() && entry.name !== 'info'))
+ }
+ finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})