Skip to content

Commit f686257

Browse files
fix(ui): preserve repeated late finals
1 parent ebb1d35 commit f686257

2 files changed

Lines changed: 67 additions & 6 deletions

File tree

‎ui/src/ui/controllers/chat.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,55 @@ describe("loadChatHistory retry handling", () => {
24382438
expect(state.chatMessages).toEqual([persistedUser, liveAssistant]);
24392439
});
24402440

2441+
it("keeps a newer repeated final when stale history has older reasoning with the same text", async () => {
2442+
const olderUser = {
2443+
role: "user",
2444+
content: [{ type: "text", text: "First ask" }],
2445+
__openclaw: { seq: 1 },
2446+
};
2447+
const olderReasoningAssistant = {
2448+
role: "assistant",
2449+
content: [
2450+
{ type: "thinking", thinking: "older private reasoning" },
2451+
{ type: "text", text: "Repeated final" },
2452+
],
2453+
timestamp: 100,
2454+
__openclaw: { seq: 2 },
2455+
};
2456+
const newerUser = {
2457+
role: "user",
2458+
content: [{ type: "text", text: "Ask again" }],
2459+
timestamp: 200,
2460+
};
2461+
const newerLiveAssistant = {
2462+
role: "assistant",
2463+
content: [{ type: "text", text: "Repeated final" }],
2464+
timestamp: 300,
2465+
};
2466+
const history = createDeferred<{ messages: unknown[]; thinkingLevel: string }>();
2467+
const request = vi.fn(() => history.promise);
2468+
const state = createState({
2469+
connected: true,
2470+
client: { request } as unknown as ChatState["client"],
2471+
chatMessages: [olderUser, olderReasoningAssistant],
2472+
});
2473+
2474+
const load = loadChatHistory(state);
2475+
state.chatMessages = [olderUser, olderReasoningAssistant, newerUser, newerLiveAssistant];
2476+
history.resolve({
2477+
messages: [olderUser, olderReasoningAssistant],
2478+
thinkingLevel: "low",
2479+
});
2480+
await load;
2481+
2482+
expect(state.chatMessages).toEqual([
2483+
olderUser,
2484+
olderReasoningAssistant,
2485+
newerUser,
2486+
newerLiveAssistant,
2487+
]);
2488+
});
2489+
24412490
it("keeps active streamed assistant text when history reload returns a stale snapshot", async () => {
24422491
const persistedUser = {
24432492
role: "user",

‎ui/src/ui/controllers/chat.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,31 @@ function historyHasSameOrNewerDisplayMessage(
251251
historyMessages: unknown[],
252252
signature: string,
253253
message: unknown,
254+
previousMessages: unknown[] = [],
254255
): boolean {
255256
return historyMessages.some((historyMessage) => {
256257
if (messageDisplaySignature(historyMessage) !== signature) {
257258
return false;
258259
}
259-
const role = normalizeLowercaseStringOrEmpty((message as { role?: unknown }).role);
260-
if (role === "assistant" && extractThinking(historyMessage) && !extractThinking(message)) {
261-
return true;
262-
}
263260
const timestamp = messageTimestampMs(message);
264261
if (timestamp == null) {
265262
return true;
266263
}
267264
const historyTimestamp = messageTimestampMs(historyMessage);
268-
return historyTimestamp != null && historyTimestamp >= timestamp;
265+
if (historyTimestamp != null && historyTimestamp >= timestamp) {
266+
return true;
267+
}
268+
const role = normalizeLowercaseStringOrEmpty((message as { role?: unknown }).role);
269+
return (
270+
role === "assistant" &&
271+
Boolean(extractThinking(historyMessage)) &&
272+
!extractThinking(message) &&
273+
!previousMessages.some(
274+
(previousMessage) =>
275+
messageDisplaySignature(previousMessage) === signature &&
276+
Boolean(extractThinking(previousMessage)),
277+
)
278+
);
269279
});
270280
}
271281

@@ -342,7 +352,9 @@ function collectLateOptimisticTailMessages(
342352
if (!signature) {
343353
return [];
344354
}
345-
if (historyHasSameOrNewerDisplayMessage(historyMessages, signature, message)) {
355+
if (
356+
historyHasSameOrNewerDisplayMessage(historyMessages, signature, message, previousMessages)
357+
) {
346358
continue;
347359
}
348360
lateTail.push(message);

0 commit comments

Comments
 (0)