fix(tui): tolerate clock skew in pending-history reconciliation

This commit is contained in:
Vincent Koc
2026-04-03 20:48:08 +09:00
parent a3f34a8f77
commit f0a4423271
2 changed files with 17 additions and 2 deletions

View File

@@ -138,12 +138,23 @@ describe("ChatLog", () => {
expect(chatLog.countPendingUsers()).toBe(0);
});
it("reconciles pending users when the gateway clock is slightly behind the client", () => {
const chatLog = new ChatLog(40);
chatLog.addPendingUser("run-1", "queued hello", 65_000);
expect(chatLog.reconcilePendingUsers([{ text: "queued hello", timestamp: 20_000 }])).toEqual([
"run-1",
]);
expect(chatLog.countPendingUsers()).toBe(0);
});
it("does not hide a new repeated prompt when only older history matches", () => {
const chatLog = new ChatLog(40);
chatLog.addPendingUser("run-1", "continue", 5_000);
expect(chatLog.reconcilePendingUsers([{ text: "continue", timestamp: 4_000 }])).toEqual([]);
expect(chatLog.reconcilePendingUsers([{ text: "continue", timestamp: -56_000 }])).toEqual([]);
expect(chatLog.countPendingUsers()).toBe(1);
});
});

View File

@@ -6,6 +6,8 @@ import { BtwInlineMessage } from "./btw-inline-message.js";
import { ToolExecutionComponent } from "./tool-execution.js";
import { UserMessageComponent } from "./user-message.js";
const PENDING_HISTORY_CLOCK_SKEW_TOLERANCE_MS = 60_000;
export class ChatLog extends Container {
private readonly maxComponents: number;
private toolById = new Map<string, ToolExecutionComponent>();
@@ -156,7 +158,9 @@ export class ChatLog extends Container {
}
const matchIndex = normalizedHistory.findIndex(
(historyEntry) =>
historyEntry.text === pendingText && (historyEntry.timestamp ?? 0) >= entry.createdAt,
historyEntry.text === pendingText &&
(historyEntry.timestamp ?? 0) >=
entry.createdAt - PENDING_HISTORY_CLOCK_SKEW_TOLERANCE_MS,
);
if (matchIndex === -1) {
continue;