The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | DEFAULT_SERVER_SETTINGS, |
| 3 | ModelSelection, |
| 4 | ProjectId, |
| 5 | ProjectScript, |
| 6 | keepUnknownOverrideKeys, |
| 7 | ProjectSettingsOverrides, |
| 8 | type ProviderInstanceConfig, |
| 9 | type ProviderInstanceMutation, |
| 10 | defaultInstanceIdForDriver, |
| 11 | isProviderDriverKind, |
| 12 | ProviderDriverKind, |
| 13 | ProviderInstanceId, |
| 14 | resolveProviderInstanceEnabled, |
| 15 | ResponseStreamingMode, |
| 16 | ServerSettings, |
| 17 | ServerSettingsError, |
| 18 | type ServerSettingsPatch, |
| 19 | type ManagedPolicySummary, |
| 20 | globalIdleCompaction, |
| 21 | isSecretReference, |
| 22 | type SecretReference, |
| 23 | withIdleCompaction, |
| 24 | } from "@t3tools/contracts"; |
| 25 | import * as Cache from "effect/Cache"; |
| 26 | import * as Cause from "effect/Cause"; |
| 27 | import * as Context from "effect/Context"; |
| 28 | import * as Deferred from "effect/Deferred"; |
| 29 | import * as Duration from "effect/Duration"; |
| 30 | import * as Equal from "effect/Equal"; |
| 31 | import * as Effect from "effect/Effect"; |
| 32 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 33 | import * as Exit from "effect/Exit"; |
| 34 | import * as FileSystem from "effect/FileSystem"; |
| 35 | import * as Layer from "effect/Layer"; |
| 36 | import * as Option from "effect/Option"; |
| 37 | import * as Path from "effect/Path"; |
| 38 | import * as PubSub from "effect/PubSub"; |
| 39 | import * as Ref from "effect/Ref"; |
| 40 | import * as Schema from "effect/Schema"; |
| 41 | import * as SchemaTransformation from "effect/SchemaTransformation"; |
| 42 | import * as Semaphore from "effect/Semaphore"; |
| 43 | import * as Scope from "effect/Scope"; |
| 44 | import * as Stream from "effect/Stream"; |
| 45 | import * as SqlClient from "effect/sql/SqlClient"; |
| 46 | import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite"; |
| 47 | import { resolveSymlinkTarget } from "@t3tools/shared/symlink"; |
| 48 | import * as ServerConfig from "./config.ts"; |
| 49 | import { type DeepPartial, deepMerge } from "@t3tools/shared/Struct"; |
| 50 | import { fromJsonStringPretty, fromLenientJson } from "@t3tools/shared/schemaJson"; |
| 51 | import { |
| 52 | applyServerSettingsPatch, |
| 53 | deriveLegacyProjectOverrides, |
| 54 | fallbackTextGenerationModelSelection, |
| 55 | isModelSelectionProviderEnabled, |
| 56 | } from "@t3tools/shared/serverSettings"; |
| 57 | import * as ServerSecretStore from "./auth/ServerSecretStore.ts"; |
| 58 | import * as ManagedPolicy from "./policy/ManagedPolicy.ts"; |
| 59 | import { secretProviderDefinitions } from "./secrets/secretProviderDefinitions.ts"; |
| 60 | import { watchDirectoryReplacements } from "./watchDirectoryReplacements.ts"; |
| 61 | import { |
| 62 | readSecretFieldValues, |
| 63 | sourceControlHostSecretName, |
| 64 | } from "./sourceControl/sourceControlHostSecrets.ts"; |
| 65 | |
| 66 | import { |
| 67 | mapSettingsSecrets, |
| 68 | mapSettingsSecretValues, |
| 69 | withoutRemovedSecretProviderReferences, |
| 70 | withoutRedirectedSimlockTokens, |
| 71 | SECRET_REDACTED, |
| 72 | type SettingsSecret, |
| 73 | } from "./settingsSecrets.ts"; |
| 74 | |
| 75 | export { |
| 76 | resolveReviewGuideModelSelection, |
| 77 | resolveSourceControlWriterModelSelection, |
| 78 | } from "@t3tools/shared/serverSettings"; |
| 79 | |
| 80 | const encodeServerSettings = Schema.encodeEffect(ServerSettings); |
| 81 | const encodeServerSettingsJson = Schema.encodeUnknownEffect(fromJsonStringPretty(ServerSettings)); |
| 82 | const decodeServerSettings = Schema.decodeUnknownEffect(ServerSettings); |
| 83 | |
| 84 | const textEncoder = new TextEncoder(); |
| 85 | const textDecoder = new TextDecoder(); |
| 86 | |
| 87 | // Conflicting flags resolve to false to preserve user disables. |
| 88 | const foldProviderInstanceEnabledFlags = (settings: ServerSettings): ServerSettings => { |
| 89 | let changed = false; |
| 90 | const providerInstances: Record<string, ProviderInstanceConfig> = {}; |
| 91 | for (const [instanceId, instance] of Object.entries(settings.providerInstances)) { |
| 92 | const config = instance.config; |
| 93 | // Leave malformed flags for driver schema validation rather than silently repairing them. |
| 94 | if ( |
| 95 | config === null || |
| 96 | typeof config !== "object" || |
| 97 | Array.isArray(config) || |
| 98 | typeof (config as { readonly enabled?: unknown }).enabled !== "boolean" |
| 99 | ) { |
| 100 | providerInstances[instanceId] = instance; |
| 101 | continue; |
| 102 | } |
| 103 | const { enabled: configEnabled, ...restConfig } = config as Record<string, unknown> & { |
| 104 | readonly enabled: boolean; |
| 105 | }; |
| 106 | const resolved = |
| 107 | instance.enabled === false || configEnabled === false |
| 108 | ? false |
| 109 | : (instance.enabled ?? configEnabled); |
| 110 | changed = true; |
| 111 | providerInstances[instanceId] = { |
| 112 | ...instance, |
| 113 | enabled: resolved, |
| 114 | config: restConfig, |
| 115 | } satisfies ProviderInstanceConfig; |
| 116 | } |
| 117 | if (!changed) { |
| 118 | return settings; |
| 119 | } |
| 120 | return { |
| 121 | ...settings, |
| 122 | providerInstances: providerInstances as ServerSettings["providerInstances"], |
| 123 | }; |
| 124 | }; |
| 125 | |
| 126 | const normalizeServerSettings = ( |
| 127 | settings: ServerSettings, |
| 128 | ): Effect.Effect<ServerSettings, ServerSettingsError> => |
| 129 | encodeServerSettings(settings).pipe( |
| 130 | Effect.flatMap(decodeServerSettings), |
| 131 | Effect.map(foldProviderInstanceEnabledFlags), |
| 132 | Effect.map((next) => withIdleCompaction(next, globalIdleCompaction(next))), |
| 133 | Effect.map((next) => ({ ...next, ...deriveLegacyProjectOverrides(next) })), |
| 134 | Effect.mapError( |
| 135 | (cause) => |
| 136 | new ServerSettingsError({ |
| 137 | settingsPath: "<memory>", |
| 138 | operation: "normalize", |
| 139 | cause, |
| 140 | }), |
| 141 | ), |
| 142 | ); |
| 143 | |
| 144 | export function redactServerSettingsForClient( |
| 145 | settings: ServerSettings, |
| 146 | client: { readonly secretReferences: boolean }, |
| 147 | ): ServerSettings { |
| 148 | const redacted = mapSettingsSecrets(settings, (secret) => |
| 149 | secret.sensitive |
| 150 | ? { value: "", redacted: secret.redacted || secret.value.length > 0 } |
| 151 | : { value: secret.value, redacted: false }, |
| 152 | ); |
| 153 | // Older clients decode secret fields as strings and fail on a reference, so they see a saved secret. |
| 154 | const shaped = client.secretReferences |
| 155 | ? redacted |
| 156 | : mapSettingsSecretValues(redacted, (value) => |
| 157 | isSecretReference(value) ? SECRET_REDACTED : value, |
| 158 | ); |
| 159 | return { ...shaped, secretProviders: withoutUnknownSecretProviderConfigs(shaped) }; |
| 160 | } |
| 161 | |
| 162 | /** A client shown a reference as a saved secret sends that marker back; it keeps the reference. */ |
| 163 | function withEchoedSecretReferences(current: ServerSettings, next: ServerSettings): ServerSettings { |
| 164 | const references = new Map<string, SecretReference>(); |
| 165 | mapSettingsSecretValues(current, (value, secretName) => { |
| 166 | if (isSecretReference(value)) references.set(secretName, value); |
| 167 | return value; |
| 168 | }); |
| 169 | if (references.size === 0) return next; |
| 170 | return mapSettingsSecretValues(next, (value, secretName) => |
| 171 | value === SECRET_REDACTED ? (references.get(secretName) ?? value) : value, |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | // A newer kind's config may hold credentials this build cannot recognize, so it never leaves the server. |
| 176 | function withoutUnknownSecretProviderConfigs( |
| 177 | settings: ServerSettings, |
| 178 | ): ServerSettings["secretProviders"] { |
| 179 | return Object.fromEntries( |
| 180 | Object.entries(settings.secretProviders).map(([id, entry]) => { |
| 181 | if (secretProviderConfigGuards.has(entry.kind)) return [id, entry]; |
| 182 | const { config: _config, ...envelope } = entry; |
| 183 | return [id, envelope]; |
| 184 | }), |
| 185 | ) as ServerSettings["secretProviders"]; |
| 186 | } |
| 187 | |
| 188 | export function applyProviderInstanceMutation( |
| 189 | settings: ServerSettings, |
| 190 | mutation: ProviderInstanceMutation, |
| 191 | ): ServerSettings { |
| 192 | const providerInstances = { ...settings.providerInstances }; |
| 193 | if (mutation.operation === "upsert" || mutation.operation === "create") { |
| 194 | providerInstances[mutation.instanceId] = mutation.instance; |
| 195 | } else { |
| 196 | delete providerInstances[mutation.instanceId]; |
| 197 | } |
| 198 | return { ...settings, providerInstances }; |
| 199 | } |
| 200 | |
| 201 | const secretProviderConfigGuards = new Map( |
| 202 | secretProviderDefinitions.definitions.map((definition) => [ |
| 203 | definition.kind as string, |
| 204 | Schema.is(definition.configSchema), |
| 205 | ]), |
| 206 | ); |
| 207 | |
| 208 | // Rejects a kind this server has not loaded rather than storing a config it cannot check. |
| 209 | function ensureSecretProviderWritesAllowed( |
| 210 | patch: ServerSettingsPatch, |
| 211 | settingsPath: string, |
| 212 | ): Effect.Effect<void, ServerSettingsError> { |
| 213 | for (const [id, entry] of Object.entries(patch.secretProviders ?? {})) { |
| 214 | if (entry === null) continue; |
| 215 | const isConfig = secretProviderConfigGuards.get(entry.kind); |
| 216 | const problem = |
| 217 | isConfig === undefined |
| 218 | ? `Unsupported secret provider kind: ${entry.kind}.` |
| 219 | : isConfig(entry.config) |
| 220 | ? undefined |
| 221 | : `Invalid ${entry.kind} config for secret provider ${id}.`; |
| 222 | if (problem !== undefined) { |
| 223 | return Effect.fail( |
| 224 | new ServerSettingsError({ |
| 225 | settingsPath, |
| 226 | operation: "write-secret-provider", |
| 227 | cause: new Error(problem), |
| 228 | }), |
| 229 | ); |
| 230 | } |
| 231 | } |
| 232 | return Effect.void; |
| 233 | } |
| 234 | |
| 235 | function ensureProviderInstanceMutationAllowed( |
| 236 | settings: ServerSettings, |
| 237 | mutation: ProviderInstanceMutation, |
| 238 | settingsPath: string, |
| 239 | ): Effect.Effect<void, ServerSettingsError> { |
| 240 | if ( |
| 241 | mutation.operation === "create" && |
| 242 | Object.hasOwn(settings.providerInstances, mutation.instanceId) |
| 243 | ) { |
| 244 | return Effect.fail( |
| 245 | new ServerSettingsError({ |
| 246 | settingsPath, |
| 247 | operation: "create-provider-instance", |
| 248 | providerInstanceId: mutation.instanceId, |
| 249 | }), |
| 250 | ); |
| 251 | } |
| 252 | return Effect.void; |
| 253 | } |
| 254 | |
| 255 | export class ServerSettingsService extends Context.Service< |
| 256 | ServerSettingsService, |
| 257 | { |
| 258 | readonly start: Effect.Effect<void, ServerSettingsError>; |
| 259 | |
| 260 | readonly ready: Effect.Effect<void, ServerSettingsError>; |
| 261 | |
| 262 | /** The administrator's policy as clients see it; absent when unmanaged. */ |
| 263 | readonly managedPolicy?: ManagedPolicySummary | undefined; |
| 264 | |
| 265 | readonly getSettings: Effect.Effect<ServerSettings, ServerSettingsError>; |
| 266 | |
| 267 | readonly updateSettings: ( |
| 268 | patch: ServerSettingsPatch, |
| 269 | ) => Effect.Effect<ServerSettings, ServerSettingsError>; |
| 270 | |
| 271 | readonly updateProviderInstance: ( |
| 272 | mutation: ProviderInstanceMutation, |
| 273 | patch?: ServerSettingsPatch, |
| 274 | ) => Effect.Effect<ServerSettings, ServerSettingsError>; |
| 275 | |
| 276 | readonly withSettingsSnapshot: <A, E, R>( |
| 277 | use: (settings: ServerSettings) => Effect.Effect<A, E, R>, |
| 278 | ) => Effect.Effect<A, E | ServerSettingsError, R>; |
| 279 | |
| 280 | readonly streamChanges: Stream.Stream<ServerSettings>; |
| 281 | |
| 282 | /** Subscribe before reading a snapshot to retain changes before lazy stream startup. */ |
| 283 | readonly subscribeChanges: Effect.Effect<Stream.Stream<ServerSettings>, never, Scope.Scope>; |
| 284 | } |
| 285 | >()("t3/serverSettings/ServerSettingsService") { |
| 286 | static readonly layerTest = ( |
| 287 | overrides: DeepPartial<ServerSettings> = {}, |
| 288 | managedPolicy?: ManagedPolicySummary, |
| 289 | ) => layerTest(overrides, managedPolicy); |
| 290 | } |
| 291 | |
| 292 | const makeTest = Effect.fn("serverSettings.makeTest")(function* ( |
| 293 | overrides: DeepPartial<ServerSettings> = {}, |
| 294 | managedPolicy?: ManagedPolicySummary, |
| 295 | ) { |
| 296 | const { automaticGitFetchInterval, providerHealthRefreshInterval, ...overridesForMerge } = |
| 297 | overrides; |
| 298 | const merged = deepMerge(DEFAULT_SERVER_SETTINGS, overridesForMerge); |
| 299 | const initialSettings = yield* normalizeServerSettings({ |
| 300 | ...merged, |
| 301 | ...(automaticGitFetchInterval !== undefined |
| 302 | ? { automaticGitFetchInterval: automaticGitFetchInterval as Duration.Duration } |
| 303 | : {}), |
| 304 | ...(providerHealthRefreshInterval !== undefined |
| 305 | ? { providerHealthRefreshInterval: providerHealthRefreshInterval as Duration.Duration } |
| 306 | : {}), |
| 307 | }); |
| 308 | const currentSettingsRef = yield* Ref.make<ServerSettings>(initialSettings); |
| 309 | const writeSemaphore = yield* Semaphore.make(1); |
| 310 | const getSettings = Ref.get(currentSettingsRef).pipe(Effect.map(resolveTextGenerationProvider)); |
| 311 | |
| 312 | const updateTestSettings = ( |
| 313 | update: (current: ServerSettings) => Effect.Effect<ServerSettings, ServerSettingsError>, |
| 314 | ): Effect.Effect<ServerSettings, ServerSettingsError> => |
| 315 | writeSemaphore.withPermits(1)( |
| 316 | Ref.get(currentSettingsRef).pipe( |
| 317 | Effect.flatMap(update), |
| 318 | Effect.flatMap(normalizeServerSettings), |
| 319 | Effect.tap((nextSettings) => Ref.set(currentSettingsRef, nextSettings)), |
| 320 | Effect.map(resolveTextGenerationProvider), |
| 321 | ), |
| 322 | ); |
| 323 | |
| 324 | return { |
| 325 | managedPolicy, |
| 326 | start: Effect.void, |
| 327 | ready: Effect.void, |
| 328 | getSettings, |
| 329 | updateSettings: (patch) => |
| 330 | updateTestSettings((currentSettings) => |
| 331 | Effect.succeed(applyServerSettingsPatch(currentSettings, patch)), |
| 332 | ), |
| 333 | updateProviderInstance: (mutation, patch = {}) => |
| 334 | updateTestSettings((currentSettings) => |
| 335 | Effect.gen(function* () { |
| 336 | yield* ensureProviderInstanceMutationAllowed(currentSettings, mutation, "test settings"); |
| 337 | const patched = applyServerSettingsPatch(currentSettings, patch); |
| 338 | return applyProviderInstanceMutation(patched, mutation); |
| 339 | }), |
| 340 | ), |
| 341 | withSettingsSnapshot: (use) => |
| 342 | writeSemaphore.withPermits(1)(getSettings.pipe(Effect.flatMap(use))), |
| 343 | streamChanges: Stream.empty, |
| 344 | subscribeChanges: Effect.succeed(Stream.empty), |
| 345 | } satisfies ServerSettingsService["Service"]; |
| 346 | }); |
| 347 | |
| 348 | /** Test settings; `managedPolicy` only tells consumers a policy applies, it does not lock values. */ |
| 349 | export const layerTest = ( |
| 350 | overrides: DeepPartial<ServerSettings> = {}, |
| 351 | managedPolicy?: ManagedPolicySummary, |
| 352 | ) => Layer.effect(ServerSettingsService, makeTest(overrides, managedPolicy)); |
| 353 | |
| 354 | // Accept retired token delivery only on load so one old value cannot reset all settings. |
| 355 | const PersistedResponseStreamingMode = Schema.Union([ |
| 356 | ResponseStreamingMode, |
| 357 | Schema.Literal("token"), |
| 358 | ]).pipe( |
| 359 | Schema.decodeTo( |
| 360 | ResponseStreamingMode, |
| 361 | SchemaTransformation.transform({ |
| 362 | decode: (mode) => (mode === "token" ? "paragraph" : mode), |
| 363 | encode: (mode) => mode, |
| 364 | }), |
| 365 | ), |
| 366 | ); |
| 367 | const PersistedServerSettings = Schema.Struct({ |
| 368 | ...ServerSettings.fields, |
| 369 | responseStreamingMode: PersistedResponseStreamingMode.pipe( |
| 370 | Schema.withDecodingDefault(Effect.succeed("paragraph" as const)), |
| 371 | ), |
| 372 | projectSettingsOverrides: Schema.Record( |
| 373 | ProjectId, |
| 374 | keepUnknownOverrideKeys( |
| 375 | Schema.Struct({ |
| 376 | ...ProjectSettingsOverrides.fields, |
| 377 | responseStreamingMode: Schema.optionalKey(PersistedResponseStreamingMode), |
| 378 | }), |
| 379 | ), |
| 380 | ).pipe(Schema.withDecodingDefault(Effect.succeed({}))), |
| 381 | }); |
| 382 | // Parse first and decode second, so a managed policy can fill its defaults in between. |
| 383 | const decodeSettingsFileJsonExit = Schema.decodeUnknownExit(fromLenientJson(Schema.Unknown)); |
| 384 | const decodePersistedServerSettingsExit = Schema.decodeUnknownExit(PersistedServerSettings); |
| 385 | const LegacyProviderSettingsJson = fromLenientJson( |
| 386 | Schema.Struct({ |
| 387 | providers: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)), |
| 388 | bitbucket: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)), |
| 389 | github: Schema.optionalKey(Schema.Record(Schema.String, Schema.Unknown)), |
| 390 | }), |
| 391 | ); |
| 392 | const decodeLegacyProviderSettingsJsonExit = Schema.decodeUnknownExit(LegacyProviderSettingsJson); |
| 393 | |
| 394 | const LEGACY_BITBUCKET_SECRET_NAMES: Readonly<Record<string, string>> = { |
| 395 | accessToken: "bitbucket-access-token", |
| 396 | apiToken: "bitbucket-api-token", |
| 397 | }; |
| 398 | const legacyGitHubTokenSecretName = (host: string) => |
| 399 | `github-token-${Buffer.from(host.trim().toLowerCase(), "utf8").toString("base64url")}`; |
| 400 | |
| 401 | function migrateLegacySourceControlSettings( |
| 402 | settings: ServerSettings, |
| 403 | legacy: { |
| 404 | readonly bitbucket?: Readonly<Record<string, unknown>> | undefined; |
| 405 | readonly github?: Readonly<Record<string, unknown>> | undefined; |
| 406 | }, |
| 407 | ): { |
| 408 | readonly settings: ServerSettings; |
| 409 | readonly secretMoves: ReadonlyArray<{ readonly from: string; readonly to: string }>; |
| 410 | } { |
| 411 | const sourceControlHosts = { ...settings.sourceControlHosts }; |
| 412 | const secretMoves: Array<{ readonly from: string; readonly to: string }> = []; |
| 413 | if (legacy.bitbucket !== undefined && sourceControlHosts.bitbucket === undefined) { |
| 414 | sourceControlHosts.bitbucket = legacy.bitbucket; |
| 415 | for (const [field, from] of Object.entries(LEGACY_BITBUCKET_SECRET_NAMES)) { |
| 416 | if (legacy.bitbucket[field] !== SECRET_REDACTED) continue; |
| 417 | secretMoves.push({ |
| 418 | from, |
| 419 | to: sourceControlHostSecretName({ kind: "bitbucket", field, serverHost: null }), |
| 420 | }); |
| 421 | } |
| 422 | } |
| 423 | if (legacy.github !== undefined && sourceControlHosts.github === undefined) { |
| 424 | sourceControlHosts.github = legacy.github; |
| 425 | for (const [serverHost, value] of readSecretFieldValues(legacy.github.tokens)) { |
| 426 | if (serverHost === null || value !== SECRET_REDACTED) continue; |
| 427 | secretMoves.push({ |
| 428 | from: legacyGitHubTokenSecretName(serverHost), |
| 429 | to: sourceControlHostSecretName({ kind: "github", field: "tokens", serverHost }), |
| 430 | }); |
| 431 | } |
| 432 | } |
| 433 | return { settings: { ...settings, sourceControlHosts }, secretMoves }; |
| 434 | } |
| 435 | |
| 436 | const HISTORY_RESTORED_DRIVERS: ReadonlySet<ProviderDriverKind> = new Set( |
| 437 | ["cursor", "grok", "opencode"].map((driver) => ProviderDriverKind.make(driver)), |
| 438 | ); |
| 439 | |
| 440 | function migrateLegacyProviderSettings( |
| 441 | settings: ServerSettings, |
| 442 | legacyProviders: Readonly<Record<string, unknown>>, |
| 443 | providerHistory: ReadonlyArray<{ |
| 444 | readonly providerName: string; |
| 445 | readonly providerInstanceId: string | null; |
| 446 | }>, |
| 447 | ): ServerSettings { |
| 448 | const usedProviders = new Set<string>(providerHistory.map(({ providerName }) => providerName)); |
| 449 | const usedProviderInstances = new Set<string>( |
| 450 | providerHistory.map( |
| 451 | ({ providerName, providerInstanceId }) => providerInstanceId ?? providerName, |
| 452 | ), |
| 453 | ); |
| 454 | |
| 455 | const providerInstances: Record<ProviderInstanceId, ProviderInstanceConfig> = {}; |
| 456 | for (const [instanceId, instance] of Object.entries(settings.providerInstances) as Array< |
| 457 | [ProviderInstanceId, ProviderInstanceConfig] |
| 458 | >) { |
| 459 | providerInstances[instanceId] = |
| 460 | instance.enabled === undefined && |
| 461 | HISTORY_RESTORED_DRIVERS.has(instance.driver) && |
| 462 | usedProviderInstances.has(instanceId) |
| 463 | ? { ...instance, enabled: true } |
| 464 | : instance; |
| 465 | } |
| 466 | |
| 467 | const legacyEntries = new Map(Object.entries(legacyProviders)); |
| 468 | for (const driver of HISTORY_RESTORED_DRIVERS) { |
| 469 | if (!legacyEntries.has(driver)) legacyEntries.set(driver, {}); |
| 470 | } |
| 471 | for (const [kind, blob] of legacyEntries) { |
| 472 | if (!isProviderDriverKind(kind) || blob === null || typeof blob !== "object") continue; |
| 473 | if (Array.isArray(blob)) continue; |
| 474 | const driver = kind; |
| 475 | const instanceId = defaultInstanceIdForDriver(driver); |
| 476 | if (Object.hasOwn(providerInstances, instanceId)) continue; |
| 477 | |
| 478 | const { enabled: rawEnabled, ...config } = blob as Record<string, unknown>; |
| 479 | const explicitEnabled = typeof rawEnabled === "boolean" ? rawEnabled : undefined; |
| 480 | const enabled = |
| 481 | explicitEnabled ?? |
| 482 | (HISTORY_RESTORED_DRIVERS.has(driver) && usedProviders.has(driver) ? true : undefined); |
| 483 | const driverDefault = resolveProviderInstanceEnabled({ driver, config: {} }); |
| 484 | if (Object.keys(config).length === 0 && (enabled === undefined || enabled === driverDefault)) { |
| 485 | continue; |
| 486 | } |
| 487 | providerInstances[instanceId] = { |
| 488 | driver, |
| 489 | ...(enabled === undefined ? {} : { enabled }), |
| 490 | config, |
| 491 | } satisfies ProviderInstanceConfig; |
| 492 | } |
| 493 | |
| 494 | return { |
| 495 | ...settings, |
| 496 | providerInstances, |
| 497 | }; |
| 498 | } |
| 499 | |
| 500 | const ACP_REGISTRY_DRIVER = ProviderDriverKind.make("acpRegistry"); |
| 501 | |
| 502 | function selectionSupportsTextGeneration( |
| 503 | settings: ServerSettings, |
| 504 | selection: ModelSelection, |
| 505 | ): boolean { |
| 506 | return settings.providerInstances[selection.instanceId]?.driver !== ACP_REGISTRY_DRIVER; |
| 507 | } |
| 508 | |
| 509 | type DriverAllowed = (driver: ProviderDriverKind) => boolean; |
| 510 | const anyDriver: DriverAllowed = () => true; |
| 511 | |
| 512 | function resolveTextGenerationProvider( |
| 513 | settings: ServerSettings, |
| 514 | isDriverAllowed: DriverAllowed = anyDriver, |
| 515 | ): ServerSettings { |
| 516 | const selection = settings.textGenerationModelSelection; |
| 517 | return isModelSelectionProviderEnabled(settings, selection) && |
| 518 | selectionSupportsTextGeneration(settings, selection) && |
| 519 | isDriverAllowed( |
| 520 | settings.providerInstances[selection.instanceId]?.driver ?? |
| 521 | ProviderDriverKind.make(selection.instanceId), |
| 522 | ) |
| 523 | ? settings |
| 524 | : fallbackTextGenerationProvider(settings, isDriverAllowed); |
| 525 | } |
| 526 | |
| 527 | function fallbackTextGenerationProvider( |
| 528 | settings: ServerSettings, |
| 529 | isDriverAllowed: DriverAllowed, |
| 530 | ): ServerSettings { |
| 531 | const fallback = fallbackTextGenerationModelSelection( |
| 532 | settings.providerInstances, |
| 533 | (instanceId, driver) => |
| 534 | // A default instance id can be configured with another driver. |
| 535 | isDriverAllowed(settings.providerInstances[instanceId]?.driver ?? driver) && |
| 536 | resolveProviderInstanceEnabled( |
| 537 | settings.providerInstances[instanceId] ?? { driver, config: {} }, |
| 538 | ), |
| 539 | { defaultModelSelection: settings.defaultModelSelection }, |
| 540 | ); |
| 541 | return fallback === null ? settings : { ...settings, textGenerationModelSelection: fallback }; |
| 542 | } |
| 543 | |
| 544 | const ATOMIC_SETTINGS_KEYS: ReadonlySet<string> = new Set([ |
| 545 | "backgroundActivity", |
| 546 | "automaticGitFetchInterval", |
| 547 | "providerHealthRefreshInterval", |
| 548 | "sourceControlWriterModelSelection", |
| 549 | "reviewGuideModelSelection", |
| 550 | "textGenerationModelSelection", |
| 551 | "fallbackModelSelection", |
| 552 | "pullRequestMergeMethod", |
| 553 | // Empty by default, these hold one whole value, which a managed policy can default. |
| 554 | ...Object.entries(DEFAULT_SERVER_SETTINGS).flatMap(([key, value]) => |
| 555 | value === null ? [key] : [], |
| 556 | ), |
| 557 | ]); |
| 558 | |
| 559 | function stripDefaultServerSettings(current: unknown, defaults: unknown): unknown | undefined { |
| 560 | if (Array.isArray(current) || Array.isArray(defaults)) { |
| 561 | return Equal.equals(current, defaults) ? undefined : current; |
| 562 | } |
| 563 | |
| 564 | if ( |
| 565 | current !== null && |
| 566 | defaults !== null && |
| 567 | typeof current === "object" && |
| 568 | typeof defaults === "object" |
| 569 | ) { |
| 570 | const currentRecord = current as Record<string, unknown>; |
| 571 | const defaultsRecord = defaults as Record<string, unknown>; |
| 572 | const next: Record<string, unknown> = {}; |
| 573 | |
| 574 | for (const key of Object.keys(currentRecord)) { |
| 575 | if (ATOMIC_SETTINGS_KEYS.has(key)) { |
| 576 | if (!Equal.equals(currentRecord[key], defaultsRecord[key])) { |
| 577 | next[key] = currentRecord[key]; |
| 578 | } |
| 579 | } else { |
| 580 | const stripped = stripDefaultServerSettings(currentRecord[key], defaultsRecord[key]); |
| 581 | if (stripped !== undefined) { |
| 582 | next[key] = stripped; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return Object.keys(next).length > 0 ? next : undefined; |
| 588 | } |
| 589 | |
| 590 | return Object.is(current, defaults) ? undefined : current; |
| 591 | } |
| 592 | |
| 593 | const decodeProjectScriptsJson = Schema.decodeUnknownOption( |
| 594 | Schema.fromJsonString(Schema.Array(ProjectScript)), |
| 595 | ); |
| 596 | const decodeModelSelectionJson = Schema.decodeUnknownOption( |
| 597 | Schema.fromJsonString(Schema.NullOr(ModelSelection)), |
| 598 | ); |
| 599 | |
| 600 | interface LegacyProjectSettingsRow { |
| 601 | readonly projectId: string; |
| 602 | readonly defaultModelSelection: string | null; |
| 603 | readonly defaultThreadEnvMode: string | null; |
| 604 | readonly autoPull: number; |
| 605 | readonly scripts: string; |
| 606 | } |
| 607 | |
| 608 | // projectSettingsFolded prevents restarts from undoing later UI resets. |
| 609 | function foldLegacyProjectSettings( |
| 610 | settings: ServerSettings, |
| 611 | rows: ReadonlyArray<LegacyProjectSettingsRow>, |
| 612 | ): ServerSettings { |
| 613 | if (settings.projectSettingsFolded) return settings; |
| 614 | if ( |
| 615 | rows.length === 0 && |
| 616 | Object.keys(settings.projectAgentBrowserAccessOverrides).length === 0 && |
| 617 | Object.keys(settings.projectAutoPullOverrides).length === 0 && |
| 618 | Object.keys(settings.projectScriptOverrides).length === 0 |
| 619 | ) { |
| 620 | return settings; |
| 621 | } |
| 622 | const entries: Record<string, ProjectSettingsOverrides> = { |
| 623 | ...settings.projectSettingsOverrides, |
| 624 | }; |
| 625 | const set = <K extends keyof ProjectSettingsOverrides>( |
| 626 | projectId: string, |
| 627 | key: K, |
| 628 | value: ProjectSettingsOverrides[K] | undefined, |
| 629 | ) => { |
| 630 | if (value === undefined) return; |
| 631 | const entry = entries[projectId] ?? {}; |
| 632 | if (Object.hasOwn(entry, key)) return; |
| 633 | entries[projectId] = { ...entry, [key]: value }; |
| 634 | }; |
| 635 | for (const [projectId, value] of Object.entries(settings.projectAgentBrowserAccessOverrides)) { |
| 636 | set(projectId, "enableAgentBrowserAccess", value); |
| 637 | } |
| 638 | for (const [projectId, value] of Object.entries(settings.projectAutoPullOverrides)) { |
| 639 | set(projectId, "defaultAutoPull", value); |
| 640 | } |
| 641 | // A stored null resets to machine defaults, so the project's aggregate scripts must not resurface. |
| 642 | const resetScripts = new Set<string>(); |
| 643 | for (const [projectId, value] of Object.entries(settings.projectScriptOverrides)) { |
| 644 | if (value === null) resetScripts.add(projectId); |
| 645 | else set(projectId, "defaultProjectScripts", value); |
| 646 | } |
| 647 | for (const row of rows) { |
| 648 | const model = decodeModelSelectionJson(row.defaultModelSelection ?? "null"); |
| 649 | if (Option.isSome(model) && model.value !== null) { |
| 650 | set(row.projectId, "defaultModelSelection", model.value); |
| 651 | } |
| 652 | if (row.defaultThreadEnvMode === "local" || row.defaultThreadEnvMode === "worktree") { |
| 653 | set(row.projectId, "defaultThreadEnvMode", row.defaultThreadEnvMode); |
| 654 | } |
| 655 | if (row.autoPull === 1) set(row.projectId, "defaultAutoPull", true); |
| 656 | const scripts = decodeProjectScriptsJson(row.scripts); |
| 657 | if (Option.isSome(scripts) && scripts.value.length > 0 && !resetScripts.has(row.projectId)) { |
| 658 | set(row.projectId, "defaultProjectScripts", scripts.value); |
| 659 | } |
| 660 | } |
| 661 | const projectSettingsOverrides = Object.fromEntries( |
| 662 | Object.entries(entries).filter(([, entry]) => Object.keys(entry).length > 0), |
| 663 | ); |
| 664 | return { |
| 665 | ...settings, |
| 666 | projectSettingsOverrides, |
| 667 | projectSettingsFolded: true, |
| 668 | ...deriveLegacyProjectOverrides({ projectSettingsOverrides }), |
| 669 | }; |
| 670 | } |
| 671 | |
| 672 | const make = Effect.gen(function* () { |
| 673 | const { settingsPath } = yield* ServerConfig.ServerConfig; |
| 674 | const fs = yield* FileSystem.FileSystem; |
| 675 | const pathService = yield* Path.Path; |
| 676 | const secretStore = yield* ServerSecretStore.ServerSecretStore; |
| 677 | const sql = yield* SqlClient.SqlClient; |
| 678 | const { |
| 679 | settings: managed, |
| 680 | providers: managedProviders, |
| 681 | summary: managedPolicy, |
| 682 | } = yield* ManagedPolicy.ManagedPolicy; |
| 683 | // Locks are laid over every settings value that leaves this service, never persisted. |
| 684 | const toEffective = (settings: ServerSettings) => |
| 685 | resolveTextGenerationProvider( |
| 686 | managedProviders.applyLimits(managed.applyLocks(settings)), |
| 687 | managedProviders.isDriverAllowed, |
| 688 | ); |
| 689 | const writeSemaphore = yield* Semaphore.make(1); |
| 690 | const cacheKey = "settings" as const; |
| 691 | const changesPubSub = yield* PubSub.unbounded<ServerSettings>(); |
| 692 | const startedRef = yield* Ref.make(false); |
| 693 | const startedDeferred = yield* Deferred.make<void, ServerSettingsError>(); |
| 694 | const watcherScope = yield* Scope.make("sequential"); |
| 695 | yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void)); |
| 696 | |
| 697 | const emitChange = (settings: ServerSettings) => |
| 698 | PubSub.publish(changesPubSub, settings).pipe(Effect.asVoid); |
| 699 | |
| 700 | const readConfigExists = fs.exists(settingsPath).pipe( |
| 701 | Effect.mapError( |
| 702 | (cause) => |
| 703 | new ServerSettingsError({ |
| 704 | settingsPath, |
| 705 | operation: "check-exists", |
| 706 | cause, |
| 707 | }), |
| 708 | ), |
| 709 | ); |
| 710 | |
| 711 | const readRawConfig = fs.readFileString(settingsPath).pipe( |
| 712 | Effect.mapError( |
| 713 | (cause) => |
| 714 | new ServerSettingsError({ |
| 715 | settingsPath, |
| 716 | operation: "read-file", |
| 717 | cause, |
| 718 | }), |
| 719 | ), |
| 720 | ); |
| 721 | |
| 722 | // The file before this write, parsed; only read when policy defaults need it. |
| 723 | const readStoredSettings = Effect.gen(function* () { |
| 724 | if (!(yield* readConfigExists)) return undefined; |
| 725 | const parsed = decodeSettingsFileJsonExit(yield* readRawConfig); |
| 726 | return Exit.isSuccess(parsed) ? parsed.value : undefined; |
| 727 | }); |
| 728 | |
| 729 | const writeSettingsAtomically = Effect.fnUntraced( |
| 730 | function* (settings: ServerSettings) { |
| 731 | const stripped = stripDefaultServerSettings(settings, managed.defaults) ?? {}; |
| 732 | const sparseSettingsJson = yield* encodeServerSettingsJson( |
| 733 | managed.keepStored === undefined |
| 734 | ? stripped |
| 735 | : managed.keepStored(stripped, settings, yield* readStoredSettings, (key) => |
| 736 | ATOMIC_SETTINGS_KEYS.has(key), |
| 737 | ), |
| 738 | ); |
| 739 | |
| 740 | return yield* writeFileStringAtomically({ |
| 741 | filePath: settingsPath, |
| 742 | contents: `${sparseSettingsJson}\n`, |
| 743 | }).pipe( |
| 744 | Effect.provideService(FileSystem.FileSystem, fs), |
| 745 | Effect.provideService(Path.Path, pathService), |
| 746 | ); |
| 747 | }, |
| 748 | Effect.mapError( |
| 749 | (cause) => |
| 750 | new ServerSettingsError({ |
| 751 | settingsPath, |
| 752 | operation: "write-file", |
| 753 | cause, |
| 754 | }), |
| 755 | ), |
| 756 | ); |
| 757 | |
| 758 | const moveInlineSettingsSecrets = Effect.fn("serverSettings.moveInlineSettingsSecrets")( |
| 759 | function* (settings: ServerSettings) { |
| 760 | const pending: SettingsSecret[] = []; |
| 761 | mapSettingsSecrets(settings, (secret) => { |
| 762 | if (secret.kind === "field" && !secret.redacted && secret.value.length > 0) { |
| 763 | pending.push(secret); |
| 764 | } |
| 765 | return secret; |
| 766 | }); |
| 767 | const moved = new Set<string>(); |
| 768 | for (const secret of pending) { |
| 769 | const stored = yield* secretStore |
| 770 | .set(secret.secretName, textEncoder.encode(secret.value)) |
| 771 | .pipe( |
| 772 | Effect.as(true), |
| 773 | Effect.catch(() => |
| 774 | Effect.logWarning("failed to move a settings secret into the secret store").pipe( |
| 775 | Effect.as(false), |
| 776 | ), |
| 777 | ), |
| 778 | ); |
| 779 | if (stored) moved.add(secret.secretName); |
| 780 | } |
| 781 | if (moved.size === 0) return settings; |
| 782 | return mapSettingsSecrets(settings, (secret) => |
| 783 | moved.has(secret.secretName) ? { value: "", redacted: true } : secret, |
| 784 | ); |
| 785 | }, |
| 786 | ); |
| 787 | |
| 788 | const loadSettingsFromDisk = Effect.gen(function* () { |
| 789 | let settings = managed.defaults; |
| 790 | let legacyProviders: Readonly<Record<string, unknown>> | undefined; |
| 791 | let legacySourceControl: Parameters<typeof migrateLegacySourceControlSettings>[1] = {}; |
| 792 | let parseFailure = Option.none<unknown>(); |
| 793 | |
| 794 | if (yield* readConfigExists) { |
| 795 | const raw = yield* readRawConfig; |
| 796 | const parsed = decodeSettingsFileJsonExit(raw); |
| 797 | const decoded = Exit.isFailure(parsed) |
| 798 | ? parsed |
| 799 | : decodePersistedServerSettingsExit(managed.withDefaults(parsed.value)); |
| 800 | const legacySettings = decodeLegacyProviderSettingsJsonExit(raw); |
| 801 | if (legacySettings._tag === "Success") { |
| 802 | legacyProviders = legacySettings.value.providers; |
| 803 | legacySourceControl = { |
| 804 | bitbucket: legacySettings.value.bitbucket, |
| 805 | github: legacySettings.value.github, |
| 806 | }; |
| 807 | } |
| 808 | if (decoded._tag === "Failure" || legacySettings._tag === "Failure") { |
| 809 | const failure = decoded._tag === "Failure" ? decoded : legacySettings; |
| 810 | // The file may hold an opt-out we can no longer read; never resume threads on a guess. |
| 811 | settings = { |
| 812 | ...managed.defaults, |
| 813 | continueThreadsAfterServerUpdate: false, |
| 814 | usageHistoryRetentionDays: "unlimited", |
| 815 | }; |
| 816 | if (failure._tag === "Failure") { |
| 817 | parseFailure = Option.some(Cause.squash(failure.cause)); |
| 818 | yield* Effect.logWarning("failed to parse settings.json, using defaults", { |
| 819 | path: settingsPath, |
| 820 | issues: Cause.pretty(failure.cause), |
| 821 | cause: failure.cause, |
| 822 | }); |
| 823 | } |
| 824 | } else { |
| 825 | settings = decoded.value; |
| 826 | } |
| 827 | } |
| 828 | // Settings saved by a server that kept the mode in `compactIdleThreads` move |
| 829 | // it to `idleCompaction`, so that switch goes to clients as a boolean. The |
| 830 | // defaults gain the field too, which tells clients this server reads modes. |
| 831 | settings = withIdleCompaction(settings, globalIdleCompaction(settings)); |
| 832 | |
| 833 | // A file that failed to decode must stay on disk for the user to repair. |
| 834 | const settingsFileTrusted = Option.isNone(parseFailure); |
| 835 | const providerHistory = yield* sql<{ |
| 836 | readonly providerName: string; |
| 837 | readonly providerInstanceId: string | null; |
| 838 | }>` |
| 839 | SELECT DISTINCT |
| 840 | provider_name AS "providerName", |
| 841 | provider_instance_id AS "providerInstanceId" |
| 842 | FROM projection_thread_sessions |
| 843 | WHERE provider_name IN ('cursor', 'grok', 'opencode') |
| 844 | UNION |
| 845 | SELECT DISTINCT |
| 846 | provider_name AS "providerName", |
| 847 | provider_instance_id AS "providerInstanceId" |
| 848 | FROM provider_session_runtime |
| 849 | WHERE provider_name IN ('cursor', 'grok', 'opencode') |
| 850 | `.pipe( |
| 851 | NodeSqliteClient.readOnly, |
| 852 | Effect.mapError( |
| 853 | (cause) => |
| 854 | new ServerSettingsError({ |
| 855 | settingsPath, |
| 856 | operation: "read-provider-history", |
| 857 | cause, |
| 858 | }), |
| 859 | ), |
| 860 | ); |
| 861 | |
| 862 | const legacyProjectRows = |
| 863 | settings.projectSettingsFolded || !settingsFileTrusted |
| 864 | ? [] |
| 865 | : yield* sql<LegacyProjectSettingsRow>` |
| 866 | SELECT |
| 867 | project_id AS "projectId", |
| 868 | default_model_selection_json AS "defaultModelSelection", |
| 869 | default_thread_env_mode AS "defaultThreadEnvMode", |
| 870 | auto_pull AS "autoPull", |
| 871 | scripts_json AS "scripts" |
| 872 | FROM projection_projects |
| 873 | WHERE deleted_at IS NULL |
| 874 | `.pipe( |
| 875 | NodeSqliteClient.readOnly, |
| 876 | Effect.mapError( |
| 877 | (cause) => |
| 878 | new ServerSettingsError({ |
| 879 | settingsPath, |
| 880 | operation: "read-project-settings", |
| 881 | cause, |
| 882 | }), |
| 883 | ), |
| 884 | ); |
| 885 | |
| 886 | const loaded = foldProviderInstanceEnabledFlags( |
| 887 | legacyProviders === undefined |
| 888 | ? settings |
| 889 | : migrateLegacyProviderSettings(settings, legacyProviders, providerHistory), |
| 890 | ); |
| 891 | const folded = settingsFileTrusted |
| 892 | ? foldLegacyProjectSettings(loaded, legacyProjectRows) |
| 893 | : loaded; |
| 894 | // Remove retired keys only after copying secrets and writing the new file; failures retry on next load. |
| 895 | const legacySecretsCopied = yield* Effect.forEach( |
| 896 | settingsFileTrusted |
| 897 | ? migrateLegacySourceControlSettings(folded, legacySourceControl).secretMoves |
| 898 | : [], |
| 899 | (move) => |
| 900 | secretStore.get(move.from).pipe( |
| 901 | Effect.flatMap((secret) => |
| 902 | Option.isNone(secret) ? Effect.void : secretStore.set(move.to, secret.value), |
| 903 | ), |
| 904 | Effect.as(true), |
| 905 | Effect.catch(() => |
| 906 | Effect.logWarning("failed to copy a source control secret to its new name", { |
| 907 | secretName: move.to, |
| 908 | }).pipe(Effect.as(false)), |
| 909 | ), |
| 910 | ), |
| 911 | ).pipe(Effect.map((copied) => copied.every(Boolean))); |
| 912 | const hasLegacySourceControl = |
| 913 | settingsFileTrusted && |
| 914 | legacySecretsCopied && |
| 915 | (legacySourceControl.bitbucket !== undefined || legacySourceControl.github !== undefined); |
| 916 | const sourceControlMigration = hasLegacySourceControl |
| 917 | ? migrateLegacySourceControlSettings(folded, legacySourceControl) |
| 918 | : { settings: folded, secretMoves: [] }; |
| 919 | const migrated = settingsFileTrusted |
| 920 | ? yield* moveInlineSettingsSecrets(sourceControlMigration.settings) |
| 921 | : folded; |
| 922 | if ( |
| 923 | migrated !== loaded || |
| 924 | (settingsFileTrusted && legacyProviders !== undefined) || |
| 925 | hasLegacySourceControl |
| 926 | ) { |
| 927 | yield* writeSettingsAtomically(migrated); |
| 928 | for (const move of sourceControlMigration.secretMoves) { |
| 929 | yield* secretStore.remove(move.from).pipe(Effect.ignore); |
| 930 | } |
| 931 | } |
| 932 | return { settings: migrated, parseFailure }; |
| 933 | }); |
| 934 | |
| 935 | const settingsCache = yield* Cache.makeWith((_key: typeof cacheKey) => loadSettingsFromDisk, { |
| 936 | capacity: 1, |
| 937 | timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero), |
| 938 | }); |
| 939 | |
| 940 | const getSettingsFromCache = Cache.get(settingsCache, cacheKey).pipe( |
| 941 | Effect.map((loaded) => loaded.settings), |
| 942 | ); |
| 943 | const reloadSettings = Cache.invalidate(settingsCache, cacheKey).pipe( |
| 944 | Effect.andThen(Cache.get(settingsCache, cacheKey)), |
| 945 | ); |
| 946 | |
| 947 | const materializeSettingsSecrets = Effect.fn("serverSettings.materializeSettingsSecrets")( |
| 948 | function* (settings: ServerSettings): Effect.fn.Return<ServerSettings, ServerSettingsError> { |
| 949 | const storedSecrets = new Map<string, string>(); |
| 950 | const pending = new Map<string, SettingsSecret>(); |
| 951 | mapSettingsSecrets(settings, (secret) => { |
| 952 | if (secret.sensitive && secret.redacted) pending.set(secret.secretName, secret); |
| 953 | return secret; |
| 954 | }); |
| 955 | for (const secret of pending.values()) { |
| 956 | const value = yield* secretStore.get(secret.secretName).pipe( |
| 957 | Effect.mapError( |
| 958 | (cause) => |
| 959 | new ServerSettingsError({ |
| 960 | settingsPath, |
| 961 | operation: "read-secret", |
| 962 | providerInstanceId: secret.providerInstanceId, |
| 963 | environmentVariable: secret.environmentVariable, |
| 964 | cause, |
| 965 | }), |
| 966 | ), |
| 967 | ); |
| 968 | storedSecrets.set( |
| 969 | secret.secretName, |
| 970 | Option.isSome(value) ? textDecoder.decode(value.value) : "", |
| 971 | ); |
| 972 | } |
| 973 | return mapSettingsSecrets(settings, (secret) => ({ |
| 974 | value: |
| 975 | secret.sensitive && secret.redacted |
| 976 | ? (storedSecrets.get(secret.secretName) ?? "") |
| 977 | : secret.value, |
| 978 | redacted: secret.sensitive && secret.kind === "environment" && secret.redacted, |
| 979 | })); |
| 980 | }, |
| 981 | ); |
| 982 | |
| 983 | // Background sweeps read settings every few seconds; reading secret files each time is wasted. |
| 984 | // Every secret write rewrites the settings file, so a new cached object implies fresh secrets. |
| 985 | // A read that overlaps a secret write may see files a rollback then restores, so it is not kept. |
| 986 | let materialized: { readonly source: ServerSettings; readonly value: ServerSettings } | null = |
| 987 | null; |
| 988 | let secretWriteGeneration = 0; |
| 989 | const invalidateMaterialized = Effect.sync(() => { |
| 990 | secretWriteGeneration++; |
| 991 | materialized = null; |
| 992 | }); |
| 993 | const getMaterializedSettings = getSettingsFromCache.pipe( |
| 994 | Effect.flatMap((source) => { |
| 995 | if (materialized?.source === source) return Effect.succeed(materialized.value); |
| 996 | const generation = secretWriteGeneration; |
| 997 | return materializeSettingsSecrets(source).pipe( |
| 998 | Effect.map(toEffective), |
| 999 | Effect.tap((value) => |
| 1000 | Effect.sync(() => { |
| 1001 | if (generation === secretWriteGeneration) materialized = { source, value }; |
| 1002 | }), |
| 1003 | ), |
| 1004 | ); |
| 1005 | }), |
| 1006 | ); |
| 1007 | |
| 1008 | const materializeChanges = (changes: Stream.Stream<ServerSettings>) => |
| 1009 | changes.pipe( |
| 1010 | Stream.mapEffect((settings) => |
| 1011 | materializeSettingsSecrets(settings).pipe( |
| 1012 | Effect.catch((error: ServerSettingsError) => |
| 1013 | Effect.logWarning("failed to materialize settings secrets", { |
| 1014 | operation: error.operation, |
| 1015 | providerInstanceId: error.providerInstanceId, |
| 1016 | environmentVariable: error.environmentVariable, |
| 1017 | cause: error.cause, |
| 1018 | }).pipe(Effect.as(settings)), |
| 1019 | ), |
| 1020 | ), |
| 1021 | ), |
| 1022 | Stream.map(toEffective), |
| 1023 | ); |
| 1024 | |
| 1025 | type SecretChange = { |
| 1026 | readonly secretName: string; |
| 1027 | readonly providerInstanceId?: string; |
| 1028 | readonly environmentVariable?: string; |
| 1029 | } & ( |
| 1030 | | { readonly kind: "write"; readonly value: Uint8Array } |
| 1031 | | { readonly kind: "remove"; readonly operation: "remove-secret" | "remove-stale-secret" } |
| 1032 | ); |
| 1033 | |
| 1034 | const persistSettingsSecrets = (current: ServerSettings, next: ServerSettings) => |
| 1035 | Effect.sync(() => { |
| 1036 | // Only sensitive rows own a stored secret; a plain duplicate must not hide one from cleanup. |
| 1037 | const previous = new Map<string, SettingsSecret>(); |
| 1038 | const inlineValues = new Map<string, string>(); |
| 1039 | mapSettingsSecrets(current, (secret) => { |
| 1040 | if (!secret.sensitive) return secret; |
| 1041 | previous.set(secret.secretName, secret); |
| 1042 | if (!secret.redacted && secret.value.length > 0) { |
| 1043 | inlineValues.set(secret.secretName, secret.value); |
| 1044 | } |
| 1045 | return secret; |
| 1046 | }); |
| 1047 | const changes: SecretChange[] = []; |
| 1048 | const nextSecretNames = new Set<string>(); |
| 1049 | const keptSecretNames = new Set<string>(); |
| 1050 | const writtenSecretNames = new Set<string>(); |
| 1051 | const inlineWrites = new Map<string, SecretChange>(); |
| 1052 | const settings = mapSettingsSecrets(withEchoedSecretReferences(current, next), (secret) => { |
| 1053 | const metadata = { |
| 1054 | secretName: secret.secretName, |
| 1055 | ...(secret.providerInstanceId !== undefined |
| 1056 | ? { providerInstanceId: secret.providerInstanceId } |
| 1057 | : {}), |
| 1058 | ...(secret.environmentVariable !== undefined |
| 1059 | ? { environmentVariable: secret.environmentVariable } |
| 1060 | : {}), |
| 1061 | }; |
| 1062 | if (!secret.sensitive) { |
| 1063 | changes.push({ ...metadata, kind: "remove", operation: "remove-secret" }); |
| 1064 | return { value: secret.value, redacted: false }; |
| 1065 | } |
| 1066 | if (secret.redacted) { |
| 1067 | keptSecretNames.add(secret.secretName); |
| 1068 | nextSecretNames.add(secret.secretName); |
| 1069 | const inlineValue = inlineValues.get(secret.secretName); |
| 1070 | if (inlineValue !== undefined) { |
| 1071 | inlineWrites.set(secret.secretName, { |
| 1072 | ...metadata, |
| 1073 | kind: "write", |
| 1074 | value: textEncoder.encode(inlineValue), |
| 1075 | }); |
| 1076 | } |
| 1077 | return { value: "", redacted: true }; |
| 1078 | } |
| 1079 | if (secret.value.length === 0) { |
| 1080 | changes.push({ ...metadata, kind: "remove", operation: "remove-secret" }); |
| 1081 | return { value: "", redacted: false }; |
| 1082 | } |
| 1083 | nextSecretNames.add(secret.secretName); |
| 1084 | writtenSecretNames.add(secret.secretName); |
| 1085 | changes.push({ ...metadata, kind: "write", value: textEncoder.encode(secret.value) }); |
| 1086 | return { value: "", redacted: true }; |
| 1087 | }); |
| 1088 | // A legacy inline value only moves to the store when no row in this save replaces it. |
| 1089 | for (const [secretName, change] of inlineWrites) { |
| 1090 | if (!writtenSecretNames.has(secretName)) changes.push(change); |
| 1091 | } |
| 1092 | for (const secret of previous.values()) { |
| 1093 | if (nextSecretNames.has(secret.secretName)) continue; |
| 1094 | if (changes.some((change) => change.secretName === secret.secretName)) continue; |
| 1095 | changes.push({ |
| 1096 | kind: "remove", |
| 1097 | secretName: secret.secretName, |
| 1098 | operation: "remove-stale-secret", |
| 1099 | ...(secret.providerInstanceId !== undefined |
| 1100 | ? { providerInstanceId: secret.providerInstanceId } |
| 1101 | : {}), |
| 1102 | ...(secret.environmentVariable !== undefined |
| 1103 | ? { environmentVariable: secret.environmentVariable } |
| 1104 | : {}), |
| 1105 | }); |
| 1106 | } |
| 1107 | return { |
| 1108 | settings, |
| 1109 | // A redacted row still claims its stored secret, so a duplicate row must not remove it. |
| 1110 | changes: changes.filter( |
| 1111 | (change) => change.kind === "write" || !keptSecretNames.has(change.secretName), |
| 1112 | ), |
| 1113 | }; |
| 1114 | }); |
| 1115 | |
| 1116 | const rollbackSettingsSecretWrites = ( |
| 1117 | writes: ReadonlyArray<{ |
| 1118 | readonly secretName: string; |
| 1119 | readonly previousValue: Option.Option<Uint8Array>; |
| 1120 | readonly providerInstanceId?: string; |
| 1121 | readonly environmentVariable?: string; |
| 1122 | }>, |
| 1123 | ) => |
| 1124 | Effect.forEach( |
| 1125 | writes.toReversed(), |
| 1126 | (write) => |
| 1127 | (Option.isSome(write.previousValue) |
| 1128 | ? secretStore.set(write.secretName, write.previousValue.value) |
| 1129 | : secretStore.remove(write.secretName) |
| 1130 | ).pipe( |
| 1131 | Effect.catch((cause) => |
| 1132 | Effect.logWarning("failed to roll back settings secret", { |
| 1133 | providerInstanceId: write.providerInstanceId, |
| 1134 | environmentVariable: write.environmentVariable, |
| 1135 | cause, |
| 1136 | }), |
| 1137 | ), |
| 1138 | ), |
| 1139 | { discard: true }, |
| 1140 | ); |
| 1141 | |
| 1142 | const applySettingsSecretChanges = (changes: ReadonlyArray<SecretChange>) => { |
| 1143 | const applied: Array<{ |
| 1144 | readonly secretName: string; |
| 1145 | readonly previousValue: Option.Option<Uint8Array>; |
| 1146 | readonly providerInstanceId?: string; |
| 1147 | readonly environmentVariable?: string; |
| 1148 | }> = []; |
| 1149 | const rollback = Effect.suspend(() => rollbackSettingsSecretWrites(applied)); |
| 1150 | return Effect.forEach( |
| 1151 | changes, |
| 1152 | (change) => |
| 1153 | Effect.gen(function* () { |
| 1154 | const previousValue = yield* secretStore.get(change.secretName).pipe( |
| 1155 | Effect.mapError( |
| 1156 | (cause) => |
| 1157 | new ServerSettingsError({ |
| 1158 | settingsPath, |
| 1159 | operation: "read-secret", |
| 1160 | providerInstanceId: change.providerInstanceId, |
| 1161 | environmentVariable: change.environmentVariable, |
| 1162 | cause, |
| 1163 | }), |
| 1164 | ), |
| 1165 | ); |
| 1166 | // A store operation may mutate before reporting an error (for example chmod after rename). |
| 1167 | applied.push({ ...change, previousValue }); |
| 1168 | yield* ( |
| 1169 | change.kind === "write" |
| 1170 | ? secretStore.set(change.secretName, change.value) |
| 1171 | : secretStore.remove(change.secretName) |
| 1172 | ).pipe( |
| 1173 | Effect.mapError( |
| 1174 | (cause) => |
| 1175 | new ServerSettingsError({ |
| 1176 | settingsPath, |
| 1177 | operation: change.kind === "write" ? "write-secret" : change.operation, |
| 1178 | providerInstanceId: change.providerInstanceId, |
| 1179 | environmentVariable: change.environmentVariable, |
| 1180 | cause, |
| 1181 | }), |
| 1182 | ), |
| 1183 | ); |
| 1184 | }), |
| 1185 | { discard: true }, |
| 1186 | ).pipe( |
| 1187 | Effect.tapError(() => rollback), |
| 1188 | Effect.as(rollback), |
| 1189 | ); |
| 1190 | }; |
| 1191 | |
| 1192 | const updateAndPersistSettings = ( |
| 1193 | patch: ServerSettingsPatch, |
| 1194 | update: (current: ServerSettings) => Effect.Effect<ServerSettings, ServerSettingsError>, |
| 1195 | ): Effect.Effect<ServerSettings, ServerSettingsError> => |
| 1196 | writeSemaphore.withPermits(1)( |
| 1197 | Effect.gen(function* () { |
| 1198 | const { settings: current, parseFailure } = yield* reloadSettings; |
| 1199 | if (Option.isSome(parseFailure)) { |
| 1200 | return yield* new ServerSettingsError({ |
| 1201 | settingsPath, |
| 1202 | operation: "read-file", |
| 1203 | cause: parseFailure.value, |
| 1204 | }); |
| 1205 | } |
| 1206 | // An older client echoes a reference as the redacted marker; restore it before vetting so |
| 1207 | // a removed provider's reference is cleared below rather than restored by persistence. |
| 1208 | const updated = withoutRedirectedSimlockTokens( |
| 1209 | current, |
| 1210 | withEchoedSecretReferences(current, yield* update(current)), |
| 1211 | ); |
| 1212 | // Vet before secrets: a variable the policy hides from clients is not one they removed. |
| 1213 | const vetted = managed.vetWrite(current, yield* normalizeServerSettings(updated), patch); |
| 1214 | if (vetted._tag === "Locked") { |
| 1215 | return yield* new ServerSettingsError({ |
| 1216 | settingsPath, |
| 1217 | operation: "write-managed-setting", |
| 1218 | managedSetting: vetted.path, |
| 1219 | managedBy: managedPolicy?.organization, |
| 1220 | }); |
| 1221 | } |
| 1222 | const vettedProviders = managedProviders.vetWrite(current, vetted.settings); |
| 1223 | if (vettedProviders._tag === "Locked") { |
| 1224 | return yield* new ServerSettingsError({ |
| 1225 | settingsPath, |
| 1226 | operation: "write-managed-setting", |
| 1227 | managedSetting: vettedProviders.path, |
| 1228 | managedBy: managedPolicy?.organization, |
| 1229 | }); |
| 1230 | } |
| 1231 | if (vettedProviders._tag === "Disallowed") { |
| 1232 | return yield* new ServerSettingsError({ |
| 1233 | settingsPath, |
| 1234 | operation: "use-disallowed-provider", |
| 1235 | providerInstanceId: vettedProviders.instanceId, |
| 1236 | providerLabel: vettedProviders.label, |
| 1237 | managedBy: managedPolicy?.organization, |
| 1238 | }); |
| 1239 | } |
| 1240 | // After vetting, which can keep stored variables a policy hides from clients. |
| 1241 | const persisted = yield* persistSettingsSecrets( |
| 1242 | current, |
| 1243 | withoutRemovedSecretProviderReferences(current, vettedProviders.settings), |
| 1244 | ); |
| 1245 | const next = yield* normalizeServerSettings(persisted.settings); |
| 1246 | const materializedNext = yield* Effect.uninterruptibleMask(() => |
| 1247 | Effect.gen(function* () { |
| 1248 | yield* invalidateMaterialized; |
| 1249 | const rollbackSecretChanges = yield* applySettingsSecretChanges(persisted.changes); |
| 1250 | const materializedExit = yield* Effect.exit(materializeSettingsSecrets(next)); |
| 1251 | if (Exit.isFailure(materializedExit)) { |
| 1252 | yield* rollbackSecretChanges; |
| 1253 | return yield* Effect.failCause(materializedExit.cause); |
| 1254 | } |
| 1255 | const writeExit = yield* Effect.exit(writeSettingsAtomically(next)); |
| 1256 | if (Exit.isFailure(writeExit)) { |
| 1257 | yield* rollbackSecretChanges; |
| 1258 | return yield* Effect.failCause(writeExit.cause); |
| 1259 | } |
| 1260 | return materializedExit.value; |
| 1261 | }).pipe(Effect.ensuring(invalidateMaterialized)), |
| 1262 | ); |
| 1263 | yield* Cache.set(settingsCache, cacheKey, { settings: next, parseFailure: Option.none() }); |
| 1264 | const resolved = toEffective(materializedNext); |
| 1265 | materialized = { source: next, value: resolved }; |
| 1266 | yield* emitChange(next); |
| 1267 | return resolved; |
| 1268 | }), |
| 1269 | ); |
| 1270 | |
| 1271 | const withSettingsSnapshot: ServerSettingsService["Service"]["withSettingsSnapshot"] = (use) => |
| 1272 | writeSemaphore.withPermits(1)(getMaterializedSettings.pipe(Effect.flatMap(use))); |
| 1273 | |
| 1274 | const revalidateAndEmit = writeSemaphore.withPermits(1)( |
| 1275 | Effect.gen(function* () { |
| 1276 | const { settings } = yield* reloadSettings; |
| 1277 | yield* emitChange(settings); |
| 1278 | }), |
| 1279 | ); |
| 1280 | |
| 1281 | // `ready` completes once the watch is registered, or once it fails to be. |
| 1282 | const watchFileChanges = (filePath: string, ready: Deferred.Deferred<void>) => { |
| 1283 | const directory = pathService.dirname(filePath); |
| 1284 | const fileName = pathService.basename(filePath); |
| 1285 | const resolvedFilePath = pathService.resolve(filePath); |
| 1286 | const markReady = () => Deferred.doneUnsafe(ready, Exit.void); |
| 1287 | return fs.watch(directory, { onReady: markReady }).pipe( |
| 1288 | Stream.filter( |
| 1289 | (event) => |
| 1290 | event.path === fileName || |
| 1291 | event.path === filePath || |
| 1292 | pathService.resolve(directory, event.path) === resolvedFilePath, |
| 1293 | ), |
| 1294 | Stream.as(undefined), |
| 1295 | Stream.ensuring(Effect.sync(markReady)), |
| 1296 | ); |
| 1297 | }; |
| 1298 | |
| 1299 | // A directory watch follows the directory's inode, so when the target's |
| 1300 | // directory is renamed away and another takes its path, watch the new one. |
| 1301 | const watchLinkTargetChanges = (linkTargetPath: string, ready: Deferred.Deferred<void>) => |
| 1302 | watchDirectoryReplacements( |
| 1303 | (parentReady) => watchFileChanges(pathService.dirname(linkTargetPath), parentReady), |
| 1304 | Stream.unwrap( |
| 1305 | Effect.gen(function* () { |
| 1306 | const registered = yield* Deferred.make<void>(); |
| 1307 | return Stream.merge( |
| 1308 | watchFileChanges(linkTargetPath, registered).pipe(Stream.ignoreCause({ log: true })), |
| 1309 | // Startup reloads once ready. Later watches trigger their own reload. |
| 1310 | Stream.fromEffect( |
| 1311 | Deferred.await(registered).pipe(Effect.andThen(Deferred.succeed(ready, undefined))), |
| 1312 | ).pipe( |
| 1313 | Stream.filter((completedStartup) => !completedStartup), |
| 1314 | Stream.as(undefined), |
| 1315 | ), |
| 1316 | ); |
| 1317 | }), |
| 1318 | ), |
| 1319 | ); |
| 1320 | |
| 1321 | const startWatcher = Effect.gen(function* () { |
| 1322 | const settingsDir = pathService.dirname(settingsPath); |
| 1323 | |
| 1324 | yield* fs.makeDirectory(settingsDir, { recursive: true }).pipe( |
| 1325 | Effect.mapError( |
| 1326 | (cause) => |
| 1327 | new ServerSettingsError({ |
| 1328 | settingsPath, |
| 1329 | operation: "prepare-directory", |
| 1330 | cause, |
| 1331 | }), |
| 1332 | ), |
| 1333 | ); |
| 1334 | |
| 1335 | const revalidateAndEmitSafely = revalidateAndEmit.pipe(Effect.ignoreCause({ log: true })); |
| 1336 | |
| 1337 | // Watch symlink targets' directories for rewrites; re-resolve link changes to follow repointing. |
| 1338 | const watchLinkTarget = Effect.gen(function* () { |
| 1339 | const linkTargetPath = yield* resolveSymlinkTarget(settingsPath).pipe( |
| 1340 | Effect.provideService(FileSystem.FileSystem, fs), |
| 1341 | Effect.provideService(Path.Path, pathService), |
| 1342 | ); |
| 1343 | if (linkTargetPath === pathService.resolve(settingsPath)) { |
| 1344 | return Option.none<string>(); |
| 1345 | } |
| 1346 | yield* fs |
| 1347 | .makeDirectory(pathService.dirname(linkTargetPath), { recursive: true }) |
| 1348 | .pipe(Effect.ignore({ log: true })); |
| 1349 | return Option.some(linkTargetPath); |
| 1350 | }).pipe(Effect.orElseSucceed(() => Option.none<string>())); |
| 1351 | |
| 1352 | const settingsReady = yield* Deferred.make<void>(); |
| 1353 | const linkTargetReady = yield* Deferred.make<void>(); |
| 1354 | yield* Effect.gen(function* () { |
| 1355 | const [settingsEvents, resolveEvents] = yield* watchFileChanges( |
| 1356 | settingsPath, |
| 1357 | settingsReady, |
| 1358 | ).pipe( |
| 1359 | Stream.ignoreCause({ log: true }), |
| 1360 | Stream.broadcastN({ n: 2, capacity: 1, strategy: "sliding" }), |
| 1361 | ); |
| 1362 | // Buffer settings path changes while resolving the first target. |
| 1363 | const linkTargetEvents = Stream.fromEffect( |
| 1364 | Deferred.await(settingsReady).pipe(Effect.andThen(watchLinkTarget)), |
| 1365 | ).pipe( |
| 1366 | Stream.concat(resolveEvents.pipe(Stream.mapEffect(() => watchLinkTarget))), |
| 1367 | Stream.changes, |
| 1368 | Stream.switchMap( |
| 1369 | Option.match({ |
| 1370 | onNone: () => |
| 1371 | Stream.fromEffect(Deferred.succeed(linkTargetReady, undefined)).pipe(Stream.drain), |
| 1372 | onSome: (linkTargetPath) => watchLinkTargetChanges(linkTargetPath, linkTargetReady), |
| 1373 | }), |
| 1374 | ), |
| 1375 | ); |
| 1376 | |
| 1377 | // Saves emit multiple events; fs.watch may fire before content is flushed. |
| 1378 | yield* Stream.merge(settingsEvents, linkTargetEvents).pipe( |
| 1379 | Stream.debounce(Duration.millis(100)), |
| 1380 | Stream.runForEach(() => revalidateAndEmitSafely), |
| 1381 | ); |
| 1382 | }).pipe( |
| 1383 | Effect.scoped, |
| 1384 | Effect.ignoreCause({ log: true }), |
| 1385 | Effect.ensuring(Deferred.succeed(linkTargetReady, undefined)), |
| 1386 | Effect.forkIn(watcherScope), |
| 1387 | ); |
| 1388 | // Edits made once `start` returns must reach a registered watch. |
| 1389 | yield* Deferred.await(linkTargetReady); |
| 1390 | }); |
| 1391 | |
| 1392 | const start = Effect.gen(function* () { |
| 1393 | const shouldStart = yield* Ref.modify(startedRef, (started) => [!started, true]); |
| 1394 | if (!shouldStart) { |
| 1395 | return yield* Deferred.await(startedDeferred); |
| 1396 | } |
| 1397 | |
| 1398 | const startup = Effect.gen(function* () { |
| 1399 | yield* startWatcher; |
| 1400 | yield* reloadSettings; |
| 1401 | }); |
| 1402 | |
| 1403 | const startupExit = yield* Effect.exit(startup); |
| 1404 | if (startupExit._tag === "Failure") { |
| 1405 | yield* Deferred.failCause(startedDeferred, startupExit.cause).pipe(Effect.orDie); |
| 1406 | return yield* Effect.failCause(startupExit.cause); |
| 1407 | } |
| 1408 | |
| 1409 | yield* Deferred.succeed(startedDeferred, undefined).pipe(Effect.orDie); |
| 1410 | }); |
| 1411 | |
| 1412 | return { |
| 1413 | managedPolicy, |
| 1414 | start, |
| 1415 | ready: Deferred.await(startedDeferred), |
| 1416 | getSettings: getMaterializedSettings, |
| 1417 | updateSettings: (patch) => |
| 1418 | updateAndPersistSettings(patch, (current) => |
| 1419 | ensureSecretProviderWritesAllowed(patch, settingsPath).pipe( |
| 1420 | Effect.as(applyServerSettingsPatch(current, patch)), |
| 1421 | ), |
| 1422 | ), |
| 1423 | updateProviderInstance: (mutation, patch = {}) => |
| 1424 | updateAndPersistSettings(patch, (current) => |
| 1425 | Effect.gen(function* () { |
| 1426 | yield* ensureSecretProviderWritesAllowed(patch, settingsPath); |
| 1427 | yield* ensureProviderInstanceMutationAllowed(current, mutation, settingsPath); |
| 1428 | const patched = applyServerSettingsPatch(current, patch); |
| 1429 | return applyProviderInstanceMutation(patched, mutation); |
| 1430 | }), |
| 1431 | ), |
| 1432 | withSettingsSnapshot, |
| 1433 | get streamChanges() { |
| 1434 | return materializeChanges(Stream.fromPubSub(changesPubSub)); |
| 1435 | }, |
| 1436 | get subscribeChanges() { |
| 1437 | return PubSub.subscribe(changesPubSub).pipe( |
| 1438 | Effect.map((subscription) => materializeChanges(Stream.fromSubscription(subscription))), |
| 1439 | ); |
| 1440 | }, |
| 1441 | } satisfies ServerSettingsService["Service"]; |
| 1442 | }); |
| 1443 | |
| 1444 | export const layer = Layer.effect(ServerSettingsService, make); |