|
| 1 | +"""Spec 020 — deterministic empirical-value strip + the planning GUARANTEE (offline). |
| 2 | +
|
| 3 | +``planning_scan.strip_empirical_values`` removes high-confidence empirical values |
| 4 | +(comma-grouped counts, percentages, timed quantities) while preserving structural |
| 5 | +numbers. The integration test proves the GUARANTEE: even when the LLM extractor |
| 6 | +returns ZERO claims (its non-deterministic worst case), the planning branch still |
| 7 | +strips the empirical value via this deterministic pass. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from pathlib import Path |
| 13 | +from typing import Any |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from llmxive.claims import service as svc |
| 18 | +from llmxive.claims.gate import CLAIM_MARKER_PREFIX |
| 19 | +from llmxive.claims.planning_scan import has_empirical_value, strip_empirical_values |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize( |
| 23 | + "text,gone", |
| 24 | + [ |
| 25 | + ("There are 27,635 prime knots.", "27,635"), |
| 26 | + ("~2,000 prime knots downloaded.", "2,000"), |
| 27 | + ("≥95% data completeness.", "95%"), |
| 28 | + ("reference coverage ≥10%.", "10%"), |
| 29 | + ("Complete within 15 minutes.", "15 minutes"), |
| 30 | + ("Exponential backoff (1s → 60s max).", "60s"), |
| 31 | + ("The first 1,701,936 knots.", "1,701,936"), |
| 32 | + ], |
| 33 | +) |
| 34 | +def test_strips_empirical(text: str, gone: str) -> None: |
| 35 | + out = strip_empirical_values(text) |
| 36 | + assert gone not in out |
| 37 | + assert not has_empirical_value(out) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize( |
| 41 | + "text,kept", |
| 42 | + [ |
| 43 | + ("prime knots at ≤13 crossings", "13"), # scope bound |
| 44 | + ("crossing number 13", "13"), # index |
| 45 | + ("crossing numbers 1-10", "1-10"), # range |
| 46 | + ("Phase 1 analysis", "Phase 1"), # phase |
| 47 | + ("checksummed via SHA-256", "SHA-256"), # identifier |
| 48 | + ("version 1.0.0 ratified", "1.0.0"), # version |
| 49 | + ("on 2026-05-29", "2026-05-29"), # date |
| 50 | + ("citation title overlap ≥0.7", "0.7"), # bare decimal threshold |
| 51 | + ("dataset ds002800", "ds002800"), # dataset id |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_preserves_structural(text: str, kept: str) -> None: |
| 55 | + assert kept in strip_empirical_values(text) |
| 56 | + |
| 57 | + |
| 58 | +def test_idempotent_and_headers_and_fences() -> None: |
| 59 | + doc = ( |
| 60 | + "# Heading with 27,635 in it\n" |
| 61 | + "There are 27,635 knots, ≥95% complete.\n" |
| 62 | + "```\ncode with 1,234 stays\n```\n" |
| 63 | + ) |
| 64 | + once = strip_empirical_values(doc) |
| 65 | + # header line + fenced code are untouched |
| 66 | + assert "# Heading with 27,635 in it" in once |
| 67 | + assert "code with 1,234 stays" in once |
| 68 | + # the prose value is gone |
| 69 | + assert "There are 27,635 knots" not in once |
| 70 | + assert strip_empirical_values(once) == once # idempotent |
| 71 | + |
| 72 | + |
| 73 | +def _lowlevel_claim_doc() -> str: |
| 74 | + return ( |
| 75 | + "# Plan\n\n## Scale/Scope\n\n" |
| 76 | + "~2,000 prime knots (1-10), ~27,635 at crossing number 13 (Phase 1).\n" |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def test_planning_branch_guarantees_strip_even_when_extractor_finds_nothing( |
| 81 | + monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 82 | +) -> None: |
| 83 | + # Simulate the LLM extractor's worst case: it returns ZERO claims. The |
| 84 | + # deterministic pass must STILL strip the empirical values (the guarantee). |
| 85 | + monkeypatch.setattr(svc, "extract_claims", lambda *a, **k: []) |
| 86 | + |
| 87 | + def _boom(*a: Any, **k: Any) -> Any: |
| 88 | + raise AssertionError("resolve() called in a planning stage") |
| 89 | + |
| 90 | + monkeypatch.setattr(svc, "resolve", _boom) |
| 91 | + |
| 92 | + out, claims, report = svc.process_document( |
| 93 | + _lowlevel_claim_doc(), |
| 94 | + artifact_path="projects/PROJ-x/specs/plan.md", |
| 95 | + project_id="PROJ-x", backend=object(), model=None, |
| 96 | + repo_root=tmp_path, stage_label="plan", |
| 97 | + ) |
| 98 | + assert "27,635" not in out and "2,000" not in out # stripped (guaranteed) |
| 99 | + assert "crossing number 13" in out and "Phase 1" in out # structural preserved |
| 100 | + assert CLAIM_MARKER_PREFIX not in out # still no kickback |
| 101 | + assert report.blocked is False |
| 102 | + assert claims == [] |
0 commit comments