Skip to content

Commit 6dc2e17

Browse files
committed
fix(core): carry the loop-iteration stamp in run options, not a contextvar
The contextvar marker leaked in two ways. Held across a streamed yield it bled into the caller's context, suppressing turn-scoped providers on an unrelated same-agent run while the stream was paused, and a reset from a different consuming task raised on the token. Keyed to the agent instance, it also swallowed the boundary flush of a nested loop on the same agent with its own session. Stamp the runs the loop drives through their options instead. Run options reach only the inner runs (they never enter the model request), a nested or concurrent run starts with fresh options and keeps its own turn, and there is no token to reset, so stream consumption is safe from any task.
1 parent 97bcea5 commit 6dc2e17

3 files changed

Lines changed: 131 additions & 68 deletions

File tree

‎python/packages/core/agent_framework/_agents.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import sys
88
from collections.abc import Awaitable, Callable, Mapping, MutableMapping, Sequence
99
from contextlib import AbstractAsyncContextManager, AsyncExitStack
10-
from contextvars import ContextVar
1110
from copy import deepcopy
1211
from functools import partial
1312
from itertools import chain
@@ -74,12 +73,14 @@
7473

7574
logger = logging.getLogger("agent_framework")
7675

77-
# Set by AgentLoopMiddleware to the looping agent while a loop iteration is
78-
# running, so providers scoped to the whole user turn
76+
# AgentLoopMiddleware stamps this key into the run options while a loop
77+
# iteration is running, so providers scoped to the whole user turn
7978
# (``after_run_once_per_turn``) skip their per-iteration ``after_run`` and only
80-
# fire once at the loop boundary. Keyed to the agent instance: a nested
81-
# ``agent.run()`` inside the iteration is a separate run, not a loop iteration.
82-
_LOOP_ITERATION_ACTIVE: ContextVar[Any] = ContextVar("agent_loop_iteration_active", default=None)
79+
# fire once at the loop boundary. It rides the run's options rather than a
80+
# context variable: options reach only the runs the loop itself drives, so a
81+
# nested ``agent.run()`` (fresh options, its own session) keeps its own turn,
82+
# and nothing leaks into the caller's context while a stream is paused.
83+
_LOOP_ITERATION_TOKEN_KEY = "_agent_loop_iteration"
8384

8485
if TYPE_CHECKING:
8586
ResponseModelBoundT = TypeVar("ResponseModelBoundT", bound=BaseModel)
@@ -558,7 +559,9 @@ async def _run_after_providers(
558559
per_service_call_history_required = self.require_per_service_call_history_persistence and any(
559560
isinstance(provider, HistoryProvider) for provider in self.context_providers
560561
)
561-
in_loop_iteration = _LOOP_ITERATION_ACTIVE.get() is self
562+
# The loop stamps the runs it drives via their options; anything else
563+
# (nested run, caller-side run while a stream is paused) is its own turn.
564+
in_loop_iteration = context.options.get(_LOOP_ITERATION_TOKEN_KEY) is not None
562565
for provider in reversed(self.context_providers):
563566
if per_service_call_history_required and isinstance(provider, HistoryProvider):
564567
continue

‎python/packages/core/agent_framework/_harness/_loop.py‎

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from pydantic import BaseModel, Field
3535
from typing_extensions import Self
3636

37-
from .._agents import _LOOP_ITERATION_ACTIVE
37+
from .._agents import _LOOP_ITERATION_TOKEN_KEY
3838
from .._feature_stage import ExperimentalFeature, experimental
3939
from .._middleware import AgentContext, AgentMiddleware, MiddlewareTermination
4040
from .._sessions import SessionContext
@@ -518,57 +518,45 @@ async def _process_non_streaming(
518518
aggregated: list[Message] = []
519519
aggregated_usage: UsageDetails | None = None
520520
final_result: AgentResponse | None = None
521-
while True:
522-
loop_token = _LOOP_ITERATION_ACTIVE.set(context.agent)
523-
try:
521+
stamped_options = dict(context.options) if context.options is not None else {}
522+
stamped_options[_LOOP_ITERATION_TOKEN_KEY] = object()
523+
context.options = stamped_options
524+
try:
525+
while True:
524526
await call_next()
525-
finally:
526-
_LOOP_ITERATION_ACTIVE.reset(loop_token)
527-
iteration += 1
528-
529-
result = context.result
530-
if not isinstance(result, AgentResponse):
531-
raise TypeError(
532-
"AgentLoopMiddleware expected an AgentResponse from a non-streaming run, "
533-
f"got {type(result).__name__}."
534-
)
527+
iteration += 1
535528

536-
final_result = result
537-
aggregated.extend(result.messages)
538-
if result.usage_details is not None:
539-
aggregated_usage = add_usage_details(aggregated_usage, result.usage_details)
540-
541-
# Escape hatch: if this iteration is asking for tool approval, stop and return the
542-
# response so the caller can approve, instead of continuing or injecting next_message.
543-
if self._has_pending_approval_request(result):
544-
break
545-
546-
messages_used = context.messages
547-
loop_kwargs = self._build_loop_kwargs(
548-
context=context,
549-
iteration=iteration,
550-
last_result=result,
551-
messages_used=messages_used,
552-
original_messages=original_messages,
553-
progress=progress,
554-
)
529+
result = context.result
530+
if not isinstance(result, AgentResponse):
531+
raise TypeError(
532+
"AgentLoopMiddleware expected an AgentResponse from a non-streaming run, "
533+
f"got {type(result).__name__}."
534+
)
555535

556-
work_iterations += 1
557-
# Decide whether to stop and capture any feedback from should_continue first, so the
558-
# feedback is available to both the progress and next-message callables this iteration.
559-
stop, feedback = await self._evaluate_stop(loop_kwargs, work_iterations)
560-
loop_kwargs = self._build_loop_kwargs(
561-
context=context,
562-
iteration=iteration,
563-
last_result=result,
564-
messages_used=messages_used,
565-
original_messages=original_messages,
566-
progress=progress,
567-
feedback=feedback,
568-
)
569-
# Capture this iteration's progress entry, then refresh loop_kwargs so the next-message
570-
# resolution sees the latest entry.
571-
if await self._record_progress(result, loop_kwargs, progress):
536+
final_result = result
537+
aggregated.extend(result.messages)
538+
if result.usage_details is not None:
539+
aggregated_usage = add_usage_details(aggregated_usage, result.usage_details)
540+
541+
# Escape hatch: if this iteration is asking for tool approval, stop and return the
542+
# response so the caller can approve, instead of continuing or injecting next_message.
543+
if self._has_pending_approval_request(result):
544+
break
545+
546+
messages_used = context.messages
547+
loop_kwargs = self._build_loop_kwargs(
548+
context=context,
549+
iteration=iteration,
550+
last_result=result,
551+
messages_used=messages_used,
552+
original_messages=original_messages,
553+
progress=progress,
554+
)
555+
556+
work_iterations += 1
557+
# Decide whether to stop and capture any feedback from should_continue first, so the
558+
# feedback is available to both the progress and next-message callables this iteration.
559+
stop, feedback = await self._evaluate_stop(loop_kwargs, work_iterations)
572560
loop_kwargs = self._build_loop_kwargs(
573561
context=context,
574562
iteration=iteration,
@@ -578,15 +566,29 @@ async def _process_non_streaming(
578566
progress=progress,
579567
feedback=feedback,
580568
)
581-
if stop:
582-
break
583-
if snapshot is not None and context.session is not None:
584-
# Reset the session to the pre-loop baseline so the next run starts fresh; only the
585-
# progress log (injected by _resolve_next_message) carries continuity forward.
586-
self._restore_session(context.session, snapshot)
587-
next_messages = await self._resolve_next_message(loop_kwargs, messages_used, original_messages)
588-
context.messages = next_messages
589-
aggregated.extend(next_messages)
569+
# Capture this iteration's progress entry, then refresh loop_kwargs so the next-message
570+
# resolution sees the latest entry.
571+
if await self._record_progress(result, loop_kwargs, progress):
572+
loop_kwargs = self._build_loop_kwargs(
573+
context=context,
574+
iteration=iteration,
575+
last_result=result,
576+
messages_used=messages_used,
577+
original_messages=original_messages,
578+
progress=progress,
579+
feedback=feedback,
580+
)
581+
if stop:
582+
break
583+
if snapshot is not None and context.session is not None:
584+
# Reset the session to the pre-loop baseline so the next run starts fresh; only the
585+
# progress log (injected by _resolve_next_message) carries continuity forward.
586+
self._restore_session(context.session, snapshot)
587+
next_messages = await self._resolve_next_message(loop_kwargs, messages_used, original_messages)
588+
context.messages = next_messages
589+
aggregated.extend(next_messages)
590+
finally:
591+
context.options.pop(_LOOP_ITERATION_TOKEN_KEY, None)
590592

591593
if not self.return_final_only:
592594
context.result = self._aggregate_response(final_result, aggregated, aggregated_usage)
@@ -611,9 +613,11 @@ async def _generator() -> Any:
611613
iteration = 0
612614
work_iterations = 0
613615
progress: list[str] = []
616+
stamped_options = dict(context.options) if context.options is not None else {}
617+
stamped_options[_LOOP_ITERATION_TOKEN_KEY] = object()
618+
context.options = stamped_options
614619
try:
615620
while True:
616-
loop_token = _LOOP_ITERATION_ACTIVE.set(context.agent)
617621
try:
618622
await call_next()
619623
inner = context.result
@@ -633,8 +637,6 @@ async def _generator() -> Any:
633637
# raised by a downstream middleware or during stream consumption surfaces here.
634638
# Stop cleanly and keep whatever final response we have from a prior iteration.
635639
return
636-
finally:
637-
_LOOP_ITERATION_ACTIVE.reset(loop_token)
638640

639641
iteration += 1
640642

@@ -691,6 +693,7 @@ async def _generator() -> Any:
691693
for message in next_messages:
692694
yield self._message_to_update(message)
693695
finally:
696+
context.options.pop(_LOOP_ITERATION_TOKEN_KEY, None)
694697
await self._fire_turn_scoped_after_providers(context, holder["final"], original_messages)
695698

696699
def _finalize(updates: Sequence[AgentResponseUpdate]) -> AgentResponse:

‎python/packages/core/tests/core/test_harness_loop.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,3 +1530,60 @@ async def run_inner() -> str:
15301530
await outer.run("start")
15311531

15321532
assert inner_provider.after_calls == 1
1533+
1534+
1535+
async def test_nested_same_agent_run_with_separate_session_is_not_suppressed() -> None:
1536+
turn_scoped = _TurnScopedRecordingProvider()
1537+
holder: dict[str, Any] = {}
1538+
1539+
@tool(name="run_nested", approval_mode="never_require")
1540+
async def run_nested() -> str:
1541+
# Recursively running the same agent on its own session is a separate
1542+
# turn boundary, so its turn-scoped providers must still fire.
1543+
await holder["agent"].run("nested task", session=AgentSession())
1544+
return "done"
1545+
1546+
outer_client = MockBaseChatClient()
1547+
outer_client.run_responses = [
1548+
ChatResponse(
1549+
messages=Message(
1550+
role="assistant",
1551+
contents=[Content.from_function_call(call_id="c1", name="run_nested", arguments="{}")],
1552+
)
1553+
),
1554+
ChatResponse(messages=Message(role="assistant", contents=["finished"])),
1555+
]
1556+
outer = Agent(
1557+
client=outer_client,
1558+
tools=[run_nested],
1559+
middleware=[AgentLoopMiddleware(always_continue, max_iterations=1)],
1560+
context_providers=[turn_scoped],
1561+
)
1562+
holder["agent"] = outer
1563+
1564+
await outer.run("start", session=AgentSession())
1565+
1566+
# once for the loop's turn boundary, once for the nested run's own turn
1567+
assert turn_scoped.after_calls == 2
1568+
1569+
1570+
async def test_concurrent_same_agent_run_during_stream_pause_is_not_suppressed() -> None:
1571+
turn_scoped = _TurnScopedRecordingProvider()
1572+
agent = Agent(
1573+
client=RecordingChatClient(),
1574+
middleware=[AgentLoopMiddleware(always_continue, max_iterations=1)],
1575+
context_providers=[turn_scoped],
1576+
)
1577+
1578+
stream = agent.run("start", stream=True)
1579+
first = True
1580+
async for _update in stream:
1581+
if first:
1582+
first = False
1583+
# a run started by the caller while the outer stream is paused is
1584+
# its own turn, not a loop iteration
1585+
await agent.run("concurrent turn", session=AgentSession())
1586+
await stream.get_final_response()
1587+
1588+
# once for the loop boundary, once for the concurrent run's own boundary
1589+
assert turn_scoped.after_calls == 2

0 commit comments

Comments
 (0)