Skip to content

knowledge.search can rerank its fused hits, per request or by deployment default - #223

Merged
zlareb1 merged 21 commits into
mainfrom
feat/knowledge-search-rerank
Nov 3, 2026
Merged

zlareb1 merged 21 commits into
mainfrom
feat/knowledge-search-rerank

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Related: #206
Related: #193

Builds on #206 (merged): it reads the chunk context that PR stores.

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 takes rerank (a builtin reranker or a configured endpoint, as memory.search does) and a deployment default LOOM_KNOWLEDGE_SEARCH_RERANK_DEFAULT; the HTTP and MCP response's reranked_by names the reranker that ordered the hits.

Why

memory.search and code.search can rerank their fused candidates; knowledge.search could not, so document retrieval stopped at reciprocal-rank fusion. On the labelled fixture of #193, fusion puts the right passage first on fewer queries than the vector leg alone, and a reranker over the fused head fixes that and more.

Measured with bench/knowledge_retrieval.py (#193: 21 synthetic documents, 76 answerable queries; text-embedding-3-large; one ingest on this branch's server, three search arms on it):

Arm R@1 R@5 R@10 MRR nDCG@10 context MRR paraphrase MRR identifier MRR
hybrid (default) 0.645 0.921 0.961 0.770 0.784 0.790 0.643 0.798
hybrid, rerank=builtin:openai 0.987 0.987 0.987 0.987 0.987 1.000 0.938 1.000
semantic 0.855 0.987 1.000 0.911 0.916 0.974 0.969 0.697
PYTHONPATH=$PWD .venv/bin/python bench/knowledge_retrieval.py --base-url <server> --namespace knowledge-retrieval-<name> --phase ingest
PYTHONPATH=$PWD .venv/bin/python bench/knowledge_retrieval.py --base-url <server> --namespace knowledge-retrieval-<name> --phase search --param search_mode=hybrid
PYTHONPATH=$PWD .venv/bin/python bench/knowledge_retrieval.py --base-url <server> --namespace knowledge-retrieval-<name> --phase search --param search_mode=hybrid --param rerank=builtin:openai
PYTHONPATH=$PWD .venv/bin/python bench/knowledge_retrieval.py --base-url <server> --namespace knowledge-retrieval-<name> --phase search --param search_mode=semantic

Paired by query against the hybrid arm on the same ingest (bench.bootstrap.paired_bootstrap over the fixture's 36 query clusters, 95%): R@1 +0.342 [+0.208, +0.483], MRR +0.217 [+0.134, +0.307], nDCG@10 +0.203 [+0.136, +0.276]; 26 queries improve at R@1 and none get worse.

The treatment ran: replaying the 81 fixture queries against the same server with rerank=builtin:openai, 69 answered reranked_by: "builtin:openai" and 12 answered "", which is the reranker handing back the fused order unchanged (the server logged no rerank failure or time-limit cut). The harness checks only ignored_args; it does not count reranked_by. The cost is one LLM call per search over 30 candidates of up to 800 characters: about 2 s per query here, against tens of milliseconds without it. That is why the deployment default stays empty: a deployment opts in, or a caller asks per request.

Design

  • KnowledgeSearchInput.rerank: RerankName (the type memory.search and code.search use): "" takes knowledge_search_rerank_default, "none" turns it off, otherwise a builtin (builtin:openai[:<model>], builtin:cohere[:<model>]) or a name in LOOM_RERANK_ENDPOINTS_JSON. A URL is refused at validation (422).
  • The rerank reuses server.memory.app.rerank.rerank: named endpoints only, the model client's gateway rules, and every failure hands back the input, which the handler reports as no rerank. The call is bounded by knowledge_search_rerank_timeout_s (8 s), since a builtin:openai call is otherwise bounded only by the model client's timeout and retries; past it the fused order is kept and reranked_by is empty. The cut is the search's own deadline, so it records no provider failure in health.
  • Under LOOM_READS_LLM_FREE a read reranks only as its request names (the deployment default is a model call), and a request-named builtin:openai is dropped before the legs widen their pool, as memory.search does.
  • The deployment default is checked with the same check_rerank_name a request's value passes: a URL or an unconfigured name is no reranker (logged once per value), and "none" is none, so the pool is not widened for nothing. builtin:cohere behind a required gateway fails at boot, as it already does for LOOM_CODE_SEARCH_RERANK_DEFAULT.
  • reranked_by is set only when the reranker's order differs from the fused one (it is empty when no reranker ran, it failed, or it kept the fused order): the service hands back its input, or a copy in input order, when it fails or cannot read a reply.
  • When a reranker applies, both legs fetch at least knowledge_search_rerank_top (30) hits (the page's own over-fetch for collapsing sections is not multiplied on top), so the reranker reads a head larger than a small page. Each candidate is the chunk's stored context (title, description, breadcrumb, from knowledge.search's lexical leg scores each chunk's title and section path with its text #206) followed by its text, cut at knowledge_search_rerank_excerpt_chars (800), with the context capped at half of it so a long description cannot crowd out the chunk's own text: the context is what tells a reranker that a "Refunds" section belongs to one product's handbook.
  • The reranked order is applied before collapse_sections and the top_k cut; score keeps the fused value, and KnowledgeSearchOutput.reranked_by says the order came from a reranker.
  • Settings are LIVE, in the knowledge section next to the other knowledge_search_* fields, as code_search_rerank_default sits with the code settings.

Scope and risk

knowledge.search, plus one change to the shared rerank service: a builtin:cohere reply whose JSON is not an object keeps the input order instead of raising, for memory.search and code.search too. Callers that do not name rerank (the op itself and loom_search's knowledge section) take the deployment default when one is set, and pay its latency once per call (a caller that searches again to widen its read pays again, and loom_search then reports the wider search's reranked_by; each loom_search page runs the knowledge leg again and pays it too). The other callers pass rerank="none": memory.ask's knowledge passages and its synthesize-route probe (the ask's synthesis reads the passages, and a rerank would run before its cost cap and synthesis switch are checked), context.pack's knowledge section (a pack runs on every chat turn), and loom_search's repository-docs leg, which runs under knowledge_repo_docs_timeout_s (1.5 s), which a model call would outlast. When that leg adds hits to a reranked answer, the fused scores still decide which repository hits earn a place (the rule of the unreranked merge), and the reranked hits keep their order ahead of them. With the default empty and no rerank on the request the ranking is unchanged; both legs now also select the chunk's context column. The op's 250 ms latency budget does not hold for a call that reranks with an LLM; a deployment that sets the default accepts that.

Compatibility, rollout and rollback

  • Wire: additive. knowledge.search input gains rerank (default ""); output gains reranked_by (default ""). N-1 ignores an unknown rerank as an ignored_args read field.
  • Operator steps: none. To rerank by default, set LOOM_KNOWLEDGE_SEARCH_RERANK_DEFAULT (for example builtin:openai, or an endpoint name from LOOM_RERANK_ENDPOINTS_JSON).
  • SDKs: the Python SDK's knowledge.search() returns the hit list only, as it does for namespaces_skipped; a caller passes rerank through its **extra and reads reranked_by from the raw op (loom.ops, or HTTP/MCP).
  • Rollback: revert; nothing is stored.

Tests

  • Unit tests/unit/test_knowledge_search_rerank.py: which reranker a call uses (request, default, "none", LLM-free reads, a default no request may name), a URL refused, a held model call failing the search, sections collapsing after the rerank, the head reordered with each candidate's context before its text and the tail kept, a failed rerank keeping the fused order with reranked_by empty, the legs fetching the rerank head for a smaller page, a reranker past its time limit keeping the fused order, a Cohere reply without a results list (a list, a string, or an object whose results is not a list) keeping the input order.
  • Unit tests/unit/test_repo_docs.py: the repository leg passes rerank="none", and a reranked answer admits repository hits by fused score and keeps its own order. tests/unit/test_llm_gateway.py: either rerank default naming builtin:cohere behind a required gateway fails at boot. tests/unit/test_assistant_learnings.py and test_ask_knowledge_plane_tags.py: a pack's and an ask's knowledge reads pass rerank="none". test_a_pinned_tokens_loom_search_serves_its_signed_bots_rules: each loom_search pass names its own reranker, and the section reports the last (wider) pass's reranked_by.
  • Integration tests/integration/test_knowledge.py: rerank="none" answers reranked_by: ""; a URL is a 422. Ran against this branch's server on its own ClickHouse.
  • Generated: settings reference, settings schema and example, SDK op tables, operations reference; tests/unit/snapshots/live_settings.json and settings_defaults.json updated.
  • make format lint typecheck, pytest tests/analyzer; unit files around search, settings, package contracts, docs and public copy: 721 passed.
  • The fixture table above.

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 measurement and the integration run were made locally.

  • 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.
  • 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-contextual-lexical branch from dcc15aa to f99c415 Compare November 3, 2026 15:59
@zlareb1
zlareb1 force-pushed the feat/knowledge-search-rerank branch from 37a3877 to a1be47c Compare November 3, 2026 16:07
@zlareb1
zlareb1 force-pushed the feat/knowledge-contextual-lexical branch from 7ec43f2 to b390b3d Compare November 3, 2026 16:44
@zlareb1
zlareb1 deleted the branch feat/knowledge-contextual-lexical November 3, 2026 16:44
@zlareb1 zlareb1 closed this Nov 3, 2026
@zlareb1 zlareb1 reopened this Nov 3, 2026
@zlareb1
zlareb1 changed the base branch from feat/knowledge-contextual-lexical to main November 3, 2026 16:46
@zlareb1
zlareb1 force-pushed the feat/knowledge-search-rerank branch 5 times, most recently from 8a8d81c to 23a0d9f Compare November 3, 2026 19:37
zlareb1 and others added 18 commits November 3, 2026 20:31
…ent default

A request's rerank (or LOOM_KNOWLEDGE_SEARCH_RERANK_DEFAULT) names a reranker:
a builtin or an endpoint in LOOM_RERANK_ENDPOINTS_JSON, as memory.search and
code.search take. The legs fetch at least the rerank head
(knowledge_search_rerank_top), each candidate shows the chunk's stored context
before its text, and the reranker's order is the page before sections collapse.
reranked_by names the reranker that produced the order; a failed rerank keeps
the fused order and leaves it empty.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…y-docs leg and defaults no request may name

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…nothing; a reranked answer keeps its order beside the repository's docs

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…age, and the legs fetch the rerank head once

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…t checks each chunk's context reaches the reranker

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…tory merge's comment says what it gives

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…y and LLM-free pages name the knowledge default

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]>
…nothing; the shared reranker names its callers

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ked merge does

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… what a rerank orders, and a held call is tested through the search

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

LOOM_KNOWLEDGE_SEARCH_RERANK_TIMEOUT_S (8 s) bounds the rerank call; a
Cohere reply that is not a JSON object keeps the input order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
zlareb1 and others added 3 commits November 3, 2026 20:31
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/knowledge-search-rerank branch from 23a0d9f to f30163b Compare November 3, 2026 20:39
@zlareb1
zlareb1 merged commit 71378a6 into main Nov 3, 2026
21 of 22 checks passed
@zlareb1
zlareb1 deleted the feat/knowledge-search-rerank branch November 3, 2026 21:09
@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