Eliminate TOCTOU race in terminal server port auto-detection
Context
PR #113 introduced auto-port detection for terminal WebSocket servers. When no explicit port is configured, buildDashboardEnv probes for a free port pair starting at 14800, then passes the ports as env vars to the dashboard process.
Problem
There's a small TOCTOU (Time Of Check To Time Of Use) race: we probe the port (bind + release), then the terminal server binds to it ~2 seconds later. In that window, another process could theoretically grab the port.
Practical risk: extremely low. The 14800 range has zero known consumers (zero IANA registrations, no dev tools). This would only matter in heavily loaded CI environments with many parallel processes.
Potential solutions
- Next.js WebSocket proxy — Route all terminal WebSocket traffic through the dashboard port (e.g.,
/ws/terminal). Eliminates separate terminal ports entirely. Cleanest long-term but most work. - Port discovery via file — Terminal servers write their actual bound port to a known file. Add an
/api/terminal-portsendpoint that reads it. Client fetches real ports before connecting.
Priority
Low — the current probe approach works 99.99% of the time. Only revisit if someone actually hits this in practice.
Closing this as part of the migration cleanup for the ground-up rewrite of Agent Orchestrator.
This item was opened more than a month before the migration batch and the codebase has been replaced, so this thread no longer maps cleanly onto the current source tree.
If something here still needs tracking, please reopen this item or open a fresh one against the new tree. Thank you for contributing.