mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-06 14:02:36 +01:00
## 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>
396 lines
16 KiB
TypeScript
396 lines
16 KiB
TypeScript
import { expect, test, type APIRequestContext } from "@playwright/test";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { startReviewProvider } from "../fixtures/connection-review-provider";
|
|
|
|
type Json = Record<string, unknown>;
|
|
type Seed = { companyId: string; prefix: string };
|
|
type Agent = { id: string; name: string };
|
|
|
|
async function json<T = Json>(
|
|
response: Awaited<ReturnType<APIRequestContext["get"]>>,
|
|
): Promise<T> {
|
|
expect(
|
|
response.ok(),
|
|
`${response.url()} failed ${response.status()}: ${await response.text()}`,
|
|
).toBe(true);
|
|
return (await response.json()) as T;
|
|
}
|
|
|
|
async function newCompany(request: APIRequestContext): Promise<Seed> {
|
|
const company = await json<{ id: string; issuePrefix: string }>(
|
|
await request.post("/api/companies", {
|
|
data: { name: `Connection review E2E ${Date.now()}` },
|
|
}),
|
|
);
|
|
return { companyId: company.id, prefix: company.issuePrefix };
|
|
}
|
|
|
|
async function createAgent(
|
|
request: APIRequestContext,
|
|
companyId: string,
|
|
name: string,
|
|
): Promise<Agent> {
|
|
return await json<Agent>(
|
|
await request.post(`/api/companies/${companyId}/agents`, {
|
|
data: {
|
|
name,
|
|
role: "qa",
|
|
title: "Connection intent fixture agent",
|
|
capabilities: "Exercises deterministic connection intent wiring.",
|
|
adapterType: "process",
|
|
adapterConfig: {
|
|
command: process.execPath,
|
|
args: ["--input-type=module", "-e", "process.exit(0)"],
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
|
|
function reviewAgentScript(connectionId: string, query: string) {
|
|
return `
|
|
const base = process.env.PAPERCLIP_API_URL + "/api";
|
|
|
|
const headers = { authorization: "Bearer " + process.env.PAPERCLIP_API_KEY, "content-type": "application/json", "x-paperclip-run-id": process.env.PAPERCLIP_RUN_ID };
|
|
const api = async (path, method = "GET", body) => {
|
|
const response = await fetch(base + path, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
if (!response.ok) throw new Error(await response.text());
|
|
return await response.json();
|
|
};
|
|
const describePages = (raw) => {
|
|
let value = raw;
|
|
if (typeof value === "string") { try { value = JSON.parse(value); } catch {} }
|
|
const content = typeof value === "string" ? value : typeof value?.content === "string" ? value.content : value?.data?.content?.filter(item => item.type === "text").map(item => item.text).join(" ") ?? "No pages returned.";
|
|
return "I found these recent pages: " + content.replace(/^Pages: /, "") + ".";
|
|
};
|
|
const issueId = process.env.PAPERCLIP_TASK_ID ?? (await api("/heartbeat-runs/" + process.env.PAPERCLIP_RUN_ID)).contextSnapshot.issueId;
|
|
const interactions = await api("/issues/" + issueId + "/interactions");
|
|
const review = interactions.find(i => i.payload?.toolAction);
|
|
if (review?.status === "pending") { console.log("Still waiting for the existing review; no retry."); process.exit(0); }
|
|
if (review && review.status !== "pending") {
|
|
const result = review.result?.toolAction;
|
|
if (review.status === "accepted" && (!result || ["approved", "executing"].includes(result.status))) {
|
|
console.log("Review accepted; waiting for the stored execution result. No retry.");
|
|
process.exit(0);
|
|
}
|
|
const message = review.status === "rejected" ? "Review declined. No pages were read." : result?.status === "failed" ? "Read failed: " + result.errorMessage : ["expired", "cancelled"].includes(review.status) ? "The review expired or was cancelled. No pages were read." : describePages(result?.resultSummary);
|
|
await api("/issues/" + issueId + "/comments", "POST", { body: message });
|
|
await api("/issues/" + issueId, "PATCH", { status: "done" });
|
|
console.log(message);
|
|
process.exit(0);
|
|
}
|
|
const session = await api("/tool-gateway/sessions", "POST", { runId: process.env.PAPERCLIP_RUN_ID, ttlMs: 60000 });
|
|
const gatewayHeaders = { "x-paperclip-tool-gateway-token": session.token, "content-type": "application/json" };
|
|
const tools = await (await fetch(base + "/tool-gateway/tools", { headers: gatewayHeaders })).json();
|
|
const tool = tools.find(t => t.connectionId === ${JSON.stringify(connectionId)} && t.upstreamToolName === "notion:list_pages");
|
|
if (!tool) throw new Error("Missing fixture connection");
|
|
const call = await fetch(base + "/tool-gateway/tools/call", { method: "POST", headers: gatewayHeaders, body: JSON.stringify({ tool: tool.name, parameters: { query: ${JSON.stringify(query)} } }) });
|
|
const result = await call.json();
|
|
if (!call.ok) {
|
|
if (result.reasonCode !== "approval_required" && result.code !== "approval_required" && !JSON.stringify(result).includes("approval_required")) throw new Error(JSON.stringify(result));
|
|
await api("/issues/" + issueId, "PATCH", { status: "in_review" });
|
|
console.log("Waiting for human review. No retry.");
|
|
process.exit(0);
|
|
}
|
|
await api("/issues/" + issueId + "/comments", "POST", { body: describePages(result.result) });
|
|
await api("/issues/" + issueId, "PATCH", { status: "done" });
|
|
`;
|
|
}
|
|
|
|
for (const journey of [
|
|
"approve",
|
|
"decline",
|
|
"always",
|
|
"failure",
|
|
"restart",
|
|
] as const) {
|
|
test(`connection review: ${journey}, synchronized task history and actual continuation`, async ({
|
|
page,
|
|
context,
|
|
request,
|
|
}, testInfo) => {
|
|
test.setTimeout(180_000);
|
|
test.skip(
|
|
journey === "restart" && !process.env.PAPERCLIP_REVIEW_RESTART_FILE,
|
|
"Use connection-reviews.config.ts for controlled server restart",
|
|
);
|
|
let suppressReviewEvents = journey === "decline";
|
|
if (journey === "decline") {
|
|
// Reproduce a review arriving between the initial fetch and subscription:
|
|
// return an empty first snapshot and drop its live creation notification.
|
|
const initialSnapshots = new Set<string>();
|
|
await page.route("**/api/issues/*/interactions", async (route) => {
|
|
const url = route.request().url();
|
|
if (route.request().method() === "GET" && !initialSnapshots.has(url)) {
|
|
initialSnapshots.add(url);
|
|
await route.fulfill({ json: [] });
|
|
} else await route.continue();
|
|
});
|
|
await page.routeWebSocket("**/api/companies/*/events/ws", (socket) => {
|
|
const server = socket.connectToServer();
|
|
server.onMessage((message) => {
|
|
if (suppressReviewEvents && String(message).includes("issue.thread_interaction_")) return;
|
|
socket.send(message);
|
|
});
|
|
});
|
|
}
|
|
const provider = await startReviewProvider();
|
|
try {
|
|
const seed = await newCompany(request);
|
|
const agent = await createAgent(request, seed.companyId, "Page reader");
|
|
await page.goto(`/${seed.prefix}/apps`);
|
|
const connector = page
|
|
.getByRole("list", { name: "Connector list" })
|
|
.getByRole("listitem")
|
|
.filter({ hasText: "Connect your own tool" });
|
|
await connector
|
|
.getByRole("button", { name: "Connect", exact: true })
|
|
.click();
|
|
await connector
|
|
.getByRole("button", { name: "Connect your own MCP server" })
|
|
.click();
|
|
await page
|
|
.getByPlaceholder("https://example.com/actions")
|
|
.fill(provider.url);
|
|
await page.getByRole("button", { name: "Continue", exact: true }).click();
|
|
await page.getByRole("button", { name: /Check link/i }).click();
|
|
await expect(
|
|
page.getByRole("heading", { name: /is ready/i }),
|
|
).toBeVisible({ timeout: 30_000 });
|
|
const {
|
|
connections: [connection],
|
|
} = await json<{ connections: Array<{ id: string }> }>(
|
|
await request.get(`/api/companies/${seed.companyId}/tools/connections`),
|
|
);
|
|
await json(
|
|
await request.put(`/api/tool-connections/${connection.id}/installs`, {
|
|
data: { installs: [{ targetType: "agent", targetId: agent.id }] },
|
|
}),
|
|
);
|
|
await page.goto(`/${seed.prefix}/apps/${connection.id}/permissions`);
|
|
await page
|
|
.getByRole("radio", { name: "List fixture pages: Ask first" })
|
|
.click();
|
|
const configureAgent = async (query: string) =>
|
|
json(
|
|
await request.patch(`/api/agents/${agent.id}`, {
|
|
data: {
|
|
adapterConfig: {
|
|
command: process.execPath,
|
|
args: [
|
|
"--input-type=module",
|
|
"-e",
|
|
reviewAgentScript(connection.id, query),
|
|
],
|
|
},
|
|
replaceAdapterConfig: true,
|
|
},
|
|
}),
|
|
);
|
|
await configureAgent(journey === "failure" ? "fail" : "recent");
|
|
const issue = await json<{ id: string; identifier: string }>(
|
|
await request.post(`/api/companies/${seed.companyId}/issues`, {
|
|
data: {
|
|
title: "Read recent pages",
|
|
status: "in_progress",
|
|
assigneeAgentId: agent.id,
|
|
},
|
|
}),
|
|
);
|
|
await page.goto(`/${seed.prefix}/issues/${issue.identifier}`);
|
|
await expect(
|
|
page.getByRole("button", { name: "Approve & run", exact: true }),
|
|
).toBeVisible({ timeout: 45_000 });
|
|
suppressReviewEvents = false;
|
|
const originatingReviews = await json<Array<{ id: string; sourceRunId: string | null }>>(await request.get(`/api/issues/${issue.id}/interactions`));
|
|
const originatingReview = originatingReviews[0];
|
|
const calls = () =>
|
|
provider.captures.filter((c) => c.method === "tools/call").length;
|
|
expect(calls()).toBe(0);
|
|
await page.screenshot({
|
|
path: testInfo.outputPath("pending-review.png"),
|
|
});
|
|
await page
|
|
.getByRole("button", { name: "Dismiss Approve tool action" })
|
|
.click();
|
|
await expect(
|
|
page.getByRole("button", { name: "Approve & run", exact: true }),
|
|
).not.toBeVisible();
|
|
await page.reload();
|
|
await expect(page.getByRole("button", { name: "Review request", exact: true })).toBeVisible({ timeout: 30_000 });
|
|
await expect(
|
|
page.getByRole("button", { name: "Approve & run", exact: true }),
|
|
).not.toBeVisible();
|
|
await json(
|
|
await request.post(`/api/issues/${issue.id}/comments`, {
|
|
data: { body: "Keeping this review pending while I check." },
|
|
}),
|
|
);
|
|
await page
|
|
.getByRole("button", { name: "Review request", exact: true })
|
|
.click();
|
|
await expect(
|
|
page.getByRole("button", { name: "Approve & run", exact: true }),
|
|
).toBeVisible();
|
|
const queue = await context.newPage();
|
|
await queue.goto(`/${seed.prefix}/apps/review`);
|
|
await expect(
|
|
queue.getByRole("button", { name: "Decline", exact: true }),
|
|
).toBeVisible({ timeout: 20_000 });
|
|
if (journey === "restart") {
|
|
const control = process.env.PAPERCLIP_REVIEW_RESTART_FILE!;
|
|
const token = String(Date.now());
|
|
await writeFile(control, `restart:${token}`);
|
|
await expect
|
|
.poll(() => readFile(control, "utf8"), { timeout: 60_000 })
|
|
.toBe(`started:${token}`);
|
|
await expect
|
|
.poll(
|
|
async () => {
|
|
try {
|
|
return (await request.get("/api/health")).ok();
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
{ timeout: 60_000 },
|
|
)
|
|
.toBe(true);
|
|
await page.reload();
|
|
await queue.reload();
|
|
await expect(
|
|
page.getByRole("button", { name: "Approve & run", exact: true }),
|
|
).not.toBeVisible();
|
|
await page.getByRole("button", { name: "Review request", exact: true }).click();
|
|
await expect(
|
|
page.getByRole("button", { name: "Approve & run", exact: true }),
|
|
).toBeVisible();
|
|
expect(calls()).toBe(0);
|
|
}
|
|
if (journey === "decline") {
|
|
// Resolve from Connections and observe the still-open task tab update.
|
|
await queue
|
|
.getByRole("button", { name: "Decline", exact: true })
|
|
.click();
|
|
|
|
} else {
|
|
if (journey === "always") {
|
|
await page.getByRole("button", { name: "Approval options", exact: true }).click();
|
|
await page.getByRole("menuitem", { name: "Always allow", exact: true }).click();
|
|
} else {
|
|
await page.getByRole("button", { name: "Approve & run", exact: true }).click();
|
|
}
|
|
}
|
|
await expect
|
|
.poll(
|
|
async () =>
|
|
(
|
|
await json<Array<{ body: string }>>(
|
|
await request.get(`/api/issues/${issue.id}/comments`),
|
|
)
|
|
)
|
|
.map((c) => c.body)
|
|
.join("\n"),
|
|
{ timeout: 60_000 },
|
|
)
|
|
.toContain(
|
|
journey === "decline"
|
|
? "Review declined"
|
|
: journey === "failure"
|
|
? "Read failed"
|
|
: "Roadmap",
|
|
);
|
|
const replies = await json<Array<{ id: string; body: string; authorAgentId: string | null; createdByRunId: string | null }>>(await request.get(`/api/issues/${issue.id}/comments`));
|
|
const reply = replies.find(comment => comment.authorAgentId === agent.id)!;
|
|
expect(reply.createdByRunId).toBeTruthy();
|
|
expect(originatingReview.sourceRunId).toBeTruthy();
|
|
expect(reply.createdByRunId).not.toBe(originatingReview.sourceRunId);
|
|
expect(reply.body).not.toContain('"content":');
|
|
if (!["decline", "failure"].includes(journey)) {
|
|
await expect(page.getByRole("button", { name: "Show result details" })).toBeVisible();
|
|
await expect(page.locator("pre")).not.toBeVisible();
|
|
await page.getByRole("button", { name: "Show result details" }).click();
|
|
await expect(page.locator("pre")).toContainText('"content":');
|
|
await page.getByRole("button", { name: "Hide result details" }).click();
|
|
}
|
|
expect(calls()).toBe(journey === "decline" ? 0 : 1);
|
|
await expect(
|
|
page.getByRole("button", { name: "Approve & run", exact: true }),
|
|
).not.toBeVisible();
|
|
await expect(
|
|
queue.getByRole("button", { name: "Decline", exact: true }),
|
|
).not.toBeVisible({ timeout: 20_000 });
|
|
await page.bringToFront();
|
|
expect(await page.evaluate(() => document.visibilityState)).toBe("visible");
|
|
await page.reload();
|
|
const resolvedReview = page.locator(`[id=${JSON.stringify(`interaction-${originatingReview.id}`)}]`);
|
|
if (!["decline", "failure"].includes(journey)) {
|
|
await expect(resolvedReview.getByRole("button", { name: "Show result details", exact: true }).filter({ hasText: "Succeeded" })).toBeVisible();
|
|
} else {
|
|
await expect(resolvedReview.getByText(journey === "decline" ? "Declined" : "Execution failed", { exact: false })).toBeVisible();
|
|
}
|
|
await expect(resolvedReview).toContainText("List fixture pages");
|
|
const persistedReply = page.locator(`[id=${JSON.stringify(`comment-${reply.id}`)}]`);
|
|
await expect(persistedReply).toBeVisible();
|
|
await expect(persistedReply).toContainText(
|
|
journey === "decline" ? "Review declined" : journey === "failure" ? "Read failed" : "Roadmap",
|
|
);
|
|
await expect(page.getByTestId("task-chat-history-content")).toBeVisible({
|
|
timeout: 30_000,
|
|
});
|
|
await page.screenshot({
|
|
path: testInfo.outputPath("resolved-review.png"),
|
|
});
|
|
if (journey === "always") {
|
|
await configureAgent("different search options");
|
|
const later = await json<{ id: string }>(
|
|
await request.post(`/api/companies/${seed.companyId}/issues`, {
|
|
data: {
|
|
title: "Read pages with different arguments",
|
|
status: "in_progress",
|
|
assigneeAgentId: agent.id,
|
|
},
|
|
}),
|
|
);
|
|
await expect
|
|
.poll(
|
|
async () =>
|
|
(
|
|
await json<Array<{ body: string }>>(
|
|
await request.get(`/api/issues/${later.id}/comments`),
|
|
)
|
|
)
|
|
.map((c) => c.body)
|
|
.join("\n"),
|
|
{ timeout: 45_000 },
|
|
)
|
|
.toContain("Roadmap");
|
|
expect(calls()).toBe(2);
|
|
expect(
|
|
await json<unknown[]>(
|
|
await request.get(`/api/issues/${later.id}/interactions`),
|
|
),
|
|
).toHaveLength(0);
|
|
}
|
|
await testInfo.attach("journey", {
|
|
body: JSON.stringify(
|
|
{
|
|
source: "local MCP fixture; no live Notion",
|
|
journey,
|
|
...seed,
|
|
issue,
|
|
agent,
|
|
providerCalls: calls(),
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
contentType: "application/json",
|
|
});
|
|
await queue.close();
|
|
} finally {
|
|
await provider.close();
|
|
}
|
|
});
|
|
}
|