* feat(adr-0182): anchor-skip + intra-clause accumulation — distractor 0016 refuses, twin 0017 solves (confuser wrong 1->0)
The last confuser wrong. 0016 ("A train travels 60 miles per hour for 2 hours. Tom
has 8 tickets and buys 4 more tickets. How many tickets?") committed the blunt
product 60x2x8x4=3840 because it was the unique complete reading: the train sentence
is an all-foreign anchor-position block (2 quantities, can't seed an anchor) and the
Tom sentence packs state+change in ONE sentence ("has 8 ... and buys 4 more"), which
the sentence-level reader couldn't decompose -> accumulation_candidates was empty ->
no rival reading -> product committed.
The microscope confirmed intra-clause state+change is a REAL GSM8K pattern (train-0010
"Yun had 20 paperclips initially, but then lost 12"; practice-0121 "Sam has 30 apples
and gives 10 to Anna") -- so this is genuine comprehension, not a 0016-only patch.
Mechanism (added to accumulation_candidates ONLY -> feeds only the pool; train_sample
serving + practice use compose_accumulation, unchanged -> 3/47/0 byte-identical by
construction):
- _sub_clauses: sentence clauses further split on coordinating conjunctions
(and / then / and then). LOCAL to the ungated candidate generator -- the global
segmenter (GB-1/GB-2/serving) is untouched.
- _build_accumulation_anchor_skip: anchor = first single-quantity sub-clause (leading
non-anchorable all-foreign blocks are skipped); chain the conjunction-mate change
("buys 4 more" -> +4). Referent guard + polarity-cue requirement still gate it
(a no-cue sub-clause -> refuse), so GB-2 same-unit lists ("6 apples and 4 apples")
produce no spurious candidate.
Result (sealed lane):
- confuser wrong 1 -> 0. distractor-quantity 0 wrong / 2 refused: 0016 refuses
(product 3840 [complete] vs additive 12 [exempt] disagree). BONUS: the clean twin
0017 ("Tom has 8 tickets and buys 4 more tickets", no distractor) now *solves* 12
-- genuine comprehension of a real positive, the comprehension-vs-surface-match
discrimination the corpus exists to measure. genuine positives 7 -> 8 solved.
- the only non-clean verdict left is 1 spurious (0010 multi-referent "altogether" --
a separate H1 graduation question, not this lever).
- train_sample 3/47/0 and practice 3/47/0 byte-identical; 243 derivation/pool tests
+ 40 architectural invariants green.
Tests:
- test_adr_0182_pool.py TestAnchorSkipIntraClause: intra-clause twin resolves to 12;
the 0016 anchor-skip candidate classifies `exempt`; 0016 refuses via disagreement;
no spurious extra candidate without a conjunction.
- test_adr_0163_f2_confusers.py: baseline wrong 1->0, positives_solved 7->8; renamed
test_distractor_quantity_refuses asserts BOTH 0014 and 0016; new
test_intra_clause_twin_0017_solves.
Stacked on #480 (prior-state guard); merge #480 first. Confuser arc wrong 7->5->2->1->0.
* test(adr-0182): close anchor-skip refuse-branch coverage gap (lookback finding)
The four-PR lookback review (EX-6/pooling/prior-state/anchor-skip) found the
anchor-skip refuse branches in _build_accumulation_anchor_skip untested: the
referent guard (new named actor -> refuse) and the polarity-cue requirement were
asserted but not proven (no test would fail if removed). Per the schema-obligation
discipline, add NON-VACUOUS failing-under-violation tests:
- test_anchor_skip_referent_guard_discriminates_actor: identical structure, change
sub-clause subject is a pronoun ('he', same referent -> 12 IS produced) vs a new
name ('Sara' -> guard suppresses it). Removing _same_referent makes the new-actor
case also produce 12 -> the second assertion fails. (Non-vacuous: the positive
control proves the path is reachable, so the negative isn't trivially-empty.)
- test_anchor_skip_requires_a_polarity_cue: 'gets 4 more' (cue -> 12) vs 'owns 4'
(no cue -> not guessed). Removing the polarity gate makes the no-cue case produce
a reading -> fails.
- test_anchor_skip_refuses_without_single_quantity_anchor: no single-quantity
sub-clause -> () (does not force a multi-quantity clause to anchor).
No code change; behavior unchanged. Confuser wrong stays 0; 281 derivation tests +
40 invariants green.
251 lines
13 KiB
Python
251 lines
13 KiB
Python
"""ADR-0182 — cross-composer disagreement pooling (distractor refusal).
|
|
|
|
Two surfaces under test:
|
|
|
|
* :func:`generate.derivation.verify.classify_derivation` — the commit-eligibility
|
|
class (``complete`` / ``exempt`` / ``None``) and, critically, that the
|
|
isolated-foreign exemption is *narrow* (an empty-unit or same-unit unused
|
|
quantity is never exempt — it is real signal, not a distractor).
|
|
* :func:`generate.derivation.pool.resolve_pooled` — the pooled resolution: a clean
|
|
reading commits, a distractor problem's product-vs-additive disagreement refuses,
|
|
and (the wrong=0-critical property) an ``exempt``-only answer **never commits**.
|
|
|
|
Sealed lane: ``chat/`` does not import these; serving ``3/47/0`` cannot move.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from generate.derivation import pool
|
|
from generate.derivation.accumulate import accumulation_candidates
|
|
from generate.derivation.model import GroundedDerivation, Quantity, Step
|
|
from generate.derivation.pool import resolve_pooled
|
|
from generate.derivation.target import asks_prior_state
|
|
from generate.derivation.verify import classify_derivation
|
|
|
|
_DISTRACTOR_0014 = (
|
|
"Kate has 20 pencils. She studies for 3 hours and then buys 5 more pencils. "
|
|
"How many pencils does Kate have?"
|
|
)
|
|
# A distractor whose pool contains an exempt additive reading (20+5=25, "3 hours"
|
|
# unused) AND a complete product (20*3*5=300) — three distinct answers, so refusal
|
|
# here comes from the DISAGREEMENT rule, not commit-ineligibility. (The aggressive
|
|
# composers manufacture the product from any such text, which is exactly why a
|
|
# natural fixture cannot isolate the commit-ineligibility branch — see
|
|
# test_exempt_only_never_commits, which injects a single-exempt pool directly.)
|
|
_EXEMPT_PLUS_PRODUCT = (
|
|
"Kate has 20 pencils. She rests for 3 hours and buys 5 more pencils."
|
|
)
|
|
_CLEAN_ACCUMULATION = "Sam has 14 apples. He buys 9 more apples."
|
|
|
|
|
|
class TestClassifyDerivation:
|
|
def test_complete_reading_is_commit_eligible(self) -> None:
|
|
# uses every quantity -> complete
|
|
derivation = accumulation_candidates(_CLEAN_ACCUMULATION)[0]
|
|
assert classify_derivation(derivation, _CLEAN_ACCUMULATION) == "complete"
|
|
|
|
def test_isolated_foreign_unused_is_exempt(self) -> None:
|
|
# the additive reading of 0014 leaves "3 hours" unused; hours is foreign to
|
|
# the used unit (pencils) -> exempt (commit-ineligible).
|
|
derivation = GroundedDerivation(
|
|
start=Quantity(20.0, "pencils", "20"),
|
|
steps=(Step(op="add", operand=Quantity(5.0, "pencils", "5"), cue="more"),),
|
|
)
|
|
assert classify_derivation(derivation, _DISTRACTOR_0014) == "exempt"
|
|
|
|
def test_same_unit_unused_is_not_exempt(self) -> None:
|
|
# a quantity sharing the reading's unit is real signal, never a distractor:
|
|
# leaving it unused must NOT be exempted (it stays invalid -> None).
|
|
text = "Sam has 14 apples. He buys 9 more apples. He eats 2 apples."
|
|
derivation = GroundedDerivation(
|
|
start=Quantity(14.0, "apples", "14"),
|
|
steps=(Step(op="add", operand=Quantity(9.0, "apples", "9"), cue="more"),),
|
|
)
|
|
assert classify_derivation(derivation, text) is None
|
|
|
|
def test_empty_unit_unused_is_not_exempt(self) -> None:
|
|
# an unused quantity with an unknown (empty) unit cannot be shown foreign,
|
|
# so it is never exempt — completeness still rejects the reading.
|
|
text = "Sam has 14 apples. He buys 9 more apples. He had 2."
|
|
derivation = GroundedDerivation(
|
|
start=Quantity(14.0, "apples", "14"),
|
|
steps=(Step(op="add", operand=Quantity(9.0, "apples", "9"), cue="more"),),
|
|
)
|
|
assert classify_derivation(derivation, text) is None
|
|
|
|
def test_ungrounded_operand_is_invalid(self) -> None:
|
|
derivation = GroundedDerivation(
|
|
start=Quantity(14.0, "apples", "14"),
|
|
steps=(Step(op="add", operand=Quantity(999.0, "apples", "999"), cue="more"),),
|
|
)
|
|
assert classify_derivation(derivation, _CLEAN_ACCUMULATION) is None
|
|
|
|
|
|
class TestResolvePooled:
|
|
def test_clean_accumulation_commits(self) -> None:
|
|
resolution = resolve_pooled(_CLEAN_ACCUMULATION)
|
|
assert resolution is not None
|
|
assert resolution.answer == 23.0
|
|
|
|
def test_distractor_0014_refuses_via_disagreement(self) -> None:
|
|
# product 300 (complete) vs additive 25 (exempt) disagree -> refuse.
|
|
assert resolve_pooled(_DISTRACTOR_0014) is None
|
|
|
|
def test_extra_exempt_readings_do_not_break_refusal(self) -> None:
|
|
# A pool carrying an exempt additive reading alongside the complete product
|
|
# still refuses (three distinct answers -> disagreement). Guards that the
|
|
# exempt class does not accidentally suppress the disagreement rule.
|
|
assert accumulation_candidates(_EXEMPT_PLUS_PRODUCT), "expected a candidate"
|
|
assert classify_derivation(
|
|
accumulation_candidates(_EXEMPT_PLUS_PRODUCT)[-1], _EXEMPT_PLUS_PRODUCT
|
|
) == "exempt"
|
|
assert resolve_pooled(_EXEMPT_PLUS_PRODUCT) is None
|
|
|
|
def test_exempt_only_never_commits(self, monkeypatch) -> None:
|
|
# THE wrong=0-critical obligation, isolated. A pool whose ONLY verifying
|
|
# reading is exempt — a single distinct answer with no `complete` reading —
|
|
# must refuse on commit-ineligibility (pool.resolve_pooled requires a
|
|
# `complete` candidate to commit; an exempt-only answer never commits).
|
|
#
|
|
# The aggressive composers synthesise a competing `complete` product for any
|
|
# natural text (see test_extra_exempt_readings_do_not_break_refusal), so a
|
|
# corpus fixture cannot isolate this branch. We inject a single-exempt pool
|
|
# directly. Removing the commit-ineligibility clause makes this commit 25 and
|
|
# fails loudly; it is otherwise unguarded.
|
|
exempt = GroundedDerivation(
|
|
start=Quantity(20.0, "pencils", "20"),
|
|
steps=(Step(op="add", operand=Quantity(5.0, "pencils", "5"), cue="more"),),
|
|
)
|
|
assert classify_derivation(exempt, _DISTRACTOR_0014) == "exempt"
|
|
monkeypatch.setattr(pool, "pooled_candidates", lambda *_a: [exempt])
|
|
# one distinct answer (25), zero `complete` readings -> commit-ineligibility
|
|
# is the only clause that can refuse here.
|
|
assert resolve_pooled(_DISTRACTOR_0014) is None
|
|
|
|
def test_deterministic(self) -> None:
|
|
assert resolve_pooled(_DISTRACTOR_0014) == resolve_pooled(_DISTRACTOR_0014)
|
|
a = resolve_pooled(_CLEAN_ACCUMULATION)
|
|
b = resolve_pooled(_CLEAN_ACCUMULATION)
|
|
assert a is not None and b is not None and a.answer == b.answer
|
|
|
|
|
|
_BEFORE_Q = "Lisa had 50 dollars. She spent 20 on lunch. How much money did Lisa have before lunch?"
|
|
_LEFT_TWIN = "Lisa had 50 dollars. She spent 20 on lunch. How much money does Lisa have left?"
|
|
|
|
|
|
class TestPriorStateQuestionGuard:
|
|
"""ADR-0182 — a question asking for a *prior* state is refused (the forward
|
|
composers compute the final state, the wrong temporal point). Question-clause
|
|
scoped, so body narrative ('before school starts') does not trip it."""
|
|
|
|
def test_before_question_detected(self) -> None:
|
|
assert asks_prior_state(_BEFORE_Q) is True
|
|
|
|
def test_left_twin_not_detected(self) -> None:
|
|
# the minimal-pair twin asks for the net ('left') -> forward reading, solvable.
|
|
assert asks_prior_state(_LEFT_TWIN) is False
|
|
|
|
def test_before_in_body_not_detected(self) -> None:
|
|
# 'before' in narrative (not the question clause) must NOT trip the guard,
|
|
# or it would wrongly refuse train-0003 (gold 864, currently committed).
|
|
body_before = (
|
|
"The student council sells erasers in the morning before school starts. "
|
|
"There are 24 erasers in each box. If they sell 48 boxes, how many erasers?"
|
|
)
|
|
assert asks_prior_state(body_before) is False
|
|
|
|
def test_used_to_make_is_not_a_prior_marker(self) -> None:
|
|
# the purpose infinitive 'used to make' is a false positive guarded against.
|
|
assert asks_prior_state("If 50 beads are used to make one bracelet, how many bracelets?") is False
|
|
|
|
def test_prior_state_question_refuses(self) -> None:
|
|
# the forward reading computes 50-20=30 (the net); the question asks the
|
|
# pre-change state -> refuse, not commit 30.
|
|
assert resolve_pooled(_BEFORE_Q) is None
|
|
|
|
def test_left_twin_still_resolves_forward(self) -> None:
|
|
# discrimination: the twin asking 'left' commits the forward net (30).
|
|
resolution = resolve_pooled(_LEFT_TWIN)
|
|
assert resolution is not None and resolution.answer == 30.0
|
|
|
|
|
|
_ANCHOR_SKIP_0016 = (
|
|
"A train travels at 60 miles per hour for 2 hours. Tom has 8 tickets and "
|
|
"buys 4 more tickets. How many tickets does Tom have?"
|
|
)
|
|
_INTRACLAUSE_TWIN = "Tom has 8 tickets and buys 4 more tickets. How many tickets does Tom have?"
|
|
|
|
|
|
class TestAnchorSkipIntraClause:
|
|
"""ADR-0182 anchor-skip: a sentence packing state+change ("Tom has 8 tickets and
|
|
buys 4 more tickets") is read by splitting on the conjunction, and a leading
|
|
all-foreign block ("A train travels 60 miles ... for 2 hours") is skipped from
|
|
anchor selection. Its quantities go unused -> the pool's isolated-foreign
|
|
exemption makes the reading commit-ineligible -> it forces a disagreement refusal
|
|
on the distractor case, while the clean twin commits."""
|
|
|
|
def test_intra_clause_state_and_change_resolves(self) -> None:
|
|
# the clean twin: "has 8 ... and buys 4 more" -> 12, committed.
|
|
resolution = resolve_pooled(_INTRACLAUSE_TWIN)
|
|
assert resolution is not None and resolution.answer == 12.0
|
|
|
|
def test_anchor_skip_candidate_is_exempt(self) -> None:
|
|
# the 0016 reading skips the train block; 8+4=12 leaves 60/2 unused-foreign.
|
|
cands = accumulation_candidates(_ANCHOR_SKIP_0016)
|
|
assert cands, "expected an anchor-skip accumulation candidate"
|
|
twelve = [d for d in cands if d.answer == 12.0]
|
|
assert twelve, "expected the 8+4=12 reading"
|
|
assert classify_derivation(twelve[0], _ANCHOR_SKIP_0016) == "exempt"
|
|
|
|
def test_distractor_0016_refuses_via_disagreement(self) -> None:
|
|
# product 3840 (complete) vs additive 12 (exempt) disagree -> refuse.
|
|
assert resolve_pooled(_ANCHOR_SKIP_0016) is None
|
|
|
|
def test_no_anchor_skip_candidate_without_conjunction(self) -> None:
|
|
# a plain single-quantity sentence yields no spurious extra reading.
|
|
cands = accumulation_candidates("Sam has 14 apples. He buys 9 more apples.")
|
|
assert all(d.answer == 23.0 for d in cands)
|
|
|
|
# --- ADR-0182 lookback: the anchor-skip refuse branches (failing-under-violation) ---
|
|
|
|
def test_anchor_skip_referent_guard_discriminates_actor(self) -> None:
|
|
# NON-VACUOUS failing-under-violation: the ONLY difference between these two
|
|
# is the change sub-clause's subject (pronoun 'he' vs new name 'Sara').
|
|
# Same referent -> the 8+4=12 anchor-skip reading IS produced; new actor ->
|
|
# the referent guard suppresses it. If `_same_referent` were removed, the
|
|
# new-actor case would also produce 12 and the second assertion fails.
|
|
same_referent = (
|
|
"A train travels at 60 miles per hour for 2 hours. Tom has 8 tickets and "
|
|
"he buys 4 more tickets. How many tickets does Tom have?"
|
|
)
|
|
new_actor = (
|
|
"A train travels at 60 miles per hour for 2 hours. Tom has 8 tickets and "
|
|
"Sara buys 4 more tickets. How many tickets does Tom have?"
|
|
)
|
|
assert any(d.answer == 12.0 for d in accumulation_candidates(same_referent)), (
|
|
"same-referent anchor-skip should produce the 8+4=12 reading"
|
|
)
|
|
assert all(d.answer != 12.0 for d in accumulation_candidates(new_actor)), (
|
|
"anchor-skip chained across a new actor (Sara onto Tom)"
|
|
)
|
|
|
|
def test_anchor_skip_requires_a_polarity_cue(self) -> None:
|
|
# NON-VACUOUS: same structure, change sub-clause with vs without a licensed
|
|
# cue. "gets 4 more" -> +4 -> 12 produced; "owns 4" (no gain/loss/more cue)
|
|
# -> polarity None -> not guessed -> no candidate.
|
|
with_cue = (
|
|
"A train travels at 60 miles per hour for 2 hours. Tom has 8 tickets and "
|
|
"gets 4 more tickets. How many tickets does Tom have?"
|
|
)
|
|
no_cue = (
|
|
"A train travels at 60 miles per hour for 2 hours. Tom has 8 tickets and "
|
|
"owns 4 tickets. How many tickets does Tom have?"
|
|
)
|
|
assert any(d.answer == 12.0 for d in accumulation_candidates(with_cue))
|
|
assert all(d.answer != 12.0 for d in accumulation_candidates(no_cue))
|
|
|
|
def test_anchor_skip_refuses_without_single_quantity_anchor(self) -> None:
|
|
# No sub-clause yields a single-quantity anchor -> the anchor-skip path
|
|
# produces nothing (it does not force a multi-quantity clause to anchor).
|
|
no_anchor = "A train travels at 60 miles per hour for 2 hours and covers 5 towns."
|
|
assert accumulation_candidates(no_anchor) == ()
|