alanhuangyoo 99902b7791
fix(agents): keep token budget signals for runs without a run_id (#5436)
* fix(agents): keep token budget signals for runs without a run_id

#5410 moved every invocation without a non-empty string run_id onto
str(id(runtime)). Two things break on that key:

- SubagentExecutor passes the parent's run_id, None when the parent run has
  none (LangGraph Server, direct create_deerflow_agent callers), and reads the
  stop reason back with that None. The hard stop stored it under the id string,
  so a token-capped subagent reported a clean completion to the lead.
- LangGraph gives each graph node its own Runtime wrapper, so the key changed
  between after_model and the next model call: the budget warning was never
  delivered, and each after_model counted every AIMessage in the thread.

Key those invocations by Runtime.control, as LoopDetectionMiddleware does,
release it in after_agent, and store the stop reason under the context run_id
as given.

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

* fix(agents): keep an active invocation's budget key when the anchor map is full

The fallback anchor map was FIFO, so with 1000 run_id-less invocations on a
shared instance an active one could lose its anchor mid-run and restart with a
fresh budget. Move the anchor to the end on every lookup, as loop detection
does, and note why execution_info.run_id is not consulted.

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

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 06:56:38 +08:00
..