Skip to content

Implement stale Git lock file detection and handling (Issue #175)#182

Merged
kaeawc merged 1 commit intomainfrom
work/175-bug--claude-code-stale-lock-files-interf
Jan 7, 2026
Merged

Implement stale Git lock file detection and handling (Issue #175)#182
kaeawc merged 1 commit intomainfrom
work/175-bug--claude-code-stale-lock-files-interf

Conversation

@kaeawc
Copy link
Copy Markdown
Owner

@kaeawc kaeawc commented Jan 7, 2026

Summary

This PR implements comprehensive stale Git lock file detection and handling to address Issue #175, which describes how Claude Code's background Git operations can leave stale lock files that interfere with auto-worktree operations.

Changes

New Files

  • internal/git/lockfile.go - Lock file detection, process verification, and management utilities
  • internal/git/lockfile_test.go - Comprehensive test suite (8 test suites, all passing)

Modified Files

  • internal/git/executor.go - Added retry logic with lock file detection for all Git operations
  • internal/cmd/commands.go - New RunDoctor command and startup lock detection
  • cmd/auto-worktree/main.go - CLI integration for doctor command
  • README.md - Added troubleshooting section with lock file documentation

Features

1. Lock File Detection

  • Process-based stale detection (checks if owning process exists via PID)
  • Handles both main repo and worktree-specific lock files
  • Supports .git files (worktree pointers) and directories

2. Automatic Retry Logic

  • Git operations retry up to 3 times when encountering lock file errors
  • 1-second delay between retries
  • Displays warnings with detailed lock file information
  • Non-lock errors fail immediately without retries

3. Doctor Command

auto-worktree doctor --check-locks                    # Detect and report
auto-worktree doctor --check-locks --remove-locks     # Detect and remove
  • Distinguishes between active and stale locks
  • Safe removal (refuses to remove locks from running processes)
  • Clear, actionable output

4. Startup Integration

  • Detects stale locks during initialization
  • Warns users before attempting operations
  • Non-blocking (doesn't prevent operations from continuing)

Implementation Details

Conservative Approach

  • Warn-only by default: Never auto-removes locks without explicit user action
  • Process verification: Only considers locks stale if owning process doesn't exist
  • Safe operations: Refuses to remove active locks

Error Detection Pattern

Identifies lock file errors by checking for:

  • "index.lock" in error message
  • ".lock" extension
  • "unable to create" messages
  • "file exists" in .git directory

Test Coverage

  • ✅ Lock file error detection
  • ✅ Stale lock identification
  • ✅ Safe lock removal
  • ✅ Process verification
  • ✅ Warning message formatting
  • ✅ Edge cases (non-existent files, invalid repos, etc.)

Testing

All validation passed:

✅ All Go tests passing (100% success rate)
✅ Build successful
✅ go vet clean
✅ gofmt compliant

Documentation

Added comprehensive troubleshooting section to README.md covering:

  • Symptoms of lock file issues
  • Solutions (doctor command, automatic detection, manual cleanup)
  • Prevention and best practices
  • Integration with AI agents

Usage Examples

Check for stale locks:

$ auto-worktree doctor --check-locks
Running repository diagnostics...

🔍 Checking for Git lock files...

⚠️  Found 1 stale lock file(s):
  • /path/.git/index.lock (age: 5m, pid: 12345, stale)

These lock files may be preventing Git operations.
To remove them, run: auto-worktree doctor --check-locks --remove-locks

✓ Diagnostics complete

Remove stale locks:

$ auto-worktree doctor --check-locks --remove-locks
Running repository diagnostics...

🔍 Checking for Git lock files...

⚠️  Found 1 stale lock file(s):
  • /path/.git/index.lock (age: 5m, pid: 12345, stale)

Removing stale lock files...
  ✓ Removed /path/.git/index.lock

✓ Removed 1 stale lock file(s)

✓ Diagnostics complete

Related Issues

Test Plan

  • Unit tests for all lock file functionality
  • Integration with existing Git operations
  • Manual testing with simulated stale locks
  • Verification of process-based detection
  • Doctor command with both flags
  • Startup warning display

🤖 Generated with Claude Code

This comprehensive solution handles stale lock files from background Git
operations (like Claude Code) that can interfere with auto-worktree operations.

Features:
- Process-based stale detection: Checks if lock file owner process exists
- Automatic retry logic: Git operations retry up to 3 times on lock errors
- New 'doctor' command: Detects and optionally removes stale locks
- Startup integration: Warns about stale locks during initialization
- Comprehensive tests: Full test coverage for all lock file functionality

Components:
1. internal/git/lockfile.go - Lock file detection and management
2. internal/git/lockfile_test.go - Complete test suite
3. internal/git/executor.go - Retry logic for git operations
4. internal/cmd/commands.go - New RunDoctor command and startup integration
5. cmd/auto-worktree/main.go - CLI integration for doctor command
6. README.md - Documentation with troubleshooting section

The implementation is conservative (warn-only by default), user-friendly,
and designed to work seamlessly with AI agent workflows.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@kaeawc kaeawc enabled auto-merge (squash) January 7, 2026 17:35
@kaeawc kaeawc merged commit 1212d33 into main Jan 7, 2026
11 checks passed
@kaeawc kaeawc deleted the work/175-bug--claude-code-stale-lock-files-interf branch January 7, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Claude Code stale lock files interfere with auto-worktree operations

1 participant