Skip to content

Commit 6dac48d

Browse files
T3 Code Testclaude
andcommitted
fix(mobile): keep send errors in one map and clear them when they stop applying
- One keep-alive map instead of an atom per opened thread, so dismissed errors don't accumulate. - Errors from the outbox record which message they describe. If that message is delivered on a later retry, its error clears. An error about a different message stays. - Removing an environment clears its threads' errors with its drafts and outbox. - Announce the error to VoiceOver; accessibilityLiveRegion only reaches TalkBack. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5a1f594 commit 6dac48d

8 files changed

Lines changed: 139 additions & 26 deletions

‎apps/mobile/src/connection/platform.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as MobileStorage from "../persistence/mobile-storage";
2727
import { appAtomRegistry } from "../state/atom-registry";
2828
import { clearThreadOutboxEnvironment } from "../state/thread-outbox-removal";
2929
import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts";
30+
import { clearThreadComposerErrorsForEnvironment } from "../state/thread-composer-error";
3031
import { mobileApplicationActiveWakeup } from "./app-state-wakeups";
3132
import { connectionStorageLayer } from "./storage";
3233

@@ -218,6 +219,7 @@ const environmentOwnedDataCleanupLayer = Layer.succeed(
218219
[
219220
Effect.promise(() => clearThreadOutboxEnvironment(environmentId)),
220221
Effect.promise(() => clearComposerDraftsEnvironment(environmentId)),
222+
Effect.sync(() => clearThreadComposerErrorsForEnvironment(environmentId)),
221223
],
222224
{ concurrency: "unbounded", discard: true },
223225
).pipe(

‎apps/mobile/src/features/threads/ComposerErrorNotice.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Pressable, View } from "react-native";
1+
import { useEffect } from "react";
2+
import { AccessibilityInfo, Platform, Pressable, View } from "react-native";
23

34
import { AppText as Text } from "../../components/AppText";
45
import { SymbolView } from "../../components/AppSymbol";
@@ -11,6 +12,13 @@ export function ComposerErrorNotice({
1112
readonly message: string;
1213
readonly onDismiss: () => void;
1314
}) {
15+
// accessibilityLiveRegion below only reaches TalkBack; VoiceOver needs an
16+
// explicit announcement.
17+
useEffect(() => {
18+
if (Platform.OS === "ios") {
19+
AccessibilityInfo.announceForAccessibility(message);
20+
}
21+
}, [message]);
1422
return (
1523
<View className="px-4 pb-3">
1624
<View className="flex-row items-start gap-3 rounded-[20px] border-continuous bg-card p-4">

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ import { useEnvironmentQuery } from "../../state/query";
9494
import { threadDevicePreviews } from "../devices/threadDevicePreviews";
9595
import type { QueuedThreadMessage } from "../../state/thread-outbox-model";
9696
import { scopedThreadKey } from "../../lib/scopedEntities";
97-
import { setThreadComposerError, threadComposerErrorAtom } from "../../state/thread-composer-error";
97+
import {
98+
clearThreadComposerError,
99+
threadComposerErrorsAtom,
100+
} from "../../state/thread-composer-error";
98101
import { threadEnvironment } from "../../state/threads";
99102
import { useAtomCommand } from "../../state/use-atom-command";
100103
import { useDelayedStatus } from "../../lib/useDelayedStatus";
@@ -365,7 +368,7 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
365368
const navigationHeaderHeight = useContext(HeaderHeightContext) || insets.top + 44;
366369
const agentLabel = `${props.selectedThread.modelSelection.instanceId} agent`;
367370
const selectedThreadKey = scopedThreadKey(props.environmentId, props.selectedThread.id);
368-
const composerError = useAtomValue(threadComposerErrorAtom(selectedThreadKey));
371+
const composerError = useAtomValue(threadComposerErrorsAtom)[selectedThreadKey]?.message ?? null;
369372
const queuedCount = useThreadQueuedCount({
370373
environmentId: props.environmentId,
371374
threadId: props.selectedThread.id,
@@ -1202,7 +1205,7 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
12021205
>
12031206
<ComposerErrorNotice
12041207
message={composerError}
1205-
onDismiss={() => setThreadComposerError(selectedThreadKey, null)}
1208+
onDismiss={() => clearThreadComposerError(selectedThreadKey)}
12061209
/>
12071210
</Animated.View>
12081211
) : null}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { afterEach, describe, expect, it } from "vite-plus/test";
2+
3+
import { appAtomRegistry } from "./atom-registry";
4+
import {
5+
clearThreadComposerError,
6+
clearThreadComposerErrorsForEnvironment,
7+
setThreadComposerError,
8+
threadComposerErrorsAtom,
9+
} from "./thread-composer-error";
10+
11+
afterEach(() => {
12+
appAtomRegistry.set(threadComposerErrorsAtom, {});
13+
});
14+
15+
describe("thread composer errors", () => {
16+
it("clears a message-scoped error only for that message", () => {
17+
setThreadComposerError("environment-1:thread-1", "rejected", "message-1");
18+
19+
clearThreadComposerError("environment-1:thread-1", "message-2");
20+
expect(appAtomRegistry.get(threadComposerErrorsAtom)["environment-1:thread-1"]?.message).toBe(
21+
"rejected",
22+
);
23+
24+
clearThreadComposerError("environment-1:thread-1");
25+
expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({});
26+
});
27+
28+
it("clears every thread's error for a removed environment and no others", () => {
29+
setThreadComposerError("environment-1:thread-1", "one");
30+
setThreadComposerError("environment-1:thread-2", "two");
31+
setThreadComposerError("environment-10:thread-1", "other environment");
32+
33+
clearThreadComposerErrorsForEnvironment("environment-1");
34+
35+
expect(Object.keys(appAtomRegistry.get(threadComposerErrorsAtom))).toEqual([
36+
"environment-10:thread-1",
37+
]);
38+
});
39+
});

‎apps/mobile/src/state/thread-composer-error.ts‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,55 @@ import { Atom } from "effect/unstable/reactivity";
22

33
import { appAtomRegistry } from "./atom-registry";
44

5+
interface ThreadComposerError {
6+
readonly message: string;
7+
/** The queued message this error is about, when it is about one. */
8+
readonly messageId: string | null;
9+
}
10+
511
/**
612
* Why a thread's last message did not go out, shown above that thread's
713
* composer. The outbox drain can reject a message after the user has left the
8-
* thread, so the reason is kept per thread until they dismiss it or send again.
9-
* Keyed by `scopedThreadKey`.
14+
* thread, so the reason is kept per thread until they dismiss it, send again,
15+
* or the message it describes is delivered after all. Keyed by `scopedThreadKey`.
1016
*/
11-
export const threadComposerErrorAtom = Atom.family((threadKey: string) =>
12-
Atom.make<string | null>(null).pipe(
13-
Atom.keepAlive,
14-
Atom.withLabel(`mobile:thread-composer-error:${threadKey}`),
15-
),
16-
);
17+
export const threadComposerErrorsAtom = Atom.make<Readonly<Record<string, ThreadComposerError>>>(
18+
{},
19+
).pipe(Atom.keepAlive, Atom.withLabel("mobile:thread-composer-errors"));
20+
21+
export function setThreadComposerError(
22+
threadKey: string,
23+
message: string,
24+
messageId: string | null = null,
25+
): void {
26+
appAtomRegistry.set(threadComposerErrorsAtom, {
27+
...appAtomRegistry.get(threadComposerErrorsAtom),
28+
[threadKey]: { message, messageId },
29+
});
30+
}
31+
32+
/** With `messageId`, clears only an error that describes that message. */
33+
export function clearThreadComposerError(threadKey: string, messageId?: string): void {
34+
const current = appAtomRegistry.get(threadComposerErrorsAtom);
35+
const error = current[threadKey];
36+
if (!error || (messageId !== undefined && error.messageId !== messageId)) {
37+
return;
38+
}
39+
const next = { ...current };
40+
delete next[threadKey];
41+
appAtomRegistry.set(threadComposerErrorsAtom, next);
42+
}
1743

18-
export function setThreadComposerError(threadKey: string, message: string | null): void {
19-
appAtomRegistry.set(threadComposerErrorAtom(threadKey), message);
44+
export function clearThreadComposerErrorsForEnvironment(environmentId: string): void {
45+
const current = appAtomRegistry.get(threadComposerErrorsAtom);
46+
const prefix = `${environmentId}:`;
47+
const keys = Object.keys(current).filter((key) => key.startsWith(prefix));
48+
if (keys.length === 0) {
49+
return;
50+
}
51+
const next = { ...current };
52+
for (const key of keys) {
53+
delete next[key];
54+
}
55+
appAtomRegistry.set(threadComposerErrorsAtom, next);
2056
}

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import {
9696
useQueuedRunEdit,
9797
} from "./queued-run-edit";
9898
import { setPendingConnectionError } from "../state/use-remote-environment-registry";
99-
import { setThreadComposerError } from "./thread-composer-error";
99+
import { clearThreadComposerError, setThreadComposerError } from "./thread-composer-error";
100100
import {
101101
useSelectedThreadProjection,
102102
useSelectedThreadVisibleTurnItems,
@@ -673,7 +673,7 @@ export function useThreadComposerState() {
673673
const metadata = makeQueuedMessageMetadata();
674674
const messageId = MessageId.make(metadata.messageId);
675675
// A new send supersedes the reason the previous one bounced back.
676-
setThreadComposerError(threadKey, null);
676+
clearThreadComposerError(threadKey);
677677
// Enqueue publishes the queued atom synchronously (the durable write
678678
// happens behind it), so clearing the draft here gives send feedback on
679679
// the tap frame instead of after file I/O. If the write fails the message

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ import {
135135
clearPendingThreadCreationOutcome,
136136
pendingThreadCreationOutcomesAtom,
137137
} from "./pending-thread-creation";
138-
import { threadComposerErrorAtom } from "./thread-composer-error";
138+
import { setThreadComposerError, threadComposerErrorsAtom } from "./thread-composer-error";
139139
import type { QueuedThreadMessage } from "./thread-outbox-model";
140140
import * as composerDrafts from "./use-composer-drafts";
141141
import { recoverFailedThreadDraft } from "./recover-failed-thread-draft";
@@ -208,7 +208,7 @@ afterEach(() => {
208208
appAtomRegistry.set(composerDrafts.composerCloudDraftsAtom, { accountId: null, signedOut: {} });
209209
appAtomRegistry.set(editingQueuedMessageIdsAtom, {});
210210
appAtomRegistry.set(pendingThreadCreationOutcomesAtom, {});
211-
appAtomRegistry.set(threadComposerErrorAtom("environment-1:thread-1"), null);
211+
appAtomRegistry.set(threadComposerErrorsAtom, {});
212212
harness.draftFile.setWriteError(null);
213213
harness.removePersistedFile.mockClear();
214214
harness.removeOutboxMessage.mockClear();
@@ -441,6 +441,25 @@ describe("thread outbox drain delivery cleanup", () => {
441441
expect(appAtomRegistry.get(acknowledgedThreadMessagesAtom)).toEqual([message]);
442442
});
443443

444+
it("clears an error about the delivered message but keeps one about another message", async () => {
445+
const threadKey = "environment-1:thread-1";
446+
const retried = queuedMessage({ messageId: "message-retried", text: "retried" });
447+
await harness.manager.enqueue(retried);
448+
// A failed recovery left this message queued with an error about it.
449+
setThreadComposerError(threadKey, "could not be restored", retried.messageId);
450+
451+
await completeQueuedMessageDelivery(retried, harness.manager.revisionOf(retried.messageId));
452+
expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]).toBeUndefined();
453+
454+
const other = queuedMessage({ messageId: "message-other", text: "other" });
455+
await harness.manager.enqueue(other);
456+
// The thread's error explains a different, rejected message still in the draft.
457+
setThreadComposerError(threadKey, "rejected", "message-rejected");
458+
459+
await completeQueuedMessageDelivery(other, harness.manager.revisionOf(other.messageId));
460+
expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe("rejected");
461+
});
462+
444463
it("keeps a delivered message when its editor opens during storage removal", async () => {
445464
const message = queuedMessage({
446465
messageId: "message-editor-removal-race",
@@ -697,9 +716,7 @@ describe("thread outbox recovery rollback", () => {
697716
});
698717
expect(remainingMessages()).toEqual([]);
699718
// The creation's failure card shows the reason; the composer is hidden.
700-
expect(
701-
appAtomRegistry.get(threadComposerErrorAtom(`${message.environmentId}:${message.threadId}`)),
702-
).toBeNull();
719+
expect(appAtomRegistry.get(threadComposerErrorsAtom)).toEqual({});
703720
// The thread screen opened for this creation reads the failure from here.
704721
expect(
705722
appAtomRegistry.get(pendingThreadCreationOutcomesAtom)[
@@ -738,8 +755,8 @@ describe("thread outbox recovery rollback", () => {
738755
expect(appAtomRegistry.get(pendingThreadCreationOutcomesAtom)).toEqual({});
739756
// The thread screen shows why the message came back into the composer.
740757
expect(
741-
appAtomRegistry.get(threadComposerErrorAtom(`${message.environmentId}:${message.threadId}`)),
742-
).toBe("rejected");
758+
appAtomRegistry.get(threadComposerErrorsAtom)[`${message.environmentId}:${message.threadId}`],
759+
).toEqual({ message: "rejected", messageId: message.messageId });
743760
});
744761

745762
it("rolls a failed recovery merge back so the retry cannot duplicate the text", async () => {
@@ -765,6 +782,6 @@ describe("thread outbox recovery rollback", () => {
765782
"typed offline\n\nqueued text",
766783
);
767784
expect(remainingMessages()).toEqual([]);
768-
expect(appAtomRegistry.get(threadComposerErrorAtom(draftKey))).toBe("too large");
785+
expect(appAtomRegistry.get(threadComposerErrorsAtom)[draftKey]?.message).toBe("too large");
769786
});
770787
});

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ import {
8080
useThreadOutboxMessages,
8181
useThreadOutboxShellStatuses,
8282
} from "./use-thread-outbox";
83-
import { setThreadComposerError } from "./thread-composer-error";
83+
import { clearThreadComposerError, setThreadComposerError } from "./thread-composer-error";
8484
import { useRemoteConnectionStatus } from "./use-remote-environment-registry";
8585

8686
// Ordinary offline behavior (a socket dropping mid-request, a retryable
@@ -280,6 +280,12 @@ export async function completeQueuedMessageDelivery(
280280
queuedMessage: QueuedThreadMessage,
281281
deliveryRevision: number,
282282
): Promise<"removed" | "edited" | "failed"> {
283+
// The server took it after all: an error left by an earlier failed recovery
284+
// of this same message no longer applies.
285+
clearThreadComposerError(
286+
scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId),
287+
queuedMessage.messageId,
288+
);
283289
try {
284290
await removeDeliveredCloudQueuedMessage(queuedMessage).catch((error) => {
285291
console.warn("[thread-outbox] could not update sign-out snapshot after delivery", {
@@ -469,6 +475,7 @@ export async function restoreRejectedQueuedMessage(
469475
setThreadComposerError(
470476
threadKey,
471477
`Remove attachments from the draft before restoring this message. Messages can contain at most ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} attachments.`,
478+
queuedMessage.messageId,
472479
);
473480
return "blocked";
474481
}
@@ -547,7 +554,7 @@ export async function restoreRejectedQueuedMessage(
547554
reason: message,
548555
});
549556
} else {
550-
setThreadComposerError(threadKey, message);
557+
setThreadComposerError(threadKey, message, queuedMessage.messageId);
551558
}
552559
return "restored";
553560
} catch (error) {
@@ -565,6 +572,7 @@ export async function restoreRejectedQueuedMessage(
565572
setThreadComposerError(
566573
threadKey,
567574
error instanceof Error ? error.message : "The unsent message could not be restored.",
575+
queuedMessage.messageId,
568576
);
569577
return "retry";
570578
}

0 commit comments

Comments
 (0)