@loucompanion/forge-bridge 0.1.4-dev.fb770705136a → 0.2.1-dev.0f8fb73715e8
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 +183 -4
- package/dist/bridge/bridge-chat-giphy-look.d.ts +48 -0
- package/dist/bridge/bridge-chat-giphy-look.js +55 -0
- package/dist/bridge/bridge-chat-provider-command.d.ts +40 -0
- package/dist/bridge/bridge-chat-provider-command.js +230 -0
- package/dist/bridge/bridge-chat.types.d.ts +20 -0
- package/dist/bridge/bridge-chat.types.js +28 -1
- package/dist/bridge/bridge-state-file.d.ts +161 -0
- package/dist/bridge/bridge-state-file.js +376 -0
- package/dist/bridge/bridge-state-io.d.ts +15 -0
- package/dist/bridge/bridge-state-io.js +68 -0
- package/dist/bridge/bridge-state-lock.d.ts +11 -0
- package/dist/bridge/bridge-state-lock.js +102 -0
- package/dist/bridge/bridge-state-unpublished.d.ts +17 -0
- package/dist/bridge/bridge-state-unpublished.js +111 -0
- package/dist/bridge/bridge.base.d.ts +9 -0
- package/dist/bridge/bridge.service.d.ts +102 -14
- package/dist/bridge/bridge.service.js +499 -471
- package/dist/bridge/bridge.types.d.ts +37 -0
- package/dist/bridge/bridge.types.js +2 -1
- package/dist/bridge/default-session-service.d.ts +13 -0
- package/dist/bridge/default-session-service.js +22 -0
- package/dist/bridge/dispatch-offer-policy.d.ts +22 -0
- package/dist/bridge/dispatch-offer-policy.js +58 -0
- package/dist/bridge/in-flight-registry.d.ts +62 -0
- package/dist/bridge/in-flight-registry.js +207 -0
- package/dist/bridge/local-yield-flag.d.ts +6 -0
- package/dist/bridge/local-yield-flag.js +34 -0
- package/dist/bridge/readiness-result-outbox.d.ts +5 -1
- package/dist/bridge/readiness-result-outbox.js +20 -0
- package/dist/bridge/runner-host-capabilities.d.ts +5 -0
- package/dist/bridge/runner-host-capabilities.js +31 -1
- package/dist/bridge/sandbox-resource-accounting.d.ts +16 -0
- package/dist/bridge/sandbox-resource-accounting.js +150 -0
- package/dist/bridge/test-postgres-capability.d.ts +14 -0
- package/dist/bridge/test-postgres-capability.js +120 -0
- package/dist/bridge/windows-bash-resolution.d.ts +31 -0
- package/dist/bridge/windows-bash-resolution.js +153 -0
- package/dist/cleanup/cleanup-wip.d.ts +12 -0
- package/dist/cleanup/cleanup-wip.js +66 -0
- package/dist/cleanup/cleanup.service.d.ts +20 -1
- package/dist/cleanup/cleanup.service.js +150 -169
- package/dist/cleanup/cleanup.types.d.ts +6 -0
- package/dist/cleanup/forge-owned-paths.d.ts +11 -0
- package/dist/cleanup/forge-owned-paths.js +62 -0
- package/dist/cleanup/host-runtime-ledger.d.ts +7 -0
- package/dist/cleanup/host-runtime-ledger.js +130 -0
- package/dist/cleanup/managed-branch-sweep.d.ts +27 -0
- package/dist/cleanup/managed-branch-sweep.js +111 -0
- package/dist/cli/cli.help.js +75 -13
- package/dist/cli/cli.service.d.ts +19 -10
- package/dist/cli/cli.service.js +161 -228
- package/dist/cli/idle-status.d.ts +84 -0
- package/dist/cli/idle-status.js +181 -0
- package/dist/cli/local-config.d.ts +18 -1
- package/dist/cli/local-config.js +67 -5
- package/dist/cli/provider-cli-advertising.d.ts +24 -0
- package/dist/cli/provider-cli-advertising.js +88 -0
- package/dist/cli/provider-cli-detection.d.ts +10 -0
- package/dist/cli/provider-cli-detection.js +160 -0
- package/dist/runtime/aspnet-runtime-prebuild.d.ts +20 -0
- package/dist/runtime/aspnet-runtime-prebuild.js +184 -18
- package/dist/runtime/attempt-database-broker.d.ts +1 -1
- package/dist/runtime/attempt-node-runtime-launcher.d.ts +36 -0
- package/dist/runtime/attempt-node-runtime-launcher.js +169 -0
- package/dist/runtime/attempt-process-supervisor.d.ts +2 -0
- package/dist/runtime/attempt-process-supervisor.js +12 -6
- package/dist/runtime/docker-engine-probe.d.ts +5 -0
- package/dist/runtime/docker-engine-probe.js +7 -0
- package/dist/runtime/dotnet-build-outputs.d.ts +107 -0
- package/dist/runtime/dotnet-build-outputs.js +402 -0
- package/dist/runtime/ephemeral-test-postgres-grant.d.ts +37 -0
- package/dist/runtime/ephemeral-test-postgres-grant.js +85 -0
- package/dist/runtime/ephemeral-test-postgres.d.ts +57 -0
- package/dist/runtime/ephemeral-test-postgres.js +303 -0
- package/dist/runtime/host-facts.d.ts +2 -1
- package/dist/runtime/host-listener-inspector.d.ts +26 -0
- package/dist/runtime/host-listener-inspector.js +84 -3
- package/dist/runtime/host-load-admission.d.ts +72 -0
- package/dist/runtime/host-load-admission.js +99 -0
- package/dist/runtime/host-load-capacity.d.ts +198 -0
- package/dist/runtime/host-load-capacity.js +353 -0
- package/dist/runtime/host-load-child-environment.d.ts +40 -0
- package/dist/runtime/host-load-child-environment.js +110 -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 +82 -0
- package/dist/runtime/host-load-governor-config.js +104 -0
- package/dist/runtime/host-load-governor.d.ts +273 -0
- package/dist/runtime/host-load-governor.js +831 -0
- package/dist/runtime/host-load-ledger-lock.d.ts +69 -0
- package/dist/runtime/host-load-ledger-lock.js +187 -0
- package/dist/runtime/host-load-live-test-slots.d.ts +61 -0
- package/dist/runtime/host-load-live-test-slots.js +193 -0
- package/dist/runtime/host-load-runtime-settings.d.ts +25 -0
- package/dist/runtime/host-load-runtime-settings.js +80 -0
- package/dist/runtime/host-load-sampler.d.ts +92 -2
- package/dist/runtime/host-load-sampler.js +166 -8
- 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 +107 -0
- package/dist/runtime/host-load-step-run.js +158 -0
- package/dist/runtime/host-load-test-slot.d.ts +46 -0
- package/dist/runtime/host-load-test-slot.js +100 -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 +21 -1
- package/dist/runtime/host-pressure-stall.js +48 -1
- package/dist/runtime/host-runtime-contract.d.ts +15 -0
- package/dist/runtime/host-runtime-contract.js +39 -6
- package/dist/runtime/host-runtime-lease.d.ts +101 -6
- package/dist/runtime/host-runtime-lease.js +408 -294
- package/dist/runtime/host-runtime-ledger-recovery.d.ts +20 -0
- package/dist/runtime/host-runtime-ledger-recovery.js +134 -0
- package/dist/runtime/host-runtime-promotion.d.ts +101 -2
- package/dist/runtime/host-runtime-promotion.js +355 -14
- 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.js +5 -16
- package/dist/runtime/native-test-postgres-fallback.d.ts +51 -7
- package/dist/runtime/native-test-postgres-fallback.js +60 -8
- package/dist/runtime/playwright-host-runtime-adapter.d.ts +7 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +13 -1
- package/dist/runtime/prepared-runtime-identity.d.ts +4 -0
- package/dist/runtime/prepared-runtime-identity.js +130 -9
- package/dist/runtime/prepared-runtime-readiness.d.ts +23 -1
- package/dist/runtime/prepared-runtime-readiness.js +89 -18
- package/dist/runtime/runner-settings.d.ts +104 -0
- package/dist/runtime/runner-settings.js +236 -0
- package/dist/runtime/supervised-runtime-build-guard.d.ts +45 -0
- package/dist/runtime/supervised-runtime-build-guard.js +112 -0
- package/dist/sandbox/host-capacity-broker.d.ts +115 -0
- package/dist/sandbox/host-capacity-broker.js +336 -0
- package/dist/sandbox/managed-branch.d.ts +13 -0
- package/dist/sandbox/managed-branch.js +90 -0
- package/dist/sandbox/provider-auth/codex.js +11 -11
- package/dist/sandbox/runtime.d.ts +11 -3
- package/dist/sandbox/runtime.js +53 -20
- package/dist/sandbox/sandbox-cgroups.d.ts +14 -0
- package/dist/sandbox/sandbox-cgroups.js +42 -0
- package/dist/sandbox/sandbox-input.d.ts +22 -0
- package/dist/sandbox/sandbox-input.js +49 -0
- package/dist/sandbox/workspace.service.js +15 -2
- 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 +2 -7
- package/dist/session/managed-attempt-tools.js +204 -141
- package/dist/session/managed-build-coordinator.d.ts +16 -0
- package/dist/session/managed-build-coordinator.js +10 -0
- package/dist/session/managed-build-receipt.d.ts +7 -0
- package/dist/session/managed-build-receipt.js +11 -3
- package/dist/session/managed-command-environment.d.ts +8 -0
- package/dist/session/managed-command-environment.js +28 -0
- package/dist/session/managed-command-host-load.d.ts +52 -0
- package/dist/session/managed-command-host-load.js +87 -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.artifact-bundle.d.ts +20 -0
- package/dist/session/phase-completion.artifact-bundle.js +167 -0
- package/dist/session/phase-completion.artifacts.d.ts +9 -17
- package/dist/session/phase-completion.artifacts.js +158 -190
- 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 +31 -2
- package/dist/session/phase-completion.helpers.js +18 -65
- package/dist/session/phase-completion.js +19 -32
- package/dist/session/phase-completion.mcp-args.d.ts +9 -0
- package/dist/session/phase-completion.mcp-args.js +195 -0
- package/dist/session/phase-completion.phase-aliases.d.ts +5 -0
- package/dist/session/phase-completion.phase-aliases.js +43 -0
- package/dist/session/phase-completion.pr-review-schema.d.ts +1 -0
- package/dist/session/phase-completion.pr-review-schema.js +75 -0
- package/dist/session/phase-completion.pr-review-validation.d.ts +11 -0
- package/dist/session/phase-completion.pr-review-validation.js +212 -0
- package/dist/session/phase-completion.pr-review.d.ts +36 -0
- package/dist/session/phase-completion.pr-review.js +294 -0
- package/dist/session/phase-completion.prompts.d.ts +1 -1
- package/dist/session/phase-completion.prompts.js +20 -1
- package/dist/session/phase-completion.runtime.d.ts +13 -12
- package/dist/session/phase-completion.runtime.js +113 -214
- 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 +219 -232
- package/dist/session/phase-completion.validation.d.ts +3 -3
- package/dist/session/phase-completion.validation.js +58 -94
- package/dist/session/pr-review-diff-base.d.ts +18 -0
- package/dist/session/pr-review-diff-base.js +44 -0
- package/dist/session/pr-review-placement.d.ts +50 -0
- package/dist/session/pr-review-placement.js +91 -0
- package/dist/session/pre-publication-change-set.d.ts +62 -0
- package/dist/session/pre-publication-change-set.js +166 -0
- package/dist/session/pre-publication-host-load.d.ts +27 -0
- package/dist/session/pre-publication-host-load.js +112 -0
- package/dist/session/pre-publication-native-postgres.d.ts +30 -0
- package/dist/session/pre-publication-native-postgres.js +111 -0
- package/dist/session/pre-publication-pass.d.ts +61 -0
- package/dist/session/pre-publication-pass.js +148 -0
- package/dist/session/pre-publication-process.d.ts +3 -0
- package/dist/session/pre-publication-process.js +32 -19
- package/dist/session/pre-publication-step-execution.d.ts +12 -0
- package/dist/session/pre-publication-step-execution.js +50 -0
- package/dist/session/pre-publication-validation.d.ts +39 -22
- package/dist/session/pre-publication-validation.js +241 -207
- package/dist/session/provider-cli/claude-background-work.d.ts +67 -0
- package/dist/session/provider-cli/claude-background-work.js +181 -0
- package/dist/session/provider-cli/dispatch-target-branch.d.ts +7 -0
- package/dist/session/provider-cli/dispatch-target-branch.js +8 -0
- package/dist/session/provider-cli/host-build-lock.d.ts +18 -0
- package/dist/session/provider-cli/host-build-lock.js +30 -0
- package/dist/session/provider-cli/pr-review-credential-isolation.d.ts +83 -0
- package/dist/session/provider-cli/pr-review-credential-isolation.js +144 -0
- package/dist/session/provider-cli/pr-review-guardrails.d.ts +70 -0
- package/dist/session/provider-cli/pr-review-guardrails.js +360 -0
- package/dist/session/provider-cli/pr-review-prompt.d.ts +2 -0
- package/dist/session/provider-cli/pr-review-prompt.js +119 -0
- package/dist/session/provider-cli/pre-publication-repair.d.ts +56 -0
- package/dist/session/provider-cli/pre-publication-repair.js +281 -0
- package/dist/session/provider-cli/provider-cli.base.d.ts +1 -0
- package/dist/session/provider-cli/provider-cli.child-environment.d.ts +7 -0
- package/dist/session/provider-cli/provider-cli.child-environment.js +75 -0
- package/dist/session/provider-cli/provider-cli.codex-models-cache.d.ts +3 -0
- package/dist/session/provider-cli/provider-cli.codex-models-cache.js +37 -0
- package/dist/session/provider-cli/provider-cli.deterministic.d.ts +1 -0
- package/dist/session/provider-cli/provider-cli.deterministic.js +5 -0
- package/dist/session/provider-cli/provider-cli.merge-integrity.d.ts +6 -3
- package/dist/session/provider-cli/provider-cli.merge-integrity.js +29 -10
- package/dist/session/provider-cli/provider-cli.pr-review.d.ts +23 -0
- package/dist/session/provider-cli/provider-cli.pr-review.js +213 -0
- package/dist/session/provider-cli/provider-cli.process.d.ts +15 -36
- package/dist/session/provider-cli/provider-cli.process.js +41 -432
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +15 -3
- package/dist/session/provider-cli/provider-cli.prompts.js +183 -90
- package/dist/session/provider-cli/provider-cli.reapply-conflicts.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.reapply-conflicts.js +37 -0
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +15 -0
- package/dist/session/provider-cli/provider-cli.runtime.js +43 -1
- package/dist/session/provider-cli/provider-cli.service-support.d.ts +37 -0
- package/dist/session/provider-cli/provider-cli.service-support.js +146 -0
- package/dist/session/provider-cli/provider-cli.service.d.ts +18 -18
- package/dist/session/provider-cli/provider-cli.service.js +127 -317
- package/dist/session/provider-cli/provider-cli.types.d.ts +35 -0
- package/dist/session/provider-cli/provider-cli.windows-environment.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.windows-environment.js +151 -0
- package/dist/session/provider-cli/provider-cli.windows-launch.d.ts +23 -0
- package/dist/session/provider-cli/provider-cli.windows-launch.js +131 -0
- package/dist/session/provider-cli/provider-in-flight-work.d.ts +4 -0
- package/dist/session/provider-cli/provider-in-flight-work.js +45 -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 +2 -0
- package/dist/session/provider-cli/provider-output-state.js +6 -0
- package/dist/session/provider-cli/provider-user-config-isolation.d.ts +69 -0
- package/dist/session/provider-cli/provider-user-config-isolation.js +164 -0
- package/dist/session/qa-sidecar-validation.d.ts +6 -0
- package/dist/session/qa-sidecar-validation.js +28 -0
- package/dist/session/sandbox-host-capacity-gate.d.ts +51 -0
- package/dist/session/sandbox-host-capacity-gate.js +279 -0
- package/dist/session/session-host-project.d.ts +22 -0
- package/dist/session/session-host-project.js +98 -0
- package/dist/session/session-redaction.js +31 -181
- package/dist/session/session.base.d.ts +9 -1
- package/dist/session/session.parse-helpers.d.ts +10 -0
- package/dist/session/session.parse-helpers.js +84 -0
- package/dist/session/session.runtime-refresh.d.ts +46 -0
- package/dist/session/session.runtime-refresh.js +116 -0
- package/dist/session/session.service.d.ts +16 -2
- package/dist/session/session.service.js +208 -155
- package/dist/session/session.types.d.ts +8 -0
- package/dist/session/session.types.js +13 -83
- package/dist/session/session.types.pr-review.d.ts +48 -0
- package/dist/session/session.types.pr-review.js +51 -0
- package/dist/session/sidecar-json-shape.d.ts +31 -0
- package/dist/session/sidecar-json-shape.js +217 -0
- package/dist/session/workspace-runtime-fingerprint.d.ts +32 -0
- package/dist/session/workspace-runtime-fingerprint.js +138 -0
- package/dist/shared/fs/file-identity.d.ts +10 -0
- package/dist/shared/fs/file-identity.js +15 -0
- package/dist/shared/fs/replace-file.d.ts +7 -0
- package/dist/shared/fs/replace-file.js +33 -0
- package/dist/shared/process/host-process-recovery.d.ts +14 -3
- package/dist/shared/process/host-process-recovery.js +23 -3
- package/dist/shared/process/host-process-supervisor.d.ts +14 -1
- package/dist/shared/process/host-process-supervisor.js +67 -15
- package/dist/shared/process/process-tree.d.ts +52 -0
- package/dist/shared/process/process-tree.js +144 -0
- package/dist/shared/process/process.d.ts +67 -0
- package/dist/shared/process/process.js +213 -11
- package/dist/shared/process/windows-app-execution-alias.d.ts +19 -0
- package/dist/shared/process/windows-app-execution-alias.js +45 -0
- package/dist/shared/process/windows-launch.d.ts +18 -0
- package/dist/shared/process/windows-launch.js +68 -0
- package/dist/shared/provider-capabilities.d.ts +2 -0
- package/dist/shared/provider-capabilities.js +23 -7
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/artifact-injector.js +24 -0
- package/dist/worktree/dependency-warmer.d.ts +25 -0
- package/dist/worktree/dependency-warmer.js +46 -10
- package/dist/worktree/orphan-relink.d.ts +19 -0
- package/dist/worktree/orphan-relink.js +106 -0
- package/dist/worktree/wip-git.d.ts +58 -0
- package/dist/worktree/wip-git.js +182 -0
- package/dist/worktree/wip-refs.d.ts +85 -0
- package/dist/worktree/wip-refs.js +169 -0
- package/dist/worktree/wip-restore.d.ts +51 -0
- package/dist/worktree/wip-restore.js +324 -0
- package/dist/worktree/wip-snapshot.d.ts +53 -0
- package/dist/worktree/wip-snapshot.js +270 -0
- package/dist/worktree/worktree-inputs.d.ts +8 -0
- package/dist/worktree/worktree-inputs.js +134 -0
- package/dist/worktree/worktree-origin-sync.d.ts +35 -0
- package/dist/worktree/worktree-origin-sync.js +128 -0
- package/dist/worktree/worktree-wip-events.d.ts +20 -0
- package/dist/worktree/worktree-wip-events.js +148 -0
- package/dist/worktree/worktree-wip.d.ts +57 -0
- package/dist/worktree/worktree-wip.js +200 -0
- package/dist/worktree/worktree.base.d.ts +7 -1
- package/dist/worktree/worktree.service.d.ts +58 -5
- package/dist/worktree/worktree.service.js +237 -161
- package/dist/worktree/worktree.types.d.ts +54 -0
- package/package.json +2 -2
- package/runtime/windows-listener-owner.ps1 +12 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { captureHostProcessIdentity } from "../shared/process/host-process-supervisor.js";
|
|
2
|
+
import { systemWindowsProcessTable, WindowsProcessTree } from "../shared/process/process-tree.js";
|
|
3
|
+
import { monotonicNowMs } from "../shared/time/monotonic.js";
|
|
4
|
+
const graceMs = 250;
|
|
5
|
+
/**
|
|
6
|
+
* Stops the recorded tree with SIGTERM, then SIGKILL, and confirms it is gone.
|
|
7
|
+
* A root that cannot be identified (no birth marker) is never signalled and
|
|
8
|
+
* counts as not confirmed, so its token keeps its reserved cost.
|
|
9
|
+
*/
|
|
10
|
+
export const terminateHostLoadStepProcess = async (step) => process.platform === "win32"
|
|
11
|
+
? await windowsStepTerminator(systemWindowsProcessTable)(step)
|
|
12
|
+
: await terminateUnixStep(step);
|
|
13
|
+
/**
|
|
14
|
+
* Windows has no process groups and does not reparent orphans. A root with a
|
|
15
|
+
* recorded birth marker is stopped together with every descendant created
|
|
16
|
+
* after it (found through parent ids, even if the root already exited), and
|
|
17
|
+
* the reservation is reused only once none is left. Without a birth marker the
|
|
18
|
+
* root cannot be told from a reused pid, so it is never signalled and only its
|
|
19
|
+
* absence counts.
|
|
20
|
+
*/
|
|
21
|
+
export function windowsStepTerminator(table, waitMs = 10_000) {
|
|
22
|
+
return async (step) => {
|
|
23
|
+
const createdAt = step.rootIdentity?.startsWith("win32:") ? step.rootIdentity.slice("win32:".length) : null;
|
|
24
|
+
if (createdAt === null)
|
|
25
|
+
return !pidExists(step.rootPid);
|
|
26
|
+
return await new WindowsProcessTree(step.rootPid, table, { exactCreatedAt: createdAt }).stop(Date.now() + waitMs);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async function terminateUnixStep(step) {
|
|
30
|
+
if (!memberExists(step))
|
|
31
|
+
return true;
|
|
32
|
+
const unixGroup = step.processGroup && process.platform !== "win32";
|
|
33
|
+
if (!pidExists(step.rootPid)) {
|
|
34
|
+
// The root exited but its group lives on. A process group id is never reissued
|
|
35
|
+
// while the group has members, so these are the step's own orphans: stop them.
|
|
36
|
+
if (!unixGroup || !step.rootIdentity)
|
|
37
|
+
return !memberExists(step);
|
|
38
|
+
return await stopTree(step);
|
|
39
|
+
}
|
|
40
|
+
const identity = captureHostProcessIdentity(step.rootPid);
|
|
41
|
+
// The root pid now names another process, so the step's root and its group are gone.
|
|
42
|
+
if (step.rootIdentity && identity && identity !== step.rootIdentity)
|
|
43
|
+
return true;
|
|
44
|
+
if (!step.rootIdentity || !identity)
|
|
45
|
+
return !memberExists(step);
|
|
46
|
+
return await stopTree(step);
|
|
47
|
+
}
|
|
48
|
+
async function stopTree(step) {
|
|
49
|
+
for (const signal of ["SIGTERM", "SIGKILL"]) {
|
|
50
|
+
send(step, signal);
|
|
51
|
+
const deadline = monotonicNowMs() + graceMs;
|
|
52
|
+
while (memberExists(step) && monotonicNowMs() < deadline)
|
|
53
|
+
await delay(10);
|
|
54
|
+
if (!memberExists(step))
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
function memberExists(step) {
|
|
60
|
+
return step.processGroup && process.platform !== "win32" ? groupExists(step.rootPid) : pidExists(step.rootPid);
|
|
61
|
+
}
|
|
62
|
+
function send(step, signal) {
|
|
63
|
+
try {
|
|
64
|
+
process.kill(step.processGroup && process.platform !== "win32" ? -step.rootPid : step.rootPid, signal);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error.code !== "ESRCH")
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function pidExists(pid) {
|
|
72
|
+
return signalZero(pid);
|
|
73
|
+
}
|
|
74
|
+
function groupExists(pid) {
|
|
75
|
+
return signalZero(-pid);
|
|
76
|
+
}
|
|
77
|
+
function signalZero(target) {
|
|
78
|
+
try {
|
|
79
|
+
process.kill(target, 0);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return error.code === "EPERM";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async function delay(ms) {
|
|
87
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
88
|
+
}
|
|
89
|
+
export function validHostLoadStepProcess(value) {
|
|
90
|
+
const step = value;
|
|
91
|
+
return Boolean(step)
|
|
92
|
+
&& Number.isSafeInteger(step.rootPid) && step.rootPid > 1
|
|
93
|
+
&& (step.rootIdentity === null || typeof step.rootIdentity === "string")
|
|
94
|
+
&& typeof step.processGroup === "boolean";
|
|
95
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { HostLoadAdmissionDecision } from "./host-load-admission.js";
|
|
2
|
+
import { type HostLoadAdmission, type HostLoadStepGate } from "./host-load-governor.js";
|
|
3
|
+
import type { HostLoadStepProcess } from "./host-load-step-reaper.js";
|
|
4
|
+
import type { HostLoadStepClassification } from "./host-load-step-class.js";
|
|
5
|
+
import type { HostLoadTokenPriority } from "./host-load-token-ledger.js";
|
|
6
|
+
export interface HostGovernedStep {
|
|
7
|
+
readonly classification: HostLoadStepClassification;
|
|
8
|
+
/** What the step is, for diagnostics. */
|
|
9
|
+
readonly label: string;
|
|
10
|
+
readonly dispatchAttemptId: string | null;
|
|
11
|
+
readonly priority: HostLoadTokenPriority;
|
|
12
|
+
readonly signal?: AbortSignal;
|
|
13
|
+
/** Called when the step first has to wait for capacity, and again whenever that wait changes. */
|
|
14
|
+
readonly onWaiting?: (decision: HostLoadAdmissionDecision, fence: WaitReportFence) => void | Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* The step records its own fenced outcome (a managed command's receipt). Otherwise a
|
|
17
|
+
* step whose token was reclaimed while it ran fails with host_capacity_token_fenced.
|
|
18
|
+
*/
|
|
19
|
+
readonly reportsOwnFence?: boolean;
|
|
20
|
+
/** Called when the token could not be returned; it then lapses with its lease. */
|
|
21
|
+
readonly onReleaseFailed?: (error: unknown) => void | Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/** Queue time, or null when the governor is off and nothing was queued. */
|
|
24
|
+
export type HostLoadWait = number | null;
|
|
25
|
+
/** What an admitted step gets from its token; both absent while the governor is off. */
|
|
26
|
+
export interface HostLoadStepControl {
|
|
27
|
+
/** Aborted when the token was reclaimed: the step must stop. */
|
|
28
|
+
readonly fenced?: AbortSignal;
|
|
29
|
+
/** Records the step's process tree so a reclaim stops it before reusing its capacity. */
|
|
30
|
+
readonly attachProcess?: (stepProcess: HostLoadStepProcess) => Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* For a step that reports its own outcome (a managed command's receipt) when its
|
|
33
|
+
* process tree could not be proven gone: the token is kept, not returned, until
|
|
34
|
+
* `stillGone` proves it (null: see HostLoadAdmission.retainUntilStopped).
|
|
35
|
+
*/
|
|
36
|
+
readonly holdUntilStopped?: (stillGone: (() => Promise<boolean>) | null, reason: Error) => void;
|
|
37
|
+
}
|
|
38
|
+
/** Why a step's token is kept after it ended, and what proves its tree gone. */
|
|
39
|
+
export interface HostLoadRetention {
|
|
40
|
+
readonly stillGone: (() => Promise<boolean>) | null;
|
|
41
|
+
readonly reason: Error;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The retention a step's failure calls for: a tree that could not be proven gone,
|
|
45
|
+
* whether the runner said so with its own check (ProcessTreeTerminationError) or a
|
|
46
|
+
* supervisor did (host_process_supervisor_termination_failed, possibly wrapped).
|
|
47
|
+
*/
|
|
48
|
+
export declare function retentionFor(error: unknown): HostLoadRetention | null;
|
|
49
|
+
/**
|
|
50
|
+
* With the governor off (or no gate), `run` is called directly with a null
|
|
51
|
+
* wait, so behavior is unchanged. With it on, `run` starts only after
|
|
52
|
+
* admission, so the step's own timers never include queue time, and the token
|
|
53
|
+
* is returned when `run` settles, whether it succeeded, failed or was stopped.
|
|
54
|
+
*/
|
|
55
|
+
export declare function runHostGovernedStep<T>(gate: HostLoadStepGate | undefined, step: HostGovernedStep, run: (hostLoadWaitMs: HostLoadWait, control: HostLoadStepControl) => Promise<T>): Promise<T>;
|
|
56
|
+
/** A step's wait reports, run in order; see orderedWaitReports. */
|
|
57
|
+
export interface OrderedWaitReports {
|
|
58
|
+
readonly onWaiting: ((decision: HostLoadAdmissionDecision) => void) | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Resolves once every report made so far has landed, or after the governor's drain bound
|
|
61
|
+
* (afterNotified) at the latest. Either way the reports are then closed: one still queued, or
|
|
62
|
+
* made later, is dropped as stale and never runs, and one still running can no longer commit
|
|
63
|
+
* (WaitReportFence). A commit already under way is awaited, so it lands before what follows.
|
|
64
|
+
*/
|
|
65
|
+
drained(): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Handed to each wait report. The bound in `drained` stops waiting for a report, not the report
|
|
69
|
+
* itself, so a slow write that finally succeeds would land after the step's own state. A report
|
|
70
|
+
* that replaces durable state (a receipt) makes that last replace through `commit`: it then runs
|
|
71
|
+
* only while the reports are open, and never after the step's state is written.
|
|
72
|
+
*/
|
|
73
|
+
export interface WaitReportFence {
|
|
74
|
+
/** False once the reports are closed. */
|
|
75
|
+
readonly current: boolean;
|
|
76
|
+
/** Runs `replace` only while the reports are open; false when it was skipped as stale. */
|
|
77
|
+
commit(replace: () => Promise<void>): Promise<boolean>;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Runs a step's wait reports one at a time, in the order they were made, and hands the governor a
|
|
81
|
+
* callback that returns at once. The governor waits only a few seconds for a report (afterNotified),
|
|
82
|
+
* so a report it waited on could still land after the step's admitted or failed state. The step
|
|
83
|
+
* awaits `drained` before writing that state instead, so a late "waiting" can never replace it.
|
|
84
|
+
* `drained` keeps the governor's bound, so a report that never settles (a stuck checkpoint or
|
|
85
|
+
* event) cannot hold the step's admission, or its failure, back for longer than that. A report
|
|
86
|
+
* still running after the bound can no longer replace a receipt (WaitReportFence.commit); an
|
|
87
|
+
* appended event it already sent stays in the log as history of the wait, which it was.
|
|
88
|
+
*/
|
|
89
|
+
export declare function orderedWaitReports(report: ((decision: HostLoadAdmissionDecision, fence: WaitReportFence) => void | Promise<void>) | undefined): OrderedWaitReports;
|
|
90
|
+
/**
|
|
91
|
+
* Returns a step's token, unless part of its process tree could not be proven
|
|
92
|
+
* gone: the token (and its cost) is then kept until the tree is.
|
|
93
|
+
*/
|
|
94
|
+
export declare function returnHostLoadToken(admission: HostLoadAdmission, retention: HostLoadRetention | null, onReleaseFailed?: (error: unknown) => void | Promise<void>): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* A step stopped because its token was reclaimed reports host_capacity_token_fenced
|
|
97
|
+
* (retryable), not the exit status or error it died with.
|
|
98
|
+
*/
|
|
99
|
+
export declare function throwIfFenced<T>(fenced: AbortSignal, outcome: T): T;
|
|
100
|
+
/**
|
|
101
|
+
* Records a supervised step's process tree on its host load token, from the
|
|
102
|
+
* metadata its process supervisor wrote (`FORGE_PROCESS_SUPERVISOR_METADATA_PATH`):
|
|
103
|
+
* a reclaim then stops exactly this tree before reusing its capacity. Rejects when
|
|
104
|
+
* the tree cannot be read or recorded; the caller must then stop the step, since
|
|
105
|
+
* it would otherwise run on without a reservation the host can account for.
|
|
106
|
+
*/
|
|
107
|
+
export declare function attachSupervisedStepProcess(control: HostLoadStepControl, metadataPath: string): Promise<void>;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { isProcessTreeTerminationUnproven, ProcessTreeTerminationError } from "../shared/process/process.js";
|
|
3
|
+
import { afterNotified } from "./host-load-governor.js";
|
|
4
|
+
/**
|
|
5
|
+
* The retention a step's failure calls for: a tree that could not be proven gone,
|
|
6
|
+
* whether the runner said so with its own check (ProcessTreeTerminationError) or a
|
|
7
|
+
* supervisor did (host_process_supervisor_termination_failed, possibly wrapped).
|
|
8
|
+
*/
|
|
9
|
+
export function retentionFor(error) {
|
|
10
|
+
if (error instanceof ProcessTreeTerminationError)
|
|
11
|
+
return { stillGone: error.stillGone, reason: error };
|
|
12
|
+
if (!isProcessTreeTerminationUnproven(error))
|
|
13
|
+
return null;
|
|
14
|
+
return { stillGone: null, reason: error instanceof Error ? error : new Error(String(error)) };
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* With the governor off (or no gate), `run` is called directly with a null
|
|
18
|
+
* wait, so behavior is unchanged. With it on, `run` starts only after
|
|
19
|
+
* admission, so the step's own timers never include queue time, and the token
|
|
20
|
+
* is returned when `run` settles, whether it succeeded, failed or was stopped.
|
|
21
|
+
*/
|
|
22
|
+
export async function runHostGovernedStep(gate, step, run) {
|
|
23
|
+
if (!gate?.enabled)
|
|
24
|
+
return await run(null, {});
|
|
25
|
+
const reports = orderedWaitReports(step.onWaiting);
|
|
26
|
+
let admission;
|
|
27
|
+
try {
|
|
28
|
+
admission = await gate.acquire({
|
|
29
|
+
stepClass: step.classification.stepClass,
|
|
30
|
+
label: step.label,
|
|
31
|
+
cost: step.classification.cost,
|
|
32
|
+
dispatchAttemptId: step.dispatchAttemptId,
|
|
33
|
+
priority: step.priority,
|
|
34
|
+
}, { signal: step.signal, onWaiting: reports.onWaiting });
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
// Every wait report lands before the step's own outcome (its launch, or why it was never admitted).
|
|
38
|
+
await reports.drained();
|
|
39
|
+
}
|
|
40
|
+
let retention = null;
|
|
41
|
+
try {
|
|
42
|
+
let outcome;
|
|
43
|
+
try {
|
|
44
|
+
outcome = await run(Math.round(admission.waitMs), {
|
|
45
|
+
fenced: admission.fenced,
|
|
46
|
+
attachProcess: async (stepProcess) => await admission.attachProcess(stepProcess),
|
|
47
|
+
holdUntilStopped: (stillGone, reason) => { retention = { stillGone, reason }; },
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
// Part of the step may still run: that, not a fence, is what the step failed with.
|
|
52
|
+
const unproven = retentionFor(error);
|
|
53
|
+
if (unproven) {
|
|
54
|
+
retention = unproven;
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
if (step.reportsOwnFence !== true)
|
|
58
|
+
throwIfFenced(admission.fenced, undefined);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
return step.reportsOwnFence === true ? outcome : throwIfFenced(admission.fenced, outcome);
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
await returnHostLoadToken(admission, retention, step.onReleaseFailed);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Runs a step's wait reports one at a time, in the order they were made, and hands the governor a
|
|
69
|
+
* callback that returns at once. The governor waits only a few seconds for a report (afterNotified),
|
|
70
|
+
* so a report it waited on could still land after the step's admitted or failed state. The step
|
|
71
|
+
* awaits `drained` before writing that state instead, so a late "waiting" can never replace it.
|
|
72
|
+
* `drained` keeps the governor's bound, so a report that never settles (a stuck checkpoint or
|
|
73
|
+
* event) cannot hold the step's admission, or its failure, back for longer than that. A report
|
|
74
|
+
* still running after the bound can no longer replace a receipt (WaitReportFence.commit); an
|
|
75
|
+
* appended event it already sent stays in the log as history of the wait, which it was.
|
|
76
|
+
*/
|
|
77
|
+
export function orderedWaitReports(report) {
|
|
78
|
+
let chain = Promise.resolve();
|
|
79
|
+
let closed = false;
|
|
80
|
+
let committing = Promise.resolve();
|
|
81
|
+
const fence = {
|
|
82
|
+
get current() { return !closed; },
|
|
83
|
+
commit: async (replace) => {
|
|
84
|
+
if (closed)
|
|
85
|
+
return false;
|
|
86
|
+
// Started while open: `drained` waits for it, so it cannot land after the step's state.
|
|
87
|
+
const replaced = replace();
|
|
88
|
+
committing = replaced.catch(() => undefined);
|
|
89
|
+
await replaced;
|
|
90
|
+
return true;
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
onWaiting: report ? decision => {
|
|
95
|
+
if (closed)
|
|
96
|
+
return;
|
|
97
|
+
chain = chain.then(async () => { if (!closed)
|
|
98
|
+
await report(decision, fence); }).catch(() => undefined);
|
|
99
|
+
} : undefined,
|
|
100
|
+
drained: async () => {
|
|
101
|
+
// Once closed nothing new runs or commits: a report still in flight was already given its bound.
|
|
102
|
+
if (closed)
|
|
103
|
+
return;
|
|
104
|
+
try {
|
|
105
|
+
await afterNotified(chain);
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
closed = true;
|
|
109
|
+
}
|
|
110
|
+
await committing;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Returns a step's token, unless part of its process tree could not be proven
|
|
116
|
+
* gone: the token (and its cost) is then kept until the tree is.
|
|
117
|
+
*/
|
|
118
|
+
export async function returnHostLoadToken(admission, retention, onReleaseFailed) {
|
|
119
|
+
if (retention) {
|
|
120
|
+
admission.retainUntilStopped(retention.stillGone, retention.reason);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
await admission.release();
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
await Promise.resolve(onReleaseFailed?.(error)).catch(() => undefined);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A step stopped because its token was reclaimed reports host_capacity_token_fenced
|
|
132
|
+
* (retryable), not the exit status or error it died with.
|
|
133
|
+
*/
|
|
134
|
+
export function throwIfFenced(fenced, outcome) {
|
|
135
|
+
if (fenced.aborted)
|
|
136
|
+
throw fenced.reason;
|
|
137
|
+
return outcome;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Records a supervised step's process tree on its host load token, from the
|
|
141
|
+
* metadata its process supervisor wrote (`FORGE_PROCESS_SUPERVISOR_METADATA_PATH`):
|
|
142
|
+
* a reclaim then stops exactly this tree before reusing its capacity. Rejects when
|
|
143
|
+
* the tree cannot be read or recorded; the caller must then stop the step, since
|
|
144
|
+
* it would otherwise run on without a reservation the host can account for.
|
|
145
|
+
*/
|
|
146
|
+
export async function attachSupervisedStepProcess(control, metadataPath) {
|
|
147
|
+
if (!control.attachProcess)
|
|
148
|
+
return;
|
|
149
|
+
const metadata = JSON.parse(await fs.readFile(metadataPath, "utf8"));
|
|
150
|
+
if (typeof metadata.rootPid !== "number" || !Number.isSafeInteger(metadata.rootPid) || metadata.rootPid <= 1) {
|
|
151
|
+
throw new Error(`the step's process supervisor recorded no usable root process in ${metadataPath}.`);
|
|
152
|
+
}
|
|
153
|
+
await control.attachProcess({
|
|
154
|
+
rootPid: metadata.rootPid,
|
|
155
|
+
rootIdentity: typeof metadata.rootIdentity === "string" ? metadata.rootIdentity : null,
|
|
156
|
+
processGroup: metadata.strategy === "unix_process_group",
|
|
157
|
+
});
|
|
158
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { HostLoadAdmissionDecision } from "./host-load-admission.js";
|
|
2
|
+
import type { HostLoadToken, HostLoadTokenOwner, HostLoadTokenPriority } from "./host-load-token-ledger.js";
|
|
3
|
+
/** Every governed step class takes the slot unless it is known to be light. */
|
|
4
|
+
export declare function takesHostTestSlot(stepClass: string): boolean;
|
|
5
|
+
/** A step waiting for the slot, as its owner records it in the ledger. */
|
|
6
|
+
export interface HostTestSlotTicketRequest {
|
|
7
|
+
readonly ticketId: string;
|
|
8
|
+
readonly stepClass: string;
|
|
9
|
+
readonly label: string;
|
|
10
|
+
readonly dispatchAttemptId: string | null;
|
|
11
|
+
readonly priority: HostLoadTokenPriority;
|
|
12
|
+
/** Wall clock (ISO): every process on the host orders by the same clock. */
|
|
13
|
+
readonly enqueuedAt: string;
|
|
14
|
+
}
|
|
15
|
+
/** A ticket in the ledger: its owner renews `expiresAt` on every transaction while it waits. */
|
|
16
|
+
export interface HostTestSlotTicket extends HostTestSlotTicketRequest {
|
|
17
|
+
readonly owner: HostLoadTokenOwner;
|
|
18
|
+
readonly expiresAt: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The host queue in admission order: foreground first, except background that has
|
|
22
|
+
* waited past the anti-starvation window, then oldest first.
|
|
23
|
+
*/
|
|
24
|
+
export declare function orderHostTestSlotQueue(tickets: readonly HostTestSlotTicketRequest[], nowMs: number, antiStarvationMs: number): HostTestSlotTicketRequest[];
|
|
25
|
+
export interface HostTestSlotInput {
|
|
26
|
+
readonly ticket: HostTestSlotTicketRequest;
|
|
27
|
+
/** Every live ticket on the host, this one included or not. */
|
|
28
|
+
readonly queue: readonly HostTestSlotTicketRequest[];
|
|
29
|
+
/** Every live token on the host; fenced and degraded ones still hold their slot. */
|
|
30
|
+
readonly tokens: readonly HostLoadToken[];
|
|
31
|
+
/** The count in force: the configured count, or lower while other work needs the host. */
|
|
32
|
+
readonly testSlots: number;
|
|
33
|
+
/** Why testSlots is below the configured count (host-load-capacity.ts); empty or absent when it is not. */
|
|
34
|
+
readonly testSlotsDetail?: string;
|
|
35
|
+
readonly nowMs: number;
|
|
36
|
+
readonly antiStarvationMs: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Admitted once the slots the holders leave free reach this ticket's place in the
|
|
40
|
+
* host queue. A token kept until its attempt's teardown (`retainedAt`) holds the slot
|
|
41
|
+
* against every other attempt, but not against its own: that attempt's next step
|
|
42
|
+
* would otherwise wait for its own teardown.
|
|
43
|
+
*/
|
|
44
|
+
export declare function decideHostTestSlot(input: HostTestSlotInput): HostLoadAdmissionDecision;
|
|
45
|
+
/** A ticket read back from the ledger; a malformed one is dropped, never trusted. */
|
|
46
|
+
export declare function validHostTestSlotTicket(value: unknown): value is HostTestSlotTicket;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// The host test slot (decision 0018): two Forge sessions on one host share it, so
|
|
2
|
+
// their builds, test runs, Playwright runs and runtime builds take turns instead of
|
|
3
|
+
// competing for the CPU and memory. Waiters queue host-wide in the shared ledger,
|
|
4
|
+
// foreground before background and first come, first served within a class, so a
|
|
5
|
+
// step in another process (the completion MCP server) is never overtaken by the
|
|
6
|
+
// bridge's own steps. A slot holder that crashed keeps its slot only until the
|
|
7
|
+
// ledger reclaims its token (dead owner, process tree stopped, or lease fenced).
|
|
8
|
+
/** Steps that never take the slot: plain managed commands (git, ls, cat) and dependency installs. */
|
|
9
|
+
const slotFreeStepClasses = new Set(["managed.command", "deps.install"]);
|
|
10
|
+
/** Every governed step class takes the slot unless it is known to be light. */
|
|
11
|
+
export function takesHostTestSlot(stepClass) {
|
|
12
|
+
return !slotFreeStepClasses.has(stepClass);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The host queue in admission order: foreground first, except background that has
|
|
16
|
+
* waited past the anti-starvation window, then oldest first.
|
|
17
|
+
*/
|
|
18
|
+
export function orderHostTestSlotQueue(tickets, nowMs, antiStarvationMs) {
|
|
19
|
+
const rank = (ticket) => ticket.priority === "foreground" || nowMs - Date.parse(ticket.enqueuedAt) >= antiStarvationMs ? 0 : 1;
|
|
20
|
+
return [...tickets].sort((a, b) => rank(a) - rank(b)
|
|
21
|
+
|| Date.parse(a.enqueuedAt) - Date.parse(b.enqueuedAt)
|
|
22
|
+
|| a.ticketId.localeCompare(b.ticketId));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Admitted once the slots the holders leave free reach this ticket's place in the
|
|
26
|
+
* host queue. A token kept until its attempt's teardown (`retainedAt`) holds the slot
|
|
27
|
+
* against every other attempt, but not against its own: that attempt's next step
|
|
28
|
+
* would otherwise wait for its own teardown.
|
|
29
|
+
*/
|
|
30
|
+
export function decideHostTestSlot(input) {
|
|
31
|
+
const ownRetained = (token) => Boolean(token.retainedAt)
|
|
32
|
+
&& token.dispatchAttemptId !== null && token.dispatchAttemptId === input.ticket.dispatchAttemptId;
|
|
33
|
+
const holders = input.tokens.filter(token => takesHostTestSlot(token.stepClass) && !ownRetained(token));
|
|
34
|
+
const queue = input.queue.some(entry => entry.ticketId === input.ticket.ticketId)
|
|
35
|
+
? input.queue
|
|
36
|
+
: [...input.queue, input.ticket];
|
|
37
|
+
const ahead = orderHostTestSlotQueue(queue, input.nowMs, input.antiStarvationMs)
|
|
38
|
+
.findIndex(entry => entry.ticketId === input.ticket.ticketId);
|
|
39
|
+
const free = Math.max(0, input.testSlots - holders.length);
|
|
40
|
+
const admitted = ahead < free;
|
|
41
|
+
const reserved = {
|
|
42
|
+
reservedCpuCores: input.tokens.reduce((sum, token) => sum + token.cost.cpuCores, 0),
|
|
43
|
+
reservedMemoryBytes: input.tokens.reduce((sum, token) => sum + token.cost.memoryBytes, 0),
|
|
44
|
+
cpuBudgetCores: null,
|
|
45
|
+
memoryBudgetBytes: null,
|
|
46
|
+
};
|
|
47
|
+
if (admitted) {
|
|
48
|
+
return { ...reserved, admitted, reason: "admitted_test_slot", detail: "The host test slot is free." };
|
|
49
|
+
}
|
|
50
|
+
const heldBy = holders.length > 0
|
|
51
|
+
? `held by ${holders.map(token => describeHolder(token, input.ticket.dispatchAttemptId)).join("; ")}`
|
|
52
|
+
: "the steps ahead of this one take it first";
|
|
53
|
+
return {
|
|
54
|
+
...reserved,
|
|
55
|
+
admitted,
|
|
56
|
+
reason: "host_test_slot",
|
|
57
|
+
detail: `Waiting for the host test slot (${input.testSlots} at once${input.testSlotsDetail ? `, ${input.testSlotsDetail}` : ""}): ${heldBy}; ${ahead} step(s) ahead in the host queue.`,
|
|
58
|
+
// Only a running, healthy holder is progress. With no holder the steps ahead are waiting too (for
|
|
59
|
+
// the budget, say), and that wait must count as stalled, not reset the clock.
|
|
60
|
+
progressing: holders.length > 0 && holders.every(healthyHolder),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** A holder whose step is still running under a live, renewed lease; the ledger reclaims the rest. */
|
|
64
|
+
function healthyHolder(token) {
|
|
65
|
+
return !token.degradedAt && !token.fencedAt && !token.retainedAt;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Names the holder for the waiting step's agent: whose step it is and since when. A step
|
|
69
|
+
* label alone reads like the agent's own: an agent that saw "held by dotnet_api_component"
|
|
70
|
+
* took another session's live test run for its own finished one and gave up the wait.
|
|
71
|
+
*/
|
|
72
|
+
function describeHolder(token, waiterAttemptId) {
|
|
73
|
+
const attempt = token.dispatchAttemptId ? `, attempt ${token.dispatchAttemptId}` : "";
|
|
74
|
+
const whose = !token.dispatchAttemptId ? ""
|
|
75
|
+
: token.dispatchAttemptId === waiterAttemptId ? ", this session's own step" : ", another session's step";
|
|
76
|
+
const admittedAtMs = Date.parse(token.admittedAt);
|
|
77
|
+
const since = Number.isFinite(admittedAtMs)
|
|
78
|
+
? ` since ${new Date(admittedAtMs).toISOString().replace(/\.\d{3}Z$/u, "Z")}`
|
|
79
|
+
: "";
|
|
80
|
+
const state = token.degradedAt
|
|
81
|
+
? `, held${since}; its owner is gone, and it is returned once its processes are stopped`
|
|
82
|
+
: token.fencedAt ? `, held${since} and past its lease`
|
|
83
|
+
: token.retainedAt ? `, held${since}; its processes could not be proven stopped, and it is returned at attempt teardown`
|
|
84
|
+
: `, still running${since}`;
|
|
85
|
+
return `${token.label} (${token.stepClass}${attempt}${whose}${state})`;
|
|
86
|
+
}
|
|
87
|
+
/** A ticket read back from the ledger; a malformed one is dropped, never trusted. */
|
|
88
|
+
export function validHostTestSlotTicket(value) {
|
|
89
|
+
const ticket = value;
|
|
90
|
+
return Boolean(ticket)
|
|
91
|
+
&& typeof ticket.ticketId === "string"
|
|
92
|
+
&& typeof ticket.stepClass === "string"
|
|
93
|
+
&& typeof ticket.label === "string"
|
|
94
|
+
&& (ticket.dispatchAttemptId === null || typeof ticket.dispatchAttemptId === "string")
|
|
95
|
+
&& (ticket.priority === "foreground" || ticket.priority === "background")
|
|
96
|
+
&& Number.isFinite(Date.parse(String(ticket.enqueuedAt)))
|
|
97
|
+
&& Number.isFinite(Date.parse(String(ticket.expiresAt)))
|
|
98
|
+
&& Number.isSafeInteger(ticket.owner?.pid)
|
|
99
|
+
&& typeof ticket.owner?.processInstanceId === "string";
|
|
100
|
+
}
|