DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
test(turnrewind): assert the source repository stays untouched
yingtianlan08ac4eec5343
diff --git a/docs/TURN_REWIND_GIT_DIR_PROGRESS.md b/docs/TURN_REWIND_GIT_DIR_PROGRESS.mdindex da078d47..685e1a35 100644--- a/docs/TURN_REWIND_GIT_DIR_PROGRESS.md+++ b/docs/TURN_REWIND_GIT_DIR_PROGRESS.md@@ -59,11 +59,15 @@ DSH_HOME- `security.test.js` 的敏感文件测试已改写为新语义:ignore 规则委托给源仓库(被 `.gitignore` 忽略的文件不进快照;未忽略的 secret 命名文件会被捕获,token.ts 等合法源码不再被误伤);- `guard.test.js` 已重写为:Git-required(普通目录拒绝)、Git 根目录 canonicalization(子目录 session 归并到同一 worktree/snapshot repo)、系统目录拒绝;- 旧 `guard.js` 中已无调用方的全目录预算扫描(`scanWithinBudget`/`assessWorkspace`/`defaultBudget` 及 `TURNREWIND_MAX_FILES`/`TURNREWIND_MAX_BYTES` 覆盖)已随测试一并删除,`guard.js` 只保留 `isSystemSensitiveWorkspace`;+- 新增 `test/git-state.test.js`:用户 Git 状态不变断言(必做项 1)——+ - 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-*` 临时文件残留;- 当前已执行的全量插件回归:```text-Test Files: 10 passed-Tests: 59 passed+Test Files: 11 passed+Tests: 61 passedFailed: 0```@@ -73,13 +77,12 @@ Failed: 0### 必须完成-1. 增加用户 Git 状态不变断言:capture 前后 `HEAD`、当前 branch、index、`git status` 均不应被插件改变;-2. 增加 `.gitignore`、`.git/info/exclude`、global ignore 和 `.gitattributes` 语义测试;-3. 解决 source object alternates、source index 初始化和 snapshot 私有对象之间的兼容性问题;-4. 给同一个 Git common-dir 下的 linked worktree 增加 snapshot 隔离测试;-5. 更新 `maintenance.js`、`purge-workspace.js`、ledger workspace metadata 和 purge 测试,保证只清理 turnrewind 的 snapshot repo,不误删用户 `.git`;-6. 更新插件 README、`docs/TURN_REWIND.md` 和生产审计报告,明确本模式是实验分支、Git-only workspace 和仍未解决的风险(README 中的预算守卫/`TURNREWIND_MAX_*` 章节已过时);-7. 运行完整插件回归、lint、Node import smoke,以及必要的真实 DSH Host 验证。+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 验证。### 仍需保留的安全边界diff --git a/plugins/dsh-tauri-turnrewind/test/git-state.test.js b/plugins/dsh-tauri-turnrewind/test/git-state.test.jsnew file mode 100644index 00000000..9077b88d--- /dev/null+++ b/plugins/dsh-tauri-turnrewind/test/git-state.test.js@@ -0,0 +1,107 @@+import assert from 'node:assert/strict'+import { mkdtemp, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { it } from 'vitest'+import { captureSnapshot, createSnapshotStore, restorePath } from '../lib/core/git-snapshot.js'+import { commitAll, gitOutput, initGitWorkspace } from './git-test-utils.js'++/**+ * The full logical state of the user's source repository. If any snapshot or+ * restore command leaked into the real HEAD, branch, index, refs, stash or+ * commit history, one of these fields would drift.+ */+async function sourceRepoState(workspace) {+ return {+ head: await gitOutput(workspace, ['rev-parse', 'HEAD']),+ branch: await gitOutput(workspace, ['branch', '--show-current']),+ symbolicRef: await gitOutput(workspace, ['symbolic-ref', 'HEAD']),+ status: await gitOutput(workspace, ['status', '--porcelain=v1', '-z']),+ index: await gitOutput(workspace, ['ls-files', '-s']),+ staged: await gitOutput(workspace, ['diff', '--cached']),+ refs: await gitOutput(workspace, ['for-each-ref']),+ history: await gitOutput(workspace, ['log', '--oneline', '--all']),+ stash: await gitOutput(workspace, ['stash', 'list']),+ }+}++/**+ * A deliberately dirty repository: one commit, one staged change, one+ * unstaged change on the same file and one untracked file. A clean repo+ * could hide index pollution; this state cannot.+ */+async function setupDirtyWorkspace(root) {+ const workspace = join(root, 'workspace')+ await initGitWorkspace(workspace)+ await writeFile(join(workspace, 'tracked.txt'), 'v1\n')+ await commitAll(workspace, 'initial')+ await writeFile(join(workspace, 'tracked.txt'), 'staged v2\n')+ await gitOutput(workspace, ['add', 'tracked.txt'])+ await writeFile(join(workspace, 'tracked.txt'), 'unstaged v3\n')+ await writeFile(join(workspace, 'untracked.txt'), 'untracked\n')+ return workspace+}++async function workspaceEntries(workspace) {+ return (await readdir(workspace)).filter(entry => entry !== '.git').sort()+}++it('captures snapshots without touching HEAD, branch, index, status, refs or stash', async () => {+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-git-state-'))+ try {+ const workspace = await setupDirtyWorkspace(root)+ const before = await sourceRepoState(workspace)++ const store = createSnapshotStore(join(root, 'data'), workspace)+ const first = await captureSnapshot(store, 'refs/turnrewind/git-state-before', 'before')+ // A second capture with a parent exercises the incremental path; the+ // workspace itself is untouched between captures so the full logical+ // state, including `git status`, must stay byte-identical.+ const second = await captureSnapshot(store, 'refs/turnrewind/git-state-after', 'after', first.commit)+ assert.notEqual(first.commit, second.commit)++ assert.deepEqual(await sourceRepoState(workspace), before)+ assert.deepEqual(await workspaceEntries(workspace), ['tracked.txt', 'untracked.txt'])++ // Snapshot refs live only under refs/turnrewind/ in the private repo...+ const snapshotRefs = await gitOutput(workspace, ['--git-dir', store.repoDir, 'for-each-ref'])+ assert.match(snapshotRefs, /refs\/turnrewind\//)+ assert.doesNotMatch(snapshotRefs, /refs\/(heads|tags|remotes)\//)+ // ...and never leak into the user's ref namespace.+ assert.doesNotMatch(before.refs, /turnrewind/)+ }+ finally {+ await rm(root, { recursive: true, force: true })+ }+})++it('restores worktree files while the index, HEAD, refs and stash stay untouched', async () => {+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-git-state-restore-'))+ try {+ const workspace = await setupDirtyWorkspace(root)+ const store = createSnapshotStore(join(root, 'data'), workspace)+ const baseline = await captureSnapshot(store, 'refs/turnrewind/restore-before', 'before')+ const stateBefore = await sourceRepoState(workspace)++ // The "turn": rewrite a tracked file and add a new one.+ await writeFile(join(workspace, 'tracked.txt'), 'turn output\n')+ await writeFile(join(workspace, 'turn-output.txt'), 'output\n')+ await captureSnapshot(store, 'refs/turnrewind/restore-after', 'after', baseline.commit)++ await restorePath(store, baseline.commit, 'tracked.txt')+ await restorePath(store, baseline.commit, 'turn-output.txt')++ // The undo restored the pre-turn worktree content ('unstaged v3'), not+ // the staged blob: snapshots capture the filesystem, never the index.+ assert.equal(await readFile(join(workspace, 'tracked.txt'), 'utf8'), 'unstaged v3\n')+ await assert.rejects(stat(join(workspace, 'turn-output.txt')))+ assert.deepEqual(await workspaceEntries(workspace), ['tracked.txt', 'untracked.txt'])++ // The index still holds the staged 'v2' blob, so the porcelain state is+ // byte-identical: same MM entry for tracked.txt, same untracked file.+ assert.deepEqual(await sourceRepoState(workspace), stateBefore)+ }+ 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.jsindex 7453bcfb..c0b35e74 100644--- a/plugins/dsh-tauri-turnrewind/test/git-test-utils.js+++ b/plugins/dsh-tauri-turnrewind/test/git-test-utils.js@@ -18,6 +18,29 @@ export function runGit(cwd, args) {})}+export function gitOutput(cwd, args) {+ return new Promise((resolvePromise, rejectPromise) => {+ const child = spawn('git', args, { cwd })+ const chunks = []+ const errors = []+ child.stdout.on('data', chunk => chunks.push(chunk))+ child.stderr.on('data', chunk => errors.push(chunk))+ child.on('error', rejectPromise)+ child.on('close', (code) => {+ if (code !== 0) {+ rejectPromise(new Error(`git ${args.join(' ')} failed: ${Buffer.concat(errors).toString('utf8').trim()}`))+ return+ }+ resolvePromise(Buffer.concat(chunks).toString('utf8'))+ })+ })+}++export async function commitAll(workspace, message) {+ await runGit(workspace, ['add', '--all'])+ await runGit(workspace, ['commit', '--quiet', '-m', message])+}+export async function initGitWorkspace(workspace) {await mkdir(workspace, { recursive: true })await runGit(workspace, ['init', '--quiet'])