Skip to content

fix(tui): let Esc cancel a prompt that has not started - #66941

Open
StAr1e wants to merge 1 commit into
anomalyco:devfrom
StAr1e:interrupt-starting-prompt
Open

StAr1e wants to merge 1 commit into
anomalyco:devfrom
StAr1e:interrupt-starting-prompt

Conversation

@StAr1e

@StAr1e StAr1e commented Nov 28, 2026 •

Copy link
Copy Markdown

Issue for this PR

Fixes #53652
Fixes #66942

Type of change

  • Bug fix

What does this PR do?

On a cold Location, a prompt is admitted before its execution starts. Until session.execution.started arrives the session reports idle, but session.interrupt is only enabled for running, so Esc does nothing. When it does reach the server, POST /interrupt returns interrupted: false and the run proceeds.

This makes the preparation window visible and cancellable:

  • A pending user inbox item with pending status starting counts as busy. The footer shows the spinner and esc cancel.
  • One Esc cancels: it sends session.interrupt with resume: false (which does not wake the pending input).
  • If the interrupt reports an active execution, the cancel path waits for idle, reverts the promoted message (including any partial text reply), and restores the captured prompt. It does not rely on the before-first-answer heuristic. If the reverted turn contains a tool call, which may have edited files, it keeps the plain interrupt so the edits are not left on disk with the turn removed.
  • If nothing was running, it cancels the still-pending inbox rows; the captured prompt is restored when session.inbox.cancelled arrives.
  • The cancel intent is tied to the inbox rows being cancelled, not a time window. It is dropped when those rows are cancelled and spent on the first running transition, so a later run in the session is not interrupted.
  • aborting… clears on session.execution.interrupted, so a resumed successor run does not leave it stuck.
  • session.interrupt no longer requires composer focus.

Measured on the local server: cold-location admission is ~0.9–1.3 s vs 16–21 ms warm.

How did you verify your code works?

  • New packages/tui/test/session-interrupt-starting.test.tsx has two deterministic cases:
    • a successful inbox cancel restores the text;
    • a promoted prompt whose inbox cancel does not land is taken back, asserted through the request sequence: interrupt, wait, revert.stage, revert.commit.
  • Ran 10× locally with no timeouts.
  • bun test test/session-interrupt-starting.test.tsx test/session-interrupt-undo.test.tsx test/cli/tui/prompt-interrupt-status.test.tsx passes.
  • bun run check passes (37/37).

Screenshots / recordings

Not a visual redesign; the footer changes from the location label to [spinner] esc cancel and then aborting….

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Refs #55273 #53653 #42960


Added by the opencode bot (AI-generated).

Fixes #53652

Related: #55273, #53653

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Nov 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works. Built from source with the simulated model: on the base, Esc does nothing during first-prompt preparation and the reply streams in. With this PR the footer shows esc cancel right away, and one Esc stops the run. session-interrupt-undo and prompt-interrupt-status pass, and tsc -b in packages/tui passes.

The public API is unchanged: TUI-only, using the existing session.interrupt and session.inbox.cancel endpoints.

Things to fix:

  • New test is flaky. session-interrupt-starting.test.tsx timed out in waitForFrame in 2 of 8 local runs. Please make it deterministic, or cut it down to the core case: one Esc on a starting row sends interrupt?resume=false and cancels the row.
  • Leftover cancel intent can stop unrelated runs. See the inline comment.
  • aborting… can get stuck. See the inline comment.
  • The cancelled text is lost. The cancel path skips the restore, so the prompt doesn't come back to the composer. That's at odds with the take-back reasoning from #65505, where stopping early usually means the user wants to reword. Putting the cancelled text back in the composer would match it.

Fixes #53652
Related: #55273, #53653

Before

After

// the intent and interrupt again once the server starts it.
createEffect(
on(
() => (status() === "running" ? store.cancel : undefined),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cancel intent lasts up to 30 s and is only cleared by a typed, non-empty submit. Any run that starts in the session in that time gets interrupted by this effect. That includes an empty-Enter promotion of a queued prompt (onEmptySubmit never reaches the clearing branch), a prompt from another client, or a queued item. The intent only needs to catch the execution that the cancelled inbox row would start. Could you clear it on the first execution.started/running transition, or tie it to the inbox IDs being cancelled, instead of a time window?

aborting is now also set on the normal double-Esc path, but it's only cleared when the session goes idle with nothing starting. interrupt() keeps using resume: true, which can wake a pending steer and start the next execution. If status never reports idle in between, the footer stays on aborting… for that new run and hides esc interrupt. Clearing it when the interrupted execution ends (e.g. on session.execution.interrupted, or on any running→idle edge) would avoid that.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@StAr1e

StAr1e commented Nov 28, 2026

Copy link
Copy Markdown
Author

Addressed the review in 2a54d3b:

  • The cancel intent is now tied to the inbox IDs being cancelled, not a 30 s window. It is dropped when those rows are cancelled and spent on the first running transition, so a later run in the session is not interrupted. The second test starts a later run after the intent is spent and asserts no interrupt is sent.
  • aborting… clears on session.execution.interrupted, so a resumed successor run does not leave it stuck.
  • A successful session.inbox.cancelled restores the prompt text and attachments into the composer when no new draft exists.
  • Split the test into two deterministic cases: a successful cancel that restores the text, and a raced start that interrupts once and then leaves a later run alone. Ran 10× locally with no timeouts.
  • bun run check passes (37/37).

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both points from my earlier thread are fixed: the cancel intent is now tied to the cancelled inbox rows instead of a 30 s window, and aborting… clears on session.execution.interrupted. The new test passed 6/6 local runs (it was flaky before). session-interrupt-undo, prompt-interrupt-status and tsc -b in packages/tui pass.

The public API is unchanged.

One gap, from driving the TUI against a real local server with the simulated model: one Esc during preparation does stop the run. But the composer stays empty, so the text restore in the description doesn't happen, and the prompt stays in the transcript. When I then sent another prompt, the model got both of them in one turn. The new test mocks the inbox DELETE and the session.inbox.cancelled event, so it doesn't cover this. Details are in the inline comment.

Before

After


// Stop a run that has not started yet without resuming the input it was about to deliver, then drop the
// inbox rows that are still waiting so they do not run on the next wake.
async function cancelStarting(sessionID: string, ids: string[]) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Against a real local server (simulated model), one Esc here stops the run, but the cancelled prompt isn't removed or restored. hello there stays in the transcript, the composer stays empty, and the next prompt I sent went to the model together with it in one turn. startingEntries only picks rows that already have a projected user message, so the row has likely been promoted by the time Esc lands. inbox.cancel then fails, the .catch(() => undefined) hides it, session.inbox.cancelled never arrives, and nothing is restored.

It also leaves cancelling/store.cancel set until the next running transition. A typed submit clears it, but an empty-Enter promotion of a queued prompt or a run started from another client would still be interrupted.

Could the cancel handle a failed inbox.cancel? For example: restore the text from the captured prompt anyway, clear the intent, and tell the user the prompt was already sent. Or move the cancel to the server side (#55273). A test against the real inbox, not a mocked DELETE and event, would catch this.

@StAr1e
StAr1e force-pushed the interrupt-starting-prompt branch 2 times, most recently from 6ffd65f to b33221b Compare November 28, 2026 07:32
@StAr1e

StAr1e commented Nov 28, 2026

Copy link
Copy Markdown
Author

Added the promoted-row path in b33221b:

  • cancelStarting now uses the existing take-back (interrupt with restore) when the run is interrupted, so a prompt promoted before the cancel lands is reverted and its text returns to the composer. session.inbox.cancel still handles rows that are still pending, with the captured prompt restored on session.inbox.cancelled.
  • The test now simulates the real race: session.inbox.delivered before execution.started, a no-op inbox.cancel with no session.inbox.cancelled, and a revert.committed event. It asserts the transcript row is gone, the text is back in the composer, and a later run is not interrupted.
  • 10/10 local runs; the related tests and bun run check pass.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The take-back from this commit doesn't happen in the real flow, and the new test mostly fails.

Driven against a real local server with the simulated model: one Esc during esc cancel still stops the session. But the run had already started, so an interrupted partial reply shows up. The composer stays empty and the prompt stays in the transcript, so the next prompt is sent with it still in context. The inline comment explains why the take-back doesn't kick in.

session-interrupt-starting.test.tsx: the second case, "a run that starts after a cancel is interrupted once", failed 7 of 8 local runs (waitForFrame timeout). It also mocks the inbox, message, revert and event flow so heavily that it passes or fails on the mock's ordering, not the server's. A smaller test of the one-Esc cancel, plus a real-server check of the promoted case, would be more useful.

session-interrupt-undo, prompt-interrupt-status and tsc -b in packages/tui pass.

The public API is unchanged.

The earlier 30 s intent window and the stuck aborting… are fixed.

Before

After

// the cancel landed, the interrupt takes it back and restores the composer text.
async function cancelStarting(sessionID: string, ids: string[]) {
setStore("aborting", true)
await interrupt(sessionID, { resume: false })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropping restore: false alone doesn't give a take-back on the first press. interrupt() computes takeBack from data.session.pending.list(sessionID).length === 0, but starting() is only true while the row is still in that list. So the Esc press always takes the plain interrupt-then-cancel path.

Take-back can only happen on the intent's second call, after the run has started, and only if the model hasn't answered with text yet. That's a race. Against a real local server with the simulated model, I got a partial · interrupted reply, an empty composer, and the prompt left in history, sent along with the next prompt.

The cancel path needs to restore the captured prompt (from cancelling) itself when the inbox cancel doesn't land. It also needs to remove the promoted message, via revert, without relying on the before-first-answer heuristic. Otherwise this belongs on the server (#55273).

@StAr1e
StAr1e force-pushed the interrupt-starting-prompt branch from b33221b to b955f7a Compare November 28, 2026 07:52
@StAr1e

StAr1e commented Nov 28, 2026

Copy link
Copy Markdown
Author

Reworked the promoted-row path in b955f7a:

  • cancelStarting no longer relies on interrupt()'s take-back heuristic. When the interrupt reports an active execution, it waits for idle, finds the promoted message, reverts it (including any partial reply), and restores the captured prompt. The before-first-answer heuristic remains only for the normal double-Esc path.
  • If the interrupt reports nothing running, it cancels the still-pending inbox rows; the captured prompt is restored on session.inbox.cancelled.
  • Simplified the second test to assert the request sequence (interrupt, wait, revert.stage, revert.commit) after a no-op inbox cancel, instead of frame timing. The one-Esc cancel test keeps the frame assertion.
  • 10/10 local runs; related tests and bun run check pass.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works. Against a local server built from source with the simulated model, using #66942's slow-preparation script: on the base, Esc does nothing and the run starts. With this PR, one Esc shows aborting…, no model request is made, the transcript stays empty, and the prompt text returns to the composer. When Esc lands just as the run starts, the prompt is now also taken back and restored instead of staying in history. The new test passed 4/4 local runs. session-interrupt-undo, prompt-interrupt-status and tsc -b in packages/tui pass.

The public API is unchanged.

The earlier points (time-window intent, stuck aborting…, prompt not removed or restored) are addressed. One small remaining edge case is noted inline.

Before

After

Screenshot

if (!prompt) return
const restored = await restorePrompt(prompt)
if (message) {
await client.api.session.revert.stage({ sessionID, messageID: message.id, files: false })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This take-back now runs even when the model already produced output. Reverting with files: false means that if the run got as far as a tool call that edited files before the interrupt landed, those edits stay on disk while the turn disappears from the transcript. It's unlikely in the starting window, but you could skip the take-back (or keep the plain interrupt) when the reverted turn contains a tool part. Or revert files too in that case.

On a cold location the prompt is admitted before the run starts, so the session still reports idle, the interrupt command was disabled, and Esc did nothing. Show the preparation as busy with an esc cancel hint, cancel on one press, and keep the intent so a run that starts anyway is interrupted.

Also show aborting until the session settles and allow the session-level interrupt when the composer is not focused.
@StAr1e
StAr1e force-pushed the interrupt-starting-prompt branch from b955f7a to 6157192 Compare November 28, 2026 08:08
@StAr1e

StAr1e commented Nov 28, 2026

Copy link
Copy Markdown
Author

Handled the tool-edit edge case in 6157192: the cancel take-back now checks the reverted range for a tool part. If one is present, it keeps the plain interrupt and does not revert or restore, so file edits made by the tool are not left on disk with the turn removed. Text-only turns still take back.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tool-call case is now handled: when the turn already contains a tool call, the cancel keeps the plain interrupt instead of reverting the transcript and leaving file edits behind. Re-checked with #66942's slow-preparation script against a local server built from source and the simulated model: one Esc cancels, no model request is made, and the prompt returns to the composer. The new tests, session-interrupt-undo, prompt-interrupt-status and tsc -b in packages/tui pass.

The public API is unchanged.

Before

After

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tui: Esc does nothing while the first prompt is still being prepared TUI: no feedback between the second Escape and the session going idle

2 participants