Skip to content

Commit 6101515

Browse files
Pysephclaude
andcommitted
fix(server): publish rotated secrets and never serve a half-done reload
The watcher compared a reload with the settings it last published. An external edit that rotates a provider key looks unchanged once the load moves the key into the secret store, so the live instance kept the old key. Skip only a reload that reads exactly the bytes of our last published save, and publish every other change as before. A reload also invalidated the cache before loading, so a getSettings call during a reload of an invalid file could join that lookup and get defaults. Load beside the cached entry and swap the result in, so readers keep the last good settings for the whole reload. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 276a860 commit 6101515

2 files changed

Lines changed: 134 additions & 46 deletions

File tree

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

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as Effect from "effect/Effect";
1818
import * as Duration from "effect/Duration";
1919
import * as FileSystem from "effect/FileSystem";
2020
import * as Fiber from "effect/Fiber";
21+
import * as Latch from "effect/Latch";
2122
import * as Layer from "effect/Layer";
2223
import * as Logger from "effect/Logger";
2324
import * as Option from "effect/Option";
@@ -66,8 +67,13 @@ const layerServerSettingsWithSecrets = () =>
6667
),
6768
);
6869

69-
// Reports each read of settings.json, so a test knows when the watcher has reloaded the file.
70-
const layerServerSettingsReportingReads = (reads: Queue.Queue<void>) =>
70+
// Runs the service's reads of settings.json through `aroundRead`, so a test can tell when the
71+
// watcher has reloaded the file, or hold a reload open.
72+
const layerServerSettingsAroundReads = (
73+
aroundRead: (
74+
read: Effect.Effect<string, PlatformError.PlatformError>,
75+
) => Effect.Effect<string, PlatformError.PlatformError>,
76+
) =>
7177
ServerSettingsModule.layer.pipe(
7278
Layer.provide(ServerSecretStore.layer),
7379
Layer.provide(
@@ -79,13 +85,9 @@ const layerServerSettingsReportingReads = (reads: Queue.Queue<void>) =>
7985
return FileSystem.FileSystem.of({
8086
...fs,
8187
readFileString: (path, encoding) =>
82-
fs
83-
.readFileString(path, encoding)
84-
.pipe(
85-
Effect.ensuring(
86-
path === settingsPath ? Queue.offer(reads, undefined) : Effect.void,
87-
),
88-
),
88+
path === settingsPath
89+
? aroundRead(fs.readFileString(path, encoding))
90+
: fs.readFileString(path, encoding),
8991
});
9092
}),
9193
),
@@ -100,6 +102,11 @@ const layerServerSettingsReportingReads = (reads: Queue.Queue<void>) =>
100102
),
101103
);
102104

105+
const layerServerSettingsReportingReads = (reads: Queue.Queue<void>) =>
106+
layerServerSettingsAroundReads((read) =>
107+
read.pipe(Effect.ensuring(Queue.offer(reads, undefined))),
108+
);
109+
103110
const usageScript = {
104111
id: "usage",
105112
name: "Usage",
@@ -475,16 +482,21 @@ it.layer(NodeServices.layer)("server settings", (it) => {
475482
const changes = yield* service.subscribeChanges;
476483

477484
yield* service.updateSettings({ responseStreamingMode: "turn" });
485+
const savedContents = yield* fs.readFileString(config.settingsPath);
478486
// One read is the save's own; the next is the watcher reloading the saved file.
479487
yield* Queue.take(reads);
480488
yield* Queue.take(reads).pipe(Effect.timeout("2 seconds"));
481489
yield* fs.writeFileString(
482490
config.settingsPath,
483491
JSON.stringify({ responseStreamingMode: "turn", defaultProjectScripts: [usageScript] }),
484492
);
493+
yield* Queue.take(reads).pipe(Effect.timeout("2 seconds"));
494+
yield* service.withSettingsSnapshot(() => Effect.void);
495+
// Once another edit is published, the saved contents are an edit of their own.
496+
yield* fs.writeFileString(config.settingsPath, savedContents);
485497

486-
const [saved, edited] = yield* changes.pipe(
487-
Stream.take(2),
498+
const [saved, edited, restored] = yield* changes.pipe(
499+
Stream.take(3),
488500
Stream.runCollect,
489501
Effect.timeout("2 seconds"),
490502
);
@@ -494,6 +506,79 @@ it.layer(NodeServices.layer)("server settings", (it) => {
494506
edited?.defaultProjectScripts.map((script) => script.id),
495507
["usage"],
496508
);
509+
assert.deepEqual(restored?.defaultProjectScripts, []);
510+
}),
511+
).pipe(Effect.provide(layerServerSettingsReportingReads(reads)));
512+
}).pipe(TestClock.withLive),
513+
);
514+
515+
it.effect("serves the last good settings while a reload of an invalid file is in flight", () =>
516+
Effect.gen(function* () {
517+
const started = yield* Queue.unbounded<void>();
518+
const reloads = yield* Latch.make(true);
519+
yield* Effect.scoped(
520+
Effect.gen(function* () {
521+
const config = yield* ServerConfig.ServerConfig;
522+
const fs = yield* FileSystem.FileSystem;
523+
const service = yield* ServerSettingsModule.ServerSettingsService;
524+
yield* fs.writeFileString(config.settingsPath, `{ "responseStreamingMode": "turn" }`);
525+
yield* service.start;
526+
yield* Queue.clear(started);
527+
528+
yield* reloads.close;
529+
yield* fs.writeFileString(config.settingsPath, `{ "responseStreamingMode": `);
530+
yield* Queue.take(started).pipe(Effect.timeout("2 seconds"));
531+
const during = yield* service.getSettings.pipe(Effect.timeout("2 seconds"));
532+
yield* reloads.open;
533+
534+
assert.equal(during.responseStreamingMode, "turn");
535+
const after = yield* service.withSettingsSnapshot(Effect.succeed);
536+
assert.equal(after.responseStreamingMode, "turn");
537+
}),
538+
).pipe(
539+
Effect.provide(
540+
layerServerSettingsAroundReads((read) =>
541+
Queue.offer(started, undefined).pipe(
542+
Effect.andThen(reloads.await),
543+
Effect.andThen(read),
544+
),
545+
),
546+
),
547+
);
548+
}).pipe(TestClock.withLive),
549+
);
550+
551+
it.effect("publishes a provider key that another program rotates in settings.json", () =>
552+
Effect.gen(function* () {
553+
const reads = yield* Queue.unbounded<void>();
554+
yield* Effect.scoped(
555+
Effect.gen(function* () {
556+
const config = yield* ServerConfig.ServerConfig;
557+
const fs = yield* FileSystem.FileSystem;
558+
const service = yield* ServerSettingsModule.ServerSettingsService;
559+
const instanceId = ProviderInstanceId.make("antigravity");
560+
const withApiKey = (apiKey: string) =>
561+
JSON.stringify({
562+
providerInstances: { antigravity: { driver: "antigravity", config: { apiKey } } },
563+
});
564+
yield* fs.writeFileString(config.settingsPath, withApiKey("first-key"));
565+
// Startup moves the key into the secret store and rewrites the file, which the
566+
// watcher then reloads.
567+
yield* service.start;
568+
yield* Queue.take(reads);
569+
yield* Queue.take(reads).pipe(Effect.timeout("2 seconds"));
570+
yield* service.withSettingsSnapshot(() => Effect.void);
571+
const changes = yield* service.subscribeChanges;
572+
573+
yield* fs.writeFileString(config.settingsPath, withApiKey("second-key"));
574+
575+
const change = yield* changes.pipe(Stream.runHead, Effect.timeout("2 seconds"));
576+
assert.propertyVal(
577+
Option.getOrUndefined(change)?.providerInstances[instanceId]?.config,
578+
"apiKey",
579+
"second-key",
580+
);
581+
assert.notInclude(yield* fs.readFileString(config.settingsPath), "second-key");
497582
}),
498583
).pipe(Effect.provide(layerServerSettingsReportingReads(reads)));
499584
}).pipe(TestClock.withLive),

‎apps/server/src/serverSettings.ts‎

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,13 @@ const make = Effect.gen(function* () {
572572
const startedDeferred = yield* Deferred.make<void, ServerSettingsError>();
573573
const watcherScope = yield* Scope.make("sequential");
574574
yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void));
575-
// What subscribers last received. A reload that finds the same settings, such as our own
576-
// write coming back through the watcher, publishes nothing.
577-
const publishedRef = yield* Ref.make(Option.none<ServerSettings>());
575+
// The file contents of our last save, set only once the save is published. The watcher skips
576+
// a reload that reads exactly these bytes; any other contents are published as before.
577+
const publishedSaveRef = yield* Ref.make(Option.none<string>());
578578
const parseFailureLoggedRef = yield* Ref.make(false);
579579

580580
const emitChange = (settings: ServerSettings) =>
581-
Ref.set(publishedRef, Option.some(settings)).pipe(
582-
Effect.andThen(PubSub.publish(changesPubSub, settings)),
583-
Effect.asVoid,
584-
);
581+
PubSub.publish(changesPubSub, settings).pipe(Effect.asVoid);
585582

586583
const readConfigExists = fs.exists(settingsPath).pipe(
587584
Effect.mapError(
@@ -610,13 +607,12 @@ const make = Effect.gen(function* () {
610607
const sparseSettingsJson = yield* encodeServerSettingsJson(
611608
stripDefaultServerSettings(settings, DEFAULT_SERVER_SETTINGS) ?? {},
612609
);
610+
const contents = `${sparseSettingsJson}\n`;
613611

614-
return yield* writeFileStringAtomically({
615-
filePath: settingsPath,
616-
contents: `${sparseSettingsJson}\n`,
617-
}).pipe(
612+
return yield* writeFileStringAtomically({ filePath: settingsPath, contents }).pipe(
618613
Effect.provideService(FileSystem.FileSystem, fs),
619614
Effect.provideService(Path.Path, pathService),
615+
Effect.as(contents),
620616
);
621617
},
622618
Effect.mapError(
@@ -664,9 +660,11 @@ const make = Effect.gen(function* () {
664660
let legacyProviders: Readonly<Record<string, unknown>> | undefined;
665661
let legacySourceControl: Parameters<typeof migrateLegacySourceControlSettings>[1] = {};
666662
let parseFailure = Option.none<unknown>();
663+
let isPublishedSave = false;
667664

668665
if (yield* readConfigExists) {
669666
const raw = yield* readRawConfig;
667+
isPublishedSave = Option.contains(yield* Ref.get(publishedSaveRef), raw);
670668
const decoded = decodeServerSettingsJsonExit(raw);
671669
const legacySettings = decodeLegacyProviderSettingsJsonExit(raw);
672670
if (legacySettings._tag === "Success") {
@@ -801,27 +799,35 @@ const make = Effect.gen(function* () {
801799
yield* secretStore.remove(move.from).pipe(Effect.ignore);
802800
}
803801
}
804-
return { settings: migrated, parseFailure };
802+
return { settings: migrated, parseFailure, isPublishedSave };
805803
});
806804

807-
const settingsCache = yield* Cache.makeWith((_key: typeof cacheKey) => loadSettingsFromDisk, {
808-
capacity: 1,
809-
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
810-
});
805+
const settingsCache = yield* Cache.makeWith(
806+
(_key: typeof cacheKey) =>
807+
loadSettingsFromDisk.pipe(
808+
Effect.map(({ settings, parseFailure }) => ({ settings, parseFailure })),
809+
),
810+
{
811+
capacity: 1,
812+
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
813+
},
814+
);
811815

812816
const getSettingsFromCache = Cache.get(settingsCache, cacheKey).pipe(
813817
Effect.map((loaded) => loaded.settings),
814818
);
815-
// A file caught mid-write or left invalid keeps the settings that last loaded instead of
816-
// resetting them to defaults. Its parse failure still blocks saves.
819+
// Loads beside the cached entry and then replaces it, so readers never wait on a reload or
820+
// see a half-finished one. A file caught mid-write or left invalid keeps the settings that
821+
// last loaded instead of resetting them to defaults; its parse failure still blocks saves.
817822
const reloadSettings = Effect.gen(function* () {
823+
const { isPublishedSave, ...loaded } = yield* loadSettingsFromDisk;
818824
const previous = yield* Cache.getSuccess(settingsCache, cacheKey);
819-
yield* Cache.invalidate(settingsCache, cacheKey);
820-
const loaded = yield* Cache.get(settingsCache, cacheKey);
821-
if (Option.isNone(loaded.parseFailure) || Option.isNone(previous)) return loaded;
822-
const kept = { settings: previous.value.settings, parseFailure: loaded.parseFailure };
823-
yield* Cache.set(settingsCache, cacheKey, kept);
824-
return kept;
825+
const next =
826+
Option.isSome(loaded.parseFailure) && Option.isSome(previous)
827+
? { settings: previous.value.settings, parseFailure: loaded.parseFailure }
828+
: loaded;
829+
yield* Cache.set(settingsCache, cacheKey, next);
830+
return { ...next, isPublishedSave };
825831
});
826832

827833
const materializeSettingsSecrets = Effect.fn("serverSettings.materializeSettingsSecrets")(
@@ -1035,7 +1041,7 @@ const make = Effect.gen(function* () {
10351041
const updated = yield* update(current);
10361042
const persisted = yield* persistSettingsSecrets(current, updated);
10371043
const next = yield* normalizeServerSettings(persisted.settings);
1038-
const materialized = yield* Effect.uninterruptibleMask(() =>
1044+
const { materialized, written } = yield* Effect.uninterruptibleMask(() =>
10391045
Effect.gen(function* () {
10401046
const rollbackSecretChanges = yield* applySettingsSecretChanges(persisted.changes);
10411047
const materializedExit = yield* Effect.exit(materializeSettingsSecrets(next));
@@ -1048,11 +1054,12 @@ const make = Effect.gen(function* () {
10481054
yield* rollbackSecretChanges;
10491055
return yield* Effect.failCause(writeExit.cause);
10501056
}
1051-
return materializedExit.value;
1057+
return { materialized: materializedExit.value, written: writeExit.value };
10521058
}),
10531059
);
10541060
yield* Cache.set(settingsCache, cacheKey, { settings: next, parseFailure: Option.none() });
10551061
yield* emitChange(next);
1062+
yield* Ref.set(publishedSaveRef, Option.some(written));
10561063
return resolveTextGenerationProvider(materialized);
10571064
}),
10581065
);
@@ -1068,10 +1075,11 @@ const make = Effect.gen(function* () {
10681075

10691076
const revalidateAndEmit = writeSemaphore.withPermits(1)(
10701077
Effect.gen(function* () {
1071-
const { settings } = yield* reloadSettings;
1072-
const published = yield* Ref.get(publishedRef);
1073-
if (Option.isSome(published) && Equal.equals(published.value, settings)) return;
1078+
const { settings, parseFailure, isPublishedSave } = yield* reloadSettings;
1079+
// Subscribers already have our own save, and keep what they have while the file is invalid.
1080+
if (isPublishedSave || Option.isSome(parseFailure)) return;
10741081
yield* emitChange(settings);
1082+
yield* Ref.set(publishedSaveRef, Option.none());
10751083
}),
10761084
);
10771085

@@ -1159,12 +1167,7 @@ const make = Effect.gen(function* () {
11591167

11601168
const startup = Effect.gen(function* () {
11611169
yield* startWatcher;
1162-
const { settings } = yield* reloadSettings;
1163-
// Clients start from a snapshot of these, so they count as published.
1164-
yield* Ref.update(
1165-
publishedRef,
1166-
Option.orElseSome(() => settings),
1167-
);
1170+
yield* reloadSettings;
11681171
});
11691172

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

0 commit comments

Comments
 (0)