feat(p20): the requirement that is RIGHT, and a clause number that is not a price

Three seams, one commit: A, B and C touch the same four modules (run.py carries
the debate task, the grounding composition and the announcement; okf.py carries
one reference-number vocabulary read by both A and B), so splitting them into
three commits would have meant hunk-level staging of entangled files. Stated
rather than silently restructured.

A — the declaration answers with the DOCUMENT's own words. Measured: 13
declarations over round 3 and P17b, not one naming a fasit concept, while the
tool answered {"declared": true, ...} by echoing the caller's own arguments. It
now returns the document's title and req_number, read off Bundle.context_files
(so the type: verdict layer can never be named back), plus the sentence saying
what the declaration binds. A path the base carries as no concept answers with
empty strings rather than refusing. The commission's success_criteria now reach
the DEBATE task through mandate.criteria_block, the one renderer, empty when
there are none — which is what keeps every un-commissioned prompt, and the
golden, byte-identical.

B — a clause number is not a price. THE ORDER'S OWN RULE WAS FELLED BY
MEASUREMENT: it asks to refuse a code that IS declared req_number/prosessnr,
and neither of its two known positives is. n500 declares seksjon 10.4.1..10.4.4
but never the bare 10.4; r761 declares 2727 prosessnr and 2753 seksjon, none of
them 1.10.4, which occurs once, as prose ("iht. vegnormal N200 kap. 1.10.4").
The COMPLEMENT fires on both and closes the hole _ground_against_input already
admits in writing -- "it fails OPEN on a coincidental match". Unanchored run +
requirement-shaped code + the base declares a vocabulary + the code is not in
it -> refused, naming the denominator. All five of kontrakt-sorasen's real
process codes ARE declared and pass, which is what keeps the one context set
built on real codes measurable. Replayed over all 24 codes of round 3 + P17b:
exactly the two known positives flip validated -> rejected, 22 unchanged.

C — a parse failure no longer burns the round ledger blind. _fetch_parsed takes
a BUILDER instead of a finished message list, so the retry carries the parse
reason; measured, kontrakt-sorasen-04 spent 11 of 12 rounds re-asking the same
question. And announced_subject names the routed bases instead of saying "the
portfolio" for a two-base commission.

Suite 1807/5 (from 1781, +26, 0 removed), golden demo-transcript.stdout
BYTE-UNCHANGED (shasum -a 1 of the CONTENT = ea8c534773acdbe41ae68f2c55724d69aaf8be4f),
ruff and mypy clean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-09-15 06:02:46 +02:00
commit c8f0c8f7c4
12 changed files with 1098 additions and 37 deletions

View file

@ -250,29 +250,50 @@ _GROUNDING_MIN_INERT_DOCUMENTS: Final = 10
#: Bare numbers are deliberately EXCLUDED, with the number: K2 carries 46 394 occurrences over
#: 2 117 distinct values (P7 § 2), so counting them would make every report positive and the
#: measurement inert — the repo's cardinal class, a gate that can only come out green.
#: The four forms, NAMED rather than reached by index: P20/B needs two of them by themselves
#: (a requirement/process number), and ``IDENTIFIER_FORMS[1:3]`` in a second module would be a
#: positional dependency on a tuple literal — the kø-(p) shape with no compiler to catch it.
_FORM_SEPARATED_UPPER: Final = re.compile(r"\b[A-ZÆØÅ][A-ZÆØÅ0-9]*(?:[-_][A-ZÆØÅ0-9]+)+\b")
_FORM_REQUIREMENT_NUMBER: Final = re.compile(r"Krav\s+\d+(?:\.\d+)*\s*[\u2014-]\s*\d+(?:_\d+)?")
_FORM_PROCESS_NUMBER: Final = re.compile(r"(?<![\d.])[1-9]\d{0,2}(?:\.\d{1,3}){1,4}\b(?!\.\d)")
_FORM_PROCESS_HEADING: Final = re.compile(r"(?<!\d )(?<![\d.])[1-9]\d{0,2} [A-ZÆØÅ]{5,}\b")
IDENTIFIER_FORMS: Final = (
# ``SHA-01``, ``RIM-02``, ``B-20-00-00``, ``FOR-2011-12-06-1357`` (K2's 50) — and, since P19/B2
# made the same forms decide ``prose`` vs ``identifier``, an UPPERCASE separated token with no
# digits at all. MEASURED: this repo's own ``ENERGI-TOTAL-EL`` matched neither of the pre-P19
# forms, so the classifier called a real cost code prose; a gate is only allowed to be wrong in
# the direction that admits too much.
re.compile(r"\b[A-ZÆØÅ][A-ZÆØÅ0-9]*(?:[-_][A-ZÆØÅ0-9]+)+\b"),
_FORM_SEPARATED_UPPER,
# ``Krav 3.3.1—13`` (the N corpora's dominant form). EM-DASH U+2014 AND the hyphen, because the
# binding known positive is the em-dash spelling and only the em-dash spelling scores 6 of 6.
# The trailing ``(?:_\d+)?`` is MEASURED, not defensive: one of the 26 fasit references is
# ``Krav 3.3.2—1_1``, and without it the classifier called that real reference prose.
re.compile(r"Krav\s+\d+(?:\.\d+)*\s*[\u2014-]\s*\d+(?:_\d+)?"),
_FORM_REQUIREMENT_NUMBER,
# R761's process numbers, ``12.1`` / ``52.11`` (P19 B1). MEASURED: all six ``ref`` values in
# ``contexts/kontrakt-sorasen-2027/fasit.json`` are of this shape and NEITHER of the first two
# forms matches one of them, so r761's whole offer was 3 identifiers over 6.5 MB. The trailing
# ``(?!\.\d)`` is what keeps a Norwegian date out: ``15.09.2026`` would otherwise contribute
# its ``15.09`` prefix, and a date is not a requirement.
re.compile(r"(?<![\d.])[1-9]\d{0,2}(?:\.\d{1,3}){1,4}\b(?!\.\d)"),
_FORM_PROCESS_NUMBER,
# ``65 ASFALTDEKKER`` — a process number and its heading, the form a price schedule's section
# rows carry (P18 § 2 measured it at 29 of 2 756 documents).
re.compile(r"(?<!\d )(?<![\d.])[1-9]\d{0,2} [A-ZÆØÅ]{5,}\b"),
_FORM_PROCESS_HEADING,
)
#: The two forms a REQUIREMENT or PROCESS number takes (P20/B). MEASURED over the four delivered
#: bases: these are the shapes that live in ``req_number`` ("Krav 4.1.2—1"), ``prosessnr``
#: ("'11.11'") and ``seksjon`` ("'10.4.3'") — the fields a base uses to number its own clauses.
#: The other two forms are NOT here: ``SHA-01``-style tokens are what a price schedule's cost lines
#: look like, and ``65 ASFALTDEKKER`` IS a schedule section row.
REQUIREMENT_FORMS: Final = (_FORM_REQUIREMENT_NUMBER, _FORM_PROCESS_NUMBER)
def has_requirement_form(code: str) -> bool:
"""Whether ``code`` is shaped like a requirement or process number. FULL-MATCH, never a search,
for ``has_identifier_form``'s reason: ``impulsventilator 12.1`` is not a clause number."""
return any(form.fullmatch(code) for form in REQUIREMENT_FORMS)
def identifier_tokens(text: str) -> set[str]:
"""Every DISTINCT token of any ``IDENTIFIER_FORMS`` shape in ``text``. One reader, two callers.
@ -296,9 +317,27 @@ def has_identifier_form(code: str) -> bool:
return any(form.fullmatch(code) for form in IDENTIFIER_FORMS)
def classify_codes(codes: Sequence[str]) -> dict[str, str]:
"""``{code: "identifier" | "prose"}`` — P19/B2's report, in ONE place for both consumers."""
return {code: "identifier" if has_identifier_form(code) else "prose" for code in codes}
def classify_codes(codes: Sequence[str], grounding: Grounding | None = None) -> dict[str, str]:
"""``{code: "identifier" | "prose" | "requirement"}`` — P19/B2's report, widened by P20/B, in
ONE place for both consumers.
``requirement`` is the third value: a code shaped like a clause number (``REQUIREMENT_FORMS``)
AND declared as one by the input's own documents. It is a REPORT about what the run made of a
code, not the gate — the gate is ``_reference_refusal`` below and fires on the COMPLEMENT, a
requirement-shaped code the base's vocabulary does NOT contain.
``grounding=None`` is the pre-P20 answer exactly: without the input there is no vocabulary to
check against, so no code can be called a requirement. ``stress.py`` re-derives with ``None``
for runs that predate the field, and says so.
"""
vocabulary = frozenset() if grounding is None else grounding.reference_vocabulary
out: dict[str, str] = {}
for code in codes:
if has_requirement_form(code) and code in vocabulary:
out[code] = "requirement"
else:
out[code] = "identifier" if has_identifier_form(code) else "prose"
return out
@dataclass(frozen=True)
@ -320,6 +359,16 @@ class Grounding:
"""
documents: tuple[str, ...]
#: Every reference number the input's documents DECLARE in their own top-level frontmatter
#: (``okf.declared_reference_numbers``), one entry per declaration — the base's own vocabulary
#: of requirement, process and section numbers. P20/B checks a requirement-shaped code against
#: it.
#:
#: DEFAULTED, the ``skipped_links`` half rather than ``cost_baseline_anchored``'s: an empty
#: vocabulary is an honest POSITIVE statement ("this input declares no clause numbers"), and it
#: is what keeps every caller written before today — the road path, every fixture, ``of`` —
#: unchanged by construction, since the gate cannot fire without one.
declared_references: tuple[str, ...] = ()
@classmethod
def of(cls, text: str) -> Grounding:
@ -335,6 +384,11 @@ class Grounding:
"""How many of the documents contain ``token`` — the numerator, in the unit of the rule."""
return sum(1 for document in self.documents if token in document)
@cached_property
def reference_vocabulary(self) -> frozenset[str]:
"""The DISTINCT reference numbers this input declares. The denominator P20/B names."""
return frozenset(self.declared_references)
@cached_property
def identifiers(self) -> frozenset[str]:
"""Every distinct identifier-shaped token this input OFFERS (P8's count, P19/B3's guard).
@ -401,10 +455,60 @@ def _form_refusal(grounding: Grounding, code: str, anchored_codes: frozenset[str
)
def _reference_refusal(grounding: Grounding, code: str) -> str | None:
"""Why a requirement-shaped ``code`` cannot be a cost line of an UNANCHORED input (P20/B).
**The order's own rule was FELLED BY MEASUREMENT before anything was built on it.** It reads:
a code is a requirement when it matches form 2 or 3 AND "står som ``req_number``/``prosessnr``
i toppnivå-frontmatter" — refuse that. Measured 15.09 against the two known positives the same
order names:
* ``10.4`` (n500, tunnel-04, ``validated``) is declared NOWHERE in n500's frontmatter. The base
declares ``seksjon: 10.4.1`` … ``10.4.4`` and ``req_number: Krav 10.4.3—2``; the bare ``10.4``
is a section PREFIX that occurs in 12 of 274 documents and is no document's own number;
* ``1.10.4`` (r761, lindaas a4, ``validated``) is not one of r761's 2 727 ``prosessnr`` nor one
of its 2 753 ``seksjon`` values. It occurs in ONE of 2 756 documents, as prose: "iht.
vegnormal N200 Vegbygging kap. 1.10.4".
So the ordered rule fires on NEITHER of its own known positives. The COMPLEMENT does, and it is
the better-grounded rule besides: ``_ground_against_input``'s docstring already admits that this
stage "fails OPEN … on a coincidental match", and for one shape — a clause number — the base
hands us the vocabulary needed to close exactly that hole. A form-3 token that is NOT one of the
numbers this base declares was matched in prose by accident.
MEASURED over every code of round 3 and P17b (24 codes, 10 runs): exactly two are
requirement-shaped, they are the two known positives, and neither is in its base's vocabulary.
All five of ``contexts/kontrakt-sorasen-2027``'s REAL process codes (``12.1``, ``12.12``,
``22.1``, ``52.11``, ``51.1``) ARE declared ``prosessnr`` and pass — which is what keeps the
R761 risk the order names (a process number is both a clause and a settlement post) from
turning into a wholesale refusal of the one context set built on real codes.
**The generality guard, ``_form_refusal``'s pattern:** an input that declares no reference
numbers at all cannot be answered in a vocabulary it does not have, so the rule cannot fire
there. That is what leaves every pre-P20 fixture untouched rather than exempted.
The message NAMES THE DENOMINATOR (ansikt 4, and Step 5 feeds it verbatim into the next
attempt): "not one of the 2 765 it declares" is actionable where "ungrounded" is not.
"""
if not has_requirement_form(code):
return None
vocabulary = grounding.reference_vocabulary
if not vocabulary or code in vocabulary:
return None
sample = ", ".join(sorted(vocabulary)[:3])
return (
f"is shaped like a requirement or process number, but it is not one of the "
f"{len(vocabulary)} this knowledge base declares (for example {sample}) — it was matched "
"in prose by coincidence, and an unanchored base carries no price for a clause number"
)
def _ground_against_input(
proposal: SavingsProposal,
grounding: Grounding,
anchored_codes: frozenset[str] = frozenset(),
*,
anchored: bool = True,
) -> Rejection | None:
"""P7: every identifier the proposal builds on must appear VERBATIM in the input it was built
from, or the verdict falls.
@ -459,6 +563,15 @@ def _ground_against_input(
shapeless = _form_refusal(grounding, item.code, anchored_codes)
if shapeless is not None:
violations.append(f"ungrounded identifier {item.code!r}: it {shapeless}")
continue
# P20/B: shaped, grounded, not inert — and still a clause number the base never declared.
# UNANCHORED only: with a baseline, stage 0 has already ruled every code that reaches here
# a real line of this project, and the weaker stage must not overrule the stronger one (the
# sentence ``_form_refusal`` and ``_grounding_text`` both carry).
if not anchored:
coincidental = _reference_refusal(grounding, item.code)
if coincidental is not None:
violations.append(f"ungrounded identifier {item.code!r}: it {coincidental}")
if not violations:
return None
return Rejection(proposal=proposal, reason="; ".join(violations))
@ -504,6 +617,11 @@ def validate_proposal(
proposal,
grounding,
frozenset() if baseline is None else frozenset(baseline.items),
# P20/B: an EXPLICIT flag, never ``not anchored_codes``. A baseline with no items and
# no baseline at all are different facts, and conflating them is the very shape this
# repo refuses elsewhere (``cost_baseline_anchored`` is required without a default for
# the same reason).
anchored=baseline is not None,
)
if adrift is not None:
return adrift