#!/usr/bin/env node import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs"; import { createRequire } from "node:module"; import path from "node:path"; import process from "node:process"; import { spawn, spawnSync } from "node:child_process"; const NOT_EVALUATED = Object.freeze([ "native-network-and-filesystem-attempt-observation", "transactional-crash-and-race-certification", "benchmark-80-90-100", "real-host-configuration-writes", "public-release", ]); const require = createRequire(import.meta.url); function fail(message) { throw new Error(`AAS_PREVIEW_${message}`); } function parseArgs(argv) { if (argv.length % 2 !== 0) fail("ARGUMENTS_INVALID"); const values = {}; for (let index = 0; index < argv.length; index += 2) { const key = argv[index]; const value = argv[index + 1]; if (!key?.startsWith("--") || !value || value.startsWith("--")) fail("ARGUMENTS_INVALID"); const name = key.slice(2); if (Object.hasOwn(values, name)) fail("ARGUMENT_DUPLICATE"); values[name] = value; } for (const key of ["tarball", "package-root", "work-root", "job-id", "out"]) { if (!values[key]) fail("ARGUMENT_REQUIRED"); } for (const key of ["tarball", "package-root", "work-root", "out"]) { if (!path.isAbsolute(values[key])) fail("ABSOLUTE_PATH_REQUIRED"); } if (!/^(linux|macos|windows)-node-(22|24)$/.test(values["job-id"])) fail("JOB_ID_INVALID"); return values; } function sha256(bytes) { return `sha256-${crypto.createHash("sha256").update(bytes).digest("hex")}`; } function sha512Sri(bytes) { return `sha512-${crypto.createHash("sha512").update(bytes).digest("base64")}`; } function stable(value) { if (Array.isArray(value)) return `[${value.map(stable).join(",")}]`; if (value && typeof value === "object") { return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stable(value[key])}`).join(",")}}`; } return JSON.stringify(value); } function runNode(script, args, options = {}) { const result = spawnSync(process.execPath, [script, ...args], { cwd: options.cwd, env: options.env || process.env, encoding: "utf8", timeout: 60_000, maxBuffer: 8 * 1024 * 1024, windowsHide: true, }); if (result.error) throw result.error; return result; } function parseCliSuccess(result, label) { if (result.status !== 0 || result.stderr.trim()) fail(`${label}_FAILED`); const value = JSON.parse(result.stdout); if (value.ok !== true || value.schemaVersion !== 1) fail(`${label}_ENVELOPE_INVALID`); return value; } function parseCliFailure(result, { exitCode, code, category }, label) { if (result.status !== exitCode || result.stdout.trim()) fail(`${label}_EXIT_INVALID`); const value = JSON.parse(result.stderr); if (value.ok !== false || value.code !== code || value.category !== category) fail(`${label}_ERROR_INVALID`); return value; } function parseCliError(result, code, label) { return parseCliFailure(result, { exitCode: 3, code, category: "policy" }, label); } function snapshotTree(root) { const records = []; function visit(directory, prefix = "") { for (const entry of fs.readdirSync(directory, { withFileTypes: true }).sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))) { const relative = prefix ? `${prefix}/${entry.name}` : entry.name; const absolute = path.join(directory, entry.name); const stat = fs.lstatSync(absolute); if (stat.isSymbolicLink()) fail("SNAPSHOT_SYMLINK_FORBIDDEN"); if (stat.isDirectory()) visit(absolute, relative); else if (stat.isFile()) records.push({ path: relative, size: stat.size, sha256: sha256(fs.readFileSync(absolute)) }); else fail("SNAPSHOT_SPECIAL_FILE_FORBIDDEN"); } } if (fs.existsSync(root)) visit(root); return sha256(stable(records)); } async function provisionVerifiedPreviewRuntime(aas, { cacheRoot, release, parsed }) { const scanned = aas.cache.runtimeRecords(parsed.entries, release.version); const targetPath = aas.cache.runtimeCachePath({ cacheRoot, packageVersion: release.version, integrity: release.integrity, }); fs.mkdirSync(targetPath, { recursive: true, mode: 0o700 }); for (const record of scanned.records) { const destination = path.join(targetPath, ...record.path.split("/")); fs.mkdirSync(path.dirname(destination), { recursive: true, mode: 0o700 }); fs.writeFileSync(destination, record.bytes, { flag: "wx", mode: 0o600 }); } const identity = aas.cache.validateRuntimeIdentity({ schemaVersion: 1, package: release.package, version: release.version, integrity: release.integrity, closureDigest: scanned.closureDigest, digestVersion: aas.cache.DIGEST_VERSION, assets: scanned.assets, provenance: release.provenance, }); fs.writeFileSync( path.join(targetPath, aas.cache.RUNTIME_IDENTITY_FILE), `${aas.canonicalJson(identity)}\n`, { flag: "wx", mode: 0o600 }, ); const verified = await aas.cache.runtimeStatus({ cacheRoot, packageVersion: release.version, integrity: release.integrity, closureDigest: scanned.closureDigest, }); assert.equal(verified.status, "verified"); return verified; } class JsonLineClient { constructor(script, args, cwd) { this.child = spawn(process.execPath, [script, ...args], { cwd, env: process.env, stdio: ["pipe", "pipe", "pipe"], windowsHide: true, }); this.pending = new Map(); this.buffer = ""; this.stderr = ""; this.fatalError = null; this.exit = new Promise((resolve) => { this.child.once("exit", (code, signal) => resolve({ code, signal })); }); this.child.stderr.setEncoding("utf8"); this.child.stderr.on("data", (chunk) => { this.stderr += chunk; }); this.child.stdout.setEncoding("utf8"); this.child.stdout.on("data", (chunk) => { this.buffer += chunk; while (this.buffer.includes("\n")) { const newline = this.buffer.indexOf("\n"); const line = this.buffer.slice(0, newline).replace(/\r$/, ""); this.buffer = this.buffer.slice(newline + 1); if (!line) continue; try { const message = JSON.parse(line); const waiter = this.pending.get(message.id); if (!waiter) throw new Error("AAS_PREVIEW_MCP_UNEXPECTED_RESPONSE"); this.pending.delete(message.id); waiter.resolve(message); } catch (error) { this.fatalError = error; for (const waiter of this.pending.values()) waiter.reject(error); this.pending.clear(); this.child.kill(); } } }); this.child.on("error", (error) => { for (const waiter of this.pending.values()) waiter.reject(error); this.pending.clear(); }); } notify(method, params = {}) { this.child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", method, params })}\n`); } request(id, method, params = {}) { return new Promise((resolve, reject) => { const timer = setTimeout(() => { this.pending.delete(id); reject(new Error("AAS_PREVIEW_MCP_TIMEOUT")); }, 20_000); this.pending.set(id, { resolve: (value) => { clearTimeout(timer); resolve(value); }, reject: (error) => { clearTimeout(timer); reject(error); }, }); this.child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`); }); } async close() { this.child.stdin.end(); let timer; const exit = await Promise.race([ this.exit, new Promise((_, reject) => { timer = setTimeout(() => { this.child.kill(); reject(new Error("AAS_PREVIEW_MCP_EXIT_TIMEOUT")); }, 10_000); }), ]).finally(() => clearTimeout(timer)); if (this.fatalError) throw this.fatalError; if (exit.code !== 0 || exit.signal || this.stderr.trim() || this.buffer.trim() || this.pending.size) fail("MCP_EXIT_INVALID"); } } async function main() { const args = parseArgs(process.argv.slice(2)); const tarball = path.resolve(args.tarball); const packageRoot = path.resolve(args["package-root"]); const workRoot = path.resolve(args["work-root"]); const out = path.resolve(args.out); const projectRoot = path.join(workRoot, "project"); const cacheRoot = path.join(workRoot, "cache"); fs.mkdirSync(projectRoot, { recursive: true, mode: 0o700 }); fs.mkdirSync(cacheRoot, { recursive: true, mode: 0o700 }); const projectEvidencePath = path.join(projectRoot, "README.md"); const projectEvidenceBytes = Buffer.from("# AAS packed preview project\n", "utf8"); fs.writeFileSync(projectEvidencePath, projectEvidenceBytes, { flag: "wx", mode: 0o600 }); const metadata = JSON.parse(fs.readFileSync(path.join(packageRoot, "package.json"), "utf8")); assert.equal(metadata.name, "agentic-awesome-skills"); assert.deepEqual(Object.keys(metadata.bin).sort(), ["aas", "aas-mcp", "agentic-awesome-skills"]); const aasBin = path.join(packageRoot, metadata.bin.aas); const mcpBin = path.join(packageRoot, metadata.bin["aas-mcp"]); const legacyBin = path.join(packageRoot, metadata.bin["agentic-awesome-skills"]); for (const entrypoint of [aasBin, mcpBin, legacyBin]) assert.equal(fs.statSync(entrypoint).isFile(), true); parseCliSuccess(runNode(aasBin, ["help"], { cwd: projectRoot }), "HELP"); const legacyBefore = snapshotTree(projectRoot); const legacyHelp = runNode(legacyBin, ["--help"], { cwd: projectRoot }); if (legacyHelp.status !== 0) fail("LEGACY_HELP_FAILED"); assert.equal(snapshotTree(projectRoot), legacyBefore, "legacy help changed project state"); const tarballBytes = fs.readFileSync(tarball); const runtimeIntegrity = sha512Sri(tarballBytes); const aas = require(path.join(packageRoot, "tools/lib/aas-v1/index.js")); const parsedArchive = aas.cache.parsePackageArchive(tarballBytes); const release = { package: metadata.name, version: metadata.version, integrity: runtimeIntegrity, provenance: { registryOrigin: "https://registry.npmjs.org", signaturesPresent: false, attestationsPresent: false }, }; // Windows directory-flush capability is outside the supported preview. // The preview runner materializes only its isolated test cache, then // requires the production core to verify every byte before lifecycle use. const promoted = process.platform === "win32" ? await provisionVerifiedPreviewRuntime(aas, { cacheRoot, release, parsed: parsedArchive }) : await aas.cache.promoteRuntime({ cacheRoot, release, parsed: parsedArchive }); const initializedManifestPath = path.join(workRoot, "aas-stack.initialized.json"); const previewOutputArgs = process.platform === "win32" ? ["--preview-windows-output"] : []; const initialized = parseCliSuccess(runNode(aasBin, [ "stack", "init", "--out", initializedManifestPath, "--name", "preview-smoke", "--goal", "agent-boundaries", ...previewOutputArgs, ], { cwd: projectRoot }), "INIT"); assert.equal(initialized.status, "initialized"); if (process.platform === "win32") { assert.equal(initialized.certificationStatus, "certifiable"); assert.equal(initialized.outputDurability, "directorySynced"); } const initializedManifest = JSON.parse(fs.readFileSync(initializedManifestPath, "utf8")); assert.equal(initializedManifest.schemaVersion, 2); assert.deepEqual(initializedManifest.skills, []); const selectionPath = path.join(workRoot, "selection.json"); const selection = { name: "preview-smoke", targets: [{ host: "codex", scope: "project" }], profile: { goals: ["agent-boundaries"], languages: ["javascript"], frameworks: [], constraints: [], }, skillIds: ["ai-agents-architect"], }; fs.writeFileSync(selectionPath, `${stable(selection)}\n`, { mode: 0o600 }); const manifestPath = path.join(workRoot, "aas-stack.json"); const replayManifestPath = path.join(workRoot, "aas-stack.replay.json"); const composed = parseCliSuccess(runNode(aasBin, [ "stack", "create", "--selection", selectionPath, "--out", manifestPath, ...previewOutputArgs, ], { cwd: projectRoot }), "CREATE"); const replayed = parseCliSuccess(runNode(aasBin, [ "stack", "create", "--selection", selectionPath, "--out", replayManifestPath, ...previewOutputArgs, ], { cwd: projectRoot }), "CREATE_REPLAY"); assert.equal(composed.status, "created"); assert.equal(composed.selectionSource, "agent"); assert.deepEqual(composed.selectedSkillIds, selection.skillIds); assert.equal(replayed.manifestDigest, composed.manifestDigest); assert.equal(fs.readFileSync(manifestPath, "utf8"), fs.readFileSync(replayManifestPath, "utf8"), "agent selection replay drifted"); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); assert.equal(manifest.schemaVersion, 2); assert.deepEqual(manifest.skills, [{ id: "ai-agents-architect" }]); assert.deepEqual(manifest.profile, selection.profile); const malformedSelectionPath = path.join(workRoot, "malformed-selection.json"); fs.writeFileSync(malformedSelectionPath, `${stable({ ...selection, policy: { allowedRisk: ["safe"] } })}\n`, { mode: 0o600 }); parseCliFailure( runNode(aasBin, ["stack", "create", "--selection", malformedSelectionPath, "--out", path.join(workRoot, "malformed.json")], { cwd: projectRoot }), { exitCode: 2, code: "AAS_SELECTION_INPUT_INVALID", category: "invalidInput" }, "MALFORMED_INPUT_GUARD", ); const validated = parseCliSuccess(runNode(aasBin, ["stack", "validate", "--manifest", manifestPath], { cwd: projectRoot }), "VALIDATE"); assert.equal(validated.status, "valid"); const planPath = path.join(workRoot, "plan.json"); const planned = parseCliSuccess(runNode(aasBin, [ "stack", "plan", "--manifest", manifestPath, "--target", "codex:project", "--target-root", projectRoot, "--cache-root", cacheRoot, "--runtime-version", metadata.version, "--runtime-integrity", runtimeIntegrity, "--out", planPath, ...previewOutputArgs, ], { cwd: projectRoot }), "PLAN"); assert.equal(planned.status, "planned"); if (process.platform === "win32") { assert.equal(planned.certificationStatus, "certifiable"); assert.equal(planned.outputDurability, "directorySynced"); } const plan = JSON.parse(fs.readFileSync(planPath, "utf8")); const beforeDoctor = { project: snapshotTree(projectRoot), cache: snapshotTree(cacheRoot) }; const doctor = parseCliSuccess(runNode(aasBin, [ "stack", "doctor", "--plan", planPath, "--target-root", projectRoot, "--cache-root", cacheRoot, ], { cwd: projectRoot }), "DOCTOR"); assert.equal(doctor.status, "healthy"); assert.deepEqual({ project: snapshotTree(projectRoot), cache: snapshotTree(cacheRoot) }, beforeDoctor, "doctor changed persistent state"); const beforeWriteGuards = { project: snapshotTree(projectRoot), cache: snapshotTree(cacheRoot) }; const applyError = parseCliError(runNode(aasBin, [ "stack", "apply", "--plan", planPath, "--target-root", projectRoot, "--cache-root", cacheRoot, "--approve", plan.digest, ], { cwd: projectRoot }), "AAS_STACK_APPLY_EXPERIMENTAL_DISABLED", "APPLY_GUARD"); assert.equal(applyError.details.certificationStatus, "notCertified"); parseCliError(runNode(aasBin, [ "stack", "recover", "--plan", planPath, "--target-root", projectRoot, "--cache-root", cacheRoot, "--id", "preview", "--action", "cleanup", ], { cwd: projectRoot }), "AAS_STACK_RECOVERY_EXPERIMENTAL_DISABLED", "RECOVERY_GUARD"); assert.deepEqual({ project: snapshotTree(projectRoot), cache: snapshotTree(cacheRoot) }, beforeWriteGuards, "default write guards changed persistent state"); assert.equal(fs.existsSync(path.join(projectRoot, ".agents")), false); assert.equal(fs.existsSync(path.join(projectRoot, ".aas")), false); const beforeMcp = { project: snapshotTree(projectRoot), cache: snapshotTree(cacheRoot) }; const client = new JsonLineClient(mcpBin, ["--cache-root", cacheRoot], projectRoot); const initialize = await client.request(1, "initialize", { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "aas-preview", version: "1" }, }); assert.equal(initialize.result.protocolVersion, "2025-06-18"); client.notify("notifications/initialized"); const tools = await client.request(2, "tools/list"); const toolNames = tools.result.tools.map((tool) => tool.name); assert.deepEqual(toolNames, [ "search_skills", "get_skill", "compose_stack", "inspect_stack", "diff_stack", "export_selection_evidence", "inspect_selection_evidence", ]); const templates = await client.request(3, "resources/templates/list"); assert.deepEqual(templates.result.resourceTemplates.map((item) => item.uriTemplate), ["aas://skills/{id}"]); const search = await client.request(4, "tools/call", { name: "search_skills", arguments: { query: "android ui", limit: 3 } }); assert.equal(search.result.structuredContent.ok, true); const skillId = search.result.structuredContent.results[0].id; const get = await client.request(5, "tools/call", { name: "get_skill", arguments: { id: skillId } }); assert.equal(get.result.structuredContent.skill.id, skillId); assert.equal(get.result.structuredContent.untrustedContent.authority, "untrusted"); const resource = await client.request(6, "resources/read", { uri: `aas://skills/${skillId}` }); assert.equal(resource.result.contents[0].uri, `aas://skills/${skillId}`); assert.equal(resource.result.contents[0].mimeType, "application/json"); const resourcePayload = JSON.parse(resource.result.contents[0].text); assert.equal(resourcePayload.skill.id, skillId); assert.equal(resourcePayload.untrustedContent.authority, "untrusted"); assert.equal(resourcePayload.untrustedContent.available, true); const mcpComposition = await client.request(7, "tools/call", { name: "compose_stack", arguments: selection, }); assert.equal(mcpComposition.result.structuredContent.ok, true); assert.equal(mcpComposition.result.structuredContent.selectionSource, "agent"); assert.deepEqual(mcpComposition.result.structuredContent.manifest, manifest); const inspection = await client.request(8, "tools/call", { name: "inspect_stack", arguments: { manifest } }); assert.equal(inspection.result.structuredContent.ok, true); const projectFiles = [{ path: "README.md", size: projectEvidenceBytes.length, sha256: sha256(projectEvidenceBytes), }]; const project = { schemaVersion: 1, files: projectFiles, fingerprint: aas.sha256(aas.canonicalJson({ schemaVersion: 1, files: projectFiles })), }; const dimensions = aas.evidence.DIMENSION_IDS.map((id) => ({ id, status: id === "architecture-runtime" ? "applicable" : "not-applicable", capabilityIds: id === "architecture-runtime" ? ["agent-selection-lifecycle"] : [], })); const capabilities = [{ id: "agent-selection-lifecycle", dimensionId: "architecture-runtime", status: "covered", evidence: [{ path: "README.md", sha256: projectFiles[0].sha256 }], selectedSkillIds: selection.skillIds, }]; const exported = await client.request(9, "tools/call", { name: "export_selection_evidence", arguments: { manifestDigest: composed.manifestDigest, project, dimensions, capabilities, }, }); assert.equal(exported.result.structuredContent.ok, true); assert.deepEqual(exported.result.structuredContent.evidence.payload.selectedSkillIds, selection.skillIds); const evidenceInspection = await client.request(10, "tools/call", { name: "inspect_selection_evidence", arguments: { evidence: exported.result.structuredContent.evidence, manifest }, }); assert.equal(evidenceInspection.result.structuredContent.ok, true); assert.equal(evidenceInspection.result.structuredContent.status, "valid"); const diff = await client.request(11, "tools/call", { name: "diff_stack", arguments: { stack: manifest, toCatalogDigest: manifest.catalog.integrity }, }); assert.equal(diff.result.structuredContent.ok, true); await client.close(); const afterMcp = { project: snapshotTree(projectRoot), cache: snapshotTree(cacheRoot) }; assert.deepEqual(afterMcp, beforeMcp, "MCP changed persistent project or cache state"); const receipt = { schemaVersion: 1, assuranceProfile: "agent-first-preview-1", previewQualified: true, certifiedV1: false, jobId: args["job-id"], runtime: { node: process.version, platform: process.platform, architecture: process.arch }, package: { name: metadata.name, version: metadata.version, tarballIntegrity: runtimeIntegrity, tarballSha256: sha256(tarballBytes) }, selectionDigest: sha256(fs.readFileSync(selectionPath)), mcpContractDigest: sha256(stable({ toolNames, templates: ["aas://skills/{id}"] })), lifecycle: { initialized: true, selected: true, composed: true, validated: true, planned: true, doctorReadOnly: true }, writeGuards: { applyDisabledByDefault: true, recoveryDisabledByDefault: true, targetStateCreated: false }, mcp: { localStdio: true, readOnlySnapshot: true, nativeAttemptObservation: "notEvaluated" }, runtimeCache: { integrity: promoted.runtimeIdentity.integrity, closureDigest: promoted.runtimeIdentity.closureDigest }, notEvaluated: NOT_EVALUATED, }; fs.mkdirSync(path.dirname(out), { recursive: true, mode: 0o700 }); fs.writeFileSync(out, `${stable(receipt)}\n`, { flag: "wx", mode: 0o600 }); process.stdout.write(`${stable(receipt)}\n`); } main().catch((error) => { process.stderr.write(`${error?.message || "AAS_PREVIEW_FAILED"}\n`); process.exitCode = 1; });