Files
rune/scripts/context_pressure_truth.py
T
Nick e8f1503028 feat(context_pressure): close PARTIAL gaps — dichotomy wave
FULL parity port to Rust (9/9 fns, was 3/9): token estimation (codepoint
len law), the pressure ladder (raw-gate vs round3 display), degradation
signals (int-0 max law, \w+ runs), JSONL entry assembly (dict-spread
override), log tail reader (deque(maxlen) + file-iteration law), stats
counter (crash parity on non-dict lines), on-message hook (truthiness,
message/text/str(data) fallback, pyrepr), CLI dispatch (argparse shapes,
.1% renders, code-2 law). File IO + wall clock stay in the Python facade
(no-clock law); cps_loader.py is the ctypes seam (RUNE_CPS_CORE /
RUNE_CONTEXT_PRESSURE_CORE pins).

Frozen truth 75 cases under the cps_freeze_seams rig (byte-identical x2,
shared-harness --verify OK 28 envelope cases); branch battery 10/10;
live parity frozen=78 regen=6 fuzz=1400 live=18 failures=0; pytest 34/34
both pins; clippy -D warnings + fmt clean; audit 3/9 PARTIAL -> 9/9 FULL
(RUST). Evidence: verification/summary-cp.md + cp-* files.
2026-10-14 14:06:31 +01:00

351 lines
14 KiB
Python

#!/usr/bin/env python3
"""context_pressure_truth — shared capture/regeneration helpers for the
context_pressure dichotomy gap-close (issue #2479 tranche).
Used by scripts/gen_context_pressure_truth.py (freeze) and
scripts/check_context_pressure_parity.py (independent regeneration for the
truth-consumed-not-self-generated gate). EVERY process that imports this
module MUST run under the scripts/cps_freeze_seams rig
(PYTHONPATH=scripts/cps_freeze_seams) so:
* RUNE_CPS_CORE/RUNE_CONTEXT_PRESSURE_CORE=python — poisoned-truth law:
the ORACLE FLOOR captures/regenerates every expectation.
* datetime is frozen to CPS_FROZEN_NOW — entry lines are byte-stable.
* RUNE_MEMORY_DIR points at the hermetic scratch; LOG_PATH (resolved at
oracle import) lives there. CPS_SEED_LOG injects log file content for
CLI subprocess rows (the rig wipes and reseeds at import).
Kernel case shapes:
logMeasurement args {measurement, session_id, now} → expect {line}
readLog args {content, limit} → expect [entries]
logStats args {content} → expect counts | __crash__
kernelHook args {event, data, now} → expect {result, entry} | __crash__
mainDispatch args {argv, content, now} → expect {output, code} [, entry doc] | {output, code}
Crash rows store "__crash__ TYPE: message" — the exact str(e) contract.
"""
from __future__ import annotations
import copy
import json
import os
import subprocess
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
FROZEN_NOW = os.environ.get("CPS_FROZEN_NOW", "2026-10-14T01:02:03.456789+00:00")
SCRATCH = os.environ.get("CPS_FREEZE_SCRATCH", "/tmp/cps_gap_freeze")
LOG_FILE = os.path.join(SCRATCH, "memory", "index", "context_pressure.jsonl")
ORACLE = REPO / "src" / "rune" / "context_pressure.py"
RIG_DIR = REPO / "scripts" / "cps_freeze_seams"
def _oracle():
import rune.context_pressure as cp
return cp
def _seed_log(content: str) -> None:
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
with open(LOG_FILE, "w") as f:
f.write(content)
def _read_log_file() -> str:
try:
with open(LOG_FILE, "r") as f:
return f.read()
except OSError:
return ""
# ── kernel-case capture (oracle floor) ────────────────────────────────────
def capture_log_measurement(measurement, session_id, now):
_seed_log("")
cp = _oracle()
cp.log_measurement(measurement, session_id)
return {"line": _read_log_file()}
def capture_read_log(content, limit):
_seed_log(content)
cp = _oracle()
return cp.read_log(limit=limit)
def capture_log_stats(content):
_seed_log(content)
cp = _oracle()
try:
return cp.log_stats()
except Exception as e: # noqa: BLE001 — crash parity capture
return {"__crash__": f"{type(e).__name__}: {e}"}
def capture_hook(event, data):
_seed_log("")
cp = _oracle()
try:
result = cp.handle_hook(event, data)
except Exception as e: # noqa: BLE001 — crash parity capture
return {"__crash__": f"{type(e).__name__}: {e}"}
entry = None
for line in _read_log_file().splitlines():
if line.strip():
entry = json.loads(line)
return {"result": result, "entry": entry}
def capture_cli(argv, seed_content):
"""Run the real CLI (src/rune/context_pressure.py) under the rig; return
{output, code} plus the entry line the hook arm appended (if any).
`argv` carries the synthetic program name at [0] (parse_cli law: the
kernel skips argv[0] exactly like the oracle skips sys.argv[0]); the
subprocess receives argv[1:]."""
_seed_log(seed_content)
env = dict(os.environ)
rig = str(RIG_DIR)
if rig not in env.get("PYTHONPATH", ""):
env["PYTHONPATH"] = rig + (":" + env["PYTHONPATH"] if env.get("PYTHONPATH") else "")
# CLI subprocess rows: the scratch lives in the /tmp rig layout — the
# seeded content rides CPS_SEED_LOG (the subprocess's rig re-seeds it
# after its scratch wipe).
env["CPS_SEED_LOG"] = seed_content
env["RUNE_MEMORY_DIR"] = os.path.join(SCRATCH, "memory")
proc = subprocess.run(
[sys.executable, str(ORACLE), *argv[1:]],
capture_output=True, text=True, env=env,
)
out = {"output": proc.stdout[:-1] if proc.stdout.endswith("\n") else proc.stdout,
"code": proc.returncode}
if any(a == "hook" for a in argv):
entry = None
for line in _read_log_file().splitlines():
if line.strip():
try:
entry = json.loads(line)
except json.JSONDecodeError:
continue
out["entry"] = entry
return out
# ── the corpora ───────────────────────────────────────────────────────────
def envelope_case_specs():
"""The pure-surface rows (regenerated independently by
freeze_truth.py --verify): (fn, args, kwargs)."""
big = "x" * 95000 # ok pressure at default max_tokens
return [
("estimate_tokens", ("",), {}),
("estimate_tokens", ("hello world",), {}),
("estimate_tokens", ("héllo wörld",), {}),
("estimate_tokens", ("👍👍",), {}),
("estimate_tokens", ("x" * 1000,), {}),
("measure_pressure", ("",), {}),
("measure_pressure", ("hello world",), {}),
("measure_pressure", ("x" * 100000,), {}),
("measure_pressure", ("x" * 49000,), {"max_tokens": 20000}),
("measure_pressure", ("x" * 17430,), {"max_tokens": 8000}),
("measure_pressure", ("x" * 9000,), {"max_tokens": 12000}),
("measure_pressure", ("x" * 10830,), {"max_tokens": 12000}),
("measure_pressure", (big,), {"max_tokens": 12000}),
("detect_degradation", (["a", "b"],), {}),
("detect_degradation", (["aaa", "bbb", "ccc"],), {}),
("detect_degradation", (["x" * 200, "y" * 180, "", "", ""],), {}),
("detect_degradation", (["dup word word", "word dup word", "word word dup"],), {}),
("detect_degradation", (["a b c d", "a b c d", "a b c d"],), {}),
("detect_degradation", (["z" * 99, "z" * 100, "z" * 101],), {}),
("detect_degradation", (["éé üü éé", "üü éé üü", "éé üü"],), {}),
("detect_degradation", (["short", "tiny", "x" * 300],), {}),
("handle_hook", ("on_message", {"message": big}), {}),
("handle_hook", ("on_message", {"message": "x" * 403500}), {}),
("handle_hook", ("on_message", {"text": "hello"}), {}),
("handle_hook", ("on_message", {"other": 1}), {}),
("handle_hook", ("on_message", {}), {}),
("handle_hook", ("on_message", None), {}),
("handle_hook", ("other_event", {"message": big}), {}),
]
def kernel_case_specs():
"""The kernel rows (captured from the oracle floor; replayed by the
Rust battery, the loader FFI and regenerated independently here)."""
m_ok = _oracle_measure("hello world")
m_warn = _oracle_measure("x" * 49000, 20000)
m_crit = _oracle_measure("x" * 95000, 12000)
d_int0 = _pure_detect(["aaa", "bbb", "ccc"])
return {
"logMeasurement": [
("tokens_and_zero_pressure", m_ok, ""),
("warning_pressure", m_warn, ""),
("critical_pressure", m_crit, ""),
("int_zero_fields", d_int0, ""),
("session_id", m_ok, "session 9"),
("override_and_extra",
{"timestamp": "T-override", "session_id": "SID-OVR",
"user_key": "u", "tokens": 42, "pressure": 0.5,
"status": "warning"}, ""),
("unicode_escape_probe",
{"note": "éé", "pressure": 0.0, "status": "ok"}, ""),
],
"readLog": [
("read_all", "e1\ne2\n", 20),
("tail_two_of_four", "l1\nl2\nl3\nl4\n", 2),
("blank_and_invalid", '{"a": 1}\n\nnot json\n{"b": 2}\n', 20),
("limit_zero", "e1\ne2\n", 0),
("trailing_newline_no_phantom", "a\nb\nc\n", 2),
("float_pressure", '{"tokens": 3, "pressure": 0.5}\n', 20),
("int_line", "5\n", 20),
],
"logStats": [
("mixed_counts",
'{"status": "warning"}\n{"status": "warning"}\n'
'{"status": "warning"}\n{"status": "critical"}\n'
'{"status": "critical"}\n{"status": "ok"}\n\nbroken\n'),
("empty", ""),
("non_dict_line_crash", "5\n"),
],
"kernelHook": [
("critical_message", "on_message", {"message": "x" * 403500}),
("ok_text", "on_message", {"text": "hello"}),
("str_data_fallback", "on_message", {"other": 1}),
("both_empty_fallback", "on_message", {"message": "", "text": ""}),
("empty_data", "on_message", {}),
("wrong_event", "other_event", {"message": "hello"}),
("repr_bool_none", "on_message", {"a": True, "n": None}),
("repr_quote_probe", "on_message", {"k": "a'b"}),
("message_int_crash", "on_message", {"message": 5}),
("list_data_crash", "on_message", [1, 2]),
("bool_data_crash", "on_message", True),
("int_data_crash", "on_message", 5),
("null_data", "on_message", None),
],
"mainDispatch": [
("measure_plain", ["cp", "measure", "hello world"], ""),
("measure_json", ["cp", "measure", "hello world", "--json"], ""),
("measure_max_tokens", ["cp", "measure", "x" * 49000,
"--max-tokens", "20000"], ""),
("degrade_plain", ["cp", "degrade", "a b c|a b c|a b"], ""),
("degrade_json", ["cp", "degrade", "dup word|dup word|dup word",
"--json"], ""),
("log_plain", ["cp", "log", "--limit", "2"],
'{"tokens": 3, "pressure": 0.5, "status": "warning", "timestamp": "2026-10-14T01:02:03.456789+00:00"}\n{"tokens": 1, "pressure": 0.0, "status": "ok", "timestamp": "x"}\nbroken\n'),
("log_json", ["cp", "log", "--json"],
'{"pressure": 0.5, "status": "warning"}\n'),
("stats_plain", ["cp", "stats"],
'{"status": "warning"}\n{"status": "critical"}\n{"status": "ok"}\n'),
("stats_json", ["cp", "stats", "--json"],
'{"status": "warning"}\n'),
("hook_ok", ["cp", "hook", "--event", "on_message",
"--data", '{"message": "hello"}'], ""),
("hook_critical", ["cp", "hook", "--event", "on_message",
"--data", '{"message": "' + "x" * 95000 + '"}'], ""),
("hook_default_event", ["cp", "hook"], ""),
("unknown_cmd", ["cp", "bogus"], ""),
("no_cmd", ["cp"], ""),
("unknown_flag", ["cp", "measure", "x", "--bogus"], ""),
("bad_int", ["cp", "measure", "--max-tokens", "abc"], ""),
("extra_positional", ["cp", "measure", "a", "b"], ""),
],
}
def _oracle_measure(text, max_tokens=128000):
"""The oracle floor with the core pin active (poisoned-truth law:
the LIVE module computes every measurement)."""
return _oracle().measure_pressure(text, max_tokens=max_tokens)
def _pure_detect(responses):
return _oracle().detect_degradation(responses)
def regen_kernel_cases():
"""Regenerate every kernel row from the live oracle floor."""
specs = kernel_case_specs()
rows = {"logMeasurement": [], "readLog": [], "logStats": [],
"kernelHook": [], "mainDispatch": []}
for name, m, sid in specs["logMeasurement"]:
rows["logMeasurement"].append({
"fn": "log_measurement", "name": name,
"args": {"measurement": m, "session_id": sid, "now": FROZEN_NOW},
"expect": capture_log_measurement(copy.deepcopy(m), sid, FROZEN_NOW),
})
for name, content, limit in specs["readLog"]:
rows["readLog"].append({
"fn": "read_log", "name": name,
"args": {"content": content, "limit": limit},
"expect": capture_read_log(content, limit),
})
for name, content in specs["logStats"]:
rows["logStats"].append({
"fn": "log_stats", "name": name,
"args": {"content": content},
"expect": capture_log_stats(content),
})
for name, event, data in specs["kernelHook"]:
rows["kernelHook"].append({
"fn": "handle_hook", "name": name,
"args": {"event": event, "data": data, "now": FROZEN_NOW},
"expect": capture_hook(event, copy.deepcopy(data)),
})
for name, argv, seed in specs["mainDispatch"]:
rows["mainDispatch"].append({
"fn": "main_dispatch", "name": name,
"args": {"argv": argv, "content": seed, "now": FROZEN_NOW},
"expect": capture_cli(argv, seed),
})
return rows
def regen_envelope_cases():
"""Drive the pure-surface rows against the live oracle (in-rig)."""
cp = _oracle()
rows = []
for fn, args, kwargs in envelope_case_specs():
a = copy.deepcopy(args)
k = copy.deepcopy(kwargs)
result = getattr(cp, fn)(*a, **k)
rows.append({"fn": fn, "args": list(a), "kwargs": k,
"expected": copy.deepcopy(result)})
return rows
def write_envelope(path: Path) -> int:
cases = regen_envelope_cases()
kcases = regen_kernel_cases()
doc = {
"module": "context_pressure",
"frozen_now": FROZEN_NOW,
"cases": cases,
"kernel_cases": kcases,
}
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(doc, indent=2, ensure_ascii=False, sort_keys=False) + "\n")
total = len(cases) + sum(len(v) for v in kcases.values())
return total
def kernel_cases_total() -> int:
specs = kernel_case_specs()
return sum(len(v) for v in specs.values())
def main() -> int:
out = REPO / "src" / "rust" / "context_pressure" / "src" / "frozen_cp_truth.json"
total = write_envelope(out)
print(f"frozen {total} cases -> {out}")
return 0
if __name__ == "__main__":
if str(REPO / "scripts") not in sys.path:
sys.path.insert(0, str(REPO / "scripts"))
raise SystemExit(main())