Skip to content

fix(acp-server): show a running command's output to editors without terminals - #5525

Merged
RealKai42 merged 2 commits into
mainfrom
fix/acp-live-command-output
Nov 26, 2026
Merged

RealKai42 merged 2 commits into
mainfrom
fix/acp-live-command-output

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Screenshots / Recordings

编辑器不声明终端时,kimi acp 跑 printf 'rea%s\n' dy; sleep 30,编辑器收到的这张工具卡的通知(节选,省略了 sessionId)。修复前的取自 E2E acp-bare 跑法的失败附件,修复后的取自 acp-server 新增的集成测试,所以调用 id 不同:

修复前(ready 在第一秒就打印了,编辑器要到 30 秒后命令结束才看到):

<- {"sessionUpdate":"tool_call","toolCallId":"0:call_0_0","title":"Bash","kind":"execute","status":"pending","content":[…""…]}
<- {"sessionUpdate":"tool_call_update","toolCallId":"0:call_0_0","status":"in_progress","content":[…"{\"command\":…}"…]}
<- {"sessionUpdate":"tool_call_update","toolCallId":"0:call_0_0","title":"Running: printf 'rea%s\n' dy; sleep 30","status":"in_progress",…}
(30 秒里没有别的更新)
<- {"sessionUpdate":"tool_call_update","toolCallId":"0:call_0_0","status":"completed","content":[{"type":"content","content":{"type":"text","text":"ready\n"}}],"rawOutput":"ready\n"}

修复后(命令还在跑时就收到已打印的内容;用户停掉命令后,结果里也还在):

<- {"sessionUpdate":"tool_call","toolCallId":"0:call_slow","title":"Bash","kind":"execute","status":"pending","content":[…""…]}
<- {"sessionUpdate":"tool_call_update","toolCallId":"0:call_slow","status":"in_progress","content":[…"{\"command\":…}"…]}
<- {"sessionUpdate":"tool_call_update","toolCallId":"0:call_slow","title":"Running: printf 'rea%s\n' dy; sleep 30","status":"in_progress",…}
<- {"sessionUpdate":"tool_call_update","toolCallId":"0:call_slow","content":[{"type":"content","content":{"type":"text","text":"ready\n"}}]}
<- {"sessionUpdate":"tool_call_update","toolCallId":"0:call_slow","status":"failed","content":[{"type":"content","content":{"type":"text","text":"ready\nInterrupted by user"}}],"rawOutput":"ready\nInterrupted by user"}

Requirement or Bug

编辑器在 initialize 里不声明 terminal 时,kimi acp 在本机自己跑命令。命令跑着的时候编辑器看不到任何输出,要等命令结束才一次给出。长命令(构建、测试)跑着时,用户看不到进度,也就判断不了要不要停。TUI、Web、Desktop,以及声明了终端的编辑器,都能看到运行中的输出。

这个 PR 之后,这类编辑器在命令运行中就能看到它已经打印的内容。

Bug Reproduction Steps

  1. 用一个不声明 terminal 能力的 ACP 客户端连上 kimi acp,让 agent 运行 printf 'rea%s\n' dy; sleep 30。
  2. 修复前:30 秒里,这张工具卡只收到参数和「Running: …」标题,ready 要到命令结束时才随结果一起出现。

Root Cause

输出其实已经到了 ACP 宿主,是宿主自己丢掉的:

  • 工具运行中每写出一段输出,引擎就发一次 tool.updated,带 output: { offset, text }(packages/agent-core-v2/src/agent/toolExecutor/toolExecutorService.ts 的 observe)。klient 的事件契约里也有这个字段。
  • packages/acp-server/src/session.ts 的 onToolUpdated 只读 fields.status(「Running: …」标题),output 整个被忽略了。

声明了终端时没有这个问题:命令跑在编辑器的终端里,卡片内容是终端,编辑器自己显示输出。

这是根本修复:把引擎已经发出的运行中输出转给编辑器,和 KAP(Web、Desktop)、TUI 用的是同一份数据。

Code Changes

tool.updated(带 output)
  └─ AcpSession.onToolUpdated
       ├─ 这次调用还没有结果,且没有接上编辑器的终端?否 → 不转发
       └─ 是 → LiveToolOutput.append(acp 调用 id, offset, text)
                 ├─ offset 不接着已有的输出(重复、乱序)→ 丢掉
                 ├─ 只留最后 50,000 个字符,截过就在前面加 "[…truncated]"
                 └─ 限频:空闲时立即发一条,之后 250 ms 内的新输出合并,到点再发一条
                      └─ tool_call_update { content: [累积的文字] }

tool.result / 接上编辑器终端 → LiveToolOutput.end(清掉还没发的那一条)
会话 dispose → LiveToolOutput.dispose
  • 新模块 packages/acp-server/src/live-output.ts:按调用保存累积的文字、下一个 offset 和限频定时器。单独成模块是为了能用假时钟测限频,session.ts 只负责判断哪些调用要转发、把结果发出去。
  • 为什么每次发全文:ACP 的 tool_call_update.content 是整体替换,没有追加。所以要截断、要限频,否则一条输出很多的命令会发出大量大消息。
    • 截断长度和 TUI 的 MAX_LIVE_OUTPUT_CHARS 一样是 50,000 个字符。
    • 限频间隔和声明了终端时轮询编辑器终端的 OUTPUT_POLL_MS 一样是 250 ms。
    • 第一段输出立即发出,不必等一个间隔。
  • 按字节算 offset、丢掉对不上的块:和 KAP 的 conversationFeed.ts 一样,引擎的 offset 是输出日志的字节数。
  • 两种情况不转发:
    • 已经有结果的调用:命令转到后台后,工具结果先返回,同一个调用的输出事件还会继续来。结果之后不再更新卡片,KAP、TUI 也是这样。用「这次调用还在 toolCallStreamArgs 里」判断,这是现有的、从调用开始到结果之间的状态。
    • 接上编辑器终端的调用:声明了终端时,输出也走同一个事件(产品轮询编辑器的终端,写回工具的输出),但卡片内容是终端,再发文字会把终端顶掉。接上终端时也会清掉还没发的那一条。
  • 结果到达时不补发最后一段:结果本身的 content 就是完整输出,会替换掉运行中的内容,补发只会多一条马上被覆盖的消息。

阅读顺序:live-output.ts,然后 session.ts 的 onToolUpdated、onToolResult、attachTerminal。events-map.ts 只多了一个构造通知的函数。用户文档 docs/{en,zh}/reference/kimi-acp.md 的 terminal/create 一行补了一句:不声明终端时,运行中的输出以 tool_call_update 的文字内容发出。

Behavior Changes and Affected Users

行为 改动前 改动后 谁依赖旧行为 退路
不声明终端的编辑器,命令运行中的工具卡 只有参数和「Running: …」标题,输出要等结果 收到 tool_call_update,内容是已打印的输出(最后 50,000 个字符,每条命令最多每 250 ms 一次);卡片里原来的参数文字被输出替换,参数仍在 rawInput 里 无:协议允许运行中更新内容,编辑器本来就会处理带内容的 tool_call_update 无
其他运行中写出输出的工具(如走 legacy adapter、带 stdout 更新的工具) 同上,输出要等结果 同上,运行中就能看到 无 无
Agent(子 agent)、AskUserQuestion 这类只在结束前写一次输出的工具 只有结果那一条更新 结果之前多一条带同样内容的 tool_call_update,随即被结果替换 无:显示的内容不变 无
声明了终端的编辑器 卡片显示编辑器的终端 不变:接上终端的调用不转发文字输出 — —
转到后台的命令,结果之后的输出 不发 不变:结果之后不再更新卡片 — —
工具结果、session/load 回放 — 不变 — —

使用方:Zed 等 ACP 客户端,尤其是不声明 terminal 能力的。packages/acp-server 属于契约目录:通知的形状都是协议里已有的 tool_call_update,只是运行中多发了几条,上一个版本的客户端不需要改动。

覆盖:

  • 单元测试 packages/acp-server/test/live-output.test.ts(假时钟):第一段立即发出、之后每个间隔最多一条且带全部已打印内容;各调用互不影响;offset 对不上的块丢掉,offset 按字节算(含多字节字符);超长输出只留最后 50,000 个字符并标明截断;结束或 dispose 之后不再发送,包括还没发的那一条。
  • ACP 集成测试 packages/acp-server/test/e2e-turn.test.ts 新增 2 条:
    • 不声明终端的编辑器跑一条打印后等待的命令,结果之前就能看到 ready;停掉之后,失败的结果里也还在。修复前它失败。
    • 声明终端的编辑器,卡片最后仍是终端,输出没有作为文字出现在任何一条卡片更新里。去掉「接上终端就不转发」的判断时它失败。
  • E2E:acp-bare 下原先排除的 3 条放回来了(turn/stop-running-command、expectTool 和 stop 自测里各一条运行中输出的用例),tests/e2e/AGENTS.md 里对应的说明一并更新。修复前这 3 条在 acp-bare 上都超时在「运行中、输出 ready」这一步。

没有覆盖:

  • 转到后台的命令在结果之后继续输出:只在单元层验证了结束后不再发送,没有写端到端用例,因为结果和后续输出的先后依赖时序,难以稳定复现。
  • Windows 上没有跑;这次改动不涉及路径、子进程或终端输入,命令仍由原来的本机进程执行。
本机验证(macOS)
  • 先红后绿:
    • 集成测试:修复前,新增的「运行中输出」用例 10 秒内等不到 ready;修复后通过。把 session.ts 里「接上终端就不转发」的判断临时去掉,终端用例失败;恢复后通过。
    • E2E:把 session.ts 临时换回 main 的版本并重新构建,放回的 3 条在 acp-bare 上都失败在「运行中、输出 ready」;换回修复后通过。
  • E2E 压测(构建产物):放回的 3 条在 acp、acp-bare 上各跑 10 遍,60/60 通过。这 3 个文件的全部用例在两种跑法上:72 通过,4 条是只在 TUI 上跑的用例,按设定跳过。
  • ACP 整套 E2E(构建产物,acp 和 acp-bare):406 条,320 通过,86 条按场景设定跳过,没有失败。
  • 检查:
    • packages/acp-server 的类型检查通过、全部 203 条测试通过;
    • tests/e2e 的类型检查通过;
    • pnpm lint、pnpm run check:duplication 以 0 退出。

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.

…erminals

When the editor does not offer terminals, kimi acp runs commands itself.
The engine reports their output while they run as tool.updated events
with an output chunk, but the ACP session only read the status field of
those events, so the editor saw nothing until the command ended.

Forward the output as tool_call_update text content. ACP replaces the
content on every update, so each update carries the output so far:
the latest 50,000 characters, marked as truncated once earlier output
was cut, and at most one update every 250 ms per tool call, with the
first one sent at once. Chunks whose byte offset does not continue the
output are dropped. A call shown on an editor terminal is not
forwarded, and nothing is sent for a call once it has its result,
which also covers commands moved to the background.

Bring back the three E2E cases that were left out on the bare editor
for this gap.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 22cd736

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/kimi-code Patch
@moonshot-ai/acp-server 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 26, 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-26T11:38:00.531920Z 22cd736 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.

@pkg-pr-new

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

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

commit: 22cd736

@RealKai42
RealKai42 merged commit d9f23b5 into main Nov 26, 2026
28 checks passed
@RealKai42
RealKai42 deleted the fix/acp-live-command-output branch November 26, 2026 11:58
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.

1 participant