Skip to content

fix(kap-server): turn the global search index off by default - #4516

Merged
sailist merged 1 commit into
mainfrom
fix/global-search-default-off
Oct 29, 2026
Merged

sailist merged 1 commit into
mainfrom
fix/global-search-default-off

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

全局全文搜索索引没有任何第一方消费者,却在每次启动时后台扫描全部历史会话,重度用户的搜索 worker 因此 OOM 并带崩整个进程;改为默认关闭,会话索引保持开启。

Bug Reproduction Steps

  1. 使用桌面端 1.2.0 或 kimi web,~/.kimi-code/sessions 下积累数 GB 的 wire.jsonl(用户现场:3239 个文件共 10.8 GiB,其中 10 个超过 50 MiB,最大 400 MiB)。
  2. 正常启动,不做任何操作。
  3. 约 1 到 3 分钟后进程以 SIGABRT 退出;macOS 崩溃报告的触发线程为 kimi-search-worker,栈顶为 node::OOMErrorHandler,用 Electron 43.1.1 的符号重新解析后为 Runtime_MapGrow → OrderedHashMap::Rehash → HeapAllocator::AllocateRawSlowPath → V8::FatalProcessOutOfMemory("CALL_AND_RETRY_LAST")。
  4. 重启后同样的同步从持久化的 offset 继续,几十秒内再次崩溃,形成循环。

Root Cause

搜索 worker 以 resourceLimits.maxOldGenerationSizeMb = 1024 创建,minidb 的全文索引把写缓冲 delta(term → Map)、deltaDocs 和整个词典常驻内存;同步每轮摄入最多 64 MiB、轮间只去抖 2 秒,而能清空 delta 的 generation 构建最多每 60 秒踢一次、失败后退避 5 分钟,delta 相对 1 GB 实际无界。Node 对 worker 的 near-heap-limit 回调只放宽 16 MiB,一次装不下的大分配(百万级条目的 Map 扩容)直接走 CALL_AND_RETRY_LAST 致命路径,abort() 掉整个进程。已用同版本 Electron 复现:worker 限 256 MiB、数字键 Map 增长,进程以 134 退出,堆仅用到约 114 MiB。

本 PR 是绕过而非根本修复:既然没有消费者,默认不启动索引;内存收敛(背压、词典表示)与进程隔离另行处理。

Code Changes

 GlobalSearchService
   constructor
     requestSync
       runSync
+        enabled()            # await config.ready; [database] global_search ?? false
+          -> false: log once, return   (no backend, no sync, search-index untouched)
         ensureBackend
+          -> throws GlobalSearchError('index_unavailable', how-to-enable) when off
   search
     normalizeQuery           # invalid bodies still fail with 40001
+    enabled() -> false: throw GlobalSearchError('index_unavailable', how-to-enable)
   status
+    enabled() -> false: { sessions: 0, ..., lifecycle: { state: 'stopped', detail } }
   dispose
-    await backendPromise
+    await backendPromise?.catch(() => undefined)
 packages/agent-core-v2/src/persistence/configSection.ts
+  globalSearch: boolean            # TOML global_search, env KIMI_CODE_GLOBAL_SEARCH, default false
+  databaseGlobalSearchEnabled()
 packages/agent-core-v2/docs/config-manifest.toml     # regenerated
 packages/kap-server/src/search/searchService.ts
 packages/kap-server/test/search/searchService.test.ts
 packages/kap-server/test/search/searchRoute.test.ts
 docs/{en,zh}/configuration/config-files.md           # [database] table
 docs/{en,zh}/configuration/env-vars.md               # KIMI_CODE_GLOBAL_SEARCH
 docs/{en,zh}/guides/web.md                           # global search row needs the switch
 .changeset/global-search-off-by-default.md

原有 [database] search 键的语义不变:它只决定开启后索引跑在 worker 线程还是服务器进程内,从来不是关闭开关。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
启动后后台扫描并索引全部历史会话 总是进行,SearchService 构造即 requestSync() 仅当 global_search = true 所有默认配置用户:TUI、kimi web、desktop 与 web,三者共用 kap-server [database] global_search = true 或 KIMI_CODE_GLOBAL_SEARCH=1
POST /api/v1/search 返回命中或 building 页 默认返回 50001,msg 说明如何开启;请求体校验仍先返回 40001 调用该接口的客户端。code-app 仓已核对:desktop 与 web 的"搜索会话"对话框只在前端按标题过滤,不调用此接口;SDK、ACP、VS Code 也不调用 同上
~/.kimi-code/search-index 目录与 db.lock 启动即创建并打开 默认不触碰;已有目录原样保留 依赖该目录存在的外部脚本(无已知) 同上
线程 kimi-search-worker 启动即创建 默认不创建 同第一行 同上
搜索服务 status() 打开后端并返回索引统计 默认返回零统计,lifecycle.state = 'stopped' 并带说明 无路由消费,仅内部接口 同上
启动日志 global search: worker started 等 默认只打一条 global search: global search is disabled; ... 读日志排障的人 同上
[database] search、search_sync_session_cap、search_sync_debounce_ms 控制 worker/inline 与同步节奏 语义不变,仅在开启后生效 已设置这些键的用户 不需要
CLI 启动时从 SEA 提取 search-worker 资产 提取 不变,仍提取 不适用 不适用

受影响模块与测试覆盖:

  • packages/kap-server/src/search/searchService.ts:test/search/searchService.test.ts 原有套件通过 globalSearch: true 显式开启,钉住开启人群的旧行为;新增 global search switched off 套件覆盖默认关闭与显式 false 两种配置下的 status()、search()、reindex()、lifecycleReport() 与索引目录不存在。
  • packages/kap-server/src/routes/search.ts(未改):test/search/searchRoute.test.ts 原有套件通过 env 开启;新增套件钉住默认关闭时 /api/v1/search 返回 50001、GET /api/v1/sessions 正常、不生成 search-index。
  • packages/agent-core-v2/src/persistence/configSection.ts:test/app/config/configManifest.test.ts 钉住 manifest 中的新键与 env 绑定。

验证:agent-core-v2 与 kap-server 类型检查通过;kap-server 全量 1431 个用例通过,另有 plugins.test.ts 远端回退与 telemetry.test.ts 云端 appender 两个用例在本机环境于未改动的基线上同样失败,与本 PR 无关。

合入后需要在 code-app 仓 bump submodule 并附处理过的 changeset。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve). 无关联 issue,来源于用户崩溃报告分析。
  • 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.

Copilot AI balanced review requested due to automatic review settings October 29, 2026 08:54
@changeset-bot

changeset-bot Bot commented Oct 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 572223f

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

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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 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-29T08:57:01.219990Z 572223f PR opened
🔒 Security Review ✅ Completed 2026-10-29T09:04:26.104560Z 572223f 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 29, 2026

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

commit: 572223f

@sailist
sailist merged commit be89803 into main Oct 29, 2026
17 checks passed
@sailist
sailist deleted the fix/global-search-default-off branch October 29, 2026 09:04
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
The behaviour of MoonshotAI#4421 has been in the requester since 94397aa, but its
suite still asserted only that the lowered system message lost its
`tools` field, which a message kept in place would also satisfy. Assert
instead that no system message survives and the history keeps its
length, and rename the case after what it protects.

Also settle ten ledger rows that were already implemented here: MoonshotAI#3733,
MoonshotAI#4413, MoonshotAI#4418, MoonshotAI#4421, MoonshotAI#4435, MoonshotAI#4460, MoonshotAI#4465, MoonshotAI#4516, MoonshotAI#4523, MoonshotAI#4532 and the
MoonshotAI#4558 test calibration, each with a probe over the suite that pins it.
Two rows carried a probe without a test-name selector (MoonshotAI#4557, MoonshotAI#4558),
which aborted `--verify` for every other row; MoonshotAI#4557 is a changelog sync
and now carries a manual probe.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 5, 2026
Ports the docs half of upstream MoonshotAI/kimi-code 8b6a003 (MoonshotAI#4673): the
2.5.0 section from the upstream release notes lands in both changelogs, en
and zh, word for word.

Every feature the section names is in this tree - busy /model and /effort
(MoonshotAI#4621), merged project-level skills (MoonshotAI#4520), remote sessions in SSH, Docker
and custom-command environments (MoonshotAI#4062), [workspace] require_trust (MoonshotAI#4517),
web search off by default (MoonshotAI#4516), and the subagent model pool (MoonshotAI#4418) - so
the notes describe what the fork actually does.

Deliberately not touched: apps/kimi-code/CHANGELOG.md still tops out at 2.4.1
and package.json still says 2.4.1. That file is generated by `changeset
version`, this tree has 377 pending changesets, and AGENTS.md forbids running
version or publish from this repository, so the entry is written on the docs
side only. Whoever next cuts a release here should reconcile this section
with what changeset version generates rather than re-deriving it by hand.
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