Skip to content

fix: restore chronological order in MessageFilterAgent._apply_filter - #8275

Open
Varshith (Varshith-Kali) wants to merge 2 commits into
microsoft:mainfrom
Varshith-Kali:fix/message-filter-chronological-order
Open

Varshith (Varshith-Kali) wants to merge 2 commits into
microsoft:mainfrom
Varshith-Kali:fix/message-filter-chronological-order

Conversation

@Varshith-Kali

Copy link
Copy Markdown

Summary

MessageFilterAgent._apply_filter collected filtered messages grouped by per-source config order, so the wrapped agent received messages in config order rather than the order they appear in the message history. This re-sorts the result on each message's original position before returning.

The index map is keyed by object identity (id(m)) deliberately: list indexing would be O(n²) and incorrect for value-identical duplicates (BaseChatMessage.__eq__ is pydantic value equality) — e.g. a message and its model_copy() would both resolve to the first duplicate's position.

Fixes #7971.

Changes

  • python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_graph/_agents/_message_filter_agent.py: 3-line fix + explanatory comment, plus a chronological-order guarantee in the class docstring.
  • python/packages/autogen-agentchat/tests/test_message_filter_agent_order.py: 10 tests covering multi-source reordering, position=None select-all filters, empty history, the streaming entry point, input non-mutation, and duplicate/value-equal messages.

Test results

  • 10/10 ordering tests pass
  • 76/76 existing graph tests pass (test_group_chat_graph.py, incl. existing MessageFilterAgent tests)
  • 202 passed in the wider autogen_agentchat package run
  • ruff check and ruff format --check clean

@Varshith-Kali

Copy link
Copy Markdown
Author

Checking in on this MessageFilterAgent ordering fix. The change is small and covered by tests. The branch has fallen behind main since it was opened; happy to rebase if that helps the review.

_apply_filter collected filtered messages grouped by per_source config
order, so the wrapped agent received messages in config order rather
than the order they appear in the message history. Re-sort the result
on each message's original position before returning.

The index map is keyed by object identity (id(m)) deliberately: list
indexing would be O(n^2) and incorrect for value-identical duplicates
(BaseChatMessage.__eq__ is pydantic value equality), e.g. a message and
its model_copy() would both resolve to the first duplicate's position.

Fixes microsoft#7971
- Add test for position=None (select-all) filters: config order [B, ghost, A]
  still yields chronological output, unmatched sources are ignored, and the
  input sequence is not mutated by the re-sort.
- Add empty-history and on_messages_stream ordering tests; the stream entry
  point shares _apply_filter but previously had no coverage.
- Document the chronological-order guarantee in the MessageFilterAgent
  class docstring.
- Fix import order in the new test module per repo ruff (isort) config.

All 10 ordering tests pass; 76 graph tests and 202 package tests pass.
The 5 remaining collection errors are missing optional third-party
dependencies (openai, tiktoken) in unrelated modules, pre-existing.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com>
@Varshith-Kali
Varshith (Varshith-Kali) force-pushed the fix/message-filter-chronological-order branch from 2567996 to ac92d35 Compare October 24, 2026 05:43
@Varshith-Kali

Copy link
Copy Markdown
Author

Rebased onto latest main; message-filter tests green.

@Varshith-Kali

Copy link
Copy Markdown
Author

Hi, the rebased head has its main CI workflows parked waiting for a first-time-contributor approval (action_required). Could a maintainer approve the runs so CI can proceed? Thanks.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MessageFilterAgent orders filtered messages by per_source config order, not chronological order

1 participant