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

sharedSettings.ts

116 lines4.3 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 * 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. */
23export const SHARED_SERVER_SETTING_KEYS = [
24 "sidebarAutoSettleAfterDays",
25 "sidebarAutoSettleOnMerge",
26 "defaultThreadEnvMode",
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 };
54​
55/** The shared subset of one environment's settings, as a patch that can be written elsewhere. */
56export function pickSharedServerSettings(settings: ServerSettings): ServerSettingsPatch {
57 return Struct.pick(settings, SHARED_SERVER_SETTING_KEYS);
59​
60/**
61 * Whether an environment can participate in shared-settings sync right now.
62 * Auto-settlement is the newest feature backed by a shared key, so a server
63 * advertising `threadAutoSettlement` can hold every shared key.
64 */
65export function supportsSharedSettingsSync(environment: {
66 readonly connection: { readonly phase: EnvironmentConnectionPhase };
67 readonly serverConfig: {
68 readonly environment: {
69 readonly capabilities: Pick<ExecutionEnvironmentCapabilities, "threadAutoSettlement">;
70 };
71 } | null;
72}): boolean {
73 return (
74 environment.connection.phase === "connected" &&
75 environment.serverConfig?.environment.capabilities.threadAutoSettlement === true
76 );
78​
79export interface SharedSettingsEnvironment {
80 readonly environmentId: EnvironmentId;
81 readonly label: string;
82 readonly syncEligible: boolean;
83 readonly settings: ServerSettings | null;
85​
86/**
87 * Shared-settings sync targets whose values differ from the primary
88 * environment's. Other environments are skipped: nothing can be read from or
89 * written to them, or their server cannot hold every shared key. With no
90 * primary settings loaded there is nothing to compare against, so nothing is
91 * reported. Callers must pass the real loaded settings, never a default
92 * fallback, or "apply to all" would push defaults over real values.
93 */
94export function findSharedSettingsMismatches(input: {
95 readonly primaryEnvironmentId: EnvironmentId | null;
96 readonly primarySettings: ServerSettings | null;
97 readonly environments: ReadonlyArray<SharedSettingsEnvironment>;
98}): ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }> {
99 if (input.primaryEnvironmentId === null || input.primarySettings === null) {
100 return [];
101 }
102 const expected = pickSharedServerSettings(input.primarySettings);
103 return input.environments.flatMap((environment) => {
104 if (
105 environment.environmentId === input.primaryEnvironmentId ||
106 !environment.syncEligible ||
107 environment.settings === null
108 ) {
109 return [];
110 }
111 const actual = pickSharedServerSettings(environment.settings);
112 return Equal.equals(actual, expected)
113 ? []
114 : [{ environmentId: environment.environmentId, label: environment.label }];
115 });