Files
PaperClipAI/server/src/__tests__/workspace-instance-cleanup.test.ts
T
dcac49a4fd feat(workspaces): defer isolated setup until runtime start (#10653)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Isolated workspaces give each task a safe and reproducible checkout.
> - The existing setup cloned the development database before an agent
needed to run the app.
> - This made worktree creation slower and heavier for tasks that never
start a service.
> - Runtime services already use one server start path for heartbeat,
operator, and startup recovery flows.
> - This pull request moves heavy setup to that start path and keeps
worktree creation lean.
> - The benefit is faster isolated workspace creation with the same
reliable runtime setup when a service starts.

## Linked Issues or Issue Description

Related pull request: #10652 covers the initial deferred
database-seeding slice. This pull request supersedes it with end-to-end
runtime provisioning and safe cleanup.

**What existing behavior does this improve?**

This improves isolated worktree creation, runtime service startup, and
isolated instance cleanup.

**Subsystem affected**

Cross-cutting: CLI worktree setup, server runtime orchestration, shared
workspace contracts, and development scripts.

**Current behavior**

Paperclip seeds an isolated development database during worktree
creation. It can also leave an isolated instance directory after
workspace teardown. This work happens even when no runtime service
starts.

**Proposed behavior**

Paperclip creates the worktree with a lean eager setup. It runs an
idempotent runtime provision command before the first managed service
spawn. Concurrent starts share one provision attempt. Teardown removes
the isolated instance safely.

**Reason and benefit**

Many agent tasks only edit and test code. They do not need a running
Paperclip instance. Deferring the database seed reduces workspace
startup cost while preserving automatic setup for tasks that start the
app.

**Breaking changes**

None. The new runtime provision command is optional. Existing workspace
behavior is unchanged when it is absent.

## What Changed

- Split Paperclip worktree setup into a lean eager script and an
idempotent runtime provision script.
- Added `runtimeProvisionCommand` to project, issue, realized workspace,
and persisted workspace contracts.
- Added a per-workspace provision mutex before local service spawn for
heartbeat, operator, and startup recovery flows.
- Added a persisted `provisioning` service state and the
`workspace_runtime_provision` operation phase.
- Kept provision time outside the service readiness timeout and made
failed attempts visible and retryable.
- Reclaimed isolated instance data during safe workspace teardown.
- Serialized deferred database seeding across processes and bound
teardown to the instance root captured in persisted workspace metadata.
- Added tests for config flow, concurrency, retry, no-op behavior,
readiness timing, scripts, CLI commands, and cleanup.
- Documented the eager and runtime provisioning contracts.

## Verification

- `pnpm -r typecheck`
- `pnpm build`
- `pnpm test:run` (server: 3,201 passed; UI: 3,345 passed; the CLI phase
exposed one environment-sensitive AWS doctor assertion because the agent
runtime injects static AWS credentials)
- `env -u AWS_ACCESS_KEY_ID -u AWS_SECRET_ACCESS_KEY pnpm exec vitest
run cli/src/__tests__/secrets.test.ts -t 'passes AWS doctor checks when
non-secret provider config is present'`
- Focused runtime tests cover serialized provisioning, retry after
stderr failure, absent-command no-op behavior, operation logging,
persisted state order, and readiness timeout exclusion.
- Focused CLI and cleanup tests cover concurrent seed serialization,
stale-lock fail-closed behavior, persisted instance ownership, and
rewritten sibling pointers.

## Risks

- A faulty runtime provision script blocks service startup. Paperclip
records stderr, marks the service failed, and retries on the next start.
- Concurrent service requests share an in-process provision attempt,
while the seed command uses an atomic filesystem lock across processes.
A stale lock fails closed and requires an operator to verify no seed is
running before removing it.
- Isolated instance cleanup is destructive. The cleanup service
validates ownership and path containment before removal.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- OpenAI Codex, `gpt-5.6-sol`, with agentic reasoning, tool use, and
code execution. The service does not expose the context-window size.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-02 10:37:10 -05:00

357 lines
16 KiB
TypeScript

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
cleanupWorktreeInstanceArtifacts,
deriveWorktreeInstanceId,
readManagedWorktreeInstanceOwnership,
readWorktreeInstancePointer,
stopEmbeddedPostgresIfRunning,
} from "../services/workspace-instance-cleanup.js";
import type { WorkspaceOperation } from "@paperclipai/shared";
import type { WorkspaceOperationRecorder } from "../services/workspace-operations.js";
const tempRoots = new Set<string>();
async function makeTempRoot(prefix: string): Promise<string> {
const root = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
tempRoots.add(root);
return root;
}
async function writeWorkspaceEnv(workspacePath: string, homeDir: string, instanceId: string): Promise<void> {
const envDir = path.join(workspacePath, ".paperclip");
await fs.mkdir(envDir, { recursive: true });
await fs.writeFile(
path.join(envDir, ".env"),
`PAPERCLIP_HOME=${JSON.stringify(homeDir)}\nPAPERCLIP_INSTANCE_ID=${JSON.stringify(instanceId)}\n`,
"utf8",
);
}
function createRecorderDouble() {
const operations: Array<{
status: string;
metadata: Record<string, unknown> | null;
system: string | null;
}> = [];
const recorder: WorkspaceOperationRecorder = {
attachExecutionWorkspaceId: async () => {},
recordOperation: async (input) => {
const result = await input.run();
operations.push({
status: result.status ?? "succeeded",
metadata: input.metadata ?? null,
system: result.system ?? null,
});
return {
id: `op-${operations.length}`,
companyId: "company-1",
executionWorkspaceId: null,
heartbeatRunId: null,
issueId: null,
phase: input.phase,
command: input.command ?? null,
cwd: input.cwd ?? null,
status: (result.status ?? "succeeded") as WorkspaceOperation["status"],
exitCode: result.exitCode ?? null,
logStore: null,
logRef: null,
logBytes: null,
logSha256: null,
logCompressed: false,
stdoutExcerpt: result.stdout ?? null,
stderrExcerpt: result.stderr ?? null,
metadata: input.metadata ?? null,
startedAt: new Date(),
finishedAt: new Date(),
createdAt: new Date(),
updatedAt: new Date(),
};
},
};
return { recorder, operations };
}
afterEach(async () => {
await Promise.all(Array.from(tempRoots, (root) => fs.rm(root, { recursive: true, force: true })));
tempRoots.clear();
vi.restoreAllMocks();
});
describe("worktree instance cleanup", () => {
it("derives different instance IDs for distinct paths with the same normalized name", () => {
const parent = path.join(os.tmpdir(), "paperclip-instance-id-collision");
const plusPath = path.join(parent, "feature+cleanup");
const dashPath = path.join(parent, "feature-cleanup");
expect(deriveWorktreeInstanceId(plusPath)).not.toBe(deriveWorktreeInstanceId(dashPath));
expect(deriveWorktreeInstanceId(plusPath)).toMatch(/^feature-cleanup-[a-f0-9]{12}$/);
expect(deriveWorktreeInstanceId(dashPath)).toMatch(/^feature-cleanup-[a-f0-9]{12}$/);
});
it("removes an instance directory inside the managed worktree instances root", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const instanceId = "pap-16075";
const instanceRoot = path.join(worktreesDir, "instances", instanceId);
await fs.mkdir(path.join(instanceRoot, "db"), { recursive: true });
await fs.writeFile(path.join(instanceRoot, "marker"), "remove me", "utf8");
await writeWorkspaceEnv(workspacePath, worktreesDir, instanceId);
const pointer = await readWorktreeInstancePointer(workspacePath);
expect(pointer).not.toBeNull();
const result = await cleanupWorktreeInstanceArtifacts({
pointer: pointer!,
workspaceId: "workspace-1",
workspacePath,
expectedInstanceId: instanceId,
expectedInstanceRoot: instanceRoot,
worktreesDir,
});
expect(result).toMatchObject({ status: "removed", postgresStopped: false });
await expect(fs.stat(instanceRoot)).rejects.toMatchObject({ code: "ENOENT" });
});
it("falls back to deterministic instance ownership when persisted root metadata is absent", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const instanceId = deriveWorktreeInstanceId(workspacePath);
const instanceRoot = path.join(worktreesDir, "instances", instanceId);
await fs.mkdir(path.join(instanceRoot, "db"), { recursive: true });
await fs.writeFile(path.join(instanceRoot, "marker"), "remove me", "utf8");
await writeWorkspaceEnv(workspacePath, worktreesDir, instanceId);
const pointer = await readWorktreeInstancePointer(workspacePath);
const result = await cleanupWorktreeInstanceArtifacts({
pointer: pointer!,
workspaceId: "workspace-1",
workspacePath,
expectedInstanceId: instanceId,
expectedInstanceRoot: null,
worktreesDir,
});
expect(result).toMatchObject({ status: "removed", instanceRoot });
await expect(fs.stat(instanceRoot)).rejects.toMatchObject({ code: "ENOENT" });
});
it("refuses and logs an instance pointer outside the managed worktree root", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const liveHome = await makeTempRoot("paperclip-live-home-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const liveInstanceRoot = path.join(liveHome, "instances", "default");
await fs.mkdir(liveInstanceRoot, { recursive: true });
await fs.writeFile(path.join(liveInstanceRoot, "marker"), "keep me", "utf8");
await writeWorkspaceEnv(workspacePath, liveHome, "default");
const { recorder, operations } = createRecorderDouble();
const stopEmbeddedPostgres = vi.fn(async () => false);
const removeInstanceRoot = vi.fn(async () => {});
const pointer = await readWorktreeInstancePointer(workspacePath);
const result = await cleanupWorktreeInstanceArtifacts({
pointer: pointer!,
workspaceId: "workspace-1",
workspacePath,
expectedInstanceId: "default",
expectedInstanceRoot: path.join(worktreesDir, "instances", "default"),
worktreesDir,
recorder,
dependencies: { stopEmbeddedPostgres, removeInstanceRoot },
});
expect(result).toMatchObject({ status: "refused", instanceRoot: liveInstanceRoot });
expect(stopEmbeddedPostgres).not.toHaveBeenCalled();
expect(removeInstanceRoot).not.toHaveBeenCalled();
expect(await fs.readFile(path.join(liveInstanceRoot, "marker"), "utf8")).toBe("keep me");
expect(operations).toEqual([
expect.objectContaining({
status: "skipped",
metadata: expect.objectContaining({
cleanupAction: "remove_worktree_instance",
refusalReason: "outside_managed_instances_dir",
}),
}),
]);
});
it("treats a missing repo-local env file as a no-op", async () => {
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
await expect(readWorktreeInstancePointer(workspacePath)).resolves.toBeNull();
});
it("captures the managed instance root for persisted workspace ownership", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const instanceRoot = path.join(worktreesDir, "instances", "owned-instance");
await fs.mkdir(instanceRoot, { recursive: true });
await writeWorkspaceEnv(workspacePath, worktreesDir, "owned-instance");
await expect(
readManagedWorktreeInstanceOwnership(workspacePath, worktreesDir),
).resolves.toEqual({ instanceId: "owned-instance", instanceRoot });
});
it("stops embedded Postgres before deleting the instance root", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const instanceRoot = path.join(worktreesDir, "instances", "ordered-cleanup");
await fs.mkdir(path.join(instanceRoot, "db"), { recursive: true });
await writeWorkspaceEnv(workspacePath, worktreesDir, "ordered-cleanup");
const calls: string[] = [];
const pointer = await readWorktreeInstancePointer(workspacePath);
const result = await cleanupWorktreeInstanceArtifacts({
pointer: pointer!,
workspaceId: "workspace-1",
workspacePath,
expectedInstanceId: "ordered-cleanup",
expectedInstanceRoot: instanceRoot,
worktreesDir,
dependencies: {
stopEmbeddedPostgres: async (dataDir) => {
expect(dataDir).toBe(path.join(instanceRoot, "db"));
expect(await fs.stat(instanceRoot)).toBeDefined();
calls.push("stop");
return true;
},
removeInstanceRoot: async (target) => {
calls.push("remove");
await fs.rm(target, { recursive: true, force: true });
},
},
});
expect(result).toMatchObject({ status: "removed", postgresStopped: true });
expect(calls).toEqual(["stop", "remove"]);
});
it("refuses a pointer rewritten to another workspace's managed instance", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const siblingInstanceRoot = path.join(worktreesDir, "instances", "feature-sibling");
await fs.mkdir(path.join(siblingInstanceRoot, "db"), { recursive: true });
await fs.writeFile(path.join(siblingInstanceRoot, "marker"), "keep me", "utf8");
await writeWorkspaceEnv(workspacePath, worktreesDir, "feature-sibling");
const { recorder, operations } = createRecorderDouble();
const stopEmbeddedPostgres = vi.fn(async () => false);
const removeInstanceRoot = vi.fn(async () => {});
const pointer = await readWorktreeInstancePointer(workspacePath);
const result = await cleanupWorktreeInstanceArtifacts({
pointer: pointer!,
workspaceId: "workspace-1",
workspacePath,
expectedInstanceId: "feature-sibling",
expectedInstanceRoot: path.join(worktreesDir, "instances", "feature-owner"),
worktreesDir,
recorder,
dependencies: { stopEmbeddedPostgres, removeInstanceRoot },
});
expect(result).toMatchObject({ status: "refused", instanceRoot: siblingInstanceRoot });
expect((result as { warning: string }).warning).toContain("persisted instance root");
expect(stopEmbeddedPostgres).not.toHaveBeenCalled();
expect(removeInstanceRoot).not.toHaveBeenCalled();
expect(await fs.readFile(path.join(siblingInstanceRoot, "marker"), "utf8")).toBe("keep me");
expect(operations).toEqual([
expect.objectContaining({
status: "skipped",
metadata: expect.objectContaining({
expectedInstanceRoot: path.join(worktreesDir, "instances", "feature-owner"),
refusalReason: "instance_root_workspace_mismatch",
}),
}),
]);
});
it("refuses a managed-root symlink that canonically escapes the guard", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const outsideRoot = await makeTempRoot("paperclip-outside-instance-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const instancesDir = path.join(worktreesDir, "instances");
await fs.mkdir(instancesDir, { recursive: true });
await fs.symlink(outsideRoot, path.join(instancesDir, "escaped"), "dir");
await writeWorkspaceEnv(workspacePath, worktreesDir, "escaped");
const pointer = await readWorktreeInstancePointer(workspacePath);
const result = await cleanupWorktreeInstanceArtifacts({
pointer: pointer!,
workspaceId: "workspace-1",
workspacePath,
expectedInstanceId: "escaped",
expectedInstanceRoot: path.join(worktreesDir, "instances", "escaped"),
worktreesDir,
});
expect(result).toMatchObject({ status: "refused" });
expect((result as { warning: string }).warning).toContain("canonical path");
await expect(fs.stat(outsideRoot)).resolves.toBeDefined();
});
it("refuses when the managed instances directory itself is a symlink", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const liveInstancesDir = await makeTempRoot("paperclip-live-instances-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const liveInstanceRoot = path.join(liveInstancesDir, "default");
await fs.mkdir(liveInstanceRoot, { recursive: true });
await fs.symlink(liveInstancesDir, path.join(worktreesDir, "instances"), "dir");
await writeWorkspaceEnv(workspacePath, worktreesDir, "default");
const pointer = await readWorktreeInstancePointer(workspacePath);
const result = await cleanupWorktreeInstanceArtifacts({
pointer: pointer!,
workspaceId: "workspace-1",
workspacePath,
expectedInstanceId: "default",
expectedInstanceRoot: path.join(worktreesDir, "instances", "default"),
worktreesDir,
});
expect(result).toMatchObject({ status: "refused" });
expect((result as { warning: string }).warning).toContain("managed instances directory");
await expect(fs.stat(liveInstanceRoot)).resolves.toBeDefined();
});
it("refuses an instance pointer that belongs to a sibling worktree", async () => {
const worktreesDir = await makeTempRoot("paperclip-managed-worktrees-");
const workspacePath = await makeTempRoot("paperclip-cleanup-workspace-");
const siblingRoot = path.join(worktreesDir, "instances", "sibling-worktree");
await fs.mkdir(siblingRoot, { recursive: true });
await fs.writeFile(path.join(siblingRoot, "marker"), "keep me", "utf8");
await writeWorkspaceEnv(workspacePath, worktreesDir, "sibling-worktree");
const stopEmbeddedPostgres = vi.fn(async () => false);
const removeInstanceRoot = vi.fn(async () => {});
const pointer = await readWorktreeInstancePointer(workspacePath);
const result = await cleanupWorktreeInstanceArtifacts({
pointer: pointer!,
workspaceId: "workspace-1",
workspacePath,
expectedInstanceId: "owned-worktree",
expectedInstanceRoot: null,
worktreesDir,
dependencies: { stopEmbeddedPostgres, removeInstanceRoot },
});
expect(result).toMatchObject({ status: "refused", instanceRoot: siblingRoot });
expect((result as { warning: string }).warning).toContain("expected workspace instance");
expect(stopEmbeddedPostgres).not.toHaveBeenCalled();
expect(removeInstanceRoot).not.toHaveBeenCalled();
expect(await fs.readFile(path.join(siblingRoot, "marker"), "utf8")).toBe("keep me");
});
it("treats an ESRCH signal race as an already-stopped PostgreSQL process", async () => {
const instanceRoot = await makeTempRoot("paperclip-postgres-exit-race-");
const dataDir = path.join(instanceRoot, "db");
await fs.mkdir(dataDir, { recursive: true });
await fs.writeFile(path.join(dataDir, "postmaster.pid"), `4242\n${dataDir}\n`, "utf8");
const signalError = Object.assign(new Error("process exited"), { code: "ESRCH" });
await expect(stopEmbeddedPostgresIfRunning(dataDir, {
processIsAlive: () => true,
readVerifiedPostgresCommand: async () => `postgres -D ${dataDir}`,
signalProcess: () => { throw signalError; },
wait: async () => {},
})).resolves.toBe(false);
});
});