feat(consume): BM25 ranking by passage and title, a large concept delivered as its passage

C1. `okf consume` and MCP's `okf_ask` now rank with BM25 (`bm25.py`) instead
of the three-signal fusion. Two signals, fused by reciprocal rank:

- passage: every body cut into 500-character windows every 250, a concept
  scored by its BEST window -- a narrow question is answered in one place;
- field: title three times, the id path and source name twice, then the body
  -- a broad question is answered by what a section is called.

The document prior and the rarity weight are gone from the default: the first
favoured big documents full of common words, the second gave its largest
weight to a word the collection does not hold. Under BM25 such a word weighs
exactly zero. A signal that scores a concept zero adds nothing to it, and ties
share a rank, so alphabetical order lifts nothing either.

Three rules carried over from the fusion, each with its own test, because the
suite showed what BM25 alone lost:
- a directory every concept shares is not read (K3-20's defect, one signal on);
- a number a section is known by (`4.2`, `10.2-2`) is kept as one token, or
  a question naming a section by its number matches nothing in it;
- a question word the collection does NOT hold is read as the collection's
  words it shares a leading word with (`consume.tokens_match`) -- Norwegian
  inflection and compounding -- at that word's idf, never at its own.

The lookup and title-covered partitions are shared with the fusion
(`_partitioned`). `ranking="fusion"` / `--ranking fusion` keeps the old order
reachable; `--cost-vocabulary` and `--rarity-weight` widen only the fusion and
are refused with the default (`ranking_flag_conflict`) rather than ignored.

C3. A concept longer than `PASSAGE_CHARS` (4 000) is delivered as the span
around its best window, snapped to whole lines, under the nearest heading
above it, with `[...]` where text was left out. `passage: {start, end, of}`
says so, `text_sha256` covers what was delivered, and `sha256` stays the
file's, so the whole can be fetched by `concept_id`. 4 000 because eight
excerpts of it stay far under a tool response's limit even with several
sub-questions merged, while a 500-character window keeps 3 500 characters of
surroundings. The budget pays for the passage, not the file.

Tests moved with the default, each stated rather than silenced:
- fusion-mechanism tests (cost vocabulary, rarity weight, reservation, shared
  rank, the reference-bundle pins) ask for `ranking="fusion"`, the order they
  were measured on; the BM25 reading of the reference bundle is a separate
  measurement, kept in local state;
- the retrieval gate still measures the shipped default. Row 1 holds. Four of
  its premises were built against the fusion (a concept forced below k that
  BM25 now delivers, a quota that no longer decides, mutants patching fusion
  code) and are `xfail(strict=True)` until the fixtures are re-measured;
- the shipped example payload is regenerated; the shipped skill is unchanged.

README's Consume section and CLAUDE.md state the new default and that the
flags described after it belong to the fusion.

The search gate's table for this commit is kept in local state: the question
sets belong to a consumer whose content does not go on a public mirror.

Suite on a clean tree after `git add`: 2390 passed, 2 skipped, 4 xfailed.
ruff, ruff format, mypy --strict clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-09-21 06:22:56 +02:00
commit 735468f600
11 changed files with 988 additions and 118 deletions

View file

@ -949,7 +949,7 @@ def test_every_gold_document_in_the_local_set_is_reached_or_named_as_a_miss() ->
assert len(questions) >= 5, "fewer than five questions is not the measurement"
hits = 0
for entry in questions:
payload = okf_consume.build_payload(K2_BUNDLE, question=entry["question"])
payload = okf_consume.build_payload(K2_BUNDLE, question=entry["question"], ranking="fusion")
excerpts = payload["excerpts"]
assert isinstance(excerpts, list)
if okf_consume_measure.hit_rank(excerpts, entry["gold_document"]) is not None:
@ -1484,7 +1484,7 @@ def test_a_cost_question_reaches_no_price_concept_without_the_flag() -> None:
def test_the_cost_vocabulary_flag_bridges_a_question_and_a_document_that_share_no_word() -> None:
payload = okf_consume.build_payload(
FIXTURE, question="Hvor kan vi kutte kostnader?", cost_vocabulary=True
FIXTURE, question="Hvor kan vi kutte kostnader?", cost_vocabulary=True, ranking="fusion"
)
excerpts = payload["excerpts"]
assert isinstance(excerpts, list)
@ -1506,9 +1506,13 @@ def test_the_flag_changes_nothing_when_the_question_names_no_such_term() -> None
# The GATE is the question, never the flag: a question with no cost term
# gets byte-identical bytes whether the flag is set or not.
question = "Hvor ofte er den årlige kontrollen?"
off = okf_consume.serialise(okf_consume.build_payload(FIXTURE, question=question))
off = okf_consume.serialise(
okf_consume.build_payload(FIXTURE, question=question, ranking="fusion")
)
on = okf_consume.serialise(
okf_consume.build_payload(FIXTURE, question=question, cost_vocabulary=True)
okf_consume.build_payload(
FIXTURE, question=question, cost_vocabulary=True, ranking="fusion"
)
)
assert off == on
@ -1559,13 +1563,15 @@ def test_the_cli_exposes_the_flag_and_omitting_it_reproduces_the_default_bytes()
question = "Hvordan skal prisene fylles ut?"
plain = _run(str(FIXTURE), "--question", question)
assert plain.returncode == 0
flagged = _run(str(FIXTURE), "--question", question, "--cost-vocabulary")
flagged = _run(str(FIXTURE), "--question", question, "--cost-vocabulary", "--ranking", "fusion")
assert flagged.returncode == 0
assert plain.stdout == okf_consume.serialise(
okf_consume.build_payload(FIXTURE, question=question)
)
assert flagged.stdout == okf_consume.serialise(
okf_consume.build_payload(FIXTURE, question=question, cost_vocabulary=True)
okf_consume.build_payload(
FIXTURE, question=question, cost_vocabulary=True, ranking="fusion"
)
)
@ -1631,6 +1637,9 @@ def _eviction_payload(
k=k,
limit=limit,
reserve_top_rank=reserve_top_rank,
# The knapsack and the reservation are measured on the fusion, whose
# order these fixtures were written against.
ranking="fusion",
)
@ -1852,6 +1861,8 @@ def test_the_cli_exposes_the_reservation_and_omitting_it_reproduces_the_default_
"16",
"--limit",
str(EVICTION_LIMIT),
"--ranking",
"fusion",
)
plain = _run(*common)
assert plain.returncode == 0
@ -2115,14 +2126,14 @@ def test_the_weight_is_off_by_default_and_the_default_payload_is_unmoved() -> No
def test_the_cli_exposes_the_weight_and_omitting_it_reproduces_the_default_bytes() -> None:
question = "Hva krever Krav 10.2-2 om sentrale vilkår?"
plain = _run(str(FIXTURE), "--question", question)
weighted = _run(str(FIXTURE), "--question", question, "--rarity-weight")
weighted = _run(str(FIXTURE), "--question", question, "--rarity-weight", "--ranking", "fusion")
assert plain.returncode == 0, plain.stderr
assert weighted.returncode == 0, weighted.stderr
assert plain.stdout == okf_consume.serialise(
okf_consume.build_payload(FIXTURE, question=question)
)
assert weighted.stdout == okf_consume.serialise(
okf_consume.build_payload(FIXTURE, question=question, rarity_weight=True)
okf_consume.build_payload(FIXTURE, question=question, rarity_weight=True, ranking="fusion")
)
@ -2161,8 +2172,8 @@ def test_build_payload_hands_the_same_weights_to_the_document_prior(
monkeypatch.setattr(okf_consume, "document_scores", spy)
question = "Hvordan skal prisene fylles ut?"
okf_consume.build_payload(FIXTURE, question=question)
okf_consume.build_payload(FIXTURE, question=question, rarity_weight=True)
okf_consume.build_payload(FIXTURE, question=question, ranking="fusion")
okf_consume.build_payload(FIXTURE, question=question, rarity_weight=True, ranking="fusion")
assert seen[0] is None
expected = okf_consume.rarity_weights(
okf_consume.normalise(question), okf_consume.searchable_text(_fixture_concepts())