MCPcopy Create free account
hub / github.com/Kilo-Org/cloud / cmdUp

Function cmdUp

dev/local/cli.ts:81–472  ·  view source on GitHub ↗
(args: string[], repoRoot: string)

Source from the content-addressed store, hash-verified

79 waitForPort,
80 restartServiceInTmux,
81 buildStartCommand,
82 snapshotCloudAgentPublicTunnelEnv,
83 waitForCloudAgentPublicTunnelCapture,
84} from './runner';
85import { shellQuote } from './shell';
86
87const sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms));
88
89type 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
101type 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.
112async 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;

Callers 1

mainFunction · 0.85

Calls 15

isTmuxAvailableFunction · 0.90
findOtherKiloDevSessionsFunction · 0.90
getSessionNameFunction · 0.90
sessionExistsFunction · 0.90
attachSessionFunction · 0.90
resolveGroupsFunction · 0.90
getAlwaysOnGroupIdsFunction · 0.90
resolveTargetsFunction · 0.90
topologicalSortFunction · 0.90
readEnvValueFunction · 0.90
getServiceFunction · 0.90
startInfraFunction · 0.90

Tested by

no test coverage detected