Skip to content

fix: per-message agent attribution broken when snapshotting#68

Open
scmmishra wants to merge 4 commits into
mainfrom
fix/per-message-agent-name
Open

fix: per-message agent attribution broken when snapshotting#68
scmmishra wants to merge 4 commits into
mainfrom
fix/per-message-agent-name

Conversation

@scmmishra
Copy link
Copy Markdown
Member

In multi-agent conversations, MessageExtractor.extract_messages stamps every assistant message with the currently active agent's name. After a handoff, the chat contains assistant messages produced by earlier agents, but snapshotting the chat under the final agent re-attributes all of them to that final agent.

That polluted snapshot then leaks into AgentRunner#determine_conversation_agent, which picks the next active agent from the last assistant message's agent_name — so subsequent runs can start with the wrong specialist.

Example of the broken snapshot after a Triage → Billing handoff:

[
  { role: :user, content: "I need help with billing" },
  { role: :assistant, content: "I'll route this.",            agent_name: "Billing" }, # wrong
  { role: :assistant, content: "I can help with your invoice.", agent_name: "Billing" }
]

Fixes #67.

Fix

Attach agent ownership to each assistant message at the moment it is produced, then read that per-message attribution at extraction time instead of inferring it from current_agent.

RubyLLM::Message has no metadata/extension API, so attribution is stored as an SDK-namespaced instance variable (:@agents_authoring_agent) on the message object. Two new module functions on MessageExtractor encapsulate this storage:

  • assign_agent_name(message, agent_name) — stamp a message.
  • attributed_agent_name_for(message) — read the stamp back.

Runner calls assign_agent_name in two places:

  1. After every chat.ask / chat.complete round trip, on the assistant messages appended during that turn (assign_agent_name_to_new_assistant_messages).
  2. When restoring messages from context[:conversation_history], preserving any prior agent_name (assign_restored_agent_name).

MessageExtractor.extract_user_or_assistant_message now reads attributed_agent_name_for(msg) first and falls back to current_agent.name only when no per-message attribution exists. This keeps legacy/un-tagged history backwards compatible.

Result

[
  { role: :user, content: "I need help with billing" },
  { role: :assistant, content: "I'll route this.",            agent_name: "Triage" },
  { role: :assistant, content: "I can help with your invoice.", agent_name: "Billing" }
]

@netlify
Copy link
Copy Markdown

netlify Bot commented May 22, 2026

Deploy Preview for ruby-ai-agents canceled.

Name Link
🔨 Latest commit d679e89
🔍 Latest deploy log https://app.netlify.com/projects/ruby-ai-agents/deploys/6a1080f8b71bc70007dd01be

@scmmishra
Copy link
Copy Markdown
Member Author

@codex please review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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.

Preserve per-message agent attribution when snapshotting and restoring multi-agent history

1 participant