Skip to content

Commit e946d22

Browse files
authored
fix(ui): keep live chat sends pending (#131266)
1 parent cfc6370 commit e946d22

3 files changed

Lines changed: 7 additions & 122 deletions

File tree

‎ui/src/pages/chat/chat-outbox-drain.ts‎

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
type ChatCommandResetOptions,
1818
} from "./chat-commands.ts";
1919
import { loadChatHistory, type ChatHistoryResult } from "./chat-history.ts";
20-
import { chatOutboxOwner } from "./chat-outbox-owner.ts";
2120
import {
2221
consumeChatOutboxRetry,
2322
retryableGatewayDelayMs,
@@ -98,8 +97,6 @@ type StoredChatOutboxDrainLane = {
9897
type StoredChatOutboxClientState = {
9998
lanes: Map<string, StoredChatOutboxDrainLane>;
10099
};
101-
const STORED_OUTBOX_CONFIRMATION_GRACE_MS = 5_000;
102-
const STORED_OUTBOX_RETRY_DEFAULT_MS = 500;
103100
export const UNCONFIRMED_CHAT_SEND_ERROR =
104101
"Reconnected before delivery was confirmed. Check the conversation — retry only if your message didn't arrive.";
105102
const UNCERTAIN_CLEAR_SUCCESSOR_ERROR =
@@ -152,10 +149,6 @@ function sameQueuedDeliveryVersion(left: ChatQueueItem, right: ChatQueueItem): b
152149
);
153150
}
154151

155-
function queuedDeliveryVersion(item: ChatQueueItem): string {
156-
return `${item.id}\u0000${item.sendRunId ?? ""}\u0000${item.sendAttempts ?? 0}`;
157-
}
158-
159152
function sessionRunProvesQueuedDelivery(
160153
sessionInfo: ChatHistoryResult["sessionInfo"],
161154
item: ChatQueueItem,
@@ -166,7 +159,6 @@ function sessionRunProvesQueuedDelivery(
166159
sessionInfo?.lastRunId === item.sendRunId),
167160
);
168161
}
169-
170162
async function readCurrentStoredChatHistory(
171163
host: ChatHost,
172164
outbox: StoredChatOutbox,
@@ -297,24 +289,19 @@ async function reconcileStoredChatOutboxHead(
297289
return "blocked";
298290
}
299291
}
300-
const outboxOwner = chatOutboxOwner(host);
301-
const clearConfirmationGrace = () => outboxOwner.clearConfirmationGrace(outbox, item.id);
302292
const historyArgs = [host, outbox, item, client, connectionEpoch, dependencies] as const;
303293
const history = await readCurrentStoredChatHistory(...historyArgs);
304294
// Keyed unknown sends reach history only for exact proof; absence stays blocked.
305295
if (history === "blocked" || history === "continue" || item.sendState === "unconfirmed") {
306-
clearConfirmationGrace();
307296
return history === "continue" ? "continue" : "blocked";
308297
}
309298
if (visibleSessionMatches(host, outbox.sessionKey, outbox.agentId) && isChatBusy(host)) {
310-
clearConfirmationGrace();
311299
return "blocked";
312300
}
313301
if ((item.sendAttempts ?? 0) > 0) {
314302
// History and run metadata are non-atomic; verify idle before parking unknown.
315303
const verifiedHistory = await readCurrentStoredChatHistory(...historyArgs);
316304
if (verifiedHistory === "blocked" || verifiedHistory === "continue") {
317-
clearConfirmationGrace();
318305
return verifiedHistory;
319306
}
320307
const liveSendCurrent = anyChatOutboxPaneMatches(host, (pane) => {
@@ -325,27 +312,9 @@ async function reconcileStoredChatOutboxHead(
325312
);
326313
});
327314
if (liveSendCurrent) {
328-
// Start the bound only after idle is verified; a valid run can outlive the send request.
329-
const now = Date.now();
330-
const deadlineMs = outboxOwner.confirmationDeadline(
331-
outbox,
332-
item.id,
333-
queuedDeliveryVersion(item),
334-
now,
335-
STORED_OUTBOX_CONFIRMATION_GRACE_MS,
336-
);
337-
if (deadlineMs !== null && now < deadlineMs) {
338-
scheduleStoredChatOutboxRetry(
339-
host,
340-
outbox,
341-
Math.min(STORED_OUTBOX_RETRY_DEFAULT_MS, deadlineMs - now),
342-
dependencies,
343-
false,
344-
);
345-
return "blocked";
346-
}
315+
// Elapsed time cannot turn a current-connection send into reconnect uncertainty.
316+
return "blocked";
347317
}
348-
clearConfirmationGrace();
349318
const parked = updateQueuedMessageForSession(host, outbox.sessionKey, item.id, (entry) => ({
350319
...entry,
351320
sendError: UNCONFIRMED_CHAT_SEND_ERROR,

‎ui/src/pages/chat/chat-outbox-owner.ts‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ type HostProjection = {
1515
durableSeen: Set<string>;
1616
retryable: Set<string>;
1717
};
18-
type LiveProjection = {
19-
confirmationGrace?: { deadlineMs: number; deliveryVersion: string };
20-
item: ChatQueueItem;
21-
owner: Host;
22-
};
18+
type LiveProjection = { item: ChatQueueItem; owner: Host };
2319
const LIVE_VERSION_KEYS = ["sendRunId", "sendAttempts", "sendState", "sendError"] as const;
2420
const routePresentation = new WeakSet<ChatQueueItem>();
2521
const storageIds = new WeakMap<Storage, number>();
@@ -237,30 +233,6 @@ class ChatOutboxGatewayOwner {
237233
hasVolatile(host: Host, id: string): boolean {
238234
return this.hosts.get(host)?.retryable.has(id) ?? false;
239235
}
240-
confirmationDeadline(
241-
scope: Scope,
242-
itemId: string,
243-
deliveryVersion: string,
244-
now: number,
245-
graceMs: number,
246-
): number | null {
247-
const live = this.live.get(storedChatOutboxScopeKey(scope))?.get(itemId);
248-
if (!live) {
249-
return null;
250-
}
251-
if (live.confirmationGrace?.deliveryVersion === deliveryVersion) {
252-
return live.confirmationGrace.deadlineMs;
253-
}
254-
const deadlineMs = now + graceMs;
255-
live.confirmationGrace = { deadlineMs, deliveryVersion };
256-
return deadlineMs;
257-
}
258-
clearConfirmationGrace(scope: Scope, itemId: string): void {
259-
const live = this.live.get(storedChatOutboxScopeKey(scope))?.get(itemId);
260-
if (live) {
261-
delete live.confirmationGrace;
262-
}
263-
}
264236
// Panes share this outbox and its drain while composer state stays per pane, so
265237
// a pane-local fact that blocks delivery has to be answerable from any of them.
266238
anyPane(matches: (host: Host) => boolean): boolean {
@@ -285,7 +257,7 @@ class ChatOutboxGatewayOwner {
285257
const key = storedChatOutboxScopeKey(scope);
286258
const live = this.live.get(key) ?? new Map<string, LiveProjection>();
287259
if (item) {
288-
live.set(id, { confirmationGrace: live.get(id)?.confirmationGrace, item, owner: host });
260+
live.set(id, { item, owner: host });
289261
this.live.set(key, live);
290262
} else {
291263
live.delete(id);

‎ui/src/pages/chat/chat-send.test.ts‎

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ function writeChatQueueForScope(
8383
chatOutboxOwner(host).replace(host, scope, queue);
8484
}
8585

86-
function setTransientQueuedMessageProjection(
87-
host: TestChatHost,
88-
sessionKey: string,
89-
item: ChatQueueItem,
90-
): boolean {
91-
const owner = chatOutboxOwner(host);
92-
const scope = resolveStoredChatOutboxScope(host, sessionKey, item.agentId);
93-
if (!owner.durable(host, item.id)) {
94-
return false;
95-
}
96-
owner.projectLive(host, scope, item.id, item);
97-
return true;
98-
}
99-
10086
function requireChatMessageCache(host: ChatHost): ChatMessageCache {
10187
if (!host.chatMessagesBySession) {
10288
throw new Error("Expected chat message cache");
@@ -6074,15 +6060,13 @@ describe("handleSendChat", () => {
60746060

60756061
expect(host.chatQueue[0]?.sendState).toBe("sending");
60766062
expect(host.lastError).toBeNull();
6077-
await waitForFast(() => expect(listStoredChatOutboxes(host)).toStrictEqual([]), {
6078-
timeout: 1_000,
6079-
});
6063+
await flushChatQueueForEvent(host);
60806064
expect(historyRequests).toBeGreaterThanOrEqual(3);
60816065
expect(host.chatQueue).toStrictEqual([]);
60826066
expect(host.lastError).toBeNull();
60836067
});
60846068

6085-
it("marks an acknowledged live send unconfirmed after history stays missing", async () => {
6069+
it("keeps an acknowledged live send pending while its connection stays current", async () => {
60866070
let now = 1_000;
60876071
vi.spyOn(Date, "now").mockImplementation(() => now);
60886072
const host = makeChatHost({
@@ -6107,48 +6091,8 @@ describe("handleSendChat", () => {
61076091
await flushChatQueueForEvent(host);
61086092

61096093
expect(host.chatQueue[0]).toMatchObject({
6110-
sendError: UNCONFIRMED_CHAT_SEND_ERROR,
6111-
sendState: "unconfirmed",
6112-
text: "history never catches up",
6113-
});
6114-
// The parked bubble's inline footer owns the outcome; no pane banner.
6115-
expect(host.lastError).toBeNull();
6116-
});
6117-
6118-
it("starts a fresh confirmation grace after the prior outbox scope is removed", async () => {
6119-
let now = 1_000;
6120-
vi.spyOn(Date, "now").mockImplementation(() => now);
6121-
const host = makeChatHost({
6122-
requestHandlers: {
6123-
"chat.history": () => idleChatHistory(),
6124-
"chat.send": (params: unknown) => {
6125-
const payload = requireRecord(params, "live send payload");
6126-
const runId = String(payload.idempotencyKey);
6127-
return Promise.resolve({ runId, status: "started" });
6128-
},
6129-
},
6130-
});
6131-
6132-
await handleSendChat(host, "reuse this delivery version");
6133-
host.chatRunId = null;
6134-
await flushChatQueueForEvent(host);
6135-
6136-
const item = expectDefined(host.chatQueue[0], "queued live send");
6137-
const sessionKey = expectDefined(item.sessionKey, "queued session key");
6138-
expect(removeQueuedMessage(host, item.id)).toBe("removed");
6139-
6140-
now = 5_500;
6141-
expect(admitQueuedMessageForSession(host, sessionKey, item)).toBe(true);
6142-
expect(setTransientQueuedMessageProjection(host, sessionKey, item)).toBe(true);
6143-
await flushChatQueueForEvent(host);
6144-
6145-
now = 6_001;
6146-
await flushChatQueueForEvent(host);
6147-
6148-
expect(host.chatQueue[0]).toMatchObject({
6149-
id: item.id,
61506094
sendState: "sending",
6151-
text: "reuse this delivery version",
6095+
text: "history never catches up",
61526096
});
61536097
expect(host.lastError).toBeNull();
61546098
});

0 commit comments

Comments
 (0)