Files
PaperClipAI/tests/runner-e2e/cursor-cloud.test.ts
T
DottaandPaperclip 27d35314be feat: connect experimental Cursor Cloud to native Runner (#18069)
## Thinking Path

> - Paperclip helps people manage AI agents at work.
> - Native Runner owns execution and task authority.
> - Operators need an explicit native Cursor Cloud choice.
> - Tools/files need company, task, session and run scope.
> - This connects setup and lifecycle to Runner Core.
> - Legacy agents retain their selected runtime/history.

## Linked Issues or Issue Description

Depends on #18068. Managed-only API placement and API-owned Stop
containment are in #18310. Refs #5664, #11803, #17299, and #17471. No
duplicate native integration was found.

## What Changed

- Add explicit native setup, Test, saved settings and dispatch with
no-prompt account/catalog/callback checks.
- Scope MCP to company, agent, task, session and run through
authenticated HTTPS. Register verified artifacts/work products; preserve
unpriced accounting, completion and budgets.
- Connect Question/Plan continuation, cold handoff and admitted fresh
sessions; recheck waiting admission under the task lock.
- Present audited Stop intent while preserving physical containment
proof; bound typed Stop metadata. Preserve title edits through
preference/navigation readiness.
- Select one live/history representation per run to prevent duplicate
settled cards. Integrate upstream task/read-state/retry/schema contracts
and repair two test fixtures.

## Verification

Core #18068 merged normally as `3910352c923a`. Product head
`5a60f541c982` has passing repository CI/build/browser checks, including
required `ci / verify` and `ci / e2e`, plus a fresh exact-head Greptile
5/5 review with zero unresolved threads. Two non-required external app
checks remain pending: Paperclip Review is queued waiting for its
assigned agent; Superagent Security Scan is reviewing the diff. These
are not claimed completed. The normal merge endpoint enforces the
repository rules; no policy or status override is used. The dependent
Top PR is addressing its fresh deferred-Stop review finding.

Product `5a60f541c982` passed 270 UI and 20 selected
Cloud/native/disk-full tests, UI/server typechecks, tokens and maps.
Top’s clean normal merge passed tokens/maps/diff checks; its matrix is
pending. Earlier receipts retain Core’s 47 Rust, 4 compiled-native
retirement and 19 settlement controls, normal Runner build, and runtime
9af build/recursive typecheck. Reproduced loading/shutdown CI fixture
failures passed 7/7 browser and 121/121 unit tests, including a
broken-unsubscribe negative control. No timeout/oracle was relaxed.

The actual-parent restack adopts 75 mainline paths, including
schema/permission and generic runtime changes, beyond the prior two
fixture repairs and client projection fix. The full difference from
hosted `9af1a5de42a2` is therefore larger than four files; backend
source also differs. The merged config-read checks match Core391, and
the Cursor placement/runtime/relay/selection modules match frozen9af;
selected affected regressions pass. Paid proof keeps its actual 9af
source/image identity. The separately tested live/history card fix is
not claimed deployed on that host.

On 9af, original saved settings survived reopening and fresh no-prompt
Test authenticated catalog/model/binding; no new Save is claimed. Fresh
Linux admission passed. With `claude-sonnet-4-5`, Artifact downloaded
exact 27 bytes and matched 121 canonical identities in complete
S3-served history. First Stop’s strict quiet FAILED on a 10.373-second
gap despite independent cancellation/no late artifact. One targeted
unchanged-sampler retry retained all 24 samples: 35.076 seconds quiet /
maximum gap 4.584 seconds, 67 ordered canonical identities, and separate
CANCELLED/artifact-absent status after the original 180-second deadline.
The pre-reload duplicate settled card is disclosed. Exact-ID cleanup for
the three new provider agents is awaiting authorization, so full grades
and the fresh Artifact’s post-cleanup download remain blocked. A
historical deleted-provider artifact was newly downloaded on 9af;
original F226 Question/Plan/reconnect and cleanup proof keep their
identities.

Retained failures: stale fixture 132/133; E8 48/135 with 87 reset-hook
failures; Mac 17 SQL failures/skips; earlier
title/startup/custody/Stop/deletion failures with unknown causes/bounded
recoveries; `dea023` Stop’s warning/50 duplicate identities; pre-repair
ACK and original Core CI failures. WSP’s 251-assertion, 7/8-suite
attempt failed physical PostgreSQL teardown; the unchanged wrapper rerun
passed 128. Retry alone establishes no cause or full local-suite pass.
All paid attempts remain recorded; vendor dollar cost is UNKNOWN.

## Risks

Experimental: Cursor independently acknowledges neither the effective
model nor instruction digest before the first prompt. Explicit values
are submitted and checked through behavior; unpriced provider dollar
cost stays UNKNOWN.

Callbacks need authenticated reachable HTTPS. Scope, revocation,
budgets, completion and cleanup remain authoritative. Operator Stop
intent is separate from physical containment. Saved legacy agents retain
their selected runtime. Prior shared title-save/deletion failures remain
disclosed.

## Model Used

OpenAI Codex, GPT-6 family, assisted with reasoning, repository tools,
code execution, browser verification, and delegated code review. The
serving model’s exact API ID and context window are not exposed. No
value is inferred from the model catalog.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used and the limits of available model
metadata
- [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 linked public issues or described the issue in this PR
- [x] I have not referenced internal or instance-local Paperclip issues
or links
- [x] My branch describes the change and contains no internal ticket ID
- [x] I have run the scoped local tests reported above; pending gates
are stated separately
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation
- [x] I have considered and documented the risks
- [x] All required CI gates pass on the current head
- [x] Fresh Greptile is 5/5 with no unresolved findings on the current
head
- [x] I will address all reviewer comments before requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-03 09:01:39 -06:00

203 lines
16 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { createServer, type Server } from "node:http";
import { cursorCloudProfile, runnerMatrix } from "./catalog.js";
import { parseRunnerSelectors, selectRunnerExecutions } from "./selectors.js";
import { assertRunnerE2EPrerequisites } from "./prerequisites.js";
import { readCursorCloudRemoteIdentity, cursorCloudIdentityProof, CursorCloudOwnedObserver } from "./cursor-cloud-observer.js";
import { assertCursorCloudCallbackIngress, startCursorCloudCallbackProxy } from "./cursor-cloud-callback-proxy.js";
import type { RunnerApi } from "./api.js";
import { assertPublicCampaign } from "./history-publish.js";
import { CursorCloudClientError } from "../../packages/paperclip-runner/src/drivers/cursor-cloud/cloud-client.js";
const cells = runnerMatrix.filter(cell => cell.suite.id === "cursor-cloud");
const agentId = "bc-11111111-1111-4111-8111-111111111111", runId = "run-22222222-2222-4222-8222-222222222222";
function notice(ownerRunId = "local-run") {
return { runId: ownerRunId, protocolSchemaVersion: 1, payload: { prpEvent: { schema: "paperclip.prp.event.v1", schemaVersion: 1, runId: ownerRunId,
sourceKind: "runner", eventType: "provider.notice.recorded", payload: { summary: JSON.stringify({ schema: "paperclip.cursor-cloud.remote-identity.v1", provider: "cursor_cloud", agentId, runId, ownerRunId, normalizedSessionId: "owned-session" }) } } } };
}
afterEach(() => { vi.useRealTimers(); vi.unstubAllEnvs(); });
describe("Cursor Cloud Product E2E registration", () => {
it("registers artifact, follow-up, active Stop and semantic planning as single-attempt explicit local cells", () => {
expect(cells.map(cell => cell.task.id)).toEqual(["task-artifact", "cloud-followup", "active-run-stop", "plan-revise-accept"]);
for (const cell of cells) {
expect(cell.requiredCredentials).toEqual(["CURSOR_API_KEY"]);
expect(cell.environment.id).toBe("local"); expect(cell.task.automaticRetryPolicy).toBe("single_attempt");
expect(cell.suite.manualOnly).toBe(true);
expect(selectRunnerExecutions(parseRunnerSelectors(["--id", cell.id])).map(row => row.id)).toEqual([cell.id]);
}
expect(cells.map(cell => cell.task.expectedRunCount)).toEqual([1, 2, 1, 3]);
expect(selectRunnerExecutions(parseRunnerSelectors(["--all"])).some(cell => cell.suite.id === "cursor-cloud")).toBe(false);
expect(cells[0]!.task.buildMatchers("nonce", cells[0]!)).toContainEqual({ kind: "artifact_exact", name: "cursor-cloud-nonce.txt", expected: "verified-nonce\n", mimeType: "text/plain" });
const plan = cells.find(cell => cell.task.id === "plan-revise-accept")!.task;
expect(plan.workMode).toBe("planning");
expect(plan.flow).toBe("plan_revision_acceptance");
expect(plan.buildPlanMarkers?.("nonce")).toEqual({ draft: "PAPERCLIP_E2E_PLAN_DRAFT_nonce", revised: "PAPERCLIP_E2E_PLAN_REVISED_nonce" });
expect(plan.buildRevisionRequest?.("nonce")).toContain("targetRevisionId");
expect(plan.buildPrompt("nonce")).toContain("do not request Cursor native Plan mode");
expect(plan.buildPrompt("nonce")).not.toContain("remain in the requested planning collaboration mode");
});
it("binds the exact explicit model and encrypted Cloud credential without native permission fiction", () => {
vi.stubEnv("PAPERCLIP_CURSOR_CLOUD_ACCEPTANCE_REPO", "https://github.com/owned/disposable");
const profile = { ...cursorCloudProfile, model: "explicit-account-model" };
const payload = profile.buildAgent({ executionId: "owned", environmentId: "local-env", environmentFixtureId: "local", workspacePath: "/tmp/owned",
secretRefs: { CURSOR_API_KEY: { type: "secret_ref", secretId: "secret", version: "latest" } } });
expect(payload.adapterConfig).toMatchObject({ provider: "cursor_cloud", model: "explicit-account-model", repoUrl: "https://github.com/owned/disposable", lifecycleMode: "per_turn", env: { CURSOR_API_KEY: { type: "secret_ref", secretId: "secret" } } });
expect(payload.adapterConfig).not.toHaveProperty("codexPermissionMode"); expect(payload.adapterConfig).not.toHaveProperty("cwd");
});
it("refuses public publication of native provider identity snapshots", () => {
expect(() => assertPublicCampaign({ results: [{ suiteId: "cursor-cloud" } as never] })).toThrow("Private");
expect(() => assertPublicCampaign({ results: [{ suiteId: "core-compatibility" } as never] })).not.toThrow();
});
it("setup-blocks missing prerequisites before credential loading and accepts an explicitly configured local profile", () => {
expect(() => assertRunnerE2EPrerequisites(cells, {})).toThrow("setup-blocked");
const environment = { PAPERCLIP_CURSOR_CLOUD_ACCEPTANCE_MODEL: "explicit-model", PAPERCLIP_CURSOR_CLOUD_ACCEPTANCE_REPO: "https://github.com/owned/disposable", PAPERCLIP_PUBLIC_URL: "https://callback.example.test", PAPERCLIP_CURSOR_CLOUD_CALLBACK_PORT: "43121", PAPERCLIP_CURSOR_CLOUD_ALLOW_UNPRICED_QUALIFICATION: "1" };
const configured = cells.map(cell => ({ ...cell, profile: { ...cell.profile, model: "explicit-model" } }));
expect(() => assertRunnerE2EPrerequisites(configured, environment)).not.toThrow();
expect(() => assertRunnerE2EPrerequisites(configured, { ...environment, PAPERCLIP_CURSOR_CLOUD_ALLOW_UNPRICED_QUALIFICATION: undefined })).toThrow("ALLOW_UNPRICED_QUALIFICATION");
expect(() => assertRunnerE2EPrerequisites(configured, { ...environment, PAPERCLIP_PUBLIC_URL: "http://localhost" })).toThrow("setup-blocked");
expect(() => assertRunnerE2EPrerequisites(configured, { ...environment, PAPERCLIP_CURSOR_CLOUD_ACCEPTANCE_REPO: "https://user:secret@example.test/repo" })).toThrow("setup-blocked");
expect(() => assertRunnerE2EPrerequisites(cells, environment)).toThrow("exact configured model");
});
});
describe("Cloud observer evidence calibration", () => {
function planObserver(change?: (rows: Array<Record<string, any>>, identities: Array<Record<string, any>>) => void) {
const rows = [0, 1, 2].map(index => ({ id: `local-${index}`, companyId: "owned-company", agentId: "owned-agent", sessionIdBefore: null,
contextSnapshot: index === 2 ? { forceFreshSession: true, workspaceRefreshReason: "accepted_plan_confirmation",
source: "issue.interaction.accept", interactionStatus: "accepted", interactionId: "revised-confirmation" } : {} }));
const identities = rows.map((row, index) => ({ agentId: index < 2 ? agentId : "bc-33333333-3333-4333-8333-333333333333",
runId: `run-22222222-2222-4222-8222-22222222222${index}`, normalizedSessionId: index < 2 ? "planning-session" : "accepted-session" }));
change?.(rows, identities);
const api = { get: vi.fn(async (path: string) => {
if (path.includes("companies/")) return rows;
const index = rows.findIndex(row => path.includes(`/heartbeat-runs/${row.id}`));
if (index < 0) throw new Error("Unknown owned fixture run");
if (!path.includes("/events?")) return rows[index];
const value = notice(rows[index]!.id);
const identity = JSON.parse(value.payload.prpEvent.payload.summary);
value.payload.prpEvent.payload.summary = JSON.stringify({ ...identity, ...identities[index] });
return [{ ...value, seq: 1 }];
}) } as unknown as RunnerApi;
return new CursorCloudOwnedObserver(api, "owned-company", "owned-agent", "test-key");
}
it("allows only the revision-bound accepted Plan to rotate its owned Cloud session", async () => {
const observer = planObserver();
expect((await observer.assertContinuity(3))[0]?.passed).toBe(false);
expect((await observer.assertPlanTransitions(["local-0", "local-1", "local-2"], "revised-confirmation"))[0]?.passed).toBe(true);
});
it.each(["early rotation", "same accepted agent", "same accepted session", "duplicate native run", "wrong interaction", "missing reset", "wrong source", "not accepted", "prior session"])("rejects %s during Plan transitions", async reason => {
const observer = planObserver((rows, identities) => {
if (reason === "early rotation") identities[1]!.normalizedSessionId = "unexplained-session";
if (reason === "same accepted agent") identities[2]!.agentId = identities[1]!.agentId;
if (reason === "same accepted session") identities[2]!.normalizedSessionId = identities[1]!.normalizedSessionId;
if (reason === "duplicate native run") identities[2]!.runId = identities[1]!.runId;
if (reason === "wrong interaction") rows[2]!.contextSnapshot.interactionId = "old-confirmation";
if (reason === "missing reset") rows[2]!.contextSnapshot.forceFreshSession = false;
if (reason === "wrong source") rows[2]!.contextSnapshot.source = "issue.comment";
if (reason === "not accepted") rows[2]!.contextSnapshot.interactionStatus = "pending";
if (reason === "prior session") rows[2]!.sessionIdBefore = "planning-session";
});
expect((await observer.assertPlanTransitions(["local-0", "local-1", "local-2"], "revised-confirmation"))[0]?.passed).toBe(false);
});
it("rejects missing, duplicate or reordered Plan turn ownership", async () => {
for (const owners of [["local-0", "local-1"], ["local-0", "local-0", "local-2"], ["local-2", "local-1", "local-0"]]) {
expect((await planObserver().assertPlanTransitions(owners, "revised-confirmation"))[0]?.passed).toBe(false);
}
expect((await planObserver().assertPlanTransitions(["local-0", "local-1", "local-2"], null))[0]?.passed).toBe(false);
});
it("requires persisted runner-owned identity and exports digests without native IDs", () => {
const identity = readCursorCloudRemoteIdentity([notice()], "local-run")!;
expect(identity).toMatchObject({ agentId, runId, ownerRunId: "local-run" });
expect(JSON.stringify(cursorCloudIdentityProof(identity))).not.toContain(agentId);
expect(readCursorCloudRemoteIdentity([], "local-run")).toBeUndefined();
expect(readCursorCloudRemoteIdentity([notice("foreign-run")], "local-run")).toBeUndefined();
});
it.each(["provider", "text", "schema", "row", "owner", "native-id"])("rejects plausible wrong %s identity evidence", reason => {
const value = notice(), event = value.payload.prpEvent;
if (reason === "provider") event.sourceKind = "provider";
if (reason === "text") event.eventType = "assistant.message";
if (reason === "schema") event.schemaVersion = 2;
if (reason === "row") value.runId = "other-run";
if (["owner", "native-id"].includes(reason)) {
const identity = JSON.parse(event.payload.summary); identity[reason === "owner" ? "ownerRunId" : "agentId"] = "foreign"; event.payload.summary = JSON.stringify(identity);
expect(() => readCursorCloudRemoteIdentity([value], "local-run")).toThrow("invalid owner");
} else expect(readCursorCloudRemoteIdentity([value], "local-run")).toBeUndefined();
});
it("refuses unrelated agents before any provider cleanup mutation", async () => {
const api = { get: vi.fn(async (path: string) => path.includes("companies/") ? [{ id: "foreign" }] : { id: "foreign", companyId: "owned-company", agentId: "other-agent" }) } as unknown as RunnerApi;
const observer = new CursorCloudOwnedObserver(api, "owned-company", "owned-agent", "test-key");
const remove = vi.spyOn(observer.client, "deleteAgent");
expect((await observer.cleanup())[0]?.passed).toBe(false); expect(remove).not.toHaveBeenCalled();
});
function ownedObserver() {
const api = { get: vi.fn(async (path: string) => path.includes("/events?") ? [{ ...notice(), seq: 1 }] : path.includes("companies/") ? [{ id: "local-run" }] : { id: "local-run", companyId: "owned-company", agentId: "owned-agent" }) } as unknown as RunnerApi;
const observer = new CursorCloudOwnedObserver(api, "owned-company", "owned-agent", "test-key");
vi.spyOn(observer.client, "getRun").mockResolvedValue({ id: runId, agentId, status: "CANCELLED", createdAt: "2026-10-28T00:00:00Z", updatedAt: "2026-10-28T00:00:00Z" });
return observer;
}
it("does not equate a deletion acknowledgment with remote absence", async () => {
vi.useFakeTimers();
const observer = ownedObserver();
vi.spyOn(observer.client, "deleteAgent").mockResolvedValue({ id: agentId });
const lookup = vi.spyOn(observer.client, "getAgent").mockResolvedValue({ id: agentId, status: "IDLE", env: { type: "cloud" }, url: "https://cursor.com/agents/test", createdAt: "2026-10-28T00:00:00Z", updatedAt: "2026-10-28T00:00:00Z" });
const startedAt = Date.now();
const completed = observer.cleanup();
await vi.runAllTimersAsync();
expect(await completed).toMatchObject([{ passed: false }]);
expect(Date.now() - startedAt).toBeLessThanOrEqual(30_000);
expect(lookup).toHaveBeenCalledWith(agentId, expect.objectContaining({ signal: expect.any(AbortSignal) }));
});
it.each(["timeout", "acknowledged"])("waits for authoritative absence after an %s delete without issuing another mutation", async outcome => {
vi.useFakeTimers();
const observer = ownedObserver();
const remove = vi.spyOn(observer.client, "deleteAgent");
if (outcome === "timeout") remove.mockRejectedValue(new CursorCloudClientError("timeout"));
else remove.mockResolvedValue({ id: agentId });
const lookup = vi.spyOn(observer.client, "getAgent")
.mockResolvedValueOnce({ id: agentId, status: "IDLE", env: { type: "cloud" }, url: "https://cursor.com/agents/test", createdAt: "2026-10-28T00:00:00Z", updatedAt: "2026-10-28T00:00:00Z" })
.mockRejectedValue(new CursorCloudClientError("http_error", { status: 404 }));
const completed = observer.cleanup();
await vi.runAllTimersAsync();
expect(await completed).toMatchObject([{ passed: true }]);
expect(remove).toHaveBeenCalledOnce();
expect(lookup).toHaveBeenCalledTimes(2);
});
it("reconciles an ambiguous deletion reply through authoritative absence", async () => {
const observer = ownedObserver();
vi.spyOn(observer.client, "deleteAgent").mockRejectedValue(new CursorCloudClientError("invalid_response"));
vi.spyOn(observer.client, "getAgent").mockRejectedValue(new CursorCloudClientError("http_error", { status: 404 }));
expect((await observer.cleanup())[0]?.passed).toBe(true);
});
it("retains a deletion authorization failure even if a later lookup hides the resource", async () => {
const observer = ownedObserver();
vi.spyOn(observer.client, "deleteAgent").mockRejectedValue(new CursorCloudClientError("http_error", { status: 403 }));
vi.spyOn(observer.client, "getAgent").mockRejectedValue(new CursorCloudClientError("http_error", { status: 404 }));
expect((await observer.cleanup())[0]?.passed).toBe(false);
});
it("does not accept an unavailable absence check", async () => {
const observer = ownedObserver();
vi.spyOn(observer.client, "deleteAgent").mockResolvedValue({ id: agentId });
vi.spyOn(observer.client, "getAgent").mockRejectedValue(new CursorCloudClientError("timeout"));
expect((await observer.cleanup())[0]?.passed).toBe(false);
});
});
const listen = async (server: Server) => { await new Promise<void>((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolve); }); return (server.address() as { port: number }).port; };
const close = (server: Server) => new Promise<void>((resolve, reject) => { server.closeAllConnections(); server.close(error => error ? reject(error) : resolve()); });
it("exposes only the callback path over a real loopback connection and strips operator cookies", async () => {
const requests: Array<{ path?: string; cookie?: string; auth?: string }> = [];
const app = createServer((request, response) => { requests.push({ path: request.url, cookie: request.headers.cookie, auth: request.headers.authorization }); response.writeHead(401).end(); });
const appPort = await listen(app), reservation = createServer(), proxyPort = await listen(reservation); await close(reservation);
const stop = await startCursorCloudCallbackProxy({ appPort, port: proxyPort });
try {
const origin = `http://127.0.0.1:${proxyPort}`;
await assertCursorCloudCallbackIngress(origin);
const response = await fetch(`${origin}/mcp/native-runs/00000000-0000-4000-8000-000000000000`, { method: "POST", headers: { cookie: "operator-session", authorization: "Bearer owned-capability" }, body: "{}" });
expect(response.status).toBe(401);
expect(requests).toHaveLength(2); expect(requests.every(request => !request.cookie)).toBe(true);
expect(requests.at(-1)?.auth).toBe("Bearer owned-capability");
await expect(assertCursorCloudCallbackIngress("https://bad.example.test", async () => new Response("operator app", { status: 200 }))).rejects.toThrow("callback listener");
} finally { await stop(); await close(app); }
});