Repository navigation
fix(service-knowledge): defer a backlog reconcile asked for during a bulk import until its record syncs drain - #38767
Conversation
…bulk import until its record syncs drain A backlog no larger than the source's rows is about one message per record: the drain embeds each record once, and a walk beside it could cover only the messages published RECORD_SYNC_COVER_MARGIN_MS before it began, so it embedded and wrote every imported record a second time. The walk now waits until the backlog has drained (bounded by RECORD_SYNC_BACKLOG_RETRY_MAX_MS) and then writes only the rows no message reached. A backlog that outnumbers the rows is walked beside the drain, as before. Claude-Session: https://claude.ai/code/session_01ADHkmPDtfMCw4FJfyFfQ7v Co-authored-by: Claude <noreply@anthropic.com>
📓 Docs Drift Check7 anchor(s) derived from 1 changed package(s); no hand-written page names any of them, so this run has nothing to list — not a clean bill of health. This check sees only pages that NAME a derived anchor: one that documents this change in prose, or enumerates it in an authoring dialect, names none and stays invisible to it on every run. What this run could not see
Coarse fallback — 5 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): Which tree this was computed onThis run read A worktree cut from an older # while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 810eaaddedc67f1c8b8042a2014d65f6b333b0d7 && git checkout 810eaaddedc67f1c8b8042a2014d65f6b333b0d7
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 111ad632d02cff93cc2786657925370a9c16c521 785feded899206fa0e829b8190e0c0b25ed75d50 && git checkout -B drift-repro 111ad632d02cff93cc2786657925370a9c16c521 && git merge --no-ff 785feded899206fa0e829b8190e0c0b25ed75d50
node scripts/docs-audit/affected-docs.mjs --json 111ad632d02cff93cc2786657925370a9c16c521 |
|
repo:objectstack#3 · os-steve · session_01ADHkmPDtfMCw4FJfyFfQ7v Final head for review — zone o3-K · 2026-11-30T13:37Z |
|
repo:objectstack#3 · os-steve · session_01ADHkmT9HTWLMN33WUVxVWB 席位核对ACCEPT @ 785fede (card #38591,
Checks at this head (read before writing): 39 success, 18 skipped, 0 failure. |
|
repo:objectstack#3 · os-steve · session_01ADHkmPDtfMCw4FJfyFfQ7v Landing — zone o3-K · 2026-11-30T13:44Z
L1 ACCEPT 7280239039 (L1-b) on this head; no L2 owed (not governed, |
Requested by Steve · project thread
Fixes #38591
Clause-②: no
Metadata-protocol: none
repo:objectstack#3 · os-steve · session_01ADHkmPDtfMCw4FJfyFfQ7v
维护者速读
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 leastRECORD_SYNC_BACKLOG_THRESHOLDand at leastRECORD_SYNC_BACKLOG_ROW_SHAREof its rows). What it does next now depends on one comparison:backlogDrained): the queue's waiting count is read afresh everyRECORD_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 afterRECORD_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.where). Walked beside the drain, exactly as before: one walk costs at most one embed per row and covers the messages. This is service-knowledge: a bulk import syncs one record per queue message, read, embedded and written alone, so the index trails the data for hours #38413's case, pinned by theknowledge-build.test.tsservice-knowledge: a bulk import syncs one record per queue message, read, embedded and written alone, so the index trails the data for hours #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.tsis untouched.Choice: (b), and why not (a)
IQueueServiceexposes 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 inpackages/spec(aneeds_decision), and the walk lives inknowledge-service-indexing.ts, outside this card's file surface. An in-process tracker would not see messages published on another node.The margin's own case (#38556) stays closed
coveredByBuildandRECORD_SYNC_COVER_MARGIN_MSare untouched, so a message published inside the margin of a build's start is still synced.knowledge-record-sync-cover-margin.test.tsis unchanged and green.Premise check (origin/main acf3b33)
considerBacklogReconcileis atknowledge-service-record-sync.ts:645, called from the batch path at:516; it schedules the reconcile withvoid this.enqueueReconcile(...)beside the batch.coveredByBuild(:480) covers a message only whenpublishedAt < startedAt - RECORD_SYNC_COVER_MARGIN_MS, and only after the build endeddonewith a purge; a build running now covers nothing.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.30141766IS fix(service-knowledge): widen the record-sync covered margin to the queue lease #38577's merge (margin30_000there), 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.covered.Measurement (counts)
A real kernel:
ObjectQLPluginoverSqlDriver(better-sqlite3, a file),QueueServicePlugin({ adapter: 'db' })with default options,KnowledgeServicePluginwith one object source,buildAtBoot: false. The store is a counting double that lists its documents: oneupsertcall is one embedding request, each document one write, with 100 ms per request. As in #38529: a complete build over the empty table first, thenql.insertof 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.acf3b33e, reconcile beside the drainHarness (scratchpad only, not committed)
The harness boots the kernel above with a counting store (
upsertincrements a request count and adds the batch length to writes, then waitsREQ_MS), wrapsprocessRecordSyncBatchto tally outcomes, inserts the records, waits untilsys_job_queueholds nopendingorrunningrow ofknowledge-record-syncand any reconcile has ended, then boots a second kernel over the same file with a fresh store and runsrebuildSource('kb', { trigger: 'operator' }). Run withM38591_N=50000 M38591_REQ_MS=100: 180 s before, 196 s after, underos-verify-lock.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'sreason, fake timers):RECORD_SYNC_BACKLOG_RETRY_MAX_MS, then it walks;Readings at 785fede
pnpm --filter @objectstack/service-knowledge test: Test Files 26 passed (26), Tests 840 passed (840). This includes the unchangedknowledge-build.test.tsservice-knowledge: a bulk import syncs one record per queue message, read, embedded and written alone, so the index trails the data for hours #38413 suite andknowledge-record-sync-cover-margin.test.ts.pnpm --filter @objectstack/service-knowledge typecheck: exit 0;tsc --listFilesnames the new test once intsconfig.jsonand once intsconfig.test.json;check:test-typecheckOK.pnpm --filter '@objectstack/service-knowledge^...' build: exit 0.dispatch-gates --commands --repo objectstack-ai/objectstackat 785fede derives 68 families. 67 exit 0.check:dual-build-cjs-loadsexits 3, PREREQUISITE NOT MET (it needs a whole-repo build): NOT MEASURED locally, and CI runs it.--ranreconciliation: 68 derived, 67 run, 1 NOT-MEASURED, 0 UNRUN.check-plugin-teardown-shape --self-testfirst exited 3 because this shallow checkout could not reach its pinned fixture commit; after a--depth=1fetch of that commit it exited 0.check:query-options-erasure(392 s) andcheck: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-bytesexit 0.overlap-check.mjsbefore the push:origin/main: merges cleanly,VERDICT clean exit 0.Ablation
node scripts/ablation-replace.mjsturnedconst deferred = estimate <= rows;intoconst deferred = false;on disk (anchor 1 → 0, blob8cdba5dc3e67→8ae94f8819d7; the test imports the source directly, so nodist/is involved). Result: the new file goes 4 failed / 4. The card's pin fails withexpected [ 'kb:imp00100', …(5899) ] to deeply equal []: the walk wrote 5,900 records whose messages were still waiting. Restored: the blob equals HEAD andgit diff HEADis empty.Acceptance notes
getQueueSizecounts pending messages, not claimed ones, so the walk can begin while the last batch (at mostRECORD_SYNC_BATCH_SIZErecords) is still syncing; at most one batch is written twice. The 30 s read interval makes this rare.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.RECORD_SYNC_BACKLOG_RETRY_MAX_MS; no new constant.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.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:
knowledge-service-record-sync.tsconsiderBacklogReconcile: thedeferredbranch and the rewritten completion chain (a dropped deferral must not count as a failed reconcile).knowledge-service-record-sync.tsbacklogDrained: the drained test, the 60 min bound and the stop conditions.Generated by Claude Code
🤖 Generated with Claude Code
https://claude.ai/code/session_01ADHkmPDtfMCw4FJfyFfQ7v