portfolio-optimiser/tests/test_ingest_stamp_fail_closed_loadbearing.py
Kjell Tore Guttormsen fed69790ac fix(okf): close the inert ingest-stamp guard, then land okf 0.8.5 -- and read the block sources form all four bases actually write
P13 measured this lift and REFUSED it, because okf >=0.8.5 emits the ownership
stamp as the V1 flow mapping `generated: { by: process:okf-ingest, at: ... }`
where 0.3.2 emitted `true`, and `_carries_complete_ingest_stamp` read the new
form as NOT a stamp -- write_concept_file's forgery refusal would have shipped
DISARMED with the whole fail-closed suite green. That blocker is closed first,
red-first, and then the pin moves.

ROW 1, THE SECURITY HALF. `_claims_ingest_ownership` widens the predicate from
"reads as boolean True" to "claims ingest ownership", of which the boolean is
the pre-V1 spelling. The recogniser for the new half is `decode_flow_value` --
the module's ONE flow decoder, the same argument write_concept_file already
makes for `verified`: the writer refuses exactly what the reader can read. A
value the decoder REFUSES is therefore not an ownership claim and writes
through, which is what keeps this from collapsing into "any non-empty
generated". Two arms red before the fix; no YAML library introduced.

THE PIN. okf v0.3.2 -> v0.8.5, guard v0.3.4 -> v1.4.0 spelled `tag =`, not
`rev =`, and not the declared floor 1.2.0 -- both P13 premises hold and the
reason now lives next to the pin in pyproject.toml. The ":40" comment is
corrected: okf has ONE runtime dependency, the guard, and that is what binds
the two lines together. 27/27 imported names resolve across five modules.

THE GOLDENS, REGENERATED AS A DECISION. Seven concept files across four
examples/ingest-golden-* bundles, one line each. Two were regenerated by the
REAL materializer; the other five are derived (http/sql/mcp cannot materialize
outside the tests' stubs) and then MEASURED -- all four golden suites compare
byte for byte against what the stubs produce, and all four are green. The four
`generated == "true"` asserts now read ONE source, conftest.
expected_generated_stamp: four literals for one emitter fact are four places a
later release can leave half-corrected, which is exactly how the pre-V1 form
survived until P13 measured it. tests/test_okf.py keeps its literal on purpose
-- that one round-trips a CURATED half-stamp through our own writer.

THE BLOCK READER. Measured with the full denominator: all four delivered
knowledge bases write `sources` as a BLOCK sequence and none in flow form
(n100 446/446, n200 1133/1133, n500 270/270, r761 2756/2756 = 4605/4605), and
`evidence_for` reported `unreadable` on 4605 of 4605 -- the falsification layer
had no address for any document in any base. `okf.decode_block_mappings` is the
second CARRIER of one grammar, never a second grammar: colon-SPACE separator,
unquote_scalar, duplicate keys refused, SPEC 5.2's actor rule applied. okf's
consume.read_sources was READ for the form and not called; po calls no okf
reader, which is measured and deliberate. After: 4605 present / 4605 entries.
Reading is not a licence to WRITE -- the emitter is untouched and both writers
still refuse what decode_flow_value refuses.

THREE FINDINGS. (1) The first block reader INVENTED data on `- { k: v }` items
-- SPEC-canonical, and the shape tests/golden/block-form-provenance writes for
`verified` -- decoding it as `{'{ id': '...'}`. No arm caught it: the 5.2 actor
rule shielded the fixture by accident. Closed with a flow-decoder branch and
four new arms. (2) One of my own arms was VACUOUS, found by my own mutation M5:
it claimed to prove the colon-SPACE rule and stayed green under first-colon,
because the two rules agree on every delivered value. Renamed, labelled, and
the claim moved to the arm that actually witnesses it. (3) OPEN, and it needs
the operator: the commons-owned worked example declares its second concept
`unreadable`/`block-sequence`, which is now false for po. `shared/` is
pull-only, so closing it needs a commons amendment; the test asserts the
divergence instead of skipping it, keeping the discriminating half (the example
says two entries were seen and the reader returns exactly two).

NINE EXISTING ARMS REWRITTEN, NONE WEAKENED. All nine pinned "the block form is
unreadable" -- the behaviour this order changes. Each keeps its claim on a
specimen that is still unreadable for a reason of its own (5.2: an entry naming
no actor), or pins the REVERSED direction where the old arm stood so the change
cannot be silent. Two got STRONGER: multi-verified.md was authored for "a reader
keeping the last entry reports machine-confirmed for a concept a human signed",
and that could not be tested while the form was unreadable. Three node ids were
renamed; nothing was removed in substance.

Suite 1582 -> 1606 passed / 5 skipped. Both demo goldens byte-unchanged
(ea8c534... / ede3e2f..., shasum -a 1 of the CONTENT, never the git blob id).
ruff check / ruff format / mypy green. shared/ untouched.

Six mutations, all red against the WHOLE suite, each with its own signature:
row 1 detached (2) / block reader detached (17) / flow-item branch detached (7)
/ a stray indented line folds into an INVENTED entry (4) / separator becomes the
first colon (1 -- and that is finding 2) / the stamp expectation reverts to
"true" (4).

Order: 20260912T195112Z-995611104-from-.claude
Record: docs/2026-09-12-p13b-okf-bump.md

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 23:02:38 +02:00

140 lines
7.2 KiB
Python

"""RED-FIRST: the ingest ownership stamp check must be FAIL-CLOSED on the *value*, not just on
field presence. Before this fix ``_carries_complete_ingest_stamp`` compared ``generated`` against
the single literal string ``"true"`` — a YAML-1.1 truthy form such as ``yes``/``on`` slipped the
gate open even though a real YAML reader reads it as boolean ``True`` (measured with PyYAML's
``safe_load`` core-schema resolver, the same resolver family ``ruamel``/most YAML tooling ships:
``yes``/``no``/``true``/``false``/``on``/``off``, any case, are read as bool; bare ``y``/``n`` and
``1``/``0`` are NOT — they resolve to string/int). ``okf.py`` stays a hand-rolled, PyYAML-free
line parser (D7 constraint), so the vocabulary is a plain literal set, not a real parser call."""
import pytest
from portfolio_optimiser import okf
def test_yaml_truthy_form_yes_is_recognized_as_the_stamp(tmp_path) -> None:
"""``generated: yes`` is read as boolean True by a real YAML reader, so it is the SAME stamp
as ``generated: true`` and must be refused identically."""
fm = {"type": "reference", "generated": "yes", "ingest_manifest": "bygg@0123456789abcdef"}
with pytest.raises(okf.IngestStampError):
okf.write_concept_file(str(tmp_path), "forged.md", fm, "body\n")
assert not (tmp_path / "forged.md").exists() # refused, not silently repaired
def test_yaml_truthy_form_on_mixed_case_is_recognized_as_the_stamp(tmp_path) -> None:
"""Case-insensitivity must hold for every truthy form, not only ``true``/``True``/``TRUE``."""
fm = {"type": "reference", "generated": "On", "ingest_manifest": "bygg@0123456789abcdef"}
with pytest.raises(okf.IngestStampError):
okf.write_concept_file(str(tmp_path), "forged.md", fm, "body\n")
assert not (tmp_path / "forged.md").exists()
def test_literal_true_still_refused_control(tmp_path) -> None:
"""Control: the literal the gate has always recognized must still raise — otherwise the two
assertions above could pass against a gate that raises unconditionally, proving nothing."""
fm = {"type": "reference", "generated": "true", "ingest_manifest": "bygg@0123456789abcdef"}
with pytest.raises(okf.IngestStampError):
okf.write_concept_file(str(tmp_path), "forged.md", fm, "body\n")
assert not (tmp_path / "forged.md").exists()
def test_non_yaml_boolean_forms_are_not_treated_as_the_stamp(tmp_path) -> None:
"""``1`` and bare ``y`` are NOT read as boolean True by a real YAML reader (measured: PyYAML
resolves them to int ``1`` and string ``"y"``, never bool) — widening the true-vocabulary to
include them would over-block curated content no ingest pipeline ever produces, and no YAML
reader would read as the stamp either. Both write through unchanged, same as any other
unrecognized ``generated`` value paired with a manifest."""
for value in ("1", "y", "maybe"):
name = f"curated-{value}.md"
okf.write_concept_file(
str(tmp_path),
name,
{"type": "reference", "generated": value, "ingest_manifest": "bygg@0123456789abcdef"},
"body\n",
)
assert (tmp_path / name).exists()
def test_half_stamp_remains_legal_generated_alone(tmp_path) -> None:
"""CLAUDE.md's ingest-stamp invariant: either half of the stamp alone is still legal curated
content — even when that half is written in a non-``true`` truthy form."""
okf.write_concept_file(str(tmp_path), "a.md", {"type": "reference", "generated": "yes"}, "b\n")
assert (tmp_path / "a.md").exists()
def test_half_stamp_remains_legal_manifest_alone(tmp_path) -> None:
okf.write_concept_file(
str(tmp_path),
"b.md",
{"type": "reference", "ingest_manifest": "bygg@0123456789abcdef"},
"c\n",
)
assert (tmp_path / "b.md").exists()
def test_unstamped_file_written_as_before(tmp_path) -> None:
okf.write_concept_file(str(tmp_path), "c.md", {"type": "reference"}, "d\n")
assert (tmp_path / "c.md").exists()
# --- P13b row 1: the V1 provenance form of the SAME stamp -------------------------------------
#
# RED-FIRST, measured 2026-09-12: ``llm-ingestion-okf`` v0.8.5 emits the ownership stamp as a flow
# MAPPING — ``generated: { by: process:okf-ingest, at: <ingested_at> }`` — where v0.3.2 emitted the
# literal ``true``. ``_YAML_TRUE_LITERALS`` cannot see that form (measured: ``True`` on the literal,
# ``False`` on the mapping), so the forgery refusal went INERT on exactly the form the pinned writer
# now produces, with this whole file GREEN. That is the trap this file's own docstring was written
# for, arriving by a spelling it did not anticipate.
#
# The predicate widens from "reads as boolean True" to "claims ingest ownership", of which the
# boolean is the pre-V1 spelling. The recogniser for the new half is ``decode_flow_value`` — the ONE
# decoder, the same argument ``write_concept_file`` already makes for ``verified``: the writer
# refuses exactly what the reader can read, and a second copy of the rule would be free to drift.
_V1_STAMP = "{ by: process:okf-ingest, at: 2026-07-03T12:00:00Z }"
def test_v1_flow_mapping_form_is_recognized_as_the_stamp(tmp_path) -> None:
"""The form okf >=0.8.5 actually emits, measured against the golden bundles it materialises."""
fm = {"type": "reference", "generated": _V1_STAMP, "ingest_manifest": "bygg@0123456789abcdef"}
with pytest.raises(okf.IngestStampError):
okf.write_concept_file(str(tmp_path), "forged.md", fm, "body\n")
assert not (tmp_path / "forged.md").exists() # refused, not silently repaired
def test_v1_flow_sequence_form_is_recognized_too(tmp_path) -> None:
"""SPEC §5.2's one-element MUST reads a bare mapping as a one-element list, so the sequence
spelling of the same claim cannot be the way past the gate. Fail-closed by construction."""
fm = {
"type": "reference",
"generated": f"[{_V1_STAMP}]",
"ingest_manifest": "bygg@0123456789abcdef",
}
with pytest.raises(okf.IngestStampError):
okf.write_concept_file(str(tmp_path), "forged.md", fm, "body\n")
assert not (tmp_path / "forged.md").exists()
def test_v1_half_stamp_remains_legal(tmp_path) -> None:
"""The half-stamp rule is UNCHANGED by the widening: a provenance mapping alone, with no
manifest, is still legitimate curated content and is written."""
okf.write_concept_file(
str(tmp_path), "a.md", {"type": "reference", "generated": _V1_STAMP}, "b\n"
)
assert (tmp_path / "a.md").exists()
def test_an_undecodable_generated_value_is_not_the_stamp(tmp_path) -> None:
"""The discriminator that keeps the new half from collapsing into "any non-empty value": a
``generated`` the decoder REFUSES is not an ownership claim, so it writes through paired with a
manifest exactly as ``1``/``y``/``maybe`` do above. Without this arm, a gate widened to
``bool(generated)`` would pass every arm in this file."""
for value in ("{ broken", "[not-a-mapping]", "{ nosep }"):
name = f"curated-{abs(hash(value))}.md"
okf.write_concept_file(
str(tmp_path),
name,
{"type": "reference", "generated": value, "ingest_manifest": "bygg@0123456789abcdef"},
"body\n",
)
assert (tmp_path / name).exists()