portfolio-optimiser/tests/test_shared_root_loadbearing.py
Kjell Tore Guttormsen cebba7d954 feat(shared-root): S3 — configurable shared-root resolver with load-bearing override test
One call-time resolver (env PORTFOLIO_SHARED_ROOT, default the in-repo
shared/) consumed by both MAF-side readers of the shared core:
persona._example_path() (the _EXAMPLE_PATH monkeypatch seam is kept) and
simulation._default_bundle_dir() (replaces the _BUNDLE_DIR module global).
De-risks the S4 extraction: re-pointing the commons becomes an env var,
not a code change. Override test proves the marker follows a tmp copy of
the whole shared tree; both detach points proven RED. Suite 155->157.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AaQCFnfsh3tfq1VfzdJpoi
2026-07-03 01:10:05 +02:00

61 lines
3 KiB
Python

"""Load-bearing tests for the configurable shared-root resolver (S3, R1-forberedelse).
Both MAF-side consumers of the shared framework-neutral core resolve its location through ONE
seam — ``shared_root()`` (env ``PORTFOLIO_SHARED_ROOT``, default: the in-repo ``shared/``) — read
at CALL time. That seam is what makes the S4 extraction (``shared/`` -> commons repo) a re-point,
not a code change. The override test goes RED the moment a consumer stops resolving through the
seam: a hardcoded path keeps reading the repo tree, so the tmp copy's swapped marker never
surfaces.
"""
from __future__ import annotations
import json
import shutil
from pathlib import Path
from portfolio_optimiser import persona, simulation
from portfolio_optimiser.shared_root import shared_root
from portfolio_optimiser.simulation import simulate_learning_loop
REPO_ROOT = Path(__file__).resolve().parents[1]
def test_shared_root_defaults_to_in_repo_tree(monkeypatch) -> None:
"""Without the env override, the resolver yields today's in-repo ``shared/`` — the S3 change is
behaviour-preserving for every existing caller."""
monkeypatch.delenv("PORTFOLIO_SHARED_ROOT", raising=False)
assert shared_root() == REPO_ROOT / "shared"
async def test_shared_root_override_redirects_both_consumers(tmp_path: Path, monkeypatch) -> None:
"""Point ``PORTFOLIO_SHARED_ROOT`` at a tmp COPY of ``shared/`` carrying a swapped persona
marker and confirm (a) the persona loader reads the copy, (b) the simulation's default bundle
resolves under the copy, and (c) the end-to-end sim traces the COPY's marker across the
promotion loop. RED without the resolver: the env var is ignored and the repo artifact's
marker is read instead."""
copy_root = tmp_path / "shared-copy"
shutil.copytree(REPO_ROOT / "shared", copy_root)
swapped_marker = "realiseringsgrad=0.57"
example_path = copy_root / "skills" / "expert-reviewer" / "references" / "example-verdict.json"
data = json.loads(example_path.read_text(encoding="utf-8"))
assert data["marker"] != swapped_marker, "control: the swap must differ from the repo artifact"
data["marker"] = swapped_marker
data["rationale"] = f"Godkjent. I drift realiseres erfaringsvis ~57% ({swapped_marker})."
example_path.write_text(json.dumps(data), encoding="utf-8")
monkeypatch.setenv("PORTFOLIO_SHARED_ROOT", str(copy_root))
example = persona.load_persona_example()
assert example.marker == swapped_marker, "persona loader did not resolve via shared_root()"
bundle_dir = simulation._default_bundle_dir()
assert bundle_dir == copy_root / "examples" / "bygg-energi-mikro", (
"simulation default bundle did not resolve via shared_root()"
)
result = await simulate_learning_loop(str(bundle_dir), str(tmp_path / "work"))
assert result.marker == swapped_marker
assert not result.marker_in_run_a_prompt, "swapped marker leaked into Run A (causality broken)"
assert result.marker_in_run_b_prompt, "the copy's marker did not cross into Run B's prompt"