Skip to content

fix: clean citation leaks and duplicate agent output in thinking process#1094

Open
Ayaz-Microsoft wants to merge 2 commits into
devfrom
bugfix/repeated-agents
Open

fix: clean citation leaks and duplicate agent output in thinking process#1094
Ayaz-Microsoft wants to merge 2 commits into
devfrom
bugfix/repeated-agents

Conversation

@Ayaz-Microsoft

Copy link
Copy Markdown
Contributor

Purpose

Fix citation leaks and duplicated agent output in the AI Thinking Process

Summary

Fixes four related issues in the multi-agent streaming/orchestration layer: citation markers leaking into responses, agent names/responses duplicating in the "AI Thinking Process", the same agent repeating across consecutive rounds, and a lack of diagnostics for the Magentic manager's round loop.

Background

The Magentic orchestrator re-invokes agents across rounds by design, and streaming output arrives in partial deltas. The previous handling assumed append-only rendering and per-chunk cleaning, which broke down for multi-round runs and split markers.

Changes

Backend —  src/backend/callbacks/response_handlers.py 

• Cross-chunk citation cleaning: buffer a trailing, not-yet-closed  【…  marker until its closing  】  arrives in a later delta, then strip it. Prevents markers like  【5:0†source】  from leaking when split across streaming chunks.
• Per-agent grouped "thinking" snapshot: accumulate output into an ordered  {agent → latest text}  map and emit the full grouped snapshot each delta, so each agent appears once with its latest response.
• Turn-boundary reset:  mark_streaming_turn_complete()  resets the current section at each agent turn boundary, so consecutive same-agent rounds don't concatenate.

Backend —  src/backend/orchestration/orchestration_manager.py 

• Apply  clean_citations()  to the Group Chat Manager's final answer, which previously bypassed citation cleaning entirely.
• Call  mark_streaming_turn_complete()  at each  executor_completed  turn boundary.
• Add a per-round diagnostic log:  [ROUND N] next_speaker=… satisfied=… in_loop=… progress=…  from the manager's progress ledger.

Frontend —  src/App/src/hooks/usePlanWebSocket.tsx 

• Replace the streaming buffer with the latest backend snapshot instead of appending, matching the new backend grouping.

Issues fixed:

• Citation markers ( 【5:0†source】 ) leaking in agent responses — fixed by cross-chunk citation buffering.
• Same markers leaking in the Group Chat Manager final answer — fixed by applying  clean_citations()  to  final_text .
• Agent names/responses duplicated in the AI Thinking Process — fixed by the backend grouped snapshot + frontend replace.
• Same agent repeated ×N in consecutive rounds (e.g. Triage ×7) — fixed by the turn-boundary section reset.
• No visibility into the manager round loop — fixed by the per-round  [ROUND N]  log.

Does this introduce a breaking change?

  • Yes
  • No

How to Test

  • Get the code
git clone [repo-address]
cd [repo-name]
git checkout [branch-name]
npm install
  • Test the code
•  py_compile  clean on both backend files;  tsc --noEmit  clean on the frontend.
• Verified  clean_citations()  strips the exact marker  【5:0†source】 .
• Deployed to  rg-macae-az0  and confirmed on live runs: clean 6-round convergence with real output + image, no duplicated/repeated agents in the thinking process, and per-round logs present. Group Chat Manager final answer no longer shows the citation marker.

What to Check

Verify that the following are valid

  • ...

Other Information

- Strip citation markers (【x:y†source】) from agent streams (cross-chunk
  buffered) and the Group Chat Manager's final answer
- Group AI Thinking Process output per agent (keep-latest snapshot) and
  switch frontend buffer from append to replace, fixing duplicated agent
  names/responses across manager rounds
- Reset streaming section on each turn boundary to fix consecutive
  same-agent repetition (e.g. Triage repeated 7x)
- Add per-round [ROUND N] next_speaker/satisfied/loop/progress log

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes streaming/orchestration “AI Thinking Process” rendering issues by cleaning citation markers across chunk boundaries, emitting a grouped per-agent snapshot (instead of append-only deltas), and adding per-round diagnostics in the Magentic round loop.

Changes:

  • Backend: add cross-chunk citation stripping + grouped per-agent thinking snapshot state (with turn-boundary resets).
  • Backend: clean citations from the Group Chat Manager’s final answer and add per-round [ROUND N] ... diagnostic logging.
  • Frontend: treat streamed messages as full snapshots and replace (not append) the streaming buffer.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/backend/orchestration/orchestration_manager.py Resets thinking state per run, cleans citations on final manager output, adds per-round logs, and marks turn boundaries for streaming snapshot behavior.
src/backend/callbacks/response_handlers.py Implements citation buffering across streamed chunks and builds/emits grouped “thinking” snapshots per user.
src/App/src/hooks/usePlanWebSocket.tsx Updates client buffering to replace with the latest backend snapshot instead of concatenating.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/backend/orchestration/orchestration_manager.py Outdated
Comment thread src/backend/callbacks/response_handlers.py
Comment thread src/backend/orchestration/orchestration_manager.py
Comment thread src/backend/orchestration/orchestration_manager.py
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.

2 participants