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 23, 04:15 UTC
fix(mobile): serialize initial device identity creation (#18837)
Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com>
Alex Southwell committed
Oct 19, 2026 at 06:18 UTC
216c4801714c2b98c1ab8bc768eec3399c950357
2 files changed
+88
-1
apps/mobile/src/persistence/mobile-storage.test.ts
new
+85
@@ -0,0 +1,85 @@
1
+import { describe, expect, it } from "@effect/vitest";
2
+import * as Effect from "effect/Effect";
3
+import { beforeEach, vi } from "vite-plus/test";
4
+
5
+const { values, getItemAsync, setItemAsync, uuidv4 } = vi.hoisted(() => {
6
+ const values = new Map<string, string>();
7
+ return {
8
+ values,
9
+ getItemAsync: vi.fn(async (key: string) => {
10
+ const value = values.get(key) ?? null;
11
+ await Promise.resolve();
12
+ return value;
13
+ }),
14
+ setItemAsync: vi.fn(async (key: string, value: string) => {
15
+ values.set(key, value);
16
+ }),
17
+ uuidv4: vi.fn(),
18
+ };
19
+});
20
+
21
+vi.mock("expo-secure-store", () => ({ getItemAsync, setItemAsync }));
22
+vi.mock("expo-crypto", () => ({ randomUUID: uuidv4 }));
23
+
24
+import "../lib/uuid";
25
+
26
+import * as MobileSecureStorage from "./mobile-secure-storage";
27
+import * as MobileStorage from "./mobile-storage";
28
+
29
+const deviceIdKey = "t3code.agent-awareness.device-id";
30
+const makeStorage = MobileStorage.make().pipe(Effect.provide(MobileSecureStorage.layer));
31
+
32
+beforeEach(() => {
33
+ values.clear();
34
+ getItemAsync.mockClear();
35
+ setItemAsync.mockClear();
36
+ uuidv4.mockReset();
37
+ let generated = 0;
38
+ uuidv4.mockImplementation(() => `generated-device-${++generated}`);
39
+});
40
+
41
+describe("mobile device identity", () => {
42
+ it.effect("concurrent first-use callers share one persisted device identity", () =>
43
+ Effect.gen(function* () {
44
+ const storage = yield* makeStorage;
45
+ const deviceIds = yield* Effect.all(
46
+ [storage.loadOrCreateAgentAwarenessDeviceId, storage.loadOrCreateAgentAwarenessDeviceId],
47
+ { concurrency: "unbounded" },
48
+ );
49
+
50
+ expect(deviceIds).toEqual(["generated-device-1", "generated-device-1"]);
51
+ expect(yield* storage.loadAgentAwarenessDeviceId).toBe(deviceIds[0]);
52
+ expect(values.get(deviceIdKey)).toBe(deviceIds[0]);
53
+ expect(uuidv4).toHaveBeenCalledTimes(1);
54
+ expect(setItemAsync).toHaveBeenCalledTimes(1);
55
+ }),
56
+ );
57
+
58
+ it.effect("reuses an existing device identity without generating or writing", () =>
59
+ Effect.gen(function* () {
60
+ values.set(deviceIdKey, "existing-device");
61
+ const storage = yield* makeStorage;
62
+
63
+ expect(yield* storage.loadOrCreateAgentAwarenessDeviceId).toBe("existing-device");
64
+ expect(uuidv4).not.toHaveBeenCalled();
65
+ expect(setItemAsync).not.toHaveBeenCalled();
66
+ }),
67
+ );
68
+
69
+ it.effect("retries a failed first write without retaining an unpersisted identity", () =>
70
+ Effect.gen(function* () {
71
+ setItemAsync.mockRejectedValueOnce(new Error("Secure storage unavailable"));
72
+ const storage = yield* makeStorage;
73
+ const first = yield* Effect.result(storage.loadOrCreateAgentAwarenessDeviceId);
74
+
75
+ expect(first).toMatchObject({
76
+ _tag: "Failure",
77
+ failure: { _tag: "MobileSecureStorageError", operation: "write", key: deviceIdKey },
78
+ });
79
+ expect(yield* storage.loadAgentAwarenessDeviceId).toBeNull();
80
+ expect(yield* storage.loadOrCreateAgentAwarenessDeviceId).toBe("generated-device-2");
81
+ expect(yield* storage.loadAgentAwarenessDeviceId).toBe("generated-device-2");
82
+ expect(uuidv4).toHaveBeenCalledTimes(2);
83
+ }),
84
+ );
85
+});
apps/mobile/src/persistence/mobile-storage.ts
+3
-1
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
5
import { pipe } from "effect/Function";
6
import * as Layer from "effect/Layer";
7
import * as Schema from "effect/Schema";
8
+import * as Semaphore from "effect/Semaphore";
9
10
import {
11
isRelayManagedConnection,
@@ -119,6 +120,7 @@ export class MobileStorage extends Context.Service<
120
121
export const make = Effect.fn("MobileStorage.make")(function* () {
122
const secureStorage = yield* MobileSecureStorage.MobileSecureStorage;
123
+ const deviceIdLock = yield* Semaphore.make(1);
124
125
const parseJson = <A>(key: string, raw: string): A | null => {
126
if (!raw.trim()) return null;
@@ -197,7 +199,7 @@ export const make = Effect.fn("MobileStorage.make")(function* () {
199
});
200
yield* secureStorage.setItem(AGENT_AWARENESS_DEVICE_ID_KEY, deviceId);
201
return deviceId;
200
- });
202
+ }).pipe(deviceIdLock.withPermits(1));
203
204
const loadAgentAwarenessDeviceId = secureStorage
205
.getItem(AGENT_AWARENESS_DEVICE_ID_KEY)