The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 14:45 UTC
fix(mobile): removing an environment no longer fails when a queued message can't be read (#28322)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Victor committed
Nov 14, 2026 at 06:02 UTC
0e5e0bb6527c5ac45c415ce4ae35dfecf9dd7ce0
3 files changed
+126
-26
apps/mobile/src/connection/environment-owned-data-cleanup.test.ts
new
+74
@@ -0,0 +1,74 @@
1
+import { Persistence } from "@t3tools/client-runtime/platform";
2
+import { EnvironmentId } from "@t3tools/contracts";
3
+import { describe, expect, it } from "@effect/vitest";
4
+import * as Effect from "effect/Effect";
5
+import * as Layer from "effect/Layer";
6
+import * as Logger from "effect/Logger";
7
+import { vi } from "vite-plus/test";
8
+
9
+const harness = vi.hoisted(() => ({
10
+ clearOutbox: vi.fn<(environmentId: string) => Promise<void>>(),
11
+ clearDrafts: vi.fn<(environmentId: string) => Promise<void>>(),
12
+ clearComposerErrors: vi.fn(),
13
+}));
14
+
15
+vi.mock("../state/thread-outbox-removal", () => ({
16
+ clearThreadOutboxEnvironment: harness.clearOutbox,
17
+}));
18
+vi.mock("../state/use-composer-drafts", () => ({
19
+ clearComposerDraftsEnvironment: harness.clearDrafts,
20
+}));
21
+vi.mock("../state/thread-composer-error", () => ({
22
+ clearThreadComposerErrorsForEnvironment: harness.clearComposerErrors,
23
+}));
24
+
25
+import * as EnvironmentOwnedDataCleanup from "./environment-owned-data-cleanup";
26
+
27
+const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
28
+
29
+describe("EnvironmentOwnedDataCleanup", () => {
30
+ it.effect("waits for the other clears and logs a bounded warning when one rejects", () => {
31
+ const warnings: Array<unknown> = [];
32
+ let releaseDrafts = () => {};
33
+ let draftsCleared = false;
34
+ // The drafts clear finishes only after the outbox failure is logged.
35
+ const logger = Logger.make(({ message }) => {
36
+ warnings.push(message);
37
+ releaseDrafts();
38
+ });
39
+ return Effect.gen(function* () {
40
+ harness.clearOutbox.mockRejectedValue(new Error("Some queued messages could not be read."));
41
+ harness.clearDrafts.mockImplementation(() =>
42
+ new Promise<void>((resolve) => {
43
+ releaseDrafts = resolve;
44
+ }).then(() => {
45
+ draftsCleared = true;
46
+ }),
47
+ );
48
+
49
+ const cleanup = yield* Persistence.EnvironmentOwnedDataCleanup;
50
+ yield* cleanup.clear(ENVIRONMENT_ID);
51
+
52
+ expect(draftsCleared).toBe(true);
53
+ expect(warnings).toEqual([
54
+ [
55
+ "Could not clear mobile environment-owned data.",
56
+ {
57
+ environmentId: ENVIRONMENT_ID,
58
+ operation: "outbox",
59
+ errorType: "error",
60
+ errorName: "Error",
61
+ stack: expect.any(String),
62
+ },
63
+ ],
64
+ ]);
65
+ }).pipe(
66
+ Effect.provide(
67
+ Layer.merge(
68
+ EnvironmentOwnedDataCleanup.layer,
69
+ Logger.layer([logger], { mergeWithExisting: false }),
70
+ ),
71
+ ),
72
+ );
73
+ });
74
+});
apps/mobile/src/connection/environment-owned-data-cleanup.ts
new
+48
@@ -0,0 +1,48 @@
1
+import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
2
+import { Persistence } from "@t3tools/client-runtime/platform";
3
+import type { EnvironmentId } from "@t3tools/contracts";
4
+import * as Effect from "effect/Effect";
5
+import * as Layer from "effect/Layer";
6
+import * as Schema from "effect/Schema";
7
+
8
+import { clearThreadComposerErrorsForEnvironment } from "../state/thread-composer-error";
9
+import { clearThreadOutboxEnvironment } from "../state/thread-outbox-removal";
10
+import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts";
11
+
12
+class EnvironmentOwnedDataClearError extends Schema.TaggedError<EnvironmentOwnedDataClearError>()(
13
+ "EnvironmentOwnedDataClearError",
14
+ { cause: Schema.Defect() },
15
+) {}
16
+
17
+export const layer = Layer.succeed(
18
+ Persistence.EnvironmentOwnedDataCleanup,
19
+ Persistence.EnvironmentOwnedDataCleanup.of({
20
+ clear: Effect.fn("Persistence.EnvironmentOwnedDataCleanup.clear")(function (environmentId) {
21
+ // Each clear settles on its own: a failure must not interrupt a clear that is still running.
22
+ const settle = (
23
+ operation: "outbox" | "drafts",
24
+ clear: (environmentId: EnvironmentId) => Promise<void>,
25
+ ) =>
26
+ Effect.tryPromise({
27
+ try: () => clear(environmentId),
28
+ catch: (cause) => new EnvironmentOwnedDataClearError({ cause }),
29
+ }).pipe(
30
+ Effect.catch((error) =>
31
+ Effect.logWarning("Could not clear mobile environment-owned data.", {
32
+ environmentId,
33
+ operation,
34
+ ...safeErrorLogAttributes(error.cause),
35
+ }),
36
+ ),
37
+ );
38
+ return Effect.all(
39
+ [
40
+ settle("outbox", clearThreadOutboxEnvironment),
41
+ settle("drafts", clearComposerDraftsEnvironment),
42
+ Effect.sync(() => clearThreadComposerErrorsForEnvironment(environmentId)),
43
+ ],
44
+ { concurrency: "unbounded", discard: true },
45
+ );
46
+ }),
47
+ }),
48
+);
apps/mobile/src/connection/platform.ts
+4
-26
@@ -1,8 +1,4 @@
1
-import {
2
- ClientCapabilities,
3
- PlatformConnectionSource,
4
- Persistence,
5
-} from "@t3tools/client-runtime/platform";
1
+import { ClientCapabilities, PlatformConnectionSource } from "@t3tools/client-runtime/platform";
2
import {
3
ConnectionBlockedError,
4
ConnectionTransientError,
@@ -25,10 +21,8 @@ import { authClientMetadata } from "../lib/authClientMetadata";
21
import * as Runtime from "../lib/runtime";
22
import * as MobileStorage from "../persistence/mobile-storage";
23
import { appAtomRegistry } from "../state/atom-registry";
28
-import { clearThreadOutboxEnvironment } from "../state/thread-outbox-removal";
29
-import { clearComposerDraftsEnvironment } from "../state/use-composer-drafts";
30
-import { clearThreadComposerErrorsForEnvironment } from "../state/thread-composer-error";
24
import { mobileApplicationActiveWakeup } from "./app-state-wakeups";
25
+import * as EnvironmentOwnedDataCleanup from "./environment-owned-data-cleanup";
26
import * as ConnectionStorage from "./storage";
27
28
function networkStatus(state: Network.NetworkState): "unknown" | "offline" | "online" {
@@ -255,22 +249,6 @@ const layerPlatformConnectionSource = Layer.succeed(
249
const layerProvidedConnectionStorage = ConnectionStorage.layer.pipe(Layer.provide(Runtime.layer));
250
const layerProvidedCapabilities = layerCapabilities.pipe(Layer.provide(Runtime.layer));
251
258
-const layerEnvironmentOwnedDataCleanup = Layer.succeed(
259
- Persistence.EnvironmentOwnedDataCleanup,
260
- Persistence.EnvironmentOwnedDataCleanup.of({
261
- clear: Effect.fn("Persistence.EnvironmentOwnedDataCleanup.clear")((environmentId) =>
262
- Effect.all(
263
- [
264
- Effect.promise(() => clearThreadOutboxEnvironment(environmentId)),
265
- Effect.promise(() => clearComposerDraftsEnvironment(environmentId)),
266
- Effect.sync(() => clearThreadComposerErrorsForEnvironment(environmentId)),
267
- ],
268
- { concurrency: "unbounded", discard: true },
269
- ),
270
- ),
271
- }),
272
-);
273
-
252
type ConnectionPlatformLayerSource =
253
| typeof layerProvidedConnectionStorage
254
| typeof Runtime.layer
@@ -278,7 +256,7 @@ type ConnectionPlatformLayerSource =
256
| typeof layerWakeups
257
| typeof layerProvidedCapabilities
258
| typeof layerPlatformConnectionSource
281
- | typeof layerEnvironmentOwnedDataCleanup;
259
+ | typeof EnvironmentOwnedDataCleanup.layer;
260
261
export const layer: Layer.Layer<
262
Layer.Success<ConnectionPlatformLayerSource>,
@@ -291,5 +269,5 @@ export const layer: Layer.Layer<
269
layerWakeups,
270
layerProvidedCapabilities,
271
layerPlatformConnectionSource,
294
- layerEnvironmentOwnedDataCleanup,
272
+ EnvironmentOwnedDataCleanup.layer,
273
);