Skip to content

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

fix(dsh-tauri-worktree): .agents 改为复制继承,修复 POSIX 上污染工作树状态

hairyf0898e165c57f
diff --git a/packages/dsh-tauri-worktree/src/host/service/worktree.test.ts b/packages/dsh-tauri-worktree/src/host/service/worktree.test.ts
index e90e3e0f..7547c7c9 100644
--- a/packages/dsh-tauri-worktree/src/host/service/worktree.test.ts
+++ b/packages/dsh-tauri-worktree/src/host/service/worktree.test.ts
@@ -399,7 +399,7 @@ describe('依赖链接', () => {
expect(created.binding.linkedDependencies).toBeUndefined()
})
- it('无条件链接 .agents,即使关闭依赖链接', async () => {
+ it('无条件继承 .agents(复制而非链接),即使关闭依赖链接', async () => {
const repository = createRepository()
mkdirSync(join(repository, '.agents', 'skills', 'handle'), { recursive: true })
writeFileSync(join(repository, '.agents', 'skills', 'handle', 'SKILL.md'), '# handle\n')
@@ -410,14 +410,14 @@ describe('依赖链接', () => {
if (!created.ok)
return
- const link = join(created.binding.worktreePath, '.agents')
- expect(lstatSync(link).isSymbolicLink()).toBe(true)
- expect(readFileSync(join(link, 'skills', 'handle', 'SKILL.md'), 'utf8')).toBe('# handle\n')
+ const agents = join(created.binding.worktreePath, '.agents')
+ expect(lstatSync(agents).isSymbolicLink()).toBe(false)
+ expect(readFileSync(join(agents, 'skills', 'handle', 'SKILL.md'), 'utf8')).toBe('# handle\n')
expect(existsSync(join(created.binding.worktreePath, 'node_modules'))).toBe(false)
const removed = await worktree.remove(sessionId)
expect(removed.ok).toBe(true)
- expect(existsSync(link)).toBe(false)
+ expect(existsSync(agents)).toBe(false)
expect(readFileSync(join(repository, '.agents', 'skills', 'handle', 'SKILL.md'), 'utf8')).toBe('# handle\n')
})
@@ -433,7 +433,7 @@ describe('依赖链接', () => {
expect(existsSync(join(created.binding.worktreePath, '.agents'))).toBe(false)
})
- it('源仓库忽略 .agents 时,链接不弄脏工作树状态', async () => {
+ it('源仓库忽略 .agents 时,继承不弄脏工作树状态(POSIX 不可用符号链接)', async () => {
const repository = createRepository()
mkdirSync(join(repository, '.agents', 'skills', 'handle'), { recursive: true })
writeFileSync(join(repository, '.agents', 'skills', 'handle', 'SKILL.md'), '# handle\n')
@@ -447,8 +447,10 @@ describe('依赖链接', () => {
if (!created.ok)
return
+ // POSIX 上符号链接对 git 是「文件」,`.agents/skills/` 忽略规则匹配不到它,
+ // 会留下 `?? .agents`;复制成真实目录后状态干净。
expect(git(created.binding.worktreePath, 'status', '--porcelain=v1')).toBe('')
- expect(lstatSync(join(created.binding.worktreePath, '.agents')).isSymbolicLink()).toBe(true)
+ expect(lstatSync(join(created.binding.worktreePath, '.agents')).isSymbolicLink()).toBe(false)
})
it('部分内容已跟踪时,补齐被忽略的 .agents/skills 子目录', async () => {
@@ -467,11 +469,10 @@ describe('依赖链接', () => {
return
const skills = join(created.binding.worktreePath, '.agents', 'skills')
- expect(lstatSync(skills).isSymbolicLink()).toBe(true)
expect(readFileSync(join(skills, 'handle', 'SKILL.md'), 'utf8')).toBe('# handle\n')
- // `.agents` 本身必须是工作树里的真实目录,链接子项不能破坏被跟踪的 config.json
expect(lstatSync(join(created.binding.worktreePath, '.agents')).isSymbolicLink()).toBe(false)
expect(readFileSync(join(created.binding.worktreePath, '.agents', 'config.json'), 'utf8').trim()).toBe('{}')
+ expect(git(created.binding.worktreePath, 'status', '--porcelain=v1')).toBe('')
const removed = await worktree.remove(sessionId)
expect(removed.ok).toBe(true)
diff --git a/packages/dsh-tauri-worktree/src/host/service/worktree.ts b/packages/dsh-tauri-worktree/src/host/service/worktree.ts
index ad1cc3c6..2ab9ff7b 100644
--- a/packages/dsh-tauri-worktree/src/host/service/worktree.ts
+++ b/packages/dsh-tauri-worktree/src/host/service/worktree.ts
@@ -14,8 +14,8 @@ import { join, resolve } from 'pathe'
import { TRASH_DIR, WORKTREES_DIR } from '../config/constants'
import { getCurrentHostInstance } from '../config/runtime'
import {
+ copyMissingChildren,
isDependencyInstallCommand,
- linkMissingChildren,
linkWorktreeDependencies,
normalizeLinkDirectories,
shellCommandFrom,
@@ -78,9 +78,9 @@ export const worktree = defineService({
if (!registration.ok)
return { ok: false, error: `检查工作树残留状态失败:${registration.error}` }
if (registration.registered) {
- // 旧版本创建的工作树没有这条链接,重复创建时补齐,让继承会话能读到项目级技能
+ // 旧版本创建的工作树没有这份拷贝,重复创建时补齐,让继承会话能读到项目级技能
const repairedLog: string[] = []
- await linkAgentSkills(root, path, repairedLog)
+ await inheritAgentSkills(root, path, repairedLog)
if (repairedLog.length === 0)
return { ok: true, binding: existing, existed: true, log: [] }
const repaired: Binding = { ...existing, log: [...existing.log, ...repairedLog] }
@@ -155,7 +155,7 @@ export const worktree = defineService({
log.push(`HEAD is now at ${await shortHead(path)} ${await headSubject(path)}`)
log.push(`Worktree created at ${path}`)
- await linkAgentSkills(root, path, log)
+ await inheritAgentSkills(root, path, log)
const linkedDependencies: string[] = []
if (linkDependencies) {
@@ -482,31 +482,24 @@ async function pruneWorktreeAdmin(root: string, signal?: AbortSignal): Promise<O
}
/**
- * 把源仓库的 `.agents` 链接进工作树:技能目录被 gitignore,`git worktree add` 不会带过来,
- * 不补这条链接,继承会话就读不到初始会话的项目级技能。源仓库没有 `.agents` 时静默返回。
+ * 把源仓库的 `.agents` 复制进工作树。技能目录被 gitignore,`git worktree add` 不会带过来,
+ * 不补这一步,继承会话就读不到初始会话的项目级技能。
+ *
+ * 用复制而不是符号链接:POSIX 上符号链接对 git 是「文件」,`.agents/skills/` 这类带尾斜杠的
+ * 忽略规则匹配不到它,工作树会多出未跟踪记录 `?? .agents`,既污染状态又会在检出时被判为
+ * 「存在未跟踪改动」。源仓库没有 `.agents` 时静默返回。
*/
-async function linkAgentSkills(root: string, path: string, log: string[]): Promise<void> {
+async function inheritAgentSkills(root: string, path: string, log: string[]): Promise<void> {
const source = join(root, AGENT_SKILLS_DIRECTORY)
if (!existsSync(source))
return
try {
- const linked = await linkWorktreeDependencies(root, path, [AGENT_SKILLS_DIRECTORY])
- if (linked.linked.length > 0) {
- log.push(`Linked the agent skills directory from the source repository (${AGENT_SKILLS_DIRECTORY})`)
- return
- }
- if (linked.skipped.includes(AGENT_SKILLS_DIRECTORY)) {
- // `.agents` 已在工作树里(部分内容被提交进索引)时无法整体链接,
- // 改为补齐被 gitignore 掉、worktree add 搬不过来的子目录(如 skills)。
- const children = await linkMissingChildren(source, join(path, AGENT_SKILLS_DIRECTORY))
- if (children.length > 0)
- log.push(`Linked the missing agent directories from the source repository (${children.join(', ')})`)
- return
- }
- log.push('Agent skills directory link skipped')
+ const copied = await copyMissingChildren(source, join(path, AGENT_SKILLS_DIRECTORY))
+ if (copied.length > 0)
+ log.push(`Copied the agent skills directory from the source repository (${copied.join(', ')})`)
}
catch (error) {
- log.push(`Agent skills directory link skipped: ${get(error, 'message', String(error))}`)
+ log.push(`Agent skills directory copy skipped: ${get(error, 'message', String(error))}`)
}
}
@@ -578,8 +571,8 @@ async function removeWorktreeOnDisk(
function removalLinkDirectories(binding: Binding | null, configured?: readonly string[]): string[] {
const configuredDirectories = normalizeLinkDirectories(configured)
if (!binding || isEmpty(binding.linkedDependencies))
- return normalizeLinkDirectories([AGENT_SKILLS_DIRECTORY, ...configuredDirectories])
- return normalizeLinkDirectories([AGENT_SKILLS_DIRECTORY, ...binding.linkedDependencies ?? [], ...configuredDirectories])
+ return configuredDirectories
+ return normalizeLinkDirectories([...binding.linkedDependencies ?? [], ...configuredDirectories])
}
async function deleteOwnedBranch(root: string, branch: string, signal?: AbortSignal): Promise<OperationResult> {
diff --git a/packages/dsh-tauri-worktree/src/host/utils/dependencies.test.ts b/packages/dsh-tauri-worktree/src/host/utils/dependencies.test.ts
index 34dcec3a..6b3f27f0 100644
--- a/packages/dsh-tauri-worktree/src/host/utils/dependencies.test.ts
+++ b/packages/dsh-tauri-worktree/src/host/utils/dependencies.test.ts
@@ -3,6 +3,7 @@ import { tmpdir } from 'node:os'
import { join } from 'pathe'
import { afterEach, describe, expect, it } from 'vitest'
import {
+ copyMissingChildren,
isDependencyInstallCommand,
linkWorktreeDependencies,
normalizeLinkDirectories,
@@ -122,16 +123,6 @@ describe('unlinkWorktreeDependencies', () => {
expect(await readFile(marker, 'utf8')).toBe('shared-dependency\n')
})
- it('unlinks the agent skills link without touching the source skills tree', async () => {
- const { project, worktree, skill } = await createSkillsFixture()
- await expect(linkWorktreeDependencies(project, worktree, ['.agents'])).resolves.toEqual({ linked: ['.agents'], skipped: [] })
- expect(await readFile(join(worktree, '.agents', 'skills', 'handle', 'SKILL.md'), 'utf8')).toBe('# handle\n')
-
- await expect(unlinkWorktreeDependencies(worktree, ['.agents'])).resolves.toEqual(['.agents'])
- await expect(lstat(join(worktree, '.agents'))).rejects.toMatchObject({ code: 'ENOENT' })
- expect(await readFile(skill, 'utf8')).toBe('# handle\n')
- })
-
it('leaves a real (independently installed) directory untouched', async () => {
const worktree = await temporaryRoot('dsh-deps-worktree-')
const installed = join(worktree, 'node_modules', 'pkg')
@@ -147,3 +138,31 @@ describe('unlinkWorktreeDependencies', () => {
await expect(unlinkWorktreeDependencies(worktree, ['node_modules'])).resolves.toEqual([])
})
})
+
+describe('copyMissingChildren', () => {
+ it('copies a missing source directory into the target as a real directory', async () => {
+ const { project, skill } = await createSkillsFixture()
+ const worktree = await temporaryRoot('dsh-deps-worktree-')
+
+ await expect(copyMissingChildren(join(project, '.agents'), join(worktree, '.agents'))).resolves.toEqual(['skills'])
+ const copied = join(worktree, '.agents', 'skills')
+ expect((await lstat(copied)).isSymbolicLink()).toBe(false)
+ expect(await readFile(join(copied, 'handle', 'SKILL.md'), 'utf8')).toBe('# handle\n')
+ expect(await readFile(skill, 'utf8')).toBe('# handle\n')
+ })
+
+ it('skips entries already present in the target and keeps their content', async () => {
+ const { project } = await createSkillsFixture()
+ const worktree = await temporaryRoot('dsh-deps-worktree-')
+ await mkdir(join(worktree, '.agents', 'skills'), { recursive: true })
+ await writeFile(join(worktree, '.agents', 'skills', 'local.md'), 'local\n')
+
+ await expect(copyMissingChildren(join(project, '.agents'), join(worktree, '.agents'))).resolves.toEqual([])
+ expect(await readFile(join(worktree, '.agents', 'skills', 'local.md'), 'utf8')).toBe('local\n')
+ })
+
+ it('returns no copies when the source is absent', async () => {
+ const worktree = await temporaryRoot('dsh-deps-worktree-')
+ await expect(copyMissingChildren(join(worktree, 'missing'), join(worktree, 'target'))).resolves.toEqual([])
+ })
+})
diff --git a/packages/dsh-tauri-worktree/src/host/utils/dependencies.ts b/packages/dsh-tauri-worktree/src/host/utils/dependencies.ts
index f8c4cbaf..dd30e113 100644
--- a/packages/dsh-tauri-worktree/src/host/utils/dependencies.ts
+++ b/packages/dsh-tauri-worktree/src/host/utils/dependencies.ts
@@ -1,9 +1,8 @@
-import { existsSync } from 'node:fs'
-import { lstat, rm, symlink } from 'node:fs/promises'
+import { existsSync, readdirSync } from 'node:fs'
+import { cp, lstat, mkdir, rm, symlink } from 'node:fs/promises'
import process from 'node:process'
import { filter, find, get, isEmpty, isString, map, trimEnd, uniqBy } from 'lodash-es'
import { resolve } from 'pathe'
-import { listDirectoryNames } from './filesystem'
const DEFAULT_LINK_DIRECTORIES: readonly string[] = ['node_modules']
@@ -109,25 +108,32 @@ export async function unlinkWorktreeDependencies(
}
/**
- * 把源目录里缺失的子项逐条链接进目标目录。用于目标目录已存在(源仓库把 `.agents` 的一部分
- * 提交进了索引)而技能等子目录被 gitignore、`git worktree add` 搬不过来的情况。
+ * 把源目录里缺失的条目复制进目标目录,供 `.agents` 这类被 gitignore、`git worktree add`
+ * 搬不过来的内容使用。目标目录不存在时即整树复制,已存在时只补缺失子项(源仓库把
+ * `.agents` 的一部分提交进了索引时就是后者)。
+ *
+ * 这里刻意用复制而不是符号链接:POSIX 上符号链接对 git 而言是「文件」,`.agents/skills/`
+ * 这种带尾斜杠的忽略规则匹配不到它,工作树会多出一条未跟踪记录 `?? .agents`,
+ * 既污染状态,又会在检出时被判为「存在未跟踪改动」而卡死。复制出的真实目录与源仓库
+ * 忽略语义完全一致,平台无关。
*/
-export async function linkMissingChildren(sourceDirectory: string, targetDirectory: string): Promise<string[]> {
- if (!existsSync(sourceDirectory) || !existsSync(targetDirectory) || await isSymbolicLink(targetDirectory))
+export async function copyMissingChildren(sourceDirectory: string, targetDirectory: string): Promise<string[]> {
+ if (!existsSync(sourceDirectory) || await isSymbolicLink(targetDirectory))
return []
- const linked: string[] = []
- for (const name of listDirectoryNames(sourceDirectory)) {
+ await mkdir(targetDirectory, { recursive: true })
+ const copied: string[] = []
+ for (const name of readdirSync(sourceDirectory)) {
const source = resolve(sourceDirectory, name)
const target = resolve(targetDirectory, name)
- if (!existsSync(source) || await pathExists(target))
+ if (await pathExists(target))
continue
try {
- await symlink(source, target, process.platform === 'win32' ? 'junction' : 'dir')
- linked.push(name)
+ await cp(source, target, { recursive: true })
+ copied.push(name)
}
catch {}
}
- return linked
+ return copied
}
// --- internal ---
diff --git a/packages/dsh-tauri-worktree/src/host/utils/worktree-facts.ts b/packages/dsh-tauri-worktree/src/host/utils/worktree-facts.ts
index 95cb915e..ae889379 100644
--- a/packages/dsh-tauri-worktree/src/host/utils/worktree-facts.ts
+++ b/packages/dsh-tauri-worktree/src/host/utils/worktree-facts.ts
@@ -17,7 +17,7 @@ export function worktreeSectionText(binding: Binding): string {
`${worktreeFactsText(binding)}\n\n`
+ `Resolve every path inside the worktree path. The project path is a different checkout: `
+ `do not read from or modify it in this session, even when the inherited history refers to it. `
- + `Dependency directories (e.g. node_modules) are linked from the source repository so the worktree works out of the box. `
+ + `Dependency directories (e.g. node_modules) are linked from the source repository, and the agent skills directory (.agents) is copied into the worktree, so it works out of the box. `
+ `Running a package manager install (e.g. \`pnpm install\`) inside the worktree first detaches that link and materializes an independent copy, `
+ `leaving the source repository untouched. `
+ `checkout_worktree is user-authorized only: call it only after a direct human user explicitly requests or approves checkout. `