@loucompanion/forge-bridge 0.1.4-dev.fb770705136a → 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/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 +90 -14
- package/dist/bridge/bridge.service.js +436 -350
- package/dist/bridge/bridge.types.d.ts +21 -0
- 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.d.ts +36 -0
- package/dist/runtime/attempt-node-runtime-launcher.js +169 -0
- package/dist/runtime/attempt-process-supervisor.d.ts +2 -0
- package/dist/runtime/attempt-process-supervisor.js +12 -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-facts.d.ts +2 -1
- package/dist/runtime/host-listener-inspector.d.ts +26 -0
- package/dist/runtime/host-listener-inspector.js +84 -3
- 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 +75 -2
- package/dist/runtime/host-load-sampler.js +142 -8
- 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 +21 -1
- package/dist/runtime/host-pressure-stall.js +48 -1
- package/dist/runtime/host-runtime-contract.d.ts +15 -0
- package/dist/runtime/host-runtime-contract.js +33 -2
- package/dist/runtime/host-runtime-lease.d.ts +101 -6
- package/dist/runtime/host-runtime-lease.js +408 -294
- 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 +101 -2
- package/dist/runtime/host-runtime-promotion.js +355 -14
- 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.js +5 -16
- 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-host-runtime-adapter.d.ts +7 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +13 -1
- 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/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 +2 -7
- package/dist/session/managed-attempt-tools.js +207 -121
- package/dist/session/managed-build-coordinator.d.ts +16 -0
- package/dist/session/managed-build-coordinator.js +10 -0
- 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 +96 -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 +91 -215
- 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 +3 -0
- package/dist/session/pre-publication-process.js +31 -19
- 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 +221 -182
- 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.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.process.d.ts +15 -36
- package/dist/session/provider-cli/provider-cli.process.js +41 -432
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +5 -0
- package/dist/session/provider-cli/provider-cli.prompts.js +72 -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 +98 -173
- 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 +4 -0
- package/dist/session/provider-cli/provider-in-flight-work.js +45 -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 +2 -0
- package/dist/session/provider-cli/provider-output-state.js +6 -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/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 +15 -2
- package/dist/session/session.service.js +242 -88
- package/dist/session/session.types.d.ts +3 -0
- package/dist/session/session.types.js +5 -5
- 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
- package/runtime/windows-listener-owner.ps1 +12 -1
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import { defaultHostEnvironment, detectHostFacts, readOptional, } from "./host-facts.js";
|
|
3
|
-
import { cgroupLeafName, isDedicatedBridgeUnit, parseCgroupCpuUsageUsec, parseCgroupMemoryAnonBytes, parseMemAvailableBytes, parseNonNegativeInteger, parsePressureStall, parseUnifiedCgroupPath, } from "./host-pressure-stall.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";
|
|
4
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;
|
|
5
9
|
/**
|
|
6
10
|
* 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
|
|
8
|
-
* (
|
|
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).
|
|
9
14
|
*/
|
|
10
15
|
export class HostLoadSampler {
|
|
11
16
|
environment;
|
|
@@ -19,7 +24,15 @@ export class HostLoadSampler {
|
|
|
19
24
|
inFlight = null;
|
|
20
25
|
hostFacts = null;
|
|
21
26
|
latest = null;
|
|
27
|
+
history = [];
|
|
22
28
|
cpuMark = null;
|
|
29
|
+
hostCpuMark = null;
|
|
30
|
+
latestCpuAttribution = null;
|
|
31
|
+
latestMemoryHeadroom = null;
|
|
32
|
+
windowsCpuMark = null;
|
|
33
|
+
windowsCpuPressure = new WindowsCpuPressureEstimator();
|
|
34
|
+
forgeCgroups;
|
|
35
|
+
sandboxCpu;
|
|
23
36
|
constructor(options = {}) {
|
|
24
37
|
this.environment = options.environment ?? defaultHostEnvironment();
|
|
25
38
|
this.intervalMs = options.intervalMs !== undefined && Number.isFinite(options.intervalMs) && options.intervalMs > 0
|
|
@@ -30,6 +43,8 @@ export class HostLoadSampler {
|
|
|
30
43
|
this.cgroupUnit = options.cgroupUnit !== undefined
|
|
31
44
|
? options.cgroupUnit
|
|
32
45
|
: process.env.FORGE_BRIDGE_CGROUP_UNIT?.trim() || null;
|
|
46
|
+
this.forgeCgroups = options.forgeCgroups ?? null;
|
|
47
|
+
this.sandboxCpu = new ForgeCgroupCpuMeter(this.environment);
|
|
33
48
|
}
|
|
34
49
|
facts() {
|
|
35
50
|
return this.hostFacts;
|
|
@@ -37,6 +52,15 @@ export class HostLoadSampler {
|
|
|
37
52
|
current() {
|
|
38
53
|
return this.latest;
|
|
39
54
|
}
|
|
55
|
+
cpuAttribution() {
|
|
56
|
+
return this.latestCpuAttribution;
|
|
57
|
+
}
|
|
58
|
+
cgroupMemoryHeadroomBytes() {
|
|
59
|
+
return this.latestMemoryHeadroom;
|
|
60
|
+
}
|
|
61
|
+
recent() {
|
|
62
|
+
return [...this.history];
|
|
63
|
+
}
|
|
40
64
|
/** Detects static facts once, takes a first sample, then samples on the interval. Idempotent. */
|
|
41
65
|
start() {
|
|
42
66
|
if (this.timer)
|
|
@@ -57,7 +81,13 @@ export class HostLoadSampler {
|
|
|
57
81
|
sample() {
|
|
58
82
|
this.inFlight ??= this.readSnapshot()
|
|
59
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;
|
|
60
87
|
this.latest = snapshot;
|
|
88
|
+
this.history.push(snapshot);
|
|
89
|
+
if (this.history.length > retainedSampleCount)
|
|
90
|
+
this.history.splice(0, this.history.length - retainedSampleCount);
|
|
61
91
|
return snapshot;
|
|
62
92
|
})
|
|
63
93
|
.finally(() => {
|
|
@@ -81,19 +111,77 @@ export class HostLoadSampler {
|
|
|
81
111
|
async readSnapshot() {
|
|
82
112
|
const sampledAtMs = this.now();
|
|
83
113
|
const monotonicMs = this.monotonicNow();
|
|
114
|
+
if (this.environment.platform === "win32")
|
|
115
|
+
return this.readWindowsSnapshot(sampledAtMs, monotonicMs);
|
|
84
116
|
const linux = this.environment.platform === "linux";
|
|
85
|
-
const [systemPressure, ownCgroup, memoryAvailableBytes] = linux
|
|
86
|
-
? await Promise.all([
|
|
87
|
-
|
|
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);
|
|
88
133
|
return {
|
|
89
|
-
sampledAt
|
|
134
|
+
sampledAt,
|
|
90
135
|
psiAvailable: systemPressure !== null,
|
|
136
|
+
pressureSource: systemPressure !== null ? "psi" : null,
|
|
91
137
|
systemPressure,
|
|
92
138
|
ownCgroupDedicated: ownCgroup !== null,
|
|
93
139
|
ownCgroup,
|
|
94
140
|
memoryAvailableBytes,
|
|
141
|
+
hostCpuPercent: hostCpu === null ? null : hostCpuPercent(hostCpu.utilisation),
|
|
95
142
|
};
|
|
96
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
|
+
}
|
|
97
185
|
async readSystemPressure() {
|
|
98
186
|
const pressureRoot = path.posix.join(this.environment.procRoot, "pressure");
|
|
99
187
|
const [cpu, memory, io] = await Promise.all(["cpu", "memory", "io"].map(resource => this.readPressure(path.posix.join(pressureRoot, resource))));
|
|
@@ -142,7 +230,21 @@ export class HostLoadSampler {
|
|
|
142
230
|
// A restarted unit resets its counter; elapsed time comes from a monotonic clock.
|
|
143
231
|
if (elapsedUsec <= 0 || usedUsec < 0)
|
|
144
232
|
return null;
|
|
145
|
-
return
|
|
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 };
|
|
146
248
|
}
|
|
147
249
|
async readMemoryAvailable() {
|
|
148
250
|
const meminfo = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "meminfo"));
|
|
@@ -162,3 +264,35 @@ export class HostLoadSampler {
|
|
|
162
264
|
return content ? parsePressureStall(content) : null;
|
|
163
265
|
}
|
|
164
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
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { HostLoadAdmissionDecision } from "./host-load-admission.js";
|
|
2
|
+
import type { HostLoadAdmission, HostLoadStepGate } from "./host-load-governor.js";
|
|
3
|
+
import type { HostLoadStepProcess } from "./host-load-step-reaper.js";
|
|
4
|
+
import type { HostLoadStepClassification } from "./host-load-step-class.js";
|
|
5
|
+
import type { HostLoadTokenPriority } from "./host-load-token-ledger.js";
|
|
6
|
+
export interface HostGovernedStep {
|
|
7
|
+
readonly classification: HostLoadStepClassification;
|
|
8
|
+
/** What the step is, for diagnostics. */
|
|
9
|
+
readonly label: string;
|
|
10
|
+
readonly dispatchAttemptId: string | null;
|
|
11
|
+
readonly priority: HostLoadTokenPriority;
|
|
12
|
+
readonly signal?: AbortSignal;
|
|
13
|
+
/** Called once, when the step first has to wait for capacity. */
|
|
14
|
+
readonly onWaiting?: (decision: HostLoadAdmissionDecision) => void | Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* The step records its own fenced outcome (a managed command's receipt). Otherwise a
|
|
17
|
+
* step whose token was reclaimed while it ran fails with host_capacity_token_fenced.
|
|
18
|
+
*/
|
|
19
|
+
readonly reportsOwnFence?: boolean;
|
|
20
|
+
/** Called when the token could not be returned; it then lapses with its lease. */
|
|
21
|
+
readonly onReleaseFailed?: (error: unknown) => void | Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/** Queue time, or null when the governor is off and nothing was queued. */
|
|
24
|
+
export type HostLoadWait = number | null;
|
|
25
|
+
/** What an admitted step gets from its token; both absent while the governor is off. */
|
|
26
|
+
export interface HostLoadStepControl {
|
|
27
|
+
/** Aborted when the token was reclaimed: the step must stop. */
|
|
28
|
+
readonly fenced?: AbortSignal;
|
|
29
|
+
/** Records the step's process tree so a reclaim stops it before reusing its capacity. */
|
|
30
|
+
readonly attachProcess?: (stepProcess: HostLoadStepProcess) => Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* For a step that reports its own outcome (a managed command's receipt) when its
|
|
33
|
+
* process tree could not be proven gone: the token is kept, not returned, until
|
|
34
|
+
* `stillGone` proves it (null: see HostLoadAdmission.retainUntilStopped).
|
|
35
|
+
*/
|
|
36
|
+
readonly holdUntilStopped?: (stillGone: (() => Promise<boolean>) | null, reason: Error) => void;
|
|
37
|
+
}
|
|
38
|
+
/** Why a step's token is kept after it ended, and what proves its tree gone. */
|
|
39
|
+
export interface HostLoadRetention {
|
|
40
|
+
readonly stillGone: (() => Promise<boolean>) | null;
|
|
41
|
+
readonly reason: Error;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The retention a step's failure calls for: a tree that could not be proven gone,
|
|
45
|
+
* whether the runner said so with its own check (ProcessTreeTerminationError) or a
|
|
46
|
+
* supervisor did (host_process_supervisor_termination_failed, possibly wrapped).
|
|
47
|
+
*/
|
|
48
|
+
export declare function retentionFor(error: unknown): HostLoadRetention | null;
|
|
49
|
+
/**
|
|
50
|
+
* With the governor off (or no gate), `run` is called directly with a null
|
|
51
|
+
* wait, so behavior is unchanged. With it on, `run` starts only after
|
|
52
|
+
* admission, so the step's own timers never include queue time, and the token
|
|
53
|
+
* is returned when `run` settles, whether it succeeded, failed or was stopped.
|
|
54
|
+
*/
|
|
55
|
+
export declare function runHostGovernedStep<T>(gate: HostLoadStepGate | undefined, step: HostGovernedStep, run: (hostLoadWaitMs: HostLoadWait, control: HostLoadStepControl) => Promise<T>): Promise<T>;
|
|
56
|
+
/**
|
|
57
|
+
* Returns a step's token, unless part of its process tree could not be proven
|
|
58
|
+
* gone: the token (and its cost) is then kept until the tree is.
|
|
59
|
+
*/
|
|
60
|
+
export declare function returnHostLoadToken(admission: HostLoadAdmission, retention: HostLoadRetention | null, onReleaseFailed?: (error: unknown) => void | Promise<void>): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* A step stopped because its token was reclaimed reports host_capacity_token_fenced
|
|
63
|
+
* (retryable), not the exit status or error it died with.
|
|
64
|
+
*/
|
|
65
|
+
export declare function throwIfFenced<T>(fenced: AbortSignal, outcome: T): T;
|
|
66
|
+
/**
|
|
67
|
+
* Records a supervised step's process tree on its host load token, from the
|
|
68
|
+
* metadata its process supervisor wrote (`FORGE_PROCESS_SUPERVISOR_METADATA_PATH`):
|
|
69
|
+
* a reclaim then stops exactly this tree before reusing its capacity. Rejects when
|
|
70
|
+
* the tree cannot be read or recorded; the caller must then stop the step, since
|
|
71
|
+
* it would otherwise run on without a reservation the host can account for.
|
|
72
|
+
*/
|
|
73
|
+
export declare function attachSupervisedStepProcess(control: HostLoadStepControl, metadataPath: string): Promise<void>;
|