Skip to content
Merged
98 changes: 73 additions & 25 deletions test/e2e/fixtures/managed-image-receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
isShippedManagedImageAgent,
MANAGED_IMAGE_PLATFORMS,
MANAGED_IMAGE_REPOSITORIES,
managedImagePlatformForNodeArchitecture,
SHIPPED_MANAGED_IMAGE_AGENTS,
type ManagedImageContractV1,
type ManagedImagePlatform,
type ShippedManagedImageAgent,
} from "../../../src/lib/onboard/managed-image/contract.ts";
import { readManagedWorkloadAuthority } from "../../../src/lib/onboard/workload/authority.ts";
Expand Down Expand Up @@ -55,11 +57,19 @@ function selectedManagedImageRevision(environment: NodeJS.ProcessEnv): string {
return catalog.revision;
}

export function assertManagedImageReceiptMatchesSelectedCohort(options: {
interface SelectedManagedImageAuthority {
readonly platform: ManagedImagePlatform;
readonly reference: string;
readonly release: string | null;
readonly sourceCohort: string;
readonly sourceRevision: string;
}

function selectedManagedImageAuthority(options: {
readonly environment: NodeJS.ProcessEnv;
readonly expectedAgent: ShippedManagedImageAgent;
readonly workload?: Record<string, unknown>;
}): void {
readonly platform: unknown;
}): SelectedManagedImageAuthority {
const revision = options.environment.E2E_MANAGED_IMAGE_REVISION?.trim() ?? "";
const rawReceipt = options.environment.E2E_MANAGED_IMAGE_COHORT_RECEIPT?.trim() ?? "";
if (!revision) {
Expand All @@ -68,26 +78,23 @@ export function assertManagedImageReceiptMatchesSelectedCohort(options: {
"stock onboarding requires the complete selected managed-image cohort receipt",
);
}
const platform = options.workload?.platform;
if (
typeof platform !== "string" ||
!(MANAGED_IMAGE_PLATFORMS as readonly string[]).includes(platform)
typeof options.platform !== "string" ||
!(MANAGED_IMAGE_PLATFORMS as readonly string[]).includes(options.platform)
) {
throw new Error("stock onboarding candidate managed-image catalog is invalid");
}
const contract = readCandidateCatalog(options.environment).get(options.expectedAgent);
if (
!contract ||
contract.platform !== platform ||
options.workload?.kind !== "managed-image" ||
options.workload.reference !== contract.reference ||
options.workload.release !== contract.source.release ||
options.workload.sourceRevision !== contract.source.revision ||
options.workload.sourceCohort !== contract.source.cohort
) {
if (!contract || contract.platform !== options.platform) {
throw new Error("stock onboarding must use the exact agent image from the selected cohort");
}
return;
return {
platform: contract.platform,
reference: contract.reference,
release: contract.source.release,
sourceCohort: contract.source.cohort,
sourceRevision: contract.source.revision,
};
}
if (!REVISION_PATTERN.test(revision) || !rawReceipt || Buffer.byteLength(rawReceipt) > 8 * 1024) {
throw new Error("stock onboarding requires the complete selected managed-image cohort receipt");
Expand Down Expand Up @@ -123,11 +130,10 @@ export function assertManagedImageReceiptMatchesSelectedCohort(options: {
throw new Error("stock onboarding selected managed-image cohort receipt is invalid");
}

const platform = options.workload?.platform;
const agentImages = (images as Record<string, unknown>)[options.expectedAgent];
if (
typeof platform !== "string" ||
!(MANAGED_IMAGE_PLATFORMS as readonly string[]).includes(platform) ||
typeof options.platform !== "string" ||
!(MANAGED_IMAGE_PLATFORMS as readonly string[]).includes(options.platform) ||
!agentImages ||
typeof agentImages !== "object" ||
Array.isArray(agentImages) ||
Expand All @@ -136,14 +142,56 @@ export function assertManagedImageReceiptMatchesSelectedCohort(options: {
) {
throw new Error("stock onboarding selected managed-image cohort receipt is invalid");
}
const expectedReference = (agentImages as Record<string, unknown>)[platform];
const reference = (agentImages as Record<string, unknown>)[options.platform];
const referencePrefix = `${MANAGED_IMAGE_REPOSITORIES[options.expectedAgent]}@sha256:`;
if (
typeof reference !== "string" ||
!reference.startsWith(referencePrefix) ||
!/^[a-f0-9]{64}$/i.test(reference.slice(referencePrefix.length))
) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
throw new Error("stock onboarding must use the exact agent image from the selected cohort");
}
return {
platform: options.platform as ManagedImagePlatform,
reference,
release: null,
sourceCohort: cohort as string,
sourceRevision: revision,
};
}

/** Resolve the selected E2E image from its candidate catalog or publication receipt. */
export function selectedE2eManagedImageReference(options: {
readonly environment: NodeJS.ProcessEnv;
readonly expectedAgent: ShippedManagedImageAgent;
readonly nodeArchitecture?: string;
}): string {
const nodeArchitecture = options.nodeArchitecture ?? process.arch;
const platform = managedImagePlatformForNodeArchitecture(nodeArchitecture);
if (platform === null) {
throw new Error(
`selected E2E managed image does not support host architecture '${nodeArchitecture}'`,
);
}
return selectedManagedImageAuthority({ ...options, platform }).reference;
}

export function assertManagedImageReceiptMatchesSelectedCohort(options: {
readonly environment: NodeJS.ProcessEnv;
readonly expectedAgent: ShippedManagedImageAgent;
readonly workload?: Record<string, unknown>;
}): void {
const selected = selectedManagedImageAuthority({
environment: options.environment,
expectedAgent: options.expectedAgent,
platform: options.workload?.platform,
});
if (
typeof expectedReference !== "string" ||
!expectedReference.startsWith(`${MANAGED_IMAGE_REPOSITORIES[options.expectedAgent]}@sha256:`) ||
options.workload?.kind !== "managed-image" ||
options.workload.reference !== expectedReference ||
options.workload.sourceRevision !== revision ||
options.workload.sourceCohort !== cohort
options.workload.reference !== selected.reference ||
(selected.release !== null && options.workload.release !== selected.release) ||
options.workload.sourceRevision !== selected.sourceRevision ||
options.workload.sourceCohort !== selected.sourceCohort
) {
throw new Error("stock onboarding must use the exact agent image from the selected cohort");
}
Expand Down
79 changes: 46 additions & 33 deletions test/e2e/live/openclaw-inference-switch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ import type { AddressInfo } from "node:net";
import os from "node:os";
import path from "node:path";

import {
liveE2eManagedImageCatalog,
readLiveE2eManagedImageCatalogContracts,
} from "../../../src/lib/onboard/workload/preparation.ts";
import { execTimeout, testTimeout } from "../../helpers/timeouts.ts";
import type { ArtifactSink } from "../fixtures/artifacts.ts";
import { buildAvailabilityProbeEnv } from "../fixtures/availability-env.ts";
Expand All @@ -43,6 +39,7 @@ import {
startFakeOpenAiCompatibleServer,
} from "../fixtures/fake-openai-compatible.ts";
import { requireHostedInferenceConfig } from "../fixtures/hosted-inference.ts";
import { selectedE2eManagedImageReference } from "../fixtures/managed-image-receipt.ts";
import type { TestProgress } from "../fixtures/progress.ts";
import {
inferenceResponseModel,
Expand Down Expand Up @@ -99,6 +96,13 @@ interface AnthropicResponse {
content?: Array<{ text?: unknown }>;
}

interface OpenClawModelConfig {
id?: unknown;
name?: unknown;
contextWindow?: unknown;
maxTokens?: unknown;
}

interface OpenClawConfig {
agents?: {
defaults?: {
Expand All @@ -114,12 +118,7 @@ interface OpenClawConfig {
baseUrl?: unknown;
apiKey?: unknown;
api?: unknown;
models?: Array<{
id?: unknown;
name?: unknown;
contextWindow?: unknown;
maxTokens?: unknown;
}>;
models?: OpenClawModelConfig[];
}
>;
};
Expand Down Expand Up @@ -257,8 +256,10 @@ function commandEnv(home: string, extra: NodeJS.ProcessEnv = {}): NodeJS.Process

function writeCustomOpenClawDockerfile(home: string): string {
const environment = commandEnv(home);
const selectedCatalog = liveE2eManagedImageCatalog(environment)!;
const contract = readLiveE2eManagedImageCatalogContracts(selectedCatalog).get("openclaw")!;
const baseImage = selectedE2eManagedImageReference({
environment,
expectedAgent: "openclaw",
});

const buildContext = path.join(home, "custom-openclaw-image");
const dockerfilePath = path.join(buildContext, "Dockerfile");
Expand All @@ -276,7 +277,7 @@ function writeCustomOpenClawDockerfile(home: string): string {
fs.writeFileSync(
dockerfilePath,
[
`FROM ${contract.reference}`,
`FROM ${baseImage}`,
"ARG NEMOCLAW_TOOL_DISCLOSURE=progressive",
"ENV NEMOCLAW_TOOL_DISCLOSURE=${NEMOCLAW_TOOL_DISCLOSURE}",
`RUN node -e ${shellQuote(bakeRoute)}`,
Expand Down Expand Up @@ -592,17 +593,15 @@ async function assertRegistryAndSession(
}
}

async function assertOpenClawConfig(
async function readAndAssertOpenClawConfig(
sandbox: SandboxClient,
home: string,
expected: {
model: string;
inferenceApi: string;
artifactName: string;
contextWindow?: number | null;
maxTokens?: number | null;
},
): Promise<void> {
): Promise<OpenClawModelConfig | undefined> {
const configResult = await sandbox.exec(
SANDBOX_NAME,
["cat", "/sandbox/.openclaw/openclaw.json"],
Expand All @@ -629,18 +628,34 @@ async function assertOpenClawConfig(
expect(provider?.apiKey).toBe("unused");
expect(provider?.api).toBe(expected.inferenceApi);
expect(selectedModel?.name).toBe(expectedPrimary);
const expectedContextWindow =
expected.contextWindow === null
? undefined
: (expected.contextWindow ?? selectedModel?.contextWindow);
const expectedMaxTokens =
expected.maxTokens === null ? undefined : (expected.maxTokens ?? selectedModel?.maxTokens);
expect(selectedModel?.contextWindow).toBe(expectedContextWindow);
expect(selectedModel?.maxTokens).toBe(expectedMaxTokens);
expect(
expected.maxTokens === null ||
(typeof selectedModel?.maxTokens === "number" && selectedModel.maxTokens > 0),
).toBe(true);
return selectedModel;
}

async function assertOpenClawConfig(
sandbox: SandboxClient,
home: string,
expected: {
model: string;
inferenceApi: string;
artifactName: string;
},
): Promise<void> {
const selectedModel = await readAndAssertOpenClawConfig(sandbox, home, expected);
expect(typeof selectedModel?.maxTokens === "number" && selectedModel.maxTokens > 0).toBe(true);
}

async function assertInitialOpenClawConfig(
sandbox: SandboxClient,
home: string,
expected: {
model: string;
inferenceApi: string;
artifactName: string;
},
): Promise<void> {
const selectedModel = await readAndAssertOpenClawConfig(sandbox, home, expected);
expect(selectedModel?.contextWindow).toBeUndefined();
expect(selectedModel?.maxTokens).toBeUndefined();
}

function httpStatusFromResponse(response: string): string {
Expand Down Expand Up @@ -933,11 +948,9 @@ async function runInitialRouteLifecycle(options: {
sandbox: SandboxClient;
}): Promise<void> {
const verify = async (artifactSuffix: string): Promise<void> => {
await assertOpenClawConfig(options.sandbox, options.home, {
await assertInitialOpenClawConfig(options.sandbox, options.home, {
model: options.model,
inferenceApi: "openai-completions",
contextWindow: null,
maxTokens: null,
artifactName: `read-openclaw-initial-route-${artifactSuffix}`,
});
await checkOpenClawGatewayInference(
Expand Down Expand Up @@ -1130,7 +1143,6 @@ test(
);

const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-openclaw-switch-home-"));
const customDockerfile = writeCustomOpenClawDockerfile(home);
let mockProvider: MockAnthropicProvider | undefined;
cleanup.trackDisposable(
`remove OpenClaw inference switch test home for ${SANDBOX_NAME}`,
Expand All @@ -1144,6 +1156,7 @@ test(
cleanup.trackDisposable("close baseline inference provider", async () => {
await baselineProvider?.close();
});
const customDockerfile = writeCustomOpenClawDockerfile(home);
cleanup.trackGateway(host, "nemoclaw", {
artifactName: "cleanup-openshell-gateway-destroy-openclaw-inference-switch",
env: commandEnv(home),
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/mock-parity.json
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@
"test/e2e/live/openclaw-admin-scope.ts",
"test/e2e/fixtures/admin-approval-connect.ts",
"test/e2e/fixtures/admin-approval-connect.sh",
"test/e2e/fixtures/issue-4462-admin-approval-evidence.ts"
"test/e2e/fixtures/issue-4462-admin-approval-evidence.ts",
"test/e2e/fixtures/managed-image-receipt.ts"
],
"fast": [
"src/lib/actions/inference-set-compatible-provider.test.ts",
Expand All @@ -842,6 +843,7 @@
"test/e2e/support/compatible-anthropic-switch.test.ts",
"test/e2e/support/e2e-cleanup-resources.test.ts",
"test/e2e/support/e2e-clients.test.ts",
"test/e2e/support/managed-image-receipt.test.ts",
"test/e2e/support/openclaw-inference-switch-helpers.test.ts",
"test/security/admin-approval-helper.test.ts",
"test/e2e/support/hosted-inference.test.ts"
Expand Down
Loading
Loading