Fix #433: allow test_context() for sink-less (non-yielding) agents#699
Open
wbarnha wants to merge 1 commit into
Open
Fix #433: allow test_context() for sink-less (non-yielding) agents#699wbarnha wants to merge 1 commit into
wbarnha wants to merge 1 commit into
Conversation
Agent.test_context() wraps the agent in an AgentTestWrapper that always
added an internal results sink. For an agent that never yields, that sink
tripped _prepare_actor's ImproperlyConfigured('Agent must yield to use
sinks') at startup, so test_context() could not be used to unit-test
sink-less agents at all.
Detect whether the wrapped agent yields (inspect.isasyncgenfunction on
its function). Only attach the results sink when it does; for a non-yielding
agent, observe processed values with a stream processor instead. The
processor records each value and wakes any put(wait=True) caller, so the
test wrapper behaves the same from the caller's point of view without
forcing the agent to yield.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HHPL4VFWQRQPpjR1gXSKyL
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #699 +/- ##
=======================================
Coverage 94.14% 94.15%
=======================================
Files 104 104
Lines 11136 11137 +1
Branches 1201 1201
=======================================
+ Hits 10484 10486 +2
+ Misses 551 550 -1
Partials 101 101 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Agent.test_context()wraps the agent in anAgentTestWrapperthat unconditionally added an internal "results" sink. For an agent that neveryields, that sink tripped_prepare_actor's guard:…which is raised at agent startup, so
test_context()could not be used to unit-test sink-less agents at all — you had to add a throwawayyieldjust to make the harness work.Fixes #433.
How
Detect whether the wrapped agent actually yields, via
inspect.isasyncgenfunction(self.fun):_on_value_processed), which records yielded values and wakesput(wait=True).ImproperlyConfiguredguard is not tripped) and instead attach a stream processor. The processor records each incoming value intoresultsand notifiesnew_value_processed, soput(wait=True)still returns after the value is processed. The processor returns the value unchanged, leaving the agent's own iteration untouched.This mirrors the approach a maintainer suggested on the issue ("check in test_context if it is already yielding and if not … [handle it another way]"), without requiring the user to change their agent.
Test
Added
test_context__sinkless_agentintests/unit/agents/test_agent.py(alongside the existingtest_context_calls_sink): defines a non-yielding agent, enterstest_context(),await agent.put("hello"), and asserts the value was processed and recorded — a case that previously raisedImproperlyConfiguredat startup. Fulltests/unit/agents/test_agent.pypasses (73 passed, 1 skipped).🤖 Generated with Claude Code
Generated by Claude Code