@loucompanion/forge-bridge 0.1.4-dev.318bc3bd5b6a → 0.1.4-dev.494233aa2e50
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/bridge/bridge.service.d.ts +12 -1
- package/dist/bridge/bridge.service.js +51 -5
- package/dist/bridge/bridge.types.d.ts +6 -0
- package/dist/bridge/dispatch-result-outbox.d.ts +11 -1
- package/dist/bridge/dispatch-result-outbox.js +31 -6
- package/dist/runtime/host-database-capability-boundary.d.ts +1 -1
- package/dist/runtime/host-database-capability-boundary.js +1 -0
- package/dist/runtime/host-facts.d.ts +35 -0
- package/dist/runtime/host-facts.js +95 -0
- package/dist/runtime/host-load-sampler.d.ts +83 -0
- package/dist/runtime/host-load-sampler.js +164 -0
- package/dist/runtime/host-pressure-stall.d.ts +41 -0
- package/dist/runtime/host-pressure-stall.js +121 -0
- package/dist/runtime/host-runtime-contract.d.ts +1 -0
- package/dist/runtime/host-runtime-contract.js +2 -0
- package/dist/runtime/host-runtime-lease.js +5 -0
- package/dist/runtime/playwright-browser-cache.d.ts +39 -0
- package/dist/runtime/playwright-browser-cache.js +227 -0
- package/dist/runtime/playwright-host-runtime-adapter.d.ts +11 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +48 -54
- package/dist/session/phase-completion.artifacts.js +4 -0
- package/dist/session/pre-publication-process.d.ts +20 -0
- package/dist/session/pre-publication-process.js +97 -0
- package/dist/session/pre-publication-skipped-tests.d.ts +21 -0
- package/dist/session/pre-publication-skipped-tests.js +109 -0
- package/dist/session/pre-publication-validation.js +27 -57
- package/dist/session/provider-cli/provider-cli.artifacts.js +2 -9
- package/dist/session/provider-cli/provider-cli.service.js +54 -7
- package/dist/session/publishable-worktree-changes.d.ts +45 -0
- package/dist/session/publishable-worktree-changes.js +177 -0
- package/dist/session/session.types.js +3 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { defaultHostEnvironment, detectHostFacts, readOptional, } from "./host-facts.js";
|
|
3
|
+
import { cgroupLeafName, isDedicatedBridgeUnit, parseCgroupCpuUsageUsec, parseCgroupMemoryAnonBytes, parseMemAvailableBytes, parseNonNegativeInteger, parsePressureStall, parseUnifiedCgroupPath, } from "./host-pressure-stall.js";
|
|
4
|
+
export const defaultHostLoadSampleIntervalMs = 5_000;
|
|
5
|
+
/**
|
|
6
|
+
* Samples Linux PSI and the bridge's own cgroup on an interval and caches the
|
|
7
|
+
* latest snapshot. Every read is async and failure-tolerant: a host without PSI
|
|
8
|
+
* (non-Linux, old kernel, `psi=0`) reports `psiAvailable: false` and no pressure.
|
|
9
|
+
*/
|
|
10
|
+
export class HostLoadSampler {
|
|
11
|
+
environment;
|
|
12
|
+
intervalMs;
|
|
13
|
+
now;
|
|
14
|
+
monotonicNow;
|
|
15
|
+
cgroupUnit;
|
|
16
|
+
generation = 0;
|
|
17
|
+
timer = null;
|
|
18
|
+
starting = null;
|
|
19
|
+
inFlight = null;
|
|
20
|
+
hostFacts = null;
|
|
21
|
+
latest = null;
|
|
22
|
+
cpuMark = null;
|
|
23
|
+
constructor(options = {}) {
|
|
24
|
+
this.environment = options.environment ?? defaultHostEnvironment();
|
|
25
|
+
this.intervalMs = options.intervalMs !== undefined && Number.isFinite(options.intervalMs) && options.intervalMs > 0
|
|
26
|
+
? options.intervalMs
|
|
27
|
+
: defaultHostLoadSampleIntervalMs;
|
|
28
|
+
this.now = options.now ?? Date.now;
|
|
29
|
+
this.monotonicNow = options.monotonicNow ?? (() => performance.now());
|
|
30
|
+
this.cgroupUnit = options.cgroupUnit !== undefined
|
|
31
|
+
? options.cgroupUnit
|
|
32
|
+
: process.env.FORGE_BRIDGE_CGROUP_UNIT?.trim() || null;
|
|
33
|
+
}
|
|
34
|
+
facts() {
|
|
35
|
+
return this.hostFacts;
|
|
36
|
+
}
|
|
37
|
+
current() {
|
|
38
|
+
return this.latest;
|
|
39
|
+
}
|
|
40
|
+
/** Detects static facts once, takes a first sample, then samples on the interval. Idempotent. */
|
|
41
|
+
start() {
|
|
42
|
+
if (this.timer)
|
|
43
|
+
return Promise.resolve();
|
|
44
|
+
this.starting ??= this.startOnce().finally(() => {
|
|
45
|
+
this.starting = null;
|
|
46
|
+
});
|
|
47
|
+
return this.starting;
|
|
48
|
+
}
|
|
49
|
+
stop() {
|
|
50
|
+
// Invalidates an in-progress start() so it cannot arm the interval afterwards.
|
|
51
|
+
this.generation += 1;
|
|
52
|
+
if (this.timer)
|
|
53
|
+
clearInterval(this.timer);
|
|
54
|
+
this.timer = null;
|
|
55
|
+
}
|
|
56
|
+
/** Takes one sample now and caches it. Concurrent callers share one read. */
|
|
57
|
+
sample() {
|
|
58
|
+
this.inFlight ??= this.readSnapshot()
|
|
59
|
+
.then(snapshot => {
|
|
60
|
+
this.latest = snapshot;
|
|
61
|
+
return snapshot;
|
|
62
|
+
})
|
|
63
|
+
.finally(() => {
|
|
64
|
+
this.inFlight = null;
|
|
65
|
+
});
|
|
66
|
+
return this.inFlight;
|
|
67
|
+
}
|
|
68
|
+
async startOnce() {
|
|
69
|
+
const generation = this.generation;
|
|
70
|
+
if (!this.hostFacts) {
|
|
71
|
+
this.hostFacts = await detectHostFacts(this.environment).catch(() => null);
|
|
72
|
+
}
|
|
73
|
+
await this.sample().catch(() => undefined);
|
|
74
|
+
if (this.timer || generation !== this.generation)
|
|
75
|
+
return;
|
|
76
|
+
this.timer = setInterval(() => {
|
|
77
|
+
void this.sample().catch(() => undefined);
|
|
78
|
+
}, this.intervalMs);
|
|
79
|
+
this.timer.unref?.();
|
|
80
|
+
}
|
|
81
|
+
async readSnapshot() {
|
|
82
|
+
const sampledAtMs = this.now();
|
|
83
|
+
const monotonicMs = this.monotonicNow();
|
|
84
|
+
const linux = this.environment.platform === "linux";
|
|
85
|
+
const [systemPressure, ownCgroup, memoryAvailableBytes] = linux
|
|
86
|
+
? await Promise.all([this.readSystemPressure(), this.readOwnCgroup(monotonicMs), this.readMemoryAvailable()])
|
|
87
|
+
: [null, null, this.fallbackFreeMemory()];
|
|
88
|
+
return {
|
|
89
|
+
sampledAt: new Date(sampledAtMs).toISOString(),
|
|
90
|
+
psiAvailable: systemPressure !== null,
|
|
91
|
+
systemPressure,
|
|
92
|
+
ownCgroupDedicated: ownCgroup !== null,
|
|
93
|
+
ownCgroup,
|
|
94
|
+
memoryAvailableBytes,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
async readSystemPressure() {
|
|
98
|
+
const pressureRoot = path.posix.join(this.environment.procRoot, "pressure");
|
|
99
|
+
const [cpu, memory, io] = await Promise.all(["cpu", "memory", "io"].map(resource => this.readPressure(path.posix.join(pressureRoot, resource))));
|
|
100
|
+
// CPU pressure is the governor's primary signal; without it PSI is unavailable.
|
|
101
|
+
return cpu ? { cpu, memory: memory ?? null, io: io ?? null } : null;
|
|
102
|
+
}
|
|
103
|
+
async readOwnCgroup(monotonicMs) {
|
|
104
|
+
const membership = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "self", "cgroup"));
|
|
105
|
+
const cgroupPath = membership ? parseUnifiedCgroupPath(membership) : null;
|
|
106
|
+
const unit = cgroupPath ? cgroupLeafName(cgroupPath) : null;
|
|
107
|
+
if (!cgroupPath || !unit || !isDedicatedBridgeUnit(unit, this.cgroupUnit)) {
|
|
108
|
+
this.cpuMark = null;
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const directory = path.posix.join(this.environment.cgroupRoot, cgroupPath);
|
|
112
|
+
if (!directory.startsWith(`${this.environment.cgroupRoot.replace(/\/+$/u, "")}/`))
|
|
113
|
+
return null;
|
|
114
|
+
const [cpuPressure, memoryPressure, cpuStat, memoryStat, memoryCurrent] = await Promise.all([
|
|
115
|
+
this.readPressure(path.posix.join(directory, "cpu.pressure")),
|
|
116
|
+
this.readPressure(path.posix.join(directory, "memory.pressure")),
|
|
117
|
+
readOptional(this.environment, path.posix.join(directory, "cpu.stat")),
|
|
118
|
+
readOptional(this.environment, path.posix.join(directory, "memory.stat")),
|
|
119
|
+
readOptional(this.environment, path.posix.join(directory, "memory.current")),
|
|
120
|
+
]);
|
|
121
|
+
const usageUsec = cpuStat ? parseCgroupCpuUsageUsec(cpuStat) : null;
|
|
122
|
+
const memoryAnonBytes = memoryStat ? parseCgroupMemoryAnonBytes(memoryStat) : null;
|
|
123
|
+
const memoryChargedBytes = memoryCurrent ? parseNonNegativeInteger(memoryCurrent) : null;
|
|
124
|
+
// Ownership is settled by membership and the unit name above; metric files that are
|
|
125
|
+
// unavailable yield null values (and reset the CPU mark) but never demote the unit to shared.
|
|
126
|
+
return {
|
|
127
|
+
unit,
|
|
128
|
+
cpuPressure,
|
|
129
|
+
memoryPressure,
|
|
130
|
+
cpuCoresInUse: this.coresInUse(cgroupPath, usageUsec, monotonicMs),
|
|
131
|
+
memoryAnonBytes,
|
|
132
|
+
memoryChargedBytes,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
coresInUse(cgroupPath, usageUsec, atMs) {
|
|
136
|
+
const previous = this.cpuMark;
|
|
137
|
+
this.cpuMark = usageUsec === null ? null : { cgroupPath, usageUsec, atMs };
|
|
138
|
+
if (!previous || usageUsec === null || previous.cgroupPath !== cgroupPath)
|
|
139
|
+
return null;
|
|
140
|
+
const elapsedUsec = (atMs - previous.atMs) * 1_000;
|
|
141
|
+
const usedUsec = usageUsec - previous.usageUsec;
|
|
142
|
+
// A restarted unit resets its counter; elapsed time comes from a monotonic clock.
|
|
143
|
+
if (elapsedUsec <= 0 || usedUsec < 0)
|
|
144
|
+
return null;
|
|
145
|
+
return Math.round((usedUsec / elapsedUsec) * 100) / 100;
|
|
146
|
+
}
|
|
147
|
+
async readMemoryAvailable() {
|
|
148
|
+
const meminfo = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "meminfo"));
|
|
149
|
+
return (meminfo ? parseMemAvailableBytes(meminfo) : null) ?? this.fallbackFreeMemory();
|
|
150
|
+
}
|
|
151
|
+
fallbackFreeMemory() {
|
|
152
|
+
try {
|
|
153
|
+
const free = this.environment.freeMemoryBytes();
|
|
154
|
+
return Number.isFinite(free) && free >= 0 ? free : null;
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async readPressure(filePath) {
|
|
161
|
+
const content = await readOptional(this.environment, filePath);
|
|
162
|
+
return content ? parsePressureStall(content) : null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface PressureStallAverages {
|
|
2
|
+
/** Percent of wall time stalled over the last 10 seconds (0-100). */
|
|
3
|
+
readonly avg10: number;
|
|
4
|
+
readonly avg60: number;
|
|
5
|
+
readonly avg300: number;
|
|
6
|
+
}
|
|
7
|
+
export interface PressureStallResource {
|
|
8
|
+
readonly some: PressureStallAverages | null;
|
|
9
|
+
readonly full: PressureStallAverages | null;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Parses one PSI file. Returns null when the content has no well-formed
|
|
13
|
+
* `some`/`full` line, so callers can report PSI as unavailable instead of
|
|
14
|
+
* publishing zeros for a file they could not interpret.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parsePressureStall(content: string): PressureStallResource | null;
|
|
17
|
+
/** Reads `usage_usec` from a cgroup v2 `cpu.stat` file. */
|
|
18
|
+
export declare function parseCgroupCpuUsageUsec(content: string): number | null;
|
|
19
|
+
/** Reads anonymous (RSS-like, excludes page cache) bytes from cgroup v2 `memory.stat`. */
|
|
20
|
+
export declare function parseCgroupMemoryAnonBytes(content: string): number | null;
|
|
21
|
+
/** Reads a single non-negative integer file such as cgroup v2 `memory.current`. */
|
|
22
|
+
export declare function parseNonNegativeInteger(content: string): number | null;
|
|
23
|
+
/** Reads `MemAvailable` (bytes) from /proc/meminfo. */
|
|
24
|
+
export declare function parseMemAvailableBytes(content: string): number | null;
|
|
25
|
+
/**
|
|
26
|
+
* Resolves the unified (cgroup v2) hierarchy path from /proc/self/cgroup.
|
|
27
|
+
* Returns null on pure cgroup v1 hosts (no `0::` entry) and for the root cgroup.
|
|
28
|
+
*/
|
|
29
|
+
export declare function parseUnifiedCgroupPath(content: string): string | null;
|
|
30
|
+
/** The leaf cgroup name, e.g. `forge-bridge.service`. */
|
|
31
|
+
export declare function cgroupLeafName(cgroupPath: string): string | null;
|
|
32
|
+
/**
|
|
33
|
+
* Whether the bridge's cgroup is dedicated to it, so its usage can later be
|
|
34
|
+
* subtracted from system pressure as "Forge's own load". Only a systemd
|
|
35
|
+
* `.service` qualifies by default, or exactly the unit an operator names via
|
|
36
|
+
* FORGE_BRIDGE_CGROUP_UNIT. A `.scope` is rejected: a bridge started from a
|
|
37
|
+
* terminal, SSH session or desktop app shares `session-N.scope` /
|
|
38
|
+
* `app-*.scope` with unrelated work (possibly the owner's game).
|
|
39
|
+
* Container cgroups (e.g. `docker-<id>.scope`) are added in decision 0013 step 2.
|
|
40
|
+
*/
|
|
41
|
+
export declare function isDedicatedBridgeUnit(unit: string, configuredUnit: string | null | undefined): boolean;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Linux Pressure Stall Information (PSI) parsing shared by the host load sampler.
|
|
2
|
+
// Format (system-wide /proc/pressure/<resource> and cgroup v2 <resource>.pressure):
|
|
3
|
+
// some avg10=0.12 avg60=0.34 avg300=0.56 total=123456
|
|
4
|
+
// full avg10=0.00 avg60=0.00 avg300=0.00 total=0
|
|
5
|
+
const pressureLinePattern = /^(some|full)\s+(.*)$/u;
|
|
6
|
+
/**
|
|
7
|
+
* Parses one PSI file. Returns null when the content has no well-formed
|
|
8
|
+
* `some`/`full` line, so callers can report PSI as unavailable instead of
|
|
9
|
+
* publishing zeros for a file they could not interpret.
|
|
10
|
+
*/
|
|
11
|
+
export function parsePressureStall(content) {
|
|
12
|
+
let some = null;
|
|
13
|
+
let full = null;
|
|
14
|
+
for (const rawLine of content.split(/\r?\n/u)) {
|
|
15
|
+
const match = pressureLinePattern.exec(rawLine.trim());
|
|
16
|
+
if (!match)
|
|
17
|
+
continue;
|
|
18
|
+
const averages = parseAverages(match[2]);
|
|
19
|
+
if (!averages)
|
|
20
|
+
continue;
|
|
21
|
+
if (match[1] === "some")
|
|
22
|
+
some = averages;
|
|
23
|
+
else
|
|
24
|
+
full = averages;
|
|
25
|
+
}
|
|
26
|
+
return some || full ? { some, full } : null;
|
|
27
|
+
}
|
|
28
|
+
const pressureFieldPattern = /^(avg10|avg60|avg300|total)=(\d+(?:\.\d+)?)$/u;
|
|
29
|
+
/** A PSI line must carry exactly avg10, avg60, avg300 and an integer total; anything else is malformed. */
|
|
30
|
+
function parseAverages(fields) {
|
|
31
|
+
const values = new Map();
|
|
32
|
+
for (const field of fields.trim().split(/\s+/u)) {
|
|
33
|
+
const match = pressureFieldPattern.exec(field);
|
|
34
|
+
if (!match || values.has(match[1]))
|
|
35
|
+
return null;
|
|
36
|
+
values.set(match[1], Number(match[2]));
|
|
37
|
+
}
|
|
38
|
+
const avg10 = values.get("avg10");
|
|
39
|
+
const avg60 = values.get("avg60");
|
|
40
|
+
const avg300 = values.get("avg300");
|
|
41
|
+
const total = values.get("total");
|
|
42
|
+
if (avg10 === undefined || avg60 === undefined || avg300 === undefined
|
|
43
|
+
|| total === undefined || !Number.isSafeInteger(total))
|
|
44
|
+
return null;
|
|
45
|
+
return { avg10: clampPercent(avg10), avg60: clampPercent(avg60), avg300: clampPercent(avg300) };
|
|
46
|
+
}
|
|
47
|
+
function clampPercent(value) {
|
|
48
|
+
return Math.min(100, Math.max(0, value));
|
|
49
|
+
}
|
|
50
|
+
/** Reads `usage_usec` from a cgroup v2 `cpu.stat` file. */
|
|
51
|
+
export function parseCgroupCpuUsageUsec(content) {
|
|
52
|
+
const match = /^usage_usec\s+(\d+)\s*$/mu.exec(content);
|
|
53
|
+
if (!match)
|
|
54
|
+
return null;
|
|
55
|
+
const value = Number(match[1]);
|
|
56
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
57
|
+
}
|
|
58
|
+
/** Reads anonymous (RSS-like, excludes page cache) bytes from cgroup v2 `memory.stat`. */
|
|
59
|
+
export function parseCgroupMemoryAnonBytes(content) {
|
|
60
|
+
const match = /^anon\s+(\d+)\s*$/mu.exec(content);
|
|
61
|
+
if (!match)
|
|
62
|
+
return null;
|
|
63
|
+
const value = Number(match[1]);
|
|
64
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
65
|
+
}
|
|
66
|
+
/** Reads a single non-negative integer file such as cgroup v2 `memory.current`. */
|
|
67
|
+
export function parseNonNegativeInteger(content) {
|
|
68
|
+
const trimmed = content.trim();
|
|
69
|
+
if (!/^\d+$/u.test(trimmed))
|
|
70
|
+
return null;
|
|
71
|
+
const value = Number(trimmed);
|
|
72
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
73
|
+
}
|
|
74
|
+
/** Reads `MemAvailable` (bytes) from /proc/meminfo. */
|
|
75
|
+
export function parseMemAvailableBytes(content) {
|
|
76
|
+
const match = /^MemAvailable:\s+(\d+)\s*kB\s*$/mu.exec(content);
|
|
77
|
+
if (!match)
|
|
78
|
+
return null;
|
|
79
|
+
const value = Number(match[1]) * 1024;
|
|
80
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Resolves the unified (cgroup v2) hierarchy path from /proc/self/cgroup.
|
|
84
|
+
* Returns null on pure cgroup v1 hosts (no `0::` entry) and for the root cgroup.
|
|
85
|
+
*/
|
|
86
|
+
export function parseUnifiedCgroupPath(content) {
|
|
87
|
+
for (const rawLine of content.split(/\r?\n/u)) {
|
|
88
|
+
const line = rawLine.trim();
|
|
89
|
+
if (!line.startsWith("0::"))
|
|
90
|
+
continue;
|
|
91
|
+
const cgroupPath = line.slice(3).trim();
|
|
92
|
+
if (!cgroupPath.startsWith("/") || cgroupPath === "/")
|
|
93
|
+
return null;
|
|
94
|
+
if (cgroupPath.split("/").some(segment => segment === ".."))
|
|
95
|
+
return null;
|
|
96
|
+
return cgroupPath;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
/** The leaf cgroup name, e.g. `forge-bridge.service`. */
|
|
101
|
+
export function cgroupLeafName(cgroupPath) {
|
|
102
|
+
return cgroupPath.split("/").filter(Boolean).at(-1) ?? null;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Whether the bridge's cgroup is dedicated to it, so its usage can later be
|
|
106
|
+
* subtracted from system pressure as "Forge's own load". Only a systemd
|
|
107
|
+
* `.service` qualifies by default, or exactly the unit an operator names via
|
|
108
|
+
* FORGE_BRIDGE_CGROUP_UNIT. A `.scope` is rejected: a bridge started from a
|
|
109
|
+
* terminal, SSH session or desktop app shares `session-N.scope` /
|
|
110
|
+
* `app-*.scope` with unrelated work (possibly the owner's game).
|
|
111
|
+
* Container cgroups (e.g. `docker-<id>.scope`) are added in decision 0013 step 2.
|
|
112
|
+
*/
|
|
113
|
+
export function isDedicatedBridgeUnit(unit, configuredUnit) {
|
|
114
|
+
const configured = configuredUnit?.trim();
|
|
115
|
+
if (configured)
|
|
116
|
+
return unit === configured;
|
|
117
|
+
// `app-*` services are desktop-launched apps (XDG naming); `user@N` is the whole user manager.
|
|
118
|
+
return /^[^@\s]+(@[^\s]+)?\.service$/u.test(unit)
|
|
119
|
+
&& !/^app-/u.test(unit)
|
|
120
|
+
&& !/^user@\d+\.service$/u.test(unit);
|
|
121
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare const hostRuntimeContractPath = ".forge/forge.runtime.yml";
|
|
2
2
|
export declare const hostRuntimeTestPostgresAdminEnvironment = "FORGE_TEST_POSTGRES_ADMIN_CONNECTION_STRING";
|
|
3
3
|
export declare const hostRuntimeLegacyTestPostgresEnvironment = "FORGE_TEST_POSTGRES_CONNECTION_STRING";
|
|
4
|
+
export declare const hostRuntimeTestPostgresProvisionerEnvironment = "FORGE_TEST_POSTGRES_PROVISIONER_CONNECTION_STRING";
|
|
4
5
|
export type RequestedHostConcurrency = "serial" | "parallel" | "disabled";
|
|
5
6
|
export type HostRuntimePortAdapter = "node" | "vite" | "next" | "aspnetcore";
|
|
6
7
|
export interface HostRuntimeLaunchConfig {
|
|
@@ -7,6 +7,8 @@ import { isGeneratedOutputDirectoryName } from "./generated-output-reset.js";
|
|
|
7
7
|
export const hostRuntimeContractPath = ".forge/forge.runtime.yml";
|
|
8
8
|
export const hostRuntimeTestPostgresAdminEnvironment = "FORGE_TEST_POSTGRES_ADMIN_CONNECTION_STRING";
|
|
9
9
|
export const hostRuntimeLegacyTestPostgresEnvironment = "FORGE_TEST_POSTGRES_CONNECTION_STRING";
|
|
10
|
+
// Optional superuser login for operator-run .NET fixtures. Never reaches attempt, validation or provider processes.
|
|
11
|
+
export const hostRuntimeTestPostgresProvisionerEnvironment = "FORGE_TEST_POSTGRES_PROVISIONER_CONNECTION_STRING";
|
|
10
12
|
const maximumContractBytes = 64 * 1024;
|
|
11
13
|
const maximumDepth = 12;
|
|
12
14
|
// Every discovery scan stops after this many directory entries. A monorepo needs a few
|
|
@@ -1172,6 +1172,11 @@ export class HostRuntimeLeaseManager {
|
|
|
1172
1172
|
executionClass: active.executionClass,
|
|
1173
1173
|
};
|
|
1174
1174
|
}
|
|
1175
|
+
const playwrightConfig = active.runtimeContract.contract.runtime.playwright;
|
|
1176
|
+
// A host without the project's pinned browsers fails here, before any database,
|
|
1177
|
+
// build, launch, or promotion work that the attempt would otherwise waste.
|
|
1178
|
+
if (playwrightConfig)
|
|
1179
|
+
await this.playwrightAdapter.assertBrowserCacheReady(playwrightConfig, active.lease.worktreePath);
|
|
1175
1180
|
const owner = hostPortOwner(active.lease);
|
|
1176
1181
|
const logDirectory = active.lease.resources.paths.find(entry => entry.role === "logs")?.path;
|
|
1177
1182
|
if (!logDirectory)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ForgeFailureError } from "../session/session.types.js";
|
|
2
|
+
/** Host browser caches to try, given the directory of the project's resolved playwright-core (if any). */
|
|
3
|
+
export type PlaywrightBrowserCacheCandidates = (playwrightCoreDirectory: string | null) => readonly string[];
|
|
4
|
+
export declare function defaultPlaywrightBrowserCacheCandidates(playwrightCoreDirectory: string | null, env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform, homeDirectory?: string): readonly string[];
|
|
5
|
+
export interface PinnedChromiumBuilds {
|
|
6
|
+
readonly version: string;
|
|
7
|
+
readonly playwrightCoreDirectory: string;
|
|
8
|
+
readonly builds: readonly PinnedChromiumBuild[];
|
|
9
|
+
}
|
|
10
|
+
interface PinnedChromiumBuild {
|
|
11
|
+
readonly directory: string;
|
|
12
|
+
readonly executables: readonly string[];
|
|
13
|
+
}
|
|
14
|
+
export interface PlaywrightBrowserCacheResolution {
|
|
15
|
+
/** The first host cache holding every pinned build, or the legacy any-Chromium match when nothing is pinned. */
|
|
16
|
+
readonly cache: string | null;
|
|
17
|
+
readonly pinned: PinnedChromiumBuilds | null;
|
|
18
|
+
readonly checked: readonly string[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Finds a host browser cache holding the Chromium and Chromium headless shell
|
|
22
|
+
* builds the project's installed Playwright pins. Only those two builds are
|
|
23
|
+
* checked: they are what the project's Chromium projects launch.
|
|
24
|
+
*/
|
|
25
|
+
export declare function findPlaywrightBrowserCache(realWorktree: string, configDirectory: string, candidatesFor: PlaywrightBrowserCacheCandidates, hostPlatform: string): Promise<PlaywrightBrowserCacheResolution>;
|
|
26
|
+
export declare function missingPlaywrightBrowsersError(resolution: PlaywrightBrowserCacheResolution & {
|
|
27
|
+
readonly pinned: PinnedChromiumBuilds;
|
|
28
|
+
}, installCommand: string): ForgeFailureError;
|
|
29
|
+
export interface PlaywrightHostProbe {
|
|
30
|
+
readonly env: NodeJS.ProcessEnv;
|
|
31
|
+
readonly platform: NodeJS.Platform;
|
|
32
|
+
readonly release: string;
|
|
33
|
+
readonly arch: string;
|
|
34
|
+
readonly appleCpu: boolean;
|
|
35
|
+
readonly osRelease: () => string | null;
|
|
36
|
+
}
|
|
37
|
+
/** Mirrors Playwright's own host platform key, which selects `revisionOverrides` entries. */
|
|
38
|
+
export declare function playwrightHostPlatform(probe?: PlaywrightHostProbe): string;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { ForgeFailureError } from "../session/session.types.js";
|
|
7
|
+
export function defaultPlaywrightBrowserCacheCandidates(playwrightCoreDirectory, env = process.env, platform = process.platform, homeDirectory = os.homedir()) {
|
|
8
|
+
const browsersPath = env.PLAYWRIGHT_BROWSERS_PATH?.trim();
|
|
9
|
+
return [
|
|
10
|
+
// PLAYWRIGHT_BROWSERS_PATH=0 makes Playwright use the browsers installed beside playwright-core.
|
|
11
|
+
browsersPath === "0"
|
|
12
|
+
? playwrightCoreDirectory ? path.join(playwrightCoreDirectory, ".local-browsers") : null
|
|
13
|
+
: browsersPath || null,
|
|
14
|
+
env.LOCALAPPDATA ? path.join(env.LOCALAPPDATA, "ms-playwright") : null,
|
|
15
|
+
env.XDG_CACHE_HOME ? path.join(env.XDG_CACHE_HOME, "ms-playwright") : null,
|
|
16
|
+
platform === "darwin" ? path.join(homeDirectory, "Library", "Caches", "ms-playwright") : null,
|
|
17
|
+
path.join(homeDirectory, ".cache", "ms-playwright"),
|
|
18
|
+
].filter((candidate) => Boolean(candidate?.trim()));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Finds a host browser cache holding the Chromium and Chromium headless shell
|
|
22
|
+
* builds the project's installed Playwright pins. Only those two builds are
|
|
23
|
+
* checked: they are what the project's Chromium projects launch.
|
|
24
|
+
*/
|
|
25
|
+
export async function findPlaywrightBrowserCache(realWorktree, configDirectory, candidatesFor, hostPlatform) {
|
|
26
|
+
const pinned = await readPinnedChromiumBuilds(realWorktree, configDirectory, hostPlatform);
|
|
27
|
+
const checked = [...new Set(candidatesFor(pinned?.playwrightCoreDirectory ?? null).map(candidate => path.resolve(candidate)))];
|
|
28
|
+
for (const candidate of checked) {
|
|
29
|
+
const ready = pinned
|
|
30
|
+
? await hasPinnedChromiumBuilds(candidate, pinned.builds)
|
|
31
|
+
: await hasChromiumExecutable(candidate);
|
|
32
|
+
if (ready)
|
|
33
|
+
return { cache: candidate, pinned, checked };
|
|
34
|
+
}
|
|
35
|
+
return { cache: null, pinned, checked };
|
|
36
|
+
}
|
|
37
|
+
export function missingPlaywrightBrowsersError(resolution, installCommand) {
|
|
38
|
+
const missing = resolution.pinned.builds.map(build => build.directory).join(" and ");
|
|
39
|
+
const checked = resolution.checked.length > 0 ? resolution.checked.join(", ") : "none";
|
|
40
|
+
return new ForgeFailureError("host_runtime_project_start_failed", `host_runtime_project_start_failed: playwright_browsers_missing: Playwright ${resolution.pinned.version} launches `
|
|
41
|
+
+ `Chromium from ${missing}, and no host browser cache holds them (checked: ${checked}). Only the pinned Chromium `
|
|
42
|
+
+ `and Chromium headless shell builds are checked. The attempt stopped before it started; install them on this `
|
|
43
|
+
+ `host with \`${installCommand}\` and dispatch again.`, "bridge");
|
|
44
|
+
}
|
|
45
|
+
// Executable layouts Playwright launches from, per browser directory (current and legacy builds).
|
|
46
|
+
const chromiumExecutables = {
|
|
47
|
+
win32: [path.join("chrome-win64", "chrome.exe"), path.join("chrome-win", "chrome.exe")],
|
|
48
|
+
darwin: [
|
|
49
|
+
path.join("chrome-mac-arm64", "Google Chrome for Testing.app", "Contents", "MacOS", "Google Chrome for Testing"),
|
|
50
|
+
path.join("chrome-mac-x64", "Google Chrome for Testing.app", "Contents", "MacOS", "Google Chrome for Testing"),
|
|
51
|
+
path.join("chrome-mac", "Chromium.app", "Contents", "MacOS", "Chromium"),
|
|
52
|
+
],
|
|
53
|
+
linux: [path.join("chrome-linux64", "chrome"), path.join("chrome-linux", "chrome")],
|
|
54
|
+
};
|
|
55
|
+
const headlessShellExecutables = {
|
|
56
|
+
win32: [path.join("chrome-headless-shell-win64", "chrome-headless-shell.exe"), path.join("chrome-win", "headless_shell.exe")],
|
|
57
|
+
darwin: [
|
|
58
|
+
path.join("chrome-headless-shell-mac-arm64", "chrome-headless-shell"),
|
|
59
|
+
path.join("chrome-headless-shell-mac-x64", "chrome-headless-shell"),
|
|
60
|
+
path.join("chrome-mac", "headless_shell"),
|
|
61
|
+
],
|
|
62
|
+
linux: [path.join("chrome-headless-shell-linux64", "chrome-headless-shell"), path.join("chrome-linux", "headless_shell")],
|
|
63
|
+
};
|
|
64
|
+
function platformExecutables(table) {
|
|
65
|
+
return table[process.platform] ?? table.linux;
|
|
66
|
+
}
|
|
67
|
+
async function readPinnedChromiumBuilds(realWorktree, configDirectory, hostPlatform) {
|
|
68
|
+
const manifestPath = await resolvePlaywrightCoreManifest(realWorktree, configDirectory);
|
|
69
|
+
if (!manifestPath)
|
|
70
|
+
return null;
|
|
71
|
+
let version;
|
|
72
|
+
let browsers;
|
|
73
|
+
try {
|
|
74
|
+
version = String(JSON.parse(await fs.readFile(manifestPath, "utf8")).version ?? "unknown");
|
|
75
|
+
const parsed = JSON.parse(await fs.readFile(path.join(path.dirname(manifestPath), "browsers.json"), "utf8"));
|
|
76
|
+
browsers = Array.isArray(parsed.browsers) ? parsed.browsers : [];
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const builds = [];
|
|
82
|
+
for (const [name, executables] of [
|
|
83
|
+
["chromium", platformExecutables(chromiumExecutables)],
|
|
84
|
+
["chromium-headless-shell", platformExecutables(headlessShellExecutables)],
|
|
85
|
+
]) {
|
|
86
|
+
const entry = browsers.find(browser => browser.name === name);
|
|
87
|
+
if (!entry || typeof entry.revision !== "string")
|
|
88
|
+
continue;
|
|
89
|
+
// Playwright replaces the revision only for the host platform it runs on.
|
|
90
|
+
const override = entry.revisionOverrides && typeof entry.revisionOverrides === "object"
|
|
91
|
+
? entry.revisionOverrides[hostPlatform]
|
|
92
|
+
: undefined;
|
|
93
|
+
const revision = typeof override === "string" ? override : entry.revision;
|
|
94
|
+
builds.push({ directory: `${name.replaceAll("-", "_")}-${revision}`, executables });
|
|
95
|
+
}
|
|
96
|
+
return builds.length > 0 ? { version, playwrightCoreDirectory: path.dirname(manifestPath), builds } : null;
|
|
97
|
+
}
|
|
98
|
+
async function resolvePlaywrightCoreManifest(realWorktree, configDirectory) {
|
|
99
|
+
// Follow the chain the project's test runner resolves first: pnpm does not
|
|
100
|
+
// hoist playwright-core next to @playwright/test, and a hoisted bare
|
|
101
|
+
// playwright-core may be a different version than the runner's own.
|
|
102
|
+
for (const chain of [
|
|
103
|
+
["@playwright/test", "playwright", "playwright-core"],
|
|
104
|
+
["playwright", "playwright-core"],
|
|
105
|
+
["playwright-core"],
|
|
106
|
+
]) {
|
|
107
|
+
let from = path.join(configDirectory, "package.json");
|
|
108
|
+
try {
|
|
109
|
+
for (const packageName of chain)
|
|
110
|
+
from = createRequire(from).resolve(`${packageName}/package.json`);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// A Playwright found above the worktree is not the project's own pin.
|
|
116
|
+
const realManifest = await fs.realpath(from).catch(() => null);
|
|
117
|
+
if (realManifest && isContained(realWorktree, realManifest))
|
|
118
|
+
return realManifest;
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
async function hasPinnedChromiumBuilds(cachePath, builds) {
|
|
123
|
+
for (const build of builds) {
|
|
124
|
+
if (!await anyFile(build.executables.map(executable => path.join(cachePath, build.directory, executable))))
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
async function hasChromiumExecutable(cachePath) {
|
|
130
|
+
let entries;
|
|
131
|
+
try {
|
|
132
|
+
entries = await fs.readdir(cachePath, { withFileTypes: true });
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
const executableSuffixes = platformExecutables(chromiumExecutables);
|
|
138
|
+
for (const entry of entries) {
|
|
139
|
+
if (!entry.isDirectory() || !entry.name.startsWith("chromium-"))
|
|
140
|
+
continue;
|
|
141
|
+
if (await anyFile(executableSuffixes.map(suffix => path.join(cachePath, entry.name, suffix))))
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
async function anyFile(candidates) {
|
|
147
|
+
for (const candidate of candidates) {
|
|
148
|
+
try {
|
|
149
|
+
if ((await fs.stat(candidate)).isFile())
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// Continue through known Playwright layouts.
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
function isContained(parent, child) {
|
|
159
|
+
const relative = path.relative(parent, child);
|
|
160
|
+
return Boolean(relative) && !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
161
|
+
}
|
|
162
|
+
const defaultHostProbe = {
|
|
163
|
+
env: process.env,
|
|
164
|
+
platform: process.platform,
|
|
165
|
+
release: os.release(),
|
|
166
|
+
arch: os.arch(),
|
|
167
|
+
appleCpu: os.cpus().some(cpu => cpu.model.includes("Apple")),
|
|
168
|
+
osRelease: () => {
|
|
169
|
+
try {
|
|
170
|
+
return readFileSync("/etc/os-release", "utf8");
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
/** Mirrors Playwright's own host platform key, which selects `revisionOverrides` entries. */
|
|
178
|
+
export function playwrightHostPlatform(probe = defaultHostProbe) {
|
|
179
|
+
if (probe.env.PLAYWRIGHT_HOST_PLATFORM_OVERRIDE)
|
|
180
|
+
return probe.env.PLAYWRIGHT_HOST_PLATFORM_OVERRIDE;
|
|
181
|
+
if (probe.platform === "win32")
|
|
182
|
+
return "win64";
|
|
183
|
+
if (probe.platform === "darwin") {
|
|
184
|
+
const major = Number.parseInt(probe.release.split(".")[0] ?? "", 10);
|
|
185
|
+
if (major < 18)
|
|
186
|
+
return "mac10.13";
|
|
187
|
+
if (major === 18)
|
|
188
|
+
return "mac10.14";
|
|
189
|
+
if (major === 19)
|
|
190
|
+
return "mac10.15";
|
|
191
|
+
const macVersion = major < 25 ? major - 9 : Math.min(major + 1, 26);
|
|
192
|
+
return `mac${macVersion}${probe.appleCpu ? "-arm64" : ""}`;
|
|
193
|
+
}
|
|
194
|
+
if (probe.platform !== "linux" || !["x64", "arm64"].includes(probe.arch))
|
|
195
|
+
return "<unknown>";
|
|
196
|
+
const suffix = `-${probe.arch}`;
|
|
197
|
+
const fields = new Map((probe.osRelease() ?? "").split("\n").map(line => {
|
|
198
|
+
const [name = "", ...rest] = line.split("=");
|
|
199
|
+
return [name.toLowerCase(), rest.join("=").trim().replace(/^"(.*)"$/u, "$1")];
|
|
200
|
+
}));
|
|
201
|
+
const id = fields.get("id") ?? "";
|
|
202
|
+
const version = fields.get("version_id") ?? "";
|
|
203
|
+
const major = Number.parseInt(version, 10);
|
|
204
|
+
if (["ubuntu", "pop", "neon", "tuxedo"].includes(id)) {
|
|
205
|
+
if (major < 20)
|
|
206
|
+
return `ubuntu18.04${suffix}`;
|
|
207
|
+
if (major < 22)
|
|
208
|
+
return `ubuntu20.04${suffix}`;
|
|
209
|
+
if (major < 24)
|
|
210
|
+
return `ubuntu22.04${suffix}`;
|
|
211
|
+
if (major < 26)
|
|
212
|
+
return `ubuntu24.04${suffix}`;
|
|
213
|
+
return `ubuntu${version}${suffix}`;
|
|
214
|
+
}
|
|
215
|
+
if (id === "linuxmint") {
|
|
216
|
+
if (major <= 20)
|
|
217
|
+
return `ubuntu20.04${suffix}`;
|
|
218
|
+
return major === 21 ? `ubuntu22.04${suffix}` : `ubuntu24.04${suffix}`;
|
|
219
|
+
}
|
|
220
|
+
if (id === "debian" || id === "raspbian") {
|
|
221
|
+
if (["11", "12", "13"].includes(version))
|
|
222
|
+
return `debian${version}${suffix}`;
|
|
223
|
+
if (version === "")
|
|
224
|
+
return `debian13${suffix}`;
|
|
225
|
+
}
|
|
226
|
+
return `ubuntu24.04${suffix}`;
|
|
227
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { HostRuntimePlaywrightConfig } from "./host-runtime-contract.js";
|
|
2
|
+
import { type PlaywrightBrowserCacheCandidates } from "./playwright-browser-cache.js";
|
|
2
3
|
export interface PlaywrightHostRuntimeContext {
|
|
3
4
|
readonly worktreePath: string;
|
|
4
5
|
readonly temporaryDirectory: string;
|
|
@@ -17,5 +18,15 @@ export interface PreparedPlaywrightHostRuntime {
|
|
|
17
18
|
* Playwright-managed web servers, and keeps writable artifacts attempt-local.
|
|
18
19
|
*/
|
|
19
20
|
export declare class PlaywrightHostRuntimeAdapter {
|
|
21
|
+
private readonly browserCacheCandidates;
|
|
22
|
+
private readonly hostPlatform;
|
|
23
|
+
constructor(browserCacheCandidates?: PlaywrightBrowserCacheCandidates, hostPlatform?: () => string);
|
|
24
|
+
/**
|
|
25
|
+
* Fails a host that lacks the project's pinned Chromium builds before any
|
|
26
|
+
* database, build, launch, or promotion work, so the attempt never runs
|
|
27
|
+
* toward a Playwright gate whose every spec would fail at browser launch.
|
|
28
|
+
*/
|
|
29
|
+
assertBrowserCacheReady(config: HostRuntimePlaywrightConfig, worktreePath: string): Promise<void>;
|
|
30
|
+
private readyBrowserCache;
|
|
20
31
|
prepare(config: HostRuntimePlaywrightConfig, context: PlaywrightHostRuntimeContext): Promise<PreparedPlaywrightHostRuntime>;
|
|
21
32
|
}
|