Heuristic:1mcp app Agent File Watch Debounce Delay
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Configuration |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Debounce file-system watch events by 500ms to coalesce rapid successive writes into a single reload, avoiding redundant processing.
Description
The `PresetManager` watches `presets.json` for external changes and reloads the preset store when the file is modified. File-system watchers often emit multiple events for a single logical change (especially during atomic write-then-rename operations, editor save sequences, or batch edits). A 500ms debounce delay coalesces these burst events into a single reload, preventing redundant parsing, change detection, and client notification cycles.
Usage
Use this heuristic when:
- Experiencing duplicate or excessive reload notifications after external config edits
- Tuning responsiveness vs. noise for file-watch-driven hot-reload
- Understanding why preset change notifications arrive with a slight delay after file saves
The Insight (Rule of Thumb)
- Action: The debounce delay is a fixed 500ms constant (`DEBOUNCE_DELAY`) in `PresetManager`.
- Value: 500ms
- Trade-off: A longer delay reduces false-positive reloads but makes the system less responsive to legitimate changes. A shorter delay increases responsiveness but risks processing incomplete writes.
- Not configurable: This value is a hardcoded constant, not exposed via CLI or app config.
Reasoning
File-system watchers (e.g., `chokidar`/Node `fs.watch`) can emit multiple events for a single user action:
- An editor's "save" may trigger `change` + `rename` events
- Atomic writes (write to temp, rename to target) emit two events
- Batch file operations emit events in rapid succession
Without debouncing, each event would trigger a full reload cycle: parse JSON, diff server lists, detect changes, notify callbacks. With 500ms debounce, only the final stable state is processed. The 500ms window is long enough to coalesce typical burst events but short enough to feel responsive to interactive users.
Code Evidence
Debounce constant from `src/domains/preset/manager/presetManager.ts:33`:
private readonly DEBOUNCE_DELAY = 500; // 500ms debounce delay
Debounce timer usage in the watch callback:
private reloadTimeout: ReturnType<typeof setTimeout> | null = null;
// On file change:
if (this.reloadTimeout) clearTimeout(this.reloadTimeout);
this.reloadTimeout = setTimeout(() => {
this.loadPresets();
// ... detect changes and notify callbacks
}, this.DEBOUNCE_DELAY);