You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: watch service can go stale while process stays alive (self-heal + heartbeat) #580
bm watch can remain alive but stop indexing filesystem changes after long uptime (seen overnight on macOS).
This creates a silent failure mode for downstream tools: files exist on disk, but search/read by permalink and graph traversal lag indefinitely until manual watcher restart.
High — long-running watcher silently stops indexing; no automatic recovery
Component
sync/ (watch service), cli/ (watch command)
Complexity
Complex — requires heartbeat mechanism, staleness detection, auto-restart logic, and status JSON schema changes
No duplicate issues found.
Labels applied: bug, enhancement
Root Cause Analysis
After reading src/basic_memory/sync/watch_service.py, the current design relies entirely on watchfiles.awatch() to deliver filesystem events. If awatch() internally goes stale (e.g., due to macOS FSEvents kernel buffer overflow, inotify limit exhaustion on Linux, or a hung OS-level watch descriptor), the async for loop simply blocks forever — there are no events delivered and no timeout or health check to detect this.
The existing _schedule_restart() mechanism does restart the watch cycle periodically (via watch_project_reload_interval), but:
It only resets after the previous cycle completes, meaning a hung awatch() blocks the restart timer
There is no heartbeat to distinguish "no files changed" from "watcher is stuck"
watch_status.json only records last_scan (updated when files actually change), not a liveness heartbeat
Key gap in _watch_projects_cycle (line 120–157):
asyncforchangesinawatch(*project_paths, stop_event=stop_event):
# This blocks indefinitely if awatch goes silent
If awatch stalls, neither the timer task nor the outer loop detect it — the process remains alive and healthy-looking but processes zero events.
Requested Changes Summary
The issue requests four additions:
Heartbeat timestamp — updated periodically even when no file events occur (needs a concurrent heartbeat coroutine alongside awatch)
Staleness detection — compare last_heartbeat to now; if gap exceeds configurable threshold, trigger restart
Forced cycle restart — cancel the hung awatch task, log a structured warning with restart reason
Status JSON exposure — add last_heartbeat, stale_cycle_count, last_restart_reason to WatchServiceState / watch_status.json
Implementation Notes
A heartbeat coroutine should run concurrently with awatch using asyncio.gather or asyncio.create_task, updating last_heartbeat every N seconds and checking for staleness
The staleness threshold should be configurable via BasicMemoryConfig (e.g., watch_stale_threshold_seconds, defaulting to something like 300s)
When staleness is detected, signal stop_event to break the awatch loop and let the outer while self.state.running loop restart the cycle
WatchServiceState needs new fields: last_heartbeat, stale_cycle_count, last_restart_reason
An integration/deterministic unit test should inject a mock awatch that stalls and verify the staleness detection fires and restarts the cycle
Activity
claude commentedon Feb 18, 2026
Claude finished @phernandez's task in 1m 26s —— View job
Triage Analysis
Issue Classification
sync/(watch service),cli/(watch command)No duplicate issues found.
Labels applied:
bug,enhancementRoot Cause Analysis
After reading
src/basic_memory/sync/watch_service.py, the current design relies entirely onwatchfiles.awatch()to deliver filesystem events. Ifawatch()internally goes stale (e.g., due to macOSFSEventskernel buffer overflow, inotify limit exhaustion on Linux, or a hung OS-level watch descriptor), theasync forloop simply blocks forever — there are no events delivered and no timeout or health check to detect this.The existing
_schedule_restart()mechanism does restart the watch cycle periodically (viawatch_project_reload_interval), but:awatch()blocks the restart timerwatch_status.jsononly recordslast_scan(updated when files actually change), not a liveness heartbeatKey gap in
_watch_projects_cycle(line 120–157):If
awatchstalls, neither the timer task nor the outer loop detect it — the process remains alive and healthy-looking but processes zero events.Requested Changes Summary
The issue requests four additions:
awatch)last_heartbeattonow; if gap exceeds configurable threshold, trigger restartawatchtask, log a structured warning with restart reasonlast_heartbeat,stale_cycle_count,last_restart_reasontoWatchServiceState/watch_status.jsonImplementation Notes
awatchusingasyncio.gatherorasyncio.create_task, updatinglast_heartbeatevery N seconds and checking for stalenessBasicMemoryConfig(e.g.,watch_stale_threshold_seconds, defaulting to something like 300s)stop_eventto break theawatchloop and let the outerwhile self.state.runningloop restart the cycleWatchServiceStateneeds new fields:last_heartbeat,stale_cycle_count,last_restart_reasonawatchthat stalls and verify the staleness detection fires and restarts the cyclephernandez commentedon Feb 18, 2026
Downstream tracking