Skip to content

Commit 77f34b2

Browse files
committed
refactor(server): carry settings file trust with the cached settings
The cache entry now records whether settings.json parsed, so a save reads that fact from the reload it just did instead of a side flag. A shared reloadSettings replaces three invalidate-then-get pairs, and the parse failure test asserts the read-file error it expects.
1 parent aa6cc09 commit 77f34b2

2 files changed

Lines changed: 23 additions & 27 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,13 @@ it.layer(NodeServices.layer)("server settings", (it) => {
518518
const raw = `{ "responseStreamingMode": "turn", `;
519519
yield* fs.writeFileString(config.settingsPath, raw);
520520

521-
const saved = yield* Effect.exit(
521+
const error = yield* Effect.flip(
522522
serverSettings.updateSettings({
523523
observability: { otlpTracesUrl: "http://localhost:4318/v1/traces" },
524524
}),
525525
);
526526

527-
assert.isTrue(saved._tag === "Failure");
527+
assert.equal(error.operation, "read-file");
528528
assert.equal(yield* fs.readFileString(config.settingsPath), raw);
529529
}).pipe(Effect.provide(layerServerSettings())),
530530
);

‎apps/server/src/serverSettings.ts‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ const make = Effect.gen(function* () {
629629
const writeSemaphore = yield* Semaphore.make(1);
630630
const cacheKey = "settings" as const;
631631
const changesPubSub = yield* PubSub.unbounded<ServerSettings>();
632-
const settingsFileReadable = yield* Ref.make(true);
633632
const startedRef = yield* Ref.make(false);
634633
const startedDeferred = yield* Deferred.make<void, ServerSettingsError>();
635634
const watcherScope = yield* Scope.make("sequential");
@@ -853,20 +852,25 @@ const make = Effect.gen(function* () {
853852
yield* secretStore.remove(move.from).pipe(Effect.ignore);
854853
}
855854
}
856-
yield* Ref.set(settingsFileReadable, settingsFileTrusted);
857-
return migrated;
855+
return { settings: migrated, fileTrusted: settingsFileTrusted };
858856
});
859857

860858
// A failed read is not kept: the next read retries instead of replaying the failure.
861-
const settingsCache = yield* Cache.makeWith<typeof cacheKey, ServerSettings, ServerSettingsError>(
862-
() => loadSettingsFromDisk,
863-
{
864-
capacity: 1,
865-
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
866-
},
867-
);
859+
const settingsCache = yield* Cache.makeWith<
860+
typeof cacheKey,
861+
{ readonly settings: ServerSettings; readonly fileTrusted: boolean },
862+
ServerSettingsError
863+
>(() => loadSettingsFromDisk, {
864+
capacity: 1,
865+
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
866+
});
868867

869-
const getSettingsFromCache = Cache.get(settingsCache, cacheKey);
868+
const getSettingsFromCache = Cache.get(settingsCache, cacheKey).pipe(
869+
Effect.map((loaded) => loaded.settings),
870+
);
871+
const reloadSettings = Cache.invalidate(settingsCache, cacheKey).pipe(
872+
Effect.andThen(Cache.get(settingsCache, cacheKey)),
873+
);
870874

871875
const materializeSettingsSecrets = (
872876
settings: ServerSettings,
@@ -1070,15 +1074,9 @@ const make = Effect.gen(function* () {
10701074
): Effect.Effect<ServerSettings, ServerSettingsError> =>
10711075
writeSemaphore.withPermits(1)(
10721076
Effect.gen(function* () {
1073-
// Patch the file as it is now. The watcher reloads external edits only after 100 ms of
1074-
// quiet, so the cached copy can predate an edit and writing it would revert that edit.
1075-
yield* Cache.invalidate(settingsCache, cacheKey);
1076-
const current = yield* getSettingsFromCache;
1077-
// Writing now would replace the file the user has to repair with defaults plus this patch.
1078-
if (!(yield* Ref.get(settingsFileReadable))) {
1079-
return yield* Effect.fail(
1080-
new ServerSettingsError({ settingsPath, operation: "read-file" }),
1081-
);
1077+
const { settings: current, fileTrusted } = yield* reloadSettings;
1078+
if (!fileTrusted) {
1079+
return yield* new ServerSettingsError({ settingsPath, operation: "read-file" });
10821080
}
10831081
const updated = yield* update(current);
10841082
const persisted = yield* persistSettingsSecrets(current, updated);
@@ -1099,7 +1097,7 @@ const make = Effect.gen(function* () {
10991097
return materializedExit.value;
11001098
}),
11011099
);
1102-
yield* Cache.set(settingsCache, cacheKey, next);
1100+
yield* Cache.set(settingsCache, cacheKey, { settings: next, fileTrusted: true });
11031101
yield* emitChange(next);
11041102
return resolveTextGenerationProvider(materialized);
11051103
}),
@@ -1116,8 +1114,7 @@ const make = Effect.gen(function* () {
11161114

11171115
const revalidateAndEmit = writeSemaphore.withPermits(1)(
11181116
Effect.gen(function* () {
1119-
yield* Cache.invalidate(settingsCache, cacheKey);
1120-
const settings = yield* getSettingsFromCache;
1117+
const { settings } = yield* reloadSettings;
11211118
yield* emitChange(settings);
11221119
}),
11231120
);
@@ -1207,8 +1204,7 @@ const make = Effect.gen(function* () {
12071204

12081205
const startup = Effect.gen(function* () {
12091206
yield* startWatcher;
1210-
yield* Cache.invalidate(settingsCache, cacheKey);
1211-
yield* getSettingsFromCache;
1207+
yield* reloadSettings;
12121208
});
12131209

12141210
const startupExit = yield* Effect.exit(startup);

0 commit comments

Comments
 (0)