mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 21:21:27 +01:00
Replaces the legacy 853 MiB IDE-extension ACP bundle (agy_acp_server.par 1.5.2, localharness_external, and Python admission patching) and the separate 1.5.15 CLI pin with a single pinned agy 1.5.19 binary (1.5.19-4821414830800896, ~268 MiB on linux-x64) across ordinary turns, /swarm, sign-in, and model discovery. - Pin agy 1.5.19 across darwin-arm64, darwin-x64, linux-x64, and linux-arm64 and bump the Antigravity ACPX profile to v3 (regenerating TS and Rust profiles). - Add direct ACP runtime openAntigravityAcpRuntime for ordinary turns with OAuth and GEMINI_API_KEY auth, session/resume, plan mode, MCP bridge, setModel, and lineage retirement. - Stage file-backed OAuth tokens with AGY_CLI_FORCE_FILE_TOKEN_STORAGE=1, align --gemini-dir with agentHomeDirectory, and migrate login to agy acp --login. - Enable remote /swarm using --dangerously-skip-permissions and remove the input.remote refusal in antigravity-swarm.ts. - Regenerate the 25-model Antigravity catalog from agy 1.5.19. Tested with Runner unit/script/live suites, shared, UI, server, Rust cargo test, runner-e2e unit suites, and live E2E against real agy 1.5.19 on macOS and headless Linux Docker (--security-opt no-new-privileges). Co-Authored-By: Paperclip <noreply@paperclip.ing>
283 lines
16 KiB
JavaScript
283 lines
16 KiB
JavaScript
import { createHash } from "node:crypto";
|
|
import { constants } from "node:fs";
|
|
import { chmod, lstat, mkdir, mkdtemp, open, readdir, realpath, rename, rm, writeFile } from "node:fs/promises";
|
|
import { basename, dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
import { isDeepStrictEqual } from "node:util";
|
|
import { gunzipSync } from "node:zlib";
|
|
|
|
const MANIFEST_PATH = fileURLToPath(new URL("../antigravity-distributions.json", import.meta.url));
|
|
// The Linux x64 CLI archive is about 104 MB; its unpacked Go binary is about 281 MB.
|
|
const MAX_ARCHIVE_BYTES = 192 * 1024 * 1024;
|
|
const MAX_TREE_BYTES = 512 * 1024 * 1024;
|
|
export const ANTIGRAVITY_TREE_MANIFEST = ".paperclip-antigravity-closure.json";
|
|
const EXECUTABLE = "agy";
|
|
const ARCHIVE_MEMBER = "antigravity";
|
|
const RUNTIME_FILES = [EXECUTABLE];
|
|
const SHA256 = /^[a-f0-9]{64}$/;
|
|
const SHA512 = /^[a-f0-9]{128}$/;
|
|
const digest = (bytes, algorithm = "sha256") => createHash(algorithm).update(bytes).digest("hex");
|
|
|
|
function assertTrustedOwner(stat) {
|
|
const uid = process.getuid?.();
|
|
if (!Number.isInteger(uid) || !Number.isInteger(stat.uid) || (stat.uid !== uid && stat.uid !== 0)) {
|
|
throw new Error("Antigravity assets have an untrusted filesystem owner");
|
|
}
|
|
}
|
|
|
|
/** Resolve parent aliases once, then use only the checked canonical path.
|
|
* A root-owned sticky temporary directory protects each owned child entry. */
|
|
export async function ownedDirectory(directory, { privateRoot = false, parentAlias = false } = {}) {
|
|
const original = await lstat(directory);
|
|
if ((!original.isDirectory() || original.isSymbolicLink()) && !(parentAlias && original.isSymbolicLink())) {
|
|
throw new Error("Antigravity closure must be a real directory with private write access");
|
|
}
|
|
const canonical = await realpath(directory);
|
|
let current = canonical;
|
|
let root;
|
|
while (true) {
|
|
const stat = await lstat(current);
|
|
if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("Antigravity assets have an unsafe ancestor directory");
|
|
assertTrustedOwner(stat);
|
|
if (!root) root = stat;
|
|
const trustedSticky = stat.uid === 0 && (stat.mode & 0o1000) !== 0;
|
|
if ((stat.mode & 0o022) && !(trustedSticky && !(privateRoot && current === canonical))) {
|
|
throw new Error("Antigravity assets have an unsafe ancestor with shared write access; closures require private write access");
|
|
}
|
|
const parent = dirname(current);
|
|
if (parent === current) break;
|
|
current = parent;
|
|
}
|
|
if (!original.isSymbolicLink() && (root.dev !== original.dev || root.ino !== original.ino)) {
|
|
throw new Error("Antigravity asset directory changed during ownership verification");
|
|
}
|
|
return { directory: canonical, stat: root };
|
|
}
|
|
|
|
export function assertOwnedFile(stat, maximum) {
|
|
if (!stat.isFile() || stat.nlink !== 1) throw new Error("Antigravity closure contains links or special files");
|
|
assertTrustedOwner(stat);
|
|
if (stat.mode & 0o7022) throw new Error("Antigravity closure contains unsafe file permissions");
|
|
if (stat.size > maximum) throw new Error("Antigravity asset exceeds its file size limit");
|
|
}
|
|
|
|
export async function openOwnedFile(path, maximum) {
|
|
const parent = await ownedDirectory(dirname(path), { parentAlias: true });
|
|
const canonical = join(parent.directory, basename(path));
|
|
const before = await lstat(canonical);
|
|
assertOwnedFile(before, maximum);
|
|
const file = await open(canonical, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
try {
|
|
const stat = await file.stat();
|
|
assertOwnedFile(stat, maximum);
|
|
if (stat.dev !== before.dev || stat.ino !== before.ino) throw new Error("Antigravity asset changed while opening");
|
|
return { file, path: canonical, before: stat };
|
|
} catch (error) { await file.close(); throw error; }
|
|
}
|
|
|
|
export async function readOwnedFile(path, maximum) {
|
|
const { file, before } = await openOwnedFile(path, maximum);
|
|
try {
|
|
const bytes = await file.readFile();
|
|
const after = await file.stat();
|
|
assertOwnedFile(after, maximum);
|
|
if (bytes.length > maximum || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs) {
|
|
throw new Error("Antigravity asset changed while reading");
|
|
}
|
|
return bytes;
|
|
} finally { await file.close(); }
|
|
}
|
|
|
|
/** Reject an unsafe existing ancestor before creating even a staging directory. */
|
|
export async function ownedDestination(destination) {
|
|
const missing = [];
|
|
let ancestor = dirname(destination);
|
|
while (true) {
|
|
try { await lstat(ancestor); break; }
|
|
catch (error) {
|
|
if (error.code !== "ENOENT") throw error;
|
|
const parent = dirname(ancestor);
|
|
if (parent === ancestor) throw error;
|
|
missing.unshift(basename(ancestor));
|
|
ancestor = parent;
|
|
}
|
|
}
|
|
let current = (await ownedDirectory(ancestor, { parentAlias: true })).directory;
|
|
for (const segment of missing) {
|
|
current = join(current, segment);
|
|
await mkdir(current, { mode: 0o700 });
|
|
current = (await ownedDirectory(current)).directory;
|
|
}
|
|
return join(current, basename(destination));
|
|
}
|
|
|
|
function octal(field) {
|
|
const text = field.toString("latin1").replace(/[\0 ]+$/, "").replace(/^ +/, "");
|
|
if (!/^[0-7]{1,12}$/.test(text)) return -1;
|
|
return Number.parseInt(text, 8);
|
|
}
|
|
|
|
/** Only the checked-in declaration selects downloads and runtime contents. */
|
|
export async function antigravityDistribution(platform = process.platform, architecture = process.arch) {
|
|
const manifest = JSON.parse((await readOwnedFile(MANIFEST_PATH, 64 * 1024)).toString("utf8"));
|
|
const item = manifest.platforms?.[`${platform}-${architecture}`];
|
|
if (manifest.schema !== "paperclip.antigravity_distribution.v1" || !/^\d+\.\d+\.\d+$/.test(manifest.version)
|
|
|| typeof manifest.build !== "string" || !manifest.build.startsWith(`${manifest.version}-`) || !/^\d+\.\d+\.\d+-\d+$/.test(manifest.build)) {
|
|
throw new Error("Invalid Antigravity distribution declaration");
|
|
}
|
|
if (!item) throw new Error(`Antigravity has no pinned distribution for ${platform}/${architecture}`);
|
|
if (item.provisioning !== "verified-closure") throw new Error(`Antigravity ${platform}/${architecture} is inventory-only; its complete runtime closure is not verified`);
|
|
const url = new URL(item.url);
|
|
if (url.protocol !== "https:" || url.hostname !== "storage.googleapis.com" || url.port || url.username || url.password || url.search || url.hash
|
|
|| !url.pathname.startsWith(`/antigravity-public/antigravity-cli/${manifest.build}/`) || url.pathname.includes("..")) {
|
|
throw new Error("Antigravity requires an official pinned distribution URL");
|
|
}
|
|
if (item.archiveFormat !== "tar.gz" || !Number.isSafeInteger(item.archiveSize) || item.archiveSize < 1 || item.archiveSize > MAX_ARCHIVE_BYTES
|
|
|| !SHA256.test(item.archiveSha256) || !SHA512.test(item.archiveSha512) || !SHA512.test(item.executableSha512)
|
|
|| !SHA256.test(item.closureSha256) || item.archiveMember !== ARCHIVE_MEMBER || item.executable !== EXECUTABLE
|
|
|| !Array.isArray(item.args) || item.args.some(arg => typeof arg !== "string")) {
|
|
throw new Error("Invalid Antigravity distribution pins");
|
|
}
|
|
if (!Array.isArray(item.entries) || item.entries.length !== RUNTIME_FILES.length
|
|
|| item.entries.some((entry, index) => entry.path !== RUNTIME_FILES[index] || !SHA256.test(entry.sha256) || !Number.isSafeInteger(entry.size) || entry.size < 1 || entry.size > MAX_TREE_BYTES || entry.executable !== true)
|
|
|| digest(JSON.stringify(item.entries)) !== item.closureSha256) {
|
|
throw new Error("Invalid Antigravity complete execution closure pins");
|
|
}
|
|
return { ...item, version: manifest.version, build: manifest.build, environment: { ...(manifest.environment ?? {}) }, platform, architecture };
|
|
}
|
|
|
|
/** Hash the native binary; a cached local manifest never establishes trust. */
|
|
export async function antigravityDistributionClosure(directory) {
|
|
const root = await ownedDirectory(directory, { privateRoot: true });
|
|
directory = root.directory;
|
|
const names = (await readdir(directory)).sort();
|
|
const files = names.filter(name => name !== ANTIGRAVITY_TREE_MANIFEST);
|
|
if (JSON.stringify(files) !== JSON.stringify(RUNTIME_FILES)) throw new Error("Antigravity closure must contain exactly its pinned native executable");
|
|
let bytes = 0;
|
|
const entries = [];
|
|
for (const name of names) {
|
|
const path = join(directory, name);
|
|
const maximum = name === ANTIGRAVITY_TREE_MANIFEST ? 64 * 1024 : MAX_TREE_BYTES;
|
|
const { file, before } = await openOwnedFile(path, maximum);
|
|
try {
|
|
if (name === ANTIGRAVITY_TREE_MANIFEST) continue;
|
|
bytes += before.size;
|
|
if (bytes > MAX_TREE_BYTES) throw new Error("Antigravity closure exceeds size limit");
|
|
const hash = createHash("sha256");
|
|
let length = 0;
|
|
for await (const chunk of file.createReadStream({ autoClose: false })) { hash.update(chunk); length += chunk.length; }
|
|
const after = await file.stat();
|
|
const current = await lstat(path);
|
|
assertOwnedFile(after, maximum);
|
|
if (before.ino !== current.ino || before.dev !== current.dev || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs || length !== after.size) throw new Error("Antigravity closure changed while being hashed");
|
|
entries.push({ path: name, sha256: hash.digest("hex"), size: length, executable: Boolean(after.mode & 0o111) });
|
|
} finally { await file.close(); }
|
|
}
|
|
const current = await ownedDirectory(directory, { privateRoot: true });
|
|
if (current.stat.dev !== root.stat.dev || current.stat.ino !== root.stat.ino) throw new Error("Antigravity closure directory changed while being hashed");
|
|
return { entries, sha256: digest(JSON.stringify(entries)) };
|
|
}
|
|
|
|
export async function verifyAntigravityDistribution(directory, distribution) {
|
|
directory = (await ownedDirectory(directory, { privateRoot: true })).directory;
|
|
const pinned = await antigravityDistribution(distribution?.platform, distribution?.architecture);
|
|
if (distribution && !isDeepStrictEqual(distribution, pinned)) throw new Error("Antigravity runtime verification requires the checked-in distribution pins");
|
|
const closure = await antigravityDistributionClosure(directory);
|
|
if (closure.sha256 !== pinned.closureSha256) throw new Error("Antigravity full execution closure digest mismatch");
|
|
const record = JSON.parse((await readOwnedFile(join(directory, ANTIGRAVITY_TREE_MANIFEST), 64 * 1024)).toString("utf8"));
|
|
if (!isDeepStrictEqual(record, closureRecord(pinned, closure))) throw new Error("Antigravity cached closure record does not match the checked-in pins");
|
|
return closure;
|
|
}
|
|
|
|
function closureRecord(distribution, closure) {
|
|
return {
|
|
schema: "paperclip.antigravity_closure.v1",
|
|
version: distribution.version,
|
|
build: distribution.build,
|
|
archiveSha256: distribution.archiveSha256,
|
|
archiveSha512: distribution.archiveSha512,
|
|
closureSha256: distribution.closureSha256,
|
|
entries: closure.entries,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Strictly inspect Google's single-member tar: one regular executable named
|
|
* `antigravity`, no links, no other members, and only zero blocks after it.
|
|
*/
|
|
export function antigravityArchiveExecutable(archive, member = ARCHIVE_MEMBER) {
|
|
const invalid = () => { throw new Error("Antigravity archive contains unsafe or unsupported tar entries"); };
|
|
if (!Buffer.isBuffer(archive) || archive.length < 512 + 1024 || archive.length % 512) invalid();
|
|
const header = archive.subarray(0, 512);
|
|
const nameEnd = header.indexOf(0);
|
|
if (nameEnd < 1 || nameEnd > 100 || header.subarray(0, nameEnd).toString("utf8") !== member || header.subarray(nameEnd, 100).some(byte => byte !== 0)) invalid();
|
|
let sum = 0;
|
|
for (let index = 0; index < 512; index++) sum += index >= 148 && index < 156 ? 0x20 : header[index];
|
|
const mode = octal(header.subarray(100, 108));
|
|
const magic = header.subarray(257, 265).toString("latin1");
|
|
if (octal(header.subarray(148, 156)) !== sum || ![0x30, 0].includes(header[156]) || header.subarray(157, 257).some(byte => byte !== 0)
|
|
|| (magic !== "ustar \0" && magic !== "ustar\x0000") || mode < 0 || (mode & 0o7000) || (mode & 0o111) !== 0o111) invalid();
|
|
const size = octal(header.subarray(124, 136));
|
|
if (size < 1 || size > MAX_TREE_BYTES) invalid();
|
|
const end = 512 + Math.ceil(size / 512) * 512;
|
|
if (archive.length < end + 1024 || archive.subarray(512 + size).some(byte => byte !== 0)) invalid();
|
|
return archive.subarray(512, 512 + size);
|
|
}
|
|
|
|
/** Explicit setup only: no vendor installer, ambient binary, or npm lifecycle. */
|
|
export async function materializePinnedAntigravityDistribution(options = {}) {
|
|
const distribution = await antigravityDistribution(options.platform, options.architecture);
|
|
if (!options.destination) throw new Error("Antigravity materialization requires an explicit destination");
|
|
const destination = await ownedDestination(resolve(options.destination));
|
|
const parent = await ownedDirectory(dirname(destination));
|
|
try { await lstat(destination); throw new Error("Antigravity destination already exists; refusing to overwrite it"); }
|
|
catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
const temporary = await mkdtemp(join(dirname(destination), ".antigravity-install-"));
|
|
try {
|
|
let archive;
|
|
if (options.archivePath) {
|
|
archive = await readOwnedFile(options.archivePath, MAX_ARCHIVE_BYTES);
|
|
} else {
|
|
const response = await (options.fetch ?? fetch)(distribution.url, { redirect: "error", signal: AbortSignal.timeout(300_000) });
|
|
if (!response.ok || !response.body) throw new Error(`Antigravity archive download failed: ${response.status}`);
|
|
const chunks = [];
|
|
let length = 0;
|
|
for await (const chunk of response.body) {
|
|
length += chunk.length;
|
|
if (length > distribution.archiveSize) throw new Error("Antigravity archive download exceeds size limit");
|
|
chunks.push(chunk);
|
|
}
|
|
archive = Buffer.concat(chunks);
|
|
}
|
|
if (archive.length !== distribution.archiveSize || digest(archive, "sha256") !== distribution.archiveSha256 || digest(archive, "sha512") !== distribution.archiveSha512) {
|
|
throw new Error("Antigravity archive digest mismatch");
|
|
}
|
|
let tar;
|
|
try { tar = gunzipSync(archive, { maxOutputLength: distribution.entries[0].size + 64 * 1024 }); }
|
|
catch { throw new Error("Antigravity archive contains unsafe or unsupported tar entries"); }
|
|
archive = undefined;
|
|
const content = antigravityArchiveExecutable(tar, distribution.archiveMember);
|
|
if (content.length !== distribution.entries[0].size
|
|
|| digest(content, "sha256") !== distribution.entries[0].sha256
|
|
|| digest(content, "sha512") !== distribution.executableSha512) {
|
|
throw new Error("Antigravity full execution closure digest mismatch");
|
|
}
|
|
const staged = join(temporary, "runtime");
|
|
await mkdir(staged, { mode: 0o700 });
|
|
await writeFile(join(staged, EXECUTABLE), content, { mode: 0o500, flag: "wx" });
|
|
await chmod(join(staged, EXECUTABLE), 0o500);
|
|
const closure = await antigravityDistributionClosure(staged);
|
|
if (closure.sha256 !== distribution.closureSha256) throw new Error("Antigravity full execution closure digest mismatch");
|
|
await writeFile(join(staged, ANTIGRAVITY_TREE_MANIFEST), `${JSON.stringify(closureRecord(distribution, closure))}\n`, { mode: 0o400, flag: "wx" });
|
|
const currentParent = await ownedDirectory(dirname(destination));
|
|
if (currentParent.stat.dev !== parent.stat.dev || currentParent.stat.ino !== parent.stat.ino) throw new Error("Antigravity cache parent changed before publication");
|
|
await rename(staged, destination);
|
|
return { ...distribution, destination, executable: join(destination, distribution.executable) };
|
|
} finally { await rm(temporary, { recursive: true, force: true }); }
|
|
}
|
|
|
|
if (process.argv[1]?.endsWith("/materialize-antigravity-distribution.mjs") && pathToFileURL(resolve(process.argv[1])).href === import.meta.url) {
|
|
const [destination, platform, architecture, archivePath] = process.argv.slice(2);
|
|
materializePinnedAntigravityDistribution({ destination, platform, architecture, archivePath }).then(result => process.stdout.write(`${JSON.stringify(result)}\n`)).catch(error => { console.error(error.message); process.exitCode = 1; });
|
|
}
|