#!/usr/bin/env node // write-schedule-config.mjs — Spor C / C2.3: onboarding writes the opt-in // scheduled-detection config to the USER-owned config path // (~/.claude/ms-ai-architect/ms-ai-architect.local.md), so the user never has to // hand-edit the gitignored file. Operator: "avgjøres i onboardingen, egen // innstilling, daglig default". // // STRUCTURAL ToS GUARANTEE: pure node — it only reads/writes a local config // file and NEVER contacts Anthropic/Claude (same boundary as the rest of the // detection layer; see detection-schedule.mjs for the Consumer Terms §3.7 // rationale). It is not a DETECTION_STEP — it is the onboarding write-path. // // GATED: it MERGES onto the existing config (parse → override only the two // onboarding-owned keys: enabled + os_scheduler_cadence → serialize), backs up // any prior file via lib/backup, and writes atomically via lib/atomic-write — // never a raw fs write. interval_days / include_skill_lifecycle set elsewhere // survive the rewrite. // // node scripts/kb-update/write-schedule-config.mjs --enabled true --cadence daily // node scripts/kb-update/write-schedule-config.mjs --enabled false --cadence interval // node scripts/kb-update/write-schedule-config.mjs --enabled true --cadence daily --courses true // // --courses true|false toggles include_course_detection (Spor C / C3 opt-in); // OPTIONAL — omit it to leave a value set elsewhere untouched. --home // overrides the home dir (tests). Prints the resolved config path. import { existsSync, readFileSync, mkdirSync } from 'node:fs'; import { join } from 'node:path'; import { homedir } from 'node:os'; import { resolveConfigPath, resolveUserDataDir } from './lib/user-data.mjs'; import { parseScheduleConfig, serializeScheduleConfig } from './lib/detection-schedule.mjs'; import { atomicWriteSync } from './lib/atomic-write.mjs'; import { backupFile } from './lib/backup.mjs'; function parseArgs(argv) { const out = {}; for (let i = 0; i < argv.length; i++) { if (argv[i] === '--enabled') out.enabled = argv[++i]; else if (argv[i] === '--cadence') out.cadence = argv[++i]; else if (argv[i] === '--courses') out.courses = argv[++i]; else if (argv[i] === '--home') out.home = argv[++i]; } return out; } /** * Write (merge onto) the user-owned scheduler config from onboarding answers. * @param {{enabled: boolean|string, cadence: string, courses?: boolean|string, home?: string}} args * @returns {{configPath: string, config: object}} */ export function writeScheduleConfig({ enabled, cadence, courses, home = homedir() }) { const configPath = resolveConfigPath(home); const existing = existsSync(configPath) ? readFileSync(configPath, 'utf8') : ''; const parsed = parseScheduleConfig(existing); // existing values (or OFF defaults) // Only the two onboarding-owned keys are overridden; everything else is // carried forward from `parsed`. coerce() in detection-schedule normalizes on // read, so we just hand it values it understands. const merged = { ...parsed, enabled: enabled === true || enabled === 'true', os_scheduler_cadence: cadence === 'interval' ? 'interval' : 'daily', }; // --courses is OPTIONAL (opt-in inside opt-in): only override // include_course_detection when the flag was explicitly supplied, so a value // set elsewhere survives an onboarding rewrite that omits it. if (courses !== undefined) { merged.include_course_detection = courses === true || courses === 'true'; } mkdirSync(resolveUserDataDir(home), { recursive: true }); backupFile(configPath, join(resolveUserDataDir(home), '.backups')); // no-op if absent atomicWriteSync(configPath, serializeScheduleConfig(merged)); return { configPath, config: merged }; } // CLI entry — only when run directly (not when imported by tests). if (import.meta.url === `file://${process.argv[1]}`) { const { enabled, cadence, courses, home } = parseArgs(process.argv.slice(2)); const { configPath, config } = writeScheduleConfig({ enabled, cadence, courses, home }); console.log(configPath); console.error( `scheduled_detection: enabled=${config.enabled} os_scheduler_cadence=${config.os_scheduler_cadence} ` + `(interval_days=${config.interval_days} include_course_detection=${config.include_course_detection})`, ); }