Files
ai-agent/backend/test_ask_cancel.py
gabrielvidalandClaude Sonnet 5.5 7c983e9c94 feat(notify): an agent can cancel its ask; the time waited moves bottom right
- `notify cancel ask_… [--reason R]` (also `notify-ask cancel`) withdraws a
  pending ask: POST /api/ask/{id}/cancel on the lab, the sidecar outbox's
  cancel (now with its reason) on a worker, mirrored to the hub record.
  The ask turns `cancelled` (cancelledAt / cancelledBy / cancelReason), its
  waiters wake, the push leaves every device, and `notify wait` exits 3.
- The person can cancel too: a Cancel (second tap confirms) on the thread's
  ask card and on the home *Waiting for feedback* card; an ask cancelled from
  the viewer ends a worker's wait the same way.
- A cancelled ask's card greys out with a `cancelled` badge and
  "Cancelled by the agent — <reason>" / "Cancelled by you".
- The live time-waited counter no longer sits beside the question (each tick
  changed its width and reflowed the text): bottom right, next to Cancel and
  Dismiss, on the home card and in the bell.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-30 16:51:22 +01:00

100 lines
3.9 KiB
Python

"""Cancelling an ask (2026-11-30): the agent's ``notify cancel`` and the
card's Cancel both end a pending ask — its waiters wake, its push leaves the
phone, an ended ask stays as it was. Imports ``main`` against throwaway paths
(no startup event → no threads)."""
import os
import pathlib
import tempfile
import threading
import unittest
from unittest.mock import patch
TMP = pathlib.Path(tempfile.mkdtemp())
for k, v in {"DB_PATH": TMP / "x.db", "TRANSCRIPTS_DIR": TMP / "t",
"WORKSPACE": TMP, "REPO_DIR": TMP, "ARCHIVE_DIR": TMP / "arch",
"META_PATH": TMP / "meta.json", "WORKERS_PATH": TMP / "w.json",
"PROFILES_PATH": TMP / "profiles.json",
"WORKERS_TRANSCRIPTS_DIR": TMP / "mirror",
"CRON_PATH": TMP / "cron.json", "NOTIFY_PATH": TMP / "n.json",
"FORMS_PATH": TMP / "f.json", "UI_STATE_PATH": TMP / "ui.json",
"NOTIF_READ_PATH": TMP / "nr.json",
"USERS_PATH": TMP / "users.json", "AUTH_PATH": TMP / "auth.json",
"WEBPUSH_PATH": TMP / "webpush.json", "VAPID_KEY_PATH": TMP / "vapid.pem",
"PLUGINS_DIR": TMP / "plugins", "TOOLS_TOKEN_KEY": TMP / "tools.key",
"PROJECTS_DIR": TMP / "projects", "INTERNAL_API_TOKEN": "tok",
"SIDECAR_TOKEN": "", "PI_TRANSCRIPTS_DIR": ""}.items():
os.environ[k] = str(v)
from fastapi.testclient import TestClient # noqa: E402
import main # noqa: E402
from notifications.service import _create_ask # noqa: E402
DEPS = main.app.state.ai
OWNER_H = {"X-Internal-Auth": "tok"}
class TestCancelAsk(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.c = TestClient(main.app)
def setUp(self):
self.cleared = []
p = patch("notifications.service.deliver.clear",
side_effect=lambda _d, _sid, tag, *a: self.cleared.append(tag))
p.start()
self.addCleanup(p.stop)
def ask(self):
return _create_ask(DEPS, {"question": "Deploy?", "options": ["Yes", "No"]})["id"]
def cancel(self, aid, **body):
return self.c.post(f"/api/ask/{aid}/cancel", headers=OWNER_H, json=body or None)
def test_the_agent_cancels_with_a_reason(self):
aid = self.ask()
r = self.cancel(aid, reason=" found the answer in the README ")
self.assertEqual(r.status_code, 200, r.text)
rec = r.json()
self.assertEqual((rec["status"], rec["cancelledBy"], rec["cancelReason"]),
("cancelled", "agent", "found the answer in the README"))
self.assertTrue(rec["cancelledAt"])
self.assertEqual(self.cleared, [aid])
# It is no longer waiting on anyone.
self.assertNotIn(aid, [e["id"] for e in DEPS.notify_store.log()
if e.get("status") == "pending"])
def test_the_card_cancels_as_the_user(self):
aid = self.ask()
rec = self.cancel(aid, by="user").json()
self.assertEqual((rec["status"], rec["cancelledBy"], rec["cancelReason"]),
("cancelled", "user", None))
def test_a_waiting_long_poll_wakes(self):
aid = self.ask()
got = {}
t = threading.Thread(target=lambda: got.update(
DEPS.notify_store.wait_for_answer(aid, 20)))
t.start()
self.cancel(aid)
t.join(5)
self.assertFalse(t.is_alive())
self.assertEqual(got["status"], "cancelled")
def test_an_answered_ask_stays_answered(self):
aid = self.ask()
self.c.post(f"/api/ask/{aid}/answer", headers=OWNER_H, json={"index": 0})
self.cleared.clear()
rec = self.cancel(aid).json()
self.assertEqual((rec["status"], rec["answer"]), ("answered", "Yes"))
self.assertEqual(self.cleared, [])
def test_an_unknown_ask_is_404(self):
self.assertEqual(self.cancel("ask_000000000000").status_code, 404)
if __name__ == "__main__":
unittest.main()