Skip to content

fix(agent-core-v2): stop rebuilding mismatched session indexes - #4980

Merged
7Sageer merged 1 commit into
release/2026-11-16from
fix/session-index-schema-mismatch-degrade
Nov 16, 2026
Merged

7Sageer merged 1 commit into
release/2026-11-16from
fix/session-index-schema-mismatch-degrade

Conversation

@7Sageer

@7Sageer 7Sageer commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

会话很多、并且 2.5 与 2.6 进程同时运行时,共享会话索引的 schema 版本不一致会让进程反复全量重建。重建把 16 个 shard 的索引装进 V8 堆,约 8000 个会话可在一分钟内触及约 4GB 堆上限。这个 PR 让版本不一致的进程直接提供文件索引,停止重建循环。

Bug Reproduction Steps

  1. 准备约 8000 个会话,并保留由旧版本写入的会话索引。
  2. 同时运行索引 schema 版本不同的 2.5 与 2.6 进程。
  3. 打开 Desktop 会话列表,观察进程反复重建索引并快速增长到约 4GB 堆。

Root Cause

prepare 看到 checkpoint 的 schemaVersion 与当前版本不一致时,直接执行全量投影。新旧进程共享同一套索引,于是 v2 与 v3 来回重建。

这是止血,不是最终修复。版本不一致时不再重建;后续会移除 Minidb 会话读模型。

Code Changes

schema 不匹配时,进程标记为稳定降级,不再调用投影:

 prepare
   read checkpoint
-  missing or schema mismatch
-    project every session
+  schema mismatch
+    mark degraded and stop retrying
+  missing
+    project every session
   matching schema
     reconcile when stale

降级后的列表、计数和按 ID 查询继续走文件路径,并在 30 秒内复用同一次目录扫描:

 list or count while read model is unavailable
-  uninitialized or preparing
-    join the in-flight scan
-  degraded
-    read every state.json
+  join or reuse the shared scan

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
已发布索引的 schema 与当前进程不一致 当前进程全量重建并发布新 generation 保持原索引不变,列表改由文件扫描提供 同时运行不同 schema 版本的 Desktop、Web、TUI、print、ACP、SDK 进程 无;这是为停止重建循环而移除的行为
降级或准备期间,30 秒内的重复列表与计数 每次请求重新扫描 复用第一次扫描结果 依赖每次降级读取立即看到其他进程刚写入会话的客户端 无;本进程写入仍通过 mirror 立即可见
schema 匹配或索引不存在 按现有路径准备、reconciliation 或首次投影 不变 单一版本用户 不适用

受影响模块是 agent-core-v2 的会话索引。测试覆盖:

  • schema 不匹配时不扫描、不改 checkpoint,列表仍返回文件中的会话,第二次列表复用第一次扫描。
  • 准备期间的降级读取在复用窗口内不再重复扫描。
  • 现有 54 个 session index 回归通过,包括 schema 匹配时的准备、reconciliation 和热读取。

Performance Benchmark

在 Linux 压测环境中使用真实 FileSessionIndex、JsonAtomicDocumentStore、FileStorageService 和 16 分片 MiniDb,构造 8,000 个会话、80 个工作区,对比 base 5af9942 与本 PR 0b4ccec。每个进程使用独立数据目录和异版本 checkpoint,关键场景运行 3 轮取中位数;模拟 Desktop 的 2 页分组读取,以及重连时按每页 100 条拉取全部 8,000 个会话。

场景 Base PR
异版本索引 prepare 1,265 ms 0.4 ms
prepare 扫描 / 索引写入 1 次 / 8,000 条 0 / 0
首次分组读取(PR 包含冷文件扫描) 142 ms 820 ms
重连拉取 80 页 499 ms 111 ms(约 4.5×)
测试结束 RSS 394 MB 187 MB(约低 53%)

PR 的首次分组读取承担了一次真实文件扫描;连同 prepare 计算,首轮总耗时约 820 ms,base 的重建与首次分组约 1,407 ms。之后 30 秒复用窗口内,80 页重连拉取没有再次访问会话文件。

缓存过期且 scan.json 全部命中时,2 页分组读取约 326–348 ms,检查 16,000 次文件状态,未重新读取或解析 state.json;随后 80 页重连拉取约 65 ms。正常 schema 匹配时,80 页 MiniDb 查询仍约 500 ms,这条路径不在本 PR 改动范围内。已存在完整 MiniDb 时,PR 进程仍约 250–264 MB RSS,说明它停止继续重建,但不会消除打开已有索引的成本。

这是核心存储路径基准,未运行完整 Electron 双版本进程,也未模拟旧代际残留、锁冲突、长会话恢复和 WebSocket 重连风暴;结果验证 MiniDb 会话索引链路的性能改善,不将其等同于完整故障现场的 4 GB 堆峰值验证。

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.

@changeset-bot

changeset-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0b4ccec

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

@7Hanrui

7Hanrui commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T09:53:24.670578Z 0b4ccec Manual request
ℹ️ 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 16, 2026

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

commit: 0b4ccec

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0b4ccec321

ℹ️ 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".

Comment on lines +86 to +87
if (slot !== undefined && (!slot.settled || Date.now() < slot.reusableUntil)) {
return slot.promise;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid caching a rejected fallback scan

When an authoritative scan fails quickly—for example, an unguarded workspace mtime call rejects—this condition returns the already-rejected promise for every list or count request until the 30-second reuse deadline. Previously, a settled fallback scan was never reused, so a later request could recover immediately; clear the slot on rejection or reuse only successfully fulfilled scans.

Useful? React with 👍 / 👎.

"@moonshot-ai/kimi-code": patch
---

Keep session lists available when an older session index is present instead of rebuilding it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Disclose the loss of immediate fallback freshness

This entry only says mismatched indexes stop rebuilding, but the same change also makes degraded or preparing list/count reads reuse a settled scan for 30 seconds, so sessions written by another process are no longer immediately visible and there is no escape hatch. Name that lost behavior in the user-facing changeset as required for removed existing behavior.

AGENTS.md reference: AGENTS.md:L114-L116

Useful? React with 👍 / 👎.

Comment on lines +144 to +147
manifest !== undefined &&
manifest.schemaVersion !== SESSION_INDEX_SCHEMA_VERSION
) {
this.markSchemaMismatch();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Recheck the checkpoint schema after startup

When this process becomes ready before an older concurrent process publishes its checkpoint, doPrepare is not called again: both withReadModel and tick fetch the replacement manifest and use or reconcile its generation without checking schemaVersion. The targeted 2.5/2.6 startup order can therefore still make the fixed process query and mutate an incompatible index instead of switching to authoritative files; apply the mismatch transition whenever a checkpoint is fetched, not only during initial preparation.

AGENTS.md reference: AGENTS.md:L110-L112

Useful? React with 👍 / 👎.

manifest !== undefined &&
manifest.schemaVersion !== SESSION_INDEX_SCHEMA_VERSION
) {
this.markSchemaMismatch();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Stop mirror flushes while the index schema is mismatched

After this branch permanently degrades the read model, ordinary local metadata writes still enter SessionIndexMirror; its flushChunk reads the mismatched manifest, writes current-version rows and counters into sessionCollection(manifest.seq), and then clears the pending entries. In the mixed-version scenario this process therefore mutates the index it is meant to leave untouched and can expose incompatible records to the version that owns it; gate mirror flushes on the manifest schema while preserving pending updates.

AGENTS.md reference: AGENTS.md:L110-L112

Useful? React with 👍 / 👎.

@7Hanrui

7Hanrui commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0b4ccec321

ℹ️ 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".

Comment on lines +86 to +87
if (slot !== undefined && (!slot.settled || Date.now() < slot.reusableUntil)) {
return slot.promise;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep local writes overlaid for the cache lifetime

When a compatible read model is temporarily degraded and a local session create/update occurs after a fallback scan, the mirror initially keeps that change in pending(), but its 100 ms flush removes the pending entry; this branch then reuses the pre-write summaries for up to 30 seconds. Desktop, Web, and TUI session-list clients can therefore see their own new or updated session briefly and then see it disappear until the scan expires, whereas settled fallback scans were not reused before this change. Invalidate the cached scan when local mutations flush, or retain an equivalent overlay for the entire reuse window.

AGENTS.md reference: AGENTS.md:L110-L112

Useful? React with 👍 / 👎.

@7Sageer
7Sageer merged commit 192f8ab into release/2026-11-16 Nov 16, 2026
16 checks passed
@7Sageer
7Sageer deleted the fix/session-index-schema-mismatch-degrade branch November 16, 2026 09:54
@github-actions github-actions Bot mentioned this pull request Nov 16, 2026
wbxl2000 pushed a commit that referenced this pull request Nov 17, 2026
* fix: resume sessions with removed default models (#4982)

* fix(node-sdk): resume sessions with missing default model

* fix(cli): check auth for the resumed session model

* chore: add changeset for unavailable model recovery

* fix(agent-core-v2): stop rebuilding mismatched session indexes (#4980)

* fix(agent-core-v2): reject repeated model responses (#4977)

* fix(agent-core-v2): reject repeated model responses

* chore: add changeset for repeated model responses

---------

Co-authored-by: 7Sageer <[email protected]>

* fix(agent-core-v2): inline deferred tools without dynamic loading (#4971)

* fix(agent-core-v2): record restored task notifications without model requests (#4949) (#4989)

* fix(agent-core-v2): record restored task notifications without model requests

- queue restored task notifications as prompt-only turns after loop initialization
- preserve interrupted and queued turns during resume and fork
- complete reconciliation before initialization and reset return to keep Wire reads ordered
- run loop and container cleanup when reconciliation fails
- surface restored notification persistence failures

* chore: add changeset for restored task notifications

* fix(agent-core-v2): restore overflow recovery after LLM retries (#4950) (#4990)

* fix(agent-core-v2): restore step state after LLM retries

* test(agent-core-v2): scope retry event to overflow regression

* fix(config): allow sessions with invalid user config (#4951) (#4991)

* fix(agent-core-v2): tolerate config read failures for permission rules

* chore: add changeset for permission config recovery

* docs: document permission config recovery

* docs: qualify permission config recovery scope

* fix(config): surface read error diagnostics

* fix(config): allow sessions with invalid user config

* fix(config): preserve valid values and replay diagnostics

* fix(config): remove startup diagnostic replay

* fix(agent-core-v2): ignore non-user prompts during Tower wake (#4952) (#4992)

* fix(agent-core-v2): ignore non-user prompts during Tower wake

* chore: add Tower notification fix changeset

* fix(agent-core-v2): preserve slash prompts during Tower wake

* fix(agent-core-v2): route foreground shell events by origin (#4954) (#4994)

* fix(agent-core-v2): route foreground shell events by origin

* test(agent-core-v2): expect tool event origins in snapshots

* fix(klient): accept tool events from pre-origin hosts

* fix(agent-core-v2): run external Stop hooks after turn completion (#4956) (#4995)

* fix(agent-core-v2): run external Stop hooks after turn completion

* chore: add changeset for external Stop hooks

* docs: clarify post-turn Stop hook timing

* fix(agent-core-v2): wait for subagent quiescence (#4953) (#4993)

* fix(agent-core-v2): reject incomplete model streams (#5006)

* fix(agent-core-v2): preserve choice-level OpenAI token usage (#5017)

* fix(capability): confirm stopping WebBridge before Windows updates (#4999)

* fix(capability): confirm stopping WebBridge before Windows updates

* fix(capability): harden WebBridge update recovery

* fix(capability): restore WebBridge after startup failure

* fix(capability): wait for WebBridge process exit

* fix(capability): handle WebBridge confirmation edge cases

---------

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

* chore: sync release 2.6.3 back to main

* chore: finish release 2.6.3 backsync

* chore: sync node-sdk lockfile importer

* fix(sdk): preserve main core type dependencies

* fix: complete release 2.6.3 backsync

---------

Co-authored-by: Haozhe <[email protected]>
Co-authored-by: 7Sageer <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
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.

3 participants