@loucompanion/forge-bridge 0.1.4-dev.8f4846c6e7a9 → 0.1.4-dev.93045aceba29

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.
Files changed (109) hide show
  1. package/README.md +33 -0
  2. package/dist/bridge/bridge.base.d.ts +9 -0
  3. package/dist/bridge/bridge.service.d.ts +70 -1
  4. package/dist/bridge/bridge.service.js +288 -8
  5. package/dist/bridge/bridge.types.d.ts +11 -0
  6. package/dist/bridge/local-yield-flag.d.ts +6 -0
  7. package/dist/bridge/local-yield-flag.js +34 -0
  8. package/dist/cleanup/cleanup.service.d.ts +9 -0
  9. package/dist/cleanup/cleanup.service.js +72 -109
  10. package/dist/cleanup/cleanup.types.d.ts +4 -0
  11. package/dist/cleanup/forge-owned-paths.d.ts +9 -0
  12. package/dist/cleanup/forge-owned-paths.js +53 -0
  13. package/dist/cleanup/host-runtime-ledger.d.ts +5 -0
  14. package/dist/cleanup/host-runtime-ledger.js +109 -0
  15. package/dist/cleanup/managed-branch-sweep.d.ts +27 -0
  16. package/dist/cleanup/managed-branch-sweep.js +111 -0
  17. package/dist/cli/cli.help.js +18 -0
  18. package/dist/cli/cli.service.d.ts +16 -0
  19. package/dist/cli/cli.service.js +140 -16
  20. package/dist/cli/local-config.d.ts +4 -0
  21. package/dist/cli/local-config.js +26 -4
  22. package/dist/runtime/aspnet-runtime-prebuild.js +94 -6
  23. package/dist/runtime/host-load-admission.d.ts +9 -1
  24. package/dist/runtime/host-load-admission.js +19 -4
  25. package/dist/runtime/host-load-child-environment.d.ts +22 -0
  26. package/dist/runtime/host-load-child-environment.js +67 -0
  27. package/dist/runtime/host-load-forge-cgroups.d.ts +41 -0
  28. package/dist/runtime/host-load-forge-cgroups.js +93 -4
  29. package/dist/runtime/host-load-governor-config.d.ts +9 -0
  30. package/dist/runtime/host-load-governor-config.js +30 -0
  31. package/dist/runtime/host-load-governor.d.ts +62 -11
  32. package/dist/runtime/host-load-governor.js +168 -42
  33. package/dist/runtime/host-load-runtime-settings.d.ts +25 -0
  34. package/dist/runtime/host-load-runtime-settings.js +78 -0
  35. package/dist/runtime/host-load-sampler.d.ts +5 -0
  36. package/dist/runtime/host-load-sampler.js +12 -4
  37. package/dist/runtime/host-load-step-class.d.ts +35 -0
  38. package/dist/runtime/host-load-step-class.js +60 -0
  39. package/dist/runtime/host-load-step-reaper.d.ts +27 -0
  40. package/dist/runtime/host-load-step-reaper.js +95 -0
  41. package/dist/runtime/host-load-step-run.d.ts +73 -0
  42. package/dist/runtime/host-load-step-run.js +102 -0
  43. package/dist/runtime/host-load-token-ledger.d.ts +58 -19
  44. package/dist/runtime/host-load-token-ledger.js +111 -35
  45. package/dist/runtime/host-runtime-lease.d.ts +61 -2
  46. package/dist/runtime/host-runtime-lease.js +255 -11
  47. package/dist/runtime/playwright-host-runtime-adapter.d.ts +7 -0
  48. package/dist/runtime/playwright-host-runtime-adapter.js +13 -1
  49. package/dist/runtime/runner-settings.d.ts +100 -0
  50. package/dist/runtime/runner-settings.js +214 -0
  51. package/dist/sandbox/host-capacity-broker.d.ts +107 -0
  52. package/dist/sandbox/host-capacity-broker.js +315 -0
  53. package/dist/sandbox/runtime.d.ts +10 -1
  54. package/dist/sandbox/runtime.js +45 -2
  55. package/dist/sandbox/sandbox-cgroups.d.ts +14 -0
  56. package/dist/sandbox/sandbox-cgroups.js +42 -0
  57. package/dist/session/managed-attempt-tools.d.ts +2 -0
  58. package/dist/session/managed-attempt-tools.js +156 -37
  59. package/dist/session/managed-command-host-load.d.ts +45 -0
  60. package/dist/session/managed-command-host-load.js +82 -0
  61. package/dist/session/mcp-argument-coercion.d.ts +23 -0
  62. package/dist/session/mcp-argument-coercion.js +121 -0
  63. package/dist/session/phase-completion.artifacts.d.ts +7 -0
  64. package/dist/session/phase-completion.artifacts.js +94 -25
  65. package/dist/session/phase-completion.command-validation.d.ts +7 -0
  66. package/dist/session/phase-completion.command-validation.js +77 -0
  67. package/dist/session/phase-completion.d.ts +4 -0
  68. package/dist/session/phase-completion.helpers.js +13 -12
  69. package/dist/session/phase-completion.js +4 -0
  70. package/dist/session/phase-completion.prompts.js +6 -1
  71. package/dist/session/phase-completion.runtime.d.ts +5 -3
  72. package/dist/session/phase-completion.runtime.js +49 -19
  73. package/dist/session/phase-completion.schema-parts.d.ts +19 -0
  74. package/dist/session/phase-completion.schema-parts.js +188 -0
  75. package/dist/session/phase-completion.schemas.d.ts +3 -3
  76. package/dist/session/phase-completion.schemas.js +184 -209
  77. package/dist/session/phase-completion.validation.d.ts +2 -3
  78. package/dist/session/phase-completion.validation.js +46 -92
  79. package/dist/session/pre-publication-host-load.js +31 -16
  80. package/dist/session/pre-publication-process.d.ts +3 -0
  81. package/dist/session/pre-publication-process.js +23 -11
  82. package/dist/session/pre-publication-validation.js +4 -0
  83. package/dist/session/provider-cli/provider-cli.process.js +2 -2
  84. package/dist/session/provider-cli/provider-cli.prompts.js +5 -2
  85. package/dist/session/provider-cli/provider-cli.service.d.ts +6 -0
  86. package/dist/session/provider-cli/provider-cli.service.js +123 -15
  87. package/dist/session/provider-cli/provider-cli.types.d.ts +30 -0
  88. package/dist/session/sandbox-host-capacity-gate.d.ts +30 -0
  89. package/dist/session/sandbox-host-capacity-gate.js +229 -0
  90. package/dist/session/session.base.d.ts +6 -1
  91. package/dist/session/session.service.d.ts +7 -2
  92. package/dist/session/session.service.js +115 -5
  93. package/dist/session/session.types.js +2 -2
  94. package/dist/session/workspace-runtime-fingerprint.d.ts +32 -0
  95. package/dist/session/workspace-runtime-fingerprint.js +138 -0
  96. package/dist/shared/process/host-process-supervisor.d.ts +1 -0
  97. package/dist/shared/process/host-process-supervisor.js +1 -1
  98. package/dist/shared/process/process-tree.d.ts +52 -0
  99. package/dist/shared/process/process-tree.js +144 -0
  100. package/dist/shared/process/process.d.ts +67 -0
  101. package/dist/shared/process/process.js +213 -11
  102. package/dist/version.d.ts +1 -1
  103. package/dist/version.js +1 -1
  104. package/dist/worktree/dependency-warmer.d.ts +25 -0
  105. package/dist/worktree/dependency-warmer.js +46 -10
  106. package/dist/worktree/worktree.service.d.ts +10 -1
  107. package/dist/worktree/worktree.service.js +42 -2
  108. package/dist/worktree/worktree.types.d.ts +7 -0
  109. package/package.json +2 -2
@@ -0,0 +1,67 @@
1
+ import path from "node:path";
2
+ import { HostLoadGovernor } from "./host-load-governor.js";
3
+ import { 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
+ const cgroupUnitEnvironment = "FORGE_BRIDGE_CGROUP_UNIT";
13
+ /** True for the variables that carry the governor into a child process. */
14
+ /**
15
+ * A process environment with every host load governor variable removed
16
+ * (FORGE_HOST_LOAD_*, any case). A governed step's own processes must never join
17
+ * the host ledger, or broker control, that their step's token is held in.
18
+ */
19
+ export function withoutHostLoadGovernorVariables(environment) {
20
+ const kept = {};
21
+ for (const [name, value] of Object.entries(environment)) {
22
+ if (!name.toUpperCase().startsWith("FORGE_HOST_LOAD_"))
23
+ kept[name] = value;
24
+ }
25
+ return kept;
26
+ }
27
+ // Decision 0014 operator ceilings the child's governor applies itself (host-load-governor-config.ts).
28
+ const hostCeilingEnvironmentNames = new Set(["FORGE_HOST_CPU_BUDGET_PERCENT", "FORGE_HOST_MAX_HEAVY_STEPS"]);
29
+ export function isHostLoadChildEnvironmentName(name) {
30
+ return name.startsWith("FORGE_HOST_LOAD_") || name === cgroupUnitEnvironment || hostCeilingEnvironmentNames.has(name);
31
+ }
32
+ /**
33
+ * The variables a child needs to share this bridge's ledger: the flag, the
34
+ * state root, the operator's FORGE_HOST_LOAD_* tuning and the bridge's cgroup
35
+ * unit. Empty with the governor off, so the child stays ungoverned.
36
+ */
37
+ export function hostLoadChildEnvironment(stateRoot, environment = process.env) {
38
+ if (loadHostLoadGovernorConfig(environment).mode !== "on")
39
+ return {};
40
+ const forwarded = {};
41
+ for (const [name, value] of Object.entries(environment)) {
42
+ if (typeof value === "string" && value.trim() && isHostLoadChildEnvironmentName(name))
43
+ forwarded[name] = value;
44
+ }
45
+ forwarded[hostLoadGovernorEnvironment] = "on";
46
+ forwarded[hostLoadStateRootEnvironment] = path.resolve(stateRoot);
47
+ return forwarded;
48
+ }
49
+ /**
50
+ * The governor a child process uses, from the variables above. Undefined when
51
+ * the flag is off or no state root was handed over: the child is ungoverned.
52
+ */
53
+ export function hostLoadGovernorFromChildEnvironment(environment = process.env) {
54
+ const config = loadHostLoadGovernorConfig(environment);
55
+ const stateRoot = environment[hostLoadStateRootEnvironment]?.trim();
56
+ if (config.mode !== "on" || !stateRoot || !path.isAbsolute(stateRoot))
57
+ return undefined;
58
+ return new HostLoadGovernor({
59
+ stateRoot,
60
+ config,
61
+ load: new HostLoadSampler({
62
+ cgroupUnit: environment[cgroupUnitEnvironment]?.trim() || null,
63
+ // The bridge registers its Docker sandboxes here: their CPU is Forge's own here too.
64
+ forgeCgroups: new FileForgeCgroupRegistry(stateRoot),
65
+ }),
66
+ });
67
+ }
@@ -1,4 +1,45 @@
1
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
+ }
2
43
  /**
3
44
  * Free memory under the tightest `memory.max` from the cgroup up to and
4
45
  * including the root (`memory.max − memory.current`), or null when no limit is
@@ -1,9 +1,98 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import fs from "node:fs/promises";
1
3
  import path from "node:path";
2
4
  import { readOptional } from "./host-facts.js";
3
- import { parseNonNegativeInteger } from "./host-pressure-stall.js";
4
- // Decision 0013 cgroup accounting: the memory budget follows the tightest
5
- // `memory.max` on the bridge's cgroup path, so a step is never admitted past
6
- // the limit that would OOM-kill it.
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
+ }
7
96
  /**
8
97
  * Free memory under the tightest `memory.max` from the cgroup up to and
9
98
  * including the root (`memory.max − memory.current`), or null when no limit is
@@ -28,6 +28,15 @@ export interface HostLoadGovernorConfig {
28
28
  readonly sampleIntervalMs: number;
29
29
  /** Without a dedicated cgroup, a step this old is assumed to use its reserved memory. */
30
30
  readonly memoryRampMs: number;
31
+ /**
32
+ * Decision 0014 operator ceilings, applied by every governor itself so they hold even
33
+ * without the bridge's runtime settings file (before its first write, after a failed
34
+ * write, or a file that can't be read): the lower of FORGE_HOST_CPU_BUDGET_PERCENT (as a
35
+ * fraction) and an explicitly set FORGE_HOST_LOAD_CPU_BUDGET_FRACTION, and
36
+ * FORGE_HOST_MAX_HEAVY_STEPS. Null when unset.
37
+ */
38
+ readonly cpuBudgetCeilingFraction?: number | null;
39
+ readonly maxHeavyStepsCeiling?: number | null;
31
40
  }
32
41
  export declare const defaultHostLoadGovernorConfig: HostLoadGovernorConfig;
33
42
  export declare function parseHostLoadGovernorMode(value: string | undefined): HostLoadGovernorMode;
@@ -36,8 +36,38 @@ export function loadHostLoadGovernorConfig(environment = process.env) {
36
36
  sampleStaleMs: defaults.sampleStaleMs,
37
37
  sampleIntervalMs: defaults.sampleIntervalMs,
38
38
  memoryRampMs: defaults.memoryRampMs,
39
+ cpuBudgetCeilingFraction: lowestFraction(optionalCpuPercentFraction(environment.FORGE_HOST_CPU_BUDGET_PERCENT), optionalFraction(environment.FORGE_HOST_LOAD_CPU_BUDGET_FRACTION)),
40
+ maxHeavyStepsCeiling: optionalHeavySteps(environment.FORGE_HOST_MAX_HEAVY_STEPS),
39
41
  };
40
42
  }
43
+ /** Matches runner-settings' parse of FORGE_HOST_CPU_BUDGET_PERCENT (Number, 10-100); unparsable is unset. */
44
+ function optionalCpuPercentFraction(raw) {
45
+ if (raw === undefined || raw.trim() === "")
46
+ return null;
47
+ const value = Number(raw);
48
+ return Number.isFinite(value) ? Math.min(100, Math.max(10, value)) / 100 : null;
49
+ }
50
+ /** An explicitly set FORGE_HOST_LOAD_CPU_BUDGET_FRACTION is a ceiling too (runner-settings' cpuBudgetCeilings). */
51
+ function optionalFraction(raw) {
52
+ if (raw === undefined || raw.trim() === "")
53
+ return null;
54
+ const value = Number(raw);
55
+ return Number.isFinite(value) ? Math.min(1, Math.max(0.05, value)) : null;
56
+ }
57
+ function lowestFraction(a, b) {
58
+ if (a === null)
59
+ return b;
60
+ if (b === null)
61
+ return a;
62
+ return Math.min(a, b);
63
+ }
64
+ /** Matches runner-settings' parse of FORGE_HOST_MAX_HEAVY_STEPS (parseInt, 1-64); unparsable is unset. */
65
+ function optionalHeavySteps(raw) {
66
+ if (raw === undefined || raw.trim() === "")
67
+ return null;
68
+ const value = Number.parseInt(raw, 10);
69
+ return Number.isFinite(value) ? Math.min(64, Math.max(1, value)) : null;
70
+ }
41
71
  function bounded(raw, fallback, minimum, maximum) {
42
72
  if (raw === undefined || raw.trim() === "")
43
73
  return fallback;
@@ -3,6 +3,7 @@ import { type HostLoadAdmissionDecision } from "./host-load-admission.js";
3
3
  import { type HostLoadGovernorConfig } from "./host-load-governor-config.js";
4
4
  import type { HostLoadGovernorSource } from "./host-load-sampler.js";
5
5
  import type { HostLoadStepCost } from "./host-load-step-class.js";
6
+ import { type HostLoadStepProcess, type HostLoadStepTerminator } from "./host-load-step-reaper.js";
6
7
  import { type HostLoadOwnerLiveness, type HostLoadToken, type HostLoadTokenOwner, type HostLoadTokenPriority } from "./host-load-token-ledger.js";
7
8
  export interface HostLoadStepRequest {
8
9
  readonly stepClass: string;
@@ -25,17 +26,31 @@ export interface HostLoadAdmission {
25
26
  readonly waitMs: number;
26
27
  readonly decision: HostLoadAdmissionDecision;
27
28
  /**
28
- * Aborted when the reservation is lost while the step runs: the token was
29
- * fenced (its lease lapsed and another process saw it), or the ledger could
30
- * not renew it for a whole lease. The reason is the failure; the step must stop.
29
+ * Aborted when the reservation is lost while the step runs: with
30
+ * host_capacity_token_fenced when the token was fenced (this owner stalled
31
+ * past its lease and another process saw it), or with
32
+ * host_capacity_ledger_unavailable when it could not be renewed for a whole
33
+ * lease. The step must stop; the token keeps its cost until it is released.
31
34
  */
32
- readonly signal: AbortSignal;
35
+ readonly fenced: AbortSignal;
36
+ /** Records the step's process tree, so a reclaim stops it before reusing its capacity. */
37
+ attachProcess(stepProcess: HostLoadStepProcess): Promise<void>;
33
38
  /** Returns the token. Rejects with host_capacity_token_fenced if it was fenced or reclaimed. */
34
39
  release(): Promise<void>;
40
+ /**
41
+ * Instead of release: the step ended but part of its process tree could not
42
+ * be proven gone. The token stays held, with its cost, and is returned only
43
+ * once `stillGone` (re-checked on every renewal) resolves true. Without a
44
+ * check, the token's recorded process tree is checked instead; with neither,
45
+ * the token is kept until attempt teardown has stopped the attempt's processes.
46
+ */
47
+ retainUntilStopped(stillGone: (() => Promise<boolean>) | null, reason: Error): void;
35
48
  }
36
49
  /** The gate heavy steps call before launching. */
37
50
  export interface HostLoadStepGate {
38
51
  readonly enabled: boolean;
52
+ /** The longest a step may queue before host_capacity_timeout, when the gate knows it. */
53
+ readonly maxWaitMs?: number;
39
54
  acquire(request: HostLoadStepRequest, options?: HostLoadAcquireOptions): Promise<HostLoadAdmission>;
40
55
  }
41
56
  export declare class HostCapacityTimeoutError extends ForgeFailureError {
@@ -53,6 +68,10 @@ export interface HostLoadGovernorOptions {
53
68
  /** Owner recorded on tokens (tests model separate processes); defaults to this process. */
54
69
  readonly owner?: HostLoadTokenOwner;
55
70
  readonly now?: () => number;
71
+ /** Test seam for the ledger's process-tree reaper. */
72
+ readonly terminateStep?: HostLoadStepTerminator;
73
+ /** Test seam for the host suspend counter. */
74
+ readonly suspendedMs?: () => number;
56
75
  }
57
76
  /**
58
77
  * Host-wide admission for heavy steps (decision 0013). Waiters queue
@@ -74,12 +93,29 @@ export declare class HostLoadGovernor implements HostLoadStepGate {
74
93
  private drainAgain;
75
94
  /** Own cgroup anonymous memory with no heavy step running: the bridge's baseline. */
76
95
  private idleOwnMemoryBytes;
96
+ /** The bridge's pushed settings (decision 0014), shared with every governor on this state root. */
97
+ private readonly runtimeSettings;
77
98
  constructor(options: HostLoadGovernorOptions);
99
+ get maxWaitMs(): number;
78
100
  acquire(request: HostLoadStepRequest, options?: HostLoadAcquireOptions): Promise<HostLoadAdmission>;
79
101
  /** Reclaims tokens whose owner is gone or whose lease expired (bridge startup). */
80
102
  reclaim(): Promise<readonly HostLoadToken[]>;
81
- /** Attempt teardown: cancels the attempt's waiters and returns every token it holds. */
103
+ /** Returns dead owners' unrecorded tokens the caller proved stopped (see the ledger). */
104
+ releaseOrphaned(belongs: (token: HostLoadToken) => boolean): Promise<readonly HostLoadToken[]>;
105
+ /**
106
+ * Attempt teardown: cancels the attempt's queued steps and stops its running
107
+ * ones (their `fenced` signal aborts with dispatch_cancelled). A running step
108
+ * keeps its token, and its cost, until the step itself returns it once its
109
+ * processes are gone, so the capacity is never reused while it still runs.
110
+ */
111
+ stopAttempt(dispatchAttemptId: string): void;
112
+ /**
113
+ * Cancels the attempt's queued steps and returns every token it holds at
114
+ * once. Only for callers that know the attempt's steps are no longer running;
115
+ * attempt teardown uses stopAttempt.
116
+ */
82
117
  releaseAttempt(dispatchAttemptId: string): Promise<void>;
118
+ private cancelAttemptWaiters;
83
119
  /** Waiting and admitted steps of this process, for diagnostics. */
84
120
  snapshot(): {
85
121
  readonly waiting: number;
@@ -103,21 +139,36 @@ export declare class HostLoadGovernor implements HostLoadStepGate {
103
139
  private runningStepsMemoryBytes;
104
140
  private logicalCpus;
105
141
  private settle;
142
+ /** Renewed tokens reset their lease clock; fenced ones stop their steps (they keep their cost until released). */
143
+ private applyHeldOutcome;
144
+ /** The step must stop: its reservation is no longer guaranteed. */
145
+ private lose;
146
+ /** Ledger failures reach every queued step instead of leaving it to time out. */
147
+ private failWaiters;
148
+ private attachProcess;
106
149
  /**
107
150
  * Returns a step's token. It stays held (and renewed) until the ledger takes
108
151
  * it back, so a ledger outage cannot leave it to lapse into a fenced
109
- * reservation; the renewal timer retries the release.
152
+ * reservation; the renewal timer retries the release. A fenced token is
153
+ * removed too (its step is over) and the release reports the fence.
110
154
  */
111
155
  private releaseHeld;
112
156
  private forget;
113
- /** The step must stop: its reservation is no longer guaranteed. */
114
- private lose;
115
- private onHeldTokens;
116
- /** Ledger failures reach every queued step instead of leaving it to time out. */
117
- private failWaiters;
118
157
  private armRecheck;
119
158
  private disarmRecheck;
120
159
  private armRenewal;
121
160
  private disarmRenewal;
161
+ /**
162
+ * The step ended but part of its process tree may still run: its token stays
163
+ * held and renewed, with its cost, and the step's own termination check is
164
+ * retried on every renewal tick until it proves the tree gone.
165
+ */
166
+ private retainUntilStopped;
167
+ /**
168
+ * Attempt teardown stopped the attempt's processes (its process supervisor
169
+ * proved them gone): tokens kept for a tree nothing else can check are returned.
170
+ */
171
+ releaseRetainedAfterTeardown(dispatchAttemptId: string): Promise<void>;
172
+ private checkRetained;
122
173
  private renewHeld;
123
174
  }