Closes the honest Fase 2a limitation: docs_dir==bundle_dir let keyword
chunk-stuffing leak the verdict's realization rate ("0.82") into the debate /
generation prompt regardless of the ExpeL fold (it surfaced from both
verdict-led-fro.md AND golden.json). The realization signal now reaches the
hypothesis prompt ONLY via the gated ExpeL fold.
- okf.py: bundle_context() + Bundle.context_files render the navigated bundle
(index + frontmatter + cross-links) as the agent read-context, EXCLUDING
type: verdict (maalbilde §2/§4). Pure stdlib, still MAF-free.
- datasource.py: bundle_citations() derives first-class citations from the
navigated non-verdict files.
- run_project: on the bundle path context + citations + debate tools come from
navigation (tools=[]; navigation replaces query-time RAG); the road path keeps
chunk-stuffing unchanged.
Load-bearing (maalbilde §7): the marker is upgraded from the minted verdict id
to the realization signal itself. The empty-store control now asserts "0.82"
reaches NO prompt — RED against the pre-2b chunk-stuffing path, green after
navigation (TDD red->green). New okf-level test_bundle_context_excludes_verdict_layer
guards the seam directly.
Suite 133->134 passed, 4 skipped; mypy + ruff check clean. Reverted unrelated
ruff-format drift (backends/budget/verdicts/test_contracts) to keep the diff
surgical.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MHR8iKxJRxDiDfNw8HZmWE
116 lines
5.3 KiB
Python
116 lines
5.3 KiB
Python
"""OKF bundle navigation (okf.py) — the framework-neutral, D7-portable context seam.
|
|
|
|
These tests pin the minimal navigation the Step-1 ExpeL wiring depends on: read ``index.md``,
|
|
follow intra-bundle cross-links, parse each file's frontmatter, classify by ``type``, and locate
|
|
the candidate IR projection. Robustness (OKF SPEC §4): broken links are tolerated, never raised.
|
|
|
|
No ``agent_framework``/``mcp`` import is allowed in ``okf`` — guarded by ``test_okf_is_maf_free``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from portfolio_optimiser import okf
|
|
|
|
BUNDLE_DIR = Path(__file__).resolve().parents[1] / "shared" / "examples" / "bygg-energi-mikro"
|
|
|
|
|
|
def test_navigate_classifies_every_typed_file() -> None:
|
|
"""The energi bundle resolves to its six typed OKF files (index + project + hypothesis +
|
|
methodology + reference + verdict), each carrying its declared ``type``."""
|
|
bundle = okf.navigate_bundle(str(BUNDLE_DIR))
|
|
types = {f.name: f.type for f in bundle.files}
|
|
assert types == {
|
|
"index.md": "index",
|
|
"bygg-kontor-nord.md": "project",
|
|
"tiltak-led-retrofit.md": "hypothesis",
|
|
"metode-ipmvp-a.md": "methodology",
|
|
"kilder-realiseringsgap.md": "reference",
|
|
"verdict-led-fro.md": "verdict",
|
|
}
|
|
|
|
|
|
def test_navigate_exposes_verdicts_and_hypothesis() -> None:
|
|
"""The navigation surfaces exactly one ``type: verdict`` file (the ExpeL seed) and the
|
|
candidate hypothesis — the two the Step-1 wiring keys on."""
|
|
bundle = okf.navigate_bundle(str(BUNDLE_DIR))
|
|
assert [f.name for f in bundle.verdicts] == ["verdict-led-fro.md"]
|
|
assert bundle.verdicts[0].frontmatter["realization_rate"] == "0.82"
|
|
assert bundle.hypothesis is not None
|
|
assert bundle.hypothesis.name == "tiltak-led-retrofit.md"
|
|
|
|
|
|
def test_navigate_index_summary_is_progressive_disclosure() -> None:
|
|
"""``index_summary`` is the index body (the progressive-disclosure entry point), not stuffing
|
|
the whole bundle."""
|
|
bundle = okf.navigate_bundle(str(BUNDLE_DIR))
|
|
assert "progressiv disclosure" in bundle.index_summary.lower()
|
|
|
|
|
|
def test_bundle_context_excludes_verdict_layer() -> None:
|
|
"""Fase 2b LOAD-BEARING (okf level): ``bundle_context`` renders the concept files for the agent
|
|
prompt but EXCLUDES the ``type: verdict`` file, so the realization signal reaches a prompt only
|
|
via the gated ExpeL fold — never by stuffing it into the read-context (målbilde §2/§4)."""
|
|
bundle = okf.navigate_bundle(str(BUNDLE_DIR))
|
|
assert {f.type for f in bundle.context_files} == {
|
|
"project",
|
|
"hypothesis",
|
|
"methodology",
|
|
"reference",
|
|
}
|
|
context = okf.bundle_context(bundle)
|
|
assert "0.82" not in context # the verdict's realization signal is NOT stuffed in
|
|
assert (
|
|
bundle.verdicts[0].frontmatter["realization_rate"] == "0.82"
|
|
) # though it IS in the bundle
|
|
assert "## hypothesis:" in context # concept files ARE rendered (progressive disclosure)
|
|
assert "progressiv disclosure" in context.lower() # the index summary is the entry point
|
|
|
|
|
|
def test_navigate_tolerates_broken_links(tmp_path) -> None:
|
|
"""OKF SPEC §4: a consumer MUST tolerate broken links. An index linking a missing file
|
|
navigates without raising, simply omitting the absent target."""
|
|
(tmp_path / "index.md").write_text(
|
|
"---\ntype: index\n---\n\nSee [gone](missing.md) and [here](real.md).\n",
|
|
encoding="utf-8",
|
|
)
|
|
(tmp_path / "real.md").write_text("---\ntype: project\n---\n\nbody\n", encoding="utf-8")
|
|
bundle = okf.navigate_bundle(str(tmp_path))
|
|
names = {f.name for f in bundle.files}
|
|
assert names == {"index.md", "real.md"} # missing.md silently skipped, no raise
|
|
|
|
|
|
def test_load_ir_projection_returns_candidate_ir() -> None:
|
|
"""The bundle's IR projection (``validator-input.json``) is the candidate measure's cost-IR —
|
|
the pre-hypothesis ExpeL query key source."""
|
|
ir = okf.load_ir_projection(str(BUNDLE_DIR))
|
|
assert ir["project_id"] == "BYGG-KONTOR-NORD"
|
|
assert [a["code"] for a in ir["affected_items"]] == ["ENERGI-TOTAL-EL"]
|
|
assert ir["claimed_saving_nok"] == 30000
|
|
|
|
|
|
def test_parse_frontmatter_reads_scalar_fields() -> None:
|
|
"""The minimal frontmatter reader returns the leading ``---`` block as key:value strings."""
|
|
fm = okf.parse_frontmatter(BUNDLE_DIR / "verdict-led-fro.md")
|
|
assert fm["type"] == "verdict"
|
|
assert fm["decision"] == "approved_with_adjustment"
|
|
|
|
|
|
def test_okf_is_maf_free() -> None:
|
|
"""D7 portability: ``okf.py`` IMPORTS no ``agent_framework`` / ``mcp`` (the docstring may name
|
|
them to document the constraint, exactly as ``retrieval.py`` does) — checked via the AST, not a
|
|
raw substring, so the prose claim doesn't trip the guard."""
|
|
import ast
|
|
|
|
src = (
|
|
Path(__file__).resolve().parents[1] / "src" / "portfolio_optimiser" / "okf.py"
|
|
).read_text(encoding="utf-8")
|
|
imported: list[str] = []
|
|
for node in ast.walk(ast.parse(src)):
|
|
if isinstance(node, ast.Import):
|
|
imported += [a.name for a in node.names]
|
|
elif isinstance(node, ast.ImportFrom):
|
|
imported.append(node.module or "")
|
|
forbidden = [m for m in imported if m.split(".")[0] in {"agent_framework", "mcp"}]
|
|
assert forbidden == [], f"okf.py must not import MAF/mcp, found: {forbidden}"
|