Repository navigation
ref(openai-agents): Use first class agent hooks when available - #7029
alexander-alderman-webb wants to merge 40 commits into
Conversation
Coverage Results 📊✅ 139228 passed | ⏭️ 7857 skipped | Total: 147085 | Pass Rate: 94.66% | Execution Time: 482m 7s 📊 Comparison with Base Branch
➖ Removed Tests (1)View removed tests
All tests are passing successfully. ✅ Patch coverage is 88.57% (93 of 105 changed executable lines covered; target 80%). Changed files with executable lines (4)
Generated by Coverage Action |
| if use_run_hooks: | ||
| run_hooks = kwargs.get("hooks") | ||
| if run_hooks is not None: |
There was a problem hiding this comment.
To confirm my understanding of what's happening here - are run hooks something that are passed in by users (which is why they could potentially be None)?
| and `on_tool_end()` | ||
| Responsible for creating Execute Tool and Invoke Agent spans. Both | ||
| spans can also be closed by hooks, and Execute Tool spans are exclusively | ||
| closed in the hook. |
There was a problem hiding this comment.
When you say "the hook" here - is there are specific hook that you're referring to? Or do you mean this as just a generalized "run hook"?
bbc2001 to
67b4d2b
Compare
67b4d2b to
2066975
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 2066975. Configure here.
| context: "AgentHookContext[TContext]", | ||
| agent: "Agent[TContext]", | ||
| ) -> "None": | ||
| self._sentry_invoke_agent_span = invoke_agent_span(agent, context.turn_input) |
There was a problem hiding this comment.
Shared hooks clobber concurrent agent spans
Medium Severity
The invoke-agent span is kept as a single attribute on the shared _SentryRunHooks instance created when user hooks are patched. Concurrent Runner.run calls that reuse the same RunHooks overwrite _sentry_invoke_agent_span, so one run can close the other run's span or leave a span unfinished. The previous design stored the span on the per-run context_wrapper.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 2066975. Configure here.
| ) -> "SingleStepResult": | ||
| return await _run_single_turn( | ||
| run_loop.run_single_turn, *args, **kwargs | ||
| run_loop.run_single_turn, use_run_hooks, *args, **kwargs |
There was a problem hiding this comment.
Agent hooks gated on wrong version
Medium Severity
use_run_hooks turns on at openai-agents 0.3.2, but on_agent_start reads context.turn_input, which exists only on AgentHookContext from 0.6.5. On 0.3.2–0.6.4 that access fails inside capture_internal_exceptions, the monkey-patch span path is skipped, and invoke-agent spans are silently dropped.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 2066975. Configure here.


Description
Add
on_agent_start(),on_agent_end()andon_handoff()hooks.Unlike tool hooks, the Invoke Agent span is stored on the
RunHooksinstance.If the hooks are active, start and finish Invoke Agent spans in the hooks in successful executions. If an exception bubbles up,
on_agent_end()does not run. Modify monkey-patches so that the span stored on theRunHooksinstance is finished if the hooks are present.Make the signatures of
invoke_agent_span(),update_invoke_agent_span()and_maybe_start_agent_span()more minimal so that they can be re-used in theon_agent_start()andon_agent_end()hooks.Issues
Closes #6989
Reminders
uv run ruff.feat:,fix:,ref:,meta:)