Repository navigation
feat(kap-server): name the session project root and agent location - #5290
Conversation
🦋 Changeset detectedLatest commit: ae7c1e1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 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. |
There was a problem hiding this comment.
💡 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".
commit: |
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.
…n-cwd # Conflicts: # packages/kap-server/test/sessions.test.ts
There was a problem hiding this comment.
💡 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(), |
There was a problem hiding this comment.
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 👍 / 👎.
…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)
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
准备一个 monorepo
<home>/project,子包在<home>/project/packages/app。在 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"表示这个目录在本机。创建响应里的
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)environment_cwd)cwdenvironment_cwd(复现步骤的例子)<home>/project<home>/project/packages/app<home>/project/packages/appenvironment_cwd<home>/project<home>/project<home>/project<home>/projectdev-box上的/srv/project<home>/projectGET、列表和: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_idwd_project_…GET /api/v1/sessions/{session_id}/environment的cwd<home>/project/packages/appapp-only.txt40409找不到root-only.txt40409找不到两个会话属于同一个项目,但带
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}/environmentcwd、metadata.cwd→workspace_rootworkspace_rootlocal或配置的远程环境environment_id(不变)environment_idenvironment_id(不变)environment_cwd(不变)environment_cwd(设置过才返回)cwd→environment_cwdcwd(不改名,标 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。关键决策
kap.md只写新名字/api/v1/sessions是核心会话 API,需要向后兼容;按旧版kap.md写好的外部 agent 不能因此失效HOST_API_LEVELworkspace_root、不返回新字段,但旧字段照常工作;提升会让新版桌面端拒绝连接所有上一版的远程主机metadata.cwd,读会话仍读cwdHOST_API_LEVEL时再切environment_id是否必填workspace_root声明 capabilitymeta.capabilities里声明kap.md由每个服务端自己提供,外部 agent 读到的说明总和它连接的服务端一致具体变化
会话对象的来源分两类:创建、
:wait、:restore、:reload从活跃会话的元数据和上下文生成,共用一个函数;GET和列表从会话索引生成。workspace_root都取工作区注册表里登记的根目录;会话的workspace_id是某个已登记工作区的别名时,取那个工作区的根目录。阅读指南
packages/kap-server/src/protocol/session.ts:新字段和 deprecated 标记,对外契约的全貌。packages/kap-server/src/routes/sessions.ts:创建请求怎么从三种写法里取出项目根目录,以及会话对象怎么拼出来。packages/kap-server/src/services/workspaceVisibility.ts:workspace_root的取值来源。packages/kap-server/docs/kap.md:外部 agent 读到的说明。可以略读:列表、profile、WS 会话帧和环境绑定接口里的字段透传,
schema/下的生成文件,以及apiSurface.test.ts的摘要更新。Behavior Changes and Affected Users
environment_cwd创建时,创建响应和会话创建帧里的cwdenvironment_cwd,与GET、列表、:wait一致environment_cwd,其他调用方随后读取拿到的也是environment_cwdcwdGET、列表一致GET、列表、:wait、:fork、:restore、:reload、/profile、WSsession帧)workspace_root、environment_id、environment_cwdenvironment_cwd只在设置过时出现;原有字段不变GET /api/v1/sessions/{session_id}/environment{ workspace_id, environment_id, cwd? }environment_cwd,与cwd同值POST /api/v1/sessions接受的项目根目录写法cwd、metadata.cwdworkspace_root;三者任意两个不一致时返回40001,只传旧字段时报错文案不变workspace_id也没传根目录时40001的detailspath: "metadata.cwd",either workspace_id or cwd is requiredpath: "workspace_root",either workspace_id or workspace_root is requiredkap.mddeprecated: true;kap.md只写新名字kap.md写的外部 agentHOST_API_LEVEL受影响模块:
packages/kap-server的会话路由、会话列表、profile、环境绑定接口、WS 会话帧和kap.md。packages/agent-core-v2只给metadata.cwd加了 deprecated 标记,并删除了失去最后一个使用方的旧SessionWireFields类型。desktop 和 web 的客户端代码不变。测试覆盖:
sessions.test.ts的两条修复用例,在 main 上失败、修复后通过。sessions.test.ts新增一条用例,让同一个会话依次经过创建、GET、列表、:wait、/profile、:fork、:restore、:reload和 WS 创建 / 更新帧,逐个断言三个新字段;environment.test.ts覆盖远程环境(不传environment_cwd时取环境的默认目录)和本地默认(不出现environment_cwd)。environment.test.ts。sessions.test.ts新增用workspace_root建会话、三组字段冲突(含原有cwd与metadata.cwd冲突的报错文案)和缺少根目录的用例。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
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.