Open History to see the last commit
1/**
2 * Shared server settings.
3 *
4 * Every server keeps its own `settings.json`, but some keys are user
5 * preferences that only live on the server because the server has to act on
6 * them (auto-settlement runs with no client attached). A user does not want
7 * those to differ per machine. Clients write these keys to every shared-settings
8 * sync target, and warn when another target still holds a different value so
9 * the user can push their current value out.
10 */
11import type {
12 EnvironmentId,
13 ExecutionEnvironmentCapabilities,
14 ServerSettings,
15 ServerSettingsPatch,
16} from "@t3tools/contracts";
17import { isModelSelectionProviderEnabled } from "@t3tools/shared/serverSettings";
18import * as Equal from "effect/Equal";
19import * as Struct from "effect/Struct";
20
21import type { EnvironmentConnectionPhase } from "../connection/presentation.ts";
22
23/** Server keys that hold a user preference rather than machine config. */
24const SHARED_SERVER_SETTING_KEYS = [
25 "continueThreadsAfterServerUpdate",
26 "sidebarAutoSettleAfterDays",
27 "sidebarAutoSettleOnMerge",
28 "newWorktreesStartFromOrigin",
29 "sourceControlWritingStyle",
30 "textGenerationModelSelection",
31] as const satisfies ReadonlyArray<keyof ServerSettings & keyof ServerSettingsPatch>;
32
33export type SharedServerSettingKey = (typeof SHARED_SERVER_SETTING_KEYS)[number];
34
35const SHARED_KEY_SET = new Set<string>(SHARED_SERVER_SETTING_KEYS);
36
37/** Split a server patch into the keys every environment should receive and the primary-only rest. */
38export function splitSharedServerPatch(patch: ServerSettingsPatch): {
39 sharedPatch: ServerSettingsPatch;
40 localPatch: ServerSettingsPatch;
41} {
42 const sharedPatch: Record<string, unknown> = {};
43 const localPatch: Record<string, unknown> = {};
44 for (const [key, value] of Object.entries(patch)) {
45 if (SHARED_KEY_SET.has(key)) {
46 sharedPatch[key] = value;
47 } else {
48 localPatch[key] = value;
49 }
50 }
51 return {
52 sharedPatch: sharedPatch as ServerSettingsPatch,
53 localPatch: localPatch as ServerSettingsPatch,
54 };
55}
56
57/** Filter unsupported preferences; direct model writes retain the server's fallback behavior. */
58export function filterSharedServerPatch(
59 patch: ServerSettingsPatch,
60 capabilities: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation"> | undefined,
61 settings?: ServerSettings,
62 sourceSettings = settings,
63 targetIsSource = false,
64): ServerSettingsPatch {
65 const instanceId =
66 patch.textGenerationModelSelection?.instanceId ??
67 sourceSettings?.textGenerationModelSelection.instanceId;
68 if (
69 !targetIsSource &&
70 patch.textGenerationModelSelection &&
71 (!settings ||
72 (instanceId !== undefined &&
73 (sourceSettings?.providerInstances[instanceId]?.driver ?? instanceId) !==
74 (settings.providerInstances[instanceId]?.driver ?? instanceId)) ||
75 !isModelSelectionProviderEnabled(settings, {
76 ...settings.textGenerationModelSelection,
77 ...patch.textGenerationModelSelection,
78 }))
79 ) {
80 patch = Struct.omit(patch, ["textGenerationModelSelection"]);
81 }
82 return capabilities?.threadRestartContinuation === true
83 ? patch
84 : Struct.omit(patch, ["continueThreadsAfterServerUpdate"]);
85}
86
87/** The shared subset supported by one environment. */
88export function pickSharedServerSettings(
89 settings: ServerSettings,
90 capabilities?: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">,
91): ServerSettingsPatch {
92 return filterSharedServerPatch(
93 Struct.pick(settings, SHARED_SERVER_SETTING_KEYS),
94 capabilities,
95 settings,
96 );
97}
98
99/**
100 * Whether an environment can participate in shared-settings sync right now.
101 * Auto-settlement establishes baseline support; newer preferences are filtered separately.
102 */
103export function supportsSharedSettingsSync(environment: {
104 readonly connection: { readonly phase: EnvironmentConnectionPhase };
105 readonly serverConfig: {
106 readonly environment: {
107 readonly capabilities: Pick<ExecutionEnvironmentCapabilities, "threadAutoSettlement">;
108 };
109 } | null;
110}): boolean {
111 return (
112 environment.connection.phase === "connected" &&
113 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
114 );
115}
116
117export interface SharedSettingsEnvironment {
118 readonly environmentId: EnvironmentId;
119 readonly label: string;
120 readonly syncEligible: boolean;
121 readonly settings: ServerSettings | null;
122 readonly capabilities?:
123 | Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">
124 | undefined;
125}
126
127/**
128 * Shared-settings sync targets whose values differ from the primary
129 * environment's. Other environments are skipped: nothing can be read from or
130 * written to them, or their server lacks baseline shared-settings support. With no
131 * primary settings loaded there is nothing to compare against, so nothing is
132 * reported. Callers must pass the real loaded settings, never a default
133 * fallback, or "apply to all" would push defaults over real values.
134 */
135export function findSharedSettingsMismatches(input: {
136 readonly primaryEnvironmentId: EnvironmentId | null;
137 readonly primarySettings: ServerSettings | null;
138 readonly primaryCapabilities?:
139 | Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">
140 | undefined;
141 readonly environments: ReadonlyArray<SharedSettingsEnvironment>;
142}): ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }> {
143 if (input.primaryEnvironmentId === null || input.primarySettings === null) {
144 return [];
145 }
146 const primarySettings = pickSharedServerSettings(
147 input.primarySettings,
148 input.primaryCapabilities,
149 );
150 return input.environments.flatMap((environment) => {
151 if (
152 environment.environmentId === input.primaryEnvironmentId ||
153 !environment.syncEligible ||
154 environment.settings === null
155 ) {
156 return [];
157 }
158 const expected = filterSharedServerPatch(
159 primarySettings,
160 environment.capabilities,
161 environment.settings,
162 input.primarySettings ?? undefined,
163 );
164 let actual = filterSharedServerPatch(
165 pickSharedServerSettings(environment.settings, environment.capabilities),
166 input.primaryCapabilities,
167 environment.settings,
168 );
169 if (!expected.textGenerationModelSelection) {
170 actual = Struct.omit(actual, ["textGenerationModelSelection"]);
171 }
172 return Equal.equals(actual, expected)
173 ? []
174 : [{ environmentId: environment.environmentId, label: environment.label }];
175 });
176}