From 72c27cb64516a5e16f57e7a6b06719c868710e7d Mon Sep 17 00:00:00 2001 From: Devin Phillip Davis <354534380+quantum-osiris@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:18:42 -0500 Subject: [PATCH] fix(runs): don't mistake execution-kernel artifacts for sprint runs The exec kernel writes run-/output.jsonl into ~/.osiris/runs, the same directory as sprint runs (run--). Those names sort after every timestamped id, so '[1] Last run' picked a kernel artifact and failed with 'unreadable run record (FileNotFoundError ... run.json)', and /runs reported them as orphaned sprint runs ('16 not in the ledger'). resolve() now only considers directories with a run.json; orphans() and the status count skip run-<32 hex> directories without one, so empty or partial sprint directories are still reported. The home card offers 'Last run' only when a sprint run exists. Co-Authored-By: Claude Opus 5.5 --- osiris_termux_console.py | 5 ++-- run_record.py | 37 +++++++++++++++++++++++-- tests/test_run_record_resolve.py | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 tests/test_run_record_resolve.py diff --git a/osiris_termux_console.py b/osiris_termux_console.py index 79b0d6a9..36170a96 100755 --- a/osiris_termux_console.py +++ b/osiris_termux_console.py @@ -1792,7 +1792,7 @@ def _home(command: str = "", quiet: bool = False): ui.dim(f"will: {nb['will']}"), ui.dim(f"won't: {nb['wont']}")], tone="rich")) options = [] - has_runs = os.path.isdir(run_record.RUNS_DIR) and bool(os.listdir(run_record.RUNS_DIR)) + has_runs = run_record.has_runs() if pending: options += [("This proposal's run: every candidate", "/run show"), ("Why did the last sandbox run pass or fail", "/why"), ("Full status", "/status")] @@ -3958,7 +3958,8 @@ def _status(): entries = None rows.append(("Genome ledger", f"unavailable: {type(e).__name__}: {e}")) if os.path.isdir(run_record.RUNS_DIR): - n_runs = len(os.listdir(run_record.RUNS_DIR)) + n_runs = sum(1 for n in os.listdir(run_record.RUNS_DIR) + if run_record._is_sprint_run_dir(run_record.RUNS_DIR, n)) if entries is None: rows.append(("Runs", f"{n_runs} on disk ยท ledger unreadable, not checked")) else: diff --git a/run_record.py b/run_record.py index 9ec9c109..15bd42f9 100644 --- a/run_record.py +++ b/run_record.py @@ -32,6 +32,7 @@ from collections import Counter import json import os +import re import secrets import time @@ -89,13 +90,33 @@ def load(run_id: str, runs_dir: str = None) -> dict: return json.load(f) +_SPRINT_RUN_ID = re.compile(r"^run-\d{8}T\d{6}Z-[0-9a-f]{6}$") + + +def is_sprint_run_id(name: str) -> bool: + """new_run_id() format. Execution-kernel artifacts in the same directory are run-<32 hex>.""" + return bool(_SPRINT_RUN_ID.match(name)) + + +_KERNEL_ARTIFACT_ID = re.compile(r"^run-[0-9a-f]{32}$") # exec_kernel: "run-" + uuid4().hex + + +def _is_sprint_run_dir(d: str, name: str) -> bool: + """Every directory except execution-kernel artifacts (run-<32 hex> without a run.json). + An empty or partial sprint directory still counts: it is evidence of a failed write.""" + path = os.path.join(d, name) + if not os.path.isdir(path): + return False + return not (_KERNEL_ARTIFACT_ID.match(name) and not os.path.isfile(os.path.join(path, "run.json"))) + + def orphans(ledger_entries, runs_dir: str = None) -> list: """Run ids on disk with no "sprint_run" ledger entry naming them -- a record or ledger append that failed part-way. Never valid input for /apply or /replay; kept, not deleted, as evidence of what was tried.""" d = runs_dir or RUNS_DIR try: - on_disk = sorted(n for n in os.listdir(d) if os.path.isdir(os.path.join(d, n))) + on_disk = sorted(n for n in os.listdir(d) if _is_sprint_run_dir(d, n)) except FileNotFoundError: return [] known = {(e.get("payload") or {}).get("run_id") for e in ledger_entries if e.get("kind") == "sprint_run"} @@ -135,13 +156,25 @@ def verify(run_id: str, record_sha256: str = None, runs_dir: str = None): return None +def has_runs(runs_dir: str = None) -> bool: + """True when at least one sprint run (a directory with run.json) is recorded.""" + try: + resolve(None, runs_dir) + return True + except LookupError: + return False + + def resolve(ref: str = None, runs_dir: str = None) -> str: """The run id for ref: the newest run when ref is empty, else the exact id or the one id containing ref (e.g. its last 6 characters). LookupError otherwise.""" d = runs_dir or RUNS_DIR + # Only directories holding a run.json are sprint runs. The execution kernel writes its own + # run-/output.jsonl artifacts into the same directory; those sort after every + # run- id and must not be taken for "the newest run". try: - ids = sorted(n for n in os.listdir(d) if os.path.isdir(os.path.join(d, n))) + ids = sorted(n for n in os.listdir(d) if os.path.isfile(os.path.join(d, n, "run.json"))) except FileNotFoundError: ids = [] if not ids: diff --git a/tests/test_run_record_resolve.py b/tests/test_run_record_resolve.py new file mode 100644 index 00000000..35f2dad0 --- /dev/null +++ b/tests/test_run_record_resolve.py @@ -0,0 +1,47 @@ +"""'Last run' must ignore execution-kernel artifact directories that share ~/.osiris/runs.""" + +import pytest + +import run_record + + +def _sprint_run(runs, run_id): + (runs / run_id).mkdir(parents=True) + (runs / run_id / "run.json").write_text("{}", encoding="utf-8") + + +def test_newest_run_skips_kernel_artifact_dirs(tmp_path): + runs = tmp_path / "runs" + _sprint_run(runs, "run-20261121T010000Z-aaaaaa") + _sprint_run(runs, "run-20261122T020000Z-bbbbbb") + kernel = runs / "run-d3b4bb35bcea4a51b14867e65b8fb83d" # sorts after every run-2026... id + kernel.mkdir() + (kernel / "output.jsonl").write_text("{}\n", encoding="utf-8") + assert run_record.resolve(None, runs_dir=str(runs)) == "run-20261122T020000Z-bbbbbb" + assert run_record.resolve("aaaaaa", runs_dir=str(runs)) == "run-20261121T010000Z-aaaaaa" + with pytest.raises(LookupError): + run_record.resolve("d3b4bb", runs_dir=str(runs)) + + +def test_only_kernel_dirs_means_no_runs_yet(tmp_path): + runs = tmp_path / "runs" + (runs / "run-0123abcd").mkdir(parents=True) + (runs / "run-0123abcd" / "output.jsonl").write_text("", encoding="utf-8") + with pytest.raises(LookupError, match="no runs recorded yet"): + run_record.resolve(None, runs_dir=str(runs)) + + +def test_has_runs_ignores_kernel_dirs(tmp_path): + runs = tmp_path / "runs" + (runs / "run-0123abcd").mkdir(parents=True) + assert run_record.has_runs(str(runs)) is False + _sprint_run(runs, "run-20261122T020000Z-cccccc") + assert run_record.has_runs(str(runs)) is True + + +def test_kernel_dirs_are_not_reported_as_orphaned_sprint_runs(tmp_path): + runs = tmp_path / "runs" + (runs / "run-d3b4bb35bcea4a51b14867e65b8fb83d").mkdir(parents=True) # kernel artifact + (runs / "run-20261122T020000Z-dddddd").mkdir() # sprint run, no run.json + assert run_record.orphans([], runs_dir=str(runs)) == ["run-20261122T020000Z-dddddd"] + assert run_record.is_sprint_run_id(run_record.new_run_id())