From ad70a73280765f2c94d73d6900d82a03b8e3effa Mon Sep 17 00:00:00 2001 From: LegGasai Date: Mon, 16 Nov 2026 23:34:54 +0800 Subject: [PATCH] feat(working-directory): ignore stored working-directory/change events Earlier releases recorded both a model's `cd` and an entered worktree as `working-directory/change`, and a log cannot tell them apart. The fold kept such a directory when it lay inside the Session's original project. That let a stored `cd` into a subdirectory narrow the workspace once consumers follow the directory, and dropped a worktree outside the directory a Session was opened in. Readers now ignore the event: a stored Session resumes in its original project, or in the directory of its last `workspace-root/change`, and returns to a worktree through `enter_worktree`. The fold state is the directory alone (state version 3), and the forge worktree fold reads only `workspace-root/change` (state version 7). Tests and Web fixtures that simulated a Host move with the old event record `workspace-root/change`. --- ...-09-13-session-working-directory.i18n.yaml | 4 +- .../2026-09-13-session-working-directory.md | 4 +- ...2026-09-13-session-working-directory.zh.md | 4 +- ...026-11-03-session-workspace-root.i18n.yaml | 12 ++-- .../2026-11-03-session-workspace-root.md | 12 ++-- .../2026-11-03-session-workspace-root.zh.md | 12 ++-- apps/web/tests/markdown-file-links.e2e.ts | 2 +- apps/web/tests/produced-file-mentions.e2e.ts | 4 +- docs/config-catalog.md | 4 +- docs/config-catalog.zh.md | 4 +- docs/persistence-catalog.i18n.yaml | 12 ++-- docs/persistence-catalog.md | 13 +++-- docs/persistence-catalog.zh.md | 13 +++-- ...2026-11-13-workspace-root-change.i18n.yaml | 8 +-- .../2026-11-13-workspace-root-change.md | 4 +- .../2026-11-13-workspace-root-change.zh.md | 4 +- docs/persistence-schema.json | 2 +- docs/subsystems/working-directory.i18n.yaml | 4 +- docs/subsystems/working-directory.md | 4 +- docs/subsystems/working-directory.zh.md | 4 +- .../guide.i18n.yaml | 4 +- .../working-directory-tool-removed/guide.md | 4 +- .../guide.zh.md | 4 +- .../client-ui-forge/README.i18n.yaml | 4 +- .../experimental/client-ui-forge/README.md | 2 +- .../experimental/client-ui-forge/README.zh.md | 2 +- .../experimental/client-ui-forge/src/types.ts | 4 +- .../client-ui-forge/src/worktree.ts | 23 +++----- .../tests/controller.host.spec.ts | 2 +- .../tests/states-remote.host.spec.ts | 2 +- .../tests/worktree.host.spec.ts | 30 ++++------ .../worktree/tests/worktree.spec.ts | 4 +- .../extensions/tool-cordis/src/api-catalog.ts | 2 +- .../working-directory/README.i18n.yaml | 8 +-- packages/session/working-directory/README.md | 4 +- .../session/working-directory/README.zh.md | 4 +- .../session/working-directory/src/index.ts | 57 ++++++------------- .../tests/stored-changes.spec.ts | 56 ++++++------------ 38 files changed, 144 insertions(+), 202 deletions(-) diff --git a/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.i18n.yaml b/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.i18n.yaml index df17da4e1b..958a8e1545 100644 --- a/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.i18n.yaml +++ b/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.i18n.yaml @@ -9,8 +9,8 @@ en: 31fba384d90ce7d2 zh: f8b814868f3280c3 /agent-note-session-working-directory/decision: - en: f30ed945f7dfd117 - zh: 754a045cec0b6ccd + en: 5de5171ee52a62b4 + zh: 7f78197ea38af658 /agent-note-session-working-directory/alternatives-considered: en: f0995feb9b6bcce8 zh: 97417085fd17f70f diff --git a/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.md b/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.md index 4bcb648fd4..779dd302d1 100644 --- a/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.md +++ b/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.md @@ -10,9 +10,9 @@ A fixed Session creation directory cannot represent a task that changes checkout ## Decision -`dsh-working-directory` owns one logged effective directory per Session. Its projection initializes from the immutable original project and folds `workspace-root/change` and stored `working-directory/change` events; consumers read that owner instead of deriving execution paths from the header or sandbox root. Directory changes validate through the filesystem provider, serialize within the Session, and publish only after the new directory is available. +`dsh-working-directory` owns one logged effective directory per Session. Its projection initializes from the immutable original project and folds `workspace-root/change` events; consumers read that owner instead of deriving execution paths from the header or sandbox root. Directory changes validate through the filesystem provider, serialize within the Session, and publish only after the new directory is available. -Only Host operations change the directory: worktree creation and exit, the forge panel, SDK requests, and child startup call the service. The [workspace-root note](2026-11-03-session-workspace-root.md) removed the model-facing `working_directory` tool, records each change as `workspace-root/change`, and reads a stored `working-directory/change` only inside the original project. +Only Host operations change the directory: worktree creation and exit, the forge panel, SDK requests, and child startup call the service. The [workspace-root note](2026-11-03-session-workspace-root.md) removed the model-facing `working_directory` tool, records each change as `workspace-root/change`, and ignores stored `working-directory/change` events. Required user-context snapshots carry the current value; each committed change attempts to queue a notice through the ordinary Agent inbox. Cancellation or disposal may discard unadmitted notices while the directory event survives for subsequent requests. The original project is the recovery destination when the selected directory disappears. Missing recovery destinations produce an error. Write permissions remain independent. diff --git a/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.zh.md b/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.zh.md index eda1907ad3..18c7f9afea 100644 --- a/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.zh.md +++ b/.agents/notes/implemented/architecture/2026-09-13-session-working-directory.zh.md @@ -10,9 +10,9 @@ Status: implemented ## Decision -`dsh-working-directory` 拥有每个 Session 的一个日志化有效目录。投影从不可变的原始项目初始化,并折叠 `workspace-root/change` 与已存储的 `working-directory/change` 事件;消费者读取该服务,而不是从 header 或沙箱根目录派生执行路径。目录变更通过文件系统 provider 验证,在 Session 内串行执行,并只在新目录可用后发布。 +`dsh-working-directory` 拥有每个 Session 的一个日志化有效目录。投影从不可变的原始项目初始化,并折叠 `workspace-root/change` 事件;消费者读取该服务,而不是从 header 或沙箱根目录派生执行路径。目录变更通过文件系统 provider 验证,在 Session 内串行执行,并只在新目录可用后发布。 -只有 Host 操作改变目录:工作树的创建与退出、forge 面板、SDK 请求和子代理启动调用该服务。[工作区根记录](2026-11-03-session-workspace-root.zh.md)移除了面向模型的 `working_directory` 工具,把每次变更记录为 `workspace-root/change`,并且只在原始项目之内读取已存储的 `working-directory/change`。 +只有 Host 操作改变目录:工作树的创建与退出、forge 面板、SDK 请求和子代理启动调用该服务。[工作区根记录](2026-11-03-session-workspace-root.zh.md)移除了面向模型的 `working_directory` 工具,把每次变更记录为 `workspace-root/change`,并忽略已存储的 `working-directory/change` 事件。 必需的用户上下文快照携带当前值;每次已提交变更都会尝试通过普通 Agent 收件箱排入通知。取消或销毁可能丢弃尚未进入请求的通知,但目录事件仍然保留,供后续请求使用。所选目录消失时恢复到原始项目。恢复目录也消失时返回错误。写权限保持独立。 diff --git a/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.i18n.yaml b/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.i18n.yaml index eb3405aa68..863d2f26eb 100644 --- a/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.i18n.yaml +++ b/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.i18n.yaml @@ -9,14 +9,14 @@ en: 6938aa715106bb99 zh: ea1497b337712f33 /agent-note-session-workspace-root/decision: - en: 1b87e20c69803861 - zh: 243144cd3a18374a + en: b42597fdf9838587 + zh: cb36853abde00754 /agent-note-session-workspace-root/deferred: en: 8807b3829badfdcd zh: 2ec10ce7f0dda3f4 /agent-note-session-workspace-root/alternatives-considered: - en: 1e587eca01523f32 - zh: 76d0a8110378092a + en: 35ccc31b8b40d014 + zh: 7bf3c9f45afae8ab /agent-note-session-workspace-root/consequences: - en: 993644fdc9e6101b - zh: 9e22417cd38ad7c6 + en: 7cef9fcd81f95833 + zh: 4211d4ae53dd4974 diff --git a/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.md b/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.md index 0e341b46bd..e7e6eac1a3 100644 --- a/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.md +++ b/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.md @@ -12,7 +12,7 @@ A Session that enters a Git worktree records only a changed current directory. T A Session works in one directory, and only Host operations move it. The model-facing `working_directory` tool and the `cwd` parameter of the delegation tools are removed; the model reaches a subdirectory through paths and the shell's per-call `workdir`. `header.cwd` remains the immutable original project: Session identity, workspace grouping, and the recovery destination. `dsh-working-directory` owns the directory and has one read, `get`: relative paths resolve from the directory, and it is the workspace root that consumers bound writes, file views, and discovery by. Every change the service records, through `set` or the recovery of `ensure`, is a `workspace-root/change`. -`workspace-root/change` is a new ordinary event type, recorded as a same-version persistence change in the [acknowledgement](../../../../docs/persistence-changes/2026-11-13-workspace-root-change.md). `working-directory/change` stays in the event vocabulary, deprecated and without a writer. The model could choose the directory of a stored one, so the fold keeps it only when it lies inside the Session's original project: a Session that worked in a worktree or a subdirectory of its project resumes there, with the relative paths its history used. A stored directory outside the project returns the Session to the project, and the next request's context reports the directory. An older build refuses a log that holds `workspace-root/change`; a Session that never moves its workspace holds none. +`workspace-root/change` is a new ordinary event type, recorded as a same-version persistence change in the [acknowledgement](../../../../docs/persistence-changes/2026-11-13-workspace-root-change.md). `working-directory/change` stays in the event vocabulary, deprecated and without a writer. Readers ignore a stored one: the same event recorded the model's `cd` and an entered worktree, and most stored ones are a `cd`, since few stored Sessions ran the worktree plugin. A Session that holds them resumes in its original project, or in the directory of its last `workspace-root/change`, and the next request's context reports the directory. An older build refuses a log that holds `workspace-root/change`; a Session that never moves its workspace holds none. Worktree creation enters the checkout through `set`. The model has one tool for entering a checkout, `enter_worktree`: a name selects a worktree inside the pool of the checkout the Session works in. An existing one is entered through `ctx.worktrees.enter`, and a new one is created through `ctx.worktrees.create` when none has that name. The name cannot leave that pool, so the model still cannot choose an arbitrary directory. `ctx.worktrees.exit`, exposed as `exit_worktree`, returns the Session from the recorded worktree that contains its directory to the checkout it created or entered that worktree from. A child subagent keeps the parent's original project in its header and starts, through `set`, in the parent's directory or a Host-chosen directory. @@ -30,7 +30,7 @@ The sandbox write boundary, the workspace file service, desktop file drag, the c **An optional `workspace` marker on `working-directory/change`.** Older readers ignore the property and keep reading every log. Every change the service records would then carry a constant, and a missing property would be the only sign of a stored change; a separate event names the workspace move and leaves the stored event unchanged. -**Retire `working-directory/change` through a format migration.** The stored event carries no origin, so a migration would classify each one by its neighbouring events. The containment rule reads stored events as they are. The [compatibility rules](../../../../docs/persistence-changes/README.md#compatibility-rules) reserve a version bump for changes that an existing discriminator cannot make safe. +**Retire `working-directory/change` through a format migration.** The stored event carries no origin, so a migration would classify each one by its neighbouring events. Ignoring stored events needs no rewrite of the log. The [compatibility rules](../../../../docs/persistence-changes/README.md#compatibility-rules) reserve a version bump for changes that an existing discriminator cannot make safe. **Enter a worktree by path.** A model-supplied path would move the write boundary to any registered worktree, which removing the directory tool ruled out unless the user approves each move. A `tools/pre-execute` listener can ask for that approval through the approval service; this decision leaves the path form, and with it entry of a sibling from inside a worktree, to a later change. A name inside the pool keeps the boundary inside the checkout. @@ -38,10 +38,12 @@ The sandbox write boundary, the workspace file service, desktop file drag, the c **Persist the shell's `cd` between calls.** That is an implicit cursor. In that developer's Sessions, 68% of `cd` prefixes named a directory outside the Session directory and 30% the Session directory itself, so a persisted directory confined to the workspace would apply to about 2%. -**Two public reads.** `get` for relative paths beside `rootOf` for the workspace root kept every stored directory, and made each consumer choose between two values that are equal in every Session started from this version on. One read with the difference inside the fold removes that choice. +**Two public reads.** `get` for relative paths beside `rootOf` for the workspace root kept every stored directory, and made each consumer choose between two values that are equal in every Session started from this version on. One read removes that choice. -**Keep every stored directory.** Reading every stored `working-directory/change` as the directory needs no rule, but a stored model `cd` to a directory outside the project, such as a home directory, would become the write boundary once consumers follow the directory. +**Keep every stored directory.** Reading every stored `working-directory/change` as the directory would let a stored model `cd`, including one to a home directory, become the write boundary once consumers follow the directory. + +**Keep stored directories inside the original project.** A Session that entered a worktree would resume there, but the rule cannot tell a worktree from a `cd`: a stored `cd` into a subdirectory would narrow the write boundary and the file views, and a worktree outside the directory a Session was opened in would be dropped. ## Consequences -Consumers read one directory through `get`. A stored Session that worked inside its project resumes where it was and follows later Host moves like any other. A stored Session whose log left its project resumes in the project; that includes one opened in a subdirectory of a repository that entered a worktree at the repository root, which `enter_worktree` or another Host move returns to the worktree. A later model-facing cursor needs its own event, because stored `working-directory/change` events are read as the Session's directory. The model enters a checkout only through `enter_worktree`: a new one, or a registered one in the pool. A checkout elsewhere needs a Host operation. The ACP, SDK, and Web producers record the original project by its canonical spelling, so a Host move back to it records no change. The worktree snapshot fixture covers entering, leaving, and losing a worktree. Moving the consumers listed under Deferred is a separate change. +Consumers read one directory through `get`. A stored Session resumes in its original project whatever directory its `working-directory/change` events recorded, and follows Host moves from there. One that worked in a worktree returns to it through `enter_worktree`; until then its relative paths resolve in the original project. The model enters a checkout only through `enter_worktree`: a new one, or a registered one in the pool. A checkout elsewhere needs a Host operation. The ACP, SDK, and Web producers record the original project by its canonical spelling, so a Host move back to it records no change. The worktree snapshot fixture covers entering, leaving, and losing a worktree. Moving the consumers listed under Deferred is a separate change. diff --git a/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.zh.md b/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.zh.md index 22775da9bb..96791eb6a6 100644 --- a/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.zh.md +++ b/.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.zh.md @@ -12,7 +12,7 @@ Status: implemented 一个 Session 在一个目录中工作,只有 Host 操作移动它。面向模型的 `working_directory` 工具与委派工具的 `cwd` 参数被移除;模型通过路径和 shell 的单次调用参数 `workdir` 到达子目录。`header.cwd` 仍是不可变的原始项目:Session 身份、工作区归属和恢复目的地。`dsh-working-directory` 拥有该目录,只有一个读取接口 `get`:相对路径从该目录解析,消费方也以它作为工作区根来限定写入、文件视图和发现。服务通过 `set` 或 `ensure` 的恢复记录的每次变更都是 `workspace-root/change`。 -`workspace-root/change` 是新增的普通事件类型,作为同版本持久化变更记录在[确认记录](../../../../docs/persistence-changes/2026-11-13-workspace-root-change.zh.md)中。`working-directory/change` 保留在事件词汇表中,已标为 deprecated,没有写入方。已存储的这类事件的目录可能由模型选择,因此折叠只在它位于 Session 的原始项目之内时保留它:曾在工作树或项目子目录中工作的 Session 会在原处恢复,历史中使用的相对路径照常解析。位于项目之外的已存储目录让 Session 回到项目,下一次请求的上下文会报告该目录。旧版本拒绝含有 `workspace-root/change` 的日志;从未移动工作区的 Session 不含该事件。 +`workspace-root/change` 是新增的普通事件类型,作为同版本持久化变更记录在[确认记录](../../../../docs/persistence-changes/2026-11-13-workspace-root-change.zh.md)中。`working-directory/change` 保留在事件词汇表中,已标为 deprecated,没有写入方。读者忽略已存储的这类事件:同一种事件既记录模型的 `cd`,也记录进入工作树,而已存储的 Session 很少启用工作树插件,其中大多数是 `cd`。含有这类事件的 Session 在原始项目中恢复,或在最近一条 `workspace-root/change` 的目录中恢复,下一次请求的上下文会报告该目录。旧版本拒绝含有 `workspace-root/change` 的日志;从未移动工作区的 Session 不含该事件。 工作树创建通过 `set` 进入检出目录。模型只有一个进入检出的工具 `enter_worktree`:名称在 Session 所在检出的池中选中一个工作树。已有的工作树通过 `ctx.worktrees.enter` 进入,没有同名工作树时通过 `ctx.worktrees.create` 新建。名称无法离开该池,因此模型仍然不能选择任意目录。`ctx.worktrees.exit` 以 `exit_worktree` 的形式提供,它找到包含 Session 目录的已记录工作树,把 Session 送回创建或进入该工作树时所在的检出。子代理的 header 保留父级的原始项目,并通过 `set` 从父级的目录或 Host 选定的目录启动。 @@ -30,7 +30,7 @@ Status: implemented **在 `working-directory/change` 上增加可选的 `workspace` 标记。** 旧读者忽略该属性,仍能读取所有日志。但服务记录的每次变更都要带一个恒定值,缺少属性成为识别已存储变更的唯一依据;单独的事件为工作区移动命名,并让已存储的事件保持不变。 -**通过格式迁移退役 `working-directory/change`。** 已存储的事件不带来源,迁移只能依据相邻事件逐条分类。包含规则按原样读取已存储的事件。[兼容性规则](../../../../docs/persistence-changes/README.zh.md#compatibility-rules)只在现有判别符无法保证安全时才要求升版本。 +**通过格式迁移退役 `working-directory/change`。** 已存储的事件不带来源,迁移只能依据相邻事件逐条分类。忽略已存储的事件不需要改写日志。[兼容性规则](../../../../docs/persistence-changes/README.zh.md#compatibility-rules)只在现有判别符无法保证安全时才要求升版本。 **按路径进入工作树。** 模型提供的路径会把写边界移到任意已登记的工作树,除非用户逐次批准,否则这正是移除目录工具所排除的。`tools/pre-execute` 监听器可以通过审批服务请求这一批准;本决定把路径形式,以及随之而来的“从工作树内进入同级工作树”,留给之后的变更。池内的名称把边界留在检出之内。 @@ -38,10 +38,12 @@ Status: implemented **在调用之间保留 shell 的 `cd`。** 这是一个隐式的光标。在该开发者的 Session 中,68% 的 `cd` 前缀指向 Session 目录之外,30% 指向 Session 目录本身,限制在工作区内的保留只对约 2% 生效。 -**两个公开读取接口。** 用 `get` 读相对路径的基准、用 `rootOf` 读工作区根,可以保留每个已存储的目录,但每个消费方都要在两个值之间做选择,而这两个值在从这个版本起启动的每个 Session 中都相等。只保留一个读取接口、把差异收进折叠,就不需要这种选择。 +**两个公开读取接口。** 用 `get` 读相对路径的基准、用 `rootOf` 读工作区根,可以保留每个已存储的目录,但每个消费方都要在两个值之间做选择,而这两个值在从这个版本起启动的每个 Session 中都相等。只保留一个读取接口,就不需要这种选择。 -**保留每个已存储的目录。** 把每条已存储的 `working-directory/change` 都读作目录不需要任何规则,但消费方跟随目录之后,已存储的模型 `cd` 所到的项目之外的目录(例如用户主目录)会成为写边界。 +**保留每个已存储的目录。** 把每条已存储的 `working-directory/change` 都读作目录,消费方跟随目录之后,已存储的模型 `cd`(包括进入用户主目录的)会成为写边界。 + +**保留原始项目之内的已存储目录。** 进入过工作树的 Session 会在工作树中恢复,但这条规则分不清工作树与 `cd`:已存储的进入子目录的 `cd` 会收窄写边界和文件视图,而位于 Session 打开目录之外的工作树会被丢弃。 ## Consequences -消费方通过 `get` 读取一个目录。曾在项目之内工作的已存储 Session 会在原处恢复,并像其他 Session 一样跟随之后的 Host 移动。日志曾离开项目的已存储 Session 会在项目中恢复;这包括在仓库子目录中打开、随后进入仓库根下工作树的 Session,`enter_worktree` 或其他 Host 移动可以让它回到工作树。以后若要增加面向模型的光标,需要它自己的事件,因为已存储的 `working-directory/change` 事件被读作 Session 的目录。模型只能通过 `enter_worktree` 进入检出:新建的检出,或池中已登记的检出。其他位置的检出需要 Host 操作。ACP、SDK 与 Web 的生产方以规范拼写记录原始项目,因此 Host 移回原始项目时不记录变更。工作树快照夹具覆盖进入、离开和失去工作树。迁移 Deferred 中列出的消费方是另一项变更。 +消费方通过 `get` 读取一个目录。无论 `working-directory/change` 事件记录了什么目录,已存储的 Session 都在原始项目中恢复,并从那里跟随 Host 移动。曾在工作树中工作的 Session 通过 `enter_worktree` 回到工作树;在此之前,它的相对路径在原始项目中解析。模型只能通过 `enter_worktree` 进入检出:新建的检出,或池中已登记的检出。其他位置的检出需要 Host 操作。ACP、SDK 与 Web 的生产方以规范拼写记录原始项目,因此 Host 移回原始项目时不记录变更。工作树快照夹具覆盖进入、离开和失去工作树。迁移 Deferred 中列出的消费方是另一项变更。 diff --git a/apps/web/tests/markdown-file-links.e2e.ts b/apps/web/tests/markdown-file-links.e2e.ts index 4354a018f6..b0b14a25ca 100644 --- a/apps/web/tests/markdown-file-links.e2e.ts +++ b/apps/web/tests/markdown-file-links.e2e.ts @@ -77,7 +77,7 @@ function changedDirectoryFixture(path: string): string { content: [{ type: 'text', text: 'Enter the worktree and link its notes.' }], source: { kind: 'user' }, }), { surfaceOp: 'append' }) session.append('session/title', { title: 'Changed directory links', messageSeqs: [user.seq], source: { kind: 'fallback' } }) - session.append('working-directory/change', { cwd: '{{cwd}}/.agents/worktrees/w' }) + session.append('workspace-root/change', { cwd: '{{cwd}}/.agents/worktrees/w' }) session.append('step/start', { turn: 1, step: 1 }) session.append('assistant/message', { turn: 1, step: 1, stream: [], diff --git a/apps/web/tests/produced-file-mentions.e2e.ts b/apps/web/tests/produced-file-mentions.e2e.ts index d22547e641..20d09034db 100644 --- a/apps/web/tests/produced-file-mentions.e2e.ts +++ b/apps/web/tests/produced-file-mentions.e2e.ts @@ -60,7 +60,7 @@ function mentionFixture(): string { }), }, { surfaceOp: 'append' }) for (const call of calls) { - if (call.path === 'report.txt') session.append('working-directory/change', { cwd: '{{cwd}}/child' }) + if (call.path === 'report.txt') session.append('workspace-root/change', { cwd: '{{cwd}}/child' }) const source = session.append('tool/call', { turn: 1, step: 1, @@ -79,7 +79,7 @@ function mentionFixture(): string { }), }, { surfaceOp: 'append', sourceEventSeqs: [source.seq] }) } - session.append('working-directory/change', { cwd: '{{cwd}}' }) + session.append('workspace-root/change', { cwd: '{{cwd}}' }) session.append('deliverables/presented', { turn: 1, callId: brandString('present-child'), files: [{ path: '{{cwd}}/child/report.txt' }] }) session.append('step/end', { turn: 1, step: 1 }) session.append('step/start', { turn: 1, step: 2 }) diff --git a/docs/config-catalog.md b/docs/config-catalog.md index 5ac9f9b499..4d7b57d2ff 100644 --- a/docs/config-catalog.md +++ b/docs/config-catalog.md @@ -1182,7 +1182,7 @@ export interface CotTranslationPreferences { ## `@deepseek-ai/dsh-experimental-client-ui-forge` - `inject`: `forge` · `forgeSession` · `sessionProjections` · `workingDirectory` · `fs` · `subprocess` · `sandbox` · `sandboxPolicy` · `agents` -- `source`: [`packages/experimental/client-ui-forge/src/types.ts:487`](../packages/experimental/client-ui-forge/src/types.ts) +- `source`: [`packages/experimental/client-ui-forge/src/types.ts:485`](../packages/experimental/client-ui-forge/src/types.ts) ```ts config-catalog /** Host settings of the forge panel Remote. */ @@ -5179,7 +5179,7 @@ export interface Config { ## `@deepseek-ai/dsh-working-directory` - `inject`: `fs` · `sessionProjections` · `systemPrompt` -- `source`: [`packages/session/working-directory/src/index.ts:74`](../packages/session/working-directory/src/index.ts) +- `source`: [`packages/session/working-directory/src/index.ts:75`](../packages/session/working-directory/src/index.ts) ```ts config-catalog /** Deployment default for Sessions without an original project directory. */ diff --git a/docs/config-catalog.zh.md b/docs/config-catalog.zh.md index 8f4bf4b496..70950d0976 100644 --- a/docs/config-catalog.zh.md +++ b/docs/config-catalog.zh.md @@ -1184,7 +1184,7 @@ export interface CotTranslationPreferences { ## `@deepseek-ai/dsh-experimental-client-ui-forge` - `inject`: `forge` · `forgeSession` · `sessionProjections` · `workingDirectory` · `fs` · `subprocess` · `sandbox` · `sandboxPolicy` · `agents` -- `source`: [`packages/experimental/client-ui-forge/src/types.ts:487`](../packages/experimental/client-ui-forge/src/types.ts) +- `source`: [`packages/experimental/client-ui-forge/src/types.ts:485`](../packages/experimental/client-ui-forge/src/types.ts) ```ts config-catalog /** Host settings of the forge panel Remote. */ @@ -5181,7 +5181,7 @@ export interface Config { ## `@deepseek-ai/dsh-working-directory` - `inject`: `fs` · `sessionProjections` · `systemPrompt` -- `source`: [`packages/session/working-directory/src/index.ts:74`](../packages/session/working-directory/src/index.ts) +- `source`: [`packages/session/working-directory/src/index.ts:75`](../packages/session/working-directory/src/index.ts) ```ts config-catalog /** Deployment default for Sessions without an original project directory. */ diff --git a/docs/persistence-catalog.i18n.yaml b/docs/persistence-catalog.i18n.yaml index 394f523f89..c37e3abbdc 100644 --- a/docs/persistence-catalog.i18n.yaml +++ b/docs/persistence-catalog.i18n.yaml @@ -300,8 +300,8 @@ en: e8dd0cebbb930eb7 zh: e8dd0cebbb930eb7 /session-persistence-event-catalog/events/working-directory/working-directory-change-log-only: - en: a7826f149a09b9b6 - zh: 16b54a5b469e7436 + en: 361780c2afc0b56f + zh: 53c14164db8772bb /session-persistence-event-catalog/events/workspace: en: 63ee1a85e4b59ae3 zh: 63ee1a85e4b59ae3 @@ -312,8 +312,8 @@ en: e15e7133290b732a zh: e15e7133290b732a /session-persistence-event-catalog/events/workspace-root/workspace-root-change-log-only: - en: 3f23638849143611 - zh: cdc5e0cbfc4655fd + en: 2a92a2732bc32a7c + zh: 5635944a9ce51002 /session-persistence-event-catalog/resolved-persistence-types: en: 3493ecf0993e5397 zh: ec95688f593a4ed4 @@ -1506,8 +1506,8 @@ en: bef51e0442ebb903 zh: 240ebeb160533d8c /session-persistence-event-catalog/resolved-persistence-types/cwd: - en: b5e8f345d5f15813 - zh: 8115c4e99a4d37d6 + en: dfc595f52f794ea2 + zh: ae9754ae00a97b5f /session-persistence-event-catalog/resolved-persistence-types/decision-durationms-exitcode-handlerid: en: a99ac57bbec0370f zh: 9073329e324a66e0 diff --git a/docs/persistence-catalog.md b/docs/persistence-catalog.md index 0adb75f4a2..ae474257b8 100644 --- a/docs/persistence-catalog.md +++ b/docs/persistence-catalog.md @@ -1324,9 +1324,10 @@ Source: [`packages/web/web-search-deepseek/src/provider.ts:89`](../packages/web/ ```ts persistence-catalog /** - * Directory change recorded by releases whose model could move the directory. A reader keeps `cwd` only - * when it lies inside the Session's original project; otherwise the Session returns to that project. - * @deprecated Read only: no writer records this event. `WorkingDirectoryService.set` records + * Directory change recorded by releases whose model could move the directory. Readers ignore it: a + * Session that holds one resumes in its original project, or in the directory of its last + * `workspace-root/change`. + * @deprecated No writer records this event. `WorkingDirectoryService.set` records * `workspace-root/change`. */ 'working-directory/change': { @@ -1335,7 +1336,7 @@ Source: [`packages/web/web-search-deepseek/src/provider.ts:89`](../packages/web/ } ``` -Source: [`packages/session/working-directory/src/index.ts:61`](../packages/session/working-directory/src/index.ts) +Source: [`packages/session/working-directory/src/index.ts:62`](../packages/session/working-directory/src/index.ts) ### `workspace/*` @@ -1368,7 +1369,7 @@ Source: [`packages/deliverables/workspace-changes/src/types.ts:106`](../packages } ``` -Source: [`packages/session/working-directory/src/index.ts:66`](../packages/session/working-directory/src/index.ts) +Source: [`packages/session/working-directory/src/index.ts:67`](../packages/session/working-directory/src/index.ts) ## Resolved persistence types @@ -6246,7 +6247,7 @@ Sources: [`packages/compaction/compaction/src/types.ts:24`](../packages/compacti SHA-256: `da60ed58a8fa23d7aa4920ab5c1f4e3d28109cb0797ef9e5c8b125721b847582` -Sources: [`packages/session/working-directory/src/index.ts:61`](../packages/session/working-directory/src/index.ts) · [`packages/session/working-directory/src/index.ts:66`](../packages/session/working-directory/src/index.ts) +Sources: [`packages/session/working-directory/src/index.ts:62`](../packages/session/working-directory/src/index.ts) · [`packages/session/working-directory/src/index.ts:67`](../packages/session/working-directory/src/index.ts) | Property | Presence | Type | |---|---|---| diff --git a/docs/persistence-catalog.zh.md b/docs/persistence-catalog.zh.md index 5a04ad3611..cab300e96b 100644 --- a/docs/persistence-catalog.zh.md +++ b/docs/persistence-catalog.zh.md @@ -1326,9 +1326,10 @@ export type SessionEvent = { ```ts persistence-catalog /** - * Directory change recorded by releases whose model could move the directory. A reader keeps `cwd` only - * when it lies inside the Session's original project; otherwise the Session returns to that project. - * @deprecated Read only: no writer records this event. `WorkingDirectoryService.set` records + * Directory change recorded by releases whose model could move the directory. Readers ignore it: a + * Session that holds one resumes in its original project, or in the directory of its last + * `workspace-root/change`. + * @deprecated No writer records this event. `WorkingDirectoryService.set` records * `workspace-root/change`. */ 'working-directory/change': { @@ -1337,7 +1338,7 @@ export type SessionEvent = { } ``` -来源:[`packages/session/working-directory/src/index.ts:61`](../packages/session/working-directory/src/index.ts) +来源:[`packages/session/working-directory/src/index.ts:62`](../packages/session/working-directory/src/index.ts) ### `workspace/*` @@ -1370,7 +1371,7 @@ export type SessionEvent = { } ``` -来源:[`packages/session/working-directory/src/index.ts:66`](../packages/session/working-directory/src/index.ts) +来源:[`packages/session/working-directory/src/index.ts:67`](../packages/session/working-directory/src/index.ts) ## 已解析的持久化类型 @@ -6248,7 +6249,7 @@ SHA-256: `3e4e7683b0192c9f0e6a75c59076108f017892cdb302a98032c2632fd79c356f` SHA-256: `da60ed58a8fa23d7aa4920ab5c1f4e3d28109cb0797ef9e5c8b125721b847582` -来源:[`packages/session/working-directory/src/index.ts:61`](../packages/session/working-directory/src/index.ts) · [`packages/session/working-directory/src/index.ts:66`](../packages/session/working-directory/src/index.ts) +来源:[`packages/session/working-directory/src/index.ts:62`](../packages/session/working-directory/src/index.ts) · [`packages/session/working-directory/src/index.ts:67`](../packages/session/working-directory/src/index.ts) | 属性 | 存在性 | 类型 | |---|---|---| diff --git a/docs/persistence-changes/2026-11-13-workspace-root-change.i18n.yaml b/docs/persistence-changes/2026-11-13-workspace-root-change.i18n.yaml index b4770566d2..e4a79dce1b 100644 --- a/docs/persistence-changes/2026-11-13-workspace-root-change.i18n.yaml +++ b/docs/persistence-changes/2026-11-13-workspace-root-change.i18n.yaml @@ -18,11 +18,11 @@ en: 6907136f28e48c90 zh: 02f8298485f3d8e7 /2026-11-13-workspace-root-change/compatibility: - en: fe9a1a881c1ddb37 - zh: 160fc86c5221bf66 + en: 79cf117b9054b763 + zh: eb1d4f4125ac72bd /2026-11-13-workspace-root-change/verification: - en: ef15e084d2680beb - zh: cc47c3104755e131 + en: 0b94b76f379e118d + zh: 9f7a996db780a565 /2026-11-13-workspace-root-change/dev-note: en: b75c8458928b72b9 zh: 003ba4e17b4b5269 diff --git a/docs/persistence-changes/2026-11-13-workspace-root-change.md b/docs/persistence-changes/2026-11-13-workspace-root-change.md index 5b15d8c8ea..390b9f2895 100644 --- a/docs/persistence-changes/2026-11-13-workspace-root-change.md +++ b/docs/persistence-changes/2026-11-13-workspace-root-change.md @@ -35,12 +35,12 @@ changes: ## Compatibility -New readers fold both events into one directory. workspace-root/change sets it. A stored working-directory/change sets it only when its directory lies inside the Session's original project, which keeps the directory and the relative paths of a Session that worked in a worktree or a subdirectory of its project. This narrows the meaning of a stored working-directory/change whose directory lies outside the original project: earlier readers kept it as the current directory, and this reader returns the Session to the project, because the model could choose that directory and consumers bound writes by the Session's directory. The next request's context reports the directory to the model. A Session that never moves holds neither event and older readers read it as before. Older readers do not know workspace-root/change, which carries no ignorable marker, so they refuse a log that holds it when they decode the row; they never reconstruct a directory that misses the move. +New readers set a Session's directory from workspace-root/change and ignore working-directory/change. This changes the meaning of a stored working-directory/change: earlier readers moved the current directory to it, and this reader leaves the Session in its original project, or in the directory of its last workspace-root/change. The event recorded both a model's cd and an entered worktree without telling them apart, and consumers bound writes by the Session's directory, so a directory the model chose must not become that boundary. A Session that had entered a worktree resumes in its original project and returns through enter_worktree. The next request's context reports the directory to the model. A Session that never moves holds neither event and older readers read it as before. Older readers do not know workspace-root/change, which carries no ignorable marker, so they refuse a log that holds it when they decode the row and do not rebuild a directory that lacks the move. ## Verification -pnpm exec vitest run packages/session/working-directory/tests: 30 tests passed, including replay of stored working-directory/change events inside the project, outside it, after a Host move, in a Session without origin metadata, and under a symlinked header. pnpm exec vitest run packages/session/session-format-catalog/tests/catalog.spec.ts: refusal of a required event type outside the reader's vocabulary. +pnpm exec vitest run packages/session/working-directory/tests: 29 tests passed, including replay of stored working-directory/change events inside and outside the project, before and after a Host move, and in a Session without origin metadata, none of which moves the directory. pnpm exec vitest run packages/session/session-format-catalog/tests/catalog.spec.ts: refusal of a required event type outside the reader's vocabulary. ## Dev Note diff --git a/docs/persistence-changes/2026-11-13-workspace-root-change.zh.md b/docs/persistence-changes/2026-11-13-workspace-root-change.zh.md index 06caf9d9b8..9871690953 100644 --- a/docs/persistence-changes/2026-11-13-workspace-root-change.zh.md +++ b/docs/persistence-changes/2026-11-13-workspace-root-change.zh.md @@ -35,12 +35,12 @@ changes: ## 兼容性 -新读者把两种事件折叠成一个目录。workspace-root/change 设置该目录。已存储的 working-directory/change 只在其目录位于 Session 的原始项目之内时设置该目录,这样曾在工作树或项目子目录中工作的 Session 保留它的目录和相对路径。这收窄了目录位于原始项目之外的已存储 working-directory/change 的含义:早期读者把它保留为当前目录,本读者让 Session 回到项目,因为该目录可能由模型选择,而消费方以 Session 的目录限定写入。下一次请求的上下文向模型报告该目录。从未移动的 Session 不含这两种事件,旧读者照常读取。旧读者不认识 workspace-root/change,它不带 ignorable 标记,所以旧读者在解码该行时拒绝含有它的日志,不会重建出缺少这次移动的目录。 +新读者依据 workspace-root/change 设置 Session 的目录,并忽略 working-directory/change。这改变了已存储的 working-directory/change 的含义:早期读者把当前目录移到它记录的目录,本读者让 Session 留在原始项目,或留在最近一条 workspace-root/change 的目录。该事件既记录模型的 cd,也记录进入工作树,两者无法区分;而消费方以 Session 的目录限定写入,所以模型选择的目录不能成为这条边界。进入过工作树的 Session 在原始项目中恢复,通过 enter_worktree 回到工作树。下一次请求的上下文向模型报告该目录。从未移动的 Session 不含这两种事件,旧读者照常读取。旧读者不认识 workspace-root/change,它不带 ignorable 标记,所以旧读者在解码该行时拒绝含有它的日志,不会重建出缺少这次移动的目录。 ## 验证 -pnpm exec vitest run packages/session/working-directory/tests:30 个测试通过,包括回放位于项目之内、项目之外、Host 移动之后、没有来源元数据的 Session 中以及 header 为符号链接时的已存储 working-directory/change 事件。pnpm exec vitest run packages/session/session-format-catalog/tests/catalog.spec.ts:拒绝读者词汇表之外的必需事件类型。 +pnpm exec vitest run packages/session/working-directory/tests:29 个测试通过,包括回放位于项目之内与之外、Host 移动之前与之后、以及没有来源元数据的 Session 中的已存储 working-directory/change 事件,它们都不移动目录。pnpm exec vitest run packages/session/session-format-catalog/tests/catalog.spec.ts:拒绝读者词汇表之外的必需事件类型。 ## 开发备注 diff --git a/docs/persistence-schema.json b/docs/persistence-schema.json index 8fb9c0366f..b76de8d506 100644 --- a/docs/persistence-schema.json +++ b/docs/persistence-schema.json @@ -611,7 +611,7 @@ {"digest":"d9196086e41e4dd5c2ce87f5c25529c8112a1731d258cf0728747d41a9ee3241","names":[],"sources":[]}, {"digest":"d9c462b9f62fdd3efc76a44b9524dc8781f6b91cd9c7e584b17d971defafebab","names":["packages/llm/llm/src/message.ts#ToolMessageSource"],"sources":["packages/llm/llm/src/message.ts:28"]}, {"digest":"d9edf1862e4471d6ad8a106a3f4160e0643874368707d50ecd8e27e5ee7a2567","names":[],"sources":[]}, - {"digest":"da60ed58a8fa23d7aa4920ab5c1f4e3d28109cb0797ef9e5c8b125721b847582","names":[],"sources":["packages/session/working-directory/src/index.ts:61","packages/session/working-directory/src/index.ts:66"]}, + {"digest":"da60ed58a8fa23d7aa4920ab5c1f4e3d28109cb0797ef9e5c8b125721b847582","names":[],"sources":["packages/session/working-directory/src/index.ts:62","packages/session/working-directory/src/index.ts:67"]}, {"digest":"dacf3d5f3e4ff619c1320e64a48dec5e7ecd7870bece65fbd449a0e63adfa521","names":[],"sources":[]}, {"digest":"db06ef35b27a38425046b2228fa6bce9582abe140d781d363137eb2f4d233a1c","names":[],"sources":[]}, {"digest":"db6c5246911d47a7969f6b4cb47c7d0dbbde75c402710b22ace3672fa97bebe0","names":[],"sources":[]}, diff --git a/docs/subsystems/working-directory.i18n.yaml b/docs/subsystems/working-directory.i18n.yaml index 0559991dc8..8e77a990b4 100644 --- a/docs/subsystems/working-directory.i18n.yaml +++ b/docs/subsystems/working-directory.i18n.yaml @@ -6,8 +6,8 @@ en: 45832e24372ea387 zh: 7e0f31726fa1dc4e /working-directory/directory-selection: - en: b1092859d5a6d970 - zh: 79bcec32ecaf585a + en: 10d9f5bed386adfe + zh: c112d1b4356166b7 /working-directory/model-context: en: 872112549d003dca zh: 48bfd30fdf25c234 diff --git a/docs/subsystems/working-directory.md b/docs/subsystems/working-directory.md index 13af4243c1..e879ff9b3f 100644 --- a/docs/subsystems/working-directory.md +++ b/docs/subsystems/working-directory.md @@ -8,7 +8,7 @@ A Session's active directory is owned by [`dsh-working-directory`](../../package `ctx.workingDirectory.get(session)` reads the logged value. `ensure(agent)` validates it and restores the original project when the directory disappeared. `set(agent, path)` validates and records a move that a Host operation chose, such as worktree creation, the forge panel, or an SDK request; the model has no tool that moves the directory. Sessions without a header directory use the configured runtime fallback; an unavailable fallback produces an error. -`get` is the one read of a Session's directory: relative paths resolve from it, and it is the workspace root for everything that follows the Session's checkout. Every change the service records is a `workspace-root/change`. A log written while the model could move the directory holds `working-directory/change` events, which have no writer now. The service keeps such a directory only when it lies inside the Session's original project, so a Session that worked in a worktree or a subdirectory of its project resumes there. A stored directory outside the project returns the Session to the project, and the next request's context reports the directory. +`get` is the one read of a Session's directory: relative paths resolve from it, and it is the workspace root for everything that follows the Session's checkout. Every change the service records is a `workspace-root/change`. A log written while the model could move the directory holds `working-directory/change` events, which have no writer now. The service ignores them: such a Session resumes in its original project, or in the directory of its last `workspace-root/change`, and the next request's context reports the directory. Changes are local to one Session. Relative filesystem operations, newly started processes, instruction discovery, skills, and file completion use the active directory. Existing terminals and persistent shells retain their own directories. Sandbox writable roots and project membership remain anchored to the original project. @@ -30,7 +30,7 @@ Generated from source by `scripts/gen-cordis-catalog.ts` (verified fresh by `pnp One owner for each Session's directory and its model-visible changes. -A Session works in one directory. Relative paths resolve from it, and it is the workspace root that the write boundary, file views, and discovery follow. Only Host operations move it, through set or the recovery of ensure. A log written while the model could move the directory is read under the rule the `working-directory/change` event documents. +A Session works in one directory. Relative paths resolve from it, and it is the workspace root that the write boundary, file views, and discovery follow. Only Host operations move it, through set or the recovery of ensure. The `working-directory/change` events of a log written while the model could move the directory do not move it. ```ts cordis-catalog /** diff --git a/docs/subsystems/working-directory.zh.md b/docs/subsystems/working-directory.zh.md index 1045a3294c..df9b0ac5da 100644 --- a/docs/subsystems/working-directory.zh.md +++ b/docs/subsystems/working-directory.zh.md @@ -8,7 +8,7 @@ Session 的当前目录由 [`dsh-working-directory`](../../packages/session/work `ctx.workingDirectory.get(session)` 读取日志中的值。`ensure(agent)` 验证它,并在目录消失时恢复原始项目。`set(agent, path)` 验证并记录由 Host 操作选定的移动,例如创建工作树、forge 面板或 SDK 请求;模型没有可以移动目录的工具。header 没有目录的 Session 使用配置的运行时回退目录;回退目录不可用时返回错误。 -`get` 是读取 Session 目录的唯一接口:相对路径从它解析,跟随 Session 检出的一切也以它为工作区根。服务记录的每次变更都是 `workspace-root/change`。写于模型还能移动目录时期的日志含有 `working-directory/change` 事件,现在没有写入方。服务只在这类目录位于 Session 的原始项目之内时保留它,因此曾在工作树或项目子目录中工作的 Session 会在原处恢复。位于项目之外的已存储目录让 Session 回到项目,下一次请求的上下文会报告该目录。 +`get` 是读取 Session 目录的唯一接口:相对路径从它解析,跟随 Session 检出的一切也以它为工作区根。服务记录的每次变更都是 `workspace-root/change`。写于模型还能移动目录时期的日志含有 `working-directory/change` 事件,现在没有写入方。服务忽略它们:这样的 Session 在原始项目中恢复,或在最近一条 `workspace-root/change` 的目录中恢复,下一次请求的上下文会报告该目录。 变更仅影响一个 Session。相对路径文件系统操作、新启动的进程、指令发现、技能和文件补全使用当前目录。已有终端和持久 shell 保留自己的目录。沙箱可写根目录与项目归属继续绑定原始项目。 @@ -30,7 +30,7 @@ Generated from source by `scripts/gen-cordis-catalog.ts` (verified fresh by `pnp One owner for each Session's directory and its model-visible changes. -A Session works in one directory. Relative paths resolve from it, and it is the workspace root that the write boundary, file views, and discovery follow. Only Host operations move it, through set or the recovery of ensure. A log written while the model could move the directory is read under the rule the `working-directory/change` event documents. +A Session works in one directory. Relative paths resolve from it, and it is the workspace root that the write boundary, file views, and discovery follow. Only Host operations move it, through set or the recovery of ensure. The `working-directory/change` events of a log written while the model could move the directory do not move it. ```ts cordis-catalog /** diff --git a/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.i18n.yaml b/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.i18n.yaml index a88861c65b..5283aa02eb 100644 --- a/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.i18n.yaml +++ b/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.i18n.yaml @@ -9,8 +9,8 @@ en: 9f20155a3198221f zh: 4e8fbfcc6e819ac6 /the-working-directory-tool-and-the-subagent-cwd-parameter-are-removed/change: - en: f6ee14728e468841 - zh: 66e08a2631df03b7 + en: acd53092c2453313 + zh: d1c7068ef6df4fe8 /the-working-directory-tool-and-the-subagent-cwd-parameter-are-removed/migration: en: 80fcd319f8db5cd5 zh: 8253a72d4413cbd8 diff --git a/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.md b/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.md index b6326e32fd..49fad71c1d 100644 --- a/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.md +++ b/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.md @@ -10,9 +10,9 @@ English | [中文](guide.zh.md) The model tool `working_directory` and its package `@deepseek-ai/dsh-tool-working-directory` no longer exist; the `dsh-base` and `dsh-sdk-minimal` bundles no longer mount the `tool-working-directory` entry. The delegation tools registered by `@deepseek-ai/dsh-tool-subagent`, and `subagent_oar`, no longer accept `cwd`: a child starts in the caller's workspace directory. -A Session's directory now changes only through Host operations: `enter_worktree`, the new `exit_worktree`, the forge panel, SDK `setWorkingDirectory` (`set_working_directory` in Python), and recovery when the directory disappears. Each change is recorded as `workspace-root/change`; the [persistence record](../../../persistence-changes/2026-11-13-workspace-root-change.md) covers stored logs and readers of earlier releases. A stored Session resumes in its recorded directory when that directory lies inside the Session's original project, and in the original project otherwise. Its recorded `working_directory` calls remain history, and a new call returns `unknown tool "working_directory"`. +A Session's directory now changes only through Host operations: `enter_worktree`, the new `exit_worktree`, the forge panel, SDK `setWorkingDirectory` (`set_working_directory` in Python), and recovery when the directory disappears. Each change is recorded as `workspace-root/change`; the [persistence record](../../../persistence-changes/2026-11-13-workspace-root-change.md) covers stored logs and readers of earlier releases. A stored Session resumes in its original project: the directory changes an earlier release recorded are ignored. Its recorded `working_directory` calls remain history, and a new call returns `unknown tool "working_directory"`. -Who observes it: prompts, skills, and evaluation sets that expect the tool or the parameter in the tool list; profiles and patches that name the `tool-working-directory` entry or the package; stored Sessions whose recorded directory lies outside their original project, such as one opened in a subdirectory of a repository that entered a worktree at the repository root. +Who observes it: prompts, skills, and evaluation sets that expect the tool or the parameter in the tool list; profiles and patches that name the `tool-working-directory` entry or the package; stored Sessions that had entered a worktree or whose model had changed directory. ## Migration diff --git a/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.zh.md b/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.zh.md index 418ed24c31..905e4370fe 100644 --- a/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.zh.md +++ b/docs/upgrade-guide/v0.2.2-rc.3/working-directory-tool-removed/guide.zh.md @@ -10,9 +10,9 @@ description: "模型工具 `working_directory`、它的包以及子代理工具 模型工具 `working_directory` 及其包 `@deepseek-ai/dsh-tool-working-directory` 不再存在;`dsh-base` 与 `dsh-sdk-minimal` 组合包不再挂载 `tool-working-directory` 条目。`@deepseek-ai/dsh-tool-subagent` 注册的委派工具以及 `subagent_oar` 不再接受 `cwd`:子代理从调用方的工作区目录启动。 -Session 的目录现在只通过 Host 操作改变:`enter_worktree`、新增的 `exit_worktree`、forge 面板、SDK 的 `setWorkingDirectory`(Python 中为 `set_working_directory`),以及目录消失时的恢复。每次变更都记录为 `workspace-root/change`;[持久化记录](../../../persistence-changes/2026-11-13-workspace-root-change.zh.md)说明已存储日志的读法和早期版本读者的行为。已存储的 Session 在记录的目录位于其原始项目之内时在该目录恢复,否则在原始项目中恢复。其中记录的 `working_directory` 调用仍是历史,新的调用返回 `unknown tool "working_directory"`。 +Session 的目录现在只通过 Host 操作改变:`enter_worktree`、新增的 `exit_worktree`、forge 面板、SDK 的 `setWorkingDirectory`(Python 中为 `set_working_directory`),以及目录消失时的恢复。每次变更都记录为 `workspace-root/change`;[持久化记录](../../../persistence-changes/2026-11-13-workspace-root-change.zh.md)说明已存储日志的读法和早期版本读者的行为。已存储的 Session 在原始项目中恢复:早期版本记录的目录变更被忽略。其中记录的 `working_directory` 调用仍是历史,新的调用返回 `unknown tool "working_directory"`。 -受影响的对象:期望工具列表中有该工具或该参数的提示词、技能与评测集;引用 `tool-working-directory` 条目或该包的 profile 与 patch;记录的目录位于原始项目之外的已存储 Session,例如在仓库子目录中打开、随后进入仓库根下工作树的 Session。 +受影响的对象:期望工具列表中有该工具或该参数的提示词、技能与评测集;引用 `tool-working-directory` 条目或该包的 profile 与 patch;进入过工作树或模型切换过目录的已存储 Session。 ## 迁移 diff --git a/packages/experimental/client-ui-forge/README.i18n.yaml b/packages/experimental/client-ui-forge/README.i18n.yaml index 6e11bcde40..77042f96b2 100644 --- a/packages/experimental/client-ui-forge/README.i18n.yaml +++ b/packages/experimental/client-ui-forge/README.i18n.yaml @@ -18,8 +18,8 @@ en: 91e5f113f2e5c739 zh: 52eefce9ed4a1239 /deepseek-ai-dsh-experimental-client-ui-forge/understand-the-implementation: - en: 61cbc29f9c015339 - zh: 2e37d83ceba5acf4 + en: b98cb51f33ed09ec + zh: 2950d24bfd887d69 /deepseek-ai-dsh-experimental-client-ui-forge/further-exploration: en: fa4d8c579ffc49ff zh: 5856ed7469e4585b diff --git a/packages/experimental/client-ui-forge/README.md b/packages/experimental/client-ui-forge/README.md index 7d2a7c5bac..58316a7237 100644 --- a/packages/experimental/client-ui-forge/README.md +++ b/packages/experimental/client-ui-forge/README.md @@ -65,7 +65,7 @@ All files opens a searchable directory tree with the selected file scrolled to i Inspection runs `rev-parse --show-toplevel`, `rev-parse --path-format=absolute --git-common-dir`, `rev-parse --path-format=absolute --git-dir`, `symbolic-ref --short HEAD`, `rev-parse --short HEAD`, `rev-parse --abbrev-ref @{upstream}`, and `remote -v`; `branches` runs `symbolic-ref` and `for-each-ref --count=`; `diffBases` finds the default fork point as `localDiff` does below, runs `for-each-ref --sort=-committerdate` over `refs/heads/` and `refs/remotes/` within `maxDiffBytes`, leaves out symbolic refs such as `origin/HEAD`, and keeps at most `maxBranches` branches of each kind whose names contain the query. Inspection then sizes the `branch` scope for the local changes row: it finds the fork point, runs `diff --numstat -z -M` against it with the same helper and submodule restrictions as `localDiff` below, and lists untracked files. Every command uses the Session's resolved sandbox mode: `read-only` and `workspace-write` require confinement, while `danger-full-access` runs without a sandbox backend. Commands disable optional index writes, hooks, the filesystem monitor, prompts, and lazy object fetches, remove inherited Git selectors and secret-bearing variables, and use their own `gitTimeoutMs` deadline and `maxOutputBytes` limit per stream. The top-level, Git, and common Git directories Git prints are resolved through `ctx.fs` into the canonical process paths that the working-directory service records; a checkout whose Git directory differs from the common one is a linked worktree. The worktree option is usable under the `danger-full-access` sandbox mode, never under `read-only`, and under `workspace-write` only when both directories lie inside the canonical workspace root of the Session's sandbox policy. -A worktree request is a `plugin:client-ui-forge/worktree` record, folded by the `forgeWorktree` projection: requests and withdrawals apply until the first `turn/start` or creation, and the first creation of an open request, read from the worktree service's `plugin:worktree/created` record or from the `created` record earlier panels wrote, applies until the first `step/start`. The projection also records the Session's directory from its header and each `workspace-root/change` or `working-directory/change`; its client view, which the Session summary carries, is the created worktree while that directory is the worktree or lies below it, else null. `requestWorktree` refuses with `forgePanel/session-started` after a turn started or the worktree was created, with `forgePanel/worktrees-unavailable` without a `worktrees` service, and with `forgePanel/invalid-branch` for an empty branch; `cancelWorktree` refuses only with the first and needs no `worktrees` service; both forget a failed attempt. When a message enters the inbox of a Session whose request is open, an `agent/inbox/inserted` listener starts maintenance of the Session's Agent before the delivery wakes it, so the Agent latches the wake and starts the message's turn once that hold ends. The hold makes one attempt with a new branch name, publishing the running stage in the panel state with `held` set and `waiting` as `creation`, and records `created` with the source repository root. After a failed attempt, whose branch, base, failed step, and reason stay in the panel state as `failure`, or after `stopCreation`, the hold waits: `retryWorktree` makes another attempt, refused with `forgePanel/not-waiting` while none waits or one runs, and withdrawing the request lets the message go on in the current checkout. Each message that enters while the Agent is idle and the Session works in its created worktree starts a hold that checks whether the worktree is missing; while it is, the hold waits with `waiting` as `restoration`, and `restoreWorktree` or `continueInProject` runs inside it and then lets the message go on; a check that cannot answer lets the message go at once. The Agent's cancellation and the Remote's stop end a hold, and a message that enters while the Agent runs a turn or other maintenance gets none. The worktree service queues the working-directory notice, which the first step claims. An `agent/pre-step` listener creates the worktree at step 1 of a Session whose request is still open, which covers a message no hold took; it moves the notice into the proposed messages, and a failure ends the turn with the request kept. `restoreWorktree` and `continueInProject` run as maintenance of their own while no message waits; `restoreWorktree` publishes its running step as `restoration`, and clears `missing` in the same state that ends it. `track` rejects a URL no provider recognizes with `forgePanel/unrecognized-url`. `markReady` and `merge` act only on a visible binding of the Session that a provider serves, else they refuse with `forgePanel/not-bound`; `merge` passes the head commit the card showed, and a moved head refuses with `forgePanel/stale-head`, while any other refusal is `forgePanel/action-failed` with the host's reason. +A worktree request is a `plugin:client-ui-forge/worktree` record, folded by the `forgeWorktree` projection: requests and withdrawals apply until the first `turn/start` or creation, and the first creation of an open request, read from the worktree service's `plugin:worktree/created` record or from the `created` record earlier panels wrote, applies until the first `step/start`. The projection also records the Session's directory from its header and each `workspace-root/change`; its client view, which the Session summary carries, is the created worktree while that directory is the worktree or lies below it, else null. `requestWorktree` refuses with `forgePanel/session-started` after a turn started or the worktree was created, with `forgePanel/worktrees-unavailable` without a `worktrees` service, and with `forgePanel/invalid-branch` for an empty branch; `cancelWorktree` refuses only with the first and needs no `worktrees` service; both forget a failed attempt. When a message enters the inbox of a Session whose request is open, an `agent/inbox/inserted` listener starts maintenance of the Session's Agent before the delivery wakes it, so the Agent latches the wake and starts the message's turn once that hold ends. The hold makes one attempt with a new branch name, publishing the running stage in the panel state with `held` set and `waiting` as `creation`, and records `created` with the source repository root. After a failed attempt, whose branch, base, failed step, and reason stay in the panel state as `failure`, or after `stopCreation`, the hold waits: `retryWorktree` makes another attempt, refused with `forgePanel/not-waiting` while none waits or one runs, and withdrawing the request lets the message go on in the current checkout. Each message that enters while the Agent is idle and the Session works in its created worktree starts a hold that checks whether the worktree is missing; while it is, the hold waits with `waiting` as `restoration`, and `restoreWorktree` or `continueInProject` runs inside it and then lets the message go on; a check that cannot answer lets the message go at once. The Agent's cancellation and the Remote's stop end a hold, and a message that enters while the Agent runs a turn or other maintenance gets none. The worktree service queues the working-directory notice, which the first step claims. An `agent/pre-step` listener creates the worktree at step 1 of a Session whose request is still open, which covers a message no hold took; it moves the notice into the proposed messages, and a failure ends the turn with the request kept. `restoreWorktree` and `continueInProject` run as maintenance of their own while no message waits; `restoreWorktree` publishes its running step as `restoration`, and clears `missing` in the same state that ends it. `track` rejects a URL no provider recognizes with `forgePanel/unrecognized-url`. `markReady` and `merge` act only on a visible binding of the Session that a provider serves, else they refuse with `forgePanel/not-bound`; `merge` passes the head commit the card showed, and a moved head refuses with `forgePanel/stale-head`, while any other refusal is `forgePanel/action-failed` with the host's reason. Restoration records `restoring` before mutation and `restored` only after success. Projection version 3 retains incomplete restoration across reloads. Directory existence alone cannot release a waiting message: the checkout root and shared Git directory must match the recorded worktree. Plugin unload cancels and joins all creation, restoration, and waiting tasks before releasing panel readers and the projection. diff --git a/packages/experimental/client-ui-forge/README.zh.md b/packages/experimental/client-ui-forge/README.zh.md index 1f4df4748e..b7db1dd5e9 100644 --- a/packages/experimental/client-ui-forge/README.zh.md +++ b/packages/experimental/client-ui-forge/README.zh.md @@ -65,7 +65,7 @@ diff 按文件树的深度优先顺序排列:同层目录在前、文件在后 检查运行 `rev-parse --show-toplevel`、`rev-parse --path-format=absolute --git-common-dir`、`rev-parse --path-format=absolute --git-dir`、`symbolic-ref --short HEAD`、`rev-parse --short HEAD`、`rev-parse --abbrev-ref @{upstream}` 与 `remote -v`;`branches` 运行 `symbolic-ref` 与 `for-each-ref --count=`;`diffBases` 像下文的 `localDiff` 一样找出默认分叉点,在 `maxDiffBytes` 以内对 `refs/heads/` 与 `refs/remotes/` 运行 `for-each-ref --sort=-committerdate`,排除 `origin/HEAD` 等符号引用,每类最多保留 `maxBranches` 个名称包含查询文字的分支。之后,检查会为本地变更行统计 `branch` 范围的规模:找出分叉点,对它运行 `diff --numstat -z -M`,采用与下文 `localDiff` 相同的辅助程序和子模块限制,并列出未跟踪文件。每个命令都使用 Session 解析出的沙箱模式:`read-only` 与 `workspace-write` 需要沙箱,`danger-full-access` 无需沙箱后端。命令禁用可选的索引写入、钩子、文件系统监视器、提示与对象的延迟获取,移除继承的 Git 选择器与携带密钥的变量,并各自受 `gitTimeoutMs` 时限与每个输出流的 `maxOutputBytes` 上限约束。Git 打印的顶层目录、Git 目录与公共 Git 目录经 `ctx.fs` 解析为工作目录服务记录的规范进程路径;Git 目录与公共 Git 目录不同的检出是链接 worktree。工作树选项在 `danger-full-access` 沙箱模式下可用,在 `read-only` 下不可用,在 `workspace-write` 下只有两个目录都位于 Session 沙箱策略的规范工作区根内时才可用。 -worktree 请求是一条 `plugin:client-ui-forge/worktree` 记录,由 `forgeWorktree` 投影折叠:请求与撤回在第一个 `turn/start` 或创建之前生效;在第一个 `step/start` 之前,未完成请求的第一条 `created` 记录生效。该投影还从 Session 头部与每个 `workspace-root/change` 或 `working-directory/change` 记录 Session 的目录;它的客户端视图随 Session 摘要下发,在该目录就是所创建的 worktree 或位于其下时为该 worktree,否则为 null。一轮开始或 worktree 已创建之后,`requestWorktree` 以 `forgePanel/session-started` 拒绝;没有 `worktrees` 服务时以 `forgePanel/worktrees-unavailable` 拒绝;分支名为空时以 `forgePanel/invalid-branch` 拒绝;`cancelWorktree` 只以第一种拒绝,也不需要 `worktrees` 服务;两者都会忘掉失败的尝试。消息进入有未完成请求的 Session 的收件箱时,一个 `agent/inbox/inserted` 监听器在这次投递唤醒 Agent 之前启动 Session 所属 Agent 的维护任务,因此 Agent 会暂存唤醒,等这次暂留结束后才开始这条消息的这一轮。暂留使用新分支名进行一次尝试,在面板状态中发布正在进行的阶段,设置 `held`,`waiting` 为 `creation`,并记录带源仓库根目录的 `created`。尝试失败(其分支、基准、失败步骤与原因以 `failure` 留在面板状态中)或经 `stopCreation` 停止后,暂留继续等待:`retryWorktree` 再做一次尝试,没有消息等待或已有尝试进行时以 `forgePanel/not-waiting` 拒绝;撤回请求则让消息在当前检出中继续。Agent 空闲且 Session 在所创建 worktree 中工作时,进入的每条消息都会启动一次暂留,检查该 worktree 是否缺失;缺失期间暂留以 `waiting` 为 `restoration` 等待,`restoreWorktree` 或 `continueInProject` 在暂留内运行,之后放行消息;检查无法给出结果时立即放行。Agent 的取消与 Remote 的停止都会结束暂留;Agent 正在运行一轮或其他维护任务时进入的消息不会被暂留。worktree 服务把工作目录通知排进队列,由第一步认领。一个 `agent/pre-step` 监听器在请求仍未完成的 Session 的第 1 步创建 worktree,覆盖没有被暂留的消息;它把通知移入本步提议的消息,失败时这一轮结束、请求保留。没有消息等待时,`restoreWorktree` 与 `continueInProject` 以各自的维护任务运行;`restoreWorktree` 以 `restoration` 发布正在进行的步骤,并在结束恢复的同一状态中清除 `missing`。套件把它的 `worktrees` 服务隔离,因此同时启用 Git Worktrees 时会有两个互不影响的服务。`track` 以 `forgePanel/unrecognized-url` 拒绝没有提供方识别的 URL。`markReady` 与 `merge` 只作用于该 Session 中由提供方服务的可见绑定,否则以 `forgePanel/not-bound` 拒绝;`merge` 带上卡片显示的 head 提交,head 已变化时以 `forgePanel/stale-head` 拒绝,其他拒绝都是带主机原因的 `forgePanel/action-failed`。 +worktree 请求是一条 `plugin:client-ui-forge/worktree` 记录,由 `forgeWorktree` 投影折叠:请求与撤回在第一个 `turn/start` 或创建之前生效;在第一个 `step/start` 之前,未完成请求的第一条 `created` 记录生效。该投影还从 Session 头部与每个 `workspace-root/change` 记录 Session 的目录;它的客户端视图随 Session 摘要下发,在该目录就是所创建的 worktree 或位于其下时为该 worktree,否则为 null。一轮开始或 worktree 已创建之后,`requestWorktree` 以 `forgePanel/session-started` 拒绝;没有 `worktrees` 服务时以 `forgePanel/worktrees-unavailable` 拒绝;分支名为空时以 `forgePanel/invalid-branch` 拒绝;`cancelWorktree` 只以第一种拒绝,也不需要 `worktrees` 服务;两者都会忘掉失败的尝试。消息进入有未完成请求的 Session 的收件箱时,一个 `agent/inbox/inserted` 监听器在这次投递唤醒 Agent 之前启动 Session 所属 Agent 的维护任务,因此 Agent 会暂存唤醒,等这次暂留结束后才开始这条消息的这一轮。暂留使用新分支名进行一次尝试,在面板状态中发布正在进行的阶段,设置 `held`,`waiting` 为 `creation`,并记录带源仓库根目录的 `created`。尝试失败(其分支、基准、失败步骤与原因以 `failure` 留在面板状态中)或经 `stopCreation` 停止后,暂留继续等待:`retryWorktree` 再做一次尝试,没有消息等待或已有尝试进行时以 `forgePanel/not-waiting` 拒绝;撤回请求则让消息在当前检出中继续。Agent 空闲且 Session 在所创建 worktree 中工作时,进入的每条消息都会启动一次暂留,检查该 worktree 是否缺失;缺失期间暂留以 `waiting` 为 `restoration` 等待,`restoreWorktree` 或 `continueInProject` 在暂留内运行,之后放行消息;检查无法给出结果时立即放行。Agent 的取消与 Remote 的停止都会结束暂留;Agent 正在运行一轮或其他维护任务时进入的消息不会被暂留。worktree 服务把工作目录通知排进队列,由第一步认领。一个 `agent/pre-step` 监听器在请求仍未完成的 Session 的第 1 步创建 worktree,覆盖没有被暂留的消息;它把通知移入本步提议的消息,失败时这一轮结束、请求保留。没有消息等待时,`restoreWorktree` 与 `continueInProject` 以各自的维护任务运行;`restoreWorktree` 以 `restoration` 发布正在进行的步骤,并在结束恢复的同一状态中清除 `missing`。套件把它的 `worktrees` 服务隔离,因此同时启用 Git Worktrees 时会有两个互不影响的服务。`track` 以 `forgePanel/unrecognized-url` 拒绝没有提供方识别的 URL。`markReady` 与 `merge` 只作用于该 Session 中由提供方服务的可见绑定,否则以 `forgePanel/not-bound` 拒绝;`merge` 带上卡片显示的 head 提交,head 已变化时以 `forgePanel/stale-head` 拒绝,其他拒绝都是带主机原因的 `forgePanel/action-failed`。 恢复在修改前记录 `restoring`,仅在成功后记录 `restored`。投影版本 3 在重载后保留未完成的恢复。目录存在本身不能放行等待的消息:检出根目录与公共 Git 目录必须匹配记录的工作树。插件卸载会取消并等待所有创建、恢复与消息等待任务,然后释放面板读取器和投影。 diff --git a/packages/experimental/client-ui-forge/src/types.ts b/packages/experimental/client-ui-forge/src/types.ts index 2b194f040e..a8b575a629 100644 --- a/packages/experimental/client-ui-forge/src/types.ts +++ b/packages/experimental/client-ui-forge/src/types.ts @@ -71,9 +71,7 @@ export interface ForgeWorktreeState { readonly locked: boolean /** Whether a step has started; no worktree is created afterwards. */ readonly stepped: boolean - /** The Session's original project, or its first recorded directory without one; null before either. */ - readonly project: string | null - /** The Session's working directory as the working-directory service reads its log, or null when none is recorded. */ + /** The Session's working directory as its log last recorded it, or null when none is recorded. */ readonly cwd: string | null /** Whether a restoration started without a successful completion, including across reloads. */ readonly restoring: boolean diff --git a/packages/experimental/client-ui-forge/src/worktree.ts b/packages/experimental/client-ui-forge/src/worktree.ts index 17fd165293..65a54e3c18 100644 --- a/packages/experimental/client-ui-forge/src/worktree.ts +++ b/packages/experimental/client-ui-forge/src/worktree.ts @@ -19,7 +19,7 @@ import type { /** Plugin record type of worktree requests, withdrawals, and creations. */ export const WORKTREE_RECORD = 'plugin:client-ui-forge/worktree' -const EMPTY: Omit = { request: null, created: null, locked: false, stepped: false, restoring: false } +const EMPTY: Omit = { request: null, created: null, locked: false, stepped: false, restoring: false } const worktreeSchema: z.ZodType = z.object({ path: z.string().min(1), @@ -43,7 +43,6 @@ const stateSchema: z.ZodType = z.object({ locked: z.boolean(), stepped: z.boolean(), restoring: z.boolean(), - project: z.string().min(1).nullable(), cwd: z.string().min(1).nullable(), }).strict() @@ -60,9 +59,9 @@ const stateSchema: z.ZodType = z.object({ */ export const forgeWorktreeProjection = { key: 'forgeWorktree', - stateVersion: 6, + stateVersion: 7, stateSchema, - init: header => ({ ...EMPTY, project: header.cwd ?? null, cwd: header.cwd ?? null }), + init: header => ({ ...EMPTY, cwd: header.cwd ?? null }), apply, wire: { viewSchema: worktreeSchema.nullable(), @@ -71,10 +70,9 @@ export const forgeWorktreeProjection = { } satisfies ProjectionDefinition<'forgeWorktree', ForgeWorktreeState> /** - * Whether `path` is `root` or lies below it. Both are canonical absolute paths of one execution - * world; the test is lexical and never touches the filesystem. - * @param root - canonical absolute directory. - * @param path - canonical absolute path to test. + * Whether `path` is `root` or lies below it. + * @param root - absolute directory. + * @param path - absolute path. * @returns whether `root` contains `path`. */ export function within(root: string, path: string): boolean { @@ -98,20 +96,13 @@ function apply(state: ForgeWorktreeState, event: SessionEvent): ForgeWorktreeSta case 'step/start': return state.stepped ? state : { ...state, stepped: true } case 'workspace-root/change': - return moved(state, event.data.cwd) - // As the working-directory service reads it: a stored directory outside the project returns the Session to the project. - case 'working-directory/change': - return moved(state, state.project === null || within(state.project, event.data.cwd) ? event.data.cwd : state.project) + return state.cwd === event.data.cwd ? state : { ...state, cwd: event.data.cwd } // Every other event type, including plugin records, which sit outside SessionEventMap. default: return applyRecord(state, event) } } -function moved(state: ForgeWorktreeState, cwd: string): ForgeWorktreeState { - return state.cwd === cwd ? state : { ...state, project: state.project ?? cwd, cwd } -} - function applyRecord(state: ForgeWorktreeState, event: SessionEvent): ForgeWorktreeState { const record = pluginRecordOf(event) if (record?.type === CREATED_RECORD) { diff --git a/packages/experimental/client-ui-forge/tests/controller.host.spec.ts b/packages/experimental/client-ui-forge/tests/controller.host.spec.ts index 524fe9f85b..cd8bf7f3b3 100644 --- a/packages/experimental/client-ui-forge/tests/controller.host.spec.ts +++ b/packages/experimental/client-ui-forge/tests/controller.host.spec.ts @@ -2057,7 +2057,7 @@ describe('forgePanel lifecycle', () => { await opened.ctx.plugin(ForgePanelController, {}) expect(opened.ctx.sessionProjections.checkpoint(opened.session).forgeWorktree?.val) - .toEqual({ request: { from: null }, created: null, locked: false, stepped: false, project: repo, cwd: repo, restoring: false }) + .toEqual({ request: { from: null }, created: null, locked: false, stepped: false, cwd: repo, restoring: false }) }) }) diff --git a/packages/experimental/client-ui-forge/tests/states-remote.host.spec.ts b/packages/experimental/client-ui-forge/tests/states-remote.host.spec.ts index e7a76eb0fb..76754fcb34 100644 --- a/packages/experimental/client-ui-forge/tests/states-remote.host.spec.ts +++ b/packages/experimental/client-ui-forge/tests/states-remote.host.spec.ts @@ -137,7 +137,7 @@ describe('forgePanel.changeRequestStates', () => { expect([...first.values()]).toEqual(expect.arrayContaining([state(7), state(8)])) expect(first.has(pullUrl(10))).toBe(false) // Other client-visible units of a loaded Session change without rebinding anything. - b.session.append('working-directory/change', { cwd: `${b.session.header.cwd ?? ''}/sub` }) + b.session.append('workspace-root/change', { cwd: `${b.session.header.cwd ?? ''}/sub` }) b.ctx.forgeSession.track(b.session, pullUrl(9)) expect((await r.until(states => states.has(pullUrl(9)))).get(pullUrl(9))).toEqual(state(9)) expect(records.get(storedKey(pullUrl(9)))).toEqual(state(9)) diff --git a/packages/experimental/client-ui-forge/tests/worktree.host.spec.ts b/packages/experimental/client-ui-forge/tests/worktree.host.spec.ts index d1d5cea473..f3245e94c9 100644 --- a/packages/experimental/client-ui-forge/tests/worktree.host.spec.ts +++ b/packages/experimental/client-ui-forge/tests/worktree.host.spec.ts @@ -31,9 +31,7 @@ const WORKTREE: ForgeSessionWorktree = { } /** The state before any worktree record, turn, step, or directory change of a Session without a recorded directory. */ -const EMPTY: ForgeWorktreeState = { - request: null, created: null, locked: false, stepped: false, project: null, cwd: null, restoring: false, -} +const EMPTY: ForgeWorktreeState = { request: null, created: null, locked: false, stepped: false, cwd: null, restoring: false } /** A live Session whose `forgeWorktree` state a registry folds without the panel Remote. */ async function folded(): Promise<{ ctx: Context; session: Session; state: () => ForgeWorktreeState }> { @@ -69,7 +67,7 @@ describe('forgeWorktree projection', () => { appendPluginRecord(session, WORKTREE_RECORD, { action: 'created', worktree: WORKTREE }) startTurn(session, 1) startStep(session, 1) - session.append('working-directory/change', { cwd: join(WORKTREE.path, 'packages') }) + session.append('workspace-root/change', { cwd: join(WORKTREE.path, 'packages') }) const inside = [...published] session.append('workspace-root/change', { cwd: WORKTREE.repositoryRoot }) // Shares the worktree's path as a string prefix without lying below it. @@ -88,19 +86,13 @@ describe('forgeWorktree projection', () => { }) it('starts from the directory the Session header records', () => { - expect(forgeWorktreeProjection.init(plainSession('/work/repo').header)).toEqual({ ...EMPTY, project: '/work/repo', cwd: '/work/repo' }) + expect(forgeWorktreeProjection.init(plainSession('/work/repo').header)).toEqual({ ...EMPTY, cwd: '/work/repo' }) }) - it('reads a stored directory change as the working-directory service does', () => { - // Opened below the repository root, so the worktree lies outside the Session's project. - const session = plainSession('/work/repo/packages/app') - let state = forgeWorktreeProjection.init(session.header) - state = forgeWorktreeProjection.apply(state, session.append('working-directory/change', { cwd: '/work/repo/packages/app/src' })) - expect(state.cwd).toBe('/work/repo/packages/app/src') - state = forgeWorktreeProjection.apply(state, session.append('working-directory/change', { cwd: WORKTREE.path })) - expect(state).toEqual({ ...EMPTY, project: '/work/repo/packages/app', cwd: '/work/repo/packages/app' }) - state = forgeWorktreeProjection.apply(state, session.append('workspace-root/change', { cwd: WORKTREE.path })) - expect(state).toEqual({ ...EMPTY, project: '/work/repo/packages/app', cwd: WORKTREE.path }) + it('ignores a directory change an earlier release recorded, as the working-directory service does', () => { + const session = plainSession('/work/repo') + const state = forgeWorktreeProjection.init(session.header) + expect(forgeWorktreeProjection.apply(state, session.append('working-directory/change', { cwd: WORKTREE.path }))).toBe(state) }) it('replays incomplete restoration until an explicit successful completion', async () => { @@ -375,7 +367,7 @@ describe('creating a requested worktree', () => { const running = { phase: 'prepare', branch, from: 'later', held: false } expect(opened.changes).toEqual([running, running, null]) expect(opened.records).toEqual([{ action: 'request', from: 'later' }]) - expect(stateOf(opened)).toEqual({ ...EMPTY, request: { from: 'later' }, project: repo, cwd: repo }) + expect(stateOf(opened)).toEqual({ ...EMPTY, request: { from: 'later' }, cwd: repo }) expect(opened.ctx.workingDirectory.get(opened.session)).toBe(repo) }) @@ -534,7 +526,7 @@ describe('worktree creation at the first step proposal', () => { await expect(proposeStep(opened.ctx, opened.agent, { turn: 1, step: 1, messages: [prompt('go')] })) .rejects.toMatchObject({ name: 'CreationFailure', branch: GENERATED_BRANCH, phase: 'prepare', reason: MISSING_BASE }) const failed = opened.changes[0]?.branch - expect(stateOf(opened)).toEqual({ ...EMPTY, request: { from: 'later' }, locked: true, project: repo, cwd: repo }) + expect(stateOf(opened)).toEqual({ ...EMPTY, request: { from: 'later' }, locked: true, cwd: repo }) expect(opened.records).toEqual([{ action: 'request', from: 'later' }]) expect(opened.ctx.workingDirectory.get(opened.session)).toBe(repo) @@ -565,7 +557,7 @@ describe('worktree creation at the first step proposal', () => { expect(await settled).toBe(cause) expect(opened.changes.at(-1)).toBeNull() expect(opened.records).toEqual([{ action: 'request', from: null }]) - expect(stateOf(opened)).toEqual({ ...EMPTY, request: { from: null }, project: repo, cwd: repo }) + expect(stateOf(opened)).toEqual({ ...EMPTY, request: { from: null }, cwd: repo }) expect(opened.ctx.workingDirectory.get(opened.session)).toBe(repo) expect(await branches(repo)).toEqual(['main']) }) @@ -577,7 +569,7 @@ describe('worktree creation at the first step proposal', () => { await expect(proposeStep(opened.ctx, opened.agent, { turn: 1, step: 1, messages: [prompt('go')] })) .rejects.toThrow('No worktree service is mounted') - expect(stateOf(opened)).toEqual({ ...EMPTY, request: { from: null }, project: repo, cwd: repo }) + expect(stateOf(opened)).toEqual({ ...EMPTY, request: { from: null }, cwd: repo }) expect(opened.changes).toEqual([]) expect(opened.ctx.workingDirectory.get(opened.session)).toBe(repo) }) diff --git a/packages/experimental/worktree/tests/worktree.spec.ts b/packages/experimental/worktree/tests/worktree.spec.ts index 168bf292ad..07db077dbd 100644 --- a/packages/experimental/worktree/tests/worktree.spec.ts +++ b/packages/experimental/worktree/tests/worktree.spec.ts @@ -78,13 +78,13 @@ describe('creating and entering retained worktrees', () => { expect(await git(root, 'rev-parse', '--verify', 'refs/heads/inner')).toBe(inner.baseCommit) }) - it('exits a worktree whose subdirectory a working-directory/change recorded', async () => { + it('exits a worktree from one of its subdirectories', async () => { const root = await repo() ctx = await harness(root) const agent = testAgent(ctx, root) const created = await ctx.worktrees.create(agent, { name: 'topic' }) await mkdir(join(created.path, 'src')) - agent.session.append('working-directory/change', { cwd: join(created.path, 'src') }) + await ctx.workingDirectory.set(agent, join(created.path, 'src')) await expect(ctx.worktrees.exit(agent)).resolves.toEqual(created) expect(ctx.workingDirectory.get(agent.session)).toBe(root) }) diff --git a/packages/extensions/tool-cordis/src/api-catalog.ts b/packages/extensions/tool-cordis/src/api-catalog.ts index 2b985d0b6f..bcdd04cff8 100644 --- a/packages/extensions/tool-cordis/src/api-catalog.ts +++ b/packages/extensions/tool-cordis/src/api-catalog.ts @@ -4843,7 +4843,7 @@ export const SERVICE_API: readonly ServiceApiEntry[] = [ { key: 'workingDirectory', summary: 'One owner for each Session\'s directory and its model-visible changes.', - description: 'One owner for each Session\'s directory and its model-visible changes.\n\nA Session works in one directory. Relative paths resolve from it, and it is the workspace root that the write boundary, file views, and discovery follow. Only Host operations move it, through set or the recovery of ensure. A log written while the model could move the directory is read under the rule the `working-directory/change` event documents.', + description: 'One owner for each Session\'s directory and its model-visible changes.\n\nA Session works in one directory. Relative paths resolve from it, and it is the workspace root that the write boundary, file views, and discovery follow. Only Host operations move it, through set or the recovery of ensure. The `working-directory/change` events of a log written while the model could move the directory do not move it.', methods: [ { signature: 'readonly defaultDirectory: string', diff --git a/packages/session/working-directory/README.i18n.yaml b/packages/session/working-directory/README.i18n.yaml index 2fd1cc9979..6aa746a74e 100644 --- a/packages/session/working-directory/README.i18n.yaml +++ b/packages/session/working-directory/README.i18n.yaml @@ -18,8 +18,8 @@ en: 4bb4eccf3b4b5f2f zh: 41aae306e7fc82cf /deepseek-ai-dsh-working-directory/understand-the-implementation: - en: 6da2a21fdcafacb1 - zh: 3497b6e8b7e01013 + en: 72f9d764f7caa22e + zh: 8598c11c937f2236 /deepseek-ai-dsh-working-directory/further-exploration: en: eabff683146786c8 zh: 7997dec17802ef64 @@ -39,8 +39,8 @@ en: f5a095acef81df3b zh: 758796edbc4337fc /deepseek-ai-dsh-working-directory/known-limitations-and-deferred-work: - en: a03a49e5ea8214d0 - zh: 9b152deb6a74812d + en: 9c1348e472dfecf4 + zh: 9fd16d792ca14853 /deepseek-ai-dsh-working-directory/known-limitations-and-deferred-work/dev-note: en: e67f633dc6fe7773 zh: a4b126f9a4eaca6f diff --git a/packages/session/working-directory/README.md b/packages/session/working-directory/README.md index 2d706fd8b2..4689e81f48 100644 --- a/packages/session/working-directory/README.md +++ b/packages/session/working-directory/README.md @@ -43,7 +43,7 @@ Directory validation and publication delegate through `working-directory/access`
Implementation internals — click to expand -The Session projection owns the effective directory and exposes it in Session observations, so cold readers can use it without activating an Agent; the header continues to identify the original project. Every change the service records is a `workspace-root/change`. The same fold reads the `working-directory/change` events earlier releases stored, which no writer records now: it keeps such a directory only when it lies inside the Session's original project and otherwise returns to the project, so a directory the model once chose outside the project never becomes the workspace. The containment test compares paths as recorded. Changes serialize per Session, validate through the filesystem provider, check cancellation and Agent lifetime, commit an event, and queue a user-context notice. If notice queuing fails after commit, the operation returns the committed directory and logs a warning; the next request or resume still receives the required directory context. Prompt assembly validates the current directory before publishing its required context contribution. Existing processes and sandbox grants keep their independent ownership. The [service implementation](src/index.ts) defines the API. Failures are `WorkingDirectoryError` instances: `WORKING_DIRECTORY_INVALID` for an empty or malformed path, `WORKING_DIRECTORY_NOT_FOUND` when a requested change names no existing directory, and `WORKING_DIRECTORY_UNAVAILABLE` when neither the current directory nor the original project exists or the service has been disposed. +The Session projection owns the effective directory and exposes it in Session observations, so cold readers can use it without activating an Agent; the header continues to identify the original project. Every change the service records is a `workspace-root/change`. The fold ignores the `working-directory/change` events earlier releases stored, which no writer records now, so a directory the model once chose never becomes the workspace. Changes serialize per Session, validate through the filesystem provider, check cancellation and Agent lifetime, commit an event, and queue a user-context notice. If notice queuing fails after commit, the operation returns the committed directory and logs a warning; the next request or resume still receives the required directory context. Prompt assembly validates the current directory before publishing its required context contribution. Existing processes and sandbox grants keep their independent ownership. The [service implementation](src/index.ts) defines the API. Failures are `WorkingDirectoryError` instances: `WORKING_DIRECTORY_INVALID` for an empty or malformed path, `WORKING_DIRECTORY_NOT_FOUND` when a requested change names no existing directory, and `WORKING_DIRECTORY_UNAVAILABLE` when neither the current directory nor the original project exists or the service has been disposed. No runtime invariant companion is published because the service reads its only directory state from the Session projection and checks filesystem validity at use. @@ -82,7 +82,7 @@ Directory transitions append context without replacing the stable system-prompt - **Header readers** — the sandbox write boundary, the workspace file service, the change recorder, and the client file views still read the Session header, so they stay on the original project after a Host move. Connecting them to `get` is the deferred work the [workspace-root note](../../../.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.md#deferred) lists. -- **Stored directories outside the project** — a Session whose stored log left its original project, including one opened in a subdirectory of a repository that then entered a worktree at the repository root, resumes in the original project. `enter_worktree` or another Host move returns it to the worktree. +- **Stored directory changes** — a Session stored by an earlier release resumes in its original project, even when it had entered a worktree or its model had changed directory. `enter_worktree` or another Host move returns it to the worktree. - **Missing original project** — when the current directory and the original project are both unavailable, prompt assembly rejects every model turn, including chat-only turns. The service does not choose or recreate a repository. Restore the original directory, or use SDK `setWorkingDirectory` (`set_working_directory` in Python) or `ctx.workingDirectory.set` to select an existing absolute directory before retrying. Existing shells retain their process-local directories. diff --git a/packages/session/working-directory/README.zh.md b/packages/session/working-directory/README.zh.md index 55f563242c..d03c22710e 100644 --- a/packages/session/working-directory/README.zh.md +++ b/packages/session/working-directory/README.zh.md @@ -43,7 +43,7 @@ kind: "package-reference"
实现细节 — 点击展开 -Session 投影拥有当前目录,并通过 Session observation 暴露它,因此冷态读取方无需激活 Agent 即可使用;header 继续标识原始项目。服务记录的每次变更都是 `workspace-root/change`。同一个折叠也读取早期版本存储的 `working-directory/change` 事件,现在没有写入方:只有当这类目录位于 Session 的原始项目之内时才保留它,否则回到项目,因此模型曾在项目之外选择的目录永远不会成为工作区。包含判断按记录的路径拼写比较。每个 Session 的变更串行执行,通过文件系统 provider 验证,并检查取消状态与 Agent 生命周期,提交事件后排入用户上下文通知。若提交后通知入队失败,操作仍返回已提交的目录并记录警告;下一次请求或恢复仍会收到必需的目录上下文。提示词组装在发布必需的上下文条目前验证当前目录。已有进程与沙箱授权继续由各自的服务拥有。[服务实现](src/index.ts) 定义 API。失败均为 `WorkingDirectoryError`:路径为空或格式错误时为 `WORKING_DIRECTORY_INVALID`,请求切换到不存在的目录时为 `WORKING_DIRECTORY_NOT_FOUND`,当前目录与原始项目都不存在或服务已释放时为 `WORKING_DIRECTORY_UNAVAILABLE`。 +Session 投影拥有当前目录,并通过 Session observation 暴露它,因此冷态读取方无需激活 Agent 即可使用;header 继续标识原始项目。服务记录的每次变更都是 `workspace-root/change`。折叠忽略早期版本存储的 `working-directory/change` 事件,现在没有写入方,因此模型曾选择的目录永远不会成为工作区。每个 Session 的变更串行执行,通过文件系统 provider 验证,并检查取消状态与 Agent 生命周期,提交事件后排入用户上下文通知。若提交后通知入队失败,操作仍返回已提交的目录并记录警告;下一次请求或恢复仍会收到必需的目录上下文。提示词组装在发布必需的上下文条目前验证当前目录。已有进程与沙箱授权继续由各自的服务拥有。[服务实现](src/index.ts) 定义 API。失败均为 `WorkingDirectoryError`:路径为空或格式错误时为 `WORKING_DIRECTORY_INVALID`,请求切换到不存在的目录时为 `WORKING_DIRECTORY_NOT_FOUND`,当前目录与原始项目都不存在或服务已释放时为 `WORKING_DIRECTORY_UNAVAILABLE`。 不发布 `./invariant` 伴生模块,因为服务从 Session 投影读取唯一的目录状态,并在使用时检查文件系统有效性。 @@ -82,7 +82,7 @@ Session 投影拥有当前目录,并通过 Session observation 暴露它,因 - **读取 header 的消费方** — 沙箱写边界、工作区文件服务、变更记录器和客户端文件视图仍然读取 Session header,因此 Host 移动之后它们仍停在原始项目。让它们改读 `get` 是[工作区根记录](../../../.agents/notes/implemented/architecture/2026-11-03-session-workspace-root.zh.md#deferred)列出的后续工作。 -- **项目之外的已存储目录** — 已存储日志曾离开原始项目的 Session 会在原始项目中恢复,包括在仓库子目录中打开、随后进入仓库根下工作树的 Session。`enter_worktree` 或其他 Host 移动可以让它回到工作树。 +- **已存储的目录变更** — 早期版本存储的 Session 在原始项目中恢复,即使它进入过工作树或模型切换过目录。`enter_worktree` 或其他 Host 移动可以让它回到工作树。 - **原始项目缺失** — 当前目录与原始项目均不可用时,提示词组装会拒绝每个模型轮次,包括纯聊天轮次。服务不会另选或重建仓库。请先恢复原始目录,或通过 SDK 的 `setWorkingDirectory`(Python 中为 `set_working_directory`)或 `ctx.workingDirectory.set` 选择现有的绝对目录,再重试。已有 shell 保留各自进程的目录。 diff --git a/packages/session/working-directory/src/index.ts b/packages/session/working-directory/src/index.ts index c8604b1edc..4ba1f4a391 100644 --- a/packages/session/working-directory/src/index.ts +++ b/packages/session/working-directory/src/index.ts @@ -3,7 +3,7 @@ * @module @deepseek-ai/dsh-working-directory */ -import { isAbsolute, relative, sep } from 'node:path' +import { isAbsolute } from 'node:path' import { Context, Service } from '@deepseek-ai/cordis' import z from '@deepseek-ai/schemastery' import { z as zod } from 'zod' @@ -53,9 +53,10 @@ declare module '@deepseek-ai/dsh-llm' { declare module '@deepseek-ai/dsh-session/types' { interface SessionEventMap { /** - * Directory change recorded by releases whose model could move the directory. A reader keeps `cwd` only - * when it lies inside the Session's original project; otherwise the Session returns to that project. - * @deprecated Read only: no writer records this event. `WorkingDirectoryService.set` records + * Directory change recorded by releases whose model could move the directory. Readers ignore it: a + * Session that holds one resumes in its original project, or in the directory of its last + * `workspace-root/change`. + * @deprecated No writer records this event. `WorkingDirectoryService.set` records * `workspace-root/change`. */ 'working-directory/change': { @@ -76,34 +77,13 @@ export interface Config { defaultDirectory?: string } -/** The `workingDirectory` fold state. */ -interface DirectoryState { - /** Original project, or the first recorded directory of a Session without one; null before either. */ - readonly project: string | null - /** Directory the Session works in, or null when none is recorded. */ - readonly cwd: string | null -} - -/** - * Whether `path` is `root` or lies below it. Both are absolute paths of one execution world; the test is - * lexical, so a recorded directory is compared with the spelling the Session header recorded. - */ -function within(root: string, path: string): boolean { - const rest = relative(root, path) - return rest === '' || (rest !== '..' && !rest.startsWith(`..${sep}`) && !isAbsolute(rest)) -} - -function moved(state: DirectoryState, cwd: string): DirectoryState { - return state.cwd === cwd ? state : { project: state.project ?? cwd, cwd } -} - /** * One owner for each Session's directory and its model-visible changes. * * A Session works in one directory. Relative paths resolve from it, and it is the workspace root that * the write boundary, file views, and discovery follow. Only Host operations move it, through {@link set} - * or the recovery of {@link ensure}. A log written while the model could move the directory is read under - * the rule the `working-directory/change` event documents. + * or the recovery of {@link ensure}. The `working-directory/change` events of a log written while the + * model could move the directory do not move it. */ export class WorkingDirectoryService extends Service { static inject = ['fs', 'sessionProjections', 'systemPrompt'] @@ -118,7 +98,7 @@ export class WorkingDirectoryService extends Service { private readonly operations = new Set>() private readonly lifecycle = new AbortController() - private projection!: ProjectionHandle + private workingDirectoryProjectionHandle!: ProjectionHandle constructor(ctx: Context, config: Config) { super(ctx, 'workingDirectory') @@ -130,18 +110,13 @@ export class WorkingDirectoryService extends Service { protected async [Service.init](): Promise { const ctx = this.ctx const directorySchema = zod.string().min(1).refine(isAbsolute).nullable() - this.projection = await ctx.sessionProjections.register({ + this.workingDirectoryProjectionHandle = await ctx.sessionProjections.register({ key: 'workingDirectory', - stateVersion: 2, - stateSchema: zod.object({ project: directorySchema, cwd: directorySchema }).strict(), - init: header => ({ project: header.cwd ?? null, cwd: header.cwd ?? null }), - apply: (state, event) => { - if (event.type === 'workspace-root/change') return moved(state, event.data.cwd) - if (event.type !== 'working-directory/change') return state - // The model chose some of these directories; one outside the project must not become the workspace. - return moved(state, state.project === null || within(state.project, event.data.cwd) ? event.data.cwd : state.project) - }, - wire: { viewSchema: directorySchema, view: state => state.cwd }, + stateVersion: 3, + stateSchema: directorySchema, + init: header => header.cwd ?? null, + apply: (state, event) => event.type === 'workspace-root/change' ? event.data.cwd : state, + wire: { viewSchema: directorySchema, view: state => state }, }) ctx.systemPrompt.context({ name: 'working-directory:current', @@ -178,7 +153,7 @@ export class WorkingDirectoryService extends Service { * default when none is recorded. */ get(session: Session): string { - return this.projection.stateOf(session).cwd ?? this.defaultDirectory + return this.workingDirectoryProjectionHandle.stateOf(session) ?? this.defaultDirectory } /** @@ -198,7 +173,7 @@ export class WorkingDirectoryService extends Service { control.throwIfAborted() agent.ctx.fiber.assertActive() if (info?.type === 'directory') { - if (this.projection.stateOf(agent.session).cwd === null) { + if (this.workingDirectoryProjectionHandle.stateOf(agent.session) === null) { agent.session.append('workspace-root/change', { cwd: current }) } return current diff --git a/packages/session/working-directory/tests/stored-changes.spec.ts b/packages/session/working-directory/tests/stored-changes.spec.ts index 783a400a06..66d2bef608 100644 --- a/packages/session/working-directory/tests/stored-changes.spec.ts +++ b/packages/session/working-directory/tests/stored-changes.spec.ts @@ -50,74 +50,54 @@ async function fixture() { } describe('stored working-directory/change events', () => { - it('keeps a stored directory inside the original project, so relative paths resolve where they did', async () => { + it('ignores a stored directory change, inside or outside the original project', async () => { await using f = await fixture() - const session = f.stored() - session.append('working-directory/change', { cwd: f.worktree }) - expect(f.directories(session)).toEqual([f.worktree, f.worktree, f.worktree]) - session.append('working-directory/change', { cwd: f.inside }) - expect(f.directories(session)).toEqual([f.inside, f.inside, f.inside]) - session.append('working-directory/change', { cwd: f.project }) - expect(f.directories(session)).toEqual([f.project, f.project, f.project]) - }) - - it('returns the Session to its original project for a stored directory outside it', async () => { - await using f = await fixture() - const agent = await f.harness.create(brandString('left-project'), {}, { cwd: f.project }) - agent.session.append('working-directory/change', { cwd: f.inside }) - for (const left of [f.outside, dirname(f.project), `${f.project}-sibling`]) { - agent.session.append('working-directory/change', { cwd: left }) + const agent = await f.harness.create(brandString('stored-changes'), {}, { cwd: f.project }) + for (const stored of [f.worktree, f.inside, f.outside, dirname(f.project)]) { + agent.session.append('working-directory/change', { cwd: stored }) expect(f.directories(agent.session)).toEqual([f.project, f.project, f.project]) } // The next request tells the model, whose history still names the stored directory. expect(renderContextSnapshot(await f.ctx.systemPrompt.assemble({ agent, scope: agent }))) .toContain(`Current working directory: ${JSON.stringify(f.project)}.`) - agent.session.append('working-directory/change', { cwd: f.inside }) - expect(f.directories(agent.session)).toEqual([f.inside, f.inside, f.inside]) }) - it('lets a Host move leave the project, and reads stored changes against the project afterwards', async () => { + it('follows Host moves in a log that also holds stored changes', async () => { await using f = await fixture() const agent = await f.harness.create(brandString('host-moved'), {}, { cwd: f.project }) agent.session.append('working-directory/change', { cwd: f.inside }) - // The stored directory is already the Session's directory: the Host move records nothing. + // The stored change moved nothing, so a Host move to the same directory is a change of its own. await expect(f.ctx.workingDirectory.set(agent, f.inside)).resolves.toBe(f.inside) - expect(agent.session.snapshotEvents().some(event => event.type === 'workspace-root/change')).toBe(false) - expect(f.harness.claim(agent, 'next-step', 1)).toEqual([]) - + expect(f.harness.claim(agent, 'next-step', 1)).toHaveLength(1) await expect(f.ctx.workingDirectory.set(agent, f.outside)).resolves.toBe(f.outside) + expect(agent.session.snapshotEvents().filter(event => event.type === 'workspace-root/change').map(event => event.data)) + .toEqual([{ cwd: f.inside }, { cwd: f.outside }]) + agent.session.append('working-directory/change', { cwd: f.project }) expect(f.directories(agent.session)).toEqual([f.outside, f.outside, f.outside]) - expect(agent.session.snapshotEvents().filter(event => event.type === 'workspace-root/change').map(event => event.data)).toEqual([{ cwd: f.outside }]) - agent.session.append('working-directory/change', { cwd: f.outside }) - expect(f.directories(agent.session)).toEqual([f.project, f.project, f.project]) }) - it('bounds a Session without origin metadata by its first recorded directory', async () => { + it('leaves a Session without origin metadata on the deployment default until a Host operation records a directory', async () => { await using f = await fixture() const stored = f.stored({}) - expect(f.ctx.sessionProjections.snapshot(stored).values.workingDirectory).toBeNull() - stored.append('working-directory/change', { cwd: f.project }) stored.append('working-directory/change', { cwd: f.inside }) - expect(f.directories(stored)).toEqual([f.inside, f.inside, f.inside]) - stored.append('working-directory/change', { cwd: f.outside }) - expect(f.directories(stored)).toEqual([f.project, f.project, f.project]) + expect(f.ctx.sessionProjections.snapshot(stored).values.workingDirectory).toBeNull() + expect(f.ctx.workingDirectory.get(stored)).toBe(f.ctx.workingDirectory.defaultDirectory) const agent = await f.harness.create(brandString('without-origin')) + agent.session.append('working-directory/change', { cwd: f.inside }) const fallback = await f.ctx.workingDirectory.ensure(agent) - agent.session.append('working-directory/change', { cwd: f.outside }) + expect(fallback).toBe(f.ctx.workingDirectory.defaultDirectory) + expect(agent.session.snapshotEvents().filter(event => event.type === 'workspace-root/change').map(event => event.data)) + .toEqual([{ cwd: fallback }]) expect(f.directories(agent.session)).toEqual([fallback, fallback, fallback]) }) - it('compares a stored directory with the header as recorded, and records the canonical project on return', async () => { + it('records the canonical project when a Host move or a recovery returns to a symlinked header directory', async () => { await using f = await fixture() const link = join(dirname(f.project), 'link') await symlink(f.project, link) const agent = await f.harness.create(brandString('linked-origin'), {}, { cwd: link }) - // The canonical spelling of a directory inside the project is outside the header's symlinked spelling. - agent.session.append('working-directory/change', { cwd: f.inside }) expect(f.ctx.workingDirectory.get(agent.session)).toBe(link) - agent.session.append('working-directory/change', { cwd: join(link, 'packages', 'foo') }) - expect(f.ctx.workingDirectory.get(agent.session)).toBe(join(link, 'packages', 'foo')) await f.ctx.workingDirectory.set(agent, f.outside) await expect(f.ctx.workingDirectory.set(agent, link)).resolves.toBe(f.project)