[codex] Keep desktop path probes observable

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-06-20 20:17:07 -07:00
1 parent 0d94e227e6
commit c0f27160bf
4 files changed
+114 -109

No files matched your search

+26 -16
View File
@@ -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 = <A, E, R>(
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<ReadonlyArray<unknown>> = [];
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", () => {
+15 -24
View File
@@ -18,22 +18,10 @@ const AppPackageMetadata = Schema.Struct({
});
const decodeAppPackageMetadata = Schema.decodeEffect(Schema.fromJsonString(AppPackageMetadata));
export class DesktopUserDataPathResolutionError extends Schema.TaggedErrorClass<DesktopUserDataPathResolutionError>()(
"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<string, DesktopUserDataPathResolutionError>;
readonly resolveUserDataPath: Effect.Effect<string>;
readonly configure: Effect.Effect<void>;
}
>()("@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* () {
+53 -36
View File
@@ -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<ReadonlyArray<unknown>> = [];
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));
});
});
+20 -33
View File
@@ -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<string>;
}
export class DesktopAssetProbeError extends Schema.TaggedErrorClass<DesktopAssetProbeError>()(
"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<DesktopIconPaths>;
readonly resolveResourcePath: (
fileName: string,
) => Effect.Effect<Option.Option<string>, DesktopAssetProbeError>;
readonly resolveResourcePath: (fileName: string) => Effect.Effect<Option.Option<string>>;
}
>()("@t3tools/desktop/app/DesktopAssets") {}
@@ -40,20 +24,23 @@ const resolveResourcePath = Effect.fn("desktop.assets.resolveResourcePath")(func
fileName: string,
): Effect.fn.Return<
Option.Option<string>,
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<string>,
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);