Skip to content

fix(kap-server): restore legacy ACP session file tools - #4600

Merged
7Sageer merged 1 commit into
mainfrom
fix-acp-session-runtime-rebind
Nov 2, 2026
Merged

7Sageer merged 1 commit into
mainfrom
fix-acp-session-runtime-rebind

Conversation

@7Sageer

@7Sageer 7Sageer commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

用户先通过 ACP 客户端(如 Zed)创建并使用会话,随后在 Desktop 中打开同一会话时,Read / Write / Bash 会因 environment acp:session_... does not exist 无法运行;直接由 Desktop 新建的会话则正常。本 PR 让这些来自 ACP 的会话在 Desktop 所用的 kap-server 恢复后重新获得本地文件与命令执行能力,并将迁移结果持久化,后续重启不再重复失败。

Bug Reproduction Steps

  1. 在 ACP 客户端(如 Zed)中创建会话。ACP 服务激活会话时将 main agent 绑定到 acp:<sessionId>,并通过 environment.set_binding 持久化到 wire。
  2. 关闭 ACP 服务,改用 Desktop 打开同一会话。Desktop 的 kap-server 恢复 wire 中的绑定,但当前进程没有注册对应的 ACP 执行环境。
  3. 让 main agent 调用 Read;工具在读取文件前解析执行环境,抛出 environment acp:session_... does not exist。

回归测试向真实 kap-server 会话写入同样的 environment.set_binding,再完整重启服务并用 IReadTool 复现恢复与读取链路;未额外驱动真实 ACP 客户端或 Desktop UI。

Root Cause

ACP 会话创建时,session metadata 使用 local,main agent 随后在 ACP 会话激活时绑定 acp:<sessionId>。Desktop 通过 kap-server 恢复该会话时可以创建本地 workdir,但 wire replay 会把 main agent 恢复到旧的 ACP ID;当前进程的环境注册表中没有这个 ID,因此所有依赖环境租约的工具都会失败。

这是针对 ACP 会话数据的跨客户端兼容迁移:恢复完成后,仅当绑定恰好是当前会话的 acp:<sessionId> 且注册表已没有该环境时,保留原 cwd 并改绑 local,再把新绑定写回 wire。通用 environment 解析仍对失效的远程环境显式报错,ACP 服务自身的恢复路径不变。

Code Changes

核心改动位于 kap-server 的 session-created 生命周期,确保任何 REST / WebSocket 恢复入口都先修正绑定,再向调用方返回会话。

 resume persisted session
   replay main-agent environment binding
+  if binding is acp:<current sessionId>
+    and the environment is absent from the registry
+    bind local with the replayed cwd
+    persist environment.set_binding
 resolve Read / Write / Bash execution

这里同时检查注册表:如果同名 ACP 环境仍然可用,恢复后继续使用它。其他 environment ID、远程环境和新会话均不进入迁移分支。

阅读顺序:

  • packages/kap-server/src/start.ts:查看生命周期接入、迁移条件以及订阅释放。
  • packages/kap-server/test/sessions.test.ts:查看旧绑定复现、真实 Read、二次重启持久化,以及同名 ACP 环境仍可解析时的 guard。
  • .changeset/fix-legacy-acp-sessions.md:用户可见修复说明。

Behavior Changes and Affected Users

行为 修改前 修改后 依赖旧行为的人群 退路
Desktop / web 通过 kap-server 恢复 ACP 创建的会话,且当前会话的 acp:<sessionId> 环境已缺失 Read / Write / Bash 在环境解析阶段失败 保留 cwd,改绑 local 并持久化,工具继续执行 从 ACP 客户端转到 desktop 或 web 继续同一会话的用户;失效的 ACP 路径已不可用 无需,恢复文件与命令执行能力
恢复同名 ACP 环境仍注册的会话 使用原 ACP 环境 保持原 ACP 绑定 仍有有效 ACP provider 的调用方 不适用,guard 测试固定旧行为
其他 environment ID、远程环境、新会话及 ACP 服务自身恢复路径 按原逻辑解析或显式报错 不变 SSH / Docker 等远程环境用户、新建会话用户、Zed 等 ACP 客户端 不适用,迁移条件不覆盖这些输入

受影响模块与覆盖:

  • kap-server 会话恢复:新增迁移与 guard 用例,覆盖完整服务重启、真实 Read 和迁移后的第二次重启。
  • desktop / web 消费方:实际 ACP 客户端与 Desktop UI 均未手工操作;验证直接构造 ACP 持久化绑定后,走 kap-server 的实际恢复与文件工具路径。
  • ACP 服务:无代码变更,未单独复跑 ACP 套件。
验证记录
  • kap-server pnpm exec vitest run --maxWorkers=4:72 个测试文件通过,1441 passed / 1 skipped。默认并发运行曾两次命中 searchRoute.test.ts 的既有间歇失败,该文件单独复跑通过,最终全量复跑通过。
  • kap-server pnpm typecheck:通过。
  • kap-server pnpm build:通过。
  • 仓库 pnpm lint:0 errors。
  • git diff --check:通过。
  • 修复前,迁移用例在 ReadTool.resolveExecution 稳定抛出 environment acp:session_... does not exist;修复后同一场景通过,并能跨第二次重启保留 local 绑定。
  • 只读 agent 已审计完整 diff,未发现真实内部域名、账号、密钥、真实用户路径或其他内部标识。

Checklist

  • I have read the CONTRIBUTING document.
  • Related issue: N/A — this PR includes a self-contained reproduction.
  • 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.

Copilot AI balanced review requested due to automatic review settings November 2, 2026 05:06
@changeset-bot

changeset-bot Bot commented Nov 2, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 338fc5d

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@7Hanrui

7Hanrui commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T07:28:18.394444Z 338fc5d 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 338fc5dab3

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

@7Sageer
7Sageer marked this pull request as ready for review November 2, 2026 07:20
@pkg-pr-new

pkg-pr-new Bot commented Nov 2, 2026

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

commit: 338fc5d

@7Hanrui

7Hanrui commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 338fc5dab3

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

@7Sageer
7Sageer merged commit 1401bdd into main Nov 2, 2026
30 of 31 checks passed
@7Sageer
7Sageer deleted the fix-acp-session-runtime-rebind branch November 2, 2026 07:48
@github-actions github-actions Bot mentioned this pull request Nov 3, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
…pe gap

MoonshotAI#4526 is already in the tree: the loadable-tools announcement refreshes
on every turn and only awaits the MCP initial load when the turn is new.

MoonshotAI#4600 does not port as written. This fork binds a session to
`{ workspaceId, runtimeId }` rather than upstream's
`{ environmentId, cwd }`, and `IEnvironmentService` is not exported from
the agent-core-v2 root, so the legacy `acp:<id>` rebind needs a field
mapping plus a public export. Recorded rather than forced, so the next
attempt starts from the shape difference instead of rediscovering it.

Ledger: closed 34 -> 35, remaining 64.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 5, 2026
The repair reads IAgentEnvironmentBindingService, which MoonshotAI#4062
introduces and re-exports; this tree still runs the old runtimeBinding
layer, so the environmentBinding files present here have no importers
and nothing reaches them from the engine root. A trial port was written
and reverted.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 5, 2026
… it blocked

The full remote-environment port landed with 4166ca3, so MoonshotAI#4062 is `ported`
with the evidence that closes it out: 29 port commits, typecheck 23/23, the
fifteen check gates, a 26/26 build whose bundle answers `--version` and an
`exec-server` initialize frame, and every suite green (agent-core-v2 8415,
kap-server 1314, node-sdk 321, klient 118, acp-server 177, apps/kimi-code 5049).

Three rows were parked behind it and now have their prerequisites:

- MoonshotAI#4600 (legacy ACP session file tools) — `IAgentEnvironmentBindingService` and
  `IEnvironmentService` are exported from the engine root, `start.ts` constructs
  `RemoteEnvironmentProviderFactory`, and the provider registration block the
  repair edits exists.
- MoonshotAI#4649 (TUI footer/welcome on the session environment) — the engine half landed
  in 2fa7b6b; the missing piece was the session workdir, which now carries the
  environment cwd.
- MoonshotAI#4543 (remote file round trips) — every remote file it changes exists; the
  batched walk methods it adds are still absent from `methods.ts`.

All three move from `blocked`/`in_progress` to `pending`; none is ported.

Ledger: closed 68 -> 69, remaining 31 -> 30.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 5, 2026
Port of upstream MoonshotAI/kimi-code 1401bdd (MoonshotAI#4600).

A desktop session written by the pre-environment client carries
`acp:<session>` as its environment id. Nothing registers that environment in
this server, so after an upgrade its main agent still resolved the tool
filesystem through it and local file tools failed on paths that exist —
`Read` on a file in the session cwd included.

`startServer` now subscribes to `ISessionManager.onDidCreateSession` and, when
the main agent's binding names `acp:<session>` and no such environment is
registered, rebinds it to `local` at the binding cwd (falling back to the
session cwd). A binding whose environment is registered is left alone, and the
subscription is disposed on close with the rest of the server teardown.

Verified: kap-server 1316 pass / 14 skip; both new cases fail with the
migration short-circuited and pass with it restored. check:no-comments,
check:deep-imports and check:boundaries green.

Ledger: MoonshotAI#4600 ported; closed 69 -> 70, remaining 30 -> 29.
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.

4 participants