fix(p20): code_forms follows ITS OWN approach, and the announcement has a witness [skip-docs]

Two defects the mutation battery and the paid round found, both measured before
being touched.

(1) code_forms described the WRONG candidate. Every per-approach artefact copied
the run's stamp and overrode only validator_decision, so an artefact about
approach 2 reported approach 1's codes. Measured in BOTH round 3 and round 4 --
and stress.py, which reads this field before re-deriving, then produced an EMPTY
prose_codes for every approach but the first, which is what round 3's table was
built on. The field's own comment already says it is stamped "off the proposal
being stamped"; run-level was the drift, not the intent. Model, citations and
token usage stay the run's, because they are the run's.

(2) The C2 announcement seam had no witness. Mutation C-iii reverted the call
site to `args.project_id or "the portfolio"` and the WHOLE suite stayed green
(1808/5): all three arms drove announced_subject directly. The missing arm drives
main() on a free dry run and reads the announcement off STDOUT, where an operator
reads it, and is red against exactly that mutation.

Sixteen mutations, ALL red against the whole suite. Green control 1809/5 (from
1781, +28, 0 removed), golden demo-transcript.stdout BYTE-UNCHANGED
(shasum -a 1 of the CONTENT = ea8c534773acdbe41ae68f2c55724d69aaf8be4f).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-09-15 08:28:44 +02:00
commit e513bc97ad
4 changed files with 139 additions and 3 deletions

View file

@ -28,9 +28,12 @@ What each arm pins:
from __future__ import annotations
import asyncio
import json
from pathlib import Path
from typing import Any
import pytest
from portfolio_optimiser.budget import Budget, TokenMeter
from portfolio_optimiser.generate import ParseFailure, _build_messages, generate_via_llm
from portfolio_optimiser.reference_domain import Project
@ -122,6 +125,42 @@ def test_an_unresolvable_base_falls_back_to_its_directory_name(tmp_path: Path) -
assert announced_subject(None, (str(missing),)) == "not-a-base"
def test_the_cli_prints_the_routed_bases(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""(e2) The WIRING, not the renderer.
MEASURED: mutation C-iii reverted the call site to ``args.project_id or "the portfolio"`` and
the WHOLE suite stayed green (1808/5) the renderer had three arms and the call site none, so
the seam C2 exists for was unwitnessed. This arm drives ``main()`` on a free dry run and reads
the announcement off stdout, which is where an operator reads it.
"""
from portfolio_optimiser.run import main
mandate = tmp_path / "m.json"
mandate.write_text(
json.dumps({"objective": "cut cost", "approaches": [], "allow_own_proposals": True}),
encoding="utf-8",
)
rc = main(
[
"--across-bundle",
str(_TUNNEL),
"--mandate",
str(mandate),
"--run-id",
"r1",
"--outbox-dir",
str(tmp_path / "out"),
"--live-dry-run",
]
)
out = capsys.readouterr().out
assert rc == 0, out
assert "Run mandate for tunnel-hauglia" in out
assert "the portfolio" not in out
def test_the_two_older_subjects_are_unchanged() -> None:
"""(g) A named project wins; no bases at all is still the portfolio."""
assert announced_subject("proj-1", ()) == "proj-1"

View file

@ -273,3 +273,80 @@ def test_a_run_composes_the_vocabulary_from_the_base_it_opened(tmp_path: Path) -
)
assert result.provenance.code_forms == {"12.1": "requirement"}
assert result.provenance.validator_decision == "validated"
def test_each_approachs_artefact_carries_its_own_code_forms(tmp_path: Path) -> None:
"""(j) MEASURED on round 3 AND round 4: every per-approach artefact carried the SELECTED
proposal's codes.
``code_forms``' own comment says it is derived "off the proposal being stamped", and the
per-approach branch copied the run's stamp while overriding only ``validator_decision``. So an
artefact about approach 2 reported approach 1's codes — and ``stress.py``, which reads this
field before re-deriving, then produced an EMPTY ``prose_codes`` for every approach but the
first, because none of that approach's codes was a key in the map it was handed.
Run-level was the drift, not the intent: model, citations and token usage ARE the run's, and
they stay so.
"""
import asyncio
from agent_framework import BaseChatClient
from portfolio_optimiser.mandate import Approach, Mandate
from portfolio_optimiser.run import run_project
from portfolio_optimiser.simulation import ScriptedChatClient
base = tmp_path / "mini"
base.mkdir()
(base / "index.md").write_text(
"---\nbundle_id: mini\n---\n\n- [Krav](krav.md) — one clause.\n", encoding="utf-8"
)
(base / "krav.md").write_text(
"---\ntype: Krav\ntitle: Krav 4.1.2-1\nprosessnr: '12.1'\n---\n\nLines 12.1 and PRIS-EN.\n",
encoding="utf-8",
)
(base / "validator-input.json").write_text(
json.dumps({"project_id": "mini-p", "measure": "m", "affected_codes": ["12.1"]}),
encoding="utf-8",
)
outbox = tmp_path / "out"
# Each approach's prompt names its own label, so the selector answers with ITS OWN code.
def select(prompt: str, role: str) -> str:
if role == "checker":
return "Reasoning holds.\nVERDICT: APPROVE"
code = "PRIS-EN" if "SECOND" in prompt else "12.1"
return (
'{"measure":"m","affected_items":[{"code":"%s","quantity":10,"unit_cost":1000}],'
'"claimed_saving_nok":1000}' % code
)
def factory(role: str) -> BaseChatClient:
return ScriptedChatClient(reply_selector=select, role=role)
asyncio.run(
run_project(
"mini-p",
"local",
docs_dir=str(base),
bundle_dir=str(base),
client_factory=factory,
mandate=Mandate(
objective="o",
approaches=(
Approach(id="a1", label="FIRST", description="d"),
Approach(id="a2", label="SECOND", description="d"),
),
allow_own_proposals=False,
),
outbox_dir=str(outbox),
run_id="r1",
)
)
forms = {}
for name in ("a1", "a2"):
payload = json.loads((outbox / f"r1-{name}-proposal.json").read_text(encoding="utf-8"))
codes = [i["code"] for i in payload["proposal"]["affected_items"]]
forms[name] = (codes, payload["provenance"]["code_forms"])
assert forms["a1"] == (["12.1"], {"12.1": "requirement"}), forms["a1"]
assert forms["a2"] == (["PRIS-EN"], {"PRIS-EN": "identifier"}), forms["a2"]