Skip to content

Commit 0d0a149

Browse files
committed
fix(web): warn when an environment silently drops a setting
1 parent bed9239 commit 0d0a149

2 files changed

Lines changed: 162 additions & 30 deletions

File tree

‎apps/web/src/components/settings/scopedSettings.test.ts‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,72 @@ describe("scoped settings writes", () => {
230230
expect(persistClient).not.toHaveBeenCalled();
231231
});
232232

233+
describe("an environment that saves but drops keys it does not know", () => {
234+
// Laptop knows every key; Server is an older build that keeps its settings as they were.
235+
const upgradedLaptop = (saved: Partial<ServerSettings>) =>
236+
vi.fn(async ({ environmentId }: { environmentId: EnvironmentId }) => ({
237+
_tag: "Success" as const,
238+
value: {
239+
...DEFAULT_SERVER_SETTINGS,
240+
...(environmentId === laptop.environmentId ? saved : {}),
241+
},
242+
}));
243+
const outcome = async (
244+
plan: ReturnType<typeof planScopedSettingsPatch>,
245+
persistServer: ReturnType<typeof upgradedLaptop>,
246+
) => {
247+
const result = await persistScopedSettingsPatch(plan, persistServer, vi.fn());
248+
return {
249+
saved: result.savedEnvironments.map(({ label }) => label),
250+
failed: result.failedEnvironments.map(({ label, message }) => ({ label, message })),
251+
};
252+
};
253+
const unsupported = [
254+
{
255+
label: server.label,
256+
message: "This environment does not support this setting. Update T3 Code on it.",
257+
},
258+
];
259+
260+
it("reports a dropped toggle", async () => {
261+
expect(
262+
await outcome(
263+
planScopedSettingsPatch(all, environments, { reviewGuidesEnabled: true }),
264+
upgradedLaptop({ reviewGuidesEnabled: true }),
265+
),
266+
).toEqual({ saved: [laptop.label], failed: unsupported });
267+
});
268+
269+
it("reports dropped text but accepts the server trimming it", async () => {
270+
expect(
271+
await outcome(
272+
planScopedSettingsPatch(all, environments, { reviewGuideInstructions: " Read tests " }),
273+
upgradedLaptop({ reviewGuideInstructions: "Read tests" }),
274+
),
275+
).toEqual({ saved: [laptop.label], failed: unsupported });
276+
});
277+
278+
it("reports a dropped key inside a project override", async () => {
279+
expect(
280+
await outcome(
281+
planScopedSettingsPatch(project, environments, { defaultAutoPull: true }),
282+
upgradedLaptop({
283+
projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } },
284+
}),
285+
),
286+
).toEqual({ saved: [laptop.label], failed: unsupported });
287+
});
288+
289+
it("accepts a write that matches what the environment already had", async () => {
290+
expect(
291+
await outcome(
292+
planScopedSettingsPatch(all, environments, { reviewGuidesEnabled: false }),
293+
upgradedLaptop({}),
294+
),
295+
).toEqual({ saved: [laptop.label, server.label], failed: [] });
296+
});
297+
});
298+
233299
it("persists client keys locally at any scope alongside server keys", async () => {
234300
const persistServer = vi.fn().mockResolvedValue({ _tag: "Success" });
235301
const persistClient = vi.fn();
@@ -262,6 +328,7 @@ describe("scoped settings writes", () => {
262328
{
263329
environmentId: server.environmentId,
264330
label: server.label,
331+
previous: expect.anything(),
265332
patch: {
266333
projectSettingsOverrides: {
267334
[projectId]: { enableAgentBrowserAccess: false, defaultAutoPull: true },
@@ -271,6 +338,7 @@ describe("scoped settings writes", () => {
271338
{
272339
environmentId: laptop.environmentId,
273340
label: laptop.label,
341+
previous: expect.anything(),
274342
patch: { projectSettingsOverrides: { [laptopProjectId]: { defaultAutoPull: true } } },
275343
},
276344
]);
@@ -391,6 +459,7 @@ describe("scoped settings writes", () => {
391459
{
392460
environmentId: server.environmentId,
393461
label: server.label,
462+
previous: expect.anything(),
394463
patch: { projectSettingsOverrides: { [projectId]: { enableAgentBrowserAccess: false } } },
395464
},
396465
]);
@@ -527,6 +596,7 @@ describe("project overrides at environment scope", () => {
527596
{
528597
environmentId: laptop,
529598
label: laptop,
599+
previous: expect.anything(),
530600
patch: {
531601
projectSettingsOverrides: { [fleet]: { defaultThreadEnvMode: "local" }, [t3]: null },
532602
},
@@ -655,6 +725,11 @@ it("routes legacy model preference keys exclusively to the selected environment"
655725
expect(plan.clientPatch).toEqual({});
656726
expect(plan.hasClientWrite).toBe(false);
657727
expect(plan.serverWrites).toEqual([
658-
{ environmentId: server.environmentId, label: server.label, patch },
728+
{
729+
environmentId: server.environmentId,
730+
label: server.label,
731+
previous: expect.anything(),
732+
patch,
733+
},
659734
]);
660735
});

‎apps/web/src/components/settings/scopedSettings.ts‎

Lines changed: 86 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ interface ScopedServerWrite {
166166
readonly environmentId: EnvironmentId;
167167
readonly label: string;
168168
readonly patch: ServerSettingsPatch;
169+
/** The environment's settings before the write, to tell a dropped key from a saved one. */
170+
readonly previous: ServerSettings;
169171
}
170172

171173
function projectOverrideWrites(
@@ -194,6 +196,7 @@ function projectOverrideWrites(
194196
writes.set(member.environmentId, {
195197
environmentId: member.environmentId,
196198
label: environment.label,
199+
previous: settings,
197200
patch: {
198201
projectSettingsOverrides: {
199202
...existing?.patch.projectSettingsOverrides,
@@ -266,29 +269,38 @@ export function planScopedSettingsPatch(
266269
return next as ProjectSettingsOverrides;
267270
})
268271
: scope.kind === "all" || scope.kind === "environment"
269-
? connectedEnvironments.map((environment) => ({
270-
environmentId: environment.environmentId,
271-
label: environment.label,
272-
patch:
273-
environment.serverConfig?.settings.worktreeCleanup != null &&
274-
serverPatch.storageCleanup &&
275-
Object.keys(serverPatch.storageCleanup).some((key) => key.startsWith("worktree"))
276-
? {
277-
...serverPatch,
278-
worktreeCleanup: {
279-
mode: "custom",
280-
rules: {
281-
...resolveWorktreeCleanup(environment.serverConfig.settings, null),
282-
...Object.fromEntries(
283-
Object.entries(serverPatch.storageCleanup).filter(([key]) =>
284-
key.startsWith("worktree"),
285-
),
286-
),
287-
},
288-
},
289-
}
290-
: serverPatch,
291-
}))
272+
? connectedEnvironments.flatMap((environment) => {
273+
const previous = environment.serverConfig?.settings;
274+
if (!previous) return [];
275+
return [
276+
{
277+
environmentId: environment.environmentId,
278+
label: environment.label,
279+
previous,
280+
patch:
281+
previous.worktreeCleanup != null &&
282+
serverPatch.storageCleanup &&
283+
Object.keys(serverPatch.storageCleanup).some((key) =>
284+
key.startsWith("worktree"),
285+
)
286+
? {
287+
...serverPatch,
288+
worktreeCleanup: {
289+
mode: "custom",
290+
rules: {
291+
...resolveWorktreeCleanup(previous, null),
292+
...Object.fromEntries(
293+
Object.entries(serverPatch.storageCleanup).filter(([key]) =>
294+
key.startsWith("worktree"),
295+
),
296+
),
297+
},
298+
},
299+
}
300+
: serverPatch,
301+
},
302+
];
303+
})
292304
: [];
293305
const hasClientWrite = Object.keys(clientPatch).length > 0;
294306
const hasWrite = hasClientWrite || serverWrites.length > 0;
@@ -371,6 +383,7 @@ export function planProjectOverridesClear(
371383
writes.set(environmentId, {
372384
environmentId,
373385
label: environment.label,
386+
previous: settings,
374387
patch: {
375388
projectSettingsOverrides: {
376389
...existing?.patch.projectSettingsOverrides,
@@ -389,13 +402,44 @@ export function planProjectOverridesClear(
389402
};
390403
}
391404

405+
/** Whether writing `value` leaves `current` as it is. Partial objects compare only the fields they set. */
406+
function patchLeavesUnchanged(value: unknown, current: unknown): boolean {
407+
if (typeof value === "string" && typeof current === "string") return value.trim() === current;
408+
if (isPlainObject(value) && isPlainObject(current)) {
409+
return Object.entries(value).every(([field, next]) =>
410+
next === null ? current[field] == null : patchLeavesUnchanged(next, current[field]),
411+
);
412+
}
413+
return Equal.equals(value, current);
414+
}
415+
416+
/**
417+
* An older server drops keys it does not know, top-level or nested, and still
418+
* succeeds. A key that should have changed but reads back as it was before
419+
* the write was dropped; left unreported, the row stays mixed forever.
420+
*/
421+
function droppedSettingKeys(write: ScopedServerWrite, saved: ServerSettings | undefined) {
422+
if (saved === undefined) return [];
423+
return Object.entries(write.patch).flatMap(([key, value]) => {
424+
const before = write.previous[key as keyof ServerSettings];
425+
return Equal.equals(saved[key as keyof ServerSettings], before) &&
426+
!patchLeavesUnchanged(value, before)
427+
? [key]
428+
: [];
429+
});
430+
}
431+
392432
/** Wait for every target so a failed environment does not hide successful or later writes. */
393433
export async function persistScopedSettingsPatch(
394434
plan: ReturnType<typeof planScopedSettingsPatch>,
395435
persistServer: (input: {
396436
environmentId: EnvironmentId;
397437
input: { patch: ServerSettingsPatch };
398-
}) => Promise<{ readonly _tag: "Success" | "Failure"; readonly cause?: Cause.Cause<unknown> }>,
438+
}) => Promise<{
439+
readonly _tag: "Success" | "Failure";
440+
readonly cause?: Cause.Cause<unknown>;
441+
readonly value?: ServerSettings;
442+
}>,
399443
persistClient: (patch: ClientSettingsPatch) => void,
400444
) {
401445
if (plan.hasClientWrite) persistClient(plan.clientPatch);
@@ -404,9 +448,25 @@ export async function persistScopedSettingsPatch(
404448
persistServer({ environmentId, input: { patch } }),
405449
),
406450
);
451+
const saved = (index: number) => {
452+
const result = results[index];
453+
return (
454+
result?.status === "fulfilled" &&
455+
result.value._tag === "Success" &&
456+
droppedSettingKeys(plan.serverWrites[index]!, result.value.value).length === 0
457+
);
458+
};
407459
const failedEnvironments = plan.serverWrites.flatMap((environment, index) => {
460+
if (saved(index)) return [];
408461
const result = results[index];
409-
if (result?.status === "fulfilled" && result.value._tag === "Success") return [];
462+
if (result?.status === "fulfilled" && result.value._tag === "Success") {
463+
return [
464+
{
465+
...environment,
466+
message: "This environment does not support this setting. Update T3 Code on it.",
467+
},
468+
];
469+
}
410470
const error: unknown =
411471
result?.status === "rejected"
412472
? result.reason
@@ -423,10 +483,7 @@ export async function persistScopedSettingsPatch(
423483
},
424484
];
425485
});
426-
const savedEnvironments = plan.serverWrites.filter((_, index) => {
427-
const result = results[index];
428-
return result?.status === "fulfilled" && result.value._tag === "Success";
429-
});
486+
const savedEnvironments = plan.serverWrites.filter((_, index) => saved(index));
430487
return {
431488
failedEnvironments,
432489
savedEnvironments,

0 commit comments

Comments
 (0)