@loucompanion/forge-bridge 0.1.4-dev.63db04bcffca → 0.1.4-dev.724427ea7be8
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/README.md +144 -3
- package/dist/bridge/bridge-chat-provider-command.d.ts +19 -0
- package/dist/bridge/bridge-chat-provider-command.js +132 -0
- package/dist/bridge/bridge-chat.types.d.ts +10 -0
- package/dist/bridge/bridge-chat.types.js +16 -1
- package/dist/bridge/bridge-state-file.d.ts +161 -0
- package/dist/bridge/bridge-state-file.js +376 -0
- package/dist/bridge/bridge-state-io.d.ts +11 -0
- package/dist/bridge/bridge-state-io.js +50 -0
- package/dist/bridge/bridge-state-lock.d.ts +11 -0
- package/dist/bridge/bridge-state-lock.js +102 -0
- package/dist/bridge/bridge-state-unpublished.d.ts +17 -0
- package/dist/bridge/bridge-state-unpublished.js +111 -0
- package/dist/bridge/bridge.base.d.ts +9 -0
- package/dist/bridge/bridge.service.d.ts +101 -15
- package/dist/bridge/bridge.service.js +472 -345
- package/dist/bridge/bridge.types.d.ts +27 -0
- package/dist/bridge/dispatch-result-outbox.d.ts +11 -1
- package/dist/bridge/dispatch-result-outbox.js +31 -6
- package/dist/bridge/in-flight-registry.d.ts +62 -0
- package/dist/bridge/in-flight-registry.js +207 -0
- package/dist/bridge/local-yield-flag.d.ts +6 -0
- package/dist/bridge/local-yield-flag.js +34 -0
- package/dist/bridge/readiness-result-outbox.d.ts +5 -1
- package/dist/bridge/readiness-result-outbox.js +20 -0
- package/dist/bridge/runner-host-capabilities.d.ts +5 -0
- package/dist/bridge/runner-host-capabilities.js +17 -0
- package/dist/bridge/sandbox-resource-accounting.d.ts +16 -0
- package/dist/bridge/sandbox-resource-accounting.js +150 -0
- package/dist/bridge/test-postgres-capability.d.ts +14 -0
- package/dist/bridge/test-postgres-capability.js +120 -0
- package/dist/cleanup/cleanup-wip.d.ts +12 -0
- package/dist/cleanup/cleanup-wip.js +66 -0
- package/dist/cleanup/cleanup.service.d.ts +20 -1
- package/dist/cleanup/cleanup.service.js +150 -169
- package/dist/cleanup/cleanup.types.d.ts +6 -0
- package/dist/cleanup/forge-owned-paths.d.ts +9 -0
- package/dist/cleanup/forge-owned-paths.js +53 -0
- package/dist/cleanup/host-runtime-ledger.d.ts +7 -0
- package/dist/cleanup/host-runtime-ledger.js +130 -0
- package/dist/cleanup/managed-branch-sweep.d.ts +27 -0
- package/dist/cleanup/managed-branch-sweep.js +111 -0
- package/dist/cli/cli.help.js +74 -12
- package/dist/cli/cli.service.d.ts +19 -10
- package/dist/cli/cli.service.js +152 -187
- package/dist/cli/idle-status.d.ts +84 -0
- package/dist/cli/idle-status.js +181 -0
- package/dist/cli/local-config.d.ts +6 -0
- package/dist/cli/local-config.js +32 -4
- package/dist/cli/provider-cli-detection.d.ts +10 -0
- package/dist/cli/provider-cli-detection.js +160 -0
- package/dist/runtime/aspnet-runtime-prebuild.d.ts +20 -0
- package/dist/runtime/aspnet-runtime-prebuild.js +184 -18
- package/dist/runtime/attempt-database-broker.d.ts +1 -1
- package/dist/runtime/attempt-node-runtime-launcher.js +9 -0
- package/dist/runtime/attempt-process-supervisor.d.ts +21 -0
- package/dist/runtime/attempt-process-supervisor.js +62 -6
- package/dist/runtime/docker-engine-probe.d.ts +5 -0
- package/dist/runtime/docker-engine-probe.js +7 -0
- package/dist/runtime/dotnet-build-outputs.d.ts +107 -0
- package/dist/runtime/dotnet-build-outputs.js +402 -0
- package/dist/runtime/ephemeral-test-postgres-grant.d.ts +37 -0
- package/dist/runtime/ephemeral-test-postgres-grant.js +85 -0
- package/dist/runtime/ephemeral-test-postgres.d.ts +57 -0
- package/dist/runtime/ephemeral-test-postgres.js +303 -0
- 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 +36 -0
- package/dist/runtime/host-facts.js +95 -0
- package/dist/runtime/host-load-admission.d.ts +67 -0
- package/dist/runtime/host-load-admission.js +99 -0
- package/dist/runtime/host-load-child-environment.d.ts +27 -0
- package/dist/runtime/host-load-child-environment.js +76 -0
- package/dist/runtime/host-load-forge-cgroups.d.ts +49 -0
- package/dist/runtime/host-load-forge-cgroups.js +140 -0
- package/dist/runtime/host-load-governor-config.d.ts +52 -0
- package/dist/runtime/host-load-governor-config.js +83 -0
- package/dist/runtime/host-load-governor.d.ts +186 -0
- package/dist/runtime/host-load-governor.js +632 -0
- package/dist/runtime/host-load-ledger-lock.d.ts +69 -0
- package/dist/runtime/host-load-ledger-lock.js +187 -0
- package/dist/runtime/host-load-runtime-settings.d.ts +25 -0
- package/dist/runtime/host-load-runtime-settings.js +80 -0
- package/dist/runtime/host-load-sampler.d.ts +156 -0
- package/dist/runtime/host-load-sampler.js +298 -0
- package/dist/runtime/host-load-step-class.d.ts +70 -0
- package/dist/runtime/host-load-step-class.js +161 -0
- package/dist/runtime/host-load-step-reaper.d.ts +27 -0
- package/dist/runtime/host-load-step-reaper.js +95 -0
- package/dist/runtime/host-load-step-run.d.ts +73 -0
- package/dist/runtime/host-load-step-run.js +102 -0
- package/dist/runtime/host-load-test-slot.d.ts +43 -0
- package/dist/runtime/host-load-test-slot.js +81 -0
- package/dist/runtime/host-load-token-ledger.d.ts +200 -0
- package/dist/runtime/host-load-token-ledger.js +453 -0
- package/dist/runtime/host-load-windows-cpu.d.ts +34 -0
- package/dist/runtime/host-load-windows-cpu.js +85 -0
- package/dist/runtime/host-pressure-stall.d.ts +61 -0
- package/dist/runtime/host-pressure-stall.js +168 -0
- package/dist/runtime/host-runtime-contract.d.ts +16 -0
- package/dist/runtime/host-runtime-contract.js +35 -2
- package/dist/runtime/host-runtime-lease.d.ts +76 -5
- package/dist/runtime/host-runtime-lease.js +322 -129
- package/dist/runtime/host-runtime-ledger-recovery.d.ts +20 -0
- package/dist/runtime/host-runtime-ledger-recovery.js +134 -0
- package/dist/runtime/host-runtime-promotion.d.ts +23 -0
- package/dist/runtime/host-runtime-promotion.js +48 -0
- package/dist/runtime/host-runtime-requirements-check.d.ts +17 -0
- package/dist/runtime/host-runtime-requirements-check.js +29 -0
- package/dist/runtime/host-runtime-requirements.d.ts +36 -0
- package/dist/runtime/host-runtime-requirements.js +117 -0
- package/dist/runtime/managed-command-teardown.d.ts +62 -0
- package/dist/runtime/managed-command-teardown.js +596 -0
- package/dist/runtime/native-test-postgres-fallback.d.ts +51 -7
- package/dist/runtime/native-test-postgres-fallback.js +60 -8
- 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 +18 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +61 -55
- package/dist/runtime/runner-settings.d.ts +104 -0
- package/dist/runtime/runner-settings.js +236 -0
- package/dist/runtime/supervised-runtime-build-guard.d.ts +45 -0
- package/dist/runtime/supervised-runtime-build-guard.js +112 -0
- package/dist/sandbox/host-capacity-broker.d.ts +115 -0
- package/dist/sandbox/host-capacity-broker.js +336 -0
- package/dist/sandbox/managed-branch.d.ts +13 -0
- package/dist/sandbox/managed-branch.js +90 -0
- package/dist/sandbox/provider-auth/codex.js +11 -11
- package/dist/sandbox/runtime.d.ts +10 -1
- package/dist/sandbox/runtime.js +47 -8
- package/dist/sandbox/sandbox-cgroups.d.ts +14 -0
- package/dist/sandbox/sandbox-cgroups.js +42 -0
- package/dist/session/architect-waits.d.ts +2 -0
- package/dist/session/architect-waits.js +138 -0
- package/dist/session/managed-attempt-tools.d.ts +27 -7
- package/dist/session/managed-attempt-tools.js +406 -123
- package/dist/session/managed-build-coordinator.d.ts +17 -1
- package/dist/session/managed-build-coordinator.js +14 -1
- package/dist/session/managed-build-receipt.d.ts +5 -0
- package/dist/session/managed-build-receipt.js +8 -3
- package/dist/session/managed-command-host-load.d.ts +47 -0
- package/dist/session/managed-command-host-load.js +83 -0
- package/dist/session/mcp-argument-coercion.d.ts +23 -0
- package/dist/session/mcp-argument-coercion.js +121 -0
- package/dist/session/phase-completion.artifacts.d.ts +7 -0
- package/dist/session/phase-completion.artifacts.js +100 -26
- package/dist/session/phase-completion.command-validation.d.ts +7 -0
- package/dist/session/phase-completion.command-validation.js +77 -0
- package/dist/session/phase-completion.d.ts +12 -0
- package/dist/session/phase-completion.helpers.js +13 -12
- package/dist/session/phase-completion.js +5 -0
- package/dist/session/phase-completion.mcp-args.d.ts +9 -0
- package/dist/session/phase-completion.mcp-args.js +156 -0
- package/dist/session/phase-completion.prompts.js +6 -1
- package/dist/session/phase-completion.runtime.d.ts +5 -11
- package/dist/session/phase-completion.runtime.js +111 -230
- package/dist/session/phase-completion.schema-parts.d.ts +19 -0
- package/dist/session/phase-completion.schema-parts.js +188 -0
- package/dist/session/phase-completion.schemas.d.ts +3 -3
- package/dist/session/phase-completion.schemas.js +208 -231
- package/dist/session/phase-completion.validation.d.ts +2 -3
- package/dist/session/phase-completion.validation.js +49 -93
- package/dist/session/pre-publication-host-load.d.ts +27 -0
- package/dist/session/pre-publication-host-load.js +111 -0
- package/dist/session/pre-publication-native-postgres.d.ts +30 -0
- package/dist/session/pre-publication-native-postgres.js +111 -0
- package/dist/session/pre-publication-pass.d.ts +61 -0
- package/dist/session/pre-publication-pass.js +148 -0
- package/dist/session/pre-publication-process.d.ts +23 -0
- package/dist/session/pre-publication-process.js +109 -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-step-execution.d.ts +12 -0
- package/dist/session/pre-publication-step-execution.js +50 -0
- package/dist/session/pre-publication-validation.d.ts +37 -21
- package/dist/session/pre-publication-validation.js +228 -219
- package/dist/session/provider-cli/claude-background-work.d.ts +67 -0
- package/dist/session/provider-cli/claude-background-work.js +181 -0
- package/dist/session/provider-cli/host-build-lock.d.ts +18 -0
- package/dist/session/provider-cli/host-build-lock.js +30 -0
- package/dist/session/provider-cli/pre-publication-repair.d.ts +56 -0
- package/dist/session/provider-cli/pre-publication-repair.js +276 -0
- package/dist/session/provider-cli/provider-cli.artifacts.d.ts +2 -1
- package/dist/session/provider-cli/provider-cli.artifacts.js +21 -15
- package/dist/session/provider-cli/provider-cli.child-environment.d.ts +7 -0
- package/dist/session/provider-cli/provider-cli.child-environment.js +75 -0
- package/dist/session/provider-cli/provider-cli.codex-models-cache.d.ts +3 -0
- package/dist/session/provider-cli/provider-cli.codex-models-cache.js +37 -0
- package/dist/session/provider-cli/provider-cli.merge-integrity.d.ts +89 -0
- package/dist/session/provider-cli/provider-cli.merge-integrity.js +310 -0
- package/dist/session/provider-cli/provider-cli.process.d.ts +46 -31
- package/dist/session/provider-cli/provider-cli.process.js +286 -403
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.prompts.js +87 -32
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +8 -0
- package/dist/session/provider-cli/provider-cli.runtime.js +29 -1
- package/dist/session/provider-cli/provider-cli.service.d.ts +7 -1
- package/dist/session/provider-cli/provider-cli.service.js +207 -192
- package/dist/session/provider-cli/provider-cli.types.d.ts +30 -0
- package/dist/session/provider-cli/provider-cli.windows-environment.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.windows-environment.js +151 -0
- package/dist/session/provider-cli/provider-cli.windows-launch.d.ts +23 -0
- package/dist/session/provider-cli/provider-cli.windows-launch.js +131 -0
- package/dist/session/provider-cli/provider-in-flight-work.d.ts +10 -0
- package/dist/session/provider-cli/provider-in-flight-work.js +92 -0
- package/dist/session/provider-cli/provider-output-pipes.d.ts +30 -0
- package/dist/session/provider-cli/provider-output-pipes.js +67 -0
- package/dist/session/provider-cli/provider-output-state.d.ts +8 -0
- package/dist/session/provider-cli/provider-output-state.js +15 -0
- package/dist/session/provider-cli/provider-user-config-isolation.d.ts +69 -0
- package/dist/session/provider-cli/provider-user-config-isolation.js +164 -0
- package/dist/session/publishable-worktree-changes.d.ts +54 -0
- package/dist/session/publishable-worktree-changes.js +193 -0
- package/dist/session/qa-sidecar-validation.d.ts +6 -0
- package/dist/session/qa-sidecar-validation.js +28 -0
- package/dist/session/sandbox-host-capacity-gate.d.ts +51 -0
- package/dist/session/sandbox-host-capacity-gate.js +257 -0
- package/dist/session/session-redaction.js +31 -181
- package/dist/session/session.base.d.ts +9 -1
- package/dist/session/session.service.d.ts +11 -2
- package/dist/session/session.service.js +189 -69
- package/dist/session/session.types.d.ts +3 -0
- package/dist/session/session.types.js +15 -3
- package/dist/session/sidecar-json-shape.d.ts +31 -0
- package/dist/session/sidecar-json-shape.js +217 -0
- package/dist/session/workspace-runtime-fingerprint.d.ts +32 -0
- package/dist/session/workspace-runtime-fingerprint.js +138 -0
- package/dist/shared/fs/file-identity.d.ts +10 -0
- package/dist/shared/fs/file-identity.js +15 -0
- package/dist/shared/fs/replace-file.d.ts +7 -0
- package/dist/shared/fs/replace-file.js +33 -0
- package/dist/shared/process/host-process-recovery.d.ts +14 -3
- package/dist/shared/process/host-process-recovery.js +23 -3
- package/dist/shared/process/host-process-supervisor.d.ts +14 -1
- package/dist/shared/process/host-process-supervisor.js +67 -15
- package/dist/shared/process/process-tree.d.ts +52 -0
- package/dist/shared/process/process-tree.js +144 -0
- package/dist/shared/process/process.d.ts +67 -0
- package/dist/shared/process/process.js +213 -11
- package/dist/shared/process/windows-app-execution-alias.d.ts +19 -0
- package/dist/shared/process/windows-app-execution-alias.js +45 -0
- package/dist/shared/process/windows-launch.d.ts +18 -0
- package/dist/shared/process/windows-launch.js +68 -0
- package/dist/shared/provider-capabilities.js +14 -7
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/dependency-warmer.d.ts +25 -0
- package/dist/worktree/dependency-warmer.js +46 -10
- package/dist/worktree/orphan-relink.d.ts +19 -0
- package/dist/worktree/orphan-relink.js +106 -0
- package/dist/worktree/wip-git.d.ts +33 -0
- package/dist/worktree/wip-git.js +122 -0
- package/dist/worktree/wip-refs.d.ts +62 -0
- package/dist/worktree/wip-refs.js +119 -0
- package/dist/worktree/wip-restore.d.ts +38 -0
- package/dist/worktree/wip-restore.js +190 -0
- package/dist/worktree/wip-snapshot.d.ts +50 -0
- package/dist/worktree/wip-snapshot.js +268 -0
- package/dist/worktree/worktree-inputs.d.ts +8 -0
- package/dist/worktree/worktree-inputs.js +134 -0
- package/dist/worktree/worktree-origin-sync.d.ts +34 -0
- package/dist/worktree/worktree-origin-sync.js +127 -0
- package/dist/worktree/worktree-wip-events.d.ts +18 -0
- package/dist/worktree/worktree-wip-events.js +98 -0
- package/dist/worktree/worktree-wip.d.ts +50 -0
- package/dist/worktree/worktree-wip.js +185 -0
- package/dist/worktree/worktree.base.d.ts +7 -1
- package/dist/worktree/worktree.service.d.ts +36 -5
- package/dist/worktree/worktree.service.js +124 -155
- package/dist/worktree/worktree.types.d.ts +38 -0
- package/package.json +2 -2
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { defaultHostEnvironment, detectHostFacts, readOptional, } from "./host-facts.js";
|
|
3
|
+
import { cgroupLeafName, isDedicatedBridgeUnit, parseCgroupCpuUsageUsec, parseCgroupMemoryAnonBytes, parseMemAvailableBytes, parseNonNegativeInteger, parsePressureStall, parseProcStatCpuTotals, parseUnifiedCgroupMembership, parseUnifiedCgroupPath, } from "./host-pressure-stall.js";
|
|
4
|
+
import { ForgeCgroupCpuMeter, readCgroupMemoryHeadroomBytes, } from "./host-load-forge-cgroups.js";
|
|
5
|
+
import { cpuTimeTotals, cpuUtilisation, WindowsCpuPressureEstimator, } from "./host-load-windows-cpu.js";
|
|
6
|
+
export const defaultHostLoadSampleIntervalMs = 5_000;
|
|
7
|
+
/** Samples kept for the governor's pressure streak (FORGE_HOST_LOAD_BACKPRESSURE_SAMPLES is at most 60). */
|
|
8
|
+
const retainedSampleCount = 64;
|
|
9
|
+
/**
|
|
10
|
+
* Samples Linux PSI and the bridge's own cgroup on an interval and caches the
|
|
11
|
+
* latest snapshot. Every read is async and failure-tolerant: a Linux host without
|
|
12
|
+
* PSI (old kernel, `psi=0`) or another non-Windows host reports `psiAvailable: false`
|
|
13
|
+
* and no pressure. Windows reports a CPU pressure estimate instead (decision 0021).
|
|
14
|
+
*/
|
|
15
|
+
export class HostLoadSampler {
|
|
16
|
+
environment;
|
|
17
|
+
intervalMs;
|
|
18
|
+
now;
|
|
19
|
+
monotonicNow;
|
|
20
|
+
cgroupUnit;
|
|
21
|
+
generation = 0;
|
|
22
|
+
timer = null;
|
|
23
|
+
starting = null;
|
|
24
|
+
inFlight = null;
|
|
25
|
+
hostFacts = null;
|
|
26
|
+
latest = null;
|
|
27
|
+
history = [];
|
|
28
|
+
cpuMark = null;
|
|
29
|
+
hostCpuMark = null;
|
|
30
|
+
latestCpuAttribution = null;
|
|
31
|
+
latestMemoryHeadroom = null;
|
|
32
|
+
windowsCpuMark = null;
|
|
33
|
+
windowsCpuPressure = new WindowsCpuPressureEstimator();
|
|
34
|
+
forgeCgroups;
|
|
35
|
+
sandboxCpu;
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
this.environment = options.environment ?? defaultHostEnvironment();
|
|
38
|
+
this.intervalMs = options.intervalMs !== undefined && Number.isFinite(options.intervalMs) && options.intervalMs > 0
|
|
39
|
+
? options.intervalMs
|
|
40
|
+
: defaultHostLoadSampleIntervalMs;
|
|
41
|
+
this.now = options.now ?? Date.now;
|
|
42
|
+
this.monotonicNow = options.monotonicNow ?? (() => performance.now());
|
|
43
|
+
this.cgroupUnit = options.cgroupUnit !== undefined
|
|
44
|
+
? options.cgroupUnit
|
|
45
|
+
: process.env.FORGE_BRIDGE_CGROUP_UNIT?.trim() || null;
|
|
46
|
+
this.forgeCgroups = options.forgeCgroups ?? null;
|
|
47
|
+
this.sandboxCpu = new ForgeCgroupCpuMeter(this.environment);
|
|
48
|
+
}
|
|
49
|
+
facts() {
|
|
50
|
+
return this.hostFacts;
|
|
51
|
+
}
|
|
52
|
+
current() {
|
|
53
|
+
return this.latest;
|
|
54
|
+
}
|
|
55
|
+
cpuAttribution() {
|
|
56
|
+
return this.latestCpuAttribution;
|
|
57
|
+
}
|
|
58
|
+
cgroupMemoryHeadroomBytes() {
|
|
59
|
+
return this.latestMemoryHeadroom;
|
|
60
|
+
}
|
|
61
|
+
recent() {
|
|
62
|
+
return [...this.history];
|
|
63
|
+
}
|
|
64
|
+
/** Detects static facts once, takes a first sample, then samples on the interval. Idempotent. */
|
|
65
|
+
start() {
|
|
66
|
+
if (this.timer)
|
|
67
|
+
return Promise.resolve();
|
|
68
|
+
this.starting ??= this.startOnce().finally(() => {
|
|
69
|
+
this.starting = null;
|
|
70
|
+
});
|
|
71
|
+
return this.starting;
|
|
72
|
+
}
|
|
73
|
+
stop() {
|
|
74
|
+
// Invalidates an in-progress start() so it cannot arm the interval afterwards.
|
|
75
|
+
this.generation += 1;
|
|
76
|
+
if (this.timer)
|
|
77
|
+
clearInterval(this.timer);
|
|
78
|
+
this.timer = null;
|
|
79
|
+
}
|
|
80
|
+
/** Takes one sample now and caches it. Concurrent callers share one read. */
|
|
81
|
+
sample() {
|
|
82
|
+
this.inFlight ??= this.readSnapshot()
|
|
83
|
+
.then(snapshot => {
|
|
84
|
+
// A Windows reading that did not advance the CPU window returns the last snapshot.
|
|
85
|
+
if (snapshot === this.latest)
|
|
86
|
+
return snapshot;
|
|
87
|
+
this.latest = snapshot;
|
|
88
|
+
this.history.push(snapshot);
|
|
89
|
+
if (this.history.length > retainedSampleCount)
|
|
90
|
+
this.history.splice(0, this.history.length - retainedSampleCount);
|
|
91
|
+
return snapshot;
|
|
92
|
+
})
|
|
93
|
+
.finally(() => {
|
|
94
|
+
this.inFlight = null;
|
|
95
|
+
});
|
|
96
|
+
return this.inFlight;
|
|
97
|
+
}
|
|
98
|
+
async startOnce() {
|
|
99
|
+
const generation = this.generation;
|
|
100
|
+
if (!this.hostFacts) {
|
|
101
|
+
this.hostFacts = await detectHostFacts(this.environment).catch(() => null);
|
|
102
|
+
}
|
|
103
|
+
await this.sample().catch(() => undefined);
|
|
104
|
+
if (this.timer || generation !== this.generation)
|
|
105
|
+
return;
|
|
106
|
+
this.timer = setInterval(() => {
|
|
107
|
+
void this.sample().catch(() => undefined);
|
|
108
|
+
}, this.intervalMs);
|
|
109
|
+
this.timer.unref?.();
|
|
110
|
+
}
|
|
111
|
+
async readSnapshot() {
|
|
112
|
+
const sampledAtMs = this.now();
|
|
113
|
+
const monotonicMs = this.monotonicNow();
|
|
114
|
+
if (this.environment.platform === "win32")
|
|
115
|
+
return this.readWindowsSnapshot(sampledAtMs, monotonicMs);
|
|
116
|
+
const linux = this.environment.platform === "linux";
|
|
117
|
+
const [systemPressure, ownCgroup, memoryAvailableBytes, hostCpu, cgroupMemoryHeadroomBytes, sandboxCores] = linux
|
|
118
|
+
? await Promise.all([
|
|
119
|
+
this.readSystemPressure(),
|
|
120
|
+
this.readOwnCgroup(monotonicMs),
|
|
121
|
+
this.readMemoryAvailable(),
|
|
122
|
+
this.readHostCpuUse(),
|
|
123
|
+
this.readCgroupMemoryHeadroom(),
|
|
124
|
+
Promise.resolve(this.forgeCgroups?.paths() ?? [])
|
|
125
|
+
.then(paths => this.sandboxCpu.coresInUse(paths, monotonicMs)).catch(() => 0),
|
|
126
|
+
])
|
|
127
|
+
: [null, null, this.fallbackFreeMemory(), null, null, 0];
|
|
128
|
+
const sampledAt = new Date(sampledAtMs).toISOString();
|
|
129
|
+
// Docker sandbox CPU is Forge's own work, already counted through reserved cost.
|
|
130
|
+
const ownCores = ownCgroup?.cpuCoresInUse ?? null;
|
|
131
|
+
this.latestMemoryHeadroom = cgroupMemoryHeadroomBytes;
|
|
132
|
+
this.latestCpuAttribution = hostCpuAttribution(sampledAt, hostCpu === null ? null : roundCores(hostCpu.utilisation * hostCpu.cpuCount), ownCores === null ? null : ownCores + sandboxCores);
|
|
133
|
+
return {
|
|
134
|
+
sampledAt,
|
|
135
|
+
psiAvailable: systemPressure !== null,
|
|
136
|
+
pressureSource: systemPressure !== null ? "psi" : null,
|
|
137
|
+
systemPressure,
|
|
138
|
+
ownCgroupDedicated: ownCgroup !== null,
|
|
139
|
+
ownCgroup,
|
|
140
|
+
memoryAvailableBytes,
|
|
141
|
+
hostCpuPercent: hostCpu === null ? null : hostCpuPercent(hostCpu.utilisation),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Decision 0021: Windows has no PSI or cgroups. CPU pressure is estimated from
|
|
146
|
+
* whole-host CPU use between samples, and memory is available physical memory.
|
|
147
|
+
* The bridge's own share is not attributed, so external load stays unknown and
|
|
148
|
+
* Forge's work counts through reserved cost and the pressure gate (as on a Linux
|
|
149
|
+
* host whose bridge has no dedicated cgroup).
|
|
150
|
+
*/
|
|
151
|
+
readWindowsSnapshot(sampledAtMs, monotonicMs) {
|
|
152
|
+
let totals;
|
|
153
|
+
try {
|
|
154
|
+
totals = cpuTimeTotals(this.environment.cpus());
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
totals = null;
|
|
158
|
+
}
|
|
159
|
+
const previous = this.windowsCpuMark;
|
|
160
|
+
const sampledAt = new Date(sampledAtMs).toISOString();
|
|
161
|
+
if (this.latest && totals && previous && previous.totals.cpuCount === totals.cpuCount
|
|
162
|
+
&& monotonicMs - previous.atMs < minimumWindowsCpuWindowMs) {
|
|
163
|
+
// Too soon after the last reading (start() right after a sample) for a stable delta:
|
|
164
|
+
// the window stays open and the last snapshot is returned as is. sample() does not
|
|
165
|
+
// record it again, so it never counts as another sample in the pressure streak.
|
|
166
|
+
return this.latest;
|
|
167
|
+
}
|
|
168
|
+
this.windowsCpuMark = totals ? { totals, atMs: monotonicMs } : null;
|
|
169
|
+
const elapsedMs = previous ? monotonicMs - previous.atMs : 0;
|
|
170
|
+
const utilisation = previous && elapsedMs > 0 ? cpuUtilisation(previous.totals, totals) : null;
|
|
171
|
+
// The first sample, or a break in readings (no times, a CPU count change, a counter
|
|
172
|
+
// reset), reports no pressure and restarts the averages from the next window.
|
|
173
|
+
if (utilisation === null)
|
|
174
|
+
this.windowsCpuPressure.reset();
|
|
175
|
+
const cpu = utilisation === null ? null : this.windowsCpuPressure.update(utilisation, elapsedMs);
|
|
176
|
+
this.latestMemoryHeadroom = null;
|
|
177
|
+
this.latestCpuAttribution = hostCpuAttribution(sampledAt, utilisation !== null && totals ? roundCores(utilisation * totals.cpuCount) : null, null);
|
|
178
|
+
return windowsSnapshot(sampledAt, cpu, this.fallbackFreeMemory(), utilisation === null ? null : hostCpuPercent(utilisation));
|
|
179
|
+
}
|
|
180
|
+
async readCgroupMemoryHeadroom() {
|
|
181
|
+
const membership = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "self", "cgroup"));
|
|
182
|
+
const cgroupPath = membership ? parseUnifiedCgroupMembership(membership) : null;
|
|
183
|
+
return cgroupPath ? await readCgroupMemoryHeadroomBytes(this.environment, cgroupPath).catch(() => null) : null;
|
|
184
|
+
}
|
|
185
|
+
async readSystemPressure() {
|
|
186
|
+
const pressureRoot = path.posix.join(this.environment.procRoot, "pressure");
|
|
187
|
+
const [cpu, memory, io] = await Promise.all(["cpu", "memory", "io"].map(resource => this.readPressure(path.posix.join(pressureRoot, resource))));
|
|
188
|
+
// CPU pressure is the governor's primary signal; without it PSI is unavailable.
|
|
189
|
+
return cpu ? { cpu, memory: memory ?? null, io: io ?? null } : null;
|
|
190
|
+
}
|
|
191
|
+
async readOwnCgroup(monotonicMs) {
|
|
192
|
+
const membership = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "self", "cgroup"));
|
|
193
|
+
const cgroupPath = membership ? parseUnifiedCgroupPath(membership) : null;
|
|
194
|
+
const unit = cgroupPath ? cgroupLeafName(cgroupPath) : null;
|
|
195
|
+
if (!cgroupPath || !unit || !isDedicatedBridgeUnit(unit, this.cgroupUnit)) {
|
|
196
|
+
this.cpuMark = null;
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
const directory = path.posix.join(this.environment.cgroupRoot, cgroupPath);
|
|
200
|
+
if (!directory.startsWith(`${this.environment.cgroupRoot.replace(/\/+$/u, "")}/`))
|
|
201
|
+
return null;
|
|
202
|
+
const [cpuPressure, memoryPressure, cpuStat, memoryStat, memoryCurrent] = await Promise.all([
|
|
203
|
+
this.readPressure(path.posix.join(directory, "cpu.pressure")),
|
|
204
|
+
this.readPressure(path.posix.join(directory, "memory.pressure")),
|
|
205
|
+
readOptional(this.environment, path.posix.join(directory, "cpu.stat")),
|
|
206
|
+
readOptional(this.environment, path.posix.join(directory, "memory.stat")),
|
|
207
|
+
readOptional(this.environment, path.posix.join(directory, "memory.current")),
|
|
208
|
+
]);
|
|
209
|
+
const usageUsec = cpuStat ? parseCgroupCpuUsageUsec(cpuStat) : null;
|
|
210
|
+
const memoryAnonBytes = memoryStat ? parseCgroupMemoryAnonBytes(memoryStat) : null;
|
|
211
|
+
const memoryChargedBytes = memoryCurrent ? parseNonNegativeInteger(memoryCurrent) : null;
|
|
212
|
+
// Ownership is settled by membership and the unit name above; metric files that are
|
|
213
|
+
// unavailable yield null values (and reset the CPU mark) but never demote the unit to shared.
|
|
214
|
+
return {
|
|
215
|
+
unit,
|
|
216
|
+
cpuPressure,
|
|
217
|
+
memoryPressure,
|
|
218
|
+
cpuCoresInUse: this.coresInUse(cgroupPath, usageUsec, monotonicMs),
|
|
219
|
+
memoryAnonBytes,
|
|
220
|
+
memoryChargedBytes,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
coresInUse(cgroupPath, usageUsec, atMs) {
|
|
224
|
+
const previous = this.cpuMark;
|
|
225
|
+
this.cpuMark = usageUsec === null ? null : { cgroupPath, usageUsec, atMs };
|
|
226
|
+
if (!previous || usageUsec === null || previous.cgroupPath !== cgroupPath)
|
|
227
|
+
return null;
|
|
228
|
+
const elapsedUsec = (atMs - previous.atMs) * 1_000;
|
|
229
|
+
const usedUsec = usageUsec - previous.usageUsec;
|
|
230
|
+
// A restarted unit resets its counter; elapsed time comes from a monotonic clock.
|
|
231
|
+
if (elapsedUsec <= 0 || usedUsec < 0)
|
|
232
|
+
return null;
|
|
233
|
+
return roundCores(usedUsec / elapsedUsec);
|
|
234
|
+
}
|
|
235
|
+
/** Busy share (0-1) of all logical CPUs since the last /proc/stat reading. */
|
|
236
|
+
async readHostCpuUse() {
|
|
237
|
+
const content = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "stat"));
|
|
238
|
+
const totals = content ? parseProcStatCpuTotals(content) : null;
|
|
239
|
+
const previous = this.hostCpuMark;
|
|
240
|
+
this.hostCpuMark = totals;
|
|
241
|
+
if (!totals || !previous || previous.cpuCount !== totals.cpuCount)
|
|
242
|
+
return null;
|
|
243
|
+
const totalDelta = totals.totalTicks - previous.totalTicks;
|
|
244
|
+
const busyDelta = totals.busyTicks - previous.busyTicks;
|
|
245
|
+
if (totalDelta <= 0 || busyDelta < 0)
|
|
246
|
+
return null;
|
|
247
|
+
return { utilisation: Math.min(1, busyDelta / totalDelta), cpuCount: totals.cpuCount };
|
|
248
|
+
}
|
|
249
|
+
async readMemoryAvailable() {
|
|
250
|
+
const meminfo = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "meminfo"));
|
|
251
|
+
return (meminfo ? parseMemAvailableBytes(meminfo) : null) ?? this.fallbackFreeMemory();
|
|
252
|
+
}
|
|
253
|
+
fallbackFreeMemory() {
|
|
254
|
+
try {
|
|
255
|
+
const free = this.environment.freeMemoryBytes();
|
|
256
|
+
return Number.isFinite(free) && free >= 0 ? free : null;
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
async readPressure(filePath) {
|
|
263
|
+
const content = await readOptional(this.environment, filePath);
|
|
264
|
+
return content ? parsePressureStall(content) : null;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/** Windows CPU deltas shorter than this are too noisy to fold into the pressure estimate. */
|
|
268
|
+
const minimumWindowsCpuWindowMs = 250;
|
|
269
|
+
function windowsSnapshot(sampledAt, cpu, memoryAvailableBytes, cpuPercent) {
|
|
270
|
+
return {
|
|
271
|
+
sampledAt,
|
|
272
|
+
psiAvailable: cpu !== null,
|
|
273
|
+
pressureSource: cpu !== null ? "windows_estimate" : null,
|
|
274
|
+
systemPressure: cpu ? { cpu, memory: null, io: null } : null,
|
|
275
|
+
ownCgroupDedicated: false,
|
|
276
|
+
ownCgroup: null,
|
|
277
|
+
memoryAvailableBytes,
|
|
278
|
+
hostCpuPercent: cpuPercent,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
function roundCores(value) {
|
|
282
|
+
return Math.round(value * 100) / 100;
|
|
283
|
+
}
|
|
284
|
+
/** A 0-1 busy share as a percentage with two decimals. */
|
|
285
|
+
function hostCpuPercent(utilisation) {
|
|
286
|
+
return Math.round(Math.min(1, Math.max(0, utilisation)) * 10_000) / 100;
|
|
287
|
+
}
|
|
288
|
+
function hostCpuAttribution(sampledAt, hostCoresInUse, ownCoresInUse) {
|
|
289
|
+
if (hostCoresInUse === null)
|
|
290
|
+
return null;
|
|
291
|
+
return {
|
|
292
|
+
sampledAt,
|
|
293
|
+
hostCoresInUse,
|
|
294
|
+
ownCoresInUse,
|
|
295
|
+
// Own usage only exists for a dedicated cgroup; a shared scope never yields external load.
|
|
296
|
+
externalCoresInUse: ownCoresInUse === null ? null : roundCores(Math.max(0, hostCoresInUse - ownCoresInUse)),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export interface HostLoadStepCost {
|
|
2
|
+
/** Cores the step is expected to keep busy. */
|
|
3
|
+
readonly cpuCores: number;
|
|
4
|
+
/** Peak resident memory the step is expected to need. */
|
|
5
|
+
readonly memoryBytes: number;
|
|
6
|
+
}
|
|
7
|
+
export interface HostLoadStepClassification {
|
|
8
|
+
readonly stepClass: string;
|
|
9
|
+
readonly cost: HostLoadStepCost;
|
|
10
|
+
}
|
|
11
|
+
export declare const defaultHostLoadStepCosts: {
|
|
12
|
+
readonly dotnetTier: {
|
|
13
|
+
readonly cpuCores: 2;
|
|
14
|
+
readonly memoryBytes: number;
|
|
15
|
+
};
|
|
16
|
+
readonly vitest: {
|
|
17
|
+
readonly cpuCores: 1;
|
|
18
|
+
readonly memoryBytes: number;
|
|
19
|
+
};
|
|
20
|
+
readonly playwright: {
|
|
21
|
+
readonly cpuCores: 2;
|
|
22
|
+
readonly memoryBytes: number;
|
|
23
|
+
};
|
|
24
|
+
readonly generic: {
|
|
25
|
+
readonly cpuCores: 1;
|
|
26
|
+
readonly memoryBytes: number;
|
|
27
|
+
};
|
|
28
|
+
/** A `dotnet build` (or test with its build) started as a managed command. */
|
|
29
|
+
readonly managedBuild: {
|
|
30
|
+
readonly cpuCores: 2;
|
|
31
|
+
readonly memoryBytes: number;
|
|
32
|
+
};
|
|
33
|
+
readonly aspnetPrebuild: {
|
|
34
|
+
readonly cpuCores: 2;
|
|
35
|
+
readonly memoryBytes: number;
|
|
36
|
+
};
|
|
37
|
+
/** Dependency installs are mostly network and disk bound, so they cost less than a build. */
|
|
38
|
+
readonly dependencyInstall: {
|
|
39
|
+
readonly cpuCores: 0.5;
|
|
40
|
+
readonly memoryBytes: number;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
export declare const aspNetPrebuildStepClass: HostLoadStepClassification;
|
|
44
|
+
export declare const dependencyInstallStepClass: HostLoadStepClassification;
|
|
45
|
+
/**
|
|
46
|
+
* Maps a pre-publication validation step to its class: `dotnet.tier.<tier>`,
|
|
47
|
+
* `vitest.<suite>`, `playwright.e2e`, or `generic` for everything else.
|
|
48
|
+
*/
|
|
49
|
+
export declare function classifyValidationStep(checkId: string, command: string, args: readonly string[]): HostLoadStepClassification;
|
|
50
|
+
export interface ManagedCommandStepInput {
|
|
51
|
+
readonly command: string;
|
|
52
|
+
readonly args: readonly string[];
|
|
53
|
+
/** The runner-authored verification profile, when the command is one. */
|
|
54
|
+
readonly verificationProfile?: string;
|
|
55
|
+
/** Whether the command builds (see isBuildProducingManagedCommand). */
|
|
56
|
+
readonly buildProducing: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Maps a managed command to its class: `managed.<profile>` for verification
|
|
60
|
+
* profiles (a .NET tier's cost per parallel shard), `managed.playwright`,
|
|
61
|
+
* `managed.build` for build-producing commands, `managed.test` for a test run
|
|
62
|
+
* that reuses an earlier build, and `managed.command` otherwise. Only
|
|
63
|
+
* `managed.command` skips the host test slot (decision 0018).
|
|
64
|
+
*/
|
|
65
|
+
export declare function classifyManagedCommand(input: ManagedCommandStepInput): HostLoadStepClassification;
|
|
66
|
+
/**
|
|
67
|
+
* The default cost of a step class, for a step whose cost the requester does
|
|
68
|
+
* not get to choose (a Docker sandbox asking the host bridge for admission).
|
|
69
|
+
*/
|
|
70
|
+
export declare function hostLoadStepCostForClass(stepClass: string): HostLoadStepCost;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
const gib = 1024 ** 3;
|
|
3
|
+
export const defaultHostLoadStepCosts = {
|
|
4
|
+
dotnetTier: { cpuCores: 2, memoryBytes: 3 * gib },
|
|
5
|
+
vitest: { cpuCores: 1, memoryBytes: 1.5 * gib },
|
|
6
|
+
playwright: { cpuCores: 2, memoryBytes: 2 * gib },
|
|
7
|
+
generic: { cpuCores: 1, memoryBytes: 1 * gib },
|
|
8
|
+
/** A `dotnet build` (or test with its build) started as a managed command. */
|
|
9
|
+
managedBuild: { cpuCores: 2, memoryBytes: 2 * gib },
|
|
10
|
+
aspnetPrebuild: { cpuCores: 2, memoryBytes: 2 * gib },
|
|
11
|
+
/** Dependency installs are mostly network and disk bound, so they cost less than a build. */
|
|
12
|
+
dependencyInstall: { cpuCores: 0.5, memoryBytes: 0.75 * gib },
|
|
13
|
+
};
|
|
14
|
+
export const aspNetPrebuildStepClass = {
|
|
15
|
+
stepClass: "aspnet.prebuild",
|
|
16
|
+
cost: defaultHostLoadStepCosts.aspnetPrebuild,
|
|
17
|
+
};
|
|
18
|
+
export const dependencyInstallStepClass = {
|
|
19
|
+
stepClass: "deps.install",
|
|
20
|
+
cost: defaultHostLoadStepCosts.dependencyInstall,
|
|
21
|
+
};
|
|
22
|
+
const dotnetTierScripts = ["scripts/run-dotnet-test-tier.ps1", "scripts/run-dotnet-test-tier-set.ps1"];
|
|
23
|
+
const affectedDotnetScript = "scripts/run-affected-dotnet-tests.mjs";
|
|
24
|
+
const vitestSuites = new Set(["client", "bridge"]);
|
|
25
|
+
/**
|
|
26
|
+
* Maps a pre-publication validation step to its class: `dotnet.tier.<tier>`,
|
|
27
|
+
* `vitest.<suite>`, `playwright.e2e`, or `generic` for everything else.
|
|
28
|
+
*/
|
|
29
|
+
export function classifyValidationStep(checkId, command, args) {
|
|
30
|
+
const normalized = args.map(argument => argument.replaceAll("\\", "/"));
|
|
31
|
+
if (checkId === "playwright-e2e") {
|
|
32
|
+
return { stepClass: "playwright.e2e", cost: defaultHostLoadStepCosts.playwright };
|
|
33
|
+
}
|
|
34
|
+
if (normalized.some(argument => dotnetTierScripts.some(script => argument.endsWith(script)))) {
|
|
35
|
+
return { stepClass: `dotnet.tier.${stepClassSegment(tierArgument(normalized))}`, cost: defaultHostLoadStepCosts.dotnetTier };
|
|
36
|
+
}
|
|
37
|
+
if (normalized.some(argument => argument.endsWith(affectedDotnetScript))) {
|
|
38
|
+
return { stepClass: "dotnet.tier.affected", cost: defaultHostLoadStepCosts.dotnetTier };
|
|
39
|
+
}
|
|
40
|
+
if (vitestSuites.has(checkId) && isTestScript(command, normalized)) {
|
|
41
|
+
return { stepClass: `vitest.${checkId}`, cost: defaultHostLoadStepCosts.vitest };
|
|
42
|
+
}
|
|
43
|
+
return { stepClass: "generic", cost: defaultHostLoadStepCosts.generic };
|
|
44
|
+
}
|
|
45
|
+
function tierArgument(args) {
|
|
46
|
+
const index = args.findIndex(argument => /^-(tier|tiers)$/iu.test(argument));
|
|
47
|
+
return index >= 0 ? args[index + 1] ?? "unknown" : "unknown";
|
|
48
|
+
}
|
|
49
|
+
function isTestScript(command, args) {
|
|
50
|
+
if (!["npm", "pnpm", "npx", "vitest"].includes(command))
|
|
51
|
+
return false;
|
|
52
|
+
const last = args.at(-1);
|
|
53
|
+
return last === "test" || command === "vitest" || args.includes("vitest");
|
|
54
|
+
}
|
|
55
|
+
function stepClassSegment(value) {
|
|
56
|
+
const segment = value.trim().toLowerCase().replace(/[^a-z0-9]+/gu, "-").replace(/^-+|-+$/gu, "");
|
|
57
|
+
return segment || "unknown";
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Maps a managed command to its class: `managed.<profile>` for verification
|
|
61
|
+
* profiles (a .NET tier's cost per parallel shard), `managed.playwright`,
|
|
62
|
+
* `managed.build` for build-producing commands, `managed.test` for a test run
|
|
63
|
+
* that reuses an earlier build, and `managed.command` otherwise. Only
|
|
64
|
+
* `managed.command` skips the host test slot (decision 0018).
|
|
65
|
+
*/
|
|
66
|
+
export function classifyManagedCommand(input) {
|
|
67
|
+
if (input.verificationProfile) {
|
|
68
|
+
const parallelism = maxParallelismArgument(input.args);
|
|
69
|
+
const tier = defaultHostLoadStepCosts.dotnetTier;
|
|
70
|
+
return {
|
|
71
|
+
stepClass: `managed.${stepClassSegment(input.verificationProfile)}`,
|
|
72
|
+
cost: {
|
|
73
|
+
cpuCores: tier.cpuCores * parallelism,
|
|
74
|
+
memoryBytes: tier.memoryBytes + (parallelism - 1) * 1.5 * gib,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const invocation = [input.command, ...input.args].map(argument => argument.toLowerCase().replaceAll("\\", "/"));
|
|
79
|
+
const programs = programsRun(invocation);
|
|
80
|
+
if (programs.some(program => path.posix.basename(program).includes("playwright"))) {
|
|
81
|
+
return { stepClass: "managed.playwright", cost: defaultHostLoadStepCosts.playwright };
|
|
82
|
+
}
|
|
83
|
+
if (input.buildProducing)
|
|
84
|
+
return { stepClass: "managed.build", cost: defaultHostLoadStepCosts.managedBuild };
|
|
85
|
+
if (isTestRun(invocation, programs))
|
|
86
|
+
return { stepClass: "managed.test", cost: defaultHostLoadStepCosts.dotnetTier };
|
|
87
|
+
return { stepClass: "managed.command", cost: defaultHostLoadStepCosts.generic };
|
|
88
|
+
}
|
|
89
|
+
const testRunScripts = [...dotnetTierScripts, affectedDotnetScript].map(script => script.toLowerCase());
|
|
90
|
+
const packageManagers = new Set(["npm", "pnpm", "yarn", "npx"]);
|
|
91
|
+
const scriptHosts = new Set(["node", "pwsh", "powershell", "bash", "sh", "tsx"]);
|
|
92
|
+
const programName = (argument) => path.posix.basename(argument).replace(/\.(exe|cmd)$/u, "");
|
|
93
|
+
// Package-manager options that take a value (`pnpm --dir tests/e2e`, `npm --prefix x`), in
|
|
94
|
+
// lower case like the invocation, so `-c` and `-f` are pnpm's `-C` and `-F`.
|
|
95
|
+
const packageManagerValueOptions = new Set(["--dir", "-c", "--prefix", "--filter", "-f", "--workspace", "--cwd", "--config"]);
|
|
96
|
+
/** The operands of an invocation, without options and, for a package manager, the values its options take. */
|
|
97
|
+
function operandsOf(command, args) {
|
|
98
|
+
const operands = [];
|
|
99
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
100
|
+
const argument = args[index];
|
|
101
|
+
if (!argument.startsWith("-"))
|
|
102
|
+
operands.push(argument);
|
|
103
|
+
else if (packageManagers.has(command) && packageManagerValueOptions.has(argument))
|
|
104
|
+
index += 1;
|
|
105
|
+
}
|
|
106
|
+
return operands;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The programs an invocation runs: the executable and, for a script host or a
|
|
110
|
+
* package manager's exec, the script or tool it starts. Arguments after that
|
|
111
|
+
* (a search pattern, a file to read) never classify the command, so
|
|
112
|
+
* `rg vitest src` or `cat playwright.config.ts` stays a plain command.
|
|
113
|
+
*/
|
|
114
|
+
function programsRun(invocation) {
|
|
115
|
+
const command = programName(invocation[0] ?? "");
|
|
116
|
+
const operands = operandsOf(command, invocation.slice(1));
|
|
117
|
+
if (scriptHosts.has(command))
|
|
118
|
+
return [command, ...operands.slice(0, 1)];
|
|
119
|
+
if (command === "npx")
|
|
120
|
+
return [command, ...operands.slice(0, 1)];
|
|
121
|
+
if (packageManagers.has(command) && ["exec", "dlx"].includes(operands[0] ?? ""))
|
|
122
|
+
return [command, ...operands.slice(1, 2)];
|
|
123
|
+
// `pnpm vitest`, `yarn playwright test`: these run a package's binary by name.
|
|
124
|
+
if (command === "pnpm" || command === "yarn")
|
|
125
|
+
return [command, ...operands.slice(0, 1)];
|
|
126
|
+
return [command];
|
|
127
|
+
}
|
|
128
|
+
/** `dotnet test`, the tier and affected scripts, Vitest, or a package manager's test script. */
|
|
129
|
+
function isTestRun(invocation, programs) {
|
|
130
|
+
const command = programs[0] ?? "";
|
|
131
|
+
const operands = operandsOf(command, invocation.slice(1));
|
|
132
|
+
if (command === "dotnet" && operands[0] === "test")
|
|
133
|
+
return true;
|
|
134
|
+
if (programs.some(program => testRunScripts.some(script => program.endsWith(script))))
|
|
135
|
+
return true;
|
|
136
|
+
if (programs.some(program => programName(program) === "vitest"))
|
|
137
|
+
return true;
|
|
138
|
+
return packageManagers.has(command) && (operands[0] === "test" || (operands[0] === "run" && operands[1] === "test"));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The default cost of a step class, for a step whose cost the requester does
|
|
142
|
+
* not get to choose (a Docker sandbox asking the host bridge for admission).
|
|
143
|
+
*/
|
|
144
|
+
export function hostLoadStepCostForClass(stepClass) {
|
|
145
|
+
if (stepClass.startsWith("dotnet.tier."))
|
|
146
|
+
return defaultHostLoadStepCosts.dotnetTier;
|
|
147
|
+
if (stepClass.startsWith("vitest."))
|
|
148
|
+
return defaultHostLoadStepCosts.vitest;
|
|
149
|
+
if (stepClass === "playwright.e2e")
|
|
150
|
+
return defaultHostLoadStepCosts.playwright;
|
|
151
|
+
if (stepClass === aspNetPrebuildStepClass.stepClass)
|
|
152
|
+
return defaultHostLoadStepCosts.aspnetPrebuild;
|
|
153
|
+
if (stepClass === dependencyInstallStepClass.stepClass)
|
|
154
|
+
return defaultHostLoadStepCosts.dependencyInstall;
|
|
155
|
+
return defaultHostLoadStepCosts.generic;
|
|
156
|
+
}
|
|
157
|
+
function maxParallelismArgument(args) {
|
|
158
|
+
const index = args.findIndex(argument => argument.toLowerCase() === "-maxparallelism");
|
|
159
|
+
const value = index >= 0 ? Number(args[index + 1]) : 1;
|
|
160
|
+
return Number.isSafeInteger(value) && value >= 1 ? Math.min(value, 32) : 1;
|
|
161
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type WindowsProcessTable } from "../shared/process/process-tree.js";
|
|
2
|
+
/** The process tree a governed step runs in, recorded on its token once it launches. */
|
|
3
|
+
export interface HostLoadStepProcess {
|
|
4
|
+
readonly rootPid: number;
|
|
5
|
+
/** OS birth marker of the root, so a reused pid is never signalled. Null where unsupported. */
|
|
6
|
+
readonly rootIdentity: string | null;
|
|
7
|
+
/** The root leads its own process group (Unix): the whole group is stopped and checked. */
|
|
8
|
+
readonly processGroup: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** Stops a step's process tree; resolves true only when no member is left. */
|
|
11
|
+
export type HostLoadStepTerminator = (step: HostLoadStepProcess) => Promise<boolean>;
|
|
12
|
+
/**
|
|
13
|
+
* Stops the recorded tree with SIGTERM, then SIGKILL, and confirms it is gone.
|
|
14
|
+
* A root that cannot be identified (no birth marker) is never signalled and
|
|
15
|
+
* counts as not confirmed, so its token keeps its reserved cost.
|
|
16
|
+
*/
|
|
17
|
+
export declare const terminateHostLoadStepProcess: HostLoadStepTerminator;
|
|
18
|
+
/**
|
|
19
|
+
* Windows has no process groups and does not reparent orphans. A root with a
|
|
20
|
+
* recorded birth marker is stopped together with every descendant created
|
|
21
|
+
* after it (found through parent ids, even if the root already exited), and
|
|
22
|
+
* the reservation is reused only once none is left. Without a birth marker the
|
|
23
|
+
* root cannot be told from a reused pid, so it is never signalled and only its
|
|
24
|
+
* absence counts.
|
|
25
|
+
*/
|
|
26
|
+
export declare function windowsStepTerminator(table: WindowsProcessTable, waitMs?: number): HostLoadStepTerminator;
|
|
27
|
+
export declare function validHostLoadStepProcess(value: unknown): value is HostLoadStepProcess;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { captureHostProcessIdentity } from "../shared/process/host-process-supervisor.js";
|
|
2
|
+
import { systemWindowsProcessTable, WindowsProcessTree } from "../shared/process/process-tree.js";
|
|
3
|
+
import { monotonicNowMs } from "../shared/time/monotonic.js";
|
|
4
|
+
const graceMs = 250;
|
|
5
|
+
/**
|
|
6
|
+
* Stops the recorded tree with SIGTERM, then SIGKILL, and confirms it is gone.
|
|
7
|
+
* A root that cannot be identified (no birth marker) is never signalled and
|
|
8
|
+
* counts as not confirmed, so its token keeps its reserved cost.
|
|
9
|
+
*/
|
|
10
|
+
export const terminateHostLoadStepProcess = async (step) => process.platform === "win32"
|
|
11
|
+
? await windowsStepTerminator(systemWindowsProcessTable)(step)
|
|
12
|
+
: await terminateUnixStep(step);
|
|
13
|
+
/**
|
|
14
|
+
* Windows has no process groups and does not reparent orphans. A root with a
|
|
15
|
+
* recorded birth marker is stopped together with every descendant created
|
|
16
|
+
* after it (found through parent ids, even if the root already exited), and
|
|
17
|
+
* the reservation is reused only once none is left. Without a birth marker the
|
|
18
|
+
* root cannot be told from a reused pid, so it is never signalled and only its
|
|
19
|
+
* absence counts.
|
|
20
|
+
*/
|
|
21
|
+
export function windowsStepTerminator(table, waitMs = 10_000) {
|
|
22
|
+
return async (step) => {
|
|
23
|
+
const createdAt = step.rootIdentity?.startsWith("win32:") ? step.rootIdentity.slice("win32:".length) : null;
|
|
24
|
+
if (createdAt === null)
|
|
25
|
+
return !pidExists(step.rootPid);
|
|
26
|
+
return await new WindowsProcessTree(step.rootPid, table, { exactCreatedAt: createdAt }).stop(Date.now() + waitMs);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async function terminateUnixStep(step) {
|
|
30
|
+
if (!memberExists(step))
|
|
31
|
+
return true;
|
|
32
|
+
const unixGroup = step.processGroup && process.platform !== "win32";
|
|
33
|
+
if (!pidExists(step.rootPid)) {
|
|
34
|
+
// The root exited but its group lives on. A process group id is never reissued
|
|
35
|
+
// while the group has members, so these are the step's own orphans: stop them.
|
|
36
|
+
if (!unixGroup || !step.rootIdentity)
|
|
37
|
+
return !memberExists(step);
|
|
38
|
+
return await stopTree(step);
|
|
39
|
+
}
|
|
40
|
+
const identity = captureHostProcessIdentity(step.rootPid);
|
|
41
|
+
// The root pid now names another process, so the step's root and its group are gone.
|
|
42
|
+
if (step.rootIdentity && identity && identity !== step.rootIdentity)
|
|
43
|
+
return true;
|
|
44
|
+
if (!step.rootIdentity || !identity)
|
|
45
|
+
return !memberExists(step);
|
|
46
|
+
return await stopTree(step);
|
|
47
|
+
}
|
|
48
|
+
async function stopTree(step) {
|
|
49
|
+
for (const signal of ["SIGTERM", "SIGKILL"]) {
|
|
50
|
+
send(step, signal);
|
|
51
|
+
const deadline = monotonicNowMs() + graceMs;
|
|
52
|
+
while (memberExists(step) && monotonicNowMs() < deadline)
|
|
53
|
+
await delay(10);
|
|
54
|
+
if (!memberExists(step))
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
function memberExists(step) {
|
|
60
|
+
return step.processGroup && process.platform !== "win32" ? groupExists(step.rootPid) : pidExists(step.rootPid);
|
|
61
|
+
}
|
|
62
|
+
function send(step, signal) {
|
|
63
|
+
try {
|
|
64
|
+
process.kill(step.processGroup && process.platform !== "win32" ? -step.rootPid : step.rootPid, signal);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error.code !== "ESRCH")
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function pidExists(pid) {
|
|
72
|
+
return signalZero(pid);
|
|
73
|
+
}
|
|
74
|
+
function groupExists(pid) {
|
|
75
|
+
return signalZero(-pid);
|
|
76
|
+
}
|
|
77
|
+
function signalZero(target) {
|
|
78
|
+
try {
|
|
79
|
+
process.kill(target, 0);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return error.code === "EPERM";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async function delay(ms) {
|
|
87
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
88
|
+
}
|
|
89
|
+
export function validHostLoadStepProcess(value) {
|
|
90
|
+
const step = value;
|
|
91
|
+
return Boolean(step)
|
|
92
|
+
&& Number.isSafeInteger(step.rootPid) && step.rootPid > 1
|
|
93
|
+
&& (step.rootIdentity === null || typeof step.rootIdentity === "string")
|
|
94
|
+
&& typeof step.processGroup === "boolean";
|
|
95
|
+
}
|