Repository navigation
Conversation
…ut in loop results _process_non_streaming and _process_streaming aggregated the whole next input instead of the messages the loop injects, so response.text repeated the caller's question once per iteration and leaked the rubric system message; turn-scoped providers received the duplicated aggregate. Return the injected messages separately and surface only those.
There was a problem hiding this comment.
🟡 Changes recommended
Consecutive iterations can merge assistant responses without message IDs, allowing a later replace update to erase earlier output; iteration boundaries need preservation and a streaming reconstruction regression test.
1 open issue
What changed in this PR
This PR updates Python’s AgentLoopMiddleware to exclude replayed input from loop results while preserving the messages sent to each iteration.
Changes:
- Separates next-iteration input from loop-injected messages.
- Surfaces only injected messages alongside responses in aggregated results and streaming updates.
- Adds three regression tests and adapts two tests to the private return contract.
| File | Description |
|---|---|
python/packages/core/tests/core/test_harness_loop.py |
Tests replay filtering and adapts assertions for the new return shape. |
python/packages/core/agent_framework/_harness/_loop.py |
Separates model input from messages surfaced in loop results. |
Review effort: Balanced
| for message in injected_messages: | ||
| yield self._message_to_update(message) |
Nick (surlymochan)
left a comment
There was a problem hiding this comment.
Reviewed the diff (non-binding review, no approve).
Verified: _resolve_next_message now returns (next_input, injected) and both production call sites (non-streaming _process_non_streaming and the streaming _generator) were updated to surface only injected. I searched the repo for other callers — only _loop.py and its test file reference it, so nothing else still expects the old single-list return. In fresh_context mode injected is [progress?, *next_msgs], so the replayed caller input and the additional_instructions system message are sent to the agent but no longer echoed back; in the non-fresh path injected == next_input, so that path's behaviour is unchanged. The new tests cover non-streaming, streaming, and the next_message -> None case (surfaces nothing).
One question, non-blocking: the Copilot review flags that consecutive iterations' streamed assistant responses could merge without distinct message IDs, letting a later replace update erase earlier output. I couldn't confirm or rule that out from this diff alone — the injected nudges are yielded via _message_to_update, but the iteration's assistant answer itself isn't part of this change. If that merge risk is real, a regression test asserting distinct message IDs per iteration in the streamed output would pin it. Otherwise the change looks correct and well tested.

Problem
AgentLoopMiddleware._process_non_streamingand_process_streamingaggregate the whole next input rather than only the messages the loop injects:_resolve_next_messagedeliberately builds the agent input, which contains replayed material: in fresh-context mode[*original_messages, progress_msg?, *next_msgs](including theadditional_instructionssystem message), andlist(messages_used)verbatim whennext_messagereturnsNone. The class docstring and thereturn_final_only/next_messagedocs promise only "each iteration's response messages plus the injected next_message nudge messages".Consequences:
response.textrepeats the caller's question once per extra iteration;systemmessage;systemrole update;Fix
_resolve_next_messagereturns(next_input, injected), whereinjectedis the progress message plus thenext_messagemessages (empty when reusing the caller's list). Both loop bodies assigncontext.messages = next_inputand surface onlyinjected. No public API change.Tests
python/packages/core/tests/core/test_harness_loop.py(3 new regression tests; 2 existing tests adapted to the private return contract):Whole package
pytest tests -m "not integration"leaves only 3 pre-existing environment failures (missingaiohttp/agent_framework_durabletaskextras).