Skip to content

Conversation

habema
Copy link
Contributor

@habema habema commented Oct 14, 2025

Resolves #1895

Solves the problem where canceling mid-turn discards partial work by adding an optional mode that waits for turn completion.

Implementation

API change (backward compatible):

def cancel(self, mode: Literal["immediate", "after_turn"] = "immediate")

Default is "immediate" (existing behavior). New "after_turn" mode sets a flag and lets the background task finish the current turn.

Core changes:

  • result.py: Add _cancel_mode field and mode parameter to cancel()
  • run.py: Add 3 checkpoint checks in streaming loop at top of turn loop, after handoff, and after tool execution

Checkpoints are placed after session saves, so when they trigger, the turn is already persisted.

How it works:

  1. User calls cancel(mode="after_turn") - sets flag, doesn't clean up
  2. Background task checks flag at next checkpoint
  3. Marks complete and exits loop (cleanup happens in finally block as usual)

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

@seratch seratch added enhancement New feature or request feature:core labels Oct 15, 2025
Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

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

LGTM @rm-openai let me know if you have any thoughts on this

@seratch seratch requested a review from rm-openai October 15, 2025 08:38
@seratch seratch merged commit 3b7aed7 into openai:main Oct 17, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streaming runs can't be canceled gracefully

2 participants