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 (e.g. "Operation cancelled by user.", or the signal the caller passed). It is not converted into an error result: it propagates out of execute(), and the interactive session reports it and 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.

This also means a leftover flag blocks new work: run_scope() checks it on entry, so an agent whose flag is still set returns an Err on its next execute() instead of silently continuing. agent.cancel() sets nothing while the agent is idle, precisely so that a stray "cancel" never poisons the next 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.1 (unreleased; package 1.4.0, source 9e8246e, 2026-10-14) documentation, generated from the source tree.