()
| 877 | |
| 878 | // Find port: explicit BROWSE_PORT, or random in 10000-60000 |
| 879 | async function findPort(): Promise<number> { |
| 880 | // Explicit port override (for debugging) |
| 881 | if (BROWSE_PORT) { |
| 882 | const result = await checkPortAvailable(BROWSE_PORT); |
| 883 | if (result.available) { |
| 884 | return BROWSE_PORT; |
| 885 | } |
| 886 | throw formatExplicitPortUnavailableError(BROWSE_PORT, result); |
| 887 | } |
| 888 | |
| 889 | // Random port with retry |
| 890 | const attempts: FailedPortAttempt[] = []; |
| 891 | for (let attempt = 0; attempt < RANDOM_PORT_RETRIES; attempt++) { |
| 892 | const port = RANDOM_PORT_MIN + Math.floor(Math.random() * (RANDOM_PORT_MAX - RANDOM_PORT_MIN)); |
| 893 | const result = await checkPortAvailable(port); |
| 894 | if (result.available) { |
| 895 | return port; |
| 896 | } |
| 897 | attempts.push({ port, result }); |
| 898 | } |
| 899 | throw formatRandomPortUnavailableError(attempts); |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * Translate Playwright errors into actionable messages for AI agents. |
no test coverage detected