@loucompanion/forge-bridge 0.1.3-dev.fbe15023cdfe → 0.1.4-dev.03584818ede5
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 +146 -5
- package/dist/bridge/bridge-chat-provider-command.d.ts +16 -0
- package/dist/bridge/bridge-chat-provider-command.js +137 -0
- package/dist/bridge/bridge-chat.types.d.ts +121 -0
- package/dist/bridge/bridge-chat.types.js +416 -0
- package/dist/bridge/bridge.base.d.ts +13 -1
- package/dist/bridge/bridge.service.d.ts +155 -4
- package/dist/bridge/bridge.service.js +1805 -155
- package/dist/bridge/bridge.types.d.ts +68 -1
- package/dist/bridge/dispatch-result-outbox.d.ts +35 -0
- package/dist/bridge/dispatch-result-outbox.js +87 -0
- package/dist/bridge/dispatch-result-worker.d.ts +2 -0
- package/dist/bridge/dispatch-result-worker.js +122 -0
- package/dist/bridge/internals.d.ts +2 -0
- package/dist/bridge/internals.js +1 -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 +21 -0
- package/dist/bridge/readiness-result-outbox.js +54 -0
- package/dist/bridge/readiness-result-worker.d.ts +2 -0
- package/dist/bridge/readiness-result-worker.js +121 -0
- package/dist/bridge/runner-host-capabilities.d.ts +20 -0
- package/dist/bridge/runner-host-capabilities.js +110 -0
- package/dist/bridge/test-postgres-capability.d.ts +14 -0
- package/dist/bridge/test-postgres-capability.js +120 -0
- package/dist/cleanup/cleanup.service.d.ts +10 -0
- package/dist/cleanup/cleanup.service.js +128 -104
- package/dist/cleanup/cleanup.types.d.ts +4 -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 +5 -0
- package/dist/cleanup/host-runtime-ledger.js +109 -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 +22 -4
- package/dist/cli/cli.service.d.ts +30 -6
- package/dist/cli/cli.service.js +216 -31
- package/dist/cli/local-config.d.ts +5 -0
- package/dist/cli/local-config.js +30 -4
- package/dist/runtime/aspnet-runtime-prebuild.d.ts +36 -0
- package/dist/runtime/aspnet-runtime-prebuild.js +429 -0
- package/dist/runtime/attempt-database-broker.d.ts +89 -0
- package/dist/runtime/attempt-database-broker.js +390 -0
- package/dist/runtime/attempt-node-runtime-launcher.d.ts +2 -1
- package/dist/runtime/attempt-node-runtime-launcher.js +57 -18
- package/dist/runtime/attempt-path-operation.d.ts +21 -0
- package/dist/runtime/attempt-path-operation.js +94 -0
- package/dist/runtime/attempt-private-directory.d.ts +6 -0
- package/dist/runtime/attempt-private-directory.js +54 -0
- package/dist/runtime/attempt-process-supervisor.d.ts +35 -0
- package/dist/runtime/attempt-process-supervisor.js +144 -23
- package/dist/runtime/canonical-source-file.d.ts +3 -0
- package/dist/runtime/canonical-source-file.js +54 -0
- package/dist/runtime/docker-engine-probe.d.ts +90 -0
- package/dist/runtime/docker-engine-probe.js +333 -0
- package/dist/runtime/generated-output-reset.d.ts +10 -0
- package/dist/runtime/generated-output-reset.js +62 -8
- package/dist/runtime/host-database-capability-boundary.d.ts +16 -0
- package/dist/runtime/host-database-capability-boundary.js +63 -0
- package/dist/runtime/host-diagnostics.d.ts +11 -0
- package/dist/runtime/host-diagnostics.js +192 -0
- package/dist/runtime/host-execution-root.d.ts +4 -0
- package/dist/runtime/host-execution-root.js +39 -0
- package/dist/runtime/host-facts.d.ts +36 -0
- package/dist/runtime/host-facts.js +95 -0
- package/dist/runtime/host-listener-inspector.d.ts +8 -1
- package/dist/runtime/host-listener-inspector.js +121 -60
- 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 +184 -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 +185 -0
- package/dist/runtime/host-load-runtime-settings.d.ts +25 -0
- package/dist/runtime/host-load-runtime-settings.js +78 -0
- package/dist/runtime/host-load-sampler.d.ts +149 -0
- package/dist/runtime/host-load-sampler.js +291 -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 -1
- package/dist/runtime/host-runtime-contract.js +174 -56
- package/dist/runtime/host-runtime-lease.d.ts +163 -9
- package/dist/runtime/host-runtime-lease.js +1464 -121
- package/dist/runtime/host-runtime-promotion.d.ts +35 -3
- package/dist/runtime/host-runtime-promotion.js +107 -12
- 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 +607 -0
- package/dist/runtime/memory-capacity.d.ts +70 -0
- package/dist/runtime/memory-capacity.js +123 -0
- package/dist/runtime/native-test-postgres-fallback.d.ts +25 -0
- package/dist/runtime/native-test-postgres-fallback.js +91 -0
- package/dist/runtime/node-host-runtime-adapter.d.ts +3 -0
- package/dist/runtime/node-host-runtime-adapter.js +29 -11
- package/dist/runtime/pinned-directory-process.d.ts +9 -0
- package/dist/runtime/pinned-directory-process.js +61 -0
- package/dist/runtime/pinned-source-directory.d.ts +17 -0
- package/dist/runtime/pinned-source-directory.js +94 -0
- 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 +19 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +113 -59
- package/dist/runtime/prepared-runtime-identity.d.ts +6 -0
- package/dist/runtime/prepared-runtime-identity.js +218 -0
- package/dist/runtime/prepared-runtime-readiness.d.ts +30 -0
- package/dist/runtime/prepared-runtime-readiness.js +285 -0
- package/dist/runtime/runner-settings.d.ts +100 -0
- package/dist/runtime/runner-settings.js +214 -0
- package/dist/runtime/runtime-isolation-lease.js +4 -1
- package/dist/runtime/workspace-resource-estimator.d.ts +17 -0
- package/dist/runtime/workspace-resource-estimator.js +174 -0
- package/dist/sandbox/host-capacity-broker.d.ts +113 -0
- package/dist/sandbox/host-capacity-broker.js +334 -0
- package/dist/sandbox/preflight.d.ts +31 -1
- package/dist/sandbox/preflight.js +237 -51
- package/dist/sandbox/repository-lock.d.ts +2 -1
- package/dist/sandbox/repository-lock.js +19 -9
- package/dist/sandbox/runtime.d.ts +16 -2
- package/dist/sandbox/runtime.js +182 -109
- package/dist/sandbox/sandbox-cgroups.d.ts +14 -0
- package/dist/sandbox/sandbox-cgroups.js +42 -0
- package/dist/sandbox/sandbox.types.d.ts +7 -0
- package/dist/sandbox/workspace.service.js +2 -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 +44 -1
- package/dist/session/managed-attempt-tools.js +816 -46
- package/dist/session/managed-build-coordinator.d.ts +5 -0
- package/dist/session/managed-build-coordinator.js +109 -0
- package/dist/session/managed-build-receipt.d.ts +21 -0
- package/dist/session/managed-build-receipt.js +337 -0
- package/dist/session/managed-command-host-load.d.ts +47 -0
- package/dist/session/managed-command-host-load.js +83 -0
- package/dist/session/managed-verification-timeouts.d.ts +8 -0
- package/dist/session/managed-verification-timeouts.js +17 -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 +24 -0
- package/dist/session/phase-completion.artifacts.js +319 -44
- 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 +15 -1
- package/dist/session/phase-completion.helpers.d.ts +5 -0
- package/dist/session/phase-completion.helpers.js +43 -19
- package/dist/session/phase-completion.js +18 -3
- package/dist/session/phase-completion.prompts.js +22 -18
- package/dist/session/phase-completion.runtime.d.ts +6 -2
- package/dist/session/phase-completion.runtime.js +191 -47
- 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 +250 -226
- package/dist/session/phase-completion.validation.d.ts +3 -3
- package/dist/session/phase-completion.validation.js +135 -89
- 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-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 +11 -0
- package/dist/session/pre-publication-step-execution.js +49 -0
- package/dist/session/pre-publication-validation.d.ts +87 -0
- package/dist/session/pre-publication-validation.js +668 -0
- package/dist/session/provider-cli/bridge-cli.conformance-fixtures.d.ts +72 -0
- package/dist/session/provider-cli/bridge-cli.conformance-fixtures.js +462 -0
- package/dist/session/provider-cli/ci-repair-readiness.d.ts +8 -0
- package/dist/session/provider-cli/ci-repair-readiness.js +23 -3
- package/dist/session/provider-cli/claude-background-work.d.ts +66 -0
- package/dist/session/provider-cli/claude-background-work.js +180 -0
- package/dist/session/provider-cli/pre-publication-repair.d.ts +40 -0
- package/dist/session/provider-cli/pre-publication-repair.js +243 -0
- package/dist/session/provider-cli/provider-cli.artifacts.d.ts +6 -3
- package/dist/session/provider-cli/provider-cli.artifacts.js +62 -22
- package/dist/session/provider-cli/provider-cli.child-environment.d.ts +7 -0
- package/dist/session/provider-cli/provider-cli.child-environment.js +73 -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.deterministic.js +34 -2
- 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 +92 -14
- package/dist/session/provider-cli/provider-cli.process.js +583 -210
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +12 -0
- package/dist/session/provider-cli/provider-cli.prompts.js +200 -25
- package/dist/session/provider-cli/provider-cli.repair-epoch.d.ts +6 -0
- package/dist/session/provider-cli/provider-cli.repair-epoch.js +11 -0
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +14 -1
- package/dist/session/provider-cli/provider-cli.runtime.js +61 -3
- package/dist/session/provider-cli/provider-cli.service.d.ts +33 -5
- package/dist/session/provider-cli/provider-cli.service.js +819 -84
- package/dist/session/provider-cli/provider-cli.types.d.ts +32 -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-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 +18 -0
- package/dist/session/provider-cli/provider-output-state.js +376 -17
- package/dist/session/provider-cli/provider-usage.d.ts +9 -1
- package/dist/session/provider-cli/provider-usage.js +28 -5
- package/dist/session/publishable-worktree-changes.d.ts +54 -0
- package/dist/session/publishable-worktree-changes.js +193 -0
- package/dist/session/sandbox-host-capacity-gate.d.ts +30 -0
- package/dist/session/sandbox-host-capacity-gate.js +238 -0
- package/dist/session/session-redaction.js +434 -9
- package/dist/session/session.base.d.ts +9 -1
- package/dist/session/session.service.d.ts +9 -2
- package/dist/session/session.service.js +264 -27
- package/dist/session/session.types.d.ts +150 -2
- package/dist/session/session.types.js +599 -7
- package/dist/session/unreferenced-exports.d.ts +12 -0
- package/dist/session/unreferenced-exports.js +59 -0
- package/dist/session/workspace-runtime-fingerprint.d.ts +32 -0
- package/dist/session/workspace-runtime-fingerprint.js +138 -0
- package/dist/shared/format/format-bytes.d.ts +1 -0
- package/dist/shared/format/format-bytes.js +9 -0
- package/dist/shared/git/git.js +2 -1
- package/dist/shared/path/path.d.ts +1 -0
- package/dist/shared/path/path.js +18 -0
- package/dist/shared/process/dotnet-toolchain.d.ts +17 -0
- package/dist/shared/process/dotnet-toolchain.js +307 -0
- package/dist/shared/process/host-process-recovery.js +3 -2
- package/dist/shared/process/host-process-supervisor.d.ts +3 -0
- package/dist/shared/process/host-process-supervisor.js +61 -12
- 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 +69 -0
- package/dist/shared/process/process.js +223 -16
- 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.d.ts +3 -0
- package/dist/shared/provider-capabilities.js +84 -0
- package/dist/shared/redaction/redaction.d.ts +1 -0
- package/dist/shared/redaction/redaction.js +39 -3
- package/dist/shared/time/monotonic.d.ts +1 -0
- package/dist/shared/time/monotonic.js +8 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/dependency-warmer.d.ts +66 -9
- package/dist/worktree/dependency-warmer.js +609 -24
- package/dist/worktree/index.d.ts +1 -1
- package/dist/worktree/story-qa-reference-hydrator.d.ts +4 -0
- package/dist/worktree/story-qa-reference-hydrator.js +253 -0
- package/dist/worktree/worktree.service.d.ts +17 -1
- package/dist/worktree/worktree.service.js +293 -21
- package/dist/worktree/worktree.types.d.ts +14 -1
- package/package.json +8 -6
- package/runtime/windows-job-supervisor.ps1 +380 -14
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { defaultWorktreeCoordinatorStateRoot } from "../worktree/worktree-paths.js";
|
|
5
|
+
import { monotonicNowMs } from "../shared/time/monotonic.js";
|
|
6
|
+
const heartbeatIntervalMs = 30_000;
|
|
7
|
+
const staleAfterMs = 2 * 60_000;
|
|
8
|
+
const acquisitionTimeoutMs = 2 * 60 * 60_000;
|
|
9
|
+
const pollIntervalMs = 100;
|
|
10
|
+
export class ManagedBuildCoordinator {
|
|
11
|
+
stateRoot;
|
|
12
|
+
constructor(stateRoot = defaultWorktreeCoordinatorStateRoot()) {
|
|
13
|
+
this.stateRoot = stateRoot;
|
|
14
|
+
}
|
|
15
|
+
async withBuildSlot(worktreePath, action, onWaiting, signal) {
|
|
16
|
+
const resolved = path.resolve(worktreePath);
|
|
17
|
+
const key = createHash("sha256").update(resolved.toLowerCase()).digest("hex");
|
|
18
|
+
const root = path.join(this.stateRoot, "managed-build-locks");
|
|
19
|
+
const target = path.join(root, `${key}.lock`);
|
|
20
|
+
const token = randomUUID();
|
|
21
|
+
const startedAt = monotonicNowMs();
|
|
22
|
+
let waitingReported = false;
|
|
23
|
+
await fs.mkdir(root, { recursive: true, mode: 0o700 });
|
|
24
|
+
while (true) {
|
|
25
|
+
if (signal?.aborted) {
|
|
26
|
+
throw signal.reason instanceof Error ? signal.reason : new Error("managed_build_slot_cancelled: the build-slot wait was cancelled.");
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const handle = await fs.open(target, "wx", 0o600);
|
|
30
|
+
try {
|
|
31
|
+
await handle.writeFile(`${JSON.stringify({
|
|
32
|
+
schemaVersion: 1,
|
|
33
|
+
token,
|
|
34
|
+
pid: process.pid,
|
|
35
|
+
worktreePath: resolved,
|
|
36
|
+
acquiredAt: new Date().toISOString(),
|
|
37
|
+
})}\n`);
|
|
38
|
+
await handle.sync();
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
await handle.close();
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error.code !== "EEXIST")
|
|
47
|
+
throw error;
|
|
48
|
+
if (await staleLock(target)) {
|
|
49
|
+
await fs.rm(target, { force: true });
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!waitingReported) {
|
|
53
|
+
waitingReported = true;
|
|
54
|
+
await onWaiting?.();
|
|
55
|
+
}
|
|
56
|
+
if (monotonicNowMs() - startedAt >= acquisitionTimeoutMs) {
|
|
57
|
+
throw new Error("managed_build_slot_timeout: another build held this worktree for more than two hours.", { cause: error });
|
|
58
|
+
}
|
|
59
|
+
await delay(pollIntervalMs);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const heartbeat = setInterval(() => void refreshHeartbeat(target, token), heartbeatIntervalMs);
|
|
63
|
+
heartbeat.unref?.();
|
|
64
|
+
try {
|
|
65
|
+
return await action(Math.round(monotonicNowMs() - startedAt));
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
clearInterval(heartbeat);
|
|
69
|
+
const owner = await readOwner(target);
|
|
70
|
+
if (owner?.token === token)
|
|
71
|
+
await fs.rm(target, { force: true });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function staleLock(target) {
|
|
76
|
+
const stat = await fs.lstat(target).catch(() => null);
|
|
77
|
+
if (!stat || stat.isSymbolicLink() || !stat.isFile() || stat.size > 16 * 1024)
|
|
78
|
+
return true;
|
|
79
|
+
if (Date.now() - stat.mtimeMs >= staleAfterMs)
|
|
80
|
+
return true;
|
|
81
|
+
const owner = await readOwner(target);
|
|
82
|
+
// A competing process can observe the exclusive lock file between open("wx")
|
|
83
|
+
// and the owner JSON being flushed. Keep a fresh unreadable lock rather than
|
|
84
|
+
// deleting a lock whose writer is still publishing its ownership record.
|
|
85
|
+
if (!owner || !Number.isInteger(owner.pid) || (owner.pid ?? 0) <= 0)
|
|
86
|
+
return false;
|
|
87
|
+
try {
|
|
88
|
+
process.kill(owner.pid, 0);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function refreshHeartbeat(target, token) {
|
|
96
|
+
const owner = await readOwner(target);
|
|
97
|
+
if (owner?.token !== token)
|
|
98
|
+
return;
|
|
99
|
+
const now = new Date();
|
|
100
|
+
await fs.utimes(target, now, now).catch(() => undefined);
|
|
101
|
+
}
|
|
102
|
+
async function readOwner(target) {
|
|
103
|
+
return await fs.readFile(target, "utf8")
|
|
104
|
+
.then(value => JSON.parse(value))
|
|
105
|
+
.catch(() => null);
|
|
106
|
+
}
|
|
107
|
+
async function delay(ms) {
|
|
108
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
109
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface ManagedBuildReceiptEvidence {
|
|
2
|
+
readonly fingerprint: string;
|
|
3
|
+
readonly receiptPath: string;
|
|
4
|
+
readonly producerCommand: readonly string[];
|
|
5
|
+
readonly configuration: string;
|
|
6
|
+
readonly coverage: string;
|
|
7
|
+
readonly outputCount: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function publishManagedBuildReceipt(options: {
|
|
10
|
+
readonly worktreePath: string;
|
|
11
|
+
readonly command: string;
|
|
12
|
+
readonly args: readonly string[];
|
|
13
|
+
readonly configuration: string;
|
|
14
|
+
readonly coverage: string;
|
|
15
|
+
}): Promise<ManagedBuildReceiptEvidence | null>;
|
|
16
|
+
export declare function verifyManagedBuildReceipt(options: {
|
|
17
|
+
readonly worktreePath: string;
|
|
18
|
+
readonly configuration: string;
|
|
19
|
+
readonly requiredCoverage: string;
|
|
20
|
+
}): Promise<ManagedBuildReceiptEvidence>;
|
|
21
|
+
export declare function managedBuildConfiguration(command: string, args: readonly string[]): string;
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { defaultWorktreeCoordinatorStateRoot } from "../worktree/worktree-paths.js";
|
|
6
|
+
const schemaVersion = 1;
|
|
7
|
+
const maximumRetainedReceipts = 8;
|
|
8
|
+
const maximumOutputManifestEntries = 20_000;
|
|
9
|
+
const skippedTraversalDirectories = new Set([
|
|
10
|
+
".git",
|
|
11
|
+
".forge-worktrees",
|
|
12
|
+
"node_modules",
|
|
13
|
+
".venv",
|
|
14
|
+
"bin",
|
|
15
|
+
"obj",
|
|
16
|
+
"TestResults",
|
|
17
|
+
"artifacts",
|
|
18
|
+
]);
|
|
19
|
+
export async function publishManagedBuildReceipt(options) {
|
|
20
|
+
const worktreePath = path.resolve(options.worktreePath);
|
|
21
|
+
const inputs = await currentBuildInputs(worktreePath);
|
|
22
|
+
const outputs = await collectBuildOutputs(worktreePath);
|
|
23
|
+
if (outputs.length === 0)
|
|
24
|
+
return null;
|
|
25
|
+
const producerCommand = [options.command, ...options.args];
|
|
26
|
+
const producerCommandFingerprint = sha256Json(producerCommand);
|
|
27
|
+
const fingerprint = `sha256:${sha256Json({
|
|
28
|
+
...inputs,
|
|
29
|
+
configuration: options.configuration,
|
|
30
|
+
coverage: options.coverage,
|
|
31
|
+
producerCommandFingerprint,
|
|
32
|
+
outputs,
|
|
33
|
+
})}`;
|
|
34
|
+
const directory = managedBuildReceiptDirectory(worktreePath);
|
|
35
|
+
const receiptPath = path.join(directory, `${fingerprint.slice("sha256:".length)}.json`);
|
|
36
|
+
const receipt = {
|
|
37
|
+
schemaVersion,
|
|
38
|
+
fingerprint,
|
|
39
|
+
...inputs,
|
|
40
|
+
configuration: options.configuration,
|
|
41
|
+
coverage: options.coverage,
|
|
42
|
+
producerCommand,
|
|
43
|
+
producerCommandFingerprint,
|
|
44
|
+
outputs,
|
|
45
|
+
completedAt: new Date().toISOString(),
|
|
46
|
+
};
|
|
47
|
+
await fs.mkdir(directory, { recursive: true, mode: 0o700 });
|
|
48
|
+
await persistImmutableReceipt(receiptPath, receipt);
|
|
49
|
+
await persistJsonAtomically(path.join(directory, "current.json"), {
|
|
50
|
+
schemaVersion,
|
|
51
|
+
fingerprint,
|
|
52
|
+
receiptPath,
|
|
53
|
+
});
|
|
54
|
+
await pruneReceipts(directory, receiptPath);
|
|
55
|
+
return evidence(receipt, receiptPath);
|
|
56
|
+
}
|
|
57
|
+
export async function verifyManagedBuildReceipt(options) {
|
|
58
|
+
const worktreePath = path.resolve(options.worktreePath);
|
|
59
|
+
const directory = managedBuildReceiptDirectory(worktreePath);
|
|
60
|
+
const pointer = await readJson(path.join(directory, "current.json"));
|
|
61
|
+
const receiptPath = typeof pointer?.receiptPath === "string" ? pointer.receiptPath : "";
|
|
62
|
+
if (!receiptPath || path.dirname(receiptPath) !== directory) {
|
|
63
|
+
throw new Error("managed_verification_build_receipt_required: no verified local build receipt is available.");
|
|
64
|
+
}
|
|
65
|
+
const receipt = await readBuildReceipt(receiptPath);
|
|
66
|
+
if (!receipt || receipt.fingerprint !== pointer?.fingerprint) {
|
|
67
|
+
throw new Error("managed_verification_build_receipt_invalid: the local build receipt is missing or corrupt.");
|
|
68
|
+
}
|
|
69
|
+
if (receipt.configuration.toLowerCase() !== options.configuration.toLowerCase()) {
|
|
70
|
+
throw new Error(`managed_verification_build_receipt_mismatch: expected ${options.configuration} outputs but receipt proves ${receipt.configuration}.`);
|
|
71
|
+
}
|
|
72
|
+
if (receipt.coverage !== options.requiredCoverage) {
|
|
73
|
+
throw new Error(`managed_verification_build_receipt_mismatch: expected ${options.requiredCoverage} coverage but receipt proves ${receipt.coverage}.`);
|
|
74
|
+
}
|
|
75
|
+
const current = await currentBuildInputs(worktreePath);
|
|
76
|
+
if (receipt.sourceDigest !== current.sourceDigest
|
|
77
|
+
|| receipt.dependencyDigest !== current.dependencyDigest
|
|
78
|
+
|| receipt.toolchain !== current.toolchain
|
|
79
|
+
|| receipt.platform !== current.platform
|
|
80
|
+
|| receipt.architecture !== current.architecture
|
|
81
|
+
|| receipt.policyFingerprint !== current.policyFingerprint) {
|
|
82
|
+
throw new Error("managed_verification_build_receipt_mismatch: source, dependencies, toolchain, platform, or policy changed after the verified build.");
|
|
83
|
+
}
|
|
84
|
+
if (!(await outputsMatch(worktreePath, receipt.outputs))) {
|
|
85
|
+
throw new Error("managed_verification_build_receipt_output_missing: one or more verified build outputs are missing or changed.");
|
|
86
|
+
}
|
|
87
|
+
return evidence(receipt, receiptPath);
|
|
88
|
+
}
|
|
89
|
+
export function managedBuildConfiguration(command, args) {
|
|
90
|
+
const optionIndex = args.findIndex(argument => ["-c", "--configuration", "-Configuration"].includes(argument));
|
|
91
|
+
if (optionIndex >= 0 && args[optionIndex + 1])
|
|
92
|
+
return args[optionIndex + 1];
|
|
93
|
+
const inline = args.find(argument => /^--configuration=/iu.test(argument));
|
|
94
|
+
if (inline)
|
|
95
|
+
return inline.slice(inline.indexOf("=") + 1);
|
|
96
|
+
const executable = path.basename(command).toLowerCase();
|
|
97
|
+
const scriptIndex = args.findIndex(argument => argument.toLowerCase() === "-file");
|
|
98
|
+
const script = scriptIndex >= 0 ? path.basename(args[scriptIndex + 1] ?? "").toLowerCase() : "";
|
|
99
|
+
// The affected-tests runner builds Release before running the Release tier scripts.
|
|
100
|
+
const runsAffectedTests = args.some(argument => path.basename(argument).toLowerCase() === "run-affected-dotnet-tests.mjs");
|
|
101
|
+
return executable.includes("powershell") || executable === "pwsh" || executable === "pwsh.exe"
|
|
102
|
+
|| script.startsWith("run-dotnet-test-") || runsAffectedTests
|
|
103
|
+
? "Release"
|
|
104
|
+
: "Debug";
|
|
105
|
+
}
|
|
106
|
+
function managedBuildReceiptDirectory(worktreePath) {
|
|
107
|
+
const key = createHash("sha256").update(path.resolve(worktreePath).toLowerCase()).digest("hex");
|
|
108
|
+
return path.join(defaultWorktreeCoordinatorStateRoot(), "managed-build-receipts", key);
|
|
109
|
+
}
|
|
110
|
+
async function currentBuildInputs(worktreePath) {
|
|
111
|
+
const [sourceDigest, dependencyDigest, toolchain] = await Promise.all([
|
|
112
|
+
worktreeSourceDigest(worktreePath),
|
|
113
|
+
dependencyReceiptsDigest(worktreePath),
|
|
114
|
+
dotnetVersion(worktreePath),
|
|
115
|
+
]);
|
|
116
|
+
return {
|
|
117
|
+
sourceDigest,
|
|
118
|
+
dependencyDigest,
|
|
119
|
+
toolchain,
|
|
120
|
+
platform: process.platform,
|
|
121
|
+
architecture: process.arch,
|
|
122
|
+
policyFingerprint: `sha256:${sha256Json({
|
|
123
|
+
contract: "managed-build-reuse-v1",
|
|
124
|
+
runtimeMode: process.env.FORGE_RUNTIME_MODE ?? "host",
|
|
125
|
+
trustDomain: sha256Text(process.env.FORGE_TRUST_DOMAIN_ID ?? "default"),
|
|
126
|
+
invalidationEpoch: sha256Text(process.env.FORGE_BUILD_CACHE_EPOCH ?? "default"),
|
|
127
|
+
dotnetRoot: process.env.DOTNET_ROOT ?? null,
|
|
128
|
+
msbuildSdksPath: process.env.MSBuildSDKsPath ?? null,
|
|
129
|
+
nugetPackages: process.env.NUGET_PACKAGES ?? null,
|
|
130
|
+
})}`,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
async function worktreeSourceDigest(worktreePath) {
|
|
134
|
+
const names = await gitFileNames(worktreePath);
|
|
135
|
+
const hash = createHash("sha256");
|
|
136
|
+
for (const relative of names) {
|
|
137
|
+
const absolute = path.resolve(worktreePath, relative);
|
|
138
|
+
const containment = path.relative(worktreePath, absolute);
|
|
139
|
+
if (containment.startsWith(`..${path.sep}`) || path.isAbsolute(containment))
|
|
140
|
+
continue;
|
|
141
|
+
const stat = await fs.stat(absolute).catch(() => null);
|
|
142
|
+
if (!stat?.isFile())
|
|
143
|
+
continue;
|
|
144
|
+
hash.update(relative.replaceAll("\\", "/"));
|
|
145
|
+
hash.update("\0");
|
|
146
|
+
hash.update(await fs.readFile(absolute));
|
|
147
|
+
hash.update("\0");
|
|
148
|
+
}
|
|
149
|
+
return `sha256:${hash.digest("hex")}`;
|
|
150
|
+
}
|
|
151
|
+
async function gitFileNames(worktreePath) {
|
|
152
|
+
const output = await runProcess(worktreePath, "git", [
|
|
153
|
+
"ls-files",
|
|
154
|
+
"-z",
|
|
155
|
+
"--cached",
|
|
156
|
+
"--others",
|
|
157
|
+
"--exclude-standard",
|
|
158
|
+
]);
|
|
159
|
+
if (output.exitCode !== 0) {
|
|
160
|
+
throw new Error(`managed_build_source_digest_failed: git ls-files exited ${output.exitCode}.`);
|
|
161
|
+
}
|
|
162
|
+
return output.stdout.split("\0").filter(Boolean).sort((left, right) => left.localeCompare(right));
|
|
163
|
+
}
|
|
164
|
+
async function dependencyReceiptsDigest(worktreePath) {
|
|
165
|
+
const receipts = [];
|
|
166
|
+
await walkDirectories(worktreePath, 0, async (directory, name) => {
|
|
167
|
+
if (!["node_modules", ".venv", "obj"].includes(name))
|
|
168
|
+
return;
|
|
169
|
+
const receiptPath = path.join(directory, name, ".forge-dependency-preparation.json");
|
|
170
|
+
const content = await fs.readFile(receiptPath).catch(() => null);
|
|
171
|
+
if (content)
|
|
172
|
+
receipts.push({
|
|
173
|
+
path: path.relative(worktreePath, receiptPath).replaceAll("\\", "/"),
|
|
174
|
+
digest: createHash("sha256").update(content).digest("hex"),
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
receipts.sort((left, right) => left.path.localeCompare(right.path));
|
|
178
|
+
return `sha256:${sha256Json(receipts)}`;
|
|
179
|
+
}
|
|
180
|
+
async function collectBuildOutputs(worktreePath) {
|
|
181
|
+
const outputs = [];
|
|
182
|
+
await walkDirectories(worktreePath, 0, async (directory, name) => {
|
|
183
|
+
if (name !== "bin")
|
|
184
|
+
return;
|
|
185
|
+
await collectFiles(path.join(directory, name), worktreePath, outputs);
|
|
186
|
+
});
|
|
187
|
+
outputs.sort((left, right) => left.path.localeCompare(right.path));
|
|
188
|
+
if (outputs.length > maximumOutputManifestEntries) {
|
|
189
|
+
throw new Error(`managed_build_receipt_too_large: build produced more than ${maximumOutputManifestEntries} output files.`);
|
|
190
|
+
}
|
|
191
|
+
return outputs;
|
|
192
|
+
}
|
|
193
|
+
async function collectFiles(directory, worktreePath, outputs) {
|
|
194
|
+
const entries = await fs.readdir(directory, { withFileTypes: true }).catch(() => []);
|
|
195
|
+
for (const entry of entries) {
|
|
196
|
+
const absolute = path.join(directory, entry.name);
|
|
197
|
+
if (entry.isDirectory())
|
|
198
|
+
await collectFiles(absolute, worktreePath, outputs);
|
|
199
|
+
else if (entry.isFile()) {
|
|
200
|
+
const stat = await fs.stat(absolute);
|
|
201
|
+
outputs.push({
|
|
202
|
+
path: path.relative(worktreePath, absolute).replaceAll("\\", "/"),
|
|
203
|
+
size: stat.size,
|
|
204
|
+
digest: createHash("sha256").update(await fs.readFile(absolute)).digest("hex"),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async function walkDirectories(directory, depth, observe) {
|
|
210
|
+
if (depth > 8)
|
|
211
|
+
return;
|
|
212
|
+
const entries = await fs.readdir(directory, { withFileTypes: true }).catch(() => []);
|
|
213
|
+
for (const entry of entries) {
|
|
214
|
+
if (!entry.isDirectory())
|
|
215
|
+
continue;
|
|
216
|
+
await observe(directory, entry.name);
|
|
217
|
+
if (!skippedTraversalDirectories.has(entry.name) && !entry.name.startsWith(".")) {
|
|
218
|
+
await walkDirectories(path.join(directory, entry.name), depth + 1, observe);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async function outputsMatch(worktreePath, outputs) {
|
|
223
|
+
if (outputs.length === 0)
|
|
224
|
+
return false;
|
|
225
|
+
for (const output of outputs) {
|
|
226
|
+
const absolute = path.resolve(worktreePath, output.path);
|
|
227
|
+
const containment = path.relative(worktreePath, absolute);
|
|
228
|
+
if (containment.startsWith(`..${path.sep}`) || path.isAbsolute(containment))
|
|
229
|
+
return false;
|
|
230
|
+
const stat = await fs.stat(absolute).catch(() => null);
|
|
231
|
+
if (!stat?.isFile() || stat.size !== output.size)
|
|
232
|
+
return false;
|
|
233
|
+
const digest = createHash("sha256").update(await fs.readFile(absolute)).digest("hex");
|
|
234
|
+
if (digest !== output.digest)
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
async function dotnetVersion(cwd) {
|
|
240
|
+
const result = await runProcess(cwd, "dotnet", ["--version"]);
|
|
241
|
+
return result.exitCode === 0
|
|
242
|
+
? `dotnet@${result.stdout.trim() || result.stderr.trim() || "unknown"}`
|
|
243
|
+
: `dotnet@unavailable:${result.exitCode}`;
|
|
244
|
+
}
|
|
245
|
+
async function runProcess(cwd, command, args) {
|
|
246
|
+
return await new Promise((resolve, reject) => {
|
|
247
|
+
const child = spawn(command, args, { cwd, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] });
|
|
248
|
+
let stdout = "";
|
|
249
|
+
let stderr = "";
|
|
250
|
+
child.stdout.setEncoding("utf8");
|
|
251
|
+
child.stderr.setEncoding("utf8");
|
|
252
|
+
child.stdout.on("data", chunk => { stdout += String(chunk); });
|
|
253
|
+
child.stderr.on("data", chunk => { stderr += String(chunk); });
|
|
254
|
+
child.once("error", reject);
|
|
255
|
+
child.once("close", exitCode => resolve({ exitCode, stdout, stderr }));
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
async function persistImmutableReceipt(receiptPath, receipt) {
|
|
259
|
+
const existing = await readBuildReceipt(receiptPath);
|
|
260
|
+
if (existing)
|
|
261
|
+
return;
|
|
262
|
+
const temporaryPath = `${receiptPath}.${randomUUID()}.tmp`;
|
|
263
|
+
await fs.writeFile(temporaryPath, `${JSON.stringify(receipt, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
|
|
264
|
+
try {
|
|
265
|
+
await fs.rename(temporaryPath, receiptPath).catch(async (error) => {
|
|
266
|
+
if (error.code !== "EEXIST")
|
|
267
|
+
throw error;
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
finally {
|
|
271
|
+
await fs.rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
async function persistJsonAtomically(receiptPath, value) {
|
|
275
|
+
const temporaryPath = `${receiptPath}.${randomUUID()}.tmp`;
|
|
276
|
+
await fs.writeFile(temporaryPath, `${JSON.stringify(value, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
|
|
277
|
+
try {
|
|
278
|
+
await fs.rename(temporaryPath, receiptPath);
|
|
279
|
+
}
|
|
280
|
+
finally {
|
|
281
|
+
await fs.rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async function readBuildReceipt(receiptPath) {
|
|
285
|
+
const value = await readJson(receiptPath);
|
|
286
|
+
return value?.schemaVersion === schemaVersion
|
|
287
|
+
&& typeof value.fingerprint === "string"
|
|
288
|
+
&& typeof value.sourceDigest === "string"
|
|
289
|
+
&& typeof value.dependencyDigest === "string"
|
|
290
|
+
&& typeof value.toolchain === "string"
|
|
291
|
+
&& typeof value.policyFingerprint === "string"
|
|
292
|
+
&& typeof value.configuration === "string"
|
|
293
|
+
&& typeof value.coverage === "string"
|
|
294
|
+
&& Array.isArray(value.producerCommand)
|
|
295
|
+
&& Array.isArray(value.outputs)
|
|
296
|
+
? value
|
|
297
|
+
: null;
|
|
298
|
+
}
|
|
299
|
+
async function readJson(receiptPath) {
|
|
300
|
+
try {
|
|
301
|
+
return JSON.parse(await fs.readFile(receiptPath, "utf8"));
|
|
302
|
+
}
|
|
303
|
+
catch (error) {
|
|
304
|
+
if (error.code === "ENOENT" || error instanceof SyntaxError)
|
|
305
|
+
return null;
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
async function pruneReceipts(directory, preservedPath) {
|
|
310
|
+
const entries = await fs.readdir(directory, { withFileTypes: true });
|
|
311
|
+
const receipts = await Promise.all(entries
|
|
312
|
+
.filter(entry => entry.isFile() && /^[0-9a-f]{64}\.json$/u.test(entry.name))
|
|
313
|
+
.map(async (entry) => {
|
|
314
|
+
const receiptPath = path.join(directory, entry.name);
|
|
315
|
+
return { receiptPath, stat: await fs.stat(receiptPath) };
|
|
316
|
+
}));
|
|
317
|
+
receipts.sort((left, right) => right.stat.mtimeMs - left.stat.mtimeMs);
|
|
318
|
+
await Promise.all(receipts.slice(maximumRetainedReceipts)
|
|
319
|
+
.filter(entry => entry.receiptPath !== preservedPath)
|
|
320
|
+
.map(entry => fs.rm(entry.receiptPath, { force: true })));
|
|
321
|
+
}
|
|
322
|
+
function evidence(receipt, receiptPath) {
|
|
323
|
+
return {
|
|
324
|
+
fingerprint: receipt.fingerprint,
|
|
325
|
+
receiptPath,
|
|
326
|
+
producerCommand: receipt.producerCommand,
|
|
327
|
+
configuration: receipt.configuration,
|
|
328
|
+
coverage: receipt.coverage,
|
|
329
|
+
outputCount: receipt.outputs.length,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
function sha256Json(value) {
|
|
333
|
+
return sha256Text(JSON.stringify(value));
|
|
334
|
+
}
|
|
335
|
+
function sha256Text(value) {
|
|
336
|
+
return createHash("sha256").update(value).digest("hex");
|
|
337
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type HostLoadStepGate } from "../runtime/host-load-governor.js";
|
|
2
|
+
import { type HostLoadStepClassification, type ManagedCommandStepInput } from "../runtime/host-load-step-class.js";
|
|
3
|
+
import { type HostLoadStepControl } from "../runtime/host-load-step-run.js";
|
|
4
|
+
/** Host-load facts a managed command receipt carries; absent while the governor is off. */
|
|
5
|
+
export interface ManagedCommandHostLoadFields {
|
|
6
|
+
readonly stepClass: string;
|
|
7
|
+
/** Queue time before admission; never part of elapsed_ms. */
|
|
8
|
+
readonly waitMs: number;
|
|
9
|
+
/** Set while the command is still queued: the receipt reports the wait so far. */
|
|
10
|
+
readonly waitStartedAt?: number;
|
|
11
|
+
/** Why it waits, for example for the host test slot and who holds it (decision 0018). */
|
|
12
|
+
readonly waitDetail?: string;
|
|
13
|
+
/** When the command was admitted; elapsed_ms counts from here. */
|
|
14
|
+
readonly executionStartedAt?: number;
|
|
15
|
+
}
|
|
16
|
+
/** This MCP server's governor, built once from the environment the bridge handed it. */
|
|
17
|
+
export declare function managedCommandHostLoadGate(): HostLoadStepGate | undefined;
|
|
18
|
+
/** Test support: installs a gate (undefined disables it; null rebuilds from the environment). */
|
|
19
|
+
export declare function setManagedCommandHostLoadGateForTesting(value: HostLoadStepGate | undefined | null): void;
|
|
20
|
+
/**
|
|
21
|
+
* Admits one managed command. The waiting receipt is written while the command
|
|
22
|
+
* is queued and always lands before the launch receipts, so a stale "waiting"
|
|
23
|
+
* write can never replace a later status.
|
|
24
|
+
*/
|
|
25
|
+
export declare class ManagedCommandHostCapacity {
|
|
26
|
+
private readonly persistWaiting;
|
|
27
|
+
private readonly gateFor;
|
|
28
|
+
readonly classification: HostLoadStepClassification;
|
|
29
|
+
private readonly label;
|
|
30
|
+
private requestedAtMs;
|
|
31
|
+
private waitingWrite;
|
|
32
|
+
constructor(input: ManagedCommandStepInput & {
|
|
33
|
+
readonly label: string;
|
|
34
|
+
}, persistWaiting: (fields: ManagedCommandHostLoadFields) => Promise<void>, gateFor?: () => HostLoadStepGate | undefined);
|
|
35
|
+
/** Runs `launch` once admitted; `fields` is undefined with the governor off. */
|
|
36
|
+
run<T>(signal: AbortSignal, launch: (fields: ManagedCommandHostLoadFields | undefined, control: HostLoadStepControl) => Promise<T>, onReleaseFailed?: (error: unknown) => Promise<void>): Promise<T>;
|
|
37
|
+
/**
|
|
38
|
+
* Host-load facts for a command that was never admitted (it timed out in
|
|
39
|
+
* the queue or was stopped while waiting), once its waiting receipt landed.
|
|
40
|
+
*/
|
|
41
|
+
unadmittedFields(error: unknown): Promise<ManagedCommandHostLoadFields | undefined>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The managed command's process tree as its supervisor recorded it, for its
|
|
45
|
+
* host load token: a reclaim then stops exactly this tree before reuse.
|
|
46
|
+
*/
|
|
47
|
+
export declare function attachManagedCommandProcess(control: HostLoadStepControl, metadataPath: string): Promise<void>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { hostLoadGovernorFromChildEnvironment } from "../runtime/host-load-child-environment.js";
|
|
2
|
+
import { HostCapacityTimeoutError } from "../runtime/host-load-governor.js";
|
|
3
|
+
import { classifyManagedCommand } from "../runtime/host-load-step-class.js";
|
|
4
|
+
import { attachSupervisedStepProcess, runHostGovernedStep } from "../runtime/host-load-step-run.js";
|
|
5
|
+
let gate = null;
|
|
6
|
+
/** This MCP server's governor, built once from the environment the bridge handed it. */
|
|
7
|
+
export function managedCommandHostLoadGate() {
|
|
8
|
+
if (gate === null)
|
|
9
|
+
gate = hostLoadGovernorFromChildEnvironment();
|
|
10
|
+
return gate;
|
|
11
|
+
}
|
|
12
|
+
/** Test support: installs a gate (undefined disables it; null rebuilds from the environment). */
|
|
13
|
+
export function setManagedCommandHostLoadGateForTesting(value) {
|
|
14
|
+
gate = value;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Admits one managed command. The waiting receipt is written while the command
|
|
18
|
+
* is queued and always lands before the launch receipts, so a stale "waiting"
|
|
19
|
+
* write can never replace a later status.
|
|
20
|
+
*/
|
|
21
|
+
export class ManagedCommandHostCapacity {
|
|
22
|
+
persistWaiting;
|
|
23
|
+
gateFor;
|
|
24
|
+
classification;
|
|
25
|
+
label;
|
|
26
|
+
requestedAtMs = null;
|
|
27
|
+
waitingWrite = Promise.resolve();
|
|
28
|
+
constructor(input, persistWaiting, gateFor = managedCommandHostLoadGate) {
|
|
29
|
+
this.persistWaiting = persistWaiting;
|
|
30
|
+
this.gateFor = gateFor;
|
|
31
|
+
this.classification = classifyManagedCommand(input);
|
|
32
|
+
this.label = input.label;
|
|
33
|
+
}
|
|
34
|
+
/** Runs `launch` once admitted; `fields` is undefined with the governor off. */
|
|
35
|
+
async run(signal, launch, onReleaseFailed) {
|
|
36
|
+
const current = this.gateFor();
|
|
37
|
+
if (current?.enabled)
|
|
38
|
+
this.requestedAtMs = Date.now();
|
|
39
|
+
return await runHostGovernedStep(current, {
|
|
40
|
+
classification: this.classification,
|
|
41
|
+
label: this.label,
|
|
42
|
+
dispatchAttemptId: process.env.FORGE_DISPATCH_ATTEMPT_ID?.trim() || null,
|
|
43
|
+
// An agent waits on its managed commands; readiness probes never start one.
|
|
44
|
+
priority: "foreground",
|
|
45
|
+
signal,
|
|
46
|
+
// The receipt says host_capacity_token_fenced itself (termination_reason).
|
|
47
|
+
reportsOwnFence: true,
|
|
48
|
+
onWaiting: decision => {
|
|
49
|
+
this.waitingWrite = this.persistWaiting({
|
|
50
|
+
stepClass: this.classification.stepClass,
|
|
51
|
+
waitMs: 0,
|
|
52
|
+
waitStartedAt: this.requestedAtMs ?? Date.now(),
|
|
53
|
+
waitDetail: decision.detail,
|
|
54
|
+
}).catch(() => undefined);
|
|
55
|
+
},
|
|
56
|
+
onReleaseFailed,
|
|
57
|
+
}, async (waitMs, control) => {
|
|
58
|
+
await this.waitingWrite;
|
|
59
|
+
// The launch timer and timeout_ms start inside `launch`, after admission.
|
|
60
|
+
return await launch(waitMs === null
|
|
61
|
+
? undefined
|
|
62
|
+
: { stepClass: this.classification.stepClass, waitMs, executionStartedAt: Date.now() }, control);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Host-load facts for a command that was never admitted (it timed out in
|
|
67
|
+
* the queue or was stopped while waiting), once its waiting receipt landed.
|
|
68
|
+
*/
|
|
69
|
+
async unadmittedFields(error) {
|
|
70
|
+
await this.waitingWrite;
|
|
71
|
+
if (this.requestedAtMs === null)
|
|
72
|
+
return undefined;
|
|
73
|
+
const waitMs = error instanceof HostCapacityTimeoutError ? error.waitMs : Date.now() - this.requestedAtMs;
|
|
74
|
+
return { stepClass: this.classification.stepClass, waitMs: Math.round(waitMs), executionStartedAt: Date.now() };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The managed command's process tree as its supervisor recorded it, for its
|
|
79
|
+
* host load token: a reclaim then stops exactly this tree before reuse.
|
|
80
|
+
*/
|
|
81
|
+
export async function attachManagedCommandProcess(control, metadataPath) {
|
|
82
|
+
await attachSupervisedStepProcess(control, metadataPath);
|
|
83
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const managedQuickTierTimeoutMs = 900000;
|
|
2
|
+
export declare const managedLongTierTimeoutMs = 2700000;
|
|
3
|
+
export declare const managedShardWaveTimeoutMs = 900000;
|
|
4
|
+
export declare const managedMaximumCommandTimeoutMs = 7200000;
|
|
5
|
+
export declare function managedVerificationTierTimeoutMs(tier: string, allShards?: {
|
|
6
|
+
readonly shardCount: number;
|
|
7
|
+
readonly maxParallelism: number;
|
|
8
|
+
}): number;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Runner-owned limits for managed .NET verification. The comment-repair prompt quotes
|
|
2
|
+
// them, so an agent knows a tier that is still running has not failed: Forge terminates
|
|
3
|
+
// a hung command at its limit and the receipt then reports timed_out.
|
|
4
|
+
export const managedQuickTierTimeoutMs = 900_000;
|
|
5
|
+
export const managedLongTierTimeoutMs = 2_700_000;
|
|
6
|
+
export const managedShardWaveTimeoutMs = 900_000;
|
|
7
|
+
export const managedMaximumCommandTimeoutMs = 7_200_000;
|
|
8
|
+
export function managedVerificationTierTimeoutMs(tier, allShards) {
|
|
9
|
+
if (tier === "Unit" || tier === "Contract")
|
|
10
|
+
return managedQuickTierTimeoutMs;
|
|
11
|
+
if (!allShards)
|
|
12
|
+
return managedLongTierTimeoutMs;
|
|
13
|
+
// One window covers the build and every wave of shards, so a single-shard limit would
|
|
14
|
+
// end a healthy all-shard run of a large tier.
|
|
15
|
+
const waves = Math.ceil(allShards.shardCount / Math.max(1, allShards.maxParallelism));
|
|
16
|
+
return Math.min(managedMaximumCommandTimeoutMs, Math.max(managedLongTierTimeoutMs, managedShardWaveTimeoutMs * (waves + 1)));
|
|
17
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP clients sometimes send a structured tool argument as its JSON text, for
|
|
3
|
+
* example `sidecars: "[{...}]"` instead of `sidecars: [{...}]`. At the tool-call
|
|
4
|
+
* boundary, before any validation, this replaces such a string with its parsed
|
|
5
|
+
* value, but only where the tool's declared input schema says the value is an
|
|
6
|
+
* array or an object and the string parses to exactly that kind of value.
|
|
7
|
+
*
|
|
8
|
+
* Nothing else is coerced: a string field that happens to contain JSON stays a
|
|
9
|
+
* string, an undeclared field is left alone, and a malformed string is passed
|
|
10
|
+
* through unchanged so the validator reports it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function coerceJsonStringArguments(value: unknown, schema: unknown): unknown;
|
|
13
|
+
type StructuredKind = "array" | "object";
|
|
14
|
+
/**
|
|
15
|
+
* Explains why a value is not the expected array or object, for validator
|
|
16
|
+
* messages. A string gets an explicit instruction because that is the usual
|
|
17
|
+
* client mistake; the hint never echoes the value itself.
|
|
18
|
+
*/
|
|
19
|
+
export declare function structuredTypeHint(value: unknown, expected: StructuredKind): string;
|
|
20
|
+
/** True when a tool call arrived with no usable arguments at all. */
|
|
21
|
+
export declare function isEmptyToolInput(input: unknown): boolean;
|
|
22
|
+
export declare const emptyToolInputHint: string;
|
|
23
|
+
export {};
|