Skip to content

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

test(turnrewind): pin ignore semantics, worktree isolation and purge safety

yingtianlan77cf157fb29a
diff --git a/docs/TURN_REWIND_GIT_DIR_PROGRESS.md b/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
index 685e1a35..ac22f76a 100644
--- a/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
+++ b/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
@@ -63,11 +63,15 @@ DSH_HOME
- capture 前后 `HEAD`、当前 branch、`symbolic-ref`、`git status --porcelain`、`ls-files -s`(index 内容)、`git diff --cached`、`for-each-ref`、`git log --all`、`git stash list` 全部逐字节不变;
- restore 只改工作区文件,index/HEAD/refs/stash 不变,且快照捕获的是文件系统内容而非 index;
- 私有 snapshot repo 的 refs 仅含 `refs/turnrewind/`,用户 refs 命名空间无泄漏,工作区无 `.turnrewind-*` 临时文件残留;
+- 新增 `test/git-ignore.test.js`:ignore/attributes 语义——嵌套 `.gitignore` 与 `!` 取反、`.git/info/exclude`(含源规则变更后逐次 capture 的重同步)、global excludes(`GIT_CONFIG_GLOBAL` + `core.excludesFile`)、`.gitattributes`(`text=auto` 入库 LF 归一化、`-text` 字节精确,blob 经 `cat-file` 直接断言);
+- 新增 `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 依赖机器配置(本机已实测踩到);
- 当前已执行的全量插件回归:
```text
-Test Files: 11 passed
-Tests: 61 passed
+Test Files: 13 passed
+Tests: 68 passed
Failed: 0
```
@@ -77,12 +81,9 @@ Failed: 0
### 必须完成
-1. 增加 `.gitignore`、`.git/info/exclude`、global ignore 和 `.gitattributes` 语义测试;
-2. 解决 source object alternates、source index 初始化和 snapshot 私有对象之间的兼容性问题;
-3. 给同一个 Git common-dir 下的 linked worktree 增加 snapshot 隔离测试;
-4. 更新 `maintenance.js`、`purge-workspace.js`、ledger workspace metadata 和 purge 测试,保证只清理 turnrewind 的 snapshot repo,不误删用户 `.git`;
-5. 更新插件 README、`docs/TURN_REWIND.md` 和生产审计报告,明确本模式是实验分支、Git-only workspace 和仍未解决的风险(README 中的预算守卫/`TURNREWIND_MAX_*` 章节已过时);
-6. 运行完整插件回归、lint、Node import smoke,以及必要的真实 DSH Host 验证。
+1. alternates 剩余风险:源仓库 `git gc`/`git prune` 可能删除仅被 snapshot 链引用(经 alternates 复用、源侧已不可达)的对象;需要失效检测或回退独立对象存储。常规路径(打包对象读写、source index 初始化、dirty 仓库)已有测试钉住;
+2. 更新插件 README、`docs/TURN_REWIND.md` 和生产审计报告,明确本模式是实验分支、Git-only workspace 和仍未解决的风险(README 中的预算守卫/`TURNREWIND_MAX_*` 章节已过时);
+3. 完整插件回归、lint、Node import smoke 已全部通过(见上节),剩余必要的真实 DSH Host 真机验证。
### 仍需保留的安全边界
diff --git a/plugins/dsh-tauri-turnrewind/test/git-ignore.test.js b/plugins/dsh-tauri-turnrewind/test/git-ignore.test.js
new file mode 100644
index 00000000..45e463eb
--- /dev/null
+++ b/plugins/dsh-tauri-turnrewind/test/git-ignore.test.js
@@ -0,0 +1,130 @@
+import assert from 'node:assert/strict'
+import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { dirname, join } from 'node:path'
+import { it } from 'vitest'
+import { captureSnapshot, createSnapshotStore, stateAt } from '../lib/core/git-snapshot.js'
+import { gitOutput, initGitWorkspace } from './git-test-utils.js'
+
+it('applies nested .gitignore patterns including negations', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-ignore-nested-'))
+ const workspace = join(root, 'workspace')
+ try {
+ await initGitWorkspace(workspace)
+ await writeFile(join(workspace, '.gitignore'), '*.log\n!keep.log\n')
+ await mkdir(join(workspace, 'nested'))
+ await writeFile(join(workspace, 'nested', '.gitignore'), 'inner-secret.txt\n')
+ await writeFile(join(workspace, 'debug.log'), 'log')
+ await writeFile(join(workspace, 'keep.log'), 'keep')
+ await writeFile(join(workspace, 'nested', 'inner-secret.txt'), 'secret')
+ await writeFile(join(workspace, 'nested', 'ok.txt'), 'ok')
+
+ const store = createSnapshotStore(join(root, 'data'), workspace)
+ const snapshot = await captureSnapshot(store, 'refs/turnrewind/ignore-nested', 'nested ignore')
+
+ assert.equal((await stateAt(store, snapshot.commit, 'debug.log')).kind, 'absent')
+ assert.equal((await stateAt(store, snapshot.commit, 'keep.log')).kind, 'file')
+ assert.equal((await stateAt(store, snapshot.commit, 'nested/inner-secret.txt')).kind, 'absent')
+ assert.equal((await stateAt(store, snapshot.commit, 'nested/ok.txt')).kind, 'file')
+ // The rule files themselves are ordinary tracked-able files.
+ assert.equal((await stateAt(store, snapshot.commit, '.gitignore')).kind, 'file')
+ assert.equal((await stateAt(store, snapshot.commit, 'nested/.gitignore')).kind, 'file')
+ }
+ finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+it('honors .git/info/exclude and re-syncs it when the source rules change', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-ignore-exclude-'))
+ const workspace = join(root, 'workspace')
+ try {
+ await initGitWorkspace(workspace)
+ await writeFile(join(workspace, 'excluded.txt'), 'excluded')
+ await writeFile(join(workspace, 'kept.txt'), 'kept')
+ const sourceExclude = join(workspace, '.git', 'info', 'exclude')
+ await mkdir(dirname(sourceExclude), { recursive: true })
+ await writeFile(sourceExclude, 'excluded.txt\n')
+
+ const store = createSnapshotStore(join(root, 'data'), workspace)
+ const first = await captureSnapshot(store, 'refs/turnrewind/ignore-exclude-1', 'first')
+ assert.equal((await stateAt(store, first.commit, 'excluded.txt')).kind, 'absent')
+ assert.equal((await stateAt(store, first.commit, 'kept.txt')).kind, 'file')
+
+ // The source exclude file is mirrored into the private snapshot repo on
+ // every capture, so relaxing the rule re-opens the file to snapshots.
+ await writeFile(sourceExclude, '')
+ const second = await captureSnapshot(store, 'refs/turnrewind/ignore-exclude-2', 'second', first.commit)
+ assert.equal((await stateAt(store, second.commit, 'excluded.txt')).kind, 'file')
+ }
+ finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+it('honors the global excludes file like the source repository', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-ignore-global-'))
+ const workspace = join(root, 'workspace')
+ try {
+ await initGitWorkspace(workspace)
+ await writeFile(join(workspace, 'globally-ignored.txt'), 'ignored')
+ await writeFile(join(workspace, 'kept.txt'), 'kept')
+ const globalExcludes = join(root, 'global-excludes')
+ const globalConfig = join(root, 'global-gitconfig')
+ await writeFile(globalExcludes, 'globally-ignored.txt\n')
+ await writeFile(globalConfig, `[core]\n\texcludesFile = ${globalExcludes.replaceAll('\\', '/')}\n`)
+
+ const previous = process.env.GIT_CONFIG_GLOBAL
+ process.env.GIT_CONFIG_GLOBAL = globalConfig
+ try {
+ const store = createSnapshotStore(join(root, 'data'), workspace)
+ const snapshot = await captureSnapshot(store, 'refs/turnrewind/ignore-global', 'global ignore')
+ assert.equal((await stateAt(store, snapshot.commit, 'globally-ignored.txt')).kind, 'absent')
+ assert.equal((await stateAt(store, snapshot.commit, 'kept.txt')).kind, 'file')
+ }
+ finally {
+ if (previous === undefined)
+ delete process.env.GIT_CONFIG_GLOBAL
+ else
+ process.env.GIT_CONFIG_GLOBAL = previous
+ }
+ }
+ finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+it('normalizes text=auto files on capture and keeps -text files byte-exact', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-ignore-attributes-'))
+ const workspace = join(root, 'workspace')
+ try {
+ await initGitWorkspace(workspace)
+ await writeFile(join(workspace, '.gitattributes'), '*.txt text=auto\n*.bin -text\n')
+ await writeFile(join(workspace, 'crlf.txt'), 'a\r\nb\r\n')
+ await writeFile(join(workspace, 'lf.txt'), 'a\nb\n')
+ await writeFile(join(workspace, 'crlf.bin'), 'a\r\nb\r\n')
+
+ const store = createSnapshotStore(join(root, 'data'), workspace)
+ const snapshot = await captureSnapshot(store, 'refs/turnrewind/attributes', 'attributes')
+
+ // text=auto: the stored blob is LF-normalized, so the CRLF and LF twins
+ // share one object. (stateAt digests normalize line endings too, so the
+ // blob bytes are read directly to pin the object content.)
+ const crlf = await stateAt(store, snapshot.commit, 'crlf.txt')
+ const lf = await stateAt(store, snapshot.commit, 'lf.txt')
+ assert.equal(crlf.kind, 'file')
+ assert.equal(lf.kind, 'file')
+ assert.equal(crlf.digest, lf.digest)
+ assert.equal(await gitOutput(workspace, ['--git-dir', store.repoDir, 'cat-file', 'blob', `${snapshot.commit}:crlf.txt`]), 'a\nb\n')
+ assert.equal(await gitOutput(workspace, ['--git-dir', store.repoDir, 'cat-file', 'blob', `${snapshot.commit}:lf.txt`]), 'a\nb\n')
+
+ // -text: no conversion anywhere, the blob is byte-identical to the disk file.
+ assert.equal(
+ await gitOutput(workspace, ['--git-dir', store.repoDir, 'cat-file', 'blob', `${snapshot.commit}:crlf.bin`]),
+ await readFile(join(workspace, 'crlf.bin'), 'utf8'),
+ )
+ }
+ finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
diff --git a/plugins/dsh-tauri-turnrewind/test/git-test-utils.js b/plugins/dsh-tauri-turnrewind/test/git-test-utils.js
index c0b35e74..05b22ad0 100644
--- a/plugins/dsh-tauri-turnrewind/test/git-test-utils.js
+++ b/plugins/dsh-tauri-turnrewind/test/git-test-utils.js
@@ -46,5 +46,9 @@ export async function initGitWorkspace(workspace) {
await runGit(workspace, ['init', '--quiet'])
await runGit(workspace, ['config', 'user.name', 'Turn Rewind Test'])
await runGit(workspace, ['config', 'user.email', 'turnrewind-test@localhost'])
+ // Pin line-ending behavior so fixtures are deterministic on machines whose
+ // Git for Windows system config sets core.autocrlf=true: checkout and add
+ // then write and hash the exact bytes the test wrote.
+ await runGit(workspace, ['config', 'core.autocrlf', 'false'])
return workspace
}
diff --git a/plugins/dsh-tauri-turnrewind/test/git-worktree.test.js b/plugins/dsh-tauri-turnrewind/test/git-worktree.test.js
new file mode 100644
index 00000000..498606fd
--- /dev/null
+++ b/plugins/dsh-tauri-turnrewind/test/git-worktree.test.js
@@ -0,0 +1,105 @@
+import assert from 'node:assert/strict'
+import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { join, resolve } from 'node:path'
+import { it } from 'vitest'
+import { captureSnapshot, createSnapshotStore, gitRef, restorePath, stateAt } from '../lib/core/git-snapshot.js'
+import { commitAll, gitOutput, initGitWorkspace, runGit } from './git-test-utils.js'
+
+it('isolates linked worktrees of one repository into separate snapshot stores', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-worktree-test-'))
+ const main = join(root, 'main')
+ const linked = join(root, 'linked')
+ try {
+ await initGitWorkspace(main)
+ await writeFile(join(main, 'shared.txt'), 'committed\n')
+ await commitAll(main, 'initial')
+ await runGit(main, ['worktree', 'add', '--quiet', linked, '-b', 'linked-branch'])
+ await writeFile(join(linked, 'linked-only.txt'), 'linked\n')
+
+ const data = join(root, 'data')
+ const mainStore = createSnapshotStore(data, main)
+ const linkedStore = createSnapshotStore(data, linked)
+
+ // Two worktrees of one repository are two snapshot domains, not one.
+ assert.notEqual(mainStore.repoDir, linkedStore.repoDir)
+ assert.equal(mainStore.workspaceDir, resolve(main))
+ assert.equal(linkedStore.workspaceDir, resolve(linked))
+
+ const beforeStatus = await gitOutput(main, ['status', '--porcelain=v1', '-z'])
+ const beforeLinkedStatus = await gitOutput(linked, ['status', '--porcelain=v1', '-z'])
+ const mainHead = await gitOutput(main, ['rev-parse', 'HEAD'])
+ const linkedHead = await gitOutput(linked, ['rev-parse', 'HEAD'])
+ const mainBranch = await gitOutput(main, ['branch', '--show-current'])
+ const mainRefs = await gitOutput(main, ['for-each-ref'])
+
+ const mainSnap = await captureSnapshot(mainStore, 'refs/turnrewind/wt-main', 'main worktree')
+ const linkedSnap = await captureSnapshot(linkedStore, 'refs/turnrewind/wt-linked', 'linked worktree')
+
+ // Both private repos borrow objects from the same common object store.
+ const mainAlternates = (await readFile(join(mainStore.repoDir, 'objects', 'info', 'alternates'), 'utf8')).trim()
+ const linkedAlternates = (await readFile(join(linkedStore.repoDir, 'objects', 'info', 'alternates'), 'utf8')).trim()
+ assert.equal(mainAlternates, linkedAlternates)
+ assert.match(mainAlternates, /main[\\/]\.git[\\/]objects/)
+
+ // Each ref exists only in its own snapshot repository.
+ assert.ok(await gitRef(mainStore.repoDir, main, 'refs/turnrewind/wt-main'))
+ assert.equal(await gitRef(linkedStore.repoDir, linked, 'refs/turnrewind/wt-main'), undefined)
+ assert.ok(await gitRef(linkedStore.repoDir, linked, 'refs/turnrewind/wt-linked'))
+ assert.equal(await gitRef(mainStore.repoDir, main, 'refs/turnrewind/wt-linked'), undefined)
+
+ // The linked worktree's own file is captured only by its own store.
+ assert.equal((await stateAt(linkedStore, linkedSnap.commit, 'linked-only.txt')).kind, 'file')
+ assert.equal((await stateAt(mainStore, mainSnap.commit, 'linked-only.txt')).kind, 'absent')
+
+ // Neither capture touched the user repository: same HEADs, refs and
+ // porcelain status in both worktrees.
+ assert.equal(await gitOutput(main, ['rev-parse', 'HEAD']), mainHead)
+ assert.equal(await gitOutput(linked, ['rev-parse', 'HEAD']), linkedHead)
+ assert.equal(await gitOutput(main, ['branch', '--show-current']), mainBranch)
+ assert.equal((await gitOutput(linked, ['branch', '--show-current'])).trim(), 'linked-branch')
+ assert.equal(await gitOutput(main, ['status', '--porcelain=v1', '-z']), beforeStatus)
+ assert.equal(await gitOutput(linked, ['status', '--porcelain=v1', '-z']), beforeLinkedStatus)
+ assert.equal(await gitOutput(main, ['for-each-ref']), mainRefs)
+
+ // A session started inside a subdirectory of the linked worktree joins
+ // that worktree's snapshot domain, not the main worktree's.
+ await mkdir(join(linked, 'sub'))
+ const subStore = createSnapshotStore(data, join(linked, 'sub'))
+ assert.equal(subStore.repoDir, linkedStore.repoDir)
+ assert.equal(subStore.workspaceDir, resolve(linked))
+ }
+ finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
+
+it('captures and restores through alternates when the source objects are packed', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-worktree-packed-'))
+ const main = join(root, 'main')
+ const linked = join(root, 'linked')
+ try {
+ await initGitWorkspace(main)
+ await writeFile(join(main, 'packed.txt'), 'packed content\n')
+ await commitAll(main, 'initial')
+ // Pack the source repository so the reused blobs live in packfiles, the
+ // common real-world shape alternates have to read through.
+ await runGit(main, ['gc', '--quiet', '--prune=now'])
+ await runGit(main, ['worktree', 'add', '--quiet', linked, '-b', 'linked-branch'])
+
+ const store = createSnapshotStore(join(root, 'data'), linked)
+ const before = await captureSnapshot(store, 'refs/turnrewind/packed-before', 'before')
+
+ await writeFile(join(linked, 'packed.txt'), 'changed\n')
+ const after = await captureSnapshot(store, 'refs/turnrewind/packed-after', 'after', before.commit)
+ assert.notEqual(before.commit, after.commit)
+
+ // The baseline blob is the source's packed object, read through the
+ // alternates link, and restorePath writes those exact bytes back.
+ await restorePath(store, before.commit, 'packed.txt')
+ assert.equal(await readFile(join(linked, 'packed.txt'), 'utf8'), 'packed content\n')
+ }
+ finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})
diff --git a/plugins/dsh-tauri-turnrewind/test/maintenance.test.js b/plugins/dsh-tauri-turnrewind/test/maintenance.test.js
index 7e378589..1f487f39 100644
--- a/plugins/dsh-tauri-turnrewind/test/maintenance.test.js
+++ b/plugins/dsh-tauri-turnrewind/test/maintenance.test.js
@@ -1,12 +1,13 @@
import assert from 'node:assert/strict'
import { existsSync } from 'node:fs'
-import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
+import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { it } from 'vitest'
-import { workspaceHash } from '../lib/core/git-snapshot.js'
+import { captureSnapshot, createSnapshotStore, workspaceHash, workspaceKey } from '../lib/core/git-snapshot.js'
import { createOperation, getTurn, insertTurn, openLedger, queueRewindNotice, registerWorkspace, settleTurn } from '../lib/core/ledger.js'
import { purgeWorkspace } from '../lib/core/maintenance.js'
+import { commitAll, gitOutput, initGitWorkspace } from './git-test-utils.js'
it('purges ledger rows and the snapshot repository for one workspace', async () => {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-purge-test-'))
@@ -67,3 +68,62 @@ it('purges ledger rows and the snapshot repository for one workspace', async ()
await rm(root, { recursive: true, force: true })
}
})
+
+it('purges only turnrewind data; the user repository and other workspaces stay intact', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-purge-safety-'))
+ const dataRoot = join(root, 'dsh-home')
+ const userProject = join(root, 'user-project')
+ const otherProject = join(root, 'other-project')
+ try {
+ await initGitWorkspace(userProject)
+ await initGitWorkspace(otherProject)
+ await writeFile(join(userProject, 'tracked.txt'), 'v1\n')
+ await commitAll(userProject, 'initial')
+ const userHead = await gitOutput(userProject, ['rev-parse', 'HEAD'])
+ const userStatus = await gitOutput(userProject, ['status', '--porcelain=v1', '-z'])
+
+ const userStore = createSnapshotStore(dataRoot, userProject)
+ await captureSnapshot(userStore, 'refs/turnrewind/purge-before', 'before')
+ const otherStore = createSnapshotStore(dataRoot, otherProject)
+ await captureSnapshot(otherStore, 'refs/turnrewind/other-before', 'other')
+
+ const db = openLedger(dataRoot)
+ registerWorkspace(db, workspaceKey(userProject), userProject, userStore.repoDir)
+ registerWorkspace(db, workspaceKey(otherProject), otherProject, otherStore.repoDir)
+ insertTurn(db, {
+ turnId: 'user-session:1',
+ sessionId: 'user-session',
+ workspaceKey: workspaceKey(userProject),
+ startedAt: '2026-01-01T00:00:00.000Z',
+ beforeRef: 'refs/turnrewind/purge-before',
+ })
+ db.close()
+
+ const summary = purgeWorkspace(dataRoot, userProject)
+ assert.equal(summary.repoExisted, true)
+ assert.deepEqual(summary.ledger, { operations: 0, notices: 0, turns: 1, workspaces: 1 })
+ assert.equal(existsSync(userStore.repoDir), false)
+
+ // The user's repository is completely untouched: .git directory, HEAD,
+ // porcelain status and worktree files all survive the purge.
+ assert.equal(existsSync(join(userProject, '.git')), true)
+ assert.equal(await gitOutput(userProject, ['rev-parse', 'HEAD']), userHead)
+ assert.equal(await gitOutput(userProject, ['status', '--porcelain=v1', '-z']), userStatus)
+ assert.equal(await readFile(join(userProject, 'tracked.txt'), 'utf8'), 'v1\n')
+
+ // The purge removed exactly this workspace's data; the other workspace's
+ // snapshot repository and ledger rows survive.
+ assert.equal(existsSync(otherStore.repoDir), true)
+ const verify = openLedger(dataRoot)
+ try {
+ assert.equal(verify.prepare('SELECT COUNT(*) AS count FROM workspaces').get().count, 1)
+ assert.equal(verify.prepare('SELECT COUNT(*) AS count FROM turns').get().count, 0)
+ }
+ finally {
+ verify.close()
+ }
+ }
+ finally {
+ await rm(root, { recursive: true, force: true })
+ }
+})