Skip to content

Tool approval is not honored on resume when Runner.run is given a context - #4245

Merged
seratch merged 13 commits into
openai:mainfrom
ojassharma7:autocontrib/issue-4244
Aug 7, 2026
Merged

Tool approval is not honored on resume when Runner.run is given a context#4245
seratch merged 13 commits into
openai:mainfrom
ojassharma7:autocontrib/issue-4244

Conversation

@ojassharma7

Copy link
Copy Markdown
Contributor

Fixes #4244.

What changed

  • src/agents/run_internal/agent_runner_helpers.py
  • tests/test_run_state.py

Verification

The project's own test suite was run before and after this change; it introduces no new test failures or lint violations.

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

Copy link
Copy Markdown

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.

Reviewed commit: bfa6fb8b80

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/run_internal/agent_runner_helpers.py Outdated
@seratch seratch added this to the 0.20.x milestone Aug 6, 2026

@seratch seratch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the contribution. The underlying issue is valid, but please revise the context-resolution change so the restored RunContextWrapper remains authoritative and only its application context value is replaced. Copying selected private fields into a new wrapper omits persisted metadata such as tool_input and will drift as additional run-owned state is added.

Please also add a regression covering the issue's serialized to_json() / from_json() resume path, plus the equivalent Runner.run_streamed() path. Both should prove that the approved call executes, the supplied application context is visible, and SDK-managed wrapper state survives the override.

Also, getting 👍 from Codex's PR review is required before merging in this repo, so please continue resolving all the review comments by Codex here (you don't need to mention codex; Codex automatically reviews all commits for this repo).

ojassharma7 and others added 2 commits August 6, 2026 11:13
Replace only the application context value on the restored wrapper so
approvals, usage, turn_input, and tool_input survive context= on resume.
Add JSON and streamed regression coverage for issue openai#4244.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ojassharma7

Copy link
Copy Markdown
Contributor Author

Thanks @seratch — revised in 6a0c0ae:

  • resolve_resumed_context now keeps the restored RunContextWrapper and only replaces its application .context value (no field-by-field copy onto a new wrapper).
  • Added regressions for the to_json() / from_json() resume path and Runner.run_streamed(), asserting the approved tool runs, the override context is visible, and wrapper state (tool_input) survives.

Add a unit test that proves resolve_resumed_context keeps the restored
wrapper identity and run-owned state, and share HITL setup across the
JSON and streamed regressions with approvals-identity assertions.

Co-authored-by: Cursor <cursoragent@cursor.com>

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

Copy link
Copy Markdown

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.

Reviewed commit: d05ddd9826

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/run_internal/agent_runner_helpers.py
When Runner.run resumes with context=, also update cached Agent.as_tool()
RunState application contexts for the resume scope so nested tools see the
override while keeping nested approvals and tool_input intact.

Co-authored-by: Cursor <cursoragent@cursor.com>

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

Copy link
Copy Markdown

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.

Reviewed commit: 9c042f22d7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/agent_tool_state.py Outdated
Walk only the resumed RunState's pending function tool calls instead of
every unscoped agent-tool cache entry, so concurrent live interruptions
cannot leak a context= override into each other.

Co-authored-by: Cursor <cursoragent@cursor.com>

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

Copy link
Copy Markdown

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.

Reviewed commit: d0ce12a92a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/agent_tool_state.py Outdated
Comment thread src/agents/agent_tool_state.py Outdated
ojassharma7 and others added 2 commits August 6, 2026 12:09
Recurse through live Agent.as_tool() wrappers so A→B→C overrides reach C,
only consider _is_agent_tool function runs, and avoid unscoped signature
fallback so ordinary functions cannot overwrite unrelated nested context.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

@seratch seratch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for continuing to work through the feedback. The underlying issue is valid, and keeping the restored RunContextWrapper authoritative is the correct direction. However, the current head has expanded into recursive traversal and mutation of the shared agent-tool cache.

Please simplify this to one context-resolution path: keep the restored wrapper and replace only its application .context; when a pending Agent.as_tool() run is actually resumed, pass the current parent application context into its nested Runner.run() or Runner.run_streamed() call so the same resolver applies there. Please remove apply_application_context_to_agent_tool_states and the synthetic cache-propagation tests.

Keep the JSON round-trip and streaming regressions, and add an end-to-end nested Agent.as_tool() regression proving that the new application context is visible while nested approvals and tool_input remain owned by the restored wrapper.

ojassharma7 and others added 2 commits August 6, 2026 19:57
Keep a single resolve_resumed_context path that only replaces the restored
wrapper's application context, and pass the parent application context into
nested Runner.run/run_streamed on Agent.as_tool() resume instead of mutating
the shared agent-tool cache.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ojassharma7

Copy link
Copy Markdown
Contributor Author

Thanks @seratch — simplified as requested in the latest commits.

  • resolve_resumed_context only keeps the restored wrapper and replaces its application .context
  • Removed apply_application_context_to_agent_tool_states and the synthetic cache-propagation tests
  • On pending Agent.as_tool() resume, nested Runner.run / Runner.run_streamed now receive the current parent application context so the same resolver applies
  • Kept the JSON + streamed regressions, and added an end-to-end nested Agent.as_tool() regression for context override + wrapper-owned approvals/tool_input

@seratch seratch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the update. The latest head now has the right ownership model: it keeps the restored RunContextWrapper authoritative, replaces only the application context, and routes nested Agent.as_tool() resumes through the same resolver. The JSON round-trip, streaming, and nested regressions cover the required behavior, and CI is green.

One small repository-style fix remains before re-review: in the resolve_resumed_context docstring, replace the Unicode ellipsis after tool input, with three ASCII dots (tool input, ...) as required by AGENTS.md. After that change, this looks ready for final review.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ojassharma7

ojassharma7 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, updated the resolve_resumed_context docstring to use ASCII ... instead of the Unicode ellipsis.

@seratch

seratch commented Aug 7, 2026

Copy link
Copy Markdown
Member

@ojassharma7 oops, can you resolve the conflicts?

Co-authored-by: Cursor <cursoragent@cursor.com>
@ojassharma7

ojassharma7 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Resolved, merged latest main and fixed the import conflict in tests/test_run_state.py (kept both the new error-handler imports and resolve_resumed_context).

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

Copy link
Copy Markdown

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.

Reviewed commit: b95b07708a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/agent.py

@seratch seratch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmm, codex pointed a new issue out. Can you resolve it as well?

When resuming an approved nested agent-tool interruption, keep the restored
wrapper's approvals/tool_input but point its Usage back at the parent
ToolContext accumulator so post-resume nested turns are not undercounted.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ojassharma7

Copy link
Copy Markdown
Contributor Author

@seratch addressed the Codex usage note as well — nested Agent.as_tool() resume now reattaches the restored nested wrapper's usage to the parent ToolContext accumulator so post-resume nested turns are counted on the parent.

@seratch
seratch merged commit 00c9d26 into openai:main Aug 7, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool approval is not honored on resume when Runner.run is given a context

2 participants