()
| 995 | // fs.watchFile polls stat on the libuv threadpool and only calls us when mtime |
| 996 | // changed — a stalled stat never blocks the main thread. |
| 997 | function startGlobalConfigFreshnessWatcher(): void { |
| 998 | if (freshnessWatcherStarted || process.env.NODE_ENV === 'test') return |
| 999 | freshnessWatcherStarted = true |
| 1000 | const file = getGlobalClaudeFile() |
| 1001 | watchFile( |
| 1002 | file, |
| 1003 | { interval: CONFIG_FRESHNESS_POLL_MS, persistent: false }, |
| 1004 | curr => { |
| 1005 | // Our own writes fire this too — the write-through's Date.now() |
| 1006 | // overshoot makes cache.mtime > file mtime, so we skip the re-read. |
| 1007 | // Bun/Node also fire with curr.mtimeMs=0 when the file doesn't exist |
| 1008 | // (initial callback or deletion) — the <= handles that too. |
| 1009 | if (curr.mtimeMs <= globalConfigCache.mtime) return |
| 1010 | void getFsImplementation() |
| 1011 | .readFile(file, { encoding: 'utf-8' }) |
| 1012 | .then(content => { |
| 1013 | // A write-through may have advanced the cache while we were reading; |
| 1014 | // don't regress to the stale snapshot watchFile stat'd. |
| 1015 | if (curr.mtimeMs <= globalConfigCache.mtime) return |
| 1016 | const parsed = safeParseJSON(stripBOM(content)) |
| 1017 | if (parsed === null || typeof parsed !== 'object') return |
| 1018 | globalConfigCache = { |
| 1019 | config: migrateConfigFields({ |
| 1020 | ...createDefaultGlobalConfig(), |
| 1021 | ...(parsed as Partial<GlobalConfig>), |
| 1022 | }), |
| 1023 | mtime: curr.mtimeMs, |
| 1024 | } |
| 1025 | lastReadFileStats = { mtime: curr.mtimeMs, size: curr.size } |
| 1026 | }) |
| 1027 | .catch(() => {}) |
| 1028 | }, |
| 1029 | ) |
| 1030 | registerCleanup(async () => { |
| 1031 | unwatchFile(file) |
| 1032 | freshnessWatcherStarted = false |
| 1033 | }) |
| 1034 | } |
| 1035 | |
| 1036 | // Write-through: what we just wrote IS the new config. cache.mtime overshoots |
| 1037 | // the file's real mtime (Date.now() is recorded after the write) so the |
no test coverage detected