English
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
| Call | Behaviour |
|---|---|
agent.cancel(*, force=False) | Set the agent's flag. Returns False (and sets nothing) while the agent is idle — cancelling a finished run is a no-op. force=True sets it regardless, which is what finalize() uses. |
agent.check_cancel() | Raise CancelledError(reason) if this agent or any ancestor is flagged. |
agent.is_running | True while an execution or a tracked command is active. |
agent.cancel_event | The 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 and every step, before every model call and between streamed chunks, while waiting for the model-call semaphore, before every tool call, and while waiting for a sub-agent; run_scope() checks once more on exit. Tools that take seconds should call it themselves:
python
from xun import ToolCallContext
def crawl(urls: list[str], ctx: ToolCallContext):
"""Fetch a list of pages."""
for url in urls:
ctx.agent.check_cancel() # the checkpoint you choose
...The annotation is what marks the parameter as injected — leave ctx unannotated and the model is asked to supply it.
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) always sets child.cancel_event.parent = parent.cancel_event, 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 that a custom getter builds as a plain
Agent(...)instead ofAgent.inherit(...)is detached — its chain starts with itself: it keeps running, andagent_waitdeliberately leaves it alone when the parent is cancelled. Chained children, in contrast, are handed their own cancel byagent_waitso 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
| Entrypoint | Trigger |
|---|---|
terminal (xun) | Ctrl-C — the running loop is cancelled, the prompt stays usable |
| web UI | the stop button sends a cancel message for that agent |
xunx | the same, per user session |
| code | agent.cancel(), typically from a UI thread or a signal handler |
| agent finalize | before_finalize cancels every sub-agent still in its pool |
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.