Open History to see the last commit
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":{}},"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("opencode", "opencode_work");
622
623 const settings = yield* serverSettings.getSettings;
624
625 assert.isTrue(settings.providers.cursor.enabled);
626 assert.isTrue(settings.providerInstances[ProviderInstanceId.make("cursor_work")]?.enabled);
627 assert.isTrue(settings.providerInstances[ProviderInstanceId.make("grok")]?.enabled);
628 assert.isTrue(settings.providerInstances[ProviderInstanceId.make("opencode_work")]?.enabled);
629 const unused = settings.providerInstances[ProviderInstanceId.make("opencode_unused")];
630 assert.isDefined(unused);
631 assert.isFalse(resolveProviderInstanceEnabled(unused));
632 }).pipe(Effect.provide(makeServerSettingsLayer())),
633 );
634
635 it.effect("preserves explicit provider disables in existing settings files", () =>
636 Effect.gen(function* () {
637 const serverConfig = yield* ServerConfig.ServerConfig;
638 const fileSystem = yield* FileSystem.FileSystem;
639 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
640 yield* fileSystem.writeFileString(
641 serverConfig.settingsPath,
642 '{"providers":{"grok":{"enabled":false},"opencode":{"enabled":false},"cursor":{"enabled":false}},"providerInstances":{"grok":{"driver":"grok","enabled":false,"config":{}},"opencode":{"driver":"opencode","config":{"enabled":false}},"cursor":{"driver":"cursor","enabled":false,"config":{}}}}',
643 );
644 yield* recordProviderUsage("grok");
645 yield* recordProviderUsage("opencode");
646 yield* recordProviderUsage("cursor");
647
648 const settings = yield* serverSettings.getSettings;
649
650 assert.isFalse(settings.providers.grok.enabled);
651 assert.isFalse(settings.providers.opencode.enabled);
652 assert.isFalse(settings.providers.cursor.enabled);
653 assert.isFalse(settings.providerInstances[ProviderInstanceId.make("grok")]?.enabled);
654 assert.isFalse(settings.providerInstances[ProviderInstanceId.make("opencode")]?.enabled);
655 assert.isFalse(settings.providerInstances[ProviderInstanceId.make("cursor")]?.enabled);
656 }).pipe(Effect.provide(makeServerSettingsLayer())),
657 );
658
659 it.effect("skips a disabled provider instance when picking the text generation fallback", () =>
660 Effect.gen(function* () {
661 const serverConfig = yield* ServerConfig.ServerConfig;
662 const fileSystem = yield* FileSystem.FileSystem;
663 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
664 // The Providers UI writes providerInstances only, so the legacy providers
665 // map decodes to defaults where codex is enabled and listed first.
666 yield* fileSystem.writeFileString(
667 serverConfig.settingsPath,
668 '{"providerInstances":{"codex":{"driver":"codex","enabled":false,"config":{}}}}',
669 );
670
671 const settings = yield* serverSettings.getSettings;
672
673 assert.equal(settings.textGenerationModelSelection.instanceId, "claudeAgent");
674 }).pipe(Effect.provide(makeServerSettingsLayer())),
675 );
676
677 it.effect("keeps unused providers disabled in existing sparse settings files", () =>
678 Effect.gen(function* () {
679 const serverConfig = yield* ServerConfig.ServerConfig;
680 const fileSystem = yield* FileSystem.FileSystem;
681 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
682 yield* fileSystem.writeFileString(serverConfig.settingsPath, "{}");
683
684 const settings = yield* serverSettings.getSettings;
685
686 assert.isFalse(settings.providers.grok.enabled);
687 assert.isFalse(settings.providers.opencode.enabled);
688 assert.isFalse(settings.providers.cursor.enabled);
689 }).pipe(Effect.provide(makeServerSettingsLayer())),
690 );
691
692 it.effect("preserves provider history when no settings file exists", () =>
693 Effect.gen(function* () {
694 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
695 yield* recordProviderUsage("grok");
696
697 const settings = yield* serverSettings.getSettings;
698
699 assert.isTrue(settings.providers.grok.enabled);
700 assert.isFalse(settings.providers.opencode.enabled);
701 assert.isFalse(settings.providers.cursor.enabled);
702 }).pipe(Effect.provide(makeServerSettingsLayer())),
703 );
704
705 it.effect("preserves provider history when the settings file is invalid", () =>
706 Effect.gen(function* () {
707 const serverConfig = yield* ServerConfig.ServerConfig;
708 const fileSystem = yield* FileSystem.FileSystem;
709 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
710 yield* fileSystem.writeFileString(serverConfig.settingsPath, "{invalid json");
711 yield* recordProviderUsage("cursor");
712
713 const settings = yield* serverSettings.getSettings;
714
715 assert.isTrue(settings.providers.cursor.enabled);
716 assert.isFalse(settings.providers.grok.enabled);
717 assert.isFalse(settings.providers.opencode.enabled);
718 }).pipe(Effect.provide(makeServerSettingsLayer())),
719 );
720
721 it.effect("preserves valid provider flags when another settings field is invalid", () =>
722 Effect.gen(function* () {
723 const serverConfig = yield* ServerConfig.ServerConfig;
724 const fileSystem = yield* FileSystem.FileSystem;
725 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
726 yield* fileSystem.writeFileString(
727 serverConfig.settingsPath,
728 '{"addProjectBaseDirectory":42,"providers":{"cursor":{"enabled":false},"grok":{"enabled":true}}}',
729 );
730 yield* recordProviderUsage("cursor");
731
732 const settings = yield* serverSettings.getSettings;
733
734 assert.isFalse(settings.providers.cursor.enabled);
735 assert.isTrue(settings.providers.grok.enabled);
736 assert.isFalse(settings.providers.opencode.enabled);
737 }).pipe(Effect.provide(makeServerSettingsLayer())),
738 );
739
740 it.effect("restores providers from persisted runtime sessions", () =>
741 Effect.gen(function* () {
742 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
743 const sql = yield* SqlClient.SqlClient;
744 yield* sql`
745 INSERT INTO provider_session_runtime (
746 thread_id,
747 provider_name,
748 provider_instance_id,
749 adapter_key,
750 status,
751 last_seen_at
752 )
753 VALUES (
754 ${"thread-opencode-runtime"},
755 ${"opencode"},
756 ${"opencode"},
757 ${"opencode"},
758 ${"ready"},
759 ${"2026-08-25T00:00:00.000Z"}
760 )
761 `;
762
763 const settings = yield* serverSettings.getSettings;
764
765 assert.isFalse(settings.providers.grok.enabled);
766 assert.isTrue(settings.providers.opencode.enabled);
767 assert.isFalse(settings.providers.cursor.enabled);
768 }).pipe(Effect.provide(makeServerSettingsLayer())),
769 );
770
771 it.effect("persists explicit disables after a provider has been used", () =>
772 Effect.gen(function* () {
773 const serverConfig = yield* ServerConfig.ServerConfig;
774 const fileSystem = yield* FileSystem.FileSystem;
775 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
776 yield* recordProviderUsage("grok");
777
778 assert.isTrue((yield* serverSettings.getSettings).providers.grok.enabled);
779
780 const settings = yield* serverSettings.updateSettings({
781 providers: { grok: { enabled: false } },
782 });
783 assert.isFalse(settings.providers.grok.enabled);
784
785 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
786 // @effect-diagnostics-next-line preferSchemaOverJson:off
787 assert.isFalse(JSON.parse(raw).providers.grok.enabled);
788 }).pipe(Effect.provide(makeServerSettingsLayer())),
789 );
790
791 it.effect("persists explicit provider enables before their first use", () =>
792 Effect.gen(function* () {
793 const serverConfig = yield* ServerConfig.ServerConfig;
794 const fileSystem = yield* FileSystem.FileSystem;
795 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
796
797 yield* serverSettings.updateSettings({
798 providers: {
799 cursor: { enabled: true },
800 devin: { enabled: true },
801 grok: { enabled: true },
802 opencode: { enabled: true },
803 },
804 });
805 yield* serverSettings.updateSettings({ addProjectBaseDirectory: "~/Development" });
806
807 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
808 // @effect-diagnostics-next-line preferSchemaOverJson:off
809 const persisted = JSON.parse(raw);
810 assert.isTrue(persisted.providers.cursor.enabled);
811 assert.isTrue(persisted.providers.devin.enabled);
812 assert.isTrue(persisted.providers.grok.enabled);
813 assert.isTrue(persisted.providers.opencode.enabled);
814 }).pipe(Effect.provide(makeServerSettingsLayer())),
815 );
816
817 it.effect("keeps optional providers disabled after a new installation writes settings", () =>
818 Effect.gen(function* () {
819 const serverConfig = yield* ServerConfig.ServerConfig;
820 const fileSystem = yield* FileSystem.FileSystem;
821 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
822
823 const initial = yield* serverSettings.getSettings;
824 assert.isFalse(initial.providers.grok.enabled);
825 assert.isFalse(initial.providers.opencode.enabled);
826 assert.isFalse(initial.providers.cursor.enabled);
827 assert.isFalse(initial.providers.devin.enabled);
828
829 const next = yield* serverSettings.updateSettings({
830 addProjectBaseDirectory: "~/Development",
831 providerInstances: {
832 [ProviderInstanceId.make("grok")]: {
833 driver: ProviderDriverKind.make("grok"),
834 config: {},
835 },
836 },
837 });
838
839 assert.isFalse(next.providers.grok.enabled);
840 assert.isFalse(next.providers.opencode.enabled);
841 assert.isFalse(next.providers.cursor.enabled);
842 const grok = next.providerInstances[ProviderInstanceId.make("grok")];
843 assert.isDefined(grok);
844 assert.isFalse(resolveProviderInstanceEnabled(grok));
845
846 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
847 // @effect-diagnostics-next-line preferSchemaOverJson:off
848 const persisted = JSON.parse(raw);
849 assert.isFalse(persisted.providers.cursor.enabled);
850 assert.isFalse(persisted.providers.grok.enabled);
851 assert.isFalse(persisted.providers.opencode.enabled);
852 assert.isUndefined(persisted.providerInstances.grok.enabled);
853 }).pipe(Effect.provide(makeServerSettingsLayer())),
854 );
855
856 it.effect("folds a legacy in-config enabled flag into the envelope on load", () =>
857 Effect.gen(function* () {
858 const serverConfig = yield* ServerConfig.ServerConfig;
859 const fileSystem = yield* FileSystem.FileSystem;
860 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
861 // Old settings files can carry both flags with conflicting values.
862 // The explicit false must win so a user's disable sticks.
863 yield* fileSystem.writeFileString(
864 serverConfig.settingsPath,
865 '{"providerInstances":{"grok":{"driver":"grok","enabled":true,"config":{"enabled":false}},"codex_work":{"driver":"codex","config":{"enabled":true,"homePath":"~/.codex"}},"cursor":{"driver":"cursor","config":{"enabled":"nope"}}}}',
866 );
867
868 const settings = yield* serverSettings.getSettings;
869
870 const grokId = ProviderInstanceId.make("grok");
871 const codexWorkId = ProviderInstanceId.make("codex_work");
872 assert.deepEqual(settings.providerInstances[grokId], {
873 driver: ProviderDriverKind.make("grok"),
874 enabled: false,
875 config: {},
876 });
877 // A lone in-config flag is lifted to the envelope and stripped.
878 assert.deepEqual(settings.providerInstances[codexWorkId], {
879 driver: ProviderDriverKind.make("codex"),
880 enabled: true,
881 config: { homePath: "~/.codex" },
882 });
883 // A malformed flag is left alone so driver schema validation can
884 // surface it instead of the fold silently repairing the config.
885 assert.deepEqual(settings.providerInstances[ProviderInstanceId.make("cursor")], {
886 driver: ProviderDriverKind.make("cursor"),
887 config: { enabled: "nope" },
888 });
889 }).pipe(Effect.provide(makeServerSettingsLayer())),
890 );
891
892 it.effect("folds in-config enabled flags arriving through updates", () =>
893 Effect.gen(function* () {
894 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
895 const grokId = ProviderInstanceId.make("grok");
896
897 const next = yield* serverSettings.updateSettings({
898 providerInstances: {
899 [grokId]: {
900 driver: ProviderDriverKind.make("grok"),
901 enabled: true,
902 config: { enabled: false, binaryPath: "/opt/grok" },
903 },
904 },
905 });
906
907 assert.deepEqual(next.providerInstances[grokId], {
908 driver: ProviderDriverKind.make("grok"),
909 enabled: false,
910 config: { binaryPath: "/opt/grok" },
911 });
912 }).pipe(Effect.provide(makeServerSettingsLayer())),
913 );
914
915 it.effect("trims provider path settings when updates are applied", () =>
916 Effect.gen(function* () {
917 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
918
919 const next = yield* serverSettings.updateSettings({
920 providers: {
921 codex: {
922 binaryPath: " /opt/homebrew/bin/codex ",
923 homePath: " ",
924 },
925 claudeAgent: {
926 binaryPath: " /opt/homebrew/bin/claude ",
927 },
928 opencode: {
929 binaryPath: " /opt/homebrew/bin/opencode ",
930 serverUrl: " http://127.0.0.1:4096 ",
931 serverPassword: " secret-password ",
932 },
933 },
934 });
935
936 assert.deepEqual(next.providers.codex, {
937 enabled: true,
938 binaryPath: "/opt/homebrew/bin/codex",
939 homePath: "",
940 shadowHomePath: "",
941 launchArgs: "",
942 customModels: [],
943 });
944 assert.deepEqual(next.providers.claudeAgent, {
945 enabled: true,
946 binaryPath: "/opt/homebrew/bin/claude",
947 homePath: "",
948 customModels: [],
949 launchArgs: "",
950 autoCompactWindow: "",
951 });
952 assert.deepEqual(next.providers.opencode, {
953 // OpenCode is disabled by default; this update only touches paths.
954 enabled: false,
955 binaryPath: "/opt/homebrew/bin/opencode",
956 serverUrl: "http://127.0.0.1:4096",
957 serverPassword: "secret-password",
958 customModels: [],
959 });
960 }).pipe(Effect.provide(makeServerSettingsLayer())),
961 );
962
963 it.effect("trims observability settings when updates are applied", () =>
964 Effect.gen(function* () {
965 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
966
967 const next = yield* serverSettings.updateSettings({
968 addProjectBaseDirectory: " ~/Development ",
969 observability: {
970 otlpTracesUrl: " http://localhost:4318/v1/traces ",
971 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
972 },
973 });
974
975 assert.equal(next.addProjectBaseDirectory, "~/Development");
976 assert.deepEqual(next.observability, {
977 otlpTracesUrl: "http://localhost:4318/v1/traces",
978 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
979 });
980 }).pipe(Effect.provide(makeServerSettingsLayer())),
981 );
982
983 it.effect("defaults blank binary paths to provider executables", () =>
984 Effect.gen(function* () {
985 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
986
987 const next = yield* serverSettings.updateSettings({
988 providers: {
989 codex: {
990 binaryPath: " ",
991 },
992 claudeAgent: {
993 binaryPath: "",
994 },
995 },
996 });
997
998 assert.equal(next.providers.codex.binaryPath, "codex");
999 assert.equal(next.providers.claudeAgent.binaryPath, "claude");
1000 }).pipe(Effect.provide(makeServerSettingsLayer())),
1001 );
1002
1003 it.effect("writes non-default settings and explicit optional provider defaults to disk", () =>
1004 Effect.gen(function* () {
1005 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1006 const serverConfig = yield* ServerConfig.ServerConfig;
1007 const fileSystem = yield* FileSystem.FileSystem;
1008 const next = yield* serverSettings.updateSettings({
1009 addProjectBaseDirectory: "~/Development",
1010 observability: {
1011 otlpTracesUrl: "http://localhost:4318/v1/traces",
1012 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
1013 },
1014 providers: {
1015 codex: {
1016 binaryPath: "/opt/homebrew/bin/codex",
1017 },
1018 opencode: {
1019 serverUrl: "http://127.0.0.1:4096",
1020 serverPassword: "secret-password",
1021 },
1022 },
1023 automaticGitFetchInterval: Duration.seconds(10),
1024 });
1025
1026 assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
1027
1028 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1029 // @effect-diagnostics-next-line preferSchemaOverJson:off
1030 assert.deepEqual(JSON.parse(raw), {
1031 addProjectBaseDirectory: "~/Development",
1032 observability: {
1033 otlpTracesUrl: "http://localhost:4318/v1/traces",
1034 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
1035 },
1036 providers: {
1037 codex: {
1038 binaryPath: "/opt/homebrew/bin/codex",
1039 },
1040 cursor: {
1041 enabled: false,
1042 },
1043 devin: {
1044 enabled: false,
1045 },
1046 grok: {
1047 enabled: false,
1048 },
1049 opencode: {
1050 enabled: false,
1051 serverUrl: "http://127.0.0.1:4096",
1052 serverPassword: "secret-password",
1053 },
1054 },
1055 backgroundActivity: {
1056 schemaVersion: 1,
1057 profile: "custom",
1058 baseProfile: "balanced",
1059 overrides: {
1060 automaticGitFetchInterval: 10_000,
1061 },
1062 },
1063 automaticGitFetchInterval: 10_000,
1064 });
1065 }).pipe(Effect.provide(makeServerSettingsLayer())),
1066 );
1067
1068 it.effect("keeps the inline value on disk when secret migration fails", () => {
1069 const cause = new ServerSecretStore.SecretStorePersistError({
1070 resource: "provider environment secret",
1071 cause: new Error("Secret storage unavailable"),
1072 });
1073 const secretLayer = Layer.effect(
1074 ServerSecretStore.ServerSecretStore,
1075 Effect.map(ServerSecretStore.ServerSecretStore, (store) => ({
1076 ...store,
1077 set: () => Effect.fail(cause),
1078 })),
1079 ).pipe(Layer.provide(ServerSecretStore.layer));
1080 const settingsLayer = ServerSettingsModule.layer.pipe(
1081 Layer.provide(secretLayer),
1082 Layer.provideMerge(Layer.fresh(SqlitePersistenceMemory)),
1083 Layer.provideMerge(
1084 Layer.fresh(
1085 ServerConfig.layerTest(process.cwd(), {
1086 prefix: "t3code-inline-secret-failure-test-",
1087 }),
1088 ),
1089 ),
1090 );
1091 return Effect.gen(function* () {
1092 const instanceId = ProviderInstanceId.make("codex_personal");
1093 const service = yield* ServerSettingsModule.ServerSettingsService;
1094 const config = yield* ServerConfig.ServerConfig;
1095 const fs = yield* FileSystem.FileSystem;
1096 const original =
1097 '{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"API_TOKEN","value":"inline-test-token","sensitive":true}],"config":{}}}}';
1098 yield* fs.writeFileString(config.settingsPath, original);
1099 const error = yield* Effect.flip(
1100 service.updateSettings({
1101 providerInstances: {
1102 [instanceId]: {
1103 driver: ProviderDriverKind.make("codex"),
1104 environment: [{ name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true }],
1105 config: {},
1106 },
1107 },
1108 }),
1109 );
1110 assert.equal(error.operation, "write-secret");
1111 assert.strictEqual(error.cause, cause);
1112 assert.equal(yield* fs.readFileString(config.settingsPath), original);
1113 const settings = yield* service.getSettings;
1114 assert.equal(
1115 settings.providerInstances[instanceId]?.environment?.[0]?.value,
1116 "inline-test-token",
1117 );
1118 }).pipe(Effect.provide(settingsLayer));
1119 });
1120
1121 for (const { label, variable, expected, duplicate } of [
1122 {
1123 label: "preserves an inline secret on a redacted settings save",
1124 variable: { name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true },
1125 expected: "inline-test-token",
1126 },
1127 {
1128 label: "preserves the effective last inline secret when names are duplicated",
1129 variable: { name: "API_TOKEN", value: "", sensitive: true, valueRedacted: true },
1130 expected: "last-inline-test-token",
1131 duplicate: true,
1132 },
1133 {
1134 label: "replaces an inline secret with an explicit value",
1135 variable: { name: "API_TOKEN", value: "replacement-test-token", sensitive: true },
1136 expected: "replacement-test-token",
1137 },
1138 {
1139 label: "clears an inline secret with an explicit empty value",
1140 variable: { name: "API_TOKEN", value: "", sensitive: true },
1141 expected: "",
1142 },
1143 ]) {
1144 it.effect(label, () =>
1145 Effect.gen(function* () {
1146 const instanceId = ProviderInstanceId.make("codex_personal");
1147 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1148 const serverConfig = yield* ServerConfig.ServerConfig;
1149 const fileSystem = yield* FileSystem.FileSystem;
1150 yield* fileSystem.writeFileString(
1151 serverConfig.settingsPath,
1152 duplicate
1153 ? '{"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":{}}}}'
1154 : '{"providerInstances":{"codex_personal":{"driver":"codex","environment":[{"name":"API_TOKEN","value":"inline-test-token","sensitive":true}],"config":{}}}}',
1155 );
1156 const initial = yield* serverSettings.getSettings;
1157 assert.equal(
1158 initial.providerInstances[instanceId]?.environment?.[0]?.value,
1159 "inline-test-token",
1160 );
1161
1162 const next = yield* serverSettings.updateSettings({
1163 providerInstances: {
1164 [instanceId]: {
1165 driver: ProviderDriverKind.make("codex"),
1166 displayName: "Renamed provider",
1167 environment: duplicate ? [variable, variable] : [variable],
1168 config: {},
1169 },
1170 },
1171 });
1172 assert.equal(next.providerInstances[instanceId]?.environment?.[0]?.value, expected);
1173 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1174 assert.notInclude(raw, "inline-test-token");
1175 assert.notInclude(raw, "replacement-test-token");
1176
1177 const reloaded = yield* Effect.gen(function* () {
1178 const fresh = yield* ServerSettingsModule.ServerSettingsService;
1179 return yield* fresh.getSettings;
1180 }).pipe(
1181 Effect.provide(
1182 Layer.fresh(ServerSettingsModule.layer).pipe(Layer.provide(ServerSecretStore.layer)),
1183 ),
1184 );
1185 assert.equal(reloaded.providerInstances[instanceId]?.environment?.[0]?.value, expected);
1186 }).pipe(Effect.provide(makeServerSettingsLayer())),
1187 );
1188 }
1189
1190 it.effect("stores sensitive provider instance environment values outside settings.json", () =>
1191 Effect.gen(function* () {
1192 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1193 const serverConfig = yield* ServerConfig.ServerConfig;
1194 const fileSystem = yield* FileSystem.FileSystem;
1195 const instanceId = ProviderInstanceId.make("codex_personal");
1196
1197 const next = yield* serverSettings.updateSettings({
1198 providerInstances: {
1199 [instanceId]: {
1200 driver: ProviderDriverKind.make("codex"),
1201 environment: [
1202 { name: "OPENROUTER_API_KEY", value: "sk-or-secret", sensitive: true },
1203 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
1204 ],
1205 config: {},
1206 },
1207 },
1208 });
1209
1210 assert.deepEqual(next.providerInstances[instanceId]?.environment, [
1211 {
1212 name: "OPENROUTER_API_KEY",
1213 value: "sk-or-secret",
1214 sensitive: true,
1215 valueRedacted: true,
1216 },
1217 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
1218 ]);
1219
1220 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1221 assert.notInclude(raw, "sk-or-secret");
1222 // @effect-diagnostics-next-line preferSchemaOverJson:off
1223 assert.deepEqual(JSON.parse(raw).providerInstances.codex_personal.environment, [
1224 {
1225 name: "OPENROUTER_API_KEY",
1226 value: "",
1227 sensitive: true,
1228 valueRedacted: true,
1229 },
1230 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
1231 ]);
1232
1233 const roundTripped = yield* serverSettings.updateSettings({
1234 providerInstances: {
1235 [instanceId]: {
1236 driver: ProviderDriverKind.make("codex"),
1237 displayName: "Codex Personal",
1238 environment: [
1239 { name: "OPENROUTER_API_KEY", value: "", sensitive: true, valueRedacted: true },
1240 { name: "ANTHROPIC_BASE_URL", value: "https://openrouter.ai/api", sensitive: false },
1241 ],
1242 config: {},
1243 },
1244 },
1245 });
1246
1247 assert.equal(
1248 roundTripped.providerInstances[instanceId]?.environment?.[0]?.value,
1249 "sk-or-secret",
1250 );
1251 }).pipe(Effect.provide(makeServerSettingsLayer())),
1252 );
1253
1254 it.effect("materializes provider secrets for terminal environment resolution", () =>
1255 Effect.gen(function* () {
1256 const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1257 const serverConfig = yield* ServerConfig.ServerConfig;
1258 const fileSystem = yield* FileSystem.FileSystem;
1259 const path = yield* Path.Path;
1260 const instanceId = ProviderInstanceId.make("codex_terminal");
1261
1262 yield* serverSettings.updateSettings({
1263 providerInstances: {
1264 [instanceId]: {
1265 driver: ProviderDriverKind.make("codex"),
1266 environment: [
1267 { name: "OPENROUTER_API_KEY", value: "sk-terminal-secret", sensitive: true },
1268 ],
1269 config: { homePath: "~/.codex-terminal" },
1270 },
1271 },
1272 });
1273
1274 const environment = yield* resolveProviderInstanceTerminalEnvironment({
1275 serverSettings,
1276 path,
1277 rawProviderInstanceId: instanceId,
1278 env: undefined,
1279 });
1280 const persisted = yield* fileSystem.readFileString(serverConfig.settingsPath);
1281
1282 assert.equal(environment.OPENROUTER_API_KEY, "sk-terminal-secret");
1283 assert.match(environment.CODEX_HOME ?? "", /[\\/][.]codex-terminal$/);
1284 assert.notInclude(persisted, "sk-terminal-secret");
1285 assert.include(persisted, '"valueRedacted": true');
1286 }).pipe(Effect.provide(makeServerSettingsLayer())),
1287 );
1288});