Skip to content

knowledge.search cites each hit's revision and locator, filters by format and block type, and finds exact keys - #252

Merged
zlareb1 merged 16 commits into
mainfrom
feat/knowledge-structural-search
Nov 4, 2026
Merged

zlareb1 merged 16 commits into
mainfrom
feat/knowledge-structural-search

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Related: #157

Changelog category (leave one):

  • New Feature

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):

knowledge.search hits name the document revision and format they came from and, for an ingested file, the block type and locator (lines, key path) of the passage. New formats and block_types filters narrow a search, and an exact-key leg finds the chunk that defines or reads an identifier (events, server.port). knowledge.synthesize lists each cited chunk with its revision and locator.

Why

Ingestion (#229) publishes extracted files as revisions whose chunks carry a locator, a block type and the exact names their blocks define or read (keys), but search did not read any of it: a hit could not say which revision it cited or where in the file it sits, a caller could not ask for SQL statements only, and an identifier such as orders_daily read to BM25 like daily_orders (the same tokens). This makes search use what extraction records.

Scope and risk

  • What a chunk records (server/knowledge/store/chunks.py): every chunk row's chunk_metadata now also holds the document revision it is written as and the document's format, so a hit names the revision its own text belongs to, not whichever revision is live when it is read (during a publish the new version's chunks are live before its document row). Text writes record their revision (bound once per write, for the document row and every chunk row) and ""; an ingest decides its revision before it inserts the chunks (ingest_run._publish), and under the source's claim refuses the publish (revision_conflict, the chunks retired) when the source has changed since (_check_unchanged: any other write moves its latest version) or its latest row would outrank this one. One reader parses the metadata for search, synthesis and the outline (server/knowledge/domain/chunk_metadata.py).

  • Hits (server/knowledge/app/search.py, knowledge_search.py): each leg reads the chunk's section path, revision, format, block type and locator. No document row is read.

  • Filters: formats and block_types are JSONExtractString(chunk_metadata, ...) IN ... on every leg, inside the sort-key prefix the leg already reads. Text a caller extracted has no block type, so any block_types entry leaves it out; its format is "", which formats can name.

  • Exact-key leg (keys_search, hybrid mode only): the query's identifier-shaped terms (server/knowledge/domain/exact_keys.py) against hasAny(keys, ...) with the keys bloom-filter index; the chunks matching the most keys first, then by distance. Its ranks join reciprocal-rank fusion as a third term (fuse_rrf(..., keyed=)), and a hit from it reports keys_matched. A term counts when it cannot be a plain word (an underscore, a dot, a $ or a digit, or a capital after a lowercase letter, so userId counts and the acronyms ID and TTL do not), or when it is the whole query; a dash joins the segments of a dotted path (pool.max-size, a TOML bare key) and nothing else. Keys are stored as their source spells them and each term is looked up as written and lowercased, so Events also finds events but not the reverse. Why plain words are out: SQL chunk keys include column names, and with every word a candidate a prose question's status or name pulled the SQL statements above the document that answers (measured below). A hybrid query with no BM25 token (id, db.ts) still runs the leg, fused with the vector leg. A hybrid search now returns fused scores whenever the lexical or exact-key leg ran, even when it found nothing: the passes of a private-first fan-out are sorted together, and a pass of raw similarities (about 0.1-0.3) beside a fused pass (about 0.016-0.05) used to put every public vector hit above a private exact-key hit. Semantic and lexical modes are unchanged.

  • Synthesize: cited_chunks lists each chunk the kept citations name (a range expands over the chunks read), with the revision and locator its chunk recorded when it was read. Citation grounding is unchanged.

  • Outline: it reads the section path each chunk records (which the structural chunker writes) through the same reader now, with its depth and the text-only fields (section_idx, char_start), so the outline, search and synthesis agree on a chunk's section; an empty heading is no path segment in any of them.

  • No new table, column, setting or migration.

  • Architecture page: docs/architecture/document-extraction.md says each chunk records its revision and format, and gives the publish order as it now is.

  • Agent-facing wording: the assistant toolset's knowledge.search description, the assistant primer, docs/reference/mcp.md and the LibreChat integration page now say a hit carries the revision its passage was written as (and an ingested file's hit its locator), and that a citation names it, citing none for a revision of 0 (a chunk written before revisions were recorded); they said the revision came from knowledge.get, which returns the live one. The core toolset's knowledge.search description names the exact-key leg, the new hit fields and the two filters.

Compatibility, rollout and rollback

  • Wire: knowledge.search gains input fields formats and block_types (both default empty) and output fields revision, format, block_type, locator, keys_matched per hit; knowledge.synthesize gains cited_chunks. No field is removed or changed.
  • Stored data: chunk metadata gains revision and format keys. A chunk written before (or by build N-1 during a rollout) records neither and reads revision 0 and format "" (so a formats filter naming sql leaves it out) until its document is written again; nothing is backfilled.
  • Operator steps: none. A hybrid search whose query has an identifier runs one more statement (the exact-key leg).
  • Rollback: chunks keep the two extra metadata keys, which an earlier build ignores.

Tests

  • Unit tests/unit/test_knowledge_structural_search.py (13): the query's candidate keys (identifiers kept, plain words and acronyms dropped unless the whole query, a dashed TOML path kept whole and prose dashes not); the exact-key leg's statement and the structure it reads; a keyed hit's fusion term and keys; the two filters on all three legs; the handler fusing the keys leg, a semantic search running none, a query with no BM25 token still running it, and a cold corpus (no lexical hit) fusing the vector and exact-key legs; a private-first fan-out whose private pass has a keyed hit and whose public pass has vector hits only, for a query with no BM25 token and one with tokens, the keyed hit level with the public pass's first hit rather than under all of them; the metadata reader (with the outline's text fields); cited chunks expanding ranges; synthesize listing each cited chunk's revision and locator. tests/unit/test_knowledge_chunk_rows.py: add, upsert and bulk_add each record on every chunk the revision their document row takes (5 over a source at 4) and format "". tests/unit/test_knowledge_ingest.py: each published chunk records its revision and format; a write between the revision decision and the claim fails the publish and retires its chunks. The lexical tests stub the new leg where they stub the others.

  • Integration tests/integration/test_knowledge_structural_search.py (3), against a server built from this branch, a migrated ClickHouse and the reference extractor run as a service: a SQL file and a TOML file ingested; sessions and server.port each find the defining chunk first, with its keys, locator, block type, format and revision 1; formats=["sql"], formats=[""] and block_types=["sql_statement"] keep only those chunks. A prose question over a SQL file whose columns are common words and a document that answers it finds the document first, with no keys matched (the regression the comparison below found, pinned). test_knowledge_ingest_live.py passes with the new chunk metadata.

  • pytest tests/unit -k "knowledge or mcp or search or sdk or docs_reference or reach or synth or corpus or package_contracts or alias or tunable or inventory or public_copy or doc_links" and pytest tests/integration -k "knowledge or document or ingest or search or synth": 4,474 passed, and 149 passed, 58 skipped (settings this server does not set).

  • Retrieval comparison over the store functions (the fake n-gram embedder; the current fusion is vector + BM25):

    Corpus and queries current hit@1 (MRR) with the exact-key leg
    20 identifier questions, three SQL files of five tables (each also joined, inserted into and named in comments) and a TOML file, distinct names 20/20 (1.000) 20/20 (1.000)
    the same, names sharing their words (orders_daily / daily_orders, ...) 18/20 (0.950) 20/20 (1.000)
    8 prose questions over five prose documents and a SQL file whose columns are common words (status, name, value), every word a key candidate 8/8 (1.000) 0/8 (0.385)
    the same, identifier-shaped terms only (this PR) 8/8 (1.000) 8/8 (1.000)

    The leg puts the defining statement first where an identifier's words are shared, and changes nothing for prose once plain words are not candidates. The harness is a local script over the store functions (it fuses with and without the leg, which no shipped path does), so it is not committed; the corpora are as described, and the prose case is pinned by the integration test above.

  • The exact-key leg's cost, from system.query_log on a local ClickHouse: one namespace of 20,000 SQL statement chunks (3072-dim vectors), every one keyed status, among 200,000 chunks in 20 other namespaces of the org; top_k 10, three runs each.

    Statement read_rows read_bytes query_duration_ms memory
    vector leg 36,670 99 MiB 46-52 25 MiB
    exact-key leg, status (all 20,000 match) 30,006 120 MiB 82-84 60 MiB
    exact-key leg, t123 (one match) 3,336 3.5 MiB 15 10 MiB

    The worst case (a key every chunk of the namespace holds) stays inside the namespace's primary-key range and costs about what the vector leg already does; a selective key reads a tenth of it, since the keys bloom index skips the granules without it.

  • make format, make lint (generators current), make typecheck: pass.

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.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed. (The full unit suite passed locally at 63b9a90, before the last doc-only commit: 26,844 passed, 18 skipped, 1 xfailed; the knowledge integration tests pass.)
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/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).
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

@zlareb1
zlareb1 force-pushed the feat/knowledge-structural-search branch 8 times, most recently from 5626f11 to 425322f Compare November 4, 2026 12:22
zlareb1 and others added 16 commits November 4, 2026 13:25
…by format and block type, and finds exact keys

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… only for identifier-shaped terms

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ch write binds its revision once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…le; a cold corpus test

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…on tests skip without an extractor

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…unk lookup in synthesize

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…hunk's depth comes with its section path

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…n one scale

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…es its revision

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…nd a revision of 0 is cited as none

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… description's sentence

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…unk records

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/knowledge-structural-search branch from 63b9a90 to 9bf8f1f Compare November 4, 2026 13:26
@zlareb1
zlareb1 merged commit 24d31ee into main Nov 4, 2026
22 checks passed
@zlareb1
zlareb1 deleted the feat/knowledge-structural-search branch November 4, 2026 13:52
@pufit pufit mentioned this pull request Nov 4, 2026
9 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant