Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions osiris_termux_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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:
Expand Down
37 changes: 35 additions & 2 deletions run_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from collections import Counter
import json
import os
import re
import secrets
import time

Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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-<hex>/output.jsonl artifacts into the same directory; those sort after every
# run-<timestamp> 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:
Expand Down
47 changes: 47 additions & 0 deletions tests/test_run_record_resolve.py
Original file line number Diff line number Diff line change
@@ -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())
Loading