Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(chat): clear a "Failed to send" bubble once its message is echoed back (#17639)

Hiep Lef4de8c826ef0
diff --git a/.github/pr-assets/pr-17639-01-regression-test-fails-on-main.png b/.github/pr-assets/pr-17639-01-regression-test-fails-on-main.png
new file mode 100644
index 0000000000..973fe8419d
Binary files /dev/null and b/.github/pr-assets/pr-17639-01-regression-test-fails-on-main.png differ
diff --git a/.github/pr-assets/pr-17639-02-suites-pass-with-fix.png b/.github/pr-assets/pr-17639-02-suites-pass-with-fix.png
new file mode 100644
index 0000000000..34e215ceb0
Binary files /dev/null and b/.github/pr-assets/pr-17639-02-suites-pass-with-fix.png differ
diff --git a/.pr/desktop-universal-boot.png b/.pr/desktop-universal-boot.png
deleted file mode 100644
index 8133b40554..0000000000
Binary files a/.pr/desktop-universal-boot.png and /dev/null differ
diff --git a/__tests__/stores/optimistic-user-message-store.test.ts b/__tests__/stores/optimistic-user-message-store.test.ts
index c768ae6c73..710d4d1280 100644
--- a/__tests__/stores/optimistic-user-message-store.test.ts
+++ b/__tests__/stores/optimistic-user-message-store.test.ts
@@ -166,7 +166,7 @@ describe("optimistic-user-message-store", () => {
expect(remaining[0].status).toBe("error");
});
- it("consumeMatchingPendingMessage is a no-op when nothing is sending", () => {
+ it("consumeMatchingPendingMessage is a no-op when only errored entries exist and none match exactly", () => {
const store = useOptimisticUserMessageStore.getState();
const id = store.enqueuePendingMessage({
conversationId: CONVO,
@@ -174,7 +174,7 @@ describe("optimistic-user-message-store", () => {
});
store.markPendingMessageError(id, "boom");
- const consumed = store.consumeMatchingPendingMessage(CONVO, "broken");
+ const consumed = store.consumeMatchingPendingMessage(CONVO, "unrelated");
expect(consumed).toBeNull();
expect(
@@ -182,6 +182,26 @@ describe("optimistic-user-message-store", () => {
).toHaveLength(1);
});
+ it("consumeMatchingPendingMessage clears an errored entry when the echo matches its content exactly", () => {
+ const store = useOptimisticUserMessageStore.getState();
+ const id = store.enqueuePendingMessage({
+ conversationId: CONVO,
+ text: "late echo",
+ });
+ // The watchdog gives up before the server echoes the message back.
+ vi.advanceTimersByTime(PENDING_MESSAGE_TIMEOUT_MS);
+ expect(
+ useOptimisticUserMessageStore.getState().pendingMessages[0].status,
+ ).toBe("error");
+
+ const consumed = store.consumeMatchingPendingMessage(CONVO, "late echo");
+
+ expect(consumed?.id).toBe(id);
+ expect(
+ useOptimisticUserMessageStore.getState().pendingMessages,
+ ).toHaveLength(0);
+ });
+
it("consumeMatchingPendingMessage only consumes entries for the given conversation", () => {
const store = useOptimisticUserMessageStore.getState();
const aId = store.enqueuePendingMessage({
diff --git a/src/stores/optimistic-user-message-store.ts b/src/stores/optimistic-user-message-store.ts
index 69490d6177..9cb49ab6bf 100644
--- a/src/stores/optimistic-user-message-store.ts
+++ b/src/stores/optimistic-user-message-store.ts
@@ -71,12 +71,14 @@ interface OptimisticUserMessageActions {
/**
* Remove the pending message that matches the given echoed `content` in
* the given conversation. Matching is done by exact content equality on
- * messages still in "sending" state; if no match exists we fall back to
- * removing the oldest "sending" entry in that conversation so that an echo
- * with a slightly munged body (e.g. trailing-whitespace stripped by the
- * server) still clears its bubble. Scoping by `conversationId` ensures a
- * stale ack for one conversation never pops a pending entry belonging to
- * another.
+ * messages in either state: an echo with identical text proves the message
+ * was delivered, so it also clears an entry the watchdog already flipped to
+ * "error". If no exact match exists we fall back to removing the oldest
+ * "sending" entry in that conversation so that an echo with a slightly
+ * munged body (e.g. trailing-whitespace stripped by the server) still
+ * clears its bubble; the fallback never touches "error" entries. Scoping by
+ * `conversationId` ensures a stale ack for one conversation never pops a
+ * pending entry belonging to another.
*/
consumeMatchingPendingMessage: (
conversationId: string,
@@ -170,22 +172,25 @@ export const useOptimisticUserMessageStore = create<OptimisticUserMessageStore>(
// Single atomic `set` so the find + filter can't observe an interleaved
// mutation from another action. We prefer an exact content match (this
// is what makes out-of-order echoes safe: an echo of "world" will pop
- // the "world" bubble, not the older "hello" one). If no exact match
- // exists β€” e.g. the server slightly munged the body β€” fall back to the
- // oldest "sending" entry in this conversation so the user doesn't end
- // up with a permanently-stuck bubble in the happy-path single-message
- // case.
+ // the "world" bubble, not the older "hello" one). The exact match may
+ // hit an entry already in "error" state: the watchdog flips a bubble to
+ // "error" on a timer, so a late echo (reconnect replay, slow server-side
+ // accept, returning from another conversation) proves the message was
+ // delivered after all and must clear the stale failure. If no exact
+ // match exists β€” e.g. the server slightly munged the body β€” fall back
+ // to the oldest "sending" entry in this conversation so the user doesn't
+ // end up with a permanently-stuck bubble in the happy-path
+ // single-message case. The fallback deliberately skips "error" entries
+ // so an unrelated echo can never wipe a genuinely failed bubble.
let consumed: PendingUserMessage | null = null;
set((state) => {
- const sending = state.pendingMessages
+ const inConversation = state.pendingMessages
.map((m, i) => ({ m, i }))
- .filter(
- ({ m }) =>
- m.status === "sending" && m.conversationId === conversationId,
- );
- if (sending.length === 0) return state;
- const exact = sending.find(({ m }) => m.content === content);
- const target = exact ?? sending[0];
+ .filter(({ m }) => m.conversationId === conversationId);
+ const exact = inConversation.find(({ m }) => m.content === content);
+ const target =
+ exact ?? inConversation.find(({ m }) => m.status === "sending");
+ if (!target) return state;
consumed = target.m;
return {
pendingMessages: [