Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(chat): clear answered question cards whose server row lost its batch id (#23063)

YuTengjingf0ce2becf503
diff --git a/src/store/chat/utils/interventionSync.test.ts b/src/store/chat/utils/interventionSync.test.ts
index ec7bf14776f..75999894ffd 100644
--- a/src/store/chat/utils/interventionSync.test.ts
+++ b/src/store/chat/utils/interventionSync.test.ts
@@ -1,6 +1,8 @@
import type { UIChatMessage } from '@lobechat/types';
import { describe, expect, it } from 'vitest';
+import { mergeFetchedMessagesWithLocalState } from '@/store/chat/slices/message/transcript/mergeFetched';
+
import {
hasPendingInterventions,
reconcileIntervention,
@@ -100,6 +102,29 @@ describe('cross-device intervention reconciliation', () => {
).toBeUndefined();
});
+ it('settles a resolving card from a terminal row whose producer write dropped the batch identity', () => {
+ // Producer ACK writes replace the whole intervention with `{ status, rejectedReason }`,
+ // so the server row loses operationId/batchId. The local card still carries them,
+ // plus a client-clock updatedAt that can be newer than the server's.
+ const resolving: UIChatMessage = {
+ ...question('pending', 2000),
+ pluginIntervention: {
+ batchId: 'batch-1',
+ operationId: 'op-1',
+ resolving: true,
+ status: 'pending',
+ },
+ };
+ const skipped: UIChatMessage = {
+ ...question('rejected', 1999),
+ pluginIntervention: { rejectedReason: 'user_cancelled', status: 'rejected' },
+ };
+ expect(reconcileIntervention(resolving, skipped)).toBe(skipped);
+ expect(
+ mergeFetchedMessagesWithLocalState([skipped], [resolving], new Set(), new Set()),
+ ).toEqual([skipped]);
+ });
+
it('supports legacy plugin intervention rows and ignores optimistic rows', () => {
const legacy = {
...question('pending'),
diff --git a/src/store/chat/utils/interventionSync.ts b/src/store/chat/utils/interventionSync.ts
index edbc5a2fd24..21349e146f7 100644
--- a/src/store/chat/utils/interventionSync.ts
+++ b/src/store/chat/utils/interventionSync.ts
@@ -23,6 +23,26 @@ export const hasPendingInterventions = (messages: UIChatMessage[]) =>
const isTerminal = (status: string | undefined) =>
status === 'approved' || status === 'rejected' || status === 'aborted';
+/**
+ * A server row that names a different operation or batch belongs to another
+ * request on the same tool call and must not settle this one.
+ */
+const isSameRequest = (
+ previous: ReturnType<typeof intervention>,
+ next: ReturnType<typeof intervention>,
+) => previous?.operationId === next?.operationId && previous?.batchId === next?.batchId;
+
+/**
+ * A terminal row with no request identity is still the server's answer for
+ * this tool call: the producer's ACK write replaces the whole intervention with
+ * `{ status, rejectedReason }`, dropping the operationId/batchId the pending
+ * card carries. A tool message is never re-raised after a terminal state
+ * (`reduceInterventionRequest` keeps the earlier terminal), so such a row
+ * cannot belong to a newer request than the local pending one.
+ */
+const isUnscopedTerminal = (next: ReturnType<typeof intervention>) =>
+ isTerminal(next?.status) && !next?.operationId && !next?.batchId;
+
/** Plugin rows can change without advancing the parent message's updatedAt. */
export const reconcileIntervention = (
local: UIChatMessage,
@@ -34,12 +54,16 @@ export const reconcileIntervention = (
local.role !== 'tool' ||
incoming.role !== 'tool' ||
local.id !== incoming.id ||
- local.tool_call_id !== incoming.tool_call_id ||
- previous?.operationId !== next?.operationId ||
- previous?.batchId !== next?.batchId
+ local.tool_call_id !== incoming.tool_call_id
)
return;
+ // Without this, the head merge falls back to updatedAt, where the card's
+ // client-clock timestamp can beat the server row and keep the answered card
+ // pending, even across reloads, because the stale row is persisted.
+ if (previous?.status === 'pending' && isUnscopedTerminal(next)) return incoming;
+ if (!isSameRequest(previous, next)) return;
+
if (previous?.status === 'pending' && isTerminal(next?.status)) return incoming;
// Continuation startup can fail and restore this request to pending.
if (