Skip to content
TypeScript

fix(mobile): serialize initial device identity creation (#18837)

Alex Southwell216c4801714c
diff --git a/apps/mobile/src/persistence/mobile-storage.test.ts b/apps/mobile/src/persistence/mobile-storage.test.ts
new file mode 100644
index 00000000000..9bccff93fa7
--- /dev/null
+++ b/apps/mobile/src/persistence/mobile-storage.test.ts
@@ -0,0 +1,85 @@
+import { describe, expect, it } from "@effect/vitest";
+import * as Effect from "effect/Effect";
+import { beforeEach, vi } from "vite-plus/test";
+
+const { values, getItemAsync, setItemAsync, uuidv4 } = vi.hoisted(() => {
+ const values = new Map<string, string>();
+ return {
+ values,
+ getItemAsync: vi.fn(async (key: string) => {
+ const value = values.get(key) ?? null;
+ await Promise.resolve();
+ return value;
+ }),
+ setItemAsync: vi.fn(async (key: string, value: string) => {
+ values.set(key, value);
+ }),
+ uuidv4: vi.fn(),
+ };
+});
+
+vi.mock("expo-secure-store", () => ({ getItemAsync, setItemAsync }));
+vi.mock("expo-crypto", () => ({ randomUUID: uuidv4 }));
+
+import "../lib/uuid";
+
+import * as MobileSecureStorage from "./mobile-secure-storage";
+import * as MobileStorage from "./mobile-storage";
+
+const deviceIdKey = "t3code.agent-awareness.device-id";
+const makeStorage = MobileStorage.make().pipe(Effect.provide(MobileSecureStorage.layer));
+
+beforeEach(() => {
+ values.clear();
+ getItemAsync.mockClear();
+ setItemAsync.mockClear();
+ uuidv4.mockReset();
+ let generated = 0;
+ uuidv4.mockImplementation(() => `generated-device-${++generated}`);
+});
+
+describe("mobile device identity", () => {
+ it.effect("concurrent first-use callers share one persisted device identity", () =>
+ Effect.gen(function* () {
+ const storage = yield* makeStorage;
+ const deviceIds = yield* Effect.all(
+ [storage.loadOrCreateAgentAwarenessDeviceId, storage.loadOrCreateAgentAwarenessDeviceId],
+ { concurrency: "unbounded" },
+ );
+
+ expect(deviceIds).toEqual(["generated-device-1", "generated-device-1"]);
+ expect(yield* storage.loadAgentAwarenessDeviceId).toBe(deviceIds[0]);
+ expect(values.get(deviceIdKey)).toBe(deviceIds[0]);
+ expect(uuidv4).toHaveBeenCalledTimes(1);
+ expect(setItemAsync).toHaveBeenCalledTimes(1);
+ }),
+ );
+
+ it.effect("reuses an existing device identity without generating or writing", () =>
+ Effect.gen(function* () {
+ values.set(deviceIdKey, "existing-device");
+ const storage = yield* makeStorage;
+
+ expect(yield* storage.loadOrCreateAgentAwarenessDeviceId).toBe("existing-device");
+ expect(uuidv4).not.toHaveBeenCalled();
+ expect(setItemAsync).not.toHaveBeenCalled();
+ }),
+ );
+
+ it.effect("retries a failed first write without retaining an unpersisted identity", () =>
+ Effect.gen(function* () {
+ setItemAsync.mockRejectedValueOnce(new Error("Secure storage unavailable"));
+ const storage = yield* makeStorage;
+ const first = yield* Effect.result(storage.loadOrCreateAgentAwarenessDeviceId);
+
+ expect(first).toMatchObject({
+ _tag: "Failure",
+ failure: { _tag: "MobileSecureStorageError", operation: "write", key: deviceIdKey },
+ });
+ expect(yield* storage.loadAgentAwarenessDeviceId).toBeNull();
+ expect(yield* storage.loadOrCreateAgentAwarenessDeviceId).toBe("generated-device-2");
+ expect(yield* storage.loadAgentAwarenessDeviceId).toBe("generated-device-2");
+ expect(uuidv4).toHaveBeenCalledTimes(2);
+ }),
+ );
+});
diff --git a/apps/mobile/src/persistence/mobile-storage.ts b/apps/mobile/src/persistence/mobile-storage.ts
index cf6550546e5..6ffb34d7a7c 100644
--- a/apps/mobile/src/persistence/mobile-storage.ts
+++ b/apps/mobile/src/persistence/mobile-storage.ts
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
import { pipe } from "effect/Function";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
+import * as Semaphore from "effect/Semaphore";
import {
isRelayManagedConnection,
@@ -119,6 +120,7 @@ export class MobileStorage extends Context.Service<
export const make = Effect.fn("MobileStorage.make")(function* () {
const secureStorage = yield* MobileSecureStorage.MobileSecureStorage;
+ const deviceIdLock = yield* Semaphore.make(1);
const parseJson = <A>(key: string, raw: string): A | null => {
if (!raw.trim()) return null;
@@ -197,7 +199,7 @@ export const make = Effect.fn("MobileStorage.make")(function* () {
});
yield* secureStorage.setItem(AGENT_AWARENESS_DEVICE_ID_KEY, deviceId);
return deviceId;
- });
+ }).pipe(deviceIdLock.withPermits(1));
const loadAgentAwarenessDeviceId = secureStorage
.getItem(AGENT_AWARENESS_DEVICE_ID_KEY)