Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
[codex] Structure mobile secure storage failures (#3345)
Julius Marminge49c23221d80c
diff --git a/apps/mobile/src/lib/storage.test.ts b/apps/mobile/src/lib/storage.test.tsindex c3dd28ac3..084f9430d 100644--- a/apps/mobile/src/lib/storage.test.ts+++ b/apps/mobile/src/lib/storage.test.ts@@ -69,4 +69,35 @@ describe("mobile connection storage", () => {toStableSavedRemoteConnection(managedConnection),]);});++ it("preserves secure-storage read failures with operation and key context", async () => {+ const cause = new Error("keychain unavailable");+ mocks.getItemAsync.mockRejectedValueOnce(cause);++ await expect(loadSavedConnections()).rejects.toMatchObject({+ _tag: "MobileSecureStorageError",+ operation: "read",+ key: "t3code.connections",+ cause,+ message: "Mobile secure storage operation read failed for key t3code.connections.",+ });+ });++ it("logs structured decode failures before using the empty fallback", async () => {+ await mocks.setItemAsync("t3code.connections", "{");+ const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);++ await expect(loadSavedConnections()).resolves.toEqual([]);+ expect(warn).toHaveBeenCalledWith(+ "[mobile-storage] ignored invalid JSON",+ expect.objectContaining({+ _tag: "MobileStorageDecodeError",+ key: "t3code.connections",+ cause: expect.any(SyntaxError),+ message: "Failed to decode mobile storage value for key t3code.connections.",+ }),+ );++ warn.mockRestore();+ });});diff --git a/apps/mobile/src/lib/storage.ts b/apps/mobile/src/lib/storage.tsindex da54f9294..114648277 100644--- a/apps/mobile/src/lib/storage.ts+++ b/apps/mobile/src/lib/storage.ts@@ -1,5 +1,6 @@import * as Arr from "effect/Array";import { pipe } from "effect/Function";+import * as Schema from "effect/Schema";import * as SecureStore from "expo-secure-store";import { EnvironmentId } from "@t3tools/contracts";@@ -12,21 +13,72 @@ import {const CONNECTIONS_KEY = "t3code.connections";const PREFERENCES_KEY = "t3code.preferences";const AGENT_AWARENESS_DEVICE_ID_KEY = "t3code.agent-awareness.device-id";+const MobileStorageKey = Schema.Literals([+ CONNECTIONS_KEY,+ PREFERENCES_KEY,+ AGENT_AWARENESS_DEVICE_ID_KEY,+]);+type MobileStorageKeyValue = typeof MobileStorageKey.Type;++export class MobileSecureStorageError extends Schema.TaggedErrorClass<MobileSecureStorageError>()(+ "MobileSecureStorageError",+ {+ operation: Schema.Literals(["read", "write", "generate-device-id"]),+ key: MobileStorageKey,+ cause: Schema.Defect(),+ },+) {+ override get message(): string {+ return `Mobile secure storage operation ${this.operation} failed for key ${this.key}.`;+ }+}++export class MobileStorageDecodeError extends Schema.TaggedErrorClass<MobileStorageDecodeError>()(+ "MobileStorageDecodeError",+ {+ key: MobileStorageKey,+ cause: Schema.Defect(),+ },+) {+ override get message(): string {+ return `Failed to decode mobile storage value for key ${this.key}.`;+ }+}++export class MobileStorageEncodeError extends Schema.TaggedErrorClass<MobileStorageEncodeError>()(+ "MobileStorageEncodeError",+ {+ key: MobileStorageKey,+ cause: Schema.Defect(),+ },+) {+ override get message(): string {+ return `Failed to encode mobile storage value for key ${this.key}.`;+ }+}export interface Preferences {readonly liveActivitiesEnabled?: boolean;readonly terminalFontSize?: number;}-async function readStorageItem(key: string): Promise<string | null> {- return await SecureStore.getItemAsync(key);+async function readStorageItem(key: MobileStorageKeyValue): Promise<string | null> {+ try {+ return await SecureStore.getItemAsync(key);+ } catch (cause) {+ throw new MobileSecureStorageError({ operation: "read", key, cause });+ }}-async function writeStorageItem(key: string, value: string): Promise<void> {- await SecureStore.setItemAsync(key, value);+async function writeStorageItem(key: MobileStorageKeyValue, value: string): Promise<void> {+ try {+ await SecureStore.setItemAsync(key, value);+ } catch (cause) {+ throw new MobileSecureStorageError({ operation: "write", key, cause });+ }}-async function readJsonStorageItem<T>(key: string): Promise<T | null> {+async function readJsonStorageItem<T>(key: MobileStorageKeyValue): Promise<T | null> {const raw = (await readStorageItem(key)) ?? "";if (!raw.trim()) {return null;@@ -34,11 +86,25 @@ async function readJsonStorageItem<T>(key: string): Promise<T | null> {try {return JSON.parse(raw) as T;- } catch {+ } catch (cause) {+ console.warn(+ "[mobile-storage] ignored invalid JSON",+ new MobileStorageDecodeError({ key, cause }),+ );return null;}}+async function writeJsonStorageItem(key: MobileStorageKeyValue, value: unknown) {+ let encoded: string;+ try {+ encoded = JSON.stringify(value);+ } catch (cause) {+ throw new MobileStorageEncodeError({ key, cause });+ }+ await writeStorageItem(key, encoded);+}+export async function loadSavedConnections(): Promise<ReadonlyArray<SavedRemoteConnection>> {const parsed = await readJsonStorageItem<{readonly connections?: ReadonlyArray<SavedRemoteConnection>;@@ -67,7 +133,7 @@ export async function saveConnection(connection: SavedRemoteConnection): Promise): pipe(current, Arr.append(stableConnection));- await writeStorageItem(CONNECTIONS_KEY, JSON.stringify({ connections: next }));+ await writeJsonStorageItem(CONNECTIONS_KEY, { connections: next });}export async function clearSavedConnection(environmentId: EnvironmentId): Promise<void> {@@ -76,7 +142,7 @@ export async function clearSavedConnection(environmentId: EnvironmentId): Promiscurrent,Arr.filter((entry) => entry.environmentId !== environmentId),);- await writeStorageItem(CONNECTIONS_KEY, JSON.stringify({ connections: next }));+ await writeJsonStorageItem(CONNECTIONS_KEY, { connections: next });}export async function loadPreferences(): Promise<Preferences> {@@ -106,7 +172,7 @@ export async function savePreferencesPatch(patch: Partial<Preferences>): Promise...current,...patch,};- await writeStorageItem(PREFERENCES_KEY, JSON.stringify(next));+ await writeJsonStorageItem(PREFERENCES_KEY, next);return next;}@@ -116,8 +182,15 @@ export async function loadOrCreateAgentAwarenessDeviceId(): Promise<string> {return existing;}- const { uuidv4 } = await import("./uuid");- const deviceId = uuidv4();+ const deviceId = await import("./uuid")+ .then(({ uuidv4 }) => uuidv4())+ .catch((cause) => {+ throw new MobileSecureStorageError({+ operation: "generate-device-id",+ key: AGENT_AWARENESS_DEVICE_ID_KEY,+ cause,+ });+ });await writeStorageItem(AGENT_AWARENESS_DEVICE_ID_KEY, deviceId);return deviceId;}