Skip to content

Conversation

@dapi
Copy link
Owner

@dapi dapi commented Feb 2, 2026

Summary

Improve --lock PORT command with smart --force logic and fix bugs where locked ports were incorrectly handled.

Part 1: --lock PORT improvements

  • Show directory in lock success message
  • Smart --force logic per Decision Matrix
  • Unlock old locked port when locking new one for same directory+name

Part 2: Bug fix - locked+busy port ignored

  • port-selector now correctly returns locked+busy port (user's service already running)
  • New priority: locked+free > locked+busy > unlocked+free > unlocked+busy(skip)

Part 3: Bug fix - multiple locked ports

  • Invariant: at most one locked port per directory+name combination

Test plan

  • Unit tests for FindByDirectoryAndNameWithPriority
  • Unit tests for UnlockOtherLockedPorts
  • Integration tests for all Decision Matrix scenarios
  • All existing tests pass
  • Manual testing of --lock behavior

Closes #77

🤖 Generated with Claude Code

dapi and others added 2 commits February 2, 2026 23:26
## Summary

Improve `--lock PORT` command with smart --force logic and fix bugs
where locked ports were incorrectly handled.

## Changes

### Part 1: --lock PORT improvements
- Show directory in lock success message: "Locked port 3001 for 'main' in ~/project"
- Smart --force logic:
  - Free + unlocked port from another dir: allowed without --force (abandoned)
  - Free + locked port from another dir: requires --force
  - Busy port from another dir: blocked completely (stop service first)
  - Busy unallocated port: requires --force (user takes responsibility)
- Unlock old locked port when locking new one for same directory+name

### Part 2: Bug fix - locked+busy port ignored
- Changed FindByDirectoryAndNameWithPriority to use priority:
  1. Locked + free → return
  2. Locked + busy → return (user's service already running)
  3. Unlocked + free → return
  4. Unlocked + busy → skip, find another

### Part 3: Bug fix - multiple locked ports
- UnlockOtherLockedPorts ensures at most one locked port per directory+name
- Called after setting new lock to maintain invariant

## Tests
- Added unit tests for FindByDirectoryAndNameWithPriority
- Added unit tests for UnlockOtherLockedPorts
- Added integration tests for all Decision Matrix scenarios

Refs #77

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Improvements based on code review feedback:

- Add warning to stderr when UpdateLastUsedByPort fails (MEDIUM fix)
- Add debug logging to UnlockOtherLockedPorts
- Add TestFindByDirectoryAndNameWithPriority_NilPortChecker
- Add TestFindByDirectoryAndNameWithPriority_TieBreakByPort
- Add TestFindByDirectoryAndNameWithPriority_NoMatchingAllocations
- Add TestUnlockOtherLockedPorts_EmptyStore
- Add TestLockPort_SameDirectoryDifferentName
- Add TestLockPort_SameDirectorySamePortIdempotent

Total: +191 lines of tests and improvements

Refs #77

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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: --lock PORT locks wrong directory's allocation instead of reassigning

1 participant