import { existsSync, mkdirSync, readFileSync, statSync } from "node:fs"; import { dirname } from "node:path"; import type { ModelChoice } from "./adapters/index.js"; import { deleteSecretJsonSync, getSecretJsonSync, storeSecretJsonSync, } from "./connectors/tokenStorage.js"; import { patchworkPath, warnIfLegacyConfigStranded } from "./patchworkHome.js"; import { writeFileAtomicSync } from "./writeFileAtomic.js"; /** * Single source of truth for AI driver modes. Both the runtime `Driver` * type and `config.schema.json` derive from this list — the test in * src/__tests__/configSchemaAlignment.test.ts asserts they stay aligned. */ export const DRIVERS = [ "subprocess", "api", "openai", "grok", "gemini", "gemini-api", "local", "none", ] as const; export type Driver = (typeof DRIVERS)[number]; export interface PatchworkConfig { model: ModelChoice; defaultModel?: string; apiKeys?: { anthropic?: string; openai?: string; google?: string; xai?: string; }; localEndpoint?: string; localModel?: string; localEmbeddingsEndpoint?: string; localEmbeddingsModel?: string; dashboard?: { port: number; requireApproval: Array<"low" | "medium" | "high">; pushNotifications: boolean; webhookUrl?: string; }; recipesDir?: string; /** * Governance profile (src/governance/profile.ts). `governed` resolves every * opt-in control to a conservative default; absent or `compat` is * byte-identical to pre-profile behaviour. `patchwork init` writes * `governed` for a NEW install; an existing install is never changed by an * upgrade — opt in with `patchwork profile governed`. */ profile?: "governed" | "compat"; /** * Operator-controlled allowlist for recipe `servers:` plugins. Under the * governed profile a plugin spec not listed here is refused at install, * dashboard save, lint AND runtime load. `spec` is the exact string a * recipe may name (path or package); `integrity` is an optional * `sha256-` over the entrypoint file, checked at load when present. */ plugins?: { allow?: Array<{ spec: string; version?: string; integrity?: string }>; }; /** Approval gate level — mirrors CLI --approval-gate. Persisted so dashboard changes survive restart. */ approvalGate?: "off" | "high" | "all"; /** * Per-risk-tier approval timeout override, in ms — mirrors CLI * `--approval-timeout-`. A tier's value of `0` means * "no expiry" (held until a human decides). Unset tiers fall back to * `ApprovalQueue.DEFAULT_TTL_MS`. Persisted so dashboard changes survive * restart. */ approvalTimeouts?: Partial>; /** * Opt-in toggle for personalSignals heuristic 10 (time-of-day anomaly). * Mirrors CLI --enable-time-of-day-anomaly. Persisted so dashboard * changes survive restart. Default false — h10 is off until the user * explicitly enables it. */ enableTimeOfDayAnomaly?: boolean; /** Absolute path to a managed settings file (admin-controlled, highest rule precedence). */ managedSettingsPath?: string; recipes?: { disabled?: string[]; /** IANA timezone name for cron schedules, e.g. "America/New_York". Defaults to "UTC". */ timezone?: string; }; /** AI driver mode — persisted so dashboard changes survive restart. */ driver?: Driver; /** Notification channel config */ notifications?: { slackChannel?: string; }; /** * Path to the `claude` CLI binary. Used by recipe `agent:` steps * (`defaultClaudeCodeFn`) when the runtime can't rely on PATH-based * lookup — e.g. a launchd-managed bridge whose env diverges from the * developer's interactive shell, or a `npm link`-ed install where * the global `claude` symlink resolves into a sandboxed dir. * * Override priority (highest first): * 1. `PATCHWORK_CLAUDE_BINARY` env var * 2. this `claudeBinary` config field * 3. plain `"claude"` (PATH lookup) * * Default: omitted → spawn falls back to PATH lookup, matching * pre-existing behavior. */ claudeBinary?: string; /** * Mobile-oversight push relay (FCM/APNS gateway URL the bridge POSTs to) * and its bearer token. Persisted so dashboard changes survive restart. */ pushServiceUrl?: string; pushServiceToken?: string; /** * Origin embedded in the service-worker's approveUrl/rejectUrl. The SW * POSTs the one-shot approvalToken here, so a wrong/insecure value * exfiltrates approvals — HTTPS is enforced at the /settings boundary. */ pushServiceBaseUrl?: string; /** * Allow `pushServiceUrl` to target a private/CGNAT host (e.g. a Tailscale * `*.ts.net` address, 100.64.0.0/10). Default false — the SSRF guard * treats a private-range push relay the same as any other SSRF target. * Set with `--push-service-allow-private`. */ pushServiceAllowPrivate?: boolean; /** * Public-ntfy.sh push channel. Topic acts as a bearer; server defaults * to https://ntfy.sh and can be overridden for self-hosted instances. */ ntfyTopic?: string; ntfyServer?: string; } const DEFAULTS: PatchworkConfig = { model: "claude", dashboard: { port: 3200, requireApproval: ["high"], pushNotifications: false, }, // `recipesDir` is deliberately absent: a value baked in here would freeze // whatever PATCHWORK_HOME held at module-load time, which for a long-lived // bridge is whatever the first import happened to see. Resolved per load // by `withHomeDefaults` below. // Default driver so `patchwork-os recipe run X` and dashboard task launches // work immediately after `patchwork init`. Without this the bridge defaults // to "none" and recipe execution returns "Recipe execution unavailable — // requires --driver subprocess", silently breaking the init "Next:" flow. driver: "subprocess", }; export function defaultConfigPath(): string { return patchworkPath("config.json"); } type ApiKeyProvider = keyof NonNullable; const API_KEY_PROVIDERS: ApiKeyProvider[] = [ "anthropic", "openai", "google", "xai", ]; function secretKeyFor(provider: ApiKeyProvider): string { return `apiKey.${provider}`; } interface StoredApiKey { key: string; } /** * Persist a single API key to the secure store. Empty string deletes. * Used by /settings POST so new keys never touch ~/.patchwork/config.json. */ export function saveApiKeyToSecureStore( provider: ApiKeyProvider, key: string, ): void { if (!key) { deleteSecretJsonSync(secretKeyFor(provider)); } else { storeSecretJsonSync(secretKeyFor(provider), { key } satisfies StoredApiKey); } // Secure-store change invalidates any cached loadConfig result that merged // the old key values. clearConfigCache(); } function loadApiKeysFromSecureStore(): NonNullable { const out: NonNullable = {}; for (const provider of API_KEY_PROVIDERS) { const stored = getSecretJsonSync(secretKeyFor(provider)); if (stored?.key) out[provider] = stored.key; } return out; } /** * Boolean presence check per provider — returns whether a key is in the * secure store, never the key itself. Used by /status so the dashboard can * render "key set" badges and gate the picker without ever seeing a raw key. */ export function getApiKeysPresent(): Record { const out: Record = { anthropic: false, openai: false, google: false, xai: false, }; for (const provider of API_KEY_PROVIDERS) { const stored = getSecretJsonSync(secretKeyFor(provider)); out[provider] = Boolean(stored?.key); } return out; } // TTL-based cache for loadConfig results. loadConfig reads config.json + up to // 4 provider .enc files (DPAPI spawn on Windows = 500–1500 ms each). A 30 s TTL // cuts the per-webhook 9+ kernel-call burst to ~1 per 30 s at steady state. const _configCache = new Map< string, { result: PatchworkConfig; expires: number; mtimeMs: number; size: number } >(); const _CONFIG_TTL_MS = 30_000; /** * Identity of the file backing a cache entry. * * The TTL alone was not enough: a cached entry stayed authoritative for 30s * regardless of what happened to the file, so an operator's hand edit was * invisible to the next read — and every runtime write is a read-modify-write * built on that read, so the edit was then written straight over. Silently * (#1361). * * `-1` for a missing file is a real value, not a sentinel for "unknown": it * distinguishes "no config on disk" from any real stat, so creating or * deleting the file both count as a change. */ function fileStamp(path: string): { mtimeMs: number; size: number } { try { const st = statSync(path); return { mtimeMs: st.mtimeMs, size: st.size }; } catch { return { mtimeMs: -1, size: -1 }; } } /** Default zone for cron schedules when `recipes.timezone` is unset. */ export const DEFAULT_SCHEDULE_TIMEZONE = "UTC"; /** * The zone cron schedules actually run in. Single calculation shared by the * scheduler and the status payload, so what is reported is what is used. * An empty string is treated as unset. */ export function effectiveScheduleTimezone(cfg: PatchworkConfig): string { return cfg.recipes?.timezone || DEFAULT_SCHEDULE_TIMEZONE; } /** * Whether the schedule zone was chosen ("config") or is the built-in default * ("default"). An explicit "UTC" is a choice; an unset or empty value is not. * Lets a client offer to set a zone without overwriting one someone picked. */ export function scheduleTimezoneSource( cfg: PatchworkConfig, ): "config" | "default" { return cfg.recipes?.timezone ? "config" : "default"; } /** True when `tz` is an IANA time zone this runtime's Intl data recognises. */ export function isValidIanaTimezone(tz: string): boolean { try { new Intl.DateTimeFormat("en-US", { timeZone: tz }); return true; } catch { return false; } } /** Clear the loadConfig cache. Exposed for tests and after saveConfig. */ export function clearConfigCache(): void { _configCache.clear(); } /** * Fill in the defaults that depend on the workspace root. Applied AFTER the * on-disk config is merged, so an explicit `recipesDir` still wins, and * resolved per call so the value tracks PATCHWORK_HOME rather than whatever * it was at import time. */ function withHomeDefaults(cfg: PatchworkConfig): PatchworkConfig { return cfg.recipesDir ? cfg : { ...cfg, recipesDir: patchworkPath("recipes") }; } export function loadConfig(path = defaultConfigPath()): PatchworkConfig { // Say so when an override is active but the real config was left behind. warnIfLegacyConfigStranded("config.json"); const now = Date.now(); const stamp = fileStamp(path); const cached = _configCache.get(path); // Serve from cache only when the file is BOTH un-expired and unchanged. // Size as well as mtime, because a filesystem whose timestamps are coarse // can give an edit the same mtime as the write before it. if ( cached && now < cached.expires && cached.mtimeMs === stamp.mtimeMs && cached.size === stamp.size ) { return cached.result; } if (!existsSync(path)) { const fromStore = loadApiKeysFromSecureStore(); const result: PatchworkConfig = withHomeDefaults( Object.keys(fromStore).length > 0 ? { ...DEFAULTS, apiKeys: fromStore } : { ...DEFAULTS }, ); _configCache.set(path, { result, expires: now + _CONFIG_TTL_MS, ...stamp, }); return result; } const raw = readFileSync(path, "utf8"); const parsed = JSON.parse(raw) as Partial; // One-time migration: lift plaintext apiKeys from disk into the secure // store, then rewrite the config file without them. Subsequent loads see // an empty `apiKeys` on disk and read from the secure store instead. let migrated = false; if (parsed.apiKeys) { for (const provider of API_KEY_PROVIDERS) { const v = parsed.apiKeys[provider]; if (typeof v === "string" && v) { try { storeSecretJsonSync(secretKeyFor(provider), { key: v, } satisfies StoredApiKey); migrated = true; } catch { // Secure store unavailable — leave plaintext in place rather than // delete a working credential. User can re-enter via dashboard. } } } if (migrated) { const stripped = { ...parsed }; delete (stripped as Partial).apiKeys; try { writeFileAtomicSync(path, JSON.stringify(stripped, null, 2), { mode: 0o600, }); } catch { // Read-only filesystem or permissions error — non-fatal; secure // store now holds a copy, plaintext stays on disk until next save. } } } // Merge: secure store is the source of truth; any non-migrated plaintext // (in case the migration write above failed) remains as a fallback. const fromStore = loadApiKeysFromSecureStore(); const apiKeys = Object.keys(fromStore).length > 0 || parsed.apiKeys ? { ...parsed.apiKeys, ...fromStore } : undefined; const result: PatchworkConfig = withHomeDefaults({ ...DEFAULTS, ...parsed, ...(apiKeys ? { apiKeys } : {}), }); // Don't cache migrated configs — the file has just been rewritten; next load // should see the stripped version. This path is once-per-key, not hot. if (!migrated) _configCache.set(path, { result, expires: now + _CONFIG_TTL_MS, ...stamp, }); return result; } export function saveConfig( config: PatchworkConfig, path = defaultConfigPath(), ): void { _configCache.delete(path); // invalidate before write so next load sees new content mkdirSync(dirname(path), { recursive: true }); // Strip apiKeys before persisting — they belong in the secure store, never // on disk. Defense in depth: even if a caller mutates cfg.apiKeys directly, // we won't round-trip it back to plaintext JSON. const stripped: PatchworkConfig = { ...config }; delete stripped.apiKeys; writeFileAtomicSync(path, JSON.stringify(stripped, null, 2), { mode: 0o600, }); } export function validateModelChoice(value: string): value is ModelChoice { return ["claude", "openai", "gemini", "grok", "local"].includes(value); }