(args: string[], repoRoot: string)
| 79 | waitForPort, |
| 80 | restartServiceInTmux, |
| 81 | buildStartCommand, |
| 82 | snapshotCloudAgentPublicTunnelEnv, |
| 83 | waitForCloudAgentPublicTunnelCapture, |
| 84 | } from './runner'; |
| 85 | import { shellQuote } from './shell'; |
| 86 | |
| 87 | const sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms)); |
| 88 | |
| 89 | type PortScanOptions = { |
| 90 | portProbe?: typeof probePort; |
| 91 | dockerProbe?: typeof probeDockerApi; |
| 92 | /** Compose project whose published infra ports are this stack's own. */ |
| 93 | ownProject?: string; |
| 94 | /** |
| 95 | * One `docker ps` snapshot, shared across candidate offsets. `undefined` — |
| 96 | * docker did not answer — skips the infra scan entirely. |
| 97 | */ |
| 98 | portOwners?: PortOwner[]; |
| 99 | }; |
| 100 | |
| 101 | type PortScan = { |
| 102 | conflicts: string[]; |
| 103 | reusedHostServices: Set<string>; |
| 104 | /** Subset of `conflicts` on infra ports — moving off those loses a database. */ |
| 105 | infraConflicts?: string[]; |
| 106 | foreignInfraOwners?: PortOwner[]; |
| 107 | }; |
| 108 | |
| 109 | // Scan the resolved services' full computed port set for occupants. The |
| 110 | // shared kiloclaw-docker-tcp bridge on 23750 is reused (not a conflict) when |
| 111 | // the listener really is the Docker API. |
| 112 | async function findPortConflicts( |
| 113 | serviceNames: string[], |
| 114 | options: PortScanOptions = {} |
| 115 | ): Promise<PortScan> { |
| 116 | const portProbe = options.portProbe ?? probePort; |
| 117 | const dockerProbe = options.dockerProbe ?? probeDockerApi; |
| 118 | const conflicts: string[] = []; |
| 119 | const infraConflicts: string[] = []; |
| 120 | const foreignInfraOwners: PortOwner[] = []; |
| 121 | const reusedHostServices = new Set<string>(); |
| 122 | for (const name of serviceNames) { |
| 123 | const service = getService(name); |
| 124 | if (service.port <= 0) continue; |
| 125 | // Infra ports used to be skipped here. An offset whose app ports are all |
| 126 | // free can still have another compose project sitting on its postgres, and |
| 127 | // `docker compose up` then fails with a bare "port is already allocated" — |
| 128 | // which is how an auto-selected offset ends up unusable. A container of |
| 129 | // *this* worktree's own project is not a conflict: it is the database the |
| 130 | // stack is meant to use. |
| 131 | if (service.type === 'infra') { |
| 132 | // Without a container listing there is no way to tell this worktree's own |
| 133 | // postgres from a squatter, and convicting every occupied infra port |
| 134 | // would refuse starts that a slow `docker ps` alone should never block. |
| 135 | if (options.portOwners === undefined) continue; |
| 136 | if (!(await portProbe(service.port))) continue; |
| 137 | const owner = options.portOwners.find(entry => entry.port === service.port); |
| 138 | if (owner !== undefined && owner.project === options.ownProject) continue; |
no test coverage detected