Files
rune/scripts/gen_tier_truth.py
T
Nick e54360d0b0 feat(tier_manager): close PARTIAL gaps — dichotomy wave
Audit 6/10 PARTIAL -> 10/10 FULL (RUST, cognition_core per the audit
alias — tier_manager's live chain is cc_loader -> libcognition_core.so).
Four kernels ported: tier_demotion_sweep (named alias of the get_tier
decision), tier_get_tier_config (warm default for unknown tiers; wire
order mirrors the oracle dict), tier_should_search, tier_should_compress
(empty-dict False default) — FFI + loader wrappers + facade delegation
with load-bearing _py fallbacks, drift-immune recency cases in the
delegation suite.

Fix-first (proven by the frozen battery): parse_strict_aware accepted
out-of-calendar timestamps (month 13, Feb 30, day 0, hour 24 with
minutes) that datetime.fromisoformat raises on — oracle falls back to
warm, kernel computed off-calendar epochs. Range validation added
(leap years, month lengths, ISO hour-24 end-of-day); recorded as quirk

Verification: frozen truth 121 cases via the shared harness,
byte-identical on repeated freeze_truth --verify; cargo battery green
(include_str! embed); clippy -D warnings clean; live parity 1720/1720
(frozen rows + seeded fuzz + config fuzz, both clocks pinned); Python
both pins green (12/12 live + RUNE_COGNITION_CORE=python); ruff clean;
collateral green with kernels resolved (shims+tier+witness 59/59,
cognition consumers 109/109). Evidence: verification/summary-tier_manager.md
+ tier-* exits/logs.
2026-10-14 16:02:25 +01:00

239 lines
10 KiB
Python

#!/usr/bin/env python3
"""gen_tier_truth.py — freeze oracle truth for the tier_manager gap-close
(dichotomy wave, worktree rune-wave-cz-db).
Emits TWO artifacts into src/rust/cognition_core/src/ (both embedded
VERBATIM in the cfg(test) tier_battery):
frozen_tier_truth.json — the shared scripts/freeze_truth.py harness
schema ({"frozen_now", "cases"} envelope;
recv-form method cases). Re-freezable against
the live oracle byte-identically:
RUNE_COGNITION_CORE=python PYTHONPATH=src python3 scripts/freeze_truth.py \
--verify src/rust/cognition_core/src/frozen_tier_truth.json \
--module rune.tier_manager
(RUNE_COGNITION_CORE=python is the real oracle
pin here: tier_manager's _core probe honors the
SHARED cognition pin, and the harness's own
RUNE_TIER_MANAGER_CORE derivation is a no-op.)
frozen_tier_methods.json — kernel-shaped truth {"kernel","args","expect",
"name"} for the Rust battery replay, captured
from the oracle bodies under the envelope
clock (the kernels take `now` as an argument
per the no-clock law).
Case matrix: wash-count thresholds (incl. >=20 boundary), recency brackets
(6d hot / 7d-exact warm boundary / 20d warm / 30d-exact cold / 40d cold),
tz-offset timestamps, naive/malformed/typed refusals, bool wash_count
truthiness, and the calendar-range crash class (month 13, day 0, Feb 30,
hour 24, minute 61) — where datetime.fromisoformat raises and the tier
falls back to warm. The kernel's parse_strict_aware gained fromisoformat
range validation in this wave so the crash class matches on BOTH sides.
Corpora law: no sub-second timestamps — the W3.4 kernel parser is
seconds-resolution (fractional seconds dropped), so exact-boundary rows
with microseconds are excluded as a documented divergence kept out of truth.
"""
from __future__ import annotations
import json
import sys
from datetime import datetime, timezone
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO / "scripts"))
sys.path.insert(0, str(REPO / "src"))
import os
os.environ["RUNE_COGNITION_CORE"] = "python" # the SHARED oracle pin
import rune.tier_manager as tm # noqa: E402
from freeze_truth import freeze, write, write_envelope # noqa: E402
FROZEN_NOW = "2026-10-14T09:00:00"
NOW_Z = "2026-10-14T09:00:00Z" # kernel `now` argument (same instant, Z form)
HOT, WARM = 7.0, 30.0
# Pinned clock for the kernel-shaped capture (mirror of the harness
# _frozen_clock so both files freeze the same instant).
_PINNED_DT = datetime(2026, 10, 14, 9, 0, 0, tzinfo=timezone.utc)
class _FrozenDatetime(datetime):
@classmethod
def now(cls, tz=None): # noqa: ARG003
return _PINNED_DT
tm.datetime = _FrozenDatetime
TMGR = tm.TierManager()
ENTRIES: list[tuple[str, dict]] = [
("wash25hot", {"wash_count": 25}),
("recent6d", {"wash_count": 2, "last_accessed": "2026-10-08T09:00:00Z"}),
("edge1sunder", {"wash_count": 2, "last_accessed": "2026-10-07T09:00:01Z"}),
("exact7d", {"wash_count": 2, "last_accessed": "2026-10-07T09:00:00Z"}),
("warm20d", {"wash_count": 2, "last_accessed": "2026-09-24T09:00:00Z"}),
("exact30d", {"wash_count": 2, "last_accessed": "2026-09-14T09:00:00Z"}),
("cold40d", {"wash_count": 2, "last_accessed": "2026-09-04T09:00:00Z"}),
("tzplus", {"wash_count": 2, "last_accessed": "2026-10-13T14:30:00+05:30"}),
("tzminus", {"wash_count": 2, "last_accessed": "2026-09-24T09:00:00-05:00"}),
("naive", {"wash_count": 2, "last_accessed": "2026-10-05T09:00:00"}),
("junk", {"wash_count": 2, "last_accessed": "junk"}),
("junk19", {"wash_count": 2, "last_accessed": "junkjunkjunkjunkjunk"}),
("month13", {"wash_count": 2, "last_accessed": "2026-13-01T00:00:00Z"}),
("day0", {"wash_count": 2, "last_accessed": "2026-10-00T00:00:00Z"}),
("feb30", {"wash_count": 2, "last_accessed": "2026-02-30T00:00:00Z"}),
("feb29leap", {"wash_count": 2, "last_accessed": "2028-02-29T09:00:00Z"}),
("feb29leap2024", {"wash_count": 2, "last_accessed": "2024-02-29T00:00:00Z"}),
("hour24", {"wash_count": 2, "last_accessed": "2026-10-13T24:00:00Z"}),
("hour24nonzero", {"wash_count": 2, "last_accessed": "2026-10-13T24:30:00Z"}),
("min61", {"wash_count": 2, "last_accessed": "2026-10-13T10:61:00Z"}),
("min60", {"wash_count": 2, "last_accessed": "2026-10-13T00:60:00Z"}),
("sec60", {"wash_count": 2, "last_accessed": "2026-10-13T23:59:60Z"}),
("apr31", {"wash_count": 2, "last_accessed": "2026-04-31T00:00:00Z"}),
("wash19_999", {"wash_count": 19.999}),
("wash20boundary", {"wash_count": 20}),
("wash_bool", {"wash_count": True}),
("wash_neg", {"wash_count": -5}),
("nolast", {"wash_count": 2}),
("rockhot", {"wash_count": 25, "rock_status": "rock"}),
("rockish", {"rock_status": "rockish"}),
("rocknullts", {"wash_count": 0, "rock_status": "rock", "last_accessed": None}),
("lastnone", {"wash_count": 2, "last_accessed": None}),
("lastint", {"wash_count": 2, "last_accessed": 12345}),
("lastempty", {"wash_count": 2, "last_accessed": ""}),
]
BY_NAME = {name: entry for name, entry in ENTRIES}
# The oracle tier PER ENTRY (pinned clock) — self-check of the matrix.
EXPECTED_TIERS = {
"wash25hot": "hot", "recent6d": "hot", "edge1sunder": "hot",
"exact7d": "warm", "warm20d": "warm", "exact30d": "cold",
"cold40d": "cold", "tzplus": "hot", "tzminus": "warm", "naive": "warm",
"junk": "warm", "junk19": "warm", "month13": "warm", "day0": "warm",
"feb30": "warm", "feb29leap": "hot", "feb29leap2024": "cold",
"hour24": "hot", "hour24nonzero": "warm", "min61": "warm", "min60": "warm",
"sec60": "warm", "apr31": "warm",
"wash19_999": "warm", "wash20boundary": "hot", "wash_bool": "warm",
"wash_neg": "warm", "nolast": "warm", "rockhot": "hot", "rockish": "warm",
"rocknullts": "hot", "lastnone": "warm", "lastint": "warm", "lastempty": "warm",
}
def oracle_tier(name: str) -> str:
return TMGR._py_get_tier(BY_NAME[name])
def _self_check() -> None:
for name, want in EXPECTED_TIERS.items():
got = oracle_tier(name)
assert got == want, f"{name}: oracle {got!r} != matrix {want!r}"
def _recv_case(fn: str, name: str) -> dict:
return {"fn": f"TierManager.{fn}", "recv": {"cls": "TierManager"},
"args": [BY_NAME[name]], "kwargs": {}}
def _config_case(fn: str, tier: str) -> dict:
return {"fn": f"TierManager.{fn}", "recv": {"cls": "TierManager"},
"args": [tier], "kwargs": {}}
PROMOTE_DEMOTE_NAMES = ("wash25hot", "recent6d", "exact7d", "warm20d",
"exact30d", "cold40d", "rockhot", "naive")
PUBLIC_DEMOTE_NAMES = ("wash25hot", "warm20d", "cold40d", "rockhot",
"naive", "lastint")
SWEEP_NAMES = ("wash25hot", "recent6d", "exact7d", "warm20d", "cold40d",
"rockhot", "naive", "junk", "wash20boundary", "wash19_999")
TIERS = ("hot", "warm", "cold", "unknown", "")
def build_harness_cases() -> list:
cases = []
for name, _ in ENTRIES: # oracle body + public surface, every entry
cases.append(_recv_case("_py_get_tier", name))
cases.append(_recv_case("get_tier", name))
for name in PROMOTE_DEMOTE_NAMES:
cases.append(_recv_case("_py_promote", name))
cases.append(_recv_case("_py_demote", name))
for name in PUBLIC_DEMOTE_NAMES:
cases.append(_recv_case("promote", name))
cases.append(_recv_case("demote", name))
for name in SWEEP_NAMES:
cases.append(_recv_case("demotion_sweep", name))
for tier in TIERS:
cases.append(_config_case("get_tier_config", tier))
for tier in TIERS:
cases.append(_config_case("should_search", tier))
cases.append(_config_case("should_compress", tier))
return cases
def _promote_of(name: str) -> str:
cur = oracle_tier(name)
if cur == "cold":
return "warm"
return "hot" # warm already-hot both land hot (oracle ladder)
def _demote_of(name: str) -> str:
if BY_NAME[name].get("rock_status") == "rock":
return "hot" # rock never demotes (oracle short-circuit)
cur = oracle_tier(name)
return {"hot": "warm", "warm": "cold"}.get(cur, "cold")
def kernel_rows() -> list[dict]:
rows = []
def row(kernel: str, name: str, args: list, expect):
rows.append({"kernel": kernel, "args": args, "expect": expect,
"name": name})
def now_args(name: str) -> list:
return [BY_NAME[name], HOT, WARM, NOW_Z]
for name, _ in ENTRIES: # the tier decision kernel, every entry
row("tier_get", name, now_args(name), oracle_tier(name))
for name in PROMOTE_DEMOTE_NAMES:
row("tier_promote", name, now_args(name), _promote_of(name))
row("tier_demote", name, now_args(name), _demote_of(name))
for name in SWEEP_NAMES:
row("tier_demotion_sweep", name, now_args(name), oracle_tier(name))
for tier in TIERS:
cfg = tm.TIER_CONFIG.get(tier, tm.TIER_CONFIG["warm"])
row("tier_get_tier_config", f"tier={tier!r}", [tier], dict(cfg))
row("tier_should_search", f"tier={tier!r}",
[tier], tm.TIER_CONFIG.get(tier, {}).get("searched", False))
row("tier_should_compress", f"tier={tier!r}",
[tier], tm.TIER_CONFIG.get(tier, {}).get("compressed", False))
return rows
def main() -> int:
_self_check()
cases = build_harness_cases()
truth = freeze("rune.tier_manager", cases, oracle_module="rune.tier_manager")
write_envelope(FROZEN_NOW, truth,
REPO / "src/rust/cognition_core/src/frozen_tier_truth.json")
rows = kernel_rows()
write(rows, REPO / "src/rust/cognition_core/src/frozen_tier_methods.json")
print(f"harness cases: {len(truth)}")
print(f"kernel rows: {len(rows)}")
print("self-check:", json.dumps(EXPECTED_TIERS))
return 0
if __name__ == "__main__":
sys.exit(main())