Repository navigation
fix: show a failed context compaction in the conversation - #5478
Conversation
When a manual compaction failed on Desktop or Web, the open session only showed a six-second "Context compaction did not finish" note. The error notice that carries the reason was dropped, because error notices of the open session are left to the failed-turn card, and a manual compaction belongs to no turn. Toast a compaction.failed notice in the open session unless the last turn already failed with that same error, title it "Context compaction failed", and dismiss the did-not-finish note it follows. The E2E compact() driver now fails at once on that notice, so its refusal case runs on Desktop and Web too.
🦋 Changeset detectedLatest commit: e19e138 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 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: |
A failed compaction only reached Desktop and Web as notices: a "did not finish" warning and an error notice that the open session drops, so the reason never showed and nothing stayed in the transcript. The engine now records the error on full_compaction.cancel when no turn owns the failure, kap-server projects it as a compaction item with outcome "failed" and an error, and the renderer draws it as a failed compaction divider with the reason. The client settles the compaction from that item and skips the did-not-finish note that follows it. A failed compaction is not an undo boundary and never absorbs a later summary. Background sessions keep one error toast titled "Context compaction failed".
|
@codex review |
…ilure # Conflicts: # packages/kap-server/test/apiSurface.test.ts
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2323d2bc1a
ℹ️ 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".
| const open = openTurn(state); | ||
| const turn = open ?? addTurn(state, time, 'system'); | ||
| turn.items.push({ | ||
| type: 'system', | ||
| kind: 'compaction', | ||
| item_id: nextItemId(turn), | ||
| status: 'ended', | ||
| outcome: 'completed', | ||
| outcome: error === undefined ? 'completed' : 'failed', |
There was a problem hiding this comment.
Negotiate failed-compaction items before sending them
When an older Desktop/Web client connects to an upgraded kap-server and a compaction fails, this new item is sent unconditionally; the released client ignores outcome/error, renders it as “Context compacted,” settles any compaction wait as completed, and treats it as an undo boundary. Preserve the prior response shape for unnegotiated clients, or gate the new item behind an opt-in parameter/capability, because core turn and item responses must remain backward compatible.
AGENTS.md reference: packages/kap-server/AGENTS.md:L7-L10
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Keeping this ungated, on purpose. Here's why:
- The response shape stays compatible.
outcomeon items already allowed"failed", anderroris a new optional field. A caller that reads the documented fields can tell a failed compaction from a completed one, so external agents and bots that read/turnssee a correct record. - The old-client problem is limited to released first-party Desktop/Web clients that ignore
outcome. Those clients only reach a newer server through a remote host (SSH or Remote Control) whose version doesn't match. A local Desktop always ships with its own server. - Gating would add an opt-in parameter to
/turnsand the WebSocket subscribe, plus an entry inmeta.capabilities, just to hide a record that the protocol already describes correctly.
This caveat is also listed in the PR description. Maintainers, please confirm this trade-off is acceptable. If it isn't, I'll add the opt-in.
| @@ -248,6 +247,7 @@ export const compactionItemSchema = z.object({ | |||
| tokens_after: z.number().nonnegative().optional(), | |||
| compacted_count: z.number().int().nonnegative().optional(), | |||
| summary: z.string().optional(), | |||
| error: errorInfoSchema.optional(), | |||
There was a problem hiding this comment.
Document persisted compaction failures in the KAP guide
Adding error and emitting failed compaction items changes the core conversation workflow, but packages/kap-server/docs/kap.md remains unchanged: consumers of the built-in GET /api/v1/kap.md guide are not told that a failed :compact operation is now persisted as a system item, how to distinguish it from a completed compaction, or that it can be recovered after reconnect despite notices themselves not being replayed. Update the built-in guide alongside this protocol change.
AGENTS.md reference: packages/kap-server/AGENTS.md:L13-L16
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in e19e138. The item section of kap.md now explains:
- how
outcomeseparates a completed compaction from a failed one; - the shape of
erroron a failed compaction; - that a failed compaction stays in the turns after a reconnect, even though its notices are not replayed;
- that a compaction a running turn was waiting on fails that turn instead.
| const payload = toKimiErrorPayload(error); | ||
| this.cancelActive(active, payload); |
There was a problem hiding this comment.
Preserve provider metadata on the failure item
For ordinary HTTP compaction failures, compactionRound wraps the provider error in a compaction.failed Error2 whose details are empty and whose status code/request ID exist only in cause; serializing that wrapper here therefore puts the metadata under cause, while kap-server's errorInfoOf reads only the top-level details. Consequently the new divider cannot show the promised HTTP status or request ID for typical 400/429 failures. Copy those diagnostics onto the outer payload or make the conversation projection traverse the cause chain.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch. Fixed in e19e138. The real payload has the status code only under cause.details, and serialize.ts deliberately strips statusCode from coded outer errors. So the conversation projection now reads status_code and request_id from the cause chain when they're missing at the top level. The fold test now uses that real shape, and it failed before the fix.
| if (notice.code === 'compaction.cancelled') { | ||
| if (failedCompactionSettled.delete(sessionId)) return; |
There was a problem hiding this comment.
Keep a background warning for compaction authentication failures
When a loaded background session's compaction fails with a 401/403, the persisted item carries provider.auth_error, so this branch consumes the compaction.cancelled warning; the following error notice is then discarded by isModelTurnErrorNotice because provider.auth_error is classified as a turn error. A user viewing another session therefore receives no toast or warning at all, whereas the cancellation note was visible before this change. Avoid consuming the warning unless the corresponding error notice will be surfaced, or exempt compaction failures from the model-turn suppression.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed and fixed in e19e138. When the cancelled notice is skipped because a failed compaction item already settled the compaction, the next error notice for that session is marked as the compaction failure. That notice no longer goes through the model-turn suppression, so a background session gets one toast titled "Context compaction failed". There's a regression test for the 401 case, plus a check that an unrelated auth error notice is still hidden.
Read the HTTP status and request ID of a failed compaction from the error's cause chain, raise a background toast for a compaction that fails on authentication, and describe failed compaction items in the KAP guide.
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
Screenshots / Recordings
在 Web 上手动
/compact,模型服务拒绝压缩请求(400)之后的画面。修复前只有右下角一条 6 秒就消失的提示;修复后,失败作为一条压缩分隔线留在对话里,紧跟失败原因(截图里第二次/compact成功了,是正常的「Context compacted」分隔线,可以对照;图里的 token 数来自假模型):Requirement or Bug
在 Desktop 和 Web 上压缩上下文,压缩请求被模型服务拒绝时,界面只在右下角弹一条 6 秒就消失的「Note:Context compaction did not finish」。用户不知道是模型服务拒绝了,分不清是失败还是被取消,对话里也不留任何痕迹,刷新页面或换一个客户端打开就什么都看不到。TUI 在同样的情形下会在转录里显示
Error: [compaction.failed] …。这个 PR 之后,压缩失败和压缩成功一样,作为一条压缩分隔线写进对话:标题「Context compaction failed」(自动压缩是「Auto-compaction failed」),后面是失败原因,悬停可以看到错误码、HTTP 状态和 request ID。它随会话保存,刷新、恢复会话、第二个客户端都能看到。
Bug Reproduction Steps
/compact发送,让模型服务对压缩请求返回 400(新 E2E 里是model.lane('compaction').script(httpError(400, …)))。compact()在 Desktop、Web 上看不到失败信号,只能等到 30 秒限时,所以self-test/compact.spec.ts的反例原来用onlyOn: ['tui', 'acp']排除了这两个面。Root Cause
压缩成功时,kap-server 把 wire 里的
full_compaction.complete投影成对话里的一条压缩 item,客户端据此画出「Context compacted」分隔线。压缩失败时,引擎写下的是full_compaction.cancel,它不带任何原因,kap-server 的会话投影遇到它只是把进行中的压缩丢掉。失败的信息只通过两条实时 notice 送达客户端:一条写死的警告「Context compaction did not finish」,一条错误码为compaction.failed、带原因的错误。客户端处理错误 notice 时有一条规则:当前打开的会话里的错误 notice 一律不弹提示,因为回合失败的原因已经显示在失败卡上。手动压缩不属于任何回合,没有失败卡,于是原因被这条规则丢掉了;而 notice 本来就不进对话、订阅时也不重发,所以就算弹出来,刷新之后也没了。
所以根因不在提示怎么弹,而在于失败没有进入会话的持久记录。这个 PR 是根本修复:把失败原因写进 wire 记录,投影成对话里的 item,客户端照着画。回合内失败交给失败卡的去重规则保持不变。
Code Changes
核心思路:压缩失败和压缩成功走同一条路径。引擎在压缩失败、且没有回合在等它时,把错误写进
full_compaction.cancel;kap-server 的会话投影把带错误的 cancel 变成一条outcome: 'failed'、带error的压缩 item;Desktop 和 Web 把它画成压缩分隔线的失败样式。失败因此像成功一样可以回放、跨客户端可见,客户端也不再需要从 notice 的先后顺序里倒推。sequenceDiagram participant Engine as 引擎(fullCompaction) participant Fold as kap-server 会话投影 participant Client as Desktop / Web 客户端 Engine->>Fold: full_compaction.cancel { error }(持久 wire 记录) Fold->>Client: item:kind compaction,outcome failed,error Client->>Client: 画失败分隔线,结算这次压缩 Engine->>Client: notice compaction.cancelled(did not finish) Client->>Client: 已由 item 结算,跳过这条提示 Engine->>Client: notice compaction.failed Client->>Client: 当前会话:不弹(对话里已有);后台会话:弹一条错误提示kap-server 在发 notice 之前会先把待发的回合更新推出去,所以失败 item 先于两条 notice 到达客户端。客户端收到失败 item 时结算压缩并记一笔,随后那条「did not finish」被跳过;用户主动取消时没有失败 item,警告照旧显示。
关键决策
kind: 'compaction',用已有的outcome: 'failed'加新的可选erroroutcome,错误结构复用回合已有的ErrorInfo,改动只是加一个可选字段completedfailed阅读顺序
packages/agent-core-v2/src/agent/fullCompaction/fullCompactionService.ts的compactionWorker和cancelActive:什么时候把错误写进 cancel。packages/kap-server/src/services/conversation/conversationFold.ts的cancelCompaction和appendCompaction:cancel 怎样变成失败 item,以及applyCompactionSummary不再把后来的摘要并进失败 item。packages/app-client/src/client/useKimiWebClient.ts的settleMainCompaction和onMainNotice:用失败 item 结算压缩、跳过随后的警告;删掉了第一版的提示去重和撤警告逻辑。packages/app-components/src/chat/ChatPane.vue:失败分隔线;undoableTurnIds只把成功的压缩当作撤销边界。可以略读:
packages/app-core里类型和投影的透传、i18n、wire-manifest.d.ts和 kap-serverschema/下的生成文件、E2E 驱动。失败分隔线的结构
Behavior Changes and Affected Users
full_compaction.cancelagentIderror(KimiErrorPayload)outcome总是completedoutcome: 'failed'、带error的压缩 item/turns或 WebSocketitem的外部 KAP 客户端compact()遇到压缩失败Compaction cancelled和错误replayBuilder仍把 cancel 当作取消契约文件:
packages/agent-core-v2/docs/wire-manifest.d.ts(full_compaction.cancel加可选error)、packages/kap-server/schema/contract.txt、openapi.json、asyncapi.json(压缩 item 加可选error),apiSurface.test.ts里的HOST_CONTRACT摘要随之更新,HOST_API_LEVEL保持 1。outcome,会把失败的压缩画成「Context compacted」分隔线,也会把它当成撤销边界。这是误导,但不会让客户端出错;所以没有升HOST_API_LEVEL。需要维护者确认是否接受。outcome。测试覆盖
packages/agent-core-v2/test/agent/fullCompaction/fullCompaction.test.ts:手动压缩失败时full_compaction.cancel带compaction.failed错误;回合在等压缩时 cancel 不带错误。packages/kap-server/test/services/conversation.test.ts:带错误的 cancel 投影成失败 item(回合之间是系统回合,回合内是 item),后来的摘要不会并进失败 item;不带错误的 cancel 仍然不显示(原有用例)。packages/app-client/test/conversation-events.test.ts:当前会话的失败进对话、压缩被结算、不弹提示;只有取消时警告保留;失败之后再取消一次,警告照常;后台会话只有一条标题正确的错误提示。tests/e2e/self-test/compact.spec.ts:反例去掉onlyOn,在 Desktop、Web 上也跑。上面三个单元测试文件里的新断言在没有修复的代码上都失败。
本机验证(macOS)
packages/app-client的会话事件、packages/app-core和packages/app-components全部,211 个文件、2643 条通过。self-test/compact.spec.ts在web、tui、acp上 9/9 通过;改成分隔线样式后,重新pnpm sync:web,Web 上 3/3 通过。Web 的反例约 7 秒报错,不再等满 30 秒。Desktop 和 Windows 没有跑。kimi web,确认失败分隔线出现、没有提示、刷新后还在、之后能继续发消息,第二次压缩成功后显示正常的分隔线。pnpm lint以 0 退出;pnpm run check:duplication通过;agent-core-v2、kap-server、tests/e2e的类型检查和pnpm --filter kimi-code-app run typecheck通过。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.