diff --git a/sage/gateway/hestia_dispatch.py b/sage/gateway/hestia_dispatch.py index 3929f3a0b..d013c1db5 100644 --- a/sage/gateway/hestia_dispatch.py +++ b/sage/gateway/hestia_dispatch.py @@ -47,7 +47,7 @@ from sage.gateway.being_gate_client import (BeingIntent, GatewayVerdict, ResultEnvelope, camera_command, copy_alternative, same_path_in_reach) from sage.gateway.hestia_witness import _ENDPOINT, _Mcp, _unwrap, make_hestia_witness_fn -from sage.gateway.reference_f1a import ReferenceF1aDispatcher +from sage.gateway.reference_f1a import ReferenceF1aDispatcher, retired_copy_note # The marker the seat's run-request reader keys on (sage/scripts/seat_run_requests.py). _RUN_MARKER = "[request_run]" @@ -3671,6 +3671,9 @@ def _allowed(full: str, is_script: bool) -> bool: "base name is only how it appears inside the sandbox") except Exception: # noqa: BLE001 — a hint must never turn a miss into a crash hint = "" + if not hint and i == 0: + gone = retired_copy_note(Path(full), Path(root)) + hint = f".{gone}" if gone else "" return ResultEnvelope(ok=False, error=f"run: no such file in your home: {n}{hint}") try: fd = os.open(full, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) diff --git a/sage/gateway/reference_f1a.py b/sage/gateway/reference_f1a.py index a708bb180..5e317b3a1 100644 --- a/sage/gateway/reference_f1a.py +++ b/sage/gateway/reference_f1a.py @@ -29,6 +29,7 @@ import re import textwrap import contextlib +import glob from datetime import datetime, timedelta, timezone from pathlib import Path from typing import Callable, Optional @@ -1170,6 +1171,74 @@ def _retired_header(stamp: datetime, reason: str, suffix: str = ".md") -> str: + _RETIRED_KEPT) +# BOTH HEADER FORMS (Nova HOLD on #727): since #726 a retired .py/.sh/... copy starts with +# `# RETIRED ...` (comments, so it still parses) and a .md or record with `> RETIRED ...`. A reader +# that knew only `>` found the copy of a retired script but lost its time and reason. +_RETIRED_LINE = re.compile(r"[#>] RETIRED (\S+ \S+?)Z? with retire_note\. No longer current: (.*)") + + +def retired_copy_note(p: Path, home: Path) -> str: + """One sentence for an absence: the missing file was retired, by name, when, and why. + + A MISS ON A NAME IT RETIRED ITSELF READ AS "NEVER EXISTED". Measured 2026-11-15 on + cbp-being, scratch/verify-xor-numpy-only.py: retired at 19:06Z with a correct reason; at + 19:12Z `run` said "no such file", so it wrote the script again under the same name, edited + it for 20 minutes and retired it a second time at 19:35Z citing the seat's review; at 19:57Z + retire_note said "nothing to retire"; at 20:03Z memory_edit said "does not exist", and it + stored a memory and a journal entry saying the script had never been created and had to be + written or found, and told the seat the file was missing. Both retired copies sat + beside the name the whole time. Every absence said only "absent"; none said "you retired + it". This names the newest copy and the reason it was given, so the next step is the being's + own decision recalled, not a file rewritten from memory. + + CASE 2, same day (scratch/xor-mlp-4units.py): retired whole at 21:53Z (it meant to drop lines + 27-119); at 22:00Z memory_edit on the name said "does not exist ... memory_write creates a + file", and the same beat wrote a different 28-line script there from memory + (make_classification, not XOR; a "4-unit MLP" built as LogisticRegression, the baseline's own + model), then asked the seat to run it to "confirm ~96.5%". It printed 0.8800 twice. The real + script sat in xor-mlp-4units.retired-2026-11-15-5.py, and this fix was open but held: its + header reader knew only the `>` form, and that copy's header is `#`. + + NO ACTOR IS CLAIMED (Nova HOLD on #727). A copy in this home says retire_note made it; it does + not say which caller did. The sentence names the verb and the copy, not "you". + + Existence only, inside the same directory the being named: reads one header line of a + regular file in its own home and nothing else. A symlinked copy is not evidence (it could + name any file), so it is skipped and never opened; an unreadable copy is named without a + time or reason. Empty string when there is no retired copy.""" + try: + copies = [c for c in p.parent.glob(f"{glob.escape(p.stem)}.retired-*{glob.escape(p.suffix)}") + if not c.is_symlink() and c.is_file()] + copies.sort(key=lambda c: c.lstat().st_mtime) + except OSError: + return "" + if not copies: + return "" + newest = copies[-1] + try: + rel = str(newest.relative_to(home)) + except ValueError: + rel = newest.name + when, why = "", "" + try: + fd = os.open(newest, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)) + with os.fdopen(fd, errors="replace") as f: + m = _RETIRED_LINE.match(f.readline().rstrip("\n")) + if m: + when, why = m.group(1), m.group(2).strip() + except OSError: + pass + if len(why) > 240: + why = why[:240].rstrip() + "..." + count = (f" ({len(copies)} retired copies of this name are here; this is the newest)" + if len(copies) > 1 else "") + said = f" The reason in its header: \"{why}\"." if why else "" + at = f" at {when} UTC" if when else "" + return (f" This name was retired{at} with retire_note, which moves the whole file: it is now " + f"'{rel}'{count}.{said} Nothing was lost. To look at it, memory_read '{rel}'. " + f"Writing this name again starts a new file; it does not bring the retired one back.") + + def _write_atomic(p: Path, text: str) -> None: """Write text to p whole or not at all: a tmp beside it, synced, then os.replace.""" tmp = p.with_name(f".{p.name}.tmp") @@ -1751,8 +1820,9 @@ def _do_memory_read(self, intent: BeingIntent) -> ResultEnvelope: return ResultEnvelope( ok=True, result=(f"[no such path: '{shown}' does not exist. This is not an empty file: there is " - f"nothing here to read, so it is no evidence about anything else. " - f"memory_write creates a file inside your home.]"), + f"nothing here to read, so it is no evidence about anything else." + + (retired_copy_note(p, self.memory_root) + or " memory_write creates a file inside your home.") + "]"), witness_id=self._witness(f"memory_read {p.name} (does not exist)")) if p.is_dir(): names = sorted(x.name + ("/" if x.is_dir() else "") for x in p.iterdir()) @@ -2106,7 +2176,8 @@ def _do_memory_edit(self, intent: BeingIntent) -> ResultEnvelope: if not p.exists(): return ResultEnvelope(ok=False, error=( f"nothing to edit: '{path}' does not exist. This is an absence, not a " - f"refusal." + (self._same_name_note(p, created=False) or + f"refusal." + ((retired_copy_note(p, self.memory_root) + + self._same_name_note(p, created=False)) or " memory_write creates a file; memory_edit changes one that is " "there."))) if p.is_dir(): @@ -2486,7 +2557,13 @@ def _do_retire_note(self, intent: BeingIntent) -> ResultEnvelope: return ResultEnvelope(ok=False, error=f"no such note: '{raw}' does not exist, so there is nothing to retire") return ResultEnvelope(ok=True, result=f"{p.name} is already retired; nothing changed") if not p.exists(): - return ResultEnvelope(ok=False, error=f"no such note: '{raw}' does not exist, so there is nothing to retire") + # A BARE "NOTHING TO RETIRE" READ AS "NEVER CREATED" (cbp-being 2026-11-15 19:57Z: a third + # retire of a name retired twice got this, and it concluded the file had never been + # made). Still not ok (Nova HOLD on #727: a matching name is not proof that THIS + # retire's intent was carried out), but the miss names the copy that is there. + return ResultEnvelope(ok=False, error=( + f"no such note: '{raw}' does not exist, so there is nothing to retire." + + retired_copy_note(p, self.memory_root))) return self._retire_locked(p, reason, record) def _top_level_retire_refusal(self, raw: str, p: Path) -> str: @@ -2673,8 +2750,15 @@ def _do_memory_write(self, intent: BeingIntent) -> ResultEnvelope: with open(p, "a") as f: # under the home lock (see __call__) f.write(content + ("\n" if not content.endswith("\n") else "")) if not existed: - result = (f"created {p.name} with {len(content)} chars" - + self._same_name_note(p, created=True)) + result = f"created {p.name} with {len(content)} chars" + gone = retired_copy_note(p, self.memory_root) + if gone: + # A WRITE UNDER A RETIRED NAME IS A NEW FILE (case 2 above: the 28-line rewrite's + # receipt said only "created", so nothing marked it as a different script). + result += (". This is a NEW file under a name that was retired, not the retired " + "one brought back." + gone.split(" Nothing was lost.", 1)[0]) + # ...and a same-name file one directory away is a separate fact (#739) + result += self._same_name_note(p, created=True) else: result = (f"appended {len(content)} chars to the END of {p.name}, below the {before} " f"lines already there. memory_write only adds; it never replaces or edits a line. " diff --git a/sage/gateway/tests/test_absence_names_retired_copy.py b/sage/gateway/tests/test_absence_names_retired_copy.py new file mode 100644 index 000000000..f67bfbfa4 --- /dev/null +++ b/sage/gateway/tests/test_absence_names_retired_copy.py @@ -0,0 +1,152 @@ +"""A miss on a name the being retired itself names the retired copy, when, and why. + +Measured 2026-11-15 on cbp-being (scratch/verify-xor-numpy-only.py): retired at 19:06Z with a +correct reason; at 19:12Z `run` said "no such file", so it wrote the script again under the same +name and retired it a second time at 19:35Z; at 19:57Z retire_note said "nothing to retire"; at +20:03Z memory_edit said "does not exist", and it stored a memory saying the script had never +been created and told the seat the file was missing. Both retired copies sat beside the name. +""" +import sys +import tempfile +import types +from pathlib import Path +from types import SimpleNamespace + +sys.path.insert(0, str(Path(__file__).resolve().parents[3])) + +from sage.gateway.being_gate_client import BeingIntent # noqa: E402 +from sage.gateway.reference_f1a import ReferenceF1aDispatcher # noqa: E402 + +ALLOW = SimpleNamespace(decision="allow", rule="", reason="ok", innate=False, stage="local-law") +WHY = "trains and tests on the same 4 points, so it only memorizes the XOR table" + + +def _home_with_a_retired_script(): + root = Path(tempfile.mkdtemp(prefix="retired-absence-")) + (root / "scratch").mkdir() + (root / "scratch" / "verify.py").write_text("print('xor')\n") + d = ReferenceF1aDispatcher(memory_root=root) + env = d(BeingIntent("retire_note", {"path": "scratch/verify.py", "reason": WHY}), ALLOW) + assert env.ok and "retired" in env.result, env + return d, root + + +def _names_the_copy(text): + assert "This name was retired at 20" in text, "time parsed from the header: " + text + assert "You retired" not in text, "no actor is claimed: " + text + assert "scratch/verify.retired-" in text, text + assert WHY in text, "the reason it gave travels with the miss: " + text + + +def test_memory_read_miss_names_the_retired_copy(): + d, _ = _home_with_a_retired_script() + env = d(BeingIntent("memory_read", {"path": "scratch/verify.py"}), ALLOW) + assert env.ok and "does not exist" in env.result + _names_the_copy(env.result) + assert "memory_write creates a file" not in env.result, "no nudge to rewrite it from memory" + + +def test_memory_edit_miss_names_the_retired_copy(): + d, _ = _home_with_a_retired_script() + env = d(BeingIntent("memory_edit", {"path": "scratch/verify.py", "old": "a", "new": "b"}), ALLOW) + assert env.ok is False and "nothing to edit" in env.error + _names_the_copy(env.error) + + +def test_retiring_an_already_retired_name_is_already_done_not_a_failure(): + d, root = _home_with_a_retired_script() + env = d(BeingIntent("retire_note", {"path": "scratch/verify.py", "reason": "again"}), ALLOW) + assert env.ok is False and "nothing to retire" in env.error, env + _names_the_copy(env.error) + assert len(list((root / "scratch").glob("verify.retired-*.py"))) == 1 + + +def test_the_newest_of_several_copies_is_named_with_the_count(): + d, root = _home_with_a_retired_script() + (root / "scratch" / "verify.py").write_text("print('again')\n") + env = d(BeingIntent("retire_note", {"path": "scratch/verify.py", "reason": "second reason"}), ALLOW) + assert env.ok and "-2.py" in env.result, env + miss = d(BeingIntent("memory_read", {"path": "scratch/verify.py"}), ALLOW) + assert "verify.retired-" in miss.result and "-2.py" in miss.result, miss.result + assert "2 retired copies of this name" in miss.result and "second reason" in miss.result + + +def test_a_name_never_retired_keeps_the_plain_absence(): + root = Path(tempfile.mkdtemp(prefix="retired-absence-")) + (root / "scratch").mkdir() + (root / "scratch" / "verify-other.retired-2026-11-15.py").write_text("> RETIRED x\n") + d = ReferenceF1aDispatcher(memory_root=root) + r = d(BeingIntent("memory_read", {"path": "scratch/verify.py"}), ALLOW) + assert "was retired" not in r.result and "memory_write creates a file" in r.result + e = d(BeingIntent("memory_edit", {"path": "scratch/verify.py", "old": "a", "new": "b"}), ALLOW) + assert "was retired" not in e.error and "memory_edit changes one" in e.error + t = d(BeingIntent("retire_note", {"path": "scratch/verify.py", "reason": "x"}), ALLOW) + assert t.ok is False and "nothing to retire" in t.error + + +def test_run_miss_names_the_retired_copy(tmp_path): + from sage.gateway.hestia_dispatch import HestiaF1aDispatcher as D + home = tmp_path.resolve() / "inst"; (home / "scratch").mkdir(parents=True) + (home / "scratch" / "verify.py").write_text("print('xor')\n") + ReferenceF1aDispatcher(memory_root=home)( + BeingIntent("retire_note", {"path": "scratch/verify.py", "reason": WHY}), ALLOW) + d = D.__new__(D); d.memory_root = str(home); d.member = "test-being" + d.worktree = str(home); d.workspace = None + d._verdict = types.SimpleNamespace(command=None) + d._call = lambda name, args: {"actionId": "w-run"} + m = d._do_run(BeingIntent("run", {"path": "scratch/verify.py"})) + assert m.ok is False and "no such file in your home: scratch/verify.py." in m.error, m.error + _names_the_copy(m.error) + + +def test_both_header_forms_give_time_and_reason(): + """A .py copy has the `#` header (#726), a .md copy the `>` header; both carry time + reason.""" + d, root = _home_with_a_retired_script() + py = next((root / "scratch").glob("verify.retired-*.py")).read_text() + assert py.startswith("# RETIRED "), py + (root / "scratch" / "plan.md").write_text("old plan\n") + assert d(BeingIntent("retire_note", {"path": "scratch/plan.md", "reason": "plan superseded"}), ALLOW).ok + md = next((root / "scratch").glob("plan.retired-*.md")).read_text() + assert md.startswith("> RETIRED "), md + for path, why in (("scratch/verify.py", WHY), ("scratch/plan.md", "plan superseded")): + r = d(BeingIntent("memory_read", {"path": path}), ALLOW).result + assert "This name was retired at 20" in r and " UTC with retire_note" in r, r + assert why in r, r + + +def test_a_write_under_a_retired_name_says_it_is_a_new_file(): + """Case 2 (xor-mlp-4units.py, 22:00Z): the rewrite's receipt said only "created".""" + d, _ = _home_with_a_retired_script() + w = d(BeingIntent("memory_write", {"path": "scratch/verify.py", "content": "print('other')"}), ALLOW) + assert w.ok and w.result.startswith("created verify.py"), w.result + assert "NEW file under a name that was retired" in w.result and "scratch/verify.retired-" in w.result + fresh = d(BeingIntent("memory_write", {"path": "scratch/brand-new.py", "content": "x = 1"}), ALLOW) + assert "retired" not in fresh.result, fresh.result + + +def test_a_symlinked_retired_copy_is_not_evidence(): + root = Path(tempfile.mkdtemp(prefix="retired-absence-")) + (root / "scratch").mkdir() + outside = Path(tempfile.mkdtemp(prefix="outside-")) / "x.py" + outside.write_text("# RETIRED 2026-11-15 00:00Z with retire_note. No longer current: planted\n") + (root / "scratch" / "verify.retired-2026-11-15.py").symlink_to(outside) + d = ReferenceF1aDispatcher(memory_root=root) + r = d(BeingIntent("memory_read", {"path": "scratch/verify.py"}), ALLOW).result + assert "was retired" not in r and "planted" not in r and "memory_write creates a file" in r, r + + +def test_a_retired_copy_and_a_same_name_file_elsewhere_are_both_named(): + # #739 (on main) names a same-name file one directory away; that is a separate fact from a + # retired copy beside the name, and an absence or a fresh create can have both. + d, root = _home_with_a_retired_script() + (root / "verify.py").write_text("print('top level')\n") + env = d(BeingIntent("memory_edit", {"path": "scratch/verify.py", "old": "a", "new": "b"}), ALLOW) + assert env.ok is False and "nothing to edit" in env.error + _names_the_copy(env.error) + assert "DOES exist at 'verify.py'" in env.error, env.error + assert "memory_write creates a file" not in env.error, env.error + env = d(BeingIntent("memory_write", {"path": "scratch/verify.py", "content": "print(1)\n"}), ALLOW) + assert env.ok, env.error + assert "This is a NEW file under a name that was retired" in env.result, env.result + assert "A file with this name already exists elsewhere: 'verify.py'" in env.result, env.result + assert ".." not in env.result.replace("...", ""), env.result