Skip to content

Commit 742173a

Browse files
feat(settings): resolve t3.json inside the project settings resolver (pingdotgg#12954)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 1262d2f commit 742173a

25 files changed

Lines changed: 615 additions & 236 deletions

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ const PAGE_PROJECT_KEYS: Record<SettingsPage, readonly ProjectScopedServerSettin
5050
};
5151

5252
const WORKSPACE_CHOICES: ReadonlyArray<{
53-
readonly mode: ThreadEnvMode;
53+
readonly mode: ThreadEnvMode | null;
5454
readonly label: string;
5555
readonly description: string;
5656
}> = [
57+
// Only offered at environment scope; a project falls back through "Use defaults".
58+
{
59+
mode: null,
60+
label: "Inherit",
61+
description: "Use the repository's t3.json, or the current checkout.",
62+
},
5763
{
5864
mode: "local",
5965
label: "Current checkout",
@@ -126,6 +132,10 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
126132
const value = reference.settings[key];
127133
return displayTargets.every((entry) => entry.settings[key] === value) ? value : null;
128134
};
135+
// `uniform` folds a real null into "mixed"; nullable keys need the distinction.
136+
const isMixed = (key: keyof ServerSettings) =>
137+
reference === null ||
138+
displayTargets.some((entry) => entry.settings[key] !== reference.settings[key]);
129139
const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
130140
label: "environment settings update",
131141
reportFailure: true,
@@ -219,17 +229,22 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
219229
<SettingsSection
220230
title="Default workspace"
221231
trailing={
222-
pendingWrites === 0 && uniform("defaultThreadEnvMode") === null ? (
232+
pendingWrites === 0 && isMixed("defaultThreadEnvMode") ? (
223233
<MixedValuesLabel projectSelected={projectSelected} />
224234
) : null
225235
}
226236
>
227-
{WORKSPACE_CHOICES.map((choice, index) => (
237+
{WORKSPACE_CHOICES.filter(
238+
(choice) => choice.mode !== null || !projectSelected,
239+
).map((choice, index) => (
228240
<ChoiceRow
229-
key={choice.mode}
241+
key={choice.mode ?? "inherit"}
230242
label={choice.label}
231243
description={choice.description}
232-
selected={uniform("defaultThreadEnvMode") === choice.mode}
244+
selected={
245+
!isMixed("defaultThreadEnvMode") &&
246+
uniform("defaultThreadEnvMode") === choice.mode
247+
}
233248
separated={index > 0}
234249
disabled={disabledFor("defaultThreadEnvMode")}
235250
onPress={() => write({ defaultThreadEnvMode: choice.mode })}

‎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/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import {
2020
} from "@t3tools/contracts";
2121
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
2222
import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
23-
import {
24-
isDefaultThreadEnvModeSettled,
25-
resolveDefaultThreadEnvMode,
26-
} from "@t3tools/shared/threadEnvMode";
2723
import * as Arr from "effect/Array";
2824
import { pipe } from "effect/Function";
2925

@@ -431,38 +427,35 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
431427
: null,
432428
);
433429
const t3ProjectFileData = t3ProjectFileQuery.data as ProjectReadFileResult | null;
434-
const t3ProjectFileDefaultMode = useMemo(() => {
435-
if (t3ProjectFileData === null || t3ProjectFileData.truncated) return null;
436-
return parseT3ProjectFile(t3ProjectFileData.contents)?.defaultThreadEnvMode ?? null;
437-
}, [t3ProjectFileData]);
438-
// Environment settings with the project's overrides applied; the
439-
// aggregate's own legacy fields still count until the server folds them.
430+
const t3ProjectFile = useMemo(
431+
() =>
432+
t3ProjectFileData === null || t3ProjectFileData.truncated
433+
? null
434+
: parseT3ProjectFile(t3ProjectFileData.contents),
435+
[t3ProjectFileData],
436+
);
437+
// Environment settings with the project's overrides and its t3.json
438+
// applied; the aggregate's own legacy fields still count until the server
439+
// folds them.
440440
const projectSettings = useMemo(
441441
() =>
442442
resolveProjectSettings(
443443
selectedEnvironmentServerConfig?.settings ?? DEFAULT_SERVER_SETTINGS,
444444
selectedProject?.id ?? null,
445445
selectedProject,
446+
t3ProjectFile,
446447
),
447-
[selectedEnvironmentServerConfig?.settings, selectedProject],
448+
[selectedEnvironmentServerConfig?.settings, selectedProject, t3ProjectFile],
448449
);
449-
const projectThreadEnvMode =
450-
projectSettings.sources.defaultThreadEnvMode === "project"
451-
? projectSettings.settings.defaultThreadEnvMode
452-
: undefined;
453-
const defaultWorkspaceMode: WorkspaceMode = resolveDefaultThreadEnvMode({
454-
projectSetting: projectThreadEnvMode,
455-
projectFile: t3ProjectFileDefaultMode,
456-
globalDefault: projectSettings.settings.defaultThreadEnvMode,
457-
});
458-
// While unsettled the resolved default is provisional. Nothing may write
459-
// it into the draft during that window (the auto-branch effect does), or
460-
// the frozen interim value beats the t3.json default once it loads.
461-
const defaultWorkspaceModeSettled = isDefaultThreadEnvModeSettled({
462-
explicitMode: selectedProjectDraft.workspaceSelection?.mode,
463-
projectSetting: projectThreadEnvMode,
464-
projectFilePending: t3ProjectFileQuery.isPending,
465-
});
450+
const defaultWorkspaceMode: WorkspaceMode = projectSettings.settings.defaultThreadEnvMode;
451+
// While the file read is pending and nothing above it decided, the
452+
// resolved default is provisional. Nothing may write it into the draft
453+
// during that window (the auto-branch effect does), or the frozen interim
454+
// value beats the t3.json default once it loads.
455+
const defaultWorkspaceModeSettled =
456+
selectedProjectDraft.workspaceSelection?.mode !== undefined ||
457+
projectSettings.sources.defaultThreadEnvMode !== "environment" ||
458+
!t3ProjectFileQuery.isPending;
466459
const workspaceMode = selectedProjectDraft.workspaceSelection?.mode ?? defaultWorkspaceMode;
467460
const selectedBranchName = selectedProjectDraft.workspaceSelection?.branch ?? null;
468461
const selectedWorktreePath = selectedProjectDraft.workspaceSelection?.worktreePath ?? null;

‎apps/web/src/components/files/projectFilesQueryState.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const EMPTY_PROJECT_FILE_PATH = "";
2020
const EMPTY_PROJECT_FILE_QUERY_ATOM = Atom.make(
2121
AsyncResult.initial<ProjectReadFileResult, never>(false),
2222
).pipe(Atom.withLabel("project-file-query:empty"));
23-
function optimisticFileAtom(environmentId: EnvironmentId, cwd: string, relativePath: string) {
23+
/** A pending in-app write to the file, overlaying the query until confirmed. */
24+
export function optimisticFileAtom(
25+
environmentId: EnvironmentId,
26+
cwd: string,
27+
relativePath: string,
28+
) {
2429
return projectEnvironment.optimisticFile({ environmentId, cwd, relativePath });
2530
}
2631

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

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {
22
DEFAULT_SERVER_SETTINGS,
3-
EnvironmentId,
43
type ModelSelection,
54
type ProviderInstanceId,
65
} from "@t3tools/contracts";
76
import { createModelSelection } from "@t3tools/shared/model";
7+
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
88
import { useNavigate } from "@tanstack/react-router";
99

10-
import { useT3ProjectFileState } from "../../hooks/useT3ProjectFileScripts";
1110
import { getCustomModelOptionsByInstance } from "../../modelSelection";
1211
import {
1312
applyProviderInstanceSettings,
@@ -75,25 +74,16 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
7574
const mixedAutoPull = useScopedSettingsMixed(["defaultAutoPull"]);
7675
const mixedMergeMethod = useScopedSettingsMixed(["pullRequestMergeMethod"]);
7776
const modelSource = useScopedSettingSource(["defaultModelSelection"]);
78-
const workspaceSource = useScopedSettingSource(["defaultThreadEnvMode"]);
7977
const isProjectScope = scope.kind === "project" || scope.kind === "checkout";
8078
const unavailable = connectedEnvironments.length === 0;
81-
82-
// A checkout's t3.json wins over the environment default when the project
83-
// has no override of its own; show which one "inherit" resolves to.
84-
const checkout = scope.kind === "checkout" ? scope.checkout : null;
85-
// The query is disabled without a checkout, so any id satisfies the hook.
86-
const t3File = useT3ProjectFileState(
87-
checkout?.environmentId ?? EnvironmentId.make("none"),
88-
category === "general" && checkout ? checkout.workspaceRoot : null,
89-
);
90-
const repositoryEnvMode = t3File.file?.defaultThreadEnvMode ?? null;
91-
const inheritedEnvModeLabel =
92-
workspaceSource === "project"
93-
? null
94-
: repositoryEnvMode
95-
? `${resolveEnvModeLabel(repositoryEnvMode)} (t3.json)`
96-
: null;
79+
// File-backed keys show their effective value; the target already carries
80+
// the checkout's t3.json, and a null file here only fills the built-in.
81+
// The reset arrow beside the title clears the tier (SettingsRow handles a
82+
// project override, the environment value is cleared here), so the picker
83+
// has no "inherit" item.
84+
const effective = target
85+
? resolveProjectSettings(target.settings, null, null, null).settings
86+
: null;
9787

9888
function modelDisabledReason(instanceId: ProviderInstanceId, model: string): string | null {
9989
const sourceEntry = entries.find((entry) => entry.instanceId === instanceId);
@@ -292,27 +282,20 @@ export function ProjectDefaultsSettings({ category }: { category: ProjectSetting
292282
title="Workspace"
293283
description={
294284
isProjectScope
295-
? "Where new threads in this project start. A t3.json preference applies when the project has no override."
296-
: "Where new threads start, unless overridden by the project or t3.json."
297-
}
298-
status={
299-
inheritedEnvModeLabel ? `Repository default: ${inheritedEnvModeLabel}` : undefined
285+
? "Where new threads in this project start."
286+
: "Where new threads start. Projects and their t3.json can override it."
300287
}
301288
resetAction={
302-
settings.defaultThreadEnvMode !== DEFAULT_SERVER_SETTINGS.defaultThreadEnvMode ? (
289+
!isProjectScope && settings.defaultThreadEnvMode !== null ? (
303290
<SettingResetButton
304291
label="default workspace"
305-
onClick={() =>
306-
updateSettings({
307-
defaultThreadEnvMode: DEFAULT_SERVER_SETTINGS.defaultThreadEnvMode,
308-
})
309-
}
292+
onClick={() => updateSettings({ defaultThreadEnvMode: null })}
310293
/>
311294
) : null
312295
}
313296
control={
314297
<Select
315-
value={mixedWorkspace ? null : settings.defaultThreadEnvMode}
298+
value={mixedWorkspace ? null : (effective?.defaultThreadEnvMode ?? null)}
316299
onValueChange={(value) => {
317300
if (value === "local" || value === "worktree")
318301
updateSettings({ defaultThreadEnvMode: value });

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,41 @@ describe("settingInheritanceLayers", () => {
5454
["Off", false],
5555
]);
5656
});
57+
58+
it("shows the checkout's t3.json as a layer for file-backed keys", () => {
59+
const file = { defaultThreadEnvMode: "worktree" as const };
60+
const fromFile = settingInheritanceLayers(
61+
{
62+
environmentId,
63+
label: "Laptop",
64+
projectId,
65+
...resolveProjectSettings(DEFAULT_SERVER_SETTINGS, projectId, null, file),
66+
},
67+
DEFAULT_SERVER_SETTINGS,
68+
"defaultThreadEnvMode",
69+
);
70+
expect(fromFile.map((layer) => [layer.label, layer.value, layer.effective])).toEqual([
71+
["Project", "Inherits", false],
72+
["Laptop", "Inherits", false],
73+
["t3.json", "New worktree", true],
74+
["Default", "Current checkout", false],
75+
]);
76+
const settings = { ...DEFAULT_SERVER_SETTINGS, defaultThreadEnvMode: "local" as const };
77+
const fromEnvironment = settingInheritanceLayers(
78+
{
79+
environmentId,
80+
label: "Laptop",
81+
projectId,
82+
...resolveProjectSettings(settings, projectId, null, file),
83+
},
84+
settings,
85+
"defaultThreadEnvMode",
86+
);
87+
expect(fromEnvironment.map((layer) => [layer.value, layer.effective])).toEqual([
88+
["Inherits", false],
89+
["Current checkout", true],
90+
["Inherits", false],
91+
["Current checkout", false],
92+
]);
93+
});
5794
});

0 commit comments

Comments
 (0)