Repository navigation
fix(agent-core-v2): preserve legacy steer pairing after undo - #5599
Conversation
🦋 Changeset detectedLatest commit: 2edf7bd The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 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: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8e3b40f408
ℹ️ 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".
|
|
||
| for (const record of records) { | ||
| const moveLegacySteerBeforeAppend = (index: number, generatedCount: number): void => { | ||
| if (appendIndexBySteer.get(index) !== index - 1) return; |
There was a problem hiding this comment.
Exclude injected messages from steer pre-matching
When a steer-first history contains a user-role injection immediately before the steer (append(injection), turn.steer(S), append(S)), the all-user matcher's relaxed fallback consumes S for the injection regardless of content. This condition then moves the synthesized turn boundary ahead of the injection, so sliceMainRecordsAtTurn drops that reminder from a fork of the preceding turn and replay assigns the wrong turn to it. System reminders are persisted as user messages with origin.kind === 'injection', so restored older sessions where a reminder arrived just before a steer need these appends excluded from relocation or matched only after the actual steer append.
AGENTS.md reference: AGENTS.md:L111-L115
Useful? React with 👍 / 👎.
|
|
||
| export function normalizeLegacyTurnSteerRecords(records: readonly WireRecord[]): WireRecord[] { | ||
| const appendIndexBySteer = new Map<number, number>(); | ||
| for (const match of matchWireTurnInputRecords(records, true)) { |
There was a problem hiding this comment.
Skip legacy matching when no steer records exist
For large current-format sessions that contain turn.prompt and user appends but no legacy turn.steer, this unconditional call still runs the quadratic matcher: each append searches unused and then splices an element from it. Normalization is used by ordinary wire reads and restoration, and foldWireRecords now invokes it twice before running the matcher again, so long modern histories pay this legacy migration cost repeatedly even though appendIndexBySteer must remain empty. Return early from the pre-match when there are no steer records, or use a linear matching structure, to avoid slowing cold restore, replay, and forks as histories grow.
Useful? React with 👍 / 👎.
main moved the wireRecordObject helper out of replayBuilder/fold.ts into wire/turnInput.ts (#5599). readErrorPayload in fold.ts still calls it, so export it from turnInput.ts and import it in fold.ts.
Requirement or Bug
旧版本写出的会话在同时包含撤回分支和 legacy
turn.steer时,按轮 fork 会把已撤回的提问重新带入副本,并把后续追加内容归到错误的回合。这个 PR 让这类历史在 fork、replay 和 JSON 往返后的冷恢复路径模拟中都保留有效分支的输入及正确回合归属,同时保持源会话原始 wire 不变。No external issue was provided; the reproduction and root cause are included below.
Bug Reproduction Steps
Root Cause
restorableChain为恢复回合时钟重新纳入撤回分支的turn.prompt,但这些 prompt 的有效正文已不在 active chain 中。旧 steer 归一化为turn.ended + turn.prompt后,匹配器仍按记录位置和内容重新猜测输入与正文的对应关系:普通 prompt 只能匹配其前方正文,缺 ID 时还会退到 relaxed match,于是孤立 prompt 抢走后续 steer 的正文,并被 fork 当成有效输入保留。This is a fundamental fix at the shared normalization and matching boundary, not a workaround.
Code Changes
核心思路是先使用原始有效分支确定 steer 与正文的关联,再生成可稳定重放的回合顺序;撤回 prompt 只保留时钟用途,不再参与正文匹配。
wire/turnInput.ts:集中维护共享的输入匹配器及 clock-only 标记,replayBuilder 继续导出原函数和类型,避免恢复与 fork 各自维护平行匹配逻辑。wire/human.ts:在转换旧 steer 前取得 steerIndex → appendIndex 关联;只在 steer 与前方相邻 user append 预匹配成功的 append-first 形态中,把生成的回合记录放到对应正文之前。其他记录顺序、ID 和 loop event 归属不变。wire/tree/fork.ts:重新纳入的撤回 prompt 使用进程内 Symbol 标记;它仍推进nextTurnId,但不进入正文候选,也不会因误配被写入 fork。replayBuilder/fold.ts:复用同一匹配器,保留原公开导出及 append object identity,覆盖完整和窗口化 replay。阅读顺序建议:先看
wire/turnInput.ts的共享约束,再看wire/human.ts的预匹配与归一化,随后看wire/tree/fork.ts的 clock-only 来源;replayBuilder/fold.ts主要是接线与导出。测试集中在forkTurnSlice.test.ts和fold.test.ts。Behavior Changes and Affected Users
:fork或公共 SDKforkSession指定turnIndex的调用方:fork或公共 SDKforkSession使用旧 steer 历史的调用方session/fork)WireTurnInputMatch与matchWireTurnInputRecords继续导出覆盖情况:新增回归覆盖 C/C1/C2 三个 fork 截点及各自 JSON 往返后的冷恢复路径模拟、撤回文案与 C1 重复、连续 C1=C2、仅 prompt 或 message 有 ID、steer 缺 ID、完全无 ID、relaxed/promptIds、两种 steer 顺序、现代 normal/notify 强 ID 防回归、steer-first injection,以及连续同文案下的窗口化 replay。VS Code 与 KAP/SDK 共用同一 Core 修复路径,KAP 和 SDK 套件已直接验证;没有单独运行 VS Code 或 ACP 专项测试。没有 UI 变化,也不需要文档更新。
验证记录与未验证边界
sessions.test.ts:163/163。sdk-rpc-client-v2.test.ts:173/173。pnpm lint与check:duplication通过。config.worktree失败和一项 sessionExport 默认路径 zip ENOENT。未运行纯 base,因此尚不能确认这些失败为 base 既有问题;失败测试文件不在本次 diff 中,对应两个测试文件在本机复跑为 95/95。Checklist
/approve). No external issue was provided; this PR contains a self-contained reproduction.gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.