Skip to content

fix(transcript): keep realigned turn ids unique when folding cold facts - #4658

Merged
wbxl2000 merged 2 commits into
release/2026-11-04from
qer/fold-facts-unique-turn-ids-release
Nov 3, 2026
Merged

wbxl2000 merged 2 commits into
release/2026-11-04from
qer/fold-facts-unique-turn-ids-release

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

侧边对话(BTW)在服务端从磁盘重新读取会话之后(例如服务端重启、会话被空闲回收后再打开),历史会缺轮次或乱序:按顺序问了四个问题,重新打开后第一问不见了,被第二问覆盖。这个 PR 之后,冷读出来的侧边对话轮次编号唯一,顺序和 wire 记录一致。

Bug Reproduction Steps

  1. 在一个已有对话的会话里打开侧边对话,连续问四个问题。
  2. 重启服务端(让该会话的 transcript 走冷读)。
  3. 重新打开这个会话的侧边对话:顶部从第二问开始,第一问缺失。

GET /sessions/{id}/transcript?agent_id=<fork> 的轮次编号是 t0, t1, t1, t2, t3,两轮共用 t1;客户端按 turnId 存轮次,后加载的那轮覆盖了前一轮。

Root Cause

foldWireRecordFacts 冷读时先用 groupMessagesIntoSnapshot 按消息分组出基础轮次,再把每个基础轮次重新编号到它在 wire 记录里的 turn id(realignedBaseItems)。

fork 出来的 agent 里,继承自主 agent 的历史没有 wire 记录,占用基础编号 0;fork 自己的第一轮在 wire 里也是 0。重新编号时只避让了"没有 wire 记录的轮次"(keptOrdinals),所以:

基础轮次 wire id 原逻辑结果
继承的一轮(基础编号 0) 无 保持 t0
fork 第一问(基础编号 1) 0 0 已被占用,保持 t1
fork 第二问(基础编号 2) 1 1 不在 keptOrdinals 里,改成 t1,与第一问撞号

#4544 加的已占用编号避让只覆盖后面"补缺失轮次"(missingTurnIds)那条路径,没有覆盖这一段。本 PR 是根本修复。

Code Changes

只改 realignedBaseItems 的编号认领顺序:

- keptOrdinals = ordinals of turns without a wire id
- for each turn with a wire id
-   renumber to wireId unless keptOrdinals has wireId
+ claimed = ordinals of turns without a wire id, or already at their wire id
+ for each turn with a wire id, in order
+   if wireId !== ordinal
+     take wireId if free, otherwise keep the ordinal or move to the next free one
+     claim the result

轮次在数组里的顺序不变,只是保证每个轮次最终的 ordinal / turnId 互不相同;能对齐到 wire id 的轮次照旧对齐,startedAt、wedged 等仍按 wire id 查。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
冷读时基础轮次的 wire id 已被前面的轮次占用 可能和其他轮次得到相同 turnId,客户端显示时后者覆盖前者 保留原编号或顺延到下一个空位,turnId 唯一 无(重复 turnId 本身是错误数据) 不需要
冷读时基础轮次的 wire id 空闲,或等于自身编号 对齐到 wire id 不变 — —
没有 wire 记录的基础轮次(如 fork 继承的历史) 保持原编号 不变 — —

受影响的是所有走冷读的 transcript 读取:REST GET /sessions/{id}/transcript 在会话未加载时、以及会话加载时的历史回填。主 agent 一般没有"无 wire 记录的继承轮次",不会进入撞号分支;受益的主要是 web / desktop 里的侧边对话。

测试:packages/transcript/test/layers.test.ts 新增用例覆盖"继承轮次占用 fork 第一轮的 wire id",断言五轮按顺序、turnId 和 ordinal 都唯一(修复前失败:5 轮只有 4 个不同 turnId);transcript 全部测试和 kap-server test/services 通过。另外在本地用真实会话做了端到端验证:同一份侧边对话记录冷读后由 t0, t1, t1, t2, t3 变为 t0…t4,界面上四问齐全且顺序正确。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Realigning base turns to their wire ids only skipped ordinals held by
turns without a wire id. When an inherited turn held a forked turn's
wire id, that turn kept its base ordinal, and the next turn's wire id
could land on the same ordinal, so two turns shared a turn id and the
later one replaced the earlier in clients. Claim ordinals in order:
turns without a wire id or already at their wire id first, then each
realigned turn takes its wire id when free, else keeps its ordinal or
moves to the next free one.
@changeset-bot

changeset-bot Bot commented Nov 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7c30b66

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T16:02:02.764569Z 7c30b66 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@7c30b66
npx https://pkg.pr.new/@moonshot-ai/kimi-code@7c30b66

commit: 7c30b66

@wbxl2000
wbxl2000 merged commit e9de788 into release/2026-11-04 Nov 3, 2026
15 checks passed
@wbxl2000
wbxl2000 deleted the qer/fold-facts-unique-turn-ids-release branch November 3, 2026 16:07
@github-actions github-actions Bot mentioned this pull request Nov 3, 2026
Grapedge pushed a commit that referenced this pull request Nov 4, 2026
* fix(tui): read welcome cwd and footer git status from the session environment (#4649)

* fix(tui): show remote cwd in welcome panel

* fix(tui): read footer git status through the session environment

The footer ran git and gh on the local machine, so a remote-bound session
hid the git badge. Read branch, working-tree, and pull request state through
the SDK instead: a live session's own environment, or the local work dir
before the lazy session exists. The local HEAD poll stays as a refresh
trigger only.

Core git commands now time out after 30s, and the pull request cache is
keyed by branch so a branch switch no longer serves the previous branch's
pull request.

* fix(agent-core-v2): resolve local git and gh outside the working directory

GitService spawned bare git and gh with the workspace as cwd. On Windows a
bare name is looked up in the cwd before PATH, so a binary planted in the
workspace could run, and the TUI footer now reads git status at startup,
before the workspace trust gate. Resolve both through PATH for the local
environment and refuse any match inside the working directory.

* chore: merge changesets for remote cwd and git status

* fix(tui): drop the startup Ready. row once startup succeeds

The startup progress row turned into a permanent "Ready." line under the
welcome panel. Remove it on success; a failed startup still leaves the
"Startup failed." row.

* fix(agent-core-v2): compare workspace containment case-insensitively on Windows

resolveProgramPath compared the PATH candidate against the working directory
case-sensitively, so on Windows a PATH entry spelled with different casing
(c:\repo\bin vs C:\Repo) let a workspace-local executable through.

* fix(tui): keep the footer git badge through a remote disconnect

The git cache key included the environment status, so a remote dropping to
connecting or disconnected rebuilt the cache and lost the last badge. Key
the cache by session and work dir only, and refresh it when the environment
turns ready again.

* feat: isolate plugin availability by client (#4653)

* fix: preserve marketplace descriptions

* fix: preserve localized plugin display names

* feat: isolate plugin availability by client

* fix: preserve cli marketplace loading

* fix: harden plugin availability reconciliation

* fix: hash plugin marketplace provenance

* fix: complete plugin availability reconciliation

* fix(transcript): keep realigned turn ids unique when folding cold facts (#4658)

* fix(transcript): keep realigned turn ids unique when folding cold facts

Realigning base turns to their wire ids only skipped ordinals held by
turns without a wire id. When an inherited turn held a forked turn's
wire id, that turn kept its base ordinal, and the next turn's wire id
could land on the same ordinal, so two turns shared a turn id and the
later one replaced the earlier in clients. Claim ordinals in order:
turns without a wire id or already at their wire id first, then each
realigned turn takes its wire id when free, else keeps its ordinal or
moves to the next free one.

* chore: add changeset for side chat turn order fix

* fix(kap-server): wait for an agent's history before accepting its prompt (#4659)

* fix(kap-server): backfill a side chat fork before replying to :btw

A fork's inherited history folds into a cold turn at t0, while the live
transcript only records it as markers. When the fork's first prompt
started before anything read the fork transcript, its live turn took t0;
the history backfill then merged the inherited main turn into it by turn
id, so the first side chat turn carried the main answer and the real
answer landed in a duplicate turn. Finish the fork's history backfill
before :btw replies, so the inherited turn holds t0 before the first
prompt can start.

* fix(kap-server): wait for an agent's history before accepting its prompt

Delaying the :btw reply did not order the fork's first prompt after its
history backfill: other clients learn the fork from the agent roster as
soon as it is created, and checking for a live transcript with
forSessionLive created one, so every :btw also waited for the whole main
history. Move the wait to prompt submission: a prompt for a non-main
agent awaits that agent's history backfill before it is enqueued, which
covers every client and leaves :btw unchanged. The backfill is cached
per agent, so later prompts do not wait again.

* fix: safely launch Windows SSH executors (#4664)

* feat: support Windows SSH remote environments

* Delete .changeset/windows-remote-environments.md

Signed-off-by: 7Sageer <[email protected]>

---------

Signed-off-by: 7Sageer <[email protected]>

* fix(agent-core-v2): match regional plugin locales (#4666)

* fix(plugins): confirm stopping Windows CU processes before install (#4670)

* fix(plugins): confirm stopping Windows CU processes before install

* test(plugins): cover capability install confirmation contract

* fix(plugins): preserve bodyless installs and plugin-only repairs

* chore: sync web dist from code-app (#4667)

* chore: sync web dist from code-app

code-app: 63661fc449e2ec0ce024bc08fe87f2c757d483e8

* chore: sync web dist from code-app

code-app: d0e913a6c9607be778c62f0c74ee7310bfb8862a

* chore: sync web dist from code-app

code-app: b306f4a2f9be8944e5a8064ecb076e3137c968f1

---------

Co-authored-by: kimi-agent-bot <[email protected]>

* ci: release packages (#4672)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Signed-off-by: 7Sageer <[email protected]>
Co-authored-by: 7Sageer <[email protected]>
Co-authored-by: liruifengv <[email protected]>
Co-authored-by: qer <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Port of upstream MoonshotAI/kimi-code e9de788 (MoonshotAI#4658).

Realigning base turns to their wire ids only skipped ordinals held by
turns without a wire id. When an inherited turn held a forked turn's
wire id, that turn kept its base ordinal, and the next turn's wire id
could land on the same ordinal, so two turns shared a turn id and the
later one replaced the earlier in clients.

Claim ordinals in order: turns without a wire id or already at their
wire id first, then each realigned turn takes its wire id when free,
else keeps its ordinal or moves to the next free one.

The added test folds a fork whose inherited turn holds wire id 0 and
fails on the pre-change tree with four unique turn ids for five turns.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Fifteen rows enter the ledger: the six commits that landed on
upstream/main after the last refresh, and the nine commits that reach
main only through the release/2026-11-04 sync (MoonshotAI#4674), which is where
the 2.5.0 changelog entries come from.

Each 2.5.0 row records what it needs from this tree, so the next batch
starts from the blocker instead of rediscovering it:

- MoonshotAI#4658 and MoonshotAI#4659 are ported (79a4199, 7a531f5) with tests that
  fail on the pre-change tree.
- MoonshotAI#4649 is in_progress: its agent-core-v2 half landed in 2fa7b6b,
  while the footer and welcome half needs the pull-request API of MoonshotAI#4527
  and the session workdir of MoonshotAI#4062.
- MoonshotAI#4653, MoonshotAI#4666 and MoonshotAI#4670 build on the MoonshotAI#4525 plugin marketplace, absent
  here; MoonshotAI#4670 additionally needs isError2.
- MoonshotAI#4664 needs the MoonshotAI#4062 remote environments.
- MoonshotAI#4667 and MoonshotAI#4672 are generated-bundle and release commits; settling
  them as not-applicable needs a signature.

Rows were appended by a one-off writer because --refresh fetches and
that fetch does not complete on this network; the file still parses
under the tool's own column contract, and --verify --batch B5 passes
both new probes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant