Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/lib/actions/sandbox/connect-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,4 +907,83 @@ describe("connectSandbox flow", () => {
expect(logOutput).not.toContain("Probe complete");
expect(exitSpy).toHaveBeenCalledWith(1);
});

it("does not suggest a manual forward when gateway recovery fails before forward start", async () => {
const harness = createConnectHarness({
processCheck: {
checked: true,
wasRunning: false,
recovered: false,
forwardRecovered: false,
recoveryFailureDetail:
"the replacement container identity changed during the final managed supervisor health check",
},
});

await expect(harness.connectSandbox("alpha", { probeOnly: true })).rejects.toThrow(
"process.exit(1)",
);

const errorOutput = harness.errorSpy.mock.calls.map((call) => String(call[0] ?? "")).join("\n");
expect(errorOutput).toContain("NemoClaw could not recover the OpenClaw gateway in 'alpha'");
expect(errorOutput).toContain(
"the replacement container identity changed during the final managed supervisor health check",
);
expect(errorOutput).not.toContain("gateway is running");
expect(errorOutput).not.toContain("openshell forward start");
expect(harness.runAutoPairSpy).not.toHaveBeenCalled();
expect(exitSpy).toHaveBeenCalledWith(1);
});

it("redacts untrusted gateway recovery details before reporting them", async () => {
const opaqueToken = "opaque-gateway-recovery-token";
const harness = createConnectHarness({
processCheck: {
checked: true,
wasRunning: false,
recovered: false,
forwardRecovered: false,
recoveryFailureDetail: `OpenShell failed\nAuthorization: Bearer ${opaqueToken}\u001b[31m`,
},
});

await expect(harness.connectSandbox("alpha", { probeOnly: true })).rejects.toThrow(
"process.exit(1)",
);

const errorOutput = harness.errorSpy.mock.calls.map((call) => String(call[0] ?? "")).join("\n");
expect(errorOutput).toContain("Recovery detail:");
expect(errorOutput).not.toContain(opaqueToken);
expect(errorOutput).not.toContain("\u001b");
expect(errorOutput).toMatch(/Recovery detail: .*\.$/mu);
expect(exitSpy).toHaveBeenCalledWith(1);
});

it("keeps a direct recovery failure detail separate from an earlier callback layer", () => {
const harness = createConnectHarness();
harness.checkAndRecoverSpy.mockImplementation(
(
_sandboxName: string,
options?: {
onRecoveryFailureLayer?: (layer: string, detail?: string) => void;
},
) => {
options?.onRecoveryFailureLayer?.("supervisor not running", "SUPERVISOR_NOT_RUNNING");
return {
checked: true,
wasRunning: false,
recovered: false,
forwardRecovered: false,
recoveryFailureDetail:
"the managed supervisor health check for the recreated sandbox did not pass",
};
},
);

expect(harness.restoreSandboxStartupState("alpha")).toMatchObject({
recoveryFailureDetail:
"the managed supervisor health check for the recreated sandbox did not pass",
recoveryFailureLayer: null,
});
});
});
38 changes: 33 additions & 5 deletions src/lib/actions/sandbox/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ function exitOnForwardRecoveryFailure(
process.exit(1);
}

function exitOnGatewayRecoveryFailure(
sandboxName: string,
agentName: string,
detail: string,
): never {
const safeDetail = sanitizeSandboxStartupRecoveryDetail(detail);
const terminalPunctuation = /[.!?]$/u.test(safeDetail) ? "" : ".";
console.error("");
console.error(
` Probe failed: NemoClaw could not recover the ${agentName} gateway in '${sandboxName}'.`,
);
console.error(` Recovery detail: ${safeDetail}${terminalPunctuation}`);
process.exit(1);
}

async function settlePortablePairingOrExit(sandboxName: string): Promise<boolean> {
const result = await settlePortableOpenClawPairing(sandboxName);
if (result.kind === "incomplete") {
Expand Down Expand Up @@ -318,6 +333,13 @@ async function runSandboxConnectProbe(sandboxName: string): Promise<void> {
detail,
);
}
if ("recoveryFailureDetail" in processCheck && processCheck.recoveryFailureDetail) {
exitOnGatewayRecoveryFailure(
sandboxName,
agentName,
String(processCheck.recoveryFailureDetail),
);
}
if (processCheck.wasRunning) {
await ensureSandboxInferenceRouteOrExit(sandboxName, agent);
// Defense-in-depth scope-upgrade approval on the probe-only / `recover`
Expand Down Expand Up @@ -958,16 +980,22 @@ function maybeEnsureHermesToolGatewayBroker(sb: SandboxEntry | null): void {
}

export function restoreSandboxStartupState(sandboxName: string): SandboxStartupRecoveryResult {
let recoveryFailureDetail: string | null = null;
let recoveryFailureLayer: GatewayRestartFailureLayer | null = null;
let reportedRecoveryFailureDetail: string | null = null;
let reportedRecoveryFailureLayer: GatewayRestartFailureLayer | null = null;
const processCheck = checkAndRecoverSandboxProcesses(sandboxName, {
quiet: true,
onRecoveryFailureLayer: (layer, detail) => {
recoveryFailureLayer = layer;
recoveryFailureDetail = detail ?? null;
reportedRecoveryFailureLayer = layer;
reportedRecoveryFailureDetail = detail ?? null;
},
});
return { ...processCheck, recoveryFailureDetail, recoveryFailureLayer };
const directRecoveryFailureDetail =
"recoveryFailureDetail" in processCheck ? processCheck.recoveryFailureDetail : null;
const recoveryFailureDetail = directRecoveryFailureDetail ?? reportedRecoveryFailureDetail;
const recoveryFailureLayer = directRecoveryFailureDetail
? null
: reportedRecoveryFailureLayer;
return Object.assign(processCheck, { recoveryFailureDetail, recoveryFailureLayer });
}

function restoreInteractiveTerminal(): void {
Expand Down
Loading
Loading