diff --git a/sage/gateway/heartbeat.py b/sage/gateway/heartbeat.py index 7c3f3b30f..38602f3b2 100644 --- a/sage/gateway/heartbeat.py +++ b/sage/gateway/heartbeat.py @@ -3408,8 +3408,13 @@ 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_snap=None) -> str: + """The todo section of the window: what is still open, from the whole log. + + `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: @@ -3426,6 +3431,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_snap) + 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.") @@ -3561,7 +3569,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_snap=None) -> str: from sage.gateway.being_join import carried_account, last_session_number from sage.gateway import being_params as _bp parts = [] @@ -3705,10 +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 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_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)) + parts.append(todo_view(instance, scope_snap=scope_snap)) if census_todo: parts.append(census_todo) journal = _read(instance / "journal.md", 1200) @@ -5252,9 +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)" + # 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_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 [])] @@ -5297,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 @@ -5491,6 +5518,7 @@ def _build_state(per_conv, turn_chars): services=_services, mark_conversations=False, settled_turns=_settled_turns, proprioception=_proprioception, + 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 new file mode 100644 index 000000000..d76b2a0f6 --- /dev/null +++ b/sage/gateway/tests/test_todo_scope_live_status.py @@ -0,0 +1,227 @@ +"""The todo view and the carried account put one dated hestia_scope_status read beside an open item that names +a scope request. + +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])) + +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", + "decided_at": DECIDED, "path": "/home/dp/.local/bin"} +PENDING = {"request_id": "scope-6ec4bf5583c4", "status": "pending", "path": "/home/dp/.local/bin"} +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_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("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("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_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()) == "" + + +# -- 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_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) + before = _todo(inst) + out = todo_view(inst, scope_snap=snap()) + lines = out.splitlines() + 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__": + 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 acac2feac..5a927bd89 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,6 +66,112 @@ def close_all(items: Dict[str, Tuple[str, object]], body: str) -> Optional[Tuple _DEVICE = re.compile(r"/dev/[A-Za-z]+\d*", re.I) +#: 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; 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. + + 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 = (snap.rows or {}).get(rid) + if not row: + 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 + 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]: """The one id an item is grouped under, or None (shown alone).