From c0f27160bf2b6662892854037444adb25e6df770 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 20 Jun 2026 12:14:20 -0700 Subject: [PATCH] [codex] Keep desktop path probes observable Co-authored-by: codex --- .../src/app/DesktopAppIdentity.test.ts | 42 +++++---- apps/desktop/src/app/DesktopAppIdentity.ts | 39 ++++---- apps/desktop/src/app/DesktopAssets.test.ts | 89 +++++++++++-------- apps/desktop/src/app/DesktopAssets.ts | 53 +++++------ 4 files changed, 114 insertions(+), 109 deletions(-) diff --git a/apps/desktop/src/app/DesktopAppIdentity.test.ts b/apps/desktop/src/app/DesktopAppIdentity.test.ts index 3c95b266bc..9a04310dea 100644 --- a/apps/desktop/src/app/DesktopAppIdentity.test.ts +++ b/apps/desktop/src/app/DesktopAppIdentity.test.ts @@ -3,6 +3,7 @@ import { assert, describe, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; +import * as Logger from "effect/Logger"; import * as Option from "effect/Option"; import * as PlatformError from "effect/PlatformError"; @@ -36,6 +37,14 @@ interface ElectronAppCalls { readonly setName: string[]; } +function flattenedLogText(value: unknown): string { + if (typeof value === "string") return value; + if (typeof value !== "object" || value === null) return String(value); + return Object.entries(value) + .flatMap(([key, nested]) => [key, flattenedLogText(nested)]) + .join("\n"); +} + const makeElectronAppLayer = (calls: ElectronAppCalls) => Layer.succeed(ElectronApp.ElectronApp, { metadata: Effect.die("unexpected metadata read"), @@ -123,7 +132,7 @@ const withIdentity = ( Layer.provideMerge( FileSystem.layerNoop({ exists: (path) => - input.legacyPathProbeError + path.includes("T3 Code (Alpha)") && input.legacyPathProbeError !== undefined ? Effect.fail(input.legacyPathProbeError) : Effect.succeed( input.legacyPathExists === true && path.includes("T3 Code (Alpha)"), @@ -153,31 +162,32 @@ describe("DesktopAppIdentity", () => { ), ); - it.effect("preserves failures while inspecting the legacy userData path", () => { - const legacyPath = "/Users/alice/Library/Application Support/T3 Code (Alpha)"; - const cause = PlatformError.systemError({ + it.effect("falls back and reports a failed legacy userData path probe", () => { + const probeError = PlatformError.systemError({ _tag: "PermissionDenied", module: "FileSystem", method: "exists", - description: "permission denied", - pathOrDescriptor: legacyPath, + pathOrDescriptor: "/Users/alice/Library/Application Support/T3 Code (Alpha)", + }); + const capturedLogs: Array> = []; + const logger = Logger.make(({ message }) => { + capturedLogs.push(Array.isArray(message) ? message : [message]); }); return withIdentity( Effect.gen(function* () { const identity = yield* DesktopAppIdentity.DesktopAppIdentity; - const error = yield* identity.resolveUserDataPath.pipe(Effect.flip); + const userDataPath = yield* identity.resolveUserDataPath; - assert.instanceOf(error, DesktopAppIdentity.DesktopUserDataPathResolutionError); - assert.equal(error.legacyPath, legacyPath); - assert.strictEqual(error.cause, cause); - assert.equal( - error.message, - `Failed to inspect legacy desktop user-data path at "${legacyPath}".`, - ); + assert.equal(userDataPath, "/Users/alice/Library/Application Support/t3code"); + const logText = flattenedLogText(capturedLogs); + assert.include(logText, "desktop.appIdentity.legacyUserDataProbe.failed"); + assert.include(logText, "/Users/alice/Library/Application Support/T3 Code (Alpha)"); + assert.include(logText, "/Users/alice/Library/Application Support/t3code"); + assert.include(logText, "PermissionDenied"); }), - { legacyPathProbeError: cause }, - ); + { legacyPathProbeError: probeError }, + ).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false }))); }); it.effect("configures app identity from the environment commit override", () => { diff --git a/apps/desktop/src/app/DesktopAppIdentity.ts b/apps/desktop/src/app/DesktopAppIdentity.ts index 385e694338..46badba61d 100644 --- a/apps/desktop/src/app/DesktopAppIdentity.ts +++ b/apps/desktop/src/app/DesktopAppIdentity.ts @@ -18,22 +18,10 @@ const AppPackageMetadata = Schema.Struct({ }); const decodeAppPackageMetadata = Schema.decodeEffect(Schema.fromJsonString(AppPackageMetadata)); -export class DesktopUserDataPathResolutionError extends Schema.TaggedErrorClass()( - "DesktopUserDataPathResolutionError", - { - legacyPath: Schema.String, - cause: Schema.Defect(), - }, -) { - override get message(): string { - return `Failed to inspect legacy desktop user-data path at "${this.legacyPath}".`; - } -} - export class DesktopAppIdentity extends Context.Service< DesktopAppIdentity, { - readonly resolveUserDataPath: Effect.Effect; + readonly resolveUserDataPath: Effect.Effect; readonly configure: Effect.Effect; } >()("@t3tools/desktop/app/DesktopAppIdentity") {} @@ -95,18 +83,21 @@ export const make = Effect.gen(function* () { environment.appDataDirectory, environment.legacyUserDataDirName, ); - const legacyPathExists = yield* fileSystem.exists(legacyPath).pipe( - Effect.mapError( - (cause) => - new DesktopUserDataPathResolutionError({ - legacyPath, - cause, - }), - ), + const fallbackPath = environment.path.join( + environment.appDataDirectory, + environment.userDataDirName, ); - return legacyPathExists - ? legacyPath - : environment.path.join(environment.appDataDirectory, environment.userDataDirName); + const legacyPathExists = yield* fileSystem.exists(legacyPath).pipe( + Effect.tapError((cause) => + Effect.logWarning("desktop.appIdentity.legacyUserDataProbe.failed", { + legacyPath, + fallbackPath, + cause, + }), + ), + Effect.orElseSucceed(() => false), + ); + return legacyPathExists ? legacyPath : fallbackPath; }).pipe(Effect.withSpan("desktop.appIdentity.resolveUserDataPath")); const configure = Effect.gen(function* () { diff --git a/apps/desktop/src/app/DesktopAssets.test.ts b/apps/desktop/src/app/DesktopAssets.test.ts index 2eb55c7205..685e33b6b8 100644 --- a/apps/desktop/src/app/DesktopAssets.test.ts +++ b/apps/desktop/src/app/DesktopAssets.test.ts @@ -3,55 +3,72 @@ import { assert, describe, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; +import * as Logger from "effect/Logger"; +import * as Option from "effect/Option"; import * as PlatformError from "effect/PlatformError"; import * as DesktopAssets from "./DesktopAssets.ts"; import * as DesktopConfig from "./DesktopConfig.ts"; import * as DesktopEnvironment from "./DesktopEnvironment.ts"; -const environmentLayer = DesktopEnvironment.layer({ +const environmentInput = { dirname: "/repo/apps/desktop/dist-electron", homeDirectory: "/Users/alice", - platform: "darwin", - processArch: "arm64", + platform: "linux", + processArch: "x64", appVersion: "1.2.3", - appPath: "/Applications/T3 Code.app/Contents/Resources/app.asar", + appPath: "/repo", isPackaged: true, - resourcesPath: "/Applications/T3 Code.app/Contents/Resources", + resourcesPath: "/resources", runningUnderArm64Translation: false, -}).pipe(Layer.provide(Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({})))); +} satisfies DesktopEnvironment.MakeDesktopEnvironmentInput; + +function flattenedLogText(value: unknown): string { + if (typeof value === "string") return value; + if (typeof value !== "object" || value === null) return String(value); + return Object.entries(value) + .flatMap(([key, nested]) => [key, flattenedLogText(nested)]) + .join("\n"); +} describe("DesktopAssets", () => { - it.effect("preserves the failed asset candidate and filesystem cause", () => - Effect.gen(function* () { - const fileName = "custom.bin"; - const candidatePath = "/repo/apps/desktop/resources/custom.bin"; - const cause = PlatformError.systemError({ - _tag: "PermissionDenied", - module: "FileSystem", - method: "exists", - pathOrDescriptor: candidatePath, - description: "private filesystem diagnostic", - }); - const fileSystemLayer = FileSystem.layerNoop({ - exists: (path) => (path === candidatePath ? Effect.fail(cause) : Effect.succeed(false)), - }); - const assetsLayer = DesktopAssets.layer.pipe( - Layer.provide(Layer.merge(fileSystemLayer, environmentLayer)), - ); - const assets = yield* DesktopAssets.DesktopAssets.pipe(Effect.provide(assetsLayer)); + it.effect("continues resource lookup and reports failed existence probes", () => { + const firstCandidate = "/repo/apps/desktop/resources/icon.ico"; + const fallbackCandidate = "/repo/apps/desktop/prod-resources/icon.ico"; + const probeError = PlatformError.systemError({ + _tag: "PermissionDenied", + module: "FileSystem", + method: "exists", + pathOrDescriptor: firstCandidate, + }); + const capturedLogs: Array> = []; + const logger = Logger.make(({ message }) => { + capturedLogs.push(Array.isArray(message) ? message : [message]); + }); + const fileSystemLayer = FileSystem.layerNoop({ + exists: (path) => { + if (path === firstCandidate) return Effect.fail(probeError); + return Effect.succeed(path === fallbackCandidate); + }, + }); + const environmentLayer = DesktopEnvironment.layer(environmentInput).pipe( + Layer.provide(Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({}))), + ); + const assetsLayer = DesktopAssets.layer.pipe( + Layer.provideMerge(fileSystemLayer), + Layer.provideMerge(environmentLayer), + Layer.provide(Logger.layer([logger], { mergeWithExisting: false })), + ); - const error = yield* assets.resolveResourcePath(fileName).pipe(Effect.flip); + return Effect.gen(function* () { + const assets = yield* DesktopAssets.DesktopAssets; + const iconPaths = yield* assets.iconPaths; - assert.instanceOf(error, DesktopAssets.DesktopAssetProbeError); - assert.equal(error.fileName, fileName); - assert.equal(error.candidatePath, candidatePath); - assert.strictEqual(error.cause, cause); - assert.equal( - error.message, - `Failed to probe desktop asset "${fileName}" at ${candidatePath}.`, - ); - assert.notInclude(error.message, "private filesystem diagnostic"); - }), - ); + assert.deepEqual(iconPaths.ico, Option.some(fallbackCandidate)); + const logText = flattenedLogText(capturedLogs); + assert.include(logText, "desktop.assets.resourceProbe.failed"); + assert.include(logText, firstCandidate); + assert.include(logText, "PermissionDenied"); + }).pipe(Effect.provide(assetsLayer)); + }); }); diff --git a/apps/desktop/src/app/DesktopAssets.ts b/apps/desktop/src/app/DesktopAssets.ts index 95585acab7..15c7090f9e 100644 --- a/apps/desktop/src/app/DesktopAssets.ts +++ b/apps/desktop/src/app/DesktopAssets.ts @@ -3,7 +3,6 @@ import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; -import * as Schema from "effect/Schema"; import * as DesktopEnvironment from "./DesktopEnvironment.ts"; @@ -13,26 +12,11 @@ export interface DesktopIconPaths { readonly png: Option.Option; } -export class DesktopAssetProbeError extends Schema.TaggedErrorClass()( - "DesktopAssetProbeError", - { - fileName: Schema.String, - candidatePath: Schema.String, - cause: Schema.Defect(), - }, -) { - override get message(): string { - return `Failed to probe desktop asset "${this.fileName}" at ${this.candidatePath}.`; - } -} - export class DesktopAssets extends Context.Service< DesktopAssets, { readonly iconPaths: Effect.Effect; - readonly resolveResourcePath: ( - fileName: string, - ) => Effect.Effect, DesktopAssetProbeError>; + readonly resolveResourcePath: (fileName: string) => Effect.Effect>; } >()("@t3tools/desktop/app/DesktopAssets") {} @@ -40,20 +24,23 @@ const resolveResourcePath = Effect.fn("desktop.assets.resolveResourcePath")(func fileName: string, ): Effect.fn.Return< Option.Option, - DesktopAssetProbeError, + never, FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment > { const fileSystem = yield* FileSystem.FileSystem; const environment = yield* DesktopEnvironment.DesktopEnvironment; const candidates = environment.resolveResourcePathCandidates(fileName); for (const candidate of candidates) { - const exists = yield* fileSystem - .exists(candidate) - .pipe( - Effect.mapError( - (cause) => new DesktopAssetProbeError({ fileName, candidatePath: candidate, cause }), - ), - ); + const exists = yield* fileSystem.exists(candidate).pipe( + Effect.tapError((cause) => + Effect.logWarning("desktop.assets.resourceProbe.failed", { + fileName, + candidatePath: candidate, + cause, + }), + ), + Effect.orElseSucceed(() => false), + ); if (exists) { return Option.some(candidate); } @@ -65,7 +52,7 @@ const resolveIconPath = Effect.fn("desktop.assets.resolveIconPath")(function* ( ext: keyof DesktopIconPaths, ): Effect.fn.Return< Option.Option, - DesktopAssetProbeError, + never, FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment > { const fileSystem = yield* FileSystem.FileSystem; @@ -73,14 +60,14 @@ const resolveIconPath = Effect.fn("desktop.assets.resolveIconPath")(function* ( if (environment.isDevelopment && environment.platform === "darwin" && ext === "png") { const developmentDockIconPath = environment.developmentDockIconPath; const developmentDockIconExists = yield* fileSystem.exists(developmentDockIconPath).pipe( - Effect.mapError( - (cause) => - new DesktopAssetProbeError({ - fileName: "icon.png", - candidatePath: developmentDockIconPath, - cause, - }), + Effect.tapError((cause) => + Effect.logWarning("desktop.assets.resourceProbe.failed", { + fileName: "development-dock-icon", + candidatePath: developmentDockIconPath, + cause, + }), ), + Effect.orElseSucceed(() => false), ); if (developmentDockIconExists) { return Option.some(developmentDockIconPath);