Repository navigation
Commit fc7bdeb
feat(knowledge-pgvector): a changed fingerprint builds a new generation and switches to it (#37492)
<!-- claude-projects-attribution: {"github_login":"os-bill"} -->
_Requested by **Bill** · [project
thread](https://claude.ai/code/project/chan_016iMXWSZDzqWRWvWzdNo8wg?thread=cmsg_016iMXWSZDzqWRWvWzdNo8wg3zXREECfPtgrM8TPQaxn1v)_
Fixes #37101
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
- 用 PostgreSQL
存知识库的部署,换了嵌入模型或切分设置后,重建会在旧索引旁边建一套新索引,建好才切过去,旧的分批删掉;以前这种情况会拒绝重建、要运维手工清表。
- 属于「知识库平台」大项的第二阶段,替自建 PostgreSQL 的部署解决换模型后必须手工清索引的问题。
- 出错时最坏是重建期间该知识源检索一直返回「重建中」或旧索引未删干净占空间,不会返回新旧模型混排的错结果;这个包的改动可以直接撤回。
- 无。
## What changes
`@objectstack/knowledge-pgvector` builds a new generation beside the
active one when the index fingerprint changes, and switches to it
(ADR-0161 K7, rules 1 to 4). Before, `beginBuild` under a new
fingerprint was refused and the reindex result carried hand-run SQL that
cleared the source.
- **Generations.** The generation table holds one `active` and at most
one `building` generation per source (two unique partial indexes,
created `IF NOT EXISTS` on the next boot), plus `retired` ones being
deleted. Passage tables are unchanged: they are per vector width and
already keyed by generation.
- **`beginBuild`** takes the builder lock, then: same fingerprint as the
active one, rebuild in place; same as the one under construction, take
it up (resumed with what it holds); otherwise retire any unfinished one
and open a new `building` generation numbered past every generation the
source has. Orphan passages (generation row deleted by hand) are deleted
before any generation opens, as the first generation already did.
- **Writes** go to the generation of the fingerprint they carry: during
a build, the walk and event-sync writes carry the new one and land in
the new generation only. Each write share-locks its generation row
inside its transaction, so a switch or a retirement waits for it, and a
write that finds its generation retired is planned again.
- **Search** under the new fingerprint while its generation is building
answers `KNOWLEDGE_REBUILD_REQUIRED` (409), message "is being rebuilt"
(the same code and wording as knowledge-turso). The ranking statement
holds only while its generation is active (an `EXISTS` on the generation
row in the same statement), and a search that meets a switch runs again,
at most three times; the third answers an error rather than an empty
result.
- **`completeBuild`** (the service calls it after the walk and its
single purge) retires the active generation and activates the new one in
one transaction, then deletes the retired generation's passages in
batches of `deleteBatchSize` rows (default 1000) and its row last. A
deletion that stops is logged at `warn` and finished by the next build;
a retired generation is never read.
- **One builder per source:** a PostgreSQL session advisory lock (key:
64 bits of a hash of schema and source id) held from `beginBuild` to
`completeBuild` on a connection outside the pool
(`PgPoolLike.connectSession`, provided by `poolFromPg`). A second
builder gets `LOCK_CONFLICT` (409, standard catalog), so its walk reads
and embeds nothing. Freed on completion, on process or connection death
(PostgreSQL), after `buildLockIdleMs` (15 minutes) without a call of the
build itself (calls with reason `reindex`; event-sync writes do not hold
it), and on plugin `destroy`. It is a session lock, so the store needs a
direct or session-mode PostgreSQL connection: a transaction-mode pooler
breaks it. When `pg_advisory_unlock` answers false (the session no
longer held the lock), the release is logged at `warn` and names that
requirement. A crashed build is **resumable**: the next build under the
same fingerprint keeps what was written and the walk skips it by
document hash.
- **Fresh database.** Two sessions that ran `CREATE EXTENSION IF NOT
EXISTS vector` at the same moment: the loser failed on the catalogue's
unique key, and `ready()` refused although the extension existed. It now
re-reads the extension after a failed create and refuses only when it is
still missing (pre-existing, seen as a flaky first reindex on a fresh
database while the live suite ran its files in parallel).
- **New exports, all optional (widening):** adapter options
`buildLockIdleMs`, `deleteBatchSize`, `logger`;
`PgPoolLike.connectSession`; `PgvectorKnowledgeAdapter.close()`. The
changeset stays `patch` and carries the same `Clause-②` line.
- No contract member is added: `beginBuild`, `completeBuild`,
`indexStatus.building` and `ctx.index` are ADR-0161 K8 item 8, already
on main. No error code is added. `service-knowledge` needs no change:
its walk already calls `beginBuild`, purges once and calls
`completeBuild`.
- Docs: a "Generations" subsection in
`content/docs/ai/knowledge-rag.mdx`, and one bullet in its pgvector
section. The "Rebuilds" section of
`packages/plugins/knowledge-pgvector/README.md` (it ships in the npm
tarball) and the "Rebuilds" bullet of
`content/docs/protocol/knowledge.mdx` section 4.1 now describe
generations; the hand-run clear-SQL instructions are gone (claim amend
7187694971). All three say the builder lock needs a direct or
session-mode connection.
## Tests
- New file `generations.live-postgres.test.ts` (live PostgreSQL +
pgvector, the package's existing live pattern): same width with the
model changed, through `KnowledgeService` (generation 2 built beside
generation 1; search during the build refused "being rebuilt"; an
event-sync write during the build lands only in generation 2 and is
present after the switch; purge 0; generation 1 deleted; reading
`current`). Also: two concurrent builds (one builds, the other gets
`LOCK_CONFLICT` and embeds nothing); a build whose session is terminated
is resumed by another adapter with its passage kept; the idle release
while event-sync writes keep arriving (they do not extend the lock;
released after `buildLockIdleMs`); batched deletion (5 rows at batch 2 =
3 statements) and abandoning a generation for a third fingerprint.
- New file `builder-lock.test.ts` (no database): a release whose
`pg_advisory_unlock` answers false logs one warning naming the direct or
session-mode connection; one that answers true logs nothing.
- Contract suite: `generations: true` for pgvector (it was `false`,
pending this card).
- `acceptance.live-postgres.test.ts`: two cases that pinned the retired
behaviour (reindex refused with hand-run clear SQL; `beginBuild` under
another fingerprint refused) are rewritten to the new behaviour. The
case that ran the printed clear SQL is removed, because that SQL is no
longer printed. The rebuild-required remedy is pinned to say "complete
reindex" and to carry no `DELETE FROM`, `DROP` or `TRUNCATE`.
## Acceptance notes
- Fixed in this PR (dc620cd): the README "Rebuilds" section and the
protocol section 4.1 "Rebuilds" bullet said the store does not yet build
a second generation and printed clear SQL. Both now describe the
generations behaviour, after the claim amend added the two files to the
surface. Left as is, being generic and still true: protocol section
3.6's sentence about an adapter that does not keep generations.
- The builder lock is held on a dedicated session for the whole build. A
walk that fails on a process that keeps running keeps it until
`buildLockIdleMs` passes; another process is refused `LOCK_CONFLICT`
meanwhile, and that refusal's remedy says when it frees. Fencing a build
that lost its lock mid-walk is #37465 (service lease), not this PR.
- Carried, not fixed here (seat note 7188645663): N1 (fencing a builder
that lost its lock) goes to #37465. N2, a `building` generation that
stays after the fingerprint is reverted to the active one, is deleted at
the next build under another fingerprint; until then it holds rows and
is never read. N4, two sweeps of the same retired generation at once,
can leave passage rows behind when one deletes the generation row first;
the sweep no longer sees them (it walks generation rows); they are never
read, and the orphan cleanup deletes them when the source next opens a
generation. N3 (that third error carries no code): giving it
`CONCURRENT_MODIFICATION` (409) pins a code that
`scripts/error-status-unpinned-baseline.json` lists as unpinned, and
`check:error-status-conformance` then requires that row to be removed.
The file is outside the claim surface, so the code was taken back out;
it needs a one-line claim amend. Protocol section 9's table row is
outside the claim surface and left as is.
- A second builder that took over after an idle release writes the same
documents under the same fingerprint into the same generation, so the
index stays correct; only embedding spend is doubled.
## Verification
Read first, by risk: `pgvector-adapter.ts` `completeBuild` and
`sweepRetired` (the switch and the batched delete), `searchAttempt` (the
`EXISTS` guard and the retry), `takeBuilderLock` and
`releaseBuilderLock`.
Two-minute check: with `OS_TEST_POSTGRES_URL` pointing at a PostgreSQL
that has pgvector, run `pnpm --filter @objectstack/knowledge-pgvector
exec vitest run src/__tests__/generations.live-postgres.test.ts`. You
should see 5 passed, among them the idle release under event-sync writes
and the "model changed" case that is refused "being rebuilt" mid-build.
<details><summary>Local test runs (head 06d53aa; PostgreSQL 16.16 +
pgvector 0.8.7, a local instance, fresh database)</summary>
- Package suite with the live files on, `vitest run --maxWorkers=2
--reporter=verbose` under the verify lock: `Test Files 6 passed (6)`,
`Tests 75 passed (75)`, lock VERDICT command-exit 0. All 5 generation
cases and both builder-lock cases passed. Run at tree 06d53aa (the
same tree as d1c735dce, whose commits were reworded only), plus
`not-ready.test.ts` 11/11 with the new race case.
- `pnpm typecheck` and `pnpm build` of the package: exit 0,
check-dts-emitted 2/2.
- Correction: the runs reported at bc0cab4 and dc620cd as "pgvector
0.8" were on pgvector 0.6.0. Every run since fb4146c is on 0.8.7, the
version the CI live job uses.
</details>
<details><summary>Ablations (each restored, git diff empty
afterwards)</summary>
- A: writes no longer go to the generation under construction. Red: 3
failed of 5 (model changed; session died and resumed; batches and third
fingerprint).
- B: the advisory lock answer is ignored. Red: 2 failed of 5 (two
concurrent builds; idle release).
- C: one delete batch only. Red: 1 failed of 5 (batched deletion).
- D (round 3): event-sync calls touch the lock again (`reason` check
removed). Red: 1 failed of 5, the idle release while event-sync writes
arrive.
- E (round 3): no re-read after a failed CREATE EXTENSION. Red: 1 failed
of 11 in `not-ready.test.ts`, the lost-race case.
</details>
<details><summary>Derived gates (dispatch-gates, 102 families over the
whole change)</summary>
`--ran` reconciliation at 06d53aa: 102 derived, 100 run, 2
NOT-MEASURED, 0 unrun. The two are PREREQUISITE NOT MET (exit 3) on this
checkout: `check-plugin-teardown-shape --self-test` (shallow clone) and
`spec check:skill-examples` (client-react not built). This round ran 98
families for the round-3 paths, then 61 again for the adapter and
changeset after the last code change, and 6 more for
`not-ready.test.ts`. One went red in between:
`check:error-status-conformance`, which N3's `CONCURRENT_MODIFICATION`
tripped (see Acceptance notes). It is green after that code was taken
out. `overlap-check`: clean against origin/main 54ea3c8, #37477 and
#37442. PR check-runs at 06d53aa: 53 completed, 38 success, 15
skipped, 0 failure. The CI check-runs are the authority. The live
pgvector job (`Temporal Conformance`) runs in the merge queue, not on
the PR.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01Utp6Cb4BAjgQ43tqy1DvVJ)_
<!-- signed: repo:objectstack#5 · os-bill ·
session_01Utp6Cb4BAjgQ43tqy1DvVJ -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent c8e9bec commit fc7bdeb
13 files changed
Lines changed: 1001 additions & 192 deletions
File tree
- .changeset
- content/docs
- ai
- protocol
- packages/plugins/knowledge-pgvector
- 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 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
220 | 220 | | |
221 | 221 | | |
222 | 222 | | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
223 | 233 | | |
224 | 234 | | |
225 | 235 | | |
| |||
258 | 268 | | |
259 | 269 | | |
260 | 270 | | |
| 271 | + | |
261 | 272 | | |
262 | 273 | | |
263 | 274 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
558 | 558 | | |
559 | 559 | | |
560 | 560 | | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
568 | 580 | | |
569 | 581 | | |
570 | 582 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
66 | 77 | | |
67 | 78 | | |
68 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
162 | 163 | | |
163 | 164 | | |
164 | 165 | | |
165 | | - | |
| 166 | + | |
166 | 167 | | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
| 168 | + | |
194 | 169 | | |
195 | | - | |
| 170 | + | |
196 | 171 | | |
197 | | - | |
198 | 172 | | |
199 | 173 | | |
200 | 174 | | |
| |||
252 | 226 | | |
253 | 227 | | |
254 | 228 | | |
255 | | - | |
| 229 | + | |
256 | 230 | | |
257 | 231 | | |
258 | 232 | | |
259 | 233 | | |
260 | | - | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
261 | 237 | | |
262 | | - | |
| 238 | + | |
263 | 239 | | |
264 | | - | |
| 240 | + | |
265 | 241 | | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
272 | 246 | | |
273 | 247 | | |
274 | 248 | | |
| |||
0 commit comments