core/generate/derivation/__init__.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

69 lines
2 KiB
Python

"""ADR-0175 Phase 3 — grounded derivation search + self-verification gate.
Phase 3a (this surface): the self-verification gate — grounded operands ∧
grounded operation cues ∧ unit consistency ∧ uniqueness. The wrong=0-critical
guard that keeps the (Phase 3b) bounded search honest.
"""
from __future__ import annotations
from generate.derivation.clauses import (
ClauseResult,
clause_local_results,
segment_clauses,
)
from generate.derivation.accumulate import accumulation_candidates, compose_accumulation
from generate.derivation.compose import compose_sequential
from generate.derivation.comparatives import (
ComparativeScalar,
comparative_step,
extract_comparative_scalars,
)
from generate.derivation.extract import extract_quantities
from generate.derivation.model import GroundedDerivation, Quantity, Step, VALID_OPS
from generate.derivation.multistep import candidate_chains, search_chain
from generate.derivation.pool import pooled_candidates, resolve_pooled
from generate.derivation.search import (
MULTIPLICATIVE_CUES,
multiplicative_candidates,
search_multiplicative,
)
from generate.derivation.target import Target, extract_target
from generate.derivation.verify import (
Resolution,
SelfVerification,
classify_derivation,
select_self_verified,
self_verifies,
)
__all__ = [
"ClauseResult",
"ComparativeScalar",
"GroundedDerivation",
"MULTIPLICATIVE_CUES",
"Quantity",
"Resolution",
"SelfVerification",
"Step",
"Target",
"VALID_OPS",
"accumulation_candidates",
"candidate_chains",
"classify_derivation",
"clause_local_results",
"compose_accumulation",
"compose_sequential",
"comparative_step",
"extract_comparative_scalars",
"extract_quantities",
"extract_target",
"multiplicative_candidates",
"pooled_candidates",
"resolve_pooled",
"search_chain",
"search_multiplicative",
"segment_clauses",
"select_self_verified",
"self_verifies",
]