@loucompanion/forge-bridge 0.1.3-dev.d46a8e73d03d → 0.1.3-dev.e7b62d8fdd33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/README.md +83 -0
  2. package/dist/bridge/bridge.base.d.ts +4 -1
  3. package/dist/bridge/bridge.service.d.ts +55 -3
  4. package/dist/bridge/bridge.service.js +834 -87
  5. package/dist/bridge/bridge.types.d.ts +89 -1
  6. package/dist/bridge/runner-host-capabilities.d.ts +15 -0
  7. package/dist/bridge/runner-host-capabilities.js +93 -0
  8. package/dist/cleanup/cleanup.service.d.ts +14 -2
  9. package/dist/cleanup/cleanup.service.js +496 -3
  10. package/dist/cleanup/cleanup.types.d.ts +10 -0
  11. package/dist/cleanup/index.d.ts +1 -1
  12. package/dist/cleanup/index.js +1 -1
  13. package/dist/cleanup/internals.d.ts +2 -2
  14. package/dist/cleanup/internals.js +1 -1
  15. package/dist/cli/cli.help.js +37 -3
  16. package/dist/cli/cli.service.d.ts +33 -2
  17. package/dist/cli/cli.service.js +209 -27
  18. package/dist/cli/local-config.d.ts +5 -0
  19. package/dist/cli/local-config.js +45 -1
  20. package/dist/runtime/aspnet-runtime-prebuild.d.ts +36 -0
  21. package/dist/runtime/aspnet-runtime-prebuild.js +340 -0
  22. package/dist/runtime/attempt-node-runtime-launcher.d.ts +17 -0
  23. package/dist/runtime/attempt-node-runtime-launcher.js +105 -0
  24. package/dist/runtime/attempt-process-supervisor-registry.d.ts +20 -0
  25. package/dist/runtime/attempt-process-supervisor-registry.js +64 -0
  26. package/dist/runtime/attempt-process-supervisor.d.ts +97 -0
  27. package/dist/runtime/attempt-process-supervisor.js +386 -0
  28. package/dist/runtime/docker-engine-probe.d.ts +83 -0
  29. package/dist/runtime/docker-engine-probe.js +300 -0
  30. package/dist/runtime/generated-output-reset.d.ts +10 -0
  31. package/dist/runtime/generated-output-reset.js +103 -0
  32. package/dist/runtime/host-listener-inspector.d.ts +46 -0
  33. package/dist/runtime/host-listener-inspector.js +349 -0
  34. package/dist/runtime/host-port-broker.d.ts +76 -0
  35. package/dist/runtime/host-port-broker.js +333 -0
  36. package/dist/runtime/host-runtime-contract.d.ts +47 -0
  37. package/dist/runtime/host-runtime-contract.js +700 -0
  38. package/dist/runtime/host-runtime-lease.d.ts +197 -0
  39. package/dist/runtime/host-runtime-lease.js +2203 -0
  40. package/dist/runtime/host-runtime-promotion.d.ts +23 -0
  41. package/dist/runtime/host-runtime-promotion.js +159 -0
  42. package/dist/runtime/memory-capacity.d.ts +68 -0
  43. package/dist/runtime/memory-capacity.js +121 -0
  44. package/dist/runtime/node-host-runtime-adapter.d.ts +54 -0
  45. package/dist/runtime/node-host-runtime-adapter.js +284 -0
  46. package/dist/runtime/playwright-host-runtime-adapter.d.ts +21 -0
  47. package/dist/runtime/playwright-host-runtime-adapter.js +231 -0
  48. package/dist/runtime/runtime-isolation-lease.d.ts +67 -0
  49. package/dist/runtime/runtime-isolation-lease.js +158 -0
  50. package/dist/runtime/workspace-resource-estimator.d.ts +17 -0
  51. package/dist/runtime/workspace-resource-estimator.js +174 -0
  52. package/dist/sandbox/preflight.d.ts +45 -1
  53. package/dist/sandbox/preflight.js +299 -34
  54. package/dist/sandbox/process.js +125 -40
  55. package/dist/sandbox/repository-lock.js +41 -3
  56. package/dist/sandbox/runtime.d.ts +66 -3
  57. package/dist/sandbox/runtime.js +670 -105
  58. package/dist/sandbox/sandbox.types.d.ts +10 -0
  59. package/dist/sandbox/smoke.d.ts +76 -0
  60. package/dist/sandbox/smoke.js +380 -0
  61. package/dist/sandbox/worker-supervisor.js +1 -1
  62. package/dist/sandbox/worker.js +2 -5
  63. package/dist/sandbox/workspace.service.d.ts +1 -0
  64. package/dist/sandbox/workspace.service.js +28 -5
  65. package/dist/session/managed-attempt-tools.d.ts +21 -0
  66. package/dist/session/managed-attempt-tools.js +912 -0
  67. package/dist/session/managed-build-coordinator.d.ts +5 -0
  68. package/dist/session/managed-build-coordinator.js +105 -0
  69. package/dist/session/managed-build-receipt.d.ts +21 -0
  70. package/dist/session/managed-build-receipt.js +335 -0
  71. package/dist/session/phase-completion.artifacts.d.ts +17 -0
  72. package/dist/session/phase-completion.artifacts.js +233 -24
  73. package/dist/session/phase-completion.d.ts +12 -2
  74. package/dist/session/phase-completion.helpers.d.ts +5 -0
  75. package/dist/session/phase-completion.helpers.js +51 -10
  76. package/dist/session/phase-completion.js +14 -3
  77. package/dist/session/phase-completion.prompts.d.ts +3 -3
  78. package/dist/session/phase-completion.prompts.js +24 -8
  79. package/dist/session/phase-completion.runtime.d.ts +13 -1
  80. package/dist/session/phase-completion.runtime.js +189 -8
  81. package/dist/session/phase-completion.schemas.js +117 -4
  82. package/dist/session/phase-completion.validation.d.ts +4 -1
  83. package/dist/session/phase-completion.validation.js +153 -22
  84. package/dist/session/pre-publication-validation.d.ts +46 -0
  85. package/dist/session/pre-publication-validation.js +208 -0
  86. package/dist/session/provider-cli/ci-repair-readiness.d.ts +16 -0
  87. package/dist/session/provider-cli/ci-repair-readiness.js +110 -0
  88. package/dist/session/provider-cli/provider-cli.artifacts.d.ts +3 -2
  89. package/dist/session/provider-cli/provider-cli.artifacts.js +65 -7
  90. package/dist/session/provider-cli/provider-cli.deterministic.js +49 -1
  91. package/dist/session/provider-cli/provider-cli.process.d.ts +25 -0
  92. package/dist/session/provider-cli/provider-cli.process.js +463 -34
  93. package/dist/session/provider-cli/provider-cli.prompts.d.ts +4 -1
  94. package/dist/session/provider-cli/provider-cli.prompts.js +108 -25
  95. package/dist/session/provider-cli/provider-cli.qa-sidecars.js +1 -0
  96. package/dist/session/provider-cli/provider-cli.repair-epoch.d.ts +6 -0
  97. package/dist/session/provider-cli/provider-cli.repair-epoch.js +11 -0
  98. package/dist/session/provider-cli/provider-cli.runtime.d.ts +1 -1
  99. package/dist/session/provider-cli/provider-cli.runtime.js +6 -3
  100. package/dist/session/provider-cli/provider-cli.service.d.ts +22 -2
  101. package/dist/session/provider-cli/provider-cli.service.js +524 -100
  102. package/dist/session/provider-cli/provider-cli.types.d.ts +5 -0
  103. package/dist/session/provider-cli/provider-output-state.d.ts +2 -0
  104. package/dist/session/provider-cli/provider-output-state.js +91 -16
  105. package/dist/session/provider-cli/provider-usage.d.ts +7 -0
  106. package/dist/session/provider-cli/provider-usage.js +28 -5
  107. package/dist/session/session-redaction.d.ts +3 -0
  108. package/dist/session/session-redaction.js +38 -0
  109. package/dist/session/session.base.d.ts +6 -1
  110. package/dist/session/session.service.d.ts +8 -2
  111. package/dist/session/session.service.js +522 -41
  112. package/dist/session/session.types.d.ts +72 -0
  113. package/dist/session/session.types.js +74 -3
  114. package/dist/shared/format/format-bytes.d.ts +1 -0
  115. package/dist/shared/format/format-bytes.js +9 -0
  116. package/dist/shared/path/path.d.ts +1 -0
  117. package/dist/shared/path/path.js +21 -1
  118. package/dist/shared/process/dotnet-toolchain.d.ts +14 -0
  119. package/dist/shared/process/dotnet-toolchain.js +179 -0
  120. package/dist/shared/process/host-process-recovery.d.ts +12 -0
  121. package/dist/shared/process/host-process-recovery.js +78 -0
  122. package/dist/shared/process/host-process-supervisor.d.ts +59 -0
  123. package/dist/shared/process/host-process-supervisor.js +330 -0
  124. package/dist/shared/process/process.d.ts +1 -0
  125. package/dist/shared/process/process.js +1 -0
  126. package/dist/shared/redaction/redaction.js +12 -2
  127. package/dist/version.d.ts +1 -1
  128. package/dist/version.js +1 -1
  129. package/dist/worktree/artifact-injector.d.ts +3 -2
  130. package/dist/worktree/artifact-injector.js +43 -4
  131. package/dist/worktree/dependency-warmer.d.ts +28 -1
  132. package/dist/worktree/dependency-warmer.js +308 -9
  133. package/dist/worktree/index.d.ts +2 -1
  134. package/dist/worktree/index.js +1 -0
  135. package/dist/worktree/prior-phase-artifact-hydrator.d.ts +2 -0
  136. package/dist/worktree/prior-phase-artifact-hydrator.js +136 -0
  137. package/dist/worktree/worktree-paths.d.ts +4 -0
  138. package/dist/worktree/worktree-paths.js +30 -0
  139. package/dist/worktree/worktree.base.d.ts +3 -0
  140. package/dist/worktree/worktree.service.d.ts +13 -2
  141. package/dist/worktree/worktree.service.js +238 -47
  142. package/dist/worktree/worktree.types.d.ts +5 -1
  143. package/package.json +5 -3
  144. package/runtime/windows-job-supervisor.ps1 +444 -0
  145. package/runtime/windows-listener-owner.ps1 +33 -0
@@ -0,0 +1,349 @@
1
+ import { execFile } from "node:child_process";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ const windowsListenerOwnerPath = fileURLToPath(new URL("../../runtime/windows-listener-owner.ps1", import.meta.url));
6
+ /** Proves that a loopback listener belongs to one exact supervised child. */
7
+ export class HostListenerInspector {
8
+ commands;
9
+ platform;
10
+ constructor(commands = new ExecFileInspectionCommandRunner(), platform = process.platform) {
11
+ this.commands = commands;
12
+ this.platform = platform;
13
+ }
14
+ async inspect(child, reservation, owner) {
15
+ if (reservation.hostIp !== "127.0.0.1" || reservation.protocol !== "tcp") {
16
+ throw new Error("host_listener_inspection_invalid: only TCP IPv4 loopback listeners are supported.");
17
+ }
18
+ const metadata = await loadMetadata(child.metadataPath);
19
+ if (!metadata)
20
+ return "unbound";
21
+ if (!matchesOwner(metadata, child, owner))
22
+ return "foreign";
23
+ if (this.platform === "win32") {
24
+ return await this.inspectWindows(metadata.rootPid, reservation.hostPort);
25
+ }
26
+ return await this.inspectUnix(metadata.rootPid, reservation.hostPort);
27
+ }
28
+ /** Returns loopback TCP ports opened by one exact supervised child process tree. */
29
+ async inspectOwnedLoopbackListeners(child, owner) {
30
+ const metadata = await loadMetadata(child.metadataPath);
31
+ if (!metadata || !matchesOwner(metadata, child, owner)) {
32
+ throw new Error("host_listener_inspection_invalid: supervised provider ownership metadata is unavailable.");
33
+ }
34
+ return this.platform === "win32"
35
+ ? await this.inspectAllWindows(metadata.rootPid)
36
+ : await this.inspectAllUnix(metadata.rootPid);
37
+ }
38
+ /** Snapshots loopback TCP ports that predate a supervised child launch. */
39
+ async inspectLoopbackListeners() {
40
+ if (this.platform === "win32") {
41
+ const netstat = await this.commands.run("netstat.exe", ["-ano", "-p", "TCP"]);
42
+ if (netstat.exitCode !== 0)
43
+ throw commandError("netstat", netstat);
44
+ return uniqueSorted(parseWindowsLoopbackListeners(netstat.stdout).map(listener => listener.port));
45
+ }
46
+ const lsof = await this.commands.run("lsof", [
47
+ "-nP",
48
+ "-a",
49
+ "-iTCP@127.0.0.1",
50
+ "-sTCP:LISTEN",
51
+ "-Fpn",
52
+ ]);
53
+ if (lsof.exitCode === 0 || lsof.exitCode === 1) {
54
+ return uniqueSorted(parseLsofLoopbackListeners(lsof.stdout).map(listener => listener.port));
55
+ }
56
+ if (lsof.errorCode !== "ENOENT")
57
+ throw commandError("lsof", lsof);
58
+ const ss = await this.commands.run("ss", ["-H", "-ltnp"]);
59
+ if (ss.exitCode !== 0)
60
+ throw commandError("ss", ss);
61
+ return uniqueSorted(parseSsLoopbackListeners(ss.stdout).map(listener => listener.port));
62
+ }
63
+ async inspectWindows(rootPid, port) {
64
+ const netstat = await this.commands.run("netstat.exe", ["-ano", "-p", "TCP"]);
65
+ if (netstat.exitCode !== 0) {
66
+ throw commandError("netstat", netstat);
67
+ }
68
+ const listenerPids = parseWindowsListenerPids(netstat.stdout, port);
69
+ if (listenerPids.length === 0)
70
+ return "unbound";
71
+ const powershell = windowsPowerShellPath();
72
+ const ownership = await this.commands.run(powershell, [
73
+ "-NoLogo",
74
+ "-NoProfile",
75
+ "-NonInteractive",
76
+ "-ExecutionPolicy",
77
+ "Bypass",
78
+ "-File",
79
+ windowsListenerOwnerPath,
80
+ "-RootProcessId",
81
+ String(rootPid),
82
+ "-ListenerProcessIds",
83
+ listenerPids.join(","),
84
+ ]);
85
+ if (ownership.exitCode !== 0)
86
+ throw commandError("Windows process ancestry", ownership);
87
+ let parsed;
88
+ try {
89
+ parsed = JSON.parse(ownership.stdout.trim());
90
+ }
91
+ catch (error) {
92
+ throw new Error("host_listener_inspection_failed: Windows ancestry output was invalid.", { cause: error });
93
+ }
94
+ const values = Array.isArray(parsed) ? parsed : [parsed];
95
+ return values.length === listenerPids.length && values.every(value => value === true) ? "owned" : "foreign";
96
+ }
97
+ async inspectAllWindows(rootPid) {
98
+ const netstat = await this.commands.run("netstat.exe", ["-ano", "-p", "TCP"]);
99
+ if (netstat.exitCode !== 0)
100
+ throw commandError("netstat", netstat);
101
+ const listeners = parseWindowsLoopbackListeners(netstat.stdout);
102
+ if (listeners.length === 0)
103
+ return [];
104
+ const pids = [...new Set(listeners.map(listener => listener.pid))];
105
+ const ownership = await this.commands.run(windowsPowerShellPath(), [
106
+ "-NoLogo",
107
+ "-NoProfile",
108
+ "-NonInteractive",
109
+ "-ExecutionPolicy",
110
+ "Bypass",
111
+ "-File",
112
+ windowsListenerOwnerPath,
113
+ "-RootProcessId",
114
+ String(rootPid),
115
+ "-ListenerProcessIds",
116
+ pids.join(","),
117
+ ]);
118
+ if (ownership.exitCode !== 0)
119
+ throw commandError("Windows process ancestry", ownership);
120
+ let parsed;
121
+ try {
122
+ parsed = JSON.parse(ownership.stdout.trim());
123
+ }
124
+ catch (error) {
125
+ throw new Error("host_listener_inspection_failed: Windows ancestry output was invalid.", { cause: error });
126
+ }
127
+ const values = Array.isArray(parsed) ? parsed : [parsed];
128
+ if (values.length !== pids.length || values.some(value => typeof value !== "boolean")) {
129
+ throw new Error("host_listener_inspection_failed: Windows ancestry output was incomplete.");
130
+ }
131
+ const ownedPids = new Set(pids.filter((_pid, index) => values[index] === true));
132
+ return uniqueSorted(listeners.filter(listener => ownedPids.has(listener.pid)).map(listener => listener.port));
133
+ }
134
+ async inspectUnix(rootPid, port) {
135
+ let listenerPids = null;
136
+ const lsof = await this.commands.run("lsof", [
137
+ "-nP",
138
+ "-a",
139
+ `-iTCP@127.0.0.1:${port}`,
140
+ "-sTCP:LISTEN",
141
+ "-Fp",
142
+ ]);
143
+ if (lsof.exitCode === 0 || lsof.exitCode === 1) {
144
+ listenerPids = parseLsofPids(lsof.stdout);
145
+ }
146
+ else if (lsof.errorCode !== "ENOENT") {
147
+ throw commandError("lsof", lsof);
148
+ }
149
+ if (listenerPids === null) {
150
+ const ss = await this.commands.run("ss", ["-H", "-ltnp", "sport", "=", `:${port}`]);
151
+ if (ss.exitCode !== 0)
152
+ throw commandError("ss", ss);
153
+ listenerPids = parseSsPids(ss.stdout, port);
154
+ }
155
+ if (listenerPids.length === 0)
156
+ return "unbound";
157
+ const processGroups = await this.commands.run("ps", [
158
+ "-o",
159
+ "pid=,pgid=",
160
+ "-p",
161
+ listenerPids.join(","),
162
+ ]);
163
+ if (processGroups.exitCode !== 0)
164
+ throw commandError("ps", processGroups);
165
+ const groups = parseProcessGroups(processGroups.stdout);
166
+ return listenerPids.every(pid => groups.get(pid) === rootPid) ? "owned" : "foreign";
167
+ }
168
+ async inspectAllUnix(rootPid) {
169
+ const lsof = await this.commands.run("lsof", [
170
+ "-nP",
171
+ "-a",
172
+ "-iTCP@127.0.0.1",
173
+ "-sTCP:LISTEN",
174
+ "-Fpn",
175
+ ]);
176
+ let listeners;
177
+ if (lsof.exitCode === 0 || lsof.exitCode === 1) {
178
+ listeners = parseLsofLoopbackListeners(lsof.stdout);
179
+ }
180
+ else if (lsof.errorCode === "ENOENT") {
181
+ const ss = await this.commands.run("ss", ["-H", "-ltnp"]);
182
+ if (ss.exitCode !== 0)
183
+ throw commandError("ss", ss);
184
+ listeners = parseSsLoopbackListeners(ss.stdout);
185
+ }
186
+ else {
187
+ throw commandError("lsof", lsof);
188
+ }
189
+ if (listeners.length === 0)
190
+ return [];
191
+ const pids = [...new Set(listeners.map(listener => listener.pid))];
192
+ const processGroups = await this.commands.run("ps", ["-o", "pid=,pgid=", "-p", pids.join(",")]);
193
+ if (processGroups.exitCode !== 0)
194
+ throw commandError("ps", processGroups);
195
+ const groups = parseProcessGroups(processGroups.stdout);
196
+ return uniqueSorted(listeners
197
+ .filter(listener => groups.get(listener.pid) === rootPid)
198
+ .map(listener => listener.port));
199
+ }
200
+ }
201
+ export function parseWindowsLoopbackListeners(output) {
202
+ return uniqueListeners(output.split(/\r?\n/u).flatMap(line => {
203
+ const fields = line.trim().split(/\s+/u);
204
+ if (fields.length < 5 || fields[0]?.toUpperCase() !== "TCP" || fields[3]?.toUpperCase() !== "LISTENING")
205
+ return [];
206
+ const endpoint = parseIpv4LoopbackEndpoint(fields[1]);
207
+ const pids = validPid(fields[4]);
208
+ return endpoint && pids.length === 1 ? [{ pid: pids[0], port: endpoint }] : [];
209
+ }));
210
+ }
211
+ export function parseLsofLoopbackListeners(output) {
212
+ const listeners = [];
213
+ let pid = null;
214
+ for (const line of output.split(/\r?\n/u)) {
215
+ if (line.startsWith("p"))
216
+ pid = validPid(line.slice(1))[0] ?? null;
217
+ if (!line.startsWith("n") || pid === null)
218
+ continue;
219
+ const port = parseIpv4LoopbackEndpoint(line.slice(1));
220
+ if (port !== null)
221
+ listeners.push({ pid, port });
222
+ }
223
+ return uniqueListeners(listeners);
224
+ }
225
+ export function parseSsLoopbackListeners(output) {
226
+ return uniqueListeners(output.split(/\r?\n/u).flatMap(line => {
227
+ const endpoint = line.match(/127\.0\.0\.1:(\d+)/u)?.[1];
228
+ const port = endpoint ? Number.parseInt(endpoint, 10) : 0;
229
+ if (!Number.isSafeInteger(port) || port < 1 || port > 65535)
230
+ return [];
231
+ return [...line.matchAll(/pid=(\d+)/gu)].flatMap(match => {
232
+ const pid = validPid(match[1])[0];
233
+ return pid ? [{ pid, port }] : [];
234
+ });
235
+ }));
236
+ }
237
+ export class ExecFileInspectionCommandRunner {
238
+ run(command, args) {
239
+ return new Promise(resolve => {
240
+ const executable = process.platform === "win32" && command.toLowerCase() === "netstat.exe"
241
+ ? windowsSystemExecutable("netstat.exe")
242
+ : command;
243
+ execFile(executable, args, {
244
+ windowsHide: true,
245
+ timeout: 15_000,
246
+ maxBuffer: 4 * 1024 * 1024,
247
+ }, (error, stdout, stderr) => {
248
+ const processError = error;
249
+ resolve({
250
+ exitCode: typeof processError?.code === "number" ? processError.code : error ? 1 : 0,
251
+ stdout: String(stdout),
252
+ stderr: String(stderr),
253
+ ...(typeof processError?.code === "string" ? { errorCode: processError.code } : {}),
254
+ });
255
+ });
256
+ });
257
+ }
258
+ }
259
+ export function parseWindowsListenerPids(output, port) {
260
+ const suffix = `:${port}`;
261
+ const pids = output.split(/\r?\n/u).flatMap(line => {
262
+ const fields = line.trim().split(/\s+/u);
263
+ if (fields.length < 5 || fields[0]?.toUpperCase() !== "TCP" || fields[3]?.toUpperCase() !== "LISTENING")
264
+ return [];
265
+ if (fields[1] !== `127.0.0.1${suffix}`)
266
+ return [];
267
+ return validPid(fields[4]);
268
+ });
269
+ return uniqueSorted(pids);
270
+ }
271
+ export function parseLsofPids(output) {
272
+ return uniqueSorted(output.split(/\r?\n/u).flatMap(line => line.startsWith("p") ? validPid(line.slice(1)) : []));
273
+ }
274
+ export function parseSsPids(output, port) {
275
+ return uniqueSorted(output.split(/\r?\n/u).flatMap(line => {
276
+ if (!line.includes(`127.0.0.1:${port}`))
277
+ return [];
278
+ return [...line.matchAll(/pid=(\d+)/gu)].flatMap(match => validPid(match[1]));
279
+ }));
280
+ }
281
+ export function parseProcessGroups(output) {
282
+ const groups = new Map();
283
+ for (const line of output.split(/\r?\n/u)) {
284
+ const fields = line.trim().split(/\s+/u);
285
+ const pid = validPid(fields[0]);
286
+ const group = validPid(fields[1]);
287
+ if (pid.length === 1 && group.length === 1)
288
+ groups.set(pid[0], group[0]);
289
+ }
290
+ return groups;
291
+ }
292
+ async function loadMetadata(metadataPath) {
293
+ try {
294
+ const parsed = JSON.parse(await fs.readFile(metadataPath, "utf8"));
295
+ if (parsed.schemaVersion !== 1 || !Number.isSafeInteger(parsed.rootPid) || parsed.rootPid <= 0) {
296
+ throw new Error("host_listener_inspection_invalid: process supervisor metadata is invalid.");
297
+ }
298
+ return parsed;
299
+ }
300
+ catch (error) {
301
+ if (error.code === "ENOENT")
302
+ return null;
303
+ throw error;
304
+ }
305
+ }
306
+ function matchesOwner(metadata, child, owner) {
307
+ return metadata.supervisorId === child.processSupervisorId
308
+ && metadata.runtimeLeaseId === owner.executionLeaseId
309
+ && metadata.dispatchAttemptId === owner.dispatchAttemptId
310
+ && metadata.fencingToken === owner.fencingToken;
311
+ }
312
+ function validPid(value) {
313
+ if (!value || !/^\d+$/u.test(value))
314
+ return [];
315
+ const pid = Number.parseInt(value, 10);
316
+ return Number.isSafeInteger(pid) && pid > 0 ? [pid] : [];
317
+ }
318
+ function uniqueSorted(values) {
319
+ return [...new Set(values)].sort((left, right) => left - right);
320
+ }
321
+ function parseIpv4LoopbackEndpoint(value) {
322
+ const match = value?.match(/^127\.0\.0\.1:(\d+)$/u);
323
+ if (!match)
324
+ return null;
325
+ const port = Number.parseInt(match[1], 10);
326
+ return Number.isSafeInteger(port) && port >= 1 && port <= 65535 ? port : null;
327
+ }
328
+ function uniqueListeners(values) {
329
+ const seen = new Set();
330
+ return values.filter(value => {
331
+ const key = `${value.pid}:${value.port}`;
332
+ if (seen.has(key))
333
+ return false;
334
+ seen.add(key);
335
+ return true;
336
+ }).sort((left, right) => left.port - right.port || left.pid - right.pid);
337
+ }
338
+ function commandError(label, result) {
339
+ const detail = result.stderr.replaceAll(/[\r\n\t]+/gu, " ").trim().slice(0, 512);
340
+ const code = result.errorCode ? ` (${result.errorCode})` : "";
341
+ return new Error(`host_listener_inspection_failed: ${label} exited ${result.exitCode}${code}${detail ? `: ${detail}` : ""}.`);
342
+ }
343
+ function windowsPowerShellPath() {
344
+ return path.win32.join(path.dirname(windowsSystemExecutable("netstat.exe")), "WindowsPowerShell", "v1.0", "powershell.exe");
345
+ }
346
+ function windowsSystemExecutable(name) {
347
+ const systemRoot = process.env.SystemRoot?.trim() || "C:\\Windows";
348
+ return path.win32.join(systemRoot, "System32", name);
349
+ }
@@ -0,0 +1,76 @@
1
+ import type { RuntimeIsolationPort } from "./runtime-isolation-lease.js";
2
+ export interface HostPortLeaseOwner {
3
+ readonly dispatchAttemptId: string;
4
+ readonly executionLeaseId: string;
5
+ readonly fencingToken: string;
6
+ readonly processSupervisorId: string;
7
+ }
8
+ export interface HostPortRequest {
9
+ readonly name: string;
10
+ readonly protocol?: "tcp";
11
+ readonly targetPort?: number;
12
+ }
13
+ export type HostPortReservationState = "reserved" | "handed_off" | "active";
14
+ export interface HostPortReservation extends RuntimeIsolationPort, HostPortLeaseOwner {
15
+ readonly schemaVersion: 1;
16
+ readonly reservationId: string;
17
+ readonly state: HostPortReservationState;
18
+ readonly reservedAt: string;
19
+ readonly handedOffAt: string | null;
20
+ readonly activatedAt: string | null;
21
+ }
22
+ export interface HostPortLaunchLifecycle<T> {
23
+ readonly launch: (port: HostPortReservation, signal?: AbortSignal) => Promise<T>;
24
+ readonly verifyOwnership: (value: T, port: HostPortReservation, signal?: AbortSignal) => Promise<boolean>;
25
+ readonly stop: (value: T) => Promise<void>;
26
+ }
27
+ export interface HostPortLaunch<T> {
28
+ readonly reservation: HostPortReservation;
29
+ readonly value: T;
30
+ }
31
+ export interface HostPortBrokerOptions {
32
+ readonly hostIp?: "127.0.0.1";
33
+ readonly minimumPort?: number;
34
+ readonly maximumPort?: number;
35
+ readonly maximumLaunchAttempts?: number;
36
+ }
37
+ export interface HostPortBrokerDiagnostics {
38
+ readonly hostIp: "127.0.0.1";
39
+ readonly pool: {
40
+ readonly minimumPort: number;
41
+ readonly maximumPort: number;
42
+ } | null;
43
+ readonly reservations: readonly Omit<HostPortReservation, "fencingToken">[];
44
+ }
45
+ export declare class HostPortCollisionError extends Error {
46
+ constructor(message?: string);
47
+ }
48
+ /**
49
+ * Owns runner-local loopback ports for supervised host attempts.
50
+ *
51
+ * A reservation keeps a real listening socket open until an adapter is ready to
52
+ * launch. The adapter then performs a narrow handoff and must prove that the
53
+ * supervised child owns the assigned endpoint before the reservation is active.
54
+ */
55
+ export declare class HostPortBroker {
56
+ private readonly hostIp;
57
+ private readonly minimumPort;
58
+ private readonly maximumPort;
59
+ private readonly maximumLaunchAttempts;
60
+ private readonly reservations;
61
+ private readonly claimedPorts;
62
+ private nextPort;
63
+ private launchGate;
64
+ constructor(options?: HostPortBrokerOptions);
65
+ reserve(owner: HostPortLeaseOwner, request: HostPortRequest, signal?: AbortSignal): Promise<HostPortReservation>;
66
+ handoff(reservationId: string, owner: HostPortLeaseOwner): Promise<HostPortReservation | null>;
67
+ activate(reservationId: string, owner: HostPortLeaseOwner): HostPortReservation | null;
68
+ release(reservationId: string, owner: HostPortLeaseOwner): Promise<boolean>;
69
+ snapshot(): readonly HostPortReservation[];
70
+ diagnostics(): HostPortBrokerDiagnostics;
71
+ launchWithRetry<T>(owner: HostPortLeaseOwner, request: HostPortRequest, lifecycle: HostPortLaunchLifecycle<T>, signal?: AbortSignal): Promise<HostPortLaunch<T>>;
72
+ private reserveExcluding;
73
+ private listen;
74
+ private owned;
75
+ private withLaunchGate;
76
+ }