alanhuangyoo 96d6ff3aca
fix(agents): keep the token budget across goal continuations of a run (#5410)
* fix(agents): keep the token budget across goal continuations of a run

TokenBudgetMiddleware cleared its usage in after_agent, and before_agent
marks every existing message as seen. A Gateway run re-enters the graph
for hidden goal continuations under the same run_id, so each
continuation started from zero and could spend another full budget,
even after the user turn had already hit the hard stop.

Keep the run's usage and warning state across graph entries, as
LoopDetectionMiddleware does since #5344. Only the per-message seen map
is dropped, and before_agent rebuilds it. Invocations without a run_id
in the context still clear everything.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(agents): normalize token budget run identity and test cleanup

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
2026-09-14 10:41:22 +08:00
..