Skip to content

Commit 92ef200

Browse files
committed
feat(settings): leave environments that cannot store a setting out of its row
1 parent b2dcb63 commit 92ef200

10 files changed

Lines changed: 440 additions & 105 deletions

File tree

‎apps/server/src/environment/ServerEnvironment.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => {
215215
expect(second.capabilities.attachmentUploads).toBe(true);
216216
expect(second.capabilities.fileAttachments).toEqual({ maxUploadBytes: 50 * 1024 * 1024 });
217217
expect(second.capabilities.pullRequests).toBe(true);
218+
expect(second.capabilities.settingsKeys).toContain("reviewGuidesEnabled");
218219
expect(second.capabilities.requiredWorktreeBootstrap).toBe(true);
219220
expect(second.capabilities.usagePriceOverrides).toBe(true);
220221
expect(second.capabilities.threadActiveReorder).toBe(true);

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
EnvironmentId,
33
ORCHESTRATION_PROTOCOL_VERSION,
44
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
5+
ServerSettings,
56
type ExecutionEnvironmentDescriptor,
67
} from "@t3tools/contracts";
78
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -253,6 +254,7 @@ export const make = Effect.gen(function* () {
253254
serverResolvedCommandContext: true,
254255
environmentIcon: true,
255256
projectCloneTracking: true,
257+
settingsKeys: Object.keys(ServerSettings.fields),
256258
...(serverSelfUpdate === null ? {} : { serverSelfUpdate }),
257259
...(serverInstallation === null ? {} : { serverInstallation }),
258260
// V2 restart recovery uses the environment-owned opt-in. The old

‎apps/web/src/components/settings/SettingInheritance.tsx‎

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export function SettingInheritance({
195195
target,
196196
environment: { ...environment, serverConfig: environment.serverConfig },
197197
machine: resolveEnvironmentMachineKind(environment.serverConfig),
198+
unsupported: target.unsupportedSettingKeys?.has(key) === true,
198199
layers: settingInheritanceLayers(target, environment.serverConfig.settings, key),
199200
},
200201
];
@@ -227,7 +228,7 @@ export function SettingInheritance({
227228
</Tooltip>
228229
<PopoverPopup align="start" width="md" padding="none">
229230
<div className="divide-y divide-border/60">
230-
{chains.map(({ target, environment, machine, layers }) => (
231+
{chains.map(({ target, environment, machine, unsupported, layers }) => (
231232
<section
232233
key={`${target.environmentId}:${target.projectId ?? ""}`}
233234
className="px-3 py-2.5"
@@ -236,43 +237,49 @@ export function SettingInheritance({
236237
<EnvironmentMachineIcon aria-hidden kind={machine} className="size-3.5 shrink-0" />
237238
<span className="min-w-0 truncate">{target.label}</span>
238239
</h4>
239-
<ol role="list" className="text-sm">
240-
{layers.map((layer) => (
241-
<li
242-
key={layer.key}
243-
className={cn(
244-
"grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 rounded-md px-2 py-1",
245-
layer.effective && "bg-foreground/[0.06]",
246-
)}
247-
>
248-
<span
240+
{unsupported ? (
241+
<p className="px-2 py-1 text-sm text-muted-foreground">
242+
Not supported. Update T3 Code on this environment to use this setting.
243+
</p>
244+
) : (
245+
<ol role="list" className="text-sm">
246+
{layers.map((layer) => (
247+
<li
248+
key={layer.key}
249249
className={cn(
250-
"min-w-0 truncate",
251-
layer.effective ? "font-medium text-foreground" : "text-muted-foreground",
250+
"grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 rounded-md px-2 py-1",
251+
layer.effective && "bg-foreground/[0.06]",
252252
)}
253253
>
254-
{layer.key === "environment" ? "Environment" : layer.label}
255-
</span>
256-
<span
257-
className={cn(
258-
"flex items-center gap-1.5 tabular-nums",
259-
layer.effective
260-
? "text-foreground"
261-
: layer.set
262-
? "text-muted-foreground"
263-
: "text-muted-foreground/60",
264-
)}
265-
>
266-
<span className="max-w-32 truncate">{layer.value}</span>
267-
{layer.effective ? (
268-
<CheckIcon aria-hidden className="size-3.5 shrink-0 text-primary" />
269-
) : (
270-
<span aria-hidden className="size-3.5 shrink-0" />
271-
)}
272-
</span>
273-
</li>
274-
))}
275-
</ol>
254+
<span
255+
className={cn(
256+
"min-w-0 truncate",
257+
layer.effective ? "font-medium text-foreground" : "text-muted-foreground",
258+
)}
259+
>
260+
{layer.key === "environment" ? "Environment" : layer.label}
261+
</span>
262+
<span
263+
className={cn(
264+
"flex items-center gap-1.5 tabular-nums",
265+
layer.effective
266+
? "text-foreground"
267+
: layer.set
268+
? "text-muted-foreground"
269+
: "text-muted-foreground/60",
270+
)}
271+
>
272+
<span className="max-w-32 truncate">{layer.value}</span>
273+
{layer.effective ? (
274+
<CheckIcon aria-hidden className="size-3.5 shrink-0 text-primary" />
275+
) : (
276+
<span aria-hidden className="size-3.5 shrink-0" />
277+
)}
278+
</span>
279+
</li>
280+
))}
281+
</ol>
282+
)}
276283
{(() => {
277284
const overriding = overridingProjects.filter(
278285
(project) => project.environmentId === target.environmentId,

‎apps/web/src/components/settings/SettingsScopeContext.tsx‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import { createContext, type ReactNode, useContext, useMemo } from "react";
88
import { useEnvironments, usePrimaryEnvironmentId } from "../../state/environments";
99
import { getProjectFileQueryAtom, optimisticFileAtom } from "../files/projectFilesQueryState";
1010
import { useSettingsProjectGroups } from "./useSettingsProjectGroups";
11-
import { resolveScopedSettingsTargets, selectScopedSettingsEnvironments } from "./scopedSettings";
11+
import { droppedSettingKeysAtom, droppedSettingKeysFor } from "./droppedSettingKeys";
12+
import {
13+
resolveScopedSettingsTargets,
14+
resolveUnsupportedSettingKeys,
15+
selectScopedSettingsEnvironments,
16+
} from "./scopedSettings";
1217
import { resolveSettingsScope, type SettingsScopeSearch } from "./settingsScope";
1318
import { selectSingleEnvironmentScope } from "./settingsScopeAxis";
1419

@@ -56,7 +61,22 @@ function useMemberProjectFiles(scope: ReturnType<typeof resolveSettingsScope>) {
5661

5762
function useResolvedSettingsScope(rawSearch: SettingsScopeSearch, singleEnvironment: boolean) {
5863
const groups = useSettingsProjectGroups();
59-
const { environments: availableEnvironments } = useEnvironments();
64+
const { environments } = useEnvironments();
65+
const dropped = useAtomValue(droppedSettingKeysAtom);
66+
const availableEnvironments = useMemo(
67+
() =>
68+
environments.map((environment) => {
69+
const descriptor = environment.serverConfig?.environment;
70+
const unsupportedSettingKeys = resolveUnsupportedSettingKeys(
71+
descriptor?.capabilities.settingsKeys,
72+
droppedSettingKeysFor(dropped, environment.environmentId, descriptor?.serverVersion),
73+
);
74+
return unsupportedSettingKeys.size === 0
75+
? environment
76+
: { ...environment, unsupportedSettingKeys };
77+
}),
78+
[dropped, environments],
79+
);
6080
const primaryEnvironmentId = usePrimaryEnvironmentId();
6181
const search = useMemo(
6282
() =>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { EnvironmentId } from "@t3tools/contracts";
2+
import { Atom } from "effect/reactivity";
3+
4+
import { appAtomRegistry } from "../../rpc/atomRegistry";
5+
6+
interface DroppedSettingKeys {
7+
readonly serverVersion: string;
8+
readonly keys: ReadonlySet<string>;
9+
}
10+
11+
/**
12+
* Setting keys each environment dropped on a save this session, for servers
13+
* too old to list the keys they store. Tagged with the server version, so an
14+
* update clears them.
15+
*/
16+
export const droppedSettingKeysAtom = Atom.make<ReadonlyMap<EnvironmentId, DroppedSettingKeys>>(
17+
new Map(),
18+
).pipe(Atom.keepAlive, Atom.withLabel("settings:dropped-setting-keys"));
19+
20+
export function recordDroppedSettingKeys(
21+
environmentId: EnvironmentId,
22+
serverVersion: string,
23+
keys: readonly string[],
24+
) {
25+
if (keys.length === 0) return;
26+
appAtomRegistry.update(droppedSettingKeysAtom, (current) => {
27+
const existing = current.get(environmentId);
28+
const next = new Map(current);
29+
next.set(environmentId, {
30+
serverVersion,
31+
keys: new Set([...(existing?.serverVersion === serverVersion ? existing.keys : []), ...keys]),
32+
});
33+
return next;
34+
});
35+
}
36+
37+
/** The keys an environment dropped, if it still runs the version that dropped them. */
38+
export function droppedSettingKeysFor(
39+
dropped: ReadonlyMap<EnvironmentId, DroppedSettingKeys>,
40+
environmentId: EnvironmentId,
41+
serverVersion: string | undefined,
42+
): ReadonlySet<string> | undefined {
43+
const entry = dropped.get(environmentId);
44+
return entry && entry.serverVersion === serverVersion ? entry.keys : undefined;
45+
}

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import {
1919
planScopedSettingsClear,
2020
planScopedSettingsPatch,
2121
resolveScopedSettingsTargets,
22+
resolveUnsupportedSettingKeys,
23+
scopedDisplaySettings,
2224
scopedSettingsAreMixed,
2325
scopedSettingsSource,
26+
scopedSettingsUnsupportedTargets,
2427
selectScopedSettingsEnvironments,
2528
} from "./scopedSettings";
2629
import { resolveSettingsScope } from "./settingsScope";
@@ -750,3 +753,103 @@ it("routes legacy model preference keys exclusively to the selected environment"
750753
},
751754
]);
752755
});
756+
757+
describe("environments that cannot store a setting", () => {
758+
const enabledLaptop = environment("Laptop", { settings: { reviewGuidesEnabled: true } });
759+
const outdatedServer = {
760+
...server,
761+
unsupportedSettingKeys: new Set(["reviewGuidesEnabled", "defaultAutoPull"]),
762+
};
763+
const pair = [enabledLaptop, outdatedServer];
764+
const pairScope = resolveSettingsScope({}, [], pair);
765+
const targets = resolveScopedSettingsTargets(pairScope, pair);
766+
767+
it("reads unsupported keys from the advertised list, else from dropped writes", () => {
768+
const advertised = Object.keys(DEFAULT_SERVER_SETTINGS).filter(
769+
(key) => key !== "reviewGuidesEnabled",
770+
);
771+
expect([...resolveUnsupportedSettingKeys(advertised, new Set(["branchNamePrefix"]))]).toEqual([
772+
"reviewGuidesEnabled",
773+
]);
774+
expect([...resolveUnsupportedSettingKeys(undefined, new Set(["reviewGuidesEnabled"]))]).toEqual(
775+
["reviewGuidesEnabled"],
776+
);
777+
expect(resolveUnsupportedSettingKeys(undefined, undefined).size).toBe(0);
778+
});
779+
780+
it("leaves them out of the mixed state and shows the value where it is stored", () => {
781+
expect(scopedSettingsAreMixed(targets, ["reviewGuidesEnabled"])).toBe(false);
782+
expect(scopedSettingsUnsupportedTargets(targets, ["reviewGuidesEnabled"])).toEqual([
783+
targets[1],
784+
]);
785+
expect(scopedDisplaySettings(targets[1]!, targets).reviewGuidesEnabled).toBe(true);
786+
// Keys the outdated environment does store still compare across both.
787+
expect(
788+
scopedSettingsAreMixed(
789+
[
790+
targets[0]!,
791+
{ ...targets[1]!, settings: { ...targets[1]!.settings, branchNamePrefix: "x" } },
792+
],
793+
["branchNamePrefix"],
794+
),
795+
).toBe(true);
796+
});
797+
798+
it("writes only where the setting can be stored", () => {
799+
const plan = planScopedSettingsPatch(pairScope, pair, {
800+
reviewGuidesEnabled: false,
801+
branchNamePrefix: "feat",
802+
});
803+
expect(plan.serverWrites.map(({ environmentId, patch }) => ({ environmentId, patch }))).toEqual(
804+
[
805+
{
806+
environmentId: enabledLaptop.environmentId,
807+
patch: { reviewGuidesEnabled: false, branchNamePrefix: "feat" },
808+
},
809+
{ environmentId: server.environmentId, patch: { branchNamePrefix: "feat" } },
810+
],
811+
);
812+
});
813+
814+
it("names the environments to update when none can store the setting", () => {
815+
const plan = planScopedSettingsPatch(
816+
resolveSettingsScope({ machine: server.environmentId }, [], pair),
817+
pair,
818+
{ reviewGuidesEnabled: true },
819+
);
820+
expect(plan.serverWrites).toEqual([]);
821+
expect(plan.unavailableReason).toBe("Update T3 Code on Server to use this setting.");
822+
});
823+
824+
it("skips project override writes for keys the environment cannot store", () => {
825+
const plan = planScopedSettingsPatch(project, [laptop, outdatedServer], {
826+
defaultAutoPull: true,
827+
});
828+
expect(plan.serverWrites.map(({ environmentId }) => environmentId)).toEqual([
829+
laptop.environmentId,
830+
]);
831+
});
832+
833+
it("reports the setting keys an environment dropped, including inside project overrides", async () => {
834+
const result = await persistScopedSettingsPatch(
835+
planScopedSettingsPatch(project, environments, {
836+
defaultAutoPull: true,
837+
enableAgentBrowserAccess: false,
838+
}),
839+
async ({ environmentId }) => ({
840+
_tag: "Success" as const,
841+
value: {
842+
...DEFAULT_SERVER_SETTINGS,
843+
projectSettingsOverrides:
844+
environmentId === laptop.environmentId
845+
? { [laptopProjectId]: { defaultAutoPull: true, enableAgentBrowserAccess: false } }
846+
: { [projectId]: { defaultAutoPull: true } },
847+
},
848+
}),
849+
vi.fn(),
850+
);
851+
expect(
852+
result.failedEnvironments.map(({ label, droppedKeys }) => ({ label, droppedKeys })),
853+
).toEqual([{ label: server.label, droppedKeys: ["enableAgentBrowserAccess"] }]);
854+
});
855+
});

0 commit comments

Comments
 (0)