Skip to content

Commit 11e3d50

Browse files
fix(settings): keep inherited env mode off the wire and clear overrides on Inherit
Older clients decode defaultThreadEnvMode as a required literal, so a null on the wire failed their whole settings snapshot. Encode inherit as a missing key instead. Picking Inherit at project scope wrote null into the project override, which the server rejected for this key; the planners now remove the override for keys whose override cannot store null. The resolved label only shows once a checkout's t3.json has been read. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 98256de commit 11e3d50

9 files changed

Lines changed: 173 additions & 9 deletions

File tree

‎apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ const WORKSPACE_CHOICES: ReadonlyArray<{
5757
{
5858
mode: null,
5959
label: "Inherit",
60-
description: "Follow the repository's t3.json, or the current checkout.",
60+
description:
61+
"Follow the next tier: the environment, the repository's t3.json, or the current checkout.",
6162
},
6263
{
6364
mode: "local",

‎apps/mobile/src/features/settings/settings-scoped-server.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ describe("mobile project settings scope", () => {
6969
expect(secondSettings.responseStreamingMode).toBe("token");
7070
});
7171

72+
it("removes a project override when a picker sends null for a key that cannot store it", () => {
73+
const settings: ServerSettings = {
74+
...DEFAULT_SERVER_SETTINGS,
75+
projectSettingsOverrides: {
76+
[firstProject]: { defaultThreadEnvMode: "worktree", defaultAutoPull: true },
77+
},
78+
};
79+
const targets = resolveMobileSettingsTargets(
80+
[environment(firstId, settings)],
81+
[{ environmentId: firstId, id: firstProject }],
82+
);
83+
expect(planMobileScopedSettingsPatch(targets, true, { defaultThreadEnvMode: null })).toEqual([
84+
{
85+
environmentId: firstId,
86+
patch: { projectSettingsOverrides: { [firstProject]: { defaultAutoPull: true } } },
87+
},
88+
]);
89+
expect(planMobileScopedSettingsPatch(targets, true, { defaultModelSelection: null })).toEqual([
90+
{
91+
environmentId: firstId,
92+
patch: {
93+
projectSettingsOverrides: {
94+
[firstProject]: {
95+
defaultThreadEnvMode: "worktree",
96+
defaultAutoPull: true,
97+
defaultModelSelection: null,
98+
},
99+
},
100+
},
101+
},
102+
]);
103+
});
104+
72105
it("resets only the selected page's override and rejects environment-wide writes", () => {
73106
const settings: ServerSettings = {
74107
...DEFAULT_SERVER_SETTINGS,

‎apps/mobile/src/features/settings/settings-scoped-server.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
isNullableProjectSettingsOverride,
23
PROJECT_SCOPED_SERVER_SETTING_KEYS,
34
type EnvironmentId,
45
type ProjectId,
@@ -69,7 +70,19 @@ export function planMobileScopedSettingsPatch(
6970
continue;
7071
const current =
7172
target.environment.serverConfig.settings.projectSettingsOverrides[target.projectId] ?? {};
72-
const next = { ...current, ...patch };
73+
const next: Record<string, unknown> = { ...current };
74+
for (const [key, value] of Object.entries(patch)) {
75+
// A picker's "Inherit" sends null; for keys whose override cannot
76+
// store null that means remove the override.
77+
if (
78+
value === null &&
79+
!isNullableProjectSettingsOverride(key as ProjectScopedServerSettingKey)
80+
) {
81+
delete next[key];
82+
} else {
83+
next[key] = value;
84+
}
85+
}
7386
const overrides = writes.get(target.environment.environmentId) ?? {};
7487
overrides[target.projectId] = next;
7588
writes.set(target.environment.environmentId, overrides);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,18 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
7979
const unavailable = connectedEnvironments.length === 0;
8080

8181
// A setting on either scope beats the checkout's t3.json, and "inherit"
82-
// (null) is what lets the file decide; show what inherit resolves to.
82+
// (null) is what lets the file decide; show what inherit resolves to once
83+
// a checkout's file has been read. Other scopes have no single file to ask.
8384
const checkout = scope.kind === "checkout" ? scope.checkout : null;
8485
// The query is disabled without a checkout, so any id satisfies the hook.
8586
const t3File = useT3ProjectFileState(
8687
checkout?.environmentId ?? EnvironmentId.make("none"),
8788
category === "general" && checkout ? checkout.workspaceRoot : null,
8889
);
90+
const fileSettled = checkout !== null && t3File.status !== "loading";
8991
const repositoryEnvMode = t3File.file?.defaultThreadEnvMode ?? null;
9092
const inheritedEnvModeLabel =
91-
settings.defaultThreadEnvMode !== null
93+
!fileSettled || settings.defaultThreadEnvMode !== null
9294
? null
9395
: repositoryEnvMode
9496
? `${resolveEnvModeLabel(repositoryEnvMode)} (t3.json)`

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,53 @@ describe("project overrides at environment scope", () => {
510510
});
511511
});
512512

513+
describe("null patches at project scope", () => {
514+
it("removes the override for keys that cannot store null and keeps it for keys that can", () => {
515+
const environmentId = EnvironmentId.make("laptop");
516+
const projectId = ProjectId.make("fleet");
517+
const scope = {
518+
kind: "project" as const,
519+
group: {} as never,
520+
environmentId: null,
521+
label: "fleet",
522+
members: [{ id: projectId, environmentId } as never],
523+
environmentIds: [environmentId],
524+
};
525+
const environments = [
526+
{
527+
environmentId,
528+
label: "Laptop",
529+
connection: { phase: "connected" as const },
530+
serverConfig: {
531+
settings: {
532+
...DEFAULT_SERVER_SETTINGS,
533+
projectSettingsOverrides: {
534+
[projectId]: { defaultThreadEnvMode: "worktree" as const, defaultAutoPull: true },
535+
},
536+
},
537+
environment: { capabilities: { projectSettingsOverrides: true } },
538+
},
539+
},
540+
];
541+
expect(
542+
planScopedSettingsPatch(scope, environments, { defaultThreadEnvMode: null }).serverWrites[0]
543+
?.patch,
544+
).toEqual({ projectSettingsOverrides: { [projectId]: { defaultAutoPull: true } } });
545+
expect(
546+
planScopedSettingsPatch(scope, environments, { defaultModelSelection: null }).serverWrites[0]
547+
?.patch,
548+
).toEqual({
549+
projectSettingsOverrides: {
550+
[projectId]: {
551+
defaultThreadEnvMode: "worktree",
552+
defaultAutoPull: true,
553+
defaultModelSelection: null,
554+
},
555+
},
556+
});
557+
});
558+
});
559+
513560
describe("partial object patches at project scope", () => {
514561
it("completes a writing style field patch from the target's effective value", () => {
515562
const environmentId = EnvironmentId.make("laptop");

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ClientSettingsSchema,
33
type ClientSettingsPatch,
44
type EnvironmentId,
5+
isNullableProjectSettingsOverride,
56
PROJECT_SCOPED_SERVER_SETTING_KEYS,
67
type ProjectId,
78
type ProjectScopedServerSettingKey,
@@ -234,6 +235,15 @@ export function planScopedSettingsPatch(
234235
};
235236
continue;
236237
}
238+
// A picker's "Inherit" sends null; for keys whose override
239+
// cannot store null that means remove the override.
240+
if (
241+
value === null &&
242+
!isNullableProjectSettingsOverride(key as ProjectScopedServerSettingKey)
243+
) {
244+
delete next[key];
245+
continue;
246+
}
237247
const base = effective[key as keyof ServerSettings];
238248
next[key] =
239249
isPlainObject(value) && isPlainObject(base) ? { ...base, ...value } : value;

‎packages/contracts/src/baseSchemas.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ export const ForwardCompatibleNullable = <Value extends Schema.Top>(value: Value
8282
);
8383
};
8484

85+
/**
86+
* A nullable setting whose null is "unset" and never crosses the wire: it
87+
* decodes from a missing or unknown key and encodes back to a missing key.
88+
* For a field that older clients decode as a required literal, so a null
89+
* on the wire would fail their whole settings snapshot.
90+
*/
91+
export const OmittedWhenNull = <Value extends Schema.Top>(value: Value) => {
92+
const decodeValue = Schema.decodeUnknownOption(value as never);
93+
return Schema.optionalKey(Schema.Unknown).pipe(
94+
Schema.decodeTo(
95+
Schema.NullOr(value),
96+
SchemaTransformation.transformOptional<Value["Encoded"] | null, unknown>({
97+
decode: (raw) =>
98+
Option.some(
99+
Option.isSome(raw) && Option.isSome(decodeValue(raw.value))
100+
? (raw.value as Value["Encoded"])
101+
: null,
102+
),
103+
encode: (raw) =>
104+
Option.isSome(raw) && raw.value !== null ? Option.some(raw.value) : Option.none(),
105+
}),
106+
),
107+
);
108+
};
109+
85110
export const ForwardCompatibleArray = <Element extends Schema.Top>(element: Element) => {
86111
const decodeElement = Schema.decodeUnknownOption(element as never);
87112
return Schema.Array(Schema.Unknown).pipe(

‎packages/contracts/src/settings.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,22 @@ describe("ServerSettings worktree defaults", () => {
810810
expect(decodeServerSettings({ defaultThreadEnvMode: "worktree" }).defaultThreadEnvMode).toBe(
811811
"worktree",
812812
);
813+
expect(
814+
decodeServerSettings({ defaultThreadEnvMode: "remote" }).defaultThreadEnvMode,
815+
).toBeNull();
813816
expect(
814817
decodeServerSettingsPatch({ defaultThreadEnvMode: null }).defaultThreadEnvMode,
815818
).toBeNull();
816819
});
817820

821+
it("keeps an inherited thread env mode off the wire for older clients", () => {
822+
const encode = Schema.encodeSync(ServerSettings);
823+
expect("defaultThreadEnvMode" in encode(decodeServerSettings({}))).toBe(false);
824+
expect(
825+
encode(decodeServerSettings({ defaultThreadEnvMode: "worktree" })).defaultThreadEnvMode,
826+
).toBe("worktree");
827+
});
828+
818829
it("defaults start-from-origin on for legacy configs", () => {
819830
expect(decodeServerSettings({}).newWorktreesStartFromOrigin).toBe(true);
820831
});

‎packages/contracts/src/settings.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Schema from "effect/Schema";
55
import * as SchemaTransformation from "effect/SchemaTransformation";
66
import {
77
ForwardCompatibleNullable,
8+
OmittedWhenNull,
89
ProjectId,
910
TrimmedNonEmptyString,
1011
TrimmedString,
@@ -1041,6 +1042,26 @@ export const ProjectSettingsOverrides = Schema.Struct({
10411042
} satisfies Record<ProjectScopedServerSettingKey, unknown>);
10421043
export type ProjectSettingsOverrides = typeof ProjectSettingsOverrides.Type;
10431044

1045+
/**
1046+
* Whether `null` is a stored override value for this key rather than "unset".
1047+
* Clients writing a project override treat null for every other key as a
1048+
* request to remove the override, so a picker's "Inherit" item and the row's
1049+
* reset do the same thing.
1050+
*/
1051+
export function isNullableProjectSettingsOverride(key: ProjectScopedServerSettingKey): boolean {
1052+
return NULLABLE_PROJECT_SETTINGS_OVERRIDES.has(key);
1053+
}
1054+
const NULLABLE_PROJECT_SETTINGS_OVERRIDES: ReadonlySet<ProjectScopedServerSettingKey> = new Set<
1055+
{
1056+
[K in ProjectScopedServerSettingKey]: null extends ProjectSettingsOverrides[K] ? K : never;
1057+
}[ProjectScopedServerSettingKey]
1058+
>([
1059+
"defaultModelSelection",
1060+
"sourceControlWriterModelSelection",
1061+
"pullRequestMergeMethod",
1062+
"sidebarAutoSettleAfterDays",
1063+
]);
1064+
10441065
export const StorageCleanupSettings = Schema.Struct({
10451066
worktreeAfterDays: StorageRetentionDays.pipe(Schema.withDecodingDefault(Effect.succeed(null))),
10461067
worktreeOnMerge: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
@@ -1177,12 +1198,13 @@ export const ServerSettings = Schema.Struct({
11771198
Schema.withDecodingDefault(Effect.succeed(null)),
11781199
),
11791200
/**
1180-
* Null defers to the repository's t3.json, then to "local". Older servers
1181-
* always wrote a value, so a stored one keeps winning over the file.
1201+
* Null means inherit: the repository's t3.json, then "local". The old
1202+
* default "local" was never persisted (defaults are stripped on write), so
1203+
* it now decodes as inherit, which resolves the same way because the old
1204+
* chain also let t3.json outrank the environment. Null stays off the wire
1205+
* so older clients, which require a literal here, keep decoding.
11821206
*/
1183-
defaultThreadEnvMode: ForwardCompatibleNullable(ThreadEnvMode).pipe(
1184-
Schema.withDecodingDefault(Effect.succeed(null)),
1185-
),
1207+
defaultThreadEnvMode: OmittedWhenNull(ThreadEnvMode),
11861208
newWorktreesStartFromOrigin: Schema.Boolean.pipe(
11871209
Schema.withDecodingDefault(Effect.succeed(true)),
11881210
),

0 commit comments

Comments
 (0)