Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

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

Utkarsh Patil7a08fcf2e832
diff --git a/apps/server/src/serverSettings.test.ts b/apps/server/src/serverSettings.test.ts
index d8a992f0e..26479d61b 100644
--- a/apps/server/src/serverSettings.test.ts
+++ b/apps/server/src/serverSettings.test.ts
@@ -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;
diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts
index 5a708d5c2..bdb1d5e0e 100644
--- a/apps/server/src/serverSettings.ts
+++ b/apps/server/src/serverSettings.ts
@@ -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({
diff --git a/apps/web/src/hooks/useSettings.ts b/apps/web/src/hooks/useSettings.ts
index 3dc2cf9b2..664d5ee3b 100644
--- a/apps/web/src/hooks/useSettings.ts
+++ b/apps/web/src/hooks/useSettings.ts
@@ -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);
diff --git a/packages/shared/src/serverSettings.test.ts b/packages/shared/src/serverSettings.test.ts
index 0ac5e415d..3d4a0da0b 100644
--- a/packages/shared/src/serverSettings.test.ts
+++ b/packages/shared/src/serverSettings.test.ts
@@ -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,
+ },
+ });
+ });
});
diff --git a/packages/shared/src/serverSettings.ts b/packages/shared/src/serverSettings.ts
index e7b25606d..db9bdcc59 100644
--- a/packages/shared/src/serverSettings.ts
+++ b/packages/shared/src/serverSettings.ts
@@ -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 } : {}),
+ },
+ };
+}