PolyHedra / tests /test_baseline_selector.py
yf176
feat: select typed YOLO baselines
c690519
Raw History Blame Contribute Delete
3.76 kB
"""Typed V8 baseline selection is session-local, explicit, and immutable."""
from polyhedra.core.baseline_selector import select_typed_yolo_baseline
def artifact(run: str, *, ticker: str = "SPY") -> dict:
return {
"format": "JSON",
"schema_version": "8.0",
"artifact_id": f"DataFetcher-{run}-{ticker}",
"run_id": run,
"session_id": "session1",
"producer": "DataFetcher",
"path": f"quant/runs/{run}/raw/{ticker}.json",
}
def record(
run: str,
*,
completed_at: str,
status: str = "completed",
schema_version: str = "8.0",
execution_context: str = "YOLO",
session_id: str = "session1",
tickers=("SPY",),
) -> dict:
return {
"schema_version": schema_version,
"run_id": run,
"session_id": session_id,
"execution_context": execution_context,
"status": status,
"completed_at": completed_at,
"ticker_scope": list(tickers),
"artifact_inventory": [artifact(run, ticker=tickers[0])],
"additional_runtime_fields": "ignored by the selector projection",
}
def select(records, scope=("SPY",)):
return select_typed_yolo_baseline(
records,
session_id="session1",
current_run_id="current",
ticker_scope=list(scope),
)
def test_latest_completed_eligible_run_is_selected():
result = select([
record("older", completed_at="2026-09-08T12:00:00Z"),
record("latest", completed_at="2026-09-09T12:00:00Z"),
])
assert result.run_mode == "DELTA"
assert result.baseline_run_id == "latest"
assert result.baseline_artifacts[0].run_id == "latest"
assert result.missing_tickers == []
def test_failed_latest_run_does_not_replace_completed_baseline():
result = select([
record("stable", completed_at="2026-09-08T12:00:00Z"),
record("failed", completed_at="2026-09-09T12:00:00Z", status="failed"),
])
assert result.baseline_run_id == "stable"
assert [(item.run_id, item.code) for item in result.rejections] == [
("failed", "NOT_COMPLETED"),
]
def test_cross_session_support_current_and_unsupported_runs_are_explicitly_rejected():
result = select([
record("other", completed_at="2026-09-09T12:00:00Z", session_id="session2"),
record("support", completed_at="2026-09-09T12:00:00Z", execution_context="VISUALIZATION"),
record("current", completed_at="2026-09-09T12:00:00Z"),
record("legacy", completed_at="2026-09-09T12:00:00Z", schema_version="7.1"),
])
assert result.run_mode == "BASELINE"
assert result.baseline_run_id is None
assert [item.code for item in result.rejections] == [
"CROSS_SESSION", "NOT_YOLO", "CURRENT_RUN", "UNSUPPORTED_SCHEMA",
]
def test_new_ticker_is_reported_without_discarding_prior_run():
result = select([
record("prior", completed_at="2026-09-09T12:00:00Z", tickers=("SPY",)),
], scope=("SPY", "QQQ"))
assert result.baseline_run_id == "prior"
assert result.baseline_ticker_scope == ["SPY"]
assert result.missing_tickers == ["QQQ"]
def test_invalid_inventory_is_rejected_instead_of_becoming_delta_context():
invalid = record("invalid", completed_at="2026-09-09T12:00:00Z")
invalid["artifact_inventory"][0]["run_id"] = "different"
result = select([invalid])
assert result.run_mode == "BASELINE"
assert result.rejections[0].code == "INVALID_RECORD"
def test_selection_is_frozen_and_does_not_alias_input_records():
prior = record("prior", completed_at="2026-09-09T12:00:00Z")
result = select([prior])
prior["ticker_scope"].append("QQQ")
assert result.baseline_ticker_scope == ["SPY"]
assert result.baseline_run_id == "prior"