fix(subagent): relocate a child only into a registered parent anchor

A parent without a recorded project reports the deployment fallback as its
root; the child relocated into it and registered a directory that is no
checkout. Require the parent's root to be one of its anchors. The captured
child directory now carries the canonical spelling the directory service
records, so a symlinked explicit cwd inside the parent's checkout still
relocates. The subagent README and the workspace-root note state the
longest-anchor rule and that a child registers only the parent's current root.

Review: https://github.com/deepseek-harness/deepseek-harness/pull/7817#discussion_r4454836468
Review: https://github.com/deepseek-harness/deepseek-harness/pull/7817#discussion_r4454836458
Review: https://github.com/deepseek-harness/deepseek-harness/pull/7817#discussion_r4454836518
This commit is contained in:
LegGasai committed 2026-11-04 11:02:46 +08:00
1 parent 2c25ae6b23
commit 0289252c8a
11 files changed
+81 -24

No files matched your search

@@ -9,8 +9,8 @@
en: cb5864da99a92145
zh: 210f8b4282f99c93
/agent-note-session-workspace-root/decision:
en: 2aa85360b92e618d
zh: 079b815ab1e6ece4
en: ed140682dce26955
zh: 65d73b329a43ab04
/agent-note-session-workspace-root/deferred:
en: d8167590081dfa4e
zh: 208630aac853773b
@@ -14,7 +14,7 @@ Each Session has three directories with one owner each. `header.cwd` remains the
`relocate` validates the root and the directory inside it, then commits one `working-directory/change` whose optional `anchor` names the root. A `set` inside the root moves only the cursor; a `set` into another anchor selects that anchor as the root; a `set` outside every anchor moves the cursor and leaves the root. Recovery to the original project selects it again. The event's optional property is a same-version persistence change recorded in the [acknowledgement](../../../../docs/persistence-changes/2026-11-03-working-directory-anchor.md); a log without `anchor` folds to the original project as its only anchor.
Worktree creation enters the checkout through `relocate`; leaving it is the existing `set` back into the original project, which the anchor rule turns into a root change. A child subagent keeps the parent's original project in its header; when the parent's root is another checkout and the child starts inside it, the child relocates into that root, so its root matches the parent's. The user-context snapshot adds a `Workspace root` line while the cursor differs from the root, and a change notice ends with the new root when the change selects another root. The SDK working-directory methods return `{ cwd, root }`, and `getWorkspaceRoot` (`get_workspace_root` in Python) reads the root.
Worktree creation enters the checkout through `relocate`; leaving it is the existing `set` back into the original project, which the anchor rule turns into a root change. A child subagent keeps the parent's original project in its header; when the parent's root is another registered checkout and the child starts inside it, the child relocates into that root. The child's root is then that checkout unless the original project its header registers already contains the child's directory, because the root is always the longest containing anchor; the child registers no other parent anchor, so a later `cd` into another checkout the parent entered leaves the child's root unchanged. The user-context snapshot adds a `Workspace root` line while the cursor differs from the root, and a change notice ends with the new root when the change selects another root. The SDK working-directory methods return `{ cwd, root }`, and `getWorkspaceRoot` (`get_workspace_root` in Python) reads the root.
## Deferred
@@ -14,7 +14,7 @@ Status: implemented
`relocate` 验证根及其内部的目录,然后提交一条可选 `anchor` 指向该根的 `working-directory/change`。根内部的 `set` 只移动光标;进入另一个锚的 `set` 把该锚选为根;所有锚之外的 `set` 移动光标并保留根。恢复到原始项目时再次把它选为根。事件的可选属性是一次同版本持久化变更,记录在[确认记录](../../../../docs/persistence-changes/2026-11-03-working-directory-anchor.zh.md)中;没有 `anchor` 的日志折叠后以原始项目为唯一的锚。
工作树创建通过 `relocate` 进入检出目录;离开它就是既有的 `set` 回到原始项目,锚规则将其变为根的变化。子代理在 header 中保留父级的原始项目;当父级的根是另一个检出目录且子级在其中启动时,子级迁入该根,因此子级的根与父级一致。光标与根不同时,用户上下文快照增加一行 `Workspace root`,选中另一个根的变更在通知末尾给出新根。SDK 的工作目录方法返回 `{ cwd, root }`,`getWorkspaceRoot`(Python 中为 `get_workspace_root`)读取根。
工作树创建通过 `relocate` 进入检出目录;离开它就是既有的 `set` 回到原始项目,锚规则将其变为根的变化。子代理在 header 中保留父级的原始项目;当父级的根是另一个已登记的检出目录且子级在其中启动时,子级迁入该根。子级的根随即是该检出目录,除非其 header 登记的原始项目本身已包含子级目录,因为根始终是包含目录的最长锚;子级不登记父级的其他锚,因此之后 `cd` 进父级进入过的另一个检出目录时子级的根不变。光标与根不同时,用户上下文快照增加一行 `Workspace root`,选中另一个根的变更在通知末尾给出新根。SDK 的工作目录方法返回 `{ cwd, root }`,`getWorkspaceRoot`(Python 中为 `get_workspace_root`)读取根。
## Deferred
+2 -2
View File
@@ -15,8 +15,8 @@
en: d152484eb41ac6b4
zh: 09388d293f9be9cb
/deepseek-ai-dsh-subagent/use-this-package:
en: c5438ea39eeea4e7
zh: 44512a484f795f10
en: f792ac6ae4212936
zh: 244f69217d96b6a6
/deepseek-ai-dsh-subagent/use-this-package/enabling-delegation:
en: 025605eddae02478
zh: 5659cba4340735d8
+1 -1
View File
@@ -25,7 +25,7 @@ Use `dsh-subagent` to delegate work through named providers and collect managed
<a id="use-this-package"></a>
## Use this package
Each start accepts an optional `cwd`; relative paths resolve against the parent's current working directory, and omission captures that current directory. The child keeps its parent's origin metadata and permission roots. When the parent works in a checkout other than its original project, a child that starts inside that checkout inherits it as its workspace root. A child-owned directory value follows any inherited history, survives continuation and restart, and is independent of later parent changes.
Each start accepts an optional `cwd`; relative paths resolve against the parent's current working directory, and omission captures that current directory. The child keeps its parent's origin metadata and permission roots. When the parent works in a registered checkout other than its original project, a child that starts inside that checkout registers it as a workspace anchor; the child's root is the longest of its anchors containing its directory, which is that checkout unless the original project recorded in its header already contains the directory. The child registers only the parent's current root, not the parent's other anchors, so a later `cd` into another checkout the parent entered leaves the child's root unchanged. A child-owned directory value follows any inherited history, survives continuation and restart, and is independent of later parent changes.
This package is the contract every delegation setup shares. You enable it by mounting the service together with one or more provider backends and the model-facing delegation tool; from then on, an agent can delegate work and the service routes each request to the named provider.
+1 -1
View File
@@ -25,7 +25,7 @@ kind: "package-reference"
<a id="use-this-package"></a>
## 使用本包
每次启动可传入可选的 `cwd`;相对路径以父级当前工作目录解析,省略时捕获该当前目录。子级保留父级的起始目录元数据与权限根目录。父级在原始项目之外的检出目录中工作时,在该检出目录内启动的子级继承它作为工作区根。子级自己的目录值位于继承历史之后,在继续与重启后保留,并独立于父级之后的目录变化。
每次启动可传入可选的 `cwd`;相对路径以父级当前工作目录解析,省略时捕获该当前目录。子级保留父级的起始目录元数据与权限根目录。父级在原始项目之外的已登记检出目录中工作时,在该检出目录内启动的子级把它登记为工作区锚;子级的根是其锚中包含其目录的最长者,即该检出目录,除非 header 记录的原始项目本身已包含该目录。子级只登记父级当前的根,不登记父级的其他锚,因此之后 `cd` 进父级进入过的另一个检出目录时子级的根不变。子级自己的目录值位于继承历史之后,在继续与重启后保留,并独立于父级之后的目录变化。
本包是每个委派组合都共享的约定。你通过把服务与一个或多个提供方后端以及面向模型的委派工具一起挂载来启用它;此后 agent 即可委派工作,服务会把每个请求路由到具名提供方。
+11 -10
View File
@@ -102,32 +102,33 @@
},
"devDependencies": {
"@deepseek-ai/cordis": "workspace:~",
"@deepseek-ai/cordis-plugin-include": "workspace:~",
"@deepseek-ai/cordis-plugin-loader": "workspace:~",
"@deepseek-ai/dsh-agent": "workspace:*",
"@deepseek-ai/dsh-agent-loop": "workspace:*",
"@deepseek-ai/dsh-agent-loop-testkit": "workspace:*",
"@deepseek-ai/dsh-agent-preset-registry": "workspace:*",
"@deepseek-ai/dsh-attachment": "workspace:*",
"@deepseek-ai/dsh-fs": "workspace:*",
"@deepseek-ai/dsh-fs-sandbox": "workspace:*",
"@deepseek-ai/dsh-llm": "workspace:*",
"@deepseek-ai/dsh-permission-presets": "workspace:*",
"@deepseek-ai/dsh-sandbox": "workspace:*",
"@deepseek-ai/dsh-sandbox-policy": "workspace:*",
"@deepseek-ai/dsh-scope": "workspace:*",
"@deepseek-ai/dsh-session": "workspace:*",
"@deepseek-ai/dsh-session-persistence": "workspace:*",
"@deepseek-ai/dsh-session-persistence-jsonl": "workspace:*",
"@deepseek-ai/dsh-session-projection": "workspace:*",
"@deepseek-ai/dsh-session-query": "workspace:*",
"@deepseek-ai/dsh-system-prompt": "workspace:*",
"@deepseek-ai/dsh-tool-fs": "workspace:*",
"@deepseek-ai/dsh-tool-testkit": "workspace:*",
"@deepseek-ai/dsh-tools": "workspace:*",
"@deepseek-ai/dsh-typert-protocol": "workspace:*",
"@deepseek-ai/dsh-user-approval": "workspace:*",
"@deepseek-ai/dsh-util-time": "workspace:*",
"@deepseek-ai/dsh-permission-presets": "workspace:*",
"@deepseek-ai/dsh-workspace": "workspace:*",
"@deepseek-ai/cordis-plugin-include": "workspace:~",
"@deepseek-ai/cordis-plugin-loader": "workspace:~",
"@deepseek-ai/dsh-agent-loop": "workspace:*",
"@deepseek-ai/dsh-agent-loop-testkit": "workspace:*",
"@deepseek-ai/dsh-fs-sandbox": "workspace:*",
"@deepseek-ai/dsh-tool-fs": "workspace:*",
"@deepseek-ai/dsh-session-persistence-jsonl": "workspace:*",
"@deepseek-ai/dsh-working-directory": "workspace:*"
"@deepseek-ai/dsh-working-directory": "workspace:*",
"@deepseek-ai/dsh-workspace": "workspace:*"
}
}
+13 -4
View File
@@ -8,6 +8,7 @@
import { randomUUID } from 'node:crypto'
import { isAbsolute, resolve } from 'node:path'
import type {} from '@deepseek-ai/dsh-fs'
import { within } from '@deepseek-ai/dsh-working-directory'
import type { Context } from '@deepseek-ai/cordis'
import type { Agent, AgentOptions, CreateAgentOptions } from '@deepseek-ai/dsh-agent'
@@ -435,11 +436,17 @@ export class SubagentManager {
})
}
/** Capture the explicit or inherited directory while the parent still owns startup. */
/**
* Capture the explicit or inherited directory while the parent still owns startup. The result carries
* the canonical spelling the directory service records, so containment against the parent's root sees
* the same path a symlinked request denotes; an orchestration fixture without a filesystem keeps it lexical.
*/
private async resolveDirectory(request: Pick<SubagentStartRequest, 'parent' | 'cwd'>, signal: AbortSignal): Promise<string> {
const cwd = request.cwd !== undefined && isAbsolute(request.cwd)
const lexical = request.cwd !== undefined && isAbsolute(request.cwd)
? request.cwd
: resolve(await this.ctx.workingDirectory.ensure(request.parent, signal), request.cwd ?? '.')
const fs = this.ctx.get('fs')
const cwd = fs === undefined ? lexical : fs.processPath(await fs.resolve(lexical, { signal }))
signal.throwIfAborted()
return cwd
}
@@ -748,9 +755,11 @@ export class SubagentManager {
const workingDirectory = childCtx.get('workingDirectory')
if (workingDirectory === undefined) throw new Error('local subagents require the working-directory service')
// The child header records the parent's original project; a parent working in another
// checkout passes that checkout on as the child's root when the child starts inside it.
// registered checkout passes that checkout on as the child's root when the child starts inside
// it. A parent without a recorded project reports the deployment fallback, which is no anchor.
const parentRoot = workingDirectory.rootOf(create.parent.session)
if (parentRoot !== child.session.header.cwd && within(parentRoot, create.cwd)) {
if (workingDirectory.anchorsOf(create.parent.session).includes(parentRoot)
&& parentRoot !== child.session.header.cwd && within(parentRoot, create.cwd)) {
await workingDirectory.relocate(child, parentRoot, { cwd: create.cwd, signal: inputs.signal })
} else {
await workingDirectory.set(child, create.cwd, inputs.signal)
@@ -1,6 +1,6 @@
import { startTestActivation } from './local-activation.ts'
import { mountWorkingDirectoryFixture } from './working-directory-fixture.ts'
import { mkdtemp, mkdir, realpath, rm } from 'node:fs/promises'
import { mkdtemp, mkdir, realpath, rm, symlink } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { Context } from '@deepseek-ai/cordis'
@@ -43,7 +43,7 @@ async function fixture() {
meta: { cwd: origin },
agentOptions: { provider: 'mock', model: 'mock' },
})
return { ctx, parent, origin, first, second, adapter }
return { ctx, root, parent, origin, first, second, adapter }
}
describe('subagent working directories', () => {
@@ -83,6 +83,47 @@ describe('subagent working directories', () => {
await run.dispose()
})
it('relocates a child whose explicit directory is a symlinked spelling inside the parent checkout', async () => {
const { ctx, root, parent, origin, second } = await fixture()
const inside = join(second, 'inside')
await mkdir(inside)
const link = join(root, 'link')
await symlink(second, link)
await ctx.workingDirectory.relocate(parent, second)
const run = await startTestActivation(ctx, 'spawn', {
parent, cwd: join(link, 'inside'), prompt: [{ type: 'text', text: 'child' }], signal: new AbortController().signal,
})
await run.result
const child = run.localAgent
expect(ctx.workingDirectory.get(child.session)).toBe(await realpath(inside))
expect(ctx.workingDirectory.rootOf(child.session)).toBe(await realpath(second))
expect(ctx.workingDirectory.anchorsOf(child.session)).toEqual([origin, await realpath(second)])
await run.dispose()
})
it('leaves a child of a parent without a recorded project on the deployment fallback without registering it', async () => {
const { ctx } = await fixture()
const fallback = ctx.workingDirectory.defaultDirectory
const { agent: parent } = await ctx.agents.create({
sessionId: SessionId('fallback-parent'),
meta: {},
agentOptions: { provider: 'mock', model: 'mock' },
})
expect(ctx.workingDirectory.rootOf(parent.session)).toBe(fallback)
const run = await startTestActivation(ctx, 'spawn', {
parent, cwd: fallback, prompt: [{ type: 'text', text: 'child' }], signal: new AbortController().signal,
})
await run.result
const child = run.localAgent
expect(child.session.header.cwd).toBeUndefined()
expect(ctx.workingDirectory.get(child.session)).toBe(await realpath(fallback))
expect(ctx.workingDirectory.rootOf(child.session)).toBe(fallback)
expect(ctx.workingDirectory.anchorsOf(child.session)).toEqual([])
expect(child.session.snapshotEvents().filter(event => event.type === 'working-directory/change'))
.toEqual([expect.objectContaining({ data: { cwd: await realpath(fallback) } })])
await run.dispose()
})
it.each(['relative', 'absolute'])('retains an explicit %s directory when a continuable child cold-resumes', async (form) => {
const { ctx, parent, origin, first, second } = await fixture()
const selected = await realpath(second)
+3
View File
@@ -71,6 +71,9 @@
{
"path": "../../util/chunked-list"
},
{
"path": "../../fs/fs"
},
{
"path": "../../session/working-directory"
}
+3
View File
@@ -16221,6 +16221,9 @@ importers:
'@deepseek-ai/dsh-attachment':
specifier: workspace:*
version: link:../../attachment/attachment
'@deepseek-ai/dsh-fs':
specifier: workspace:*
version: link:../../fs/fs
'@deepseek-ai/dsh-fs-sandbox':
specifier: workspace:*
version: link:../../fs/fs-sandbox