Fix FileWatcher robustness issues (bd-71)
- Fix Debouncer race condition with sequence numbers to prevent double-fire - Add parent directory watch to catch file creates/renames - Add .git/HEAD watch for branch change detection - Implement retry/backoff (50-400ms) for re-establishing JSONL watch - Handle Create/Chmod events in addition to Write - Add .git/HEAD polling in polling mode - All 18 debouncer and watcher tests pass Amp-Thread-ID: https://ampcode.com/threads/T-4029d643-b4b4-4d3b-bd85-74461f78cd7f Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
@@ -12,6 +12,7 @@ type Debouncer struct {
|
||||
timer *time.Timer
|
||||
duration time.Duration
|
||||
action func()
|
||||
seq uint64 // Sequence number to prevent stale timer fires
|
||||
}
|
||||
|
||||
// NewDebouncer creates a new debouncer with the given duration and action.
|
||||
@@ -34,11 +35,21 @@ func (d *Debouncer) Trigger() {
|
||||
d.timer.Stop()
|
||||
}
|
||||
|
||||
// Increment sequence number to invalidate any pending timers
|
||||
d.seq++
|
||||
currentSeq := d.seq
|
||||
|
||||
d.timer = time.AfterFunc(d.duration, func() {
|
||||
d.action()
|
||||
d.mu.Lock()
|
||||
d.timer = nil
|
||||
d.mu.Unlock()
|
||||
defer d.mu.Unlock()
|
||||
|
||||
// Only fire if this is still the latest trigger
|
||||
if d.seq == currentSeq {
|
||||
d.timer = nil
|
||||
d.mu.Unlock() // Unlock before calling action to avoid holding lock during callback
|
||||
d.action()
|
||||
d.mu.Lock() // Re-lock for defer
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user