Skip to content

fix: stabilize workspace primary changes - #4689

Merged
liruifengv merged 11 commits into
mainfrom
fix/workspace-primary-lifecycle
Nov 9, 2026
Merged

liruifengv merged 11 commits into
mainfrom
fix/workspace-primary-lifecycle

Conversation

@liruifengv

@liruifengv liruifengv commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

已经使用多目录的项目,需要能够更换主目录,而不破坏正在运行的会话、已注册的工具和项目信任状态。本 PR 让默认本地会话在换主后使用新目录,同时保留显式环境目录、远程绑定和历史会话数据。

Bug Reproduction Steps

  1. 创建主目录为 A、附加目录为 B 的项目,并在 A 中打开会话。
  2. 将 B 设为主目录,再加入 C,随后对现有会话执行 reload 或恢复操作。
  3. 修复前,会话可能仍以 A 为工作目录,依赖 fs/process 能力的工具可能消失,目录增删提醒也可能相互矛盾。
  4. 换主后重启应用,即使同一个工作区已经被信任,也可能再次出现信任提示。

Root Cause

本次是根本性的生命周期修复,不是兼容层 workaround。

  • 主目录被错误地纳入 Workspace/runtime 生命周期标识,换主可能关闭 Workspace,使执行环境看起来不再可用。
  • 主目录、会话 cwd、workdir 缓存键、提示词和会话摘要分别独立计算,导致实时使用、恢复、reload 和重启时可能拿到互相矛盾的旧值。
  • 执行环境暂时不可用时,已经激活的工具会被注销,而不是让具体调用返回执行错误。
  • 信任状态只按当前 root 读取,没有按稳定工作区的完整目录集合读取。

Code Changes

核心思路是:主目录只是默认工作目录和提示上下文,不再作为 Workspace、Program、Session、Agent 或工具注册的生命周期边界。默认本地会话跟随当前 primary;显式 environmentCwd 和远程绑定继续使用各自配置的目录。

工作区注册表(稳定的 workspaceId + 实时 primary + 完整目录集合)
  -> ISessionWorkspaceInfo
  -> resolveEffectiveWorkspaceRoots
       -> 默认本地会话:使用当前 primary
       -> 显式本地 cwd / 远程绑定:使用配置的 cwd
  -> 会话创建与恢复、提示词、metadata 投影和工具执行

应用级执行环境能力
  -> 调用时获取 fs/process,断线时重连或返回普通错误
  -> 不依赖 Workspace primary

内置工具注册表
  -> 激活时按 capability/profile/policy 过滤
  -> 激活后冻结注册和实例身份

工作区信任
  -> 按当前完整目录集合读取信任
  -> 只有用户显式 untrust 才撤销该集合的信任
  • 换主时复用 Workspace、Program、SessionManager controller 和 agent,同时更新实时 metadata,不销毁整个执行环境。
  • 默认本地 workdir 来自当前 primary;显式 environmentCwd、远程绑定和没有 primary 的旧数据继续使用各自的回退规则。
  • 附加目录只扩大访问范围,不作为相对路径的候选搜索目录。
  • FS、Git、AGENTS.md watcher、目录提醒和冷会话索引使用实时目录信息;不改写历史 metadata,不改变 workspace/session bucket,也不改变 REST/wire 契约。
  • 已激活的内置工具保持注册和对象身份不变。执行环境异常通过 acquire/reconnect/普通工具错误呈现,不再通过注销工具表达。
  • 中英文 server API 文档同步说明 primary 和 untrust 行为。

Behavior Changes and Affected Users

行为 修改前 修改后 依赖旧行为的对象 保留方式 / 退出路径
默认本地会话换主后的 cwd 可能停留在旧 primary,或在不同恢复路径中不一致 后续调用跟随新 primary 多目录项目中的现有本地会话 使用显式 environmentCwd 固定目录
显式本地 cwd 和远程绑定 使用配置的 cwd 保持不变 显式环境和远程执行用户 无行为变化
附加目录中的相对路径 可能受旧 cwd 或重复 root 影响 只从有效 workDir 解析;访问其他目录时使用绝对路径或显式 cwd 在附加目录中读写文件的 agent 和用户 绝对路径或显式 cwd
环境变化时的内置工具 能力相关工具可能被注销,之后重新出现 已激活注册和对象身份保持不变;执行异常仍是普通错误 所有内置工具用户,尤其是长时间运行的 Desktop 会话 初始 capability/profile/policy 过滤仍生效;调用可重连或正常失败
Workspace/Program 生命周期 换主可能关闭并重建 runtime 状态 复用稳定的 workspaceId 实例并实时更新 metadata 现有 Workspace、SessionManager 和 Agent scope 真正删除 Workspace 或销毁执行环境时仍释放资源
会话 metadata 和冷索引 cwd 重启后可能仍显示创建时的 primary 读取投影显示有效 primary;不改写历史 metadata 和 recency 会话列表、Desktop/Web 客户端和恢复查询 没有 primary 的旧数据保留原始 cwd 回退
目录提醒和提示上下文 可能提示 Session cwd remains ...,并把换主双方误报为增删目录 报告默认 cwd 的旧值 -> 新值,以及完整有效目录集合 已绑定 system prompt 的现有 agent 新绑定使用有效 primary;现有 agent 通过运行时提醒更新,不重新绑定
FS、Git 和 AGENTS.md 默认 root 可能停留在构造时的 cwd 默认路径和 watcher 跟随实时 primary;显式路径仍优先 文件树、搜索、Git status/diff 和指令加载 显式 root/path 请求保持不变
换主后的信任 新 primary 在重启后可能被视为未信任 按完整工作区目录集合读取已有信任;显式 untrust 撤销该集合的信任 已信任的多目录项目及其 MCP 配置 不迁移信任记录,也不新增隐式授权;用户可显式 untrust
REST、持久化、MCP/工具注册契约 使用现有标识和契约 保持不变 API 客户端、wire replay、MCP/tool-select 消费方 不需要兼容路径或契约迁移

测试覆盖:

  • core 覆盖 roots、生命周期、resume/reload、工具实例冻结、提示提醒、FS/Git、指令加载、信任和旧数据回退。
  • kap-server 使用真实 REST 和真实工具调用覆盖 A -> B -> C、显式 cwd、resume、reload 和 server 重启。
  • Desktop 覆盖现有会话换主、完整工具目录、/reload、应用重启,以及主目录/附加目录缺失、权限异常、后台任务和待审批。
  • Desktop/Web 共享弹窗与 store、三端 typecheck,以及中英文 API 文档构建均有验证。
详细验证记录
  • 初次 core 验证:43 个测试文件、747 个测试通过。
  • 初次 kap-server 验证:3 个测试文件、135 个测试通过。
  • 合入最新 main 后:core 7 个定向测试文件、185 个测试通过;kap-server workspace 26 个测试通过;core typecheck 通过。
  • 本轮非信任问题修复后:core 18 个定向测试文件、570 个测试通过;kap-server 3 个测试文件、56 个测试通过;core typecheck 通过。
  • Desktop/Web 共享弹窗和 workspace store:初次 70 个测试通过;合入 main 后相关 4 个测试文件、95 个测试通过。
  • Desktop/Web/rc-portal typecheck 通过。
  • Desktop stub-model 和 real-model 均验证现有会话 A -> B -> C、/reload 和应用重启;真实模型实际调用 Bash/Glob 列出 KIMI CODE LOGO 文件。
  • Desktop 异常冒烟:5/5 通过,覆盖目录失效、目录类型和权限异常、快速换主、后台任务、待审批、路径边界、trust、reload 和重启。
  • 中英文 API 文档:VitePress 构建通过。
  • 未执行真实远程 SSH 断连测试;远程绑定行为由 core 单元测试覆盖。

Checklist

  • 我已阅读 CONTRIBUTING 文档。
  • 需求和复现步骤已记录在上方;没有单独的外部 issue。
  • 我已添加证明改动可用的测试。
  • 上方的行为变化表完整;被移除的行为或默认值变化均已在 changeset 中说明,并有保留方式或明确批准。
  • 已运行 gen-changesets skill,并添加 patch changeset。
  • 已运行 gen-docs skill,更新中英文 server API 文档,并通过 VitePress 构建。

Copilot AI balanced review requested due to automatic review settings November 5, 2026 03:40
@changeset-bot

changeset-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ceb15f8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical findings allow unauthorized MCP trust and ineffective trust revocation, while primary-directory changes cause incorrect filesystem roots and inconsistent session metadata.

7 open issues
What changed in this PR

This PR aims to keep multi-directory workspaces stable when their primary directory changes, aligning session roots, runtime services, metadata, and trust without recreating live workspace state.

Changes:

  • Reuses workspace instances and centralizes effective-root resolution across session and agent services.
  • Keeps activated tools registered and updates filesystem, Git, instruction, index, and trust handling.
  • Adds regression coverage, bilingual API documentation, and a patch changeset.
File Description
packages/​kap-server/​test/​workspaces.test.ts Adds primary-swap, tool execution, resume, reload, and restart coverage.
packages/​kap-server/​src/​routes/​workspaces.ts Resolves trust across workspace directories.
packages/​kap-server/​src/​routes/​fs.ts Supplies effective roots to filesystem and Git services.
packages/​agent-core-v2/​test/​workspace/​workspaceTrust/​workspaceTrust.test.ts Tests trust preservation and explicit revocation.
packages/​agent-core-v2/​test/​workspace/​workspaceInstructions/​instructions.test.ts Tests instruction reloads and watcher relocation.
packages/​agent-core-v2/​test/​workspace/​workspaceInstance/​workspaceInstanceManager.test.ts Tests instance and Program reuse.
packages/​agent-core-v2/​test/​workspace/​workspaceFs/​fsService.test.ts Tests filesystem and Git root changes.
packages/​agent-core-v2/​test/​workspace/​workspaceDirs/​workspaceDirsService.test.ts Updates directory-set and primary-swap expectations.
packages/​agent-core-v2/​test/​session/​workspaceContext/​agentWorkspaceContext.test.ts Updates workspace context fixtures and dependencies.
packages/​agent-core-v2/​test/​session/​subagent/​subagentScopeCache.test.ts Adds session cwd and workspace-info fixtures.
packages/​agent-core-v2/​test/​session/​sessionMetadata/​sessionMetadata.test.ts Adds workspace-info metadata fixtures.
packages/​agent-core-v2/​test/​session/​agentLifecycle/​agentLifecycle.test.ts Updates lifecycle fixtures for root resolution.
packages/​agent-core-v2/​test/​features/​skill/​workspace/​skillCatalog.test.ts Adds primary to directory fixtures.
packages/​agent-core-v2/​test/​app/​sessionIndex/​sessionIndex.test.ts Tests effective-cwd projection without metadata rewrites.
packages/​agent-core-v2/​test/​agent/​toolActivation/​toolActivationService.test.ts Tests stable tool registrations and identities.
packages/​agent-core-v2/​test/​agent/​environmentBinding/​environmentBindingService.test.ts Tests default-local, explicit, and remote roots.
packages/​agent-core-v2/​test/​agent/​changeNotifier/​changeNotifier.test.ts Tests cwd and directory-change reminders.
packages/​agent-core-v2/​src/​workspace/​workspaceTrust/​workspaceTrustStore.ts Introduces grouped trust entries.
packages/​agent-core-v2/​src/​workspace/​workspaceTrust/​workspaceTrustService.ts Rebinds trust when workspace directories change.
packages/​agent-core-v2/​src/​workspace/​workspaceInstructions/​workspaceInstructionsService.ts Reloads instructions and relocates primary-root watchers.
packages/​agent-core-v2/​src/​workspace/​workspaceInstance/​workspaceInstanceManagerService.ts Refreshes metadata while retaining instances.
packages/​agent-core-v2/​src/​workspace/​workspaceInstance/​workspaceInstance.ts Exposes mutable workspace metadata.
packages/​agent-core-v2/​src/​workspace/​workspaceGit/​workspaceGitService.ts Reads the current primary for default Git operations.
packages/​agent-core-v2/​src/​workspace/​workspaceFs/​fsService.ts Resolves default filesystem roots dynamically.
packages/​agent-core-v2/​src/​workspace/​workspaceDirs/​workspaceDirsService.ts Exposes primary and revises directory aggregation.
packages/​agent-core-v2/​src/​workspace/​workspaceDirs/​workspaceDirs.ts Adds the primary-directory interface property.
packages/​agent-core-v2/​src/​workspace/​sessionLifecycle/​sessionLifecycleService.ts Resolves effective workdirs during creation and resume.
packages/​agent-core-v2/​src/​session/​workspaceContext/​workspaceRoots.ts Adds shared effective-root resolution.
packages/​agent-core-v2/​src/​session/​workspaceContext/​workspaceContextService.ts Synchronizes session root state with workspace changes.
packages/​agent-core-v2/​src/​session/​sessionMetadata/​sessionMetadataService.ts Projects effective cwd into live summaries.
packages/​agent-core-v2/​src/​program/​programWorkdirs.ts Supplies directory services to workdir consumers.
packages/​agent-core-v2/​src/​program/​program.ts Constructs trust from workspace directories.
packages/​agent-core-v2/​src/​app/​sessionIndex/​sessionIndexSource.ts Adds primary-aware summary and scan-cache data.
packages/​agent-core-v2/​src/​app/​sessionIndex/​sessionIndexService.ts Supplies workspace primaries to index readers.
packages/​agent-core-v2/​src/​app/​sessionIndex/​sessionIndexProjector.ts Uses primaries during projection and cache validation.
packages/​agent-core-v2/​src/​agent/​toolActivation/​toolActivationService.ts Removes environment-driven tool withdrawal.
packages/​agent-core-v2/​src/​agent/​profile/​profileService.ts Seeds instruction reminders with effective workdir.
packages/​agent-core-v2/​src/​agent/​environmentBinding/​environmentBindingService.ts Uses effective roots for bindings and reminders.
packages/​agent-core-v2/​src/​agent/​environmentBinding/​agentEnvironment.ts Delegates root resolution to the shared helper.
packages/​agent-core-v2/​src/​agent/​changeNotifier/​changeNotifierService.ts Reports effective cwd and complete directory sets.
docs/​zh/​reference/​server-api.md Documents primary-swap and untrust behavior in Chinese.
docs/​en/​reference/​server-api.md Documents primary-swap and untrust behavior in English.
.changeset/​tidy-workspace-primary-lifecycle.md Adds a CLI patch release note.

Review effort: Balanced

@liruifengv
liruifengv marked this pull request as ready for review November 9, 2026 02:27
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T07:09:00.015597Z ceb15f8 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@ceb15f8
npx https://pkg.pr.new/@moonshot-ai/kimi-code@ceb15f8

commit: ceb15f8

@liruifengv

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fd83b191ad

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@liruifengv

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: bde22ea880

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

cwd: environment?.environmentId === LOCAL_ENVIRONMENT_ID ? environment.cwd : opts.workDir,

P1 Badge Update hook cwd when the primary changes

When a default local session remains live across a primary swap from A to B, this context stores the work directory only at materialization and is never updated. SessionExternalHooksService and AgentExternalHooksService continue passing ISessionContext.cwd to the hook runner, so hooks without an explicit cwd still execute in A and receive A in their payload while tools operate in B; this contradicts the documented “current session local project directory” behavior and can make validation or mutation hooks act on the wrong project. Fresh evidence beyond the prior workspace-context finding is that these hook paths consume the separate immutable session context rather than IWorkspaceContext; derive their cwd from the live effective roots while preserving explicit and remote pinning.

AGENTS.md reference: AGENTS.md:L121-L123

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@liruifengv

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3858f307a3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@liruifengv

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: c99025cb77

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@liruifengv

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: ceb15f83e2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@liruifengv
liruifengv merged commit 51b47db into main Nov 9, 2026
16 checks passed
@liruifengv
liruifengv deleted the fix/workspace-primary-lifecycle branch November 9, 2026 07:14
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 11, 2026
Upstream MoonshotAI#4689 also clarifies the /workspaces/{id}/primary semantics, but that endpoint section belongs to the unported REST slice of upstream MoonshotAI#4336. The fork has no POST /workspaces/{id}/primary route, so the paragraph is not added rather than left dangling; the untrust wording matches the fork's WorkspaceTrustGroup.untrust, which revokes every root in the dir set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants