Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ CLI; on by default in the desktop shell, `--stream off` at launch). Streamed
text is provisional, and the journals record only the validated reply. The
shell pushes chat events to the main window through one caller-scoped channel
that needs no `core:` permission ([ADR-0049](docs/adr/0049-stream-local-model-replies-and-push-chat-events.md)).
A cancel request interrupts a streamed model request and the turn is recorded as
cancelled; a non-streamed request, the compaction request, and a tool call in
progress are not interruptible
([ADR-0050](docs/adr/0050-interrupt-a-streamed-model-request-on-cancellation.md)).
A cancel request interrupts any model request, streamed or not, and the
compaction requests, and the turn is recorded as cancelled; a read, list, or
edit tool call in progress is not interruptible
([ADR-0050](docs/adr/0050-interrupt-a-streamed-model-request-on-cancellation.md),
[ADR-0051](docs/adr/0051-cancel-every-model-request-and-interrupt-cli-turns.md)).
On the CLI, Ctrl-C during `chat` or `session compact` cancels instead of ending
the process.
The desktop shell has no settings interface, development panels, packaging, or
recovery interface. Most other planned features are not
implemented.
Expand Down
61 changes: 61 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,67 @@ Changes are recorded here before a release version is assigned.

### Added

- Version 0.34.0 lets a cancel request stop every model request, streamed or
not, and the compaction requests, and lets an interrupt cancel a CLI turn
(Phase D6). See ADR-0051 and
[cancelling every request](docs/provider-authentication.md#cancelling-every-request-0340).
Until now a non-streamed request and every compaction request ran to their
end or to the thirty-second deadline, a non-streamed text-only turn had no
cancel token, and Ctrl-C ended the CLI and left an open turn. It is a
prototype on branch `codex/request-cancellation`, stacked on
`codex/desktop-interface`; it is not merged. It adds no dependency and
removes reqwest's `blocking` feature, which drops `futures-channel` and
`futures-sink` from two dependency lists in `Cargo.lock`.
- **Adapter:** `complete` and `complete_with_tools` take a `CancelToken`, like
the streamed methods. All four methods share one transport: an async client
built inside a single-thread runtime created for the request, with the
settings and the thirty-second deadline of ADR-0050. The token is checked
before the request is sent, while the response head is awaited, and before
and while each piece of the body is awaited, every 25 ms. A set token drops
the request and returns `ProviderError::Cancelled`; a non-streamed body
read in part is discarded. The 1 MiB response bound is applied as pieces
arrive. The blocking client is removed. The wire request is unchanged.
- **Turn:** every chat turn creates one token after the session checks and
before the compaction check, and `ChatHooks::on_cancel_token` receives it
on every path, streamed or not. The new `ChatHooks::cancel` lets a front
end supply the token, and the new `TurnRuntime::with_cancel_token` hands it
to the runtime. A token already set when the turn would start ends it as
`cancelled` before the turn marker, recording nothing; the text-only path
now matches the runtime here.
- **Compaction:** `compact_journal` takes a token. A cancelled compaction
fails with the error kind `cancelled` and the message "Compaction was
cancelled; nothing was recorded", and the journal is unchanged.
- **CLI:** `omgcode chat` and `session compact` install the interrupt handler
of the trusted `run` command once the journal is open. Ctrl-C or
Ctrl-Break on Windows and `SIGINT` or `SIGTERM` on Linux set the token. The
command fails with the code `cancelled`; a chat turn that had started is
recorded as cancelled and the error carries the session locator. A handler
that cannot be installed fails the command with the new code `interrupt`.
`omgcode-platform` gains `watch_interrupt`, which runs a closure once on an
interrupt; `cancel_on_interrupt` wraps it. `capabilities` reports
`cancellable_adapters: ["lm_studio"]`.
- **Desktop:** no command or wire type changes. `chat_cancel` and exit now
stop a non-streamed request (`--stream off`) and a pre-turn compaction. A
turn cancelled before it handed over its token ends before it is recorded.
- **Tests:** core fixtures for a cancel of a non-streamed text and tool
request before sending and before the head, a unit test that cancels
while the rest of a held body is awaited once the head has arrived, and a
slow non-streamed reply under a live token; the runtime's caller token; app
tests for cancelled non-streamed text and tool turns, a cancelled pre-turn
compaction, a cancelled `compact_journal`, and a token set before the turn;
desktop tests for cancelling a non-streamed turn and for the shutdown grace
with a turn held in an event sink; Linux CLI tests that deliver `SIGINT`
during `chat` and `session compact`; and `watch_interrupt` in the platform
interrupt test. Two tests that asserted the old behaviour were rewritten:
`a_text_only_turn_..._calls_the_cancel_hook_once` (it skipped the hook) and
the desktop `cancelling_a_non_streamed_text_turn_stops_its_request` (it
asserted that cancelling changed nothing). Two others were replaced:
`a_non_streamed_text_turn_never_calls_the_cancel_hook_and_cannot_be_cancelled`
and the desktop shutdown-grace test's use of a held non-streamed reply,
which can now be cancelled.
- **Still not interruptible:** a read, list, or edit tool call in progress.
Not verified: a live LM Studio server, and an interrupt delivered to the
CLI on Windows.
- Version 0.33.0 interrupts a streamed model request when a turn is cancelled
(Phase D5). See ADR-0050 and
[cancellation](docs/provider-authentication.md#cancellation-0330). Until now
Expand Down
12 changes: 5 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "3"
members = ["crates/omgcode-core", "crates/omgcode-cli", "crates/omgcode-platform", "crates/omgcode-app", "crates/omgcode-desktop"]

[workspace.package]
version = "0.33.0"
version = "0.34.0"
edition = "2024"
rust-version = "1.99.1"
license = "Apache-2.0"
Expand All @@ -17,7 +17,7 @@ uuid = { version = "1", features = ["v4", "serde"] }
time = { version = "0.3", features = ["formatting", "parsing", "serde"] }
thiserror = "2"
tokio = { version = "1", default-features = false, features = ["rt", "time", "net", "fs", "io-util"] }
reqwest = { version = "0.13.5", default-features = false, features = ["blocking", "json", "native-tls"] }
reqwest = { version = "0.13.5", default-features = false, features = ["json", "native-tls"] }
sha2 = "0.11.1"
clap = { version = "4.6.7", features = ["derive"] }
directories = "6"
Expand Down
37 changes: 31 additions & 6 deletions IMPLEMENTATION_PLAN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OMGCode implementation plan

Last verified: 2026-11-16.
Last verified: 2026-11-17.

This guide is part of `main` after PR #9. Read it before selecting the next
increment. Checkpoint preparation does not mean native replacement exists.
Expand Down Expand Up @@ -99,6 +99,11 @@ This is a resume guide, not a replacement for the owning contracts in
cancelled (ADR-0050). A cancelled turn is recorded with the failure reason
`cancelled`, and the desktop exit no longer abandons a streamed turn. It is
**not merged and has no pull request**.
- Branch `codex/request-cancellation`, stacked on `codex/desktop-interface`
(open PR #33): **Phase D6**, version **0.34.0**, which lets a cancel request
stop every model request, streamed or not, and the compaction requests, and
lets Ctrl-C cancel a CLI `chat` turn or `session compact` (ADR-0051). It is
**not committed, not merged, and has no pull request**.

Implemented today:

Expand Down Expand Up @@ -234,9 +239,9 @@ Not implemented:
- Model create, delete, rename, multi-edit, network, Git, and MCP tools;
concurrent-writer detection for any apply route; live provider tool-call
qualification; and streaming against a live server, discovery, and
cancellation of a streamed request against a live provider. A non-streamed
request, the compaction request, and a tool call in progress cannot be
interrupted.
cancellation of any request against a live provider. A read, list, or edit
tool call in progress cannot be interrupted, and a CLI interrupt has not been
delivered on Windows.
- A run network destination allowlist, runtime skills, memories, mid-turn and
provider-native compaction, and caches. Nothing calls the unconfined process
runner.
Expand Down Expand Up @@ -1063,13 +1068,31 @@ login or entitlement route.
journal record changes shape. A non-streamed request, the compaction request,
and a tool call in progress are not interrupted, and the CLI has no cancel
source. The cancel path was checked against local fixtures only.
- **D6 (version 0.34.0):** every model request can be cancelled, and an
interrupt cancels a CLI turn. See
[ADR-0051](docs/adr/0051-cancel-every-model-request-and-interrupt-cli-turns.md).
`complete` and `complete_with_tools` take a `CancelToken`; all four adapter
methods share the async transport of D5, and reqwest's `blocking` feature is
removed. A non-streamed body read in part is discarded. Every chat turn
creates one token before the compaction check and hands it to
`ChatHooks::on_cancel_token` on every path; `ChatHooks::cancel` lets a front
end supply it and `TurnRuntime::with_cancel_token` passes it to the runtime.
A token set before the turn starts records nothing. `compact_journal` takes
a token, and a cancelled compaction records nothing. `omgcode chat` and
`session compact` install the platform interrupt handler through the new
`watch_interrupt`; the command fails with the code `cancelled`, and a
started chat turn is recorded as cancelled. A handler that cannot be
installed fails the command with the code `interrupt`. `capabilities`
reports `cancellable_adapters: ["lm_studio"]`. The desktop stops a
non-streamed turn and a pre-turn compaction with no command change. Checked
against local fixtures; the CLI interrupt was delivered on Linux only.

Still open in Phase D: cloud provider adapters, subscription and API-key
login, token refresh, account lifecycle, a memory-only session credential,
endpoint rules for non-loopback providers, HTTPS, and certificates,
qualification of the stream rules and of cancellation against a live LM Studio
server, a cancel source for the CLI, interrupting non-streamed and compaction
requests, and a longer deadline for long
server, an interrupt delivered to the CLI on Windows, interrupting a read,
list, or edit tool call in progress, and a longer deadline for long
generations (the thirty-second deadline covers the whole response). The frontend shows nothing about the credential source beyond the
endpoint text it already displayed.

Expand Down Expand Up @@ -1121,6 +1144,8 @@ not merged, and no pull request exists yet.
- **D5 (ADR-0050):** `chat_cancel` interrupts a streamed turn, the main window
shows the notice "Turn cancelled." decided by the error code `cancelled`, and
exit cancels every running turn. No command or wire type changes.
- **D6 (ADR-0051):** `chat_cancel` and exit also stop a non-streamed turn
(`--stream off`) and a pre-turn compaction. No command or wire type changes.

Still open for the desktop:

Expand Down
45 changes: 33 additions & 12 deletions crates/omgcode-app/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,28 @@ pub enum ChatStreamEvent<'a> {
Text(&'a str),
}

/// Optional callbacks a front end can supply to a turn.
/// Optional callbacks and cancellation source a front end can supply to a turn.
///
/// One hook delivers the cancellation token. Cancellation is cooperative: the
/// core checks it between rounds and tool calls, and a streamed model request
/// also watches it and stops within about 25 ms (ADR-0050). A request that is
/// not streamed cannot be interrupted. The other hook streams provisional
/// text.
/// Every turn has one cancel token, created before compaction and before any
/// model request (ADR-0051). Cancellation is cooperative: the core checks it
/// between rounds and tool calls, and every model request, streamed or not,
/// including the compaction requests before a turn, watches it and stops
/// within about 25 ms (ADR-0050, ADR-0051). A tool call already running is
/// not interrupted, except a command, whose run is cancelled. One hook
/// delivers the token, and the other streams provisional text.
#[derive(Default)]
pub struct ChatHooks<'a> {
/// Called once, before the first model request: on the tool-enabled path
/// right after the turn runtime is built, and on a text-only turn only when
/// it streams (`on_stream` is set). A text-only turn that does not stream
/// never calls it, because a non-streamed request cannot be interrupted.
/// The callback runs on the turn's thread and must not block; hand the
/// token to another thread and return.
/// The token the turn uses. When `None`, the turn creates its own. A front
/// end that must be able to cancel before the turn starts, such as an
/// interrupt watcher installed beforehand, supplies it here. A token
/// already set when the turn starts ends it as cancelled before any model
/// request.
pub cancel: Option<CancelToken>,
/// Called once with a clone of the turn's token, after the session checks
/// and before the compaction check and the first model request, on both
/// the text-only and the tool-enabled path. The callback runs on the
/// turn's thread and must not block; hand the token to another thread and
/// return.
pub on_cancel_token: Option<&'a mut dyn FnMut(CancelToken)>,
/// When set, every model request of the turn is streamed and this callback
/// receives [`ChatStreamEvent::RequestStarted`] before each request and
Expand Down Expand Up @@ -167,6 +174,8 @@ struct Turn<'a> {
settings: &'a ChatSettings,
token: Option<&'a str>,
data_root: &'a Path,
/// The turn's one cancel token, shared by compaction and every request.
cancel: &'a CancelToken,
}

/// Run one chat turn, building the clients from the settings first.
Expand All @@ -193,6 +202,11 @@ pub fn run_chat_turn(
/// provider request or runtime is recorded in the journal where the core can
/// do so, and an uncertain storage failure is reported, never papered over.
///
/// A cancelled turn returns `cancelled` (ADR-0051). A cancellation during the
/// compaction before a turn records nothing, because neither the compaction
/// nor the turn was recorded yet; a cancellation after the turn started is
/// recorded with the failure reason `cancelled` and leaves no open turn.
///
/// The message and instructions are cleaned of the known token before use. The
/// token is used only for redaction and is never part of any returned error.
pub fn run_chat_turn_with_clients(
Expand All @@ -205,12 +219,14 @@ pub fn run_chat_turn_with_clients(
let settings = input.settings;
let message = redact_known_secret(input.message, input.token);
let instructions = redact_known_secret(&settings.instructions, input.token);
let cancel = hooks.cancel.clone().unwrap_or_default();
let turn = Turn {
message: &message,
instructions: &instructions,
settings,
token: input.token,
data_root,
cancel: &cancel,
};
if journal.snapshot().archived {
return Err(AppError::new(
Expand All @@ -224,6 +240,11 @@ pub fn run_chat_turn_with_clients(
tools::ACTIVE_TURN_MESSAGE,
));
}
// The front end can cancel from here on, which covers the compaction
// requests below as well as the turn's own requests.
if let Some(hook) = hooks.on_cancel_token.as_deref_mut() {
hook(cancel.clone());
}
let mode = select(data_root, journal)?;
let tools_enabled = matches!(mode, ToolMode::Enabled { .. });
let tool_extra = if tools_enabled {
Expand Down
12 changes: 10 additions & 2 deletions crates/omgcode-app/src/chat/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use crate::tool_mode::tool_replay_allowed;
/// auto-compaction is a `context` error that names the operator's options.
/// With auto-compaction, the summary client compacts the journal and the
/// estimate is recomputed from live settings. No control store is held across
/// the summarizer requests.
/// the summarizer requests. The turn's cancel token stops a summarizer
/// request; a cancelled compaction is `cancelled` and records nothing.
pub(super) fn preflight_context(
journal: &mut SessionJournal,
turn: &Turn<'_>,
Expand Down Expand Up @@ -68,7 +69,14 @@ pub(super) fn preflight_context(
format!("{reason}. Use session compact or rerun chat with --auto-compact."),
));
}
let outcome = compact_journal(journal, data_root, summary_client, token, summary_budget)?;
let outcome = compact_journal(
journal,
data_root,
summary_client,
token,
summary_budget,
turn.cancel,
)?;
summarizer_calls = Some(outcome.calls);
// Settings can change during the summarizer requests, so read the
// replay authority again for the compacted history.
Expand Down
Loading