From 250e2e9b585728660ee0f1a01f39e3918541d897 Mon Sep 17 00:00:00 2001 From: F4llen Date: Mon, 12 Oct 2026 03:50:09 -0400 Subject: [PATCH 1/5] test(server): cover saves that race an external settings.json edit --- apps/server/src/serverSettings.test.ts | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index 1cbd6c7bf813a..ff94445931206 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -489,6 +489,46 @@ it.layer(NodeServices.layer)("server settings", (it) => { }).pipe(Effect.provide(layerServerSettings())), ); + it.effect("keeps an external settings edit that the watcher has not reloaded yet", () => + Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const serverSettings = yield* ServerSettingsModule.ServerSettingsService; + yield* serverSettings.getSettings; + + yield* writeFileStringAtomically({ + filePath: config.settingsPath, + contents: `{ "responseStreamingMode": "turn" }`, + }); + yield* serverSettings.updateSettings({ + observability: { otlpTracesUrl: "http://localhost:4318/v1/traces" }, + }); + + const saved = yield* decodeServerSettingsJson(yield* fs.readFileString(config.settingsPath)); + assert.equal(saved.responseStreamingMode, "turn"); + assert.equal(saved.observability.otlpTracesUrl, "http://localhost:4318/v1/traces"); + }).pipe(Effect.provide(layerServerSettings())), + ); + + it.effect("refuses to save over a settings file it cannot parse", () => + Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const fs = yield* FileSystem.FileSystem; + const serverSettings = yield* ServerSettingsModule.ServerSettingsService; + const raw = `{ "responseStreamingMode": "turn", `; + yield* fs.writeFileString(config.settingsPath, raw); + + const saved = yield* Effect.exit( + serverSettings.updateSettings({ + observability: { otlpTracesUrl: "http://localhost:4318/v1/traces" }, + }), + ); + + assert.isTrue(saved._tag === "Failure"); + assert.equal(yield* fs.readFileString(config.settingsPath), raw); + }).pipe(Effect.provide(layerServerSettings())), + ); + it.effect("creates provider instances atomically without overwriting a concurrent add", () => Effect.gen(function* () { const serverSettings = yield* ServerSettingsModule.ServerSettingsService; From aa6cc09282f7ff2cc7043c6089e02bc58e292885 Mon Sep 17 00:00:00 2001 From: F4llen Date: Mon, 12 Oct 2026 03:50:12 -0400 Subject: [PATCH 2/5] fix(server): settings saves no longer revert external edits to settings.json A save patched the cached settings, which the watcher refreshes only after 100 ms without file events. Saves arriving faster than that kept the cache stale, so each one wrote the old file back over an external edit. Reload the file under the write lock before applying the patch, and refuse the save when the file does not parse, so the user's file is never replaced by defaults plus the patch. --- apps/server/src/serverSettings.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index 44e645858c491..df25fd846b0b0 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -629,6 +629,7 @@ const make = Effect.gen(function* () { const writeSemaphore = yield* Semaphore.make(1); const cacheKey = "settings" as const; const changesPubSub = yield* PubSub.unbounded(); + const settingsFileReadable = yield* Ref.make(true); const startedRef = yield* Ref.make(false); const startedDeferred = yield* Deferred.make(); const watcherScope = yield* Scope.make("sequential"); @@ -852,6 +853,7 @@ const make = Effect.gen(function* () { yield* secretStore.remove(move.from).pipe(Effect.ignore); } } + yield* Ref.set(settingsFileReadable, settingsFileTrusted); return migrated; }); @@ -1068,7 +1070,16 @@ const make = Effect.gen(function* () { ): Effect.Effect => writeSemaphore.withPermits(1)( Effect.gen(function* () { + // Patch the file as it is now. The watcher reloads external edits only after 100 ms of + // quiet, so the cached copy can predate an edit and writing it would revert that edit. + yield* Cache.invalidate(settingsCache, cacheKey); const current = yield* getSettingsFromCache; + // Writing now would replace the file the user has to repair with defaults plus this patch. + if (!(yield* Ref.get(settingsFileReadable))) { + return yield* Effect.fail( + new ServerSettingsError({ settingsPath, operation: "read-file" }), + ); + } const updated = yield* update(current); const persisted = yield* persistSettingsSecrets(current, updated); const next = yield* normalizeServerSettings(persisted.settings); From 77f34b23d03fc34a16c199c0ffe6b96c38f83fa6 Mon Sep 17 00:00:00 2001 From: F4llen Date: Mon, 12 Oct 2026 04:03:24 -0400 Subject: [PATCH 3/5] 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. --- apps/server/src/serverSettings.test.ts | 4 +-- apps/server/src/serverSettings.ts | 46 ++++++++++++-------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index ff94445931206..7857049fca2c8 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -518,13 +518,13 @@ it.layer(NodeServices.layer)("server settings", (it) => { const raw = `{ "responseStreamingMode": "turn", `; yield* fs.writeFileString(config.settingsPath, raw); - const saved = yield* Effect.exit( + const error = yield* Effect.flip( serverSettings.updateSettings({ observability: { otlpTracesUrl: "http://localhost:4318/v1/traces" }, }), ); - assert.isTrue(saved._tag === "Failure"); + assert.equal(error.operation, "read-file"); assert.equal(yield* fs.readFileString(config.settingsPath), raw); }).pipe(Effect.provide(layerServerSettings())), ); diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index df25fd846b0b0..3534ade96c385 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -629,7 +629,6 @@ const make = Effect.gen(function* () { const writeSemaphore = yield* Semaphore.make(1); const cacheKey = "settings" as const; const changesPubSub = yield* PubSub.unbounded(); - const settingsFileReadable = yield* Ref.make(true); const startedRef = yield* Ref.make(false); const startedDeferred = yield* Deferred.make(); const watcherScope = yield* Scope.make("sequential"); @@ -853,20 +852,25 @@ const make = Effect.gen(function* () { yield* secretStore.remove(move.from).pipe(Effect.ignore); } } - yield* Ref.set(settingsFileReadable, settingsFileTrusted); - return migrated; + return { settings: migrated, fileTrusted: settingsFileTrusted }; }); // A failed read is not kept: the next read retries instead of replaying the failure. - const settingsCache = yield* Cache.makeWith( - () => loadSettingsFromDisk, - { - capacity: 1, - timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero), - }, - ); + const settingsCache = yield* Cache.makeWith< + typeof cacheKey, + { readonly settings: ServerSettings; readonly fileTrusted: boolean }, + ServerSettingsError + >(() => loadSettingsFromDisk, { + capacity: 1, + timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero), + }); - const getSettingsFromCache = Cache.get(settingsCache, cacheKey); + const getSettingsFromCache = Cache.get(settingsCache, cacheKey).pipe( + Effect.map((loaded) => loaded.settings), + ); + const reloadSettings = Cache.invalidate(settingsCache, cacheKey).pipe( + Effect.andThen(Cache.get(settingsCache, cacheKey)), + ); const materializeSettingsSecrets = ( settings: ServerSettings, @@ -1070,15 +1074,9 @@ const make = Effect.gen(function* () { ): Effect.Effect => writeSemaphore.withPermits(1)( Effect.gen(function* () { - // Patch the file as it is now. The watcher reloads external edits only after 100 ms of - // quiet, so the cached copy can predate an edit and writing it would revert that edit. - yield* Cache.invalidate(settingsCache, cacheKey); - const current = yield* getSettingsFromCache; - // Writing now would replace the file the user has to repair with defaults plus this patch. - if (!(yield* Ref.get(settingsFileReadable))) { - return yield* Effect.fail( - new ServerSettingsError({ settingsPath, operation: "read-file" }), - ); + const { settings: current, fileTrusted } = yield* reloadSettings; + if (!fileTrusted) { + return yield* new ServerSettingsError({ settingsPath, operation: "read-file" }); } const updated = yield* update(current); const persisted = yield* persistSettingsSecrets(current, updated); @@ -1099,7 +1097,7 @@ const make = Effect.gen(function* () { return materializedExit.value; }), ); - yield* Cache.set(settingsCache, cacheKey, next); + yield* Cache.set(settingsCache, cacheKey, { settings: next, fileTrusted: true }); yield* emitChange(next); return resolveTextGenerationProvider(materialized); }), @@ -1116,8 +1114,7 @@ const make = Effect.gen(function* () { const revalidateAndEmit = writeSemaphore.withPermits(1)( Effect.gen(function* () { - yield* Cache.invalidate(settingsCache, cacheKey); - const settings = yield* getSettingsFromCache; + const { settings } = yield* reloadSettings; yield* emitChange(settings); }), ); @@ -1207,8 +1204,7 @@ const make = Effect.gen(function* () { const startup = Effect.gen(function* () { yield* startWatcher; - yield* Cache.invalidate(settingsCache, cacheKey); - yield* getSettingsFromCache; + yield* reloadSettings; }); const startupExit = yield* Effect.exit(startup); From 9bbbd9f2945f4de2ef7087dc28ffa34f5e986f6e Mon Sep 17 00:00:00 2001 From: F4llen Date: Mon, 12 Oct 2026 04:36:57 -0400 Subject: [PATCH 4/5] fix(server): keep the parse failure as the cause of a refused settings save --- apps/server/src/serverSettings.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index 3534ade96c385..434a822efe80b 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -723,6 +723,7 @@ const make = Effect.gen(function* () { // A file that failed to decode must stay on disk for the user to repair; // the fold below only writes when it started from the file's real contents. let settingsFileTrusted = true; + let parseFailure = Option.none(); if (yield* readConfigExists) { const raw = yield* readRawConfig; @@ -741,6 +742,7 @@ const make = Effect.gen(function* () { // The file may hold an opt-out we can no longer read; never resume threads on a guess. settings = { ...DEFAULT_SERVER_SETTINGS, continueThreadsAfterServerUpdate: false }; if (failure._tag === "Failure") { + parseFailure = Option.some(Cause.squash(failure.cause)); yield* Effect.logWarning("failed to parse settings.json, using defaults", { path: settingsPath, issues: Cause.pretty(failure.cause), @@ -852,13 +854,13 @@ const make = Effect.gen(function* () { yield* secretStore.remove(move.from).pipe(Effect.ignore); } } - return { settings: migrated, fileTrusted: settingsFileTrusted }; + return { settings: migrated, parseFailure }; }); // A failed read is not kept: the next read retries instead of replaying the failure. const settingsCache = yield* Cache.makeWith< typeof cacheKey, - { readonly settings: ServerSettings; readonly fileTrusted: boolean }, + { readonly settings: ServerSettings; readonly parseFailure: Option.Option }, ServerSettingsError >(() => loadSettingsFromDisk, { capacity: 1, @@ -1074,9 +1076,13 @@ const make = Effect.gen(function* () { ): Effect.Effect => writeSemaphore.withPermits(1)( Effect.gen(function* () { - const { settings: current, fileTrusted } = yield* reloadSettings; - if (!fileTrusted) { - return yield* new ServerSettingsError({ settingsPath, operation: "read-file" }); + const { settings: current, parseFailure } = yield* reloadSettings; + if (Option.isSome(parseFailure)) { + return yield* new ServerSettingsError({ + settingsPath, + operation: "read-file", + cause: parseFailure.value, + }); } const updated = yield* update(current); const persisted = yield* persistSettingsSecrets(current, updated); @@ -1097,7 +1103,7 @@ const make = Effect.gen(function* () { return materializedExit.value; }), ); - yield* Cache.set(settingsCache, cacheKey, { settings: next, fileTrusted: true }); + yield* Cache.set(settingsCache, cacheKey, { settings: next, parseFailure: Option.none() }); yield* emitChange(next); return resolveTextGenerationProvider(materialized); }), From 033cb58ecd26b0ca4e2e1db9b12684b46320c874 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 15:48:34 +0000 Subject: [PATCH 5/5] fix(server): keep settings reload state and write failures together --- apps/server/src/serverSettings.test.ts | 24 ++++++++++++++++++++++++ apps/server/src/serverSettings.ts | 12 +++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts index 4fffc504ac3e4..fe8d5ea3d4f42 100644 --- a/apps/server/src/serverSettings.test.ts +++ b/apps/server/src/serverSettings.test.ts @@ -506,6 +506,27 @@ it.layer(NodeServices.layer)("server settings", (it) => { const saved = yield* decodeServerSettingsJson(yield* fs.readFileString(config.settingsPath)); assert.equal(saved.responseStreamingMode, "turn"); assert.equal(saved.observability.otlpTracesUrl, "http://localhost:4318/v1/traces"); + + yield* writeFileStringAtomically({ + filePath: config.settingsPath, + contents: `{ "responseStreamingMode": "paragraph" }`, + }); + const instanceId = ProviderInstanceId.make("acpRegistry_kilo"); + yield* serverSettings.updateProviderInstance({ + operation: "upsert", + instanceId, + instance: { + driver: ProviderDriverKind.make("acpRegistry"), + displayName: "Kilo", + config: { agentId: "kilo", distribution: "auto" }, + }, + }); + + const afterProviderSave = yield* decodeServerSettingsJson( + yield* fs.readFileString(config.settingsPath), + ); + assert.equal(afterProviderSave.responseStreamingMode, "paragraph"); + assert.equal(afterProviderSave.providerInstances[instanceId]?.displayName, "Kilo"); }).pipe(Effect.provide(layerServerSettings())), ); @@ -514,6 +535,7 @@ it.layer(NodeServices.layer)("server settings", (it) => { const config = yield* ServerConfig.ServerConfig; const fs = yield* FileSystem.FileSystem; const serverSettings = yield* ServerSettingsModule.ServerSettingsService; + yield* serverSettings.getSettings; const raw = `{ "responseStreamingMode": "turn", `; yield* fs.writeFileString(config.settingsPath, raw); @@ -524,6 +546,8 @@ it.layer(NodeServices.layer)("server settings", (it) => { ); assert.equal(error.operation, "read-file"); + assert.equal(error.settingsPath, config.settingsPath); + assert.instanceOf(error.cause, Error); assert.equal(yield* fs.readFileString(config.settingsPath), raw); }).pipe(Effect.provide(layerServerSettings())), ); diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index 37fdab5167acb..8c4d70758c4d5 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -658,9 +658,6 @@ const make = Effect.gen(function* () { let settings = DEFAULT_SERVER_SETTINGS; let legacyProviders: Readonly> | undefined; let legacySourceControl: Parameters[1] = {}; - // A file that failed to decode must stay on disk for the user to repair; - // the fold below only writes when it started from the file's real contents. - let settingsFileTrusted = true; let parseFailure = Option.none(); if (yield* readConfigExists) { @@ -676,7 +673,6 @@ const make = Effect.gen(function* () { } if (decoded._tag === "Failure" || legacySettings._tag === "Failure") { const failure = decoded._tag === "Failure" ? decoded : legacySettings; - settingsFileTrusted = false; // The file may hold an opt-out we can no longer read; never resume threads on a guess. settings = { ...DEFAULT_SERVER_SETTINGS, continueThreadsAfterServerUpdate: false }; if (failure._tag === "Failure") { @@ -692,6 +688,8 @@ const make = Effect.gen(function* () { } } + // A file that failed to decode must stay on disk for the user to repair. + const settingsFileTrusted = Option.isNone(parseFailure); const providerHistory = yield* sql<{ readonly providerName: string; readonly providerInstanceId: string | null; @@ -792,11 +790,7 @@ const make = Effect.gen(function* () { return { settings: migrated, parseFailure }; }); - const settingsCache = yield* Cache.makeWith< - typeof cacheKey, - { readonly settings: ServerSettings; readonly parseFailure: Option.Option }, - ServerSettingsError - >(() => loadSettingsFromDisk, { + const settingsCache = yield* Cache.makeWith((_key: typeof cacheKey) => loadSettingsFromDisk, { capacity: 1, timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero), });