Repository navigation
Commit a7bf18f
feat(service-knowledge): build a never-built source after boot; os knowledge rebuild and status (#37439)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ83sWXVVPC33q8uBsTDHaebw)_
Fixes #37099
Clause-②: yes
Metadata-protocol: none
## 维护者速读
- 用户看到的:知识库数据源第一次部署、或换了嵌入模型 /
索引配方之后,服务启动后会在后台自动把它建好索引,不用再等每条记录被改一遍才能搜到;管理员也可以用命令行手动重建某个源、查看每个源的索引状态和进度。
- 为什么做:属于「知识检索平台化」专题,让知识源开箱即可检索;以前一个从没建过索引的源,搜索永远是空的。
-
出错会怎样:建索引会把匹配到的内容发给嵌入服务商并产生费用;默认的内存存储每次重启都会整源重建、每个节点各建一次,只有配置了共享集群锁才是一个节点建;可以用插件开关关掉开机自动建,回退就是撤掉这个改动。
- 要你做什么:无。
## What changes
**First build after boot** (`@objectstack/service-knowledge`)
- `KnowledgeService.readIndexState(source)` reads ADR-0161 K7's table
for one source: the adapter holds nothing, a different fingerprint, a
different recipe hash, no recipe hash, current, or (an adapter without
`indexStatus`) whether `listDocuments` lists anything. Each reading says
whether a build is due.
- `KnowledgeService.buildSourcesAtBoot()` runs from the plugin's
`kernel:bootstrapped` hook, after the embedder is provided, without
holding up the boot. It walks the sources one at a time and builds each
one a build is due for. A source with `refresh.onRecordChange: false`
(an external indexer owns it) is skipped. A state it cannot read, or an
adapter that cannot say, is logged at warn and left alone; the operator
rebuild is the way for those.
- **One node builds.** `rebuildSource` takes the cluster lock
`knowledge:build:SOURCE` (`ILock`, 60 s lease renewed every 20 s,
`waitMs: 0`). A node that finds the lock held records `not-run` and
builds nothing. A node that gets the lock re-reads the state first, so a
node arriving after another finished builds nothing either. If the lock
service errors, no build starts and the node warns. Without a `cluster`
service (single process) the build runs without a lock.
- Each build logs one cost line at its start (every row the source's
`where` matches is read, and its content fields are sent to the
embedding provider, which bills for them), progress at most every 10 s,
and its totals at the end (info) or why it did not finish (warn).
- New plugin option `buildAtBoot` (default `true`). `false` turns the
boot pass off; the operator rebuild still works.
**Operator rebuild**
- Two actions on the `knowledge` settings namespace, beside `test`:
`rebuild` (`{ source }`) starts a build and answers at once;
`index_status` (`{ source? }`) answers each source's index reading and
the last build record on this node, with its progress. Both go through
the settings action door, so both need `manage_platform_settings`. A
second rebuild of a source already building on this node is refused.
- `os knowledge rebuild SOURCE` starts one and follows it to its end by
polling `index_status` (`--no-wait` to return at once, `--interval`). It
exits 1 when the build failed or did not run, or when the answer comes
from a node that is not running this build.
- `os knowledge status [source]` prints each source, its index state and
its last build on the node that answered.
**Docs**: `content/docs/ai/knowledge-rag.mdx` gains "The first build,
and rebuilding a source": the K7 table, what one build costs (a warn
callout), how to turn the boot pass off, and the two commands.
**Rework round (seat L1 REWORK, contract record PASS with should-fix
findings)**
- **S3, the admin gate is pinned.**
`knowledge-settings-contribution.test.ts` gains "the rebuild and
index_status actions are administrator-only, at the HTTP door": a real
`LiteKernel` with `KnowledgeServicePlugin` and `SettingsServicePlugin`,
driven through `registerSettingsRoutes` and the real
`SettingsService.runAction`. A signed-in caller without
`manage_platform_settings` (an organization member holding `setup.read`
/ `setup.write`, or one with no capability) gets `403
SETTINGS_FORBIDDEN` for `rebuild` and `index_status`; an unauthenticated
one gets `401 UNAUTHENTICATED`. No refusal names the source (id or
label), and no handler method on the service (`listSources`,
`getSource`, `readIndexState`, `rebuildSource`, `buildRecord`) is
called. A control shows the same door lets an administrator through and
then names the source, and the in-process `runAction` with an enforced
member context rejects with `SETTINGS_FORBIDDEN`. The gate itself is
unchanged; it still lives in the `knowledge` manifest's
`writePermission`, and relaxing it now turns these pins red (ablation
below).
- **S4, the cost callout** in `content/docs/ai/knowledge-rag.mdx` now
says the default `memory` adapter holds nothing after a restart, so with
it every boot is a first build that re-embeds the whole source on every
node; and that "only one node builds a source" holds only with a shared
cluster lock driver, because the default `memory` cluster driver's lock
is a per-process mutex. The paragraph above the callout now qualifies
its "only one builds" sentence the same way and links the cluster page.
The changeset says both.
- **S6, the CLI reference.** `content/docs/deployment/cli.mdx` gains a
`### Knowledge` section after `### MCP`, in the shape of the other
command groups (overview table, one `####` per command with examples, a
flag table): `os knowledge status` and `os knowledge rebuild`, their
exit codes, the admin-only door, and the per-node build record.
- **Nits fixed:** N2 (`os knowledge rebuild --no-wait` now prints that
it reports no outcome and points at `os knowledge status`; the flag help
and the CLI reference say so), N3 (`buildSourcesAtBoot` lists the
sources inside a `try` and resolves with a warn when it cannot; the
plugin's `void` call carries a `.catch`), N5 (an adapter reporting `{
building: true }` with no active fingerprint, a first build that never
completed, now reads `nothing` with its own sentence instead of
`new-fingerprint`). N1 (the optional `protocol/knowledge.mdx` pointer)
stays an accepted deviation.
- **Carded, not fixed here:** S1 (a lease that lapses mid-build only
warns and the walk goes on unfenced; a build whose embed call never
returns renews forever) is #37465. S2 (build records live in memory per
node, so behind a load balancer `os knowledge rebuild` can exit 1 on a
poll that lands elsewhere) is #37466. S5 (retiring the field gate's
`title` arm) is #37467.
## Why
A source that was never built had an empty index until each of its rows
was written again; search found nothing and nothing said why. After an
embedder or recipe change the index stayed stale the same way. ADR-0161
K7 says what starts a rebuild; nothing ran it.
## Tests
-
`packages/services/service-knowledge/src/__tests__/knowledge-build.test.ts`
(20 tests): every `readIndexState` reading; a fresh source is built at
boot and search finds its rows; a built source is not rebuilt; two nodes
sharing one lock build once (one `done`, one `not-run`, lock released);
a node arriving late re-reads and builds nothing; a lock error starts
nothing and warns; `onRecordChange: false` is skipped; a failed build is
logged at warn and stays due; the `rebuild` and `index_status` actions
(progress, result, an operator rebuild skipping unchanged documents, a
running build refused, bad payloads refused); the plugin's
`kernel:bootstrapped` pass and `buildAtBoot: false`.
- `packages/cli/src/commands/knowledge/knowledge.test.ts` (7 tests):
both commands against bodies written by `sendOk` / `sendError`: the
route and payload, following a build to its end, exit 1 on a failed
build, on a server refusal, and on an answer from a node not running
this build; `--no-wait`.
- Ablation: replacing `lock.acquire` with an always-granted handle fails
the two-node and lock-unreachable tests.
- Rework round: `knowledge-settings-contribution.test.ts` +10 tests (the
admin-gate pins above); `knowledge-build.test.ts` +2 (N3: a listing that
throws resolves and warns; N5: `{ building: true }` reads `nothing`);
`knowledge.test.ts`: the `--no-wait` case now also asserts the line
naming `os knowledge status` (N2).
- Rework ablations, each mutated through `scripts/ablation-replace.mjs`
(landing proved on disk, restore proved `git diff HEAD` empty):
- S3: the `knowledge` manifest's `writePermission` set to `setup.write`
in `@objectstack/service-settings`, package rebuilt (the pins read its
`dist/`): the four member cases, the admin control and the in-process
case turn red (6 failed, 4 passed); restored and rebuilt, 10/10 green,
and the built manifest reads `writePermission:
"manage_platform_settings"` again.
- N5: the new branch disabled: its test red. N3: the listing failure
rethrown: its test red. N2: the log line deleted: the `--no-wait` test
red. All three green again after restore.
## Acceptance notes
- **The `title` arm of the field gate is not retired here.** Per the
11-25 14:55Z comment it can go once a rebuild has run, but it is a
security gate and retiring it needs proof per source at search time that
the index was built under the current recipe. That is its own row,
#37467; this PR only makes the rebuild happen.
- **Build records are per node.** `index_status` reports the build the
answering node ran. Behind a load balancer, `os knowledge status` may
reach a node that is not building; `os knowledge rebuild` says so and
exits 1 rather than guessing. The index state itself is read from the
adapter and is the same on every node. Sharing the record, or letting
the CLI fall back to the index state, is #37466.
- **No Setup page button.** The rebuild entry is the CLI (and the action
route it calls). A button on the Knowledge settings page is a separate
UI row if wanted.
- The CLI reference `content/docs/deployment/cli.mdx` now lists both
commands (S6). `content/docs/frontend/cli.mdx` is not updated: it is
outside this card's file surface.
- A boot build runs sequentially per source and in the background; boot
does not wait for it.
- Lease fencing (stop the walk once the lease is lost, bound a build
whose embed call never returns) is #37465.
## Verification
- `service-knowledge`: 14 test files / 436 tests pass; `tsc --noEmit`
and `check:test-typecheck` pass.
- CLI: the knowledge tests and the vitest tier partition test pass (29
tests). `eslint --no-inline-config` on every changed file passes.
- CLI `tsc --noEmit`: **not measured locally**. This container has no
built `dist/` for the CLI's workspace dependencies (563 TS2307-family
errors, none in a knowledge file); CI owns it.
- Real kernel run: two `LiteKernel`s sharing one `MemoryLock` over a
250-row source. Boot returned in 9 ms without waiting for the build.
Node A: `done`, 250 indexed; node B: `not-run` ("another node holds the
build"); `beginBuild` called once; search found row `r0007`; the state
afterwards read `current`. A non-admin
`runAction('knowledge','rebuild')` was refused ("requires the
'manage_platform_settings' capability"); an admin rebuild then showed
`done` with 250 skipped. The log carried the cost line.
- Derived gates: the local run's gates exit 0 except six that need a
full build or full history (`check:skill-examples`,
`check:dual-build-cjs-loads`, `check:i18n`, `check:i18n-coverage`,
`check:i18n-walk-parity`, `check-plugin-teardown-shape --self-test`;
that script's scan alone exits 0). Those six go to CI.
- Overlap check against #37102 and #37004 (both touch
`knowledge-rag.mdx`, #37102 also `packages/cli`): clean at push time.
The second to land merges main.
<details><summary>Rework round readings, head 1061045</summary>
- `service-knowledge` (dependency closure rebuilt first, `turbo run
build --filter='@objectstack/service-knowledge^...'`, 18/18 tasks):
`pnpm test` 14 files / 448 tests pass; `typecheck` (`tsc --noEmit` and
`check:test-typecheck`, 0 files / 0 errors in the debt ledger) passes.
- CLI: `src/commands/knowledge/knowledge.test.ts` 7/7 pass (unit layer).
CLI `tsc --noEmit` not measured locally, for the reason above; CI's
`TypeScript Type Check` owns it.
- `eslint --no-inline-config` on the seven changed `.ts` files: exit 0.
- Derived gates (`dispatch-gates.mjs --commands`, 122 commands, run on
a753df9 with the changeset edit in the tree): 114 exit 0. Not measured
here, left to CI: six exit 3, PREREQUISITE NOT MET, the same six the
first round named (`check-plugin-teardown-shape --self-test`, whose
positive control is pinned to a commit a shallow clone cannot reach, its
scan exits 0; `spec check:skill-examples`, `check:dual-build-cjs-loads`,
`check:i18n`, `check:i18n-coverage`, `check:i18n-walk-parity`, which
read built output this container does not have); two whole-repo scans,
`check:query-options-erasure` (its self-test passed) and
`check:type-check-debt`, hit the 300 s per-command limit on the shared
box. Re-run on 1061045: the changeset family
(`check-adr-0087-registration`, `check-changeset-no-major`,
`check-empty-changeset`, `check:changeset-fixed`), `check:nul-bytes`,
`check:doc-anchors`, `check:cli-command-ids`,
`check:cli-examples-parity`, all exit 0.
- `overlap-check` before each push: clean against `origin/main`, #37442,
#37422 (shares `content/docs/deployment/cli.mdx`) and #35129. #37004 has
no PR open yet.
</details>
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; `pnpm lint` passes.
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: its generated artifacts are
regenerated and committed. (not touched)
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English.
- [x] The *Before Submitting* list in CONTRIBUTING.md is done.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01JQwxbXkQAmjg7Hf74xDKA6
<!-- signed: repo:objectstack#5 · os-bill ·
session_01Utp6Cb4BAjgQ43tqy1DvVJ -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent ec92c7c commit a7bf18f
16 files changed
Lines changed: 1988 additions & 5 deletions
File tree
- .changeset
- content/docs
- ai
- deployment
- packages
- cli
- src
- commands/knowledge
- utils
- services/service-knowledge/src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
221 | | - | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
222 | 250 | | |
223 | 251 | | |
224 | 252 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4003 | 4003 | | |
4004 | 4004 | | |
4005 | 4005 | | |
| 4006 | + | |
| 4007 | + | |
| 4008 | + | |
| 4009 | + | |
| 4010 | + | |
| 4011 | + | |
| 4012 | + | |
| 4013 | + | |
| 4014 | + | |
| 4015 | + | |
| 4016 | + | |
| 4017 | + | |
| 4018 | + | |
| 4019 | + | |
| 4020 | + | |
| 4021 | + | |
| 4022 | + | |
| 4023 | + | |
| 4024 | + | |
| 4025 | + | |
| 4026 | + | |
| 4027 | + | |
| 4028 | + | |
| 4029 | + | |
| 4030 | + | |
| 4031 | + | |
| 4032 | + | |
| 4033 | + | |
| 4034 | + | |
| 4035 | + | |
| 4036 | + | |
| 4037 | + | |
| 4038 | + | |
| 4039 | + | |
| 4040 | + | |
| 4041 | + | |
| 4042 | + | |
| 4043 | + | |
| 4044 | + | |
| 4045 | + | |
| 4046 | + | |
| 4047 | + | |
| 4048 | + | |
| 4049 | + | |
| 4050 | + | |
| 4051 | + | |
| 4052 | + | |
| 4053 | + | |
| 4054 | + | |
| 4055 | + | |
| 4056 | + | |
| 4057 | + | |
| 4058 | + | |
| 4059 | + | |
| 4060 | + | |
| 4061 | + | |
4006 | 4062 | | |
4007 | 4063 | | |
4008 | 4064 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
82 | 85 | | |
83 | 86 | | |
84 | 87 | | |
| |||
0 commit comments