Repository navigation
test(kap-server): wait for search index readiness in route tests - #4909
Conversation
|
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: |
Port MoonshotAI#4901, MoonshotAI#4909 and MoonshotAI#4911: complete the task before asserting a zero-timeout wait sees the completed snapshot, restore the instructions reload spy after the failure assertion, poll the search index until it is ready before asserting the page, and run remote-session git probes through a host process that refuses gh. Record MoonshotAI#4840 as equivalent (the refresh path already rebuilds from the effective binding here) and pin it with a regression case, record MoonshotAI#4883 as ported with the compaction profile already present, and record MoonshotAI#4830 as blocked on the absent loop requester/request-attribution layering. Clear the generated-bundle, release-sync and working-notes rows with their applicable verdicts.
Requirement or Bug
packages/kap-server/test/search/searchRoute.test.ts在 CI 分片运行时偶发失败(例如searches across sessions and returns the wire-shaped page在user与assistant命中均已通过后,断言role === 'title'得到false)。本 PR 修复该路由测试的索引就绪轮询条件,使全文与字面量两条路由测试均在首轮后台索引真正完成后再执行断言。Bug Reproduction Steps
KIMI_CODE_GLOBAL_SEARCH=1)且预置单会话s1(含wire.jsonl与state.json标题)的kap-server。syncSession刚完成wire.jsonl批次写入但尚未写入s1/$title的异步窗口内调用POST /api/v1/search查询{ query: "苹果" }(本地可在SearchIndexCore.syncSession写入titleKey前注入150ms延迟稳定复现)。body.data.items.length > 0提前退出轮询,随后在第164行expect(body!.data.items.some((h) => h.role === 'title')).toBe(true)报AssertionError: expected false to be true。Root Cause
根本修复。
startServer()(packages/kap-server/src/start.ts)初始化IGlobalSearchService时,构造函数异步触发后台requestSync(),不阻塞服务启动返回;搜索 Worker(packages/kap-server/src/search/worker/entry.ts)并发处理sync与search请求,两者在SearchIndexCore的await边界交错执行。SearchIndexCore.syncSession()(packages/kap-server/src/search/indexCore.ts)对单个会话分三步写入MiniDb:await db.set(metaKey, meta)写入会话元数据\0meta\session\s1(含目录身份与标题);syncWireFile(),通过await db.batch(ops)将user与assistant两条MessageDoc同步写入内存与全文索引,随后等待 WAL 落盘及await handle.close();await db.set(titleKey, doc)写入s1/$title(role: 'title'的TitleDoc),并在整轮runSync()写完STATS_KEY后将this.fullSyncDone置为true(index_state.state从'building'转为'ready')。searches across sessions and returns the wire-shaped page与serves literal mode through the wire中均以if (body.data.items.length > 0) break;作为轮询结束条件。当查询恰好落在MessageDoc已入索引而TitleDoc尚未写入的异步 I/O 窗口时,接口返回[user, assistant](items.length === 2、index_state.state === 'building'),导致轮询在半成品索引状态提前退出。sequenceDiagram participant Test as searchRoute.test.ts participant Worker as SearchWorker / SearchIndexCore participant DB as MiniDb Worker->>DB: set("\0meta\session\s1", meta) Worker->>DB: batch([user MessageDoc, assistant MessageDoc, fileMeta]) Note over Worker: await WAL flush / handle.close() Test->>Worker: POST /api/v1/search { query: "苹果" } Worker->>DB: searchBoundedAsync("body", "苹果") DB-->>Worker: [user, assistant] (fullSyncDone === false) Worker-->>Test: items=[user, assistant], index_state.state="building" Note over Test: 旧逻辑见 items.length > 0 提前 break -> 缺 title 失败 Worker->>DB: set("s1/$title", TitleDoc) Worker->>DB: set("\0meta\stats", stats) + fullSyncDone = true因为
index_state.state === 'building'阶段按设计渐进式暴露已提交批次,所以在测试侧等待首轮索引进入'ready'并随后发起一次稳定查询,才能在因果顺序上保证runBounded读取到完整提交的会话文档与标题文档。Code Changes
在
packages/kap-server/test/search/searchRoute.test.ts中引入postSearchWhenReady辅助函数,替换同一describe块内两处基于items.length > 0的早退轮询,并将index_state.state断言收紧为'ready':describe('server-v2 /api/v1/search') + postSearchWhenReady(queryBody) + poll postSearch(queryBody) until probe.data.index_state.state === 'ready' + return await postSearch(queryBody) it('searches across sessions and returns the wire-shaped page') - poll postSearch({ query: '苹果' }) until body.data.items.length > 0 + body = await postSearchWhenReady({ query: '苹果' }) - expect(['building', 'ready', 'readonly']).toContain(body.data.index_state.state) + expect(body.data.index_state.state).toBe('ready') it('serves literal mode through the wire') - poll postSearch({ query: '的价格', mode: 'literal' }) until body.data.items.length > 0 + body = await postSearchWhenReady({ query: '的价格', mode: 'literal' }) + expect(body.data.index_state.state).toBe('ready')因为
SearchIndexCore.search()先异步执行runBounded(serveDb)再调用readIndexView()读取fullSyncDone,所以在探测请求观测到index_state.state === 'ready'后再补发一次查询,可以确保最终断言所用的检索过程严格发生在fullSyncDone = true之后,同时保留probe.code !== 0时的立即失败行为。Behavior Changes and Affected Users
None。本 PR 仅修改测试文件
packages/kap-server/test/search/searchRoute.test.ts;GlobalSearchService、SearchIndexCore、POST /api/v1/search路由契约、配置默认值及 CLI 发布产物均未改动(对 TUI、kimi -pprint 模式、desktop 与 web、VS Code、ACP、SDK 用户无任何行为变化)。packages/kap-server/test/search/searchRoute.test.tssearches across sessions and returns the wire-shaped page:覆盖全文检索路由在首轮索引就绪后的user、assistant、title三类命中与响应结构。serves literal mode through the wire:覆盖字面量检索模式单独运行及整文件运行时的索引就绪等待与精确子串匹配。验证明细
SearchIndexCore.syncSession写入titleKey前临时注入150ms延迟模拟慢盘窗口,验证后还原):pnpm -C packages/kap-server exec vitest run test/search/searchRoute.test.ts稳定复现AssertionError: expected false to be true(test/search/searchRoute.test.ts:164:62)。150ms注入延迟下6/6全部通过。pnpm -C packages/kap-server exec vitest run test/search/searchRoute.test.ts -t "serves literal mode through the wire":通过。pnpm -C packages/kap-server exec vitest run test/search/searchRoute.test.ts:10/10轮(共60/60次用例执行)全部通过。pnpm run typecheck:0 errors。pnpm run lint --quiet(全仓oxlint --type-aware):0 errors。pnpm run sherif:No issues found。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.Requested by 刘金 via Mira.