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