Skip to content

feat(kap-server): name the session project root and agent location - #5290

Merged
wbxl2000 merged 7 commits into
mainfrom
fix/kap-create-session-cwd
Nov 24, 2026
Merged

wbxl2000 merged 7 commits into
mainfrom
fix/kap-create-session-cwd

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

会话接口里好几个字段都带 cwd,含义却有三种:

  • 建会话请求里的 cwd / metadata.cwd:选会话属于哪个项目。
  • 建会话请求里的 environment_cwd,以及环境绑定接口返回的 cwd:Agent 在哪个目录工作。
  • 返回的会话对象里的 cwd:客户端用它把聊天里的相对路径拼成完整路径。

名字相同已经造成了一个 bug:用 environment_cwd 让 Agent 在项目的某个子目录里工作时,POST /api/v1/sessions 的响应和推给所有已连接客户端的会话创建帧把 cwd 报成项目根目录,而 GET、会话列表和 :wait 报的是 Agent 实际工作的子目录。在重新拉取会话列表之前,点 Agent 回复里的 src/index.ts 会去打开项目根目录下的 src/index.ts。

本 PR 修复这个 bug,并给这几个概念各起一个名字:建会话用 workspace_root 选项目;每个会话视图都返回 workspace_root(属于哪个项目),以及 environment_id 和 environment_cwd(Agent 在哪里工作)。旧字段照常可用,在 OpenAPI / AsyncAPI 里标记为 deprecated,kap.md 只写新名字。

Bug Reproduction Steps

  1. 准备一个 monorepo <home>/project,子包在 <home>/project/packages/app。

  2. 在 main 上调用 POST /api/v1/sessions,创建一个归在 project 项目下、让 Agent 只在 packages/app 里工作的会话:

    {
      "title": "demo",
      "metadata": { "cwd": "<home>/project" },
      "environment_id": "local",
      "environment_cwd": "<home>/project/packages/app"
    }
    • metadata.cwd 指定会话属于哪个项目:侧边栏把会话挂在这个项目下,会话记录也存在它名下。本 PR 之后它的正式名字是 workspace_root。
    • environment_cwd 指定 Agent 在哪个目录工作:Agent 读写文件、执行命令都从这里出发,读 src/index.ts 读到的是 packages/app/src/index.ts。environment_id: "local" 表示这个目录在本机。
  3. 创建响应里的 cwd 是 <home>/project,紧接着 GET /api/v1/sessions/{session_id} 返回的 cwd 是 <home>/project/packages/app。

远程会话只会差在写法上:项目已用 <root> 登记后,在远程环境上以 "metadata": { "cwd": "<root>/" } 创建会话,创建响应返回 <root>,GET 返回 <root>/。

Root Cause

会话对象里的 cwd 总是本机路径:Agent 在本机工作时就是 Agent 的目录;远程会话的 Agent 在其他机器上工作,cwd 取本机的项目目录。客户端用它拼聊天里相对路径的完整路径、显示目录名。

会话 归属的项目(metadata.cwd) Agent 工作的目录(environment_cwd) 会话对象的 cwd
本地会话,传 environment_cwd(复现步骤的例子) <home>/project <home>/project/packages/app <home>/project/packages/app
本地会话,不传 environment_cwd <home>/project 默认项目根目录 <home>/project <home>/project
远程会话 本机的 <home>/project dev-box 上的 /srv/project <home>/project

GET、列表和 :wait 都按最后一列返回:会话创建时算出这个目录,同时记进会话上下文和会话索引,GET 和列表读索引,:wait 读上下文。只有创建路由例外,它一律用工作区注册表里登记的项目根目录当 cwd。后两行的正确值恰好就是项目根目录,所以问题只在第一行暴露;远程会话请求里的写法和登记的根目录不同时,也会差在写法上,见复现步骤最后一段。

这是根本修复:创建路由改为和 :wait 一样,从会话上下文取 cwd。会话对象上原本看不出 Agent 在哪里工作,只能另调 GET /api/v1/sessions/{session_id}/environment;本 PR 把这两个信息也放进会话对象,三个概念从此各有名字。

实测:复现步骤里的会话确实在 packages/app 工作

同一个项目下建两个会话,在项目根目录放 root-only.txt,在 packages/app 放 app-only.txt,再用相对路径读。

带 environment_cwd 的会话 普通会话
workspace_id wd_project_… 同一个
GET /api/v1/sessions/{session_id}/environment 的 cwd <home>/project/packages/app 无,默认项目根目录
相对路径读 app-only.txt 读到 40409 找不到
相对路径读 root-only.txt 40409 找不到 读到

两个会话属于同一个项目,但带 environment_cwd 的会话从 packages/app 解析相对路径,Bash 工具执行命令前也会先进入这个目录。

Code Changes

核心思路:先修 bug,让创建路由和 :wait 一样从会话上下文取 cwd。再做只增不减的改名:新字段成为正式名字;旧字段继续接受、继续返回,只在 OpenAPI / AsyncAPI 里标 deprecated: true,并从 kap.md 撤下。已有调用方,包括上一版的桌面端和远程主机,都不需要改动。

改名对照:一行一种含义,每格是这个位置上改名前后的字段名。

含义 POST /api/v1/sessions 请求 会话对象(所有视图) GET /api/v1/sessions/{session_id}/environment
会话属于哪个项目:本机上登记的项目根目录 cwd、metadata.cwd → workspace_root 新增 workspace_root 不涉及
Agent 在哪台机器上工作:local 或配置的远程环境 environment_id(不变) 新增 environment_id environment_id(不变)
Agent 在那台机器的哪个目录工作 environment_cwd(不变) 新增 environment_cwd(设置过才返回) cwd → environment_cwd
客户端拼聊天里相对路径用的本机目录 不涉及 cwd(不改名,标 deprecated) 不涉及

会话对象的 cwd 没有一对一的新名字,因为它一个名字混了两种含义:本地会话里是 Agent 工作的目录,远程会话里是本机的项目根目录。所以这个 PR 不改它的取值,只标 deprecated;需要这个目录的客户端可以从新字段推出:本地会话取 environment_cwd,没有时取 workspace_root;远程会话取 workspace_root。/profile 请求里的 metadata.cwd 本来就不生效,同样只标 deprecated。

复现步骤里的会话在本 PR 之后的样子(字段顺序按实际响应):

{
  "id": "session_…",
  "workspace_id": "wd_project_…",
  "workspace_root": "<home>/project",
  "cwd": "<home>/project/packages/app",
  "environment_id": "local",
  "environment_cwd": "<home>/project/packages/app",
  "title": "demo"
}

远程会话则是 workspace_root 为本机的 <home>/project,environment_id 为 dev-box,environment_cwd 为 /srv/project,cwd 仍是本机的 <home>/project。

关键决策

决策 选择 没选 理由
旧字段怎么处理 继续接受和返回,标 deprecated,kap.md 只写新名字 直接改名 /api/v1/sessions 是核心会话 API,需要向后兼容;按旧版 kap.md 写好的外部 agent 不能因此失效
是否提升 HOST_API_LEVEL 不提升,仍为 1,只更新契约摘要 提升到 2 改动只增不减,上一版远程主机虽然不认 workspace_root、不返回新字段,但旧字段照常工作;提升会让新版桌面端拒绝连接所有上一版的远程主机
仓内客户端是否换成新名字 暂不换:建会话仍发 metadata.cwd,读会话仍读 cwd 现在就换 桌面端会把请求转发给远程主机,远程主机可能还是上一版;等以后因别的原因提升 HOST_API_LEVEL 时再切
会话对象的 environment_id 是否必填 schema 里可选,本服务端总会返回 必填 远程主机原样转发的上一版会话不带这个字段,必填会让文档和实际不符
是否为 workspace_root 声明 capability 不声明 在 meta.capabilities 里声明 旧名字在所有版本上都可用;kap.md 由每个服务端自己提供,外部 agent 读到的说明总和它连接的服务端一致

具体变化

 POST /api/v1/sessions request
   workspace_id?
+  workspace_root?        selects the project
   cwd? / metadata.cwd?   deprecated aliases of workspace_root; any two that disagree -> 40001
   environment_id? / environment_cwd?

 Session (create, GET, list, :wait, :fork, :restore, :reload, /profile, WS session frame)
   workspace_id
+  workspace_root?        registered root of the session's workspace
   cwd                    deprecated; local directory for relative paths
+  environment_id         "local" or a remote environment
+  environment_cwd?       directory the agent works in, when set

 GET /api/v1/sessions/{session_id}/environment
   workspace_id, environment_id
+  environment_cwd?
   cwd?                   deprecated; same value
 POST /api/v1/sessions: response data and the pushed session-created frame
-  cwd = workspace registry root
+  cwd = session context cwd, same as GET, list and :wait

会话对象的来源分两类:创建、:wait、:restore、:reload 从活跃会话的元数据和上下文生成,共用一个函数;GET 和列表从会话索引生成。workspace_root 都取工作区注册表里登记的根目录;会话的 workspace_id 是某个已登记工作区的别名时,取那个工作区的根目录。

阅读指南

  1. packages/kap-server/src/protocol/session.ts:新字段和 deprecated 标记,对外契约的全貌。
  2. packages/kap-server/src/routes/sessions.ts:创建请求怎么从三种写法里取出项目根目录,以及会话对象怎么拼出来。
  3. packages/kap-server/src/services/workspaceVisibility.ts:workspace_root 的取值来源。
  4. packages/kap-server/docs/kap.md:外部 agent 读到的说明。

可以略读:列表、profile、WS 会话帧和环境绑定接口里的字段透传,schema/ 下的生成文件,以及 apiSurface.test.ts 的摘要更新。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
本地会话带 environment_cwd 创建时,创建响应和会话创建帧里的 cwd 项目根目录 environment_cwd,与 GET、列表、:wait 一致 无:仓内 desktop 和 web 都不发送 environment_cwd,其他调用方随后读取拿到的也是 environment_cwd 无需
远程会话请求的根目录和已登记的写法不同(末尾斜杠、Windows 路径的大小写或分隔符)时,创建响应和会话创建帧里的 cwd 登记的写法 请求里的写法,与 GET、列表一致 无:随后读取拿到的就是请求里的写法 无需
所有会话视图(创建、GET、列表、:wait、:fork、:restore、:reload、/profile、WS session 帧) 没有 workspace_root、environment_id、environment_cwd 新增这三个字段,environment_cwd 只在设置过时出现;原有字段不变 拒绝未知字段的严格校验方:仓内没有,desktop / web 的映射只读已知字段;上一版桌面端经远程主机拿到新版会话时忽略多出的字段 无需
GET /api/v1/sessions/{session_id}/environment { workspace_id, environment_id, cwd? } 新增 environment_cwd,与 cwd 同值 无 无需
POST /api/v1/sessions 接受的项目根目录写法 cwd、metadata.cwd 再加 workspace_root;三者任意两个不一致时返回 40001,只传旧字段时报错文案不变 无:旧写法行为不变 无需
既没传 workspace_id 也没传根目录时 40001 的 details path: "metadata.cwd",either workspace_id or cwd is required path: "workspace_root",either workspace_id or workspace_root is required 按报错文案匹配的外部脚本;仓内没有 错误码不变
OpenAPI / AsyncAPI 与 kap.md 只有旧字段 旧字段标 deprecated: true;kap.md 只写新名字 按旧版 kap.md 写的外部 agent 旧字段继续可用
远程主机协议版本 HOST_API_LEVEL 1 不变,只更新契约摘要 新版桌面端连上一版远程主机:客户端仍用旧字段,照常工作;上一版桌面端连新版远程主机:多出的字段被忽略 —

受影响模块:packages/kap-server 的会话路由、会话列表、profile、环境绑定接口、WS 会话帧和 kap.md。packages/agent-core-v2 只给 metadata.cwd 加了 deprecated 标记,并删除了失去最后一个使用方的旧 SessionWireFields 类型。desktop 和 web 的客户端代码不变。

测试覆盖:

  • 第 1、2 行:sessions.test.ts 的两条修复用例,在 main 上失败、修复后通过。
  • 第 3 行:sessions.test.ts 新增一条用例,让同一个会话依次经过创建、GET、列表、:wait、/profile、:fork、:restore、:reload 和 WS 创建 / 更新帧,逐个断言三个新字段;environment.test.ts 覆盖远程环境(不传 environment_cwd 时取环境的默认目录)和本地默认(不出现 environment_cwd)。
  • 第 4 行:environment.test.ts。
  • 第 5、6 行:sessions.test.ts 新增用 workspace_root 建会话、三组字段冲突(含原有 cwd 与 metadata.cwd 冲突的报错文案)和缺少根目录的用例。
  • 第 7、8 行:apiSurface.test.ts 校验契约文件和摘要;和 main 相比,契约差异只有新增的可选字段。
本地检查

合并最新 main 后:packages/kap-server 全量 71 个测试文件(1486 通过、1 跳过)、packages/app-core 全量测试、kap-server / agent-core-v2 / desktop 的类型检查、pnpm lint、pnpm run check:duplication origin/main 均通过。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ae7c1e1

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

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/kap-server Minor
@moonshot-ai/agent-core-v2 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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 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-24T04:57:17.182458Z ae7c1e1 New commits
ℹ️ 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.

@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: fc23f051f0

ℹ️ 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".

@pkg-pr-new

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

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

commit: ae7c1e1

Sessions now report workspace_root (the project directory) plus
environment_id and environment_cwd (where the agent works) on every
view: create, GET, list, :wait, :fork, :restore, :reload, /profile and
the WebSocket session frame. The environment binding also reports
environment_cwd. POST /api/v1/sessions accepts workspace_root; the
request cwd and metadata.cwd, the session cwd and the binding cwd keep
working and are marked deprecated in the OpenAPI and AsyncAPI schemas.
kap.md documents only the new names.

A create request with neither workspace_id nor a root now reports path
workspace_root in its 40001 details.

HOST_API_LEVEL stays at 1: the contract change is additive, and the
desktop client keeps sending and reading the old names so it still
works against remote hosts running the previous release. updateSession
drops its cwd parameter, which no caller passed and the server never
applied.
…n-cwd

# Conflicts:
#	packages/kap-server/schema/contract.txt
#	packages/kap-server/test/apiSurface.test.ts
Create, :wait, :restore and :reload now share one helper that reads a
live session's metadata and context into its wire view, and
SessionWireFields derives from the session summary instead of
repeating its fields. The legacy SessionWireFields type in
agent-core-v2 lost its last consumer and is removed.

The desktop client stays untouched: updateSession keeps its unused cwd
parameter until the client moves to the new field names.
@wbxl2000 wbxl2000 changed the title fix(kap-server): report the session cwd in the create response feat(kap-server): name the session project root and agent location Nov 23, 2026

@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: ae7c1e1bb6

ℹ️ 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".

id: z.string().min(1),
workspace_id: workspaceIdSchema,
cwd: z.string(),
workspace_root: z.string().optional(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the project root in first-party session models

When another KAP client creates a local session whose environment_cwd is a project subdirectory while Desktop/Web is connected, the first-party adapter discards this new distinction: WireSession and toAppSession retain only the deprecated cwd, while apps/desktop/src/renderer/App.vue:786 passes that value as BrowserUiState.workspaceRoot. The corrected created frame now makes that value the subdirectory, so browser upload policy treats files elsewhere in the same project as outside the workspace and can prompt or block unnecessarily. Map workspace_root separately with a cwd fallback for older hosts, and use it wherever the project root is required.

AGENTS.md reference: AGENTS.md:L204-L206

Useful? React with 👍 / 👎.

@wbxl2000
wbxl2000 merged commit 13b09d9 into main Nov 24, 2026
26 of 28 checks passed
@wbxl2000
wbxl2000 deleted the fix/kap-create-session-cwd branch November 24, 2026 05:17
@github-actions github-actions Bot mentioned this pull request Nov 24, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
…oonshotAI#5290)

Port of upstream 13b09d9.

Sessions now report workspace_root (the project directory) plus
environment_id and environment_cwd (where the agent works) on create,
GET, list, :wait, :fork, :restore, :reload and /profile. The environment
binding also reports environment_cwd. POST /api/v1/sessions accepts
workspace_root; metadata.cwd, the session cwd and the binding cwd keep
working and are marked deprecated in the OpenAPI and AsyncAPI schemas.
kap.md documents only the new names. A create request with neither
workspace_id nor a root now reports path workspace_root in its 40001
details.

Create, :wait, :restore and :reload share toLiveWireSession, and
SessionWireFields derives from SessionSummary instead of repeating its
fields. The create response now reports the session's own cwd rather
than the registered workspace root.

Fork adaptations:
- The create request never accepted a top-level cwd here, so the
  workspace-root conflict check compares metadata.cwd against
  workspace_root only.
- sessionProfile imports SessionWireFields from ./sessions (the barrel
  type was removed in agent-core-v2) and keeps the fork's direct
  session response shape.
- The WS broadcaster forwards event.session.created instead of
  rebuilding a session frame; the cross-view test asserts that frame.
- The cross-view test uses this fork's kapTest(kapFixtures(...),
  timeout)('name', fn) signature.
- Regenerated schema goldens pin digest db2a9783; the refresh also
  corrected stale agents/{tail} -> agents/{agent_id}:close route drift
  that predated this port.

Verification:
- bun test --isolate test/sessions.test.ts: 133 pass, 0 fail
- bun test --isolate test/environment.test.ts: 3 pass, 0 fail
- bun test --isolate test/apiSurface.test.ts test/sessionWireFields.test.ts
  test/sessionEventBroadcaster.test.ts: 94 pass, 0 fail
- bun run typecheck in packages/kap-server and packages/agent-core-v2: clean
- bun run check:no-comments: OK (2415 files)
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.

2 participants