feat(kugetsu): implement worktree isolation + lock mechanism for delegation coordination #71

Closed
opened 2026-04-01 10:47:17 +02:00 by shoko · 0 comments
Owner

Problem

Current delegation system has no coordination:

  • cmd_delegate is fire-and-forget - spawns new opencode process each time
  • Multiple delegations sent quickly result in concurrent processes
  • Concurrent processes modify same worktrees, causing conflicts
  • PM gets stuck on TTY-dependent operations (vim for git rebase)

Desired Behavior

Hybrid approach: worktree isolation + lock mechanism

Worktree Isolation (existing)

  • Each issue gets its own worktree
  • Filesystem-level separation

Lock Mechanism (new)

  • Before working on issue, acquire lock file: ~/.kugetsu/locks/<issue-ref>.lock
  • Lock contains: PID, session_id, timestamp
  • Check lock before starting work
  • If locked, either:
    • Wait and retry (with timeout)
    • Fail with "issue already being worked on"
  • Release lock when done (or on crash, stale lock detection)

Delegation Coordination

Option A - Serialized delegation:

# cmd_delegate checks PM is idle before sending
if pm_busy; then
    queue_delegation
else
    send_to_pm
fi

Option B - PM session mutex:

# PM session has a lock
# Only one delegation process can send to PM at a time
acquire_mutex(pm_session)
send_to_pm
release_mutex(pm_session)

Option C - Delegation queue:

# All delegations go to a queue file
# PM reads queue and processes one by one

Tasks

  1. Create lock directory: ~/.kugetsu/locks/
  2. Implement lock acquisition/release in kugetsu script
  3. Modify cmd_start to check lock before creating worktree
  4. Modify cmd_delegate to either queue or check PM busy status
  5. Add stale lock detection (if PID no longer exists)
  6. Document locking protocol
  7. Test concurrent delegation scenarios

Tradeoffs

Approach Pros Cons
Worktree only Simple, existing No coordination, conflicts
Lock only Coordinates access Still need isolation
Hybrid (proposed) Best of both More complexity
## Problem Current delegation system has no coordination: - cmd_delegate is fire-and-forget - spawns new opencode process each time - Multiple delegations sent quickly result in concurrent processes - Concurrent processes modify same worktrees, causing conflicts - PM gets stuck on TTY-dependent operations (vim for git rebase) ## Desired Behavior Hybrid approach: worktree isolation + lock mechanism ### Worktree Isolation (existing) - Each issue gets its own worktree - Filesystem-level separation ### Lock Mechanism (new) - Before working on issue, acquire lock file: `~/.kugetsu/locks/<issue-ref>.lock` - Lock contains: PID, session_id, timestamp - Check lock before starting work - If locked, either: - Wait and retry (with timeout) - Fail with "issue already being worked on" - Release lock when done (or on crash, stale lock detection) ### Delegation Coordination Option A - Serialized delegation: ```bash # cmd_delegate checks PM is idle before sending if pm_busy; then queue_delegation else send_to_pm fi ``` Option B - PM session mutex: ```bash # PM session has a lock # Only one delegation process can send to PM at a time acquire_mutex(pm_session) send_to_pm release_mutex(pm_session) ``` Option C - Delegation queue: ```bash # All delegations go to a queue file # PM reads queue and processes one by one ``` ## Tasks 1. Create lock directory: `~/.kugetsu/locks/` 2. Implement lock acquisition/release in kugetsu script 3. Modify cmd_start to check lock before creating worktree 4. Modify cmd_delegate to either queue or check PM busy status 5. Add stale lock detection (if PID no longer exists) 6. Document locking protocol 7. Test concurrent delegation scenarios ## Tradeoffs | Approach | Pros | Cons | |----------|------|------| | Worktree only | Simple, existing | No coordination, conflicts | | Lock only | Coordinates access | Still need isolation | | Hybrid (proposed) | Best of both | More complexity |
shoko added the parallelization label 2026-04-02 00:30:43 +02:00
shoko closed this issue 2026-04-02 04:52:45 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shoko/kugetsu#71