diff --git a/ci/source-architecture-budget.json b/ci/source-architecture-budget.json index 30a2dfb0725..503cb882d5a 100644 --- a/ci/source-architecture-budget.json +++ b/ci/source-architecture-budget.json @@ -48,7 +48,7 @@ "src/lib/actions/sandbox/status-snapshot.ts": 20, "src/lib/actions/sandbox/policy-channel.ts": 30, "src/lib/actions/sandbox/process-recovery.ts": 17, - "src/lib/actions/sandbox/rebuild-pipeline.ts": 29, + "src/lib/actions/sandbox/rebuild-pipeline.ts": 30, "src/lib/actions/uninstall/run-plan.ts": 25, "src/lib/inference/local.ts": 22, "src/lib/inference/onboard-probes.ts": 21, diff --git a/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts b/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts index e20c2d7af8c..9d40d44fcfc 100644 --- a/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts +++ b/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts @@ -17,6 +17,7 @@ import { tempFiles, } from "../../../../test/helpers/rebuild-flow-generic-harness"; import { makePreparedRecoveryManifest } from "./rebuild-flow-test-fixtures"; +import { HERMES_API_PORT_ENV, resolveOnboardHermesApiPort } from "../../onboard/hermes-api-port"; import { enforceRemovedImmutabilityMigrationBoundary } from "../../state/migrations/removed-immutability"; import type { SandboxRuntimeSnapshot } from "../../state/registry/runtime-snapshot"; import { registry } from "../../../../test/helpers/rebuild-flow-harness"; @@ -99,6 +100,68 @@ describe("rebuildSandbox flow: lifecycle", () => { } }); + it.each([undefined, "8650"])( + "keeps a rebuilt Hermes API port from pinning the next sandbox (caller value: %s) (#13628)", + async (callerPort) => { + const restoreEnv = snapshotEnv([HERMES_API_PORT_ENV]); + try { + void (callerPort === undefined + ? delete process.env[HERMES_API_PORT_ENV] + : (process.env[HERMES_API_PORT_ENV] = callerPort)); + const ports = { alpha: 8642, beta: 8643 } as Record; + const getSandbox = (name: string) => ({ name, hermesApiPort: ports[name] }); + let rebuiltPort: number | undefined; + const harness = createRebuildFlowHarness({ + onboard: () => { + rebuiltPort = resolveOnboardHermesApiPort("alpha", { + getSandbox, + allowRegisteredOverride: true, + }); + }, + }); + await harness.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }); + + expect(rebuiltPort).toBe(callerPort ? Number(callerPort) : 8642); + expect(process.env[HERMES_API_PORT_ENV]).toBe(callerPort); + expect( + resolveOnboardHermesApiPort("beta", { getSandbox, allowRegisteredOverride: true }), + ).toBe(callerPort ? Number(callerPort) : 8643); + } finally { + restoreEnv(); + } + }, + ); + + it("restores the caller Hermes port when replacement onboarding fails after publication (#13628)", async () => { + const restoreEnv = snapshotEnv([HERMES_API_PORT_ENV]); + try { + delete process.env[HERMES_API_PORT_ENV]; + const ports = { alpha: 8642, beta: 8643 } as Record; + const getSandbox = (name: string) => ({ name, hermesApiPort: ports[name] }); + let publishedPort: number | undefined; + const harness = createRebuildFlowHarness({ + onboard: () => { + publishedPort = resolveOnboardHermesApiPort("alpha", { + getSandbox, + allowRegisteredOverride: true, + }); + throw new Error("injected post-publication onboarding failure"); + }, + }); + + await expect( + harness.rebuildSandbox("alpha", ["--yes"], { throwOnError: true }), + ).rejects.toThrow("Recreate failed (sandbox destroyed)"); + expect(publishedPort).toBe(8642); + expect(process.env[HERMES_API_PORT_ENV]).toBeUndefined(); + expect( + resolveOnboardHermesApiPort("beta", { getSandbox, allowRegisteredOverride: true }), + ).toBe(8643); + } finally { + restoreEnv(); + } + }); + it.each(["relative/gateway", "/"])( "rejects an unsafe recorded gateway directory before rebuild effects (%s)", async (openshellGatewayStateDir) => { diff --git a/src/lib/actions/sandbox/rebuild-pipeline.ts b/src/lib/actions/sandbox/rebuild-pipeline.ts index 52b628ed329..b9ae2580d72 100644 --- a/src/lib/actions/sandbox/rebuild-pipeline.ts +++ b/src/lib/actions/sandbox/rebuild-pipeline.ts @@ -13,6 +13,7 @@ import { MESSAGING_SETUP_APPLIER_ENV_KEY } from "../../messaging/applier/types"; import { MESSAGING_CHANNEL_CONFIG_ENV_KEYS } from "../../messaging-channel-config"; import { hydrateCredentialEnv } from "../../onboard/credential-env"; import { DOCKER_GPU_PATCH_NETWORK_ENV } from "../../onboard/docker-gpu-patch"; +import { HERMES_API_PORT_ENV } from "../../onboard/hermes-api-port"; import { withPortableOnboardRetirementBoundary } from "../../onboard/portable-retirement-authority"; import { cleanupTempDir } from "../../onboard/temp-files"; import { withMcpLifecycleLock } from "../../state/mcp-lifecycle-lock"; @@ -156,6 +157,8 @@ export async function rebuildSandbox( MESSAGING_SETUP_APPLIER_ENV_KEY, DOCKER_GPU_PATCH_NETWORK_ENV, "NEMOCLAW_OPENSHELL_GATEWAY_STATE_DIR", + // Port selection publishes here; keep it from pinning the next rebuild (#13628). + HERMES_API_PORT_ENV, ...REBUILD_HERMES_DASHBOARD_ENV_KEYS, ...MESSAGING_CHANNEL_CONFIG_ENV_KEYS, ];