feat(kugetsu): add lock mechanism for worktree coordination #90

Closed
shoko wants to merge 1 commits from feat/issue-71-lock-mechanism into main
Owner

Summary

Fix for #71 - Worktree isolation + lock mechanism for delegation coordination.

Problem

Concurrent processes can modify the same worktree, causing conflicts. The PM agent can get stuck on TTY-dependent operations.

Solution

Add file-based locking mechanism:

Lock Files

  • Location: ~/.kugetsu/locks/<issue-ref>.lock
  • Format: PID:session_id:timestamp
  • Stale lock detection: if PID no longer exists, lock is removed

Functions

  • acquire_lock <issue_ref> <session_id> - Acquire lock before work
  • release_lock <issue_ref> - Release lock (only if held by current PID)
  • release_all_locks <session_id> - Release all locks for a session
  • check_lock <issue_ref> - Check if issue is locked

Changes

  • cmd_start: Acquires lock before creating worktree, releases after
  • cmd_destroy: Releases lock when destroying session
  • Trap handlers for cleanup on EXIT/INT/TERM

Benefits

  • Prevents concurrent access to worktrees
  • Stale lock cleanup automatically
  • Lock files released on process crash

Fixes #71

## Summary Fix for #71 - Worktree isolation + lock mechanism for delegation coordination. ## Problem Concurrent processes can modify the same worktree, causing conflicts. The PM agent can get stuck on TTY-dependent operations. ## Solution Add file-based locking mechanism: ### Lock Files - Location: `~/.kugetsu/locks/<issue-ref>.lock` - Format: `PID:session_id:timestamp` - Stale lock detection: if PID no longer exists, lock is removed ### Functions - `acquire_lock <issue_ref> <session_id>` - Acquire lock before work - `release_lock <issue_ref>` - Release lock (only if held by current PID) - `release_all_locks <session_id>` - Release all locks for a session - `check_lock <issue_ref>` - Check if issue is locked ### Changes - `cmd_start`: Acquires lock before creating worktree, releases after - `cmd_destroy`: Releases lock when destroying session - Trap handlers for cleanup on EXIT/INT/TERM ### Benefits - Prevents concurrent access to worktrees - Stale lock cleanup automatically - Lock files released on process crash Fixes #71
shoko added 1 commit 2026-04-02 01:26:38 +02:00
Add lock mechanism to prevent concurrent access to worktrees:

- Add LOCKS_DIR constant (\~/.kugetsu/locks)
- Add acquire_lock() - acquires lock file with PID, session_id, timestamp
- Add release_lock() - releases lock if held by current process
- Add release_all_locks() - releases all locks for a session
- Add check_lock() - check if issue is locked
- Modify cmd_start to acquire lock before creating worktree
- Modify cmd_destroy to release lock when destroying session
- Add trap for cleanup on EXIT/INT/TERM

Lock files are named after issue ref with format:
  <issue-ref>.lock
Contains: PID:session_id:timestamp

Stale lock detection: if PID no longer exists, lock is considered stale.

This prevents concurrent processes from modifying the same worktree.

Fixes #71
shoko closed this pull request 2026-04-02 02:57:49 +02:00

Pull request closed

Sign in to join this conversation.