Skip to content

Commit c6d3384

Browse files
fix(server): retry settings and keybindings reads after a failure
The settings and keybindings caches were built with Cache.make, whose default time-to-live is infinite for every exit, failures included. One failed read (a transient file or database error) was replayed to every later caller until a file-watcher event or a write invalidated the entry, and a failure during startup stuck until the file was touched. Both caches now use Cache.makeWith with a time-to-live of zero for failed lookups, so a failed read is dropped and the next read goes back to disk. Successful reads keep the existing behavior: cached until a write or a watcher event replaces them. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 0b73370 commit c6d3384

4 files changed

Lines changed: 56 additions & 7 deletions

File tree

‎apps/server/src/keybindings.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,28 @@ it.layer(NodeServices.layer)("keybindings", (it) => {
604604
}).pipe(Effect.provide(makeKeybindingsLayer())),
605605
);
606606

607+
it.effect("retries a failed config read instead of keeping the failure", () =>
608+
Effect.gen(function* () {
609+
const fileSystem = yield* FileSystem.FileSystem;
610+
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;
611+
// A directory where the file should be makes the read itself fail.
612+
yield* fileSystem.makeDirectory(keybindingsConfigPath, { recursive: true });
613+
614+
const keybindings = yield* Keybindings.Keybindings;
615+
const failed = yield* toDetailResult(keybindings.loadConfigState);
616+
assertFailure(failed, "failed to read keybindings config");
617+
618+
yield* fileSystem.remove(keybindingsConfigPath, { recursive: true });
619+
yield* writeKeybindingsConfig(keybindingsConfigPath, [
620+
{ key: "mod+j", command: "terminal.toggle" },
621+
]);
622+
623+
const configState = yield* keybindings.loadConfigState;
624+
assert.deepEqual(configState.issues, []);
625+
assert.isTrue(configState.keybindings.some((entry) => entry.command === "terminal.toggle"));
626+
}).pipe(Effect.provide(makeKeybindingsLayer())),
627+
);
628+
607629
it.effect("updates cached resolved config after upsert", () =>
608630
Effect.gen(function* () {
609631
const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;

‎apps/server/src/keybindings.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,14 @@ const make = Effect.gen(function* () {
480480
})),
481481
);
482482

483-
const resolvedConfigCache = yield* Cache.make<
483+
// A failed read is not kept: the next read retries instead of replaying the failure.
484+
const resolvedConfigCache = yield* Cache.makeWith<
484485
typeof resolvedConfigCacheKey,
485486
KeybindingsConfigState,
486487
KeybindingsConfigError
487-
>({
488+
>(() => loadConfigStateFromDisk, {
488489
capacity: 1,
489-
lookup: () => loadConfigStateFromDisk,
490+
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
490491
});
491492

492493
const loadConfigStateFromCacheOrDisk = Cache.get(resolvedConfigCache, resolvedConfigCacheKey);

‎apps/server/src/serverSettings.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,28 @@ it.layer(NodeServices.layer)("server settings", (it) => {
303303
}).pipe(Effect.provide(makeServerSettingsLayer())),
304304
);
305305

306+
it.effect("retries a failed settings read instead of keeping the failure", () =>
307+
Effect.gen(function* () {
308+
const serverConfig = yield* ServerConfig.ServerConfig;
309+
const fileSystem = yield* FileSystem.FileSystem;
310+
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
311+
// A directory where the file should be makes the read itself fail.
312+
yield* fileSystem.makeDirectory(serverConfig.settingsPath);
313+
314+
const error = yield* Effect.flip(serverSettings.getSettings);
315+
assert.deepInclude(error, { _tag: "ServerSettingsError", operation: "read-file" });
316+
317+
yield* fileSystem.remove(serverConfig.settingsPath, { recursive: true });
318+
yield* fileSystem.writeFileString(
319+
serverConfig.settingsPath,
320+
`{ "responseStreamingMode": "turn" }`,
321+
);
322+
323+
const settings = yield* serverSettings.getSettings;
324+
assert.equal(settings.responseStreamingMode, "turn");
325+
}).pipe(Effect.provide(makeServerSettingsLayer())),
326+
);
327+
306328
it.effect("decodes nested settings patches", () =>
307329
Effect.gen(function* () {
308330
assert.deepEqual(

‎apps/server/src/serverSettings.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,10 +802,14 @@ const make = Effect.gen(function* () {
802802
return migrated;
803803
});
804804

805-
const settingsCache = yield* Cache.make<typeof cacheKey, ServerSettings, ServerSettingsError>({
806-
capacity: 1,
807-
lookup: () => loadSettingsFromDisk,
808-
});
805+
// A failed read is not kept: the next read retries instead of replaying the failure.
806+
const settingsCache = yield* Cache.makeWith<typeof cacheKey, ServerSettings, ServerSettingsError>(
807+
() => loadSettingsFromDisk,
808+
{
809+
capacity: 1,
810+
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
811+
},
812+
);
809813

810814
const getSettingsFromCache = Cache.get(settingsCache, cacheKey);
811815

0 commit comments

Comments
 (0)