Skip to content
This repository was archived by the owner on Apr 9, 2026. It is now read-only.

fix(watcher): add debounce to prevent duplicate IPC actions - #78

Merged
aaajiao merged 6 commits into
mainfrom
fix/ipc-debounce
Jan 12, 2026
Merged

aaajiao merged 6 commits into
mainfrom
fix/ipc-debounce

Conversation

@aaajiao

@aaajiao aaajiao commented Jan 12, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes duplicate browser windows and notifications when OpenCode writes to IPC files multiple times per action.

Problem

When OpenCode triggers IPC events (e.g., opening WebUI, sending notifications), it writes to IPC files 7-11 times per action due to internal implementation. The OCD watcher had a race condition:

  1. Read content from file
  2. Execute side-effect (slow - e.g., open command)
  3. Clear file

This allowed subsequent writes to trigger additional executions before the file was cleared.

Solution

Atomic Consumption

Changed order to: read → clear → execute

This ensures the file is cleared before the slow side-effect runs, preventing race conditions.

Debounce Logic

  • URL handler: 3-second window, same URL = skip
  • Notification handler: 3-second window, same content hash = skip

State stored in /tmp/.ocd_ipc_state/ with timestamp + content tracking.

Changes

  • lib/watcher.sh: Added atomic consumption + debounce to ocd_handle_url and ocd_handle_notify
  • tests/bats/watcher.bats: Added 6 new tests for debounce behavior

Testing

# Run tests on Mac
bats tests/bats/watcher.bats

# Manual test
devocd -r  # Rebuild with fix
# In TUI: Cmd+O to open WebUI → should only open 1 window

OpenCode Agent added 2 commits January 12, 2026 23:14
…ations

- Change consumption order: read content, clear file, then execute side-effect
- Add 3-second debounce window for identical URL/notification content
- Use md5 hash for notification content comparison
- Fixes issue where OpenCode writes IPC files 7-11 times per action
@aaajiao
aaajiao changed the base branch from dev to main January 12, 2026 22:20
OpenCode Agent added 4 commits January 12, 2026 23:21
Problem: fswatch processes were becoming orphans because:
- ocd_start_watcher runs in subshell, returns subshell PID
- kill only killed subshell, not fswatch (child process)
- Result: 130+ zombie fswatch processes accumulating

Solution:
- Enable job control (set -m) before spawning watcher
- This makes subshell a process group leader
- kill -- -$PID now correctly kills entire group
- Add pkill fallback for any residual processes
- Use process group kill in startup cleanup too
The previous fix used set -m inside ocd_start_watcher, but when called
via $() command substitution, it ran in a subshell where set -m had
no effect on the main shell's process group handling.

Changes:
- ocd_start_watcher now sets global _OCD_WATCHER_PID directly
- bin/ocd calls ocd_start_watcher without $() wrapper
- This ensures set -m runs in main shell, making process group kill work
- Added pkill fallback in _ocd_cleanup for extra safety
@aaajiao
aaajiao merged commit b6edcff into main Jan 12, 2026
4 checks passed
@aaajiao
aaajiao deleted the fix/ipc-debounce branch January 12, 2026 23:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant