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 connected
8 * environment, and warn when a connected environment still holds a different
9 * value so the user can push their current value out.
10 */
11import type { EnvironmentId, ServerSettings, ServerSettingsPatch } from "@t3tools/contracts";
12import * as Equal from "effect/Equal";
13import * as Struct from "effect/Struct";
14
15/** Server keys that hold a user preference rather than machine config. */
16export const SHARED_SERVER_SETTING_KEYS = [
17 "sidebarAutoSettleAfterDays",
18 "sidebarAutoSettleOnMerge",
19 "defaultThreadEnvMode",
20 "newWorktreesStartFromOrigin",
21 "sourceControlWritingStyle",
22] as const satisfies ReadonlyArray<keyof ServerSettings & keyof ServerSettingsPatch>;
23
24export type SharedServerSettingKey = (typeof SHARED_SERVER_SETTING_KEYS)[number];
25
26const SHARED_KEY_SET = new Set<string>(SHARED_SERVER_SETTING_KEYS);
27
28/** Split a server patch into the keys every environment should receive and the primary-only rest. */
29export function splitSharedServerPatch(patch: ServerSettingsPatch): {
30 sharedPatch: ServerSettingsPatch;
31 localPatch: ServerSettingsPatch;
32} {
33 const sharedPatch: Record<string, unknown> = {};
34 const localPatch: Record<string, unknown> = {};
35 for (const [key, value] of Object.entries(patch)) {
36 if (SHARED_KEY_SET.has(key)) {
37 sharedPatch[key] = value;
38 } else {
39 localPatch[key] = value;
40 }
41 }
42 return {
43 sharedPatch: sharedPatch as ServerSettingsPatch,
44 localPatch: localPatch as ServerSettingsPatch,
45 };
46}
47
48/** The shared subset of one environment's settings, as a patch that can be written elsewhere. */
49export function pickSharedServerSettings(settings: ServerSettings): ServerSettingsPatch {
50 return Struct.pick(settings, SHARED_SERVER_SETTING_KEYS);
51}
52
53export interface SharedSettingsEnvironment {
54 readonly environmentId: EnvironmentId;
55 readonly label: string;
56 readonly connected: boolean;
57 readonly settings: ServerSettings | null;
58}
59
60/**
61 * Connected environments whose shared settings differ from the primary
62 * environment's. Offline environments are skipped: nothing can be read from
63 * or written to them, and the warning would never clear. With no primary
64 * settings loaded there is nothing to compare against, so nothing is
65 * reported. Callers must pass the real loaded settings, never a default
66 * fallback, or "apply to all" would push defaults over real values.
67 */
68export function findSharedSettingsMismatches(input: {
69 readonly primaryEnvironmentId: EnvironmentId | null;
70 readonly primarySettings: ServerSettings | null;
71 readonly environments: ReadonlyArray<SharedSettingsEnvironment>;
72}): ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }> {
73 if (input.primaryEnvironmentId === null || input.primarySettings === null) {
74 return [];
75 }
76 const expected = pickSharedServerSettings(input.primarySettings);
77 return input.environments.flatMap((environment) => {
78 if (
79 environment.environmentId === input.primaryEnvironmentId ||
80 !environment.connected ||
81 environment.settings === null
82 ) {
83 return [];
84 }
85 const actual = pickSharedServerSettings(environment.settings);
86 return Equal.equals(actual, expected)
87 ? []
88 : [{ environmentId: environment.environmentId, label: environment.label }];
89 });
90}