Skip to content

Hybrid retrieval: BM25 candidates, graph evidence, evidence-based refusal - #52

Draft
Deep-Sixed wants to merge 3 commits into
mainfrom
claude/peaceful-rubin-1bpmhh
Draft

Deep-Sixed wants to merge 3 commits into
mainfrom
claude/peaceful-rubin-1bpmhh

Conversation

@Deep-Sixed

Copy link
Copy Markdown
Owner

What

HybridRetriever (contextmesh/hybrid.py) works in four steps:

  1. Candidates: BM25 finds candidate claims, from live claims only.
  2. Re-ranking: the candidates are re-ranked.
  3. Evidence or refusal: the answer comes with its evidence path (question entity → claim → source, over live edges, with the file location), or the question is refused when the best candidate covers too little of it.
  4. Close calls: they carry one runner-up, paid for in tokens.

It's benchmarked against graph-only and BM25-only on both development suites and on held-out suites frozen before any hybrid code existed. Separate from #50 and #51, as asked. Full write-up: docs/BENCHMARKS.md → Hybrid retrieval.

Order of work (visible in the commits)

  1. eea2b08: held-out suites frozen first. bench/heldout-docs.json covers README sections the docs suite doesn't ask about; bench/heldout-code.json covers contextmesh_mcp/, which no other suite reads.
  2. cb49b46: implementation and calibration on the development suites only. The calibration searched 21,289 grid settings, constrained to stay under BM25 top-3's tokens on each suite. Its output is committed as ops/calibrate-hybrid.log.
  3. eb441c7: results. The held-out suites were run once, on cb49b46.

Results against the acceptance criteria

Criterion Target Result
Docs accuracy exceed 22/32 Not met. 22/32, tying BM25 top-3. Held out: 3/10, against BM25 top-1's 4/10.
Code accuracy at least 15/18 Not met. 13/18, against BM25 top-3's 15/18 in the same run. Graph-only is 12/18 after #51's hub fix. Held out: 7/10, against 8/10.
Out-of-scope refusals maintain or improve Met. 8/8, 5/6, 4/4, 4/4, against BM25's 3/8, 2/6, 2/4, 2/4.
Tokens below BM25 top-3 Met on every suite (73/91, 80/96, 48/95, 84/94).
Provenance complete evidence paths Met, and tested.
Invalidation never retrieve retired claims Met, and tested on rejection and on refresh.
Reproducibility same inputs, same results Met. Asking is read-only via Resolver.match, doesn't depend on order, and leaves the graph and resolver byte-identical (tested).

Held-out docs, per method: the hybrid got 3 correct, 3 wrong and 4 refused. BM25 top-1 got 4 correct and 6 wrong.

It's a precision gain, not an accuracy gain. Of the 4 refused, BM25 top-1 got 1 right, so the hybrid traded one correct answer for three fewer wrong ones.

The lexical coverage threshold overfits. It refused 4 of 10 in-scope questions on held-out docs, which are paraphrased, and none on held-out code.

Calibration gave the graph's re-ranking signals (entity overlap, embedding similarity) a weight of 0. On these suites, the graph contributes the evidence path, live-only retrieval and invalidation, not the choice of answer.

Cost: about 1 ms per question against the walk's 12–16 ms. The index peaks at about 1.2–1.4 MB for 800–1,000 claims.

Changes

  • contextmesh/lexical.py: BM25 shared by the baseline and the hybrid, plus coverage(), an absolute IDF-weighted measure in which unseen terms count at the highest IDF.
  • contextmesh/hybrid.py: the retriever, with defaults from calibration.
  • contextmesh/bench.py:
    • the hybrid is added as a method;
    • every method reports wrong answers, out-of-scope refusals and latency;
    • the hybrid's index memory and parameters are recorded;
    • build_corpus / evaluate let one built corpus be scored many times.
  • ops/calibrate-hybrid.py and ops/calibrate-hybrid.log. The log isn't a .txt file, so it isn't ingested into the docs corpus.
  • Tests:
    • tests/test_hybrid.py, 10 tests: evidence path over live edges, entity-first paths, token accounting, both kinds of refusal and the configurable threshold, runner-up limits, rejected and refreshed-away claims never retrieved, and read-only asking in any order;
    • tests/test_bench.py: every shipped suite validated structurally without running the held-out ones, held-out questions disjoint from the development suites, and machine-dependent figures excluded from the determinism check.

Live-only retrieval, refusal and read-only asking were mutation-checked. The full suite, ruff, the demo export (byte-identical) and the leak check pass.

Not in this PR

Wiring the hybrid into ask or MCP mesh_ask. Whether it should replace or sit beside the walk is a product decision these results don't settle.


Generated by Claude Code

Committed first, so the record shows these questions predate the
implementation they will evaluate.

- bench/heldout-docs.json: 10 answerable + 4 out-of-scope questions
  written from README sections the contextmesh-docs suite does not ask
  about (checkpoints, restart, health, re-execution, persistence,
  dashboard).
- bench/heldout-code.json: 10 + 4 over contextmesh_mcp/, a package no
  other suite reads, written from its raw docstrings.

Rules, stated in each suite: never used to choose or calibrate anything,
run once to evaluate. Same author as the code, so not independent;
questions from someone who has not seen the implementation are wanted.

tests/test_bench.py now checks every shipped suite structurally (labels
point at real files of the suite's own types, no duplicate questions)
without running the held-out ones, and that held-out questions share
none with the development suites.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hin evidence

contextmesh/hybrid.py (HybridRetriever):
- Candidates: BM25 top-k over the graph's live claims only; the index is
  rebuilt when the build, node count or invalidated count moves, so a
  retired claim is never served as current evidence.
- Re-ranking: relative BM25 score, idf-weighted coverage of the question,
  entity overlap with the question's resolved entities, embedding
  similarity; fixed weights.
- Evidence: the answer comes with its path over live edges (question
  entity -> claim -> the source it was lifted from) and file location.
  Close calls return up to max_answers runners-up within a margin, each
  paid for in tokens.
- Refusal: below min_coverage of the question's terms (unseen terms count
  at the highest idf), refused with the coverage stated. A path is never
  taken as proof.
- Read-only: names resolve with Resolver.match, so asking neither logs
  nor learns; answers do not depend on question order.

contextmesh/lexical.py: BM25 shared by the benchmark and the hybrid, plus
coverage(). contextmesh/bench.py: the hybrid as a method; every method now
reports wrong answers (answered, incorrect), out-of-scope refusals and
mean latency; the hybrid's index memory and parameters are recorded.

ops/calibrate-hybrid.py chose the defaults on the development suites only
(grid of 21,289 points meeting the constraint that mean tokens stay under
BM25 top-3's on each suite); its output is ops/calibrate-hybrid.log.
Entity overlap and embedding similarity earned no weight there. The
held-out suites were not read; this commit precedes running them.

tests/test_hybrid.py: evidence path over live edges, entity-first paths,
token accounting, both refusals and the configurable threshold, runner-up
limits, rejected and refreshed-away claims never retrieved, and asking
leaving graph and resolver byte-identical in any order. Live-only,
refusal and read-only were mutation-checked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…criteria

Development suites (calibrated on) and the held-out suites (run once, on
committed code cb49b46, never calibrated on), each method scored on the
same corpus in the same run, plus the acceptance criteria one by one:

- Not met: docs accuracy (22/32 ties BM25 top-3; held out 3/10 vs BM25
  top-1 4/10) and code accuracy (13/18 vs BM25 top-3 15/18; held out 7/10
  vs 8/10).
- Met: out-of-scope refusals (8/8, 5/6, 4/4, 4/4 vs BM25's 3/8, 2/6,
  2/4, 2/4), tokens below BM25 top-3 on every suite, complete evidence
  paths, no retired claim retrieved, reproducible read-only asking.

The hybrid is a precision gain, not an accuracy gain: on held-out docs it
traded one correct answer for three fewer wrong ones. The lexical coverage
threshold overfits (4/10 in-scope refusals on held-out docs, 0 on held-out
code). Calibration gave the graph's re-ranking signals no weight; the
graph's contribution here is the evidence path, live-only retrieval and
invalidation. Latency ~1 ms vs the walk's 12-16 ms; index ~1.2-1.4 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

2 participants