From 8b0be488f1ea9dd247c4ad8dfa8e5cb90baa780f Mon Sep 17 00:00:00 2001 From: Dennis Palatov Date: Sun, 15 Nov 2026 13:36:57 -0800 Subject: [PATCH 1/2] todo view + account: put hestia's live scope status beside items that name a request cbp-being 2026-11-15: scope-8b0be4d34333 and scope-6ec4bf5583c4 were refused at 20:06 UTC. Its todo.md still held 7 "pending, lapses in ~6 hours" lines copied from earlier beats' pending block. At 21:07 and 21:25 its WANT was to "renew the two scope requests about to lapse": it asked dp and searched its inbox. At 21:31 the account still called them "pending". The refusal was in the same beat's scope block, never beside the item that contradicted it. todo_membot.scope_status_note(text, rows) -> "hestia now: : by " for each scope id an open item names ("not on record for you" for an id hestia lacks). todo_view and the carried account write it under the item; the item stays verbatim. rows=None (scope status unavailable) writes nothing. Co-Authored-By: Claude Opus 5.5 --- sage/gateway/heartbeat.py | 26 ++++++-- .../tests/test_todo_scope_live_status.py | 66 +++++++++++++++++++ sage/gateway/todo_membot.py | 33 ++++++++++ 3 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 sage/gateway/tests/test_todo_scope_live_status.py diff --git a/sage/gateway/heartbeat.py b/sage/gateway/heartbeat.py index 27aaf9456d..e8ee129307 100644 --- a/sage/gateway/heartbeat.py +++ b/sage/gateway/heartbeat.py @@ -2747,8 +2747,12 @@ def _parse_todo(text: str, closures: Optional[list] = None) -> dict: return items -def todo_view(instance: Path, now=None) -> str: - """The todo section of the window: what is still open, from the whole log.""" +def todo_view(instance: Path, now=None, scope_rows: Optional[dict] = None) -> str: + """The todo section of the window: what is still open, from the whole log. + + `scope_rows` (request_id -> hestia_scope_status row) puts hestia's live status beside every + shown item that names a scope request (todo_membot.scope_status_note). None = not measured, + and no note is written.""" try: text = (instance / "todo.md").read_text(errors="replace") except OSError: @@ -2765,6 +2769,9 @@ def todo_view(instance: Path, now=None) -> str: lines.append(f"- [ ] {t}" + (f" (since {s:%Y-%m-%d %H:%M} UTC)" if s else "") # neutral: the shown item is the newest; the others are not claimed to share its state + (f" — {n} open items mention {k}; this is the newest of them" if n > 1 else "")) + live = _tm.scope_status_note(t, scope_rows) + if live: + lines.append(f" ({live})") if len(groups) > TODO_SHOWN: rest = sum(g[2] for g in groups[TODO_SHOWN:]) lines.append(f"- … and {rest} more opened in the last {TODO_WINDOW_H} h.") @@ -2900,7 +2907,8 @@ def own_state(instance: Path, member: str = "", entrusted: str = "", services: str = "", mark_conversations: bool = True, body_reading: Optional[dict] = None, settled_turns: Optional[int] = None, - proprioception: bool = True) -> str: + proprioception: bool = True, + scope_rows: Optional[dict] = None) -> str: from sage.gateway.being_join import carried_account, last_session_number from sage.gateway import being_params as _bp parts = [] @@ -3044,10 +3052,16 @@ def own_state(instance: Path, member: str = "", entrusted: str = "", acc = carried_account(instance, last_session_number(instance)) if acc: check = want_check(acc, files_facts) + # the same live fact the todo view writes: an account carried verbatim can say "pending" + # about a request hestia ruled on (cbp-being 2026-11-15 21:31, WANT naming two refused asks) + from sage.gateway import todo_membot as _tm_acc + live_acc = _tm_acc.scope_status_note(acc, scope_rows) + if live_acc: + check = (check + "\n" if check else "") + f"_Beside your account, {live_acc}._" parts.append("## Your own account\n" + acc + (("\n" + check) if check else "")) # tails are short now that recall searches the whole home (window pressure: median 6157 # of 8192 tokens per prompt, max 8013, measured 2026-09-07) - parts.append(todo_view(instance)) + parts.append(todo_view(instance, scope_rows=scope_rows)) if census_todo: parts.append(census_todo) journal = _read(instance / "journal.md", 1200) @@ -4467,9 +4481,12 @@ def main(argv=None) -> int: forum_replies = {"error": f"{type(_e).__name__}: {_e}"} # what reach the being holds and has already asked for, so it does not re-file scope = "(scope status unavailable)" + scope_rows = None # request_id -> row, for the todo view's live note; None = unmeasured if disp is not None and hasattr(disp, "_call"): try: st = disp._call("hestia_scope_status", {"plugin_id": args.member}) + scope_rows = {str(r.get("request_id")).lower(): r for r in (st.get("requests") or []) + if r.get("request_id")} grants = scope_grant_lines(st) reqs = [(r.get("request_id"), r.get("path"), r.get("decision") or r.get("status")) for r in (st.get("requests") or [])] @@ -4703,6 +4720,7 @@ def _build_state(per_conv, turn_chars): services=_services, mark_conversations=False, settled_turns=_settled_turns, proprioception=_proprioception, + scope_rows=scope_rows, body_reading=_body_cur) + _scope_tail) # A FRAME IS PROMPT TOO. It is not characters, so the ladder cannot see it unless its diff --git a/sage/gateway/tests/test_todo_scope_live_status.py b/sage/gateway/tests/test_todo_scope_live_status.py new file mode 100644 index 0000000000..66e97cdf51 --- /dev/null +++ b/sage/gateway/tests/test_todo_scope_live_status.py @@ -0,0 +1,66 @@ +"""The todo view puts hestia's live status beside an open item that names a scope request. + +cbp-being 2026-11-15: two asks refused at 20:06 UTC stayed "pending, lapses in ~6 hours" in its todo; at 21:25 +it asked dp to renew them. The refusal was in the same beat's scope block, never beside the stale item.""" +import sys +import tempfile +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[3])) + +from sage.gateway import todo_membot as tm +from sage.gateway.heartbeat import todo_view + +REFUSED = {"request_id": "scope-8b0be4d34333", "status": "refused", "decided_by": "peer:claude-code", + "path": "/home/dp/.local/bin"} +PENDING = {"request_id": "scope-6ec4bf5583c4", "status": "pending", "path": "/home/dp/.local/bin"} +ROWS = {"scope-8b0be4d34333": REFUSED, "scope-6ec4bf5583c4": PENDING} + + +def test_note_names_status_and_ruler(): + assert (tm.scope_status_note("scope-8b0be4d34333: pending, lapses in ~6 hours", ROWS) + == "hestia now: scope-8b0be4d34333: refused by peer:claude-code") + + +def test_pending_has_no_ruler(): + assert tm.scope_status_note("renew scope-6ec4bf5583c4", ROWS) == "hestia now: scope-6ec4bf5583c4: pending" + + +def test_two_ids_each_get_a_fact_once(): + note = tm.scope_status_note("find scope-8b0be4d34333 and scope-6ec4bf5583c4 (scope-8b0be4d34333 again)", ROWS) + assert note == ("hestia now: scope-8b0be4d34333: refused by peer:claude-code; " + "scope-6ec4bf5583c4: pending") + + +def test_unknown_id_is_said_not_on_record(): + assert tm.scope_status_note("scope-000000000000 waiting", ROWS) == "hestia now: scope-000000000000: not on record for you" + + +def test_unmeasured_and_no_id_write_nothing(): + assert tm.scope_status_note("scope-8b0be4d34333 pending", None) == "" + assert tm.scope_status_note("verify numpy in the sandbox", ROWS) == "" + + +def test_todo_view_shows_live_status_under_the_stale_item(): + with tempfile.TemporaryDirectory() as d: + inst = Path(d) + from datetime import datetime, timezone + stamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M") + (inst / "todo.md").write_text( + f"{stamp} UTC\n" + "- [ ] scope-8b0be4d34333: pending for /home/dp/.local/bin, lapses in ~6 hours\n" + "- [ ] verify the sandbox has numpy\n") + out = todo_view(inst, scope_rows=ROWS) + lines = out.splitlines() + i = next(k for k, l in enumerate(lines) if "lapses in ~6 hours" in l) + assert lines[i + 1] == " (hestia now: scope-8b0be4d34333: refused by peer:claude-code)" + assert "hestia now" not in lines[next(k for k, l in enumerate(lines) if "numpy" in l) + 1] + # unmeasured beat: the view is as before + assert "hestia now" not in todo_view(inst) + + +if __name__ == "__main__": + for name, fn in list(globals().items()): + if name.startswith("test_") and callable(fn): + fn() + print("ok", name) diff --git a/sage/gateway/todo_membot.py b/sage/gateway/todo_membot.py index acac2feac6..a12e3ca9b7 100644 --- a/sage/gateway/todo_membot.py +++ b/sage/gateway/todo_membot.py @@ -64,6 +64,39 @@ def close_all(items: Dict[str, Tuple[str, object]], body: str) -> Optional[Tuple _DEVICE = re.compile(r"/dev/[A-Za-z]+\d*", re.I) +def scope_status_note(text: str, rows: Optional[Dict[str, dict]]) -> str: + """What hestia holds NOW for each scope request an open item names, or "" when it names none or the beat + has no scope status. + + WHY (cbp-being, 2026-11-15). Two asks were refused at 20:06 UTC. The being's todo still held seven + "scope-…: pending, lapses in ~6 hours" lines, copied from earlier beats' pending block. Its 20:45 journal said + "all refused, none pending", but the todo view showed only its own frozen word. At 21:07 and 21:25 its + WANT was "renew the two scope requests that are about to lapse". It asked dp to renew them and searched its + inbox for the entries. The live status was in the same beat, in the scope block, but it was never next to + the item that contradicted it. The item is the being's and stays verbatim; the measurement now sits beside + it (the same move as want_check). + + `rows` maps request_id -> hestia_scope_status `requests[]` row. tool_scope_status lists every request of the + member's, decided and expired included, so an id it lacks is not one waiting on anyone (_named_scope_fact).""" + if rows is None: + return "" + ids = [] + for m in _SCOPE.finditer(text or ""): + rid = m.group(0).lower() + if rid not in ids: + ids.append(rid) + facts = [] + for rid in ids: + row = rows.get(rid) + if not row: + facts.append(f"{rid}: not on record for you") + continue + status = row.get("status") or row.get("decision") or "unknown" + by = f" by {row['decided_by']}" if row.get("decided_by") and status != "pending" else "" + facts.append(f"{rid}: {status}{by}") + return ("hestia now: " + "; ".join(facts)) if facts else "" + + def group_key(text: str) -> Optional[str]: """The one id an item is grouped under, or None (shown alone). From 2ab51bb1aee53e4d4f8b04171f880d4ddd5fa0f8 Mon Sep 17 00:00:00 2001 From: Dennis Palatov Date: Thu, 19 Nov 2026 22:16:28 -0800 Subject: [PATCH 2/2] #730 review: scope note is a dated observation; a failed read is not an empty list Nova's HOLD on #730, point by point: - "not on record for you" only when the response declares requests[] complete (todo_membot.SCOPE_COMPLETE_FIELD; no daemon serves it as of hestia f0660a7). Otherwise an absent id reads "not in this scope-status response". - The beat's read becomes a ScopeSnapshot. An error envelope, a missing request list or a raised call is a snapshot with its error: the note says the status could not be read and is unknown, and the scope block no longer prints "requests: none" for an error envelope. - Every note names when the read was made ("as read at UTC") and the ruling's own decided_at when hestia gives one; no "now". A request read as pending and ruled a minute later reads as pending at that read. - A row with no status says so instead of printing "unknown". - The note states status, ruler and time only: no renewal, retry or next-step wording. todo.md and account.json are never written; items stay verbatim. - Code comments and tests paraphrase the incident and keep the grounded scope ids and times; test fixtures are synthetic text, not the being's words. Tests: test_todo_scope_live_status.py, 18 tests (unknown status, complete-empty, undeclared-empty, truncated, error/shape/raised failures, raced ruling, no-renewal wording, todo/account untouched, beat wiring anchor). Co-Authored-By: Claude Opus 5.5 --- sage/gateway/heartbeat.py | 40 ++-- .../tests/test_todo_scope_live_status.py | 219 +++++++++++++++--- sage/gateway/todo_membot.py | 113 +++++++-- 3 files changed, 309 insertions(+), 63 deletions(-) diff --git a/sage/gateway/heartbeat.py b/sage/gateway/heartbeat.py index 6acf7ba0d2..38602f3b2b 100644 --- a/sage/gateway/heartbeat.py +++ b/sage/gateway/heartbeat.py @@ -3408,12 +3408,13 @@ def _parse_todo(text: str, closures: Optional[list] = None) -> dict: return items -def todo_view(instance: Path, now=None, scope_rows: Optional[dict] = None) -> str: +def todo_view(instance: Path, now=None, scope_snap=None) -> str: """The todo section of the window: what is still open, from the whole log. - `scope_rows` (request_id -> hestia_scope_status row) puts hestia's live status beside every - shown item that names a scope request (todo_membot.scope_status_note). None = not measured, - and no note is written.""" + `scope_snap` (todo_membot.ScopeSnapshot: this beat's one hestia_scope_status read) puts what + that read held beside every shown item that names a scope request (todo_membot.scope_status_note), + dated as an observation. None = no read this beat, and no note is written. The items themselves + are shown verbatim and todo.md is never written here.""" try: text = (instance / "todo.md").read_text(errors="replace") except OSError: @@ -3430,7 +3431,7 @@ def todo_view(instance: Path, now=None, scope_rows: Optional[dict] = None) -> st lines.append(f"- [ ] {t}" + (f" (since {s:%Y-%m-%d %H:%M} UTC)" if s else "") # neutral: the shown item is the newest; the others are not claimed to share its state + (f" — {n} open items mention {k}; this is the newest of them" if n > 1 else "")) - live = _tm.scope_status_note(t, scope_rows) + live = _tm.scope_status_note(t, scope_snap) if live: lines.append(f" ({live})") if len(groups) > TODO_SHOWN: @@ -3569,7 +3570,7 @@ def own_state(instance: Path, member: str = "", entrusted: str = "", body_reading: Optional[dict] = None, settled_turns: Optional[int] = None, proprioception: bool = True, - scope_rows: Optional[dict] = None) -> str: + scope_snap=None) -> str: from sage.gateway.being_join import carried_account, last_session_number from sage.gateway import being_params as _bp parts = [] @@ -3713,16 +3714,17 @@ def own_state(instance: Path, member: str = "", entrusted: str = "", acc = carried_account(instance, last_session_number(instance)) if acc: check = want_check(acc, files_facts) - # the same live fact the todo view writes: an account carried verbatim can say "pending" - # about a request hestia ruled on (cbp-being 2026-11-15 21:31, WANT naming two refused asks) + # the same dated observation the todo view writes: an account carried verbatim can call a + # request pending after hestia ruled on it (cbp-being 2026-11-15, scope-8b0be4d34333 and + # scope-6ec4bf5583c4, refused 20:06 UTC). The account itself is shown unchanged. from sage.gateway import todo_membot as _tm_acc - live_acc = _tm_acc.scope_status_note(acc, scope_rows) + live_acc = _tm_acc.scope_status_note(acc, scope_snap) if live_acc: check = (check + "\n" if check else "") + f"_Beside your account, {live_acc}._" parts.append("## Your own account\n" + acc + (("\n" + check) if check else "")) # tails are short now that recall searches the whole home (window pressure: median 6157 # of 8192 tokens per prompt, max 8013, measured 2026-09-07) - parts.append(todo_view(instance, scope_rows=scope_rows)) + parts.append(todo_view(instance, scope_snap=scope_snap)) if census_todo: parts.append(census_todo) journal = _read(instance / "journal.md", 1200) @@ -5266,12 +5268,17 @@ def main(argv=None) -> int: forum_replies = {"error": f"{type(_e).__name__}: {_e}"} # what reach the being holds and has already asked for, so it does not re-file scope = "(scope status unavailable)" - scope_rows = None # request_id -> row, for the todo view's live note; None = unmeasured + # This beat's one read, for the todo view's dated note (todo_membot.ScopeSnapshot). None = no read + # was made; a failed read is a snapshot WITH its error, never an empty list (Nova on #730). + scope_snap = None + from sage.gateway import todo_membot as _tm_scope if disp is not None and hasattr(disp, "_call"): try: st = disp._call("hestia_scope_status", {"plugin_id": args.member}) - scope_rows = {str(r.get("request_id")).lower(): r for r in (st.get("requests") or []) - if r.get("request_id")} + scope_snap = _tm_scope.scope_snapshot(st) + if scope_snap.error is not None: + # an error envelope read as an empty list would print "requests: none", a claim + raise _tm_scope.ScopeUnread(scope_snap.error) grants = scope_grant_lines(st) reqs = [(r.get("request_id"), r.get("path"), r.get("decision") or r.get("status")) for r in (st.get("requests") or [])] @@ -5314,7 +5321,10 @@ def main(argv=None) -> int: "granted it, or to ask whether one path is inside it: memory_read " "{\"path\": \"hestia://reach\"} or {\"path\": \"hestia://reach/\"})") except Exception as e: - scope = f"(scope status unavailable: {type(e).__name__})" + scope = (f"(scope status unavailable: {e})" if isinstance(e, _tm_scope.ScopeUnread) + else f"(scope status unavailable: {type(e).__name__})") + if scope_snap is None: + scope_snap = _tm_scope.scope_snapshot(e) # the being's own appeals and any ruling new since the last beat (see appeals_block) appeals_text, appeals_record = appeals_block(disp, last) # what it starts oriented by: its own recent writing (searched, not just the tail) and @@ -5508,7 +5518,7 @@ def _build_state(per_conv, turn_chars): services=_services, mark_conversations=False, settled_turns=_settled_turns, proprioception=_proprioception, - scope_rows=scope_rows, + scope_snap=scope_snap, body_reading=_body_cur) + _scope_tail) # A FRAME IS PROMPT TOO. It is not characters, so the ladder cannot see it unless its diff --git a/sage/gateway/tests/test_todo_scope_live_status.py b/sage/gateway/tests/test_todo_scope_live_status.py index 66e97cdf51..d76b2a0f6c 100644 --- a/sage/gateway/tests/test_todo_scope_live_status.py +++ b/sage/gateway/tests/test_todo_scope_live_status.py @@ -1,9 +1,17 @@ -"""The todo view puts hestia's live status beside an open item that names a scope request. +"""The todo view and the carried account put one dated hestia_scope_status read beside an open item that names +a scope request. -cbp-being 2026-11-15: two asks refused at 20:06 UTC stayed "pending, lapses in ~6 hours" in its todo; at 21:25 -it asked dp to renew them. The refusal was in the same beat's scope block, never beside the stale item.""" +Grounding (cbp-being, 2026-11-15): scope-8b0be4d34333 and scope-6ec4bf5583c4 were refused at 20:06 UTC while +lines in its todo still called them pending; it went on treating them as pending. The fixtures below are +synthetic text around those two grounded ids; none of them is the being's own wording. + +Nova's #730 review shapes what is tested: a failed read is not an empty list; an absent id is "not on record" +only when the response declares itself complete; a note is an observation at a time, so a ruling that lands +after the read does not make the earlier note look permanent; nothing says a refusal can be renewed; the +being's todo is never rewritten or cleared.""" import sys import tempfile +from datetime import datetime, timezone from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[3])) @@ -11,52 +19,205 @@ from sage.gateway import todo_membot as tm from sage.gateway.heartbeat import todo_view +T_READ = datetime(2026, 11, 15, 21, 7, tzinfo=timezone.utc) +AT = "2026-11-15 21:07 UTC" +DECIDED = datetime(2026, 11, 15, 20, 6, tzinfo=timezone.utc).timestamp() + REFUSED = {"request_id": "scope-8b0be4d34333", "status": "refused", "decided_by": "peer:claude-code", - "path": "/home/dp/.local/bin"} + "decided_at": DECIDED, "path": "/home/dp/.local/bin"} PENDING = {"request_id": "scope-6ec4bf5583c4", "status": "pending", "path": "/home/dp/.local/bin"} -ROWS = {"scope-8b0be4d34333": REFUSED, "scope-6ec4bf5583c4": PENDING} +RESP = {"requests": [REFUSED, PENDING]} + + +def snap(resp=RESP, at=T_READ): + return tm.scope_snapshot(resp, observed_at=at) + +# -- what one read says ---------------------------------------------------------------------------- -def test_note_names_status_and_ruler(): - assert (tm.scope_status_note("scope-8b0be4d34333: pending, lapses in ~6 hours", ROWS) - == "hestia now: scope-8b0be4d34333: refused by peer:claude-code") +def test_note_names_status_ruler_ruling_time_and_read_time(): + assert (tm.scope_status_note("scope-8b0be4d34333 still pending (synthetic)", snap()) + == f"hestia scope status as read at {AT}: " + "scope-8b0be4d34333: refused by peer:claude-code at 2026-11-15 20:06 UTC") def test_pending_has_no_ruler(): - assert tm.scope_status_note("renew scope-6ec4bf5583c4", ROWS) == "hestia now: scope-6ec4bf5583c4: pending" + assert (tm.scope_status_note("follow up on scope-6ec4bf5583c4", snap()) + == f"hestia scope status as read at {AT}: scope-6ec4bf5583c4: pending") def test_two_ids_each_get_a_fact_once(): - note = tm.scope_status_note("find scope-8b0be4d34333 and scope-6ec4bf5583c4 (scope-8b0be4d34333 again)", ROWS) - assert note == ("hestia now: scope-8b0be4d34333: refused by peer:claude-code; " + note = tm.scope_status_note("scope-8b0be4d34333 and SCOPE-6EC4BF5583C4 (scope-8b0be4d34333 again)", snap()) + assert note == (f"hestia scope status as read at {AT}: " + "scope-8b0be4d34333: refused by peer:claude-code at 2026-11-15 20:06 UTC; " "scope-6ec4bf5583c4: pending") -def test_unknown_id_is_said_not_on_record(): - assert tm.scope_status_note("scope-000000000000 waiting", ROWS) == "hestia now: scope-000000000000: not on record for you" +def test_no_read_and_no_id_write_nothing(): + assert tm.scope_status_note("scope-8b0be4d34333 pending", None) == "" + assert tm.scope_status_note("verify numpy in the sandbox", snap()) == "" -def test_unmeasured_and_no_id_write_nothing(): - assert tm.scope_status_note("scope-8b0be4d34333 pending", None) == "" - assert tm.scope_status_note("verify numpy in the sandbox", ROWS) == "" +# -- unknown status -------------------------------------------------------------------------------- + +def test_row_without_a_status_is_said_so_not_guessed(): + s = snap({"requests": [{"request_id": "scope-8b0be4d34333", "path": "/etc"}]}) + note = tm.scope_status_note("scope-8b0be4d34333", s) + assert note == f"hestia scope status as read at {AT}: scope-8b0be4d34333: listed, with no status stated" + assert "unknown" not in note and "pending" not in note + + +def test_unfamiliar_status_is_shown_verbatim(): + s = snap({"requests": [{"request_id": "scope-8b0be4d34333", "status": "superseded"}]}) + assert tm.scope_status_note("scope-8b0be4d34333", s).endswith("scope-8b0be4d34333: superseded") + + +# -- complete-empty vs incomplete vs failed -------------------------------------------------------- + +def test_absent_id_in_an_undeclared_response_is_only_absent_from_it(): + # today's daemon (hestia f0660a7) declares no completeness: an absent id is not called "not on record" + note = tm.scope_status_note("scope-000000000000 waiting", snap()) + assert note == f"hestia scope status as read at {AT}: scope-000000000000: not in this scope-status response" + assert "not on record" not in note + + +def test_absent_id_in_a_declared_complete_response_is_not_on_record(): + s = snap({"requests": [REFUSED], tm.SCOPE_COMPLETE_FIELD: True}) + assert s.complete and s.error is None + assert (tm.scope_status_note("scope-000000000000 waiting", s) + == f"hestia scope status as read at {AT}: scope-000000000000: not on record for you") + + +def test_complete_but_empty_list_is_a_real_negative(): + s = snap({"requests": [], tm.SCOPE_COMPLETE_FIELD: True}) + assert s.rows == {} and s.complete and s.error is None + assert tm.scope_status_note("scope-8b0be4d34333", s).endswith("scope-8b0be4d34333: not on record for you") + +def test_empty_list_without_a_completeness_declaration_is_not_a_negative(): + s = snap({"requests": []}) + assert s.rows == {} and not s.complete + note = tm.scope_status_note("scope-8b0be4d34333", s) + assert note.endswith("scope-8b0be4d34333: not in this scope-status response") -def test_todo_view_shows_live_status_under_the_stale_item(): + +def test_truncated_response_is_never_complete(): + s = snap({"requests": [REFUSED], tm.SCOPE_COMPLETE_FIELD: True, "truncated": True}) + assert not s.complete + assert "not on record" not in tm.scope_status_note("scope-000000000000", s) + + +def test_error_envelope_is_not_an_empty_list(): + s = snap({"_hestia_error": {"code": "hestia.unavailable", "message": "x"}}) + assert s.rows is None and s.error == "hestia.unavailable" + note = tm.scope_status_note("scope-8b0be4d34333 pending", s) + assert note == ("hestia scope status could not be read at 2026-11-15 21:07 UTC (hestia.unavailable); " + "status of scope-8b0be4d34333 unknown") + assert "not on record" not in note and "not in this" not in note + + +def test_wrong_shape_and_raised_call_are_failures_too(): + for resp, err in (({"plugin_id": "x"}, "no request list"), ({"requests": None}, "no request list"), + (None, "no response"), (TimeoutError("t"), "TimeoutError")): + s = snap(resp) + assert s.rows is None and s.error == err, resp + assert "unknown" in tm.scope_status_note("scope-8b0be4d34333", s) + + +# -- an observation at a time, not an eternal fact ------------------------------------------------- + +def test_raced_ruling_reads_as_an_earlier_observation(): + # the read at 20:05 saw it pending; hestia ruled at 20:06. The 20:05 note says when it looked and does + # not claim the request is pending now; the next read says what changed and when. + early = snap({"requests": [dict(REFUSED, status="pending", decided_by=None, decided_at=None)]}, + at=datetime(2026, 11, 15, 20, 5, tzinfo=timezone.utc)) + n1 = tm.scope_status_note("scope-8b0be4d34333", early) + assert n1 == "hestia scope status as read at 2026-11-15 20:05 UTC: scope-8b0be4d34333: pending" + for word in ("now", "still", "currently", "will"): + assert f" {word} " not in f" {n1} " + n2 = tm.scope_status_note("scope-8b0be4d34333", snap(at=datetime(2026, 11, 15, 20, 7, tzinfo=timezone.utc))) + assert n2 == ("hestia scope status as read at 2026-11-15 20:07 UTC: " + "scope-8b0be4d34333: refused by peer:claude-code at 2026-11-15 20:06 UTC") + + +# -- no renewal, no advice ------------------------------------------------------------------------- + +def test_settled_statuses_never_suggest_renewal_or_a_next_step(): + for status in ("refused", "expired", "revoked", "granted"): + s = snap({"requests": [dict(REFUSED, status=status)]}) + note = tm.scope_status_note("renew scope-8b0be4d34333 before it lapses", s).lower() + for word in ("renew", "retry", "re-file", "refile", "again", "lapse", "ask ", "should", "cleared", "closed"): + assert word not in note, (status, word, note) + + +# -- the being's todo is shown verbatim and never written ------------------------------------------ + +def _todo(inst: Path) -> str: + stamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M") + text = (f"{stamp} UTC\n" + "- [ ] check on scope-8b0be4d34333 for user-bin (synthetic)\n" + "- [ ] verify the sandbox has numpy\n") + (inst / "todo.md").write_text(text) + return text + + +def test_todo_view_writes_the_note_under_the_item_and_leaves_the_item_alone(): with tempfile.TemporaryDirectory() as d: inst = Path(d) - from datetime import datetime, timezone - stamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M") - (inst / "todo.md").write_text( - f"{stamp} UTC\n" - "- [ ] scope-8b0be4d34333: pending for /home/dp/.local/bin, lapses in ~6 hours\n" - "- [ ] verify the sandbox has numpy\n") - out = todo_view(inst, scope_rows=ROWS) + before = _todo(inst) + out = todo_view(inst, scope_snap=snap()) lines = out.splitlines() - i = next(k for k, l in enumerate(lines) if "lapses in ~6 hours" in l) - assert lines[i + 1] == " (hestia now: scope-8b0be4d34333: refused by peer:claude-code)" - assert "hestia now" not in lines[next(k for k, l in enumerate(lines) if "numpy" in l) + 1] - # unmeasured beat: the view is as before - assert "hestia now" not in todo_view(inst) + i = next(k for k, l in enumerate(lines) if "check on scope-8b0be4d34333" in l) + assert lines[i].startswith("- [ ] check on scope-8b0be4d34333 for user-bin (synthetic)") + assert lines[i + 1] == (f" (hestia scope status as read at {AT}: " + "scope-8b0be4d34333: refused by peer:claude-code at 2026-11-15 20:06 UTC)") + assert "hestia scope status" not in lines[next(k for k, l in enumerate(lines) if "numpy" in l) + 1] + assert "still open (2)" in out # the refused item is still the being's open item + assert (inst / "todo.md").read_text() == before + # no read this beat: the view is as before + assert "hestia scope status" not in todo_view(inst) + # a failed read: said, and the item is still shown and still open + failed = todo_view(inst, scope_snap=snap({"_hestia_error": {"code": "hestia.down"}})) + assert "could not be read" in failed and "check on scope-8b0be4d34333" in failed + assert "not on record" not in failed + assert (inst / "todo.md").read_text() == before + + +def test_carried_account_is_verbatim_with_the_dated_note_beside_it(tmp_path): + from sage.gateway import conversations as conv + from sage.gateway import heartbeat as hb + from sage.gateway.being_join import save_account + inst = tmp_path / "inst" + inst.mkdir() + conv.create(inst, "cbp-claude", title="seat", participants=["cbp-claude", "cbp-being"], + writable_by=["cbp-claude", "cbp-being"]) + want = "follow up on scope-8b0be4d34333 for user-bin (synthetic)" + save_account(inst, {"place": "a synthetic place line long enough to be kept as an account", + "can": "read and write my home", "want": want}, "b1") + acc_before = (inst / "account.json").read_text() + st = hb.own_state(inst, member="cbp-being", mark_conversations=False, services="", body_reading={}, + scope_snap=snap()) + sec = st[st.index("## Your own account"):] + sec = sec[:sec.index("\n## ")] + assert f"WANT: {want}" in sec + assert (f"_Beside your account, hestia scope status as read at {AT}: " + "scope-8b0be4d34333: refused by peer:claude-code at 2026-11-15 20:06 UTC._") in sec + assert (inst / "account.json").read_text() == acc_before + # no read: no note + st0 = hb.own_state(inst, member="cbp-being", mark_conversations=False, services="", body_reading={}) + assert "hestia scope status" not in st0 + + +def test_the_beat_wires_its_one_read_as_a_snapshot_not_a_dict(): + """Wiring anchor (main() is not driven end to end by any test): the beat's scope read becomes a + ScopeSnapshot, a failed read becomes a snapshot with its error, and the scope block does not print + 'requests: none' for an error envelope.""" + src = (Path(__file__).resolve().parents[1] / "heartbeat.py").read_text() + assert "scope_snap = _tm_scope.scope_snapshot(st)" in src + assert "raise _tm_scope.ScopeUnread(scope_snap.error)" in src + assert "scope_snap = _tm_scope.scope_snapshot(e)" in src + assert "scope_snap=scope_snap," in src + assert "scope_rows" not in src if __name__ == "__main__": diff --git a/sage/gateway/todo_membot.py b/sage/gateway/todo_membot.py index a12e3ca9b7..5a927bd891 100644 --- a/sage/gateway/todo_membot.py +++ b/sage/gateway/todo_membot.py @@ -22,6 +22,8 @@ from __future__ import annotations import re +from dataclasses import dataclass +from datetime import datetime, timezone from typing import Dict, List, Optional, Tuple ALL = re.compile(r"^(?:all|every)\s*:\s*(.+)$", re.I) @@ -64,37 +66,110 @@ def close_all(items: Dict[str, Tuple[str, object]], body: str) -> Optional[Tuple _DEVICE = re.compile(r"/dev/[A-Za-z]+\d*", re.I) -def scope_status_note(text: str, rows: Optional[Dict[str, dict]]) -> str: - """What hestia holds NOW for each scope request an open item names, or "" when it names none or the beat - has no scope status. +#: The response field a daemon would set to say `requests[]` is EVERY request it holds for the member, +#: unfiltered and untruncated. Only then may an id it lacks be called "not on record for you". No daemon +#: serves it as of hestia f0660a7 (2026-11-19): tool_scope_status returns the requests it holds in memory, but +#: whether that is all of them (pruning, restarts, an older daemon) is not a stated contract. Until one says +#: so, an absent id is reported as absent from this response, which is all that was observed. +SCOPE_COMPLETE_FIELD = "requests_complete" + + +class ScopeUnread(Exception): + """A hestia_scope_status read that returned no usable request list (error envelope, wrong shape).""" + + +@dataclass(frozen=True) +class ScopeSnapshot: + """One hestia_scope_status read, as observed at `observed_at`. + + Three states, never conflated (Nova on #730: a failed call must not become `{}`): + - `error` set: the read failed. Nothing is known about any id; `rows` is None. + - `error` None, `complete` False: these rows were in the response; an absent id is only absent from it. + - `error` None, `complete` True: the daemon declared the list complete; an absent id is not on record.""" + rows: Optional[Dict[str, dict]] + observed_at: datetime + complete: bool = False + error: Optional[str] = None + + +def scope_snapshot(st, observed_at: Optional[datetime] = None) -> ScopeSnapshot: + """Wrap one hestia_scope_status response (or the exception its call raised) as a ScopeSnapshot.""" + at = observed_at or datetime.now(timezone.utc) + if isinstance(st, BaseException): + return ScopeSnapshot(None, at, error=type(st).__name__) + if not isinstance(st, dict): + return ScopeSnapshot(None, at, error="no response") + err = st.get("_hestia_error") + if err is not None: + code = (err.get("code") or "hestia.error") if isinstance(err, dict) else str(err) + return ScopeSnapshot(None, at, error=str(code)) + reqs = st.get("requests") + if not isinstance(reqs, list): + return ScopeSnapshot(None, at, error="no request list") + rows = {str(r.get("request_id")).lower(): r for r in reqs if isinstance(r, dict) and r.get("request_id")} + complete = st.get(SCOPE_COMPLETE_FIELD) is True and not st.get("truncated") + return ScopeSnapshot(rows, at, complete=complete) + + +def _utc(x) -> str: + """An epoch-seconds stamp from hestia as `YYYY-MM-DD HH:MM UTC`, or "" when it is not one.""" + try: + return f"{datetime.fromtimestamp(float(x), timezone.utc):%Y-%m-%d %H:%M} UTC" + except (TypeError, ValueError, OverflowError, OSError): + return "" + + +def scope_status_note(text: str, snap: Optional[ScopeSnapshot]) -> str: + """What one hestia_scope_status read held for each scope request an open item names, or "" when the item + names none or the beat made no read (`snap` None). - WHY (cbp-being, 2026-11-15). Two asks were refused at 20:06 UTC. The being's todo still held seven - "scope-…: pending, lapses in ~6 hours" lines, copied from earlier beats' pending block. Its 20:45 journal said - "all refused, none pending", but the todo view showed only its own frozen word. At 21:07 and 21:25 its - WANT was "renew the two scope requests that are about to lapse". It asked dp to renew them and searched its - inbox for the entries. The live status was in the same beat, in the scope block, but it was never next to - the item that contradicted it. The item is the being's and stays verbatim; the measurement now sits beside - it (the same move as want_check). + WHY (cbp-being, 2026-11-15; scope-8b0be4d34333 and scope-6ec4bf5583c4). Both asks were refused at 20:06 UTC. + The being's todo kept several lines, copied from earlier beats' pending block, that called them pending with + hours left. Over the next hour and a half it treated them as pending: it asked dp about them and searched + its inbox for them, and its carried account still called them pending. The refusal was in the same beat's + scope block, never next to the item that contradicted it. The item is the being's and stays verbatim; the + measurement sits beside it (the same move as want_check). Nothing here closes, edits or rewrites an item. - `rows` maps request_id -> hestia_scope_status `requests[]` row. tool_scope_status lists every request of the - member's, decided and expired included, so an id it lacks is not one waiting on anyone (_named_scope_fact).""" - if rows is None: + WORDING IS AN OBSERVATION AT A TIME, NOT A STANDING FACT (Nova on #730). The note names when the read was + made, and a ruling's own time when hestia gives one, so a request read as pending that is ruled a minute + later reads as "pending at that read", not as pending forever. It states status and ruler and nothing + about what to do next: no advice, and nothing that says a refused or expired request can be renewed. + + AN ABSENT ID IS NOT CALLED "NOT ON RECORD" UNLESS THE LIST IS DECLARED COMPLETE (SCOPE_COMPLETE_FIELD). + A failed read says it failed, and claims nothing about any id.""" + if snap is None: return "" ids = [] for m in _SCOPE.finditer(text or ""): rid = m.group(0).lower() if rid not in ids: ids.append(rid) + if not ids: + return "" + at = f"{snap.observed_at:%Y-%m-%d %H:%M} UTC" + if snap.error is not None: + return (f"hestia scope status could not be read at {at} ({snap.error}); " + f"status of {', '.join(ids)} unknown") facts = [] for rid in ids: - row = rows.get(rid) + row = (snap.rows or {}).get(rid) if not row: - facts.append(f"{rid}: not on record for you") + facts.append(f"{rid}: not on record for you" if snap.complete + else f"{rid}: not in this scope-status response") + continue + status = row.get("status") or row.get("decision") + if not status: + facts.append(f"{rid}: listed, with no status stated") continue - status = row.get("status") or row.get("decision") or "unknown" - by = f" by {row['decided_by']}" if row.get("decided_by") and status != "pending" else "" - facts.append(f"{rid}: {status}{by}") - return ("hestia now: " + "; ".join(facts)) if facts else "" + fact = f"{rid}: {status}" + if status != "pending": + if row.get("decided_by"): + fact += f" by {row['decided_by']}" + when = _utc(row.get("decided_at")) + if when: + fact += f" at {when}" + facts.append(fact) + return f"hestia scope status as read at {at}: " + "; ".join(facts) def group_key(text: str) -> Optional[str]: