Repository navigation
fix(agent-core-v2): stage opt-in mutable replay for conversation participant - #4888
Merged
wbxl2000 merged 1 commit intoNov 12, 2026
Merged
Conversation
🦋 Changeset detectedLatest commit: 8c9297f 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: |
This was referenced Nov 12, 2026
Merged
6 tasks done
This was referenced Nov 13, 2026
11 tasks
1 task
7723qqq
pushed a commit
to 7723qqq/kimi-code
that referenced
this pull request
Nov 18, 2026
MoonshotAI#4663 and MoonshotAI#4789 now describe the recorded-event fold that serves the pinned bundle: task links come from persisted TaskStarted records, and the feed no longer rebuilds from the model-context view. MoonshotAI#4730 moves from partial to ported (record-driven items seal once, with the recorded reasoning duration), and MoonshotAI#4772's probe follows the feed suite it now lives in. MoonshotAI#4893 is unblocked: `kap.conversation` stages its replay, publishes once after rehydration, and gates live dispatch only while a mutable participant restores. The PLAYBOOK note on the release-branch commits carries the correction for MoonshotAI#4888 for the same reason.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirement or Bug
重新打开包含大量历史轮次与工具调用的本地会话时,服务端重放$O(N^2)$ 对象浅拷贝与重复深拷贝,阻塞主事件循环数秒。
wire.jsonl触发Bug Reproduction Steps
wire.jsonl)。attachLate订阅该会话。EventDispatcherService.restore()(约 4.3s)与attachLate()(150 轮即约 29.6s)在重放期间同步阻塞服务端主线程。Root Cause
根本修复。 会话恢复路径有两处未对齐 PR #4313 的可变重放机制:
EventDispatcherService仅为显式声明mutableReplay: true的ReplayableStateKey提供了暂存可变重放路径,而DurableAgentRuntimeParticipant(如kap.conversation)在restore()中对每条记录调用Immer.produce(当ConversationState.tools增长至数千键时每次写入均触发全表属性描述符浅拷贝,呈attachLate()中更对每条记录额外执行structuredClone(attachment.getState())。restoreLoopJournal折叠context.append_message与context.append_loop_event时未传入已有的mutable = true参数,导致每条记录复制整条ContextMessage[]并逐条递归冻结。Code Changes
对齐
ReplayableStateKey已有的mutableReplay设计:在DurableAgentRuntimeParticipant上增加显式 opt-in 开关mutableReplay?: boolean(启用时要求undoable参与者提供自定义onUndo并禁止使用checkpoint),仅对kap.conversation开启;重放期间在隔离的暂存草稿上原地折叠,重放成功收尾时一次性递归freeze并commit,异常时直接丢弃暂存草稿。未开启开关的参与者与AgentModel保持原Immer.produce路径不变。EventDispatcherService (restore / attachLate, silent = true) attachment.mutableReplay === false (default) unchanged: per-record Immer.produce + per-record commit attachment.mutableReplay === true (kap.conversation) - every record -> Immer.produce / produce(structuredClone(state)) + commit + first mutating record -> replayImmer.produce + structuredClone into replayMutableParticipants + subsequent records -> applier(stagedDraft) in place (structuredClone only on replacement return) + end of replay -> attachment.commit(freeze(stagedDraft, true)) once + on replay failure -> discard replayMutableParticipants (caller state untouched) restoreLoopJournal - foldAppendMessage(context, message) - foldLoopEvent(context, event) - context = foldSettle(context) + foldAppendMessage(context, message, true) + foldLoopEvent(context, event, true) + context = freeze(foldSettle(context), true)本机合成长会话实测(修改前后
ConversationFeed.turns()SHA-256 100% 一致):restore()restore()restore()attachLate()Behavior Changes and Affected Users
kap.conversation在restore()与attachLate()期间的状态重放Immer.produce(attachLate另逐条structuredClone)并逐条commitfreeze并commit;重放失败丢弃暂存草稿mutableReplay: true的DurableAgentRuntimeParticipant与AgentModelImmer.produce与commitrestoreLoopJournal重建循环上下文消息ContextMessage[]mutable = true折叠并在foldSettle后一次性递归freezepackages/agent-core-v2/src/state/eventDispatcher.tspackages/agent-core-v2/src/state/state.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缺少自定义onUndo与违规调用checkpoint报错、首次变更克隆隔离与单次冻结提交、替换对象(含带冻结子属性的外层未冻结对象)隔离、重跑restore()、外层包装容器box的attachLate()、重放异常丢弃暂存草稿不污染原状态,以及restore()读取期间实时dispatch()共存。packages/agent-core-v2/test/agent/loop/journal.test.ts:覆盖多步工具调用与步骤内压缩恢复不修改输入记录且嵌套消息内容已冻结。packages/kap-server/test/services/conversation.test.ts与conversationContract.e2e.test.ts:全量通过。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.