mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 01:39:51 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Each pull request runs CI, and the slowest check sets the time until a reviewer sees a result > - The server tests run in 12 shards, and the slowest shard sets that time > - In PR run [45186146272](https://github.com/paperclipai/paperclip/actions/runs/45186146272), the slowest check was **General tests (server (3/12))** at **872s** > - One file in that shard, `server/src/services/ssh-remote-workspace.integration.test.ts`, took **317s** on one worker, because its 11 heartbeat runs against a real SSH server ran one after another > - This pull request moves the shared setup into a fixture and divides the heartbeat runs into four files that can run in parallel > - The benefit is a shorter slowest server shard and more even shards ## Linked Issues or Issue Description No public issue exists. This is the problem: **Problem / motivation** The file `ssh-remote-workspace.integration.test.ts` took 317s in CI (`PAPERCLIP_TEST_TIMING`). The next slowest file in the same shard took 206s. The slowest files in the other shards took about 110s or less. The file was also over the 180s limit for oversized test files. Most of the time came from the 11 `it.each` heartbeat runs. Each run does a full heartbeat and a follow-up against a real SSH server. **Proposed solution** Move the shared embedded-Postgres and SSH setup and the heartbeat-run body into a fixture. Divide the heartbeat runs into one file for each large harness family, and one catch-all file for the other harnesses. **Alternatives considered** Increase the shard count. That makes every server shard pay the setup cost again and does not make the 317s file shorter. Run the `it.each` cases concurrently in one file. That puts many heartbeat runs on one shared SSH server and one database, which can cause flaky tests. ## What Changed - `__tests__/ssh-remote-workspace-fixture.ts` now holds the shared embedded-Postgres/SSH setup (`useSshWorkspaceFixture`), the harness table, and the heartbeat-run body (`describeSshHeartbeatRuns`). The body was moved without changes. - `ssh-remote-workspace.integration.test.ts` keeps its two durable-workspace tests and uses the shared fixture. - The heartbeat runs are now in four files: - `ssh-remote-workspace-heartbeat-claude` (2 runs) - `ssh-remote-workspace-heartbeat-codex` (2 runs) - `ssh-remote-workspace-heartbeat-gemini` (2 runs) - `ssh-remote-workspace-heartbeat-cli` (5 runs). This file uses `exclude`, so it runs every harness that does not have its own file. A new harness gets coverage automatically. - Each file gets its own Postgres, temporary root and sshd. Company `issuePrefix` values still come from the global run index, so they stay unique. **Expected savings:** The largest single file goes from about 317s to about 60–120s. After the timing-weighted balancer has data for the new files, shard 3/12 should go from about 872s to the level of the other server shards (about 790–855s). That is about **20–80s off the slowest server shard**. The PR critical path gets the same saving when this shard is the last to finish. ## Verification - Run `pnpm --filter @paperclipai/server exec vitest run src/services/ssh-remote-workspace` on a machine with sshd. Locally with sshd and embedded Postgres: **5 files passed, 13 tests passed** (2 + 11, the same count as before), 210s wall time with the files in parallel. - CI on this PR: all server shards, the serialized suites and typecheck passed. - Compare the timing of the new files in the CI timing output of a later run. ## Risks - Low risk. The change only affects test files. Product code does not change. - Each new file starts its own embedded Postgres and sshd. This adds a small setup cost per file, but the files run on different workers. - If a harness is added to the `claude`, `codex` or `gemini` include lists but is not added to the `exclude` list of the catch-all file, it runs twice. It is not skipped. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), by Anthropic, in Claude Code with tool use and code execution. ## 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 - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: devinfoley <139239+devinfoley@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
14 KiB
TypeScript
164 lines
14 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { agents, companies, createDb, environmentLeases, environments, executionWorkspaces, heartbeatRuns, issues } from "@paperclipai/db";
|
|
import type { AdapterSshExecutionTarget } from "@paperclipai/adapter-utils/execution-target";
|
|
import { buildSshEnvLabFixtureConfig } from "@paperclipai/adapter-utils/ssh";
|
|
import type { EnvironmentLease } from "@paperclipai/shared";
|
|
import { createLocalDiskStorageProvider } from "../storage/local-disk-provider.js";
|
|
import { createStorageService } from "../storage/service.js";
|
|
import { prepareRemoteWorkspace, readRemoteWorkspaceAuthority, reserveRemoteWorkspace, saveRemoteWorkspace } from "./remote-workspaces.js";
|
|
import { requireRemoteWorkspaceTarget, hasRemoteRecoveryAttachment } from "./remote-workspace-transport.js";
|
|
import { remoteTerminationReceipt } from "./remote-execution-termination.js";
|
|
import { withNativeWorkspaceFinalizationOwnership } from "./native-runtime/native-workspace-finalization-ownership.js";
|
|
import * as recoverySnapshots from "./workspace-recovery-snapshot.js";
|
|
import { sshWorkspaceTestsSupported, useSshWorkspaceFixture } from "./__tests__/ssh-remote-workspace-fixture.js";
|
|
|
|
describe.skipIf(!sshWorkspaceTestsSupported)("Cloud SSH durable workspace with a real SSH server", () => {
|
|
const fixture = useSshWorkspaceFixture("cloud-ssh-workspace-");
|
|
|
|
it("keeps Hello World through controller replacement and restores it to a new SSH allocation", async () => {
|
|
const { root, sshd, database } = fixture;
|
|
let { db } = fixture;
|
|
const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(), runId = randomUUID();
|
|
await db.insert(companies).values({ id: companyId, name: "SSH workspace test", issuePrefix: "SSH" });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "SSH Claude", adapterType: "claude_local" });
|
|
await db.insert(issues).values({ id: issueId, companyId, title: "Write Hello World", assigneeAgentId: agentId });
|
|
const config = await buildSshEnvLabFixtureConfig(sshd!);
|
|
const [environment] = await db.insert(environments).values({ driver: "ssh", name: "SSH test", config: {} }).returning();
|
|
const storage = { ...createStorageService(createLocalDiskStorageProvider(path.join(root, "objects"))), provider: "s3" as const };
|
|
|
|
async function acquire(id: string, remoteCwd = config.remoteWorkspacePath, providerLeaseId = "ssh-fixture") {
|
|
await db.insert(heartbeatRuns).values({ id, companyId, agentId, issueId, runtimeMode: "legacy", status: "running" });
|
|
const [lease] = await db.insert(environmentLeases).values({ companyId, environmentId: environment.id,
|
|
heartbeatRunId: id, issueId, provider: "ssh", providerLeaseId, status: "active",
|
|
metadata: { driver: "ssh", remoteCwd } }).returning();
|
|
const target: AdapterSshExecutionTarget = { kind: "remote", transport: "ssh", environmentId: environment.id,
|
|
leaseId: lease.id, remoteCwd, spec: { ...config, remoteCwd, useHostAuth: false } };
|
|
return { lease: lease as EnvironmentLease, target };
|
|
}
|
|
|
|
async function stop(id: string, lease: EnvironmentLease) {
|
|
await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, id));
|
|
const [stopped] = await db.update(environmentLeases).set({ metadata: { ...lease.metadata,
|
|
remoteExecutionTermination: remoteTerminationReceipt(lease, { state: "stopped", providerLeaseId: lease.providerLeaseId }) } })
|
|
.where(eq(environmentLeases.id, lease.id)).returning();
|
|
return stopped as EnvironmentLease;
|
|
}
|
|
|
|
const first = await acquire(runId);
|
|
const reserved = await reserveRemoteWorkspace({ db, storage, companyId, issueId, runId, agentId,
|
|
environmentId: environment.id, projectId: null, projectWorkspaceId: null, existingWorkspaceId: null, source: { kind: "empty" }, mode: "shared_workspace" });
|
|
const prepared = await prepareRemoteWorkspace({ db, storage, workspace: reserved, runId, ...first });
|
|
const firstTarget = { ...first.target, remoteCwd: prepared.remoteRoot, spec: { ...first.target.spec, remoteCwd: prepared.remoteRoot } };
|
|
const result = await requireRemoteWorkspaceTarget(firstTarget).runner!.execute({ command: "node", args: ["-e",
|
|
"require('fs').writeFileSync('hello_world.txt','Hello World\\n')"], cwd: prepared.remoteRoot, timeoutMs: 10_000 });
|
|
expect(result.exitCode).toBe(0);
|
|
const stopped = await stop(runId, prepared.lease);
|
|
await expect(withNativeWorkspaceFinalizationOwnership({ db, companyId, runId }, async () => {}))
|
|
.rejects.toThrow("binding_missing");
|
|
await expect(withNativeWorkspaceFinalizationOwnership({ db, companyId, runId, remoteWorkspaceId: randomUUID() }, async () => {}))
|
|
.rejects.toThrow("binding_missing");
|
|
await expect(withNativeWorkspaceFinalizationOwnership({ db, companyId, runId, remoteWorkspaceId: prepared.workspace.id }, ownership =>
|
|
saveRemoteWorkspace({ db, storage, workspace: prepared.workspace, runId, lease: stopped,
|
|
target: { ...firstTarget, spec: { ...firstTarget.spec, host: "127.0.0.1", port: 1 } }, assertOwned: ownership.assertHeld })))
|
|
.rejects.toThrow();
|
|
const [held] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, reserved.id));
|
|
expect(readRemoteWorkspaceAuthority(held.metadata)).toMatchObject({ writerRunId: runId, dirty: true,
|
|
snapshotId: readRemoteWorkspaceAuthority(prepared.workspace.metadata)!.snapshotId });
|
|
expect(await readFile(path.join(prepared.remoteRoot, "hello_world.txt"), "utf8")).toBe("Hello World\n");
|
|
const blockedId = randomUUID();
|
|
await db.insert(heartbeatRuns).values({ id: blockedId, companyId, agentId, issueId, runtimeMode: "legacy", status: "running" });
|
|
await expect(reserveRemoteWorkspace({ db, storage, companyId, issueId, runId: blockedId, agentId,
|
|
environmentId: environment.id, projectId: null, projectWorkspaceId: null,
|
|
existingWorkspaceId: prepared.workspace.id, source: { kind: "empty" } })).rejects.toMatchObject({ status: 409 });
|
|
const peerIssueId = randomUUID(), peerRunId = randomUUID();
|
|
await db.insert(issues).values({ id: peerIssueId, companyId, title: "Use the saved shared workspace", assigneeAgentId: agentId,
|
|
executionWorkspaceId: prepared.workspace.id });
|
|
await db.insert(heartbeatRuns).values({ id: peerRunId, companyId, agentId, issueId: peerIssueId, runtimeMode: "legacy", status: "running" });
|
|
const finalized = await withNativeWorkspaceFinalizationOwnership({ db, companyId, runId, remoteWorkspaceId: prepared.workspace.id }, async ownership => {
|
|
await saveRemoteWorkspace({ db, storage, workspace: prepared.workspace, runId,
|
|
lease: stopped, target: firstTarget, assertOwned: ownership.assertHeld });
|
|
const handedOn = await reserveRemoteWorkspace({ db, storage, companyId, issueId: peerIssueId, runId: peerRunId, agentId,
|
|
environmentId: environment.id, projectId: null, projectWorkspaceId: null, existingWorkspaceId: prepared.workspace.id, source: { kind: "empty" } });
|
|
expect(readRemoteWorkspaceAuthority(handedOn.metadata)).toMatchObject({ epoch: 2, writerRunId: peerRunId });
|
|
await ownership.assertHeld();
|
|
await expect(saveRemoteWorkspace({ db, storage, workspace: prepared.workspace, runId,
|
|
lease: stopped, target: firstTarget, assertOwned: ownership.assertHeld })).rejects.toThrow("authority_changed");
|
|
});
|
|
expect(finalized.acquired).toBe(true);
|
|
const [finishedOwner] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, runId));
|
|
expect(finishedOwner.runnerProfileJson).not.toHaveProperty("nativeWorkspaceFinalizationOwner");
|
|
await expect(withNativeWorkspaceFinalizationOwnership({ db, companyId, runId, remoteWorkspaceId: prepared.workspace.id }, async () => {}))
|
|
.rejects.toThrow("binding_missing");
|
|
await db.update(heartbeatRuns).set({ status: "cancelled", finishedAt: new Date() }).where(eq(heartbeatRuns.id, peerRunId));
|
|
|
|
// Replace the controller connection. The saved task and remote files are the authority.
|
|
db = fixture.db = createDb(database.connectionString);
|
|
const [saved] = await db.select().from(executionWorkspaces).where(and(eq(executionWorkspaces.id, reserved.id), eq(executionWorkspaces.companyId, companyId)));
|
|
const snapshotId = readRemoteWorkspaceAuthority(saved.metadata)!.snapshotId;
|
|
expect(snapshotId).toBeTruthy();
|
|
expect(await readFile(path.join(prepared.remoteRoot, "hello_world.txt"), "utf8")).toBe("Hello World\n");
|
|
|
|
const followupId = randomUUID();
|
|
const followup = await acquire(followupId);
|
|
const reused = await reserveRemoteWorkspace({ db, storage, companyId, issueId, runId: followupId, agentId,
|
|
environmentId: environment.id, projectId: null, projectWorkspaceId: null, existingWorkspaceId: saved.id, source: { kind: "empty" } });
|
|
const continued = await prepareRemoteWorkspace({ db, storage, workspace: reused, runId: followupId, ...followup });
|
|
expect(continued.remoteRoot).toBe(prepared.remoteRoot);
|
|
await saveRemoteWorkspace({ db, storage, workspace: continued.workspace, runId: followupId,
|
|
lease: await stop(followupId, continued.lease), target: followup.target, assertOwned: async () => {} });
|
|
|
|
const [latest] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, saved.id));
|
|
const restoredBase = path.join(root, "replacement-remote");
|
|
await mkdir(restoredBase);
|
|
const restoreId = randomUUID(), replacement = await acquire(restoreId, restoredBase, "replacement-ssh-allocation");
|
|
const restoring = await reserveRemoteWorkspace({ db, storage, companyId, issueId, runId: restoreId, agentId,
|
|
environmentId: environment.id, projectId: null, projectWorkspaceId: null, existingWorkspaceId: latest.id, source: { kind: "empty" } });
|
|
const restored = await prepareRemoteWorkspace({ db, storage, workspace: restoring, runId: restoreId, ...replacement });
|
|
expect(restored.remoteRoot).not.toBe(prepared.remoteRoot);
|
|
expect(await readFile(path.join(restored.remoteRoot, "hello_world.txt"), "utf8")).toBe("Hello World\n");
|
|
}, 60_000);
|
|
|
|
it("prepares a verified recovered copy on SSH without directory-sync support", async () => {
|
|
const { root, sshd, db } = fixture;
|
|
const companyId = randomUUID(), agentId = randomUUID(), issueId = randomUUID(), runId = randomUUID(), sourceId = randomUUID(), operationId = randomUUID();
|
|
await db.insert(companies).values({ id: companyId, name: "SSH recovery test", issuePrefix: "REC" });
|
|
await db.insert(agents).values({ id: agentId, companyId, name: "Recovery worker", adapterType: "claude_local" });
|
|
await db.insert(issues).values({ id: issueId, companyId, title: "Recover earlier files", assigneeAgentId: agentId });
|
|
await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, issueId, runtimeMode: "legacy", status: "running" });
|
|
const [environment] = await db.insert(environments).values({ driver: "ssh", name: "SSH recovery", config: {} }).returning();
|
|
const config = await buildSshEnvLabFixtureConfig(sshd!);
|
|
const [lease] = await db.insert(environmentLeases).values({ companyId, environmentId: environment.id,
|
|
heartbeatRunId: runId, issueId, provider: "ssh", providerLeaseId: "ssh-recovery", status: "active" }).returning();
|
|
const target: AdapterSshExecutionTarget = { kind: "remote", transport: "ssh", environmentId: environment.id,
|
|
leaseId: lease.id, remoteCwd: config.remoteWorkspacePath, spec: { ...config, remoteCwd: config.remoteWorkspacePath } };
|
|
const storage = { ...createStorageService(createLocalDiskStorageProvider(path.join(root, "recovery-objects"))), provider: "s3" as const };
|
|
const reserved = await reserveRemoteWorkspace({ db, storage, companyId, issueId, runId, agentId,
|
|
environmentId: environment.id, projectId: null, projectWorkspaceId: null, existingWorkspaceId: null,
|
|
source: { kind: "empty" }, recoverySourceRunIds: [sourceId] });
|
|
const provenance = JSON.stringify({ operationId, sourceRunId: sourceId });
|
|
// Start at the verified extraction boundary; source authorization and snapshot verification keep their existing owner.
|
|
const extract = vi.spyOn(recoverySnapshots, "attachPreservedWorkspaceSnapshots").mockImplementationOnce(async input => {
|
|
expect(input.sourceRunIds).toContain(sourceId);
|
|
const recovered = path.join(input.cwd, "recovered", operationId);
|
|
await mkdir(path.join(recovered, "files", "nested"), { recursive: true });
|
|
await writeFile(path.join(recovered, "files", "nested", "preserved.txt"), "Earlier work\n");
|
|
await writeFile(path.join(recovered, "provenance.json"), provenance);
|
|
return [{ operationId, sourceRunId: sourceId, path: recovered }];
|
|
});
|
|
try {
|
|
const prepared = await prepareRemoteWorkspace({ db, storage, workspace: reserved, runId, lease: lease as EnvironmentLease, target });
|
|
const recovered = path.join(prepared.remoteRoot, ".paperclip-recovered", operationId);
|
|
expect(await readFile(path.join(recovered, "files", "nested", "preserved.txt"), "utf8")).toBe("Earlier work\n");
|
|
expect(await readFile(path.join(recovered, "provenance.json"), "utf8")).toBe(provenance);
|
|
expect(await hasRemoteRecoveryAttachment({ target: requireRemoteWorkspaceTarget(target), remoteCwd: prepared.remoteRoot, operationId, provenance })).toBe(true);
|
|
await writeFile(path.join(recovered, "files", "nested", "preserved.txt"), "A later edit\n");
|
|
await prepareRemoteWorkspace({ db, storage, workspace: prepared.workspace, runId, lease: prepared.lease, target });
|
|
expect(await readFile(path.join(recovered, "files", "nested", "preserved.txt"), "utf8")).toBe("A later edit\n");
|
|
} finally { extract.mockRestore(); }
|
|
}, 60_000);
|
|
});
|