Skip to content

Commit 23a1eea

Browse files
T3 Code Testclaude
andcommitted
fix(mobile): a resend during send-failure recovery no longer leaves a stale error
Recovery used to set the error only after it finished persisting the restored draft. The restored text is visible before that, so a user could resend it in the gap, clearing the error, and recovery would then set it again even though the resend went through. The error is now set before the merge publishes the text, so a resend always comes after it and clears it. Each path where recovery backs out withdraws the error it set. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 6dac48d commit 23a1eea

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎apps/mobile/src/state/use-thread-outbox-drain.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ import {
135135
clearPendingThreadCreationOutcome,
136136
pendingThreadCreationOutcomesAtom,
137137
} from "./pending-thread-creation";
138-
import { setThreadComposerError, threadComposerErrorsAtom } from "./thread-composer-error";
138+
import {
139+
clearThreadComposerError,
140+
setThreadComposerError,
141+
threadComposerErrorsAtom,
142+
} from "./thread-composer-error";
139143
import type { QueuedThreadMessage } from "./thread-outbox-model";
140144
import * as composerDrafts from "./use-composer-drafts";
141145
import { recoverFailedThreadDraft } from "./recover-failed-thread-draft";
@@ -759,6 +763,42 @@ describe("thread outbox recovery rollback", () => {
759763
).toEqual({ message: "rejected", messageId: message.messageId });
760764
});
761765

766+
it("leaves no error when the restored text is resent before recovery finishes", async () => {
767+
const message = queuedMessage({ messageId: "message-resent", text: "resend me" });
768+
const threadKey = `${message.environmentId}:${message.threadId}`;
769+
await harness.manager.enqueue(message);
770+
// The user sees the restored text as soon as the merge publishes it and
771+
// sends it again while the recovery is still awaiting persistence.
772+
const unsubscribe = appAtomRegistry.subscribe(composerDrafts.composerDraftsAtom, (drafts) => {
773+
if (drafts[threadKey]?.text === "resend me") {
774+
unsubscribe();
775+
clearThreadComposerError(threadKey);
776+
void composerDrafts.clearComposerDraftContent(threadKey);
777+
}
778+
});
779+
780+
await expect(restoreRejectedQueuedMessage(message, "rejected")).resolves.toBe("restored");
781+
782+
expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({});
783+
});
784+
785+
it("withdraws the error when an edit makes the recovery back out", async () => {
786+
const message = queuedMessage({ messageId: "message-edited-mid-recovery", text: "edit me" });
787+
const threadKey = `${message.environmentId}:${message.threadId}`;
788+
await harness.manager.enqueue(message);
789+
const unsubscribe = appAtomRegistry.subscribe(composerDrafts.composerDraftsAtom, (drafts) => {
790+
if (drafts[threadKey]?.text === "edit me") {
791+
unsubscribe();
792+
appAtomRegistry.set(editingQueuedMessageIdsAtom, { [message.messageId]: true });
793+
}
794+
});
795+
796+
await expect(restoreRejectedQueuedMessage(message, "rejected")).resolves.toBe("deferred");
797+
798+
expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({});
799+
expect(remainingMessages()).toEqual([message]);
800+
});
801+
762802
it("rolls a failed recovery merge back so the retry cannot duplicate the text", async () => {
763803
const message = queuedMessage({ messageId: "message-restore", text: "queued text" });
764804
const draftKey = `${message.environmentId}:${message.threadId}`;

‎apps/mobile/src/state/use-thread-outbox-drain.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,13 @@ export async function restoreRejectedQueuedMessage(
480480
return "blocked";
481481
}
482482

483+
// Shown before the merge publishes the text, so a resend of that text,
484+
// which can happen while this recovery still awaits persistence, clears
485+
// it. Withdrawn below wherever the recovery backs out.
486+
const withdrawError = () => clearThreadComposerError(threadKey, queuedMessage.messageId);
487+
if (!queuedMessage.creation) {
488+
setThreadComposerError(threadKey, message, queuedMessage.messageId);
489+
}
483490
let mergedDraft: ComposerDraft;
484491
try {
485492
stampRecoveryDraftProject(queuedMessage, draftKey);
@@ -499,6 +506,7 @@ export async function restoreRejectedQueuedMessage(
499506
rollback = { snapshot: originalDraft, merged: mergedDraft };
500507
}
501508
if (appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]) {
509+
withdrawError();
502510
await undoComposerDraftMerge(draftKey, originalDraft, mergedDraft);
503511
return "deferred";
504512
}
@@ -527,6 +535,7 @@ export async function restoreRejectedQueuedMessage(
527535
!(await confirmThreadOutboxMessageQueued(queuedMessage)) ||
528536
appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId]
529537
) {
538+
withdrawError();
530539
await undoComposerDraftMerge(draftKey, originalDraft, restoredDraft);
531540
return "deferred";
532541
}
@@ -539,6 +548,7 @@ export async function restoreRejectedQueuedMessage(
539548
() => !appAtomRegistry.get(editingQueuedMessageIdsAtom)[queuedMessage.messageId],
540549
))
541550
) {
551+
withdrawError();
542552
await undoComposerDraftMerge(draftKey, originalDraft, restoredDraft);
543553
return "deferred";
544554
}
@@ -553,8 +563,6 @@ export async function restoreRejectedQueuedMessage(
553563
message: queuedMessage,
554564
reason: message,
555565
});
556-
} else {
557-
setThreadComposerError(threadKey, message, queuedMessage.messageId);
558566
}
559567
return "restored";
560568
} catch (error) {

0 commit comments

Comments
 (0)