Repository navigation
fix(kap-server): append new content blocks without rescanning the turn - #5344
Conversation
🦋 Changeset detectedLatest commit: 92ac847 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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: f323049af3
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 60ca5819dc
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
commit: |
Keep a per-step next-block cursor so an open step's content parts append straight to the turn and only out-of-order rewrites fall back to upsert (MoonshotAI#5344); a step with no items seeds the cursor and step.end drops it. Compare and diff jsPlugins as sets so lint-guard configs are order insensitive and a removed plugin reads as weakened (MoonshotAI#5249). Ledger: 5015 equivalent.
Requirement or Bug
有用户报告:在 Windows 上用 Kimi Code Desktop 1.4.2,一个会话里同时跑主代理和 9 个子代理。长时间运行后,全局日志反复出现
append log flush is stuck; records are accumulating in memory,最后客户端被 Windows 记录为 Application Hang 并强制关闭。排查发现,append-log 积压只是现象。真正的原因是主进程的事件循环被会话投影的同步工作反复占住几十秒。本 PR 修掉投影里的二次方开销。
Bug Reproduction Steps
reasoning_content和reasoning两个字段,导致每个 token 都成为独立的内容块。Root Cause
步骤结束时,journal 在一个同步循环里为每个内容块写一条记录并 dispatch
ContextAppendLoopEvent。kap-server 的ConversationFeed在 immerproduce中处理每个content.part,upsertItem每次都对整轮的turn.items做一次findIndex,总开销是 O(n²),并且全部发生在 Desktop 主进程上。用户日志和这个模型吻合:一个 10657 块的步骤连续写了 45 秒,一个 9671 块的步骤写了 103 秒;期间全进程 10 个代理没有插入任何其他记录,耗时曲线也呈二次方。最后一个大步骤结束后约 1.5 分钟,Windows 就记录了 Hang。
本 PR 去掉其中的线性查找。immer 每个事件复制并冻结
turn.items的开销仍然存在(见 Code Changes),所以这是缓解,不是彻底消除二次方。块数暴增的上游原因(provider 镜像推理字段导致内容块无法合并)不在本 PR 范围内。sequenceDiagram participant J as Loop journal participant F as ConversationFeed (main process) J->>F: N × content.part, one sync loop Note over F: before: each part does findIndex over turn.items → O(N²) Note over F: after: new block → push, O(1) lookupCode Changes
会话投影:已知是新块的
content.part直接追加(packages/kap-server/src/services/conversation/conversationFold.ts)这里的不变式是:带
${stepUuid}:前缀的 item 只在content.part里创建,所以只要block不小于"该步骤下一个未出现的块",就一定不存在同 id 的 item。以下情况都回到原来的upsertItem,语义不变:重复块、乱序块、没有block字段、step.end之后才到的块、fork 继承的步骤、找不到step.begin的步骤。实测(生产 fold + immer,单轮单步骤,本机):8000 块从 8.0 s 降到 1.47 s,与"只 push"的下限一致。剩下的二次方来自 immer 每个事件都会复制并冻结
turn.items,属于 dispatcher 提交实时事件的方式,本 PR 不动。Behavior Changes and Affected Users
upsertItem,结果是在末尾 pushupsertItemkimi web、经 KAP 订阅会话的客户端upsertItem受影响模块:kap-server 会话投影,进而影响 Desktop(内嵌 kap-server)和
kimi web。对外协议、事件顺序和内容都不变。测试覆盖:
packages/kap-server/test/services/conversation.test.ts新增用例,用计数 Proxy 断言追加 200 个块时对turn.items的元素读取次数是线性的(修复前 20102 次),并确认重复块仍会覆盖原 item。原有的重试、undo、fork、清空相关用例都通过。验证记录
pnpm exec vitest run --reporter=agent packages/kap-server/test/services/conversation.test.ts:通过。pnpm exec vitest run --reporter=agent packages/kap-server/test:1491 passed,1 failed(searchService.test.ts的 "keeps a query valid when readonly refresh replaces its handle during source validation")。该用例只在全量并发运行时失败,单独运行时改动前后都通过,与本 PR 无关。pnpm exec tsc -p packages/kap-server/tsconfig.json --noEmit:通过。pnpm lint:通过。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.