portfolio-optimiser-claude/tests/test_ingest_spec_loadbearing.py
Kjell Tore Guttormsen e2eb31675d test(ingest-spec): anchor the contract-field rows to §12, not to prose
`assert field in text` over the whole spec was green-but-dead: prose saturates
every field name (§1's honesty rule alone carries `generated: true` twice,
independent of the §12 table), so no amendment dropping a row could turn it red.
Measured: deleting the `generated` row from §12 left the file 15/15 green.

The spec appoints its own anchor — §12 says "completeness is enforced by the
spec-integrity test", and this is that test. The comment above _CONTRACT_FIELDS
already claimed §12; only the assertion did not. Each field must now appear as a
row's FIRST column, so a field named only inside another row's prose fails.

A slice guard comes with it: a `_cross_check_table()` that degenerated into the
full text would restore the dead state silently, so it is asserted to be a slice.

Detach proofs (mutate, run, restore from copy):
  M1 §12 `generated` row removed  -> RED [generated] only
  M2 row renamed to `generated_x` -> RED [generated] only (value proof)
  M3 §12 heading renamed          -> RED 14/16 (anchor gone, fail-closed)
  M4 slice returns whole spec     -> RED slice guard; [generated] goes GREEN
                                     again under M1 — the guard is what binds it
Control green, 637 -> 638.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TWrLqjkDvUtrGL5VXXe2ip
2026-07-31 18:26:35 +02:00

100 lines
3.7 KiB
Python

"""Spec-integrity seam for the ingest spec (ingest-spec §11).
The D7 analog of MAF's I1 framework-guard: this repo consumes ``shared/ingest-spec.md``
UNCHANGED from commons, and this test keeps the contract honest — it goes RED when the
spec goes missing, names a concrete agent toolkit (the framework-neutrality rule), or
stops documenting a contract field. It is the load-bearing guard the ingest layer relies
on to keep being implementable "from this spec alone".
"""
from __future__ import annotations
from pathlib import Path
import pytest
SPEC = Path(__file__).resolve().parents[1] / "shared" / "ingest-spec.md"
# Concrete agent toolkits / vendor stacks the framework-neutral spec MUST NOT name.
_FORBIDDEN_TOOLKITS = (
"claude",
"anthropic",
"openai",
"gpt",
"gemini",
"llama",
"langchain",
"autogen",
"crewai",
"semantic kernel",
"microsoft agent framework",
"agent sdk",
"bedrock",
"vertex",
"foundry",
"maf",
)
# The §12 cross-check table is the ANCHOR, and the spec appoints it itself: "Every field
# of the machine-readable contracts, mapped to its normative section (completeness is
# enforced by the spec-integrity test)" — this test is that enforcer. Asserting over the
# whole spec text instead would be green-but-dead: prose saturates the field names (§1's
# honesty rule alone carries `generated: true` twice), so no amendment dropping a row
# could ever turn it red.
_CROSS_CHECK_HEADING = "## 12. Cross-check table"
# Every field of the machine-readable contracts the D7 implementation depends on — the
# spec's §12 cross-check table must keep documenting each (spec-integrity).
_CONTRACT_FIELDS = (
"manifest_version",
"source",
"bundle_summary",
"extractions",
"source_system",
"source_query",
"ingested_at",
"ingest_manifest",
"generated",
"okf_type",
"max_rows",
"root",
"connection_ref", # the sql source reference the D7 sql connector (I5) depends on
)
def _cross_check_table() -> str:
"""The §12 section body — heading to end-of-spec or the next section, whichever first."""
text = SPEC.read_text(encoding="utf-8")
start = text.index(_CROSS_CHECK_HEADING) # RED (ValueError) if §12 is renamed or dropped
end = text.find("\n## ", start + len(_CROSS_CHECK_HEADING))
return text[start:] if end == -1 else text[start:end]
def test_spec_is_present() -> None:
# RED if the spec goes missing (the layer stops being implementable from spec alone).
assert SPEC.is_file(), "ingest-spec.md missing — subtree pull the commons contract"
def test_spec_names_no_agent_toolkit() -> None:
text = SPEC.read_text(encoding="utf-8").lower()
present = [tok for tok in _FORBIDDEN_TOOLKITS if tok in text]
assert not present, f"framework-neutral spec names a concrete toolkit: {present}"
def test_cross_check_slice_is_a_slice_and_not_the_whole_spec() -> None:
# Guards the anchor itself: a slice that degenerated into the full text would make
# every row assertion below green-but-dead again, silently. RED if it widens.
table = _cross_check_table()
assert table.startswith(_CROSS_CHECK_HEADING)
assert "Honesty rule" not in table, "slice leaked §1 prose — the anchor is not a slice"
assert "\n## " not in table, "slice leaked a following section"
@pytest.mark.parametrize("field", _CONTRACT_FIELDS)
def test_spec_documents_contract_field(field: str) -> None:
# The row's FIRST column is the documented-field claim; a field named only in another
# row's "Contract" prose does not count. RED when an amendment drops or renames a row.
table = _cross_check_table()
assert f"| `{field}` |" in table, (
f"contract field {field!r} is no longer a row in the §12 cross-check table"
)