@loucompanion/forge-bridge 0.2.1-dev.17ad6c965e41 → 0.2.1-dev.c3166221b6e7
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.
- package/dist/runtime/attempt-node-runtime-launcher.d.ts +36 -0
- package/dist/runtime/attempt-node-runtime-launcher.js +160 -0
- package/dist/runtime/host-listener-inspector.d.ts +26 -0
- package/dist/runtime/host-listener-inspector.js +84 -3
- package/dist/runtime/host-runtime-lease.d.ts +25 -2
- package/dist/runtime/host-runtime-lease.js +105 -137
- package/dist/runtime/host-runtime-promotion.d.ts +78 -2
- package/dist/runtime/host-runtime-promotion.js +307 -14
- package/dist/runtime/prepared-runtime-identity.d.ts +4 -0
- package/dist/runtime/prepared-runtime-identity.js +130 -9
- package/dist/runtime/prepared-runtime-readiness.d.ts +13 -1
- package/dist/runtime/prepared-runtime-readiness.js +63 -16
- package/dist/session/session.service.d.ts +4 -0
- package/dist/session/session.service.js +54 -20
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/runtime/windows-listener-owner.ps1 +12 -1
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { ForgeFailureError } from "../session/session.types.js";
|
|
1
2
|
import type { AttemptProcessChildHandle, AttemptProcessSupervisor } from "./attempt-process-supervisor.js";
|
|
2
3
|
import type { HostPortLeaseOwner } from "./host-port-broker.js";
|
|
3
4
|
import { HostListenerInspector } from "./host-listener-inspector.js";
|
|
5
|
+
import type { ProviderProcessHandlers, ProviderProcessRunner, ProviderProcessRunRequest, ProviderProcessRunResult } from "../session/provider-cli/provider-cli.process.js";
|
|
4
6
|
import type { HostListenerOwnership, NodeRuntimeCommandLauncher, NodeRuntimeLaunchRequest, NodeRuntimeReadinessDiagnostics } from "./node-host-runtime-adapter.js";
|
|
7
|
+
export declare function brokeredHostPorts(ports: readonly {
|
|
8
|
+
readonly hostPort: number;
|
|
9
|
+
}[]): ReadonlySet<number>;
|
|
10
|
+
export declare function isolationViolationError(detail: string): ForgeFailureError;
|
|
5
11
|
/** Launches one explicitly configured HTTP project beneath the attempt coordinator. */
|
|
6
12
|
export declare class AttemptNodeRuntimeLauncher implements NodeRuntimeCommandLauncher<AttemptProcessChildHandle> {
|
|
7
13
|
private readonly supervisor;
|
|
@@ -10,9 +16,39 @@ export declare class AttemptNodeRuntimeLauncher implements NodeRuntimeCommandLau
|
|
|
10
16
|
private readonly listenerInspector;
|
|
11
17
|
private readonly exactSecrets;
|
|
12
18
|
private readonly diagnosticsByChild;
|
|
19
|
+
private readonly launchedChildren;
|
|
13
20
|
constructor(supervisor: AttemptProcessSupervisor, owner: HostPortLeaseOwner, logDirectory: string, listenerInspector?: HostListenerInspector, exactSecrets?: readonly string[]);
|
|
14
21
|
launch(request: NodeRuntimeLaunchRequest, signal?: AbortSignal): Promise<AttemptProcessChildHandle>;
|
|
15
22
|
inspectListener(child: AttemptProcessChildHandle, request: NodeRuntimeLaunchRequest): Promise<HostListenerOwnership>;
|
|
23
|
+
/**
|
|
24
|
+
* Every loopback TCP port held by the process trees this launcher started that are still
|
|
25
|
+
* running, brokered or not. A launch the port broker retried leaves an exited child behind;
|
|
26
|
+
* it holds nothing and is skipped.
|
|
27
|
+
*/
|
|
28
|
+
ownedListenerPorts(): Promise<readonly number[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Why these process trees break isolation, or null when they do not: a listener the port
|
|
31
|
+
* broker did not assign, or listeners that could not be inspected (unproven isolation).
|
|
32
|
+
*/
|
|
33
|
+
unbrokeredListenerViolation(brokered: ReadonlySet<number>): Promise<string | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Re-audits these process trees on an interval until stopped, so a listener opened after
|
|
36
|
+
* readiness (say, a fixed admin port bound once a database connects) is caught within one
|
|
37
|
+
* interval. `brokered` returning null skips a tick. The first violation is reported once and
|
|
38
|
+
* ends the monitor. Returns the function that stops it.
|
|
39
|
+
*/
|
|
40
|
+
monitorListeners(brokered: () => ReadonlySet<number> | null, onViolation: (detail: string) => Promise<void>): () => void;
|
|
16
41
|
stop(child: AttemptProcessChildHandle): Promise<void>;
|
|
17
42
|
readinessDiagnostics(child: AttemptProcessChildHandle): Promise<NodeRuntimeReadinessDiagnostics>;
|
|
18
43
|
}
|
|
44
|
+
/** Runs a provider process beneath an attempt supervisor; an analysis-only provider may open no loopback listener. */
|
|
45
|
+
export declare class AttemptSupervisedProviderProcessRunner implements ProviderProcessRunner {
|
|
46
|
+
private readonly supervisor;
|
|
47
|
+
private readonly owner;
|
|
48
|
+
private readonly listenerInspector;
|
|
49
|
+
private readonly enforceNoListeners;
|
|
50
|
+
private readonly onProcessTreeStarted;
|
|
51
|
+
constructor(supervisor: AttemptProcessSupervisor, owner: HostPortLeaseOwner, listenerInspector: HostListenerInspector, enforceNoListeners: boolean, onProcessTreeStarted: () => void);
|
|
52
|
+
run(request: ProviderProcessRunRequest, handlers?: ProviderProcessHandlers, signal?: AbortSignal): Promise<ProviderProcessRunResult>;
|
|
53
|
+
private awaitProviderResult;
|
|
54
|
+
}
|
|
@@ -6,6 +6,12 @@ import { ForgeFailureError, sanitizeFailureError } from "../session/session.type
|
|
|
6
6
|
import { HostListenerInspector } from "./host-listener-inspector.js";
|
|
7
7
|
import { writeSupervisedRuntimeDescriptor } from "./supervised-runtime-build-guard.js";
|
|
8
8
|
const diagnosticTailLength = 4_000;
|
|
9
|
+
export function brokeredHostPorts(ports) {
|
|
10
|
+
return new Set(ports.map(port => port.hostPort));
|
|
11
|
+
}
|
|
12
|
+
export function isolationViolationError(detail) {
|
|
13
|
+
return new ForgeFailureError("host_runtime_isolation_violation", `host_runtime_isolation_violation: ${detail}; the repository's parallel promotion was revoked and its runs are serialized until isolation is proven again.`);
|
|
14
|
+
}
|
|
9
15
|
/** Launches one explicitly configured HTTP project beneath the attempt coordinator. */
|
|
10
16
|
export class AttemptNodeRuntimeLauncher {
|
|
11
17
|
supervisor;
|
|
@@ -14,6 +20,7 @@ export class AttemptNodeRuntimeLauncher {
|
|
|
14
20
|
listenerInspector;
|
|
15
21
|
exactSecrets;
|
|
16
22
|
diagnosticsByChild = new WeakMap();
|
|
23
|
+
launchedChildren = [];
|
|
17
24
|
constructor(supervisor, owner, logDirectory, listenerInspector = new HostListenerInspector(), exactSecrets = []) {
|
|
18
25
|
this.supervisor = supervisor;
|
|
19
26
|
this.owner = owner;
|
|
@@ -63,6 +70,7 @@ export class AttemptNodeRuntimeLauncher {
|
|
|
63
70
|
}
|
|
64
71
|
const exposedChild = sanitizedChildHandle(child, this.exactSecrets);
|
|
65
72
|
this.diagnosticsByChild.set(exposedChild, diagnostics);
|
|
73
|
+
this.launchedChildren.push(exposedChild);
|
|
66
74
|
void exposedChild.result.then(result => {
|
|
67
75
|
diagnostics.exited = true;
|
|
68
76
|
diagnostics.exitCode = result.exitCode;
|
|
@@ -79,6 +87,65 @@ export class AttemptNodeRuntimeLauncher {
|
|
|
79
87
|
async inspectListener(child, request) {
|
|
80
88
|
return await this.listenerInspector.inspect(child, request.port, this.owner);
|
|
81
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Every loopback TCP port held by the process trees this launcher started that are still
|
|
92
|
+
* running, brokered or not. A launch the port broker retried leaves an exited child behind;
|
|
93
|
+
* it holds nothing and is skipped.
|
|
94
|
+
*/
|
|
95
|
+
async ownedListenerPorts() {
|
|
96
|
+
const ports = new Set();
|
|
97
|
+
for (const child of this.launchedChildren) {
|
|
98
|
+
if (this.diagnosticsByChild.get(child)?.exited)
|
|
99
|
+
continue;
|
|
100
|
+
for (const port of await this.listenerInspector.inspectOwnedTcpListeners(child, this.owner))
|
|
101
|
+
ports.add(port);
|
|
102
|
+
}
|
|
103
|
+
return [...ports].sort((left, right) => left - right);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Why these process trees break isolation, or null when they do not: a listener the port
|
|
107
|
+
* broker did not assign, or listeners that could not be inspected (unproven isolation).
|
|
108
|
+
*/
|
|
109
|
+
async unbrokeredListenerViolation(brokered) {
|
|
110
|
+
try {
|
|
111
|
+
const stray = (await this.ownedListenerPorts()).filter(port => !brokered.has(port));
|
|
112
|
+
return stray.length === 0
|
|
113
|
+
? null
|
|
114
|
+
: `the launched runtime listens on port(s) the port broker did not assign (${stray.join(", ")})`;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
return `the launched runtime's listeners could not be inspected (${error instanceof Error ? error.message : String(error)})`;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Re-audits these process trees on an interval until stopped, so a listener opened after
|
|
122
|
+
* readiness (say, a fixed admin port bound once a database connects) is caught within one
|
|
123
|
+
* interval. `brokered` returning null skips a tick. The first violation is reported once and
|
|
124
|
+
* ends the monitor. Returns the function that stops it.
|
|
125
|
+
*/
|
|
126
|
+
monitorListeners(brokered, onViolation) {
|
|
127
|
+
let stopped = false;
|
|
128
|
+
let timer;
|
|
129
|
+
const schedule = () => {
|
|
130
|
+
timer = setTimeout(() => void tick(), this.listenerInspector.ownedListenerAuditIntervalMs);
|
|
131
|
+
timer.unref();
|
|
132
|
+
};
|
|
133
|
+
const tick = async () => {
|
|
134
|
+
const ports = stopped ? null : brokered();
|
|
135
|
+
const violation = ports === null ? null : await this.unbrokeredListenerViolation(ports);
|
|
136
|
+
if (stopped)
|
|
137
|
+
return;
|
|
138
|
+
if (violation === null)
|
|
139
|
+
return schedule();
|
|
140
|
+
stopped = true;
|
|
141
|
+
await onViolation(violation).catch(() => undefined);
|
|
142
|
+
};
|
|
143
|
+
schedule();
|
|
144
|
+
return () => {
|
|
145
|
+
stopped = true;
|
|
146
|
+
clearTimeout(timer);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
82
149
|
async stop(child) {
|
|
83
150
|
await child.stop(this.owner.fencingToken);
|
|
84
151
|
}
|
|
@@ -147,3 +214,96 @@ function replaceSanitizedErrorMessage(error, message) {
|
|
|
147
214
|
function boundedDiagnostic(value) {
|
|
148
215
|
return value.replaceAll(/[\r\n\t]+/gu, " ").trim().slice(-1_000);
|
|
149
216
|
}
|
|
217
|
+
/** Runs a provider process beneath an attempt supervisor; an analysis-only provider may open no loopback listener. */
|
|
218
|
+
export class AttemptSupervisedProviderProcessRunner {
|
|
219
|
+
supervisor;
|
|
220
|
+
owner;
|
|
221
|
+
listenerInspector;
|
|
222
|
+
enforceNoListeners;
|
|
223
|
+
onProcessTreeStarted;
|
|
224
|
+
constructor(supervisor, owner, listenerInspector, enforceNoListeners, onProcessTreeStarted) {
|
|
225
|
+
this.supervisor = supervisor;
|
|
226
|
+
this.owner = owner;
|
|
227
|
+
this.listenerInspector = listenerInspector;
|
|
228
|
+
this.enforceNoListeners = enforceNoListeners;
|
|
229
|
+
this.onProcessTreeStarted = onProcessTreeStarted;
|
|
230
|
+
}
|
|
231
|
+
async run(request, handlers = {}, signal) {
|
|
232
|
+
const baselineListeners = this.enforceNoListeners
|
|
233
|
+
? new Set(await this.listenerInspector.inspectLoopbackListeners())
|
|
234
|
+
: new Set();
|
|
235
|
+
const child = await this.supervisor.startChildReady("provider", request, handlers, signal, { maxStartAttempts: request.stdin === undefined ? 1 : 3, retryDelayMs: 100 });
|
|
236
|
+
this.onProcessTreeStarted();
|
|
237
|
+
try {
|
|
238
|
+
return await this.awaitProviderResult(child, baselineListeners, signal);
|
|
239
|
+
}
|
|
240
|
+
finally {
|
|
241
|
+
// The provider tree, and the MCP server inside it, is gone. A managed command that
|
|
242
|
+
// server started now has no supervisor and no receipt writer, so it must not keep
|
|
243
|
+
// running into the next provider generation. This never rejects; a command it
|
|
244
|
+
// cannot prove stopped is left to attempt teardown, which fails closed.
|
|
245
|
+
await this.supervisor.stopOrphanedManagedCommands();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async awaitProviderResult(child, baselineListeners, signal) {
|
|
249
|
+
if (!this.enforceNoListeners)
|
|
250
|
+
return await child.result;
|
|
251
|
+
let settled = false;
|
|
252
|
+
let inspectionFailure;
|
|
253
|
+
const monitor = (async () => {
|
|
254
|
+
while (!settled) {
|
|
255
|
+
try {
|
|
256
|
+
const ports = (await this.listenerInspector.inspectOwnedLoopbackListeners(child, this.owner))
|
|
257
|
+
.filter(port => !baselineListeners.has(port));
|
|
258
|
+
if (ports.length > 0) {
|
|
259
|
+
inspectionFailure = new Error(`host_analysis_listener_violation: analysis-only provider opened undeclared loopback listener(s): ${ports.join(", ")}.`);
|
|
260
|
+
await child.stop(this.owner.fencingToken).catch(() => undefined);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
inspectionFailure = error;
|
|
266
|
+
await child.stop(this.owner.fencingToken).catch(() => undefined);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
await waitForAnalysisInspection(250, signal).catch(error => {
|
|
270
|
+
if (!settled && !signal?.aborted)
|
|
271
|
+
inspectionFailure = error;
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
})();
|
|
275
|
+
try {
|
|
276
|
+
const result = await child.result;
|
|
277
|
+
settled = true;
|
|
278
|
+
await monitor;
|
|
279
|
+
if (inspectionFailure)
|
|
280
|
+
throw inspectionFailure;
|
|
281
|
+
return result;
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
settled = true;
|
|
285
|
+
await monitor.catch(() => undefined);
|
|
286
|
+
if (inspectionFailure)
|
|
287
|
+
throw inspectionFailure;
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function waitForAnalysisInspection(milliseconds, signal) {
|
|
293
|
+
if (signal?.aborted)
|
|
294
|
+
return Promise.reject(cancellationError(signal));
|
|
295
|
+
return new Promise((resolve, reject) => {
|
|
296
|
+
const timer = setTimeout(() => {
|
|
297
|
+
signal?.removeEventListener("abort", abort);
|
|
298
|
+
resolve();
|
|
299
|
+
}, milliseconds);
|
|
300
|
+
const abort = () => {
|
|
301
|
+
clearTimeout(timer);
|
|
302
|
+
reject(cancellationError(signal));
|
|
303
|
+
};
|
|
304
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
function cancellationError(signal) {
|
|
308
|
+
return new Error(String(signal?.reason ?? "Dispatch cancelled while waiting for host runtime singleton ownership."));
|
|
309
|
+
}
|
|
@@ -17,18 +17,29 @@ export declare class HostListenerInspector {
|
|
|
17
17
|
private readonly commands;
|
|
18
18
|
private readonly platform;
|
|
19
19
|
private readonly retryDelayMs;
|
|
20
|
+
/** How often a live shared-admission run's listeners are re-audited after readiness. */
|
|
21
|
+
ownedListenerAuditIntervalMs: number;
|
|
20
22
|
constructor(commands?: HostInspectionCommandRunner, platform?: NodeJS.Platform, retryDelayMs?: number);
|
|
21
23
|
inspect(child: AttemptProcessChildHandle, reservation: HostPortReservation, owner: HostPortLeaseOwner): Promise<HostListenerOwnership>;
|
|
22
24
|
/** Returns loopback TCP ports opened by one exact supervised child process tree. */
|
|
23
25
|
inspectOwnedLoopbackListeners(child: AttemptProcessChildHandle, owner: HostPortLeaseOwner): Promise<readonly number[]>;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the TCP ports that one exact supervised child process tree listens on, on any local
|
|
28
|
+
* address: IPv4 or IPv6 loopback, a wildcard (which covers loopback), or a specific interface.
|
|
29
|
+
* The parallel-isolation audit needs all of them, because a sibling could only collide with
|
|
30
|
+
* one it was never brokered.
|
|
31
|
+
*/
|
|
32
|
+
inspectOwnedTcpListeners(child: AttemptProcessChildHandle, owner: HostPortLeaseOwner): Promise<readonly number[]>;
|
|
24
33
|
/** Snapshots loopback TCP ports that predate a supervised child launch. */
|
|
25
34
|
inspectLoopbackListeners(): Promise<readonly number[]>;
|
|
26
35
|
private inspectWindows;
|
|
27
36
|
private inspectAllWindows;
|
|
37
|
+
private ownedWindowsPorts;
|
|
28
38
|
private runWindowsAncestry;
|
|
29
39
|
private runWindowsNetstat;
|
|
30
40
|
private inspectUnix;
|
|
31
41
|
private inspectAllUnix;
|
|
42
|
+
private ownedUnixPorts;
|
|
32
43
|
}
|
|
33
44
|
export declare function parseWindowsLoopbackListeners(output: string): readonly {
|
|
34
45
|
pid: number;
|
|
@@ -42,6 +53,21 @@ export declare function parseSsLoopbackListeners(output: string): readonly {
|
|
|
42
53
|
pid: number;
|
|
43
54
|
port: number;
|
|
44
55
|
}[];
|
|
56
|
+
/** TCP listeners on any local address (IPv4, IPv6 or wildcard) from `netstat -ano`. */
|
|
57
|
+
export declare function parseWindowsTcpListeners(output: string): readonly {
|
|
58
|
+
pid: number;
|
|
59
|
+
port: number;
|
|
60
|
+
}[];
|
|
61
|
+
/** TCP listeners on any local address from `lsof -iTCP -sTCP:LISTEN -Fpn`. */
|
|
62
|
+
export declare function parseLsofTcpListeners(output: string): readonly {
|
|
63
|
+
pid: number;
|
|
64
|
+
port: number;
|
|
65
|
+
}[];
|
|
66
|
+
/** TCP listeners on any local address from `ss -H -ltnp` (local address is the fourth column). */
|
|
67
|
+
export declare function parseSsTcpListeners(output: string): readonly {
|
|
68
|
+
pid: number;
|
|
69
|
+
port: number;
|
|
70
|
+
}[];
|
|
45
71
|
export declare class ExecFileInspectionCommandRunner implements HostInspectionCommandRunner {
|
|
46
72
|
run(command: string, args: readonly string[]): Promise<HostInspectionCommandResult>;
|
|
47
73
|
}
|
|
@@ -4,6 +4,8 @@ import path from "node:path";
|
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
const windowsListenerOwnerPath = fileURLToPath(new URL("../../runtime/windows-listener-owner.ps1", import.meta.url));
|
|
6
6
|
const windowsNetstatArgs = ["-ano", "-p", "TCP"];
|
|
7
|
+
// Without "-p", netstat lists TCP over IPv6 as well; "-p TCP" is IPv4 only.
|
|
8
|
+
const windowsAllNetstatArgs = ["-ano"];
|
|
7
9
|
const windowsNetstatAttempts = 3;
|
|
8
10
|
const windowsAncestryAttempts = 2;
|
|
9
11
|
const windowsNetstatRetryDelayMs = 250;
|
|
@@ -13,6 +15,8 @@ export class HostListenerInspector {
|
|
|
13
15
|
commands;
|
|
14
16
|
platform;
|
|
15
17
|
retryDelayMs;
|
|
18
|
+
/** How often a live shared-admission run's listeners are re-audited after readiness. */
|
|
19
|
+
ownedListenerAuditIntervalMs = 10_000;
|
|
16
20
|
constructor(commands = new ExecFileInspectionCommandRunner(), platform = process.platform, retryDelayMs = windowsNetstatRetryDelayMs) {
|
|
17
21
|
this.commands = commands;
|
|
18
22
|
this.platform = platform;
|
|
@@ -42,6 +46,32 @@ export class HostListenerInspector {
|
|
|
42
46
|
? await this.inspectAllWindows(metadata.rootPid)
|
|
43
47
|
: await this.inspectAllUnix(metadata.rootPid);
|
|
44
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Returns the TCP ports that one exact supervised child process tree listens on, on any local
|
|
51
|
+
* address: IPv4 or IPv6 loopback, a wildcard (which covers loopback), or a specific interface.
|
|
52
|
+
* The parallel-isolation audit needs all of them, because a sibling could only collide with
|
|
53
|
+
* one it was never brokered.
|
|
54
|
+
*/
|
|
55
|
+
async inspectOwnedTcpListeners(child, owner) {
|
|
56
|
+
const metadata = await loadMetadata(child.metadataPath);
|
|
57
|
+
if (!metadata || !matchesOwner(metadata, child, owner)) {
|
|
58
|
+
throw new Error("host_listener_inspection_invalid: supervised provider ownership metadata is unavailable.");
|
|
59
|
+
}
|
|
60
|
+
if (this.platform === "win32") {
|
|
61
|
+
const netstat = await this.runWindowsNetstat(windowsAllNetstatArgs);
|
|
62
|
+
return await this.ownedWindowsPorts(metadata.rootPid, parseWindowsTcpListeners(netstat.stdout));
|
|
63
|
+
}
|
|
64
|
+
const lsof = await this.commands.run("lsof", ["-nP", "-a", "-iTCP", "-sTCP:LISTEN", "-Fpn"]);
|
|
65
|
+
if (lsof.exitCode === 0 || lsof.exitCode === 1) {
|
|
66
|
+
return await this.ownedUnixPorts(metadata.rootPid, parseLsofTcpListeners(lsof.stdout));
|
|
67
|
+
}
|
|
68
|
+
if (lsof.errorCode !== "ENOENT")
|
|
69
|
+
throw commandError("lsof", lsof);
|
|
70
|
+
const ss = await this.commands.run("ss", ["-H", "-ltnp"]);
|
|
71
|
+
if (ss.exitCode !== 0)
|
|
72
|
+
throw commandError("ss", ss);
|
|
73
|
+
return await this.ownedUnixPorts(metadata.rootPid, parseSsTcpListeners(ss.stdout));
|
|
74
|
+
}
|
|
45
75
|
/** Snapshots loopback TCP ports that predate a supervised child launch. */
|
|
46
76
|
async inspectLoopbackListeners() {
|
|
47
77
|
if (this.platform === "win32") {
|
|
@@ -75,7 +105,9 @@ export class HostListenerInspector {
|
|
|
75
105
|
}
|
|
76
106
|
async inspectAllWindows(rootPid) {
|
|
77
107
|
const netstat = await this.runWindowsNetstat();
|
|
78
|
-
|
|
108
|
+
return await this.ownedWindowsPorts(rootPid, parseWindowsLoopbackListeners(netstat.stdout));
|
|
109
|
+
}
|
|
110
|
+
async ownedWindowsPorts(rootPid, listeners) {
|
|
79
111
|
if (listeners.length === 0)
|
|
80
112
|
return [];
|
|
81
113
|
const pids = [...new Set(listeners.map(listener => listener.pid))];
|
|
@@ -130,10 +162,10 @@ export class HostListenerInspector {
|
|
|
130
162
|
cause: lastParseError,
|
|
131
163
|
});
|
|
132
164
|
}
|
|
133
|
-
async runWindowsNetstat() {
|
|
165
|
+
async runWindowsNetstat(args = windowsNetstatArgs) {
|
|
134
166
|
let lastResult = null;
|
|
135
167
|
for (let attempt = 1; attempt <= windowsNetstatAttempts; attempt += 1) {
|
|
136
|
-
const result = await this.commands.run("netstat.exe",
|
|
168
|
+
const result = await this.commands.run("netstat.exe", args);
|
|
137
169
|
if (result.exitCode === 0)
|
|
138
170
|
return result;
|
|
139
171
|
lastResult = result;
|
|
@@ -198,6 +230,9 @@ export class HostListenerInspector {
|
|
|
198
230
|
else {
|
|
199
231
|
throw commandError("lsof", lsof);
|
|
200
232
|
}
|
|
233
|
+
return await this.ownedUnixPorts(rootPid, listeners);
|
|
234
|
+
}
|
|
235
|
+
async ownedUnixPorts(rootPid, listeners) {
|
|
201
236
|
if (listeners.length === 0)
|
|
202
237
|
return [];
|
|
203
238
|
const pids = [...new Set(listeners.map(listener => listener.pid))];
|
|
@@ -246,6 +281,44 @@ export function parseSsLoopbackListeners(output) {
|
|
|
246
281
|
});
|
|
247
282
|
}));
|
|
248
283
|
}
|
|
284
|
+
/** TCP listeners on any local address (IPv4, IPv6 or wildcard) from `netstat -ano`. */
|
|
285
|
+
export function parseWindowsTcpListeners(output) {
|
|
286
|
+
return uniqueListeners(output.split(/\r?\n/u).flatMap(line => {
|
|
287
|
+
const fields = line.trim().split(/\s+/u);
|
|
288
|
+
if (fields.length < 5 || fields[0]?.toUpperCase() !== "TCP" || fields[3]?.toUpperCase() !== "LISTENING")
|
|
289
|
+
return [];
|
|
290
|
+
const port = parseAnyEndpointPort(fields[1]);
|
|
291
|
+
const pids = validPid(fields[4]);
|
|
292
|
+
return port !== null && pids.length === 1 ? [{ pid: pids[0], port }] : [];
|
|
293
|
+
}));
|
|
294
|
+
}
|
|
295
|
+
/** TCP listeners on any local address from `lsof -iTCP -sTCP:LISTEN -Fpn`. */
|
|
296
|
+
export function parseLsofTcpListeners(output) {
|
|
297
|
+
const listeners = [];
|
|
298
|
+
let pid = null;
|
|
299
|
+
for (const line of output.split(/\r?\n/u)) {
|
|
300
|
+
if (line.startsWith("p"))
|
|
301
|
+
pid = validPid(line.slice(1))[0] ?? null;
|
|
302
|
+
if (!line.startsWith("n") || pid === null)
|
|
303
|
+
continue;
|
|
304
|
+
const port = parseAnyEndpointPort(line.slice(1));
|
|
305
|
+
if (port !== null)
|
|
306
|
+
listeners.push({ pid, port });
|
|
307
|
+
}
|
|
308
|
+
return uniqueListeners(listeners);
|
|
309
|
+
}
|
|
310
|
+
/** TCP listeners on any local address from `ss -H -ltnp` (local address is the fourth column). */
|
|
311
|
+
export function parseSsTcpListeners(output) {
|
|
312
|
+
return uniqueListeners(output.split(/\r?\n/u).flatMap(line => {
|
|
313
|
+
const port = parseAnyEndpointPort(line.trim().split(/\s+/u)[3]);
|
|
314
|
+
if (port === null)
|
|
315
|
+
return [];
|
|
316
|
+
return [...line.matchAll(/pid=(\d+)/gu)].flatMap(match => {
|
|
317
|
+
const pid = validPid(match[1])[0];
|
|
318
|
+
return pid ? [{ pid, port }] : [];
|
|
319
|
+
});
|
|
320
|
+
}));
|
|
321
|
+
}
|
|
249
322
|
export class ExecFileInspectionCommandRunner {
|
|
250
323
|
run(command, args) {
|
|
251
324
|
return new Promise(resolve => {
|
|
@@ -340,6 +413,14 @@ function parseIpv4LoopbackEndpoint(value) {
|
|
|
340
413
|
const port = Number.parseInt(match[1], 10);
|
|
341
414
|
return Number.isSafeInteger(port) && port >= 1 && port <= 65535 ? port : null;
|
|
342
415
|
}
|
|
416
|
+
// "127.0.0.1:80", "[::1]:80", "0.0.0.0:80", "[::]:80", "*:80", "127.0.0.53%lo:53".
|
|
417
|
+
function parseAnyEndpointPort(value) {
|
|
418
|
+
const match = value?.match(/^(?:\[[^\]\s]+\]|[^\s:[\]]+)(?:%[^\s:]+)?:(\d+)$/u);
|
|
419
|
+
if (!match)
|
|
420
|
+
return null;
|
|
421
|
+
const port = Number.parseInt(match[1], 10);
|
|
422
|
+
return Number.isSafeInteger(port) && port >= 1 && port <= 65535 ? port : null;
|
|
423
|
+
}
|
|
343
424
|
function uniqueListeners(values) {
|
|
344
425
|
const seen = new Set();
|
|
345
426
|
return values.filter(value => {
|
|
@@ -29,7 +29,7 @@ export interface HostRuntimeAdmissionCheckpoint {
|
|
|
29
29
|
}
|
|
30
30
|
export type HostRuntimeAdmissionCheckpointHandler = (checkpoint: HostRuntimeAdmissionCheckpoint) => void | Promise<void>;
|
|
31
31
|
export interface HostRuntimeProjectCheckpoint {
|
|
32
|
-
readonly code: "host_runtime_build_started" | "host_runtime_build_waiting_for_host_capacity" | "host_runtime_build_cached" | "host_runtime_build_completed" | "host_runtime_database_provisioned" | "host_runtime_contract_readmitted";
|
|
32
|
+
readonly code: "host_runtime_build_started" | "host_runtime_build_waiting_for_host_capacity" | "host_runtime_build_cached" | "host_runtime_build_completed" | "host_runtime_database_provisioned" | "host_runtime_contract_readmitted" | "host_runtime_isolation_serialized";
|
|
33
33
|
readonly message: string;
|
|
34
34
|
readonly data?: Readonly<Record<string, unknown>>;
|
|
35
35
|
}
|
|
@@ -118,6 +118,9 @@ export interface HostRuntimeLeaseRuntime {
|
|
|
118
118
|
providerMemoryTelemetry?(dispatchAttemptId: string, fencingToken: string): AttemptProcessTreeMemoryTelemetry | null;
|
|
119
119
|
release(dispatchAttemptId: string, fencingToken: string): Promise<RuntimeIsolationLease | null>;
|
|
120
120
|
invalidatePromotion?(repoId: string, scope?: "all" | "discovery"): Promise<void>;
|
|
121
|
+
promotionQuarantined?(repoId: string, runtimeContractFingerprint: string): Promise<boolean>;
|
|
122
|
+
/** The isolation violation the shared-run listener monitor found after launch, if any; the attempt must fail with it. */
|
|
123
|
+
isolationViolation?(dispatchAttemptId: string, fencingToken: string): Error | null;
|
|
121
124
|
hasForegroundDemand?(): boolean;
|
|
122
125
|
reap?(options: HostRuntimeReapOptions): Promise<HostRuntimeReapReport>;
|
|
123
126
|
snapshot?(): HostRuntimeCapacitySnapshot;
|
|
@@ -185,6 +188,16 @@ export declare class HostRuntimeLeaseManager implements HostRuntimeLeaseRuntime
|
|
|
185
188
|
hasForegroundDemand(): boolean;
|
|
186
189
|
acquirePreparation(dispatch: RunOnceDispatchPacket, signal?: AbortSignal): Promise<readonly string[]>;
|
|
187
190
|
acquire(dispatch: RunOnceDispatchPacket, signal?: AbortSignal): Promise<readonly string[]>;
|
|
191
|
+
/**
|
|
192
|
+
* Whether acquire asks for shared project admission. A parallel-eligible probe in a promoted repository
|
|
193
|
+
* runs beside its shared runs (the listener audit fails it closed); otherwise it probes exclusively.
|
|
194
|
+
*/
|
|
195
|
+
private sharedAdmissionRequested;
|
|
196
|
+
/**
|
|
197
|
+
* Whether a shared admission may launch without downgrading to exclusive: a stored proof matches the
|
|
198
|
+
* contract by isolation inputs or exact commit, or it is a still-eligible probe of an unrevoked repository.
|
|
199
|
+
*/
|
|
200
|
+
private sharedAdmissionStillValid;
|
|
188
201
|
private reserveHostCapacity;
|
|
189
202
|
private reserveBackgroundAdmission;
|
|
190
203
|
private hostCapacityAvailable;
|
|
@@ -220,8 +233,17 @@ export declare class HostRuntimeLeaseManager implements HostRuntimeLeaseRuntime
|
|
|
220
233
|
/** Stops configured project processes in reverse launch order and releases their ports. */
|
|
221
234
|
private stopConfiguredProject;
|
|
222
235
|
providerProcessRunner(dispatchAttemptId: string, fencingToken: string): ProviderProcessRunner | null;
|
|
236
|
+
isolationViolation(dispatchAttemptId: string, fencingToken: string): Error | null;
|
|
223
237
|
private markCapacityReservationRunning;
|
|
224
238
|
providerMemoryTelemetry(dispatchAttemptId: string, fencingToken: string): AttemptProcessTreeMemoryTelemetry | null;
|
|
239
|
+
/** Isolation enforcement for parallel-eligible contracts after each launch (restarts included); see monitorListeners. */
|
|
240
|
+
private auditRuntimeListeners;
|
|
241
|
+
/**
|
|
242
|
+
* Quarantines the contract and drops every promotion for the repository. An exclusive run collides with nobody, so it
|
|
243
|
+
* keeps running unpromoted and reports why; a shared one stops its project (a failed stop is left to release) and fails.
|
|
244
|
+
*/
|
|
245
|
+
private handleIsolationViolation;
|
|
246
|
+
private monitorSharedRuntimeListeners;
|
|
225
247
|
private promoteVerifiedRuntime;
|
|
226
248
|
launchNodeRuntime<T>(dispatchAttemptId: string, fencingToken: string, portName: string, command: NodeRuntimeCommand, launcher: NodeRuntimeCommandLauncher<T>, signal?: AbortSignal): Promise<HostNodeRuntimeLaunch<T>>;
|
|
227
249
|
release(dispatchAttemptId: string, fencingToken: string, preservePreparation?: boolean): Promise<RuntimeIsolationLease | null>;
|
|
@@ -251,7 +273,8 @@ export declare class HostRuntimeLeaseManager implements HostRuntimeLeaseRuntime
|
|
|
251
273
|
private drainParallelProjectWaiters;
|
|
252
274
|
private drainBackgroundAdmissionWaiters;
|
|
253
275
|
private refreshKnownPromotions;
|
|
254
|
-
invalidatePromotion(repoId: string, scope?: "all" | "discovery"): Promise<void>;
|
|
276
|
+
invalidatePromotion(repoId: string, scope?: "all" | "discovery", quarantineFingerprints?: readonly string[]): Promise<void>;
|
|
277
|
+
promotionQuarantined(repoId: string, fingerprint: string): Promise<boolean>;
|
|
255
278
|
private drainAdmissionWaiters;
|
|
256
279
|
private reconcileReservationOwners;
|
|
257
280
|
private reservationOwnerIsLive;
|