diff --git a/research/cover/audit.py b/research/cover/audit.py new file mode 100644 index 000000000..6198073cb --- /dev/null +++ b/research/cover/audit.py @@ -0,0 +1,117 @@ +"""Check mutable cover records at the boundary where a total is consumed. + +The historical ledger is source-pinned by existing RN candidates. This additive +adapter does not change those bytes or rewrite their certificates. Call +``checked_total(ledger).certified_enclosure()`` instead of trusting a previously +validated, but subsequently mutable, record collection. + +Scope: record/accounting consistency and the ledger's existing exact partition +checks. Supplied areas, integrand bounds and OUTSIDE geometry remain premises; +this function does not prove them, authenticate a caller or provide a lock. +A nonzero-width residual containing zero can be a valid conservative enclosure. +For OUTSIDE we deliberately require the canonical None/[0,0] representation, +so a stored number is never silently discarded by the publication path. +""" +from __future__ import annotations + +from fractions import Fraction +from typing import List + +from research.interval import Interval +from research.cover.ledger import ( + ACCEPTED, DISPOSITIONS, REJECTED, CellRecord, Ledger, RejectKind, Total, +) + + +def _interval(value: object, description: str) -> Interval: + if (type(value) is not Interval or type(value.lo) is not Fraction + or type(value.hi) is not Fraction or value.lo > value.hi): + raise ValueError(f"{description}: exact Interval required") + return value + + +def _sum_intervals(values: List[Interval]) -> Interval: + # A second accumulation path: sum the exact endpoints, not Interval.__add__. + return Interval(sum((value.lo for value in values), Fraction(0)), + sum((value.hi for value in values), Fraction(0))) + + +def checked_total(ledger: Ledger) -> Total: + """Return the legacy Total only after independently checking its accounting. + + Bad records or inconsistent totals raise ValueError. Existing pending-cell + and partition exceptions propagate unchanged. Well-formed noncertifying or + incomplete totals remain noncertifying/incomplete, rather than being promoted. + The ledger and all supplied intervals are left unchanged. + """ + if not isinstance(ledger, Ledger): + raise TypeError("checked_total requires a Ledger") + if type(ledger.certifying) is not bool: + raise ValueError("ledger.certifying must be a boolean") + order = ledger._order + if (type(order) is not list or any(type(cid) is not str for cid in order) + or len(set(order)) != len(order) or set(order) != set(ledger.records)): + raise ValueError("ledger record order must enumerate every unique record") + + contributions: List[Interval] = [] + areas: List[Interval] = [] + rejected_area = Fraction(0) + rejected_by_kind = {} + covers = True + for cid in order: + record = ledger.records[cid] + if not isinstance(record, CellRecord) or record.cell.cid != cid: + raise ValueError(f"cell {cid!r}: record identity mismatch") + if record.disposition not in DISPOSITIONS: + raise ValueError(f"cell {cid!r}: unknown disposition {record.disposition!r}") + if record.disposition == ACCEPTED: + area = _interval(record.area, f"cell {cid!r} area") + if area.lo < 0: + raise ValueError(f"cell {cid!r}: area must be nonnegative") + _interval(record.value_range, f"cell {cid!r} value range") + contributions.append(_interval(record.contribution, f"cell {cid!r} contribution")) + areas.append(area) + elif record.disposition == REJECTED: + kind = record.reject_kind + if kind not in RejectKind.ALL: + raise ValueError(f"cell {cid!r}: unknown rejection kind {kind!r}") + if type(record.reason) is not str or not record.reason.strip(): + raise ValueError(f"cell {cid!r}: rejection reason required") + bound = record.boundary_area_bound + if type(bound) is not Fraction or bound < 0: + raise ValueError(f"cell {cid!r}: nonnegative exact boundary area required") + residual = record.residual + if residual is not None: + residual = _interval(residual, f"cell {cid!r} residual") + rejected_area += bound + rejected_by_kind[kind] = rejected_by_kind.get(kind, Fraction(0)) + bound + if kind == RejectKind.OUTSIDE: + if residual is not None and (residual.lo != 0 or residual.hi != 0): + raise ValueError(f"cell {cid!r}: OUTSIDE residual must be None or [0,0]; " + "refusing to discard a stored interval") + elif residual is None: + covers = False + else: + contributions.append(residual) + + # Retain the original exact tiling/refinement checks and pending refusals. + result = ledger.total() + if type(result) is not Total: + raise ValueError("ledger returned an unexpected total type") + if type(result.certified) is not bool or type(result.covers_region) is not bool: + raise ValueError("total coverage/certification flags must be booleans") + if result.covers_region != covers or result.certified != (ledger.certifying and covers): + raise ValueError("total coverage/certification differs from checked records") + _interval(result.enclosure, "total enclosure") + _interval(result.area_accounted, "total accounted area") + if result.enclosure != _sum_intervals(contributions): + raise ValueError("total enclosure differs from independently summed records") + if result.area_accounted != _sum_intervals(areas): + raise ValueError("total accounted area differs from checked records") + if (type(result.area_rejected_bound) is not Fraction + or type(result.area_rejected_by_kind) is not dict + or any(type(value) is not Fraction for value in result.area_rejected_by_kind.values()) + or result.area_rejected_bound != rejected_area + or result.area_rejected_by_kind != rejected_by_kind): + raise ValueError("total rejected area differs from checked records") + return result diff --git a/tests/test_cover_audit.py b/tests/test_cover_audit.py new file mode 100644 index 000000000..949e4ed4e --- /dev/null +++ b/tests/test_cover_audit.py @@ -0,0 +1,204 @@ +"""Publication-boundary tests; synthetic accounting is not an RN field proof.""" +from dataclasses import replace +from fractions import Fraction as F +from pathlib import Path +import copy +import os +import subprocess +import sys + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from research.interval import Interval as I +from research.cover import audit +from research.cover.ledger import ( + ACCEPTED, REJECTED, Box, Cell, Ledger, PendingCellsError, + PartitionError, RejectKind, UncertifiedTotalError, +) + + +def pair(): + ledger = Ledger('accounting fixture', Box.of(0, 2, 0, 1), 'cartesian') + ledger.add(Cell('a', Box.of(0, 1, 0, 1), 0)) + ledger.add(Cell('b', Box.of(1, 2, 0, 1), 0)) + ledger.accept('a', I.exact(F(1)), I(F(-2), F(3)), I(F(-2), F(3))) + return ledger + + +@pytest.mark.parametrize('residual', [None, I.exact(F(0))]) +def test_canonical_outside_preserves_result_and_records(residual): + ledger = pair() + ledger.reject('b', RejectKind.OUTSIDE, 'declared geometry', F(1), residual) + before = copy.deepcopy(ledger.__dict__) + assert audit.checked_total(ledger) == ledger.total() + assert audit.checked_total(ledger).certified_enclosure() == I(F(-2), F(3)) + assert ledger.__dict__ == before + + +@pytest.mark.parametrize('residual', [I.exact(F(1000000)), I(F(-2), F(-1)), I(F(-1), F(1))]) +def test_outside_noncanonical_residual_refused_before_publication(residual): + ledger = pair() + ledger.reject('b', RejectKind.OUTSIDE, 'declared geometry', F(1)) + # Records are mutable; this bypasses any entry-only guard without code mutation. + ledger.records['b'].residual = residual + with pytest.raises(ValueError, match='OUTSIDE residual'): + audit.checked_total(ledger) + + +@pytest.mark.parametrize('kind', [RejectKind.EXCLUDED, RejectKind.UNRESOLVED_BOUNDARY]) +def test_nonoutside_residual_is_summed_including_signed_values(kind): + ledger = pair() + ledger.reject('b', kind, 'bounded omitted part', F(1), I(F(-5), F(7))) + result = audit.checked_total(ledger) + assert result.certified_enclosure() == I(F(-7), F(10)) + assert result.area_rejected_bound == F(1) + assert result.area_rejected_by_kind == {kind: F(1)} + + +@pytest.mark.parametrize('kind', [RejectKind.EXCLUDED, RejectKind.UNRESOLVED_BOUNDARY]) +def test_missing_residual_stays_incomplete(kind): + ledger = pair() + ledger.reject('b', kind, 'no bound yet', F(1)) + result = audit.checked_total(ledger) + assert result.certified is False and result.covers_region is False + with pytest.raises(UncertifiedTotalError): + result.certified_enclosure() + + +def test_noncertifying_stays_noncertifying(): + ledger = pair() + ledger.reject('b', RejectKind.OUTSIDE, 'declared geometry', F(1)) + ledger.mark_non_certifying('test reference') + result = audit.checked_total(ledger) + assert result.certified is False and result.covers_region is True + + +def test_pending_and_partition_refusals_are_retained(): + ledger = pair() + with pytest.raises(PendingCellsError): + audit.checked_total(ledger) + ledger.reject('b', RejectKind.OUTSIDE, 'declared geometry', F(1)) + ledger.records['b'].cell = Cell('b', Box.of(1, 3, 0, 1), 0) + with pytest.raises(PartitionError): + audit.checked_total(ledger) + + +@pytest.mark.parametrize('field,value', [ + ('disposition', 'UNKNOWN_TERMINAL'), ('reject_kind', 'TYPO'), + ('reason', ''), ('reason', ' '), ('boundary_area_bound', None), + ('boundary_area_bound', F(-1)), ('boundary_area_bound', True), + ('residual', 1), +]) +def test_malformed_rejected_record_refuses(field, value): + ledger = pair() + ledger.reject('b', RejectKind.OUTSIDE, 'declared geometry', F(1)) + setattr(ledger.records['b'], field, value) + with pytest.raises(ValueError): + audit.checked_total(ledger) + + +@pytest.mark.parametrize('field,value', [ + ('area', None), ('area', I(F(-1), F(1))), + ('value_range', None), ('contribution', None), ('contribution', '1'), +]) +def test_malformed_accepted_record_refuses(field, value): + ledger = pair() + ledger.reject('b', RejectKind.OUTSIDE, 'declared geometry', F(1)) + setattr(ledger.records['a'], field, value) + with pytest.raises(ValueError): + audit.checked_total(ledger) + + +@pytest.mark.parametrize('order', [['a'], ['a', 'a', 'b'], ['a', 'b', 'missing']]) +def test_order_cannot_omit_or_repeat_records(order): + ledger = pair() + ledger._order = order + with pytest.raises(ValueError, match='record order'): + audit.checked_total(ledger) + + +def test_record_identity_and_boolean_marker(): + ledger = pair() + ledger.certifying = 'False' + with pytest.raises(ValueError, match='boolean'): + audit.checked_total(ledger) + ledger.certifying = True + ledger.records['a'].cell = Cell('different-id', Box.of(0, 1, 0, 1), 0) + with pytest.raises(ValueError, match='record identity'): + audit.checked_total(ledger) + + +@pytest.mark.parametrize('change', [ + {'enclosure': I.exact(F(0))}, {'area_accounted': I.exact(F(0))}, + {'certified': False}, {'certified': 1}, {'covers_region': False}, + {'area_rejected_bound': F(0)}, {'area_rejected_by_kind': {}}, + {'area_rejected_bound': True}, {'area_rejected_by_kind': {RejectKind.EXCLUDED: True}}, +]) +def test_a_lying_accumulator_is_rejected(monkeypatch, change): + ledger = pair() + ledger.reject('b', RejectKind.EXCLUDED, 'bounded omitted part', F(1), I.exact(F(7))) + wrong = replace(ledger.total(), **change) + monkeypatch.setattr(ledger, 'total', lambda: wrong) + with pytest.raises(ValueError): + audit.checked_total(ledger) + + +def test_real_spatial_consumer_calls_checked_boundary(monkeypatch): + from tools import rn_side24_spatial_check as consumer + ledger = pair() + ledger.reject('b', RejectKind.OUTSIDE, 'declared geometry', F(1)) + monkeypatch.setattr(consumer.point_check, 'checked_sources', lambda root: {}) + monkeypatch.setattr(consumer, 'checked_imports', lambda root: {}) + monkeypatch.setattr(consumer, 'checked_target', lambda root: {}) + monkeypatch.setattr(consumer, 'run', lambda *a, **kw: ledger) + def stop(value): + assert value is ledger + raise ValueError('CHECKED_BOUNDARY_REACHED') + monkeypatch.setattr(consumer, 'checked_total', stop) + with pytest.raises(ValueError, match='CHECKED_BOUNDARY_REACHED'): + consumer.build_report() + + +def test_refusal_is_not_disabled_by_optimized_python(): + root = Path(__file__).resolve().parents[1] + code = ''' +from fractions import Fraction as F +from research.interval import Interval as I +from research.cover.ledger import Box, Cell, Ledger, RejectKind +from research.cover.audit import checked_total +l=Ledger('optimized',Box.of(0,1,0,1),'cartesian') +l.add(Cell('one',Box.of(0,1,0,1),0)) +l.reject('one',RejectKind.OUTSIDE,'declared geometry',F(1)) +l.records['one'].residual=I.exact(F(7)) +try: + checked_total(l) +except ValueError: + print('REFUSED') +else: + raise SystemExit('guard disappeared') +''' + env = {**os.environ, 'PYTHONDONTWRITEBYTECODE': '1'} + outputs = [] + for flags in ([], ['-O']): + p = subprocess.run([sys.executable, *flags, '-c', code], cwd=root, + env=env, capture_output=True, text=True, timeout=30) + assert p.returncode == 0, p.stderr + outputs.append(p.stdout) + assert outputs == ['REFUSED\n', 'REFUSED\n'] + + +@pytest.mark.parametrize('polar', [False, True]) +def test_actual_reference_driver_is_accepted_without_changing_its_total(polar): + from research.cover import DriverConfig, rn5_annulus_bracket, rn5_annulus_polar, run + class ConstantReference: + name = 'audit unit reference' + label = 'SYNTHETIC constant function; not a Gaussian-field bound' + certifying = True + def range_enclosure(self, region, box, prec): + return I.exact(F(1)) + region = rn5_annulus_polar() if polar else rn5_annulus_bracket() + ledger = run(region, ConstantReference(), DriverConfig(tol=F(30), max_depth=5)) + before = copy.deepcopy(ledger.__dict__) + assert audit.checked_total(ledger) == ledger.total() + assert ledger.__dict__ == before diff --git a/tools/rn_side24_spatial_check.py b/tools/rn_side24_spatial_check.py index 8c6bb7306..a793e1fd1 100644 --- a/tools/rn_side24_spatial_check.py +++ b/tools/rn_side24_spatial_check.py @@ -16,6 +16,7 @@ sys.path.insert(0, str(ROOT)) from research.cover import Box, DriverConfig, run +from research.cover.audit import checked_total from research.rn.certificate import canonical_bytes from research.rn.spatial_cover import RNRectangleRegion, RNSide24Integrand from tools import rn_side24_check as point_check @@ -63,7 +64,7 @@ def build_report(root=None): region, integrand = RNRectangleRegion(RECTANGLE), RNSide24Integrand(bits=192) ledger = run(region, integrand, DriverConfig(upper_budget=LOCAL_BUDGET, max_depth=1, max_cells=5, prec=88, sig_bits=192)) - total = ledger.total().certified_enclosure() + total = checked_total(ledger).certified_enclosure() receipt = ledger.receipt() if receipt['pending_count'] or total.lo < 0 or total.hi > LOCAL_BUDGET: raise ValueError('complete nonnegative local upper-budget cover required')