Skip to content

fix(agent-core-v2): keep archived sessions out of session lists before the index flush - #4540

Merged
RealKai42 merged 3 commits into
mainfrom
fix/session-list-pending-archived
Oct 30, 2026
Merged

RealKai42 merged 3 commits into
mainfrom
fix/session-list-pending-archived

Conversation

@RealKai42

@RealKai42 RealKai42 commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

会话归档后,在会话索引镜像落盘之前(镜像每 100ms 落盘一次),listRecent 的默认查询(不含归档)仍会返回这个会话;分页时还会把已被待写版本取代的旧行按旧位置带出来,产生过期或重复的条目。客户端在归档后立即查询会话列表时会间歇命中。修复后列表查询与 count 保持一致:镜像里有待写版本的会话,一律以待写版本为准。

Bug Reproduction Steps

  1. 调用 POST /api/v1/sessions/{id}:archive 归档一个会话。
  2. 紧接着(约 100ms 内)请求 GET /api/v1/sessions?include_archive=false。
  3. 结果仍包含刚归档的会话;而同一时刻 GET /api/v1/sessions?archived_only=true 已经把它列为已归档。

单元层面的最小复现见新增的两个测试:修复前分别返回 ['b', 'a'] 和 ['s4', 's3', 's2', 's1']。

Root Cause

FileSessionIndex.listRecentFromReadModel 先从读模型(minidb)分页读取已落盘的摘要,再由 mergePending 合并镜像里尚未落盘的摘要。mergePending 先按查询条件过滤待写摘要,再把读模型返回的行按 id 补进结果:

  • 待写版本不再满足查询条件时(例如会话已归档,而查询排除归档)会被过滤掉,于是这个会话在读模型里的旧行(仍是未归档)被补回结果。
  • 分页时同理:被待写版本取代的旧行会按旧位置出现在后续页。

count 已经以待写版本为准(减去旧行的权重、加上待写版本的权重),只有列表查询漏了这一点。这是根本修复,不是绕过。

Code Changes

核心思路:镜像里的待写摘要是这些会话的权威状态。列表查询从读模型取回数据后,用 Set 剔除这些会话的旧行,再合并满足条件的待写版本;剔除和合并使用同一份镜像快照。只有取回的窗口里真的出现了被剔除的旧行、且存储层还有更多行时,才多取一次补足。

 listRecentFromReadModel(query)
   cursor = resolveCursor(query)
+  queued = mirror.pending()                      # one snapshot per call
+  queuedIds = Set(ids of queued)
   filter = baseFilter(query) + cursor.filter     # unchanged, no id list
-  page = windowedPage(store.page(filter))
+  page = windowedPage(fetchVisible(store.page(filter)))
-  return mergePending(page, query)               # re-read mirror.pending()
+  return mergePending(page, query, queued)

+fetchVisible(fetch, fetchLimit)
+  rows = fetch(fetchLimit)
+  if no row is queued or the store ran out of rows
+    return rows without queued ids
+  rows = fetch(fetchLimit + queuedIds.size)     # at most queuedIds.size rows can drop
+  return the first fetchLimit rows without queued ids
决策 选择 没选 理由
在哪里剔除旧行 取回后在 JS 里用 Set 剔除 在读模型查询里传 id: { $nin } minidb 的 $nin 对每一行做 Array.includes,查询代价变成「扫描行数 × 待写数」;镜像写入持续失败时待写最多 1 万条,会话列表会明显卡顿(见 review)
剔除后如何保证分页不被截断 窗口里出现被剔除的行时,多取一次 fetchLimit + 待写数 只取 fetchLimit 被剔除的行最多只有待写数那么多条,多取这些就足以凑满;windowedPage 的 limit + 1 探测和会话列表路由遇空页即停的 collect() 因此保持正确
何时多取 按需:第一次取回的窗口里有被剔除的行、且存储层还有更多行 每次都多取 ClusterDb 对每个分片都按 limit 取数再全局排序,总是多取会让分页代价随待写数上升;按需多取时,常见情况与原来完全相同
镜像快照 每次查询只读一次,剔除和合并共用 合并时重新读取 取数期间如果恰好落盘,重新读取会出现「旧行已被剔除、待写版本又已不在镜像里」,会话会从结果里消失

另一种做法是在 minidb 内部把 $in / $nin 改成 Set 判断,收益相近,但要改共享存储引擎的同步、异步和快速路径三处,本 PR 不做。

阅读指南:packages/agent-core-v2/src/app/sessionIndex/sessionIndexService.ts 的 listRecentFromReadModel(含 fetchVisible)与 mergePending 是全部逻辑改动;测试在 packages/agent-core-v2/test/app/sessionIndex/sessionIndex.test.ts,紧挨着已有的 count folds the mirror queue in before the flush lands。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
归档后、镜像落盘前查询不含归档的会话列表(include_archive=false / 默认) 返回刚归档的会话(读模型旧行) 不返回 无人依赖,旧行为是 bug。受影响的是所有列表消费方:desktop 与 web 的会话侧边栏(客户端有归档墓碑兜底,平时看不到)、TUI 与 kimi web 的会话列表、SDK 不需要
镜像里有待写版本(改名、更新时间变化、归档)的会话在分页列表中 旧行可能按旧位置出现在后续页,过期或与前一页重复 每个会话只按待写版本出现一次 无人依赖 不需要
includeArchived: true、archived_only=true、count、按 sessionId 查询 以待写版本为准 不变 — —
镜像为空时的列表查询 每次取数查询一次读模型 不变(同一个查询,不做额外过滤) — —
镜像非空时的列表查询代价 每次取数查询一次读模型 取回的窗口里出现待写会话的旧行、且还有更多行时,再查询一次 fetchLimit + 待写数;不分页的查询仍只查一次 无人依赖;平时多一次小查询(约 1ms),写入持续失败时上限为一次全量扫描 不需要

受影响模块:packages/agent-core-v2 的会话索引。kap-server 的会话列表路由和 workspace sessions 服务都经由它,本身不需要改动;没有改动任何契约文件。

测试覆盖:

  • listRecent hides a session archived in the mirror queue before the flush lands:覆盖第 1、3 行,并验证落盘前后结果一致。
  • pages past stored rows whose queued summary no longer matches the query:覆盖第 2 行,钉住不会因整页旧行被剔除而提前截断翻页。
  • lists around a mirror queue that cannot flush without handing its ids to the store:镜像写入持续失败、积压 20 条时,分页与不分页的结果都正确,且交给读模型的过滤条件里没有 id 列表(对应 review 中的问题)。
  • drops a child session archived in the mirror queue from childOf listings:覆盖 childOf 这条取数路径。
  • pages same-millisecond ties around stored rows the mirror queue superseded:覆盖 windowedPage 的同毫秒并列补齐路径。
  • widens a page fetch only when stale rows of queued sessions fall inside it:钉住第 5 行,旧行不在窗口里时只查一次,在窗口里时才多查一次 fetchLimit + 待写数。
  • 已有的 1k / 10k / 50k 规模基线用例(镜像为空)不变并通过,即镜像为空时的查询次数与读取行数与原来一致。
验证
  • pnpm lint:0 error(含零注释检查);warning 数与修改前相同。

  • packages/agent-core-v2:tsc --noEmit 通过;会话索引与镜像的 62 个用例全部通过,包括 1k / 10k / 50k 规模基线。

  • 全量 vitest(agent-core-v2 7753 通过,kap-server 1440 通过)。本机另有几个失败,都与本 PR 无关:tool.test.ts 的前台子 agent 超时用例和 kap-server plugins.test.ts 的远端不可达回退用例在不含本改动时同样失败;mcpCore/oauth/service.test.ts 与 src/human/test/utils/watch.test.ts 是时序敏感用例,每次失败的用例都不同,不含本改动时同样失败。CI 全部通过。

  • 新增的 6 个用例与四个实现对照:本实现全部通过;「每次都多取」的实现只有 widens a page fetch only when… 失败;$nin 实现在 lists around a mirror queue… 与 widens a page fetch only when… 上失败;修复前的 main 6 个全部失败。

  • 基准(真实 FileSessionIndex + minidb,写入持续失败,测试 id 较短,$nin 的实际开销会更高):

    场景 本实现 分页50 / 全量 $nin 分页50 / 全量
    1 万会话,积压最旧的 5000 条 2.1 / 16.3 ms 7.2 / 60.6 ms
    5 万会话,积压最旧的 5000 条 3.1 / 89.2 ms 7.5 / 302.3 ms
    1 万会话,积压最新的 5000 条 16.8 / 15.5 ms 28.0 / 61.9 ms
    5 万会话,积压最新的 5000 条 81.1 / 90.6 ms 32.2 / 394.2 ms
    1 万会话,积压最新的 1 / 5 / 20 条 3.1 / 3.0 / 3.2 ms(分页) —

    积压的旧行落在第一页窗口里时,本实现的分页上限是一次全量扫描;不在窗口里时开销与修改前相同。镜像为空时(Q=0)两种实现与 main 一致。

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.

…e the index flush

listRecent filtered the session index mirror by the query before merging it,
so a queued summary that no longer matched (for example a session that was just
archived while the query excludes archived sessions) dropped out and its stale
stored row was added back. Exclude queued ids from the read-model query and
merge the same mirror snapshot, matching how count() already treats the queue.
Copilot AI balanced review requested due to automatic review settings October 30, 2026 03:41

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@changeset-bot

changeset-bot Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4a1633e

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 Oct 30, 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-10-30T05:59:42.937756Z 4a1633e Manual request
🔒 Security Review ✅ Completed 2026-10-30T03:44:10.272926Z 464d266 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 Oct 30, 2026 •

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

commit: 4a1633e

@wbxl2000

Copy link
Copy Markdown
Collaborator

@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: 464d266c06

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

Excluding queued sessions with `id: { $nin }` made minidb compare every scanned
row against the whole mirror queue, so a queue that cannot flush (up to 10,000
entries) turned session listing into rows x queue work. Drop queued ids with a
Set after the fetch instead, and refetch with room for the dropped rows only
when stale rows actually fell inside a full window. An empty queue keeps the
original single query, and unbounded listings stay single-pass.
@wbxl2000

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 4a1633eaca

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

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.

4 participants