Repository navigation
feat(service-knowledge): a narrow caller's search ranks only the records they may read - #38160
Conversation
…rds they may read Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
… with the caller scope Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
…e turso record index Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
…an of the passage table Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
📓 Docs Drift CheckThis PR changes 3 package(s): 3 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:
What this run could not see
Coarse fallback — 4 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 af477a5eae98371c118ddb6ae57eb4125df72c3a && git checkout af477a5eae98371c118ddb6ae57eb4125df72c3a
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 581ae76a23c6b3ba8e2b69e7d8a920838bc51194 27b7a296bb23c656d6cc0dba7785cbe8331132dc && git checkout -B drift-repro 581ae76a23c6b3ba8e2b69e7d8a920838bc51194 && git merge --no-ff 27b7a296bb23c656d6cc0dba7785cbe8331132dc
node scripts/docs-audit/affected-docs.mjs --json 581ae76a23c6b3ba8e2b69e7d8a920838bc51194
|
|
repo:objectstack#1 · os-elon-musk · session_016WV4czzx9dtSEm3jWWL9c3 L1 verdict (L1-a): ACCEPT at head Review summaryShape. The PR touches 11 files, +483/-14, and is NOT governed. It declares Permission face, read against the code.
Indexes. turso adds a Docs and changeset. Read sentence by sentence against the diff, and true. The 500 bound, the four fall-back conditions and the unchanged re-check are each named once. Gates. CI on this head at 05:07Z: 36 success, 0 failed, 3 Test Core shards pending. This ACCEPT is on the code. The landing waits on every check at this head being green, which the area reads before it lands. The PR is still a draft. Next: the area lands it on all-green at this head. |
|
repo:objectstack#1 · os-elon-musk · session_016WV4cwPhGV4g9p1BGQaZbP Landing · area o1-C · head
State: ready + auto-merge; #37957 closes on merge via |
repo:objectstack#1 · os-elon-musk · session_016WV4cwPhGV4g9p1BGQaZbP
Fixes #37957
Clause-②: no
Metadata-protocol: none
维护者速读
What changes
Problem. A knowledge search re-checks every hit as the caller after ranking and refills once at
topK × 20. A caller who may read 20 of 1,000 indexed records gets none of them when they all rank below that cap.@objectstack/service-knowledge. Before the first round, for a caller who is not the system identity (and has an engine to re-check with), each object source is narrowed to the records that caller may read (sourcesNarrowedToVisible,readVisibleIds):where, as the caller:find(object, { where: source.where, fields: ['id'], limit: 501, context }).recordscope term holding them, so the adapter ranks only those rows.narrowToVisible, a pure function inknowledge-search-scope.ts), never appended: a one-record scope stays one record.metadata) term, the card's first option, since a union of terms cannot say "this parent AND these records" without an adapter contract change; a narrowing that names no record (so a caller who reads nothing is answered as before, no hit or the source's refusal, never a new kind of empty); a read that fails (logged atwarn); the system identity.@objectstack/knowledge-turso.passageTableDdladdsCREATE INDEX TABLE_record ON TABLE (source_record_id). New generations carry it; an existing generation gets it from its next build. No migration.@objectstack/knowledge-pgvector. Its DDL lacked the index too:vectorTableDdladdsCREATE INDEX IF NOT EXISTS "TABLE_record" ON TABLE (source_id, generation, source_record_id)(the table is shared by every source and generation, hence the prefix).Conformance harness (
@objectstack/service-knowledge/testing): its engine double answers the caller's visible-id read under the case'swall, and one new case runs the narrowed search through each real adapter.Docs:
content/docs/protocol/knowledge.mdx(the re-check paragraph, both stores' table lines). Changeset: patch on the three packages.Read first
packages/services/service-knowledge/src/knowledge-search-scope.ts,narrowToVisible: the intersection. A union here would widen a caller's own scope; ablation A1 below.packages/services/service-knowledge/src/knowledge-service.ts,sourcesNarrowedToVisible/readVisibleIds: the read as the caller, the 500 bound, the fall-backs.packages/plugins/knowledge-pgvector/src/sql.ts: the new index on an existing shared table (see Acceptance notes).Tests
All on head
27b7a296bb(after mergingorigin/main9726bd8), underscripts/pm/os-verify-lock.sh.Package suites and typechecks: all exit 0
The pgvector live-Postgres files skip here (no Postgres server in this container): the pgvector index and the conformance case through pgvector are NOT MEASURED locally.
New pins:
knowledge-service.test.ts, describe "a narrow caller's search ranks only the records they may read" (13 cases): 20 of 1,000 readable, all ranked 900th to 919th, answered best first in one round; more than 500 readable takes the path before (control: none reached); exactly 500 narrows, 501 does not; a one-record scope stays one record; mixed readable/unreadable scope ids keep only the readable, in the caller's order; no readable id in scope, and no readable record at all, search as before; a parent term reads nothing and hands the caller's terms unchanged; the system identity reads nothing and is handed no scope; the organization filter is handed beside the narrowed term, equal to the unnarrowed path's; the source'swhereis the read'swhere, one read for two sources sharing it; a failed read warns and searches as before; a record the read answered but the re-check refuses is not served.knowledge-search-scope.test.ts:narrowToVisible(7 cases).testing.ts, through memory and turso): a caller who may read only the two clauses every other outranks is answered from them past the refill cap; their one-record scope stays one record.knowledge-tursoacceptance:EXPLAIN QUERY PLANof the adapter's own scoped search SQL (captured from the client) showsUSING INDEX TABLE_recordand no scan of the passage table.Fixture updates (same assertions, now naming the extra caller read): three pins that counted the caller's reads (
drops hits the user cannot read,the permission re-read stays one query as the caller,passes the caller's context to the row-level re-check) now see the visible-id read before the re-check.Ablations: each mutation reds its pins, then restored (git status clean after)
Measurements (amendment)
Scratch script, not committed: 100,000 passages (one per record) bulk-loaded into one turso generation table (
file:database, 64-wide fake embedder), the adapter's own search SQL captured and planned. Shared box, so read the ratios.EXPLAIN QUERY PLAN, 20 visible ids of 100k passages
Adapter search latency, median of 7 (index dropped: median of 3)
The 500 cap is not raised. 500 to 2,000 ids costs about 2.6x on the vector leg (3.3 to 8.5 ms) and about 1.1x on a keyword-heavy query.
Acceptance notes
MULTI-INDEX ORover the FTS matches), so a query of very common words still costs FTS-match time. Measured, not filed: no contract or recall defect.CREATE INDEX IF NOT EXISTSruns on an existing table at the first write or build per process after the upgrade. It is notCONCURRENTLY, so writes to that table wait while it builds. Same shape as the existing keyword index's creation.finddoes not promisemin(limit, remaining)rows (anafterFindhook may drop rows from a page). If a hook drops rows from the 501-row page, a caller with more than 500 rows could be read as having 500 or fewer, and their search narrowed to a subset. Recall only, never exposure: every hit is still re-checked. The platform's row security is pushed into the query, not intoafterFind.TABLE_recordshares SQLite's table/index namespace with a legacy table nameknowledge_SOURCEonly for a source id ending in_gN_record. The existingTABLE_documentindex has the same property.node scripts/pm/overlap-check.mjson the branch before the merge:VERDICT clean exit 0againstorigin/mainand feat(knowledge)!: refuse a knowledge_source save that names an adapter the running deployment does not mount #38060, feat(knowledge-turso): an approximate vector index for a large source of one organization #38056, docs(knowledge): the 18 to 19 id row names the underscore exception and the dropSource cleanup #38050, fix(lint)!: knowledge-source review follow-ups: absent platform objects refused at publish, ai_context accepts id, search refusal names list #38038, feat(metadata-core)!: a knowledge source is saved only on a deployment that mounts the knowledge service #38013. feat(knowledge-turso): an approximate vector index for a large source of one organization #38056 (F6) sharessql.tsand the turso acceptance file and merges cleanly. Whichever lands second merges main.Verification
Two minutes:
pnpm --filter @objectstack/service-knowledge exec vitest run --maxWorkers=2 src/__tests__/knowledge-service.test.ts -t "narrow caller". Expect 13 passed. Thenpnpm --filter @objectstack/knowledge-turso exec vitest run --maxWorkers=2 src/__tests__/acceptance.test.ts -t "source_record_id index". Expect 2 passed (:memory:andfile:).Local gate families (
dispatch-gates --commands) NOT MEASURED: the run was denied by the session's action classifier. CI runs them all.Generated by Claude Code