Repository navigation
perf(sources): bound the receipt migration scan and detect shared plugin drift - #2148
Merged
Merged
Conversation
The migration cursor spanned Scopes, so SQLite could not steer the (scope_id, source_type, source_id) primary key: every page used a temporary B-tree for the ORDER BY and rescanned the rows already visited, costing O(N^2) index reads per Scope at startup. Paging inside one Scope turns both statements into bounded seeks.
Four hosts ship the same client configuration and three ship the same prepared-context helper, with nothing detecting divergence. The WorkBuddy copy had already fallen behind on the bounded recall query, so this aligns it and adds a guard.
Contributor
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
This was referenced Oct 23, 2026
This was referenced Oct 26, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue or RFC does this PR close?
Refs #2084. This is the second of two PRs for that risk collection. It fixes the two items with a
single correct answer, and records two items that the investigation found already resolved or
incorrect.
Fixed here:
already diverged.
Investigated, not changed:
ArtifactProcessingLeaseRepository.load(..., for_update=True)already takes SQLite's write lock through a no-op
UPDATEbefore reading the term, so bothtopic_memory_budget._lockandstart_single_process_termvalidate the fence under the writelock. Details below.
_load_related_itemsnow authorizes and reads inpages of 100 through one
read_manyper page instead of one transaction and onegetper hit.tests/plugin.test.mjs:112asserts two submissions of thesame prompt produce different source ids, so making the id content-addressed is a semantic
decision about whether a repeated prompt is one memory or two events, not a defect fix.
Still open in the issue, with the blocking reason:
BEGIN IMMEDIATE. Removing it needs a startup-time measurement first, and thepc_artifacts(family)counts need an index decision.HOLDchanges flush semantics for every caller.APPROXIMATEfilter order needs a live tenant to confirm.GET_LOCKsemantics verified against a live tenant.Rationale for this change
R2.
migrate_handoff_receiptspaged with(scope_id, source_id) > afterwhile the primary key is(scope_id, source_type, source_id). The cursor could not steer that index, so SQLite produced:The temporary B-tree means every page materialized all remaining rows before
LIMITapplied, so oneScope with N content Sources cost O(N²/batch) index reads on every startup with access control
enabled. Adding the missing key column to the cursor is not enough — the equality on
source_typestill forces the sort. Paging inside one Scope instead gives the first two key columns as equality
and
source_idas a range:No schema change and no new index: the existing primary key is enough once the query matches it.
R14. Four hosts ship the same
powercontext_client_config.pyand three ship the samehooks/prepared_context.py. Nothing compared them, and the WorkBuddy copy had already fallen behindon
bounded_recall_queryand theMAX_QUERY_*bounds that Claude Code and Codex share. This PRaligns that copy and adds a test that fails on any future divergence, with the drifted paths in the
message.
What changes are included in this PR?
receipt_migration.py: pagination is now per Scope (_next_content_scope,_page_content_sources),each page seeks the primary key. Classification, attestation, review-table handling, and the
missing-row error are unchanged;
batch_sizestill bounds one page.integrations/workbuddy/.../hooks/prepared_context.py: same content as the Claude Code and Codexcopies.
tests/test_integration_shared_files.py: new guard for both shared sources.Are there any user-facing changes?
migrate_handoff_receiptsreturns the same counts and writes the same rows; only the query shapechanged. The WorkBuddy plugin now carries an unused-by-it helper in a shared module, so its behavior
is unchanged until its caller adopts it.
How was this change tested?
The new guard was validated by reverting the WorkBuddy copy: the test failed with
prepared_context.py differs from integrations/claude-code/... in: integrations/workbuddy/..., thenpassed after the realignment.
Query plans above were captured with
EXPLAIN QUERY PLANon a 5,000-row SQLite table using theproduction primary key. They are reported here rather than asserted in a test, because the plan text
is SQLite-version-specific.
AI usage statement
Built with WorkBuddy (DeepSeek V4.1 Flash). The R10 and R9 conclusions above come from reading
supervision.pyandatomic_memory_processing.pyonmasterat30e3f1b1, not from a test run —please re-check the reasoning if you disagree with the
for_updateclaim.