core/generate/derivation/search.py
Shay 0fbcce429b
feat(adr-0182): cross-composer disagreement pooling — distractor 0014 + disguised-polarity refuse (confuser wrong 5->2) (#476)
Implements ADR-0182's first win on top of EX-6 (#473). The distractor-quantity
confuser 0014 misfired (20x3x5=300) because a blunt product-of-all was the *unique*
self-verifying reading: the completeness clause forces the distractor into it, and
no rival reading existed to trigger the wrong=0 disagreement rule. No tight cue rule
separates it from the legitimate cross-unit products (`for` licenses both the 0014
distractor AND the correct train-0021 product) -- that is the deferred cue-precision
problem. So instead of a reactive patch, let the disagreement rule do the refusing.

Mechanism (sealed lane; chat/ does not import these -> serving 3/47/0 frozen):
- verify.classify_derivation: a derivation is `complete` (commit-eligible),
  `exempt` (verified but for an isolated-foreign unused quantity -> commit-
  INELIGIBLE), or None. Refactored self_verifies into _base_reasons +
  _unused_quantities so the two share logic and cannot drift (behavior identical;
  385 derivation tests + smoke 67 green).
- accumulate.accumulation_candidates: exposes the ungated readings, incl. a
  distractor-skip reading that drops an isolated-foreign quantity from a multi-
  quantity change clause (20+5=25). compose_accumulation is byte-identical
  (drop_isolated_foreign=False + the same gate).
- search.multiplicative_candidates / multistep.candidate_chains: ungated candidates.
- pool.resolve_pooled: pools every composer's readings; disagreement -> refuse; a
  single answer commits only if a `complete` candidate produced it (exempt-only ->
  refuse, so the commit-path completeness guarantee from ADR-0175 is untouched).
- confuser runner: _engine_answer now delegates to resolve_pooled (the prior
  first-composer-wins order could not notice it held two incompatible readings).

Result (the microscope):
- confuser wrong 5 -> 2. distractor 0014 refuses (product 300 vs additive 25);
  BONUS: both disguised-polarity cases (0001/0003) refuse -- the spurious
  "buys X for N coins" product disagrees with the accumulation reading. Remaining
  wrong: 0016 (distractor in the anchor clause -> needs anchor-skip, separate step)
  and 0020 (temporal-scope). pair-tells 4 -> 1.
- genuine positives still 7 solved, 0 wrong.
- train_sample 3/47/0 and practice 3/47/0 byte-identical (they call
  compose_accumulation/search directly -- unchanged -- not the pool).
- smoke 67, architectural invariants 40, lane-SHA freeze 8/8.

Tests:
- test_adr_0182_pool.py: classify (complete/exempt/None incl. narrow-exemption
  edges) + resolve_pooled, with the wrong=0 obligation test_exempt_only_never_commits
  (a distractor with no multiplicative cue must refuse, not commit 25 -- fails loudly
  if the exemption is made commit-eligible).
- test_adr_0163_f2_confusers.py: baseline tightened wrong 5->2, pair_tells 4->1;
  new test_distractor_0014_refuses_via_pooling + test_disguised_polarity_does_not_misfire.

Stacked on #473 (EX-6); merge #473 first. 0016 + the remaining wrongs are follow-ups.
2026-05-29 13:22:19 -07:00

82 lines
3.8 KiB
Python

"""ADR-0175 Phase 3b — bounded, deterministic multiplicative derivation search.
The first attempt generator. Conservative by design: it proposes a single
candidate — the **full product of all extracted quantities** — and only when a
multiplicative-relation cue lexeme is present in the text. The proposal is then
passed through the Phase 3a self-verification gate (grounding ∧ unit ∧ unique),
so nothing ungrounded can resolve.
The cue set is an explicit **provisional hypothesis**: it is the search's first
guess at which lexemes license multiplication. It is *not* claimed correct — the
sealed practice lane checks every attempt against gold, and wrong attempts become
elimination records (§9) that refine the hypothesis over time. That refinement is
the compounding loop; this module only stands up the first, gated attempt.
wrong=0 posture: the search runs only in the sealed practice lane (never serving),
every proposal is gated by self-verification, and a non-unique or ungrounded
proposal refuses. Bounded by :data:`MAX_QUANTITIES` (refuse rather than enumerate
an unbounded product).
"""
from __future__ import annotations
import re
from typing import Final
from generate.derivation.extract import extract_quantities
from generate.derivation.model import GroundedDerivation, Step
from generate.derivation.verify import Resolution, select_self_verified
from generate.math_roundtrip import _tokens
# Provisional multiplicative-cue lexemes (the search's first hypothesis; refined
# by practice elimination, not asserted correct). Sorted use for determinism.
MULTIPLICATIVE_CUES: Final[tuple[str, ...]] = ("each", "every", "for", "per", "times")
MAX_QUANTITIES: Final[int] = 6
_SENTENCE_SPLIT: Final[re.Pattern[str]] = re.compile(r"(?<=[.?!])\s+")
def _sentence_candidates(problem_text: str) -> list[GroundedDerivation]:
"""One in-clause product candidate per sentence that has ≥2 quantities and a
present multiplicative cue.
Per-sentence (in-clause) scope is deliberate: it targets the multiplicative
*aggregate* and avoids multiplying quantities that merely co-occur across
sentences. When two sentences each yield a product, they disagree and the
uniqueness gate refuses — so the disagreement rule does real safety work
instead of being trivially satisfied by a single whole-text candidate.
"""
candidates: list[GroundedDerivation] = []
for sentence in _SENTENCE_SPLIT.split(problem_text):
quantities = extract_quantities(sentence)
if not 2 <= len(quantities) <= MAX_QUANTITIES:
continue
present = [c for c in MULTIPLICATIVE_CUES if c in _tokens(sentence)]
if not present:
continue
cue = present[0] # deterministic (MULTIPLICATIVE_CUES is sorted-by-design)
start, *rest = quantities
candidates.append(
GroundedDerivation(
start=start,
steps=tuple(Step(op="multiply", operand=q, cue=cue) for q in rest),
)
)
return candidates
def multiplicative_candidates(problem_text: str) -> list[GroundedDerivation]:
"""ADR-0182 — the ungated in-clause product candidates, for cross-composer
pooling. Same construction :func:`search_multiplicative` gates, exposed so the
pool can weigh products against the other composers' readings."""
return _sentence_candidates(problem_text)
def search_multiplicative(problem_text: str) -> Resolution | None:
"""Attempt a grounded in-clause multiplicative product.
Builds one product candidate per qualifying sentence and runs them through
the Phase 3a gate: a single self-verifying candidate resolves; zero (no
grounded product) or several that disagree refuse. Deterministic and bounded.
"""
return select_self_verified(_sentence_candidates(problem_text), problem_text)