#1 Persistent memory for AI coding agents based on real-world benchmarks
TypeScript agentmemoryagentsaiclaudeclaudecodecodexcopilotcursorgenaiharnesshermesmemoryompopenclawopencodepi
fix(release): verify gated npm artifacts and provenance (#1561)
Rohit Ghumare3a6758ac8d21
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.ymlindex 00003399..af230453 100644--- a/.github/workflows/publish.yml+++ b/.github/workflows/publish.yml@@ -6,119 +6,59 @@ on:workflow_dispatch:inputs:packages:- description: "Packages to publish (comma-separated: agentmemory,mcp,fs-watcher)"+ description: "Packages to publish: agentmemory,mcp; add fs-watcher only for its independent release"required: false- default: "agentmemory,mcp,fs-watcher"+ default: "agentmemory,mcp"-# Workflow-level permissions stay minimal — only `contents: read`-# is required to check out the repo. `id-token: write` is granted on-# the publish job for npm's --provenance Sigstore OIDC mint.permissions:contents: read+concurrency:+ group: npm-publication+ cancel-in-progress: false+jobs:publish:runs-on: ubuntu-latest+ timeout-minutes: 45permissions:contents: readid-token: writesteps:- uses: actions/checkout@v6with:- # Don't persist the GITHUB_TOKEN to .git/config — the- # publish steps don't push back to the repo, so the token- # only needs to live in memory for this checkout.persist-credentials: false- uses: actions/setup-node@v6with:- node-version: 22+ node-version: 24registry-url: https://registry.npmjs.org+ package-manager-cache: false- # Two-step install: generate a lockfile in-runner with- # --package-lock-only, then install from it with `npm ci`. Gives a- # single deterministic dep graph across build / test / publish- # within one job — important because publish uses `--provenance`.- # Lockfiles are gitignored at the repo level.- - run: npm install --package-lock-only --legacy-peer-deps --no-audit --no-fund- - run: npm ci --legacy-peer-deps --no-audit --no-fund+ - run: npm install --global [email protected] --ignore-scripts --no-audit --no-fund+ - run: npm install --package-lock-only --ignore-scripts --legacy-peer-deps --no-audit --no-fund+ - run: npm ci --ignore-scripts --legacy-peer-deps --no-audit --no-fund- run: npm run build+ - run: npm run skills:check+ - run: npm run docs:check- run: npm test- - name: Publish @agentmemory/agentmemory- run: |- if npm view "@agentmemory/agentmemory@$(node -p "require('./package.json').version")" version >/dev/null 2>&1; then- echo "Version already published, skipping"- else- npm publish --provenance --access public- fi- env:- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}-- - name: Wait for npm registry propagation- run: |- VERSION=$(node -p "require('./package.json').version")- for i in $(seq 1 24); do- if npm view "@agentmemory/agentmemory@$VERSION" version >/dev/null 2>&1; then- echo "Registry propagated after ${i} attempt(s)"- exit 0- fi- echo "Attempt $i: not yet available, sleeping 5s..."- sleep 5- done- echo "ERROR: registry never propagated after 2 minutes" >&2- exit 1-- - name: Publish @agentmemory/mcp shim- working-directory: packages/mcp- run: |- SHIM_VERSION=$(node -p "require('./package.json').version")- if npm view "@agentmemory/mcp@$SHIM_VERSION" version >/dev/null 2>&1; then- echo "Shim version already published, skipping"- else- npm publish --provenance --access public- fi+ - name: Pack once and test the release artifacts+ run: npm run release:prepare -- --out "$RELEASE_OUT" --packages "$RELEASE_PACKAGES"env:- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}+ RELEASE_OUT: ${{ runner.temp }}/npm-release+ RELEASE_PACKAGES: ${{ github.event_name == 'release' && 'agentmemory,mcp' || inputs.packages }}- - name: Wait for @agentmemory/mcp registry propagation- working-directory: packages/mcp- run: |- SHIM_VERSION=$(node -p "require('./package.json').version")- for i in $(seq 1 24); do- if npm view "@agentmemory/mcp@$SHIM_VERSION" version >/dev/null 2>&1; then- echo "Shim propagated after ${i} attempt(s)"- exit 0- fi- echo "Attempt $i: not yet available, sleeping 5s..."- sleep 5- done- echo "ERROR: shim never propagated after 2 minutes" >&2- exit 1-- - name: Publish @agentmemory/fs-watcher connector- working-directory: integrations/filesystem-watcher- run: |- FSW_VERSION=$(node -p "require('./package.json').version")- if npm view "@agentmemory/fs-watcher@$FSW_VERSION" version >/dev/null 2>&1; then- echo "fs-watcher version already published, skipping"- else- npm publish --provenance --access public- fi+ - name: Publish gated artifacts and verify registry provenance+ run: npm run release:publish -- --manifest "$RELEASE_OUT/manifest.json"env:+ RELEASE_OUT: ${{ runner.temp }}/npm-releaseNODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}- - name: Wait for @agentmemory/fs-watcher registry propagation- working-directory: integrations/filesystem-watcher- run: |- FSW_VERSION=$(node -p "require('./package.json').version")- for i in $(seq 1 24); do- if npm view "@agentmemory/fs-watcher@$FSW_VERSION" version >/dev/null 2>&1; then- echo "fs-watcher propagated after ${i} attempt(s)"- exit 0- fi- echo "Attempt $i: not yet available, sleeping 5s..."- sleep 5- done- echo "ERROR: fs-watcher never propagated after 2 minutes" >&2- exit 1+ - if: always()+ uses: actions/upload-artifact@v4+ with:+ name: npm-release-${{ github.run_id }}-${{ github.run_attempt }}+ path: ${{ runner.temp }}/npm-release+ if-no-files-found: ignore+ retention-days: 14diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 4738fd3d..d9a398cc 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -111,7 +111,13 @@ For startup changes, also run `npm run test:startup:packaged`. It installs the pFor Hermes, OpenClaw or shared skill changes, follow [Native integration tests](docs/native-integration-tests.md). Those gates load plugins and skills from an installed tarball and inspect the native model requests and backend records. Source tests alone do not establish native lifecycle capture or skill discovery.-Then: CHANGELOG section, PR, merge, tag, GitHub release. The `Publish to npm` workflow picks up the release trigger and publishes `@agentmemory/agentmemory`, `@agentmemory/mcp`, and `@agentmemory/fs-watcher` to npm with provenance (`@agentmemory/fs-watcher` versions independently from `integrations/filesystem-watcher/package.json`).+Then: CHANGELOG section, PR, merge, tag, GitHub release. The `Publish to npm` workflow picks up the release trigger and publishes `@agentmemory/agentmemory` and `@agentmemory/mcp` to npm with provenance. It packs once, runs the release gate with both exact tarballs, checks their hashes again, and publishes those files with lifecycle scripts disabled. It retains the manifest, tarballs and gate logs as workflow artifacts. A failed gate prevents publication.++Manual runs accept a comma-separated selection of `agentmemory`, `mcp`, and `fs-watcher`. The default is `agentmemory,mcp`; `@agentmemory/fs-watcher` versions independently and must be explicitly selected when releasing a new connector version. Its exact packed CLI must pass capture, credential, scope and shutdown checks. An MCP-only run also verifies that the registry's main package is the artifact used in its gate.++Before publishing any selected package, the workflow checks existing versions. An existing version is accepted only when its SHA512 matches the gated tarball and its cryptographically verified SLSA provenance identifies this repository, workflow, ref and source commit. Conflicting or unprovenanced versions fail the run and cannot be overwritten. Only a registry version lookup returning HTTP 404 permits publication; authorization, network and other registry failures never count as absence. After publication, bounded retries allow registry metadata and provenance to propagate, then require the same verification. Reruns can skip only identical, verified artifacts from the same source identity.++These checks do not configure or prove npm publish authorization. Maintainers must separately ensure that each selected package permits this workflow to publish, using its configured trusted publisher or publish token. The workflow retains the existing credential configuration. A successful dry run does not establish that npm will accept a future publication or persist its provenance.## Security issuesdiff --git a/package.json b/package.jsonindex 5766b3ef..fb7dfc0f 100644--- a/package.json+++ b/package.json@@ -33,6 +33,8 @@"test:skills:packaged": "node scripts/release-gate/skills.mjs","test:openclaw:packaged": "node scripts/release-gate/openclaw.mjs","release:gate": "node scripts/release-gate/run.mjs",+ "release:prepare": "node scripts/release/publish.mjs prepare",+ "release:publish": "node scripts/release/publish.mjs publish","skills:gen": "tsx scripts/skills/generate.ts","engine:hashes": "node scripts/engine-hashes.mjs","skills:check": "tsx scripts/skills/generate.ts --check && tsx scripts/skills/check.ts",@@ -104,6 +106,7 @@},"devDependencies": {"@types/node": "^26.6.2",+ "sigstore": "4.1.1","tsdown": "^0.21.10","tsx": "^4.19.0","typescript": "^6.0.3",diff --git a/scripts/release-gate/filesystem-watcher.mjs b/scripts/release-gate/filesystem-watcher.mjsnew file mode 100644index 00000000..d243740c--- /dev/null+++ b/scripts/release-gate/filesystem-watcher.mjs@@ -0,0 +1,66 @@+#!/usr/bin/env node+import assert from "node:assert/strict";+import { execFileSync, spawn } from "node:child_process";+import { once } from "node:events";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { createServer } from "node:http";+import { tmpdir } from "node:os";+import { join, resolve } from "node:path";+import { setTimeout as sleep } from "node:timers/promises";++assert(process.argv[2], "Pass the packed fs-watcher tarball");+const root = mkdtempSync(join(tmpdir(), "agentmemory-watcher-gate-"));+const home = join(root, "home");+const watched = join(root, "watched");+mkdirSync(home);+mkdirSync(watched);+const env = { PATH: process.env.PATH, HOME: home, LANG: "C.UTF-8", npm_config_cache: join(root, "cache") };+const received = [];+let child;+let output = "";+let exited = false;+const server = createServer(async (request, response) => {+ let body = "";+ for await (const chunk of request) body += chunk;+ received.push({ url: request.url, authorization: request.headers.authorization, body: JSON.parse(body) });+ response.writeHead(200, { "content-type": "application/json" });+ response.end("{}");+});+async function until(check) {+ const deadline = Date.now() + 15_000;+ while (!check()) {+ assert(!exited, `Installed watcher exited early: ${output}`);+ assert(Date.now() < deadline, `Installed watcher timed out: ${output}`);+ await sleep(50);+ }+}+try {+ execFileSync("npm", ["install", "--prefix", root, "--ignore-scripts", "--package-lock=false", "--no-audit", "--no-fund", resolve(process.argv[2])], { env, stdio: "inherit", timeout: 120_000 });+ const binary = join(root, "node_modules/@agentmemory/fs-watcher/bin.mjs");+ await new Promise((done, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", done); });+ child = spawn(process.execPath, [binary, watched], {+ cwd: home,+ env: { ...env, AGENTMEMORY_URL: `http://127.0.0.1:${server.address().port}`, AGENTMEMORY_SECRET: "release-gate-fixture", AGENTMEMORY_PROJECT_NAME: "release-gate", AGENTMEMORY_SESSION_ID: "release-gate-session" },+ stdio: ["ignore", "pipe", "pipe"],+ });+ child.once("exit", () => { exited = true; });+ child.stdout.on("data", (data) => { output += data; });+ child.stderr.on("data", (data) => { output += data; });+ await until(() => output.includes("[fs-watcher] watching"));+ writeFileSync(join(watched, "fixture.txt"), "Packaged watcher capture fixture\n");+ await until(() => received.some((event) => event.body.data?.content?.includes("Packaged watcher capture fixture")));+ const event = received.find((entry) => entry.body.data?.content?.includes("Packaged watcher capture fixture"));+ assert.equal(event.url, "/agentmemory/observe");+ assert.equal(event.authorization, "Bearer release-gate-fixture");+ assert.equal(event.body.project, "release-gate");+ assert.equal(event.body.sessionId, "release-gate-session");+ const exit = once(child, "exit", { signal: AbortSignal.timeout(5000) });+ child.kill("SIGTERM");+ assert.deepEqual(await exit, [0, null], "Installed watcher did not stop cleanly");+ console.log("PASS packaged filesystem watcher: capture, credentials, scope, shutdown");+} finally {+ child?.kill("SIGKILL");+ server.closeAllConnections();+ await new Promise((done) => server.close(done));+ rmSync(root, { recursive: true, force: true });+}diff --git a/scripts/release/publish.mjs b/scripts/release/publish.mjsnew file mode 100644index 00000000..2ede1367--- /dev/null+++ b/scripts/release/publish.mjs@@ -0,0 +1,109 @@+#!/usr/bin/env node+import assert from "node:assert/strict";+import { execFileSync } from "node:child_process";+import { createHash } from "node:crypto";+import { mkdirSync, readFileSync, writeFileSync } from "node:fs";+import { dirname, join, resolve } from "node:path";+import { fileURLToPath } from "node:url";+import { PACKAGES, REGISTRY, parsePackages, publicationPlan, publishSelected, verifyPublished } from "./registry.mjs";++const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "../..");+function run(command, args, cwd = REPO, capture = false) {+ return execFileSync(command, args, { cwd, encoding: "utf8", stdio: capture ? ["ignore", "pipe", "inherit"] : "inherit", timeout: 1_800_000 });+}++export function releaseIdentity(env, head) {+ const identity = { repository: "rohitg00/agentmemory", workflow: ".github/workflows/publish.yml", ref: env.GITHUB_REF, sha: env.GITHUB_SHA };+ assert.equal(env.GITHUB_REPOSITORY, identity.repository, "Publication must run in the project repository");+ assert(/^[a-f0-9]{40}$/.test(identity.sha ?? ""), "Invalid release commit");+ assert.equal(head, identity.sha, "Checkout differs from the workflow release commit");+ assert(/^refs\/(tags|heads)\/[^\s]+$/.test(identity.ref ?? ""), "Invalid release ref");+ assert.equal(env.GITHUB_WORKFLOW_REF, `${identity.repository}/${identity.workflow}@${identity.ref}`, "Unexpected publication workflow identity");+ return identity;+}++export function artifactDigest(path) {+ const bytes = readFileSync(path);+ const sha512 = createHash("sha512").update(bytes).digest();+ return { integrity: `sha512-${sha512.toString("base64")}`, sha512: sha512.toString("hex") };+}++export function assertGatedArtifacts(manifest, identity) {+ assert.equal(manifest.format, 1, "Unsupported release manifest");+ assert.equal(manifest.gated, true, "Artifacts have not passed the packaged gates");+ assert.deepEqual(manifest.identity, identity, "Gated source differs from this release");+ const selected = parsePackages(manifest.selected.join(","));+ const plan = publicationPlan(selected);+ assert.deepEqual(manifest.artifacts.map((artifact) => artifact.key), plan.pack, "Unexpected release artifact selection");+ for (const artifact of manifest.artifacts) {+ assert.equal(artifact.name, PACKAGES[artifact.key].name, "Unexpected release package name");+ assert.deepEqual(artifactDigest(artifact.tarball), { integrity: artifact.integrity, sha512: artifact.sha512 }, `Gated tarball changed: ${artifact.name}`);+ }+ return plan;+}++export function prepareArtifacts({ out, selected, identity, command = run }) {+ const plan = publicationPlan(selected);+ mkdirSync(out);+ const packDirectory = join(out, "pack");+ mkdirSync(packDirectory);+ const artifacts = plan.pack.map((key) => {+ const pkg = PACKAGES[key];+ const directory = resolve(REPO, pkg.directory);+ const source = JSON.parse(readFileSync(join(directory, "package.json"), "utf8"));+ assert.equal(source.name, pkg.name, "Package manifest name mismatch");+ const packed = JSON.parse(command("npm", ["pack", "--json", "--pack-destination", packDirectory], directory, true));+ assert.equal(packed.length, 1, "Expected one packed artifact");+ assert.equal(packed[0].name, source.name, "Packed package name mismatch");+ assert.equal(packed[0].version, source.version, "Packed package version mismatch");+ const tarball = join(packDirectory, packed[0].filename);+ const digest = artifactDigest(tarball);+ assert.equal(packed[0].integrity, digest.integrity, "npm pack integrity mismatch");+ return { key, name: source.name, version: source.version, tarball, ...digest };+ });+ const artifact = (key) => artifacts.find((entry) => entry.key === key);+ if (artifact("agentmemory")) {+ command(process.execPath, [join(REPO, "scripts/release-gate/run.mjs"), "--tarball", artifact("agentmemory").tarball, "--mcp-tarball", artifact("mcp").tarball, "--skip-build", "--out", join(out, "gate")]);+ }+ if (artifact("fs-watcher")) {+ command(process.execPath, [join(REPO, "scripts/release-gate/filesystem-watcher.mjs"), artifact("fs-watcher").tarball]);+ }+ const manifest = { format: 1, identity, selected, artifacts, gated: true };+ assertGatedArtifacts(manifest, identity);+ writeFileSync(join(out, "manifest.json"), JSON.stringify(manifest, null, 2) + "\n");+ return manifest;+}++async function main(argv) {+ const [mode, ...args] = argv;+ const opts = {};+ for (let i = 0; i < args.length; i += 2) {+ assert(["--out", "--packages", "--manifest"].includes(args[i]) && args[i + 1] && !Object.hasOwn(opts, args[i]), "Invalid release arguments");+ opts[args[i]] = args[i + 1];+ }+ assert(["prepare", "publish"].includes(mode), "Usage: publish.mjs prepare --out directory [--packages agentmemory,mcp] | publish --manifest file");+ const identity = releaseIdentity(process.env, run("git", ["rev-parse", "HEAD"], REPO, true).trim());+ if (mode === "prepare") {+ assert(opts["--out"] && !opts["--manifest"], "Preparation requires a fresh output directory");+ prepareArtifacts({ out: resolve(opts["--out"]), selected: parsePackages(opts["--packages"]), identity });+ return;+ }+ assert(opts["--manifest"] && !opts["--out"] && !opts["--packages"], "Publication requires only the gated manifest");+ const manifest = JSON.parse(readFileSync(resolve(opts["--manifest"]), "utf8"));+ const plan = assertGatedArtifacts(manifest, identity);+ for (const key of plan.verifyOnly) {+ const artifact = manifest.artifacts.find((entry) => entry.key === key);+ assert(await verifyPublished(artifact, identity), `Publish ${artifact.name}@${artifact.version} before its MCP shim`);+ }+ await publishSelected(manifest.artifacts.filter((artifact) => plan.publish.includes(artifact.key)), identity, {+ log: console.log,+ publish: (artifact) => {+ assertGatedArtifacts(manifest, identity);+ run("npm", ["publish", artifact.tarball, "--provenance", "--access", "public", "--ignore-scripts", "--registry", REGISTRY]);+ },+ });+}++if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {+ main(process.argv.slice(2)).catch((error) => { console.error(error.message); process.exitCode = 1; });+}diff --git a/scripts/release/registry.mjs b/scripts/release/registry.mjsnew file mode 100644index 00000000..97805b24--- /dev/null+++ b/scripts/release/registry.mjs@@ -0,0 +1,113 @@+import assert from "node:assert/strict";+import { setTimeout as sleep } from "node:timers/promises";++export const REGISTRY = "https://registry.npmjs.org";+export const PACKAGES = {+ agentmemory: { name: "@agentmemory/agentmemory", directory: "." },+ mcp: { name: "@agentmemory/mcp", directory: "packages/mcp" },+ "fs-watcher": { name: "@agentmemory/fs-watcher", directory: "integrations/filesystem-watcher" },+};+const PROVENANCE = "https://slsa.dev/provenance/v1";++export function parsePackages(input = "agentmemory,mcp") {+ const selected = input.split(",").map((value) => value.trim());+ assert(selected.length && selected.every((key) => Object.hasOwn(PACKAGES, key)), "Select only agentmemory,mcp,fs-watcher");+ assert.equal(new Set(selected).size, selected.length, "Duplicate package selection");+ return Object.keys(PACKAGES).filter((key) => selected.includes(key));+}++export function publicationPlan(selected) {+ const needsMain = selected.includes("agentmemory") || selected.includes("mcp");+ return {+ pack: Object.keys(PACKAGES).filter((key) => selected.includes(key) || (needsMain && key !== "fs-watcher")),+ verifyOnly: selected.includes("mcp") && !selected.includes("agentmemory") ? ["agentmemory"] : [],+ publish: selected,+ };+}++class PendingProvenance extends Error {}++async function registryJSON(url, { fetch: request = fetch, sleep: delay = sleep, attempts = 3 } = {}) {+ for (let attempt = 0; attempt < attempts; attempt++) {+ let response;+ try {+ response = await request(url, { signal: AbortSignal.timeout(15_000), redirect: "error", headers: { accept: "application/json", "cache-control": "no-cache" } });+ } catch (error) {+ if (attempt + 1 === attempts) throw error;+ await delay(1000 * (attempt + 1));+ continue;+ }+ if (response.status === 404) return null;+ if (response.ok) return response.json();+ if ((response.status === 429 || response.status >= 500) && attempt + 1 < attempts) {+ await delay(1000 * (attempt + 1));+ continue;+ }+ throw new Error(`Registry request failed: HTTP ${response.status}`);+ }+ throw new Error("Registry request exhausted its attempts");+}++async function verifyBundle(bundle, options) {+ const { verify } = await import("sigstore");+ await verify(bundle, options);+}++export async function verifyPublished(artifact, identity, options = {}) {+ const metadata = await registryJSON(`${REGISTRY}/${encodeURIComponent(artifact.name)}/${encodeURIComponent(artifact.version)}`, options);+ if (metadata === null) return false;+ assert.equal(metadata.name, artifact.name, "Registry package name mismatch");+ assert.equal(metadata.version, artifact.version, "Registry package version mismatch");+ assert.equal(metadata.dist?.integrity, artifact.integrity, `Registry integrity differs from the gated ${artifact.name}@${artifact.version}; existing versions cannot be overwritten`);+ if (!metadata.dist?.attestations?.url) throw new PendingProvenance("Registry artifact has no provenance");+ const url = new URL(metadata.dist.attestations.url);+ assert(url.origin === REGISTRY && url.pathname.startsWith("/-/npm/v1/attestations/") && !url.username && !url.password && !url.search && !url.hash, "Unexpected registry attestation URL");+ const document = await registryJSON(url.href, options);+ const entries = document?.attestations?.filter((entry) => entry.predicateType === PROVENANCE);+ if (!entries?.length) throw new PendingProvenance("Registry artifact has no SLSA v1 provenance");+ assert.equal(entries.length, 1, "Ambiguous provenance statements");+ const bundle = entries[0].bundle;+ await (options.verifyBundle ?? verifyBundle)(bundle, {+ certificateIssuer: "https://token.actions.githubusercontent.com",+ certificateIdentityURI: `https://github.com/${identity.repository}/${identity.workflow}@${identity.ref}`,+ timeout: 15_000,+ retry: { retries: 2 },+ });+ assert.equal(bundle.dsseEnvelope?.payloadType, "application/vnd.in-toto+json", "Unexpected provenance payload type");+ const statement = JSON.parse(Buffer.from(bundle.dsseEnvelope.payload, "base64").toString("utf8"));+ assert.equal(statement._type, "https://in-toto.io/Statement/v1", "Unexpected provenance statement type");+ assert.equal(statement.predicateType, PROVENANCE, "Unexpected provenance predicate");+ assert.equal(statement.subject?.length, 1, "Unexpected provenance subjects");+ assert.equal(statement.subject[0].name, `pkg:npm/${artifact.name.replace(/^@/, "%40")}@${artifact.version}`, "Provenance subject differs from package");+ assert.equal(statement.subject[0].digest?.sha512, artifact.sha512, "Provenance subject digest differs from gated artifact");+ const definition = statement.predicate?.buildDefinition;+ assert.equal(definition?.buildType, "https://slsa-framework.github.io/github-actions-buildtypes/workflow/v1", "Unexpected provenance build type");+ assert.deepEqual(definition.externalParameters?.workflow, { repository: `https://github.com/${identity.repository}`, path: identity.workflow, ref: identity.ref }, "Provenance workflow identity differs from this release");+ assert(definition.resolvedDependencies?.some((source) => source.uri === `git+https://github.com/${identity.repository}@${identity.ref}` && source.digest?.gitCommit === identity.sha), "Provenance commit differs from this release");+ assert.equal(statement.predicate.runDetails?.builder?.id, "https://github.com/actions/runner/github-hosted", "Provenance was not built on a GitHub-hosted runner");+ return true;+}++export async function publishSelected(artifacts, identity, options) {+ const existing = [];+ for (const artifact of artifacts) existing.push(await verifyPublished(artifact, identity, options));+ for (const [index, artifact] of artifacts.entries()) {+ if (existing[index]) {+ options.log?.(`Verified existing ${artifact.name}@${artifact.version}`);+ continue;+ }+ await options.publish(artifact);+ let verified = false;+ for (let attempt = 0; attempt < (options.propagationAttempts ?? 24); attempt++) {+ try {+ verified = await verifyPublished(artifact, identity, options);+ } catch (error) {+ if (!(error instanceof PendingProvenance)) throw error;+ }+ if (verified) break;+ await (options.sleep ?? sleep)(5000);+ }+ assert(verified, `${artifact.name}@${artifact.version} did not become verifiable after publication`);+ options.log?.(`Verified published ${artifact.name}@${artifact.version}`);+ }+}diff --git a/test/release-install-policy.test.ts b/test/release-install-policy.test.tsnew file mode 100644index 00000000..85ef0e44--- /dev/null+++ b/test/release-install-policy.test.ts@@ -0,0 +1,34 @@+import { execFileSync } from "node:child_process";+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join, resolve } from "node:path";+import { parse } from "yaml";+import { expect, it } from "vitest";++it("publication dependency installs cannot execute root or dependency lifecycle scripts", () => {+ const workflow = parse(readFileSync(resolve(".github/workflows/publish.yml"), "utf8"));+ const commands = workflow.jobs.publish.steps+ .map((step: { run?: string }) => step.run)+ .filter((command?: string) => command?.startsWith("npm install --package-lock-only") || command?.startsWith("npm ci "));+ expect(commands).toHaveLength(2);+ const root = mkdtempSync(join(tmpdir(), "release-install-policy-"));+ const dependency = join(root, "dependency");+ mkdirSync(dependency);+ const marker = join(root, "lifecycle-ran");+ const script = 'require("node:fs").writeFileSync(process.env.RELEASE_LIFECYCLE_MARKER, "executed")';+ const env = { PATH: process.env.PATH, HOME: root, npm_config_cache: join(root, "cache"), npm_config_offline: "true", RELEASE_LIFECYCLE_MARKER: marker };+ try {+ writeFileSync(join(dependency, "package.json"), JSON.stringify({ name: "release-install-fixture", version: "1.0.0", scripts: { postinstall: "node install.cjs" } }));+ writeFileSync(join(dependency, "install.cjs"), script);+ const packed = JSON.parse(execFileSync("npm", ["pack", "--json", "--ignore-scripts", "--pack-destination", root], { cwd: dependency, env, encoding: "utf8", timeout: 20_000 }));+ writeFileSync(join(root, "package.json"), JSON.stringify({ name: "release-policy-root", private: true, dependencies: { "release-install-fixture": `file:./${packed[0].filename}` }, scripts: { postinstall: "node install.cjs" } }));+ writeFileSync(join(root, "install.cjs"), script);+ for (const command of commands) {+ execFileSync("npm", command.split(" ").slice(1), { cwd: root, env, encoding: "utf8", timeout: 20_000 });+ expect(existsSync(marker), `${command} executed a lifecycle script`).toBe(false);+ }+ expect(existsSync(join(root, "node_modules/release-install-fixture/install.cjs"))).toBe(true);+ } finally {+ rmSync(root, { recursive: true, force: true });+ }+}, 60_000);diff --git a/test/release-publication.test.ts b/test/release-publication.test.tsnew file mode 100644index 00000000..51129b19--- /dev/null+++ b/test/release-publication.test.ts@@ -0,0 +1,193 @@+import { createHash } from "node:crypto";+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, describe, expect, it, vi } from "vitest";+import { parsePackages, publicationPlan, verifyPublished, publishSelected } from "../scripts/release/registry.mjs";+import { artifactDigest, assertGatedArtifacts, prepareArtifacts, releaseIdentity } from "../scripts/release/publish.mjs";++const identity = { repository: "rohitg00/agentmemory", workflow: ".github/workflows/publish.yml", ref: "refs/tags/v0.9.31", sha: "a".repeat(40) };+const bytes = Buffer.from("gated tarball");+const artifact = { key: "agentmemory", name: "@agentmemory/agentmemory", version: "0.9.31", integrity: `sha512-${createHash("sha512").update(bytes).digest("base64")}`, sha512: createHash("sha512").update(bytes).digest("hex") };+type RegistryMetadata = { name: string; version: string; dist: { integrity: string; attestations?: { url: string } } };+const metadata = (): RegistryMetadata => ({ name: artifact.name, version: artifact.version, dist: { integrity: artifact.integrity, attestations: { url: "https://registry.npmjs.org/-/npm/v1/attestations/test" } } });+function provenanceStatement() {+ return {+ _type: "https://in-toto.io/Statement/v1", predicateType: "https://slsa.dev/provenance/v1",+ subject: [{ name: "pkg:npm/%40agentmemory/[email protected]", digest: { sha512: artifact.sha512 } }],+ predicate: { buildDefinition: {+ buildType: "https://slsa-framework.github.io/github-actions-buildtypes/workflow/v1",+ externalParameters: { workflow: { repository: `https://github.com/${identity.repository}`, path: identity.workflow, ref: identity.ref } },+ resolvedDependencies: [{ uri: `git+https://github.com/${identity.repository}@${identity.ref}`, digest: { gitCommit: identity.sha } }],+ }, runDetails: { builder: { id: "https://github.com/actions/runner/github-hosted" } } },+ };+}+function attestations(edit: (statement: ReturnType<typeof provenanceStatement>) => void = () => {}) {+ const statement = provenanceStatement();+ edit(statement);+ return { attestations: [{ predicateType: statement.predicateType, bundle: { dsseEnvelope: { payloadType: "application/vnd.in-toto+json", payload: Buffer.from(JSON.stringify(statement)).toString("base64") } } }] };+}+function fetcher(...responses: Array<Response | Error>) {+ return vi.fn(async () => {+ const response = responses.shift();+ if (response instanceof Error) throw response;+ if (!response) throw new Error("unexpected fetch");+ return response;+ });+}+const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status });+const options = { verifyBundle: vi.fn(async () => {}), sleep: async () => {} };++describe("verified release publication", () => {+ it("validates selectors and keeps independent fs-watcher releases opt-in", () => {+ expect(parsePackages(undefined)).toEqual(["agentmemory", "mcp"]);+ expect(parsePackages(" fs-watcher,agentmemory ")).toEqual(["agentmemory", "fs-watcher"]);+ for (const input of ["", "mcp,", "mcp,mcp", "all", "$(publish)"]) expect(() => parsePackages(input)).toThrow();+ expect(publicationPlan(["mcp"])).toEqual({ pack: ["agentmemory", "mcp"], verifyOnly: ["agentmemory"], publish: ["mcp"] });+ expect(publicationPlan(["fs-watcher"])).toEqual({ pack: ["fs-watcher"], verifyOnly: [], publish: ["fs-watcher"] });+ });++ it("accepts an identical artifact only after checking signed provenance identity", async () => {+ const verifyBundle = vi.fn(async () => {});+ expect(await verifyPublished(artifact, identity, { ...options, verifyBundle, fetch: fetcher(json(metadata()), json(attestations())) })).toBe(true);+ expect(verifyBundle).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({+ certificateIssuer: "https://token.actions.githubusercontent.com",+ certificateIdentityURI: `https://github.com/${identity.repository}/${identity.workflow}@${identity.ref}`,+ }));+ });++ it("accepts only a version endpoint 404 as absence", async () => {+ expect(await verifyPublished(artifact, identity, { ...options, fetch: fetcher(json({}, 404)) })).toBe(false);+ for (const status of [401, 403, 500]) {+ await expect(verifyPublished(artifact, identity, { ...options, attempts: 1, fetch: fetcher(json({}, status)) })).rejects.toThrow(`HTTP ${status}`);+ }+ });++ it("retries bounded transient fetch errors without mistaking them for absence", async () => {+ const fetch = fetcher(new Error("connection reset"), json({}, 503), json(metadata()), json(attestations()));+ expect(await verifyPublished(artifact, identity, { ...options, fetch })).toBe(true);+ expect(fetch).toHaveBeenCalledTimes(4);+ await expect(verifyPublished(artifact, identity, { ...options, attempts: 2, fetch: fetcher(json({}, 429), json({}, 429)) })).rejects.toThrow("HTTP 429");+ });++ it("rejects missing provenance even when registry signatures or publish attestations exist", async () => {+ const noProvenance = metadata();+ delete noProvenance.dist.attestations;+ await expect(verifyPublished(artifact, identity, { ...options, fetch: fetcher(json(noProvenance)) })).rejects.toThrow("provenance");+ await expect(verifyPublished(artifact, identity, { ...options, fetch: fetcher(json(metadata()), json({ attestations: [{ predicateType: "publish" }] })) })).rejects.toThrow("provenance");+ });++ it("rejects a conflicting registry artifact without invoking publication", async () => {+ const conflicting = metadata();+ conflicting.dist.integrity = "sha512-other";+ const publish = vi.fn();+ await expect(publishSelected([artifact], identity, { ...options, publish, fetch: fetcher(json(conflicting)) })).rejects.toThrow("integrity");+ expect(publish).not.toHaveBeenCalled();+ });++ it("preflights every selected package before publishing any missing version", async () => {+ const publish = vi.fn();+ await expect(publishSelected([artifact, { ...artifact, name: "@agentmemory/mcp" }], identity, { ...options, publish, fetch: fetcher(json({}, 404), json({}, 403)) })).rejects.toThrow("HTTP 403");+ expect(publish).not.toHaveBeenCalled();+ });++ it.each([+ ["subject", (s: ReturnType<typeof provenanceStatement>) => { s.subject[0].digest.sha512 = "b".repeat(128); }],+ ["commit", (s: ReturnType<typeof provenanceStatement>) => { s.predicate.buildDefinition.resolvedDependencies[0].digest.gitCommit = "b".repeat(40); }],+ ["workflow", (s: ReturnType<typeof provenanceStatement>) => { s.predicate.buildDefinition.externalParameters.workflow.path = ".github/workflows/other.yml"; }],+ ["repository", (s: ReturnType<typeof provenanceStatement>) => { s.predicate.buildDefinition.externalParameters.workflow.repository = "https://github.com/attacker/repo"; }],+ ["ref", (s: ReturnType<typeof provenanceStatement>) => { s.predicate.buildDefinition.externalParameters.workflow.ref = "refs/heads/main"; }],+ ])("rejects signed provenance with a different %s", async (_, edit) => {+ await expect(verifyPublished(artifact, identity, { ...options, fetch: fetcher(json(metadata()), json(attestations(edit))) })).rejects.toThrow();+ });++ it("rejects invalid signatures even when every decoded field matches", async () => {+ await expect(verifyPublished(artifact, identity, { ...options, verifyBundle: async () => { throw new Error("invalid signature"); }, fetch: fetcher(json(metadata()), json(attestations())) })).rejects.toThrow("invalid signature");+ });++ it("does not follow an attestation URL to another origin", async () => {+ const redirected = metadata();+ redirected.dist.attestations!.url = "https://attacker.example/attestation";+ const fetch = fetcher(json(redirected));+ await expect(verifyPublished(artifact, identity, { ...options, fetch })).rejects.toThrow("attestation URL");+ expect(fetch).toHaveBeenCalledTimes(1);+ });++ it("skips a legitimate verified rerun without publishing again", async () => {+ const publish = vi.fn();+ await publishSelected([artifact], identity, { ...options, publish, fetch: fetcher(json(metadata()), json(attestations())) });+ expect(publish).not.toHaveBeenCalled();+ });++ it("verifies propagation after publication and tolerates delayed provenance", async () => {+ const noProvenance = metadata();+ delete noProvenance.dist.attestations;+ const publish = vi.fn(async () => {});+ await publishSelected([artifact], identity, { ...options, publish, fetch: fetcher(json({}, 404), json({}, 404), json(noProvenance), json(metadata()), json(attestations())) });+ expect(publish).toHaveBeenCalledExactlyOnceWith(artifact);+ });++ it("fails publication when registry never supplies verified provenance", async () => {+ const publish = vi.fn(async () => {});+ await expect(publishSelected([artifact], identity, { ...options, publish, propagationAttempts: 2, fetch: fetcher(json({}, 404), json({}, 404), json({}, 404)) })).rejects.toThrow("did not become verifiable");+ });+});++describe("exact packaged release artifacts", () => {+ const directories: string[] = [];+ afterEach(() => { for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }); });+ function output() {+ const root = mkdtempSync(join(tmpdir(), "release-publication-test-"));+ directories.push(root);+ return join(root, "release");+ }+ function commands(gate: (args: string[]) => void = () => {}) {+ return vi.fn((cmd: string, args: string[], cwd: string) => {+ if (cmd !== "npm") { gate(args); return ""; }+ const source = JSON.parse(readFileSync(join(cwd, "package.json"), "utf8"));+ const path = join(args[args.indexOf("--pack-destination") + 1], source.name.replace(/[@/]/g, "") + ".tgz");+ writeFileSync(path, source.name);+ return JSON.stringify([{ name: source.name, version: source.version, filename: path.split("/").at(-1), integrity: artifactDigest(path).integrity }]);+ });+ }++ it("binds the checkout to the exact workflow ref and commit for branches and tags", () => {+ for (const ref of ["refs/tags/v0.9.31", "refs/heads/main"]) {+ const env = { GITHUB_REF: ref, GITHUB_SHA: identity.sha, GITHUB_REPOSITORY: identity.repository, GITHUB_WORKFLOW_REF: `${identity.repository}/${identity.workflow}@${ref}` };+ expect(releaseIdentity(env, identity.sha)).toEqual({ ...identity, ref });+ expect(() => releaseIdentity(env, "b".repeat(40))).toThrow("Checkout differs");+ expect(() => releaseIdentity({ ...env, GITHUB_WORKFLOW_REF: `${identity.repository}/other.yml@${ref}` }, identity.sha)).toThrow("workflow identity");+ }+ });++ it("gates both exact CLI and MCP tarballs and retains only those bytes", () => {+ const command = commands();+ const manifest = prepareArtifacts({ out: output(), selected: ["agentmemory", "mcp"], identity, command });+ const gate = command.mock.calls.find(([cmd]) => cmd !== "npm")!;+ expect(gate[1]).toEqual(expect.arrayContaining(["--tarball", manifest.artifacts[0].tarball, "--mcp-tarball", manifest.artifacts[1].tarball]));+ expect(command.mock.calls.filter(([cmd]) => cmd === "npm")).toHaveLength(2);+ expect(assertGatedArtifacts(manifest, identity).publish).toEqual(["agentmemory", "mcp"]);+ writeFileSync(manifest.artifacts[0].tarball, "changed after gate");+ expect(() => assertGatedArtifacts(manifest, identity)).toThrow("Gated tarball changed");+ });++ it("never writes a publishable manifest after a failed gate or an artifact mutation", () => {+ const failed = output();+ expect(() => prepareArtifacts({ out: failed, selected: ["agentmemory"], identity, command: commands(() => { throw new Error("gate failed"); }) })).toThrow("gate failed");+ expect(existsSync(join(failed, "manifest.json"))).toBe(false);+ const changed = output();+ expect(() => prepareArtifacts({ out: changed, selected: ["agentmemory"], identity, command: commands((args) => writeFileSync(args[args.indexOf("--tarball") + 1], "mutated")) })).toThrow("Gated tarball changed");+ expect(existsSync(join(changed, "manifest.json"))).toBe(false);+ });++ it("gates only the independent watcher when selected and preserves prior output on reruns", () => {+ const out = output();+ const command = commands();+ const manifest = prepareArtifacts({ out, selected: ["fs-watcher"], identity, command });+ expect(manifest.artifacts.map((entry: typeof artifact) => entry.key)).toEqual(["fs-watcher"]);+ expect(command.mock.calls.find(([cmd]) => cmd !== "npm")![1][0]).toContain("filesystem-watcher.mjs");+ const before = readFileSync(join(out, "manifest.json"), "utf8");+ expect(() => prepareArtifacts({ out, selected: ["fs-watcher"], identity, command })).toThrow();+ expect(readFileSync(join(out, "manifest.json"), "utf8")).toEqual(before);+ });+});