mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 10:44:52 +01:00
Merge origin/master into task workspace ownership
Preserve owned-home cleanup safety while integrating upstream runtime-context cleanup, current Runner build caching, release crate caching and verification docs. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
commit
650198cc04
65 files changed
+3843
-58
No files matched your search
@@ -0,0 +1,89 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { removeTestHome } from "./remove-test-home.js";
|
||||
|
||||
const homes: string[] = [];
|
||||
|
||||
function makeHome(): string {
|
||||
const home = fs.mkdtempSync(path.join(os.tmpdir(), "remove-test-home-"));
|
||||
homes.push(home);
|
||||
return home;
|
||||
}
|
||||
|
||||
/**
|
||||
* The tree a native session leaves behind: a runtime-context bundle whose
|
||||
* files are read-only and whose directories are read-only too, the way
|
||||
* `makeDirectoriesReadOnly` publishes it under the instance root.
|
||||
*/
|
||||
function leaveReadOnlyBundle(home: string): string {
|
||||
const bundle = path.join(home, "instances", "default", "runtime-context-assets", "bundles", "digest");
|
||||
const nested = path.join(bundle, "skills", "assigned");
|
||||
fs.mkdirSync(nested, { recursive: true });
|
||||
fs.writeFileSync(path.join(bundle, "AGENTS.md"), "# instructions\n", { mode: 0o444 });
|
||||
fs.writeFileSync(path.join(nested, "SKILL.md"), "# skill\n", { mode: 0o444 });
|
||||
fs.chmodSync(nested, 0o555);
|
||||
fs.chmodSync(path.dirname(nested), 0o555);
|
||||
fs.chmodSync(bundle, 0o555);
|
||||
return bundle;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const home of homes.splice(0)) {
|
||||
if (fs.existsSync(home)) {
|
||||
// Leave nothing behind even if an assertion failed partway.
|
||||
fs.chmodSync(home, 0o700);
|
||||
removeTestHome(home);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
describe("removeTestHome", () => {
|
||||
it("removes a home whose runtime-context bundle directories are read-only", () => {
|
||||
const home = makeHome();
|
||||
const bundle = leaveReadOnlyBundle(home);
|
||||
fs.writeFileSync(path.join(home, "instances", "default", "paperclip.json"), "{}\n");
|
||||
|
||||
// The plain removal the setup file used to run is what fails in CI.
|
||||
if (process.platform !== "win32" && process.getuid?.() !== 0) {
|
||||
expect(() => fs.rmSync(home, { recursive: true, force: true })).toThrow(/EACCES|EPERM/);
|
||||
expect(fs.existsSync(bundle)).toBe(true);
|
||||
}
|
||||
|
||||
removeTestHome(home);
|
||||
expect(fs.existsSync(home)).toBe(false);
|
||||
});
|
||||
|
||||
it("removes an ordinary home and tolerates one that is already gone", () => {
|
||||
const home = makeHome();
|
||||
fs.mkdirSync(path.join(home, "instances", "default"), { recursive: true });
|
||||
fs.writeFileSync(path.join(home, "instances", "default", "paperclip.json"), "{}\n");
|
||||
removeTestHome(home);
|
||||
expect(fs.existsSync(home)).toBe(false);
|
||||
expect(() => removeTestHome(home)).not.toThrow();
|
||||
});
|
||||
|
||||
// Windows keeps no Unix mode bits (chmod only toggles the read-only
|
||||
// attribute, so the 0o555 assertion cannot hold) and a directory symlink
|
||||
// can need elevated privileges there. POSIX-only, like the suite's other
|
||||
// permission tests.
|
||||
it.skipIf(process.platform === "win32")("does not follow a symlink out of the home while making directories writable", () => {
|
||||
const home = makeHome();
|
||||
const outside = makeHome();
|
||||
const guarded = path.join(outside, "guarded");
|
||||
fs.mkdirSync(guarded);
|
||||
fs.writeFileSync(path.join(guarded, "keep.txt"), "keep\n");
|
||||
fs.chmodSync(guarded, 0o555);
|
||||
leaveReadOnlyBundle(home);
|
||||
fs.symlinkSync(outside, path.join(home, "link-out"), "dir");
|
||||
|
||||
removeTestHome(home);
|
||||
expect(fs.existsSync(home)).toBe(false);
|
||||
expect(fs.existsSync(path.join(guarded, "keep.txt"))).toBe(true);
|
||||
expect(fs.statSync(guarded).mode & 0o777).toBe(0o555);
|
||||
fs.chmodSync(guarded, 0o700);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
/**
|
||||
* Removes a per-worker test home at worker exit.
|
||||
*
|
||||
* A plain recursive `rmSync` is not enough: a native session materializes its
|
||||
* runtime-context bundles under the instance root and makes every directory
|
||||
* in them read-only (`makeDirectoriesReadOnly` in
|
||||
* services/native-runtime/runtime-context.ts), and a run that is still
|
||||
* finishing when the worker exits can leave such a bundle under the worker's
|
||||
* home. Removing a file inside a read-only directory fails with `EACCES`,
|
||||
* Vitest reports it as an unhandled error, and the shard fails after every
|
||||
* test passed. So on a permission error every directory in the tree is made
|
||||
* writable first and the removal is tried once more.
|
||||
*/
|
||||
export function removeTestHome(home: string): void {
|
||||
const options = { recursive: true, force: true, maxRetries: 8, retryDelay: 50 } as const;
|
||||
try {
|
||||
fs.rmSync(home, options);
|
||||
} catch (error) {
|
||||
if (!isPermissionError(error)) throw error;
|
||||
makeDirectoriesWritable(home);
|
||||
fs.rmSync(home, options);
|
||||
}
|
||||
}
|
||||
|
||||
function isPermissionError(error: unknown): boolean {
|
||||
const code = (error as NodeJS.ErrnoException | null)?.code;
|
||||
return code === "EACCES" || code === "EPERM";
|
||||
}
|
||||
|
||||
function makeDirectoriesWritable(directory: string): void {
|
||||
let stat: fs.Stats;
|
||||
try {
|
||||
stat = fs.lstatSync(directory);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (!stat.isDirectory()) return;
|
||||
// Owner read, write and search: enough to list and unlink the children.
|
||||
// A symlink is not followed, so nothing outside the home is touched.
|
||||
try {
|
||||
fs.chmodSync(directory, stat.mode | 0o700);
|
||||
} catch {
|
||||
// A directory that cannot be changed still fails the removal below, loudly.
|
||||
}
|
||||
let entries: fs.Dirent[];
|
||||
try {
|
||||
entries = fs.readdirSync(directory, { withFileTypes: true });
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
for (const entry of entries) {
|
||||
if (entry.isDirectory()) makeDirectoriesWritable(path.join(directory, entry.name));
|
||||
}
|
||||
}
|
||||
@@ -120,7 +120,7 @@ describe.skipIf(!sentryPackage)("run failure context with the real Sentry SDK",
|
||||
expect(event).toMatchObject({ exception: { values: [{ value: "OpenCode execution timed out" }] },
|
||||
contexts: { run_execution: { durationMs: 100, effectiveTimeoutSec: 14400,
|
||||
timeoutSource: "sandbox_default", timeoutConfigured: false, timeoutFired: true, stopReason: "timeout" } } });
|
||||
expect(JSON.stringify(events)).not.toMatch(/private-timeout-|<html>|504|Timed out after/);
|
||||
expect(JSON.stringify(events)).not.toMatch(/private-timeout-|<html>|504 Gateway|Timed out after/);
|
||||
expect(events.find(entry => entry !== event)).not.toHaveProperty("contexts.run_execution");
|
||||
} finally {
|
||||
await fs.rm(root, { recursive: true, force: true });
|
||||
|
||||
@@ -4,6 +4,7 @@ import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { RUNNER_VENDOR_BUILD_COMMAND } from "../../scripts/prepare-runner-vendor.mjs";
|
||||
|
||||
const packageJsonPath = fileURLToPath(
|
||||
new URL("../../package.json", import.meta.url),
|
||||
@@ -57,14 +58,39 @@ describe("server package build script", () => {
|
||||
expect(packageJson.devDependencies?.["@paperclipai/paperclip-runner"]).toBe(
|
||||
"workspace:*",
|
||||
);
|
||||
// prepare-runner-vendor.mjs reuses a Runner build whose recorded inputs
|
||||
// are unchanged (the `pnpm -r build` case) and otherwise runs the Runner's
|
||||
// full build, which is what a standalone server build (the Dockerfile's
|
||||
// path) relies on. Pin both the indirection and the command behind it.
|
||||
expect(packageJson.scripts?.["prepare:runner-vendor"]).toBe(
|
||||
"node scripts/prepare-runner-vendor.mjs",
|
||||
);
|
||||
expect(RUNNER_VENDOR_BUILD_COMMAND).toBe(
|
||||
"pnpm --filter @paperclipai/paperclip-runner build",
|
||||
);
|
||||
expect(packageJson.scripts?.build.startsWith("pnpm run prepare:runner-vendor && ")).toBe(true);
|
||||
expect(packageJson.scripts?.build).toContain(
|
||||
"cp -Rf ../packages/paperclip-runner/dist/. dist/vendor/paperclip-runner/",
|
||||
);
|
||||
});
|
||||
|
||||
it("brackets the Runner's build with its input recording", () => {
|
||||
// The stamp prepare-runner-vendor.mjs trusts must only exist for a build
|
||||
// that completed every step and check, so it has to be written last; and
|
||||
// it must vouch for the inputs the build read, not for whatever the
|
||||
// sources are when it ends, so the digest has to be captured before the
|
||||
// first step reads anything (--begin) and compared again at the end
|
||||
// (--finish). Nothing else in the chain may touch the stamp.
|
||||
const runner = JSON.parse(readFileSync(new URL(
|
||||
"../../../packages/paperclip-runner/package.json", import.meta.url,
|
||||
), "utf8")) as { scripts: Record<string, string> };
|
||||
const steps = runner.scripts.build.split(" && ");
|
||||
expect(steps.at(0)).toBe("node scripts/record-build-inputs.mjs --begin");
|
||||
expect(steps.at(-1)).toBe("node scripts/record-build-inputs.mjs --finish");
|
||||
expect(steps.filter((step) => step.includes("record-build-inputs"))).toHaveLength(2);
|
||||
expect(steps.length).toBeGreaterThan(2);
|
||||
});
|
||||
|
||||
it("replaces an existing read-only image manifest with the build's copy step", () => {
|
||||
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
||||
const step = packageJson.scripts.build.split(" && ").find((command: string) =>
|
||||
@@ -104,10 +130,10 @@ describe("server package build script", () => {
|
||||
});
|
||||
|
||||
it("compiles the vendored Runner's TypeScript surface once per build", () => {
|
||||
// prepare:runner-vendor runs the Runner's full build, and `pnpm -r build`
|
||||
// has already run it once by the time the server builds. Each extra
|
||||
// prepare:runner-vendor runs the Runner's full build whenever its recorded
|
||||
// inputs are stale or missing (every standalone server build). Each extra
|
||||
// build:typescript inside that chain (two tsc projects plus the eval-kernel
|
||||
// rebuild) costs about 7s on the CI Build job's critical path, twice.
|
||||
// rebuild) costs about 7s on the CI Build job's critical path.
|
||||
// Checks that read dist/ must call their script directly, the way
|
||||
// generate-replay-goldens.mjs already does, not through a `check:*`
|
||||
// wrapper that rebuilds first for standalone use.
|
||||
|
||||
@@ -6,6 +6,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { Server as TlsServer } from "node:tls";
|
||||
|
||||
|
||||
type SupertestServer = NetServer & {
|
||||
address(): ReturnType<NetServer["address"]>;
|
||||
listen(port: number): NetServer;
|
||||
@@ -30,6 +31,7 @@ const { home: paperclipTestHome, cleanup: cleanupPaperclipTestHome } = createIso
|
||||
process.env.PAPERCLIP_HOME = paperclipTestHome;
|
||||
// Setup-file afterAll hooks run before suites drain asynchronous heartbeats.
|
||||
// Cleanup at worker exit, after all suite teardown, avoids deleting live state.
|
||||
// The owned-home cleanup also handles read-only runtime-context bundles.
|
||||
process.once("exit", cleanupPaperclipTestHome);
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
Reference in new issue
Block a user