Skip to content
2 changes: 1 addition & 1 deletion ci/source-architecture-budget.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
63 changes: 63 additions & 0 deletions src/lib/actions/sandbox/rebuild-flow-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string, number>;
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<string, number>;
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) => {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/actions/sandbox/rebuild-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
...REBUILD_HERMES_DASHBOARD_ENV_KEYS,
...MESSAGING_CHANNEL_CONFIG_ENV_KEYS,
];
Expand Down
Loading