"""The operator's kill switch, as the working loops see it (architecture L2). `stop` has to reach work that is already running, from a thread that is not running it. So it is a flag, set by whoever hears the command and checked by the loops at each tool boundary. Setting it touches nothing durable: the record of a stop is written by the worker when the stopped work returns. """ import threading from nisse_core.canonical import ToolCall, ToolResult from nisse_core.state import Transcript STOPPED_RESULT = "not run: the operator stopped the agent" class Cancel: def __init__(self) -> None: self._event = threading.Event() def set(self) -> None: self._event.set() def clear(self) -> None: self._event.clear() @property def requested(self) -> bool: return self._event.is_set() def skip_calls( transcript: Transcript, calls: list[ToolCall], reason: str = STOPPED_RESULT ) -> None: """Record calls left unrun. Every tool call needs a result for the transcript to render again, and the result says what happened.""" for call in calls: transcript.append( ToolResult(call_id=call.call_id, result=reason, success=False) )