From d7849fa7028ccf196f0eee977be5f00e65d09bf9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 20 Jun 2026 18:10:46 -0700 Subject: [PATCH] refactor(connection): preserve persistence failure causes Co-authored-by: codex --- apps/mobile/src/connection/catalog-store.ts | 3 +- apps/mobile/src/connection/storage.ts | 107 ++++++++++-------- apps/web/src/connection/storage.test.ts | 5 +- apps/web/src/connection/storage.ts | 81 +++++++------ .../client-runtime/src/connection/model.ts | 1 + .../src/connection/registry.test.ts | 5 +- .../src/platform/persistence.ts | 8 +- 7 files changed, 122 insertions(+), 88 deletions(-) diff --git a/apps/mobile/src/connection/catalog-store.ts b/apps/mobile/src/connection/catalog-store.ts index b5bda40067..88e4b06b2b 100644 --- a/apps/mobile/src/connection/catalog-store.ts +++ b/apps/mobile/src/connection/catalog-store.ts @@ -18,7 +18,8 @@ export const LEGACY_CONNECTIONS_KEY = "t3code.connections"; function catalogError(operation: string, cause: unknown) { return new ConnectionTransientError({ reason: "remote-unavailable", - detail: `Could not ${operation} the local connection catalog: ${String(cause)}`, + detail: `Could not ${operation} the local connection catalog.`, + cause, }); } diff --git a/apps/mobile/src/connection/storage.ts b/apps/mobile/src/connection/storage.ts index 276ea3c5c0..36dd765120 100644 --- a/apps/mobile/src/connection/storage.ts +++ b/apps/mobile/src/connection/storage.ts @@ -58,23 +58,8 @@ const LegacyStoredShellSnapshot = Schema.Struct({ function catalogError(operation: string, cause: unknown) { return new ConnectionTransientError({ reason: "remote-unavailable", - detail: `Could not ${operation} the local connection catalog: ${String(cause)}`, - }); -} - -function shellPersistenceError( - operation: - | "load-shell" - | "save-shell" - | "load-thread" - | "save-thread" - | "remove-thread" - | "clear-environment", - cause: unknown, -) { - return new ConnectionPersistenceError({ - operation, - message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`, + detail: `Could not ${operation} the local connection catalog.`, + cause, }); } @@ -100,7 +85,7 @@ const threadSnapshotDirectory = Effect.fn("mobile.connectionStorage.threadSnapsh } return directory; }, - catch: (cause) => shellPersistenceError(operation, cause), + catch: (cause) => new ConnectionPersistenceError({ operation, cause }), }); }, ); @@ -117,16 +102,6 @@ const threadSnapshotFile = Effect.fn("mobile.connectionStorage.threadSnapshotFil ); }); -function targetPersistenceError( - operation: "list-targets" | "register-connection" | "remove-connection", - error: ConnectionTransientError, -) { - return new ConnectionPersistenceError({ - operation, - message: error.message, - }); -} - const secureCatalogStorage: SecureCatalogStorage = { getItem: (key) => Effect.tryPromise({ @@ -163,7 +138,7 @@ const shellSnapshotFileInDirectory = Effect.fn( directory.create({ idempotent: true, intermediates: true }); return new File(directory, shellSnapshotFileName(environmentId)); }, - catch: (cause) => shellPersistenceError(operation, cause), + catch: (cause) => new ConnectionPersistenceError({ operation, cause }), }); }); @@ -184,18 +159,29 @@ export const connectionStorageLayer = Layer.effectContext( const targetStore = ConnectionTargetStore.of({ list: catalog.read.pipe( Effect.map((document) => document.targets), - Effect.mapError((error) => targetPersistenceError("list-targets", error)), + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "list-targets", cause }), + ), ), }); const registrationStore = ConnectionRegistrationStore.of({ register: (registration) => catalog .update((document) => registerConnectionInCatalog(document, registration)) - .pipe(Effect.mapError((error) => targetPersistenceError("register-connection", error))), + .pipe( + Effect.mapError( + (cause) => + new ConnectionPersistenceError({ operation: "register-connection", cause }), + ), + ), remove: (target) => catalog .update((document) => removeConnectionFromCatalog(document, target)) - .pipe(Effect.mapError((error) => targetPersistenceError("remove-connection", error))), + .pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "remove-connection", cause }), + ), + ), }); const profileStore = ProfileStore.make({ get: (connectionId) => @@ -283,15 +269,19 @@ export const connectionStorageLayer = Layer.effectContext( if (file.exists) { const raw = yield* Effect.tryPromise({ try: () => file.text(), - catch: (cause) => shellPersistenceError("load-shell", cause), + catch: (cause) => new ConnectionPersistenceError({ operation: "load-shell", cause }), }); const parsed = yield* Effect.try({ try: () => JSON.parse(raw) as unknown, - catch: (cause) => shellPersistenceError("load-shell", cause), + catch: (cause) => new ConnectionPersistenceError({ operation: "load-shell", cause }), }); const stored = yield* Effect.fromResult( Schema.decodeUnknownResult(StoredShellSnapshot)(parsed), - ).pipe(Effect.mapError((cause) => shellPersistenceError("load-shell", cause))); + ).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "load-shell", cause }), + ), + ); return stored.environmentId === environmentId ? Option.some(stored.snapshot) : Option.none(); @@ -303,15 +293,19 @@ export const connectionStorageLayer = Layer.effectContext( } const legacyRaw = yield* Effect.tryPromise({ try: () => legacyFile.text(), - catch: (cause) => shellPersistenceError("load-shell", cause), + catch: (cause) => new ConnectionPersistenceError({ operation: "load-shell", cause }), }); const legacyParsed = yield* Effect.try({ try: () => JSON.parse(legacyRaw) as unknown, - catch: (cause) => shellPersistenceError("load-shell", cause), + catch: (cause) => new ConnectionPersistenceError({ operation: "load-shell", cause }), }); const legacyStored = yield* Effect.fromResult( Schema.decodeUnknownResult(LegacyStoredShellSnapshot)(legacyParsed), - ).pipe(Effect.mapError((cause) => shellPersistenceError("load-shell", cause))); + ).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "load-shell", cause }), + ), + ); return legacyStored.environmentId === environmentId ? Option.some(legacyStored.snapshot) : Option.none(); @@ -326,7 +320,11 @@ export const connectionStorageLayer = Layer.effectContext( } as const; const encoded = yield* Effect.fromResult( Schema.encodeUnknownResult(StoredShellSnapshot)(stored), - ).pipe(Effect.mapError((cause) => shellPersistenceError("save-shell", cause))); + ).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "save-shell", cause }), + ), + ); yield* Effect.try({ try: () => { if (!file.exists) { @@ -334,7 +332,7 @@ export const connectionStorageLayer = Layer.effectContext( } file.write(JSON.stringify(encoded)); }, - catch: (cause) => shellPersistenceError("save-shell", cause), + catch: (cause) => new ConnectionPersistenceError({ operation: "save-shell", cause }), }); }), loadThread: (environmentId, threadId) => @@ -345,15 +343,19 @@ export const connectionStorageLayer = Layer.effectContext( } const raw = yield* Effect.tryPromise({ try: () => file.text(), - catch: (cause) => shellPersistenceError("load-thread", cause), + catch: (cause) => new ConnectionPersistenceError({ operation: "load-thread", cause }), }); const parsed = yield* Effect.try({ try: () => JSON.parse(raw) as unknown, - catch: (cause) => shellPersistenceError("load-thread", cause), + catch: (cause) => new ConnectionPersistenceError({ operation: "load-thread", cause }), }); const stored = yield* Effect.fromResult( Schema.decodeUnknownResult(StoredThreadSnapshot)(parsed), - ).pipe(Effect.mapError((cause) => shellPersistenceError("load-thread", cause))); + ).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "load-thread", cause }), + ), + ); return stored.environmentId === environmentId && stored.threadId === threadId ? Option.some(stored.thread) : Option.none(); @@ -368,7 +370,11 @@ export const connectionStorageLayer = Layer.effectContext( threadId: thread.id, thread, }), - ).pipe(Effect.mapError((cause) => shellPersistenceError("save-thread", cause))); + ).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "save-thread", cause }), + ), + ); yield* Effect.try({ try: () => { if (!file.exists) { @@ -376,7 +382,7 @@ export const connectionStorageLayer = Layer.effectContext( } file.write(JSON.stringify(encoded)); }, - catch: (cause) => shellPersistenceError("save-thread", cause), + catch: (cause) => new ConnectionPersistenceError({ operation: "save-thread", cause }), }); }), removeThread: (environmentId, threadId) => @@ -389,7 +395,7 @@ export const connectionStorageLayer = Layer.effectContext( Effect.mapError((cause) => cause._tag === "ConnectionPersistenceError" ? cause - : shellPersistenceError("remove-thread", cause), + : new ConnectionPersistenceError({ operation: "remove-thread", cause }), ), ), clear: (environmentId) => @@ -398,14 +404,16 @@ export const connectionStorageLayer = Layer.effectContext( if (file.exists) { yield* Effect.try({ try: () => file.delete(), - catch: (cause) => shellPersistenceError("clear-environment", cause), + catch: (cause) => + new ConnectionPersistenceError({ operation: "clear-environment", cause }), }); } const legacyFile = yield* legacyShellSnapshotFile(environmentId, "clear-environment"); if (legacyFile.exists) { yield* Effect.try({ try: () => legacyFile.delete(), - catch: (cause) => shellPersistenceError("clear-environment", cause), + catch: (cause) => + new ConnectionPersistenceError({ operation: "clear-environment", cause }), }); } const threadDirectory = yield* threadSnapshotDirectory( @@ -415,7 +423,8 @@ export const connectionStorageLayer = Layer.effectContext( if (threadDirectory.exists) { yield* Effect.try({ try: () => threadDirectory.delete(), - catch: (cause) => shellPersistenceError("clear-environment", cause), + catch: (cause) => + new ConnectionPersistenceError({ operation: "clear-environment", cause }), }); } }), diff --git a/apps/web/src/connection/storage.test.ts b/apps/web/src/connection/storage.test.ts index 6d503387bb..99bfdcd084 100644 --- a/apps/web/src/connection/storage.test.ts +++ b/apps/web/src/connection/storage.test.ts @@ -70,7 +70,10 @@ describe("makeCatalogBackend", () => { const error = yield* backend.write("{}").pipe(Effect.flip); expect(error).toBeInstanceOf(ConnectionTransientError); - expect(error.message).toContain("Desktop secure storage is unavailable"); + expect(error.message).toBe("Could not save the local connection catalog."); + expect(error.cause).toEqual( + new Error("Desktop secure storage is unavailable in this system context."), + ); expect(setConnectionCatalog).toHaveBeenCalledWith("{}"); }), ); diff --git a/apps/web/src/connection/storage.ts b/apps/web/src/connection/storage.ts index d118a428ed..ae5e591cc6 100644 --- a/apps/web/src/connection/storage.ts +++ b/apps/web/src/connection/storage.ts @@ -63,26 +63,8 @@ const encodeStoredThreadSnapshot = Schema.encodeEffect(StoredThreadSnapshotJson) function catalogError(operation: string, cause: unknown) { return new ConnectionTransientError({ reason: "remote-unavailable", - detail: `Could not ${operation} the local connection catalog: ${String(cause)}`, - }); -} - -function persistenceError( - operation: - | "list-targets" - | "register-connection" - | "remove-connection" - | "load-shell" - | "save-shell" - | "load-thread" - | "save-thread" - | "remove-thread" - | "clear-environment", - cause: unknown, -) { - return new ConnectionPersistenceError({ - operation, - message: `Could not ${operation.replaceAll("-", " ")}: ${String(cause)}`, + detail: `Could not ${operation} the local connection catalog.`, + cause, }); } @@ -233,7 +215,7 @@ export function makeCatalogBackend(database: IDBDatabase): CatalogBackend { : Effect.fail( catalogError( "save", - "Desktop secure storage is unavailable in this system context.", + new Error("Desktop secure storage is unavailable in this system context."), ), ), ), @@ -330,18 +312,29 @@ export const connectionStorageLayer = Layer.effectContext( const targetStore = ConnectionTargetStore.of({ list: catalog.read.pipe( Effect.map((document) => document.targets), - Effect.mapError((cause) => persistenceError("list-targets", cause)), + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "list-targets", cause }), + ), ), }); const registrationStore = ConnectionRegistrationStore.of({ register: (registration) => catalog .update((document) => registerConnectionInCatalog(document, registration)) - .pipe(Effect.mapError((cause) => persistenceError("register-connection", cause))), + .pipe( + Effect.mapError( + (cause) => + new ConnectionPersistenceError({ operation: "register-connection", cause }), + ), + ), remove: (target) => catalog .update((document) => removeConnectionFromCatalog(document, target)) - .pipe(Effect.mapError((cause) => persistenceError("remove-connection", cause))), + .pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "remove-connection", cause }), + ), + ), }); const profileStore = ProfileStore.make({ get: (connectionId) => @@ -430,7 +423,9 @@ export const connectionStorageLayer = Layer.effectContext( return Effect.succeed(Option.none()); } return decodeStoredShellSnapshot(raw).pipe( - Effect.mapError((cause) => persistenceError("load-shell", cause)), + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "load-shell", cause }), + ), Effect.map((stored) => stored.environmentId === environmentId ? Option.some(stored.snapshot) @@ -441,7 +436,7 @@ export const connectionStorageLayer = Layer.effectContext( Effect.mapError((cause) => cause._tag === "ConnectionPersistenceError" ? cause - : persistenceError("load-shell", cause), + : new ConnectionPersistenceError({ operation: "load-shell", cause }), ), ), saveShell: (environmentId, snapshot) => @@ -450,13 +445,17 @@ export const connectionStorageLayer = Layer.effectContext( schemaVersion: SHELL_SNAPSHOT_CACHE_SCHEMA_VERSION, environmentId, snapshot, - }).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause))); + }).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "save-shell", cause }), + ), + ); yield* writeDatabaseValue(database, SHELL_STORE_NAME, environmentId, encoded); }).pipe( Effect.mapError((cause) => cause._tag === "ConnectionPersistenceError" ? cause - : persistenceError("save-shell", cause), + : new ConnectionPersistenceError({ operation: "save-shell", cause }), ), ), loadThread: (environmentId, threadId) => @@ -470,7 +469,9 @@ export const connectionStorageLayer = Layer.effectContext( return Effect.succeed(Option.none()); } return decodeStoredThreadSnapshot(raw).pipe( - Effect.mapError((cause) => persistenceError("load-thread", cause)), + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "load-thread", cause }), + ), Effect.map((stored) => stored.environmentId === environmentId && stored.threadId === threadId ? Option.some(stored.thread) @@ -481,7 +482,7 @@ export const connectionStorageLayer = Layer.effectContext( Effect.mapError((cause) => cause._tag === "ConnectionPersistenceError" ? cause - : persistenceError("load-thread", cause), + : new ConnectionPersistenceError({ operation: "load-thread", cause }), ), ), saveThread: (environmentId, thread) => @@ -491,7 +492,11 @@ export const connectionStorageLayer = Layer.effectContext( environmentId, threadId: thread.id, thread, - }).pipe(Effect.mapError((cause) => persistenceError("save-thread", cause))); + }).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "save-thread", cause }), + ), + ); yield* writeDatabaseValue( database, THREAD_STORE_NAME, @@ -502,7 +507,7 @@ export const connectionStorageLayer = Layer.effectContext( Effect.mapError((cause) => cause._tag === "ConnectionPersistenceError" ? cause - : persistenceError("save-thread", cause), + : new ConnectionPersistenceError({ operation: "save-thread", cause }), ), ), removeThread: (environmentId, threadId) => @@ -510,7 +515,11 @@ export const connectionStorageLayer = Layer.effectContext( database, THREAD_STORE_NAME, threadCacheKey(environmentId, threadId), - ).pipe(Effect.mapError((cause) => persistenceError("remove-thread", cause))), + ).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "remove-thread", cause }), + ), + ), clear: (environmentId) => Effect.all( [ @@ -522,7 +531,11 @@ export const connectionStorageLayer = Layer.effectContext( ), ], { concurrency: "unbounded", discard: true }, - ).pipe(Effect.mapError((cause) => persistenceError("clear-environment", cause))), + ).pipe( + Effect.mapError( + (cause) => new ConnectionPersistenceError({ operation: "clear-environment", cause }), + ), + ), }); return Context.make(ConnectionTargetStore, targetStore).pipe( diff --git a/packages/client-runtime/src/connection/model.ts b/packages/client-runtime/src/connection/model.ts index fbcb302ed1..bc8c64fd7a 100644 --- a/packages/client-runtime/src/connection/model.ts +++ b/packages/client-runtime/src/connection/model.ts @@ -81,6 +81,7 @@ export class ConnectionTransientError extends Schema.TaggedErrorClass { it.effect("keeps the runtime registered when durable removal fails", () => Effect.gen(function* () { + const persistenceCause = new Error("Storage is unavailable."); const harness = yield* makeHarness([RELAY_TARGET], [], [], { beforeRegistrationRemove: () => Effect.fail( new Persistence.ConnectionPersistenceError({ operation: "remove-connection", - message: "Storage is unavailable.", + cause: persistenceCause, }), ), }); @@ -711,6 +712,8 @@ describe("EnvironmentRegistry", () => { const error = yield* Effect.flip(registry.removeRelayEnvironments()); expect(error._tag).toBe("ConnectionPersistenceError"); + expect(error.message).toBe("Could not remove connection."); + expect(error.cause).toBe(persistenceCause); expect(yield* Ref.get(harness.releasedSessions)).toBe(0); expect((yield* SubscriptionRef.get(registry.entries)).has(RELAY_TARGET.environmentId)).toBe( true, diff --git a/packages/client-runtime/src/platform/persistence.ts b/packages/client-runtime/src/platform/persistence.ts index 71664bf460..c5547ed723 100644 --- a/packages/client-runtime/src/platform/persistence.ts +++ b/packages/client-runtime/src/platform/persistence.ts @@ -26,9 +26,13 @@ export class ConnectionPersistenceError extends Schema.TaggedErrorClass