fix(server): drop stale text generation options when resetting text-gen model selection (#2076)

This commit is contained in:
Utkarsh Patil authored and GitHub committed 2026-04-16 20:01:35 +00:00
1 parent 19d47408a5
commit 7a08fcf2e8
5 files changed
+141 -5

No files matched your search

+29
View File
@@ -142,6 +142,35 @@ it.layer(NodeServices.layer)("server settings", (it) => {
}).pipe(Effect.provide(makeServerSettingsLayer())),
);
it.effect("drops stale text generation options when resetting model selection", () =>
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
yield* serverSettings.updateSettings({
textGenerationModelSelection: {
provider: "codex",
model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
options: {
reasoningEffort: "high",
fastMode: true,
},
},
});
const next = yield* serverSettings.updateSettings({
textGenerationModelSelection: {
provider: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.provider,
model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
},
});
assert.deepEqual(next.textGenerationModelSelection, {
provider: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.provider,
model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
});
}).pipe(Effect.provide(makeServerSettingsLayer())),
);
it.effect("trims provider path settings when updates are applied", () =>
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
+18 -2
View File
@@ -42,6 +42,7 @@ import * as Semaphore from "effect/Semaphore";
import { ServerConfig } from "./config";
import { type DeepPartial, deepMerge } from "@t3tools/shared/Struct";
import { fromLenientJson } from "@t3tools/shared/schemaJson";
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
export interface ServerSettingsShape {
/** Start the settings runtime and attach file watching. */
@@ -80,7 +81,20 @@ export class ServerSettingsService extends Context.Service<
getSettings: Ref.get(currentSettingsRef),
updateSettings: (patch) =>
Ref.get(currentSettingsRef).pipe(
Effect.map((currentSettings) => deepMerge(currentSettings, patch)),
Effect.flatMap((currentSettings) =>
Schema.decodeEffect(ServerSettings)(
applyServerSettingsPatch(currentSettings, patch),
).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath: "<memory>",
detail: `failed to normalize server settings: ${SchemaIssue.makeFormatterDefault()(cause.issue)}`,
cause,
}),
),
),
),
Effect.tap((nextSettings) => Ref.set(currentSettingsRef, nextSettings)),
),
streamChanges: Stream.empty,
@@ -314,7 +328,9 @@ const makeServerSettings = Effect.gen(function* () {
writeSemaphore.withPermits(1)(
Effect.gen(function* () {
const current = yield* getSettingsFromCache;
const next = yield* Schema.decodeEffect(ServerSettings)(deepMerge(current, patch)).pipe(
const next = yield* Schema.decodeEffect(ServerSettings)(
applyServerSettingsPatch(current, patch),
).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
+2 -2
View File
@@ -19,7 +19,7 @@ import {
} from "@t3tools/contracts/settings";
import { ensureLocalApi } from "~/localApi";
import { Struct } from "effect";
import { deepMerge } from "@t3tools/shared/Struct";
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
import { applySettingsUpdated, getServerConfig, useServerSettings } from "~/rpc/serverState";
const CLIENT_SETTINGS_PERSISTENCE_ERROR_SCOPE = "[CLIENT_SETTINGS]";
@@ -154,7 +154,7 @@ export function useUpdateSettings() {
if (Object.keys(serverPatch).length > 0) {
const currentServerConfig = getServerConfig();
if (currentServerConfig) {
applySettingsUpdated(deepMerge(currentServerConfig.settings, serverPatch));
applySettingsUpdated(applyServerSettingsPatch(currentServerConfig.settings, serverPatch));
}
// Fire-and-forget RPC — push will reconcile on success
void ensureLocalApi().server.updateSettings(serverPatch);
@@ -1,5 +1,7 @@
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
import { describe, expect, it } from "vitest";
import {
applyServerSettingsPatch,
extractPersistedServerObservabilitySettings,
normalizePersistedServerSettingString,
parsePersistedServerObservabilitySettings,
@@ -50,4 +52,61 @@ describe("serverSettings helpers", () => {
otlpMetricsUrl: undefined,
});
});
it("replaces text generation selection when provider/model are provided", () => {
const current = {
...DEFAULT_SERVER_SETTINGS,
textGenerationModelSelection: {
provider: "codex" as const,
model: "gpt-5.4-mini",
options: {
reasoningEffort: "high" as const,
fastMode: true,
},
},
};
expect(
applyServerSettingsPatch(current, {
textGenerationModelSelection: {
provider: "codex",
model: "gpt-5.4-mini",
},
}).textGenerationModelSelection,
).toEqual({
provider: "codex",
model: "gpt-5.4-mini",
});
});
it("still deep merges text generation selection when only options are provided", () => {
const current = {
...DEFAULT_SERVER_SETTINGS,
textGenerationModelSelection: {
provider: "codex" as const,
model: "gpt-5.4-mini",
options: {
reasoningEffort: "high" as const,
fastMode: true,
},
},
};
expect(
applyServerSettingsPatch(current, {
textGenerationModelSelection: {
options: {
fastMode: false,
},
},
}).textGenerationModelSelection,
).toEqual({
provider: "codex",
model: "gpt-5.4-mini",
options: {
reasoningEffort: "high",
fastMode: false,
},
});
});
});
+33 -1
View File
@@ -1,5 +1,6 @@
import { ServerSettings } from "@t3tools/contracts";
import { ServerSettings, type ServerSettingsPatch } from "@t3tools/contracts";
import { Schema } from "effect";
import { deepMerge } from "./Struct";
import { fromLenientJson } from "./schemaJson";
const ServerSettingsJson = fromLenientJson(ServerSettings);
@@ -38,3 +39,34 @@ export function parsePersistedServerObservabilitySettings(
return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined };
}
}
function shouldReplaceTextGenerationModelSelection(
patch: ServerSettingsPatch["textGenerationModelSelection"] | undefined,
): boolean {
return Boolean(patch && (patch.provider !== undefined || patch.model !== undefined));
}
/**
* Applies a server settings patch while treating textGenerationModelSelection as
* replace-on-provider/model updates. This prevents stale nested options from
* surviving a reset patch that intentionally omits options.
*/
export function applyServerSettingsPatch(
current: ServerSettings,
patch: ServerSettingsPatch,
): ServerSettings {
const selectionPatch = patch.textGenerationModelSelection;
const next = deepMerge(current, patch);
if (!selectionPatch || !shouldReplaceTextGenerationModelSelection(selectionPatch)) {
return next;
}
return {
...next,
textGenerationModelSelection: {
provider: selectionPatch.provider ?? current.textGenerationModelSelection.provider,
model: selectionPatch.model ?? current.textGenerationModelSelection.model,
...(selectionPatch.options ? { options: selectionPatch.options } : {}),
},
};
}