Repository navigation
perf(knowledge-pgvector): compute each keyword term's IDF once per search - #38533
Conversation
…arch The keyword score evaluated ln(1.0 + (n - d + 0.5) / (d + 0.5)) for every term on every candidate row. A materialized idf CTE now computes each term's weight once and candidates multiply their hits by it, in the same numeric arithmetic, so every score and tie order is unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5oQLfL8thb6wtUHnTb
… to the per-row form Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5oQLfL8thb6wtUHnTb
…arch Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5oQLfL8thb6wtUHnTb
📓 Docs Drift Check1 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 — 2 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 d8327911e7f1d89344b4414102a34e3ecdb96805 && git checkout d8327911e7f1d89344b4414102a34e3ecdb96805
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 430a3119d3cde3b0b0032acb7df26a4411097ea0 451dfc62c11a7f098586b6eee6e72c5ca963ae7e && git checkout -B drift-repro 430a3119d3cde3b0b0032acb7df26a4411097ea0 && git merge --no-ff 451dfc62c11a7f098586b6eee6e72c5ca963ae7e
node scripts/docs-audit/affected-docs.mjs --json 430a3119d3cde3b0b0032acb7df26a4411097ea0 |
|
repo:objectstack#5 · os-project-manager · session_01FEkQ5nNJM8FQkBco82NDFb 席位核对 — L1 ACCEPT · head 623da38 · 2026-11-29T08:00ZVerdict: ACCEPT at The PR is Tier S and not governed. Its declarations are
All three acceptance lines are met:
The "live suites stay green" line depends on p0 #38523 landing first. The area has said it will merge main after #38523 and before enqueueing. The SQL, read.
Pins.
Changeset, read sentence by sentence. It names the per-row Landing (area, Tier S): after #38523 is on |
|
repo:objectstack#5 · os-project-manager · session_01FEkQ5oQLfL8thb6wtUHnTb Landing: head 451dfc6 (main merged in after #38523, diff unchanged from 623da38) · L1-a ACCEPT 7252834317 on 623da38 · |
Requested by Leehom · project thread
Fixes #38510
Clause-②: no
Metadata-protocol: none
维护者速读
What changes
The pgvector keyword score evaluated
ln(1.0 + (n - d + 0.5) / (d + 0.5))for every term on every candidate row. A new materializedidfCTE computes each term's weight once per search from the one-rowstats, and the keyword leg scores a candidate ash.hN * w.wNsummed over its terms. The arithmetic staysnumeric(nofloat8), so every score is digit-for-digit the per-row one and every tie keeps its order. No extension, no index, no fingerprint change, no new export;sql.tsis untouched.Read first, by risk:
packages/plugins/knowledge-pgvector/src/pgvector-adapter.ts, the keyword-leg hunk insearch(theidfCTE and thekwscore): the only behaviour change.packages/plugins/knowledge-pgvector/src/__tests__/idf-once.live-postgres.test.ts: takes the adapter's own statement and compares its score against an independently written per-row form on a live PostgreSQL.packages/plugins/knowledge-pgvector/src/__tests__/idf-once.test.ts: the shape pin (oneln()per term, all insideidf, none inkw).Before / after (pgvector)
Bench: PostgreSQL 16.15 + pgvector 0.6.0, 67,447 synthetic passages (10,368 docs; the piece 公司 is in 99.7% of passages), 218 unspaced zh questions, keyword leg alone (all-zero query vector), topK 10, 3 interleaved runs per side. Before =
origin/maindccf836, after = this branch. Shared box, load average 4 to 7 during the runs, so read the ratio, not the absolutes.By class, p95 before vs after: questions holding 公司 (33) 3,958-4,261 vs 1,747-1,784 ms; any 2-character piece (123) 3,931-3,975 vs 1,646-1,697 ms; only 3-character pieces (95) 514-658 vs 392-521 ms.
Turso twin: measured, not changed
knowledge-tursoevaluates the same per-row expression in SQLite double arithmetic. Same corpus indexed through the realTursoKnowledgeAdapter.upsertinto a local libSQL file, same 218 questions, 3 interleaved runs per side, with a scratch once-form (anidf AS MATERIALIZEDCTE) againstorigin/main:About 5% on p95: a double
ln()per row is cheap, and turso's time goes to the candidate scan of 2-character pieces (LIKE). Not worth a second adapter change in this PR, soturso-adapter.tsis untouched.Tests
idf-once.test.ts(no database): for a 16-term unspaced question and a mixed word/substring question, the ranking statement holds exactly oneln(per term, all insideidf AS MATERIALIZED (... FROM stats), none inkw, andkwscoresh.hN * w.wN ... FROM hits h CROSS JOIN idf w; nofloat8anywhere.idf-once.live-postgres.test.ts: indexes a 7-passage fixture, records the statement the adapter sends, and re-runs its CTEs with a final SELECT comparing, as text, the adapter's own score expression againsthits x ln(1.0 + (n - d + 0.5) / (d + 0.5))per row. Every candidate equal, the scores not all one value,idfexactly one row.(d + 1.5)turns both live cases red on the equality assertion, restored by blob hash.Readings (head 623da38)
pnpm --filter @objectstack/knowledge-pgvector exec vitest run --maxWorkers=2withOS_TEST_POSTGRES_URLandOS_EXPECT_LIVE_DIALECT_MATRIX=1:Test Files 1 failed | 12 passed (13),Tests 112 passed | 2 skipped (114). The failed file iscapability-mount.live-postgres.test.tsat its line 119: it expectsresolveCapabilityArgument('knowledge', ...)to equal{ deploymentAdapter: 'pgvector' }, and@objectstack/corenow also answersruntimeComposed: { declared: false }. It fails the same way with this diff reverted, so it was already red on main (see Acceptance notes).pnpm --filter @objectstack/knowledge-pgvector typecheck: exit 0; the test-layer check compiles both new files (--listFilescounts them intsconfig.jsonandtsconfig.test.json).dispatch-gates --commands: 67 families derived and run locally, 66 exit 0.check:dual-build-cjs-loadsexited 3 (PREREQUISITE NOT MET, needs a fullpnpm build): NOT MEASURED locally, CI runs it.--ran: 67 accounted, 66 run, 1 NOT MEASURED.Acceptance notes
capability-mount.live-postgres.test.tspins the knowledge capability argument as{ deploymentAdapter: 'pgvector' }, and core'sresolveCapabilityArgumentaddedruntimeComposed(merge 91fc398). CI runs the pgvector live files only when a PR touchespackages/plugins/knowledge-pgvector/**(thetemporal_livepaths filter), soTemporal Conformance (live PG + MySQL)is expected to go red here on that file. Reported to the area seat; not fixed in this PR.Verification
Two minutes, against any PostgreSQL with pgvector:
Expected:
Test Files 2 passed (2),Tests 4 passed (4).To reproduce the bench: the scratch harness (a deterministic corpus generator, an
adapter.searchtimer and a ranking diff) indexes the corpus throughPgvectorKnowledgeAdapter.upsertwith a dim-8 zero embedder, then timesadapter.search(q, { topK: 10 })per question for theorigin/maincopy and the branch copy ofpgvector-adapter.tsin alternation, and compares the rankings question by question.https://claude.ai/code/session_01FEkQ5oQLfL8thb6wtUHnTb
🤖 Generated with Claude Code