"""Committed tests for cooperative Ctrl+C cancellation. One press cancels the running tool and the turn continues with the next step; three presses abort the turn and hand control back to input. Every cancelled tool reports ``code == "cancelled"`` to the model and the message history stays wire-valid (no dangling assistant ``tool_calls``). """ from __future__ import annotations # retoor import asyncio import os import tempfile import time import unittest from blackcat.agent import BlackCatBot from blackcat.cancel import ( ABORT_PRESSES, CANCEL_CODE, CancelTracker, cancelled_result, settle, ) from blackcat.fstate import FileState from blackcat.tools import BashTools try: from blackcat.tui.app import BlackCatApp from blackcat.tui.bridge import TUIBot from blackcat.tui.chrome import StatusLine HAS_TEXTUAL = True except ImportError: HAS_TEXTUAL = False class TestCancelTracker(unittest.TestCase): def test_press_counts_inside_window(self): tracker = CancelTracker(window=60.0, threshold=3) self.assertEqual(tracker.press(), 1) self.assertEqual(tracker.press(), 2) self.assertFalse(tracker.aborted) self.assertEqual(tracker.press(), 3) self.assertTrue(tracker.aborted) def test_window_expiry_restarts_count(self): tracker = CancelTracker(window=0.05, threshold=3) self.assertEqual(tracker.press(), 1) time.sleep(0.08) self.assertEqual(tracker.press(), 1) self.assertFalse(tracker.aborted) def test_abort_and_reset(self): tracker = CancelTracker(window=60.0, threshold=2) tracker.press() tracker.abort() self.assertTrue(tracker.aborted) tracker.reset() self.assertFalse(tracker.aborted) self.assertEqual(tracker.presses, 0) self.assertEqual(tracker.press(), 1) def test_threshold_matches_triple_press(self): self.assertEqual(ABORT_PRESSES, 3) class TestCancelledResult(unittest.TestCase): def test_shape_and_user_mention(self): result = cancelled_result("bash") self.assertFalse(result["ok"]) self.assertEqual(result["code"], CANCEL_CODE) self.assertIn("bash", result["error"]) self.assertIn("user", result["error"].lower()) self.assertIn("Ctrl+C", result["error"]) class TestSettle(unittest.TestCase): def test_no_task_is_safe(self): settle() def test_suppressed_cancel_keeps_result(self): async def sample(): try: await asyncio.sleep(30) except asyncio.CancelledError: settle() await asyncio.sleep(0) return "continued" return "unreached" async def main(): task = asyncio.ensure_future(sample()) await asyncio.sleep(0.05) task.cancel() return await task self.assertEqual(asyncio.run(main()), "continued") class CancelCase(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self): self.bot = BlackCatBot(provider="echo") self.addAsyncCleanup(self.bot.aclose) class TestRunToolCancel(CancelCase): async def test_cancel_converts_to_result(self): async def slow(): await asyncio.sleep(30) return {"ok": True} self.bot._extra_tools["slow"] = slow task = asyncio.ensure_future(self.bot.run_tool("slow")) await asyncio.sleep(0.05) task.cancel() result = await task self.assertFalse(result["ok"]) self.assertEqual(result["code"], "cancelled") self.assertIn("user", result["error"].lower()) self.assertIsNone(BlackCatBot.validate_wire(self.bot.messages)) async def test_cancel_names_tool_in_error(self): async def slow(): await asyncio.sleep(30) return {"ok": True} self.bot._extra_tools["slow"] = slow self.bot.messages.append( {"role": "assistant", "content": None, "tool_calls": [{"id": "call-1", "function": {"name": "slow", "arguments": "{}"}}]}) task = asyncio.ensure_future( self.bot.run_tool("slow", _tool_call_id="call-1")) await asyncio.sleep(0.05) task.cancel() result = await task self.assertEqual(result["code"], "cancelled") self.assertIn("slow", result["error"]) kinds = [msg.get("role") for msg in self.bot.messages] self.assertIn("tool", kinds) self.assertIsNone(BlackCatBot.validate_wire(self.bot.messages)) class TestWireRepair(CancelCase): def _calls(self): return [{"id": "c1", "function": {"name": "read", "arguments": "{}"}}, {"id": "c2", "function": {"name": "bash", "arguments": "{}"}}] async def test_reconcile_answers_every_call(self): calls = self._calls() self.bot.messages.append({"role": "user", "content": "go"}) self.bot.messages.append({"role": "assistant", "content": None, "tool_calls": calls}) self.assertIsNotNone(BlackCatBot.validate_wire(self.bot.messages)) await self.bot._reconcile_pending_tools(calls) self.assertIsNone(BlackCatBot.validate_wire(self.bot.messages)) answered = [msg for msg in self.bot.messages if msg.get("role") == "tool"] self.assertEqual(len(answered), 2) for msg in answered: payload = msg["content"] self.assertIn("cancelled", payload) async def test_reconcile_skips_answered_calls(self): calls = self._calls() self.bot.messages.append({"role": "user", "content": "go"}) self.bot.messages.append({"role": "assistant", "content": None, "tool_calls": calls}) await self.bot._emit_tool_result("read", {"ok": True}, "c1", {"path": "f.txt"}) await self.bot._reconcile_pending_tools(calls) self.assertIsNone(BlackCatBot.validate_wire(self.bot.messages)) answered = [msg for msg in self.bot.messages if msg.get("role") == "tool"] self.assertEqual(len(answered), 2) codes = sorted(msg["tool_call_id"] for msg in answered) self.assertEqual(codes, ["c1", "c2"]) async def test_emit_is_idempotent_per_call_id(self): await self.bot._emit_tool_result("read", {"ok": True}, "dup", {"path": "f.txt"}) await self.bot._emit_tool_result("read", cancelled_result("read"), "dup", {"cancelled": True}) dupes = [msg for msg in self.bot.messages if msg.get("tool_call_id") == "dup"] self.assertEqual(len(dupes), 1) async def test_pending_ids_detects_dangling(self): calls = self._calls() self.bot.messages.append({"role": "assistant", "content": None, "tool_calls": calls}) pending = self.bot._pending_tool_ids() self.assertEqual(sorted(cid for cid, _ in pending), ["c1", "c2"]) await self.bot._reconcile_pending_tools(calls) self.assertEqual(self.bot._pending_tool_ids(), []) class TestInterruptApi(CancelCase): def test_note_interrupt_and_abort(self): self.assertEqual(self.bot.note_interrupt(), 1) self.assertFalse(self.bot._cancel_requested()) self.bot.request_abort() self.assertTrue(self.bot._cancel_requested()) class TestBashCancel(unittest.IsolatedAsyncioTestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) self.root = os.path.join(self.tmp.name, "ws") os.mkdir(self.root) self.bash = BashTools(FileState(self.root)) async def test_cancel_kills_process_group(self): started = time.monotonic() task = asyncio.ensure_future( self.bash.run("sleep 30", timeout_ms=60000)) await asyncio.sleep(0.3) task.cancel() with self.assertRaises(asyncio.CancelledError): await task settle() self.assertLess(time.monotonic() - started, 15.0) again = await self.bash.run("echo alive") self.assertTrue(again["ok"]) self.assertEqual(again["stdout"].strip(), "alive") @unittest.skipUnless(HAS_TEXTUAL, "tui extra unavailable") class TestTuiCancel(unittest.IsolatedAsyncioTestCase): async def test_bridge_run_tool_cancel_emits_done(self): events = [] bot = TUIBot(provider="echo", emit=events.append) self.addAsyncCleanup(bot.aclose) async def slow(): await asyncio.sleep(30) return {"ok": True} bot._extra_tools["slow"] = slow task = asyncio.ensure_future(bot.run_tool("slow")) await asyncio.sleep(0.05) task.cancel() result = await task self.assertEqual(result["code"], "cancelled") kinds = [event.kind for event in events] self.assertEqual(kinds, ["tool_start", "tool_done"]) done = events[1].payload self.assertEqual(done["result"]["code"], "cancelled") self.assertIsNone(BlackCatBot.validate_wire(bot.messages)) async def test_ctrl_c_binding_cancels_without_busy_stick(self): app = BlackCatApp(bot_kwargs={"provider": "echo"}) async with app.run_test(size=(120, 40)) as pilot: await pilot.press("escape") await pilot.pause() self.assertFalse(app._busy) await pilot.press("ctrl+c") await pilot.pause() self.assertFalse(app._busy) status = app.query_one("#status", StatusLine).text self.assertIn("cancelled", status.lower()) if __name__ == "__main__": unittest.main()