Skip to content

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

test(turnrewind): migrate remaining fixtures to git workspaces

yingtianlanf93756f09288
diff --git a/docs/TURN_REWIND_GIT_DIR_PROGRESS.md b/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
index e10fc9c4..da078d47 100644
--- a/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
+++ b/docs/TURN_REWIND_GIT_DIR_PROGRESS.md
@@ -55,12 +55,15 @@ DSH_HOME
### 测试
- 新增 `test/git-test-utils.js`,用于创建临时 Git workspace;
-- `git-snapshot.test.js` 的 fixture 已开始迁移到真实 Git workspace;
-- 当前已执行的 Git snapshot 目标测试:
+- `git-snapshot.test.js`、`lifecycle.test.js`、`barrier.test.js`、`security.test.js` 的 fixture 已全部迁移到真实临时 Git workspace;
+- `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`;
+- 当前已执行的全量插件回归:
```text
-Test Files: 1 passed
-Tests: 10 passed
+Test Files: 10 passed
+Tests: 59 passed
Failed: 0
```
@@ -70,15 +73,13 @@ Failed: 0
### 必须完成
-1. 把 `lifecycle.test.js`、`barrier.test.js`、`security.test.js` 和其他 snapshot fixture 全部迁移到真实临时 Git workspace;
-2. 重写 `guard.test.js`:从旧的文件数量/总大小/目录深度预算测试改为 Git-required、Git 根目录 canonicalization、普通目录拒绝和系统目录拒绝测试;
-3. 增加用户 Git 状态不变断言:capture 前后 `HEAD`、当前 branch、index、`git status` 均不应被插件改变;
-4. 增加 `.gitignore`、`.git/info/exclude`、global ignore 和 `.gitattributes` 语义测试;
-5. 解决 source object alternates、source index 初始化和 snapshot 私有对象之间的兼容性问题;
-6. 给同一个 Git common-dir 下的 linked worktree 增加 snapshot 隔离测试;
-7. 更新 `maintenance.js`、`purge-workspace.js`、ledger workspace metadata 和 purge 测试,保证只清理 turnrewind 的 snapshot repo,不误删用户 `.git`;
-8. 更新插件 README、`docs/TURN_REWIND.md` 和生产审计报告,明确本模式是实验分支、Git-only workspace 和仍未解决的风险;
-9. 运行完整插件回归、lint、Node import smoke,以及必要的真实 DSH Host 验证。
+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 验证。
### 仍需保留的安全边界
diff --git a/plugins/dsh-tauri-turnrewind/lib/core/guard.js b/plugins/dsh-tauri-turnrewind/lib/core/guard.js
index 961459e1..363143db 100644
--- a/plugins/dsh-tauri-turnrewind/lib/core/guard.js
+++ b/plugins/dsh-tauri-turnrewind/lib/core/guard.js
@@ -1,25 +1,7 @@
-import { lstatSync, readdirSync, realpathSync } from 'node:fs'
+import { realpathSync } from 'node:fs'
import { homedir } from 'node:os'
-import { join, parse, relative, resolve, sep } from 'node:path'
+import { parse, resolve, sep } from 'node:path'
import process from 'node:process'
-import { MAX_FILE_BYTES } from './git-snapshot.js'
-
-const DEFAULT_MAX_FILES = 50_000
-const DEFAULT_MAX_TOTAL_BYTES = 1024 ** 3
-
-// Mirror the directory part of the snapshot excludes so the guard measures what
-// `git add` would actually ingest instead of the raw tree.
-const SKIP_DIR_NAMES = new Set(['.git', 'node_modules', 'dist', 'build', 'coverage', '.turnrewind'])
-
-const OVERSIZED_SAMPLE_LIMIT = 5
-
-function envCount(name, fallback) {
- const raw = process.env[name]
- if (!raw)
- return fallback
- const value = Number(raw)
- return Number.isInteger(value) && value > 0 ? value : fallback
-}
function normalizeDir(path) {
const resolved = resolve(path)
@@ -36,14 +18,6 @@ function foldCase(path) {
return process.platform === 'win32' ? path.toLowerCase() : path
}
-export function defaultBudget() {
- return {
- maxFiles: envCount('TURNREWIND_MAX_FILES', DEFAULT_MAX_FILES),
- maxTotalBytes: envCount('TURNREWIND_MAX_BYTES', DEFAULT_MAX_TOTAL_BYTES),
- maxFileBytes: MAX_FILE_BYTES,
- }
-}
-
export function isSystemSensitiveWorkspace(workspaceDir) {
const workspace = foldCase(normalizeDir(workspaceDir))
const home = foldCase(normalizeDir(homedir()))
@@ -55,87 +29,3 @@ export function isSystemSensitiveWorkspace(workspaceDir) {
return true
return parse(workspace).root === workspace
}
-
-class BudgetAbort extends Error {}
-
-/**
- * Metadata-only walk that aborts as soon as a budget is exceeded, so even a
- * 250 GB home directory costs at most one bounded scan instead of a full
- * `git add` baseline.
- */
-export function scanWithinBudget(workspaceDir, budget = defaultBudget()) {
- const root = resolve(workspaceDir)
- const state = { files: 0, bytes: 0, oversized: [] }
-
- const visit = (dir) => {
- let entries
- try {
- entries = readdirSync(dir, { withFileTypes: true })
- }
- catch {
- // Subtrees that cannot be listed are equally unusable for snapshots.
- return
- }
- for (const entry of entries) {
- if (entry.isDirectory()) {
- if (!SKIP_DIR_NAMES.has(entry.name))
- visit(join(dir, entry.name))
- continue
- }
- // Symlinks and junctions are never followed, mirroring restorePath.
- if (!entry.isFile())
- continue
- const path = join(dir, entry.name)
- const info = lstatSync(path, { throwIfNoEntry: false })
- if (!info)
- continue
- state.files += 1
- state.bytes += info.size
- if (info.size > budget.maxFileBytes && state.oversized.length < OVERSIZED_SAMPLE_LIMIT)
- state.oversized.push(relative(root, path))
- if (state.files > budget.maxFiles || state.bytes > budget.maxTotalBytes)
- throw new BudgetAbort()
- }
- }
-
- try {
- visit(root)
- }
- catch (error) {
- if (!(error instanceof BudgetAbort))
- throw error
- }
-
- const reason = state.files > budget.maxFiles
- ? `file count ${state.files} exceeds the limit ${budget.maxFiles}`
- : state.bytes > budget.maxTotalBytes
- ? `total size ${state.bytes} bytes exceeds the limit ${budget.maxTotalBytes}`
- : state.oversized.length > 0
- ? `files larger than ${budget.maxFileBytes} bytes can never be restored: ${state.oversized.join(', ')}`
- : undefined
- return {
- files: state.files,
- bytes: state.bytes,
- oversized: state.oversized,
- ok: reason === undefined,
- reason,
- }
-}
-
-export function assessWorkspace(workspaceDir) {
- const workspace = resolve(workspaceDir)
- if (isSystemSensitiveWorkspace(workspace)) {
- return {
- eligible: false,
- reason: `TURNREWIND_WORKSPACE_UNSUPPORTED: ${workspace} is a system directory (the home directory, one of its ancestors, or a drive root)`,
- }
- }
- const scan = scanWithinBudget(workspace)
- if (!scan.ok) {
- return {
- eligible: false,
- reason: `TURNREWIND_WORKSPACE_TOO_LARGE: ${workspace} exceeds the snapshot budget (${scan.reason})`,
- }
- }
- return { eligible: true, files: scan.files, bytes: scan.bytes }
-}
diff --git a/plugins/dsh-tauri-turnrewind/test/barrier.test.js b/plugins/dsh-tauri-turnrewind/test/barrier.test.js
index 2408246a..4ed67003 100644
--- a/plugins/dsh-tauri-turnrewind/test/barrier.test.js
+++ b/plugins/dsh-tauri-turnrewind/test/barrier.test.js
@@ -1,10 +1,11 @@
import assert from 'node:assert/strict'
-import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
+import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { it } from 'vitest'
import { createSnapshotStore, gitRef } from '../lib/core/git-snapshot.js'
import { apply, turnSnapshotRef } from '../lib/index.js'
+import { initGitWorkspace } from './git-test-utils.js'
function createHarnessContext() {
const events = new Map()
@@ -63,7 +64,7 @@ async function withHarness(test) {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-barrier-test-'))
const workspace = join(root, 'workspace')
const dshHome = join(root, 'dsh-home')
- await mkdir(workspace, { recursive: true })
+ await initGitWorkspace(workspace)
await writeFile(join(workspace, 'before.txt'), 'before\n')
const previousHome = process.env.DSH_HOME
diff --git a/plugins/dsh-tauri-turnrewind/test/guard.test.js b/plugins/dsh-tauri-turnrewind/test/guard.test.js
index d77111ad..40d9f7ce 100644
--- a/plugins/dsh-tauri-turnrewind/test/guard.test.js
+++ b/plugins/dsh-tauri-turnrewind/test/guard.test.js
@@ -1,16 +1,11 @@
import assert from 'node:assert/strict'
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { homedir, tmpdir } from 'node:os'
-import { dirname, join, parse } from 'node:path'
-import { afterEach, it } from 'vitest'
-import { assessWorkspace, isSystemSensitiveWorkspace, scanWithinBudget } from '../lib/core/guard.js'
-
-const LARGE_BUDGET = { maxFiles: 50_000, maxTotalBytes: 1024 ** 3, maxFileBytes: 64 * 1024 * 1024 }
-
-afterEach(() => {
- delete process.env.TURNREWIND_MAX_FILES
- delete process.env.TURNREWIND_MAX_BYTES
-})
+import { dirname, join, parse, resolve } from 'node:path'
+import { it } from 'vitest'
+import { createSnapshotStore, probeWorkspace } from '../lib/core/git-snapshot.js'
+import { isSystemSensitiveWorkspace } from '../lib/core/guard.js'
+import { initGitWorkspace } from './git-test-utils.js'
it('refuses the home directory, its ancestors, and drive roots', () => {
assert.equal(isSystemSensitiveWorkspace(homedir()), true)
@@ -19,91 +14,57 @@ it('refuses the home directory, its ancestors, and drive roots', () => {
assert.equal(isSystemSensitiveWorkspace(parse(tmpdir()).root), true)
})
-it('accepts an ordinary small workspace', async () => {
- const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-ok-'))
+it('refuses plain directories that are not Git worktrees', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-plain-'))
try {
await writeFile(join(root, 'a.txt'), 'a')
- await mkdir(join(root, 'nested'), { recursive: true })
- await writeFile(join(root, 'nested', 'b.txt'), 'b')
- const assessment = assessWorkspace(root)
- assert.equal(assessment.eligible, true)
- assert.equal(assessment.files, 2)
- assert.equal(assessment.bytes, 2)
+ const probe = probeWorkspace(root)
+ assert.equal(probe.ok, false)
+ assert.match(probe.reason, /TURNREWIND_GIT_REQUIRED/)
+ assert.throws(
+ () => createSnapshotStore(join(root, 'data'), root),
+ /TURNREWIND_GIT_REQUIRED/,
+ )
}
finally {
await rm(root, { recursive: true, force: true })
}
})
-it('refuses workspaces over the file-count budget', async () => {
- const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-files-'))
+it('accepts an ordinary Git worktree', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-git-'))
+ const workspace = join(root, 'workspace')
try {
- await mkdir(join(root, 'd'), { recursive: true })
- for (let index = 0; index < 5; index += 1)
- await writeFile(join(root, 'd', `f${index}.txt`), 'x')
- const scan = scanWithinBudget(root, { ...LARGE_BUDGET, maxFiles: 3 })
- assert.equal(scan.ok, false)
- assert.match(scan.reason, /file count/)
+ await initGitWorkspace(workspace)
+ await writeFile(join(workspace, 'a.txt'), 'a')
+ const probe = probeWorkspace(workspace)
+ assert.equal(probe.ok, true)
+ assert.equal(probe.workspaceDir, resolve(workspace))
}
finally {
await rm(root, { recursive: true, force: true })
}
})
-it('refuses workspaces over the total-size budget', async () => {
- const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-bytes-'))
+it('canonicalizes a session cwd below the worktree root to the Git root', async () => {
+ const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-subdir-'))
+ const workspace = join(root, 'workspace')
+ const nested = join(workspace, 'packages', 'app')
try {
- await writeFile(join(root, 'big.bin'), '0123456789'.repeat(10))
- const scan = scanWithinBudget(root, { ...LARGE_BUDGET, maxTotalBytes: 50 })
- assert.equal(scan.ok, false)
- assert.match(scan.reason, /total size/)
- }
- finally {
- await rm(root, { recursive: true, force: true })
- }
-})
-
-it('refuses files that could never be restored', async () => {
- const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-big-'))
- try {
- await writeFile(join(root, 'huge.bin'), '0123456789ABCDEF')
- const scan = scanWithinBudget(root, { ...LARGE_BUDGET, maxFileBytes: 10 })
- assert.equal(scan.ok, false)
- assert.match(scan.reason, /larger than 10 bytes/)
- assert.deepEqual(scan.oversized, ['huge.bin'])
- }
- finally {
- await rm(root, { recursive: true, force: true })
- }
-})
+ await initGitWorkspace(workspace)
+ await mkdir(nested, { recursive: true })
+ await writeFile(join(nested, 'a.txt'), 'a')
-it('ignores excluded directories when measuring the budget', async () => {
- const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-skip-'))
- try {
- await mkdir(join(root, 'node_modules', 'pkg'), { recursive: true })
- await mkdir(join(root, '.git'), { recursive: true })
- for (let index = 0; index < 10; index += 1) {
- await writeFile(join(root, 'node_modules', 'pkg', `f${index}.txt`), 'x')
- await writeFile(join(root, '.git', `obj${index}`), 'x')
- }
- const scan = scanWithinBudget(root, { ...LARGE_BUDGET, maxFiles: 5 })
- assert.equal(scan.ok, true)
- assert.equal(scan.files, 0)
- }
- finally {
- await rm(root, { recursive: true, force: true })
- }
-})
+ // A session started from a subdirectory still belongs to the one Git
+ // worktree: it must not become a second snapshot domain.
+ const probe = probeWorkspace(nested)
+ assert.equal(probe.ok, true)
+ assert.equal(probe.workspaceDir, resolve(workspace))
-it('honors TURNREWIND_MAX_FILES via assessWorkspace', async () => {
- const root = await mkdtemp(join(tmpdir(), 'turnrewind-guard-env-'))
- try {
- for (let index = 0; index < 3; index += 1)
- await writeFile(join(root, `f${index}.txt`), 'x')
- process.env.TURNREWIND_MAX_FILES = '2'
- const assessment = assessWorkspace(root)
- assert.equal(assessment.eligible, false)
- assert.match(assessment.reason, /TURNREWIND_WORKSPACE_TOO_LARGE/)
+ const nestedStore = createSnapshotStore(join(root, 'data'), nested)
+ const rootStore = createSnapshotStore(join(root, 'data'), workspace)
+ assert.equal(nestedStore.repoDir, rootStore.repoDir)
+ assert.equal(nestedStore.workspaceDir, resolve(workspace))
}
finally {
await rm(root, { recursive: true, force: true })
diff --git a/plugins/dsh-tauri-turnrewind/test/security.test.js b/plugins/dsh-tauri-turnrewind/test/security.test.js
index 58ee83eb..ef53c6c6 100644
--- a/plugins/dsh-tauri-turnrewind/test/security.test.js
+++ b/plugins/dsh-tauri-turnrewind/test/security.test.js
@@ -4,16 +4,27 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { it } from 'vitest'
import { captureSnapshot, createSnapshotStore, currentState, restorePath, stateAt } from '../lib/core/git-snapshot.js'
+import { initGitWorkspace } from './git-test-utils.js'
-it('does not capture common secret files into a snapshot', async () => {
+it('delegates secret-file exclusion to the source repository ignore rules', async () => {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-secret-test-'))
const workspace = join(root, 'workspace')
try {
- await mkdir(workspace, { recursive: true })
+ await initGitWorkspace(workspace)
+ await writeFile(join(workspace, '.gitignore'), [
+ '.env',
+ 'credentials.json',
+ 'secrets.yaml',
+ 'server.pem',
+ 'config/credentials.json',
+ 'nested/secrets.yaml',
+ '',
+ ].join('\n'))
await writeFile(join(workspace, '.env'), 'TOKEN=do-not-store')
await writeFile(join(workspace, 'credentials.json'), '{"token":"do-not-store"}')
await writeFile(join(workspace, 'secrets.yaml'), 'key: do-not-store')
await writeFile(join(workspace, 'server.pem'), '-----BEGIN')
+ await writeFile(join(workspace, 'unignored.pem'), '-----BEGIN UNIGNORED')
await writeFile(join(workspace, 'safe.txt'), 'safe')
await mkdir(join(workspace, 'config'), { recursive: true })
await mkdir(join(workspace, 'nested'), { recursive: true })
@@ -22,12 +33,16 @@ it('does not capture common secret files into a snapshot', async () => {
const store = createSnapshotStore(join(root, 'data'), workspace)
const snapshot = await captureSnapshot(store, 'refs/turnrewind/security', 'security')
+ // Git mode dropped the hardcoded secret-name rules: the source repo's own
+ // ignore rules decide what a snapshot contains. An unignored secret-named
+ // file is deliberately captured — the project opted in by not ignoring it.
assert.equal((await stateAt(store, snapshot.commit, '.env')).kind, 'absent')
assert.equal((await stateAt(store, snapshot.commit, 'credentials.json')).kind, 'absent')
assert.equal((await stateAt(store, snapshot.commit, 'secrets.yaml')).kind, 'absent')
assert.equal((await stateAt(store, snapshot.commit, 'server.pem')).kind, 'absent')
assert.equal((await stateAt(store, snapshot.commit, 'config/credentials.json')).kind, 'absent')
assert.equal((await stateAt(store, snapshot.commit, 'nested/secrets.yaml')).kind, 'absent')
+ assert.equal((await stateAt(store, snapshot.commit, 'unignored.pem')).kind, 'file')
assert.equal((await stateAt(store, snapshot.commit, 'safe.txt')).kind, 'file')
const snapshotFiles = await readdir(join(root, 'data', 'snapshots'))
assert.equal(snapshotFiles.length, 1)
@@ -41,6 +56,7 @@ it('keeps legitimate source files whose names resemble secrets', async () => {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-token-test-'))
const workspace = join(root, 'workspace')
try {
+ await initGitWorkspace(workspace)
await mkdir(join(workspace, 'src'), { recursive: true })
await mkdir(join(workspace, 'docs'), { recursive: true })
await writeFile(join(workspace, 'src', 'token.ts'), 'export const token = 1')
@@ -66,6 +82,7 @@ it('excludes abandoned turnrewind temporary files from snapshots', async () => {
const root = await mkdtemp(join(tmpdir(), 'turnrewind-temp-file-test-'))
const workspace = join(root, 'workspace')
try {
+ await initGitWorkspace(workspace)
await mkdir(join(workspace, 'nested'), { recursive: true })
await writeFile(join(workspace, 'file.turnrewind-01234567-89ab-cdef-0123-456789abcdef.tmp'), 'temporary')
await writeFile(join(workspace, 'nested', 'file.turnrewind-01234567-89ab-cdef-0123-456789abcdef.tmp'), 'temporary')
@@ -85,7 +102,7 @@ it('refuses dangling symlink workspace paths during inspection and restore', asy
const root = await mkdtemp(join(tmpdir(), 'turnrewind-dangling-link-test-'))
const workspace = join(root, 'workspace')
try {
- await mkdir(workspace, { recursive: true })
+ await initGitWorkspace(workspace)
try {
await symlink(join(root, 'missing-target'), join(workspace, 'dangling'))
}
@@ -111,7 +128,7 @@ it('refuses symlinked workspace paths during inspection and restore', async () =
const workspace = join(root, 'workspace')
const outside = join(root, 'outside')
try {
- await mkdir(workspace, { recursive: true })
+ await initGitWorkspace(workspace)
await mkdir(outside, { recursive: true })
await writeFile(join(outside, 'secret.txt'), 'outside')
try {