Handle remote multi-session port conflicts with port-range fallback
Summary
Remote mode currently defaults every Plannotator server to the fixed port 19432 so SSH/devcontainer port forwarding stays predictable. That works for a single session, but breaks when multiple Claude Code / OpenCode sessions are started on the same remote machine: the second session and later hit EADDRINUSE and fail.
Today the server only retries the same port a few times; it never searches for another free port.
Environment
- OpenCode v1.3.6
- Plannotator v0.16.0
- Remote mode via SSH (connecting from a local client to a remote dev machine running OpenCode)
Why this happens
- Local mode uses port
0, so the OS picks a free port. - Remote mode uses fixed port
19432inpackages/server/remote.ts. - Plan/review/annotate servers retry the same port instead of falling back to another one.
PLANNOTATOR_PORTworks as a manual escape hatch, but requiring users to manage ports in~/.zshrcis not ideal.
Proposed solution
Use a small remote port range instead of a single fixed port.
Suggested behavior:
- Keep
19432as the default starting port. - In remote mode, if
19432is busy, try19433,19434, ... up to19439. - If
PLANNOTATOR_PORTis explicitly set, preserve current behavior and only try that exact port. - Keep local behavior unchanged (
0=> OS-assigned free port).
Why this approach
Pros:
- Fixes multi-session collisions for the common case.
- Still keeps remote port forwarding predictable.
- Requires relatively small backend changes.
- No frontend changes.
- No new long-lived daemon/service.
Tradeoffs:
- Remote users should forward a small range instead of a single port.
- More than ~8 simultaneous sessions would still fail.
Files likely involved
packages/server/remote.tspackages/server/index.tspackages/server/review.tspackages/server/annotate.tsapps/pi-extension/server/network.tspackages/server/remote.test.ts- remote/devcontainer docs
Considered alternative: shared reverse proxy
A single stable proxy port with per-session routing is possible, but it looks much heavier than the problem warrants:
- frontend currently hardcodes many root-relative
/api/...calls - would require a long-lived proxy/daemon and session registration lifecycle
- would add more complexity around SSE/streaming and cleanup
For this problem, port-range fallback seems like the better tradeoff.
Here's the workaround I've been using in ~/.zshrc so far:
# Find the first available port in the Plannotator range
_pick_plannotator_port() {
python3 - <<'PY'
import socket
for port in range(19432, 19550):
with socket.socket() as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind(("127.0.0.1", port))
print(port)
break
except OSError:
pass
PY
}
# Wrapper: inject a free port before launching opencode
oc() {
local port
port="$(_pick_plannotator_port)"
if [[ -z "$port" ]]; then
print -u2 'oc: no free PLANNOTATOR_PORT found in range 19432–19549'
return 1
fi
PLANNOTATOR_PORT="$port" opencode "$@"
}
This works for the most part, but it has a race condition: the port is probed at launch time, but the socket isn't actually bound until Plannotator needs it. If two shells open in quick succession, both may see the same port as free before either has claimed it.
The port-range fallback proposed here would fix this properly by doing the selection atomically at bind time on the server side.
Closing this as not pursuing upstream in the current form.
Reference implementations in my fork:
- remote port fallback: https://github.com/foxytanuki/plannotator/pull/3
- OpenCode URL notification workaround: https://github.com/foxytanuki/plannotator/pull/2
What the remote port fallback PR does:
- in remote mode, fall back across
19432-19439instead of failing immediately on19432 - preserve exact-port behavior when
PLANNOTATOR_PORTis set - fix Bun-side
EADDRINUSEdetection so the fallback actually works
Why I’m not upstreaming it as-is:
- the backend behavior is better, but the UX is still incomplete
- the missing piece is surfacing which port was actually chosen
- that is especially problematic in Claude Code, where the
PermissionRequest/ExitPlanModesuccess path does not provide a clean user-visible channel for a customPlannotator is ready at http://...message
The OpenCode PR shows one possible workaround, but it relies on session.prompt() rather than a proper status/tool UI surface, so it comes with tradeoffs like queued delivery and session noise.
So at this point:
- the backend fallback itself works
- but the chosen-port notification story is still inconsistent across integrations
Because of that, I think this is better kept as a fork/reference implementation rather than merged upstream in its current form.