@devries-cameron20 / t3code Mirror · read-only / commits / 7e3a1150bf
Fork 0

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 23, 00:30 UTC

fix(server): settings saves no longer revert external edits to settings.json (#18582)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

F4llen committed Oct 19, 2026 at 17:17 UTC 7e3a1150bf61c552d5abc02e8a768cabaa4d7d5b
2 files changed +90 -19
apps/server/src/serverSettings.test.ts
+64
@@ -488,6 +488,70 @@ it.layer(NodeServices.layer)("server settings", (it) => {
488 }).pipe(Effect.provide(layerServerSettings())),
489 );
490
491 + it.effect("keeps an external settings edit that the watcher has not reloaded yet", () =>
492 + Effect.gen(function* () {
493 + const config = yield* ServerConfig.ServerConfig;
494 + const fs = yield* FileSystem.FileSystem;
495 + const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
496 + yield* serverSettings.getSettings;
497 +
498 + yield* writeFileStringAtomically({
499 + filePath: config.settingsPath,
500 + contents: `{ "responseStreamingMode": "turn" }`,
501 + });
502 + yield* serverSettings.updateSettings({
503 + observability: { otlpTracesUrl: "http://localhost:4318/v1/traces" },
504 + });
505 +
506 + const saved = yield* decodeServerSettingsJson(yield* fs.readFileString(config.settingsPath));
507 + assert.equal(saved.responseStreamingMode, "turn");
508 + assert.equal(saved.observability.otlpTracesUrl, "http://localhost:4318/v1/traces");
509 +
510 + yield* writeFileStringAtomically({
511 + filePath: config.settingsPath,
512 + contents: `{ "responseStreamingMode": "paragraph" }`,
513 + });
514 + const instanceId = ProviderInstanceId.make("acpRegistry_kilo");
515 + yield* serverSettings.updateProviderInstance({
516 + operation: "upsert",
517 + instanceId,
518 + instance: {
519 + driver: ProviderDriverKind.make("acpRegistry"),
520 + displayName: "Kilo",
521 + config: { agentId: "kilo", distribution: "auto" },
522 + },
523 + });
524 +
525 + const afterProviderSave = yield* decodeServerSettingsJson(
526 + yield* fs.readFileString(config.settingsPath),
527 + );
528 + assert.equal(afterProviderSave.responseStreamingMode, "paragraph");
529 + assert.equal(afterProviderSave.providerInstances[instanceId]?.displayName, "Kilo");
530 + }).pipe(Effect.provide(layerServerSettings())),
531 + );
532 +
533 + it.effect("refuses to save over a settings file it cannot parse", () =>
534 + Effect.gen(function* () {
535 + const config = yield* ServerConfig.ServerConfig;
536 + const fs = yield* FileSystem.FileSystem;
537 + const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
538 + yield* serverSettings.getSettings;
539 + const raw = `{ "responseStreamingMode": "turn", `;
540 + yield* fs.writeFileString(config.settingsPath, raw);
541 +
542 + const error = yield* Effect.flip(
543 + serverSettings.updateSettings({
544 + observability: { otlpTracesUrl: "http://localhost:4318/v1/traces" },
545 + }),
546 + );
547 +
548 + assert.equal(error.operation, "read-file");
549 + assert.equal(error.settingsPath, config.settingsPath);
550 + assert.instanceOf(error.cause, Error);
551 + assert.equal(yield* fs.readFileString(config.settingsPath), raw);
552 + }).pipe(Effect.provide(layerServerSettings())),
553 + );
554 +
555 it.effect("creates provider instances atomically without overwriting a concurrent add", () =>
556 Effect.gen(function* () {
557 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
apps/server/src/serverSettings.ts
+26 -19
@@ -658,9 +658,7 @@ const make = Effect.gen(function* () {
658 let settings = DEFAULT_SERVER_SETTINGS;
659 let legacyProviders: Readonly<Record<string, unknown>> | undefined;
660 let legacySourceControl: Parameters<typeof migrateLegacySourceControlSettings>[1] = {};
661 - // A file that failed to decode must stay on disk for the user to repair;
662 - // the fold below only writes when it started from the file's real contents.
663 - let settingsFileTrusted = true;
661 + let parseFailure = Option.none<unknown>();
662
663 if (yield* readConfigExists) {
664 const raw = yield* readRawConfig;
@@ -675,10 +673,10 @@ const make = Effect.gen(function* () {
673 }
674 if (decoded._tag === "Failure" || legacySettings._tag === "Failure") {
675 const failure = decoded._tag === "Failure" ? decoded : legacySettings;
678 - settingsFileTrusted = false;
676 // The file may hold an opt-out we can no longer read; never resume threads on a guess.
677 settings = { ...DEFAULT_SERVER_SETTINGS, continueThreadsAfterServerUpdate: false };
678 if (failure._tag === "Failure") {
679 + parseFailure = Option.some(Cause.squash(failure.cause));
680 yield* Effect.logWarning("failed to parse settings.json, using defaults", {
681 path: settingsPath,
682 issues: Cause.pretty(failure.cause),
@@ -690,6 +688,8 @@ const make = Effect.gen(function* () {
688 }
689 }
690
691 + // A file that failed to decode must stay on disk for the user to repair.
692 + const settingsFileTrusted = Option.isNone(parseFailure);
693 const providerHistory = yield* sql<{
694 readonly providerName: string;
695 readonly providerInstanceId: string | null;
@@ -787,18 +787,20 @@ const make = Effect.gen(function* () {
787 yield* secretStore.remove(move.from).pipe(Effect.ignore);
788 }
789 }
790 - return migrated;
790 + return { settings: migrated, parseFailure };
791 });
792
793 - const settingsCache = yield* Cache.makeWith<typeof cacheKey, ServerSettings, ServerSettingsError>(
794 - () => loadSettingsFromDisk,
795 - {
796 - capacity: 1,
797 - timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
798 - },
799 - );
793 + const settingsCache = yield* Cache.makeWith((_key: typeof cacheKey) => loadSettingsFromDisk, {
794 + capacity: 1,
795 + timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
796 + });
797
801 - const getSettingsFromCache = Cache.get(settingsCache, cacheKey);
798 + const getSettingsFromCache = Cache.get(settingsCache, cacheKey).pipe(
799 + Effect.map((loaded) => loaded.settings),
800 + );
801 + const reloadSettings = Cache.invalidate(settingsCache, cacheKey).pipe(
802 + Effect.andThen(Cache.get(settingsCache, cacheKey)),
803 + );
804
805 const materializeSettingsSecrets = (
806 settings: ServerSettings,
@@ -1001,7 +1003,14 @@ const make = Effect.gen(function* () {
1003 ): Effect.Effect<ServerSettings, ServerSettingsError> =>
1004 writeSemaphore.withPermits(1)(
1005 Effect.gen(function* () {
1004 - const current = yield* getSettingsFromCache;
1006 + const { settings: current, parseFailure } = yield* reloadSettings;
1007 + if (Option.isSome(parseFailure)) {
1008 + return yield* new ServerSettingsError({
1009 + settingsPath,
1010 + operation: "read-file",
1011 + cause: parseFailure.value,
1012 + });
1013 + }
1014 const updated = yield* update(current);
1015 const persisted = yield* persistSettingsSecrets(current, updated);
1016 const next = yield* normalizeServerSettings(persisted.settings);
@@ -1021,7 +1030,7 @@ const make = Effect.gen(function* () {
1030 return materializedExit.value;
1031 }),
1032 );
1024 - yield* Cache.set(settingsCache, cacheKey, next);
1033 + yield* Cache.set(settingsCache, cacheKey, { settings: next, parseFailure: Option.none() });
1034 yield* emitChange(next);
1035 return resolveTextGenerationProvider(materialized);
1036 }),
@@ -1038,8 +1047,7 @@ const make = Effect.gen(function* () {
1047
1048 const revalidateAndEmit = writeSemaphore.withPermits(1)(
1049 Effect.gen(function* () {
1041 - yield* Cache.invalidate(settingsCache, cacheKey);
1042 - const settings = yield* getSettingsFromCache;
1050 + const { settings } = yield* reloadSettings;
1051 yield* emitChange(settings);
1052 }),
1053 );
@@ -1128,8 +1136,7 @@ const make = Effect.gen(function* () {
1136
1137 const startup = Effect.gen(function* () {
1138 yield* startWatcher;
1131 - yield* Cache.invalidate(settingsCache, cacheKey);
1132 - yield* getSettingsFromCache;
1139 + yield* reloadSettings;
1140 });
1141
1142 const startupExit = yield* Effect.exit(startup);