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

projectSettings.ts

231 lines9.3 KB

1import {
2 type ModelSelection,
3 PROJECT_FILE_BACKED_SETTINGS,
4 PROJECT_SCOPED_SERVER_SETTING_KEYS,
5 type ProjectFileBackedSettingKey,
6 type ProjectId,
7 type ProjectScopedServerSettingKey,
8 type ProjectSettingsOverrides,
9 type ResolvedServerSettings,
10 type ServerSettings,
11 type T3ProjectFile,
12 type ThreadEnvMode,
13 type WorktreeCleanupRules,
14} from "@t3tools/contracts";
15import { isModelSelectionProviderEnabled } from "./serverSettings.ts";
16​
17/**
18 * Where a project-scoped value came from. The order is the priority order:
19 * a project override, then the environment value, then the repository's
20 * t3.json for keys in `PROJECT_FILE_BACKED_SETTINGS`, then the built-in
21 * default (reported as "environment", since that is what the environment
22 * value is when nothing set it).
23 */
24export type ProjectSettingSource = "environment" | "project" | "t3.json";
25​
26export type ProjectSettingSources = Readonly<
27 Record<ProjectScopedServerSettingKey, ProjectSettingSource>
28>;
29​
30export interface ResolvedProjectSettings<Settings extends ServerSettings = ServerSettings> {
31 /** Environment settings with the project's overrides applied. */
32 readonly settings: Settings;
33 /** Where each scopable key's effective value came from. */
34 readonly sources: ProjectSettingSources;
35 /** The project's raw override entry; `{}` when it has none. */
36 readonly overrides: ProjectSettingsOverrides;
38​
39const EMPTY_OVERRIDES: ProjectSettingsOverrides = {};
40​
41const ENVIRONMENT_SOURCES: ProjectSettingSources = Object.fromEntries(
42 PROJECT_SCOPED_SERVER_SETTING_KEYS.map((key) => [key, "environment"]),
43) as Record<ProjectScopedServerSettingKey, ProjectSettingSource>;
44​
45/** Cheap check so hot paths skip the projectId lookup when nothing is overridden. */
46export function hasProjectSettingsOverrides(
47 settings: Pick<ServerSettings, "projectSettingsOverrides">,
48): boolean {
49 for (const entry of Object.values(settings.projectSettingsOverrides)) {
50 // A forward-compatible decode can leave an unknown value as a present
51 // undefined; that is not an override.
52 if (Object.values(entry).some((value) => value !== undefined)) return true;
53 }
54 return false;
56​
57/**
58 * The project aggregate's own model and workspace fields. They remain the
59 * source of truth until the server has folded them into the override record;
60 * after the fold the record alone decides, so a reset there cannot be undone
61 * by a stale aggregate value.
62 */
63export interface LegacyProjectSettingsFields {
64 readonly defaultModelSelection?: ModelSelection | null | undefined;
65 readonly defaultThreadEnvMode?: ThreadEnvMode | null | undefined;
67​
68/**
69 * Apply one project's overrides on top of environment settings. A model
70 * override whose provider is disabled on this environment falls back to the
71 * environment value, the same guard the environment-level selection gets.
72 */
73export function resolveProjectSettings(
74 settings: ServerSettings,
75 projectId: ProjectId | null,
76 // Nullable, not just optional: the mobile new-task flow passes its selected
77 // project straight through, and that is null until the shell snapshot lands.
78 project?: LegacyProjectSettingsFields | null,
79): ResolvedProjectSettings;
80/**
81 * With the checkout's decoded t3.json (or null for a missing or invalid
82 * one), every file-backed key resolves to a concrete value: the file fills
83 * keys whose project and environment tiers are both unset, and the built-in
84 * default fills what is left.
85 */
86export function resolveProjectSettings(
87 settings: ServerSettings,
88 projectId: ProjectId | null,
89 project: LegacyProjectSettingsFields | null | undefined,
90 projectFile: T3ProjectFile | null,
91): ResolvedProjectSettings<ResolvedServerSettings>;
92export function resolveProjectSettings(
93 settings: ServerSettings,
94 projectId: ProjectId | null,
95 project?: LegacyProjectSettingsFields | null,
96 projectFile?: T3ProjectFile | null,
97): ResolvedProjectSettings {
98 const resolved = resolveProjectOverrides(settings, projectId, project);
99 return projectFile === undefined ? resolved : applyProjectFile(resolved, projectFile);
101​
102function applyProjectFile(
103 resolved: ResolvedProjectSettings,
104 projectFile: T3ProjectFile | null,
105): ResolvedProjectSettings {
106 let effective: Record<string, unknown> | null = null;
107 let sources: Record<ProjectScopedServerSettingKey, ProjectSettingSource> | null = null;
108 for (const key of Object.keys(PROJECT_FILE_BACKED_SETTINGS) as ProjectFileBackedSettingKey[]) {
109 if (resolved.settings[key] !== null) continue;
110 const { value, source } = resolveProjectFileBackedSetting(key, null, projectFile);
111 effective ??= { ...resolved.settings };
112 sources ??= { ...resolved.sources };
113 effective[key] = value;
114 // A project override of null defers like an unset one, so the value did
115 // not come from the project either way.
116 sources[key] = source;
117 }
118 return effective === null || sources === null
119 ? resolved
120 : { ...resolved, settings: effective as ServerSettings, sources };
122​
123/**
124 * The file and built-in tiers for one key, given the project-over-environment
125 * value (`null` when neither is set). For callers that hold the settings tier
126 * but only see the file later, such as the git driver reading the t3.json of
127 * the checkout it just created. Same chain as `resolveProjectSettings`.
128 */
129export function resolveProjectFileBackedSetting<K extends ProjectFileBackedSettingKey>(
130 key: K,
131 setting: ServerSettings[K],
132 projectFile: T3ProjectFile | null,
133): { value: ResolvedServerSettings[K]; source: ProjectSettingSource } {
134 if (setting !== null) {
135 return { value: setting as ResolvedServerSettings[K], source: "environment" };
136 }
137 const { field, builtIn } = PROJECT_FILE_BACKED_SETTINGS[key];
138 const fromFile = projectFile?.[field] as ResolvedServerSettings[K] | undefined;
139 return fromFile === undefined
140 ? { value: builtIn as ResolvedServerSettings[K], source: "environment" }
141 : { value: fromFile, source: "t3.json" };
143​
144function resolveProjectOverrides(
145 settings: ServerSettings,
146 projectId: ProjectId | null,
147 project?: LegacyProjectSettingsFields | null,
148): ResolvedProjectSettings {
149 const stored = projectId === null ? undefined : settings.projectSettingsOverrides[projectId];
150 const overrides: ProjectSettingsOverrides =
151 project == null || settings.projectSettingsFolded
152 ? (stored ?? EMPTY_OVERRIDES)
153 : {
154 ...(project.defaultModelSelection != null
155 ? { defaultModelSelection: project.defaultModelSelection }
156 : {}),
157 ...(project.defaultThreadEnvMode != null
158 ? { defaultThreadEnvMode: project.defaultThreadEnvMode }
159 : {}),
160 ...stored,
161 };
162 if (Object.keys(overrides).length === 0) {
163 return { settings, sources: ENVIRONMENT_SOURCES, overrides: EMPTY_OVERRIDES };
164 }
165 const sources: Record<ProjectScopedServerSettingKey, ProjectSettingSource> = {
166 ...ENVIRONMENT_SOURCES,
167 };
168 const effective: Record<string, unknown> = { ...settings };
169 for (const key of PROJECT_SCOPED_SERVER_SETTING_KEYS) {
170 if (!Object.hasOwn(overrides, key)) continue;
171 const value = overrides[key];
172 // A forward-compatible decode leaves an unknown value as a present
173 // undefined; that is not an override.
174 if (value === undefined) continue;
175 // A model on a disabled provider falls back to the environment, like the
176 // environment-level guards do for these keys.
177 if (
178 (key === "textGenerationModelSelection" || key === "defaultModelSelection") &&
179 value !== undefined &&
180 value !== null &&
181 !isModelSelectionProviderEnabled(settings, value as ModelSelection)
182 ) {
183 continue;
184 }
185 effective[key] = value;
186 sources[key] = "project";
187 }
188 return { settings: effective as ServerSettings, sources, overrides };
190​
191/** Replace the project's entry, dropping it entirely when nothing is overridden. */
192export function withProjectSettingsOverrides(
193 settings: Pick<ServerSettings, "projectSettingsOverrides">,
194 projectId: ProjectId,
195 next: ProjectSettingsOverrides | null,
196): ServerSettings["projectSettingsOverrides"] {
197 const { [projectId]: _removed, ...rest } = settings.projectSettingsOverrides;
198 return next === null || Object.keys(next).length === 0 ? rest : { ...rest, [projectId]: next };
200​
201/** The project's entry with `keys` removed; `null` when that leaves it empty. */
202export function clearProjectSettingsOverrides(
203 settings: Pick<ServerSettings, "projectSettingsOverrides">,
204 projectId: ProjectId,
205 keys: readonly ProjectScopedServerSettingKey[],
206): ProjectSettingsOverrides | null {
207 const current = settings.projectSettingsOverrides[projectId];
208 if (current === undefined) return null;
209 const next = { ...current };
210 for (const key of keys) delete next[key];
211 return Object.keys(next).length === 0 ? null : next;
213​
214/** Worktree rules are project-scoped; artifact and log retention stays environment-wide. */
215export function resolveWorktreeCleanup(
216 settings: ServerSettings,
217 projectId: ProjectId | null,
218): WorktreeCleanupRules {
219 const policy = resolveProjectSettings(settings, projectId).settings.worktreeCleanup;
220 if (policy?.mode === "custom") return policy.rules;
221 if (policy?.mode === "off")
222 return {
223 worktreeAfterDays: null,
224 worktreeOnMerge: false,
225 worktreeOnDelete: false,
226 worktreeUnchanged: false,
227 };
228 const { worktreeAfterDays, worktreeOnMerge, worktreeOnDelete, worktreeUnchanged } =
229 settings.storageCleanup;
230 return { worktreeAfterDays, worktreeOnMerge, worktreeOnDelete, worktreeUnchanged };