@loucompanion/forge-bridge 0.1.4-dev.494233aa2e50 → 0.1.4-dev.4e7855579f82

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 (217) hide show
  1. package/README.md +144 -3
  2. package/dist/bridge/bridge-chat-provider-command.d.ts +19 -0
  3. package/dist/bridge/bridge-chat-provider-command.js +132 -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-state-file.d.ts +161 -0
  7. package/dist/bridge/bridge-state-file.js +376 -0
  8. package/dist/bridge/bridge-state-io.d.ts +11 -0
  9. package/dist/bridge/bridge-state-io.js +50 -0
  10. package/dist/bridge/bridge-state-lock.d.ts +11 -0
  11. package/dist/bridge/bridge-state-lock.js +102 -0
  12. package/dist/bridge/bridge-state-unpublished.d.ts +17 -0
  13. package/dist/bridge/bridge-state-unpublished.js +111 -0
  14. package/dist/bridge/bridge.base.d.ts +9 -0
  15. package/dist/bridge/bridge.service.d.ts +89 -14
  16. package/dist/bridge/bridge.service.js +423 -342
  17. package/dist/bridge/bridge.types.d.ts +21 -0
  18. package/dist/bridge/in-flight-registry.d.ts +62 -0
  19. package/dist/bridge/in-flight-registry.js +207 -0
  20. package/dist/bridge/local-yield-flag.d.ts +6 -0
  21. package/dist/bridge/local-yield-flag.js +34 -0
  22. package/dist/bridge/readiness-result-outbox.d.ts +5 -1
  23. package/dist/bridge/readiness-result-outbox.js +20 -0
  24. package/dist/bridge/runner-host-capabilities.d.ts +5 -0
  25. package/dist/bridge/runner-host-capabilities.js +17 -0
  26. package/dist/bridge/sandbox-resource-accounting.d.ts +16 -0
  27. package/dist/bridge/sandbox-resource-accounting.js +150 -0
  28. package/dist/bridge/test-postgres-capability.d.ts +14 -0
  29. package/dist/bridge/test-postgres-capability.js +120 -0
  30. package/dist/cleanup/cleanup.service.d.ts +9 -0
  31. package/dist/cleanup/cleanup.service.js +72 -109
  32. package/dist/cleanup/cleanup.types.d.ts +4 -0
  33. package/dist/cleanup/forge-owned-paths.d.ts +9 -0
  34. package/dist/cleanup/forge-owned-paths.js +53 -0
  35. package/dist/cleanup/host-runtime-ledger.d.ts +5 -0
  36. package/dist/cleanup/host-runtime-ledger.js +109 -0
  37. package/dist/cleanup/managed-branch-sweep.d.ts +27 -0
  38. package/dist/cleanup/managed-branch-sweep.js +111 -0
  39. package/dist/cli/cli.help.js +74 -12
  40. package/dist/cli/cli.service.d.ts +19 -10
  41. package/dist/cli/cli.service.js +152 -187
  42. package/dist/cli/idle-status.d.ts +84 -0
  43. package/dist/cli/idle-status.js +181 -0
  44. package/dist/cli/local-config.d.ts +6 -0
  45. package/dist/cli/local-config.js +32 -4
  46. package/dist/cli/provider-cli-detection.d.ts +10 -0
  47. package/dist/cli/provider-cli-detection.js +160 -0
  48. package/dist/runtime/aspnet-runtime-prebuild.d.ts +5 -0
  49. package/dist/runtime/aspnet-runtime-prebuild.js +138 -6
  50. package/dist/runtime/attempt-database-broker.d.ts +1 -1
  51. package/dist/runtime/docker-engine-probe.d.ts +5 -0
  52. package/dist/runtime/docker-engine-probe.js +7 -0
  53. package/dist/runtime/ephemeral-test-postgres-grant.d.ts +37 -0
  54. package/dist/runtime/ephemeral-test-postgres-grant.js +85 -0
  55. package/dist/runtime/ephemeral-test-postgres.d.ts +57 -0
  56. package/dist/runtime/ephemeral-test-postgres.js +303 -0
  57. package/dist/runtime/host-facts.d.ts +2 -1
  58. package/dist/runtime/host-load-admission.d.ts +67 -0
  59. package/dist/runtime/host-load-admission.js +99 -0
  60. package/dist/runtime/host-load-child-environment.d.ts +27 -0
  61. package/dist/runtime/host-load-child-environment.js +76 -0
  62. package/dist/runtime/host-load-forge-cgroups.d.ts +49 -0
  63. package/dist/runtime/host-load-forge-cgroups.js +140 -0
  64. package/dist/runtime/host-load-governor-config.d.ts +52 -0
  65. package/dist/runtime/host-load-governor-config.js +83 -0
  66. package/dist/runtime/host-load-governor.d.ts +184 -0
  67. package/dist/runtime/host-load-governor.js +632 -0
  68. package/dist/runtime/host-load-ledger-lock.d.ts +69 -0
  69. package/dist/runtime/host-load-ledger-lock.js +185 -0
  70. package/dist/runtime/host-load-runtime-settings.d.ts +25 -0
  71. package/dist/runtime/host-load-runtime-settings.js +78 -0
  72. package/dist/runtime/host-load-sampler.d.ts +75 -2
  73. package/dist/runtime/host-load-sampler.js +142 -8
  74. package/dist/runtime/host-load-step-class.d.ts +70 -0
  75. package/dist/runtime/host-load-step-class.js +161 -0
  76. package/dist/runtime/host-load-step-reaper.d.ts +27 -0
  77. package/dist/runtime/host-load-step-reaper.js +95 -0
  78. package/dist/runtime/host-load-step-run.d.ts +73 -0
  79. package/dist/runtime/host-load-step-run.js +102 -0
  80. package/dist/runtime/host-load-test-slot.d.ts +43 -0
  81. package/dist/runtime/host-load-test-slot.js +81 -0
  82. package/dist/runtime/host-load-token-ledger.d.ts +200 -0
  83. package/dist/runtime/host-load-token-ledger.js +453 -0
  84. package/dist/runtime/host-load-windows-cpu.d.ts +34 -0
  85. package/dist/runtime/host-load-windows-cpu.js +85 -0
  86. package/dist/runtime/host-pressure-stall.d.ts +21 -1
  87. package/dist/runtime/host-pressure-stall.js +48 -1
  88. package/dist/runtime/host-runtime-contract.d.ts +15 -0
  89. package/dist/runtime/host-runtime-contract.js +33 -2
  90. package/dist/runtime/host-runtime-lease.d.ts +74 -4
  91. package/dist/runtime/host-runtime-lease.js +285 -55
  92. package/dist/runtime/host-runtime-promotion.d.ts +23 -0
  93. package/dist/runtime/host-runtime-promotion.js +48 -0
  94. package/dist/runtime/host-runtime-requirements-check.d.ts +17 -0
  95. package/dist/runtime/host-runtime-requirements-check.js +29 -0
  96. package/dist/runtime/host-runtime-requirements.d.ts +36 -0
  97. package/dist/runtime/host-runtime-requirements.js +117 -0
  98. package/dist/runtime/native-test-postgres-fallback.d.ts +51 -7
  99. package/dist/runtime/native-test-postgres-fallback.js +60 -8
  100. package/dist/runtime/playwright-host-runtime-adapter.d.ts +7 -0
  101. package/dist/runtime/playwright-host-runtime-adapter.js +13 -1
  102. package/dist/runtime/runner-settings.d.ts +104 -0
  103. package/dist/runtime/runner-settings.js +236 -0
  104. package/dist/sandbox/host-capacity-broker.d.ts +113 -0
  105. package/dist/sandbox/host-capacity-broker.js +334 -0
  106. package/dist/sandbox/runtime.d.ts +10 -1
  107. package/dist/sandbox/runtime.js +45 -2
  108. package/dist/sandbox/sandbox-cgroups.d.ts +14 -0
  109. package/dist/sandbox/sandbox-cgroups.js +42 -0
  110. package/dist/session/architect-waits.d.ts +2 -0
  111. package/dist/session/architect-waits.js +138 -0
  112. package/dist/session/managed-attempt-tools.d.ts +2 -7
  113. package/dist/session/managed-attempt-tools.js +200 -92
  114. package/dist/session/managed-command-host-load.d.ts +47 -0
  115. package/dist/session/managed-command-host-load.js +83 -0
  116. package/dist/session/mcp-argument-coercion.d.ts +23 -0
  117. package/dist/session/mcp-argument-coercion.js +121 -0
  118. package/dist/session/phase-completion.artifacts.d.ts +7 -0
  119. package/dist/session/phase-completion.artifacts.js +96 -26
  120. package/dist/session/phase-completion.command-validation.d.ts +7 -0
  121. package/dist/session/phase-completion.command-validation.js +77 -0
  122. package/dist/session/phase-completion.d.ts +12 -0
  123. package/dist/session/phase-completion.helpers.js +13 -12
  124. package/dist/session/phase-completion.js +5 -0
  125. package/dist/session/phase-completion.mcp-args.d.ts +9 -0
  126. package/dist/session/phase-completion.mcp-args.js +156 -0
  127. package/dist/session/phase-completion.prompts.js +6 -1
  128. package/dist/session/phase-completion.runtime.d.ts +5 -11
  129. package/dist/session/phase-completion.runtime.js +91 -215
  130. package/dist/session/phase-completion.schema-parts.d.ts +19 -0
  131. package/dist/session/phase-completion.schema-parts.js +188 -0
  132. package/dist/session/phase-completion.schemas.d.ts +3 -3
  133. package/dist/session/phase-completion.schemas.js +208 -231
  134. package/dist/session/phase-completion.validation.d.ts +2 -3
  135. package/dist/session/phase-completion.validation.js +49 -93
  136. package/dist/session/pre-publication-host-load.d.ts +27 -0
  137. package/dist/session/pre-publication-host-load.js +111 -0
  138. package/dist/session/pre-publication-native-postgres.d.ts +30 -0
  139. package/dist/session/pre-publication-native-postgres.js +111 -0
  140. package/dist/session/pre-publication-pass.d.ts +61 -0
  141. package/dist/session/pre-publication-pass.js +148 -0
  142. package/dist/session/pre-publication-process.d.ts +3 -0
  143. package/dist/session/pre-publication-process.js +23 -11
  144. package/dist/session/pre-publication-step-execution.d.ts +12 -0
  145. package/dist/session/pre-publication-step-execution.js +50 -0
  146. package/dist/session/pre-publication-validation.d.ts +37 -21
  147. package/dist/session/pre-publication-validation.js +221 -182
  148. package/dist/session/provider-cli/claude-background-work.d.ts +67 -0
  149. package/dist/session/provider-cli/claude-background-work.js +181 -0
  150. package/dist/session/provider-cli/host-build-lock.d.ts +18 -0
  151. package/dist/session/provider-cli/host-build-lock.js +30 -0
  152. package/dist/session/provider-cli/pre-publication-repair.d.ts +56 -0
  153. package/dist/session/provider-cli/pre-publication-repair.js +276 -0
  154. package/dist/session/provider-cli/provider-cli.artifacts.d.ts +2 -1
  155. package/dist/session/provider-cli/provider-cli.artifacts.js +20 -7
  156. package/dist/session/provider-cli/provider-cli.child-environment.d.ts +7 -0
  157. package/dist/session/provider-cli/provider-cli.child-environment.js +75 -0
  158. package/dist/session/provider-cli/provider-cli.codex-models-cache.d.ts +3 -0
  159. package/dist/session/provider-cli/provider-cli.codex-models-cache.js +37 -0
  160. package/dist/session/provider-cli/provider-cli.merge-integrity.d.ts +89 -0
  161. package/dist/session/provider-cli/provider-cli.merge-integrity.js +310 -0
  162. package/dist/session/provider-cli/provider-cli.process.d.ts +41 -31
  163. package/dist/session/provider-cli/provider-cli.process.js +139 -438
  164. package/dist/session/provider-cli/provider-cli.prompts.d.ts +10 -0
  165. package/dist/session/provider-cli/provider-cli.prompts.js +87 -32
  166. package/dist/session/provider-cli/provider-cli.runtime.d.ts +8 -0
  167. package/dist/session/provider-cli/provider-cli.runtime.js +29 -1
  168. package/dist/session/provider-cli/provider-cli.service.d.ts +7 -1
  169. package/dist/session/provider-cli/provider-cli.service.js +146 -182
  170. package/dist/session/provider-cli/provider-cli.types.d.ts +30 -0
  171. package/dist/session/provider-cli/provider-cli.windows-environment.d.ts +10 -0
  172. package/dist/session/provider-cli/provider-cli.windows-environment.js +151 -0
  173. package/dist/session/provider-cli/provider-cli.windows-launch.d.ts +23 -0
  174. package/dist/session/provider-cli/provider-cli.windows-launch.js +131 -0
  175. package/dist/session/provider-cli/provider-in-flight-work.d.ts +10 -0
  176. package/dist/session/provider-cli/provider-in-flight-work.js +92 -0
  177. package/dist/session/provider-cli/provider-output-pipes.d.ts +30 -0
  178. package/dist/session/provider-cli/provider-output-pipes.js +67 -0
  179. package/dist/session/provider-cli/provider-output-state.d.ts +8 -0
  180. package/dist/session/provider-cli/provider-output-state.js +15 -0
  181. package/dist/session/provider-cli/provider-user-config-isolation.d.ts +69 -0
  182. package/dist/session/provider-cli/provider-user-config-isolation.js +164 -0
  183. package/dist/session/publishable-worktree-changes.d.ts +10 -1
  184. package/dist/session/publishable-worktree-changes.js +21 -5
  185. package/dist/session/qa-sidecar-validation.d.ts +6 -0
  186. package/dist/session/qa-sidecar-validation.js +28 -0
  187. package/dist/session/sandbox-host-capacity-gate.d.ts +30 -0
  188. package/dist/session/sandbox-host-capacity-gate.js +238 -0
  189. package/dist/session/session-redaction.js +31 -181
  190. package/dist/session/session.base.d.ts +9 -1
  191. package/dist/session/session.service.d.ts +9 -2
  192. package/dist/session/session.service.js +120 -5
  193. package/dist/session/session.types.d.ts +3 -0
  194. package/dist/session/session.types.js +13 -4
  195. package/dist/session/sidecar-json-shape.d.ts +31 -0
  196. package/dist/session/sidecar-json-shape.js +217 -0
  197. package/dist/session/workspace-runtime-fingerprint.d.ts +32 -0
  198. package/dist/session/workspace-runtime-fingerprint.js +138 -0
  199. package/dist/shared/process/host-process-supervisor.d.ts +1 -0
  200. package/dist/shared/process/host-process-supervisor.js +1 -1
  201. package/dist/shared/process/process-tree.d.ts +52 -0
  202. package/dist/shared/process/process-tree.js +144 -0
  203. package/dist/shared/process/process.d.ts +67 -0
  204. package/dist/shared/process/process.js +213 -11
  205. package/dist/shared/process/windows-app-execution-alias.d.ts +19 -0
  206. package/dist/shared/process/windows-app-execution-alias.js +45 -0
  207. package/dist/shared/process/windows-launch.d.ts +18 -0
  208. package/dist/shared/process/windows-launch.js +68 -0
  209. package/dist/shared/provider-capabilities.js +14 -7
  210. package/dist/version.d.ts +1 -1
  211. package/dist/version.js +1 -1
  212. package/dist/worktree/dependency-warmer.d.ts +25 -0
  213. package/dist/worktree/dependency-warmer.js +46 -10
  214. package/dist/worktree/worktree.service.d.ts +10 -1
  215. package/dist/worktree/worktree.service.js +42 -2
  216. package/dist/worktree/worktree.types.d.ts +7 -0
  217. package/package.json +2 -2
@@ -0,0 +1,236 @@
1
+ import { formatBytes } from "../shared/format/format-bytes.js";
2
+ import { parseHostLoadGovernorMode } from "./host-load-governor-config.js";
3
+ // Decision 0014 §3 "Precedence" and §6 "Bridge hub": the server pushes a runner
4
+ // settings document (`ApplySettings`) and is the source of truth. Host env vars
5
+ // are one-sided safety bounds only: a ceiling on capacity, a floor on reserves.
6
+ // The bridge reports the effective values, their sources and every env bound it
7
+ // saw back to the server (`SettingsApplied`), so a host bound is never silent.
8
+ // Nothing here is persisted to bridge.json (0014 agent constraint).
9
+ export const runnerSettingKeys = [
10
+ "maxConcurrentAttempts",
11
+ "cpuBudgetPercent",
12
+ "yieldCpuBudgetPercent",
13
+ "memoryReserveBytes",
14
+ "maxHeavySteps",
15
+ "yieldToForeground",
16
+ ];
17
+ export const defaultCpuBudgetPercent = 80;
18
+ export const defaultYieldCpuBudgetPercent = 50;
19
+ const MiB = 1024 ** 2;
20
+ const GiB = 1024 ** 3;
21
+ /** The server's ranges (contract); a value outside them is rejected, not clamped, so it is never silent. */
22
+ const serverRanges = {
23
+ maxConcurrentAttempts: { min: 1, max: 32 },
24
+ cpuBudgetPercent: { min: 10, max: 100 },
25
+ yieldCpuBudgetPercent: { min: 10, max: 100 },
26
+ memoryReserveBytes: { min: 512 * MiB, max: 128 * GiB },
27
+ maxHeavySteps: { min: 1, max: 64 },
28
+ };
29
+ /**
30
+ * Computes every effective value (decision 0014 precedence):
31
+ * - ceiling keys: server != null ? min(server, ceiling ?? +inf) : (ceiling ?? default);
32
+ * - memory reserve (floor): server != null ? max(server, floor ?? 0) : (floor ?? adaptive default);
33
+ * - yield: server OR the local `forge-bridge yield on` flag.
34
+ * With no document at all (and the local flag off) the values equal today's env/default behaviour.
35
+ */
36
+ export function computeEffectiveRunnerSettings(document, environment, hostDefaults, localYield) {
37
+ const rejected = [];
38
+ const server = (key) => {
39
+ const value = document?.[key] ?? null;
40
+ if (value === null)
41
+ return null;
42
+ const range = serverRanges[key];
43
+ if (typeof value !== "number" || !Number.isFinite(value) || !Number.isInteger(value)) {
44
+ rejected.push({ key, reason: `${String(value)} is not a whole number; the host default applies.`, kind: "invalid" });
45
+ return null;
46
+ }
47
+ if (value < range.min || value > range.max) {
48
+ rejected.push({ key, reason: `${value} is outside ${range.min}-${range.max}; the host default applies.`, kind: "invalid" });
49
+ return null;
50
+ }
51
+ return value;
52
+ };
53
+ const attemptsCeiling = integerEnv(environment, "FORGE_HOST_MAX_CONCURRENT_ATTEMPTS", 1, 32);
54
+ const cpuCeilings = cpuBudgetCeilings(environment);
55
+ const reserveFloor = integerEnv(environment, "FORGE_HOST_MEMORY_RESERVE_BYTES", 512 * MiB, 128 * GiB);
56
+ const heavyStepsCeiling = integerEnv(environment, "FORGE_HOST_MAX_HEAVY_STEPS", 1, 64);
57
+ const attempts = ceilingValue(server("maxConcurrentAttempts"), lowest(attemptsCeiling), hostDefaults.maxConcurrentAttempts);
58
+ const cpu = ceilingValue(server("cpuBudgetPercent"), lowest(...cpuCeilings), defaultCpuBudgetPercent);
59
+ // The yield budget's ceilings are the CPU budget's. Without a server value they
60
+ // cap its default rather than replace it: a host allowing 90% CPU must not raise
61
+ // the 50% yield default, which no env var sets today.
62
+ const serverYieldCpu = server("yieldCpuBudgetPercent");
63
+ const yieldCpu = ceilingValue(serverYieldCpu ?? defaultYieldCpuBudgetPercent, lowest(...cpuCeilings), defaultYieldCpuBudgetPercent);
64
+ const yieldCpuSource = serverYieldCpu === null && yieldCpu.source === "server" ? "default" : yieldCpu.source;
65
+ const reserve = floorValue(server("memoryReserveBytes"), reserveFloor?.value ?? null, hostDefaults.memoryReserveBytes);
66
+ const heavySteps = ceilingValue(server("maxHeavySteps"), lowest(heavyStepsCeiling), null);
67
+ const serverYield = document?.yieldToForeground === true;
68
+ const yielding = serverYield || localYield;
69
+ // The host's own switch wins the label: turning the server toggle off would not stop it.
70
+ const yieldSource = localYield ? "host_local" : serverYield ? "server" : "default";
71
+ const bounds = [];
72
+ const addCeilings = (key, set) => {
73
+ for (const bound of set)
74
+ if (bound)
75
+ bounds.push({ key, envVar: bound.envVar, value: bound.value, kind: "ceiling" });
76
+ };
77
+ addCeilings("maxConcurrentAttempts", [attemptsCeiling]);
78
+ addCeilings("cpuBudgetPercent", cpuCeilings);
79
+ addCeilings("yieldCpuBudgetPercent", cpuCeilings);
80
+ if (reserveFloor)
81
+ bounds.push({ key: "memoryReserveBytes", envVar: reserveFloor.envVar, value: reserveFloor.value, kind: "floor" });
82
+ addCeilings("maxHeavySteps", [heavyStepsCeiling]);
83
+ const values = {
84
+ maxConcurrentAttempts: attempts.value,
85
+ cpuBudgetPercent: cpu.value,
86
+ yieldCpuBudgetPercent: yieldCpu.value,
87
+ memoryReserveBytes: reserve.value,
88
+ maxHeavySteps: heavySteps.value,
89
+ yieldToForeground: yielding,
90
+ };
91
+ const budgetPercent = yielding ? Math.min(values.yieldCpuBudgetPercent, values.cpuBudgetPercent) : values.cpuBudgetPercent;
92
+ return {
93
+ report: {
94
+ version: null,
95
+ governor: parseHostLoadGovernorMode(environment.FORGE_HOST_LOAD_GOVERNOR),
96
+ values,
97
+ sources: {
98
+ maxConcurrentAttempts: attempts.source,
99
+ cpuBudgetPercent: cpu.source,
100
+ yieldCpuBudgetPercent: yieldCpuSource,
101
+ memoryReserveBytes: reserve.source,
102
+ maxHeavySteps: heavySteps.source,
103
+ yieldToForeground: yieldSource,
104
+ },
105
+ bounds,
106
+ rejected,
107
+ },
108
+ yielding,
109
+ governorCpuBudgetFraction: budgetPercent / 100,
110
+ };
111
+ }
112
+ /**
113
+ * Parses the `ApplySettings` argument. Returns null when it has no usable version
114
+ * (the bridge then ignores it); malformed values become null so the host default applies.
115
+ */
116
+ export function parseRunnerSettingsDocument(raw) {
117
+ if (!raw || typeof raw !== "object")
118
+ return null;
119
+ const record = raw;
120
+ const version = record.version;
121
+ if (typeof version !== "number" || !Number.isSafeInteger(version) || version < 0)
122
+ return null;
123
+ const numeric = (key) => {
124
+ const value = record[key];
125
+ // Kept as-is (even out of range) so computeEffectiveRunnerSettings can report it as rejected.
126
+ return typeof value === "number" ? value : null;
127
+ };
128
+ return {
129
+ runnerId: typeof record.runnerId === "string" ? record.runnerId : null,
130
+ version,
131
+ maxConcurrentAttempts: numeric("maxConcurrentAttempts"),
132
+ cpuBudgetPercent: numeric("cpuBudgetPercent"),
133
+ yieldCpuBudgetPercent: numeric("yieldCpuBudgetPercent"),
134
+ memoryReserveBytes: numeric("memoryReserveBytes"),
135
+ maxHeavySteps: numeric("maxHeavySteps"),
136
+ yieldToForeground: record.yieldToForeground === true,
137
+ };
138
+ }
139
+ /**
140
+ * The report for what is actually in force after an apply in which the host
141
+ * failed to put some keys in force: each failed key keeps the value (and source)
142
+ * that was in force before, and its failure is reported as an `apply_failed`
143
+ * rejection, so the server never reads a desired value as applied.
144
+ */
145
+ export function runnerSettingsInForce(report, previous, failures) {
146
+ if (failures.length === 0)
147
+ return report;
148
+ const values = { ...report.values };
149
+ const sources = { ...report.sources };
150
+ for (const failure of failures) {
151
+ keepPrevious(values, previous.values, failure.key);
152
+ sources[failure.key] = previous.sources[failure.key];
153
+ }
154
+ return { ...report, values, sources, rejected: [...report.rejected, ...failures] };
155
+ }
156
+ function keepPrevious(target, previous, key) {
157
+ target[key] = previous[key];
158
+ }
159
+ /** True when two reports say the same thing (version included), so an unchanged recompute is not re-sent. */
160
+ export function sameRunnerSettingsReport(left, right) {
161
+ return JSON.stringify(left) === JSON.stringify(right);
162
+ }
163
+ function ceilingValue(server, ceiling, fallback) {
164
+ if (server !== null) {
165
+ return ceiling && ceiling.value < server
166
+ ? { value: ceiling.value, source: "host_env" }
167
+ : { value: server, source: "server" };
168
+ }
169
+ return ceiling ? { value: ceiling.value, source: "host_env" } : { value: fallback, source: "default" };
170
+ }
171
+ function floorValue(server, floor, fallback) {
172
+ if (server !== null) {
173
+ return floor !== null && floor > server
174
+ ? { value: floor, source: "host_env" }
175
+ : { value: server, source: "server" };
176
+ }
177
+ return floor !== null ? { value: floor, source: "host_env" } : { value: fallback, source: "default" };
178
+ }
179
+ function lowest(...bounds) {
180
+ let result = null;
181
+ for (const bound of bounds) {
182
+ if (bound && (result === null || bound.value < result.value))
183
+ result = bound;
184
+ }
185
+ return result;
186
+ }
187
+ /**
188
+ * Both CPU ceilings: the new FORGE_HOST_CPU_BUDGET_PERCENT (10–100) and the
189
+ * existing FORGE_HOST_LOAD_CPU_BUDGET_FRACTION (0.05–1, the governor's own
190
+ * parse), reported as a percent.
191
+ */
192
+ function cpuBudgetCeilings(environment) {
193
+ const percent = numberEnv(environment, "FORGE_HOST_CPU_BUDGET_PERCENT", 10, 100);
194
+ const fraction = numberEnv(environment, "FORGE_HOST_LOAD_CPU_BUDGET_FRACTION", 0.05, 1);
195
+ return [
196
+ percent,
197
+ fraction ? { envVar: fraction.envVar, value: Math.round(fraction.value * 10_000) / 100 } : null,
198
+ ];
199
+ }
200
+ /** Matches the lease manager's parse (parseInt, clamped); an unparsable value counts as unset. */
201
+ function integerEnv(environment, envVar, minimum, maximum) {
202
+ const raw = environment[envVar];
203
+ if (raw === undefined || raw.trim() === "")
204
+ return null;
205
+ const parsed = Number.parseInt(raw, 10);
206
+ return Number.isFinite(parsed) ? { envVar, value: Math.max(minimum, Math.min(maximum, parsed)) } : null;
207
+ }
208
+ /** Matches the governor config's parse (Number, clamped); an unparsable value counts as unset. */
209
+ function numberEnv(environment, envVar, minimum, maximum) {
210
+ const raw = environment[envVar];
211
+ if (raw === undefined || raw.trim() === "")
212
+ return null;
213
+ const parsed = Number(raw);
214
+ return Number.isFinite(parsed) ? { envVar, value: Math.min(maximum, Math.max(minimum, parsed)) } : null;
215
+ }
216
+ /** The offer refusal message while the runner yields to foreground work. */
217
+ export function runnerYieldingMessage(report) {
218
+ const where = report.sources.yieldToForeground === "host_local" ? "on this host (forge-bridge yield on)" : "in Forge";
219
+ return `runner_yielding: this runner is yielding to foreground work (turned on ${where}); it takes no new attempts until yield is off. Running attempts continue.`;
220
+ }
221
+ /** One line describing the effective settings, for the bridge log. */
222
+ export function describeRunnerSettings(report) {
223
+ const { values } = report;
224
+ const parts = [
225
+ `max attempts ${values.maxConcurrentAttempts}`,
226
+ `CPU budget ${values.cpuBudgetPercent}%`,
227
+ `yield budget ${values.yieldCpuBudgetPercent}%`,
228
+ `memory reserve ${formatBytes(values.memoryReserveBytes)}`,
229
+ `heavy steps ${values.maxHeavySteps ?? "budget only"}`,
230
+ `yield ${values.yieldToForeground ? "on" : "off"}`,
231
+ ];
232
+ const capped = report.bounds
233
+ .filter(bound => report.sources[bound.key] === "host_env")
234
+ .map(bound => `${bound.envVar}=${bound.value}`);
235
+ return `${parts.join(", ")}${capped.length > 0 ? `; host bounds in force: ${[...new Set(capped)].join(", ")}` : ""}`;
236
+ }
@@ -0,0 +1,113 @@
1
+ import type { HostLoadStepGate } from "../runtime/host-load-governor.js";
2
+ import type { HostLoadStepProcess } from "../runtime/host-load-step-reaper.js";
3
+ import type { HostLoadTokenPriority } from "../runtime/host-load-token-ledger.js";
4
+ export declare const sandboxHostCapacityBrokerEnvironment = "FORGE_HOST_LOAD_BROKER";
5
+ export declare const sandboxHostCapacityBrokerProtocol = "stdout-v1";
6
+ export declare const sandboxHostCapacityLineType = "forge_host_capacity";
7
+ export declare const sandboxHostCapacityResponseRoot = "/run/forge/host-capacity";
8
+ /** Owner of authentic replies inside the container: root, via `docker exec --user 0`. */
9
+ export declare const sandboxHostCapacityResponseOwnerUid = 0;
10
+ export declare const sandboxHostCapacityMaxWaitEnvironment = "FORGE_HOST_LOAD_BROKER_MAX_WAIT_MS";
11
+ export interface SandboxHostCapacityRequest {
12
+ readonly action: "acquire" | "release" | "retain";
13
+ readonly requestId: string;
14
+ readonly stepClass: string;
15
+ readonly label: string;
16
+ }
17
+ export interface SandboxHostCapacityResponse {
18
+ readonly requestId: string;
19
+ /** Rises with every reply the broker sends; the container keeps only the newest. */
20
+ readonly sequence: number;
21
+ readonly status: "waiting" | "admitted" | "failed" | "fenced";
22
+ readonly waitMs?: number;
23
+ readonly reason?: string;
24
+ readonly detail?: string;
25
+ readonly code?: string;
26
+ readonly message?: string;
27
+ }
28
+ export type SandboxHostCapacityDelivery = (response: SandboxHostCapacityResponse) => Promise<void>;
29
+ /** Parses one stdout line; null when it is not a well-formed broker request. */
30
+ export declare function parseSandboxHostCapacityRequest(line: string): SandboxHostCapacityRequest | null;
31
+ export interface SandboxHostCapacityBrokerOptions {
32
+ /** Requests (acquire or release) one sandbox may hold or start at once. */
33
+ readonly maximumOutstanding?: number;
34
+ /** Request lines allowed in a burst, and their sustained rate. */
35
+ readonly burst?: number;
36
+ readonly refillPerSecond?: number;
37
+ /** The sandbox's process tree (its container's init), recorded on each brokered token. */
38
+ readonly stepProcess?: () => Promise<HostLoadStepProcess | null>;
39
+ /** Delay before retrying a failed delivery; doubles up to a few seconds. */
40
+ readonly retryMs?: number;
41
+ readonly now?: () => number;
42
+ }
43
+ /**
44
+ * One sandbox's brokered tokens. The sandbox never chooses the cost or the
45
+ * attempt. Replies for one request go out one at a time, newest state only,
46
+ * each with a rising sequence the container-side writer uses to refuse an
47
+ * older reply; a failed delivery is retried until a newer state replaces it
48
+ * or the broker closes. A sandbox that floods requests or exceeds its
49
+ * outstanding limit stops the broker and fails its attempt.
50
+ */
51
+ export declare class SandboxHostCapacityBroker {
52
+ private readonly gate;
53
+ private readonly dispatchAttemptId;
54
+ private readonly priority;
55
+ private readonly deliver;
56
+ private readonly options;
57
+ private readonly tokens;
58
+ private readonly deliveries;
59
+ private readonly work;
60
+ private readonly maximumOutstanding;
61
+ private readonly burst;
62
+ private readonly refillPerSecond;
63
+ private readonly retryMs;
64
+ private readonly now;
65
+ private sequence;
66
+ private allowance;
67
+ private allowanceAtMs;
68
+ private closed;
69
+ private abuse;
70
+ constructor(gate: HostLoadStepGate, dispatchAttemptId: string, priority: HostLoadTokenPriority, deliver: SandboxHostCapacityDelivery, options?: SandboxHostCapacityBrokerOptions);
71
+ /**
72
+ * True when the line was a broker request; such a line is never a runner
73
+ * event. Throws sandbox_host_capacity_abuse once the sandbox breaks its
74
+ * limits: its tokens are returned and the attempt fails with that reason.
75
+ */
76
+ handleLine(line: string): boolean;
77
+ /** The sandbox exited: cancel its waits and return every token it holds. */
78
+ close(): Promise<void>;
79
+ /** Requests waiting or admitted, for diagnostics and tests. */
80
+ outstanding(): number;
81
+ private stop;
82
+ private takeAllowance;
83
+ private track;
84
+ private acquire;
85
+ /**
86
+ * The sandbox's step ended but part of its tree may still run: the host token is
87
+ * kept, marked retained (so the attempt's own next step does not wait for it),
88
+ * until the sandbox releases the request or exits.
89
+ */
90
+ private retain;
91
+ private release;
92
+ /** Queues the newest state for a request; at most one delivery per request is in flight. */
93
+ private respond;
94
+ private pump;
95
+ }
96
+ /**
97
+ * Adds the broker variables to a worker `docker create` command, just before
98
+ * its image: the protocol, and the host's maximum queue wait so the
99
+ * container's own fail-safe never fires before the host answers.
100
+ */
101
+ export declare function withSandboxHostCapacityBroker(createArgs: readonly string[], image: string, maxWaitMs: number): string[];
102
+ /** `docker exec` arguments that write one reply into the container as root. */
103
+ export declare function sandboxHostCapacityDeliveryExecArgs(container: string): string[];
104
+ /**
105
+ * The `node -e` script that writes one response file inside the container, run
106
+ * as root. It keeps the newest reply: one with a lower or equal sequence than
107
+ * the file already holds is dropped, so a late delivery can never replace a
108
+ * newer state. Replies are world-readable but owned by the writer (root, via
109
+ * `docker exec --user 0`). The directory must already be owned by that same uid
110
+ * and writable by no one else, i.e. the root-owned reply mount; it is never
111
+ * created here, where it could turn out to be the sandbox's own directory.
112
+ */
113
+ export declare const sandboxHostCapacityResponseScript: string;