Skip to content

fix(network): evaluate turn_within expectations on workflow and discussion channels - #3703

Open
LykenL wants to merge 1 commit into
ag2ai:mainfrom
LykenL:bug/auto_closing_stalled_workflow
Open

LykenL wants to merge 1 commit into
ag2ai:mainfrom
LykenL:bug/auto_closing_stalled_workflow

Conversation

@LykenL

@LykenL LykenL commented Nov 21, 2026 •

Copy link
Copy Markdown

Why are these changes needed?

WorkflowAdapter and DiscussionAdapter declare turn_within expectations (e.g. turn_within(600s, auto_close) so a stalled workflow "fails fast"), but default_evaluators() shipped no turn_within evaluator, and default_handlers() never registered the warn handler those manifests name. The expectation sweeper skips any expectation whose evaluator or handler is missing (if evaluator is None: continue / if handler is None: continue), so turn_within was silently never evaluated — a workflow whose expected next speaker never speaks stayed ACTIVE forever instead of auto-closing.
This PR:

  • Adds TurnWithinEvaluator (anchored on the triggering envelope's created_at, falling back to channel created_at for the fresh-channel turn) and a targeted WarnHandler (posts EV_EXPECTATION_VIOLATED to the violator(s) only), and registers both.
  • Passes the adapter's expected_next() result into ExpectationContext.expected_turn.
  • Reads adapter state and the WAL under the same channel lock in _expectation_tick, so a concurrent reply can't pair stale turn state with a fresh WAL and falsely time out a participant who just spoke.
  • Makes violation dedup turn-aware (Violation.turn_key) so the same participant can fire on distinct turns while still deduplicating within a single turn.
    Validation: pytest test/network/ — 589 passed; ruff check on the touched files passes.

Related issue number

Related issue: #3700

Checks

AI assistance

  • I understand the changes in this PR and can explain them in my own words.
  • I have verified that the PR description accurately reflects the actual diff.
  • If AI assistance was used, I reviewed, tested, and validated the generated code/text before submitting.

…ssion channels

WorkflowAdapter and DiscussionAdapter declare `turn_within` expectations,
but `default_evaluators()` shipped no turn_within evaluator, so the sweeper
silently skipped them (core.py `if evaluator is None: continue`) and a
stalled workflow never auto-closed. The `warn` handler named on those
manifests was also never registered.

- Add TurnWithinEvaluator, anchored on the triggering envelope's created_at
  (falling back to channel created_at for the fresh-channel turn).
- Add WarnHandler, posting EV_EXPECTATION_VIOLATED to the violator(s) only.
- Pass the adapter's expected_next() into ExpectationContext.expected_turn.
- Read adapter state and WAL under the same channel lock in
  _expectation_tick so a concurrent reply can't pair stale turn state with
  a fresh WAL.
- Make violation dedup turn-aware (Violation.turn_key) so the same
  participant can fire on distinct turns while still deduping within one.

The discussion `hide` handler remains unimplemented (documented as such)
and is out of scope here.
@CLAassistant

CLAassistant commented Nov 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added area:docs Documentation (website/) area:network Agent network (ag2/network) labels Nov 21, 2026

This branch has not been deployed

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

Labels

area:docs Documentation (website/) area:network Agent network (ag2/network)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants