Skip to content

Commit e1422ab

Browse files
erauner12EItanya
andauthored
fix(ui): avoid false stale chat send guard (#2034)
Fixes a false-positive chat send guard that can show `New messages loaded — please review before sending` during normal same-tab usage after an agent response has already completed and is visible. The send guard now compares backend-derived messages against all locally visible comparable messages instead of only persisted/stored messages. This preserves the intended cross-tab stale-message warning while allowing the next same-tab send to proceed. Fixes #2033. ## Changes - Count all locally visible comparable chat messages when deciding whether the backend has unseen messages. - Apply the same comparable-message filtering to backend-derived messages before comparing counts. - Add mounted regression tests covering: - same-tab completed stream does not block the next send - true cross-tab/unseen messages still block - cross-tab/unseen messages still block after a same-tab stream ## Validation Automated: ```bash cd ui npm test -- --runTestsByPath src/components/chat/__tests__/ChatInterface.sendGuard.test.tsx --runInBand ``` Result: ```text Test Suites: 1 passed, 1 total Tests: 3 passed, 3 total ``` Previously also run successfully during branch validation: ```text src/lib/__tests__/messageHandlers.test.ts: 27 passed Full UI Jest suite: 268 passed ``` Manual: - Deployed/tested locally against `kind-kagent`. - Verified the next same-tab message sends without the false `New messages loaded` toast after a completed agent response. --------- Signed-off-by: Evan Rauner <[email protected]> Co-authored-by: Eitan Yarmush <[email protected]>
1 parent 79cab39 commit e1422ab

3 files changed

Lines changed: 475 additions & 12 deletions

File tree

‎ui/src/components/chat/ChatInterface.tsx‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { getUiRuntimeConfig } from "@/app/actions/config";
2626
import { DEFAULT_STREAM_TIMEOUT_MS } from "@/lib/constants";
2727
import { toast } from "sonner";
2828
import { useRouter } from "next/navigation";
29-
import { createMessageHandlers, extractMessagesFromTasks, extractApprovalMessagesFromTasks, extractTokenStatsFromTasks, createMessage, ADKMetadata, ProcessedToolCallData } from "@/lib/messageHandlers";
29+
import { createMessageHandlers, extractMessagesFromTasks, extractApprovalMessagesFromTasks, extractTokenStatsFromTasks, createMessage, countSendGuardComparableMessages, countBackendBackedComparableMessages, ADKMetadata, ProcessedToolCallData } from "@/lib/messageHandlers";
3030
import { kagentA2AClient } from "@/lib/a2aClient";
3131
import { formatA2AClientError } from "@/lib/a2aErrors";
3232
import { useChatRunInSandbox, useChatSubstrateSandbox } from "@/components/chat/ChatAgentContext";
@@ -39,6 +39,7 @@ const RESUBSCRIBE_TASK_STATES: TaskState[] = ["submitted", "working"];
3939
// Task states that mean the session is busy (used by the cross-tab send guard).
4040
const ACTIVE_TASK_STATES: TaskState[] = ["submitted", "working", "input-required"];
4141

42+
4243
interface ChatInterfaceProps {
4344
selectedAgentName: string;
4445
selectedNamespace: string;
@@ -243,14 +244,11 @@ export default function ChatInterface({ selectedAgentName, selectedNamespace, se
243244
// the full context before their next message goes out.
244245
const guardSessionId = session?.id || sessionId;
245246
if (guardSessionId) {
246-
// Compare only non-approval messages to avoid false negatives when
247-
// storedMessages includes appended ToolApprovalRequest / AskUserRequest entries.
248-
const localMessageCount = storedMessages.filter(m => {
249-
const meta = m.metadata as ADKMetadata | undefined;
250-
return meta?.originalType !== "ToolApprovalRequest" && meta?.originalType !== "AskUserRequest";
251-
}).length;
247+
// Compare visible messages against the backend snapshot. Completed
248+
// same-tab stream messages remain in streamingMessages until the next
249+
// send promotes them, but only backend-backed matches count as local.
252250
const guardResult = await checkAndSyncSessionBeforeAction(guardSessionId, {
253-
localMessageCount,
251+
localMessages: allMessages,
254252
messages: {
255253
inFlight: "This session is already being processed — reconnecting to live updates",
256254
inputRequired: "Session is awaiting your input — please review before sending",
@@ -270,15 +268,18 @@ export default function ChatInterface({ selectedAgentName, selectedNamespace, se
270268
pendingRejectionReasonsRef.current = {};
271269
pendingTurnStatsRef.current = undefined;
272270

271+
const messageId = uuidv4();
272+
273273
// For new sessions or when no stored messages exist, show the user message immediately
274274
const userMessage: Message = {
275275
kind: "message",
276-
messageId: uuidv4(),
276+
messageId,
277277
role: "user",
278278
parts: [{
279279
kind: "text",
280280
text: userMessageText
281281
}],
282+
contextId: guardSessionId,
282283
metadata: {
283284
timestamp: Date.now()
284285
}
@@ -378,7 +379,6 @@ export default function ChatInterface({ selectedAgentName, selectedNamespace, se
378379
}
379380
}
380381

381-
const messageId = uuidv4();
382382
const a2aMessage = createMessage(userMessageText, "user", {
383383
messageId,
384384
contextId: currentSessionId,
@@ -597,6 +597,7 @@ export default function ChatInterface({ selectedAgentName, selectedNamespace, se
597597
guardSessionId: string,
598598
opts: {
599599
expectedTaskId?: string;
600+
localMessages?: Message[];
600601
localMessageCount?: number;
601602
messages: {
602603
inFlight: string;
@@ -648,9 +649,13 @@ export default function ChatInterface({ selectedAgentName, selectedNamespace, se
648649
return "blocked";
649650
}
650651

651-
if (opts.localMessageCount !== undefined) {
652+
if (opts.localMessages !== undefined || opts.localMessageCount !== undefined) {
652653
const dbMessages = extractMessagesFromTasks(tasksCheck.data);
653-
if (dbMessages.length > opts.localMessageCount) {
654+
const dbMessageCount = countSendGuardComparableMessages(dbMessages);
655+
const localMessageCount = opts.localMessages !== undefined
656+
? countBackendBackedComparableMessages(opts.localMessages, dbMessages)
657+
: opts.localMessageCount;
658+
if (localMessageCount !== undefined && dbMessageCount > localMessageCount) {
654659
await reloadSessionFromDB();
655660
toast.info(opts.messages.staleOrChanged);
656661
return "blocked";

0 commit comments

Comments
 (0)