Repository navigation
fix: stabilize workspace primary changes - #4689
Conversation
🦋 Changeset detectedLatest commit: ceb15f8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
🟡 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
Reproject indexed sessions when the primary workspace changes · New Root FS, Git, and instructions at each view’s effective cwd · New Preserve remote cwd when resolving non-local bindings · New Preserve access to associated projects’ original cwd · New Do not trust implicit projects via arbitrary additional directories · New Align trust routes with runtime scope so untrust revokes access · New Document changed default cwd behavior and the environmentCwd escape hatch · New
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
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
|
@codex review |
There was a problem hiding this comment.
💡 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".
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
There was a problem hiding this comment.
💡 Codex Review
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".
|
@codex review |
There was a problem hiding this comment.
💡 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".
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
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.


Requirement or Bug
已经使用多目录的项目,需要能够更换主目录,而不破坏正在运行的会话、已注册的工具和项目信任状态。本 PR 让默认本地会话在换主后使用新目录,同时保留显式环境目录、远程绑定和历史会话数据。
Bug Reproduction Steps
Root Cause
本次是根本性的生命周期修复,不是兼容层 workaround。
Code Changes
核心思路是:主目录只是默认工作目录和提示上下文,不再作为 Workspace、Program、Session、Agent 或工具注册的生命周期边界。默认本地会话跟随当前 primary;显式
environmentCwd和远程绑定继续使用各自配置的目录。environmentCwd、远程绑定和没有 primary 的旧数据继续使用各自的回退规则。AGENTS.mdwatcher、目录提醒和冷会话索引使用实时目录信息;不改写历史 metadata,不改变 workspace/session bucket,也不改变 REST/wire 契约。Behavior Changes and Affected Users
environmentCwd固定目录workspaceId实例并实时更新 metadataSession cwd remains ...,并把换主双方误报为增删目录AGENTS.md测试覆盖:
/reload、应用重启,以及主目录/附加目录缺失、权限异常、后台任务和待审批。详细验证记录
/reload和应用重启;真实模型实际调用 Bash/Glob 列出KIMI CODE LOGO文件。Checklist
gen-changesetsskill,并添加 patch changeset。gen-docsskill,更新中英文 server API 文档,并通过 VitePress 构建。