Skip to content

Fix #433: allow test_context() for sink-less (non-yielding) agents#699

Open
wbarnha wants to merge 1 commit into
masterfrom
claude/fix-433-sinkless-test-context
Open

Fix #433: allow test_context() for sink-less (non-yielding) agents#699
wbarnha wants to merge 1 commit into
masterfrom
claude/fix-433-sinkless-test-context

Conversation

@wbarnha

@wbarnha wbarnha commented Jul 19, 2026

Copy link
Copy Markdown
Member

What

Agent.test_context() wraps the agent in an AgentTestWrapper that unconditionally added an internal "results" sink. For an agent that never yields, that sink tripped _prepare_actor's guard:

faust.exceptions.ImproperlyConfigured: Agent must yield to use sinks

…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 throwaway yield just to make the harness work.

Fixes #433.

How

Detect whether the wrapped agent actually yields, via inspect.isasyncgenfunction(self.fun):

  • Yielding agent — unchanged: attach the results sink (_on_value_processed), which records yielded values and wakes put(wait=True).
  • Non-yielding agent — skip the sink (so the ImproperlyConfigured guard is not tripped) and instead attach a stream processor. The processor records each incoming value into results and notifies new_value_processed, so put(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_agent in tests/unit/agents/test_agent.py (alongside the existing test_context_calls_sink): defines a non-yielding agent, enters test_context(), await agent.put("hello"), and asserts the value was processed and recorded — a case that previously raised ImproperlyConfigured at startup. Full tests/unit/agents/test_agent.py passes (73 passed, 1 skipped).

🤖 Generated with Claude Code


Generated by Claude Code

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

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.15%. Comparing base (3073eb9) to head (de309e0).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't use agent.test_context() for sink-less agents

1 participant