Repository navigation
fix(agent-core-v2): keep archived sessions out of session lists before the index flush - #4540
Conversation
…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.
There was a problem hiding this comment.
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 detectedLatest commit: 4a1633e 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 SummaryThis 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: |
|
@codex review |
There was a problem hiding this comment.
💡 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.
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
Requirement or Bug
会话归档后,在会话索引镜像落盘之前(镜像每 100ms 落盘一次),
listRecent的默认查询(不含归档)仍会返回这个会话;分页时还会把已被待写版本取代的旧行按旧位置带出来,产生过期或重复的条目。客户端在归档后立即查询会话列表时会间歇命中。修复后列表查询与count保持一致:镜像里有待写版本的会话,一律以待写版本为准。Bug Reproduction Steps
POST /api/v1/sessions/{id}:archive归档一个会话。GET /api/v1/sessions?include_archive=false。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剔除这些会话的旧行,再合并满足条件的待写版本;剔除和合并使用同一份镜像快照。只有取回的窗口里真的出现了被剔除的旧行、且存储层还有更多行时,才多取一次补足。Set剔除id: { $nin }$nin对每一行做Array.includes,查询代价变成「扫描行数 × 待写数」;镜像写入持续失败时待写最多 1 万条,会话列表会明显卡顿(见 review)fetchLimit + 待写数fetchLimitwindowedPage的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
include_archive=false/ 默认)kimi web的会话列表、SDKincludeArchived: true、archived_only=true、count、按sessionId查询fetchLimit + 待写数;不分页的查询仍只查一次受影响模块:
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 + 待写数。验证
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-serverplugins.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的实际开销会更高):$nin分页50 / 全量积压的旧行落在第一页窗口里时,本实现的分页上限是一次全量扫描;不在窗口里时开销与修改前相同。镜像为空时(Q=0)两种实现与 main 一致。
Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.