@devries-cameron20 / t3code Mirror · read-only / commits / 025942d76a
Fork 0

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 21, 17:15 UTC

fix(desktop): an unreadable settings file is no longer overwritten by a bounds save (#35890)

Julius Marminge committed Nov 12, 2026 at 19:34 UTC 025942d76a53c739fa58e4e90a552c55606551f3
2 files changed +83 -13
apps/desktop/src/settings/DesktopAppSettings.test.ts
+44
@@ -4,6 +4,7 @@ import { assert, describe, it } from "@effect/vitest";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Layer from "effect/Layer";
7 +import * as PlatformError from "effect/PlatformError";
8 import * as Schema from "effect/Schema";
9 import * as Tracer from "effect/Tracer";
10
@@ -160,6 +161,49 @@ describe("DesktopSettings", () => {
161 ),
162 );
163
164 + it.effect("keeps saved preferences when a read fails before a bounds save", () =>
165 + withSettings(
166 + Effect.gen(function* () {
167 + const environment = yield* DesktopEnvironment.DesktopEnvironment;
168 + const fileSystem = yield* FileSystem.FileSystem;
169 + yield* writeSettingsPatch({ serverExposureMode: "network-accessible" });
170 + const cause = PlatformError.systemError({
171 + _tag: "PermissionDenied",
172 + module: "FileSystem",
173 + method: "readFileString",
174 + pathOrDescriptor: environment.desktopSettingsPath,
175 + });
176 + let failRead = true;
177 + const settings = yield* DesktopAppSettings.make.pipe(
178 + Effect.provideService(
179 + FileSystem.FileSystem,
180 + FileSystem.FileSystem.of({
181 + ...fileSystem,
182 + readFileString: (path) =>
183 + Effect.suspend(() => {
184 + if (failRead && path === environment.desktopSettingsPath) {
185 + failRead = false;
186 + return Effect.fail(cause);
187 + }
188 + return fileSystem.readFileString(path);
189 + }),
190 + }),
191 + ),
192 + );
193 +
194 + yield* Effect.exit(settings.load);
195 + yield* Effect.exit(
196 + settings.setMainWindowBounds({ x: 10, y: 20, width: 900, height: 600 }, false),
197 + );
198 +
199 + const persisted = yield* decodeDesktopSettingsPatch(
200 + yield* fileSystem.readFileString(environment.desktopSettingsPath),
201 + );
202 + assert.equal(persisted.serverExposureMode, "network-accessible");
203 + }),
204 + ),
205 + );
206 +
207 it.effect("loads lenient persisted desktop settings JSON", () =>
208 withSettings(
209 Effect.gen(function* () {
apps/desktop/src/settings/DesktopAppSettings.ts
+39 -13
@@ -13,6 +13,8 @@ import * as FileSystem from "effect/FileSystem";
13 import * as Layer from "effect/Layer";
14 import * as Option from "effect/Option";
15 import * as Path from "effect/Path";
16 +import * as PlatformError from "effect/PlatformError";
17 +import * as Ref from "effect/Ref";
18 import * as Schema from "effect/Schema";
19 import * as SynchronizedRef from "effect/SynchronizedRef";
20
@@ -379,15 +381,20 @@ function applyWslWindowsFallback(settings: DesktopSettings): DesktopSettings {
381 return setWslOnly(setWslBackendEnabled(settings, false), false);
382 }
383
384 +// Fails only when the file may exist but could not be read; a missing file means defaults.
385 function readSettings(
386 fileSystem: FileSystem.FileSystem,
387 settingsPath: string,
388 appVersion: string,
386 -): Effect.Effect<DesktopSettings> {
389 +): Effect.Effect<DesktopSettings, PlatformError.PlatformError> {
390 const defaultSettings = resolveDefaultDesktopSettings(appVersion);
391
392 return fileSystem.readFileString(settingsPath).pipe(
390 - Effect.option,
393 + Effect.asSome,
394 + Effect.catchIf(
395 + (cause) => cause.reason._tag === "NotFound",
396 + () => Effect.succeed(Option.none<string>()),
397 + ),
398 Effect.flatMap(
399 Option.match({
400 onNone: () => Effect.succeed(defaultSettings),
@@ -476,6 +483,8 @@ export const make = Effect.gen(function* () {
483 const path = yield* Path.Path;
484 const random = yield* SecureRandom.SecureRandom;
485 const settingsRef = yield* SynchronizedRef.make(environment.defaultDesktopSettings);
486 + // Writing over a settings file we could not read would erase the preferences it holds.
487 + const persistenceEnabled = yield* Ref.make(true);
488
489 const updateInMemory = (update: (settings: DesktopSettings) => DesktopSettings) =>
490 SynchronizedRef.modify(settingsRef, (settings) => {
@@ -492,17 +501,23 @@ export const make = Effect.gen(function* () {
501 return Effect.succeed([settingsChange(settings, false), settings] as const);
502 }
503
495 - return random.uuidV4().pipe(
496 - Effect.map((uuid) => uuid.replace(/-/g, "")),
497 - Effect.flatMap((suffix) =>
498 - writeSettings({
499 - fileSystem,
500 - path,
501 - settingsPath: environment.desktopSettingsPath,
502 - settings: nextSettings,
503 - defaultSettings: environment.defaultDesktopSettings,
504 - suffix,
505 - }),
504 + return Ref.get(persistenceEnabled).pipe(
505 + Effect.flatMap((enabled) =>
506 + enabled
507 + ? random.uuidV4().pipe(
508 + Effect.map((uuid) => uuid.replace(/-/g, "")),
509 + Effect.flatMap((suffix) =>
510 + writeSettings({
511 + fileSystem,
512 + path,
513 + settingsPath: environment.desktopSettingsPath,
514 + settings: nextSettings,
515 + defaultSettings: environment.defaultDesktopSettings,
516 + suffix,
517 + }),
518 + ),
519 + )
520 + : Effect.void,
521 ),
522 Effect.as([settingsChange(nextSettings, true), nextSettings] as const),
523 );
@@ -515,6 +530,17 @@ export const make = Effect.gen(function* () {
530 fileSystem,
531 environment.desktopSettingsPath,
532 environment.appVersion,
533 + ).pipe(
534 + Effect.catch((cause) =>
535 + Effect.logWarning(
536 + "Could not read desktop settings; using defaults without saving changes.",
537 + cause,
538 + ).pipe(
539 + Effect.annotateLogs({ settingsPath: environment.desktopSettingsPath }),
540 + Effect.andThen(Ref.set(persistenceEnabled, false)),
541 + Effect.as(resolveDefaultDesktopSettings(environment.appVersion)),
542 + ),
543 + ),
544 );
545 return yield* SynchronizedRef.setAndGet(settingsRef, settings);
546 }).pipe(Effect.withSpan("desktop.settings.load")),