sailfork / t3code-github / apps/ server/ src/ serverSettings.test.ts

import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import {
  DEFAULT_SERVER_SETTINGS,
  globalIdleCompaction,
  type ManagedPolicyDocument,
  ModelSelection,
  ProjectId,
  ProjectScript,
  ProviderDriverKind,
  ProviderInstanceId,
  resolveProviderInstanceEnabled,
  SecretProviderId,
  ServerSettings,
  ServerSettingsPatch,
  UsageLimitSourceId,
} from "@t3tools/contracts";
import { createModelSelection } from "@t3tools/shared/model";
import { assert, it } from "@effect/vitest";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as PlatformError from "effect/PlatformError";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import * as SchemaAST from "effect/SchemaAST";
import * as SqlClient from "effect/sql/SqlClient";
import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
import { sourceControlHostSecretName } from "./sourceControl/sourceControlHostSecrets.ts";
import * as ServerConfig from "./config.ts";
import * as SqlitePersistence from "./persistence/Sqlite.ts";
import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite";
import * as ServerSettingsModule from "./serverSettings.ts";
import { SECRET_REDACTED } from "./settingsSecrets.ts";
import * as ManagedPolicy from "./policy/ManagedPolicy.ts";
import { resolveProviderInstanceTerminalEnvironment } from "./terminal/Manager.ts";

const decodeServerSettings = Schema.decodeUnknownEffect(ServerSettings);
const decodeServerSettingsJson = Schema.decodeUnknownEffect(Schema.fromJsonString(ServerSettings));
const decodeServerSettingsPatch = Schema.decodeUnknownEffect(ServerSettingsPatch);

const layerServerSettings = (policy?: ManagedPolicyDocument) =>
  ServerSettingsModule.layer.pipe(
    Layer.provide(ServerSecretStore.layer),
    Layer.provide(ManagedPolicy.layerTest(policy)),
    Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)),
    Layer.provideMerge(
      Layer.fresh(
        ServerConfig.layerTest(process.cwd(), {
          prefix: "t3code-server-settings-test-",
        }),
      ),
    ),
  );

const layerServerSettingsWithSecrets = () =>
  ServerSettingsModule.layer.pipe(
    Layer.provideMerge(ServerSecretStore.layer),
    Layer.provide(ManagedPolicy.layerTest()),
    Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)),
    Layer.provideMerge(
      Layer.fresh(
        ServerConfig.layerTest(process.cwd(), {
          prefix: "t3code-server-settings-test-",
        }),
      ),
    ),
  );

const layerFailingSecretStore = (cause: ServerSecretStore.SecretStoreError) =>
  Layer.succeed(
    ServerSecretStore.ServerSecretStore,
    ServerSecretStore.ServerSecretStore.of({
      get: () => Effect.fail(cause),
      set: () => Effect.void,
      create: () => Effect.void,
      getOrCreateRandom: () => Effect.succeed(new Uint8Array()),
      remove: () => Effect.void,
    }),
  );

const recordProviderUsage = (provider: string, instanceId: string | null = provider) =>
  Effect.gen(function* () {
    const sql = yield* SqlClient.SqlClient;
    yield* sql`
      INSERT INTO projection_thread_sessions (
        thread_id,
        status,
        provider_name,
        provider_instance_id,
        updated_at
      )
      VALUES (
        ${`thread-${instanceId ?? provider}`},
        ${"ready"},
        ${provider},
        ${instanceId},
        ${"2026-08-25T00:00:00.000Z"}
      )
    `;
  });

/** Paths of fields typed `SettingsSecretValue` that clients decode with the settings schema itself. */
function strictSecretReferencePaths(
  ast: SchemaAST.AST,
  path = "",
  seen = new Set<SchemaAST.AST>(),
): ReadonlyArray<string> {
  if (seen.has(ast)) return [];
  seen.add(ast);
  const walk = (type: SchemaAST.AST, at: string): ReadonlyArray<string> =>
    type.context?.annotations?.settingsSecretReference === true
      ? [at]
      : strictSecretReferencePaths(type, `${at}.`, seen);
  if (SchemaAST.isObjects(ast)) {
    return [
      ...ast.propertySignatures.flatMap((property) =>
        walk(property.type, `${path}${String(property.name)}`),
      ),
      ...ast.indexSignatures.flatMap((signature) => walk(signature.type, `${path}*`)),
    ];
  }
  if (SchemaAST.isArrays(ast)) {
    return [...ast.elements, ...ast.rest].flatMap((element) => walk(element, `${path}[]`));
  }
  if (SchemaAST.isUnion(ast)) {
    return ast.types.flatMap((member) => strictSecretReferencePaths(member, path, seen));
  }
  if (SchemaAST.isSuspend(ast)) return strictSecretReferencePaths(ast.thunk(), path, seen);
  return [];
}

it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("server settings", (it) => {
  it.effect("serializes concurrent model preference edits without cancelling duplicate adds", () =>
    Effect.gen(function* () {
      const service = yield* ServerSettingsModule.ServerSettingsService;
      const provider = ProviderInstanceId.make("codex_work");
      yield* Effect.all(
        [
          service.updateSettings({
            setModelFavorites: [{ provider, model: "sol", favorite: true }],
          }),
          service.updateSettings({
            setModelFavorites: [{ provider, model: "sol", favorite: true }],
          }),
          service.updateSettings({
            setModelFavorites: [{ provider, model: "luna", favorite: true }],
          }),
          service.updateSettings({ setModelsHidden: [{ provider, model: "astra", hidden: true }] }),
          service.updateSettings({ setModelsHidden: [{ provider, model: "luna", hidden: true }] }),
          service.updateSettings({
            setProviderModelOrder: { provider, modelOrder: ["luna", "sol"] },
          }),
        ],
        { concurrency: "unbounded" },
      );
      const settings = yield* service.getSettings;
      assert.deepEqual(settings.favorites?.map((entry) => entry.model).sort(), ["luna", "sol"]);
      assert.deepEqual(settings.providerModelPreferences?.[provider]?.hiddenModels.toSorted(), [
        "astra",
        "luna",
      ]);
      assert.deepEqual(settings.providerModelPreferences?.[provider]?.modelOrder, ["luna", "sol"]);
    }).pipe(Effect.provide(layerServerSettings())),
  );
  it.effect("persists and broadcasts model preferences to other connected clients", () =>
    Effect.scoped(
      Effect.gen(function* () {
        const service = yield* ServerSettingsModule.ServerSettingsService;
        const config = yield* ServerConfig.ServerConfig;
        const fs = yield* FileSystem.FileSystem;
        const provider = ProviderInstanceId.make("codex_work");
        const patch = {
          favorites: [{ provider, model: "sol" }],
          providerModelPreferences: {
            [provider]: { hiddenModels: ["astra"], modelOrder: ["sol"] },
          },
        };
        const changes = yield* service.subscribeChanges;
        yield* service.updateSettings(patch);
        const received = Option.getOrThrow(yield* Stream.runHead(changes));
        assert.deepEqual(received.favorites, patch.favorites);
        assert.deepEqual(received.providerModelPreferences, patch.providerModelPreferences);
        const saved = yield* decodeServerSettingsJson(
          yield* fs.readFileString(config.settingsPath),
        );
        assert.deepEqual(saved.favorites, patch.favorites);
        assert.deepEqual(saved.providerModelPreferences, patch.providerModelPreferences);
        const hidden = yield* service.updateSettings({
          setModelsHidden: [{ provider, model: "sol", hidden: true }],
        });
        assert.deepEqual(hidden.providerModelPreferences?.[provider], {
          hiddenModels: ["astra", "sol"],
          modelOrder: ["sol"],
        });
        const shown = yield* service.updateSettings({
          setModelsHidden: [{ provider, model: "astra", hidden: false }],
        });
        assert.deepEqual(shown.providerModelPreferences?.[provider], {
          hiddenModels: ["sol"],
          modelOrder: ["sol"],
        });
        assert.deepEqual(
          (yield* decodeServerSettingsJson(yield* fs.readFileString(config.settingsPath)))
            .providerModelPreferences,
          shown.providerModelPreferences,
        );
        yield* service.updateSettings({ favorites: [], providerModelPreferences: {} });

        const migrated = yield* service.updateSettings({ migrateModelPreferences: patch });
        assert.deepEqual(migrated.favorites, []);
        assert.deepEqual(migrated.providerModelPreferences, {});
      }),
    ).pipe(Effect.provide(layerServerSettings())),
  );
  it.effect("migrates saved token delivery to paragraph buffering without resetting settings", () =>
    Effect.gen(function* () {
      const config = yield* ServerConfig.ServerConfig;
      const fs = yield* FileSystem.FileSystem;
      const service = yield* ServerSettingsModule.ServerSettingsService;
      yield* fs.writeFileString(
        config.settingsPath,
        `{
          "responseStreamingMode": "token",
          "enableAgentBrowserAccess": false,
          "projectSettingsOverrides": {
            "legacy": { "responseStreamingMode": "token", "defaultAutoPull": true },
            "buffered": { "responseStreamingMode": "turn" },
            "inherited": { "defaultAutoPull": false }
          }
        }`,
      );

      const settings = yield* service.getSettings;
      assert.equal(settings.responseStreamingMode, "paragraph");
      assert.isFalse(settings.enableAgentBrowserAccess);
      assert.deepEqual(settings.projectSettingsOverrides, {
        [ProjectId.make("legacy")]: { responseStreamingMode: "paragraph", defaultAutoPull: true },
        [ProjectId.make("buffered")]: { responseStreamingMode: "turn" },
        [ProjectId.make("inherited")]: { defaultAutoPull: false },
      });

      yield* service.updateSettings({ responseStreamingMode: "turn" });
      const persisted = yield* decodeServerSettingsJson(
        yield* fs.readFileString(config.settingsPath),
      );
      assert.equal(persisted.responseStreamingMode, "turn");
      assert.deepEqual(persisted.projectSettingsOverrides, settings.projectSettingsOverrides);
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("keeps an override key a newer server saved when a project setting is edited", () =>
    Effect.gen(function* () {
      const config = yield* ServerConfig.ServerConfig;
      const fs = yield* FileSystem.FileSystem;
      const service = yield* ServerSettingsModule.ServerSettingsService;
      const projectId = ProjectId.make("project-a");
      yield* fs.writeFileString(
        config.settingsPath,
        `{ "projectSettingsOverrides": { "project-a": { "defaultAutoPull": true, "addedByNewerServer": 3 } } }`,
      );
      const loaded = yield* service.getSettings;

      yield* service.updateSettings({
        projectSettingsOverrides: {
          [projectId]: { ...loaded.projectSettingsOverrides[projectId], defaultAutoPull: false },
        },
      });

      const persisted = JSON.parse(yield* fs.readFileString(config.settingsPath));
      assert.deepEqual(persisted.projectSettingsOverrides, {
        [projectId]: { defaultAutoPull: false, addedByNewerServer: 3 },
      });
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("moves an idle compaction mode saved in the legacy switch to its own field", () =>
    Effect.gen(function* () {
      const config = yield* ServerConfig.ServerConfig;
      const fs = yield* FileSystem.FileSystem;
      const service = yield* ServerSettingsModule.ServerSettingsService;
      yield* fs.writeFileString(config.settingsPath, `{ "compactIdleThreads": "off" }`);

      const settings = yield* service.getSettings;
      // Shipped clients decode the switch as a boolean and drop the config otherwise.
      assert.strictEqual(settings.compactIdleThreads, false);
      assert.equal(settings.idleCompaction, "off");
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("tells clients it reads idle compaction modes before any settings are saved", () =>
    Effect.gen(function* () {
      const service = yield* ServerSettingsModule.ServerSettingsService;
      const settings = yield* service.getSettings;
      assert.equal(settings.idleCompaction, "onReturn");
      assert.strictEqual(settings.compactIdleThreads, false);
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("sends clients a known secret provider's config but never an unknown kind's", () =>
    Effect.gen(function* () {
      const settings = yield* decodeServerSettings({
        secretProviders: {
          personal: { kind: "one_password", label: "Personal", config: { account: "my" } },
          vault: { kind: "openbao", label: "Vault", config: { token: "bao-token" } },
        },
      });
      assert.deepStrictEqual<unknown>(
        ServerSettingsModule.redactServerSettingsForClient(settings, { secretReferences: true })
          .secretProviders,
        {
          personal: { kind: "one_password", label: "Personal", config: { account: "my" } },
          vault: { kind: "openbao", label: "Vault" },
        },
      );
    }),
  );

  it.effect("stores no secret provider of an unloaded kind or with a config its kind rejects", () =>
    Effect.gen(function* () {
      const service = yield* ServerSettingsModule.ServerSettingsService;
      const write = (entry: unknown) =>
        decodeServerSettingsPatch({ secretProviders: { personal: entry } }).pipe(
          Effect.flatMap(service.updateSettings),
          Effect.flip,
        );
      for (const entry of [
        { kind: "one_password", label: "Personal", config: { account: "--help" } },
        { kind: "one_password", label: "Personal" },
        { kind: "openbao", label: "Vault", config: { address: "https://x" } },
      ]) {
        assert.deepInclude(yield* write(entry), { operation: "write-secret-provider" });
      }
      assert.deepStrictEqual((yield* service.getSettings).secretProviders, {});
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("saves through a symlinked settings file without replacing the link", () =>
    Effect.gen(function* () {
      const config = yield* ServerConfig.ServerConfig;
      const fs = yield* FileSystem.FileSystem;
      const path = yield* Path.Path;
      const service = yield* ServerSettingsModule.ServerSettingsService;
      const dotfiles = yield* fs.makeTempDirectoryScoped({ prefix: "t3-settings-dotfiles-" });
      const linkedSettingsPath = path.join(dotfiles, "settings.json");
      yield* fs.writeFileString(linkedSettingsPath, `{ "responseStreamingMode": "turn" }`);
      yield* fs.remove(config.settingsPath, { force: true });
      yield* fs.symlink(linkedSettingsPath, config.settingsPath);

      yield* service.updateSettings({ responseStreamingMode: "paragraph" });

      assert.equal(yield* fs.readLink(config.settingsPath), linkedSettingsPath);
      const persisted = yield* decodeServerSettingsJson(
        yield* fs.readFileString(linkedSettingsPath),
      );
      assert.equal(persisted.responseStreamingMode, "paragraph");
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("retries a failed settings read instead of keeping the failure", () =>
    Effect.gen(function* () {
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;

      yield* fileSystem.makeDirectory(serverConfig.settingsPath);

      const error = yield* Effect.flip(serverSettings.getSettings);
      assert.deepInclude(error, { _tag: "ServerSettingsError", operation: "read-file" });

      yield* fileSystem.remove(serverConfig.settingsPath, { recursive: true });
      yield* fileSystem.writeFileString(
        serverConfig.settingsPath,
        `{ "responseStreamingMode": "turn" }`,
      );

      const settings = yield* serverSettings.getSettings;
      assert.equal(settings.responseStreamingMode, "turn");
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect(
    "decodes legacy object-shaped textGenerationModelSelection.options from settings.json",
    () =>
      Effect.gen(function* () {
        const decoded = yield* decodeServerSettings({
          textGenerationModelSelection: {
            provider: ProviderDriverKind.make("codex"),
            model: "gpt-5.4-mini",
            options: { reasoningEffort: "low" },
          },
        });

        assert.deepEqual(decoded.textGenerationModelSelection, {
          instanceId: ProviderInstanceId.make("codex"),
          model: "gpt-5.4-mini",
          options: [{ id: "reasoningEffort", value: "low" }],
        });
      }),
  );

  it.effect("deep merges nested settings updates without dropping siblings", () =>
    Effect.gen(function* () {
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;

      yield* serverSettings.updateSettings({
        observability: { otlpTracesUrl: "http://localhost:4318/v1/traces" },
        textGenerationModelSelection: {
          instanceId: ProviderInstanceId.make("codex"),
          model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
          options: createModelSelection(
            ProviderInstanceId.make("codex"),
            DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
            [
              { id: "reasoningEffort", value: "high" },
              { id: "fastMode", value: true },
            ],
          ).options!,
        },
      });

      const next = yield* serverSettings.updateSettings({
        observability: { otlpMetricsUrl: "http://localhost:4318/v1/metrics" },
        textGenerationModelSelection: {
          options: [{ id: "fastMode", value: false }],
        },
      });

      assert.equal(next.observability.otlpTracesUrl, "http://localhost:4318/v1/traces");
      assert.equal(next.observability.otlpMetricsUrl, "http://localhost:4318/v1/metrics");
      assert.deepEqual(
        next.textGenerationModelSelection,
        createModelSelection(
          ProviderInstanceId.make("codex"),
          DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
          [
            { id: "reasoningEffort", value: "high" },
            { id: "fastMode", value: false },
          ],
        ),
      );
    }).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");

      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())),
  );

  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;
      yield* serverSettings.getSettings;
      const raw = `{ "responseStreamingMode": "turn", `;
      yield* fs.writeFileString(config.settingsPath, raw);

      const error = yield* Effect.flip(
        serverSettings.updateSettings({
          observability: { otlpTracesUrl: "http://localhost:4318/v1/traces" },
        }),
      );

      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())),
  );

  it.effect("creates provider instances atomically without overwriting a concurrent add", () =>
    Effect.gen(function* () {
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      const instanceId = ProviderInstanceId.make("acpRegistry_shared");
      const results = yield* Effect.forEach(
        ["First", "Second"],
        (displayName) =>
          serverSettings
            .updateProviderInstance({
              operation: "create",
              instanceId,
              instance: {
                driver: ProviderDriverKind.make("acpRegistry"),
                displayName,
                config: { agentId: "shared", distribution: "auto" },
              },
            })
            .pipe(Effect.result),
        { concurrency: "unbounded" },
      );

      assert.equal(results.filter((result) => result._tag === "Success").length, 1);
      assert.equal(results.filter((result) => result._tag === "Failure").length, 1);
      assert.isTrue(
        ["First", "Second"].includes(
          (yield* serverSettings.getSettings).providerInstances[instanceId]?.displayName ?? "",
        ),
      );
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("pauses provider-instance mutations while a settings snapshot is in use", () =>
    Effect.gen(function* () {
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      const snapshotEntered = yield* Deferred.make<void>();
      const releaseSnapshot = yield* Deferred.make<void>();
      const mutationCompleted = yield* Deferred.make<void>();
      const instanceId = ProviderInstanceId.make("acpRegistry_kilo");

      const snapshotFiber = yield* serverSettings
        .withSettingsSnapshot(() =>
          Deferred.succeed(snapshotEntered, undefined).pipe(
            Effect.andThen(Deferred.await(releaseSnapshot)),
          ),
        )
        .pipe(Effect.forkChild({ startImmediately: true }));
      yield* Deferred.await(snapshotEntered);

      const mutationFiber = yield* serverSettings
        .updateProviderInstance({
          operation: "upsert",
          instanceId,
          instance: {
            driver: ProviderDriverKind.make("acpRegistry"),
            displayName: "Kilo",
            config: { agentId: "kilo", distribution: "auto" },
          },
        })
        .pipe(
          Effect.tap(() => Deferred.succeed(mutationCompleted, undefined)),
          Effect.forkChild({ startImmediately: true }),
        );
      yield* Effect.yieldNow;

      assert.isTrue(Option.isNone(yield* Deferred.poll(mutationCompleted)));
      yield* Deferred.succeed(releaseSnapshot, undefined);
      yield* Fiber.join(snapshotFiber);
      yield* Fiber.join(mutationFiber);
      assert.equal(
        (yield* serverSettings.getSettings).providerInstances[instanceId]?.displayName,
        "Kilo",
      );
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("buffers changes after a subscription is acquired but before it is consumed", () =>
    Effect.scoped(
      Effect.gen(function* () {
        const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
        const changes = yield* serverSettings.subscribeChanges;

        yield* serverSettings.updateSettings({
          addProjectBaseDirectory: "~/next",
        });

        const firstChange = yield* changes.pipe(Stream.runHead, Effect.timeout("1 second"));
        assert.equal(Option.getOrUndefined(firstChange)?.addProjectBaseDirectory, "~/next");
      }),
    ).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("preserves saved usage settings during cleanup edits and removes custom prices", () =>
    Effect.scoped(
      Effect.gen(function* () {
        const serverConfig = yield* ServerConfig.ServerConfig;
        const fileSystem = yield* FileSystem.FileSystem;
        const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
        const prices = {
          inputCostPerMillionTokens: 2,
          outputCostPerMillionTokens: 8,
          cacheReadCostPerMillionTokens: 0,
        };
        const readPersisted = fileSystem
          .readFileString(serverConfig.settingsPath)
          .pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.fromJsonString(ServerSettings))));

        yield* serverSettings.updateSettings({ usagePriceOverrides: { "example-model": prices } });
        const usageModelAliases = { "example-preview": "example-model" };
        const sourceId = UsageLimitSourceId.make("hub");
        const usageLimitSources = {
          [sourceId]: {
            kind: "cliproxy" as const,
            url: "https://hub.example.test",
            managementKey: "",
            enabled: true,
          },
        };
        const saved = yield* serverSettings.updateSettings({
          usageModelAliases,
          usageLimitSources,
        });
        const updated = yield* serverSettings.updateSettings({
          storageCleanup: { worktreeSettledAfterDays: 0, worktreeKeepWhen: "any-local-files" },
        });
        assert.deepStrictEqual(updated.usageLimitSources, saved.usageLimitSources);
        const persisted = yield* readPersisted;
        assert.deepStrictEqual(persisted.usagePriceOverrides, { "example-model": prices });
        assert.deepStrictEqual(persisted.usageModelAliases, usageModelAliases);
        assert.deepStrictEqual(persisted.usageLimitSources, usageLimitSources);
        assert.equal(persisted.storageCleanup.worktreeSettledAfterDays, 0);
        assert.equal(persisted.storageCleanup.worktreeKeepWhen, "any-local-files");

        yield* serverSettings.updateSettings({ usagePriceOverrides: { "example-model": null } });
        const restored = yield* readPersisted;
        assert.deepStrictEqual(restored.usagePriceOverrides, {});
      }),
    ).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("preserves model when switching providers via textGenerationModelSelection", () =>
    Effect.gen(function* () {
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;

      yield* serverSettings.updateSettings({
        textGenerationModelSelection: {
          instanceId: ProviderInstanceId.make("claudeAgent"),
          model: "claude-sonnet-4-6",
          options: createModelSelection(
            ProviderInstanceId.make("claudeAgent"),
            "claude-sonnet-4-6",
            [{ id: "effort", value: "high" }],
          ).options!,
        },
      });

      const next = yield* serverSettings.updateSettings({
        textGenerationModelSelection: {
          instanceId: ProviderInstanceId.make("codex"),
          model: "gpt-5.4",
          options: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
            { id: "reasoningEffort", value: "high" },
          ]).options!,
        },
      });

      assert.deepEqual(
        next.textGenerationModelSelection,
        createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
          { id: "reasoningEffort", value: "high" },
        ]),
      );
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect(
    "keeps a custom instance's selection when the driver's default instance is disabled",
    () =>
      Effect.gen(function* () {
        const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
        const instanceId = ProviderInstanceId.make("claude_openrouter");

        const next = yield* serverSettings.updateSettings({
          providerInstances: {
            [ProviderInstanceId.make("claudeAgent")]: {
              driver: ProviderDriverKind.make("claudeAgent"),
              enabled: false,
            },
            [instanceId]: {
              driver: ProviderDriverKind.make("claudeAgent"),
              enabled: true,
              config: { customModels: ["openai/gpt-5.5"] },
            },
          },
          textGenerationModelSelection: {
            instanceId,
            model: "openai/gpt-5.5",
          },
        });

        assert.deepEqual(next.textGenerationModelSelection, {
          instanceId,
          model: "openai/gpt-5.5",
        });
      }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect.each(["codex"])(
    "falls back to enabled instance %s after disabling the selection",
    (fallbackId) =>
      Effect.scoped(
        Effect.gen(function* () {
          const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
          const serverConfig = yield* ServerConfig.ServerConfig;
          const fileSystem = yield* FileSystem.FileSystem;
          const writerId = ProviderInstanceId.make("writer");
          const fallbackInstanceId = ProviderInstanceId.make(fallbackId);
          const selection = { instanceId: writerId, model: "claude-sonnet-4-6" };
          const providerInstances = {
            [ProviderInstanceId.make("claudeAgent")]: {
              driver: ProviderDriverKind.make("claudeAgent"),
              enabled: false,
            },
            [fallbackInstanceId]: {
              driver: ProviderDriverKind.make("codex"),
              enabled: true,
              config: {},
            },
            [writerId]: {
              driver: ProviderDriverKind.make("claudeAgent"),
              enabled: true,
              config: {},
            },
          };

          yield* serverSettings.updateSettings({
            providerInstances,
            textGenerationModelSelection: selection,
          });
          const changes = yield* serverSettings.subscribeChanges;

          const next = yield* serverSettings.updateSettings({
            providerInstances: {
              ...providerInstances,
              [writerId]: { ...providerInstances[writerId]!, enabled: false },
            },
          });
          const fallbackSelection = {
            instanceId: fallbackInstanceId,
            model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
          };
          assert.deepEqual(next.textGenerationModelSelection, fallbackSelection);
          assert.deepEqual(
            (yield* serverSettings.getSettings).textGenerationModelSelection,
            fallbackSelection,
          );
          const change = Option.getOrUndefined(yield* Stream.runHead(changes));
          assert.deepEqual(change?.textGenerationModelSelection, fallbackSelection);

          const persisted = yield* fileSystem
            .readFileString(serverConfig.settingsPath)
            .pipe(
              Effect.flatMap(Schema.decodeUnknownEffect(Schema.fromJsonString(ServerSettings))),
            );
          assert.deepEqual(persisted.textGenerationModelSelection, selection);

          const restored = yield* serverSettings.updateSettings({ providerInstances });
          assert.deepEqual(restored.textGenerationModelSelection, selection);
        }),
      ).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("skips explicitly disabled default instances when choosing a fallback", () =>
    Effect.gen(function* () {
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      const next = yield* serverSettings.updateSettings({
        providerInstances: {
          [ProviderInstanceId.make("codex")]: {
            driver: ProviderDriverKind.make("codex"),
            enabled: false,
            config: {},
          },
        },
      });

      assert.equal(next.textGenerationModelSelection.instanceId, "claudeAgent");
      assert.equal(next.textGenerationModelSelection.model, "claude-haiku-5-5");
    }).pipe(Effect.provide(layerServerSettings())),
  );

  // A backend behind a custom model may refuse the built-in default.
  it.effect("falls back to the thread default when it is a custom model on that instance", () =>
    Effect.gen(function* () {
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      const claude = ProviderInstanceId.make("claudeAgent");
      const providerInstances = {
        [ProviderInstanceId.make("codex")]: {
          driver: ProviderDriverKind.make("codex"),
          enabled: false,
          config: {},
        },
        [claude]: {
          driver: ProviderDriverKind.make("claudeAgent"),
          config: { customModels: ["z-ai/glm-5.3-flash"] },
        },
      };

      const custom = yield* serverSettings.updateSettings({
        providerInstances,
        defaultModelSelection: {
          instanceId: claude,
          model: "z-ai/glm-5.3-flash",
          options: [{ id: "thinking", value: false }],
        },
      });
      assert.deepEqual(custom.textGenerationModelSelection, {
        instanceId: claude,
        model: "z-ai/glm-5.3-flash",
        options: [{ id: "thinking", value: false }],
      });

      const builtIn = yield* serverSettings.updateSettings({
        defaultModelSelection: { instanceId: claude, model: "claude-fable-5-1" },
      });
      assert.equal(builtIn.textGenerationModelSelection.model, "claude-haiku-5-5");
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect(
    "preserves the source control writer selection when its provider instance is disabled",
    () =>
      Effect.gen(function* () {
        const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
        const serverConfig = yield* ServerConfig.ServerConfig;
        const fileSystem = yield* FileSystem.FileSystem;
        const instanceId = ProviderInstanceId.make("codex_writer");
        const sourceControlWriterModelSelection = {
          instanceId,
          model: "gpt-5.4-mini",
        };

        yield* serverSettings.updateSettings({
          providerInstances: {
            [instanceId]: {
              driver: ProviderDriverKind.make("codex"),
              enabled: true,
              config: {},
            },
          },
          sourceControlWriterModelSelection,
        });

        const next = yield* serverSettings.updateSettings({
          providerInstances: {
            [instanceId]: {
              driver: ProviderDriverKind.make("codex"),
              enabled: false,
              config: {},
            },
          },
        });

        assert.deepEqual(next.sourceControlWriterModelSelection, sourceControlWriterModelSelection);
        assert.deepEqual(
          ServerSettingsModule.resolveSourceControlWriterModelSelection(next, undefined, null),
          next.textGenerationModelSelection,
        );
        assert.deepEqual(
          (yield* serverSettings.getSettings).sourceControlWriterModelSelection,
          sourceControlWriterModelSelection,
        );

        const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
        assert.deepEqual(
          JSON.parse(raw).sourceControlWriterModelSelection,
          sourceControlWriterModelSelection,
        );

        const restored = yield* serverSettings.updateSettings({
          providerInstances: {
            [instanceId]: {
              driver: ProviderDriverKind.make("codex"),
              enabled: true,
              config: {},
            },
          },
        });
        assert.deepEqual(
          ServerSettingsModule.resolveSourceControlWriterModelSelection(restored, undefined, null),
          sourceControlWriterModelSelection,
        );
      }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("moves customized legacy provider settings into default instances on load", () =>
    Effect.gen(function* () {
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      yield* fileSystem.writeFileString(
        serverConfig.settingsPath,
        JSON.stringify({
          addProjectBaseDirectory: "~/Development",
          providers: {
            codex: { binaryPath: "/opt/guard/bin/codex" },
            claudeAgent: { enabled: false },
            cursor: { enabled: false },
            grok: { enabled: false },
            opencode: { enabled: false },
          },
        }),
      );

      const settings = yield* serverSettings.getSettings;

      assert.deepEqual(settings.providerInstances, {
        [ProviderInstanceId.make("codex")]: {
          driver: ProviderDriverKind.make("codex"),
          config: { binaryPath: "/opt/guard/bin/codex" },
        },
        [ProviderInstanceId.make("claudeAgent")]: {
          driver: ProviderDriverKind.make("claudeAgent"),
          enabled: false,
          config: {},
        },
      });

      const persisted = JSON.parse(yield* fileSystem.readFileString(serverConfig.settingsPath));
      assert.isUndefined(persisted.providers);
      assert.equal(persisted.addProjectBaseDirectory, "~/Development");
      assert.deepEqual(persisted.providerInstances.codex, {
        driver: "codex",
        config: { binaryPath: "/opt/guard/bin/codex" },
      });
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("keeps an explicit default instance over the legacy blob for its driver", () =>
    Effect.gen(function* () {
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      yield* fileSystem.writeFileString(
        serverConfig.settingsPath,
        JSON.stringify({
          providers: { codex: { binaryPath: "/legacy/codex" } },
          providerInstances: {
            codex: { driver: "codex", enabled: true, config: { binaryPath: "/explicit/codex" } },
          },
        }),
      );

      const settings = yield* serverSettings.getSettings;

      assert.deepEqual(settings.providerInstances[ProviderInstanceId.make("codex")], {
        driver: ProviderDriverKind.make("codex"),
        enabled: true,
        config: { binaryPath: "/explicit/codex" },
      });
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("enables previously used optional providers while migrating legacy settings", () =>
    Effect.gen(function* () {
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      yield* fileSystem.writeFileString(
        serverConfig.settingsPath,
        JSON.stringify({
          providers: { opencode: { serverUrl: "http://127.0.0.1:4096" }, cursor: {} },
          providerInstances: {
            cursor_work: { driver: "cursor", config: {} },
            opencode_unused: { driver: "opencode", config: {} },
          },
        }),
      );
      yield* recordProviderUsage("opencode");
      yield* recordProviderUsage("grok", null);
      yield* recordProviderUsage("cursor", "cursor_work");

      const settings = yield* serverSettings.getSettings;

      assert.deepEqual(settings.providerInstances[ProviderInstanceId.make("opencode")], {
        driver: ProviderDriverKind.make("opencode"),
        enabled: true,
        config: { serverUrl: "http://127.0.0.1:4096" },
      });

      assert.isTrue(settings.providerInstances[ProviderInstanceId.make("grok")]?.enabled);
      assert.isTrue(settings.providerInstances[ProviderInstanceId.make("cursor_work")]?.enabled);

      assert.isTrue(settings.providerInstances[ProviderInstanceId.make("cursor")]?.enabled);
      const unused = settings.providerInstances[ProviderInstanceId.make("opencode_unused")];
      assert.isDefined(unused);
      assert.isFalse(resolveProviderInstanceEnabled(unused));
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("keeps explicit legacy disables even when provider history shows use", () =>
    Effect.gen(function* () {
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      yield* fileSystem.writeFileString(
        serverConfig.settingsPath,
        JSON.stringify({ providers: { grok: { enabled: false, binaryPath: "/opt/grok" } } }),
      );
      yield* recordProviderUsage("grok");

      const settings = yield* serverSettings.getSettings;

      assert.deepEqual(settings.providerInstances[ProviderInstanceId.make("grok")], {
        driver: ProviderDriverKind.make("grok"),
        enabled: false,
        config: { binaryPath: "/opt/grok" },
      });
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("migrates legacy providers from an invalid file without rewriting it", () =>
    Effect.gen(function* () {
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      const raw =
        '{"addProjectBaseDirectory":42,"continueThreadsAfterServerUpdate":"false","providers":{"codex":{"binaryPath":"/x"}}}';
      yield* fileSystem.writeFileString(serverConfig.settingsPath, raw);

      const settings = yield* serverSettings.getSettings;

      assert.deepEqual(settings.providerInstances[ProviderInstanceId.make("codex")], {
        driver: ProviderDriverKind.make("codex"),
        config: { binaryPath: "/x" },
      });

      assert.isFalse(settings.continueThreadsAfterServerUpdate);
      assert.equal(yield* fileSystem.readFileString(serverConfig.settingsPath), raw);
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("keeps a default-off provider's new instance disabled and sparse on disk", () =>
    Effect.gen(function* () {
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;

      const next = yield* serverSettings.updateSettings({
        providerInstances: {
          [ProviderInstanceId.make("grok")]: {
            driver: ProviderDriverKind.make("grok"),
            config: {},
          },
        },
      });

      const grok = next.providerInstances[ProviderInstanceId.make("grok")];
      assert.isDefined(grok);
      assert.isFalse(resolveProviderInstanceEnabled(grok));
      const persisted = JSON.parse(yield* fileSystem.readFileString(serverConfig.settingsPath));
      assert.isUndefined(persisted.providers);
      assert.isUndefined(persisted.providerInstances.grok.enabled);
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("folds a legacy in-config enabled flag into the envelope on load", () =>
    Effect.gen(function* () {
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;

      yield* fileSystem.writeFileString(
        serverConfig.settingsPath,
        '{"providerInstances":{"grok":{"driver":"grok","enabled":true,"config":{"enabled":false}},"codex_work":{"driver":"codex","config":{"enabled":true,"homePath":"~/.codex"}},"cursor":{"driver":"cursor","config":{"enabled":"nope"}}}}',
      );

      const settings = yield* serverSettings.getSettings;

      const grokId = ProviderInstanceId.make("grok");
      const codexWorkId = ProviderInstanceId.make("codex_work");
      assert.deepEqual(settings.providerInstances[grokId], {
        driver: ProviderDriverKind.make("grok"),
        enabled: false,
        config: {},
      });

      assert.deepEqual(settings.providerInstances[codexWorkId], {
        driver: ProviderDriverKind.make("codex"),
        enabled: true,
        config: { homePath: "~/.codex" },
      });

      assert.deepEqual(settings.providerInstances[ProviderInstanceId.make("cursor")], {
        driver: ProviderDriverKind.make("cursor"),
        config: { enabled: "nope" },
      });
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect("reads settings secrets once per settings change", () => {
    let reads = 0;
    const layerSecret = Layer.effect(
      ServerSecretStore.ServerSecretStore,
      Effect.map(ServerSecretStore.ServerSecretStore, (store) => ({
        ...store,
        get: (name: string) =>
          Effect.suspend(() => {
            reads++;
            return store.get(name);
          }),
      })),
    ).pipe(Layer.provide(ServerSecretStore.layer));
    const layerSettings = ServerSettingsModule.layer.pipe(
      Layer.provide(layerSecret),
      Layer.provide(ManagedPolicy.layerTest()),
      Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)),
      Layer.provideMerge(
        Layer.fresh(
          ServerConfig.layerTest(process.cwd(), { prefix: "t3code-settings-secret-cache-test-" }),
        ),
      ),
    );
    return Effect.gen(function* () {
      const instanceId = ProviderInstanceId.make("codex_cached");
      const service = yield* ServerSettingsModule.ServerSettingsService;
      const save = (value: string) =>
        service.updateSettings({
          providerInstances: {
            [instanceId]: {
              driver: ProviderDriverKind.make("codex"),
              environment: [{ name: "API_TOKEN", value, sensitive: true }],
              config: {},
            },
          },
        });
      const token = service.getSettings.pipe(
        Effect.map((settings) => settings.providerInstances[instanceId]?.environment?.[0]?.value),
      );
      yield* save("sk-first");
      reads = 0;
      assert.equal(yield* token, "sk-first");
      assert.equal(yield* token, "sk-first");
      assert.equal(reads, 0);
      yield* save("sk-second");
      assert.equal(yield* token, "sk-second");
    }).pipe(Effect.provide(layerSettings));
  });

  it.effect("keeps the inline value on disk when secret migration fails", () => {
    const cause = new ServerSecretStore.SecretStorePersistError({
      resource: "provider environment secret",
      cause: new Error("Secret storage unavailable"),
    });
    const layerSecret = Layer.effect(
      ServerSecretStore.ServerSecretStore,
      Effect.map(ServerSecretStore.ServerSecretStore, (store) => ({
        ...store,
        set: () => Effect.fail(cause),
      })),
    ).pipe(Layer.provide(ServerSecretStore.layer));
    const layerSettings = ServerSettingsModule.layer.pipe(
      Layer.provide(ManagedPolicy.layerTest()),
      Layer.provide(layerSecret),
      Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)),
      Layer.provideMerge(
        Layer.fresh(
          ServerConfig.layerTest(process.cwd(), {
            prefix: "t3code-inline-secret-failure-test-",
          }),
        ),
      ),
    );
    return Effect.gen(function* () {
      const instanceId = ProviderInstanceId.make("codex_personal");
      const service = yield* ServerSettingsModule.ServerSettingsService;
      const config = yield* ServerConfig.ServerConfig;
      const fs = yield* FileSystem.FileSystem;
      const original =
        '{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"API_TOKEN","value":"inline-test-token","sensitive":true}],"config":{}}}}';
      yield* fs.writeFileString(config.settingsPath, original);
      const error = yield* Effect.flip(
        service.updateSettings({
          providerInstances: {
            [instanceId]: {
              driver: ProviderDriverKind.make("codex"),
              environment: [{ name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true }],
              config: {},
            },
          },
        }),
      );
      assert.equal(error.operation, "write-secret");
      assert.strictEqual(error.cause, cause);
      assert.equal(yield* fs.readFileString(config.settingsPath), original);
      const settings = yield* service.getSettings;
      assert.equal(
        settings.providerInstances[instanceId]?.environment?.[0]?.value,
        "inline-test-token",
      );
    }).pipe(Effect.provide(layerSettings));
  });

  it.effect.each(
    [
      {
        label: "preserves an inline secret on a redacted settings save",
        variable: { name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true },
        expected: "inline-test-token",
      },
      {
        label: "preserves the effective last inline secret when names are duplicated",
        variable: { name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true },
        expected: "last-inline-test-token",
        duplicate: true,
      },
      {
        label: "replaces an inline secret with an explicit value",
        variable: { name: "API_TOKEN", value: "replacement-test-token", sensitive: true },
        expected: "replacement-test-token",
      },
      {
        label: "clears an inline secret with an explicit empty value",
        variable: { name: "API_TOKEN", value: "", sensitive: true },
        expected: "",
      },
    ].map((testCase) => [testCase.label, testCase] as const),
  )("%s", ([, { variable, expected, duplicate }]) =>
    Effect.gen(function* () {
      const instanceId = ProviderInstanceId.make("codex_personal");
      const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
      const serverConfig = yield* ServerConfig.ServerConfig;
      const fileSystem = yield* FileSystem.FileSystem;
      yield* fileSystem.writeFileString(
        serverConfig.settingsPath,
        duplicate
          ? '{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"API_TOKEN","value":"inline-test-token","sensitive":true},{"name":"API_TOKEN","value":"last-inline-test-token","sensitive":true}],"config":{}}}}'
          : '{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"API_TOKEN","value":"inline-test-token","sensitive":true}],"config":{}}}}',
      );
      const initial = yield* serverSettings.getSettings;
      assert.equal(
        initial.providerInstances[instanceId]?.environment?.[0]?.value,
        "inline-test-token",
      );

      const next = yield* serverSettings.updateSettings({
        providerInstances: {
          [instanceId]: {
            driver: ProviderDriverKind.make("codex"),
            displayName: "Renamed provider",
            environment: duplicate ? [variable, variable] : [variable],
            config: {},
          },
        },
      });
      assert.equal(next.providerInstances[instanceId]?.environment?.[0]?.value, expected);
      const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
      assert.notInclude(raw, "inline-test-token");
      assert.notInclude(raw, "replacement-test-token");

      const reloaded = yield* Effect.gen(function* () {
        const fresh = yield* ServerSettingsModule.ServerSettingsService;
        return yield* fresh.getSettings;
      }).pipe(
        Effect.provide(
          Layer.fresh(ServerSettingsModule.layer).pipe(
            Layer.provide(Layer.merge(ServerSecretStore.layer, ManagedPolicy.layerTest())),
          ),
        ),
      );
      assert.equal(reloaded.providerInstances[instanceId]?.environment?.[0]?.value, expected);
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect.each([true, false])(
    "preserves duplicate secret operation order (sensitive last: %s)",
    (sensitiveLast) =>
      Effect.gen(function* () {
        const service = yield* ServerSettingsModule.ServerSettingsService;
        const instanceId = ProviderInstanceId.make("codex_duplicate");
        const secret = { name: "API_TOKEN", value: "secret-last", sensitive: true };
        const plain = { name: "API_TOKEN", value: "plain-last", sensitive: false };
        const next = yield* service.updateSettings({
          providerInstances: {
            [instanceId]: {
              driver: ProviderDriverKind.make("codex"),
              environment: sensitiveLast ? [plain, secret] : [secret, plain],
              config: {},
            },
          },
        });
        assert.equal(
          next.providerInstances[instanceId]?.environment?.at(-1)?.value,
          sensitiveLast ? "secret-last" : "plain-last",
        );
        assert.equal(
          next.providerInstances[instanceId]?.environment?.find((v) => v.sensitive)?.value,
          sensitiveLast ? "secret-last" : "",
        );
      }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect(
    "uses the same secret lifecycle for provider config, environment, host tokens, and quota keys",
    () =>
      Effect.gen(function* () {
        const service = yield* ServerSettingsModule.ServerSettingsService;
        const config = yield* ServerConfig.ServerConfig;
        const fs = yield* FileSystem.FileSystem;
        const store = yield* ServerSecretStore.ServerSecretStore;
        const instanceId = ProviderInstanceId.make("antigravity_secrets");
        const saved = yield* service.updateSettings({
          providerInstances: {
            [instanceId]: {
              driver: ProviderDriverKind.make("antigravity"),
              config: { apiKey: "config-secret" },
              environment: [
                { name: "API_TOKEN", value: "env-secret", sensitive: true },
                { name: "BASE_URL", value: "https://example.test", sensitive: false },
              ],
            },
          },
          sourceControlHosts: {
            github: { tokens: { "github.com": "github-secret" } },
            bitbucket: { email: "me@example.test", accessToken: "bitbucket-secret" },
            gitcafe: { token: "gitcafe-secret" },
          },
          usageLimitSources: {
            [UsageLimitSourceId.make("hub")]: {
              kind: "cliproxy",
              url: "https://hub.example.test",
              managementKey: "quota-secret",
              enabled: true,
            },
          },
          easDeviceHosts: [
            {
              kind: "eas",
              id: "eas",
              label: "EAS Simulator",
              accessToken: "expo-secret",
              maxSessionMinutes: 30,
              idleMinutes: 10,
              maxConcurrentSessions: 2,
            },
          ],
        });
        const values = [
          "config-secret",
          "env-secret",
          "github-secret",
          "bitbucket-secret",
          "gitcafe-secret",
          "quota-secret",
          "expo-secret",
        ];
        const client = ServerSettingsModule.redactServerSettingsForClient(saved, {
          secretReferences: true,
        });
        const disk = yield* fs.readFileString(config.settingsPath);
        for (const value of values) {
          assert.include(JSON.stringify(saved), value);
          assert.notInclude(JSON.stringify(client), value);
          assert.notInclude(disk, value);
        }
        assert.include(JSON.stringify(client), "https://example.test");
        assert.include(JSON.stringify(client), "me@example.test");
        const echoed = yield* service.updateSettings({
          providerInstances: client.providerInstances,
          sourceControlHosts: client.sourceControlHosts,
          usageLimitSources: client.usageLimitSources,
          easDeviceHosts: client.easDeviceHosts,
        });
        for (const value of values) assert.include(JSON.stringify(echoed), value);
        const renamed = yield* service.updateProviderInstance({
          operation: "upsert",
          instanceId,
          instance: { ...client.providerInstances[instanceId]!, displayName: "Renamed" },
        });
        assert.propertyVal(
          renamed.providerInstances[instanceId]?.config,
          "apiKey",
          "config-secret",
        );

        for (const apiKey of ["replacement-secret", ""]) {
          const changed = yield* service.updateProviderInstance({
            operation: "upsert",
            instanceId,
            instance: { ...client.providerInstances[instanceId]!, config: { apiKey } },
          });
          assert.propertyVal(changed.providerInstances[instanceId]?.config, "apiKey", apiKey);
          const redacted = ServerSettingsModule.redactServerSettingsForClient(changed, {
            secretReferences: true,
          });
          assert.propertyVal(
            redacted.providerInstances[instanceId]?.config,
            "apiKey",
            apiKey ? "••••••" : "",
          );
        }

        yield* service.updateProviderInstance({
          operation: "upsert",
          instanceId,
          instance: saved.providerInstances[instanceId]!,
        });

        const secretNames = [
          "provider-config-YW50aWdyYXZpdHlfc2VjcmV0cw-YW50aWdyYXZpdHk-YXBpS2V5",
          "provider-env-YW50aWdyYXZpdHlfc2VjcmV0cw-QVBJX1RPS0VO",
          sourceControlHostSecretName({
            kind: "github",
            field: "tokens",
            serverHost: "github.com",
          }),
          sourceControlHostSecretName({
            kind: "bitbucket",
            field: "accessToken",
            serverHost: null,
          }),
          sourceControlHostSecretName({ kind: "gitcafe", field: "token", serverHost: null }),
          "usage-limit-source-aHVi",
        ];
        for (const name of secretNames) {
          assert.isTrue(Option.isSome(yield* store.get(name)), name);
        }
        const secret = {
          provider: SecretProviderId.make("personal"),
          reference: "op://Private/Antigravity/token",
        };
        const referenced = yield* service.updateProviderInstance({
          operation: "upsert",
          instanceId,
          instance: {
            ...client.providerInstances[instanceId]!,
            environment: [
              { name: "API_TOKEN", value: secret, sensitive: true, valueRedacted: true },
            ],
          },
        });
        assert.isTrue(Option.isNone(yield* store.get(secretNames[1]!)));
        assert.deepEqual(
          ServerSettingsModule.redactServerSettingsForClient(referenced, { secretReferences: true })
            .providerInstances[instanceId]?.environment,
          [{ name: "API_TOKEN", value: secret, sensitive: true }],
        );
        yield* service.updateSettings({
          providerInstances: {},
          sourceControlHosts: {
            github: { tokens: {} },
            bitbucket: { accessToken: "" },
            gitcafe: { token: "" },
          },
          usageLimitSources: { [UsageLimitSourceId.make("hub")]: null },
        });
        for (const name of secretNames) {
          assert.isTrue(Option.isNone(yield* store.get(name)));
        }
      }).pipe(Effect.provide(layerServerSettingsWithSecrets())),
  );

  it.effect("moves saved provider credentials to the secret store when settings load", () =>
    Effect.gen(function* () {
      const service = yield* ServerSettingsModule.ServerSettingsService;
      const config = yield* ServerConfig.ServerConfig;
      const fs = yield* FileSystem.FileSystem;
      yield* fs.writeFileString(
        config.settingsPath,
        JSON.stringify({
          providerInstances: {
            antigravity: { driver: "antigravity", config: { apiKey: "legacy-api-key" } },
            opencode: {
              driver: "opencode",
              config: { serverUrl: "http://127.0.0.1:4096", serverPassword: "legacy-password" },
            },
          },
        }),
      );
      const saved = yield* service.getSettings;
      const opencode = ProviderInstanceId.make("opencode");
      assert.propertyVal(
        saved.providerInstances[ProviderInstanceId.make("antigravity")]?.config,
        "apiKey",
        "legacy-api-key",
      );
      assert.propertyVal(
        saved.providerInstances[opencode]?.config,
        "serverPassword",
        "legacy-password",
      );
      const client = ServerSettingsModule.redactServerSettingsForClient(saved, {
        secretReferences: true,
      });
      for (const value of ["legacy-api-key", "legacy-password"]) {
        assert.notInclude(yield* fs.readFileString(config.settingsPath), value);
        assert.notInclude(JSON.stringify(client), value);
      }
      const clientConfig = client.providerInstances[opencode]?.config as Record<string, unknown>;
      const edited = yield* service.updateProviderInstance({
        operation: "upsert",
        instanceId: opencode,
        instance: {
          ...client.providerInstances[opencode]!,
          config: { ...clientConfig, serverUrl: "http://127.0.0.1:4097" },
        },
      });
      assert.deepInclude(edited.providerInstances[opencode]?.config, {
        serverUrl: "http://127.0.0.1:4097",
        serverPassword: "legacy-password",
      });
    }).pipe(Effect.provide(layerServerSettings())),
  );

  it.effect(
    "keeps source control host secrets in the secret store and tells clients only that one is set",
    () =>
      Effect.gen(function* () {
        const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
        const secrets = yield* ServerSecretStore.ServerSecretStore;
        const serverConfig = yield* ServerConfig.ServerConfig;
        const fileSystem = yield* FileSystem.FileSystem;

        const saved = yield* serverSettings.updateSettings({
          sourceControlHosts: {
            bitbucket: { email: "me@example.com", accessToken: "bb-access" },
            github: { tokens: { "github.com": "ghp_dotcom", "ghe.acme.test": "ghp_ghe" } },
          },
        });
        assert.deepEqual(saved.sourceControlHosts.bitbucket, {
          email: "me@example.com",
          accessToken: "bb-access",
        });
        const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
        for (const secret of ["bb-access", "ghp_dotcom", "ghp_ghe"]) assert.notInclude(raw, secret);
        assert.include(raw, "me@example.com");

        const forClient = ServerSettingsModule.redactServerSettingsForClient(saved, {
          secretReferences: true,
        }).sourceControlHosts;
        assert.equal(forClient.bitbucket?.email, "me@example.com");
        assert.notInclude(String(forClient.bitbucket?.accessToken), "bb-access");
        assert.isAbove(String(forClient.bitbucket?.accessToken).length, 0);

        yield* serverSettings.updateSettings({ sourceControlHosts: forClient });
        yield* serverSettings.updateSettings({
          sourceControlHosts: { bitbucket: { email: "other@example.com" } },
        });
        const kept = (yield* serverSettings.getSettings).sourceControlHosts;
        assert.deepEqual(kept.bitbucket, { email: "other@example.com", accessToken: "bb-access" });
        assert.deepEqual(kept.github?.tokens, {
          "github.com": "ghp_dotcom",
          "ghe.acme.test": "ghp_ghe",
        });

        const cleared = yield* serverSettings.updateSettings({
          sourceControlHosts: {
            bitbucket: { accessToken: "" },
            github: {
              tokens: {
                "github.com": "",
                "ghe.acme.test": (forClient.github!.tokens as Record<string, string>)[
                  "ghe.acme.test"
                ],
              },
            },
          },
        });
        assert.equal(cleared.sourceControlHosts.bitbucket?.accessToken, "");
        const stored = (kind: string, field: string, serverHost: string | null) =>
          secrets.get(sourceControlHostSecretName({ kind, field, serverHost }));
        assert.isTrue(Option.isNone(yield* stored("bitbucket", "accessToken", null)));
        assert.isTrue(Option.isNone(yield* stored("github", "tokens", "github.com")));
        assert.isTrue(Option.isSome(yield* stored("github", "tokens", "ghe.acme.test")));

        const reference = {
          provider: SecretProviderId.make("personal"),
          reference: "op://Private/Bitbucket/token",
        };
        const gitHubReference = {
          provider: SecretProviderId.make("personal"),
          reference: "op://Private/GitHub/token",
        };
        yield* serverSettings.updateSettings({
          sourceControlHosts: {
            bitbucket: { accessToken: "bb-access" },
            github: { tokens: { "github.com": "ghp_dotcom", "ghe.acme.test": "ghp_ghe" } },
          },
        });
        assert.isTrue(Option.isSome(yield* stored("bitbucket", "accessToken", null)));