Skip to content

Commit 62eed73

Browse files
fix(mobile): warn on nontransport settings-sync outbox failures
resolveThreadOutboxFailureAction always resolves settings-sync failures to a retry, even when the server rejected the command, so routing every retry to the debug log could hide a permanently rejected update forever. The delivery-failure logger now takes the raw failure (stage, error, interrupted), decides the log level from the error itself, and returns the retry-or-restore action for the caller. Tests drive the real classification with transport-tagged and server-decided tagged errors instead of a predetermined action argument, and cover the upload-retry logging path. Co-authored-by: Apex by Callstack <[email protected]>
1 parent e286d19 commit 62eed73

3 files changed

Lines changed: 134 additions & 35 deletions

File tree

‎apps/mobile/src/lib/debugLog.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020

2121
export interface DebugLogger {
22-
readonly namespace: string;
2322
readonly isEnabled: () => boolean;
2423
readonly log: (event: string, data?: Record<string, unknown>) => void;
2524
}
@@ -61,5 +60,5 @@ export function createDebugLogger(
6160
console.log(`[t3-${namespace}] ${event}`, data);
6261
}
6362
};
64-
return { namespace, isEnabled, log };
63+
return { isEnabled, log };
6564
}

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

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ import { editingQueuedMessageIdsAtom } from "./use-thread-outbox";
145145
import {
146146
completeQueuedMessageDelivery,
147147
logThreadOutboxDeliveryFailure,
148+
logThreadOutboxUploadFailure,
148149
prepareQueuedMessageAttachments,
149150
recoverEditedCreationAfterDelivery,
150151
removeAcknowledgedExistingThreadMessage,
@@ -775,44 +776,127 @@ describe("thread outbox recovery rollback", () => {
775776
});
776777

777778
describe("thread outbox failure logging", () => {
778-
it("keeps ordinary transport retries out of console.warn and silent by default", () => {
779+
// Tagged errors match shouldRetryThreadOutboxDelivery's transport tags, so
780+
// the assertions cover the real failure classification, not a
781+
// predetermined action argument.
782+
const transportError = { _tag: "RpcClientError" };
783+
const serverDecidedError = { _tag: "OrchestrationDispatchCommandError" };
784+
785+
function failureLogging(input: Parameters<typeof logThreadOutboxDeliveryFailure>[0]): {
786+
readonly action: string;
787+
readonly warnCalls: ReadonlyArray<unknown[]>;
788+
readonly logCalls: ReadonlyArray<unknown[]>;
789+
} {
779790
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
780791
const log = vi.spyOn(console, "log").mockImplementation(() => {});
781792
try {
782-
logThreadOutboxDeliveryFailure("retry", { messageId: "m1" });
783-
expect(warn).not.toHaveBeenCalled();
784-
expect(log).not.toHaveBeenCalled();
793+
return {
794+
action: logThreadOutboxDeliveryFailure(input),
795+
warnCalls: [...warn.mock.calls],
796+
logCalls: [...log.mock.calls],
797+
};
785798
} finally {
786799
warn.mockRestore();
787800
log.mockRestore();
788801
}
802+
}
803+
804+
it("keeps offline transport retries silent by default at both stages", () => {
805+
for (const stage of ["settings-sync", "start-turn"] as const) {
806+
const { action, warnCalls, logCalls } = failureLogging({
807+
stage,
808+
error: transportError,
809+
interrupted: false,
810+
context: { messageId: "m1" },
811+
});
812+
expect(action).toBe("retry");
813+
expect(warnCalls).toEqual([]);
814+
expect(logCalls).toEqual([]);
815+
}
789816
});
790817

791-
it("surfaces retry details when the thread-outbox debug filter is enabled", () => {
818+
it("surfaces transport retry details when the thread-outbox debug filter is enabled", () => {
792819
vi.stubGlobal("__T3_DEBUG__", ["thread-outbox"]);
793820
const log = vi.spyOn(console, "log").mockImplementation(() => {});
794821
try {
795-
logThreadOutboxDeliveryFailure("retry", { messageId: "m1" });
822+
logThreadOutboxDeliveryFailure({
823+
stage: "start-turn",
824+
error: transportError,
825+
interrupted: false,
826+
context: { messageId: "m1" },
827+
});
796828
expect(log).toHaveBeenCalledWith(
797829
"[t3-thread-outbox] queued message delivery failed",
798-
expect.objectContaining({ messageId: "m1" }),
830+
expect.objectContaining({ messageId: "m1", action: "retry" }),
799831
);
800832
} finally {
801833
log.mockRestore();
802834
vi.unstubAllGlobals();
803835
}
804836
});
805837

806-
it("still warns when the server decided the message must be restored", () => {
838+
it("warns when the server decided the message must be restored", () => {
839+
const { action, warnCalls } = failureLogging({
840+
stage: "start-turn",
841+
error: serverDecidedError,
842+
interrupted: false,
843+
context: { messageId: "m2" },
844+
});
845+
expect(action).toBe("restore");
846+
expect(warnCalls).toEqual([
847+
[
848+
"[thread-outbox] queued message delivery failed",
849+
expect.objectContaining({ messageId: "m2", action: "restore" }),
850+
],
851+
]);
852+
});
853+
854+
it("warns on a nontransport settings-sync failure even though it resolves to a retry", () => {
855+
const { action, warnCalls } = failureLogging({
856+
stage: "settings-sync",
857+
error: serverDecidedError,
858+
interrupted: false,
859+
context: { messageId: "m3" },
860+
});
861+
expect(action).toBe("retry");
862+
expect(warnCalls).toEqual([
863+
[
864+
"[thread-outbox] queued message delivery failed",
865+
expect.objectContaining({ stage: "settings-sync", action: "retry" }),
866+
],
867+
]);
868+
});
869+
870+
it("routes retryable attachment-upload failures to the debug log and warns on the rest", () => {
807871
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
872+
const log = vi.spyOn(console, "log").mockImplementation(() => {});
808873
try {
809-
logThreadOutboxDeliveryFailure("restore", { messageId: "m2" });
874+
logThreadOutboxUploadFailure(transportError);
875+
expect(warn).not.toHaveBeenCalled();
876+
expect(log).not.toHaveBeenCalled();
877+
878+
logThreadOutboxUploadFailure(serverDecidedError);
810879
expect(warn).toHaveBeenCalledWith(
811-
"[thread-outbox] queued message delivery failed",
812-
expect.objectContaining({ messageId: "m2" }),
880+
"[thread-outbox] failed to upload attachments",
881+
serverDecidedError,
813882
);
814883
} finally {
815884
warn.mockRestore();
885+
log.mockRestore();
886+
}
887+
});
888+
889+
it("logs retryable attachment-upload failures when the debug filter is enabled", () => {
890+
vi.stubGlobal("__T3_DEBUG__", ["thread-outbox"]);
891+
const log = vi.spyOn(console, "log").mockImplementation(() => {});
892+
try {
893+
logThreadOutboxUploadFailure(transportError);
894+
expect(log).toHaveBeenCalledWith("[t3-thread-outbox] attachment upload failed; retrying", {
895+
error: transportError,
896+
});
897+
} finally {
898+
log.mockRestore();
899+
vi.unstubAllGlobals();
816900
}
817901
});
818902
});

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

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,46 @@ import {
8787
// Ordinary offline behavior (a socket dropping mid-request, a retryable
8888
// attachment upload failure) must not spam `console.warn` on every backoff
8989
// retry; it goes to the filterable `[t3-thread-outbox]` debug log instead.
90-
// Failures the server decided — the ones that restore the message with a
91-
// user-visible error — stay on `console.warn`.
90+
// Failures the server decided stay on `console.warn`.
9291
const threadOutboxDebug = createDebugLogger("thread-outbox");
9392

9493
/**
95-
* Routes one queued-message delivery failure by outcome: ordinary transport
96-
* retries to the debug log, server-decided restores to `console.warn`.
94+
* Logs one queued-message delivery failure and returns the retry-or-restore
95+
* decision for the caller. Ordinary transport retries — what an offline
96+
* device or a flapping socket produces on every backoff attempt — go to the
97+
* debug log. Server-decided failures warn. Settings-sync failures always
98+
* resolve to a retry even when the server rejected the command, so the
99+
* error, not the resolved action, must decide the log level there; routing
100+
* every retry to debug could hide a permanently rejected update forever.
97101
* Exported for tests.
98102
*/
99-
export function logThreadOutboxDeliveryFailure(
100-
action: ThreadOutboxFailureAction,
101-
details: Record<string, unknown>,
102-
): void {
103-
if (action === "restore") {
103+
export function logThreadOutboxDeliveryFailure(input: {
104+
readonly stage: ThreadOutboxCommandStage;
105+
readonly error: unknown;
106+
readonly interrupted: boolean;
107+
readonly context: Record<string, unknown>;
108+
}): ThreadOutboxFailureAction {
109+
const action = resolveThreadOutboxFailureAction({
110+
stage: input.stage,
111+
error: input.error,
112+
interrupted: input.interrupted,
113+
});
114+
const details = { ...input.context, stage: input.stage, action };
115+
const ordinaryTransportRetry =
116+
action === "retry" &&
117+
(input.interrupted ||
118+
input.stage !== "settings-sync" ||
119+
shouldRetryThreadOutboxDelivery(input.error));
120+
if (ordinaryTransportRetry) {
121+
threadOutboxDebug.log("queued message delivery failed", details);
122+
} else {
104123
console.warn("[thread-outbox] queued message delivery failed", details);
105-
return;
106124
}
107-
threadOutboxDebug.log("queued message delivery failed", details);
125+
return action;
108126
}
109127

110-
/** Attachment uploads retry like delivery: transport failures are ordinary offline noise. */
111-
function logThreadOutboxUploadFailure(error: unknown): void {
128+
/** Attachment uploads retry like delivery: transport failures are ordinary offline noise. Exported for tests. */
129+
export function logThreadOutboxUploadFailure(error: unknown): void {
112130
if (shouldRetryThreadOutboxDelivery(error)) {
113131
threadOutboxDebug.log("attachment upload failed; retrying", { error });
114132
} else {
@@ -700,18 +718,16 @@ export function useThreadOutboxDrain(): void {
700718
return null;
701719
}
702720
const error = Cause.squash(commandResult.cause);
703-
const action = resolveThreadOutboxFailureAction({
721+
const action = logThreadOutboxDeliveryFailure({
704722
stage,
705723
error,
706724
interrupted: Cause.hasInterruptsOnly(commandResult.cause),
707-
});
708-
logThreadOutboxDeliveryFailure(action, {
709-
environmentId: queuedMessage.environmentId,
710-
threadId: queuedMessage.threadId,
711-
messageId: queuedMessage.messageId,
712-
stage,
713-
cause: commandResult.cause,
714-
action,
725+
context: {
726+
environmentId: queuedMessage.environmentId,
727+
threadId: queuedMessage.threadId,
728+
messageId: queuedMessage.messageId,
729+
cause: commandResult.cause,
730+
},
715731
});
716732
return {
717733
action,

0 commit comments

Comments
 (0)