Repository navigation
feat(agent-core-v2): add Git worktree service, session presence, and SDK APIs - #5292
Conversation
🦋 Changeset detectedLatest commit: dacf7c5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 10 packages
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 |
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: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b66787f6ef
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4ebab1dc2d
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6dc9591717
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 34178354d1
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b74588e818
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 42747e46ba
ℹ️ 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".
42747e4 to
3b0f3a2
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3b0f3a2f4f
ℹ️ 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".
d76a447 to
d8782d8
Compare
d8782d8 to
6d5f1a2
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6d5f1a2208
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2fd5147285
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 42bbafbf6a
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4b5d7ec324
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c8439046d6
ℹ️ 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".
c843904 to
8da0b9f
Compare
…d branch refs, preserve git_failed in discovery, and keep listSessions page-free
794d5ae to
dacf7c5
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: dacf7c5184
ℹ️ 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".
MoonshotAI#5204 needs the layered project port, MoonshotAI#5206 is the Workspace -> Project rename, and MoonshotAI#5292 imports the projectSessions paths both create. Keep them in dependency order rather than applying 189/320-file patches wholesale.
…equivalent Register the deliberate naming deviation in PLAYBOOK and close the rename row as equivalent. MoonshotAI#5204/MoonshotAI#5292 are adapted to workspace semantics instead of importing upstream's project paths.
MoonshotAI#5292) Port the engine half: worktree discovery, creation, listing and removal, session presence records, projectRoot session-index revivals, lock liveness/testing hooks, and the environment/session lifecycle wiring. The node-sdk client and the pi-tui history patch remain for follow-ups. Verified: agent-core-v2 typecheck clean; worktree, sessionLock, sessionLifecycle, environmentBinding and sessionExport suites 142 pass / 16 skip; check:no-comments OK.
…shotAI#5292) A cwd-based instance ref now goes through resolveImplicit instead of createOrTouch, so in-process sessions join the grouped repository project and their projectWorkDir is the repository root. The manager fake follows.
…licit (MoonshotAI#5292) When a cwd-based ref names a workspace id that has no record yet, fall back to resolveImplicit(root) instead of createOrTouch, so the instance joins the grouped repository project. The instance-manager fake follows.
…rimary differs (MoonshotAI#5292) The session context cwd now comes from the requested workDir while the project workdir (dirs/MCP) still resolves through the project primary, so a worktree session keeps its own working directory. Session lifecycle, workspace-context and project-dirs suites 45 pass.
Complete the SDK side of MoonshotAI#5292. The harness and RPC client expose resolveWorkLocation, listWorktrees, createWorktree, runWorktreeSetupScript, inspectWorktreeRemoval and removeWorktree; the SDK carries the engine's worktree error codes and restates its Error2 payloads as KimiError. Session listing accepts scope "project" and reports projectWorkDir, and the engine writes environmentCwd when the requested cwd differs from the project primary, so an index-mirror line no longer collapses the session cwd to the project root. The sync-throw overrides keep their non-async shape for require-await but route through asPromise, so a missing live session still rejects instead of throwing synchronously; cancelPromptPrediction also returns its boolean as a promise. Verified: node-sdk typecheck clean; sdk-rpc-client-v2 100 pass / 11 skip / 0 fail; list-sessions 15 pass; agent-core-v2 typecheck clean; environment binding, project sessions, session export, worktree and session lifecycle suites 140 pass / 15 skip / 0 fail. Pre-existing and unrelated: six create-session-transport telemetry cases still expect the removed tool_select experimental flag.
Port the pi-tui half of upstream 8286e8f. clearHistory() drops the stored prompt history, restores an in-progress draft and resets the recall index, so a host that switches working directories can swap in the new directory's history. navigateHistory shares the extracted restoreHistoryDraft(). Verified: pi-tui typecheck clean; editor and editor-history-keybindings 235 pass / 0 fail.
Requirement or Bug
在同一个 Git 仓库中并行推进多个任务时,单工作目录会导致分支切换与未提交改动互相干扰。本 PR 作为 Git worktree 能力的底层 PR(基于已合入的 #5204 与 #5206),在引擎与 SDK 中提供完整的 Git worktree 生命周期、活跃会话占用检测、按项目列出跨 worktree 会话,以及缺失工作目录的回合提醒;CLI 与 TUI 交互层放在后续堆叠 PR 中实现。
Bug Reproduction Steps
N/A
Root Cause
N/A
Code Changes
核心思路
所有 worktree 能力完全建立在
stack/5225的项目模型之上:同一 Git 仓库的主目录、子目录和所有 linked worktree 天然归属同一个Project,单个会话仍使用单一workDir指向实际工作目录,不再引入额外的目录映射层。核心服务拆分为独立的IWorktreeService、ISessionPresenceService与IProjectSessions.listForProject,通过@moonshot-ai/kimi-code-sdk暴露给上层宿主,并由默认关闭的实验开关experimental.worktree控制。概念
WorktreeCheckoutlocateCheckout一致的git rev-parse定位;普通仓库、bare 仓库派生的 linked worktree、submodule 与分离 git 目录均可工作,bare 仓库根目录返回未处于工作树<kimiHome>/worktrees/<projectId>/<name>,默认以 detached HEAD 检出,不污染源仓库分支列表;list/inspectRemoval/remove覆盖同一仓库的全部 linked worktree(按managed区分排序,主仓库禁止删除)SessionPresencecwd不存在时,向当轮上下文注入一条客观事实提醒,不改动任何.md系统提示词结构图
flowchart LR SDK["@moonshot-ai/kimi-code-sdk\n(worktree APIs / scope: project / projectWorkDir)"] WT["IWorktreeService\n(locate / create / include / setup / status / remove)"] PRES["ISessionPresenceService\n(PID-backed occupancy)"] PSESS["IProjectSessions\n(listForWorkdir / listForProject)"] ENV["environmentBinding\n(missing-workdir turn reminder)"] TUI["@moonshot-ai/pi-tui\n(Editor.clearHistory)"] SDK --> WT SDK --> PSESS WT --> PRES WT -.共享项目归属.-> PSESS ENV -.每轮检查 cwd.-> PSESS上述模块按职责分层,上层 CLI/TUI 只通过 SDK 契约访问 worktree 生命周期和项目级会话列表,不直接拼装 git 命令或读取持久化内部文件。
关键决策
stack/5225的locateCheckout,支持普通仓库、bare 派生 linked worktree、submodule、分离 git 目录;bare 仓库根目录拒绝.worktreeinclude/ setupsystem.md阅读指南
建议按以下顺序阅读:
packages/agent-core-v2/src/app/worktree/— 看worktreeService.ts、worktreePorcelain.ts、worktreeCheckout.ts和worktreeInclude.ts,了解定位、detached HEAD 创建、.worktreeinclude复制、setup 脚本流式执行、脏状态统计与删除保护。packages/agent-core-v2/src/app/sessionPresence/sessionPresenceService.ts与packages/agent-core-v2/src/persistence/backends/node-fs/processLiveness.ts— 了解 PID 占用登记与僵尸记录过滤。packages/agent-core-v2/src/app/projectSessions/projectSessionsService.ts— 了解listForProject如何汇总同一项目下所有 worktree/子目录的会话。packages/agent-core-v2/src/agent/environmentBinding/— 了解缺失目录回合提醒的注入条件与文本。packages/node-sdk/src/worktree.ts与packages/node-sdk/src/index.ts— 了解对外公开的 SDK 契约与错误码。packages/pi-tui/src/components/editor.ts与packages/pi-tui/UPSTREAM.md— 了解新增的Editor.clearHistory()及上游分叉登记。其余文件为 DI 注册、错误码导出、参数透传、changesets 与单元测试,可略读。
Behavior Changes and Affected Users
experimental.worktree开关KIMI_CODE_EXPERIMENTAL_WORKTREE=1或[experimental] worktree = true开启cwd不存在时,每轮注入单条客观提醒The working directory `<cwd>` no longer exists.;不改动任何.md系统提示词文件agent-core-v2恢复已被删除目录会话的客户端(CLI/TUI、Desktop/Web、VS Code、ACP、SDK)listSessions与SessionSummaryprojectWorkDirscope: 'project'与可选字段projectWorkDir;未传scope时行为不变scope保持原行为KimiHarness/SDKRpcClientV2暴露 worktree 增删查与 setup 脚本接口及worktree.*错误码Editor.clearHistory()(@moonshot-ai/pi-tui)clearHistory()方法@moonshot-ai/pi-tui的终端客户端受影响模块与验证结论:
packages/node-sdk/src/index.ts仅新增导出类型、方法与错误码,无破坏性签名变更;config-manifest.toml、wire-manifest.d.ts、state-manifest.d.ts与kap-server契约未改动;历史版本写入的会话与旧客户端均保持兼容。.worktreeinclude拷贝、setup 脚本流式输出与信号中止、PID 存活检测。Linux / Windows 路径与子进程行为依赖 CI 矩阵进一步验证。验证命令与测试覆盖详情
main(c6dec96ae1)后验证:pnpm lint:通过(5436 文件无注释违规、5567 文件 oxlint 0 warnings / 0 errors、import boundaries、guards、ratchet、test-doubles、legacy-e2e、quarantine、instructions、e2e-boundary 全部通过)pnpm run check:duplication origin/main:通过(0 new clones)pnpm exec tsc -p packages/pi-tui/tsconfig.json --noEmit && pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit && pnpm exec tsc -p packages/node-sdk/tsconfig.json --noEmit && pnpm exec tsc -p apps/kimi-code/tsconfig.json --noEmit:全部通过packages/agent-core-v2:configManifest.test.ts、test/app/worktree/、test/app/projectSessions/、test/agent/environmentBinding/、test/app/sessionExport/sessionExport.test.ts、test/project/sessionLifecycle/sessionLifecycleService.test.ts共 6 文件 156/156 通过packages/node-sdk:test/list-sessions.test.ts全部通过、test/sdk-rpc-client-v2.test.ts -t "worktree"27/27 通过packages/pi-tui:pnpm --filter @moonshot-ai/pi-tui test1153 通过、0 失败、1 跳过(含clearHistory()回归测试)apps/kimi-code:test/tui/commands/experiments.test.ts8/8 通过Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.