Skip to content
50 changes: 36 additions & 14 deletions src/lib/actions/sandbox/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ const hermesProviderAuth = require("../../hermes-provider-auth") as {
baseUrl?: string,
) => void;
};
const { LOCAL_INFERENCE_PROVIDERS, REMOTE_PROVIDER_CONFIG } = require("../../onboard/providers") as {
const {
LOCAL_INFERENCE_PROVIDERS,
REMOTE_PROVIDER_CONFIG,
providerExistsInGateway,
} = require("../../onboard/providers") as {
LOCAL_INFERENCE_PROVIDERS: string[];
REMOTE_PROVIDER_CONFIG: Record<string, { providerName: string; credentialEnv: string | null }>;
providerExistsInGateway: (name: string, runOpenshellFn: typeof runOpenshell) => boolean;
};

import {
Expand Down Expand Up @@ -363,20 +368,37 @@ export async function rebuildSandbox(
`Preflight credential check: ${rebuildCredentialEnv} → ${credentialValue ? "present" : "MISSING"}`,
);
if (!credentialValue) {
console.error("");
console.error(` ${_RD}Rebuild preflight failed:${R} provider credential not found.`);
console.error(` The non-interactive recreate step requires ${rebuildCredentialEnv},`);
console.error(" but it is not set in the environment.");
console.error("");
console.error(" To fix, do one of:");
console.error(` export ${rebuildCredentialEnv}=<your-key>`);
console.error(` ${CLI_NAME} onboard # re-enter the key interactively`);
console.error("");
console.error(" Sandbox is untouched — no data was lost.");
bail(`Missing credential: ${rebuildCredentialEnv}`);
return;
const gatewayProviderName = rebuildProvider || null;
const gatewayHasProvider =
Boolean(gatewayProviderName) &&
providerExistsInGateway(gatewayProviderName as string, runOpenshell);
log(
`Preflight credential check: gateway provider '${gatewayProviderName || "(none)"}' → ${gatewayHasProvider ? "registered" : "missing"}`,
);
if (gatewayHasProvider) {
console.log(
` ${D}Note: '${gatewayProviderName}' is already registered in the OpenShell gateway. ` +
`Skipping host env credential check; the gateway will reuse the stored credential. ` +
`To rotate, export ${rebuildCredentialEnv}=<new-key> before rebuild.${R}`,
);
rebuildCredentialEnv = null;
} else {
console.error("");
console.error(` ${_RD}Rebuild preflight failed:${R} provider credential not found.`);
console.error(` The non-interactive recreate step requires ${rebuildCredentialEnv},`);
console.error(" but it is not set in the environment.");
console.error("");
console.error(" To fix, do one of:");
console.error(` export ${rebuildCredentialEnv}=<your-key>`);
console.error(` ${CLI_NAME} onboard # re-enter the key interactively`);
console.error("");
console.error(" Sandbox is untouched — no data was lost.");
bail(`Missing credential: ${rebuildCredentialEnv}`);
return;
Comment thread
ssam18 marked this conversation as resolved.
Resolved
}
}
} else {
}
if (!rebuildCredentialEnv) {
// No credentialEnv in session — local inference (Ollama/vLLM) or
// session was lost. Either way, skip the credential preflight;
// onboard will handle it.
Expand Down
48 changes: 26 additions & 22 deletions src/lib/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7503,31 +7503,35 @@ async function setupInference(
resolvedCredentialEnv && credentialValue
? { [resolvedCredentialEnv]: credentialValue }
: {};
const providerResult = upsertProvider(
provider,
config.providerType,
resolvedCredentialEnv,
resolvedEndpointUrl,
env,
);
if (!providerResult.ok) {
console.error(` ${providerResult.message}`);
if (isNonInteractive()) {
process.exit(providerResult.status || 1);
}
const retry = await promptValidationRecovery(
config.label,
classifyApplyFailure(providerResult.message),
const skipUpsertReusingGatewayCredential =
!credentialValue && providerExistsInGateway(provider);
if (!skipUpsertReusingGatewayCredential) {
const providerResult = upsertProvider(
provider,
config.providerType,
resolvedCredentialEnv,
config.helpUrl,
resolvedEndpointUrl,
env,
);
Comment thread
ssam18 marked this conversation as resolved.
Resolved
if (retry === "credential" || retry === "retry") {
continue;
}
if (retry === "selection" || retry === "model") {
return { retry: "selection" };
if (!providerResult.ok) {
console.error(` ${providerResult.message}`);
if (isNonInteractive()) {
process.exit(providerResult.status || 1);
}
const retry = await promptValidationRecovery(
config.label,
classifyApplyFailure(providerResult.message),
resolvedCredentialEnv,
config.helpUrl,
);
if (retry === "credential" || retry === "retry") {
continue;
}
if (retry === "selection" || retry === "model") {
return { retry: "selection" };
}
process.exit(providerResult.status || 1);
}
process.exit(providerResult.status || 1);
}
const args = ["inference", "set"];
if (config.skipVerify) {
Expand Down
69 changes: 64 additions & 5 deletions test/rebuild-credential-preflight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,15 @@ function registryHasSandbox(fixture: ReturnType<typeof createFixture>): boolean
describe("Issue #2273: atomic rebuild", () => {
describe("Layer 2: preflight credential check", () => {
it(
"aborts rebuild BEFORE destroying sandbox when credential is missing",
"aborts rebuild BEFORE destroying sandbox when credential is missing and gateway has no provider",
{ timeout: 60_000 },
() => {
// No credential in env or credentials.json
// No credential in env or credentials.json AND no gateway provider —
// the only configuration that should still bail preflight after #3895.
const f = createFixture({
credentialEnv: "NVIDIA_API_KEY",
// no savedCredential
providerRegistered: false,
});

const result = runRebuild(f);
Expand All @@ -336,6 +338,32 @@ describe("Issue #2273: atomic rebuild", () => {
},
);

it(
"proceeds when credential is missing in env but provider is already registered in OpenShell (#3895)",
{ timeout: 60_000 },
() => {
// Regression for #3895: a sandbox onboarded with NVIDIA Endpoints
// and later modified (e.g. channel add → auto-rebuild) used to fail
// preflight when NVIDIA_API_KEY was no longer in the operator's
// shell, even though the credential was already stored in the
// OpenShell gateway. Rebuild now reuses the gateway-stored
// credential and proceeds without re-prompting.
const f = createFixture({
credentialEnv: "NVIDIA_API_KEY",
// no savedCredential, no env var
providerRegistered: true,
});

const result = runRebuild(f);
const output = (result.stderr || "") + (result.stdout || "");

expect(output).not.toContain("preflight failed");
expect(output).not.toContain("Missing credential: NVIDIA_API_KEY");
expect(output).toContain("already registered in the OpenShell gateway");
expect(output).toContain("Backing up sandbox state");
},
);

it(
"proceeds when credential is saved in credentials.json (not in env)",
{ timeout: 60_000 },
Expand Down Expand Up @@ -471,14 +499,15 @@ describe("Issue #2273: atomic rebuild", () => {
);

it(
"preflight works for non-NVIDIA providers (OpenAI, Anthropic, etc.)",
"preflight aborts for non-NVIDIA providers when both env var and gateway provider are missing",
{ timeout: 60_000 },
() => {
// OpenAI provider with no credential — should abort
// OpenAI provider with no credential anywhere — should abort.
const f = createFixture({
provider: "openai-api",
credentialEnv: "OPENAI_API_KEY",
// no savedCredential
providerRegistered: false,
});

const result = runRebuild(f);
Expand All @@ -491,6 +520,33 @@ describe("Issue #2273: atomic rebuild", () => {
},
);

it.each([
["openai-api", "OPENAI_API_KEY"],
["anthropic-prod", "ANTHROPIC_API_KEY"],
["gemini-api", "GEMINI_API_KEY"],
])(
"reuses the OpenShell gateway provider for %s when env var is missing (#3895)",
(provider, credentialEnv) => {
// #3895 fix extends gateway credential reuse to every remote provider
// that registers an OpenShell provider during onboard, not just NVIDIA.
const f = createFixture({
provider,
credentialEnv,
providerRegistered: true,
// no savedCredential
});

const result = runRebuild(f);
const output = (result.stderr || "") + (result.stdout || "");

expect(output).not.toContain("preflight failed");
expect(output).not.toContain(`Missing credential: ${credentialEnv}`);
expect(output).toContain("already registered in the OpenShell gateway");
expect(output).toContain("Backing up sandbox state");
},
60_000,
);

it(
"uses the registered Hermes Provider in OpenShell instead of requiring OPENAI_API_KEY",
{ timeout: 60_000 },
Expand Down Expand Up @@ -595,14 +651,17 @@ describe("Issue #2273: atomic rebuild", () => {
);

it(
"preflight failure exits non-zero when credential is missing",
"preflight failure exits non-zero when credential is missing and gateway has no provider",
{ timeout: 60_000 },
() => {
// Verifies that missing credentials cause rebuild to exit non-zero.
// This is the observable CLI behavior — the preflight check fails
// and bail() calls process.exit with a non-zero code.
// After #3895 the gateway-registered provider is reused if present,
// so this assertion only holds when the gateway also has nothing.
const f = createFixture({
credentialEnv: "NVIDIA_API_KEY",
providerRegistered: false,
// No credential — preflight will fail and exit non-zero
});

Expand Down
Loading