Repository navigation
fix(agent-core-v2): avoid quadratic cloning during session replay - #4885
Conversation
🦋 Changeset detectedLatest commit: 3e3dffb 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. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fe18c25f83
ℹ️ 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".
fe18c25 to
9c464f2
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c464f24c7
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 33aa94851a
ℹ️ 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".
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
335d257 to
660af3b
Compare
660af3b to
3e3dffb
Compare
|
@kimi-agent-bot Independent high-impact 2.6.0 reproduction and version-bisect evidence: #4917.
I confirmed #4888 / #4893 already shipped the No private session data, IDs, credentials, raw profiles, or logs were uploaded. |
* fix(agent-core-v2): avoid quadratic cloning during session replay (#4885) * fix(agent-core-v2): avoid quadratic cloning during session replay * fix(agent-core-v2): isolate replay assignments and preserve rollback order * refactor(agent-core-v2): make participant mutable replay explicit opt-in --------- Co-authored-by: kimi-agent-bot <[email protected]> * fix(agent-core-v2): validate Kimi media upload status (#4905) * fix(agent-core-v2): reject failed Kimi image extraction * chore: add Kimi image extraction changeset * fix(agent-core-v2): validate Kimi media upload status * fix(kap-server): forward swarm member task events (#4922) * test(agent-core-v2): make zero-timeout task wait deterministic (#4925) Co-authored-by: kimi-agent-bot <[email protected]> * feat(registry): support search_url and feedback_url on custom registry providers (#4875) * feat(registry): support search_url and feedback_url on custom registry providers * fix(sdk): honor provider.env credentials and custom_headers for feedback_url * refactor(core): submit feedback through the engine Web and desktop reach this machine through kap-server, so feedback submission resolves the active model's provider in the engine and is exposed on the same route and klient method. The SDK facade keeps the managed OAuth path and no longer re-derives provider credentials. * refactor(sdk): drop the unused feedback facade Product clients submit feedback through the engine. The SDK facade and oauth toolkit no longer keep a second managed-only submission path. * refactor(feedback): share engine schemas across route and klient The HTTP body uses the engine field names, so the route forwards it and the klient contract imports the same schemas. * refactor(feedback): drop unused kap and klient surfaces TUI reaches IFeedbackService through the harness. The HTTP routes, klient facade, and the schemas that only served them have no caller. * refactor(feedback): package and upload feedback attachments in the engine * refactor(feedback): keep FeedbackService internal to agent-core-v2 * test(cli): provide execFile in node:child_process mocks The feedback codebase scanner added in this branch calls promisify(execFile) at module scope and is now reachable from the agent-core-v2 index, so partial child_process mocks in these suites failed module evaluation. * feat(feedback): disclose submitted files, attach request_id, and drop text-only option (#4876) * feat(registry): support search_url and feedback_url on custom registry providers * fix(sdk): honor provider.env credentials and custom_headers for feedback_url * refactor(core): submit feedback through the engine Web and desktop reach this machine through kap-server, so feedback submission resolves the active model's provider in the engine and is exposed on the same route and klient method. The SDK facade keeps the managed OAuth path and no longer re-derives provider credentials. * refactor(sdk): drop the unused feedback facade Product clients submit feedback through the engine. The SDK facade and oauth toolkit no longer keep a second managed-only submission path. * refactor(feedback): share engine schemas across route and klient The HTTP body uses the engine field names, so the route forwards it and the klient contract imports the same schemas. * refactor(feedback): drop unused kap and klient surfaces TUI reaches IFeedbackService through the harness. The HTTP routes, klient facade, and the schemas that only served them have no caller. * refactor(feedback): package and upload feedback attachments in the engine * refactor(feedback): keep FeedbackService internal to agent-core-v2 * test(cli): provide execFile in node:child_process mocks The feedback codebase scanner added in this branch calls promisify(execFile) at module scope and is now reachable from the agent-core-v2 index, so partial child_process mocks in these suites failed module evaluation. * feat(feedback): disclose submitted files, attach request_id, and drop text-only option * fix(session-export): keep SESSION_NOT_FOUND for missing non-empty session ids The global-log-only fallback exists for feedback submissions without an active session (empty id). Scoping it to empty ids restores the 40401 envelope that kap-server's export route promises for unknown sessions. * test(agent-core-v2): pin retained request id semantics in llmRequester assertions * chore: sync web dist from code-app code-app: 11563df26b92f0035659083327976a2e9b8e6727 code-app-release: b0b6d9c7a45c96ca48039ff05789c6e90f76ef20 code-app-local-build: 005d2b437493b7ab04d01f70542f3b6356699f4d * ci: release packages (#4918) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix(agent-core-v2): align request id back-sync with main --------- Co-authored-by: kimi-agent-bot <[email protected]> Co-authored-by: Haozhe <[email protected]> Co-authored-by: 7Sageer <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Requirement or Bug
打开或恢复包含大量历史轮次与工具调用的既有本地会话(例如冷启动首次打开会话,或会话经服务端空闲回收后再次切换打开)时,服务端重放
wire.jsonl同步阻塞主事件循环数秒(450 轮 / 4,500 次工具调用的会话耗时约 5.4s–8.5s),客户端对话主区域与右侧项目栏同时停留在「加载中…」。本 PR 在
DurableAgentRuntimeParticipant上对齐ReplayableStateKey既有的显式mutableReplay?: boolean选入机制并在ConversationFeed(kap.conversation)开启,同时在restoreLoopJournal中启用loopEventFold的可变折叠模式(收尾统一递归freeze),将 450 轮 / 4,500 次工具调用会话的重放耗时从约 6.0s 降至约 0.07s(客户端完整打开由 ~8.5s 降至 ~1.2s),同时保持其余参与者原有produce路径与终态冻结语义完全不变。Bug Reproduction Steps
wire.jsonl约 16,000–25,000 行)。EventDispatcherService.restore()与LoopJournal.snapshot()在重放wire.jsonl期间同步阻塞服务端主线程数秒,客户端首帧turns响应延迟,界面长时间显示「加载中…」。Root Cause
根本修复。 服务端会话恢复路径存在两处随历史记录数呈$O(N^2)$ 增长的拷贝热点:
EventDispatcherService未向kap.conversation参与者提供可变重放路径(主因):packages/agent-core-v2/src/state/eventDispatcherService.ts在executeEvent中仅为显式声明mutableReplay的ReplayableStateKey提供了暂存可变重放路径,而kap.conversation(ConversationFeed)在restore()重放每条wire记录时均调用replayImmer.produce。当ConversationState.tools(数千键对象)、steps与turns随历史增长后,每条工具或消息事件触发 ImmershallowCopy全量拷贝属性描述符,形成attachLateNow在逐条循环内调用produce(structuredClone(attachment.getState()), ...)(4,572 条记录即耗时 5.37s)。restoreLoopJournal未开启loopEventFold可变模式(叠加热点):packages/agent-core-v2/src/agent/loop/journal.ts的restoreLoopJournal在顺序折叠context.append_message与context.append_loop_event时未向foldAppendMessage/foldLoopEvent传入mutable = true,导致每条事件都复制整条ContextMessage[]数组并对单条消息递归freeze(25,652 条记录额外耗时约 0.46s–0.81s)。Code Changes
与
ReplayableOptions.mutableReplay(state.ts)完全一致,在DurableAgentRuntimeParticipant上增加可选mutableReplay?: boolean,并仅在纯折叠且具自定义onUndo的ConversationFeed(kap.conversation)开启:重放期间在replayMutableParticipants暂存的独立structuredClone草稿上原地执行applier,中途不提前写入attachment.commit(失败直接丢弃暂存草稿),重放成功收尾时一次性递归freeze并commit;未开启mutableReplay的其余参与者保持原有produce行为逐字不变。阅读顺序:
packages/agent-core-v2/src/state/eventDispatcher.ts与eventDispatcherService.ts:对齐replayMutableStates实现mutableReplay?: boolean暂存重放与收尾freeze。packages/kap-server/src/services/conversation/conversationFeed.ts:kap.conversation开启mutableReplay: true。packages/agent-core-v2/src/agent/loop/journal.ts:restoreLoopJournal开启mutable = true并在foldSettle后统一freeze。packages/agent-core-v2/test/state/eventDispatcher.test.ts与packages/agent-core-v2/test/agent/loop/journal.test.ts:边界与失败丢弃回归测试。Screenshots / Recordings
同一份合成重度工具长会话(450 轮 / 4,500 次工具调用,25,652 行
wire.jsonl)冷启动点击打开对比:左侧修改前在t = 2.0s(直至 ~8.5s)仍停留在「加载中…」,右侧修改后快速完成加载并可交互。Behavior Changes and Affected Users
kap.conversation(ConversationFeed,mutableReplay: true)历史恢复与attachLateImmer.produce(attachLate另逐条structuredClone),重放末尾再structuredClone+freezefreeze后commit;终态数据与冻结属性逐字节一致,450 轮会话重放由 ~6.0s 降至 ~70mskap-server会话流的客户端(desktop 与 web)mutableReplay: true的DurableAgentRuntimeParticipant与AgentModelreplayImmer.produce/produce逐条折叠并commitrestoreLoopJournal重建循环上下文消息ContextMessage[]mutable = true折叠并在foldSettle后一次性递归freeze,返回消息及嵌套内容同样不可变packages/agent-core-v2/src/state/eventDispatcher.tspackages/agent-core-v2/src/state/eventDispatcherService.tspackages/agent-core-v2/src/agent/loop/journal.tspackages/kap-server/src/services/conversation/conversationFeed.tspackages/agent-core-v2/test/state/eventDispatcher.test.ts:验证mutableReplay: true要求自定义onUndo、不污染initial、重跑与失败原子丢弃、冻结替换克隆及attachLate追平后继续实时派发。packages/agent-core-v2/test/agent/loop/journal.test.ts:验证多步工具调用与步骤内压缩恢复不修改输入WireRecord且返回消息嵌套字段已冻结。ConversationFeed.turns()与restoreLoopJournal()序列化 SHA-256 100% 一致。验证与基准测试明细
合成长会话重放基准对比(同一份
wire.jsonl,3 轮均值,输出 SHA-256 全部一致)wire.jsonl行数)dispatcher.restore()修改前dispatcher.restore()修改后restoreLoopJournal修改前restoreLoopJournal修改后attachLate修改前 → 修改后Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.