Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions python/packages/core/agent_framework/_harness/_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,13 @@ async def _process_non_streaming(
# Reset the session to the pre-loop baseline so the next run starts fresh; only the
# progress log (injected by _resolve_next_message) carries continuity forward.
self._restore_session(context.session, snapshot)
next_messages = await self._resolve_next_message(loop_kwargs, messages_used, original_messages)
next_messages, injected_messages = await self._resolve_next_message(
loop_kwargs, messages_used, original_messages
)
context.messages = next_messages
aggregated.extend(next_messages)
# Surface only what the loop injects: replayed input (the caller's own messages and
# any additional_instructions system message) is sent to the agent, not echoed back.
aggregated.extend(injected_messages)
finally:
context.options.pop(_LOOP_ITERATION_TOKEN_KEY, None)

Expand Down Expand Up @@ -716,12 +720,14 @@ async def _generator() -> Any:
# response was already awaited above, so the service-side conversation id has
# been propagated and is safe to discard here.
self._restore_session(context.session, snapshot)
next_messages = await self._resolve_next_message(loop_kwargs, messages_used, original_messages)
next_messages, injected_messages = await self._resolve_next_message(
loop_kwargs, messages_used, original_messages
)
context.messages = next_messages
# Surface the injected "nudge" messages in the stream so consumers see the user
# turns that drive each subsequent iteration (the equivalent of the aggregated
# transcript that non-streaming runs return).
for message in next_messages:
# transcript that non-streaming runs return). Replayed input stays out of the stream.
for message in injected_messages:
yield self._message_to_update(message)
Comment on lines +730 to 731
finally:
context.options.pop(_LOOP_ITERATION_TOKEN_KEY, None)
Expand Down Expand Up @@ -837,7 +843,14 @@ async def _resolve_next_message(
loop_kwargs: dict[str, Any],
messages_used: list[Message],
original_messages: list[Message],
) -> list[Message]:
) -> tuple[list[Message], list[Message]]:
"""Resolve the next iteration's input and the messages the loop itself injects.

Returns ``(next_input, injected)``. ``next_input`` is everything the agent must see, which
includes replayed caller input in fresh-context mode; ``injected`` holds only the messages
this loop contributes (the progress log and the ``next_message`` messages). Callers surface
``injected`` to the caller and never echo replayed input back into the run result.
"""
# Compute the base next input. A ``next_message`` callable returning None requests a verbatim
# reuse of the previous messages (no progress injection); in fresh-context mode that escape
# hatch does not apply, so fall back to the default nudge instead.
Expand All @@ -847,7 +860,8 @@ async def _resolve_next_message(
next_input = await _maybe_await(self.next_message(**loop_kwargs))
if next_input is None:
if not self.fresh_context:
return list(messages_used)
# Verbatim reuse injects nothing, so nothing is surfaced between iterations.
return list(messages_used), []
next_msgs = normalize_messages(DEFAULT_NEXT_MESSAGE)
else:
next_msgs = normalize_messages(next_input)
Expand All @@ -862,15 +876,13 @@ async def _resolve_next_message(
progress_msg = self._render_progress(entries)

if self.fresh_context:
result = list(original_messages)
if progress_msg is not None:
result.append(progress_msg)
result.extend(next_msgs)
return result
injected = [progress_msg, *next_msgs] if progress_msg is not None else list(next_msgs)
return [*original_messages, *injected], injected

if progress_msg is not None:
return [progress_msg, *next_msgs]
return list(next_msgs)
injected = [progress_msg, *next_msgs]
return list(injected), list(injected)
return list(next_msgs), list(next_msgs)


def _running_background_tasks(session: Any, agent: Any) -> list[Any]:
Expand Down
93 changes: 89 additions & 4 deletions python/packages/core/tests/core/test_harness_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,62 @@ async def test_non_streaming_returns_aggregated_transcript_by_default() -> None:
assert roles == ["assistant", "user", "assistant"]


async def test_aggregated_transcript_excludes_replayed_input() -> None:
client = RecordingChatClient(texts=["draft 1", "draft 2", "draft 3"])
agent = Agent(
client=client,
middleware=[
AgentLoopMiddleware(
always_continue,
max_iterations=3,
fresh_context=True,
additional_instructions="RUBRIC: cite a source.",
)
],
)

response = await agent.run("Write the report")

# Only the loop's own hook messages are surfaced: the replayed caller input and the
# additional_instructions system message are sent to the agent but never echoed back.
assert [m.role for m in response.messages] == [
"assistant",
"user",
"user",
"assistant",
"user",
"user",
"assistant",
]
surfaced = [m.text or "" for m in response.messages if m.role != "assistant"]
assert all("Write the report" not in text for text in surfaced)
assert all("RUBRIC" not in text for text in surfaced)
# Each iteration's answer appears exactly once, in order.
assistant_texts = [m.text for m in response.messages if m.role == "assistant"]
assert assistant_texts == ["draft 1", "draft 2", "draft 3"]


async def test_next_message_returning_none_surfaces_nothing() -> None:
client = RecordingChatClient(texts=["draft 1", "draft 2", "draft 3"])
agent = Agent(
client=client,
middleware=[
AgentLoopMiddleware(
always_continue,
max_iterations=3,
next_message=lambda **kwargs: None,
inject_progress=False,
)
],
)

response = await agent.run("only input")

# A verbatim reuse injects no message, so nothing is surfaced between iterations.
assert [m.role for m in response.messages] == ["assistant", "assistant", "assistant"]
assert "only input" not in response.text


async def test_non_streaming_return_final_only_returns_last_response() -> None:
client = RecordingChatClient(texts=["first answer", "second answer"])
agent = Agent(
Expand Down Expand Up @@ -747,13 +803,14 @@ async def test_resolve_next_message_injects_full_log_without_session() -> None:
"agent": None,
}

result = await mw._resolve_next_message(
next_input, injected = await mw._resolve_next_message(
loop_kwargs,
messages_used=[Message(role="user", contents=["x"])],
original_messages=[Message(role="user", contents=["orig"])],
)

progress_text = result[0].text
assert next_input == injected
progress_text = injected[0].text
assert "e1" in progress_text
assert "e2" in progress_text

Expand All @@ -770,13 +827,14 @@ async def test_resolve_next_message_injects_latest_entry_with_session() -> None:
"agent": None,
}

result = await mw._resolve_next_message(
next_input, injected = await mw._resolve_next_message(
loop_kwargs,
messages_used=[Message(role="user", contents=["x"])],
original_messages=[Message(role="user", contents=["orig"])],
)

progress_text = result[0].text
assert next_input == injected
progress_text = injected[0].text
assert "e2" in progress_text
assert "e1" not in progress_text

Expand Down Expand Up @@ -1332,6 +1390,33 @@ async def test_streaming_injects_nudge_messages_as_user_updates() -> None:
assert any(DEFAULT_NEXT_MESSAGE in text for text in user_texts)


async def test_streaming_updates_exclude_replayed_input() -> None:
client = RecordingChatClient(texts=["draft 1", "draft 2"])
agent = Agent(
client=client,
middleware=[
AgentLoopMiddleware(
always_continue,
max_iterations=2,
fresh_context=True,
additional_instructions="RUBRIC: cite a source.",
)
],
)

stream = agent.run("Write the report", stream=True)
updates = [update async for update in stream]
await stream.get_final_response()

roles = [u.role for u in updates]
assert "system" not in roles
user_texts = [u.text or "" for u in updates if u.role == "user"]
assert all("Write the report" not in text for text in user_texts)
assert all("RUBRIC" not in text for text in user_texts)
# The injected nudge is still surfaced so consumers see what drives each iteration.
assert any(DEFAULT_NEXT_MESSAGE in text for text in user_texts)


async def test_streaming_stops_at_max_iterations() -> None:
client = RecordingChatClient()
agent = Agent(client=client, middleware=[AgentLoopMiddleware(always_continue, max_iterations=2)])
Expand Down
Loading