diff --git a/docs/NOTIFICATIONS.md b/docs/NOTIFICATIONS.md index 58a8f20..9a79df7 100644 --- a/docs/NOTIFICATIONS.md +++ b/docs/NOTIFICATIONS.md @@ -396,6 +396,11 @@ bytes and cannot read them. A note that could hold a credential is left out. The [`card_detail.py`](../loopdy_plugin/card_detail.py), and `fixtures/contracts/sealed-card-v1-vector.json` is shared with the app's tests. +The detail also says what kind of answer waits (`a`): `q` for a question, `c` for a command, `a` for an +approval to review in the app. `c` means the card's `q` is the whole command: one printable line of at most +90 characters that was not cut, folded or left out. Only then may the app offer Approve and Deny on the card. +Every other approval shows its description, and the person reads the command in the app first. + A card with detail gets an update for each new step, at most one every 8 seconds. A new step alone is sent with `quiet: true`, which the service sends at low priority, except that one update in every 30 seconds is not quiet. A question, a command that waits for approval and the end of the turn go at once. A chat without a key diff --git a/loopdy_plugin/card_detail.py b/loopdy_plugin/card_detail.py index 247b5fe..0293991 100644 --- a/loopdy_plugin/card_detail.py +++ b/loopdy_plugin/card_detail.py @@ -12,6 +12,8 @@ v 1 · b turn start · s steps so far · t tool in progress · n its note tr last finished steps, newest first: {t tool, c how many in a row, n note} say the model's latest words between steps · q the question that waits + a what kind of answer q waits for: "q" a question, "c" a command shown whole (the phone + may offer Approve and Deny beside it), "a" an approval to review in the app p the reply's first lines · h helper goals · cu/cm context tokens used/window · m model The phone words the steps itself from the tool names (it has the translations). `fixtures/contracts/sealed-card-v1-vector.json` is shared with the app's opener. @@ -37,6 +39,8 @@ _MAX_PLAINTEXT_BYTES = 840 _AAD_LABEL = "bighelp-card-v1|" _TRAIL = 3 +# The most of a command the phone's card shows in full beside Approve. +_COMMAND_CHARS = 90 def key_from(value: Any) -> bytes | None: @@ -51,7 +55,7 @@ def key_from(value: Any) -> bytes | None: def new_detail(started: int) -> dict[str, Any]: return {"started": int(started), "steps": 0, "tool": "", "note": "", "trail": [], "say": "", - "question": "", "preview": "", "goals": {}, "used": None, "window": None, "model": ""} + "question": "", "ask": "", "preview": "", "goals": {}, "used": None, "window": None, "model": ""} def _line(value: Any, limit: int) -> str: @@ -62,6 +66,18 @@ def _line(value: Any, limit: int) -> str: return text if len(text) <= limit else text[:limit - 1].rstrip() + "…" +def whole_command(command: Any) -> str: + """The command exactly as the card will show it, or nothing when the card cannot show all of it. + + A yes given on the card is a yes to these characters, so only one short printable line counts: + nothing the card would cut, fold or leave out. + """ + if not isinstance(command, str): return "" + text = command.strip() + if not text or len(text) > _COMMAND_CHARS or not text.isprintable(): return "" + return text if _line(text, _COMMAND_CHARS) == text else "" + + def _site(value: Any) -> str: if isinstance(value, list): value = value[0] if value else "" if not isinstance(value, str): return "" @@ -134,7 +150,9 @@ def plaintext(detail: dict[str, Any], phase: str) -> bytes: if detail["note"]: body["n"] = detail["note"] if detail["trail"]: body["tr"] = [dict(step) for step in detail["trail"]] if detail["say"] and not terminal: body["say"] = detail["say"] - if phase == "waiting" and detail["question"]: body["q"] = _line(detail["question"], 200) + if phase == "waiting" and detail["question"]: + body["q"] = _line(detail["question"], 200) + if body["q"] and detail.get("ask") in ("q", "c", "a"): body["a"] = detail["ask"] if terminal and detail["preview"]: body["p"] = _line(detail["preview"], 280) goals = [goal for goal in detail["goals"].values() if goal][:2] if goals and not terminal: body["h"] = goals @@ -152,6 +170,8 @@ def encoded() -> bytes: for key in ("p", "q"): while len(encoded()) > _MAX_PLAINTEXT_BYTES and len(body.get(key, "")) > 40: body[key] = body[key][:len(body[key]) // 2].rstrip() + "…" + # A command that was cut is no longer the one a yes would run. + if body.get("a") == "c" and body.get("q") != detail["question"]: body["a"] = "a" if len(encoded()) > _MAX_PLAINTEXT_BYTES: # Tool names or what is left in a script that takes many bytes a character: the timer and count alone. body = {"v": VERSION, "b": detail["started"], "s": detail["steps"]} diff --git a/loopdy_plugin/managed_notifications.py b/loopdy_plugin/managed_notifications.py index bacbcc2..b83b894 100644 --- a/loopdy_plugin/managed_notifications.py +++ b/loopdy_plugin/managed_notifications.py @@ -1637,10 +1637,11 @@ def _observe(self, hook: str, *, profile: str, **payload: Any): card_detail.tool_started(work["detail"], payload.get("tool_name"), payload.get("args")) question = _clarify_question(payload.get("args")) if payload.get("tool_name") == "clarify" else "" work["detail"]["question"] = question + work["detail"]["ask"] = "q" if question else "" work["phase"] = "waiting" if question else "using_tool" elif hook == "post_tool_call": card_detail.tool_finished(work["detail"], payload.get("tool_name"), payload.get("args")) - work["detail"]["question"] = "" + work["detail"]["question"] = work["detail"]["ask"] = "" work["phase"] = "using_tool" work["observed_at"] = int(self.clock()) phase, active_count, terminal = self._work_projection(work) @@ -1661,7 +1662,11 @@ def _observe_card_approval(self, hook: str, profile: str, payload: dict[str, Any if work is None or work["terminal"] or work["outcome"] is not None: return work.setdefault("detail", card_detail.new_detail(work["observed_at"])) asking = hook == "pre_approval_request" - work["detail"]["question"] = self._rich_text(payload.get("description") or payload.get("command")) if asking else "" + # A short command is shown whole, so the phone may take the answer on the card. Anything + # longer shows its description, and the person reads the command in the app first. + command = card_detail.whole_command(payload.get("command")) if asking else "" + work["detail"]["question"] = (command or self._rich_text(payload.get("description") or payload.get("command"))) if asking else "" + work["detail"]["ask"] = ("c" if command else "a") if asking else "" work["phase"] = "waiting" if asking else "using_tool" phase, active_count, terminal = self._work_projection(work) self._queue_activity(profile, session_id, turn, phase, active_count, terminal, detail=work["detail"]) diff --git a/tests/test_card_detail.py b/tests/test_card_detail.py index 5620ffc..59e1e1b 100644 --- a/tests/test_card_detail.py +++ b/tests/test_card_detail.py @@ -104,6 +104,37 @@ def test_a_new_step_alone_goes_quietly_and_one_update_in_thirty_seconds_is_loud( self.tool("post_tool_call", "read_file") self.assertNotIn("quiet", self.pending()) + def approval(self, hook: str, **fields): + self.service.observe(hook, profile="default", session_id="native-session", turn_id="turn-a", + surface="gateway", **fields) + + def test_the_card_says_what_kind_of_answer_waits(self): + self.bind() + # A question: the person answers in words, so the card only says it is one. + self.tool("pre_tool_call", "clarify", question="Send the order for $47.00?") + self.assertEqual(self.detail(self.pending())["a"], "q") + self.tool("post_tool_call", "clarify") + self.assertNotIn("a", self.detail(self.pending())) + # A short command is shown whole, so the phone may offer Approve and Deny beside it. + self.now += 9 + self.approval("pre_approval_request", command="rm -rf build/cache", description="recursive delete") + detail = self.detail(self.pending()) + self.assertEqual((detail["q"], detail["a"]), ("rm -rf build/cache", "c")) + self.approval("post_approval_response", command="rm -rf build/cache", choice="once") + self.assertNotIn("a", self.detail(self.pending())) + + def test_a_command_the_card_cannot_show_whole_is_reviewed_in_the_app(self): + self.bind() + long = "find . -name '*.log' -mtime +30 -print0 | xargs -0 rm -f && echo done > /tmp/cleanup-finished.txt && date" + for command in (long, "echo one\necho two", "echo two spaces"): + self.approval("pre_approval_request", command=command, description="deletes files") + detail = self.detail(self.pending()) + self.assertEqual((detail["q"], detail["a"]), ("deletes files", "a"), command) + self.service.drain_pending() + self.approval("post_approval_response", command=command, choice="deny") + self.service.drain_pending() + self.now += 9 + def test_a_question_waits_on_the_card_and_the_reply_shows_at_the_end(self): self.bind() self.tool("pre_tool_call", "clarify", question="Send the order for $47.00?")