mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 03:36:57 +01:00
fix(hermes): preserve pinned setup configuration and verify runtime files
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
6443853823
commit
2b9156bcb6
8 files changed
+132
-9
No files matched your search
@@ -75,7 +75,7 @@
|
||||
"typecheck:browser": "tsc -p tsconfig.browser.json --noEmit",
|
||||
"test": "pnpm run test:typescript && pnpm run test:rust",
|
||||
"test:typescript": "pnpm run test:typescript:prep && vitest run",
|
||||
"test:typescript:prep": "pnpm run ensure:eval-build-deps && pnpm run build:rust && node --test test/protocol-contract.test.mjs test/acpx-sidecar-contract.test.mjs test/acpx-codex-package-contract.test.mjs test/acpx-model-selection-package-contract.test.mjs test/acpx-rich-extensions-package-contract.test.mjs test/acpx-pi-receipt-package-contract.test.mjs test/acpx-response-delivery-package-contract.test.mjs scripts/candidate-provider-pack.test.mjs scripts/cursor-provisioner-layout.test.mjs scripts/hermes-provisioner-layout.test.mjs scripts/release-provider-pack.test.mjs scripts/aws-agentcore-provisioning.test.mjs scripts/build-verified-provider-entrypoints.test.mjs scripts/local-provider-smoke-environment.test.mjs scripts/materialize-opencode-binary.test.mjs",
|
||||
"test:typescript:prep": "pnpm run ensure:eval-build-deps && pnpm run build:rust && node --test test/protocol-contract.test.mjs test/acpx-sidecar-contract.test.mjs test/acpx-codex-package-contract.test.mjs test/acpx-model-selection-package-contract.test.mjs test/acpx-rich-extensions-package-contract.test.mjs test/acpx-pi-receipt-package-contract.test.mjs test/acpx-response-delivery-package-contract.test.mjs scripts/candidate-provider-pack.test.mjs scripts/cursor-provisioner-layout.test.mjs scripts/hermes-provisioner-layout.test.mjs scripts/hermes-provisioning-config.test.mjs scripts/release-provider-pack.test.mjs scripts/aws-agentcore-provisioning.test.mjs scripts/build-verified-provider-entrypoints.test.mjs scripts/local-provider-smoke-environment.test.mjs scripts/materialize-opencode-binary.test.mjs",
|
||||
"test:typescript:vitest": "node ./scripts/run-pr-vitest-lane.mjs",
|
||||
"test:rust": "cargo test --release --manifest-path runner/Cargo.toml --locked --workspace",
|
||||
"test:codex": "cargo test --manifest-path runner/Cargo.toml --locked -p paperclip-runner-core --test codex_provider",
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/** Project settings come only from the already archive-authenticated source.
|
||||
* --config-file prevents uv from merging operator or system configuration.
|
||||
* This extraction supports the pinned upstream table layout, not arbitrary TOML.
|
||||
*/
|
||||
export function hermesProvisioningConfig(pyproject) {
|
||||
const lines = pyproject.split(/\r?\n/);
|
||||
const roots = lines.flatMap((line, index) => line === "[tool.uv]" ? [index] : []);
|
||||
if (roots.length !== 1) throw new Error("Hermes pinned source must contain one uv configuration table");
|
||||
const body = [];
|
||||
for (const line of lines.slice(roots[0] + 1)) {
|
||||
if (line.startsWith("[")) {
|
||||
if (!line.startsWith("[tool.uv.")) break;
|
||||
if (!/^\[tool\.uv\.[a-zA-Z][a-zA-Z0-9_.-]*\]$/.test(line)) throw new Error("Hermes pinned uv table layout is unsupported");
|
||||
body.push(line.replace("[tool.uv.", "["));
|
||||
} else body.push(line);
|
||||
}
|
||||
return body.join("\n") + "\n";
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { hermesProvisioningConfig } from "./hermes-provisioning-config.mjs";
|
||||
|
||||
test("preserves pinned resolver context without selecting unrelated project tables", () => {
|
||||
const settings = 'override-dependencies = ["cryptography>=50,<51"]\nexclude-newer = "14 days"\n\n[exclude-newer-package]\nanthropic = false\n';
|
||||
assert.equal(hermesProvisioningConfig('[project]\nname = "hermes-agent"\n[tool.uv]\n' + settings.replace("[exclude-newer-package]", "[tool.uv.exclude-newer-package]") + '[tool.setuptools]\npackages = ["ambient"]\n'), settings);
|
||||
});
|
||||
|
||||
test("rejects missing, repeated or unsupported pinned uv table layouts", () => {
|
||||
for (const source of ['[project]\nname = "hermes-agent"', '[tool.uv]\n[tool.uv]\n', '[tool.uv]\n[tool.uv."unsupported"]\n']) {
|
||||
assert.throws(() => hermesProvisioningConfig(source), /pinned/);
|
||||
}
|
||||
});
|
||||
@@ -4,6 +4,7 @@ import { access, lstat, mkdir, mkdtemp, readFile, realpath, rename, rm, writeFil
|
||||
import { dirname, isAbsolute, join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { HERMES_CLOSURES } from "../src/drivers/acpx/hermes-distributions.ts";
|
||||
import { hermesProvisioningConfig } from "./hermes-provisioning-config.mjs";
|
||||
|
||||
const digest = bytes => createHash("sha256").update(bytes).digest("hex");
|
||||
|
||||
@@ -21,7 +22,7 @@ export async function materializePinnedHermesDistribution({ destination, provide
|
||||
const source = join(temporary, "source");
|
||||
const prepared = join(temporary, "distribution");
|
||||
const env = { PATH: process.env.PATH ?? "/usr/bin:/bin", LANG: "C.UTF-8", HOME: join(temporary, "home"),
|
||||
UV_CACHE_DIR: join(temporary, "uv-cache"), UV_PYTHON_INSTALL_DIR: join(temporary, "uv-python"), UV_NO_CONFIG: "1" };
|
||||
UV_CACHE_DIR: join(temporary, "uv-cache"), UV_PYTHON_INSTALL_DIR: join(temporary, "uv-python") };
|
||||
try {
|
||||
await mkdir(source, { mode: 0o700 });
|
||||
await mkdir(env.HOME, { mode: 0o700 });
|
||||
@@ -37,8 +38,11 @@ export async function materializePinnedHermesDistribution({ destination, provide
|
||||
await writeFile(archive, bytes, { mode: 0o600, flag: "wx" });
|
||||
execFileSync("tar", ["-xzf", archive, "--strip-components=1", "-C", source], { env, timeout: 30000 });
|
||||
if (digest(await readFile(join(source, "uv.lock"))) !== version.lockSha256) throw new Error("Hermes dependency lock digest mismatch");
|
||||
execFileSync("uv", ["sync", "--locked", "--no-dev", "--no-install-project", "--python", version.python,
|
||||
const configFile = join(temporary, "uv.toml");
|
||||
await writeFile(configFile, hermesProvisioningConfig(await readFile(join(source, "pyproject.toml"), "utf8")), { mode: 0o600, flag: "wx" });
|
||||
execFileSync("uv", ["sync", "--locked", "--config-file", configFile, "--no-dev", "--no-install-project", "--python", version.python,
|
||||
"--extra", "acp", "--extra", "mcp", "--extra", "anthropic", "--extra", "bedrock", "--extra", "google"], { cwd: source, env, stdio: "inherit", timeout: 300000 });
|
||||
if (digest(await readFile(join(source, "uv.lock"))) !== version.lockSha256) throw new Error("Hermes dependency lock changed during installation");
|
||||
execFileSync(join(source, ".venv/bin/python"), [materializer, source, prepared, provider], { env, stdio: "inherit", timeout: 120000 });
|
||||
const manifest = JSON.parse(await readFile(join(prepared, "manifest.json"), "utf8"));
|
||||
if (digest(JSON.stringify(manifest.entries)) !== expectedClosureSha256) throw new Error("Hermes provisioned closure does not match its reviewed pin");
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import { lstat, readFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { HERMES_CLOSURES } from "../src/drivers/acpx/hermes-distributions.ts";
|
||||
import { verifyNativeAcpxInstallation } from "../src/drivers/acpx/installation-integrity.ts";
|
||||
import { verifyHermesRuntimeFiles } from "../src/drivers/acpx/hermes-setup-integrity.ts";
|
||||
import { hermesProvisionerDestination, hermesProvisionerLayout } from "./hermes-provisioner-layout.mjs";
|
||||
import { materializePinnedHermesDistribution } from "./provision-hermes.mjs";
|
||||
|
||||
@@ -14,14 +14,12 @@ export async function provisionHermesRuntime() {
|
||||
const closureSha256 = HERMES_CLOSURES[platform];
|
||||
if (!closureSha256) throw new Error(`Hermes has no reviewed distribution for ${platform}`);
|
||||
const destination = hermesProvisionerDestination(import.meta.url, closureSha256, process.platform, process.arch);
|
||||
const verify = (distributionRoot, expectedClosureSha256) => verifyNativeAcpxInstallation({ distributionRoot, expectedClosureSha256,
|
||||
manifestPath: join(distributionRoot, "manifest.json"), executable: "python/bin/python3.12", pythonEntrypoint: "entry.py", fixedArguments: [] });
|
||||
const installed = await lstat(destination).catch(error => { if (error.code !== "ENOENT") throw error; return null; });
|
||||
if (installed) {
|
||||
if (!installed.isDirectory() || installed.isSymbolicLink()) throw new Error("Hermes runtime assets must be a real directory");
|
||||
await verify(destination, closureSha256);
|
||||
await verifyHermesRuntimeFiles(destination, closureSha256);
|
||||
} else {
|
||||
await materializePinnedHermesDistribution({ destination, provider: layout.provider, materializer: layout.materializer, verify });
|
||||
await materializePinnedHermesDistribution({ destination, provider: layout.provider, materializer: layout.materializer, verify: verifyHermesRuntimeFiles });
|
||||
}
|
||||
const version = JSON.parse(await readFile(join(layout.provider, "version.json"), "utf8"));
|
||||
console.log(`Verified Hermes ${version.release} (${platform}), Python ${version.python}`);
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { chmod, lstat, mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { expect, it } from "vitest";
|
||||
import { verifyHermesRuntimeFiles } from "./hermes-setup-integrity.js";
|
||||
|
||||
async function distribution(run: (root: string, pin: string) => Promise<void>) {
|
||||
const root = await realpath(await mkdtemp(join(tmpdir(), "paperclip-hermes-setup-test-")));
|
||||
try {
|
||||
await mkdir(join(root, "python/bin"), { recursive: true });
|
||||
const files = ["bridge.py", "entry.py", "python/bin/python3.12"];
|
||||
for (const path of files) {
|
||||
await writeFile(join(root, path), path.endsWith("python3.12") ? '#!/bin/sh\ntouch "$PWD/unexpected-spawn"\n' : "# verified\n");
|
||||
await chmod(join(root, path), path.endsWith("python3.12") ? 0o700 : 0o600);
|
||||
}
|
||||
const entries = await Promise.all(files.map(async path => ({
|
||||
path, sha256: createHash("sha256").update(await readFile(join(root, path))).digest("hex"),
|
||||
size: (await lstat(join(root, path))).size, executable: path.endsWith("python3.12"),
|
||||
})));
|
||||
await writeFile(join(root, "manifest.json"), JSON.stringify({ entries }));
|
||||
await run(root, createHash("sha256").update(JSON.stringify(entries)).digest("hex"));
|
||||
} finally { await rm(root, { recursive: true, force: true }); }
|
||||
}
|
||||
|
||||
it("checks intact runtime files without executing the interpreter", async () => {
|
||||
await distribution(async (root, pin) => {
|
||||
await verifyHermesRuntimeFiles(root, pin);
|
||||
await expect(lstat(join(root, "unexpected-spawn"))).rejects.toMatchObject({ code: "ENOENT" });
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects modified runtime bytes even with an unchanged valid manifest", async () => {
|
||||
await distribution(async (root, pin) => {
|
||||
await writeFile(join(root, "bridge.py"), "# modified\n");
|
||||
await expect(verifyHermesRuntimeFiles(root, pin)).rejects.toThrow("file digest mismatch");
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects a missing interpreter even with an unchanged valid manifest", async () => {
|
||||
await distribution(async (root, pin) => {
|
||||
await rm(join(root, "python/bin/python3.12"));
|
||||
await expect(verifyHermesRuntimeFiles(root, pin)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects a symbolic link substituted for the native entrypoint", async () => {
|
||||
await distribution(async (root, pin) => {
|
||||
await rm(join(root, "entry.py"));
|
||||
await symlink("bridge.py", join(root, "entry.py"));
|
||||
await expect(verifyHermesRuntimeFiles(root, pin)).rejects.toThrow("symbolic link");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { join } from "node:path";
|
||||
import { verifyNativeAcpxInstallation } from "./installation-integrity.js";
|
||||
|
||||
/** Setup verifies the same execution bytes as admission without spawning Hermes. */
|
||||
export async function verifyHermesRuntimeFiles(distributionRoot: string, expectedClosureSha256: string): Promise<void> {
|
||||
const installation = await verifyNativeAcpxInstallation({
|
||||
distributionRoot, expectedClosureSha256, manifestPath: join(distributionRoot, "manifest.json"),
|
||||
executable: "python/bin/python3.12", pythonEntrypoint: "entry.py", fixedArguments: [],
|
||||
});
|
||||
const lease = await installation.openCommand();
|
||||
await lease.close();
|
||||
}
|
||||
Reference in new issue
Block a user