@loucompanion/forge-bridge 0.1.4-dev.bbb61d3d6127 → 0.1.4-dev.bd2cfb6b7438

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 (169) hide show
  1. package/README.md +55 -0
  2. package/dist/bridge/bridge-chat-provider-command.d.ts +16 -0
  3. package/dist/bridge/bridge-chat-provider-command.js +137 -0
  4. package/dist/bridge/bridge-chat.types.d.ts +10 -0
  5. package/dist/bridge/bridge-chat.types.js +16 -1
  6. package/dist/bridge/bridge.base.d.ts +9 -0
  7. package/dist/bridge/bridge.service.d.ts +71 -2
  8. package/dist/bridge/bridge.service.js +292 -118
  9. package/dist/bridge/bridge.types.d.ts +11 -0
  10. package/dist/bridge/local-yield-flag.d.ts +6 -0
  11. package/dist/bridge/local-yield-flag.js +34 -0
  12. package/dist/bridge/runner-host-capabilities.d.ts +3 -0
  13. package/dist/bridge/runner-host-capabilities.js +13 -0
  14. package/dist/cleanup/cleanup.service.d.ts +9 -0
  15. package/dist/cleanup/cleanup.service.js +72 -109
  16. package/dist/cleanup/cleanup.types.d.ts +4 -0
  17. package/dist/cleanup/forge-owned-paths.d.ts +9 -0
  18. package/dist/cleanup/forge-owned-paths.js +53 -0
  19. package/dist/cleanup/host-runtime-ledger.d.ts +5 -0
  20. package/dist/cleanup/host-runtime-ledger.js +109 -0
  21. package/dist/cleanup/managed-branch-sweep.d.ts +27 -0
  22. package/dist/cleanup/managed-branch-sweep.js +111 -0
  23. package/dist/cli/cli.help.js +18 -0
  24. package/dist/cli/cli.service.d.ts +16 -0
  25. package/dist/cli/cli.service.js +140 -16
  26. package/dist/cli/local-config.d.ts +4 -0
  27. package/dist/cli/local-config.js +26 -4
  28. package/dist/runtime/aspnet-runtime-prebuild.js +94 -6
  29. package/dist/runtime/attempt-database-broker.d.ts +1 -1
  30. package/dist/runtime/docker-engine-probe.d.ts +5 -0
  31. package/dist/runtime/docker-engine-probe.js +7 -0
  32. package/dist/runtime/ephemeral-test-postgres-grant.d.ts +37 -0
  33. package/dist/runtime/ephemeral-test-postgres-grant.js +85 -0
  34. package/dist/runtime/ephemeral-test-postgres.d.ts +57 -0
  35. package/dist/runtime/ephemeral-test-postgres.js +303 -0
  36. package/dist/runtime/host-facts.d.ts +2 -1
  37. package/dist/runtime/host-load-admission.d.ts +18 -3
  38. package/dist/runtime/host-load-admission.js +19 -4
  39. package/dist/runtime/host-load-child-environment.d.ts +27 -0
  40. package/dist/runtime/host-load-child-environment.js +76 -0
  41. package/dist/runtime/host-load-forge-cgroups.d.ts +41 -0
  42. package/dist/runtime/host-load-forge-cgroups.js +93 -4
  43. package/dist/runtime/host-load-governor-config.d.ts +19 -1
  44. package/dist/runtime/host-load-governor-config.js +37 -0
  45. package/dist/runtime/host-load-governor.d.ts +72 -11
  46. package/dist/runtime/host-load-governor.js +284 -67
  47. package/dist/runtime/host-load-runtime-settings.d.ts +25 -0
  48. package/dist/runtime/host-load-runtime-settings.js +78 -0
  49. package/dist/runtime/host-load-sampler.d.ts +39 -4
  50. package/dist/runtime/host-load-sampler.js +83 -9
  51. package/dist/runtime/host-load-step-class.d.ts +37 -0
  52. package/dist/runtime/host-load-step-class.js +117 -2
  53. package/dist/runtime/host-load-step-reaper.d.ts +27 -0
  54. package/dist/runtime/host-load-step-reaper.js +95 -0
  55. package/dist/runtime/host-load-step-run.d.ts +73 -0
  56. package/dist/runtime/host-load-step-run.js +102 -0
  57. package/dist/runtime/host-load-test-slot.d.ts +43 -0
  58. package/dist/runtime/host-load-test-slot.js +81 -0
  59. package/dist/runtime/host-load-token-ledger.d.ts +82 -17
  60. package/dist/runtime/host-load-token-ledger.js +187 -79
  61. package/dist/runtime/host-load-windows-cpu.d.ts +34 -0
  62. package/dist/runtime/host-load-windows-cpu.js +85 -0
  63. package/dist/runtime/host-runtime-lease.d.ts +62 -3
  64. package/dist/runtime/host-runtime-lease.js +258 -13
  65. package/dist/runtime/host-runtime-promotion.d.ts +23 -0
  66. package/dist/runtime/host-runtime-promotion.js +36 -0
  67. package/dist/runtime/native-test-postgres-fallback.d.ts +51 -7
  68. package/dist/runtime/native-test-postgres-fallback.js +60 -8
  69. package/dist/runtime/playwright-host-runtime-adapter.d.ts +7 -0
  70. package/dist/runtime/playwright-host-runtime-adapter.js +13 -1
  71. package/dist/runtime/runner-settings.d.ts +100 -0
  72. package/dist/runtime/runner-settings.js +214 -0
  73. package/dist/sandbox/host-capacity-broker.d.ts +113 -0
  74. package/dist/sandbox/host-capacity-broker.js +334 -0
  75. package/dist/sandbox/runtime.d.ts +10 -1
  76. package/dist/sandbox/runtime.js +45 -2
  77. package/dist/sandbox/sandbox-cgroups.d.ts +14 -0
  78. package/dist/sandbox/sandbox-cgroups.js +42 -0
  79. package/dist/session/architect-waits.d.ts +2 -0
  80. package/dist/session/architect-waits.js +138 -0
  81. package/dist/session/managed-attempt-tools.d.ts +2 -7
  82. package/dist/session/managed-attempt-tools.js +200 -92
  83. package/dist/session/managed-command-host-load.d.ts +47 -0
  84. package/dist/session/managed-command-host-load.js +83 -0
  85. package/dist/session/mcp-argument-coercion.d.ts +23 -0
  86. package/dist/session/mcp-argument-coercion.js +121 -0
  87. package/dist/session/phase-completion.artifacts.d.ts +7 -0
  88. package/dist/session/phase-completion.artifacts.js +96 -26
  89. package/dist/session/phase-completion.command-validation.d.ts +7 -0
  90. package/dist/session/phase-completion.command-validation.js +77 -0
  91. package/dist/session/phase-completion.d.ts +4 -0
  92. package/dist/session/phase-completion.helpers.js +13 -12
  93. package/dist/session/phase-completion.js +4 -0
  94. package/dist/session/phase-completion.prompts.js +6 -1
  95. package/dist/session/phase-completion.runtime.d.ts +5 -3
  96. package/dist/session/phase-completion.runtime.js +50 -20
  97. package/dist/session/phase-completion.schema-parts.d.ts +19 -0
  98. package/dist/session/phase-completion.schema-parts.js +188 -0
  99. package/dist/session/phase-completion.schemas.d.ts +3 -3
  100. package/dist/session/phase-completion.schemas.js +208 -231
  101. package/dist/session/phase-completion.validation.d.ts +2 -3
  102. package/dist/session/phase-completion.validation.js +49 -93
  103. package/dist/session/pre-publication-host-load.js +31 -16
  104. package/dist/session/pre-publication-native-postgres.d.ts +30 -0
  105. package/dist/session/pre-publication-native-postgres.js +111 -0
  106. package/dist/session/pre-publication-pass.d.ts +61 -0
  107. package/dist/session/pre-publication-pass.js +148 -0
  108. package/dist/session/pre-publication-process.d.ts +3 -0
  109. package/dist/session/pre-publication-process.js +23 -11
  110. package/dist/session/pre-publication-step-execution.d.ts +2 -1
  111. package/dist/session/pre-publication-step-execution.js +6 -5
  112. package/dist/session/pre-publication-validation.d.ts +30 -14
  113. package/dist/session/pre-publication-validation.js +211 -134
  114. package/dist/session/provider-cli/claude-background-work.d.ts +66 -0
  115. package/dist/session/provider-cli/claude-background-work.js +180 -0
  116. package/dist/session/provider-cli/pre-publication-repair.d.ts +40 -0
  117. package/dist/session/provider-cli/pre-publication-repair.js +243 -0
  118. package/dist/session/provider-cli/provider-cli.child-environment.d.ts +7 -0
  119. package/dist/session/provider-cli/provider-cli.child-environment.js +73 -0
  120. package/dist/session/provider-cli/provider-cli.codex-models-cache.d.ts +3 -0
  121. package/dist/session/provider-cli/provider-cli.codex-models-cache.js +37 -0
  122. package/dist/session/provider-cli/provider-cli.process.d.ts +13 -22
  123. package/dist/session/provider-cli/provider-cli.process.js +38 -339
  124. package/dist/session/provider-cli/provider-cli.prompts.d.ts +2 -0
  125. package/dist/session/provider-cli/provider-cli.prompts.js +50 -10
  126. package/dist/session/provider-cli/provider-cli.runtime.js +7 -0
  127. package/dist/session/provider-cli/provider-cli.service.d.ts +4 -1
  128. package/dist/session/provider-cli/provider-cli.service.js +93 -162
  129. package/dist/session/provider-cli/provider-cli.types.d.ts +30 -0
  130. package/dist/session/provider-cli/provider-cli.windows-environment.d.ts +10 -0
  131. package/dist/session/provider-cli/provider-cli.windows-environment.js +151 -0
  132. package/dist/session/provider-cli/provider-in-flight-work.d.ts +4 -0
  133. package/dist/session/provider-cli/provider-in-flight-work.js +45 -0
  134. package/dist/session/provider-cli/provider-output-pipes.d.ts +30 -0
  135. package/dist/session/provider-cli/provider-output-pipes.js +67 -0
  136. package/dist/session/provider-cli/provider-output-state.d.ts +2 -0
  137. package/dist/session/provider-cli/provider-output-state.js +6 -0
  138. package/dist/session/qa-sidecar-validation.d.ts +6 -0
  139. package/dist/session/qa-sidecar-validation.js +28 -0
  140. package/dist/session/sandbox-host-capacity-gate.d.ts +30 -0
  141. package/dist/session/sandbox-host-capacity-gate.js +238 -0
  142. package/dist/session/session-redaction.js +31 -181
  143. package/dist/session/session.base.d.ts +6 -1
  144. package/dist/session/session.service.d.ts +7 -2
  145. package/dist/session/session.service.js +115 -5
  146. package/dist/session/session.types.js +4 -4
  147. package/dist/session/sidecar-json-shape.d.ts +31 -0
  148. package/dist/session/sidecar-json-shape.js +217 -0
  149. package/dist/session/workspace-runtime-fingerprint.d.ts +32 -0
  150. package/dist/session/workspace-runtime-fingerprint.js +138 -0
  151. package/dist/shared/process/host-process-supervisor.d.ts +1 -0
  152. package/dist/shared/process/host-process-supervisor.js +1 -1
  153. package/dist/shared/process/process-tree.d.ts +52 -0
  154. package/dist/shared/process/process-tree.js +144 -0
  155. package/dist/shared/process/process.d.ts +67 -0
  156. package/dist/shared/process/process.js +213 -11
  157. package/dist/shared/process/windows-app-execution-alias.d.ts +19 -0
  158. package/dist/shared/process/windows-app-execution-alias.js +45 -0
  159. package/dist/shared/process/windows-launch.d.ts +18 -0
  160. package/dist/shared/process/windows-launch.js +68 -0
  161. package/dist/shared/provider-capabilities.js +5 -6
  162. package/dist/version.d.ts +1 -1
  163. package/dist/version.js +1 -1
  164. package/dist/worktree/dependency-warmer.d.ts +25 -0
  165. package/dist/worktree/dependency-warmer.js +46 -10
  166. package/dist/worktree/worktree.service.d.ts +10 -1
  167. package/dist/worktree/worktree.service.js +42 -2
  168. package/dist/worktree/worktree.types.d.ts +7 -0
  169. package/package.json +2 -2
@@ -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
@@ -1,7 +1,13 @@
1
1
  export type HostLoadGovernorMode = "on" | "off";
2
2
  export interface HostLoadGovernorConfig {
3
- /** FORGE_HOST_LOAD_GOVERNOR. Off by default in this step; `off` is the operator override. */
3
+ /** FORGE_HOST_LOAD_GOVERNOR: the CPU and memory budget. Off by default; `off` is the operator override. */
4
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;
5
11
  /** Share of logical CPUs Forge may reserve for heavy steps. */
6
12
  readonly cpuBudgetFraction: number;
7
13
  /** Memory kept free for the rest of the host, taken off MemAvailable. */
@@ -28,7 +34,19 @@ export interface HostLoadGovernorConfig {
28
34
  readonly sampleIntervalMs: number;
29
35
  /** Without a dedicated cgroup, a step this old is assumed to use its reserved memory. */
30
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;
31
46
  }
47
+ export declare const defaultHostTestSlots = 1;
32
48
  export declare const defaultHostLoadGovernorConfig: HostLoadGovernorConfig;
33
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;
34
52
  export declare function loadHostLoadGovernorConfig(environment?: Readonly<Record<string, string | undefined>>): HostLoadGovernorConfig;
@@ -1,6 +1,8 @@
1
1
  import { defaultHostLoadSampleIntervalMs } from "./host-load-sampler.js";
2
+ export const defaultHostTestSlots = 1;
2
3
  export const defaultHostLoadGovernorConfig = {
3
4
  mode: "off",
5
+ testSlots: defaultHostTestSlots,
4
6
  cpuBudgetFraction: 0.8,
5
7
  memoryReserveBytes: 1024 ** 3,
6
8
  cpuPressureThreshold: 50,
@@ -19,10 +21,15 @@ export function parseHostLoadGovernorMode(value) {
19
21
  const normalized = value?.trim().toLowerCase() ?? "";
20
22
  return normalized === "on" || normalized === "true" || normalized === "1" ? "on" : "off";
21
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
+ }
22
28
  export function loadHostLoadGovernorConfig(environment = process.env) {
23
29
  const defaults = defaultHostLoadGovernorConfig;
24
30
  return {
25
31
  mode: parseHostLoadGovernorMode(environment.FORGE_HOST_LOAD_GOVERNOR),
32
+ testSlots: Math.round(bounded(environment.FORGE_HOST_LOAD_TEST_SLOTS, defaults.testSlots, 0, 8)),
26
33
  cpuBudgetFraction: bounded(environment.FORGE_HOST_LOAD_CPU_BUDGET_FRACTION, defaults.cpuBudgetFraction, 0.05, 1),
27
34
  memoryReserveBytes: bounded(environment.FORGE_HOST_LOAD_MEMORY_RESERVE_BYTES, defaults.memoryReserveBytes, 0, 1024 ** 4),
28
35
  cpuPressureThreshold: bounded(environment.FORGE_HOST_LOAD_CPU_PRESSURE_THRESHOLD, defaults.cpuPressureThreshold, 1, 100),
@@ -36,8 +43,38 @@ export function loadHostLoadGovernorConfig(environment = process.env) {
36
43
  sampleStaleMs: defaults.sampleStaleMs,
37
44
  sampleIntervalMs: defaults.sampleIntervalMs,
38
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),
39
48
  };
40
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
+ }
41
78
  function bounded(raw, fallback, minimum, maximum) {
42
79
  if (raw === undefined || raw.trim() === "")
43
80
  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
@@ -60,6 +79,8 @@ export interface HostLoadGovernorOptions {
60
79
  * overtake an unfit head until the head has waited past the anti-starvation
61
80
  * window. Running steps are never preempted. Reserved cost comes from the
62
81
  * shared token ledger, so other processes' steps count against the same budget.
82
+ * The host test slot (decision 0018) comes first: a heavy step waits for it in
83
+ * the host-wide queue the ledger keeps, then for the budget when that is on.
63
84
  */
64
85
  export declare class HostLoadGovernor implements HostLoadStepGate {
65
86
  private readonly options;
@@ -74,12 +95,29 @@ export declare class HostLoadGovernor implements HostLoadStepGate {
74
95
  private drainAgain;
75
96
  /** Own cgroup anonymous memory with no heavy step running: the bridge's baseline. */
76
97
  private idleOwnMemoryBytes;
98
+ /** The bridge's pushed settings (decision 0014), shared with every governor on this state root. */
99
+ private readonly runtimeSettings;
77
100
  constructor(options: HostLoadGovernorOptions);
101
+ get maxWaitMs(): number;
78
102
  acquire(request: HostLoadStepRequest, options?: HostLoadAcquireOptions): Promise<HostLoadAdmission>;
79
103
  /** Reclaims tokens whose owner is gone or whose lease expired (bridge startup). */
80
104
  reclaim(): Promise<readonly HostLoadToken[]>;
81
- /** Attempt teardown: cancels the attempt's waiters and returns every token it holds. */
105
+ /** Returns dead owners' unrecorded tokens the caller proved stopped (see the ledger). */
106
+ releaseOrphaned(belongs: (token: HostLoadToken) => boolean): Promise<readonly HostLoadToken[]>;
107
+ /**
108
+ * Attempt teardown: cancels the attempt's queued steps and stops its running
109
+ * ones (their `fenced` signal aborts with dispatch_cancelled). A running step
110
+ * keeps its token, and its cost, until the step itself returns it once its
111
+ * processes are gone, so the capacity is never reused while it still runs.
112
+ */
113
+ stopAttempt(dispatchAttemptId: string): void;
114
+ /**
115
+ * Cancels the attempt's queued steps and returns every token it holds at
116
+ * once. Only for callers that know the attempt's steps are no longer running;
117
+ * attempt teardown uses stopAttempt.
118
+ */
82
119
  releaseAttempt(dispatchAttemptId: string): Promise<void>;
120
+ private cancelAttemptWaiters;
83
121
  /** Waiting and admitted steps of this process, for diagnostics. */
84
122
  snapshot(): {
85
123
  readonly waiting: number;
@@ -87,6 +125,14 @@ export declare class HostLoadGovernor implements HostLoadStepGate {
87
125
  };
88
126
  private drain;
89
127
  private drainOnce;
128
+ /**
129
+ * Decision 0014: the CPU budget (or yield budget) and the heavy-step limit the
130
+ * bridge has in force apply from the next admission; running steps keep theirs.
131
+ * The operator's env ceilings hold with or without the file, and over whatever it says.
132
+ */
133
+ private budgetPass;
134
+ /** The host test slot first (decision 0018), then the budget when it is on. */
135
+ private decide;
90
136
  private readSignals;
91
137
  /**
92
138
  * Consecutive pressured samples ending with the latest one, read from the
@@ -103,21 +149,36 @@ export declare class HostLoadGovernor implements HostLoadStepGate {
103
149
  private runningStepsMemoryBytes;
104
150
  private logicalCpus;
105
151
  private settle;
152
+ /** Renewed tokens reset their lease clock; fenced ones stop their steps (they keep their cost until released). */
153
+ private applyHeldOutcome;
154
+ /** The step must stop: its reservation is no longer guaranteed. */
155
+ private lose;
156
+ /** Ledger failures reach every queued step instead of leaving it to time out. */
157
+ private failWaiters;
158
+ private attachProcess;
106
159
  /**
107
160
  * Returns a step's token. It stays held (and renewed) until the ledger takes
108
161
  * it back, so a ledger outage cannot leave it to lapse into a fenced
109
- * reservation; the renewal timer retries the release.
162
+ * reservation; the renewal timer retries the release. A fenced token is
163
+ * removed too (its step is over) and the release reports the fence.
110
164
  */
111
165
  private releaseHeld;
112
166
  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
167
  private armRecheck;
119
168
  private disarmRecheck;
120
169
  private armRenewal;
121
170
  private disarmRenewal;
171
+ /**
172
+ * The step ended but part of its process tree may still run: its token stays
173
+ * held and renewed, with its cost, and the step's own termination check is
174
+ * retried on every renewal tick until it proves the tree gone.
175
+ */
176
+ private retainUntilStopped;
177
+ /**
178
+ * Attempt teardown stopped the attempt's processes (its process supervisor
179
+ * proved them gone): tokens kept for a tree nothing else can check are returned.
180
+ */
181
+ releaseRetainedAfterTeardown(dispatchAttemptId: string): Promise<void>;
182
+ private checkRetained;
122
183
  private renewHeld;
123
184
  }