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
22 changes: 11 additions & 11 deletions src/lib/actions/sandbox/rebuild-credential-preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ const hermesProviderAuth = require("../../hermes-provider-auth") as {
HERMES_PROVIDER_NAME: string;
HERMES_INFERENCE_CREDENTIAL_ENV: string;
HERMES_NOUS_API_KEY_CREDENTIAL_ENV: string;
inspectHermesProviderBinding: (runOpenshellFn: typeof runOpenshell) => {
inspectHermesProviderBinding: (runOpenshellFn: typeof runOpenshell) => Promise<{
exists: boolean;
credentialKeys: string[] | null;
};
}>;
registerHermesInferenceProvider: (
apiKey: string,
runOpenshellFn: typeof runOpenshell,
credentialEnv?: string,
baseUrl?: string,
) => void;
) => Promise<void>;
};

export type RebuildBail = (message: string, code?: number) => never;
Expand Down Expand Up @@ -62,19 +62,19 @@ function nonEmptyString(value: unknown): string | null {
return normalized || null;
}

function preflightHermesProviderCredentials(
async function preflightHermesProviderCredentials(
persistedAuthMethod: unknown,
credentialEnv: string | null,
log: RebuildLog,
): boolean {
): Promise<boolean> {
const authMethod =
normalizeHermesRebuildAuthMethod(persistedAuthMethod) ||
(credentialEnv === hermesProviderAuth.HERMES_NOUS_API_KEY_CREDENTIAL_ENV ? "api_key" : null);
const expectedCredentialEnv =
authMethod === "api_key"
? hermesProviderAuth.HERMES_NOUS_API_KEY_CREDENTIAL_ENV
: hermesProviderAuth.HERMES_INFERENCE_CREDENTIAL_ENV;
const binding = hermesProviderAuth.inspectHermesProviderBinding(runOpenshell);
const binding = await hermesProviderAuth.inspectHermesProviderBinding(runOpenshell);

if (binding.exists) {
const matches =
Expand Down Expand Up @@ -107,12 +107,12 @@ function preflightHermesProviderCredentials(
console.log(
" Hermes Provider is not registered in OpenShell; registering it from the configured exported API-key environment variable before rebuild.",
);
hermesProviderAuth.registerHermesInferenceProvider(
await hermesProviderAuth.registerHermesInferenceProvider(
envKey,
runOpenshell,
hermesProviderAuth.HERMES_NOUS_API_KEY_CREDENTIAL_ENV,
);
const registered = hermesProviderAuth.inspectHermesProviderBinding(runOpenshell);
const registered = await hermesProviderAuth.inspectHermesProviderBinding(runOpenshell);
return (
registered.credentialKeys?.length === 1 &&
registered.credentialKeys[0] === hermesProviderAuth.HERMES_NOUS_API_KEY_CREDENTIAL_ENV
Expand Down Expand Up @@ -146,17 +146,17 @@ function preflightHermesProviderCredentials(
return false;
}

export function preflightRebuildCredentials(
export async function preflightRebuildCredentials(
sb: RebuildSandboxEntry,
log: RebuildLog,
bail: RebuildBail,
options: RebuildCredentialPreflightOptions = {},
): boolean {
): Promise<boolean> {
const rebuildCredentialEnv = getRebuildCredentialEnvFromRegistry(sb.provider, sb.credentialEnv);
const rebuildProvider = sb.provider;

if (rebuildProvider === hermesProviderAuth.HERMES_PROVIDER_NAME) {
if (!preflightHermesProviderCredentials(sb.hermesAuthMethod, rebuildCredentialEnv, log)) {
if (!(await preflightHermesProviderCredentials(sb.hermesAuthMethod, rebuildCredentialEnv, log))) {
bail("Missing Hermes Provider credentials");
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/actions/sandbox/rebuild-dcode-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type DcodeRebuildOrchestratorDeps = {
entry: RebuildSandboxEntry,
log: (message: string) => void,
bail: DcodeRebuildPreflightBail,
): boolean;
): boolean | Promise<boolean>;
ensureAgentBaseImage(
agentName: string | null,
bail: DcodeRebuildPreflightBail,
Expand Down
11 changes: 7 additions & 4 deletions src/lib/actions/sandbox/rebuild-local-provider-recreate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ const onboardProviders = requireDist("../../onboard/providers.js") as {
baseUrl: string | null,
env: NodeJS.ProcessEnv,
runOpenshell: typeof openshellRuntime.runOpenshell,
): { ok: boolean; status?: number; message?: string };
): Promise<{ ok: boolean; status?: number; message?: string }>;
};

type SetupResult = { done: true; result: unknown } | { done: false };

function upsertLocalProvider(
async function upsertLocalProvider(
name: string,
type: string,
credentialEnv: string,
baseUrl: string | null,
env: NodeJS.ProcessEnv = {},
) {
return onboardProviders.upsertProvider(
return await onboardProviders.upsertProvider(
name,
type,
credentialEnv,
Expand Down Expand Up @@ -174,7 +174,10 @@ describe("rebuild local-provider recreation", () => {
: {
status: args[0] === "provider" && args[1] === "get" ? 1 : 0,
stdout: "",
stderr: "",
stderr:
args[0] === "provider" && args[1] === "get"
? `provider '${provider}' not found`
: "",
};
});
const liveSource = "Name: alpha\nId: sbx-alpha-source\nPhase: Ready\n";
Expand Down
4 changes: 2 additions & 2 deletions src/lib/actions/sandbox/rebuild-target-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export async function preflightRebuildTargetRuntime(
// rows may recover provider/model from their own matching onboard session;
// checking the raw row first would miss that remote credential requirement.
if (
!preflightRebuildCredentials(
!(await preflightRebuildCredentials(
{
...sb,
provider: target.resumeConfig.provider,
Expand All @@ -268,7 +268,7 @@ export async function preflightRebuildTargetRuntime(
requiresGatewayProviderReconfigure = true;
},
},
)
))
) {
return { ok: false };
}
Expand Down
49 changes: 33 additions & 16 deletions src/lib/hermes-provider-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,39 @@ afterEach(() => {
});

describe("Hermes provider OpenShell credential handoff", () => {
it("inspects exact OpenShell credential key bindings without exposing values", () => {
it("inspects exact OpenShell credential key bindings without exposing values", async () => {
const auth = loadAuth();
const binding = auth.inspectHermesProviderBinding(() => ({
const binding = await auth.inspectHermesProviderBinding(() => ({
status: 0,
stdout: "Provider:\n\n Name: hermes-provider\n Credential keys: NOUS_API_KEY\n",
stdout:
"Name: hermes-provider\nType: openai\nCredential keys: NOUS_API_KEY\nConfig keys: OPENAI_BASE_URL\n",
stderr: "",
}));
expect(binding).toEqual({ exists: true, credentialKeys: ["NOUS_API_KEY"] });
});

it("fails closed when OpenShell provider details omit credential metadata", () => {
it("fails closed when OpenShell provider details omit credential metadata", async () => {
const auth = loadAuth();
expect(
await expect(
auth.inspectHermesProviderBinding(() => ({ status: 0, stdout: "Provider: exists" })),
).toEqual({ exists: true, credentialKeys: null });
).resolves.toEqual({ exists: true, credentialKeys: null });
});

it("imports the OpenAI profile before Hermes credential registration (#10155)", () => {
it("imports the OpenAI profile before Hermes credential registration (#10155)", async () => {
const auth = loadAuth();
const runOpenshell = vi
.fn()
.mockReturnValueOnce({ status: 1, stdout: "", stderr: "provider profile not found" })
.mockReturnValueOnce({ status: 0, stdout: "Imported", stderr: "" })
.mockReturnValueOnce({ status: 0, stdout: EXACT_OPENAI_PROFILE, stderr: "" })
.mockReturnValueOnce({ status: 1, stdout: "", stderr: "provider not found" })
.mockReturnValueOnce({
status: 1,
stdout: "",
stderr: "provider 'hermes-provider' not found",
})
.mockReturnValueOnce({ status: 0, stdout: "", stderr: "" });

auth.registerHermesInferenceProvider("nous-key", runOpenshell);
await auth.registerHermesInferenceProvider("nous-key", runOpenshell);

expect(runOpenshell.mock.calls.map(([args]) => args)).toEqual([
["provider", "profile", "export", "openai", "--output", "json"],
Expand All @@ -96,7 +101,7 @@ describe("Hermes provider OpenShell credential handoff", () => {
});
});

it("rejects an incompatible OpenAI profile before Hermes credential mutation (#10155)", () => {
it("rejects an incompatible OpenAI profile before Hermes credential mutation (#10155)", async () => {
const auth = loadAuth();
const secret = "nous-incompatible-secret";
const runOpenshell = vi.fn().mockReturnValueOnce({
Expand All @@ -113,7 +118,7 @@ describe("Hermes provider OpenShell credential handoff", () => {

let thrown: unknown;
try {
auth.registerHermesInferenceProvider(secret, runOpenshell);
await auth.registerHermesInferenceProvider(secret, runOpenshell);
} catch (error) {
thrown = error;
}
Expand All @@ -123,7 +128,7 @@ describe("Hermes provider OpenShell credential handoff", () => {
expect(runOpenshell).toHaveBeenCalledOnce();
});

it("suppresses failed OpenAI profile import output before Hermes credential mutation (#10155)", () => {
it("suppresses failed OpenAI profile import output before Hermes credential mutation (#10155)", async () => {
const auth = loadAuth();
const secret = "profile-import-secret";
const runOpenshell = vi
Expand All @@ -133,7 +138,7 @@ describe("Hermes provider OpenShell credential handoff", () => {

let thrown: unknown;
try {
auth.registerHermesInferenceProvider("nous-key", runOpenshell);
await auth.registerHermesInferenceProvider("nous-key", runOpenshell);
} catch (error) {
thrown = error;
}
Expand All @@ -158,7 +163,11 @@ describe("Hermes provider OpenShell credential handoff", () => {
return args[1] === "profile"
? { status: 0, stdout: EXACT_OPENAI_PROFILE, stderr: "" }
: args[1] === "get"
? { status: 1, stdout: "", stderr: "" }
? {
status: 1,
stdout: "",
stderr: "provider 'hermes-provider' not found",
}
: { status: 0, stdout: "", stderr: "" };
},
});
Expand Down Expand Up @@ -233,7 +242,11 @@ describe("Hermes provider OpenShell credential handoff", () => {
return args[1] === "profile"
? { status: 0, stdout: EXACT_OPENAI_PROFILE, stderr: "" }
: args[1] === "get"
? { status: 1, stdout: "", stderr: "" }
? {
status: 1,
stdout: "",
stderr: "provider 'hermes-provider' not found",
}
: { status: 0, stdout: "", stderr: "" };
},
});
Expand Down Expand Up @@ -317,7 +330,11 @@ describe("Hermes provider OpenShell credential handoff", () => {
return args[1] === "profile"
? { status: 0, stdout: EXACT_OPENAI_PROFILE, stderr: "" }
: args[1] === "get"
? { status: 1, stdout: "", stderr: "" }
? {
status: 1,
stdout: "",
stderr: "provider 'hermes-provider' not found",
}
: { status: 0, stdout: "", stderr: "" };
},
toolGatewayPresets: ["nous-web", "nous-audio"],
Expand Down
Loading
Loading