Skip to content

Commit a09ddf6

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-29297
2 parents 77e7ca4 + 8c9af54 commit a09ddf6

266 files changed

Lines changed: 10919 additions & 1235 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/app/DesktopApp.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Layer from "effect/Layer";
77
import * as FileSystem from "effect/FileSystem";
88
import * as Path from "effect/Path";
99
import * as HttpClient from "effect/http/HttpClient";
10+
import { ChildProcessSpawner } from "effect/process";
1011
import * as NetService from "@t3tools/shared/Net";
1112
import * as Fiber from "effect/Fiber";
1213
import * as Option from "effect/Option";
@@ -213,6 +214,7 @@ it.effect.each([false, true])(
213214
Effect.provide(
214215
Layer.mergeAll(
215216
Layer.mock(BrowserImport.BrowserImport, {}),
217+
Layer.mock(ChildProcessSpawner.ChildProcessSpawner, {}),
216218
Layer.mock(DesktopBackendConfiguration.DesktopBackendConfiguration, {}),
217219
Layer.mock(DesktopCliCommand.DesktopCliCommand, {}),
218220
Layer.mock(DesktopClientSettings.DesktopClientSettings, {}),

‎apps/desktop/src/app/DesktopClerk.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ export const make = Effect.gen(function* () {
116116
yield* electronApp.quit;
117117
return yield* Effect.interrupt;
118118
}
119+
// Chromium opens these caches at the first event-loop turn, before ready.
120+
yield* DesktopUserData.clearStaleGpuCaches({
121+
userDataPath,
122+
electronVersion: process.versions.electron,
123+
});
119124

120125
const pendingSecondInstances: Array<readonly string[]> = [];
121126
let handleSecondInstance = (argv: readonly string[]) => {

‎apps/desktop/src/app/DesktopPreReadyFileSystem.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ export const make = FileSystem.makeNoop({
4949
syncFs("makeDirectory", path, () => {
5050
NodeFS.mkdirSync(path, { recursive: options?.recursive ?? false });
5151
}),
52+
remove: (path, options) =>
53+
syncFs("remove", path, () =>
54+
NodeFS.rmSync(path, {
55+
recursive: options?.recursive ?? false,
56+
force: options?.force ?? false,
57+
}),
58+
),
5259
writeFileString: (path, data, options) =>
5360
syncFs("writeFileString", path, () =>
5461
NodeFS.writeFileSync(path, data, { flag: options?.flag ?? "w" }),

‎apps/desktop/src/app/DesktopUserData.test.ts‎

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { assert, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as FileSystem from "effect/FileSystem";
55
import * as Path from "effect/Path";
6+
import * as PlatformError from "effect/PlatformError";
67

7-
import { resolveUserDataPath } from "./DesktopUserData.ts";
8+
import { clearStaleGpuCaches, resolveUserDataPath } from "./DesktopUserData.ts";
89

910
it.effect.each(["t3code", "T3 Code (Alpha)"])(
1011
"preserves Windows credential keys from %s without copying browser databases",
@@ -40,3 +41,100 @@ it.effect.each(["t3code", "T3 Code (Alpha)"])(
4041
);
4142
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
4243
);
44+
45+
const gpuCacheDirectories = [
46+
"GPUCache",
47+
"GrShaderCache",
48+
"ShaderCache",
49+
"DawnGraphiteCache",
50+
"DawnWebGPUCache",
51+
"GPUPersistentCache",
52+
];
53+
54+
it.effect.each([
55+
{ profile: "written by Electron 44.10.0", marker: "44.10.0" },
56+
{ profile: "with no recorded Electron version", marker: undefined },
57+
])("clears GPU caches $profile once when Electron 44.10.1 starts", ({ marker }) =>
58+
Effect.gen(function* () {
59+
const fs = yield* FileSystem.FileSystem;
60+
const path = yield* Path.Path;
61+
const userDataPath = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-gpu-cache-" });
62+
for (const name of gpuCacheDirectories) {
63+
yield* fs.makeDirectory(path.join(userDataPath, name), { recursive: true });
64+
yield* fs.writeFileString(path.join(userDataPath, name, "data_0"), "44.10.0 shaders");
65+
}
66+
yield* fs.makeDirectory(path.join(userDataPath, "Local Storage"), { recursive: true });
67+
yield* fs.writeFileString(path.join(userDataPath, "Local Storage", "LOCK"), "profile");
68+
yield* fs.writeFileString(path.join(userDataPath, "Local State"), "profile");
69+
if (marker !== undefined) {
70+
yield* fs.writeFileString(path.join(userDataPath, "gpu-cache-electron-version"), marker);
71+
}
72+
73+
yield* clearStaleGpuCaches({ userDataPath, electronVersion: "44.10.1" });
74+
75+
const survivingCaches: Array<string> = [];
76+
for (const name of gpuCacheDirectories) {
77+
if (yield* fs.exists(path.join(userDataPath, name))) survivingCaches.push(name);
78+
}
79+
assert.deepStrictEqual(survivingCaches, []);
80+
assert.equal(
81+
yield* fs.readFileString(path.join(userDataPath, "Local Storage", "LOCK")),
82+
"profile",
83+
);
84+
assert.equal(yield* fs.readFileString(path.join(userDataPath, "Local State")), "profile");
85+
86+
yield* fs.makeDirectory(path.join(userDataPath, "GPUCache"));
87+
yield* fs.writeFileString(path.join(userDataPath, "GPUCache", "data_0"), "44.10.1 shaders");
88+
yield* clearStaleGpuCaches({ userDataPath, electronVersion: "44.10.1" });
89+
assert.equal(
90+
yield* fs.readFileString(path.join(userDataPath, "GPUCache", "data_0")),
91+
"44.10.1 shaders",
92+
);
93+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
94+
);
95+
96+
it.effect("keeps GPU caches built on the first launch of a fresh install", () =>
97+
Effect.gen(function* () {
98+
const fs = yield* FileSystem.FileSystem;
99+
const path = yield* Path.Path;
100+
const appData = yield* fs.makeTempDirectoryScoped({ prefix: "t3-v2-fresh-" });
101+
const userDataPath = path.join(appData, "t3code-v2");
102+
103+
yield* clearStaleGpuCaches({ userDataPath, electronVersion: "44.10.1" });
104+
yield* fs.makeDirectory(path.join(userDataPath, "GPUCache"));
105+
yield* fs.writeFileString(path.join(userDataPath, "GPUCache", "data_0"), "44.10.1 shaders");
106+
yield* clearStaleGpuCaches({ userDataPath, electronVersion: "44.10.1" });
107+
108+
assert.equal(
109+
yield* fs.readFileString(path.join(userDataPath, "GPUCache", "data_0")),
110+
"44.10.1 shaders",
111+
);
112+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
113+
);
114+
115+
it.effect("retries on the next launch when a GPU cache cannot be removed", () =>
116+
Effect.gen(function* () {
117+
const writes: Array<string> = [];
118+
const fileSystem = FileSystem.makeNoop({
119+
remove: (path) =>
120+
Effect.fail(
121+
PlatformError.systemError({
122+
_tag: "PermissionDenied",
123+
module: "FileSystem",
124+
method: "remove",
125+
pathOrDescriptor: path,
126+
}),
127+
),
128+
writeFileString: (path) =>
129+
Effect.sync(() => {
130+
writes.push(path);
131+
}),
132+
});
133+
134+
yield* clearStaleGpuCaches({ userDataPath: "/profile", electronVersion: "44.10.1" }).pipe(
135+
Effect.provideService(FileSystem.FileSystem, fileSystem),
136+
);
137+
138+
assert.deepStrictEqual(writes, []);
139+
}).pipe(Effect.provide(NodeServices.layer)),
140+
);

‎apps/desktop/src/app/DesktopUserData.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,43 @@ export const resolveUserDataPath = Effect.fn("desktop.userData.resolveUserDataPa
9595
return destinationPath;
9696
},
9797
);
98+
99+
const GPU_CACHE_DIRECTORIES = [
100+
"GPUCache",
101+
"GrShaderCache",
102+
"ShaderCache",
103+
"DawnGraphiteCache",
104+
"DawnWebGPUCache",
105+
"GPUPersistentCache",
106+
];
107+
108+
// Keyed on Electron, not Chromium: Electron patches ship Chromium backports under the same Chrome version.
109+
export const clearStaleGpuCaches = Effect.fn("desktop.userData.clearStaleGpuCaches")(
110+
function* (input: { readonly userDataPath: string; readonly electronVersion: string }) {
111+
const fs = yield* FileSystem.FileSystem;
112+
const path = yield* Path.Path;
113+
const markerPath = path.join(input.userDataPath, "gpu-cache-electron-version");
114+
const cachedVersion = yield* fs.readFileString(markerPath).pipe(
115+
Effect.catchIf(
116+
(error) => error.reason._tag === "NotFound",
117+
() => Effect.undefined,
118+
),
119+
);
120+
if (cachedVersion?.trim() === input.electronVersion) return;
121+
for (const name of GPU_CACHE_DIRECTORIES) {
122+
yield* fs.remove(path.join(input.userDataPath, name), { recursive: true, force: true });
123+
}
124+
yield* fs.makeDirectory(input.userDataPath, { recursive: true });
125+
yield* fs.writeFileString(markerPath, input.electronVersion);
126+
},
127+
(effect, input) =>
128+
effect.pipe(
129+
Effect.catch((error) =>
130+
Effect.logWarning("could not clear stale GPU caches", {
131+
userDataPath: input.userDataPath,
132+
electronVersion: input.electronVersion,
133+
error: error.message,
134+
}),
135+
),
136+
),
137+
);

‎apps/desktop/src/backend/DesktopBackendManager.test.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ describe("DesktopBackendManager", () => {
421421
),
422422
);
423423

424-
it.effect("starts the configured backend and closes the scoped process on stop", () =>
424+
it.effect("stops the scoped process with output still pending", () =>
425425
Effect.scoped(
426426
Effect.gen(function* () {
427427
let startCount = 0;
@@ -458,6 +458,7 @@ describe("DesktopBackendManager", () => {
458458
yield* Scope.addFinalizer(scope, close);
459459

460460
return makeProcess({
461+
stdout: Stream.never,
461462
exitCode: Deferred.await(closed).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
462463
kill: () => close,
463464
});
@@ -721,14 +722,12 @@ describe("DesktopBackendManager", () => {
721722
),
722723
);
723724

724-
it.effect("the quit stop deadline waits for a backend that only exits at the force kill", () =>
725+
it.effect("the quit stop keeps waiting for a backend that outlasts its deadline", () =>
725726
Effect.scoped(
726727
Effect.gen(function* () {
727728
const spawned = yield* Deferred.make<void>();
728729
const teardownStarted = yield* Deferred.make<void>();
729730
const teardownFinished = yield* Deferred.make<void>();
730-
// The real spawner sends SIGTERM, waits the grace, then SIGKILLs. A backend that
731-
// ignores SIGTERM therefore finishes tearing down just after the grace.
732731
const instance = yield* makeTestInstance({
733732
spawnerLayer: Layer.succeed(
734733
ChildProcessSpawner.ChildProcessSpawner,
@@ -739,7 +738,7 @@ describe("DesktopBackendManager", () => {
739738
yield* Scope.addFinalizer(
740739
scope,
741740
Deferred.succeed(teardownStarted, undefined).pipe(
742-
Effect.andThen(Effect.sleep(Duration.seconds(6))),
741+
Effect.andThen(Effect.sleep(Duration.seconds(10))),
743742
Effect.andThen(Deferred.succeed(teardownFinished, undefined)),
744743
Effect.asVoid,
745744
),
@@ -764,11 +763,11 @@ describe("DesktopBackendManager", () => {
764763
.pipe(Effect.andThen(Deferred.succeed(quitDone, undefined)), Effect.forkChild);
765764

766765
yield* Deferred.await(teardownStarted);
767-
// Past the SIGTERM grace but before the force kill lands: quit must still be waiting.
768-
yield* TestClock.adjust(Duration.millis(5_500));
766+
// Past the quit deadline, quit must still wait for the backend to exit.
767+
yield* TestClock.adjust(Duration.seconds(9));
769768
assert.isFalse(yield* Deferred.isDone(quitDone));
770769

771-
yield* TestClock.adjust(Duration.millis(500));
770+
yield* TestClock.adjust(Duration.seconds(1));
772771
yield* Fiber.join(quitFiber);
773772
assert.isTrue(yield* Deferred.isDone(teardownFinished));
774773
}).pipe(Effect.provide(TestClock.layer())),

‎apps/desktop/src/backend/DesktopBackendManager.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export interface DesktopBackendInstance {
240240
readonly id: BackendInstanceId;
241241
readonly label: Effect.Effect<string>;
242242
readonly start: Effect.Effect<void>;
243+
/** Timeout controls the warning; stop still waits for process teardown and exit. */
243244
readonly stop: (options?: { readonly timeout?: Duration.Duration }) => Effect.Effect<void>;
244245
readonly currentConfig: Effect.Effect<Option.Option<DesktopBackendStartConfig>>;
245246
readonly snapshot: Effect.Effect<DesktopBackendSnapshot>;
@@ -307,7 +308,7 @@ const closeRun = (
307308
run: ActiveBackendRun,
308309
parentScope: Scope.Scope,
309310
options?: { readonly timeout?: Duration.Duration },
310-
): Effect.Effect<boolean> => {
311+
) => {
311312
const waitForFiber = Option.match(run.fiber, {
312313
onNone: () => Effect.void,
313314
onSome: (fiber) => Fiber.await(fiber).pipe(Effect.asVoid),
@@ -319,12 +320,24 @@ const closeRun = (
319320
const timeout = options?.timeout;
320321

321322
if (!timeout) {
322-
return close.pipe(Effect.as(true));
323+
return close;
323324
}
324325

325-
return Effect.forkIn(close, parentScope).pipe(
326+
return Effect.forkIn(close, parentScope, { startImmediately: true }).pipe(
326327
Effect.flatMap((closeFiber) =>
327-
Fiber.await(closeFiber).pipe(Effect.timeoutOption(timeout), Effect.map(Option.isSome)),
328+
Fiber.await(closeFiber).pipe(
329+
Effect.timeoutOption(timeout),
330+
Effect.tap((completed) =>
331+
Option.isNone(completed)
332+
? logBackendProcessWarning("backend shutdown exceeded timeout; waiting for exit", {
333+
timeoutMs: Duration.toMillis(timeout),
334+
})
335+
: Effect.void,
336+
),
337+
// Parent-scope interruption still finishes release; do not cancel sibling stops.
338+
Effect.andThen(Fiber.await(closeFiber)),
339+
Effect.asVoid,
340+
),
328341
),
329342
);
330343
};
@@ -1099,10 +1112,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
10991112
onNone: () => Effect.void,
11001113
// The enclosing operation owns the span for this inline handler.
11011114
onSome: Effect.fnUntraced(function* (run) {
1102-
const closed = yield* closeRun(run, parentScope, options);
1103-
if (!closed) {
1104-
return;
1105-
}
1115+
yield* closeRun(run, parentScope, options);
11061116
const cleanup = yield* mutex.withPermits(1)(
11071117
SubscriptionRef.modify(
11081118
state,

0 commit comments

Comments
 (0)