Skip to content

fix(agentchat): preserve chronological order in MessageFilterAgent._apply_filter - #8034

Open
zerafachris (zerafachris) wants to merge 2 commits into
microsoft:mainfrom
zerafachris:fix/message-filter-agent-chronological-order-7971
Open

zerafachris (zerafachris) wants to merge 2 commits into
microsoft:mainfrom
zerafachris:fix/message-filter-agent-chronological-order-7971

Conversation

@zerafachris

Copy link
Copy Markdown

Issue: #7971

Problem

MessageFilterAgent._apply_filter built its result by iterating per_source filters and appending matching messages in filter-config order. When multiple sources were configured, messages from a later-listed source that chronologically preceded messages from an earlier-listed source were emitted out of order, breaking the LLM agent's conversation timeline.

For example, given messages [user, A(old), B, A(new)] and a filter listing A before user:

  • Before fix: [A(old), A(new), user] — config order, not chronological
  • After fix: [user, A(old), A(new)] — original conversation order preserved (with position="last", count=1 → [user, A(new)])

Change

Track the original message index for each selected message and emit in original conversation order. The per-source count/position filtering logic is unchanged.

# Before
result: List[BaseChatMessage] = []
for source_filter in self._filter.per_source:
    msgs = [m for m in messages if m.source == source_filter.source]
    # ... slice by position/count ...
    result.extend(msgs)  # order follows filter-config, not timeline
return result

# After
selected: set[int] = set()
for source_filter in self._filter.per_source:
    indexed = [(i, m) for i, m in enumerate(messages) if m.source == source_filter.source]
    # ... slice by position/count ...
    for i, _ in indexed:
        selected.add(i)
return [m for i, m in enumerate(messages) if i in selected]  # original order

Tests

New regression test test_message_filter_agent_preserves_chronological_order in tests/test_group_chat_graph.py:

  • Sets up per_source=[A-last(1), user-first(1)] (A listed before user)
  • Messages arrive in order: user, A(first), A(second)
  • Asserts the wrapped agent receives [user, A(second)] — original order — not [A(second), user] — config order

Prepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission.

…pply_filter

The previous implementation iterated per_source filters and appended matching
messages in filter-config order.  When multiple sources were configured,
messages from a later-listed source that chronologically preceded messages from
an earlier-listed source were emitted out of order, breaking the LLM's
conversation timeline.

Fix: track the original message index for each selected message and emit in
original conversation order.  The per-source count/position filtering logic is
unchanged; only the final order of the merged result is fixed.

Fixes microsoft#7971

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@zerafachris

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@zerafachris

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@zerafachris

Copy link
Copy Markdown
Author

westey (@westey-m) The Microsoft CLA is now signed and green — ready for review when you get a chance!

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.

1 participant