ZCode must support a single cancellation contract for every active user turn.
The local TUI Esc shortcut while output is active and ZCode app-server session/cancel
notification are input adapters for the same runtime behavior: pause the current
turn output by cancelling active model and tool work, preserve observability, and
return the session to an idle state.
Cancellation is scoped to one active turn in one session. There is no process global abort controller because concurrent sessions, subagents, and background tasks must not cancel each other by accident.
Each active turn owns a root cancellation scope:
session/prompt starts.AbortSignal, never the writable AbortController.TUI:
Esc must not exit the TUI or mutate the draft input.
It only shows a local status hint that Esc pauses active output and
confirmed double Ctrl-C exits the fullscreen TUI.Esc keeps its local meaning:
deny the approval or cancel editing the Other answer.Esc aborts the current
turn, treats that as pausing active output, and keeps the TUI open.Ctrl-C first belongs to the focused normal prompt input when that input has
draft content. It clears the entire draft text and any draft attachments,
resets the pending exit confirmation, keeps the TUI open, and does not abort
active work.Ctrl-C is protected against
accidental exits. When no local prompt owns the key and no copyable OpenTUI
text selection exists, the first press only shows Press Ctrl-C again to
exit. and arms a short confirmation window. A second consecutive Ctrl-C
inside that window aborts active work and closes the fullscreen TUI. Any
non-Ctrl-C key, draft-clearing Ctrl-C, or confirmation timeout clears the
pending exit.ZCode app-server:
session/cancel aborts the active prompt controller for that ZCode app-server session.stopReason: "cancelled" and must not surface
as a JSON-RPC error.Core treats user cancellation as a first-class turn outcome, not as an unknown failure.
turn_cancelled.Turn was cancelled.turn_error may carry error.type = "turn_cancelled" for event consumers.model_request_cancelled, DOM
AbortError, or Node ABORT_ERR maps to turn_cancelled when it belongs to
the active turn signal.Once cancellation is observed, runtime stops scheduling new work. Already running adapters receive the abort signal and perform their own best-effort cleanup according to their tool or I/O contract.
Network-backed model adapters must treat abort as a transport cancellation, not
only as a local state flag. Provider fetch implementations must pass the active
turn AbortSignal to the underlying HTTP client and, for Node http/https
fallbacks, explicitly destroy both the pending client request and any response
body stream when the signal aborts. This keeps TUI Esc and ZCode app-server
session/cancel on the same cancellation path: the local TCP/SSE stream is
closed, late provider bytes are ignored by the cancelled turn, and core receives
a structured cancellation result instead of waiting for the remote server to
finish voluntarily.
Partial assistant output may remain visible to the user, but cancelled turns must not create provider-visible dangling tool calls on resume.
traceId, sessionId, turnId, and
current phase so debug views can reconstruct where cancellation landed.Required coverage:
Esc during idle prompt input does not close the TUI and preserves the
draft input.Esc pauses an active prompt without closing the TUI.Esc during approval keeps denying the approval.Ctrl-C with a non-empty normal prompt draft clears the draft and draft
attachments without closing or aborting active output.Ctrl-C with an empty normal prompt input shows Press Ctrl-C again
to exit. without closing; a second consecutive Ctrl-C exits.turn_cancelled.AbortSignal and subprocess adapters.session/cancel resolves an in-flight prompt as cancelled, including
when the prompt throws after observing the abort signal.