Skip to content

Chained Cancellation ​

Cancellation is cooperative: the framework never kills a thread. It sets an event, and code that may run long checks it at agreed points. The event is chained, so cancelling one agent reaches every sub-agent it spawned — but not upward.

The API ​

CallBehaviour
agent.cancel()Set the agent's flag. Returns False (and sets nothing) while the agent is idle — cancelling a finished run is a no-op.
agent.check_cancel()Raise CancelledError(reason) if this agent or any ancestor is flagged.
agent.is_runningTrue while an execution or a tracked command is active.
agent.cancel_eventThe ChainedEvent itself: parent, derive(), is_set(), is_chain_of(other).
run_scope()Context manager used by execute() and command execution: marks the agent running, refuses to start while cancelled, and turns Ctrl-C into a cancel.

The built-in loop calls check_cancel() before the loop, before every model call, while waiting for the model-call semaphore, and while waiting for a sub-agent. Tools that take seconds should call it themselves:

python
from xun import CancelledError

def crawl(urls: list[str], ctx):
    """Fetch a list of pages."""
    for url in urls:
        ctx.agent.check_cancel()        # the checkpoint you choose
        ...

CancelledError carries a reason string, set where the cancellation was noticed — check_cancel() reports "Operation cancelled by user.", the foreground bash tool names the command it stopped. It is not converted into an error result: it propagates out of execute(), which shows the reason once; the interactive session then drops the trailing user message so the turn can be retried.

Chain semantics ​

Agent.inherit(parent) sets child.cancel_event.parent = parent.cancel_event (inherit_cancel_event=True), and is_set() walks the whole chain. Consequences:

  • Cancelling the parent reaches every chained descendant, however deep, without anyone forwarding flags.
  • Cancelling a child does not affect the parent.
  • A child built by a custom getter without inheriting the chain is detached: it keeps running, and agent_wait deliberately leaves it alone when the parent is cancelled. Chained children, in contrast, are handed their own cancel by agent_wait so they stop at their next checkpoint.

The signal outlives its run ​

The cancel flag is cleared when the outermost run_scope is entered, never when a run exits. A cancel therefore stays set until the agent's next run begins — which is what makes sub-agents that are still finishing observe it. Only nested scopes leave the flag untouched.

A cancel raised by an ancestor does block new work: run_scope() checks the whole chain on entry and raises CancelledError, which execute() shows once and re-raises. The agent's own flag, by contrast, is cleared at that same entry — which is why cancel() also refuses to set anything while the agent is idle: a stray "cancel" must never poison a later run.

Where cancel comes from ​

EntrypointTrigger
terminal (xun)Ctrl-C — the running loop is cancelled, the prompt stays usable
web UIthe stop button sends a cancel message for that agent
xunxthe same, per user session
codeagent.cancel(), typically from a UI thread or a signal handler

A confirmation in the terminal preempts the input prompt it interrupts: text you were already typing is set aside and restored verbatim once you answer.

Xun 1.4.2dev (unreleased; source 51d23f3, 2026-10-16) documentation, generated from the source tree.