Repository navigation
feat(agui): add InputMessageDeduplicationMiddleware for resent transcripts - #4535
Conversation
…ripts ## Summary AG-UI front ends such as CopilotKit resend the whole conversation on every run. Against an agent that keeps its conversation across calls, each call then appends the history again. agentscope-ai#4523 removed the old trimming and left a deduplicating `MiddlewareBase` to applications, because matching resent messages against the agent's state is easy to get wrong and the assistant message ids a client receives are not the persisted ones. This adds that middleware, `InputMessageDeduplicationMiddleware`, so applications register it instead of writing their own. Assistant messages are matched by content, because the ids the stream gives them are not the persisted message ids. ## Changes - `InputMessageDeduplicationMiddleware` (AG-UI extension) runs in `onAgentStateReady` and replaces the input with the messages the agent does not hold yet. It looks for the newest message of the agent's conversation that the input repeats and hands the agent only the input after its counterpart. A conversation message is recognized in the input by the tool call or tool result ids it carries, by its text, or by its id when it has no text. An incremental input is left unchanged. While the agent waits for tool results, results it is not waiting for are dropped. When the resent input carries nothing new, the last user message is handed over again under a new id, so the agent answers it again. - The AG-UI guide (EN and ZH) replaces the hand-written middleware outline with the registration and its rules. Register it only on an agent that keeps its conversation across requests. Refs agentscope-ai#2023
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
The middleware lost input in three shapes: a new message with the text of a question that got no answer, a result the agent waits for that the client sent in front of its copy of the tool call, and a new message followed by a stale tool result. The text of a user message alone no longer counts as a repetition: it takes the same message id, or messages in front of it that line up with the conversation. A result the agent waits for reaches it wherever the client put it, and so does the answer to a call that waits for approval. A new user message while a tool call waits for its result gets a cancelled result for that call, so that the agent goes on instead of refusing the call. A question that is still the last message of the conversation is not handed over again: the agent answers it from the conversation. The Javadoc is shortened and the AG-UI guide (EN and ZH) follows.
juruoO-o
left a comment
There was a problem hiding this comment.
I support shipping this as an opt-in middleware in agentscope-extensions-agui. The transcript matching is easy for applications to get wrong, and the existing onAgentStateReady hook is the right integration point. Please address these two correctness issues before merge.
- [P1] Filtering a pure replay can re-dispatch pending tools. When the request contains only replayed history and already-consumed results,
InputDelta.extract()can leave an empty input list. With a tool still pending,ReActAgenttreats empty input as a resume and callsresumeAgent(). Please keep the call suspended without dispatching it again. Add a regression through the agent or AG-UI entry point that checksTOOL_SUSPENDED, an unchanged dispatch count, and successful delivery of the real result afterward. See the regression scenario in #3766. - [P1] Filtering framework messages can drop a real new user message.
withoutFrameworkMessages()drops any incomingUSERmessage whose normalized text matches a synthetic message anywhere in the stored conversation. A new user message with a fresh ID can therefore disappear even when there is no transcript overlap. Please require evidence that the message is a replay before dropping it, and add a regression that preserves a fresh user message with matching text.
There are also two policy choices to make explicit. withCancelledCalls() synthesizes a tool result when a new user message arrives, even with enablePendingToolRecovery disabled. Please keep cancellation behind an explicit application choice. restoreLastUserPrompt() can turn a retry of an already-answered transcript into another model run. Please clarify how callers request regeneration and how a replay or retry is handled.
Reviewed head 208290cfe437c45668c20e0cb0c3051a4b65e35a. Static source review. Local Java tests were not run.
juruoO-o
left a comment
There was a problem hiding this comment.
One additional data-loss case on the reviewed head: MessageFingerprint records attachment classes, so two messages with the same text and same attachment types can compare equal even when the image or document differs. Please preserve the new message when attachment identity cannot be established. Cover same-type attachments with different content and no reliable shared message ID.
A replay that leaves the agent waiting for a tool result ends as a suspension naming the waiting calls, instead of requesting them again. Only user messages that copy, in order, the messages the framework added behind the anchor are left out. A new message that merely has the same text is kept. A new user message cancels a waiting call only on an agent built with enablePendingToolRecovery(true). Without it the run fails as before. The attachments of a user message are compared by what they hold, by address, data or file, so the same words with another picture are a new message. A resend that starts at a reply the client splits into reasoning and text is recognized. Regenerate and retry are documented. The AG-UI guide (EN and ZH) follows, and names the check a journaled agent makes and the failure messages of a HarnessAgent.
|
All five points from the two reviews are addressed.
|
juruoO-o
left a comment
There was a problem hiding this comment.
One remaining data-loss case in withoutFrameworkCopies(): isCopyOf() compares only role and collapsed text, bypassing the new attachment comparison. In leavesACopyOfAReminderOutWhileACallWaits, add a new image to the final incoming user message while keeping the reminder text. The stored reminder has no image, yet this branch still drops the entire incoming message before the agent can see it. Please retain messages with additional or different content blocks (including attachments) and add this anchored regression alongside the genuine text-only reminder-copy case.
withoutFrameworkCopies() took a user message for a copy of a framework-added reminder when only its collapsed text matched. A new message that kept the reminder's text and added a picture, a sound or any other content block was dropped before the agent saw it, without an error, while a tool call was waiting. A copy now has the stored message's text, however it is split into text blocks, and the same other content blocks. Media blocks are compared by what they hold, as resent messages are, and a block that cannot be compared does not make a copy. A tool result or an approval is never taken for a copy. The AG-UI guide (EN and ZH) follows.
|
Fixed: a user message that keeps a reminder's text but carries other content is no longer dropped as a copy of it. |
juruoO-o
left a comment
There was a problem hiding this comment.
Reviewed the reminder-copy fix and its regression coverage. Messages with new or changed attachments survive deduplication, and genuine reminder copies are still filtered.
Summary
AG-UI front ends such as CopilotKit resend the whole conversation on every run. Against an agent that keeps its conversation across calls, each call then appends the history again. #4523 removed the old trimming and left a deduplicating
MiddlewareBaseto applications, because matching resent messages against the agent's state is easy to get wrong and the assistant message ids a client receives are not the persisted ones.This PR ships that middleware,
InputMessageDeduplicationMiddleware, so applications register it instead of writing their own. Assistant messages are matched by content, because the ids the stream gives them are not the persisted message ids.Changes
InputMessageDeduplicationMiddleware(AG-UI extension) runs inonAgentStateReadyand replaces the input with the messages the agent does not hold yet. It looks for the newest message of the agent's conversation that the input repeats and hands the agent only the input after its counterpart. A conversation message is recognized in the input by the tool call or tool result ids it carries, by its text, or by its id when it has no text. An incremental input is left unchanged. While the agent waits for tool results, results it is not waiting for are dropped. When the resent input carries nothing new, the last user message is handed over again under a new id, so the agent answers it again.Register it only on an agent that keeps its conversation across requests.
Refs #2023.