Repository navigation
fix(python): run ContextOffloader result rewrite after user AfterToolCall hooks - #5601
Conversation
|
@strandly-the-agent update the pr. I don't actually want any component in the SDK to call SDK last or first. bc if we need sth to come afterwards, we'll need to move. I'd rather leave some gaps, so we can update later without changing bunch of constants. Check the ordering we have so far, and come up with a better number |
…Call hooks Register _handle_tool_result at HookOrder.SDK_LAST so hooks passed via hooks= (audit, DLP) observe the full tool result before it is replaced with the offloaded preview. Fixes strands-agents#5596
…XT_OFFLOAD, not SDK_LAST
ff1972a to
842a73a
Compare
|
Updated (842a73a): added Why 70Current scale: On FYI the other existing Rebased on |
|
@strandly-the-agent I don't want to add the number in hook orders. we will deprecate this soon, and move to context manager, and i dont want people to get confused. let's take this as a bugfix and coninue imo |
…with a plain order
|
Done (0717df0): |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Assessment: Approve Clean, well-scoped bug fix. I traced the ordering mechanism and confirmed the fix is correct and robust: What I verified
One optional, non-blocking note: Nice, tightly-scoped fix with regression tests that actually pin the ordering contract. |
…Call hooks (strands-agents#5601) Co-authored-by: strandly-the-agent <strandly-the-agent@users.noreply.github.com>
Requested by: @ekumar96 — cut PRs for the ContextOffloader / SummarizingConversationManager fixes blocking the AgentCore Harness launch (Slack, 2026-10-27).
Human Overview
Context offloader now runs after default hooks, so they don't see offloaded tool result. This is an outdated behavior that doesn't happen on context manager.
Description
ContextOffloader._handle_tool_resultreplacedevent.resultwith the preview before any hook passed viahooks=ran: plugins register afterhooks=, andAfterToolCallEventreverses registration order within aHookOrdergroup, so the offloader always ran first and audit/DLP hooks only saw the preview.The handler is no longer
@hook-discovered;init_agentregisters it withagent.add_hook(..., AfterToolCallEvent, order=70). Group order is ascending and reversal only applies within a group, so the rewrite now runs after everyDEFAULThook. Permkmeral: notSDK_LAST(SDK components shouldn't occupy the ends of the scale) and no newHookOrderconstant (hook ordering is slated for deprecation in favour of the context manager). 70 sits betweenMODEL_ROUTING(50) andINTERVENTION_INPUT(90) with room on both sides. No public API change.Related Issues
Fixes #5596
Overlaps with #5599 (
MuonChaser, usesSDK_LAST, opened independently) — maintainers can keep whichever they prefer.Documentation PR
None needed (docs don't describe hook ordering).
Type of Change
Bug fix
Testing
New:
test_init_agent_registers_tool_result_hook_last(asserts the order is strictly betweenDEFAULTandSDK_LAST) andtest_user_after_tool_call_hooks_see_full_result_before_rewrite(realAgent+hooks=callback + offloader; the hook sees the 2,000-char result, the stored message holds the preview). Both fail onmain.pytest tests/strands/vended_plugins/context_offloader tests/strands/hooks tests/strands/agent/test_agent_delegation.py: 264 passed (rebased on currentmain). ruff check/format clean; mypy clean onstrands.hooks+strands.vended_plugins.context_offloader.I ran
hatch run prepare(sandbox has no hatch; ran the equivalent ruff/mypy/pytest directly)Additional Details
Independent review (fresh-context reviewer, round 1) and ledger
Verdict: approve. Checked: no other
AfterToolCallEventregistration aboveDEFAULT; interventions (INTERVENTION_OUTPUT, -90) still run before the offloader; delegation's_on_after_tool_callreads onlystatus; no double registration (oneinit_agentper agent, plugin names unique). The laterSDK_LAST→70change doesn't alter any of these relationships.rather than via @hook) — fixed.event.retry— not taken; unrelated to this bug.vended-plugins/context-offloader/plugin.tshas the same ordering bug — tracked in the [PORT] issue.Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.