Repository navigation
Commit 4bc5a0c
fix(service-knowledge): record a build start onto an existing sync-state row without a false boot ERROR (#38460)
Fixes #38418
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:管理员每次启动带知识源的应用(如 showcase),服务日志不再出现一条假的 `[knowledge]` ERROR
和一条主键冲突 WARN;运维手动重建索引时也不再报这条假错。
- 为什么做:这是发版候选冒烟轮唯一不通过的一项(启动健康检查),维护者要在这次 next 版前修掉。
-
出错会怎样:最坏情况是知识源的同步状态行少记一次构建开始,管理员在设置页看到的上次构建信息滞后,下次构建会重写;改动只在知识服务内部,可直接回滚。
- 要你做什么:无。
## What changes
`ObjectStoreKnowledgeSyncState.upsert` (the write of a build's start,
`startBuild`) wrote the row through `updateWhere({ id: key }, data)` and
read the answer as a count. A `where` of the id alone is the engine's
by-id update even under `multi: true` (`resolveEngineUpdateDispatch`: a
pure-id `where` never reaches the predicate branch), and the by-id
update answers the record, not a count. So:
- row present: the record read as "0 rows changed", the fallback insert
hit the primary key (engine `WARN Insert operation failed … UNIQUE
constraint failed: sys_knowledge_sync_state.id`), the retry read 0
again, and `startBuild` threw the duplicate error. The service then
logged `ERROR [knowledge] the start of the build of source '…' was not
recorded`, and spent its once-per-process sync-state error report on it.
- row absent: the by-id update threw `RECORD_NOT_FOUND`, so `upsert`
threw before it ever reached its insert.
Now `upsert` calls a new `updateById`, which sends the by-id update
deliberately (no `multi`) and reads the answer the way
`IDataEngine.update` declares it: a record means the row was updated, a
count above zero means the same, `null` or `RECORD_NOT_FOUND` means
there is no row yet, so the insert runs. Any other error is rethrown.
The precedent for reading the by-id 404 as "no row" is `stamp` in
`packages/plugins/plugin-auth/src/mcp-client-last-use.ts`. `updateWhere`
now says in its docblock that it is for predicate writes only. Both
update forms go through one private `sendUpdate`, so the store still has
one engine `update` call site.
ObjectQL's update path and its return shape are not changed (the
ruling).
Measured on a real engine (ObjectQL + `SqlDriver` on better-sqlite3),
the raw answer of `engine.update(sys_knowledge_sync_state, …, { where,
multi: true })` for each `where` shape in this file:
| `where` (caller) | answer |
|---|---|
| `{ id }`, row present (`upsert`) | the record (object): **misread as
0** |
| `{ id }`, row absent (`upsert`) | throws `RECORD_NOT_FOUND` 404 |
| `{ id, build_id }` (`progressBuild`, `finishBuild`) | count: 1 on
match, 0 on a stale build id |
| `{ id, build_id, build_status }` (`closeInterrupted`) | count (0 on no
match) |
| `{ id, failure_count }` (`addFailure`, `setFailures`) | count (1 on
match) |
Only `upsert` was affected. The guarded writes take the predicate path
and answer a count. The new pin asserts each of them answers `true` and
`false` correctly.
## Tests
New file
`packages/services/service-knowledge/src/knowledge-sync-state-upsert.test.ts`,
on a real kernel (`ObjectQLPlugin`, `SqlDriver` on a file,
`QueueServicePlugin`, `KnowledgeServicePlugin`). No count and no engine
logger is mocked:
1. `startBuild` onto the existing row: one `update`, zero `insert`, and
the row reads `running` with the new build id. Onto no row: one
`insert`. Then `progressBuild` / `finishBuild` / `closeInterrupted` /
`addFailure` / `setFailures` each answer whether they landed. ObjectQL's
own logger records no warn or error.
2. A boot with `buildAtBoot: true` over an existing database, then an
operator rebuild: both builds are recorded as `done`, the knowledge
service logs nothing at warn or error, and the engine logs nothing at
warn or error.
<details><summary>Before the fix (test committed first, 19e02cc): both
red</summary>
```
× onto the existing row it updates once and inserts nothing; …
DuplicateRecordError: Duplicate record refused on 'Knowledge Sync State': a unique constraint on 'Source Key' already holds this value. No record was written.
Caused by: SqliteError: … UNIQUE constraint failed: sys_knowledge_sync_state.id
× a boot that builds the source over its existing row, and an operator rebuild after it, …
AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times
"[knowledge] the start of the build of source 'kb' was not recorded (Duplicate record refused on 'Knowledge Sync State': …"
Test Files 1 failed (1) · Tests 2 failed (2)
```
</details>
<details><summary>After the fix</summary>
At head 85d21d1, under the verify lock (`VERDICT command-exit 0`):
```
pnpm --filter @objectstack/service-knowledge exec vitest run --maxWorkers=2
Test Files 22 passed (22)
Tests 728 passed (728)
pnpm --filter @objectstack/service-knowledge typecheck (tsc --noEmit + check:test-typecheck OK, 0 errors)
pnpm --filter @objectstack/service-knowledge build (exit 0)
```
Derived gate families (`dispatch-gates.mjs --commands`, 66): 64 exit 0.
`check-plugin-teardown-shape --self-test` and
`check:dual-build-cjs-loads` exit 3 PREREQUISITE NOT MET (a shallow
clone, and a workspace without every dist), so NOT MEASURED locally; CI
runs both. `--ran` reconciliation: 66 derived, 66 run, 0 UNRUN.
</details>
## Acceptance notes
- The pin sits in a new file, as the claim's file surface says.
`AGENTS.md` (ruling record 6583487846) puts a new pin for a component
that already has a shared harness into that harness as a top-level
`describe`. Here that harness is `src/__tests__/sync-state.test.ts`,
which is outside this card's surface. The new file copies a reduced form
of its boot helper. Moving the pin there is a mechanical follow-up if
the seat wants it.
- The existing harness never caught this defect: its knowledge logger is
a `vi.fn()` that no test asserts stays unused at error. Its rebuild
tests pass on the old code, because the row is written before `upsert`
throws.
- Showcase boot before/after (`grep -c 'ERROR \[knowledge\]'`): NOT
MEASURED. A showcase boot needs the whole workspace built, and the
container was shared with a full-workspace build and a UI suite holding
the verify lock. Test 2 above reproduces the card's exact ERROR line
before the fix and asserts it is absent after.
## Verification
First read (by risk):
1. `packages/services/service-knowledge/src/knowledge-sync-state.ts`:
`updateById` and `upsert`. Risk hunk: how the by-id answer is read
(record / count / `null` / `RECORD_NOT_FOUND`).
2.
`packages/services/service-knowledge/src/knowledge-sync-state-upsert.test.ts`:
test 2, the boot-and-rebuild case.
Two-minute check: `pnpm --filter @objectstack/service-knowledge exec
vitest run src/knowledge-sync-state-upsert.test.ts`. Expect `Tests 2
passed (2)`. Revert `knowledge-sync-state.ts` to `origin/main` and
expect both tests red with `UNIQUE constraint failed:
sys_knowledge_sync_state.id`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_016WV4d13AraSmVmgVtCvkWk
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2052c89 commit 4bc5a0c
3 files changed
Lines changed: 130 additions & 8 deletions
File tree
- .changeset
- packages/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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
31 | 39 | | |
32 | 40 | | |
33 | 41 | | |
| |||
51 | 59 | | |
52 | 60 | | |
53 | 61 | | |
54 | | - | |
| 62 | + | |
55 | 63 | | |
56 | 64 | | |
57 | 65 | | |
| |||
609 | 617 | | |
610 | 618 | | |
611 | 619 | | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
312 | 312 | | |
313 | 313 | | |
314 | 314 | | |
315 | | - | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
316 | 323 | | |
317 | | - | |
318 | | - | |
319 | | - | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
320 | 348 | | |
321 | 349 | | |
322 | 350 | | |
| |||
325 | 353 | | |
326 | 354 | | |
327 | 355 | | |
328 | | - | |
| 356 | + | |
329 | 357 | | |
330 | 358 | | |
331 | 359 | | |
332 | | - | |
| 360 | + | |
333 | 361 | | |
334 | 362 | | |
335 | 363 | | |
| |||
0 commit comments