fix(event-loop): ensure all cycle metrics include end time and duration#1903
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes inaccurate event-loop cycle timing metrics when a cycle ends by recursing into another event_loop_cycle (e.g., after tool execution), ensuring cycle traces get end_time/duration recorded so aggregate response metrics (like total/average duration) are correct for multi-cycle invocations.
Changes:
- End tool-execution cycles’ metrics before deciding to stop or recurse, ensuring recursive paths still record
end_timeandduration. - Add a regression test covering the recursive-call scenario and asserting
end_cycleis invoked for both the tool cycle and the subsequent text cycle.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/strands/event_loop/event_loop.py |
Moves end_cycle so tool cycles are always finalized before recursion/stop logic. |
tests/strands/event_loop/test_event_loop.py |
Adds an async test to verify cycle-metrics finalization occurs in recursive tool-use flows. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hi @stephentreacy , thanks for raising this. After you address the failing workflows, I am happy to approve and merge this change. |
8cdc841 to
dcc5e75
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Assessment: Approve Clean fix that correctly addresses the cycle metrics regression by moving Review Notes
Good fix with clear before/after examples in the PR description. |
Description
Event loop cycle metrics (
end_timeandduration) were not being recorded for cycles that ended with a recursive call. This causedtotal_durationandaverage_cycle_timein the response metrics to be inaccurate, making it difficult to monitor agent performance in multi-cycle invocations.Response metric traces would like like this, where
total_durationis only including the Cycle 2 duration:{ "total_cycles": 2, "total_duration": 2.97, "traces": [ { "name": "Cycle 1", "start_time": 1772822921.485906, "end_time": null, "duration": null, "children": [ { "name": "stream_messages", "start_time": 1772822921.486195, "end_time": 1772822924.446701, "duration": 2.96 }, { "name": "Tool: tool_name", "start_time": 1772822924.447283, "end_time": 1772822931.842052, "duration": 7.39 }, { "name": "Recursive call", "start_time": 1772822931.842254, "end_time": 1772822934.810739, "duration": 2.97 } ] }, { "name": "Cycle 2", "start_time": 1772822931.842278, "end_time": 1772822934.810579, "duration": 2.97, "children": [ { "name": "stream_messages", "start_time": 1772822931.842385, "end_time": 1772822934.810402, "duration": 2.97 } ] } ] }Related Issues
Documentation PR
Type of Change
Bug fix
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepareTested locally with recursive call scenarios, verified cycle metrics now include accurate
end_timeanddurationvaluesCycle 1 duration includes all child durations except the recursive call.
{ "total_cycles": 2, "total_duration": 14.80, "traces": [ { "name": "Cycle 1", "start_time": 1772825264.482746, "end_time": 1772825275.644092, "duration": 11.16, "children": [ { "name": "stream_messages", "start_time": 1772825264.483011, "end_time": 1772825267.999054, "duration": 3.52 }, { "name": "Tool: tool_name", "start_time": 1772825268.000930, "end_time": 1772825275.643905, "duration": 7.64 }, { "name": "Recursive call", "start_time": 1772825275.644141, "end_time": 1772825279.281506, "duration": 3.64 } ] }, { "name": "Cycle 2", "start_time": 1772825275.644161, "end_time": 1772825279.281480, "duration": 3.64, "children": [ { "name": "stream_messages", "start_time": 1772825275.644274, "end_time": 1772825279.281442, "duration": 3.64 } ] } ] }Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.