mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-18 23:08:26 +01:00
Return the restore stage and failure category with the existing HTTP409 rejection. Preserve the unsaved comment and editable draft. Keep nested saved-commit and clone-timeout diagnostics through preparation wrappers, and distinguish local Git file errors from authentication failures. Retain only safe process flags privately; no raw command cause is exposed.
All seven affected local suites pass (288 tests) and the layered server typecheck passes on supported Node25.6.1. Reviewed head 022eb7c2eb has Greptile Apex5/5, zero unresolved findings and all applicable CI passing (55 successful checks and4 intentional skips).
Co-Authored-By: Sentry Bot <sentry-bot@paperclip.invalid>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Paperclip <noreply@paperclip.ing>
123 lines
7.8 KiB
TypeScript
123 lines
7.8 KiB
TypeScript
import { execFile as execFileCallback } from "node:child_process";
|
|
import { promisify } from "node:util";
|
|
import { mkdtemp, mkdir, writeFile, readFile, readdir, rm, stat, symlink } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { materializeManagedProjectWorkspace, ensureManagedRepositoriesIgnored, readManagedRepositoryPreparationFailure } from "../services/managed-repository-checkout.js";
|
|
|
|
const execFile = promisify(execFileCallback);
|
|
const directories: string[] = [];
|
|
afterEach(async () => { vi.unstubAllEnvs(); await Promise.all(directories.splice(0).map(dir => rm(dir, { recursive: true, force: true }))); });
|
|
async function fixture() {
|
|
const root = await mkdtemp(path.join(tmpdir(), "paperclip-repository-publication-"));
|
|
directories.push(root);
|
|
const source = path.join(root, "source");
|
|
await mkdir(source);
|
|
await execFile("git", ["init", "--initial-branch=main", source]);
|
|
await writeFile(path.join(source, "source.txt"), "initial\n");
|
|
await execFile("git", ["-C", source, "add", "."]);
|
|
await execFile("git", ["-C", source, "-c", "user.name=Fixture", "-c", "user.email=fixture@example.test", "commit", "-m", "initial"]);
|
|
return { root, source, target: path.join(root, "target") };
|
|
}
|
|
describe("atomic managed repository publication", () => {
|
|
it("keeps managed repositories separate without requiring Git or touching an ancestor", async () => {
|
|
const { source, target } = await fixture();
|
|
await mkdir(target);
|
|
await expect(ensureManagedRepositoriesIgnored(target)).resolves.toBeUndefined();
|
|
const nestedPlainRoot = path.join(source, "plain-task");
|
|
await mkdir(nestedPlainRoot);
|
|
const original = await readFile(path.join(source, ".git", "info", "exclude"), "utf8");
|
|
await ensureManagedRepositoriesIgnored(nestedPlainRoot);
|
|
expect(await readFile(path.join(source, ".git", "info", "exclude"), "utf8")).toBe(original);
|
|
await ensureManagedRepositoriesIgnored(source);
|
|
expect(await readFile(path.join(source, ".git", "info", "exclude"), "utf8")).toContain("/.paperclip-repositories/");
|
|
});
|
|
it("allows a declared plain task directory with inert Git metadata without changing its files", async () => {
|
|
const { target } = await fixture();
|
|
await mkdir(path.join(target, ".git", "info"), { recursive: true });
|
|
const file = path.join(target, ".git", "info", "exclude");
|
|
await writeFile(file, "/.paperclip-repositories/\n");
|
|
await expect(ensureManagedRepositoriesIgnored(target, { plainTaskDirectory: true })).resolves.toBeUndefined();
|
|
expect(await readFile(file, "utf8")).toBe("/.paperclip-repositories/\n");
|
|
await expect(ensureManagedRepositoriesIgnored(target)).rejects.toThrow("not a git repository");
|
|
});
|
|
it("still excludes managed repositories when a task directory contains a real checkout", async () => {
|
|
const { source } = await fixture();
|
|
await ensureManagedRepositoriesIgnored(source, { plainTaskDirectory: true });
|
|
expect(await readFile(path.join(source, ".git", "info", "exclude"), "utf8")).toContain("/.paperclip-repositories/");
|
|
});
|
|
it("retains unexpected Git errors for declared plain task directories", async () => {
|
|
const { root, target } = await fixture();
|
|
await mkdir(path.join(target, ".git"), { recursive: true });
|
|
vi.stubEnv("PATH", path.join(root, "missing-git"));
|
|
await expect(ensureManagedRepositoriesIgnored(target, { plainTaskDirectory: true })).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
it("rejects linked Git metadata even for a declared plain task directory", async () => {
|
|
const { source, target } = await fixture();
|
|
await mkdir(target);
|
|
await symlink(path.join(source, ".git"), path.join(target, ".git"), "dir");
|
|
const original = await readFile(path.join(source, ".git", "info", "exclude"), "utf8");
|
|
await expect(ensureManagedRepositoriesIgnored(target, { plainTaskDirectory: true })).rejects.toThrow("must not be a symlink");
|
|
expect(await readFile(path.join(source, ".git", "info", "exclude"), "utf8")).toBe(original);
|
|
});
|
|
it.each([
|
|
{ label: "timeout", code: null, killed: true, timedOut: true },
|
|
{ label: "filesystem", code: "EACCES", killed: false, timedOut: false },
|
|
{ label: "output limit", code: "ERR_CHILD_PROCESS_STDIO_MAXBUFFER", killed: true, timedOut: false },
|
|
])("retains safe $label metadata without adding raw command causes", async ({ code, killed, timedOut }) => {
|
|
const { source, target } = await fixture();
|
|
const original = Object.assign(new Error("Git failed for https://user:fixture-password@example.test/repo.git"), {
|
|
code, killed, stderr: "private command output",
|
|
});
|
|
const failure = await materializeManagedProjectWorkspace(target, {
|
|
repoUrl: "https://example.test/repo.git", localSource: source,
|
|
beforePublish: async () => { throw original; },
|
|
}).then(() => { throw new Error("Expected preparation to fail"); }, (error: unknown) => error);
|
|
expect(failure).toBeInstanceOf(Error);
|
|
expect(readManagedRepositoryPreparationFailure(failure)).toEqual({ code, timedOut });
|
|
expect(failure).not.toHaveProperty("code");
|
|
expect(failure).not.toHaveProperty("cause");
|
|
expect(failure).not.toHaveProperty("stderr");
|
|
expect(JSON.stringify(failure)).toBe("{}");
|
|
expect((failure as Error).message).not.toContain("fixture-password");
|
|
expect(await stat(target).catch(() => null)).toBeNull();
|
|
});
|
|
|
|
it("writes the pinned receipt inside the clone before making it visible", async () => {
|
|
const { source, target } = await fixture();
|
|
const result = await materializeManagedProjectWorkspace(target, { repoUrl: "https://github.com/test/source", localSource: source, beforePublish: async clone => {
|
|
expect(await stat(target).catch(() => null)).toBeNull();
|
|
const commit = (await execFile("git", ["-C", clone, "rev-parse", "HEAD"])).stdout.trim();
|
|
await writeFile(path.join(clone, ".git", "paperclip-workspace-owner.json"), JSON.stringify({ repositoryId: "receipt", pinnedCommit: commit }));
|
|
} });
|
|
expect(result.warning).toBeNull();
|
|
expect(JSON.parse(await readFile(path.join(target, ".git", "paperclip-workspace-owner.json"), "utf8"))).toMatchObject({ repositoryId: "receipt", pinnedCommit: expect.stringMatching(/^[a-f0-9]{40}$/) });
|
|
expect(await readFile(path.join(target, "source.txt"), "utf8")).toBe("initial\n");
|
|
});
|
|
it("authorizes the empty staging root before copying any source bytes", async () => {
|
|
const { root, source, target } = await fixture();
|
|
let stagingChecked = false;
|
|
await expect(materializeManagedProjectWorkspace(target, {
|
|
repoUrl: source,
|
|
beforeCopy: async staging => {
|
|
expect(await readdir(staging)).toEqual([]);
|
|
expect(await stat(target).catch(() => null)).toBeNull();
|
|
stagingChecked = true;
|
|
throw new Error("Private staging source denied");
|
|
},
|
|
})).rejects.toThrow("Private staging source denied");
|
|
expect(stagingChecked).toBe(true);
|
|
expect(await stat(target).catch(() => null)).toBeNull();
|
|
expect((await readdir(root)).filter(name => name.startsWith("target.clone-"))).toEqual([]);
|
|
expect(await readFile(path.join(source, "source.txt"), "utf8")).toBe("initial\n");
|
|
});
|
|
it("does not publish an incomplete checkout when its receipt cannot be committed", async () => {
|
|
const { root, source, target } = await fixture();
|
|
await expect(materializeManagedProjectWorkspace(target, { repoUrl: "https://github.com/test/source", localSource: source, beforePublish: async () => { throw new Error("receipt failed"); } })).rejects.toThrow(/receipt failed/);
|
|
expect(await stat(target).catch(() => null)).toBeNull();
|
|
expect((await readdir(root)).filter(name => name.startsWith("target.clone-"))).toEqual([]);
|
|
expect(await readFile(path.join(source, "source.txt"), "utf8")).toBe("initial\n");
|
|
});
|
|
});
|