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 * as Equal from "effect/Equal";
18import * as Struct from "effect/Struct";
19
20import type { EnvironmentConnectionPhase } from "../connection/presentation.ts";
21
22/** Server keys that hold a user preference rather than machine config. */
23const SHARED_SERVER_SETTING_KEYS = [
24 "continueThreadsAfterServerUpdate",
25 "sidebarAutoSettleAfterDays",
26 "sidebarAutoSettleOnMerge",
27 "defaultThreadEnvMode",
28 "newWorktreesStartFromOrigin",
29 "sourceControlWritingStyle",
30] as const satisfies ReadonlyArray<keyof ServerSettings & keyof ServerSettingsPatch>;
31
32export type SharedServerSettingKey = (typeof SHARED_SERVER_SETTING_KEYS)[number];
33
34const SHARED_KEY_SET = new Set<string>(SHARED_SERVER_SETTING_KEYS);
35
36/** Split a server patch into the keys every environment should receive and the primary-only rest. */
37export function splitSharedServerPatch(patch: ServerSettingsPatch): {
38 sharedPatch: ServerSettingsPatch;
39 localPatch: ServerSettingsPatch;
40} {
41 const sharedPatch: Record<string, unknown> = {};
42 const localPatch: Record<string, unknown> = {};
43 for (const [key, value] of Object.entries(patch)) {
44 if (SHARED_KEY_SET.has(key)) {
45 sharedPatch[key] = value;
46 } else {
47 localPatch[key] = value;
48 }
49 }
50 return {
51 sharedPatch: sharedPatch as ServerSettingsPatch,
52 localPatch: localPatch as ServerSettingsPatch,
53 };
54}
55
56/** Omit restart recovery on servers that cannot persist its preference. */
57export function filterSharedServerPatch(
58 patch: ServerSettingsPatch,
59 capabilities: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation"> | undefined,
60): ServerSettingsPatch {
61 return capabilities?.threadRestartContinuation === true
62 ? patch
63 : Struct.omit(patch, ["continueThreadsAfterServerUpdate"]);
64}
65
66/** The shared subset supported by one environment. */
67export function pickSharedServerSettings(
68 settings: ServerSettings,
69 capabilities?: Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">,
70): ServerSettingsPatch {
71 return filterSharedServerPatch(Struct.pick(settings, SHARED_SERVER_SETTING_KEYS), capabilities);
72}
73
74/**
75 * Whether an environment can participate in shared-settings sync right now.
76 * Auto-settlement establishes baseline support; newer preferences are filtered separately.
77 */
78export function supportsSharedSettingsSync(environment: {
79 readonly connection: { readonly phase: EnvironmentConnectionPhase };
80 readonly serverConfig: {
81 readonly environment: {
82 readonly capabilities: Pick<ExecutionEnvironmentCapabilities, "threadAutoSettlement">;
83 };
84 } | null;
85}): boolean {
86 return (
87 environment.connection.phase === "connected" &&
88 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
89 );
90}
91
92export interface SharedSettingsEnvironment {
93 readonly environmentId: EnvironmentId;
94 readonly label: string;
95 readonly syncEligible: boolean;
96 readonly settings: ServerSettings | null;
97 readonly capabilities?:
98 | Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">
99 | undefined;
100}
101
102/**
103 * Shared-settings sync targets whose values differ from the primary
104 * environment's. Other environments are skipped: nothing can be read from or
105 * written to them, or their server lacks baseline shared-settings support. With no
106 * primary settings loaded there is nothing to compare against, so nothing is
107 * reported. Callers must pass the real loaded settings, never a default
108 * fallback, or "apply to all" would push defaults over real values.
109 */
110export function findSharedSettingsMismatches(input: {
111 readonly primaryEnvironmentId: EnvironmentId | null;
112 readonly primarySettings: ServerSettings | null;
113 readonly primaryCapabilities?:
114 | Pick<ExecutionEnvironmentCapabilities, "threadRestartContinuation">
115 | undefined;
116 readonly environments: ReadonlyArray<SharedSettingsEnvironment>;
117}): ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }> {
118 if (input.primaryEnvironmentId === null || input.primarySettings === null) {
119 return [];
120 }
121 const primarySettings = pickSharedServerSettings(
122 input.primarySettings,
123 input.primaryCapabilities,
124 );
125 return input.environments.flatMap((environment) => {
126 if (
127 environment.environmentId === input.primaryEnvironmentId ||
128 !environment.syncEligible ||
129 environment.settings === null
130 ) {
131 return [];
132 }
133 const expected = filterSharedServerPatch(primarySettings, environment.capabilities);
134 const actual = filterSharedServerPatch(
135 pickSharedServerSettings(environment.settings, environment.capabilities),
136 input.primaryCapabilities,
137 );
138 return Equal.equals(actual, expected)
139 ? []
140 : [{ environmentId: environment.environmentId, label: environment.label }];
141 });
142}