{"version":3,"file":"config-watcher.d.ts","sourceRoot":"","sources":["../../src/core/config-watcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAa3C,qBAAa,aAAc,SAAQ,YAAY;IAC9C,OAAO,CAAC,KAAK,CAAqB;IAClC,OAAO,CAAC,UAAU,CAAS;IAE3B,YAAY,OAAO,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,EAsB/D;IAED;;OAEG;IACH,KAAK,IAAI,IAAI,CAIZ;IAED;;OAEG;IACH,IAAI,IAAI,IAAI,CAeX;IAED;;;;OAIG;IACH,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAIpC;IAED,OAAO,CAAC,aAAa;IAyBrB,OAAO,CAAC,YAAY;CAQpB;AAGD,wBAAgB,gBAAgB,CAAC,OAAO,CAAC,EAAE,qBAAqB,CAAC,OAAO,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,aAAa,CAKxG;AAED,wBAAgB,mBAAmB,IAAI,IAAI,CAG1C","sourcesContent":["/**\n * Config file watcher - debounced fs.watch wrapper for <agent dir>/*.json\n * (~/.phi/agent by default, or PHI_CODING_AGENT_DIR).\n *\n * Per Q5 + Q9: emits \"<file>_changed\" events when models.json or routing.json\n * are modified on disk. Debounced 300ms to coalesce burst writes (atomic\n * tmp+rename produces multiple fs events).\n *\n * Cross-platform: fs.watch is available on Linux/macOS/Windows. On some\n * Windows file systems, fs.watch may fire repeated events; the debounce\n * absorbs them.\n *\n * Events emitted via EventEmitter:\n *   - \"models_json_changed\"   : ~/.phi/agent/models.json changed\n *   - \"routing_json_changed\"  : ~/.phi/agent/routing.json changed\n *   - \"watcher_error\"         : underlying fs.watch error\n *\n * The watcher silently skips files that don't exist when start() is called.\n * Re-call start() after the file is created to begin watching it.\n */\n\nimport { EventEmitter } from \"node:events\";\nimport { existsSync, type FSWatcher, watch } from \"node:fs\";\nimport { basename, dirname, join } from \"node:path\";\nimport { getAgentDir } from \"../config.ts\";\n\ninterface WatchedFile {\n\tpath: string;\n\teventName: string;\n\twatcher: FSWatcher | null;\n\tdebounceTimer: NodeJS.Timeout | null;\n\tignoreUntil: number;\n}\n\nexport class ConfigWatcher extends EventEmitter {\n\tprivate files: WatchedFile[] = [];\n\tprivate debounceMs: number;\n\n\tconstructor(options?: { debounceMs?: number; agentDir?: string }) {\n\t\tsuper();\n\t\tthis.debounceMs = options?.debounceMs ?? 300;\n\t\t// getAgentDir() honors PHI_CODING_AGENT_DIR (and the configured config dir name);\n\t\t// a hard-coded ~/.phi/agent watched files the agent never reads.\n\t\tconst agentDir = options?.agentDir ?? getAgentDir();\n\t\tthis.files = [\n\t\t\t{\n\t\t\t\tpath: join(agentDir, \"models.json\"),\n\t\t\t\teventName: \"models_json_changed\",\n\t\t\t\twatcher: null,\n\t\t\t\tdebounceTimer: null,\n\t\t\t\tignoreUntil: 0,\n\t\t\t},\n\t\t\t{\n\t\t\t\tpath: join(agentDir, \"routing.json\"),\n\t\t\t\teventName: \"routing_json_changed\",\n\t\t\t\twatcher: null,\n\t\t\t\tdebounceTimer: null,\n\t\t\t\tignoreUntil: 0,\n\t\t\t},\n\t\t];\n\t}\n\n\t/**\n\t * Start watching all configured files (no-op for files that don't exist).\n\t */\n\tstart(): void {\n\t\tfor (const file of this.files) {\n\t\t\tthis.startWatching(file);\n\t\t}\n\t}\n\n\t/**\n\t * Stop all watchers and clear pending debounces.\n\t */\n\tstop(): void {\n\t\tfor (const file of this.files) {\n\t\t\tif (file.watcher) {\n\t\t\t\ttry {\n\t\t\t\t\tfile.watcher.close();\n\t\t\t\t} catch {\n\t\t\t\t\t// ignore close errors\n\t\t\t\t}\n\t\t\t\tfile.watcher = null;\n\t\t\t}\n\t\t\tif (file.debounceTimer) {\n\t\t\t\tclearTimeout(file.debounceTimer);\n\t\t\t\tfile.debounceTimer = null;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Tell the watcher to ignore the next change event for a file (used right\n\t * after programmatic write to avoid echoing it back to ourselves).\n\t * The ignore window is debounceMs * 2.\n\t */\n\tmuteForWrite(eventName: string): void {\n\t\tconst file = this.files.find((f) => f.eventName === eventName);\n\t\tif (!file) return;\n\t\tfile.ignoreUntil = Date.now() + this.debounceMs * 2;\n\t}\n\n\tprivate startWatching(file: WatchedFile): void {\n\t\tif (file.watcher) return;\n\t\tconst parentDir = dirname(file.path);\n\t\tconst fileName = basename(file.path);\n\n\t\t// Watch the parent directory so we catch atomic rename (tmp -> file).\n\t\t// Some platforms emit on the parent when a file is created/renamed.\n\t\tif (!existsSync(parentDir)) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst watcher = watch(parentDir, { persistent: false }, (_eventType, changedName) => {\n\t\t\t\tif (changedName !== fileName) return;\n\t\t\t\tthis.scheduleEmit(file);\n\t\t\t});\n\t\t\twatcher.on(\"error\", (err) => {\n\t\t\t\tthis.emit(\"watcher_error\", { path: file.path, error: err });\n\t\t\t});\n\t\t\tfile.watcher = watcher;\n\t\t} catch (err) {\n\t\t\tthis.emit(\"watcher_error\", { path: file.path, error: err });\n\t\t}\n\t}\n\n\tprivate scheduleEmit(file: WatchedFile): void {\n\t\tif (Date.now() < file.ignoreUntil) return;\n\t\tif (file.debounceTimer) clearTimeout(file.debounceTimer);\n\t\tfile.debounceTimer = setTimeout(() => {\n\t\t\tfile.debounceTimer = null;\n\t\t\tthis.emit(file.eventName, { path: file.path });\n\t\t}, this.debounceMs);\n\t}\n}\n\nlet _singleton: ConfigWatcher | null = null;\nexport function getConfigWatcher(options?: ConstructorParameters<typeof ConfigWatcher>[0]): ConfigWatcher {\n\tif (!_singleton) {\n\t\t_singleton = new ConfigWatcher(options);\n\t}\n\treturn _singleton;\n}\n\nexport function _resetConfigWatcher(): void {\n\tif (_singleton) _singleton.stop();\n\t_singleton = null;\n}\n"]}