@loucompanion/forge-bridge 0.1.4-dev.86fc5d1208e6 → 0.1.4-dev.894bcbd53d64
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 +74 -4
- package/dist/runtime/host-runtime-lease.js +343 -67
- 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 +120 -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,76 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { HostLoadGovernor } from "./host-load-governor.js";
|
|
3
|
+
import { hostLoadGovernorEnabled, loadHostLoadGovernorConfig } from "./host-load-governor-config.js";
|
|
4
|
+
import { FileForgeCgroupRegistry } from "./host-load-forge-cgroups.js";
|
|
5
|
+
import { HostLoadSampler } from "./host-load-sampler.js";
|
|
6
|
+
// How a process the bridge starts (the completion MCP server, which runs
|
|
7
|
+
// managed commands) joins the bridge's host load governor (decision 0013).
|
|
8
|
+
// The ledger is file-backed, so the child builds its own governor on the
|
|
9
|
+
// bridge's state root and every token counts against one host budget.
|
|
10
|
+
export const hostLoadGovernorEnvironment = "FORGE_HOST_LOAD_GOVERNOR";
|
|
11
|
+
export const hostLoadStateRootEnvironment = "FORGE_HOST_LOAD_STATE_ROOT";
|
|
12
|
+
export const hostLoadTestSlotsEnvironment = "FORGE_HOST_LOAD_TEST_SLOTS";
|
|
13
|
+
const cgroupUnitEnvironment = "FORGE_BRIDGE_CGROUP_UNIT";
|
|
14
|
+
/** True for the variables that carry the governor into a child process. */
|
|
15
|
+
/**
|
|
16
|
+
* A process environment with every host load governor variable removed
|
|
17
|
+
* (FORGE_HOST_LOAD_*, any case) and the governor turned off explicitly. A governed
|
|
18
|
+
* step's own processes must never join the host ledger, or broker control, that
|
|
19
|
+
* their step's token is held in; since the host test slot is on by default
|
|
20
|
+
* (decision 0018), removing the variables alone would not keep a nested Forge
|
|
21
|
+
* process out.
|
|
22
|
+
*/
|
|
23
|
+
export function withoutHostLoadGovernorVariables(environment) {
|
|
24
|
+
const kept = {};
|
|
25
|
+
for (const [name, value] of Object.entries(environment)) {
|
|
26
|
+
if (!name.toUpperCase().startsWith("FORGE_HOST_LOAD_"))
|
|
27
|
+
kept[name] = value;
|
|
28
|
+
}
|
|
29
|
+
kept[hostLoadGovernorEnvironment] = "off";
|
|
30
|
+
kept[hostLoadTestSlotsEnvironment] = "0";
|
|
31
|
+
return kept;
|
|
32
|
+
}
|
|
33
|
+
// Decision 0014 operator ceilings the child's governor applies itself (host-load-governor-config.ts).
|
|
34
|
+
const hostCeilingEnvironmentNames = new Set(["FORGE_HOST_CPU_BUDGET_PERCENT", "FORGE_HOST_MAX_HEAVY_STEPS"]);
|
|
35
|
+
export function isHostLoadChildEnvironmentName(name) {
|
|
36
|
+
return name.startsWith("FORGE_HOST_LOAD_") || name === cgroupUnitEnvironment || hostCeilingEnvironmentNames.has(name);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The variables a child needs to share this bridge's ledger: the budget flag and
|
|
40
|
+
* the host test slot count as this bridge resolved them, the state root, the
|
|
41
|
+
* operator's FORGE_HOST_LOAD_* tuning and the bridge's cgroup unit. Empty with
|
|
42
|
+
* the governor off, so the child stays ungoverned.
|
|
43
|
+
*/
|
|
44
|
+
export function hostLoadChildEnvironment(stateRoot, environment = process.env) {
|
|
45
|
+
const config = loadHostLoadGovernorConfig(environment);
|
|
46
|
+
if (!hostLoadGovernorEnabled(config))
|
|
47
|
+
return {};
|
|
48
|
+
const forwarded = {};
|
|
49
|
+
for (const [name, value] of Object.entries(environment)) {
|
|
50
|
+
if (typeof value === "string" && value.trim() && isHostLoadChildEnvironmentName(name))
|
|
51
|
+
forwarded[name] = value;
|
|
52
|
+
}
|
|
53
|
+
forwarded[hostLoadGovernorEnvironment] = config.mode;
|
|
54
|
+
forwarded[hostLoadTestSlotsEnvironment] = String(config.testSlots);
|
|
55
|
+
forwarded[hostLoadStateRootEnvironment] = path.resolve(stateRoot);
|
|
56
|
+
return forwarded;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The governor a child process uses, from the variables above. Undefined when
|
|
60
|
+
* the governor is off or no state root was handed over: the child is ungoverned.
|
|
61
|
+
*/
|
|
62
|
+
export function hostLoadGovernorFromChildEnvironment(environment = process.env) {
|
|
63
|
+
const config = loadHostLoadGovernorConfig(environment);
|
|
64
|
+
const stateRoot = environment[hostLoadStateRootEnvironment]?.trim();
|
|
65
|
+
if (!hostLoadGovernorEnabled(config) || !stateRoot || !path.isAbsolute(stateRoot))
|
|
66
|
+
return undefined;
|
|
67
|
+
return new HostLoadGovernor({
|
|
68
|
+
stateRoot,
|
|
69
|
+
config,
|
|
70
|
+
load: new HostLoadSampler({
|
|
71
|
+
cgroupUnit: environment[cgroupUnitEnvironment]?.trim() || null,
|
|
72
|
+
// The bridge registers its Docker sandboxes here: their CPU is Forge's own here too.
|
|
73
|
+
forgeCgroups: new FileForgeCgroupRegistry(stateRoot),
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type HostEnvironment } from "./host-facts.js";
|
|
2
|
+
/** Where a sampler learns which cgroups hold Forge's own Docker sandbox work. */
|
|
3
|
+
export interface ForgeCgroupSource {
|
|
4
|
+
paths(): readonly string[] | Promise<readonly string[]>;
|
|
5
|
+
}
|
|
6
|
+
/** Where the sandbox runtime records the cgroups of the containers it launched. */
|
|
7
|
+
export interface ForgeCgroupWriter {
|
|
8
|
+
set(owner: string, cgroupPaths: readonly string[]): void;
|
|
9
|
+
delete(owner: string): void;
|
|
10
|
+
}
|
|
11
|
+
/** Cgroups of Docker sandbox containers this bridge launched (paths below the cgroup root). */
|
|
12
|
+
export declare class ForgeCgroupRegistry implements ForgeCgroupSource, ForgeCgroupWriter {
|
|
13
|
+
private readonly entries;
|
|
14
|
+
set(owner: string, cgroupPaths: readonly string[]): void;
|
|
15
|
+
delete(owner: string): void;
|
|
16
|
+
paths(): readonly string[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The registry shared by every process on the governor's state root: the
|
|
20
|
+
* bridge's sandbox runtime writes it, and both the bridge's and the MCP
|
|
21
|
+
* servers' samplers read it, so all of them count sandbox CPU as Forge's own.
|
|
22
|
+
*/
|
|
23
|
+
export declare class FileForgeCgroupRegistry implements ForgeCgroupSource, ForgeCgroupWriter {
|
|
24
|
+
private readonly filePath;
|
|
25
|
+
private readonly registered;
|
|
26
|
+
private writing;
|
|
27
|
+
constructor(stateRoot: string);
|
|
28
|
+
set(owner: string, cgroupPaths: readonly string[]): void;
|
|
29
|
+
delete(owner: string): void;
|
|
30
|
+
paths(): Promise<readonly string[]>;
|
|
31
|
+
/** Settles once every registration so far is on disk (tests). */
|
|
32
|
+
flushed(): Promise<void>;
|
|
33
|
+
private persist;
|
|
34
|
+
}
|
|
35
|
+
/** CPU cores used by a changing set of cgroups, from `cpu.stat usage_usec` deltas. */
|
|
36
|
+
export declare class ForgeCgroupCpuMeter {
|
|
37
|
+
private readonly environment;
|
|
38
|
+
private marks;
|
|
39
|
+
constructor(environment: HostEnvironment);
|
|
40
|
+
/** Cores used since the previous call; a cgroup seen for the first time contributes from the next window. */
|
|
41
|
+
coresInUse(cgroupPaths: readonly string[], atMs: number): Promise<number>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Free memory under the tightest `memory.max` from the cgroup up to and
|
|
45
|
+
* including the root (`memory.max − memory.current`), or null when no limit is
|
|
46
|
+
* set on the path. Inside a container the root is the container's own cgroup,
|
|
47
|
+
* whose `memory.max` is the container limit.
|
|
48
|
+
*/
|
|
49
|
+
export declare function readCgroupMemoryHeadroomBytes(environment: HostEnvironment, cgroupPath: string): Promise<number | null>;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { readOptional } from "./host-facts.js";
|
|
5
|
+
import { parseCgroupCpuUsageUsec, parseNonNegativeInteger } from "./host-pressure-stall.js";
|
|
6
|
+
/** Cgroups of Docker sandbox containers this bridge launched (paths below the cgroup root). */
|
|
7
|
+
export class ForgeCgroupRegistry {
|
|
8
|
+
entries = new Map();
|
|
9
|
+
set(owner, cgroupPaths) {
|
|
10
|
+
if (cgroupPaths.length === 0)
|
|
11
|
+
this.entries.delete(owner);
|
|
12
|
+
else
|
|
13
|
+
this.entries.set(owner, [...cgroupPaths]);
|
|
14
|
+
}
|
|
15
|
+
delete(owner) {
|
|
16
|
+
this.entries.delete(owner);
|
|
17
|
+
}
|
|
18
|
+
paths() {
|
|
19
|
+
return [...new Set([...this.entries.values()].flat())];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The registry shared by every process on the governor's state root: the
|
|
24
|
+
* bridge's sandbox runtime writes it, and both the bridge's and the MCP
|
|
25
|
+
* servers' samplers read it, so all of them count sandbox CPU as Forge's own.
|
|
26
|
+
*/
|
|
27
|
+
export class FileForgeCgroupRegistry {
|
|
28
|
+
filePath;
|
|
29
|
+
registered = new ForgeCgroupRegistry();
|
|
30
|
+
writing = Promise.resolve();
|
|
31
|
+
constructor(stateRoot) {
|
|
32
|
+
this.filePath = path.join(stateRoot, "host-load-governor", "forge-cgroups.v1.json");
|
|
33
|
+
}
|
|
34
|
+
set(owner, cgroupPaths) {
|
|
35
|
+
this.registered.set(owner, cgroupPaths);
|
|
36
|
+
this.persist();
|
|
37
|
+
}
|
|
38
|
+
delete(owner) {
|
|
39
|
+
this.registered.delete(owner);
|
|
40
|
+
this.persist();
|
|
41
|
+
}
|
|
42
|
+
paths() {
|
|
43
|
+
return fs.readFile(this.filePath, "utf8")
|
|
44
|
+
.then(content => {
|
|
45
|
+
const parsed = JSON.parse(content);
|
|
46
|
+
return Array.isArray(parsed.paths)
|
|
47
|
+
? parsed.paths.filter((entry) => typeof entry === "string").slice(0, 256)
|
|
48
|
+
: [];
|
|
49
|
+
})
|
|
50
|
+
.catch(() => []);
|
|
51
|
+
}
|
|
52
|
+
/** Settles once every registration so far is on disk (tests). */
|
|
53
|
+
async flushed() {
|
|
54
|
+
await this.writing;
|
|
55
|
+
}
|
|
56
|
+
persist() {
|
|
57
|
+
const snapshot = JSON.stringify({ schemaVersion: 1, paths: this.registered.paths() });
|
|
58
|
+
this.writing = this.writing.then(async () => {
|
|
59
|
+
await fs.mkdir(path.dirname(this.filePath), { recursive: true, mode: 0o700 });
|
|
60
|
+
const temporary = `${this.filePath}.${process.pid}.${randomUUID()}.tmp`;
|
|
61
|
+
await fs.writeFile(temporary, snapshot, { mode: 0o600 });
|
|
62
|
+
await fs.rename(temporary, this.filePath);
|
|
63
|
+
}).catch(() => undefined);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** CPU cores used by a changing set of cgroups, from `cpu.stat usage_usec` deltas. */
|
|
67
|
+
export class ForgeCgroupCpuMeter {
|
|
68
|
+
environment;
|
|
69
|
+
marks = new Map();
|
|
70
|
+
constructor(environment) {
|
|
71
|
+
this.environment = environment;
|
|
72
|
+
}
|
|
73
|
+
/** Cores used since the previous call; a cgroup seen for the first time contributes from the next window. */
|
|
74
|
+
async coresInUse(cgroupPaths, atMs) {
|
|
75
|
+
const next = new Map();
|
|
76
|
+
let cores = 0;
|
|
77
|
+
for (const cgroupPath of cgroupPaths) {
|
|
78
|
+
const directory = cgroupDirectory(this.environment, cgroupPath);
|
|
79
|
+
if (!directory)
|
|
80
|
+
continue;
|
|
81
|
+
const content = await readOptional(this.environment, path.posix.join(directory, "cpu.stat"));
|
|
82
|
+
const usageUsec = content ? parseCgroupCpuUsageUsec(content) : null;
|
|
83
|
+
if (usageUsec === null)
|
|
84
|
+
continue;
|
|
85
|
+
next.set(cgroupPath, { usageUsec, atMs });
|
|
86
|
+
const previous = this.marks.get(cgroupPath);
|
|
87
|
+
const elapsedUsec = previous ? (atMs - previous.atMs) * 1_000 : 0;
|
|
88
|
+
if (previous && elapsedUsec > 0 && usageUsec >= previous.usageUsec) {
|
|
89
|
+
cores += (usageUsec - previous.usageUsec) / elapsedUsec;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
this.marks = next;
|
|
93
|
+
return cores;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Free memory under the tightest `memory.max` from the cgroup up to and
|
|
98
|
+
* including the root (`memory.max − memory.current`), or null when no limit is
|
|
99
|
+
* set on the path. Inside a container the root is the container's own cgroup,
|
|
100
|
+
* whose `memory.max` is the container limit.
|
|
101
|
+
*/
|
|
102
|
+
export async function readCgroupMemoryHeadroomBytes(environment, cgroupPath) {
|
|
103
|
+
let tightest = null;
|
|
104
|
+
for (const current of cgroupPathAndAncestors(cgroupPath)) {
|
|
105
|
+
const directory = cgroupDirectory(environment, current);
|
|
106
|
+
if (!directory)
|
|
107
|
+
return null;
|
|
108
|
+
const [maximum, used] = await Promise.all([
|
|
109
|
+
readOptional(environment, path.posix.join(directory, "memory.max")),
|
|
110
|
+
readOptional(environment, path.posix.join(directory, "memory.current")),
|
|
111
|
+
]);
|
|
112
|
+
const limit = maximum ? parseNonNegativeInteger(maximum) : null;
|
|
113
|
+
const usage = used ? parseNonNegativeInteger(used) : null;
|
|
114
|
+
if (limit === null || usage === null)
|
|
115
|
+
continue;
|
|
116
|
+
const headroom = Math.max(0, limit - usage);
|
|
117
|
+
tightest = tightest === null ? headroom : Math.min(tightest, headroom);
|
|
118
|
+
}
|
|
119
|
+
return tightest;
|
|
120
|
+
}
|
|
121
|
+
/** `/a/b/c`, `/a/b`, `/a`, `/`; nothing for a relative or traversing path. */
|
|
122
|
+
function cgroupPathAndAncestors(cgroupPath) {
|
|
123
|
+
if (!cgroupPath.startsWith("/") || cgroupPath.split("/").includes(".."))
|
|
124
|
+
return [];
|
|
125
|
+
const paths = [];
|
|
126
|
+
for (let current = path.posix.normalize(cgroupPath);; current = path.posix.dirname(current)) {
|
|
127
|
+
paths.push(current);
|
|
128
|
+
if (current === "/")
|
|
129
|
+
return paths;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function cgroupDirectory(environment, cgroupPath) {
|
|
133
|
+
if (!cgroupPath.startsWith("/") || cgroupPath.split("/").includes(".."))
|
|
134
|
+
return null;
|
|
135
|
+
const root = environment.cgroupRoot.replace(/\/+$/u, "");
|
|
136
|
+
if (cgroupPath === "/")
|
|
137
|
+
return root;
|
|
138
|
+
const directory = path.posix.join(root, cgroupPath);
|
|
139
|
+
return directory.startsWith(`${root}/`) ? directory : null;
|
|
140
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export type HostLoadGovernorMode = "on" | "off";
|
|
2
|
+
export interface HostLoadGovernorConfig {
|
|
3
|
+
/** FORGE_HOST_LOAD_GOVERNOR: the CPU and memory budget. Off by default; `off` is the operator override. */
|
|
4
|
+
readonly mode: HostLoadGovernorMode;
|
|
5
|
+
/**
|
|
6
|
+
* FORGE_HOST_LOAD_TEST_SLOTS (decision 0018): how many heavy steps (builds, test runs,
|
|
7
|
+
* Playwright, the runtime build) run at once on the host, whatever the budget says.
|
|
8
|
+
* 1 by default; 0 turns the slot off, and with the budget off too the governor is off.
|
|
9
|
+
*/
|
|
10
|
+
readonly testSlots: number;
|
|
11
|
+
/** Share of logical CPUs Forge may reserve for heavy steps. */
|
|
12
|
+
readonly cpuBudgetFraction: number;
|
|
13
|
+
/** Memory kept free for the rest of the host, taken off MemAvailable. */
|
|
14
|
+
readonly memoryReserveBytes: number;
|
|
15
|
+
/** Host CPU PSI `some avg10` (percent) at or above which a sample counts as pressured. */
|
|
16
|
+
readonly cpuPressureThreshold: number;
|
|
17
|
+
/** Consecutive pressured samples that stop new admissions (backpressure). */
|
|
18
|
+
readonly backpressureSamples: number;
|
|
19
|
+
/** Host memory PSI `full avg10` (percent) above which admission is a hard stop. */
|
|
20
|
+
readonly memoryFullThreshold: number;
|
|
21
|
+
/** Maximum queue wait before a retryable host_capacity_timeout. */
|
|
22
|
+
readonly maxWaitMs: number;
|
|
23
|
+
/** Steps allowed at once when PSI is unavailable (the per-host semaphore fallback). */
|
|
24
|
+
readonly fallbackSlots: number;
|
|
25
|
+
/** After this long at the head of the queue, later waiters may no longer overtake it. */
|
|
26
|
+
readonly antiStarvationMs: number;
|
|
27
|
+
/** Token lease; the owner renews well before it expires. */
|
|
28
|
+
readonly leaseMs: number;
|
|
29
|
+
/** How often waiters are re-evaluated against fresh samples and the shared ledger. */
|
|
30
|
+
readonly recheckIntervalMs: number;
|
|
31
|
+
/** A load sample older than this is treated as missing PSI. */
|
|
32
|
+
readonly sampleStaleMs: number;
|
|
33
|
+
/** The sampler's interval; a longer gap between samples resets the pressure streak. */
|
|
34
|
+
readonly sampleIntervalMs: number;
|
|
35
|
+
/** Without a dedicated cgroup, a step this old is assumed to use its reserved memory. */
|
|
36
|
+
readonly memoryRampMs: number;
|
|
37
|
+
/**
|
|
38
|
+
* Decision 0014 operator ceilings, applied by every governor itself so they hold even
|
|
39
|
+
* without the bridge's runtime settings file (before its first write, after a failed
|
|
40
|
+
* write, or a file that can't be read): the lower of FORGE_HOST_CPU_BUDGET_PERCENT (as a
|
|
41
|
+
* fraction) and an explicitly set FORGE_HOST_LOAD_CPU_BUDGET_FRACTION, and
|
|
42
|
+
* FORGE_HOST_MAX_HEAVY_STEPS. Null when unset.
|
|
43
|
+
*/
|
|
44
|
+
readonly cpuBudgetCeilingFraction?: number | null;
|
|
45
|
+
readonly maxHeavyStepsCeiling?: number | null;
|
|
46
|
+
}
|
|
47
|
+
export declare const defaultHostTestSlots = 1;
|
|
48
|
+
export declare const defaultHostLoadGovernorConfig: HostLoadGovernorConfig;
|
|
49
|
+
export declare function parseHostLoadGovernorMode(value: string | undefined): HostLoadGovernorMode;
|
|
50
|
+
/** The governor admits steps when either rule is on: the host test slot or the budget. */
|
|
51
|
+
export declare function hostLoadGovernorEnabled(config: Pick<HostLoadGovernorConfig, "mode" | "testSlots">): boolean;
|
|
52
|
+
export declare function loadHostLoadGovernorConfig(environment?: Readonly<Record<string, string | undefined>>): HostLoadGovernorConfig;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { defaultHostLoadSampleIntervalMs } from "./host-load-sampler.js";
|
|
2
|
+
export const defaultHostTestSlots = 1;
|
|
3
|
+
export const defaultHostLoadGovernorConfig = {
|
|
4
|
+
mode: "off",
|
|
5
|
+
testSlots: defaultHostTestSlots,
|
|
6
|
+
cpuBudgetFraction: 0.8,
|
|
7
|
+
memoryReserveBytes: 1024 ** 3,
|
|
8
|
+
cpuPressureThreshold: 50,
|
|
9
|
+
backpressureSamples: 2,
|
|
10
|
+
memoryFullThreshold: 5,
|
|
11
|
+
maxWaitMs: 30 * 60_000,
|
|
12
|
+
fallbackSlots: 1,
|
|
13
|
+
antiStarvationMs: 2 * 60_000,
|
|
14
|
+
leaseMs: 2 * 60_000,
|
|
15
|
+
recheckIntervalMs: 1_000,
|
|
16
|
+
sampleStaleMs: 30_000,
|
|
17
|
+
sampleIntervalMs: defaultHostLoadSampleIntervalMs,
|
|
18
|
+
memoryRampMs: 60_000,
|
|
19
|
+
};
|
|
20
|
+
export function parseHostLoadGovernorMode(value) {
|
|
21
|
+
const normalized = value?.trim().toLowerCase() ?? "";
|
|
22
|
+
return normalized === "on" || normalized === "true" || normalized === "1" ? "on" : "off";
|
|
23
|
+
}
|
|
24
|
+
/** The governor admits steps when either rule is on: the host test slot or the budget. */
|
|
25
|
+
export function hostLoadGovernorEnabled(config) {
|
|
26
|
+
return config.mode === "on" || config.testSlots > 0;
|
|
27
|
+
}
|
|
28
|
+
export function loadHostLoadGovernorConfig(environment = process.env) {
|
|
29
|
+
const defaults = defaultHostLoadGovernorConfig;
|
|
30
|
+
return {
|
|
31
|
+
mode: parseHostLoadGovernorMode(environment.FORGE_HOST_LOAD_GOVERNOR),
|
|
32
|
+
testSlots: Math.round(bounded(environment.FORGE_HOST_LOAD_TEST_SLOTS, defaults.testSlots, 0, 8)),
|
|
33
|
+
cpuBudgetFraction: bounded(environment.FORGE_HOST_LOAD_CPU_BUDGET_FRACTION, defaults.cpuBudgetFraction, 0.05, 1),
|
|
34
|
+
memoryReserveBytes: bounded(environment.FORGE_HOST_LOAD_MEMORY_RESERVE_BYTES, defaults.memoryReserveBytes, 0, 1024 ** 4),
|
|
35
|
+
cpuPressureThreshold: bounded(environment.FORGE_HOST_LOAD_CPU_PRESSURE_THRESHOLD, defaults.cpuPressureThreshold, 1, 100),
|
|
36
|
+
backpressureSamples: Math.round(bounded(environment.FORGE_HOST_LOAD_BACKPRESSURE_SAMPLES, defaults.backpressureSamples, 1, 60)),
|
|
37
|
+
memoryFullThreshold: bounded(environment.FORGE_HOST_LOAD_MEMORY_FULL_THRESHOLD, defaults.memoryFullThreshold, 0, 100),
|
|
38
|
+
maxWaitMs: bounded(environment.FORGE_HOST_LOAD_MAX_WAIT_MS, defaults.maxWaitMs, 1_000, 24 * 60 * 60_000),
|
|
39
|
+
fallbackSlots: Math.round(bounded(environment.FORGE_HOST_LOAD_FALLBACK_SLOTS, defaults.fallbackSlots, 1, 64)),
|
|
40
|
+
antiStarvationMs: defaults.antiStarvationMs,
|
|
41
|
+
leaseMs: defaults.leaseMs,
|
|
42
|
+
recheckIntervalMs: defaults.recheckIntervalMs,
|
|
43
|
+
sampleStaleMs: defaults.sampleStaleMs,
|
|
44
|
+
sampleIntervalMs: defaults.sampleIntervalMs,
|
|
45
|
+
memoryRampMs: defaults.memoryRampMs,
|
|
46
|
+
cpuBudgetCeilingFraction: lowestFraction(optionalCpuPercentFraction(environment.FORGE_HOST_CPU_BUDGET_PERCENT), optionalFraction(environment.FORGE_HOST_LOAD_CPU_BUDGET_FRACTION)),
|
|
47
|
+
maxHeavyStepsCeiling: optionalHeavySteps(environment.FORGE_HOST_MAX_HEAVY_STEPS),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** Matches runner-settings' parse of FORGE_HOST_CPU_BUDGET_PERCENT (Number, 10-100); unparsable is unset. */
|
|
51
|
+
function optionalCpuPercentFraction(raw) {
|
|
52
|
+
if (raw === undefined || raw.trim() === "")
|
|
53
|
+
return null;
|
|
54
|
+
const value = Number(raw);
|
|
55
|
+
return Number.isFinite(value) ? Math.min(100, Math.max(10, value)) / 100 : null;
|
|
56
|
+
}
|
|
57
|
+
/** An explicitly set FORGE_HOST_LOAD_CPU_BUDGET_FRACTION is a ceiling too (runner-settings' cpuBudgetCeilings). */
|
|
58
|
+
function optionalFraction(raw) {
|
|
59
|
+
if (raw === undefined || raw.trim() === "")
|
|
60
|
+
return null;
|
|
61
|
+
const value = Number(raw);
|
|
62
|
+
return Number.isFinite(value) ? Math.min(1, Math.max(0.05, value)) : null;
|
|
63
|
+
}
|
|
64
|
+
function lowestFraction(a, b) {
|
|
65
|
+
if (a === null)
|
|
66
|
+
return b;
|
|
67
|
+
if (b === null)
|
|
68
|
+
return a;
|
|
69
|
+
return Math.min(a, b);
|
|
70
|
+
}
|
|
71
|
+
/** Matches runner-settings' parse of FORGE_HOST_MAX_HEAVY_STEPS (parseInt, 1-64); unparsable is unset. */
|
|
72
|
+
function optionalHeavySteps(raw) {
|
|
73
|
+
if (raw === undefined || raw.trim() === "")
|
|
74
|
+
return null;
|
|
75
|
+
const value = Number.parseInt(raw, 10);
|
|
76
|
+
return Number.isFinite(value) ? Math.min(64, Math.max(1, value)) : null;
|
|
77
|
+
}
|
|
78
|
+
function bounded(raw, fallback, minimum, maximum) {
|
|
79
|
+
if (raw === undefined || raw.trim() === "")
|
|
80
|
+
return fallback;
|
|
81
|
+
const value = Number(raw);
|
|
82
|
+
return Number.isFinite(value) ? Math.min(maximum, Math.max(minimum, value)) : fallback;
|
|
83
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { ForgeFailureError } from "../session/session.types.js";
|
|
2
|
+
import { type HostLoadAdmissionDecision } from "./host-load-admission.js";
|
|
3
|
+
import { type HostLoadGovernorConfig } from "./host-load-governor-config.js";
|
|
4
|
+
import type { HostLoadGovernorSource } from "./host-load-sampler.js";
|
|
5
|
+
import type { HostLoadStepCost } from "./host-load-step-class.js";
|
|
6
|
+
import { type HostLoadStepProcess, type HostLoadStepTerminator } from "./host-load-step-reaper.js";
|
|
7
|
+
/** What HostLoadStepGate.attachProcess takes, so a gate's callers need only this module. */
|
|
8
|
+
export type { HostLoadStepProcess } from "./host-load-step-reaper.js";
|
|
9
|
+
import { type HostLoadOwnerLiveness, type HostLoadToken, type HostLoadTokenOwner, type HostLoadTokenPriority } from "./host-load-token-ledger.js";
|
|
10
|
+
export interface HostLoadStepRequest {
|
|
11
|
+
readonly stepClass: string;
|
|
12
|
+
/** What the step is, for diagnostics (for example the validation check id). */
|
|
13
|
+
readonly label: string;
|
|
14
|
+
readonly cost: HostLoadStepCost;
|
|
15
|
+
readonly dispatchAttemptId: string | null;
|
|
16
|
+
readonly priority: HostLoadTokenPriority;
|
|
17
|
+
}
|
|
18
|
+
export interface HostLoadAcquireOptions {
|
|
19
|
+
readonly signal?: AbortSignal;
|
|
20
|
+
/** Called once, when the first admission decision says the step must wait. */
|
|
21
|
+
readonly onWaiting?: (decision: HostLoadAdmissionDecision) => void | Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export interface HostLoadAdmission {
|
|
24
|
+
readonly tokenId: string;
|
|
25
|
+
readonly fencingToken: number;
|
|
26
|
+
readonly stepClass: string;
|
|
27
|
+
/** Time spent queued; never counted as step execution time. */
|
|
28
|
+
readonly waitMs: number;
|
|
29
|
+
readonly decision: HostLoadAdmissionDecision;
|
|
30
|
+
/**
|
|
31
|
+
* Aborted when the reservation is lost while the step runs: with
|
|
32
|
+
* host_capacity_token_fenced when the token was fenced (this owner stalled
|
|
33
|
+
* past its lease and another process saw it), or with
|
|
34
|
+
* host_capacity_ledger_unavailable when it could not be renewed for a whole
|
|
35
|
+
* lease. The step must stop; the token keeps its cost until it is released.
|
|
36
|
+
*/
|
|
37
|
+
readonly fenced: AbortSignal;
|
|
38
|
+
/** Records the step's process tree, so a reclaim stops it before reusing its capacity. */
|
|
39
|
+
attachProcess(stepProcess: HostLoadStepProcess): Promise<void>;
|
|
40
|
+
/** Returns the token. Rejects with host_capacity_token_fenced if it was fenced or reclaimed. */
|
|
41
|
+
release(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Instead of release: the step ended but part of its process tree could not
|
|
44
|
+
* be proven gone. The token stays held, with its cost, and is returned only
|
|
45
|
+
* once `stillGone` (re-checked on every renewal) resolves true. Without a
|
|
46
|
+
* check, the token's recorded process tree is checked instead; with neither,
|
|
47
|
+
* the token is kept until attempt teardown has stopped the attempt's processes.
|
|
48
|
+
*/
|
|
49
|
+
retainUntilStopped(stillGone: (() => Promise<boolean>) | null, reason: Error): void;
|
|
50
|
+
}
|
|
51
|
+
/** The gate heavy steps call before launching. */
|
|
52
|
+
export interface HostLoadStepGate {
|
|
53
|
+
readonly enabled: boolean;
|
|
54
|
+
/** The longest a step may queue before host_capacity_timeout, when the gate knows it. */
|
|
55
|
+
readonly maxWaitMs?: number;
|
|
56
|
+
acquire(request: HostLoadStepRequest, options?: HostLoadAcquireOptions): Promise<HostLoadAdmission>;
|
|
57
|
+
}
|
|
58
|
+
export declare class HostCapacityTimeoutError extends ForgeFailureError {
|
|
59
|
+
readonly stepClass: string;
|
|
60
|
+
readonly label: string;
|
|
61
|
+
readonly waitMs: number;
|
|
62
|
+
readonly code = "host_capacity_timeout";
|
|
63
|
+
constructor(stepClass: string, label: string, waitMs: number, detail: string);
|
|
64
|
+
}
|
|
65
|
+
export interface HostLoadGovernorOptions {
|
|
66
|
+
readonly stateRoot: string;
|
|
67
|
+
readonly load: HostLoadGovernorSource;
|
|
68
|
+
readonly config?: HostLoadGovernorConfig;
|
|
69
|
+
readonly liveness?: HostLoadOwnerLiveness;
|
|
70
|
+
/** Owner recorded on tokens (tests model separate processes); defaults to this process. */
|
|
71
|
+
readonly owner?: HostLoadTokenOwner;
|
|
72
|
+
readonly now?: () => number;
|
|
73
|
+
/** Test seam for the ledger's process-tree reaper. */
|
|
74
|
+
readonly terminateStep?: HostLoadStepTerminator;
|
|
75
|
+
/** Test seam for the host suspend counter. */
|
|
76
|
+
readonly suspendedMs?: () => number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Host-wide admission for heavy steps (decision 0013). Waiters queue
|
|
80
|
+
* foreground before background and FIFO within a class; a waiter that fits may
|
|
81
|
+
* overtake an unfit head until the head has waited past the anti-starvation
|
|
82
|
+
* window. Running steps are never preempted. Reserved cost comes from the
|
|
83
|
+
* shared token ledger, so other processes' steps count against the same budget.
|
|
84
|
+
* The host test slot (decision 0018) comes first: a heavy step waits for it in
|
|
85
|
+
* the host-wide queue the ledger keeps, then for the budget when that is on.
|
|
86
|
+
*/
|
|
87
|
+
export declare class HostLoadGovernor implements HostLoadStepGate {
|
|
88
|
+
private readonly options;
|
|
89
|
+
readonly enabled: boolean;
|
|
90
|
+
private readonly config;
|
|
91
|
+
private readonly ledger;
|
|
92
|
+
private readonly waiters;
|
|
93
|
+
private readonly held;
|
|
94
|
+
private recheckTimer;
|
|
95
|
+
private renewTimer;
|
|
96
|
+
private draining;
|
|
97
|
+
private drainAgain;
|
|
98
|
+
/** Own cgroup anonymous memory with no heavy step running: the bridge's baseline. */
|
|
99
|
+
private idleOwnMemoryBytes;
|
|
100
|
+
/** The bridge's pushed settings (decision 0014), shared with every governor on this state root. */
|
|
101
|
+
private readonly runtimeSettings;
|
|
102
|
+
constructor(options: HostLoadGovernorOptions);
|
|
103
|
+
get maxWaitMs(): number;
|
|
104
|
+
acquire(request: HostLoadStepRequest, options?: HostLoadAcquireOptions): Promise<HostLoadAdmission>;
|
|
105
|
+
/** Reclaims tokens whose owner is gone or whose lease expired (bridge startup). */
|
|
106
|
+
reclaim(): Promise<readonly HostLoadToken[]>;
|
|
107
|
+
/** Returns dead owners' unrecorded tokens the caller proved stopped (see the ledger). */
|
|
108
|
+
releaseOrphaned(belongs: (token: HostLoadToken) => boolean): Promise<readonly HostLoadToken[]>;
|
|
109
|
+
/**
|
|
110
|
+
* Attempt teardown: cancels the attempt's queued steps and stops its running
|
|
111
|
+
* ones (their `fenced` signal aborts with dispatch_cancelled). A running step
|
|
112
|
+
* keeps its token, and its cost, until the step itself returns it once its
|
|
113
|
+
* processes are gone, so the capacity is never reused while it still runs.
|
|
114
|
+
*/
|
|
115
|
+
stopAttempt(dispatchAttemptId: string): void;
|
|
116
|
+
/**
|
|
117
|
+
* Cancels the attempt's queued steps and returns every token it holds at
|
|
118
|
+
* once. Only for callers that know the attempt's steps are no longer running;
|
|
119
|
+
* attempt teardown uses stopAttempt.
|
|
120
|
+
*/
|
|
121
|
+
releaseAttempt(dispatchAttemptId: string): Promise<void>;
|
|
122
|
+
private cancelAttemptWaiters;
|
|
123
|
+
/** Waiting and admitted steps of this process, for diagnostics. */
|
|
124
|
+
snapshot(): {
|
|
125
|
+
readonly waiting: number;
|
|
126
|
+
readonly held: readonly HostLoadToken[];
|
|
127
|
+
};
|
|
128
|
+
private drain;
|
|
129
|
+
private drainOnce;
|
|
130
|
+
/**
|
|
131
|
+
* Decision 0014: the CPU budget (or yield budget) and the heavy-step limit the
|
|
132
|
+
* bridge has in force apply from the next admission; running steps keep theirs.
|
|
133
|
+
* The operator's env ceilings hold with or without the file, and over whatever it says.
|
|
134
|
+
*/
|
|
135
|
+
private budgetPass;
|
|
136
|
+
/** The host test slot first (decision 0018), then the budget when it is on. */
|
|
137
|
+
private decide;
|
|
138
|
+
private readSignals;
|
|
139
|
+
/**
|
|
140
|
+
* Consecutive pressured samples ending with the latest one, read from the
|
|
141
|
+
* sampler's history so samples taken while nothing waited still count. A gap
|
|
142
|
+
* in sampling (sampler stopped, host suspended) breaks the streak.
|
|
143
|
+
*/
|
|
144
|
+
private pressureStreak;
|
|
145
|
+
/**
|
|
146
|
+
* Memory the running steps already use, which MemAvailable already reflects,
|
|
147
|
+
* so the budget does not count it twice. Measured from the dedicated cgroup
|
|
148
|
+
* above its idle baseline; without one, a step admitted longer than the ramp
|
|
149
|
+
* window ago is assumed to hold its reservation. Never above the reservations.
|
|
150
|
+
*/
|
|
151
|
+
private runningStepsMemoryBytes;
|
|
152
|
+
private logicalCpus;
|
|
153
|
+
private settle;
|
|
154
|
+
/** Renewed tokens reset their lease clock; fenced ones stop their steps (they keep their cost until released). */
|
|
155
|
+
private applyHeldOutcome;
|
|
156
|
+
/** The step must stop: its reservation is no longer guaranteed. */
|
|
157
|
+
private lose;
|
|
158
|
+
/** Ledger failures reach every queued step instead of leaving it to time out. */
|
|
159
|
+
private failWaiters;
|
|
160
|
+
private attachProcess;
|
|
161
|
+
/**
|
|
162
|
+
* Returns a step's token. It stays held (and renewed) until the ledger takes
|
|
163
|
+
* it back, so a ledger outage cannot leave it to lapse into a fenced
|
|
164
|
+
* reservation; the renewal timer retries the release. A fenced token is
|
|
165
|
+
* removed too (its step is over) and the release reports the fence.
|
|
166
|
+
*/
|
|
167
|
+
private releaseHeld;
|
|
168
|
+
private forget;
|
|
169
|
+
private armRecheck;
|
|
170
|
+
private disarmRecheck;
|
|
171
|
+
private armRenewal;
|
|
172
|
+
private disarmRenewal;
|
|
173
|
+
/**
|
|
174
|
+
* The step ended but part of its process tree may still run: its token stays
|
|
175
|
+
* held and renewed, with its cost, and the step's own termination check is
|
|
176
|
+
* retried on every renewal tick until it proves the tree gone.
|
|
177
|
+
*/
|
|
178
|
+
private retainUntilStopped;
|
|
179
|
+
/**
|
|
180
|
+
* Attempt teardown stopped the attempt's processes (its process supervisor
|
|
181
|
+
* proved them gone): tokens kept for a tree nothing else can check are returned.
|
|
182
|
+
*/
|
|
183
|
+
releaseRetainedAfterTeardown(dispatchAttemptId: string): Promise<void>;
|
|
184
|
+
private checkRetained;
|
|
185
|
+
private renewHeld;
|
|
186
|
+
}
|