diff --git a/.github/workflows/openshell-sdk-package-pr.yaml b/.github/workflows/openshell-sdk-package-pr.yaml index c8f88a4872b..192571b510a 100644 --- a/.github/workflows/openshell-sdk-package-pr.yaml +++ b/.github/workflows/openshell-sdk-package-pr.yaml @@ -54,6 +54,7 @@ jobs: id: package env: NEMOCLAW_OPEN_SHELL_SDK_OUTPUT_DIRECTORY: ${{ runner.temp }}/openshell-sdk + NEMOCLAW_OPEN_SHELL_SDK_INCLUDE_REPLACEMENT: "1" NODE_AUTH_TOKEN: ${{ github.token }} run: | set -euo pipefail diff --git a/ci/reviewed-npm-audit.json b/ci/reviewed-npm-audit.json index 0c18030a4dd..5382ca350d8 100644 --- a/ci/reviewed-npm-audit.json +++ b/ci/reviewed-npm-audit.json @@ -11,6 +11,13 @@ "integrity": "sha512-dB4mLex23Pnw61caGMR2CMHQihy9bj7IK2elJJd718k3yevm+fOt/vG6dJg8/5us4la2BwcOdRwLvOia3tdwFw==", "tarballUrl": "https://npm.pkg.github.com/download/@nvidia/openshell-sdk/0.0.106/dc32180ba1d658fc4ec309bdf89d2b162196928d" }, + "sourceRegistryPackageReplacement": { + "artifactName": "nvidia-openshell-sdk-0.0.116.tgz", + "label": "OpenShell TypeScript SDK 0.0.116", + "packageSpec": "@nvidia/openshell-sdk@0.0.116", + "integrity": "sha512-MFnyX2vs47f/yVJRUXKmA7C6wZDH03ZGFFg+R5gZME+yMQDg9WF6G4Kdz/APM7nCUpaoGk2bwNEdUUmR60J54A==", + "tarballUrl": "https://npm.pkg.github.com/download/@nvidia/openshell-sdk/0.0.116/3380652847354127ed1bb8010490f8815040ada3" + }, "sourceNestedShrinkwrapPackages": ["@earendil-works/pi-coding-agent@0.80.6"], "sourceRegistryPackagesWithoutIntegrity": [ { diff --git a/scripts/audit-reviewed-npm-graph.mts b/scripts/audit-reviewed-npm-graph.mts index 352b3a1ee5f..80db38ae096 100755 --- a/scripts/audit-reviewed-npm-graph.mts +++ b/scripts/audit-reviewed-npm-graph.mts @@ -64,6 +64,7 @@ type AuditConfig = Readonly<{ severityThreshold: Severity; sourceNestedShrinkwrapPackages: readonly string[]; sourceRegistryPackage: SourceRegistryPackage; + sourceRegistryPackageReplacement?: SourceRegistryPackage; sourceRegistryPackagesWithoutIntegrity: readonly PackageWithoutIntegrity[]; }>; type ReviewedAuditReport = Readonly<{ @@ -93,6 +94,28 @@ const SOURCE_GRAPH = { id: "nemoclaw-cli", label: "NemoClaw CLI locked production graph", } as const; + +function isSourceRegistryPackage(value: unknown): value is SourceRegistryPackage { + if (typeof value !== "object" || value === null || Array.isArray(value)) return false; + const candidate = value as Partial; + return ( + typeof candidate.artifactName === "string" && + /^[a-z0-9][a-z0-9._-]*\.tgz$/.test(candidate.artifactName) && + typeof candidate.label === "string" && + candidate.label.length > 0 && + typeof candidate.packageSpec === "string" && + EXACT_NPM_PACKAGE_SPEC.test(candidate.packageSpec) && + typeof candidate.integrity === "string" && + candidate.integrity.length > 0 && + typeof candidate.tarballUrl === "string" && + candidate.tarballUrl.length > 0 + ); +} + +function exactPackageName(packageSpec: string): string { + const separator = packageSpec.lastIndexOf("@"); + return packageSpec.slice(0, separator); +} const OPENCLAW_DOMEXCEPTION_ALIAS = { actualName: "@nolyfill/domexception", aliasPackagePath: "node_modules/openclaw/node_modules/node-domexception", @@ -202,19 +225,15 @@ export function parseAuditConfig(contents: string): AuditConfig { ) || new Set(parsed.sourceRegistryPackagesWithoutIntegrity.map(({ packageSpec }) => packageSpec)) .size !== parsed.sourceRegistryPackagesWithoutIntegrity.length || - typeof parsed.sourceRegistryPackage !== "object" || - parsed.sourceRegistryPackage === null || - Array.isArray(parsed.sourceRegistryPackage) || - typeof parsed.sourceRegistryPackage.artifactName !== "string" || - !/^[a-z0-9][a-z0-9._-]*\.tgz$/.test(parsed.sourceRegistryPackage.artifactName) || - typeof parsed.sourceRegistryPackage.label !== "string" || - !parsed.sourceRegistryPackage.label || - typeof parsed.sourceRegistryPackage.packageSpec !== "string" || - !EXACT_NPM_PACKAGE_SPEC.test(parsed.sourceRegistryPackage.packageSpec) || - typeof parsed.sourceRegistryPackage.integrity !== "string" || - !parsed.sourceRegistryPackage.integrity || - typeof parsed.sourceRegistryPackage.tarballUrl !== "string" || - !parsed.sourceRegistryPackage.tarballUrl || + !isSourceRegistryPackage(parsed.sourceRegistryPackage) || + (parsed.sourceRegistryPackageReplacement !== undefined && + (!isSourceRegistryPackage(parsed.sourceRegistryPackageReplacement) || + exactPackageName(parsed.sourceRegistryPackageReplacement.packageSpec) !== + exactPackageName(parsed.sourceRegistryPackage.packageSpec) || + parsed.sourceRegistryPackageReplacement.packageSpec === + parsed.sourceRegistryPackage.packageSpec || + parsed.sourceRegistryPackageReplacement.artifactName === + parsed.sourceRegistryPackage.artifactName)) || parsed.lockedGraphs.some( (graph) => typeof graph.id !== "string" || @@ -472,26 +491,33 @@ export function materializeSourceGraph( sourceRegistryPackage?: ReviewedPackage, sourceNestedShrinkwrapPackages: readonly string[] = [], sourceRegistryPackagesWithoutIntegrity: readonly PackageWithoutIntegrity[] = [], + sourceRegistryPackageReplacements: readonly ReviewedPackage[] = [], ): string { assertRegularFile(sourcePackage, "NemoClaw CLI package manifest"); assertRegularFile(sourceLock, "NemoClaw CLI lockfile"); - verifyReviewedNpmLockPackages({ + const reviewedSourcePackages = [ + sourceRegistryPackage, + ...sourceRegistryPackageReplacements, + ].filter((reviewed): reviewed is ReviewedPackage => reviewed !== undefined); + const lockedPackages = verifyReviewedNpmLockPackages({ allowedNestedShrinkwrapPackages: sourceNestedShrinkwrapPackages, lockfilePath: sourceLock, omitDev: true, registryOrigin, - reviewedRegistryPackages: sourceRegistryPackage - ? [ - { - expectedIntegrity: sourceRegistryPackage.integrity, - label: sourceRegistryPackage.label, - packageSpec: sourceRegistryPackage.packageSpec, - tarballUrl: sourceRegistryPackage.tarballUrl, - }, - ] - : [], + reviewedRegistryPackages: reviewedSourcePackages.map((reviewed) => ({ + expectedIntegrity: reviewed.integrity, + label: reviewed.label, + packageSpec: reviewed.packageSpec, + tarballUrl: reviewed.tarballUrl, + })), reviewedPackagesWithoutIntegrity: sourceRegistryPackagesWithoutIntegrity, }); + if ( + reviewedSourcePackages.filter(({ packageSpec }) => lockedPackages.includes(packageSpec)) + .length > 1 + ) { + throw new Error("reviewed npm lock uses conflicting OpenShell SDK identities"); + } const lockSha256 = createHash("sha256").update(fs.readFileSync(sourceLock)).digest("hex"); fs.mkdirSync(destination); fs.copyFileSync(sourcePackage, path.join(destination, "package.json")); @@ -748,6 +774,7 @@ function auditSourceGraph( config.sourceRegistryPackage, config.sourceNestedShrinkwrapPackages, config.sourceRegistryPackagesWithoutIntegrity, + config.sourceRegistryPackageReplacement ? [config.sourceRegistryPackageReplacement] : [], ); return auditMaterializedSourceGraph({ directory, @@ -1008,10 +1035,7 @@ function isMainModule(): boolean { if ((error as NodeJS.ErrnoException).code === "ENOENT") return false; throw error; } - return ( - fs.realpathSync.native(fileURLToPath(import.meta.url)) === - invokedPath - ); + return fs.realpathSync.native(fileURLToPath(import.meta.url)) === invokedPath; } if (isMainModule()) { diff --git a/scripts/checks/extract-installer-pins.mts b/scripts/checks/extract-installer-pins.mts index 11b3d09da75..feadf27f861 100644 --- a/scripts/checks/extract-installer-pins.mts +++ b/scripts/checks/extract-installer-pins.mts @@ -499,10 +499,14 @@ const TRUSTED_OPENSHELL_RELEASES: readonly OpenShellReleaseTrust[] = [ url: "https://github.com/NVIDIA/OpenShell/releases/download/v0.0.116/openshell.rb", }, // The v0.0.116 release publishes only MUSL standalone sandbox archives. - // Trust only the prospective installer template whose stable Linux path - // selects that ABI. The v0.0.106 record retains the active GNU templates. + // Trust only the reviewed prospective installer templates whose stable + // Linux path selects that ABI. The second digest is the exact full-cutover + // template consumed by #11251; keeping this approval in a separate base + // prerequisite prevents the activation PR from authorizing its own bytes. + // The v0.0.106 record retains the active GNU templates. installerTemplateSha256: [ "243f607a1b9a67c116f80844d5cd6e7185d63537e74fb08b3994652f79cb00e9", + "2b6ad3e0730d3220da05d13b88fdba4458de46840bad57942ecad26a5d606017", ], manifests: [ { @@ -535,6 +539,8 @@ const TRUSTED_OPENSHELL_RELEASES: readonly OpenShellReleaseTrust[] = [ runtimeTemplateSha256: [ "c1922eaa4f73c1a05aa8bccf50fc40208d7f71db0e6c110dcd09d0372d1aa068", "abfc1337284d437e71e47945936af7ef0bc6f28ac2495e12fac41894eb24ce3c", + // Exact #11251 template after the reviewed 0.0.116-only recovery gate. + "593ced09573f8cea5d2323b6d388ebb5d30f6da241d4f511e5364a3057887911", ], }, pinLayout: V00116_OPENSHELL_PIN_LAYOUT, diff --git a/scripts/checks/package-openshell-sdk-for-pr.mts b/scripts/checks/package-openshell-sdk-for-pr.mts index 9fe1787d4a5..098667020ea 100755 --- a/scripts/checks/package-openshell-sdk-for-pr.mts +++ b/scripts/checks/package-openshell-sdk-for-pr.mts @@ -11,30 +11,50 @@ import { packReviewedNpmArchive, removeReviewedNpmArchive } from "../lib/reviewe const TRUSTED_REPOSITORY_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); -export function packageReviewedOpenShellSdk(outputDirectory: string): string { +export function packageReviewedOpenShellSdk( + outputDirectory: string, + includeReplacement = false, + dependencies: Readonly<{ + pack?: typeof packReviewedNpmArchive; + readAuditConfig?: () => string; + remove?: typeof removeReviewedNpmArchive; + }> = {}, +): string { if (!outputDirectory) { throw new Error("reviewed OpenShell SDK output directory is required"); } const config = parseAuditConfig( - readFileSync(join(TRUSTED_REPOSITORY_ROOT, "ci/reviewed-npm-audit.json"), "utf8"), + dependencies.readAuditConfig?.() ?? + readFileSync(join(TRUSTED_REPOSITORY_ROOT, "ci/reviewed-npm-audit.json"), "utf8"), ); - const reviewed = config.sourceRegistryPackage; - const archive = packReviewedNpmArchive({ - env: process.env, - expectedIntegrity: reviewed.integrity, - label: reviewed.label, - packageSpec: reviewed.packageSpec, - tarballUrl: reviewed.tarballUrl, - }); + if (includeReplacement && !config.sourceRegistryPackageReplacement) { + throw new Error("reviewed OpenShell SDK replacement metadata is required"); + } + const reviewedPackages = + includeReplacement && config.sourceRegistryPackageReplacement + ? [config.sourceRegistryPackage, config.sourceRegistryPackageReplacement] + : [config.sourceRegistryPackage]; + const archives: ReturnType[] = []; + const pack = dependencies.pack ?? packReviewedNpmArchive; + const remove = dependencies.remove ?? removeReviewedNpmArchive; const output = resolve(outputDirectory); try { rmSync(output, { force: true, recursive: true }); mkdirSync(output, { recursive: true }); - const artifact = join(output, reviewed.artifactName); - copyFileSync(archive.archivePath, artifact); - return artifact; + for (const reviewed of reviewedPackages) { + const archive = pack({ + env: process.env, + expectedIntegrity: reviewed.integrity, + label: reviewed.label, + packageSpec: reviewed.packageSpec, + tarballUrl: reviewed.tarballUrl, + }); + archives.push(archive); + copyFileSync(archive.archivePath, join(output, reviewed.artifactName)); + } + return reviewedPackages.length === 1 ? join(output, reviewedPackages[0]!.artifactName) : output; } finally { - removeReviewedNpmArchive(archive); + for (const archive of archives) remove(archive); } } @@ -44,8 +64,15 @@ if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1] console.error("NEMOCLAW_OPEN_SHELL_SDK_OUTPUT_DIRECTORY is required"); process.exit(1); } + const includeReplacementValue = process.env.NEMOCLAW_OPEN_SHELL_SDK_INCLUDE_REPLACEMENT; + if (includeReplacementValue !== undefined && includeReplacementValue !== "1") { + console.error("NEMOCLAW_OPEN_SHELL_SDK_INCLUDE_REPLACEMENT must be 1 when set"); + process.exit(1); + } try { - process.stdout.write(`${packageReviewedOpenShellSdk(outputDirectory)}\n`); + process.stdout.write( + `${packageReviewedOpenShellSdk(outputDirectory, includeReplacementValue === "1")}\n`, + ); } catch (error) { console.error(error instanceof Error ? error.message : String(error)); process.exit(1); diff --git a/scripts/checks/prepare-ci-npm-install.mts b/scripts/checks/prepare-ci-npm-install.mts index 369857dfbe9..2b7b4f0ffb4 100755 --- a/scripts/checks/prepare-ci-npm-install.mts +++ b/scripts/checks/prepare-ci-npm-install.mts @@ -189,16 +189,22 @@ function readTrustedAuditConfig(): AuditConfig { ); } +function reviewedSourceRegistryPackages( + config: AuditConfig, +): readonly ReviewedSourceRegistryPackage[] { + return config.sourceRegistryPackageReplacement + ? [config.sourceRegistryPackage, config.sourceRegistryPackageReplacement] + : [config.sourceRegistryPackage]; +} + function inspectReviewedLocks(targetRoot: string, config: AuditConfig) { - const reviewed = config.sourceRegistryPackage; - const reviewedRegistryPackages = [ - { - expectedIntegrity: reviewed.integrity, - label: reviewed.label, - packageSpec: reviewed.packageSpec, - tarballUrl: reviewed.tarballUrl, - }, - ]; + const reviewedPackages = reviewedSourceRegistryPackages(config); + const reviewedRegistryPackages = reviewedPackages.map((reviewed) => ({ + expectedIntegrity: reviewed.integrity, + label: reviewed.label, + packageSpec: reviewed.packageSpec, + tarballUrl: reviewed.tarballUrl, + })); const lockfiles = ["package-lock.json", "nemoclaw/package-lock.json"].map((relativePath) => { const lockfilePath = join(targetRoot, relativePath); const packages = verifyReviewedNpmLockPackages({ @@ -210,6 +216,20 @@ function inspectReviewedLocks(targetRoot: string, config: AuditConfig) { }); return { lockfilePath, packages }; }); + const lockedReviewedSpecs = new Set( + lockfiles.flatMap(({ packages }) => + reviewedPackages + .map(({ packageSpec }) => packageSpec) + .filter((packageSpec) => packages.includes(packageSpec)), + ), + ); + if (lockedReviewedSpecs.size > 1) { + throw new Error("reviewed npm locks use conflicting OpenShell SDK identities"); + } + const selectedSpec = [...lockedReviewedSpecs][0]; + const reviewed = + reviewedPackages.find(({ packageSpec }) => packageSpec === selectedSpec) ?? + config.sourceRegistryPackage; return { config, reviewed, diff --git a/test/automation/pull-requests/pr-workflow-contract.test.ts b/test/automation/pull-requests/pr-workflow-contract.test.ts index 238334711aa..1099345de3c 100644 --- a/test/automation/pull-requests/pr-workflow-contract.test.ts +++ b/test/automation/pull-requests/pr-workflow-contract.test.ts @@ -466,9 +466,7 @@ describe("pull request and main workflow contracts", () => { ) as TypeScriptConfig; const sharedActions = { staticChecks: readYaml(".github/actions/ci-static-checks/action.yaml"), - compileArtifacts: readYaml( - ".github/actions/ci-compile-artifacts/action.yaml", - ), + compileArtifacts: readYaml(".github/actions/ci-compile-artifacts/action.yaml"), buildTypecheck: readYaml(".github/actions/ci-build-typecheck/action.yaml"), cliCoverageShard: readYaml( ".github/actions/ci-cli-coverage-shard/action.yaml", @@ -493,10 +491,7 @@ describe("pull request and main workflow contracts", () => { it("verifies changed Hugging Face catalog references without credentials", () => { const job = prWorkflow.jobs["hugging-face-models"]; const filterStep = prWorkflow.jobs.changes.steps?.find((step) => step.id === "filter"); - const filters = YAML.parse(String(filterStep?.with?.filters ?? "")) as Record< - string, - string[] - >; + const filters = YAML.parse(String(filterStep?.with?.filters ?? "")) as Record; const huggingFaceModelFilters = filters.hugging_face_models ?? []; expect( @@ -922,6 +917,7 @@ describe("pull request and main workflow contracts", () => { ); expect(fetch.env).toEqual({ NEMOCLAW_OPEN_SHELL_SDK_OUTPUT_DIRECTORY: "${{ runner.temp }}/openshell-sdk", + NEMOCLAW_OPEN_SHELL_SDK_INCLUDE_REPLACEMENT: "1", NODE_AUTH_TOKEN: "${{ github.token }}", }); expect(fetch.run).toContain( diff --git a/test/automation/releases/reviewed-npm-audit-workflow.test.ts b/test/automation/releases/reviewed-npm-audit-workflow.test.ts index 583c0f7c5a7..ece41db6c46 100644 --- a/test/automation/releases/reviewed-npm-audit-workflow.test.ts +++ b/test/automation/releases/reviewed-npm-audit-workflow.test.ts @@ -606,7 +606,7 @@ describe("trusted reviewed npm audit workflow (#5896)", () => { ); }); - // source-shape-contract: security -- One reviewed package field prevents a second package identity from bypassing the credential-isolation workflow + // source-shape-contract: security -- One active package plus one same-package replacement prevents an open-ended identity list from bypassing the credential-isolation workflow it("rejects the removed plural source-registry package shape", () => { const configFile = path.join(REPO_ROOT, "ci", "reviewed-npm-audit.json"); const config = JSON.parse(fs.readFileSync(configFile, "utf-8")) as Record; diff --git a/test/repository/package-openshell-sdk-for-pr.test.ts b/test/repository/package-openshell-sdk-for-pr.test.ts new file mode 100644 index 00000000000..399c59ff087 --- /dev/null +++ b/test/repository/package-openshell-sdk-for-pr.test.ts @@ -0,0 +1,80 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +import { afterEach, describe, expect, it, vi } from "vitest"; + +import { packageReviewedOpenShellSdk } from "../../scripts/checks/package-openshell-sdk-for-pr.mts"; +import type { ReviewedNpmArchiveRequest } from "../../scripts/lib/reviewed-npm-archive.mts"; + +const roots: string[] = []; + +afterEach(() => { + for (const root of roots.splice(0)) fs.rmSync(root, { force: true, recursive: true }); +}); + +function fixture() { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-sdk-package-transition-")); + roots.push(root); + const source = path.join(root, "source"); + const output = path.join(root, "output"); + fs.mkdirSync(source); + const requests: ReviewedNpmArchiveRequest[] = []; + const pack = vi.fn((request: ReviewedNpmArchiveRequest) => { + requests.push(request); + const archivePath = path.join(source, `${String(requests.length)}.tgz`); + fs.writeFileSync(archivePath, request.packageSpec); + return { archivePath, rootDirectory: source }; + }); + const remove = vi.fn(); + return { output, pack, remove, requests }; +} + +describe("reviewed OpenShell SDK transition packaging", () => { + it("fails closed when replacement packaging lacks replacement metadata", () => { + const source = fixture(); + const configPath = path.resolve("ci/reviewed-npm-audit.json"); + const config = JSON.parse(fs.readFileSync(configPath, "utf8")) as Record; + delete config.sourceRegistryPackageReplacement; + + expect(() => + packageReviewedOpenShellSdk(source.output, true, { + ...source, + readAuditConfig: () => JSON.stringify(config), + }), + ).toThrow("reviewed OpenShell SDK replacement metadata is required"); + expect(source.pack).not.toHaveBeenCalled(); + }); + + it("keeps the default package result to the active SDK archive", () => { + const source = fixture(); + + const artifact = packageReviewedOpenShellSdk(source.output, false, source); + + expect(path.basename(artifact)).toBe("nvidia-openshell-sdk-0.0.106.tgz"); + expect(source.requests.map(({ packageSpec }) => packageSpec)).toEqual([ + "@nvidia/openshell-sdk@0.0.106", + ]); + expect(source.remove).toHaveBeenCalledOnce(); + }); + + it("packages exactly the active and replacement identities for PR selection", () => { + const source = fixture(); + + const artifactDirectory = packageReviewedOpenShellSdk(source.output, true, source); + + expect(artifactDirectory).toBe(source.output); + expect(fs.readdirSync(artifactDirectory).sort()).toEqual([ + "nvidia-openshell-sdk-0.0.106.tgz", + "nvidia-openshell-sdk-0.0.116.tgz", + ]); + expect(source.requests.map(({ packageSpec }) => packageSpec)).toEqual([ + "@nvidia/openshell-sdk@0.0.106", + "@nvidia/openshell-sdk@0.0.116", + ]); + expect(source.remove).toHaveBeenCalledTimes(2); + }); +}); diff --git a/test/repository/prepare-ci-npm-install.test.ts b/test/repository/prepare-ci-npm-install.test.ts index bfdee3c9e87..ac28f89ccda 100644 --- a/test/repository/prepare-ci-npm-install.test.ts +++ b/test/repository/prepare-ci-npm-install.test.ts @@ -34,6 +34,13 @@ const reviewed: ReviewedSourceRegistryPackage = { packageSpec: "@nvidia/openshell-sdk@0.0.106", tarballUrl: "https://npm.pkg.github.com/download/@nvidia/openshell-sdk/0.0.106/reviewed-fixture", }; +const replacement: ReviewedSourceRegistryPackage = { + ...reviewed, + artifactName: "nvidia-openshell-sdk-0.0.116.tgz", + label: "OpenShell TypeScript SDK 0.0.116", + packageSpec: "@nvidia/openshell-sdk@0.0.116", + tarballUrl: "https://npm.pkg.github.com/download/@nvidia/openshell-sdk/0.0.116/reviewed-fixture", +}; type CacheStageRequest = Readonly<{ archive: Buffer; @@ -46,15 +53,18 @@ function cacheStageMock() { } function reviewedLock(packageIdentity: ReviewedSourceRegistryPackage = reviewed) { + const version = packageIdentity.packageSpec.slice( + packageIdentity.packageSpec.lastIndexOf("@") + 1, + ); return { lockfileVersion: 3, name: "reviewed-sdk-artifact-fixture", packages: { - "": { dependencies: { "@nvidia/openshell-sdk": "0.0.106" } }, + "": { dependencies: { "@nvidia/openshell-sdk": version } }, "node_modules/@nvidia/openshell-sdk": { integrity: packageIdentity.integrity, resolved: packageIdentity.tarballUrl, - version: "0.0.106", + version, }, }, version: "1.0.0", @@ -70,7 +80,10 @@ function publicLock() { }; } -function reviewedConfigSource(packageIdentity: ReviewedSourceRegistryPackage = reviewed) { +function reviewedConfigSource( + packageIdentity: ReviewedSourceRegistryPackage = reviewed, + replacementIdentity?: ReviewedSourceRegistryPackage, +) { return JSON.stringify({ archiveGraphId: "reviewed-archive-graph", archivePackages: [], @@ -87,11 +100,12 @@ function reviewedConfigSource(packageIdentity: ReviewedSourceRegistryPackage = r severityThreshold: "high", sourceNestedShrinkwrapPackages: [], sourceRegistryPackage: packageIdentity, + ...(replacementIdentity ? { sourceRegistryPackageReplacement: replacementIdentity } : {}), sourceRegistryPackagesWithoutIntegrity: [], }); } -function fixture() { +function fixture(packageIdentity: ReviewedSourceRegistryPackage = reviewed) { const root = mkdtempSync(join(tmpdir(), "nemoclaw-reviewed-sdk-artifact-")); temporaryRoots.push(root); const artifactDirectory = join(root, "artifact"); @@ -99,8 +113,8 @@ function fixture() { const lockfilePath = join(root, "package-lock.json"); mkdirSync(artifactDirectory); mkdirSync(cacheDirectory); - writeFileSync(join(artifactDirectory, artifactName), archiveBytes); - writeFileSync(lockfilePath, JSON.stringify(reviewedLock())); + writeFileSync(join(artifactDirectory, packageIdentity.artifactName), archiveBytes); + writeFileSync(lockfilePath, JSON.stringify(reviewedLock(packageIdentity))); return { artifactDirectory, cacheDirectory, lockfilePath, root }; } @@ -108,7 +122,7 @@ function installFixture( reviewedLocation: "root" | "nemoclaw", packageIdentity: ReviewedSourceRegistryPackage = reviewed, ) { - const source = fixture(); + const source = fixture(packageIdentity); const nestedRoot = join(source.root, "nemoclaw"); mkdirSync(nestedRoot); writeFileSync( @@ -272,6 +286,51 @@ describe("trusted OpenShell SDK archive preparation", () => { expect(stage).toHaveBeenCalledOnce(); }); + it("selects and stages one base-approved replacement SDK identity", async () => { + const source = installFixture("root", replacement); + const stage = cacheStageMock(); + + await prepareCiNpmInstallWithReviewedConfig( + installRequest(source, "artifact"), + reviewedConfigSource(reviewed, replacement), + stage, + ); + + expect(stage).toHaveBeenCalledOnce(); + expect(stage.mock.calls[0]?.[0]).toMatchObject({ artifactName: replacement.artifactName }); + }); + + it("rejects a candidate that mixes active and replacement SDK identities", async () => { + const source = installFixture("root", reviewed); + writeFileSync( + join(source.root, "nemoclaw", "package-lock.json"), + JSON.stringify(reviewedLock(replacement)), + ); + + await expect( + prepareCiNpmInstallWithReviewedConfig( + installRequest(source, "registry"), + reviewedConfigSource(reviewed, replacement), + ), + ).rejects.toThrow("reviewed npm locks use conflicting OpenShell SDK identities"); + }); + + it.each([ + ["a different package", { packageSpec: "@example/other-sdk@0.0.116" }], + ["the active package version", { packageSpec: reviewed.packageSpec }], + ["the active artifact name", { artifactName: reviewed.artifactName }], + ])("rejects replacement trust for %s", async (_case, replacementPatch) => { + const source = installFixture("root"); + const invalidReplacement = { ...replacement, ...replacementPatch }; + + await expect( + prepareCiNpmInstallWithReviewedConfig( + installRequest(source, "registry"), + reviewedConfigSource(reviewed, invalidReplacement), + ), + ).rejects.toThrow("ci/reviewed-npm-audit.json is invalid"); + }); + it("uses registry mode without requiring or caching an archive", async () => { const source = installFixture("root"); const stage = cacheStageMock();