Repository navigation
Commit 95a49ad
fix(service-knowledge): defer a backlog reconcile asked for during a bulk import until its record syncs drain (#38767)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9DAwBxBGNv4CexdUyBYJXSw)_
Fixes #38591
Clause-②: no
Metadata-protocol: none
repo:objectstack#3 · os-steve · session_01ADHkmPDtfMCw4FJfyFfQ7v
## 维护者速读
-
用户能看到什么变化:往知识库来源批量导入记录时,向量化请求和向量写入从约两倍降到与整体重建持平;代价是导入后检索追上的时间回到只走批量同步时的水平(真实向量服务上约为重建的
1.36 倍,本容器未实测)。
- 为什么做:知识库检索大项里,上一轮真实服务实测发现导入时“积压对账”和批量同步把每条记录都向量化、写入了两遍,每次导入花两倍的钱。
- 出错会怎样:导入期间的对账推迟到同步排空后才跑;判断失误时最坏是对账晚启动(至多一小时)或多做一遍同步;只改一个内部函数,可直接撤回。
- 要你做什么:无。
## What changes
`considerBacklogReconcile` (`knowledge-service-record-sync.ts`) still
decides, as before, that a source's backlog is large enough for a
reconcile (estimate at least `RECORD_SYNC_BACKLOG_THRESHOLD` and at
least `RECORD_SYNC_BACKLOG_ROW_SHARE` of its rows). What it does next
now depends on one comparison:
- **Estimate no larger than the source's rows** (a bulk import or bulk
update: about one message per record). The walk is **deferred until the
backlog has drained** (new `backlogDrained`): the queue's waiting count
is read afresh every `RECORD_SYNC_BACKLOG_READ_INTERVAL_MS`, and the
walk starts once that count times the source's share of the scheduling
batch is under one message. The drain embeds and writes each imported
record once; the walk then skips every document the drain wrote
(unchanged hash) and writes only the rows no message reached. A queue
that never drains (other sources keep it busy) starts the walk after
`RECORD_SYNC_BACKLOG_RETRY_MAX_MS` (60 min), so it is never held off for
good. Builds stopped, the service held, or the source gone or no longer
walked while it waits: nothing is walked, and a later backlog may ask
again.
- **Estimate larger than the source's rows** (repeated saves of the same
records, rows deleted or moved out of `where`). Walked beside the drain,
exactly as before: one walk costs at most one embed per row and covers
the messages. This is #38413's case, pinned by the
`knowledge-build.test.ts` #38413 suite, which is unchanged and green.
An import cannot reach the second branch: each message is published
after its row commits, and the rows are counted after the waiting count
is read, so an import's estimate never exceeds its rows.
No change to `coveredByBuild`, the cover margin, the batch path,
`IQueueService`, any option, setting, error code or export.
`knowledge-record-sync-queue.ts` is untouched.
### Choice: (b), and why not (a)
- **(a) the walk skips a record whose message is queued or claimed**
needs the walk to know which records have such a message.
`IQueueService` exposes a count (`getQueueSize`, pending only) and the
dead letters (`listFailed`), and no read of pending or claimed messages
by source or record. Adding one is a contract change in `packages/spec`
(a `needs_decision`), and the walk lives in
`knowledge-service-indexing.ts`, outside this card's file surface. An
in-process tracker would not see messages published on another node.
- **(b) defer the walk until the backlog drains** is buildable inside
the record-sync facet, and measured below: the import drain lands at 501
requests and 50,000 writes, against the rebuild's 500 and 50,000.
### The margin's own case (#38556) stays closed
`coveredByBuild` and `RECORD_SYNC_COVER_MARGIN_MS` are untouched, so a
message published inside the margin of a build's start is still synced.
`knowledge-record-sync-cover-margin.test.ts` is unchanged and green.
## Premise check (origin/main acf3b33)
- `considerBacklogReconcile` is at
`knowledge-service-record-sync.ts:645`, called from the batch path at
`:516`; it schedules the reconcile with `void
this.enqueueReconcile(...)` beside the batch.
- `coveredByBuild` (`:480`) covers a message only when `publishedAt <
startedAt - RECORD_SYNC_COVER_MARGIN_MS`, and only after the build ended
`done` with a purge; a build running now covers nothing.
- The walk (`walkObjectSource`) lists the adapter's documents once,
before its first page, and skips only documents whose hash it listed.
During an import those documents do not exist yet, so the walk embeds
every row, and the drain syncs every message it takes meanwhile.
- The measurement commit `30141766` IS #38577's merge (margin `30_000`
there), so the margin change does not retire the card. Even at the old 5
s margin, a reconcile that starts 6 s into a 34 s import covers almost
none of it.
- Reproduced on the base before the fix: table below, 0 messages
`covered`.
## Measurement (counts)
A real kernel: `ObjectQLPlugin` over `SqlDriver` (better-sqlite3, a
file), `QueueServicePlugin({ adapter: 'db' })` with default options,
`KnowledgeServicePlugin` with one object source, `buildAtBoot: false`.
The store is a counting double that lists its documents: one `upsert`
call is one embedding request, each document one write, with 100 ms per
request. As in #38529: a complete build over the empty table first, then
`ql.insert` of 50,000 records in arrays of 500, timed from the first
insert until no record-sync message is pending or running and any
reconcile has ended. The rebuild runs over the drained data file with a
fresh store. The harness is not in the diff; it is a snippet in the
details below.
| run (50,000 records) | embedding requests | texts | document writes |
outcomes | reconcile |
|---|---|---|---|---|---|
| before: `acf3b33e`, reconcile beside the drain | 1,001 | 99,976 |
99,976 | 50,000 synced, 0 covered | walked beside: 50,000 read, 49,976
written |
| after: this branch | 501 | 50,000 | 50,000 | 50,000 synced, 0 covered
| after the drain: 50,000 read, 0 written, 50,000 unchanged |
| rebuild of the same records | 500 | 50,000 | 50,000 | n/a | n/a |
- After against the rebuild: requests +0.2 %, writes 0 %; the acceptance
asks for within 10 %. Before against the rebuild: 2.00x and 2.00x
(#38529 measured 985 against 503, and 99,551 against 50,012, with a real
provider).
- Wall time on this shared box, with the fake embedder: import 36.5 s /
34.4 s; drain 108.6 s before, 105.5 s after; the deferred reconcile
ended 21 s after the drain (126.8 s); rebuild 52.0 s / 51.8 s. These are
shared-box seconds, not idle-box figures.
- **Real-embedder timing: NOT MEASURED** (no AI gateway key in this
container). The counts carry the acceptance.
<details><summary>Harness (scratchpad only, not committed)</summary>
The harness boots the kernel above with a counting store (`upsert`
increments a request count and adds the batch length to writes, then
waits `REQ_MS`), wraps `processRecordSyncBatch` to tally outcomes,
inserts the records, waits until `sys_job_queue` holds no `pending` or
`running` row of `knowledge-record-sync` and any reconcile has ended,
then boots a second kernel over the same file with a fresh store and
runs `rebuildSource('kb', { trigger: 'operator' })`. Run with
`M38591_N=50000 M38591_REQ_MS=100`: 180 s before, 196 s after, under
`os-verify-lock`.
</details>
## Tests
New `src/__tests__/reconcile-during-import-38591.test.ts` (an engine
double and a store double that records each written document with its
call's `reason`, fake timers):
- a reconcile started during a pending import (6,000 messages for 6,000
rows) embeds no record that has a pending message, checked after every
batch; once drained it walks, and every imported document was written
exactly once, by its own sync;
- a record with no sync message is still reconciled once the import has
drained: the walk writes exactly that one document;
- a queue that does not drain holds the walk off for
`RECORD_SYNC_BACKLOG_RETRY_MAX_MS`, then it walks;
- builds stopped while the walk waits: nothing is walked.
<details><summary>Readings at 785fede</summary>
- `pnpm --filter @objectstack/service-knowledge test`: Test Files 26
passed (26), Tests 840 passed (840). This includes the unchanged
`knowledge-build.test.ts` #38413 suite and
`knowledge-record-sync-cover-margin.test.ts`.
- `pnpm --filter @objectstack/service-knowledge typecheck`: exit 0; `tsc
--listFiles` names the new test once in `tsconfig.json` and once in
`tsconfig.test.json`; `check:test-typecheck` OK.
- Dependency closure `pnpm --filter '@objectstack/service-knowledge^...'
build`: exit 0.
- `dispatch-gates --commands --repo objectstack-ai/objectstack` at
785fede derives 68 families. 67 exit 0. `check:dual-build-cjs-loads`
exits 3, PREREQUISITE NOT MET (it needs a whole-repo build): NOT
MEASURED locally, and CI runs it. `--ran` reconciliation: 68 derived, 67
run, 1 NOT-MEASURED, 0 UNRUN. `check-plugin-teardown-shape --self-test`
first exited 3 because this shallow checkout could not reach its pinned
fixture commit; after a `--depth=1` fetch of that commit it exited 0.
`check:query-options-erasure` (392 s) and `check:type-check-debt` (234
s) ran past a 150 s and a 300 s per-command timeout on this shared box,
and both exited 0 when re-run with a longer one. `check:nul-bytes` exit
0. `overlap-check.mjs` before the push: `origin/main: merges cleanly`,
`VERDICT clean exit 0`.
</details>
## Ablation
`node scripts/ablation-replace.mjs` turned `const deferred = estimate <=
rows;` into `const deferred = false;` on disk (anchor 1 → 0, blob
`8cdba5dc3e67` → `8ae94f8819d7`; the test imports the source directly,
so no `dist/` is involved). Result: the new file goes 4 failed / 4. The
card's pin fails with `expected [ 'kb:imp00100', …(5899) ] to deeply
equal []`: the walk wrote 5,900 records whose messages were still
waiting. Restored: the blob equals HEAD and `git diff HEAD` is empty.
## Acceptance notes
- **Wall time moves to the batch path's rate.** In #38529 the default
drain (568.6 s) beat the batch path alone (805.7 s) only because two
embedding streams ran at once, paying twice. With this change an import
drains at the batch-only rate, 1.36x the rebuild on that provider, which
is inside #38413's "small multiple". Not re-measured here.
- **Drained is judged on the pending count.** `getQueueSize` counts
pending messages, not claimed ones, so the walk can begin while the last
batch (at most `RECORD_SYNC_BATCH_SIZE` records) is still syncing; at
most one batch is written twice. The 30 s read interval makes this rare.
- **A row count cached from an earlier backlog**
(`RECORD_SYNC_SOURCE_ROWS_TTL_MS`, 10 min) can be lower than the table's
rows after a later import, which could send that import down the
walk-beside branch. It is reached only by a second large backlog of the
same source within 10 minutes of a failed reconcile (the 5 min retry);
noted, not changed.
- The 60 min bound reuses `RECORD_SYNC_BACKLOG_RETRY_MAX_MS`; no new
constant.
- AGENTS.md asks a new pin for a component with a shared harness file to
go into that file (`knowledge-build.test.ts`). The claim's file surface
names a new file, so the harness is duplicated in small; folding it in
is a follow-up for whoever holds that file.
- Not in scope, per the card: the cover margin's value, the batch size,
a remote libSQL store.
## Verification (two minutes)
`pnpm --filter @objectstack/service-knowledge exec vitest run
--maxWorkers=2 src/__tests__/reconcile-during-import-38591.test.ts` —
expect 4 passed.
Read first, by risk:
1. `knowledge-service-record-sync.ts` `considerBacklogReconcile`: the
`deferred` branch and the rewritten completion chain (a dropped deferral
must not count as a failed reconcile).
2. `knowledge-service-record-sync.ts` `backlogDrained`: the drained
test, the 60 min bound and the stop conditions.
3. The new test's first case, the card's pin.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01ADHkmPDtfMCw4FJfyFfQ7v)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01ADHkmPDtfMCw4FJfyFfQ7v
<!-- signed: repo:objectstack#3 · os-steve ·
session_01ADHkmPDtfMCw4FJfyFfQ7v -->
Co-authored-by: Claude <noreply@anthropic.com>1 parent f576dee commit 95a49ad
3 files changed
Lines changed: 377 additions & 24 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 | + | |
| 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 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 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 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 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 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
0 commit comments