The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
test(module-pins): cover multi-file source parity proofs
Steven Enamakel764c542bede7
diff --git a/scripts/__tests__/module-pins.test.mjs b/scripts/__tests__/module-pins.test.mjsindex f7c76bf993..4aa294c0da 100644--- a/scripts/__tests__/module-pins.test.mjs+++ b/scripts/__tests__/module-pins.test.mjs@@ -321,6 +321,102 @@ function sourceParityFixture() {.digest("hex");return { root, git, releaseCommit, candidateCommit, parity };}++function sourceParitySetFixture() {+ const root = mkdtempSync(join(tmpdir(), "module-source-parity-set-"));+ const git = (args) =>+ execFileSync("git", args, { cwd: root, stdio: "ignore" });+ git(["init", "-q"]);+ git(["config", "user.email", "[email protected]"]);+ git(["config", "user.name", "Pin Check"]);+ git(["config", "commit.gpgsign", "false"]);+ git(["config", "tag.gpgsign", "false"]);+ mkdirSync(join(root, "src"), { recursive: true });+ writeFileSync(join(root, "Cargo.toml"), '[package]\nname = "fixture"\nversion = "1.0.0"\n');+ writeFileSync(join(root, "src/lib.rs"), "pub mod alpha;\npub mod beta;\npub mod gamma;\n");++ const paths = ["src/alpha_tests.rs", "src/beta_tests.rs", "src/gamma_tests.rs"];+ const modulePaths = ["src/alpha.rs", "src/beta.rs", "src/gamma.rs"];+ for (let index = 0; index < paths.length; index += 1) {+ writeFileSync(+ join(root, modulePaths[index]),+ `#[cfg(test)]\n#[path = "${paths[index].split("/").at(-1)}"]\nmod tests;\npub fn value() -> u8 { ${index} }\n`,+ );+ writeFileSync(join(root, paths[index]), `#[test] fn works_${index}() {}\n`);+ }+ git(["add", "."]);+ git(["commit", "-qm", "release"]);+ git(["tag", "-a", "-m", "release", "v1.0.0"]);+ const releaseCommit = execFileSync("git", ["rev-parse", "HEAD"], {+ cwd: root,+ encoding: "utf8",+ }).trim();+ for (let index = 0; index < paths.length; index += 1) {+ writeFileSync(+ join(root, paths[index]),+ `#[test] fn works_${index}() { assert_eq!(${index}, ${index}); }\n`,+ );+ }+ git(["add", "."]);+ git(["commit", "-qm", "owner test set"]);+ const candidateCommit = execFileSync("git", ["rev-parse", "HEAD"], {+ cwd: root,+ encoding: "utf8",+ }).trim();+ const blobAt = (commit, path) =>+ execFileSync("git", ["rev-parse", `${commit}:${path}`], {+ cwd: root,+ encoding: "utf8",+ }).trim();+ const testFiles = paths.map((path) => ({+ path,+ releaseMode: "100644",+ releaseBlob: blobAt(releaseCommit, path),+ candidateMode: "100644",+ candidateBlob: blobAt(candidateCommit, path),+ }));+ const bindings = modulePaths.map((modulePath, index) => ({+ modulePath,+ moduleBlob: blobAt(candidateCommit, modulePath),+ testPath: paths[index],+ }));+ const canonicalTree = execFileSync(+ "git",+ ["ls-tree", "-r", "--full-tree", "-z", releaseCommit],+ { cwd: root, encoding: "utf8" },+ )+ .split("\0")+ .filter(Boolean)+ .map((entry) => {+ const match = entry.match(+ /^([0-7]{6}) (blob|commit) ([0-9a-f]{40})\t([\s\S]+)$/,+ );+ if (paths.includes(match[4])) return null;+ return `${match[1]} ${match[2]} ${match[3]}\t${match[4]}\n`;+ })+ .filter(Boolean)+ .sort()+ .join("");+ const parity = {+ tag: "v1.0.0",+ releaseCommit,+ candidateCommit,+ assetsSha256: createHash("sha256")+ .update(+ JSON.stringify({+ version: "1.0.0",+ assets: [+ { hostKey: "linux", archive: "fixture.tar.gz", sha256: "a".repeat(64) },+ ],+ }),+ )+ .digest("hex"),+ testFiles,+ unchangedTreeSha256: createHash("sha256").update(canonicalTree).digest("hex"),+ bindings,+ };+ return { root, git, releaseCommit, candidateCommit, parity, paths };+}const provider = {id: "tinyx-lang",version: "0.2.4",@@ -348,6 +444,96 @@ test("source parity proof accepts only its exact clean test-only Git pair", () =}});+test("source parity proof accepts an explicit set of exact test-only files", () => {+ const fixture = sourceParitySetFixture();+ try {+ const result = validateTestOnlySourceParity({+ repoPath: fixture.root,+ version: "1.0.0",+ assets: [+ { hostKey: "linux", archive: "fixture.tar.gz", sha256: "a".repeat(64) },+ ],+ actualHead: fixture.candidateCommit,+ proof: fixture.parity,+ });+ assert.equal(result.ok, true, result.message);+ } finally {+ rmSync(fixture.root, { recursive: true, force: true });+ }+});++test("multi-file source parity rejects omitted files, extra changes, and widened descendants", () => {+ const assets = [+ { hostKey: "linux", archive: "fixture.tar.gz", sha256: "a".repeat(64) },+ ];+ const validate = (fixture, proof = fixture.parity, actualHead = fixture.candidateCommit) =>+ validateTestOnlySourceParity({+ repoPath: fixture.root,+ version: "1.0.0",+ assets,+ actualHead,+ proof,+ });+ for (const changedPath of ["Cargo.toml", "src/lib.rs", "src/extra.rs"]) {+ const fixture = sourceParitySetFixture();+ try {+ writeFileSync(join(fixture.root, changedPath), "unattested source change\n");+ fixture.git(["add", changedPath]);+ fixture.git(["commit", "-qm", `change ${changedPath}`]);+ const head = execFileSync("git", ["rev-parse", "HEAD"], {+ cwd: fixture.root,+ encoding: "utf8",+ }).trim();+ const result = validate(fixture, {+ ...fixture.parity,+ candidateCommit: head,+ }, head);+ assert.equal(result.ok, false, `${changedPath} was accepted`);+ assert.match(result.message, /other source, manifest, lock, build input, or gitlink differs/);+ } finally {+ rmSync(fixture.root, { recursive: true, force: true });+ }+ }++ const fixture = sourceParitySetFixture();+ try {+ const omitted = validate(fixture, {+ ...fixture.parity,+ testFiles: fixture.parity.testFiles.slice(0, 2),+ bindings: fixture.parity.bindings.slice(0, 2),+ });+ assert.equal(omitted.ok, false, "a changed test file was omitted from the proof set");+ assert.match(omitted.message, /other source, manifest, lock, build input, or gitlink differs/);++ const duplicate = validate(fixture, {+ ...fixture.parity,+ testFiles: [...fixture.parity.testFiles, fixture.parity.testFiles[0]],+ bindings: [...fixture.parity.bindings, fixture.parity.bindings[0]],+ });+ assert.equal(duplicate.ok, false, "duplicate proof paths were accepted");+ assert.match(duplicate.message, /unique safe Rust source paths/);++ const widenedFile = validate(fixture, {+ ...fixture.parity,+ testFiles: fixture.parity.testFiles.map((file, index) =>+ index === 0 ? { ...file, candidateMode: "100755" } : file,+ ),+ });+ assert.equal(widenedFile.ok, false, "executable test path was accepted");+ assert.match(widenedFile.message, /regular non-executable/);++ const ambiguousShape = validate(fixture, {+ ...fixture.parity,+ testFile: fixture.parity.testFiles[0],+ binding: fixture.parity.bindings[0],+ });+ assert.equal(ambiguousShape.ok, false, "mixed single and set proof shapes were accepted");+ assert.match(ambiguousShape.message, /proof fields are missing or unknown/);+ } finally {+ rmSync(fixture.root, { recursive: true, force: true });+ }+});+test("the existing exact-drift exemption is proof-checked and keeps ahead visible", () => {const fixture = sourceParityFixture();try {diff --git a/scripts/lib/module-pins.mjs b/scripts/lib/module-pins.mjsindex 30f129d700..28613800ab 100644--- a/scripts/lib/module-pins.mjs+++ b/scripts/lib/module-pins.mjs@@ -191,10 +191,11 @@ export function classifyPin({/*** Verify one reviewed, exact source-parity attestation for a test-only commit.- * This deliberately proves a named Git pair and a named test file; it does not- * infer Rust test-only semantics for arbitrary diffs. The asset digest covers- * the registry version plus assets sorted by host key; the source digest covers- * sorted `mode type oid<TAB>path<LF>` entries other than the named test file.+ * The proof names either one test file (the original format) or an explicit+ * set of test files. It does not infer Rust test-only semantics for arbitrary+ * diffs. The asset digest covers the registry version plus assets sorted by+ * host key; the source digest covers sorted `mode type oid<TAB>path<LF>` entries+ * other than the named test file(s).*/export function validateTestOnlySourceParity({repoPath,@@ -213,19 +214,23 @@ export function validateTestOnlySourceParity({if (typeof repoPath !== "string" || repoPath.length === 0) {throw new Error("submodule repository path is missing");}- const required = [+ const commonFields = ["tag","releaseCommit","candidateCommit","assetsSha256",- "testFile","unchangedTreeSha256",- "binding",];- if (- !proof ||- Object.keys(proof).sort().join(",") !== required.sort().join(",")- ) {+ const singleFileProof =+ proof && Object.hasOwn(proof, "testFile") && !Object.hasOwn(proof, "testFiles");+ const fileSetProof =+ proof && Object.hasOwn(proof, "testFiles") && !Object.hasOwn(proof, "testFile");+ const expectedFields = singleFileProof+ ? [...commonFields, "testFile", "binding"]+ : fileSetProof+ ? [...commonFields, "testFiles", "bindings"]+ : [];+ if (!proof || Object.keys(proof).sort().join(",") !== expectedFields.sort().join(",")) {throw new Error("proof fields are missing or unknown");}if (!/^v\d+\.\d+\.\d+$/.test(proof.tag) || proof.tag !== `v${version}`) {@@ -286,43 +291,59 @@ export function validateTestOnlySourceParity({throw new Error("unchanged-tree digest is malformed");}- const file = proof.testFile;- if (- !file ||- Object.keys(file).sort().join(",") !==- "candidateBlob,candidateMode,path,releaseBlob,releaseMode"- ) {- throw new Error("test-file proof fields are missing or unknown");- }const validRepoPath = (value) =>typeof value === "string" &&/^[A-Za-z0-9_./-]+$/.test(value) &&!posix.isAbsolute(value) &&!value.split("/").includes("..");- if (- !proof.binding ||- !validRepoPath(file.path) ||- !validRepoPath(proof.binding.modulePath) ||- !file.path.endsWith(".rs") ||- file.path !== proof.binding.testPath ||- posix.dirname(file.path) !== posix.dirname(proof.binding.modulePath)- ) {- throw new Error(- "test path is not a safe sibling of the attested module binding",- );+ const files = singleFileProof ? [proof.testFile] : proof.testFiles;+ const bindings = singleFileProof ? [proof.binding] : proof.bindings;+ if (!Array.isArray(files) || files.length === 0 || !Array.isArray(bindings) || bindings.length !== files.length) {+ throw new Error("test-file set and module bindings must be non-empty and have equal length");}- if (file.releaseMode !== "100644" || file.candidateMode !== "100644") {- throw new Error(- "attested test file must remain a regular non-executable file",- );+ const filePaths = new Set();+ const bindingsByPath = new Map();+ for (const file of files) {+ if (+ !file ||+ Object.keys(file).sort().join(",") !==+ "candidateBlob,candidateMode,path,releaseBlob,releaseMode"+ ) {+ throw new Error("test-file proof fields are missing or unknown");+ }+ if (!validRepoPath(file.path) || !file.path.endsWith(".rs") || filePaths.has(file.path)) {+ throw new Error("test-file paths must be unique safe Rust source paths");+ }+ filePaths.add(file.path);+ if (file.releaseMode !== "100644" || file.candidateMode !== "100644") {+ throw new Error("attested test file must remain a regular non-executable file");+ }+ if (+ ![file.releaseBlob, file.candidateBlob].every((sha) =>+ /^[0-9a-f]{40}$/.test(sha),+ ) ||+ file.releaseBlob === file.candidateBlob+ ) {+ throw new Error("test-file blob pair is malformed or unchanged");+ }}- if (- ![file.releaseBlob, file.candidateBlob].every((sha) =>- /^[0-9a-f]{40}$/.test(sha),- ) ||- file.releaseBlob === file.candidateBlob- ) {- throw new Error("test-file blob pair is malformed or unchanged");+ for (const binding of bindings) {+ if (+ !binding ||+ Object.keys(binding).sort().join(",") !== "moduleBlob,modulePath,testPath" ||+ !validRepoPath(binding.modulePath) ||+ !validRepoPath(binding.testPath) ||+ !/^[0-9a-f]{40}$/.test(binding.moduleBlob) ||+ !filePaths.has(binding.testPath) ||+ posix.dirname(binding.testPath) !== posix.dirname(binding.modulePath) ||+ bindingsByPath.has(binding.testPath)+ ) {+ throw new Error("test path is not a safe sibling of an exact module binding");+ }+ bindingsByPath.set(binding.testPath, binding);+ }+ if (bindingsByPath.size !== filePaths.size) {+ throw new Error("every changed test file must have exactly one module binding");}const entries = (ref) =>runGit(["ls-tree", "-r", "--full-tree", "-z", ref])@@ -345,27 +366,27 @@ export function validateTestOnlySourceParity({});const releaseEntries = entries(proof.releaseCommit);const candidateEntries = entries(proof.candidateCommit);- const select = (items) => items.find((item) => item.path === file.path);- const releaseFile = select(releaseEntries);- const candidateFile = select(candidateEntries);- if (- !releaseFile ||- !candidateFile ||- releaseFile.mode !== file.releaseMode ||- candidateFile.mode !== file.candidateMode ||- releaseFile.type !== "blob" ||- candidateFile.type !== "blob" ||- releaseFile.oid !== file.releaseBlob ||- candidateFile.oid !== file.candidateBlob- ) {- throw new Error(- "attested test file path, mode, or blob does not match the Git trees",- );+ for (const file of files) {+ const releaseFile = releaseEntries.find((item) => item.path === file.path);+ const candidateFile = candidateEntries.find((item) => item.path === file.path);+ if (+ !releaseFile ||+ !candidateFile ||+ releaseFile.mode !== file.releaseMode ||+ candidateFile.mode !== file.candidateMode ||+ releaseFile.type !== "blob" ||+ candidateFile.type !== "blob" ||+ releaseFile.oid !== file.releaseBlob ||+ candidateFile.oid !== file.candidateBlob+ ) {+ throw new Error(`attested test file ${file.path} path, mode, or blob does not match the Git trees`);+ }}+ const attestedPaths = new Set(files.map(({ path }) => path));const treeHash = (items) =>sha256(items- .filter((item) => item.path !== file.path)+ .filter((item) => !attestedPaths.has(item.path)).map(({ mode, type, oid, path }) => `${mode} ${type} ${oid}\t${path}\n`,)@@ -383,35 +404,28 @@ export function validateTestOnlySourceParity({);}- const binding = proof.binding;- if (- !binding ||- Object.keys(binding).sort().join(",") !==- "moduleBlob,modulePath,testPath" ||- !/^[0-9a-f]{40}$/.test(binding.moduleBlob)- ) {- throw new Error("module binding proof is malformed");- }- const moduleEntry = candidateEntries.find(- (item) => item.path === binding.modulePath,- );- if (- !moduleEntry ||- moduleEntry.type !== "blob" ||- moduleEntry.mode !== "100644" ||- moduleEntry.oid !== binding.moduleBlob- ) {- throw new Error(- "test module declaration source differs from the attested blob",+ for (const binding of bindings) {+ const moduleEntry = candidateEntries.find(+ (item) => item.path === binding.modulePath,);- }- const moduleSource = runGit([- "show",- `${proof.candidateCommit}:${binding.modulePath}`,- ]);- const declaration = `#[cfg(test)]\n#[path = "${file.path.split("/").at(-1)}"]\nmod tests;`;- if (!moduleSource.includes(declaration)) {- throw new Error("the exact cfg(test) sibling module binding is absent");+ if (+ !moduleEntry ||+ moduleEntry.type !== "blob" ||+ moduleEntry.mode !== "100644" ||+ moduleEntry.oid !== binding.moduleBlob+ ) {+ throw new Error(+ "test module declaration source differs from the attested blob",+ );+ }+ const moduleSource = runGit([+ "show",+ `${proof.candidateCommit}:${binding.modulePath}`,+ ]);+ const declaration = `#[cfg(test)]\n#[path = "${binding.testPath.split("/").at(-1)}"]\nmod tests;`;+ if (!moduleSource.includes(declaration)) {+ throw new Error(`the exact cfg(test) sibling module binding for ${binding.testPath} is absent`);+ }}return { ok: true };} catch (error) {