Repository navigation
Under the Postgres authority knowledge search serves only the chunks each document's head serves - #441
Merged
Conversation
zlareb1
force-pushed
the
feat/knowledge-pg-search
branch
4 times, most recently
from
November 9, 2026 12:00
7632fdb to
d0de4a6
Compare
9 tasks done
zlareb1
force-pushed
the
feat/knowledge-pg-search
branch
2 times, most recently
from
November 9, 2026 21:14
ae24b3f to
a815d4b
Compare
…each document's head serves Each knowledge head records the revision and version of the last commit that inserted its chunks (pg migration 004). knowledge.search, knowledge.outline, document_chunks and memory.stack keep only the chunks their head serves, refill a short page once, and knowledge.search reports stale_dropped. Under that authority knowledge.add and bulk_add retire older chunk versions, as upsert does. Related: #157 Signed-off-by: zlareb1 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
zlareb1
force-pushed
the
feat/knowledge-pg-search
branch
from
November 9, 2026 23:31
a815d4b to
7339783
Compare
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.
Related: #157
Related: #405
Changelog category (leave one):
Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):
Under the Postgres storage authority,
knowledge.search,knowledge.outline,knowledge.synthesizeandmemory.stackserve only the chunks of each document's current version (a document written before its namespace had heads keeps serving every version ClickHouse holds): an older version still being retired, a deleted document whose delete is not yet published, and a write that has not committed are left out, andknowledge.searchcounts them in a newstale_droppedfield. Under that authorityknowledge.addandknowledge.bulk_addretire a source's earlier chunk versions, asknowledge.upsertdoes.Why
#405 made Postgres the authority for each knowledge document's current version (its head), but search still read
loom.chunksalone. ClickHouse reads a chunk as live until something retires it, so under the Postgres authority it also serves:This PR has search ask the head which chunks it serves, as
memory.searchalready asks the memory heads. It is the second PR of #157's Postgres work (P11b in the issue's plan).Design. A head records which chunks it serves. Postgres migration
004_knowledge_chunk_heads.sqladds two nullable columns toloom.knowledge_heads:chunks_revisionandchunks_version.chunkedargument marks them.COALESCEin the update): a delete, a metadata edit without reindex, or a restore.A chunk of revision
rat versionvis served when:ris the head's revision, and, whilechunks_revisionis that revision,vischunks_version(server/knowledge/domain/serving.py).The version tells apart two writes that numbered the same revision, where the second was refused. A chunk that recorded no revision (
r = 0) is judged by its version alone whilechunks_revisionis the head's revision, and served otherwise.Alternatives considered:
loom.documents. A ClickHouse migration, a value N-1 would have to write, and still no answer for the chunks of a write that has not committed.Reads.
pg_documents.servingreads the heads of a page's(namespace, source_uri)pairs in oneunnestjoin.knowledge.searchchecks each search pass (one statement over every namespace it reads, judged in one Postgres read) before it reranks, collapses sections or cuts the page. A full page that left a candidate out (a stale one, or a second served version of one chunk) is fetched once more, twice as deep (pg_serving.served_search), andstale_droppedcounts the stale ones.doc_version; under the Postgres authority fusion keys a chunk by it (below).knowledge.outlineanddocument_reads.document_chunks(read byknowledge.synthesizeandloom decisions extract) take the same check, and so doesmemory.stack's knowledge section (server/memory/app/stack.py), whose search callable refills the same way.context.packandmemory.askread throughknowledge.search.knowledge.getalready serves the head (Under the Postgres authority each knowledge document's head is committed in Postgres over the version its writer read #405).Scope and risk
pg_serving.stalereturns all-False without a read,keep_servedreturns every row of the outline anddocument_chunkswithout a read, and fusion merges as before.knowledge.addandknowledge.bulk_addretire the source's older chunk versions (retire_older), asknowledge.upsertdoes, a first write's included (a failed write may have left chunks behind): search serves the head's version alone, so keeping them would leave chunks nothing serves, and they would crowd every page as stale candidates (the refill fetches once, twice as deep). This matches whatknowledge.getalready serves. Under the ClickHouse authority an add again keeps them, both served, as before.fuse_rrf(by_version=...)), so the check sees the stale and the served version of one chunk as two candidates; a merged one would show one version's text under the other's identity. The page then keeps one hit per chunk, the best ranked, when two versions of one chunk are both served (a source with no head, or a head whose recorded chunks belong to an earlier revision) (served_search), and a version left out that way counts toward the refill. The reranker shows each hit its own chunk's context (carried on the hit). Under the ClickHouse authority fusion merges as before.mixed_live_versionsis labelled "Two live versions" (it was "Two versions served"), and a document's chunk line says "N live versions" (it said "N versions served"): under the Postgres authority search serves one of them. The documents section is headed "Live chunks" (it was "What search serves"), and the Assistant page's subtitle says "live chunks".knowledge.inspect's MCP description, field comments and docstrings say the same.POSTGRES_UNAVAILABLE, as a memory search does;memory.stackleaves its knowledge section out instead, as it does for any knowledge failure.knowledge.inspect(the view that shows a retirement still owed),knowledge.list'schunk_count,knowledge.dedup_listand an upsert'sduplicate_chunk_count.knowledge.getwas already head-backed.Compatibility, rollout and rollback
knowledge.searchgainsstale_dropped(int, 0 under the ClickHouse authority). The behaviour change above is named in the changelog entry.docs/architecture/rolling-upgrades.md.mainwriteschunks_revisionorchunks_version.Tests
tests/unit/test_knowledge_serving.py:document_chunksshowing the head's version only.memory.stack's knowledge section serving the head's version only and fetching again, deeper (it fails when the stack calls the search without the check).document_chunks(which synthesize reads) whose heads read fails answeringPOSTGRES_UNAVAILABLE(503), not unchecked chunks.tests/unit/test_knowledge_pg_commit.py: a chunked commit sends its revision and version for a new and an existing head, and another commit sends NULLs (the integration test pins that they keep the recorded chunks).tests/unit/test_knowledge_dedup.py: upsert, add and bulk add passchunked=True; under the Postgres authority an add and a bulk add passretire_older, a first write's included, and under the ClickHouse one they do not.tests/unit/test_knowledge_ingest.py: the ingest publish passeschunked=True.tests/unit/test_knowledge_search_service.py: every leg selectsdoc_versionand the hit carries it.tests/integration/test_knowledge_pg_heads.pygains new tests against a Postgres-authority server and its ClickHouse (five functions, two of them run two ways):document_chunksshow the head's version alone, andstale_droppedcounts the other;tests/integration/test_knowledge_inspect.pybuilds its two live versions with an add again; under the Postgres authority that now retires the earlier one, so the test removes that version's supersession rows there, the state an older build's add again leaves. CI runs it under the ClickHouse authority, where it is unchanged.maind1ec764. Under Postgres, each revision's key, status and redaction live in typed columns that every journal reader but the profile reach matches on #445 merged first with Postgres migration003_revision_metadata.sql, so this PR's migration is renumbered004_knowledge_chunk_heads.sql, and the two docs that name it follow; nothing else changed in the rebase (Under Postgres, each revision's key, status and redaction live in typed columns that every journal reader but the profile reach matches on #445's own edits are to memory statements and other sections ofstorage-authority.md).-n 4 -p randomlyon a loaded host: 32269 passed. 11 tests failed in the parallel runs and passed when rerun alone (--lf); none is in a file this PR touches, and 7 are clickhouse-local SQL tests (test_knowledge_inspect_ingest_sql.py,test_memory_keys_sql.py,test_erasure_user_chunks_sql.pyamong them). That run was onmain92a2f2b. Under Postgres, every reader takes a field a stored row leaves out as its loom.memory default #455 merged since; it sharesstorage-authority.mdwith this PR, in other sections. On the head rebased onto it,make lintandmake typecheckpass, and this PR's and Under Postgres, every reader takes a field a stored row leaves out as its loom.memory default #455's unit test files with the doc tests give 1227 passed.tests/unit/test_pg_authority.pyandtest_doc_links.py: 556 passed;test_knowledge_serving.pyandtest_knowledge_pg_commit.py: 39 passed.test_knowledge_pg_heads.py(34);-n 3 -p randomly;make format,make lint,make typecheck, the frontend's tests (2268), build and budget passed.loom.chunks(server/insights/app/provider_rebuild_batch.py), so a chunk keeps itsdoc_versionand revision, andloom rechunkwrites throughknowledge.upsert, which passeschunked=True. Nothing else changes a stored chunk'sdoc_versionor recorded revision: the erasure saga's chunk steps only retire or delete chunks, and a chunk's noise label is written with the chunk and never backfilled.documents_repo.insert_one/insert_many) is inwrites.py,restore.pyandingest_run.py; each one that inserts chunks passeschunked=True.pytest tests/analyzerpasses against a migrated ClickHouse afterpython -m tests.analyzer.corpus --write. The corpus is unchanged: the search statements only select one more column.AI assistance in the code (leave one; writing this description with AI is fine):
AI-generated (an agent wrote most of the diff): Claude Code. The serving rule was checked against every writer through the commit path (which ones insert chunks, which keep the revision) and the tests were run against a Postgres-authority server.
I can explain every line of this diff and how it interacts with the rest of Loom.
Checklist
make format-check,make lint,make typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.docs/reference/configuration.mdand thedocs/reference/tables (python scripts/codegen/docs_reference.py), the reach manifest,schemas/schema.sql(make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).main; no merged migration is edited.hooks/,lib/,bin/,skills/,.mcp.json, the Codexplugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.npm --prefix frontend run build) and its tests pass, iffrontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code