@loucompanion/forge-bridge 0.1.4-dev.fb770705136a → 0.2.1-dev.0f07274d438b
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,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Forge agents get only the context Forge gives them: Forge's prompts and `.forge/` artifacts,
|
|
3
|
+
* Forge's MCP tools, and the repository they work in. Nothing from the host user's own provider
|
|
4
|
+
* setup reaches an agent session on any runner: no user instruction files, settings, hooks,
|
|
5
|
+
* skills, plugins, rules, custom agents or commands, and no user, account or plugin MCP servers.
|
|
6
|
+
*
|
|
7
|
+
* Every agent launch path (session runs, repair and recovery turns, chat turns) builds its
|
|
8
|
+
* provider command from these helpers, and a provider whose user configuration Forge cannot
|
|
9
|
+
* shut out is refused rather than launched. `provider-user-config-isolation.test.ts` fails when a
|
|
10
|
+
* launch path stops applying them.
|
|
11
|
+
*/
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
import os from "node:os";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import { ForgeFailureError } from "../session.types.js";
|
|
16
|
+
import { claudeHeadlessToolArgs } from "./claude-background-work.js";
|
|
17
|
+
/** Providers whose user configuration the helpers below shut out. Any other provider is refused. */
|
|
18
|
+
export const userConfigIsolatedProviders = Object.freeze(["claude", "codex"]);
|
|
19
|
+
/**
|
|
20
|
+
* Variables that choose where a provider CLI finds the user's configuration: Claude Code's config
|
|
21
|
+
* directory, and the home directory under which Claude reads `.claude` and `.claude.json` (HOME;
|
|
22
|
+
* USERPROFILE on Windows, which HOMEDRIVE and HOMEPATH resolve to when it is unset). Only the
|
|
23
|
+
* bridge's own environment sets them (provider-cli.child-environment.ts); a project's runtime
|
|
24
|
+
* environment can't, or it could point the provider at another user's configuration.
|
|
25
|
+
*/
|
|
26
|
+
export const providerConfigLocationVariables = Object.freeze(["CLAUDE_CONFIG_DIR", "HOME", "USERPROFILE", "HOMEDRIVE", "HOMEPATH"]);
|
|
27
|
+
/** A strict MCP configuration with no servers, for Claude turns that get no Forge tools. */
|
|
28
|
+
export const emptyClaudeMcpConfig = JSON.stringify({ mcpServers: {} });
|
|
29
|
+
/**
|
|
30
|
+
* Claude Code launch arguments:
|
|
31
|
+
* - `--setting-sources project,local` drops the user settings source: `~/.claude/settings.json`
|
|
32
|
+
* (hooks, env, permissions, enabled and claude.ai-synced plugins), `~/.claude/CLAUDE.md`,
|
|
33
|
+
* user rules, skills, agents, commands and output styles.
|
|
34
|
+
* - `--mcp-config` plus `--strict-mcp-config` load only Forge's own MCP server (or none), never
|
|
35
|
+
* `~/.claude.json`, plugin or claude.ai connector servers.
|
|
36
|
+
*/
|
|
37
|
+
export function claudeUserConfigIsolationArgs(forgeMcpConfigPath) {
|
|
38
|
+
return [
|
|
39
|
+
"--setting-sources", "project,local",
|
|
40
|
+
"--mcp-config", forgeMcpConfigPath?.trim() || emptyClaudeMcpConfig,
|
|
41
|
+
"--strict-mcp-config",
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Claude Code settings, passed with `--settings`:
|
|
46
|
+
* - `claudeMdExcludes` drops the instruction files Claude Code reads from every directory above
|
|
47
|
+
* the working directory (the user's home and folders such as `~/Repos`). They belong to the
|
|
48
|
+
* host, not to the repository, and the project settings source would otherwise load them.
|
|
49
|
+
* - `autoMemoryEnabled: false`: without the user settings source auto memory falls back to its
|
|
50
|
+
* default, and its memory files accumulate outside the repository between sessions.
|
|
51
|
+
*/
|
|
52
|
+
export function claudeUserConfigIsolationSettings(workingDirectory, pathApi = path) {
|
|
53
|
+
return {
|
|
54
|
+
autoMemoryEnabled: false,
|
|
55
|
+
claudeMdExcludes: ancestorInstructionExcludes(workingDirectory, pathApi),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Every Claude Code agent launch: the isolation arguments, then one `--settings` value that carries
|
|
60
|
+
* the isolation settings and, for headless agent turns, the background-work restrictions with
|
|
61
|
+
* their tool deny list (`claude-background-work.ts`).
|
|
62
|
+
*/
|
|
63
|
+
export function claudeIsolatedLaunchArgs(options) {
|
|
64
|
+
const settings = claudeUserConfigIsolationSettings(options.workingDirectory);
|
|
65
|
+
return [
|
|
66
|
+
...claudeUserConfigIsolationArgs(options.forgeMcpConfigPath),
|
|
67
|
+
...(options.headless ? claudeHeadlessToolArgs(settings) : ["--settings", JSON.stringify(settings)]),
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
const ancestorInstructionEntries = ["CLAUDE.md", "CLAUDE.local.md", ".claude/CLAUDE.md", ".claude/rules/**"];
|
|
71
|
+
/**
|
|
72
|
+
* Instruction-file patterns for each directory strictly above the working directory. Windows
|
|
73
|
+
* paths are listed both as written and with forward slashes, the form glob matchers expect.
|
|
74
|
+
*/
|
|
75
|
+
export function ancestorInstructionExcludes(workingDirectory, pathApi = path) {
|
|
76
|
+
const patterns = new Set();
|
|
77
|
+
let current = pathApi.resolve(workingDirectory);
|
|
78
|
+
for (let parent = pathApi.dirname(current); parent !== current; current = parent, parent = pathApi.dirname(current)) {
|
|
79
|
+
for (const entry of ancestorInstructionEntries) {
|
|
80
|
+
const pattern = pathApi.join(parent, entry);
|
|
81
|
+
patterns.add(pattern);
|
|
82
|
+
patterns.add(pattern.replaceAll("\\", "/"));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return [...patterns];
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Codex reads `config.toml` (MCP servers, profiles, plugins), `AGENTS.md`, `AGENTS.override.md`,
|
|
89
|
+
* rules and skills from `CODEX_HOME`, so Forge runs it with a home of its own on each runner
|
|
90
|
+
* instead of the user's `~/.codex`. Codex's `--ignore-user-config` still loads the global
|
|
91
|
+
* `AGENTS.md`, so a separate home is the only full isolation. API-key turns authenticate through
|
|
92
|
+
* the environment; a local Codex CLI login is made once into this home
|
|
93
|
+
* (`CODEX_HOME=<path> codex login`).
|
|
94
|
+
*
|
|
95
|
+
* In a Forge sandbox container (`FORGE_RUNTIME_MODE=docker`) `~/.codex` is already Forge's own:
|
|
96
|
+
* an attempt-scoped volume that holds only the copied `auth.json` (sandbox/provider-auth/codex.ts).
|
|
97
|
+
*/
|
|
98
|
+
export function forgeCodexHome(homeDirectory = os.homedir(), environment = process.env) {
|
|
99
|
+
return isForgeSandboxContainer(environment)
|
|
100
|
+
? path.join(homeDirectory, ".codex")
|
|
101
|
+
: path.join(homeDirectory, ...forgeCodexHomeSegments);
|
|
102
|
+
}
|
|
103
|
+
const forgeCodexHomeSegments = [".forge", "provider-homes", "codex"];
|
|
104
|
+
export function codexUserConfigIsolationEnv(homeDirectory = os.homedir(), environment = process.env) {
|
|
105
|
+
const codexHome = forgeCodexHome(homeDirectory, environment);
|
|
106
|
+
if (!isForgeSandboxContainer(environment)) {
|
|
107
|
+
ensureForgeCodexHome(homeDirectory);
|
|
108
|
+
}
|
|
109
|
+
return { CODEX_HOME: codexHome };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Creates Forge's Codex home one directory at a time. Each directory Forge owns on the way
|
|
113
|
+
* (`.forge`, `provider-homes`, `codex`) must be a real directory: a symlink or Windows junction
|
|
114
|
+
* there, stale or user-made, would hand Codex another directory such as the user's `~/.codex`, so
|
|
115
|
+
* the launch is refused instead. The user's home directory itself may be reached through a link.
|
|
116
|
+
* The home is private whether Forge creates it now or it already exists, like Forge's other
|
|
117
|
+
* private directories; Windows keeps the profile's inherited ACL.
|
|
118
|
+
*/
|
|
119
|
+
function ensureForgeCodexHome(homeDirectory) {
|
|
120
|
+
let current = homeDirectory;
|
|
121
|
+
for (const segment of forgeCodexHomeSegments) {
|
|
122
|
+
current = path.join(current, segment);
|
|
123
|
+
const stat = lstatIfPresent(current);
|
|
124
|
+
if (stat && (stat.isSymbolicLink() || !stat.isDirectory())) {
|
|
125
|
+
throw new ForgeFailureError("provider_home_redirected", `Forge can't run Codex with its own home: ${current} is a link or not a directory, so Codex could read another configuration, such as the user's own ~/.codex. Remove it; Forge recreates its own directory there on the next launch.`);
|
|
126
|
+
}
|
|
127
|
+
if (!stat)
|
|
128
|
+
fs.mkdirSync(current, { mode: 0o700 });
|
|
129
|
+
}
|
|
130
|
+
if (process.platform !== "win32")
|
|
131
|
+
fs.chmodSync(current, 0o700);
|
|
132
|
+
}
|
|
133
|
+
function lstatIfPresent(target) {
|
|
134
|
+
try {
|
|
135
|
+
return fs.lstatSync(target);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
if (error.code === "ENOENT")
|
|
139
|
+
return null;
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function isForgeSandboxContainer(environment) {
|
|
144
|
+
return environment.FORGE_RUNTIME_MODE?.trim().toLowerCase() === "docker";
|
|
145
|
+
}
|
|
146
|
+
/** The provider environment that shuts out the user's own configuration. */
|
|
147
|
+
export function providerUserConfigIsolationEnv(provider, homeDirectory = os.homedir(), environment = process.env) {
|
|
148
|
+
return normalizeProvider(provider) === "codex" ? codexUserConfigIsolationEnv(homeDirectory, environment) : {};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* cursor-agent has no flag or environment variable that keeps it from reading `~/.cursor`
|
|
152
|
+
* (`mcp.json`, `cli-config.json`, rules), and `--approve-mcps` would approve every server there;
|
|
153
|
+
* other CLIs are unknown. Forge refuses to launch them.
|
|
154
|
+
*/
|
|
155
|
+
export function providerUserConfigIsolationUnsupported(provider) {
|
|
156
|
+
const name = provider.trim() || "this provider";
|
|
157
|
+
return new ForgeFailureError("provider_user_config_isolation_unsupported", `Forge can't launch ${name}: it can't keep the host user's own ${name} configuration (instruction files, settings, rules, MCP servers, plugins) out of the agent session. Forge agents get only the context Forge gives them, so only ${userConfigIsolatedProviders.join(" and ")} sessions run on Forge runners.`);
|
|
158
|
+
}
|
|
159
|
+
export function isProviderUserConfigIsolated(provider) {
|
|
160
|
+
return userConfigIsolatedProviders.includes(normalizeProvider(provider));
|
|
161
|
+
}
|
|
162
|
+
function normalizeProvider(provider) {
|
|
163
|
+
return provider.trim().toLowerCase();
|
|
164
|
+
}
|
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
export declare function isPathUnderRoot(root: string, target: string): boolean;
|
|
2
2
|
export declare function ensureNoSymbolicLinkPathSegments(root: string, targetDirectory: string, errorMessage: string): Promise<void>;
|
|
3
3
|
export declare function sha256File(filePath: string): Promise<string>;
|
|
4
|
+
/**
|
|
5
|
+
* Applies the end-of-run sidecar shape check when a QA gate records the sidecar, so the agent
|
|
6
|
+
* learns about a malformed file (for example a JSON array followed by a summary line) while it
|
|
7
|
+
* can still rewrite it, instead of the whole attempt failing after the run.
|
|
8
|
+
*/
|
|
9
|
+
export declare function assertQaSidecarJsonShape(fullPath: string, sidecarPath: string, label: string): Promise<void>;
|
|
@@ -2,6 +2,7 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import { createReadStream } from "node:fs";
|
|
3
3
|
import fs from "node:fs/promises";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import { looksLikeJson, maximumIntegrityJsonBytes, oversizedJsonReason, sidecarJsonShapeProblem, sidecarJsonRepairInstruction, } from "./sidecar-json-shape.js";
|
|
5
6
|
export function isPathUnderRoot(root, target) {
|
|
6
7
|
const relative = path.relative(path.resolve(root), path.resolve(target));
|
|
7
8
|
return Boolean(relative)
|
|
@@ -42,3 +43,30 @@ export async function sha256File(filePath) {
|
|
|
42
43
|
}
|
|
43
44
|
return hash.digest("hex");
|
|
44
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Applies the end-of-run sidecar shape check when a QA gate records the sidecar, so the agent
|
|
48
|
+
* learns about a malformed file (for example a JSON array followed by a summary line) while it
|
|
49
|
+
* can still rewrite it, instead of the whole attempt failing after the run.
|
|
50
|
+
*/
|
|
51
|
+
export async function assertQaSidecarJsonShape(fullPath, sidecarPath, label) {
|
|
52
|
+
const problem = await qaSidecarJsonShapeProblem(fullPath, sidecarPath);
|
|
53
|
+
if (problem !== null) {
|
|
54
|
+
throw new Error(`${label}: ${sidecarJsonRepairInstruction(sidecarPath, problem)}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async function qaSidecarJsonShapeProblem(fullPath, sidecarPath) {
|
|
58
|
+
const handle = await fs.open(fullPath, "r");
|
|
59
|
+
try {
|
|
60
|
+
const { size } = await handle.stat();
|
|
61
|
+
if (size <= maximumIntegrityJsonBytes) {
|
|
62
|
+
return sidecarJsonShapeProblem(await handle.readFile(), sidecarPath);
|
|
63
|
+
}
|
|
64
|
+
// Too large to inspect as JSON: only a file that starts like JSON is a problem.
|
|
65
|
+
const prefix = Buffer.alloc(4096);
|
|
66
|
+
const { bytesRead } = await handle.read(prefix, 0, prefix.length, 0);
|
|
67
|
+
return looksLikeJson(prefix.subarray(0, bytesRead).toString("utf8"), sidecarPath) ? oversizedJsonReason : null;
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
await handle.close();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type BigIntStats } from "node:fs";
|
|
2
|
+
import { type HostLoadAcquireOptions, type HostLoadAdmission, type HostLoadStepGate, type HostLoadStepRequest } from "../runtime/host-load-governor.js";
|
|
3
|
+
export interface SandboxHostCapacityGateOptions {
|
|
4
|
+
readonly write?: (line: string) => void;
|
|
5
|
+
readonly responseRoot?: string;
|
|
6
|
+
readonly pollMs?: number;
|
|
7
|
+
/** Fails closed if the bridge never answers (the host enforces its own maximum wait first). */
|
|
8
|
+
readonly maxWaitMs?: number;
|
|
9
|
+
/** Owner of authentic replies (root in the container; tests use their own uid). */
|
|
10
|
+
readonly trustedOwnerUid?: number;
|
|
11
|
+
/**
|
|
12
|
+
* How a reply is proven the bridge's; by default POSIX ownership by trustedOwnerUid,
|
|
13
|
+
* which is what the Linux sandbox has. A test standing in for the container on a
|
|
14
|
+
* host without POSIX ownership supplies the provenance it can prove instead.
|
|
15
|
+
*/
|
|
16
|
+
readonly replyOwnership?: SandboxReplyOwnership;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Whether the bridge owns a reply, judged from the stat of the open handle of a
|
|
20
|
+
* regular file with a single link: null when it does, otherwise why it does not.
|
|
21
|
+
* The stat is exact (bigint): no owner id or file identity is rounded to a double.
|
|
22
|
+
*/
|
|
23
|
+
export type SandboxReplyOwnership = (stat: BigIntStats) => string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Inside the Linux sandbox a reply is the bridge's when the trusted uid (root)
|
|
26
|
+
* owns it and no one else can write it: the sandbox user can create neither.
|
|
27
|
+
* Windows has no such proof in a file's stat (Node reports uid 0 for every file
|
|
28
|
+
* and derives the mode from the read-only attribute alone), so there no reply
|
|
29
|
+
* is ever trusted: the gate fails closed rather than read a read-only file as root's.
|
|
30
|
+
*/
|
|
31
|
+
export declare function posixReplyOwnership(trustedOwnerUid: number, platform?: NodeJS.Platform): SandboxReplyOwnership;
|
|
32
|
+
export declare class SandboxBrokeredHostLoadGate implements HostLoadStepGate {
|
|
33
|
+
readonly enabled = true;
|
|
34
|
+
private readonly write;
|
|
35
|
+
private readonly responseRoot;
|
|
36
|
+
private readonly pollMs;
|
|
37
|
+
private readonly replyOwnership;
|
|
38
|
+
readonly maxWaitMs: number;
|
|
39
|
+
constructor(options?: SandboxHostCapacityGateOptions);
|
|
40
|
+
acquire(request: HostLoadStepRequest, options?: HostLoadAcquireOptions): Promise<HostLoadAdmission>;
|
|
41
|
+
private admission;
|
|
42
|
+
private send;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The brokered gate when the host bridge enabled it for this sandbox. The
|
|
46
|
+
* variable is removed once read, so commands the sandbox runs never inherit it
|
|
47
|
+
* (a nested Forge process must not queue behind its parent's token).
|
|
48
|
+
*/
|
|
49
|
+
export declare function sandboxHostLoadGateFromEnvironment(environment?: NodeJS.ProcessEnv): HostLoadStepGate | undefined;
|
|
50
|
+
/** Test support: forgets the cached gate. */
|
|
51
|
+
export declare function resetSandboxHostLoadGateForTesting(): void;
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { constants as fsConstants } from "node:fs";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { afterNotified, HostCapacityTimeoutError, waitChanged, } from "../runtime/host-load-governor.js";
|
|
6
|
+
import { defaultHostLoadGovernorConfig } from "../runtime/host-load-governor-config.js";
|
|
7
|
+
import { HostCapacityTokenFencedError } from "../runtime/host-load-token-ledger.js";
|
|
8
|
+
import { sandboxHostCapacityBrokerEnvironment, sandboxHostCapacityBrokerProtocol, sandboxHostCapacityLineType, sandboxHostCapacityMaxWaitEnvironment, sandboxHostCapacityResponseOwnerUid, sandboxHostCapacityResponseRoot, } from "../sandbox/host-capacity-broker.js";
|
|
9
|
+
import { monotonicNowMs } from "../shared/time/monotonic.js";
|
|
10
|
+
import { ForgeFailureError } from "./session.types.js";
|
|
11
|
+
/**
|
|
12
|
+
* Inside the Linux sandbox a reply is the bridge's when the trusted uid (root)
|
|
13
|
+
* owns it and no one else can write it: the sandbox user can create neither.
|
|
14
|
+
* Windows has no such proof in a file's stat (Node reports uid 0 for every file
|
|
15
|
+
* and derives the mode from the read-only attribute alone), so there no reply
|
|
16
|
+
* is ever trusted: the gate fails closed rather than read a read-only file as root's.
|
|
17
|
+
*/
|
|
18
|
+
export function posixReplyOwnership(trustedOwnerUid, platform = process.platform) {
|
|
19
|
+
return stat => {
|
|
20
|
+
if (platform === "win32")
|
|
21
|
+
return "file ownership cannot be proven on Windows";
|
|
22
|
+
if (stat.uid !== BigInt(trustedOwnerUid))
|
|
23
|
+
return `it is owned by uid ${stat.uid}, not the bridge`;
|
|
24
|
+
if ((stat.mode & 18n) !== 0n)
|
|
25
|
+
return "others can write it";
|
|
26
|
+
return null;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export class SandboxBrokeredHostLoadGate {
|
|
30
|
+
enabled = true;
|
|
31
|
+
write;
|
|
32
|
+
responseRoot;
|
|
33
|
+
pollMs;
|
|
34
|
+
replyOwnership;
|
|
35
|
+
maxWaitMs;
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
this.write = options.write ?? (line => { process.stdout.write(line); });
|
|
38
|
+
this.responseRoot = options.responseRoot ?? sandboxHostCapacityResponseRoot;
|
|
39
|
+
this.pollMs = options.pollMs ?? 250;
|
|
40
|
+
this.replyOwnership = options.replyOwnership
|
|
41
|
+
?? posixReplyOwnership(options.trustedOwnerUid ?? sandboxHostCapacityResponseOwnerUid);
|
|
42
|
+
this.maxWaitMs = options.maxWaitMs ?? defaultHostLoadGovernorConfig.maxTotalWaitMs + 2 * 60_000;
|
|
43
|
+
}
|
|
44
|
+
async acquire(request, options = {}) {
|
|
45
|
+
const requestId = randomUUID();
|
|
46
|
+
const responsePath = path.join(this.responseRoot, `${requestId}.json`);
|
|
47
|
+
const startedAtMs = monotonicNowMs();
|
|
48
|
+
this.send("acquire", requestId, request);
|
|
49
|
+
// The broker answers again when the wait changes (a new slot holder, say). Each changed wait is
|
|
50
|
+
// reported by the governor's rule, in order. The admission, failure or cancellation lands after the
|
|
51
|
+
// report in flight, and a report still queued by then is dropped as stale (as in the governor).
|
|
52
|
+
let reportedWait = null;
|
|
53
|
+
let reports = Promise.resolve();
|
|
54
|
+
let settled = false;
|
|
55
|
+
try {
|
|
56
|
+
while (true) {
|
|
57
|
+
if (options.signal?.aborted)
|
|
58
|
+
throw options.signal.reason ?? new Error("The host capacity wait was cancelled.");
|
|
59
|
+
const response = await readResponse(responsePath, requestId, this.replyOwnership);
|
|
60
|
+
const wait = newWait(response, reportedWait);
|
|
61
|
+
if (wait) {
|
|
62
|
+
reportedWait = wait;
|
|
63
|
+
reports = reports.then(async () => { if (!settled)
|
|
64
|
+
await options.onWaiting?.(wait); }).catch(() => undefined);
|
|
65
|
+
}
|
|
66
|
+
if (response?.status === "admitted") {
|
|
67
|
+
settled = true;
|
|
68
|
+
await afterNotified(reports);
|
|
69
|
+
return this.admission(requestId, responsePath, request, response);
|
|
70
|
+
}
|
|
71
|
+
if (response?.status === "failed" || response?.status === "fenced")
|
|
72
|
+
throw responseError(request, response);
|
|
73
|
+
if (monotonicNowMs() - startedAtMs > this.maxWaitMs) {
|
|
74
|
+
throw new HostCapacityTimeoutError(request.stepClass, request.label, monotonicNowMs() - startedAtMs, "the host bridge did not answer the sandbox's host capacity request.");
|
|
75
|
+
}
|
|
76
|
+
await delay(this.pollMs);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
settled = true;
|
|
81
|
+
this.send("release", requestId, request);
|
|
82
|
+
await fs.rm(responsePath, { force: true }).catch(() => undefined);
|
|
83
|
+
await afterNotified(reports);
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
admission(requestId, responsePath, request, response) {
|
|
88
|
+
const fenced = new AbortController();
|
|
89
|
+
// The bridge rewrites the response as "fenced" if it lost the token; the step then stops.
|
|
90
|
+
const watch = setInterval(() => {
|
|
91
|
+
void readResponse(responsePath, requestId, this.replyOwnership).then(current => {
|
|
92
|
+
if (current?.status === "fenced" && !fenced.signal.aborted) {
|
|
93
|
+
fenced.abort(new HostCapacityTokenFencedError(requestId, "renew"));
|
|
94
|
+
}
|
|
95
|
+
}, (error) => {
|
|
96
|
+
// A reply that is not the bridge's: the reservation can no longer be trusted.
|
|
97
|
+
if (!fenced.signal.aborted)
|
|
98
|
+
fenced.abort(error);
|
|
99
|
+
});
|
|
100
|
+
}, Math.max(this.pollMs, 1_000));
|
|
101
|
+
watch.unref?.();
|
|
102
|
+
let released = false;
|
|
103
|
+
let holding = false;
|
|
104
|
+
let retainTimer = null;
|
|
105
|
+
const giveBack = async () => {
|
|
106
|
+
if (released)
|
|
107
|
+
return;
|
|
108
|
+
released = true;
|
|
109
|
+
clearInterval(watch);
|
|
110
|
+
if (retainTimer)
|
|
111
|
+
clearInterval(retainTimer);
|
|
112
|
+
this.send("release", requestId, request);
|
|
113
|
+
await fs.rm(responsePath, { force: true }).catch(() => undefined);
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
tokenId: requestId,
|
|
117
|
+
fencingToken: 0,
|
|
118
|
+
stepClass: request.stepClass,
|
|
119
|
+
waitMs: response.waitMs ?? 0,
|
|
120
|
+
decision: decision(true, response),
|
|
121
|
+
fenced: fenced.signal,
|
|
122
|
+
// The container's processes are stopped with the sandbox; nothing to record host-side.
|
|
123
|
+
attachProcess: async () => undefined,
|
|
124
|
+
release: async () => {
|
|
125
|
+
if (released || holding)
|
|
126
|
+
return;
|
|
127
|
+
await giveBack();
|
|
128
|
+
if (fenced.signal.aborted)
|
|
129
|
+
throw fenced.signal.reason;
|
|
130
|
+
},
|
|
131
|
+
// Part of the step's tree may still run in the container: the request stays
|
|
132
|
+
// outstanding, so the bridge keeps the host token, until the check proves it
|
|
133
|
+
// gone. Without a check it is kept until the container exits, which stops every
|
|
134
|
+
// process in it and makes the bridge return the token.
|
|
135
|
+
retainUntilStopped: (stillGone, reason) => {
|
|
136
|
+
if (released || holding)
|
|
137
|
+
return;
|
|
138
|
+
holding = true;
|
|
139
|
+
if (!fenced.signal.aborted)
|
|
140
|
+
fenced.abort(reason);
|
|
141
|
+
// The bridge marks the host token retained, so this attempt's next step does not wait for it.
|
|
142
|
+
this.send("retain", requestId, request);
|
|
143
|
+
if (!stillGone)
|
|
144
|
+
return;
|
|
145
|
+
let checking = false;
|
|
146
|
+
retainTimer = setInterval(() => {
|
|
147
|
+
if (checking)
|
|
148
|
+
return;
|
|
149
|
+
checking = true;
|
|
150
|
+
void stillGone()
|
|
151
|
+
.then(gone => gone ? giveBack() : undefined, () => undefined)
|
|
152
|
+
.finally(() => { checking = false; });
|
|
153
|
+
}, Math.max(this.pollMs, 1_000));
|
|
154
|
+
retainTimer.unref?.();
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
send(action, requestId, request) {
|
|
159
|
+
this.write(`${JSON.stringify({
|
|
160
|
+
type: sandboxHostCapacityLineType,
|
|
161
|
+
version: 1,
|
|
162
|
+
action,
|
|
163
|
+
requestId,
|
|
164
|
+
stepClass: request.stepClass,
|
|
165
|
+
label: request.label,
|
|
166
|
+
})}\n`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
let cachedGate = null;
|
|
170
|
+
/**
|
|
171
|
+
* The brokered gate when the host bridge enabled it for this sandbox. The
|
|
172
|
+
* variable is removed once read, so commands the sandbox runs never inherit it
|
|
173
|
+
* (a nested Forge process must not queue behind its parent's token).
|
|
174
|
+
*/
|
|
175
|
+
export function sandboxHostLoadGateFromEnvironment(environment = process.env) {
|
|
176
|
+
if (cachedGate !== null)
|
|
177
|
+
return cachedGate;
|
|
178
|
+
const protocol = environment[sandboxHostCapacityBrokerEnvironment];
|
|
179
|
+
const hostMaxWaitMs = Number(environment[sandboxHostCapacityMaxWaitEnvironment]);
|
|
180
|
+
delete environment[sandboxHostCapacityBrokerEnvironment];
|
|
181
|
+
delete environment[sandboxHostCapacityMaxWaitEnvironment];
|
|
182
|
+
cachedGate = environment.FORGE_RUNTIME_MODE === "docker" && protocol === sandboxHostCapacityBrokerProtocol
|
|
183
|
+
// The host's own maximum wait (the operator may have changed it) plus slack for the reply.
|
|
184
|
+
? new SandboxBrokeredHostLoadGate(Number.isFinite(hostMaxWaitMs) && hostMaxWaitMs > 0
|
|
185
|
+
? { maxWaitMs: hostMaxWaitMs + 2 * 60_000 }
|
|
186
|
+
: {})
|
|
187
|
+
: undefined;
|
|
188
|
+
return cachedGate;
|
|
189
|
+
}
|
|
190
|
+
/** Test support: forgets the cached gate. */
|
|
191
|
+
export function resetSandboxHostLoadGateForTesting() {
|
|
192
|
+
cachedGate = null;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* A waiting answer whose wait changed since the one last reported, by the governor's rule
|
|
196
|
+
* (`waitChanged`: a new reason, or a slot wait's new holder or place), or null.
|
|
197
|
+
*/
|
|
198
|
+
function newWait(response, reported) {
|
|
199
|
+
if (response?.status !== "waiting")
|
|
200
|
+
return null;
|
|
201
|
+
const next = decision(false, response);
|
|
202
|
+
return waitChanged(reported, next) ? next : null;
|
|
203
|
+
}
|
|
204
|
+
function decision(admitted, response) {
|
|
205
|
+
return {
|
|
206
|
+
admitted,
|
|
207
|
+
reason: (response.reason ?? (admitted ? "admitted" : "cpu_budget")),
|
|
208
|
+
cpuBudgetCores: null,
|
|
209
|
+
memoryBudgetBytes: null,
|
|
210
|
+
reservedCpuCores: 0,
|
|
211
|
+
reservedMemoryBytes: 0,
|
|
212
|
+
detail: response.detail ?? (admitted ? "The host bridge admitted the step." : "The host is at capacity."),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function responseError(request, response) {
|
|
216
|
+
const message = response.message ?? "The host bridge refused the sandbox's host capacity request.";
|
|
217
|
+
if (response.code === "host_capacity_timeout") {
|
|
218
|
+
return new HostCapacityTimeoutError(request.stepClass, request.label, response.waitMs ?? 0, message);
|
|
219
|
+
}
|
|
220
|
+
return new ForgeFailureError(response.code ?? "host_capacity_broker_failed", message, "bridge");
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The bridge's reply, or null while there is none (or the one opened was just
|
|
224
|
+
* replaced by a newer reply). Only a regular file with a single link that the
|
|
225
|
+
* reply ownership rule proves the bridge's (in the sandbox: owned by the trusted
|
|
226
|
+
* uid and writable by no one else) is the bridge's: the sandbox user can create
|
|
227
|
+
* none of those (see host-capacity-broker.ts).
|
|
228
|
+
* Anything else fails closed with sandbox_host_capacity_reply_untrusted.
|
|
229
|
+
*/
|
|
230
|
+
async function readResponse(responsePath, requestId, replyOwnership) {
|
|
231
|
+
let handle;
|
|
232
|
+
try {
|
|
233
|
+
handle = await fs.open(responsePath, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0));
|
|
234
|
+
}
|
|
235
|
+
catch (error) {
|
|
236
|
+
if (error.code === "ENOENT")
|
|
237
|
+
return null;
|
|
238
|
+
throw untrustedReply(responsePath, `it could not be opened safely (${error.code ?? "error"})`);
|
|
239
|
+
}
|
|
240
|
+
let content;
|
|
241
|
+
try {
|
|
242
|
+
const stat = await handle.stat({ bigint: true });
|
|
243
|
+
// No link left: the bridge renamed a newer reply over this one after it was opened
|
|
244
|
+
// (waiting -> admitted, admitted -> fenced). It is not a forgery, and nothing is read
|
|
245
|
+
// from it; the next poll opens the newer reply. Only the bridge can replace a reply,
|
|
246
|
+
// since the directory is root-owned and not writable by the sandbox user.
|
|
247
|
+
if (stat.isFile() && stat.nlink === 0n)
|
|
248
|
+
return null;
|
|
249
|
+
if (!stat.isFile() || stat.nlink !== 1n)
|
|
250
|
+
throw untrustedReply(responsePath, "it is not a regular file with a single link");
|
|
251
|
+
const notTheBridges = replyOwnership(stat);
|
|
252
|
+
if (notTheBridges !== null)
|
|
253
|
+
throw untrustedReply(responsePath, notTheBridges);
|
|
254
|
+
content = await handle.readFile("utf8");
|
|
255
|
+
}
|
|
256
|
+
finally {
|
|
257
|
+
await handle.close();
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
const value = JSON.parse(content);
|
|
261
|
+
if (!value || typeof value.status !== "string")
|
|
262
|
+
return null;
|
|
263
|
+
if (value.requestId !== requestId)
|
|
264
|
+
throw untrustedReply(responsePath, "it answers another request");
|
|
265
|
+
return value;
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
if (error instanceof ForgeFailureError)
|
|
269
|
+
throw error;
|
|
270
|
+
// Mid-write is impossible (replies are renamed into place); treat garbage as no reply yet.
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function untrustedReply(responsePath, reason) {
|
|
275
|
+
return new ForgeFailureError("sandbox_host_capacity_reply_untrusted", `sandbox_host_capacity_reply_untrusted: ${path.basename(responsePath)} was not written by the host bridge (${reason}); the step does not run on it.`, "sandbox");
|
|
276
|
+
}
|
|
277
|
+
async function delay(ms) {
|
|
278
|
+
await new Promise(resolve => setTimeout(resolve, ms));
|
|
279
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { LoadedHostRuntimeContract } from "../runtime/host-runtime-contract.js";
|
|
2
|
+
import { type HostRuntimeLeaseRuntime, type HostRuntimeLeaseStart } from "../runtime/host-runtime-lease.js";
|
|
3
|
+
import type { RuntimeRestartReason } from "./provider-cli/provider-cli.types.js";
|
|
4
|
+
import type { HostRuntimeReadinessProbeResult, RunOnceDispatchPacket, RunnerDispatchEvent } from "./session.types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Builds and launches the attempt's configured project from the prepared worktree and reports the
|
|
7
|
+
* verified port; or, while a WIP re-apply left unresolved conflicts that would fail its build,
|
|
8
|
+
* defers the start to pre-publication validation (deferredProjectStartEvent) so the agent runs first.
|
|
9
|
+
*/
|
|
10
|
+
export declare function startOrDeferConfiguredProject(hostRuntime: HostRuntimeLeaseRuntime, hostLease: HostRuntimeLeaseStart, dispatch: RunOnceDispatchPacket, reapplyConflicts: readonly string[] | undefined, signal: AbortSignal | undefined, emitLifecycle: (event: RunnerDispatchEvent) => Promise<void>): Promise<{
|
|
11
|
+
readonly hostLease: HostRuntimeLeaseStart;
|
|
12
|
+
readonly deferred: boolean;
|
|
13
|
+
}>;
|
|
14
|
+
export declare function managedQaLaunchTarget(runtimeContract: LoadedHostRuntimeContract): HostRuntimeReadinessProbeResult["qaAppLaunchTarget"];
|
|
15
|
+
/** The host_runtime_restart_started checkpoint text for the first cause of a validation-time restart. */
|
|
16
|
+
export declare function runtimeRestartStartedMessage(reason: RuntimeRestartReason, changes: readonly string[]): string;
|
|
17
|
+
/**
|
|
18
|
+
* The dispatch with its managed Browser QA target's URL from the started stack. While the first start
|
|
19
|
+
* is deferred (startOrDeferConfiguredProject) the stack has no URL yet: the target keeps its port,
|
|
20
|
+
* and validation starts the stack once the agent has resolved the conflicts.
|
|
21
|
+
*/
|
|
22
|
+
export declare function withMaterializedManagedQaTarget(dispatch: RunOnceDispatchPacket, hostLease: HostRuntimeLeaseStart | null, projectStartDeferred?: boolean): RunOnceDispatchPacket;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { hostRuntimeEventResources } from "../runtime/host-runtime-lease.js";
|
|
2
|
+
import { configuredLaunchPortNames } from "../runtime/host-runtime-promotion.js";
|
|
3
|
+
import { deferredProjectStartEvent } from "../worktree/worktree.service.js";
|
|
4
|
+
// The attempt's configured project on a managed host: its first start (or its deferral while a WIP
|
|
5
|
+
// re-apply left unresolved conflicts), the checkpoint text for a validation-time restart, and the
|
|
6
|
+
// Browser QA target it serves.
|
|
7
|
+
/**
|
|
8
|
+
* Builds and launches the attempt's configured project from the prepared worktree and reports the
|
|
9
|
+
* verified port; or, while a WIP re-apply left unresolved conflicts that would fail its build,
|
|
10
|
+
* defers the start to pre-publication validation (deferredProjectStartEvent) so the agent runs first.
|
|
11
|
+
*/
|
|
12
|
+
export async function startOrDeferConfiguredProject(hostRuntime, hostLease, dispatch, reapplyConflicts, signal, emitLifecycle) {
|
|
13
|
+
// Only a managed runtime with a port to launch builds anything here (launchConfiguredProject returns
|
|
14
|
+
// at once for analysis_only or no launch config), so no other setup has a build to defer.
|
|
15
|
+
const deferred = (reapplyConflicts?.length ?? 0) > 0 && Boolean(hostRuntime.restartConfiguredProject)
|
|
16
|
+
&& hostLease.executionClass === "managed_runtime"
|
|
17
|
+
&& configuredLaunchPortNames(hostLease.runtimeContract).length > 0;
|
|
18
|
+
if (deferred) {
|
|
19
|
+
await emitLifecycle(deferredProjectStartEvent(reapplyConflicts));
|
|
20
|
+
return { hostLease, deferred };
|
|
21
|
+
}
|
|
22
|
+
const unconfiguredPortCount = hostLease.lease.resources.ports.length;
|
|
23
|
+
const started = await hostRuntime.startConfiguredProject(dispatch.sandbox.dispatchAttemptId, dispatch.lease.fencingToken, signal, async (checkpoint) => {
|
|
24
|
+
await emitLifecycle({
|
|
25
|
+
role: "runner",
|
|
26
|
+
kind: "checkpoint",
|
|
27
|
+
content: checkpoint.message,
|
|
28
|
+
eventType: checkpoint.code,
|
|
29
|
+
data: checkpoint.data ? JSON.stringify(checkpoint.data) : undefined,
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
if (started.lease.resources.ports.length > unconfiguredPortCount) {
|
|
33
|
+
const assigned = started.lease.resources.ports.at(-1);
|
|
34
|
+
await emitLifecycle({
|
|
35
|
+
role: "runner",
|
|
36
|
+
kind: "checkpoint",
|
|
37
|
+
content: `Verified supervised project runtime ${assigned.name} on ${assigned.hostIp}:${assigned.hostPort}.`,
|
|
38
|
+
eventType: "host_runtime_project_ready",
|
|
39
|
+
data: JSON.stringify(hostRuntimeEventResources(started.lease, started.runtimeContract, started.executionClass)),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return { hostLease: started, deferred };
|
|
43
|
+
}
|
|
44
|
+
export function managedQaLaunchTarget(runtimeContract) {
|
|
45
|
+
const playwright = runtimeContract.contract.runtime.playwright;
|
|
46
|
+
if (!playwright)
|
|
47
|
+
return null;
|
|
48
|
+
const port = runtimeContract.contract.runtime.ports.find(candidate => candidate.name === playwright.baseUrlPort);
|
|
49
|
+
return port?.launch && port.baseUrlEnv && port.format
|
|
50
|
+
? { managedRuntimePort: port.name, routes: ["/"] }
|
|
51
|
+
: null;
|
|
52
|
+
}
|
|
53
|
+
/** The host_runtime_restart_started checkpoint text for the first cause of a validation-time restart. */
|
|
54
|
+
export function runtimeRestartStartedMessage(reason, changes) {
|
|
55
|
+
const current = "so validation runs against the current source.";
|
|
56
|
+
switch (reason) {
|
|
57
|
+
case "deferred_start":
|
|
58
|
+
return `Starting the attempt runtime stack, deferred until the restored work's re-apply conflicts were resolved, ${current}`;
|
|
59
|
+
case "workspace_changed":
|
|
60
|
+
return `The workspace changed since the attempt runtime stack was launched (${changes.join(", ")}); restarting it ${current}`;
|
|
61
|
+
case "previous_restart_incomplete":
|
|
62
|
+
return `The previous attempt runtime restart did not complete; restarting it again ${current}`
|
|
63
|
+
+ (changes.length > 0 ? ` The workspace also changed since launch (${changes.join(", ")}).` : "");
|
|
64
|
+
default:
|
|
65
|
+
return `The workspace state could not be compared with the attempt runtime stack; restarting it ${current}`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The dispatch with its managed Browser QA target's URL from the started stack. While the first start
|
|
70
|
+
* is deferred (startOrDeferConfiguredProject) the stack has no URL yet: the target keeps its port,
|
|
71
|
+
* and validation starts the stack once the agent has resolved the conflicts.
|
|
72
|
+
*/
|
|
73
|
+
export function withMaterializedManagedQaTarget(dispatch, hostLease, projectStartDeferred = false) {
|
|
74
|
+
const target = dispatch.qaBrowser?.launchTarget;
|
|
75
|
+
const portName = target?.managed_runtime_port?.trim().toLowerCase();
|
|
76
|
+
if (!target || !portName)
|
|
77
|
+
return dispatch;
|
|
78
|
+
if (!hostLease) {
|
|
79
|
+
throw new Error("qa_managed_runtime_unavailable: Browser QA requires the verified managed host runtime selected during readiness.");
|
|
80
|
+
}
|
|
81
|
+
const normalizedName = portName.toUpperCase().replaceAll(/[^A-Z0-9]/gu, "_");
|
|
82
|
+
const url = hostLease.environment[`FORGE_${normalizedName}_URL`];
|
|
83
|
+
if (!url && projectStartDeferred)
|
|
84
|
+
return withQaTargetNote(dispatch, target, "Forge starts this port's stack at validation, after the conflicts from restored work are resolved; it has no URL before then.");
|
|
85
|
+
if (!url) {
|
|
86
|
+
throw new Error(`qa_managed_runtime_unavailable: verified runtime port '${portName}' was not launched for this attempt.`);
|
|
87
|
+
}
|
|
88
|
+
return withQaTargetNote(dispatch, { ...target, url: target.url ?? url, ready_url: target.ready_url ?? url }, "Forge launched and verified this URL inside the current fenced attempt.");
|
|
89
|
+
}
|
|
90
|
+
function withQaTargetNote(dispatch, target, note) {
|
|
91
|
+
return {
|
|
92
|
+
...dispatch,
|
|
93
|
+
qaBrowser: dispatch.qaBrowser ? {
|
|
94
|
+
...dispatch.qaBrowser,
|
|
95
|
+
launchTarget: { ...target, notes: [target.notes, note].filter(Boolean).join(" ") },
|
|
96
|
+
} : dispatch.qaBrowser,
|
|
97
|
+
};
|
|
98
|
+
}
|