Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

serverSettings.test.ts

1,325 lines50.5 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import {
3 DEFAULT_SERVER_SETTINGS,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 resolveProviderInstanceEnabled,
7 ServerSettings,
8 ServerSettingsPatch,
9} from "@t3tools/contracts";
10import { createModelSelection } from "@t3tools/shared/model";
11import { assert, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13import * as Duration from "effect/Duration";
14import * as FileSystem from "effect/FileSystem";
15import * as Layer from "effect/Layer";
16import * as Option from "effect/Option";
17import * as Path from "effect/Path";
18import * as PlatformError from "effect/PlatformError";
19import * as Schema from "effect/Schema";
20import * as Stream from "effect/Stream";
21import * as SqlClient from "effect/unstable/sql/SqlClient";
22import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
23import * as ServerConfig from "./config.ts";
24import { SqlitePersistenceMemory } from "./persistence/Layers/Sqlite.ts";
25import * as ServerSettingsModule from "./serverSettings.ts";
26import { resolveProviderInstanceTerminalEnvironment } from "./terminal/Manager.ts";
27​
28const decodeSettingsPatch = Schema.decodeUnknownEffect(ServerSettingsPatch);
29const decodeServerSettings = Schema.decodeUnknownEffect(ServerSettings);
30​
31const makeServerSettingsLayer = () =>
32 ServerSettingsModule.layer.pipe(
33 Layer.provide(ServerSecretStore.layer),
34 Layer.provideMerge(Layer.fresh(SqlitePersistenceMemory)),
35 Layer.provideMerge(
36 Layer.fresh(
37 ServerConfig.layerTest(process.cwd(), {
38 prefix: "t3code-server-settings-test-",
39 }),
40 ),
41 ),
42 );
43​
44const makeFailingSecretStoreLayer = (cause: ServerSecretStore.SecretStoreError) =>
45 Layer.succeed(
46 ServerSecretStore.ServerSecretStore,
47 ServerSecretStore.ServerSecretStore.of({
48 get: () => Effect.fail(cause),
49 set: () => Effect.void,
50 create: () => Effect.void,
51 getOrCreateRandom: () => Effect.succeed(new Uint8Array()),
52 remove: () => Effect.void,
53 }),
54 );
55​
56const recordProviderUsage = (provider: string, instanceId: string | null = provider) =>
57 Effect.gen(function* () {
58 const sql = yield* SqlClient.SqlClient;
59 yield* sql`
60 INSERT INTO projection_thread_sessions (
61 thread_id,
62 status,
63 provider_name,
64 provider_instance_id,
65 updated_at
66 )
67 VALUES (
68 ${`thread-${instanceId ?? provider}`},
69 ${"ready"},
70 ${provider},
71 ${instanceId},
72 ${"2026-08-25T00:00:00.000Z"}
73 )
74 `;
75 });
76​
77it.layer(NodeServices.layer)("server settings", (it) => {
78 it.effect("preserves context when reading a provider environment secret fails", () => {
79 const platformCause = PlatformError.systemError({
80 _tag: "PermissionDenied",
81 module: "FileSystem",
82 method: "readFile",
83 pathOrDescriptor: "provider environment secret",
84 description: "Secret backend unavailable.",
85 });
86 const cause = new ServerSecretStore.SecretStoreReadError({
87 resource: "provider environment secret",
88 cause: platformCause,
89 });
90 const configLayer = Layer.fresh(
91 ServerConfig.layerTest(process.cwd(), {
92 prefix: "t3code-server-settings-secret-failure-test-",
93 }),
94 );
95 const settingsLayer = ServerSettingsModule.layer.pipe(
96 Layer.provide(makeFailingSecretStoreLayer(cause)),
97 Layer.provideMerge(Layer.fresh(SqlitePersistenceMemory)),
98 Layer.provideMerge(configLayer),
99 );
100​
101 return Effect.gen(function* () {
102 const serverConfig = yield* ServerConfig.ServerConfig;
103 const fileSystem = yield* FileSystem.FileSystem;
104 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
105 yield* fileSystem.writeFileString(
106 serverConfig.settingsPath,
107 '{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"OPENROUTER_API_KEY","value":"","sensitive":true,"valueRedacted":true}],"config":{}}}}',
108 );
109​
110 const error = yield* Effect.flip(serverSettings.getSettings);
111​
112 assert.deepInclude(error, {
113 _tag: "ServerSettingsError",
114 operation: "read-secret",
115 providerInstanceId: "codex_personal",
116 environmentVariable: "OPENROUTER_API_KEY",
117 });
118 assert.strictEqual(error.cause, cause);
119 assert.notInclude(error.message, cause.message);
120 }).pipe(Effect.provide(settingsLayer));
121 });
122​
123 it.effect("identifies provider history query failures", () =>
124 Effect.gen(function* () {
125 const serverConfig = yield* ServerConfig.ServerConfig;
126 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
127 const sql = yield* SqlClient.SqlClient;
128 yield* sql`DROP TABLE projection_thread_sessions`;
129​
130 const error = yield* Effect.flip(serverSettings.getSettings);
131​
132 assert.deepInclude(error, {
133 _tag: "ServerSettingsError",
134 operation: "read-provider-history",
135 settingsPath: serverConfig.settingsPath,
136 });
137 }).pipe(Effect.provide(makeServerSettingsLayer())),
138 );
139​
140 it.effect("decodes nested settings patches", () =>
141 Effect.gen(function* () {
142 assert.deepEqual(
143 yield* decodeSettingsPatch({ providers: { codex: { binaryPath: "/tmp/codex" } } }),
144 {
145 providers: { codex: { binaryPath: "/tmp/codex" } },
146 },
147 );
148​
149 assert.deepEqual(
150 yield* decodeSettingsPatch({
151 textGenerationModelSelection: {
152 options: [{ id: "fastMode", value: false }],
153 },
154 }),
155 {
156 textGenerationModelSelection: {
157 options: [{ id: "fastMode", value: false }],
158 },
159 },
160 );
161 }),
162 );
163​
164 it.effect(
165 "decodes legacy object-shaped textGenerationModelSelection.options from settings.json",
166 () =>
167 Effect.gen(function* () {
168 const decoded = yield* decodeServerSettings({
169 textGenerationModelSelection: {
170 provider: ProviderDriverKind.make("codex"),
171 model: "gpt-5.4-mini",
172 options: { reasoningEffort: "low" },
173 },
174 });
175​
176 assert.deepEqual(decoded.textGenerationModelSelection, {
177 instanceId: ProviderInstanceId.make("codex"),
178 model: "gpt-5.4-mini",
179 options: [{ id: "reasoningEffort", value: "low" }],
180 });
181 }),
182 );
183​
184 it.effect("deep merges nested settings updates without dropping siblings", () =>
185 Effect.gen(function* () {
186 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
187​
188 yield* serverSettings.updateSettings({
189 providers: {
190 codex: {
191 binaryPath: "/usr/local/bin/codex",
192 homePath: "/Users/julius/.codex",
193 },
194 claudeAgent: {
195 binaryPath: "/usr/local/bin/claude",
196 customModels: ["claude-custom"],
197 },
198 },
199 textGenerationModelSelection: {
200 instanceId: ProviderInstanceId.make("codex"),
201 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
202 options: createModelSelection(
203 ProviderInstanceId.make("codex"),
204 DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
205 [
206 { id: "reasoningEffort", value: "high" },
207 { id: "fastMode", value: true },
208 ],
209 ).options!,
210 },
211 });
212​
213 const next = yield* serverSettings.updateSettings({
214 providers: {
215 codex: {
216 binaryPath: "/opt/homebrew/bin/codex",
217 },
218 },
219 textGenerationModelSelection: {
220 options: [{ id: "fastMode", value: false }],
221 },
222 });
223​
224 assert.deepEqual(next.providers.codex, {
225 enabled: true,
226 binaryPath: "/opt/homebrew/bin/codex",
227 homePath: "/Users/julius/.codex",
228 shadowHomePath: "",
229 launchArgs: "",
230 customModels: [],
231 });
232 assert.deepEqual(next.providers.claudeAgent, {
233 enabled: true,
234 binaryPath: "/usr/local/bin/claude",
235 homePath: "",
236 customModels: ["claude-custom"],
237 launchArgs: "",
238 autoCompactWindow: "",
239 });
240 assert.deepEqual(
241 next.textGenerationModelSelection,
242 createModelSelection(
243 ProviderInstanceId.make("codex"),
244 DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
245 [
246 { id: "reasoningEffort", value: "high" },
247 { id: "fastMode", value: false },
248 ],
249 ),
250 );
251 }).pipe(Effect.provide(makeServerSettingsLayer())),
252 );
253​
254 it.effect("buffers changes after a subscription is acquired but before it is consumed", () =>
255 Effect.scoped(
256 Effect.gen(function* () {
257 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
258 const changes = yield* serverSettings.subscribeChanges;
259​
260 yield* serverSettings.updateSettings({
261 providers: {
262 codex: {
263 binaryPath: "/usr/local/bin/codex-next",
264 },
265 },
266 });
267​
268 const firstChange = yield* changes.pipe(Stream.runHead, Effect.timeout("1 second"));
269 assert.equal(
270 Option.getOrUndefined(firstChange)?.providers.codex.binaryPath,
271 "/usr/local/bin/codex-next",
272 );
273 }),
274 ).pipe(Effect.provide(makeServerSettingsLayer())),
275 );
276​
277 it.effect("persists custom usage prices and removes them from the settings file", () =>
278 Effect.scoped(
279 Effect.gen(function* () {
280 const serverConfig = yield* ServerConfig.ServerConfig;
281 const fileSystem = yield* FileSystem.FileSystem;
282 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
283 const prices = {
284 inputCostPerMillionTokens: 2,
285 outputCostPerMillionTokens: 8,
286 cacheReadCostPerMillionTokens: 0,
287 };
288 const readPersisted = fileSystem
289 .readFileString(serverConfig.settingsPath)
290 .pipe(Effect.flatMap(Schema.decodeUnknownEffect(Schema.fromJsonString(ServerSettings))));
291​
292 yield* serverSettings.updateSettings({ usagePriceOverrides: { "example-model": prices } });
293 const persisted = yield* readPersisted;
294 assert.deepStrictEqual(persisted.usagePriceOverrides, { "example-model": prices });
295​
296 yield* serverSettings.updateSettings({ usagePriceOverrides: { "example-model": null } });
297 const restored = yield* readPersisted;
298 assert.deepStrictEqual(restored.usagePriceOverrides, {});
299 }),
300 ).pipe(Effect.provide(makeServerSettingsLayer())),
301 );
302​
303 it.effect("persists and broadcasts thread settlement settings", () =>
304 Effect.scoped(
305 Effect.gen(function* () {
306 const serverConfig = yield* ServerConfig.ServerConfig;
307 const fileSystem = yield* FileSystem.FileSystem;
308 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
309 const changes = yield* serverSettings.subscribeChanges;
310​
311 const next = yield* serverSettings.updateSettings({
312 sidebarAutoSettleAfterDays: null,
313 sidebarAutoSettleOnMerge: false,
314 });
315 const change = Option.getOrUndefined(yield* Stream.runHead(changes));
316 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
317 // Inspect raw persisted JSON before schema decoding can apply defaults.
318 // @effect-diagnostics-next-line preferSchemaOverJson:off
319 const persisted = JSON.parse(raw) as Record<string, unknown>;
320​
321 assert.strictEqual(next.sidebarAutoSettleAfterDays, null);
322 assert.isFalse(next.sidebarAutoSettleOnMerge);
323 assert.strictEqual(change?.sidebarAutoSettleAfterDays, null);
324 assert.isFalse(change?.sidebarAutoSettleOnMerge);
325 assert.strictEqual(persisted.sidebarAutoSettleAfterDays, null);
326 assert.isFalse(persisted.sidebarAutoSettleOnMerge);
327 }),
328 ).pipe(Effect.provide(makeServerSettingsLayer())),
329 );
330​
331 it.effect("preserves model when switching providers via textGenerationModelSelection", () =>
332 Effect.gen(function* () {
333 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
334​
335 // Start with Claude text generation selection
336 yield* serverSettings.updateSettings({
337 textGenerationModelSelection: {
338 instanceId: ProviderInstanceId.make("claudeAgent"),
339 model: "claude-sonnet-4-6",
340 options: createModelSelection(
341 ProviderInstanceId.make("claudeAgent"),
342 "claude-sonnet-4-6",
343 [{ id: "effort", value: "high" }],
344 ).options!,
345 },
346 });
347​
348 // Switch to Codex — the stale Claude "effort" in options must not
349 // cause the update to lose the selected model.
350 const next = yield* serverSettings.updateSettings({
351 textGenerationModelSelection: {
352 instanceId: ProviderInstanceId.make("codex"),
353 model: "gpt-5.4",
354 options: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
355 { id: "reasoningEffort", value: "high" },
356 ]).options!,
357 },
358 });
359​
360 assert.deepEqual(
361 next.textGenerationModelSelection,
362 createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
363 { id: "reasoningEffort", value: "high" },
364 ]),
365 );
366 }).pipe(Effect.provide(makeServerSettingsLayer())),
367 );
368​
369 it.effect("preserves custom provider instance text generation selections", () =>
370 Effect.gen(function* () {
371 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
372​
373 const next = yield* serverSettings.updateSettings({
374 providerInstances: {
375 [ProviderInstanceId.make("claude_openrouter")]: {
376 driver: ProviderDriverKind.make("claudeAgent"),
377 enabled: true,
378 config: { customModels: ["openai/gpt-5.5"] },
379 },
380 },
381 textGenerationModelSelection: {
382 instanceId: ProviderInstanceId.make("claude_openrouter"),
383 model: "openai/gpt-5.5",
384 },
385 });
386​
387 assert.deepEqual(next.textGenerationModelSelection, {
388 instanceId: ProviderInstanceId.make("claude_openrouter"),
389 model: "openai/gpt-5.5",
390 });
391 }).pipe(Effect.provide(makeServerSettingsLayer())),
392 );
393​
394 it.effect(
395 "uses explicit provider instance enabled state over legacy provider enabled state",
396 () =>
397 Effect.gen(function* () {
398 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
399 const instanceId = ProviderInstanceId.make("claude_openrouter");
400​
401 const next = yield* serverSettings.updateSettings({
402 providers: {
403 claudeAgent: {
404 enabled: false,
405 },
406 },
407 providerInstances: {
408 [instanceId]: {
409 driver: ProviderDriverKind.make("claudeAgent"),
410 enabled: true,
411 config: { customModels: ["openai/gpt-5.5"] },
412 },
413 },
414 textGenerationModelSelection: {
415 instanceId,
416 model: "openai/gpt-5.5",
417 },
418 });
419​
420 assert.deepEqual(next.textGenerationModelSelection, {
421 instanceId,
422 model: "openai/gpt-5.5",
423 });
424 }).pipe(Effect.provide(makeServerSettingsLayer())),
425 );
426​
427 it.effect("preserves enabled text generation selections for non-built-in drivers", () =>
428 Effect.gen(function* () {
429 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
430 const instanceId = ProviderInstanceId.make("openrouter_text");
431​
432 const next = yield* serverSettings.updateSettings({
433 providerInstances: {
434 [instanceId]: {
435 driver: ProviderDriverKind.make("openrouter"),
436 enabled: true,
437 config: { customModels: ["openai/gpt-5.5"] },
438 },
439 },
440 textGenerationModelSelection: {
441 instanceId,
442 model: "openai/gpt-5.5",
443 },
444 });
445​
446 assert.deepEqual(next.textGenerationModelSelection, {
447 instanceId,
448 model: "openai/gpt-5.5",
449 });
450 }).pipe(Effect.provide(makeServerSettingsLayer())),
451 );
452​
453 it.effect(
454 "preserves the source control writer selection when its provider instance is disabled",
455 () =>
456 Effect.gen(function* () {
457 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
458 const serverConfig = yield* ServerConfig.ServerConfig;
459 const fileSystem = yield* FileSystem.FileSystem;
460 const instanceId = ProviderInstanceId.make("codex_writer");
461 const sourceControlWriterModelSelection = {
462 instanceId,
463 model: "gpt-5.4-mini",
464 };
465​
466 yield* serverSettings.updateSettings({
467 providerInstances: {
468 [instanceId]: {
469 driver: ProviderDriverKind.make("codex"),
470 enabled: true,
471 config: {},
472 },
473 },
474 sourceControlWriterModelSelection,
475 });
476​
477 const next = yield* serverSettings.updateSettings({
478 providerInstances: {
479 [instanceId]: {
480 driver: ProviderDriverKind.make("codex"),
481 enabled: false,
482 config: {},
483 },
484 },
485 });
486​
487 assert.deepEqual(next.sourceControlWriterModelSelection, sourceControlWriterModelSelection);
488 assert.deepEqual(
489 ServerSettingsModule.resolveSourceControlWriterModelSelection(next),
490 next.textGenerationModelSelection,
491 );
492 assert.deepEqual(
493 (yield* serverSettings.getSettings).sourceControlWriterModelSelection,
494 sourceControlWriterModelSelection,
495 );
496​
497 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
498 assert.deepEqual(
499 // @effect-diagnostics-next-line preferSchemaOverJson:off
500 JSON.parse(raw).sourceControlWriterModelSelection,
501 sourceControlWriterModelSelection,
502 );
503​
504 const restored = yield* serverSettings.updateSettings({
505 providerInstances: {
506 [instanceId]: {
507 driver: ProviderDriverKind.make("codex"),
508 enabled: true,
509 config: {},
510 },
511 },
512 });
513 assert.deepEqual(
514 ServerSettingsModule.resolveSourceControlWriterModelSelection(restored),
515 sourceControlWriterModelSelection,
516 );
517 }).pipe(Effect.provide(makeServerSettingsLayer())),
518 );
519​
520 it.effect("drops stale text generation options when resetting model selection", () =>
521 Effect.gen(function* () {
522 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
523​
524 yield* serverSettings.updateSettings({
525 textGenerationModelSelection: {
526 instanceId: ProviderInstanceId.make("codex"),
527 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
528 options: createModelSelection(
529 ProviderInstanceId.make("codex"),
530 DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
531 [
532 { id: "reasoningEffort", value: "high" },
533 { id: "fastMode", value: true },
534 ],
535 ).options!,
536 },
537 });
538​
539 const next = yield* serverSettings.updateSettings({
540 textGenerationModelSelection: {
541 instanceId: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.instanceId,
542 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
543 },
544 });
545​
546 assert.deepEqual(next.textGenerationModelSelection, {
547 instanceId: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.instanceId,
548 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
549 });
550 }).pipe(Effect.provide(makeServerSettingsLayer())),
551 );
552​
553 it.effect("replaces provider instance maps when clearing optional fields", () =>
554 Effect.gen(function* () {
555 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
556 const codexId = ProviderInstanceId.make("codex");
557​
558 yield* serverSettings.updateSettings({
559 providerInstances: {
560 [codexId]: {
561 driver: ProviderDriverKind.make("codex"),
562 displayName: "Codex Work",
563 accentColor: "#7c3aed",
564 enabled: true,
565 config: { homePath: "~/.codex" },
566 },
567 },
568 });
569​
570 const next = yield* serverSettings.updateSettings({
571 providerInstances: {
572 [codexId]: {
573 driver: ProviderDriverKind.make("codex"),
574 displayName: "Codex Work",
575 enabled: true,
576 config: { homePath: "~/.codex" },
577 },
578 },
579 });
580​
581 assert.deepEqual(next.providerInstances[codexId], {
582 driver: ProviderDriverKind.make("codex"),
583 displayName: "Codex Work",
584 enabled: true,
585 config: { homePath: "~/.codex" },
586 });
587 }).pipe(Effect.provide(makeServerSettingsLayer())),
588 );
589​
590 it.effect("enables previously used providers from sparse settings files", () =>
591 Effect.gen(function* () {
592 const serverConfig = yield* ServerConfig.ServerConfig;
593 const fileSystem = yield* FileSystem.FileSystem;
594 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
595 yield* fileSystem.writeFileString(
596 serverConfig.settingsPath,
597 '{"providers":{"opencode":{"serverUrl":"http://127.0.0.1:4096"}}}',
598 );
599 yield* recordProviderUsage("opencode");
600​
601 const settings = yield* serverSettings.getSettings;
602​
603 assert.isFalse(settings.providers.grok.enabled);
604 assert.isTrue(settings.providers.opencode.enabled);
605 assert.isFalse(settings.providers.cursor.enabled);
606 assert.equal(settings.providers.opencode.serverUrl, "http://127.0.0.1:4096");
607 }).pipe(Effect.provide(makeServerSettingsLayer())),
608 );
609​
610 it.effect("preserves existing provider instances without explicit enabled flags", () =>
611 Effect.gen(function* () {
612 const serverConfig = yield* ServerConfig.ServerConfig;
613 const fileSystem = yield* FileSystem.FileSystem;
614 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
615 yield* fileSystem.writeFileString(
616 serverConfig.settingsPath,
617 '{"providerInstances":{"cursor_work":{"driver":"cursor","config":{}},"grok":{"driver":"grok","config":{}},"hermes":{"driver":"hermes","config":{}},"opencode_work":{"driver":"opencode","config":{"serverUrl":"http://127.0.0.1:4096"}},"opencode_unused":{"driver":"opencode","config":{}}}}',
618 );
619 yield* recordProviderUsage("cursor", "cursor_work");
620 yield* recordProviderUsage("grok", null);
621 yield* recordProviderUsage("hermes", null);
622 yield* recordProviderUsage("opencode", "opencode_work");
623​
624 const settings = yield* serverSettings.getSettings;
625​
626 assert.isTrue(settings.providers.cursor.enabled);
627 assert.isTrue(settings.providerInstances[ProviderInstanceId.make("cursor_work")]?.enabled);
628 assert.isTrue(settings.providerInstances[ProviderInstanceId.make("grok")]?.enabled);
629 assert.isTrue(settings.providerInstances[ProviderInstanceId.make("hermes")]?.enabled);
630 assert.isTrue(settings.providerInstances[ProviderInstanceId.make("opencode_work")]?.enabled);
631 const unused = settings.providerInstances[ProviderInstanceId.make("opencode_unused")];
632 assert.isDefined(unused);
633 assert.isFalse(resolveProviderInstanceEnabled(unused));
634 }).pipe(Effect.provide(makeServerSettingsLayer())),
635 );
636​
637 it.effect("preserves explicit provider disables in existing settings files", () =>
638 Effect.gen(function* () {
639 const serverConfig = yield* ServerConfig.ServerConfig;
640 const fileSystem = yield* FileSystem.FileSystem;
641 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
642 yield* fileSystem.writeFileString(
643 serverConfig.settingsPath,
644 '{"providers":{"grok":{"enabled":false},"hermes":{"enabled":false},"opencode":{"enabled":false},"cursor":{"enabled":false}},"providerInstances":{"grok":{"driver":"grok","enabled":false,"config":{}},"hermes":{"driver":"hermes","enabled":false,"config":{}},"opencode":{"driver":"opencode","config":{"enabled":false}},"cursor":{"driver":"cursor","enabled":false,"config":{}}}}',
645 );
646 yield* recordProviderUsage("grok");
647 yield* recordProviderUsage("hermes");
648 yield* recordProviderUsage("opencode");
649 yield* recordProviderUsage("cursor");
650​
651 const settings = yield* serverSettings.getSettings;
652​
653 assert.isFalse(settings.providers.grok.enabled);
654 assert.isFalse(settings.providers.hermes.enabled);
655 assert.isFalse(settings.providers.opencode.enabled);
656 assert.isFalse(settings.providers.cursor.enabled);
657 assert.isFalse(settings.providerInstances[ProviderInstanceId.make("grok")]?.enabled);
658 assert.isFalse(settings.providerInstances[ProviderInstanceId.make("hermes")]?.enabled);
659 assert.isFalse(settings.providerInstances[ProviderInstanceId.make("opencode")]?.enabled);
660 assert.isFalse(settings.providerInstances[ProviderInstanceId.make("cursor")]?.enabled);
661 }).pipe(Effect.provide(makeServerSettingsLayer())),
662 );
663​
664 it.effect("skips a disabled provider instance when picking the text generation fallback", () =>
665 Effect.gen(function* () {
666 const serverConfig = yield* ServerConfig.ServerConfig;
667 const fileSystem = yield* FileSystem.FileSystem;
668 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
669 // The Providers UI writes providerInstances only, so the legacy providers
670 // map decodes to defaults where codex is enabled and listed first.
671 yield* fileSystem.writeFileString(
672 serverConfig.settingsPath,
673 '{"providerInstances":{"codex":{"driver":"codex","enabled":false,"config":{}}}}',
674 );
675​
676 const settings = yield* serverSettings.getSettings;
677​
678 assert.equal(settings.textGenerationModelSelection.instanceId, "claudeAgent");
679 }).pipe(Effect.provide(makeServerSettingsLayer())),
680 );
681​
682 it.effect("keeps unused providers disabled in existing sparse settings files", () =>
683 Effect.gen(function* () {
684 const serverConfig = yield* ServerConfig.ServerConfig;
685 const fileSystem = yield* FileSystem.FileSystem;
686 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
687 yield* fileSystem.writeFileString(serverConfig.settingsPath, "{}");
688​
689 const settings = yield* serverSettings.getSettings;
690​
691 assert.isFalse(settings.providers.grok.enabled);
692 assert.isFalse(settings.providers.opencode.enabled);
693 assert.isFalse(settings.providers.cursor.enabled);
694 }).pipe(Effect.provide(makeServerSettingsLayer())),
695 );
696​
697 it.effect("preserves provider history when no settings file exists", () =>
698 Effect.gen(function* () {
699 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
700 yield* recordProviderUsage("grok");
701​
702 const settings = yield* serverSettings.getSettings;
703​
704 assert.isTrue(settings.providers.grok.enabled);
705 assert.isFalse(settings.providers.opencode.enabled);
706 assert.isFalse(settings.providers.cursor.enabled);
707 }).pipe(Effect.provide(makeServerSettingsLayer())),
708 );
709​
710 it.effect("restores hermes from provider history when no settings file exists", () =>
711 Effect.gen(function* () {
712 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
713 yield* recordProviderUsage("hermes");
714​
715 const settings = yield* serverSettings.getSettings;
716​
717 assert.isTrue(settings.providers.hermes.enabled);
718 assert.isFalse(settings.providers.grok.enabled);
719 assert.isFalse(settings.providers.opencode.enabled);
720 assert.isFalse(settings.providers.cursor.enabled);
721 }).pipe(Effect.provide(makeServerSettingsLayer())),
722 );
723​
724 it.effect("preserves provider history when the settings file is invalid", () =>
725 Effect.gen(function* () {
726 const serverConfig = yield* ServerConfig.ServerConfig;
727 const fileSystem = yield* FileSystem.FileSystem;
728 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
729 yield* fileSystem.writeFileString(serverConfig.settingsPath, "{invalid json");
730 yield* recordProviderUsage("cursor");
731​
732 const settings = yield* serverSettings.getSettings;
733​
734 assert.isTrue(settings.providers.cursor.enabled);
735 assert.isFalse(settings.providers.grok.enabled);
736 assert.isFalse(settings.providers.opencode.enabled);
737 }).pipe(Effect.provide(makeServerSettingsLayer())),
738 );
739​
740 it.effect("preserves valid provider flags when another settings field is invalid", () =>
741 Effect.gen(function* () {
742 const serverConfig = yield* ServerConfig.ServerConfig;
743 const fileSystem = yield* FileSystem.FileSystem;
744 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
745 yield* fileSystem.writeFileString(
746 serverConfig.settingsPath,
747 '{"addProjectBaseDirectory":42,"providers":{"cursor":{"enabled":false},"grok":{"enabled":true}}}',
748 );
749 yield* recordProviderUsage("cursor");
750​
751 const settings = yield* serverSettings.getSettings;
752​
753 assert.isFalse(settings.providers.cursor.enabled);
754 assert.isTrue(settings.providers.grok.enabled);
755 assert.isFalse(settings.providers.opencode.enabled);
756 }).pipe(Effect.provide(makeServerSettingsLayer())),
757 );
758​
759 it.effect("restores providers from persisted runtime sessions", () =>
760 Effect.gen(function* () {
761 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
762 const sql = yield* SqlClient.SqlClient;
763 yield* sql`
764 INSERT INTO provider_session_runtime (
765 thread_id,
766 provider_name,
767 provider_instance_id,
768 adapter_key,
769 status,
770 last_seen_at
771 )
772 VALUES (
773 ${"thread-opencode-runtime"},
774 ${"opencode"},
775 ${"opencode"},
776 ${"opencode"},
777 ${"ready"},
778 ${"2026-08-25T00:00:00.000Z"}
779 )
780 `;
781​
782 const settings = yield* serverSettings.getSettings;
783​
784 assert.isFalse(settings.providers.grok.enabled);
785 assert.isTrue(settings.providers.opencode.enabled);
786 assert.isFalse(settings.providers.cursor.enabled);
787 }).pipe(Effect.provide(makeServerSettingsLayer())),
788 );
789​
790 it.effect("falls back to the Hermes session model when only Hermes is enabled", () =>
791 Effect.gen(function* () {
792 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
793​
794 const settings = yield* serverSettings.updateSettings({
795 providers: {
796 codex: { enabled: false },
797 claudeAgent: { enabled: false },
798 hermes: { enabled: true },
799 },
800 });
801​
802 // "default" is Hermes's product slug, not an ACP model id: the adapter
803 // skips session/set_model for it and keeps Hermes's configured model.
804 // Without a DEFAULT_MODEL_BY_PROVIDER entry the fallback would hand
805 // Hermes the Codex text-generation slug, which Hermes rejects.
806 assert.deepEqual(settings.textGenerationModelSelection.instanceId, "hermes");
807 assert.deepEqual(settings.textGenerationModelSelection.model, "default");
808 }).pipe(Effect.provide(makeServerSettingsLayer())),
809 );
810​
811 it.effect("persists explicit disables after a provider has been used", () =>
812 Effect.gen(function* () {
813 const serverConfig = yield* ServerConfig.ServerConfig;
814 const fileSystem = yield* FileSystem.FileSystem;
815 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
816 yield* recordProviderUsage("grok");
817​
818 assert.isTrue((yield* serverSettings.getSettings).providers.grok.enabled);
819​
820 const settings = yield* serverSettings.updateSettings({
821 providers: { grok: { enabled: false } },
822 });
823 assert.isFalse(settings.providers.grok.enabled);
824​
825 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
826 // @effect-diagnostics-next-line preferSchemaOverJson:off
827 assert.isFalse(JSON.parse(raw).providers.grok.enabled);
828 }).pipe(Effect.provide(makeServerSettingsLayer())),
829 );
830​
831 it.effect("persists explicit provider enables before their first use", () =>
832 Effect.gen(function* () {
833 const serverConfig = yield* ServerConfig.ServerConfig;
834 const fileSystem = yield* FileSystem.FileSystem;
835 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
836​
837 yield* serverSettings.updateSettings({
838 providers: {
839 cursor: { enabled: true },
840 grok: { enabled: true },
841 opencode: { enabled: true },
842 },
843 });
844 yield* serverSettings.updateSettings({ addProjectBaseDirectory: "~/Development" });
845​
846 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
847 // @effect-diagnostics-next-line preferSchemaOverJson:off
848 const persisted = JSON.parse(raw);
849 assert.isTrue(persisted.providers.cursor.enabled);
850 assert.isTrue(persisted.providers.grok.enabled);
851 assert.isTrue(persisted.providers.opencode.enabled);
852 }).pipe(Effect.provide(makeServerSettingsLayer())),
853 );
854​
855 it.effect("keeps optional providers disabled after a new installation writes settings", () =>
856 Effect.gen(function* () {
857 const serverConfig = yield* ServerConfig.ServerConfig;
858 const fileSystem = yield* FileSystem.FileSystem;
859 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
860​
861 const initial = yield* serverSettings.getSettings;
862 assert.isFalse(initial.providers.grok.enabled);
863 assert.isFalse(initial.providers.opencode.enabled);
864 assert.isFalse(initial.providers.cursor.enabled);
865​
866 const next = yield* serverSettings.updateSettings({
867 addProjectBaseDirectory: "~/Development",
868 providerInstances: {
869 [ProviderInstanceId.make("grok")]: {
870 driver: ProviderDriverKind.make("grok"),
871 config: {},
872 },
873 },
874 });
875​
876 assert.isFalse(next.providers.grok.enabled);
877 assert.isFalse(next.providers.opencode.enabled);
878 assert.isFalse(next.providers.cursor.enabled);
879 const grok = next.providerInstances[ProviderInstanceId.make("grok")];
880 assert.isDefined(grok);
881 assert.isFalse(resolveProviderInstanceEnabled(grok));
882​
883 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
884 // @effect-diagnostics-next-line preferSchemaOverJson:off
885 const persisted = JSON.parse(raw);
886 assert.isFalse(persisted.providers.cursor.enabled);
887 assert.isFalse(persisted.providers.grok.enabled);
888 assert.isFalse(persisted.providers.opencode.enabled);
889 assert.isUndefined(persisted.providerInstances.grok.enabled);
890 }).pipe(Effect.provide(makeServerSettingsLayer())),
891 );
892​
893 it.effect("folds a legacy in-config enabled flag into the envelope on load", () =>
894 Effect.gen(function* () {
895 const serverConfig = yield* ServerConfig.ServerConfig;
896 const fileSystem = yield* FileSystem.FileSystem;
897 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
898 // Old settings files can carry both flags with conflicting values.
899 // The explicit false must win so a user's disable sticks.
900 yield* fileSystem.writeFileString(
901 serverConfig.settingsPath,
902 '{"providerInstances":{"grok":{"driver":"grok","enabled":true,"config":{"enabled":false}},"codex_work":{"driver":"codex","config":{"enabled":true,"homePath":"~/.codex"}},"cursor":{"driver":"cursor","config":{"enabled":"nope"}}}}',
903 );
904​
905 const settings = yield* serverSettings.getSettings;
906​
907 const grokId = ProviderInstanceId.make("grok");
908 const codexWorkId = ProviderInstanceId.make("codex_work");
909 assert.deepEqual(settings.providerInstances[grokId], {
910 driver: ProviderDriverKind.make("grok"),
911 enabled: false,
912 config: {},
913 });
914 // A lone in-config flag is lifted to the envelope and stripped.
915 assert.deepEqual(settings.providerInstances[codexWorkId], {
916 driver: ProviderDriverKind.make("codex"),
917 enabled: true,
918 config: { homePath: "~/.codex" },
919 });
920 // A malformed flag is left alone so driver schema validation can
921 // surface it instead of the fold silently repairing the config.
922 assert.deepEqual(settings.providerInstances[ProviderInstanceId.make("cursor")], {
923 driver: ProviderDriverKind.make("cursor"),
924 config: { enabled: "nope" },
925 });
926 }).pipe(Effect.provide(makeServerSettingsLayer())),
927 );
928​
929 it.effect("folds in-config enabled flags arriving through updates", () =>
930 Effect.gen(function* () {
931 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
932 const grokId = ProviderInstanceId.make("grok");
933​
934 const next = yield* serverSettings.updateSettings({
935 providerInstances: {
936 [grokId]: {
937 driver: ProviderDriverKind.make("grok"),
938 enabled: true,
939 config: { enabled: false, binaryPath: "/opt/grok" },
940 },
941 },
942 });
943​
944 assert.deepEqual(next.providerInstances[grokId], {
945 driver: ProviderDriverKind.make("grok"),
946 enabled: false,
947 config: { binaryPath: "/opt/grok" },
948 });
949 }).pipe(Effect.provide(makeServerSettingsLayer())),
950 );
951​
952 it.effect("trims provider path settings when updates are applied", () =>
953 Effect.gen(function* () {
954 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
955​
956 const next = yield* serverSettings.updateSettings({
957 providers: {
958 codex: {
959 binaryPath: " /opt/homebrew/bin/codex ",
960 homePath: " ",
961 },
962 claudeAgent: {
963 binaryPath: " /opt/homebrew/bin/claude ",
964 },
965 opencode: {
966 binaryPath: " /opt/homebrew/bin/opencode ",
967 serverUrl: " http://127.0.0.1:4096 ",
968 serverPassword: " secret-password ",
969 },
970 },
971 });
972​
973 assert.deepEqual(next.providers.codex, {
974 enabled: true,
975 binaryPath: "/opt/homebrew/bin/codex",
976 homePath: "",
977 shadowHomePath: "",
978 launchArgs: "",
979 customModels: [],
980 });
981 assert.deepEqual(next.providers.claudeAgent, {
982 enabled: true,
983 binaryPath: "/opt/homebrew/bin/claude",
984 homePath: "",
985 customModels: [],
986 launchArgs: "",
987 autoCompactWindow: "",
988 });
989 assert.deepEqual(next.providers.opencode, {
990 // OpenCode is disabled by default; this update only touches paths.
991 enabled: false,
992 binaryPath: "/opt/homebrew/bin/opencode",
993 serverUrl: "http://127.0.0.1:4096",
994 serverPassword: "secret-password",
995 customModels: [],
996 });
997 }).pipe(Effect.provide(makeServerSettingsLayer())),
998 );
999​
1000 it.effect("trims observability settings when updates are applied", () =>
1001 Effect.gen(function* () {
1002 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1003​
1004 const next = yield* serverSettings.updateSettings({
1005 addProjectBaseDirectory: " ~/Development ",
1006 observability: {
1007 otlpTracesUrl: " http://localhost:4318/v1/traces ",
1008 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
1009 },
1010 });
1011​
1012 assert.equal(next.addProjectBaseDirectory, "~/Development");
1013 assert.deepEqual(next.observability, {
1014 otlpTracesUrl: "http://localhost:4318/v1/traces",
1015 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
1016 });
1017 }).pipe(Effect.provide(makeServerSettingsLayer())),
1018 );
1019​
1020 it.effect("defaults blank binary paths to provider executables", () =>
1021 Effect.gen(function* () {
1022 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1023​
1024 const next = yield* serverSettings.updateSettings({
1025 providers: {
1026 codex: {
1027 binaryPath: " ",
1028 },
1029 claudeAgent: {
1030 binaryPath: "",
1031 },
1032 },
1033 });
1034​
1035 assert.equal(next.providers.codex.binaryPath, "codex");
1036 assert.equal(next.providers.claudeAgent.binaryPath, "claude");
1037 }).pipe(Effect.provide(makeServerSettingsLayer())),
1038 );
1039​
1040 it.effect("writes non-default settings and explicit optional provider defaults to disk", () =>
1041 Effect.gen(function* () {
1042 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1043 const serverConfig = yield* ServerConfig.ServerConfig;
1044 const fileSystem = yield* FileSystem.FileSystem;
1045 const next = yield* serverSettings.updateSettings({
1046 addProjectBaseDirectory: "~/Development",
1047 observability: {
1048 otlpTracesUrl: "http://localhost:4318/v1/traces",
1049 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
1050 },
1051 providers: {
1052 codex: {
1053 binaryPath: "/opt/homebrew/bin/codex",
1054 },
1055 opencode: {
1056 serverUrl: "http://127.0.0.1:4096",
1057 serverPassword: "secret-password",
1058 },
1059 },
1060 automaticGitFetchInterval: Duration.seconds(10),
1061 });
1062​
1063 assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
1064​
1065 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1066 // @effect-diagnostics-next-line preferSchemaOverJson:off
1067 assert.deepEqual(JSON.parse(raw), {
1068 addProjectBaseDirectory: "~/Development",
1069 observability: {
1070 otlpTracesUrl: "http://localhost:4318/v1/traces",
1071 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
1072 },
1073 providers: {
1074 codex: {
1075 binaryPath: "/opt/homebrew/bin/codex",
1076 },
1077 cursor: {
1078 enabled: false,
1079 },
1080 grok: {
1081 enabled: false,
1082 },
1083 hermes: {
1084 enabled: false,
1085 },
1086 opencode: {
1087 enabled: false,
1088 serverUrl: "http://127.0.0.1:4096",
1089 serverPassword: "secret-password",
1090 },
1091 },
1092 backgroundActivity: {
1093 schemaVersion: 1,
1094 profile: "custom",
1095 baseProfile: "balanced",
1096 overrides: {
1097 automaticGitFetchInterval: 10_000,
1098 },
1099 },
1100 automaticGitFetchInterval: 10_000,
1101 });
1102 }).pipe(Effect.provide(makeServerSettingsLayer())),
1103 );
1104​
1105 it.effect("keeps the inline value on disk when secret migration fails", () => {
1106 const cause = new ServerSecretStore.SecretStorePersistError({
1107 resource: "provider environment secret",
1108 cause: new Error("Secret storage unavailable"),
1109 });
1110 const secretLayer = Layer.effect(
1111 ServerSecretStore.ServerSecretStore,
1112 Effect.map(ServerSecretStore.ServerSecretStore, (store) => ({
1113 ...store,
1114 set: () => Effect.fail(cause),
1115 })),
1116 ).pipe(Layer.provide(ServerSecretStore.layer));
1117 const settingsLayer = ServerSettingsModule.layer.pipe(
1118 Layer.provide(secretLayer),
1119 Layer.provideMerge(Layer.fresh(SqlitePersistenceMemory)),
1120 Layer.provideMerge(
1121 Layer.fresh(
1122 ServerConfig.layerTest(process.cwd(), {
1123 prefix: "t3code-inline-secret-failure-test-",
1124 }),
1125 ),
1126 ),
1127 );
1128 return Effect.gen(function* () {
1129 const instanceId = ProviderInstanceId.make("codex_personal");
1130 const service = yield* ServerSettingsModule.ServerSettingsService;
1131 const config = yield* ServerConfig.ServerConfig;
1132 const fs = yield* FileSystem.FileSystem;
1133 const original =
1134 '{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"API_TOKEN","value":"inline-test-token","sensitive":true}],"config":{}}}}';
1135 yield* fs.writeFileString(config.settingsPath, original);
1136 const error = yield* Effect.flip(
1137 service.updateSettings({
1138 providerInstances: {
1139 [instanceId]: {
1140 driver: ProviderDriverKind.make("codex"),
1141 environment: [{ name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true }],
1142 config: {},
1143 },
1144 },
1145 }),
1146 );
1147 assert.equal(error.operation, "write-secret");
1148 assert.strictEqual(error.cause, cause);
1149 assert.equal(yield* fs.readFileString(config.settingsPath), original);
1150 const settings = yield* service.getSettings;
1151 assert.equal(
1152 settings.providerInstances[instanceId]?.environment?.[0]?.value,
1153 "inline-test-token",
1154 );
1155 }).pipe(Effect.provide(settingsLayer));
1156 });
1157​
1158 for (const { label, variable, expected, duplicate } of [
1160 label: "preserves an inline secret on a redacted settings save",
1161 variable: { name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true },
1162 expected: "inline-test-token",
1163 },
1165 label: "preserves the effective last inline secret when names are duplicated",
1166 variable: { name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true },
1167 expected: "last-inline-test-token",
1168 duplicate: true,
1169 },
1171 label: "replaces an inline secret with an explicit value",
1172 variable: { name: "API_TOKEN", value: "replacement-test-token", sensitive: true },
1173 expected: "replacement-test-token",
1174 },
1176 label: "clears an inline secret with an explicit empty value",
1177 variable: { name: "API_TOKEN", value: "", sensitive: true },
1178 expected: "",
1179 },
1180 ]) {
1181 it.effect(label, () =>
1182 Effect.gen(function* () {
1183 const instanceId = ProviderInstanceId.make("codex_personal");
1184 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1185 const serverConfig = yield* ServerConfig.ServerConfig;
1186 const fileSystem = yield* FileSystem.FileSystem;
1187 yield* fileSystem.writeFileString(
1188 serverConfig.settingsPath,
1189 duplicate
1190 ? '{"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":{}}}}'
1191 : '{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"API_TOKEN","value":"inline-test-token","sensitive":true}],"config":{}}}}',
1192 );
1193 const initial = yield* serverSettings.getSettings;
1194 assert.equal(
1195 initial.providerInstances[instanceId]?.environment?.[0]?.value,
1196 "inline-test-token",
1197 );
1198​
1199 const next = yield* serverSettings.updateSettings({
1200 providerInstances: {
1201 [instanceId]: {
1202 driver: ProviderDriverKind.make("codex"),
1203 displayName: "Renamed provider",
1204 environment: duplicate ? [variable, variable] : [variable],
1205 config: {},
1206 },
1207 },
1208 });
1209 assert.equal(next.providerInstances[instanceId]?.environment?.[0]?.value, expected);
1210 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1211 assert.notInclude(raw, "inline-test-token");
1212 assert.notInclude(raw, "replacement-test-token");
1213​
1214 const reloaded = yield* Effect.gen(function* () {
1215 const fresh = yield* ServerSettingsModule.ServerSettingsService;
1216 return yield* fresh.getSettings;
1217 }).pipe(
1218 Effect.provide(
1219 Layer.fresh(ServerSettingsModule.layer).pipe(Layer.provide(ServerSecretStore.layer)),
1220 ),
1221 );
1222 assert.equal(reloaded.providerInstances[instanceId]?.environment?.[0]?.value, expected);
1223 }).pipe(Effect.provide(makeServerSettingsLayer())),
1224 );
1226​
1227 it.effect("stores sensitive provider instance environment values outside settings.json", () =>
1228 Effect.gen(function* () {
1229 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1230 const serverConfig = yield* ServerConfig.ServerConfig;
1231 const fileSystem = yield* FileSystem.FileSystem;
1232 const instanceId = ProviderInstanceId.make("codex_personal");
1233​
1234 const next = yield* serverSettings.updateSettings({
1235 providerInstances: {
1236 [instanceId]: {
1237 driver: ProviderDriverKind.make("codex"),
1238 environment: [
1239 { name: "OPENROUTER_API_KEY", value: "sk-or-secret", sensitive: true },
1240 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
1241 ],
1242 config: {},
1243 },
1244 },
1245 });
1246​
1247 assert.deepEqual(next.providerInstances[instanceId]?.environment, [
1249 name: "OPENROUTER_API_KEY",
1250 value: "sk-or-secret",
1251 sensitive: true,
1252 valueRedacted: true,
1253 },
1254 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
1255 ]);
1256​
1257 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1258 assert.notInclude(raw, "sk-or-secret");
1259 // @effect-diagnostics-next-line preferSchemaOverJson:off
1260 assert.deepEqual(JSON.parse(raw).providerInstances.codex_personal.environment, [
1262 name: "OPENROUTER_API_KEY",
1263 value: "",
1264 sensitive: true,
1265 valueRedacted: true,
1266 },
1267 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
1268 ]);
1269​
1270 const roundTripped = yield* serverSettings.updateSettings({
1271 providerInstances: {
1272 [instanceId]: {
1273 driver: ProviderDriverKind.make("codex"),
1274 displayName: "Codex Personal",
1275 environment: [
1276 { name: "OPENROUTER_API_KEY", value: "", sensitive: true, valueRedacted: true },
1277 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
1278 ],
1279 config: {},
1280 },
1281 },
1282 });
1283​
1284 assert.equal(
1285 roundTripped.providerInstances[instanceId]?.environment?.[0]?.value,
1286 "sk-or-secret",
1287 );
1288 }).pipe(Effect.provide(makeServerSettingsLayer())),
1289 );
1290​
1291 it.effect("materializes provider secrets for terminal environment resolution", () =>
1292 Effect.gen(function* () {
1293 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1294 const serverConfig = yield* ServerConfig.ServerConfig;
1295 const fileSystem = yield* FileSystem.FileSystem;
1296 const path = yield* Path.Path;
1297 const instanceId = ProviderInstanceId.make("codex_terminal");
1298​
1299 yield* serverSettings.updateSettings({
1300 providerInstances: {
1301 [instanceId]: {
1302 driver: ProviderDriverKind.make("codex"),
1303 environment: [
1304 { name: "OPENROUTER_API_KEY", value: "sk-terminal-secret", sensitive: true },
1305 ],
1306 config: { homePath: "~/.codex-terminal" },
1307 },
1308 },
1309 });
1310​
1311 const environment = yield* resolveProviderInstanceTerminalEnvironment({
1312 serverSettings,
1313 path,
1314 rawProviderInstanceId: instanceId,
1315 env: undefined,
1316 });
1317 const persisted = yield* fileSystem.readFileString(serverConfig.settingsPath);
1318​
1319 assert.equal(environment.OPENROUTER_API_KEY, "sk-terminal-secret");
1320 assert.match(environment.CODEX_HOME ?? "", /[\\/][.]codex-terminal$/);
1321 assert.notInclude(persisted, "sk-terminal-secret");
1322 assert.include(persisted, '"valueRedacted": true');
1323 }).pipe(Effect.provide(makeServerSettingsLayer())),
1324 );
1325});