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, 12:15 UTC
fix(desktop): saving keeps a connection catalog this build cannot read (#34944)
Julius Marminge committed
Nov 14, 2026 at 20:44 UTC
9dbb8028457c0fc068fa333a984a0478c07e6fb4
2 files changed
+120
-15
apps/desktop/src/app/DesktopConnectionCatalogStore.test.ts
+62
@@ -300,4 +300,66 @@ describe("DesktopConnectionCatalogStore", () => {
300
assert.deepStrictEqual(yield* store.get, Option.some('{"schemaVersion":1,"targets":[]}'));
301
}).pipe(Effect.provide(NodeServices.layer)),
302
);
303
+
304
+ const emptyCatalog =
305
+ '{"schemaVersion":1,"targets":[],"profiles":[],"credentials":[],"remoteDpopTokens":[],"disabledEnvironmentIds":[]}';
306
+ const savedFilesHolding = Effect.fn(function* (stateDir: string, contents: string) {
307
+ const fileSystem = yield* FileSystem.FileSystem;
308
+ const path = yield* Path.Path;
309
+ const kept: string[] = [];
310
+ for (const name of yield* fileSystem.readDirectory(stateDir)) {
311
+ const file = path.join(stateDir, name);
312
+ if ((yield* fileSystem.stat(file)).type !== "File") continue;
313
+ if ((yield* fileSystem.readFileString(file)) === contents) kept.push(name);
314
+ }
315
+ return kept;
316
+ });
317
+
318
+ it.effect("keeps a catalog this build cannot decode when the next save replaces it", () =>
319
+ withStore(
320
+ Effect.gen(function* () {
321
+ const path = yield* Path.Path;
322
+ const environment = yield* DesktopEnvironment.DesktopEnvironment;
323
+ const fileSystem = yield* FileSystem.FileSystem;
324
+ const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore;
325
+ const catalogPath = path.join(environment.stateDir, "connection-catalog.json");
326
+ assert.isTrue(
327
+ yield* store.set(
328
+ '{"schemaVersion":1,"targets":[{"_tag":"FutureConnectionTarget","environmentId":"remote"}],"profiles":[],"credentials":[],"remoteDpopTokens":[],"disabledEnvironmentIds":[]}',
329
+ ),
330
+ );
331
+ const original = yield* fileSystem.readFileString(catalogPath);
332
+
333
+ yield* store.get;
334
+ assert.isTrue(yield* store.set(emptyCatalog));
335
+
336
+ assert.isNotEmpty(yield* savedFilesHolding(environment.stateDir, original));
337
+ }),
338
+ ),
339
+ );
340
+
341
+ it.effect("keeps a catalog that no longer decrypts when the next save replaces it", () =>
342
+ Effect.gen(function* () {
343
+ const path = yield* Path.Path;
344
+ const fileSystem = yield* FileSystem.FileSystem;
345
+ const baseDir = yield* fileSystem.makeTempDirectoryScoped({
346
+ prefix: "t3-desktop-connection-catalog-test-",
347
+ });
348
+ const failDecrypt = yield* Ref.make(false);
349
+ const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore.pipe(
350
+ Effect.provide(layerFor(baseDir, true, failDecrypt)),
351
+ );
352
+ const stateDir = path.join(baseDir, "userdata");
353
+ assert.isTrue(yield* store.set('{"schemaVersion":1,"targets":[]}'));
354
+ const original = yield* fileSystem.readFileString(
355
+ path.join(stateDir, "connection-catalog.json"),
356
+ );
357
+
358
+ yield* Ref.set(failDecrypt, true);
359
+ yield* store.get.pipe(Effect.flip);
360
+ assert.isTrue(yield* store.set(emptyCatalog));
361
+
362
+ assert.isNotEmpty(yield* savedFilesHolding(stateDir, original));
363
+ }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
364
+ );
365
});
apps/desktop/src/app/DesktopConnectionCatalogStore.ts
+58
-15
@@ -15,6 +15,7 @@ import { fromLenientJson } from "@t3tools/shared/schemaJson";
15
import * as Context from "effect/Context";
16
import * as SecureRandom from "effect/crypto/SecureRandom";
17
import * as Effect from "effect/Effect";
18
+import * as Clock from "effect/Clock";
19
import * as Base64 from "effect/encoding/Base64";
20
import * as FileSystem from "effect/FileSystem";
21
import * as Layer from "effect/Layer";
@@ -45,6 +46,9 @@ const RuntimeConnectionCatalogDocumentJson = Schema.fromJsonString(
46
const encodeRuntimeConnectionCatalogDocumentJson = Schema.encodeEffect(
47
RuntimeConnectionCatalogDocumentJson,
48
);
49
+const decodeRuntimeConnectionCatalogDocumentJson = Schema.decodeEffect(
50
+ RuntimeConnectionCatalogDocumentJson,
51
+);
52
53
const DesktopConnectionCatalogStoreWriteOperation = Schema.Literals([
54
"create-temporary-file-name",
@@ -52,6 +56,7 @@ const DesktopConnectionCatalogStoreWriteOperation = Schema.Literals([
56
"create-directory",
57
"write-temporary-file",
58
"replace-catalog-file",
59
+ "keep-unreadable-catalog",
60
]);
61
62
const DesktopConnectionCatalogStoreMigrationOperation = Schema.Literals([
@@ -395,6 +400,57 @@ export const make = Effect.gen(function* () {
400
),
401
);
402
403
+ const decryptDocument = (document: EncryptedConnectionCatalogDocument) =>
404
+ decodeSecretBytes(catalogPath, document.encryptedCatalog).pipe(
405
+ Effect.flatMap((encryptedCatalog) =>
406
+ safeStorage.decryptString(encryptedCatalog).pipe(
407
+ Effect.mapError(
408
+ (cause) =>
409
+ new DesktopConnectionCatalogStoreProtectionError({
410
+ operation: "decrypt-catalog",
411
+ catalogPath,
412
+ cause,
413
+ }),
414
+ ),
415
+ ),
416
+ ),
417
+ );
418
+
419
+ // A catalog this build cannot decrypt or decode may still hold another build's saved
420
+ // environments and credentials, so a save copies it aside instead of destroying it.
421
+ const keepUnreadableCatalog = Effect.gen(function* () {
422
+ const readable = yield* readDocument(fileSystem, catalogPath).pipe(
423
+ Effect.flatMap(
424
+ Option.match({
425
+ onNone: () => Effect.succeed(true),
426
+ onSome: (document) =>
427
+ decryptDocument(document).pipe(
428
+ Effect.flatMap(decodeRuntimeConnectionCatalogDocumentJson),
429
+ Effect.as(true),
430
+ ),
431
+ }),
432
+ ),
433
+ Effect.orElseSucceed(() => false),
434
+ );
435
+ if (readable) {
436
+ return;
437
+ }
438
+ const keptPath = `${catalogPath}.unreadable-${yield* Clock.currentTimeMillis}`;
439
+ yield* fileSystem.copyFile(catalogPath, keptPath).pipe(
440
+ Effect.mapError(
441
+ (cause) =>
442
+ new DesktopConnectionCatalogStoreWriteError({
443
+ operation: "keep-unreadable-catalog",
444
+ path: keptPath,
445
+ cause,
446
+ }),
447
+ ),
448
+ );
449
+ yield* Effect.logWarning("Kept an unreadable connection catalog before replacing it.", {
450
+ keptPath,
451
+ });
452
+ });
453
+
454
const writeCatalog = Effect.fn("desktop.connectionCatalogStore.writeCatalog")(function* (
455
catalog: string,
456
) {
@@ -470,26 +526,13 @@ export const make = Effect.gen(function* () {
526
if (!(yield* encryptionAvailable)) {
527
return Option.none<string>();
528
}
473
- const decrypted = yield* decodeSecretBytes(catalogPath, document.value.encryptedCatalog).pipe(
474
- Effect.flatMap((encryptedCatalog) =>
475
- safeStorage.decryptString(encryptedCatalog).pipe(
476
- Effect.mapError(
477
- (cause) =>
478
- new DesktopConnectionCatalogStoreProtectionError({
479
- operation: "decrypt-catalog",
480
- catalogPath,
481
- cause,
482
- }),
483
- ),
484
- ),
485
- ),
486
- );
487
- return Option.some(decrypted);
529
+ return Option.some(yield* decryptDocument(document.value));
530
}).pipe(Effect.withSpan("desktop.connectionCatalogStore.get")),
531
set: Effect.fn("desktop.connectionCatalogStore.set")(function* (catalog) {
532
if (!(yield* encryptionAvailable)) {
533
return false;
534
}
535
+ yield* keepUnreadableCatalog;
536
yield* writeCatalog(catalog);
537
return true;
538
}),