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

scopedSettings.test.ts

784 lines27.5 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 EnvironmentAuthorizationError,
4 EnvironmentId,
5 ProjectId,
6 ProviderInstanceId,
7 type ServerSettings,
8} from "@t3tools/contracts";
9import { describe, expect, it, vi } from "vite-plus/test";
10import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
11import * as Cause from "effect/Cause";
12import { resolveWorktreeCleanup } from "@t3tools/shared/projectSettings";
13​
14import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
15import {
16 persistScopedSettingsPatch,
17 planProjectOverridesClear,
18 planScopedSettingsClear,
19 planScopedSettingsPatch,
20 scopedIdleCompaction,
21 resolveScopedSettingsTargets,
22 scopedSettingsAreMixed,
23 scopedSettingsSource,
24 selectScopedSettingsEnvironments,
25} from "./scopedSettings";
26import { resolveSettingsScope } from "./settingsScope";
27​
28function environment(
29 id: string,
30 options: {
31 connected?: boolean;
32 loaded?: boolean;
33 settings?: Partial<ServerSettings>;
34 } = {},
35) {
36 return {
37 environmentId: EnvironmentId.make(id),
38 label: id,
39 connection: {
40 phase: options.connected === false ? ("offline" as const) : ("connected" as const),
41 },
42 serverConfig:
43 options.loaded === false
44 ? null
45 : {
46 settings: { ...DEFAULT_SERVER_SETTINGS, ...options.settings },
47 },
48 };
50​
51const laptop = environment("Laptop");
52const server = environment("Server");
53const offline = environment("Offline", { connected: false });
54const loading = environment("Loading", { loaded: false });
55const environments = [laptop, server, offline, loading];
56const all = resolveSettingsScope({}, [], environments);
57const named = resolveSettingsScope({ machine: server.environmentId }, [], environments);
58​
59const projectId = ProjectId.make("project");
60const laptopProjectId = ProjectId.make("laptop-project");
61const member = {
62 id: projectId,
63 environmentId: server.environmentId,
64 title: "Project",
65 workspaceRoot: "/repo",
66 physicalProjectKey: `${server.environmentId}:/repo`,
67 environmentLabel: server.label,
68 defaultModelSelection: null,
69 scripts: [],
70 createdAt: "2026-09-07T00:00:00.000Z",
71 updatedAt: "2026-09-07T00:00:00.000Z",
72};
73const laptopMember = {
74 ...member,
75 id: laptopProjectId,
76 environmentId: laptop.environmentId,
77 physicalProjectKey: `${laptop.environmentId}:/repo`,
78 environmentLabel: laptop.label,
79};
80const group: SidebarProjectSnapshot = {
81 ...member,
82 projectKey: "project-group",
83 displayName: "Project",
84 memberProjects: [member, laptopMember],
85 memberProjectRefs: [
86 { environmentId: server.environmentId, projectId },
87 { environmentId: laptop.environmentId, projectId: laptopProjectId },
88 ],
89 groupedProjectCount: 2,
90 environmentPresence: "remote-only",
91 allRemoteMembersAreDesktopLocal: false,
92 allRemoteMembersAreWsl: false,
93 remoteEnvironmentLabels: [server.label, laptop.label],
94};
95const project = resolveSettingsScope({ project: group.projectKey }, [group], environments);
96const checkout = resolveSettingsScope(
97 { project: group.projectKey, machine: server.environmentId, checkout: member.physicalProjectKey },
98 [group],
99 environments,
100);
101​
102describe("scoped settings targets", () => {
103 it("uses the named environment even when a different primary is available", () => {
104 const selected = selectScopedSettingsEnvironments(named, environments, laptop.environmentId);
105 expect(selected.environments).toEqual([server]);
106 expect(selected.environment).toBe(server);
107 });
108​
109 it("keeps an offline named environment selected without falling back to primary", () => {
110 const scope = resolveSettingsScope({ machine: offline.environmentId }, [], environments);
111 const selected = selectScopedSettingsEnvironments(scope, environments, laptop.environmentId);
112 expect(selected.environments).toEqual([offline]);
113 expect(selected.connectedEnvironments).toEqual([]);
114 expect(selected.environment).toBeNull();
115 expect(
116 planScopedSettingsPatch(scope, environments, { enableProviderUpdateChecks: false }),
117 ).toMatchObject({
118 serverWrites: [],
119 unavailableReason: "Connect Offline to save this setting.",
120 });
121 });
122​
123 it("resolves each member's effective settings and source at project scope", () => {
124 const overridden = environment("Server", {
125 settings: {
126 defaultAutoPull: false,
127 projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
128 },
129 });
130 const targets = resolveScopedSettingsTargets(project, [laptop, overridden]);
131 expect(targets.map((target) => [target.projectId, target.settings.defaultAutoPull])).toEqual([
132 [projectId, true],
133 [laptopProjectId, false],
134 ]);
135 expect(scopedSettingsSource(targets, ["defaultAutoPull"])).toBe("mixed");
136 expect(scopedSettingsSource([targets[0]!], ["defaultAutoPull"])).toBe("project");
137 expect(scopedSettingsSource(targets, ["enableProviderUpdateChecks"])).toBe("environment");
138 expect(scopedSettingsAreMixed(targets, ["defaultAutoPull"])).toBe(true);
139 });
140});
141​
142describe("scoped settings writes", () => {
143 it("edits the effective machine policy without changing other machines' rules", () => {
144 const custom = environment("Laptop", {
145 settings: {
146 worktreeCleanup: {
147 mode: "custom",
148 rules: {
149 worktreeAfterDays: 12,
150 worktreeSettledAfterDays: null,
151 worktreeOnDelete: true,
152 worktreeOnMerge: true,
153 worktreeUnchanged: false,
154 worktreeKeepWhen: "uncommitted-changes",
155 },
156 },
157 },
158 });
159 const disabled = environment("Server", {
160 settings: { worktreeCleanup: { mode: "off" } },
161 });
162 const targets = [custom, disabled];
163 const plan = planScopedSettingsPatch(all, targets, {
164 storageCleanup: { worktreeOnDelete: false },
165 });
166 const policies = plan.serverWrites.map((write, index) =>
167 resolveWorktreeCleanup(
168 applyServerSettingsPatch(targets[index]!.serverConfig!.settings, write.patch),
169 null,
170 ),
171 );
172 expect(policies).toEqual([
173 {
174 worktreeAfterDays: 12,
175 worktreeSettledAfterDays: null,
176 worktreeOnDelete: false,
177 worktreeOnMerge: true,
178 worktreeUnchanged: false,
179 worktreeKeepWhen: "uncommitted-changes",
180 },
181 {
182 worktreeAfterDays: null,
183 worktreeSettledAfterDays: null,
184 worktreeOnDelete: false,
185 worktreeOnMerge: false,
186 worktreeUnchanged: false,
187 worktreeKeepWhen: "uncommitted-changes",
188 },
189 ]);
190 });
191​
192 it("compares idle compaction by mode, and offers Never only where every server stores it", () => {
193 const fixed = {
194 settings: {
195 ...DEFAULT_SERVER_SETTINGS,
196 compactIdleThreads: false,
197 idleCompaction: "onReturn" as const,
198 },
199 };
200 const modes = {
201 settings: { ...DEFAULT_SERVER_SETTINGS, compactIdleThreads: "onReturn" as const },
202 };
203 const legacy = { settings: { ...DEFAULT_SERVER_SETTINGS, compactIdleThreads: false } };
204 expect(scopedIdleCompaction([fixed, modes])).toEqual({ mixed: false, neverAvailable: true });
205 expect(scopedIdleCompaction([fixed, legacy])).toEqual({ mixed: false, neverAvailable: false });
206 expect(
207 scopedIdleCompaction([fixed, { settings: { ...fixed.settings, idleCompaction: "off" } }])
208 .mixed,
209 ).toBe(true);
210 });
211​
212 it("sends an idle compaction mode in the form each environment's server reads", () => {
213 const targets = [
214 environment("Fixed", { settings: { compactIdleThreads: false, idleCompaction: "onReturn" } }),
215 environment("Modes", { settings: { compactIdleThreads: "onReturn" } }),
216 environment("Switch", { settings: { compactIdleThreads: false } }),
217 ];
218 const plan = planScopedSettingsPatch(resolveSettingsScope({}, [], targets), targets, {
219 idleCompaction: "beforeCacheExpires",
220 });
221 expect(plan.serverWrites.map((write) => write.patch)).toEqual([
222 { idleCompaction: "beforeCacheExpires" },
223 { compactIdleThreads: "beforeCacheExpires" },
224 { compactIdleThreads: true },
225 ]);
226 });
227​
228 it("writes an aggregate preference only to connected environments with loaded configuration", async () => {
229 const persistServer = vi.fn().mockResolvedValue({ _tag: "Success" });
230 const persistClient = vi.fn();
231 await persistScopedSettingsPatch(
232 planScopedSettingsPatch(all, environments, { enableProviderUpdateChecks: false }),
233 persistServer,
234 persistClient,
235 );
236 expect(persistServer.mock.calls.map(([input]) => input.environmentId)).toEqual([
237 laptop.environmentId,
238 server.environmentId,
239 ]);
240 expect(persistClient).not.toHaveBeenCalled();
241 });
242​
243 describe("an environment that saves but drops keys it does not know", () => {
244 const upgradedLaptop = (saved: Partial<ServerSettings>) =>
245 vi.fn(async ({ environmentId }: { environmentId: EnvironmentId }) => ({
246 _tag: "Success" as const,
247 value: {
248 ...DEFAULT_SERVER_SETTINGS,
249 ...(environmentId === laptop.environmentId ? saved : {}),
250 },
251 }));
252 const outcome = async (
253 plan: ReturnType<typeof planScopedSettingsPatch>,
254 persistServer: ReturnType<typeof upgradedLaptop>,
255 ) => {
256 const result = await persistScopedSettingsPatch(plan, persistServer, vi.fn());
257 return {
258 saved: result.savedEnvironments.map(({ label }) => label),
259 failed: result.failedEnvironments.map(({ label, message }) => ({ label, message })),
260 };
261 };
262 const unsupported = [
263 {
264 label: server.label,
265 message: "This environment does not support this setting. Update T3 Code on it.",
266 },
267 ];
268​
269 it("reports a dropped toggle", async () => {
270 expect(
271 await outcome(
272 planScopedSettingsPatch(all, environments, { reviewGuidesEnabled: true }),
273 upgradedLaptop({ reviewGuidesEnabled: true }),
274 ),
275 ).toEqual({ saved: [laptop.label], failed: unsupported });
276 });
277​
278 it("reports dropped text but accepts the server trimming it", async () => {
279 expect(
280 await outcome(
281 planScopedSettingsPatch(all, environments, { reviewGuideInstructions: " Read tests " }),
282 upgradedLaptop({ reviewGuideInstructions: "Read tests" }),
283 ),
284 ).toEqual({ saved: [laptop.label], failed: unsupported });
285 });
286​
287 // regression: #18334 an environment silently dropped a setting with no warning
288 it("reports a dropped key inside a project override", async () => {
289 expect(
290 await outcome(
291 planScopedSettingsPatch(project, environments, { defaultAutoPull: true }),
292 upgradedLaptop({
293 projectSettingsOverrides: { [laptopProjectId]: { defaultAutoPull: true } },
294 }),
295 ),
296 ).toEqual({ saved: [laptop.label], failed: unsupported });
297 });
298​
299 it("accepts a write that matches what the environment already had", async () => {
300 expect(
301 await outcome(
302 planScopedSettingsPatch(all, environments, { reviewGuidesEnabled: false }),
303 upgradedLaptop({}),
304 ),
305 ).toEqual({ saved: [laptop.label, server.label], failed: [] });
306 });
307 });
308​
309 it("persists client keys locally at any scope alongside server keys", async () => {
310 const persistServer = vi.fn().mockResolvedValue({ _tag: "Success" });
311 const persistClient = vi.fn();
312 await persistScopedSettingsPatch(
313 planScopedSettingsPatch(named, environments, {
314 diffIgnoreWhitespace: false,
315 enableProviderUpdateChecks: false,
316 }),
317 persistServer,
318 persistClient,
319 );
320 expect(persistClient).toHaveBeenCalledExactlyOnceWith({ diffIgnoreWhitespace: false });
321 expect(persistServer).toHaveBeenCalledExactlyOnceWith({
322 environmentId: server.environmentId,
323 input: { patch: { enableProviderUpdateChecks: false } },
324 });
325 });
326​
327 it("writes project overrides into each member's entry on its environment", () => {
328 const withExisting = environment("Server", {
329 settings: {
330 projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } },
331 },
332 });
333 const plan = planScopedSettingsPatch(project, [laptop, withExisting], {
334 defaultAutoPull: true,
335 });
336 expect(plan.unavailableReason).toBeNull();
337 expect(plan.serverWrites).toEqual([
338 {
339 environmentId: server.environmentId,
340 label: server.label,
341 previous: expect.anything(),
342 patch: {
343 projectSettingsOverrides: {
344 [projectId]: { enableAgentBrowserAccess: false, defaultAutoPull: true },
345 },
346 },
347 },
348 {
349 environmentId: laptop.environmentId,
350 label: laptop.label,
351 previous: expect.anything(),
352 patch: { projectSettingsOverrides: { [laptopProjectId]: { defaultAutoPull: true } } },
353 },
354 ]);
355 expect(
356 planScopedSettingsPatch(checkout, [laptop, server], { defaultAutoPull: true }),
357 ).toMatchObject({
358 serverWrites: [{ environmentId: server.environmentId }],
359 });
360 });
361​
362 it("keeps each project's other cleanup rules when changing one rule across machines", () => {
363 const machine = environment("Laptop", {
364 settings: {
365 storageCleanup: { ...DEFAULT_SERVER_SETTINGS.storageCleanup, worktreeAfterDays: 30 },
366 },
367 });
368 const customized = environment("Server", {
369 settings: {
370 projectSettingsOverrides: {
371 [projectId]: {
372 defaultAutoPull: true,
373 worktreeCleanup: {
374 mode: "custom",
375 rules: {
376 worktreeAfterDays: 8,
377 worktreeSettledAfterDays: null,
378 worktreeOnDelete: false,
379 worktreeOnMerge: true,
380 worktreeUnchanged: false,
381 worktreeKeepWhen: "uncommitted-changes",
382 },
383 },
384 },
385 },
386 },
387 });
388 const plan = planScopedSettingsPatch(project, [machine, customized], {
389 worktreeCleanup: { mode: "custom", rules: { worktreeOnDelete: true } },
390 });
391 const settledPlan = planScopedSettingsPatch(project, [machine, customized], {
392 worktreeCleanup: { mode: "custom", rules: { worktreeSettledAfterDays: 0 } },
393 });
394 expect(
395 settledPlan.serverWrites.map((write) => write.patch.projectSettingsOverrides),
396 ).toMatchObject([
397 { [projectId]: { worktreeCleanup: { rules: { worktreeSettledAfterDays: 0 } } } },
398 { [laptopProjectId]: { worktreeCleanup: { rules: { worktreeSettledAfterDays: 0 } } } },
399 ]);
400 expect(plan.serverWrites.map((write) => write.patch.projectSettingsOverrides)).toEqual([
401 {
402 [projectId]: {
403 defaultAutoPull: true,
404 worktreeCleanup: {
405 mode: "custom",
406 rules: {
407 worktreeAfterDays: 8,
408 worktreeSettledAfterDays: null,
409 worktreeOnDelete: true,
410 worktreeOnMerge: true,
411 worktreeUnchanged: false,
412 worktreeKeepWhen: "uncommitted-changes",
413 },
414 },
415 },
416 },
417 {
418 [laptopProjectId]: {
419 worktreeCleanup: {
420 mode: "custom",
421 rules: {
422 worktreeAfterDays: 30,
423 worktreeSettledAfterDays: null,
424 worktreeOnDelete: true,
425 worktreeOnMerge: false,
426 worktreeUnchanged: false,
427 worktreeKeepWhen: "uncommitted-changes",
428 },
429 },
430 },
431 },
432 ]);
433 expect(
434 planScopedSettingsClear(checkout, [customized], ["worktreeCleanup"]).serverWrites[0]?.patch,
435 ).toEqual({
436 projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
437 });
438 expect(
439 planScopedSettingsPatch(project, [machine, customized], {
440 storageCleanup: { browserArtifactsAfterDays: 8 },
441 }).serverWrites,
442 ).toEqual([]);
443 });
444​
445 // regression: #11541 device settings were written to the wrong scope
446 it("scopes agent device access to projects while keeping hub and hosts environment-wide", () => {
447 const plan = planScopedSettingsPatch(project, [laptop, server], {
448 enableAgentDeviceAccess: true,
449 });
450 expect(plan.serverWrites.map((write) => write.patch)).toEqual([
451 { projectSettingsOverrides: { [projectId]: { enableAgentDeviceAccess: true } } },
452 { projectSettingsOverrides: { [laptopProjectId]: { enableAgentDeviceAccess: true } } },
453 ]);
454 for (const patch of [{ enableDeviceSupport: true }, { deviceHosts: [] }]) {
455 expect(planScopedSettingsPatch(project, environments, patch).serverWrites).toEqual([]);
456 }
457 });
458​
459 it("refuses environment-wide keys at project scope", () => {
460 expect(
461 planScopedSettingsPatch(project, environments, { enableProviderUpdateChecks: false }),
462 ).toMatchObject({
463 serverWrites: [],
464 unavailableReason: "This setting is environment-wide and cannot be overridden by a project.",
465 });
466 });
467​
468 it("clears overrides per member and removes an emptied entry", () => {
469 const withOverrides = environment("Server", {
470 settings: {
471 projectSettingsOverrides: {
472 [projectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false },
473 },
474 },
475 });
476 const plan = planScopedSettingsClear(checkout, [laptop, withOverrides], ["defaultAutoPull"]);
477 expect(plan.serverWrites).toEqual([
478 {
479 environmentId: server.environmentId,
480 label: server.label,
481 previous: expect.anything(),
482 patch: { projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } } },
483 },
484 ]);
485 expect(
486 planScopedSettingsClear(
487 checkout,
488 [laptop, withOverrides],
489 ["defaultAutoPull", "enableAgentBrowserAccess"],
490 ).serverWrites[0]?.patch,
491 ).toEqual({ projectSettingsOverrides: { [projectId]: null } });
492 });
493​
494 it("never substitutes an environment-default write for an invalid scope", () => {
495 const scope = resolveSettingsScope({ machine: "removed" }, [group], environments);
496 const plan = planScopedSettingsPatch(scope, environments, { enableAgentBrowserAccess: false });
497 expect(plan.serverWrites).toEqual([]);
498 expect(plan.hasClientWrite).toBe(false);
499 expect(plan.unavailableReason).not.toBeNull();
500 });
501​
502 it("waits for every target and identifies both RPC failures and rejected writes", async () => {
503 const third = environment("Third");
504 const fourth = environment("Fourth");
505 const selected = [...environments, third, fourth];
506 const scope = resolveSettingsScope({}, [], selected);
507 const persistServer = vi
508 .fn()
509 .mockResolvedValueOnce({ _tag: "Success" })
510 .mockResolvedValueOnce({
511 _tag: "Failure",
512 cause: Cause.fail(
513 new EnvironmentAuthorizationError({
514 requiredScope: "settings:write",
515 message: "This connection lacks permission to change settings.",
516 }),
517 ),
518 })
519 .mockRejectedValueOnce(new Error("Disconnected during save"))
520 .mockResolvedValueOnce({ _tag: "Success" });
521 const result = await persistScopedSettingsPatch(
522 planScopedSettingsPatch(scope, selected, { enableAgentBrowserAccess: false }),
523 persistServer,
524 vi.fn(),
525 );
526 expect(result.savedEnvironmentCount).toBe(2);
527 expect(result.savedEnvironments.map(({ label }) => label)).toEqual([
528 laptop.label,
529 fourth.label,
530 ]);
531 expect(result.failedEnvironments.map(({ message }) => message)).toEqual([
532 "This connection lacks permission to change settings.",
533 "Disconnected during save",
534 ]);
535 expect(result.failedEnvironments.map(({ label }) => label)).toEqual([
536 server.label,
537 third.label,
538 ]);
539 expect(persistServer).toHaveBeenCalledTimes(4);
540 });
541});
542​
543describe("scoped settings mixed values", () => {
544 it("treats independently decoded equal nested settings as the same value", () => {
545 const style = {
546 ...DEFAULT_SERVER_SETTINGS.sourceControlWritingStyle,
547 mode: "custom" as const,
548 customInstructions: "Use plain language",
549 };
550 const first = environment("First", { settings: { sourceControlWritingStyle: { ...style } } });
551 const second = environment("Second", { settings: { sourceControlWritingStyle: { ...style } } });
552 const targets = resolveScopedSettingsTargets(all, [first, second]);
553 expect(scopedSettingsAreMixed(targets, ["sourceControlWritingStyle"])).toBe(false);
554 });
555});
556​
557describe("project overrides at environment scope", () => {
558 const laptop = EnvironmentId.make("laptop");
559 const fleet = ProjectId.make("fleet");
560 const t3 = ProjectId.make("t3");
561 const environment = (
562 environmentId: EnvironmentId,
563 overrides: ServerSettings["projectSettingsOverrides"],
564 ) => ({
565 environmentId,
566 label: environmentId,
567 connection: { phase: "connected" as const },
568 serverConfig: {
569 settings: { ...DEFAULT_SERVER_SETTINGS, projectSettingsOverrides: overrides },
570 environment: { capabilities: { projectSettingsOverrides: true } },
571 },
572 });
573​
574 it("clears only those keys and drops entries that become empty", () => {
575 const plan = planProjectOverridesClear(
576 [
577 environment(laptop, {
578 [fleet]: { defaultAutoPull: true, defaultThreadEnvMode: "local" },
579 [t3]: { defaultAutoPull: true },
580 }),
581 ],
582 [
583 { environmentId: laptop, projectId: fleet },
584 { environmentId: laptop, projectId: t3 },
585 ],
586 ["defaultAutoPull"],
587 );
588 expect(plan.serverWrites).toEqual([
589 {
590 environmentId: laptop,
591 label: laptop,
592 previous: expect.anything(),
593 patch: {
594 projectSettingsOverrides: { [fleet]: { defaultThreadEnvMode: "local" }, [t3]: null },
595 },
596 },
597 ]);
598 });
599});
600​
601describe("null patches at project scope", () => {
602 it("removes the override for keys that cannot store null and keeps it for keys that can", () => {
603 const environmentId = EnvironmentId.make("laptop");
604 const projectId = ProjectId.make("fleet");
605 const scope = {
606 kind: "project" as const,
607 group: {} as never,
608 environmentId: null,
609 label: "fleet",
610 members: [{ id: projectId, environmentId } as never],
611 environmentIds: [environmentId],
612 };
613 const environments = [
614 {
615 environmentId,
616 label: "Laptop",
617 connection: { phase: "connected" as const },
618 serverConfig: {
619 settings: {
620 ...DEFAULT_SERVER_SETTINGS,
621 projectSettingsOverrides: {
622 [projectId]: { defaultThreadEnvMode: "worktree" as const, defaultAutoPull: true },
623 },
624 },
625 environment: { capabilities: { projectSettingsOverrides: true } },
626 },
627 },
628 ];
629 expect(
630 planScopedSettingsPatch(scope, environments, { defaultThreadEnvMode: null }).serverWrites[0]
631 ?.patch,
632 ).toEqual({ projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } } });
633 expect(
634 planScopedSettingsPatch(scope, environments, { defaultModelSelection: null }).serverWrites[0]
635 ?.patch,
636 ).toEqual({
637 projectSettingsOverrides: {
638 [projectId]: {
639 defaultThreadEnvMode: "worktree",
640 defaultAutoPull: true,
641 defaultModelSelection: null,
642 },
643 },
644 });
645 });
646});
647​
648describe("partial object patches at project scope", () => {
649 it("completes a writing style field patch from the target's effective value", () => {
650 const environmentId = EnvironmentId.make("laptop");
651 const projectId = ProjectId.make("fleet");
652 const member = {
653 id: projectId,
654 environmentId,
655 physicalProjectKey: "laptop:/repo",
656 environmentLabel: "Laptop",
657 title: "fleet",
658 workspaceRoot: "/repo",
659 defaultModelSelection: null,
660 scripts: [],
661 createdAt: "2026-01-01T00:00:00.000Z",
662 updatedAt: "2026-01-01T00:00:00.000Z",
663 };
664 const settings = {
665 ...DEFAULT_SERVER_SETTINGS,
666 projectSettingsOverrides: {
667 [projectId]: {
668 sourceControlWritingStyle: {
669 mode: "custom" as const,
670 customInstructions: "Keep it short.",
671 followChangeRequestTemplates: false,
672 },
673 },
674 },
675 };
676 const plan = planScopedSettingsPatch(
677 {
678 kind: "project",
679 group: {} as never,
680 environmentId: null,
681 label: "fleet",
682 members: [member as never],
683 environmentIds: [environmentId],
684 },
685 [
686 {
687 environmentId,
688 label: "Laptop",
689 connection: { phase: "connected" },
690 serverConfig: {
691 settings,
692 },
693 },
694 ],
695 { sourceControlWritingStyle: { customInstructions: "Be terse." } },
696 );
697 expect(plan.serverWrites[0]?.patch).toEqual({
698 projectSettingsOverrides: {
699 [projectId]: {
700 sourceControlWritingStyle: {
701 mode: "custom",
702 customInstructions: "Be terse.",
703 followChangeRequestTemplates: false,
704 },
705 },
706 },
707 });
708 });
709});
710​
711it("routes legacy model preference keys exclusively to the selected environment", () => {
712 const patch = {
713 favorites: [{ provider: ProviderInstanceId.make("codex"), model: "sol" }],
714 providerModelPreferences: {},
715 };
716 const plan = planScopedSettingsPatch(named, environments, patch);
717 expect(plan.clientPatch).toEqual({});
718 expect(plan.hasClientWrite).toBe(false);
719 expect(plan.serverWrites).toEqual([
720 {
721 environmentId: server.environmentId,
722 label: server.label,
723 previous: expect.anything(),
724 patch,
725 },
726 ]);
727});
728​
729describe("model selection patches at project scope", () => {
730 const codex = ProviderInstanceId.make("codex");
731 const claude = ProviderInstanceId.make("claudeAgent");
732 const saved = {
733 instanceId: codex,
734 model: "gpt-5.6",
735 options: [{ id: "reasoningEffort", value: "high" }],
736 };
737 const customized = environment("Server", {
738 settings: {
739 projectSettingsOverrides: {
740 [projectId]: {
741 defaultModelSelection: saved,
742 sourceControlWriterModelSelection: saved,
743 reviewGuideModelSelection: saved,
744 textGenerationModelSelection: saved,
745 },
746 },
747 },
748 });
749​
750 // regression: #16581 changing a project's model kept the old model's options
751 it("replaces old options when changing model selections", () => {
752 const plan = planScopedSettingsPatch(checkout, [customized], {
753 defaultModelSelection: { instanceId: claude, model: "claude-opus-4-8" },
754 sourceControlWriterModelSelection: { instanceId: codex, model: "gpt-5.6-mini" },
755 reviewGuideModelSelection: { instanceId: codex, model: "gpt-5.6-mini" },
756 textGenerationModelSelection: { instanceId: claude, model: "claude-sonnet-4-6" },
757 });
758 expect(plan.serverWrites[0]?.patch.projectSettingsOverrides?.[projectId]).toEqual({
759 defaultModelSelection: { instanceId: claude, model: "claude-opus-4-8" },
760 sourceControlWriterModelSelection: { instanceId: codex, model: "gpt-5.6-mini" },
761 reviewGuideModelSelection: { instanceId: codex, model: "gpt-5.6-mini" },
762 textGenerationModelSelection: { instanceId: claude, model: "claude-sonnet-4-6" },
763 });
764 });
765​
766 it("completes partial text-generation selections without retaining old options", () => {
767 const modelPlan = planScopedSettingsPatch(checkout, [customized], {
768 textGenerationModelSelection: { model: "gpt-5.6-mini" },
769 });
770 expect(
771 modelPlan.serverWrites[0]?.patch.projectSettingsOverrides?.[projectId]
772 ?.textGenerationModelSelection,
773 ).toEqual({ instanceId: codex, model: "gpt-5.6-mini" });
774​
775 const options = [{ id: "reasoningEffort", value: "low" }];
776 const optionsPlan = planScopedSettingsPatch(checkout, [customized], {
777 textGenerationModelSelection: { options },
778 });
779 expect(
780 optionsPlan.serverWrites[0]?.patch.projectSettingsOverrides?.[projectId]
781 ?.textGenerationModelSelection,
782 ).toEqual({ ...saved, options });
783 });
784});