File size: 10,033 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | import chokidar, { type FSWatcher } from 'chokidar'
import * as platformPath from 'path'
import { getAdditionalDirectoriesForClaudeMd } from '../../bootstrap/state.js'
import {
clearCommandMemoizationCaches,
clearCommandsCache,
} from '../../commands.js'
import {
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
logEvent,
} from '../../services/analytics/index.js'
import {
clearSkillCaches,
getSkillsPath,
onDynamicSkillsLoaded,
} from '../../skills/loadSkillsDir.js'
import { resetSentSkillNames } from '../attachments.js'
import { registerCleanup } from '../cleanupRegistry.js'
import { logForDebugging } from '../debug.js'
import { getFsImplementation } from '../fsOperations.js'
import { executeConfigChangeHooks, hasBlockingResult } from '../hooks.js'
import { createSignal } from '../signal.js'
/**
* Time in milliseconds to wait for file writes to stabilize before processing.
*/
const FILE_STABILITY_THRESHOLD_MS = 1000
/**
* Polling interval in milliseconds for checking file stability.
*/
const FILE_STABILITY_POLL_INTERVAL_MS = 500
/**
* Time in milliseconds to debounce rapid skill change events into a single
* reload. Prevents cascading reloads when many skill files change at once
* (e.g. during auto-update or when another session modifies skill directories).
* Without this, each file change triggers a full clearSkillCaches() +
* clearCommandsCache() + listener notification cycle, which can deadlock the
* event loop when dozens of events fire in rapid succession.
*/
const RELOAD_DEBOUNCE_MS = 300
/**
* Polling interval for chokidar when usePolling is enabled.
* Skill files change rarely (manual edits, git operations), so a 2s interval
* trades negligible latency for far fewer stat() calls than the default 100ms.
*/
const POLLING_INTERVAL_MS = 2000
/**
* Bun's native fs.watch() has a PathWatcherManager deadlock (oven-sh/bun#27469,
* #26385): closing a watcher on the main thread while the File Watcher thread
* is delivering events can hang both threads in __ulock_wait2 forever. Chokidar
* with depth: 2 on large skill trees (hundreds of subdirs) triggers this
* reliably when a git operation touches many directories at once — chokidar
* internally closes/reopens per-directory FSWatchers as dirs are added/removed.
*
* Workaround: use stat() polling under Bun. No FSWatcher = no deadlock.
* The fix is pending upstream; remove this once the Bun PR lands.
*/
const USE_POLLING = typeof Bun !== 'undefined'
let watcher: FSWatcher | null = null
let reloadTimer: ReturnType<typeof setTimeout> | null = null
const pendingChangedPaths = new Set<string>()
let initialized = false
let disposed = false
let dynamicSkillsCallbackRegistered = false
let unregisterCleanup: (() => void) | null = null
const skillsChanged = createSignal()
// Test overrides for timing constants
let testOverrides: {
stabilityThreshold?: number
pollInterval?: number
reloadDebounce?: number
/** Chokidar fs.stat polling interval when USE_POLLING is active. */
chokidarInterval?: number
} | null = null
/**
* Initialize file watching for skill directories
*/
export async function initialize(): Promise<void> {
if (initialized || disposed) return
initialized = true
// Register callback for when dynamic skills are loaded (only once)
if (!dynamicSkillsCallbackRegistered) {
dynamicSkillsCallbackRegistered = true
onDynamicSkillsLoaded(() => {
// Clear memoization caches so new skills are picked up
// Note: we use clearCommandMemoizationCaches (not clearCommandsCache)
// because clearCommandsCache would call clearSkillCaches which
// wipes out the dynamic skills we just loaded
clearCommandMemoizationCaches()
// Notify listeners that skills changed
skillsChanged.emit()
})
}
const paths = await getWatchablePaths()
if (paths.length === 0) return
logForDebugging(
`Watching for changes in skill/command directories: ${paths.join(', ')}...`,
)
watcher = chokidar.watch(paths, {
persistent: true,
ignoreInitial: true,
depth: 2, // Skills use skill-name/SKILL.md format
awaitWriteFinish: {
stabilityThreshold:
testOverrides?.stabilityThreshold ?? FILE_STABILITY_THRESHOLD_MS,
pollInterval:
testOverrides?.pollInterval ?? FILE_STABILITY_POLL_INTERVAL_MS,
},
// Ignore special file types (sockets, FIFOs, devices) - they cannot be watched
// and will error with EOPNOTSUPP on macOS. Only allow regular files and directories.
ignored: (path, stats) => {
if (stats && !stats.isFile() && !stats.isDirectory()) return true
// Ignore .git directories
return path.split(platformPath.sep).some(dir => dir === '.git')
},
ignorePermissionErrors: true,
usePolling: USE_POLLING,
interval: testOverrides?.chokidarInterval ?? POLLING_INTERVAL_MS,
atomic: true,
})
watcher.on('add', handleChange)
watcher.on('change', handleChange)
watcher.on('unlink', handleChange)
// Register cleanup to properly dispose of the file watcher during graceful shutdown
unregisterCleanup = registerCleanup(async () => {
await dispose()
})
}
/**
* Clean up file watcher
*/
export function dispose(): Promise<void> {
disposed = true
if (unregisterCleanup) {
unregisterCleanup()
unregisterCleanup = null
}
let closePromise: Promise<void> = Promise.resolve()
if (watcher) {
closePromise = watcher.close()
watcher = null
}
if (reloadTimer) {
clearTimeout(reloadTimer)
reloadTimer = null
}
pendingChangedPaths.clear()
skillsChanged.clear()
return closePromise
}
/**
* Subscribe to skill changes
*/
export const subscribe = skillsChanged.subscribe
async function getWatchablePaths(): Promise<string[]> {
const fs = getFsImplementation()
const paths: string[] = []
// User skills directory (~/.claude/skills)
const userSkillsPath = getSkillsPath('userSettings', 'skills')
if (userSkillsPath) {
try {
await fs.stat(userSkillsPath)
paths.push(userSkillsPath)
} catch {
// Path doesn't exist, skip it
}
}
// User commands directory (~/.claude/commands)
const userCommandsPath = getSkillsPath('userSettings', 'commands')
if (userCommandsPath) {
try {
await fs.stat(userCommandsPath)
paths.push(userCommandsPath)
} catch {
// Path doesn't exist, skip it
}
}
// Project skills directory (.claude/skills)
const projectSkillsPath = getSkillsPath('projectSettings', 'skills')
if (projectSkillsPath) {
try {
// For project settings, resolve to absolute path
const absolutePath = platformPath.resolve(projectSkillsPath)
await fs.stat(absolutePath)
paths.push(absolutePath)
} catch {
// Path doesn't exist, skip it
}
}
// Project commands directory (.claude/commands)
const projectCommandsPath = getSkillsPath('projectSettings', 'commands')
if (projectCommandsPath) {
try {
// For project settings, resolve to absolute path
const absolutePath = platformPath.resolve(projectCommandsPath)
await fs.stat(absolutePath)
paths.push(absolutePath)
} catch {
// Path doesn't exist, skip it
}
}
// Additional directories (--add-dir) skills
for (const dir of getAdditionalDirectoriesForClaudeMd()) {
const additionalSkillsPath = platformPath.join(dir, '.claude', 'skills')
try {
await fs.stat(additionalSkillsPath)
paths.push(additionalSkillsPath)
} catch {
// Path doesn't exist, skip it
}
}
return paths
}
function handleChange(path: string): void {
logForDebugging(`Detected skill change: ${path}`)
logEvent('tengu_skill_file_changed', {
source:
'chokidar' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
})
scheduleReload(path)
}
/**
* Debounce rapid skill changes into a single reload. When many skill files
* change at once (e.g. auto-update installs a new binary and a new session
* touches skill directories), each file fires its own chokidar event. Without
* debouncing, each event triggers clearSkillCaches() + clearCommandsCache() +
* listener notification — 30 events means 30 full reload cycles, which can
* deadlock the Bun event loop via rapid FSWatcher watch/unwatch churn.
*/
function scheduleReload(changedPath: string): void {
pendingChangedPaths.add(changedPath)
if (reloadTimer) clearTimeout(reloadTimer)
reloadTimer = setTimeout(async () => {
reloadTimer = null
const paths = [...pendingChangedPaths]
pendingChangedPaths.clear()
// Fire ConfigChange hook once for the batch — the hook query is always
// 'skills' so firing per-path (which can be hundreds during a git
// operation) just spams the hook matcher with identical queries. Pass the
// first path as a representative; hooks can inspect all paths via the
// skills directory if they need the full set.
const results = await executeConfigChangeHooks('skills', paths[0]!)
if (hasBlockingResult(results)) {
logForDebugging(
`ConfigChange hook blocked skill reload (${paths.length} paths)`,
)
return
}
clearSkillCaches()
clearCommandsCache()
resetSentSkillNames()
skillsChanged.emit()
}, testOverrides?.reloadDebounce ?? RELOAD_DEBOUNCE_MS)
}
/**
* Reset internal state for testing purposes only.
*/
export async function resetForTesting(overrides?: {
stabilityThreshold?: number
pollInterval?: number
reloadDebounce?: number
chokidarInterval?: number
}): Promise<void> {
// Clean up existing watcher if present to avoid resource leaks
if (watcher) {
await watcher.close()
watcher = null
}
if (reloadTimer) {
clearTimeout(reloadTimer)
reloadTimer = null
}
pendingChangedPaths.clear()
skillsChanged.clear()
initialized = false
disposed = false
testOverrides = overrides ?? null
}
export const skillChangeDetector = {
initialize,
dispose,
subscribe,
resetForTesting,
}
|