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 24, 22:45 UTC
[codex] Structure mobile secure storage failures (#3345)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 11:23 UTC
49c23221d80c68114fe4d88e92b05fa1129de0d3
2 files changed
+115
-11
apps/mobile/src/lib/storage.test.ts
+31
@@ -69,4 +69,35 @@ describe("mobile connection storage", () => {
69
toStableSavedRemoteConnection(managedConnection),
70
]);
71
});
72
+
73
+ it("preserves secure-storage read failures with operation and key context", async () => {
74
+ const cause = new Error("keychain unavailable");
75
+ mocks.getItemAsync.mockRejectedValueOnce(cause);
76
+
77
+ await expect(loadSavedConnections()).rejects.toMatchObject({
78
+ _tag: "MobileSecureStorageError",
79
+ operation: "read",
80
+ key: "t3code.connections",
81
+ cause,
82
+ message: "Mobile secure storage operation read failed for key t3code.connections.",
83
+ });
84
+ });
85
+
86
+ it("logs structured decode failures before using the empty fallback", async () => {
87
+ await mocks.setItemAsync("t3code.connections", "{");
88
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
89
+
90
+ await expect(loadSavedConnections()).resolves.toEqual([]);
91
+ expect(warn).toHaveBeenCalledWith(
92
+ "[mobile-storage] ignored invalid JSON",
93
+ expect.objectContaining({
94
+ _tag: "MobileStorageDecodeError",
95
+ key: "t3code.connections",
96
+ cause: expect.any(SyntaxError),
97
+ message: "Failed to decode mobile storage value for key t3code.connections.",
98
+ }),
99
+ );
100
+
101
+ warn.mockRestore();
102
+ });
103
});
apps/mobile/src/lib/storage.ts
+84
-11
@@ -1,5 +1,6 @@
1
import * as Arr from "effect/Array";
2
import { pipe } from "effect/Function";
3
+import * as Schema from "effect/Schema";
4
import * as SecureStore from "expo-secure-store";
5
import { EnvironmentId } from "@t3tools/contracts";
6
@@ -12,21 +13,72 @@ import {
13
const CONNECTIONS_KEY = "t3code.connections";
14
const PREFERENCES_KEY = "t3code.preferences";
15
const AGENT_AWARENESS_DEVICE_ID_KEY = "t3code.agent-awareness.device-id";
16
+const MobileStorageKey = Schema.Literals([
17
+ CONNECTIONS_KEY,
18
+ PREFERENCES_KEY,
19
+ AGENT_AWARENESS_DEVICE_ID_KEY,
20
+]);
21
+type MobileStorageKeyValue = typeof MobileStorageKey.Type;
22
+
23
+export class MobileSecureStorageError extends Schema.TaggedErrorClass<MobileSecureStorageError>()(
24
+ "MobileSecureStorageError",
25
+ {
26
+ operation: Schema.Literals(["read", "write", "generate-device-id"]),
27
+ key: MobileStorageKey,
28
+ cause: Schema.Defect(),
29
+ },
30
+) {
31
+ override get message(): string {
32
+ return `Mobile secure storage operation ${this.operation} failed for key ${this.key}.`;
33
+ }
34
+}
35
+
36
+export class MobileStorageDecodeError extends Schema.TaggedErrorClass<MobileStorageDecodeError>()(
37
+ "MobileStorageDecodeError",
38
+ {
39
+ key: MobileStorageKey,
40
+ cause: Schema.Defect(),
41
+ },
42
+) {
43
+ override get message(): string {
44
+ return `Failed to decode mobile storage value for key ${this.key}.`;
45
+ }
46
+}
47
+
48
+export class MobileStorageEncodeError extends Schema.TaggedErrorClass<MobileStorageEncodeError>()(
49
+ "MobileStorageEncodeError",
50
+ {
51
+ key: MobileStorageKey,
52
+ cause: Schema.Defect(),
53
+ },
54
+) {
55
+ override get message(): string {
56
+ return `Failed to encode mobile storage value for key ${this.key}.`;
57
+ }
58
+}
59
60
export interface Preferences {
61
readonly liveActivitiesEnabled?: boolean;
62
readonly terminalFontSize?: number;
63
}
64
21
-async function readStorageItem(key: string): Promise<string | null> {
22
- return await SecureStore.getItemAsync(key);
65
+async function readStorageItem(key: MobileStorageKeyValue): Promise<string | null> {
66
+ try {
67
+ return await SecureStore.getItemAsync(key);
68
+ } catch (cause) {
69
+ throw new MobileSecureStorageError({ operation: "read", key, cause });
70
+ }
71
}
72
25
-async function writeStorageItem(key: string, value: string): Promise<void> {
26
- await SecureStore.setItemAsync(key, value);
73
+async function writeStorageItem(key: MobileStorageKeyValue, value: string): Promise<void> {
74
+ try {
75
+ await SecureStore.setItemAsync(key, value);
76
+ } catch (cause) {
77
+ throw new MobileSecureStorageError({ operation: "write", key, cause });
78
+ }
79
}
80
29
-async function readJsonStorageItem<T>(key: string): Promise<T | null> {
81
+async function readJsonStorageItem<T>(key: MobileStorageKeyValue): Promise<T | null> {
82
const raw = (await readStorageItem(key)) ?? "";
83
if (!raw.trim()) {
84
return null;
@@ -34,11 +86,25 @@ async function readJsonStorageItem<T>(key: string): Promise<T | null> {
86
87
try {
88
return JSON.parse(raw) as T;
37
- } catch {
89
+ } catch (cause) {
90
+ console.warn(
91
+ "[mobile-storage] ignored invalid JSON",
92
+ new MobileStorageDecodeError({ key, cause }),
93
+ );
94
return null;
95
}
96
}
97
98
+async function writeJsonStorageItem(key: MobileStorageKeyValue, value: unknown) {
99
+ let encoded: string;
100
+ try {
101
+ encoded = JSON.stringify(value);
102
+ } catch (cause) {
103
+ throw new MobileStorageEncodeError({ key, cause });
104
+ }
105
+ await writeStorageItem(key, encoded);
106
+}
107
+
108
export async function loadSavedConnections(): Promise<ReadonlyArray<SavedRemoteConnection>> {
109
const parsed = await readJsonStorageItem<{
110
readonly connections?: ReadonlyArray<SavedRemoteConnection>;
@@ -67,7 +133,7 @@ export async function saveConnection(connection: SavedRemoteConnection): Promise
133
)
134
: pipe(current, Arr.append(stableConnection));
135
70
- await writeStorageItem(CONNECTIONS_KEY, JSON.stringify({ connections: next }));
136
+ await writeJsonStorageItem(CONNECTIONS_KEY, { connections: next });
137
}
138
139
export async function clearSavedConnection(environmentId: EnvironmentId): Promise<void> {
@@ -76,7 +142,7 @@ export async function clearSavedConnection(environmentId: EnvironmentId): Promis
142
current,
143
Arr.filter((entry) => entry.environmentId !== environmentId),
144
);
79
- await writeStorageItem(CONNECTIONS_KEY, JSON.stringify({ connections: next }));
145
+ await writeJsonStorageItem(CONNECTIONS_KEY, { connections: next });
146
}
147
148
export async function loadPreferences(): Promise<Preferences> {
@@ -106,7 +172,7 @@ export async function savePreferencesPatch(patch: Partial<Preferences>): Promise
172
...current,
173
...patch,
174
};
109
- await writeStorageItem(PREFERENCES_KEY, JSON.stringify(next));
175
+ await writeJsonStorageItem(PREFERENCES_KEY, next);
176
return next;
177
}
178
@@ -116,8 +182,15 @@ export async function loadOrCreateAgentAwarenessDeviceId(): Promise<string> {
182
return existing;
183
}
184
119
- const { uuidv4 } = await import("./uuid");
120
- const deviceId = uuidv4();
185
+ const deviceId = await import("./uuid")
186
+ .then(({ uuidv4 }) => uuidv4())
187
+ .catch((cause) => {
188
+ throw new MobileSecureStorageError({
189
+ operation: "generate-device-id",
190
+ key: AGENT_AWARENESS_DEVICE_ID_KEY,
191
+ cause,
192
+ });
193
+ });
194
await writeStorageItem(AGENT_AWARENESS_DEVICE_ID_KEY, deviceId);
195
return deviceId;
196
}