Skip to content

fix: improve --lock PORT behavior and fix locked ports handling (#77)#79

Merged
dapi merged 3 commits intomasterfrom
fix/77-lock-port-improvements
Feb 12, 2026
Merged

fix: improve --lock PORT behavior and fix locked ports handling (#77)#79
dapi merged 3 commits intomasterfrom
fix/77-lock-port-improvements

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 12, 2026 15:17
## 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>
@dapi dapi force-pushed the fix/77-lock-port-improvements branch from 9de5bf1 to 0609f57 Compare February 12, 2026 12:19
…Port

The test was using CombinedOutput() which mixed stdout and stderr.
In CI, logger warning goes to stderr, causing test to fail when
comparing port number output.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@dapi dapi merged commit 3dcd71d into master Feb 12, 2026
7 checks passed
@dapi dapi deleted the fix/77-lock-port-improvements branch February 12, 2026 13:15
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