Cancelling while the model answers tool results erases the prompt and the completed tool rounds from history, unlike cancelling during a tool
What happened?
A prompt that uses tools sends several model requests: the prompt, then one request per round of tool results. If the user cancels while one of these requests is in flight or streaming, GeminiChat rolls history back to its length before the user's prompt. That removes the prompt, the model's function calls and the function responses, even for tools that already ran and may have changed files. If the same cancel arrives while a tool is running, the prompt and the completed tool rounds are kept. More recent code states the second behaviour as the rule.
What is certain and what is inferred
-
Certain: The full rollback is deliberate. It was added in 783f6cb ("rollback entire multi-turn request on cancellation or abort", #28801) with a test (
geminiChat.test.tsL1708-L1789). -
Inferred: That it is no longer the intended behaviour. It contradicts the rule written later for the same user action in 249d51c (#29612) (
useGeminiStream.tsL2292-L2295), and the doc comment ofdiscardTrailingUnansweredToolCallTurn(geminiChat.tsL1318-L1325):The originating user prompt and any tool rounds that already completed (and may have changed files) stay in history.
-
If the full rollback is still intended, the bug is the inconsistency between the two cases, and the behaviour is worth documenting. Nothing in the docs says that a cancel forgets the turn. The explicit way to do that is
/rewind(docs/cli/rewind.mdL1-L12).
When it happens
The user cancels (Esc in the interactive CLI, or session/cancel from an ACP client) while the request carrying tool results is in flight or its reply is streaming. At that point, a file edit or a command from the same prompt has already completed.
What the code does
-
The rollback point is recorded on the first send of a prompt ID (
geminiChat.tsL505-L517). Tool-result continuations reuse that prompt ID:- in ACP, one ID covers the whole prompt (
acpSession.tsL326, L433-L438); - the interactive UI submits continuations under the original prompt ID (
useGeminiStream.tsL2351-L2362).
- in ACP, one ID covers the whole prompt (
-
When a request is aborted, the
finallyofstreamWithRetriesrolls history back to that point. It also rewrites the session file (geminiChat.tsL812-L832). In ACP mode, the cancel aborts the pending prompt (acpSession.tsL206-L213), and the stream loop then returnscancelled(L440-L443). -
A cancel while tools run takes another path:
- The interactive UI discards only the unanswered model call turn (
useGeminiStream.tsL2292-L2295 →geminiChat.tsL1318-L1343). - ACP returns before sending the responses (
acpSession.tsL421-L423), and the next prompt closes the open calls with synthetic responses (geminiChat.tsL533-L536).
In both cases, the prompt and the completed rounds stay.
- The interactive UI discards only the unanswered model call turn (
Impact
- The model loses the user's request and the results of tools that already ran, possibly including file changes. Meanwhile, the UI or ACP client still shows the whole turn. A follow-up such as "continue" or "do X instead" therefore reaches a model with no record of the task or of what was changed.
- The same user action keeps or erases the turn depending on whether the cancel lands during a tool or during a model request.
- The rollback is written to the session file, so a resumed session lacks the turn as well.
Possible fix
- When an aborted send is a continuation (a function-response send), handle it the way a failed continuation is already handled (
geminiChat.tsL833-L839): keep the committed turns, and let the next send add the "interrupted" model turn (L904-L934). - Keep the full rollback only for an abort of the prompt's first request, where nothing has run yet.
What did you expect to happen?
Cancelling behaves like the rule in useGeminiStream.ts L2292-L2294. The user's prompt and any tool rounds that already completed stay in history, and only the interrupted model response is dropped. This should hold whether the cancel lands during a tool or during a model request.
Client information
Analysed from source at commit 363a131 on main (2026-10-26). The package version at that commit is 0.67.0-nightly.20261020.gb041de017. Affects ACP mode and, by the same core code, the interactive CLI. Not platform-specific.
Client Information
> /about
# paste output here (remove the email address)
Login information
Any. The code path does not depend on the authentication method.
Anything else we need to know?
If the full rollback is the intended design, please say so. In that case, this can become a documentation request: what a cancel does to history and to the session file.
Thanks for the detailed analysis! I've opened #30017 with a fix following your suggested approach:
- The full rollback in streamWithRetries now applies only to an abort of the prompt's first request (where nothing has run yet).
- An aborted function-response continuation keeps the committed turns — the user's prompt, the model's function calls, and the function responses — matching how a failed continuation is already handled, and consistent with the terminal-user-turn invariant from #29612.
- The trailing unanswered tool response is closed with the synthetic INTERRUPTED_RESPONSE_PLACEHOLDER model turn on the next send, the same repair used for the other paths.
- The existing multi-turn abort test now asserts the new behavior (history keeps the prompt + completed rounds, and a follow-up prompt adds the interrupted turn); first-request abort still fully rolls back. Happy to adjust if maintainers intended the full rollback as the design — in that case the inconsistency between the two cancel paths would still need documenting.
Hi, i want to contribute to this can you please assign me this
This report's tool-result-continuation cancellation case overlaps #29799, which is closed and unmerged at f141d8362a0188b5dc50f3f95b667bef7399c2a0. I also see the existing #30017 contribution described above; its current head is 3d429968a9adfc8233964ed3cbdb18ac9cf825af and it remains open and unmerged.
#30017 addresses the narrow continuation case and intentionally keeps full rollback for an aborted first request. #29799 also preserves the initial prompt on cancellation or quota, keeps real JSONL history through normal quit/signout/resume, cancels retry waits promptly, and handles Escape in the quota dialog. Those broader semantics are not established by this issue alone. In particular, whether a first-request abort should retain its prompt needs an explicit maintainer decision.
Could a maintainer confirm the intended boundaries and whether any of that broader scope is wanted alongside #30017? I will keep #29799 closed while the issue and contribution eligibility are considered, and request reopening only after explicit maintainer acceptance. This is not a request to replace #30017 or claim this issue's full resolution; separate ACP retry-notification requirements are outside the proposed scope.
A data point on what this costs after a resume and in the UI, from Gemini CLI 0.70.0 against a scripted model server (the model asks for one shell command, echo one >> build.log; its reply to the request carrying that command's result is held open; the CLI is stopped while it waits).
- Headless (
gemini -p … --yolo), stopped by SIGINT, by Ctrl-C typed into its terminal, by SIGTERM (to the process or its group) or by SIGHUP, thengemini --resume latest -p Continue.: the resumed request holds neither the user's prompt nor the shell call and its result, 15 of 15 runs (3 per stop). After SIGKILL the session keeps both, since no handler runs. - Interactive: Esc while it shows "Thinking…" after the shell call. The screen keeps "✓ Shell echo one >> build.log" above "Request cancelled.", but the next request (after typing "Continue.") again has neither the prompt nor the call, 3 of 3. The person sees the command done; the model has no trace of it or of the task, so asking again repeats it.
- With real models (six open-weight families through an OpenAI-compatible endpoint: DeepSeek V4 Pro, Kimi K3.1 Pro, GLM-5.3, MiMo V2.6 Flash, MiniMax M3.1 Flash, Qwen3.8 Flash; three runs each), stopped by SIGINT in the same place and resumed with "Continue.": no model found a task to continue or wrote anything (18 of 18); 14 of the 15 that finished that turn had read build.log and seen the line the command wrote. Sent the same request again, every model ran
echo one >> build.logagain as its first step (18 of 18), so the line is there twice.
Keeping the committed turns on an aborted function-response continuation, as #30017 proposed, would avoid all three.