From e5ab1d2dca7c88ebb50b3867b4f8a72e561aaa00 Mon Sep 17 00:00:00 2001 From: jason-ma-nv Date: Thu, 22 Oct 2026 15:50:03 +0800 Subject: [PATCH 1/4] fix(rebuild): scope the Hermes API port to each rebuild Hermes API port selection publishes its result to NEMOCLAW_HERMES_API_PORT so later steps of the same onboarding run agree on one port. Rebuild did not restore that variable, so a batch upgrade that rebuilt several Hermes sandboxes in one process pinned the next sandbox to the previous one's port. Add the variable to the rebuild facade's scoped environment keys. Fixes #13628 Signed-off-by: jason-ma-nv Co-Authored-By: Claude Opus 5.5 --- .../sandbox/rebuild-flow-lifecycle.test.ts | 33 +++++++++++++++++++ src/lib/actions/sandbox/rebuild-pipeline.ts | 2 ++ 2 files changed, 35 insertions(+) diff --git a/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts b/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts index e20c2d7af8c..cd552f7a8cb 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,38 @@ 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.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 fc0fd1c1d8f..dfe24f03e92 100644 --- a/src/lib/actions/sandbox/rebuild-pipeline.ts +++ b/src/lib/actions/sandbox/rebuild-pipeline.ts @@ -156,6 +156,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). + "NEMOCLAW_HERMES_API_PORT", ...REBUILD_HERMES_DASHBOARD_ENV_KEYS, ...MESSAGING_CHANNEL_CONFIG_ENV_KEYS, ]; From 136f49ff69a7627a53d515c69d8847e5c7535110 Mon Sep 17 00:00:00 2001 From: Rebecca Sliter Date: Mon, 26 Oct 2026 16:14:51 -0700 Subject: [PATCH 2/4] test(rebuild): cover Hermes port restoration after onboarding failure Verify that a failed replacement onboarding restores the caller environment before a second sandbox resolves its API port. Refs #13628 Signed-off-by: Rebecca Sliter --- .../sandbox/rebuild-flow-lifecycle.test.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts b/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts index cd552f7a8cb..9d40d44fcfc 100644 --- a/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts +++ b/src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts @@ -132,6 +132,36 @@ describe("rebuildSandbox flow: lifecycle", () => { }, ); + 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) => { From da66194fd7674d82107324480c951e4e9ee99fa5 Mon Sep 17 00:00:00 2001 From: Rebecca Sliter Date: Mon, 26 Oct 2026 16:53:10 -0700 Subject: [PATCH 3/4] fix(rebuild): use canonical Hermes API port environment key Scope rebuild environment restoration to the allocator-owned Hermes API port key through the existing durable-config dependency. Refs #13628 Signed-off-by: Rebecca Sliter --- src/lib/actions/sandbox/rebuild-durable-config.ts | 2 ++ src/lib/actions/sandbox/rebuild-pipeline.ts | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/actions/sandbox/rebuild-durable-config.ts b/src/lib/actions/sandbox/rebuild-durable-config.ts index c6d43babfef..0e47be6365e 100644 --- a/src/lib/actions/sandbox/rebuild-durable-config.ts +++ b/src/lib/actions/sandbox/rebuild-durable-config.ts @@ -38,6 +38,8 @@ import { DCODE_AGENT_NAME } from "./rebuild-dcode-target"; import type { RebuildSandboxEntry } from "./rebuild-flow-helpers"; import type { RebuildResumeConfig } from "./rebuild-resume-config"; +export { HERMES_API_PORT_ENV } from "../../onboard/hermes-api-port"; + export type RebuildDurableConfig = { dcodeAutoApprovalMode: DcodeAutoApprovalMode; dcodeAutoApprovalModeError: string | null; diff --git a/src/lib/actions/sandbox/rebuild-pipeline.ts b/src/lib/actions/sandbox/rebuild-pipeline.ts index 6b6a75d38c6..d3c332dffe9 100644 --- a/src/lib/actions/sandbox/rebuild-pipeline.ts +++ b/src/lib/actions/sandbox/rebuild-pipeline.ts @@ -38,7 +38,7 @@ import { writeRebuildPolicyHandoff, } from "./rebuild-backup-phase"; import { runRebuildDestroyPhase } from "./rebuild-destroy-phase"; -import { REBUILD_HERMES_DASHBOARD_ENV_KEYS } from "./rebuild-durable-config"; +import { HERMES_API_PORT_ENV, REBUILD_HERMES_DASHBOARD_ENV_KEYS } from "./rebuild-durable-config"; import { delegateRebuildToOwningRegistry, disposeRebuildAgentBaseImagePreflight, @@ -157,7 +157,7 @@ export async function rebuildSandbox( DOCKER_GPU_PATCH_NETWORK_ENV, "NEMOCLAW_OPENSHELL_GATEWAY_STATE_DIR", // Port selection publishes here; keep it from pinning the next rebuild (#13628). - "NEMOCLAW_HERMES_API_PORT", + HERMES_API_PORT_ENV, ...REBUILD_HERMES_DASHBOARD_ENV_KEYS, ...MESSAGING_CHANNEL_CONFIG_ENV_KEYS, ]; From 619ee63600591cbe6ad6f97c84cd6766d1fb97e7 Mon Sep 17 00:00:00 2001 From: Rebecca Sliter Date: Mon, 26 Oct 2026 17:32:54 -0700 Subject: [PATCH 4/4] refactor(rebuild): import Hermes API port key from owner Use the canonical onboarding constant directly in rebuild. Raise only the pipeline fan-out by one edge instead of re-exporting the key through durable config. Refs #13628 Signed-off-by: Rebecca Sliter --- ci/source-architecture-budget.json | 2 +- src/lib/actions/sandbox/rebuild-durable-config.ts | 2 -- src/lib/actions/sandbox/rebuild-pipeline.ts | 3 ++- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/ci/source-architecture-budget.json b/ci/source-architecture-budget.json index 106e031f820..627260a9081 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-durable-config.ts b/src/lib/actions/sandbox/rebuild-durable-config.ts index 0e47be6365e..c6d43babfef 100644 --- a/src/lib/actions/sandbox/rebuild-durable-config.ts +++ b/src/lib/actions/sandbox/rebuild-durable-config.ts @@ -38,8 +38,6 @@ import { DCODE_AGENT_NAME } from "./rebuild-dcode-target"; import type { RebuildSandboxEntry } from "./rebuild-flow-helpers"; import type { RebuildResumeConfig } from "./rebuild-resume-config"; -export { HERMES_API_PORT_ENV } from "../../onboard/hermes-api-port"; - export type RebuildDurableConfig = { dcodeAutoApprovalMode: DcodeAutoApprovalMode; dcodeAutoApprovalModeError: string | null; diff --git a/src/lib/actions/sandbox/rebuild-pipeline.ts b/src/lib/actions/sandbox/rebuild-pipeline.ts index d3c332dffe9..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"; @@ -38,7 +39,7 @@ import { writeRebuildPolicyHandoff, } from "./rebuild-backup-phase"; import { runRebuildDestroyPhase } from "./rebuild-destroy-phase"; -import { HERMES_API_PORT_ENV, REBUILD_HERMES_DASHBOARD_ENV_KEYS } from "./rebuild-durable-config"; +import { REBUILD_HERMES_DASHBOARD_ENV_KEYS } from "./rebuild-durable-config"; import { delegateRebuildToOwningRegistry, disposeRebuildAgentBaseImagePreflight,