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

sharedSettings.ts

177 lines6.4 KB

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 "autoResumeLimitedThreads",
29 "newWorktreesStartFromOrigin",
30 "sourceControlWritingStyle",
31 "textGenerationModelSelection",
32] as const satisfies ReadonlyArray<keyof ServerSettings & keyof ServerSettingsPatch>;
33​
34export type SharedServerSettingKey = (typeof SHARED_SERVER_SETTING_KEYS)[number];
35​
36const SHARED_KEY_SET = new Set<string>(SHARED_SERVER_SETTING_KEYS);
37​
38/** Split a server patch into the keys every environment should receive and the primary-only rest. */
39export function splitSharedServerPatch(patch: ServerSettingsPatch): {
40 sharedPatch: ServerSettingsPatch;
41 localPatch: ServerSettingsPatch;
42} {
43 const sharedPatch: Record<string, unknown> = {};
44 const localPatch: Record<string, unknown> = {};
45 for (const [key, value] of Object.entries(patch)) {
46 if (SHARED_KEY_SET.has(key)) {
47 sharedPatch[key] = value;
48 } else {
49 localPatch[key] = value;
50 }
51 }
52 return {
53 sharedPatch: sharedPatch as ServerSettingsPatch,
54 localPatch: localPatch as ServerSettingsPatch,
55 };
57​
58/** Filter unsupported preferences; direct model writes retain the server's fallback behavior. */
59export function filterSharedServerPatch(
60 patch: ServerSettingsPatch,
61 capabilities: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation"> | undefined,
62 settings?: ServerSettings,
63 sourceSettings = settings,
64 targetIsSource = false,
65): ServerSettingsPatch {
66 const instanceId =
67 patch.textGenerationModelSelection?.instanceId ??
68 sourceSettings?.textGenerationModelSelection.instanceId;
69 if (
70 !targetIsSource &&
71 patch.textGenerationModelSelection &&
72 (!settings ||
73 (instanceId !== undefined &&
74 (sourceSettings?.providerInstances[instanceId]?.driver ?? instanceId) !==
75 (settings.providerInstances[instanceId]?.driver ?? instanceId)) ||
76 !isModelSelectionProviderEnabled(settings, {
77 ...settings.textGenerationModelSelection,
78 ...patch.textGenerationModelSelection,
79 }))
80 ) {
81 patch = Struct.omit(patch, ["textGenerationModelSelection"]);
82 }
83 return capabilities?.threadRestartContinuation === true
84 ? patch
85 : Struct.omit(patch, ["continueThreadsAfterServerUpdate"]);
87​
88/** The shared subset supported by one environment. */
89export function pickSharedServerSettings(
90 settings: ServerSettings,
91 capabilities?: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">,
92): ServerSettingsPatch {
93 return filterSharedServerPatch(
94 Struct.pick(settings, SHARED_SERVER_SETTING_KEYS),
95 capabilities,
96 settings,
97 );
99​
100/**
101 * Whether an environment can participate in shared-settings sync right now.
102 * Auto-settlement establishes baseline support; newer preferences are filtered separately.
103 */
104export function supportsSharedSettingsSync(environment: {
105 readonly connection: { readonly phase: EnvironmentConnectionPhase };
106 readonly serverConfig: {
107 readonly environment: {
108 readonly capabilities: Pick<ExecutionEnvironmentCapabilities, "threadAutoSettlement">;
109 };
110 } | null;
111}): boolean {
112 return (
113 environment.connection.phase === "connected" &&
114 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
115 );
117​
118export interface SharedSettingsEnvironment {
119 readonly environmentId: EnvironmentId;
120 readonly label: string;
121 readonly syncEligible: boolean;
122 readonly settings: ServerSettings | null;
123 readonly capabilities?:
124 | Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">
125 | undefined;
127​
128/**
129 * Shared-settings sync targets whose values differ from the primary
130 * environment's. Other environments are skipped: nothing can be read from or
131 * written to them, or their server lacks baseline shared-settings support. With no
132 * primary settings loaded there is nothing to compare against, so nothing is
133 * reported. Callers must pass the real loaded settings, never a default
134 * fallback, or "apply to all" would push defaults over real values.
135 */
136export function findSharedSettingsMismatches(input: {
137 readonly primaryEnvironmentId: EnvironmentId | null;
138 readonly primarySettings: ServerSettings | null;
139 readonly primaryCapabilities?:
140 | Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">
141 | undefined;
142 readonly environments: ReadonlyArray<SharedSettingsEnvironment>;
143}): ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }> {
144 if (input.primaryEnvironmentId === null || input.primarySettings === null) {
145 return [];
146 }
147 const primarySettings = pickSharedServerSettings(
148 input.primarySettings,
149 input.primaryCapabilities,
150 );
151 return input.environments.flatMap((environment) => {
152 if (
153 environment.environmentId === input.primaryEnvironmentId ||
154 !environment.syncEligible ||
155 environment.settings === null
156 ) {
157 return [];
158 }
159 const expected = filterSharedServerPatch(
160 primarySettings,
161 environment.capabilities,
162 environment.settings,
163 input.primarySettings ?? undefined,
164 );
165 let actual = filterSharedServerPatch(
166 pickSharedServerSettings(environment.settings, environment.capabilities),
167 input.primaryCapabilities,
168 environment.settings,
169 );
170 if (!expected.textGenerationModelSelection) {
171 actual = Struct.omit(actual, ["textGenerationModelSelection"]);
172 }
173 return Equal.equals(actual, expected)
174 ? []
175 : [{ environmentId: environment.environmentId, label: environment.label }];
176 });