mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-09 05:41:56 +02:00
## Thinking Path > - Paperclip manages AI agents, tasks, permissions, and execution budgets. > - Paperclip Runner gives each provider the same admitted task and tool authority. > - OpenAI Dot runs outside the local process tree and needs asynchronous work delivery. > - The merged MCP gateway supplies OAuth consent and signed event delivery. > - A personal assistant grant cannot safely stand in for an assigned agent. > - This pull request adds a separate Dot agent connection and a durable Rust Runner bridge. > - The operator can assign work to Dot and inspect its accepted work, tool receipts, and result. ## Linked Issues or Issue Description **Agent or provider** OpenAI Dot, as an experimental provider of the existing Paperclip Runner adapter. **Why this adapter is useful** An operator can assign normal Paperclip tasks to an existing Dot. Dot can read its mailbox, request work on an assigned task, use admitted task tools, and submit a result. Paperclip keeps company scope, checkout, approvals, known budget limits, and activity attribution. **How the agent is invoked** A dedicated `/mcp/runner` OAuth resource pairs one Dot grant with one agent. A signed MCP mailbox event wakes Dot. Dot explicitly accepts the assignment. The Rust Runner owns the durable turn and operation receipts. The first release supports self-hosted instances with a local Runner controller. **Additional context** This extends the merged public MCP gateway from #14846 and the assistant invitation and device-consent work from #14933. This also integrates the merged assistant tool and configuration expansion in #15380. Dot retains its dedicated agent resource and cannot receive personal configuration permission. The public assistant connection remains a personal connection. ## What Changed - Add a durable Rust Dot provider and its TypeScript Runner driver. - Add closed PRP v3 external-provider operations and native execution input v6. - Add company-scoped pairing, mailbox, assignment, and operation records. - Reuse merged browser/device consent, client metadata verification, webhook admissions, refresh, secret rotation, and warm-standby gates. - Keep Dot scopes, issuer, grants, event workers, and tool access separate from personal assistant access. - Add Dot configuration, pairing, readiness, and consent UI. Keep agent grants out of the personal Connections entry. - Regenerate the Dot-only migration after master. Preserve published gateway migrations. Make the new migration safe to reapply. - Document setup, recovery, accounting limits, evidence, and remaining account qualification. - Reverify reconnect callbacks and wake outstanding work with a fresh mailbox reference; preserve the existing assignment and operation receipts. - Clean up Dot bindings and waiting runs on OAuth revoke and refresh-token replay. Old grants cannot revoke replacement bindings. - Restore the pairing reference when an unsaved agent form is reopened; document board-only pairing routes in OpenAPI. - Accept a clean Rust exit after the acknowledged shutdown receipt. Unexpected exits still require recovery. - Clear the cached binding after a successful revoke so a failed connection refresh cannot restore it. - Add production-component Storybook states and screenshots for pairing and connection review. All preview account data is synthetic. - Persist normalized completion, serialize Dot turns and durable work admission, and poll subscription readiness. - Serialize mailbox writes and cursor reads; retain paused fence acknowledgement without task authority. - Authorize admitted review runs without changing the worker assignee. Include the fenced assignment ID in production stop notices. ## Verification - This PR integrates master `4a8178e9c`. Dot migration `0317_messy_famine.sql` follows the published history and is safe to reapply. The merge preserves the reserved migration connection, batch-commit handling, private task checks, task monitors, and native accounting. - Local workspace typecheck, full build, and UI token gates pass. The server typecheck passes after the review fixes. Database and native executor regressions pass. - All twelve real Rust/PostgreSQL Dot integration tests and twelve Dot driver tests pass. The tests cover native document writing and finalization, durable replay, queue admission, mailbox ordering, admitted reviews, stale authority, production stop references, and paused acknowledgements. - Current head `d0e7e0626` passes all 57 checks: 53 pass and four are intentionally skipped. This includes full typecheck, build, tests, Rust Runner verification, browser E2E, release verification, and Canary Dry Run. Greptile rates this exact head 5/5. All review threads are resolved. - The full local root test run is slower than the sharded CI run and has not completed. The full CI test gates pass on the current commit. Focused local regressions pass. - Real-account pairing and event delivery on this base commit remain unqualified. Live account and setup proof are recorded in the follow-up #15414. The following screenshots use synthetic preview data. They show the production pairing component and do not qualify a real account or the full agent setup journey.   ## Risks - This base adapter uses `PAPERCLIP_ENABLE_OPENAI_DOT=1` plus Public MCP and Paperclip Runner. The separate experimental-settings follow-up in #15414 replaces this environment flag with saved operator settings. - Dot does not expose provider token usage or cost. The operator must acknowledge external billing. Known Paperclip budget gates still apply. - Cancellation fences Paperclip authority. It does not confirm that Dot stopped all external activity. - Assigned skill files and third-party MCP bindings are unsupported and reject admission. There is no mounted workspace, model selector, or provider thread identifier. - Hosted agent-broker and remote controller deployments are not qualified. - The new migration follows the merged master history. Existing prototype databases still need the normal master migration history before this Dot-only migration. ## Model Used OpenAI Codex, based on GPT-6. The exact deployment ID and context window size are not exposed in this session. Capabilities used: reasoning, repository editing, code execution, and test inspection. ## 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 #123` 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 references) - [x] My branch name describes the change 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>
155 lines
12 KiB
TypeScript
155 lines
12 KiB
TypeScript
/** Disposable real-Dot transport lab. Not production scheduling or admission.
|
|
* Only the MCP listener may be tunneled. Control stays on a separate loopback
|
|
* port with a random bearer credential stored in a mode-0600 local file.
|
|
* No existing Paperclip database, user, API key, or company is loaded. */
|
|
import assert from "node:assert/strict";
|
|
import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
import { once } from "node:events";
|
|
import { createServer, type Server } from "node:http";
|
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import express from "express";
|
|
import { z } from "zod";
|
|
import { activityLog, authUsers, companies, companyMemberships, createDb, issues, mcpEventSubscriptions, mcpEventDeliveries, startEmbeddedPostgresTestDatabase } from "@paperclipai/db";
|
|
import { DotHarnessDriver } from "../../packages/paperclip-runner/src/drivers/dot/dot-harness-driver.js";
|
|
import { HarnessDriverBackend } from "../../packages/paperclip-runner/src/backends/harness-driver-backend.js";
|
|
import { publicMcpIngressRoutes } from "../src/routes/public-mcp.js";
|
|
import { createPublicMcpOAuth } from "../src/services/public-mcp/oauth.js";
|
|
import { createPublicMcpEvents } from "../src/services/public-mcp/events.js";
|
|
import { createPublicMcpExecutor, McpApiError, type ApiDispatch } from "../src/services/public-mcp/capabilities.js";
|
|
import { createDotRunnerMcpBridge } from "../src/services/public-mcp/dot-runner.js";
|
|
import { instanceSettingsService } from "../src/services/instance-settings.js";
|
|
|
|
const origin = new URL(process.env.DOT_LAB_ORIGIN ?? "").origin;
|
|
assert(origin.startsWith("https://"), "DOT_LAB_ORIGIN must be the public HTTPS tunnel origin");
|
|
const port = z.coerce.number().int().min(1024).max(65534).parse(process.env.DOT_LAB_PORT ?? 43127);
|
|
const requestIdSchema = z.string().regex(/^pcmcp_request_[A-Za-z0-9_-]{43}$/);
|
|
const scratch = await mkdtemp(join(tmpdir(), "paperclip-dot-live-"));
|
|
process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE = join(scratch, "master.key");
|
|
delete process.env.PAPERCLIP_SECRETS_MASTER_KEY;
|
|
delete process.env.PAPERCLIP_CLOUD_API_ORIGIN;
|
|
const controlToken = randomBytes(32).toString("base64url");
|
|
const app = express(); app.use(express.json({ limit: "256kb" }));
|
|
const control = express(); control.use(express.json({ limit: "16kb" }));
|
|
const http = createServer(app), admin = createServer(control);
|
|
let database: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | undefined;
|
|
let service: ReturnType<typeof createPublicMcpEvents> | undefined;
|
|
let unregister: (() => void) | undefined;
|
|
let active = true;
|
|
async function listen(server: Server, listenPort: number) { server.listen(listenPort, "127.0.0.1"); await once(server, "listening"); }
|
|
async function close(server: Server) { server.closeAllConnections(); if (server.listening) await new Promise<void>(resolve => server.close(() => resolve())); }
|
|
const escape = (value: string) => value.replace(/[&<>"']/g, c => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]!);
|
|
try {
|
|
database = await startEmbeddedPostgresTestDatabase("paperclip-dot-live-");
|
|
const db = createDb(database.connectionString);
|
|
const oauth = createPublicMcpOAuth(db, { origin, resource: origin + "/mcp/paperclip" });
|
|
await instanceSettingsService(db).updateExperimental({ enablePublicMcp: true });
|
|
const userId = randomUUID();
|
|
await db.insert(authUsers).values({ id: userId, name: "Dot lab operator", email: `${userId}@example.test`, createdAt: new Date(), updatedAt: new Date() });
|
|
const [company] = await db.insert(companies).values({ name: "Disposable Dot integration lab", issuePrefix: "DOT" }).returning(); assert(company);
|
|
await db.insert(companyMemberships).values({ companyId: company.id, principalType: "user", principalId: userId, membershipRole: "owner", status: "active" });
|
|
const [task] = await db.insert(issues).values({ companyId: company.id, title: "Dot live test inbox", status: "todo" }).returning(); assert(task);
|
|
const actor = { type: "board" as const, source: "session" as const, userId };
|
|
const pending = new Map<string, Awaited<ReturnType<typeof oauth.describeRequest>>>();
|
|
const redirects = new Map<string, string>();
|
|
const records: Array<Record<string, unknown>> = [];
|
|
const reports: string[] = [];
|
|
const transcript: unknown[] = [];
|
|
const record = (kind: string, fields: Record<string, unknown> = {}) => {
|
|
const entry = { at: new Date().toISOString(), kind, ...fields }; records.push(entry);
|
|
if (records.length > 300) records.shift();
|
|
console.log(JSON.stringify(entry));
|
|
};
|
|
let native: Awaited<ReturnType<HarnessDriverBackend["openSession"]>> | undefined;
|
|
let grantId: string | undefined;
|
|
let queued = false;
|
|
let stopLab!: () => void;
|
|
const stopped = new Promise<void>(resolve => { stopLab = resolve; });
|
|
const api: ApiDispatch = async (principal, method, path) => {
|
|
if (principal.grant.companyId !== company.id || method !== "GET" || path !== `/issues/${task.id}`) throw new McpApiError(403);
|
|
return task;
|
|
};
|
|
const bridge = createDotRunnerMcpBridge();
|
|
// Real remote callback transport: retain production HTTPS, DNS/IP pinning,
|
|
// verification challenge, encryption, signatures and retry policy unchanged.
|
|
service = createPublicMcpEvents(db, oauth, api, { enableDotPrototype: true });
|
|
app.use((req, res, next) => {
|
|
res.setHeader("Cache-Control", "no-store"); res.setHeader("Referrer-Policy", "no-referrer");
|
|
if (req.path === "/mcp/paperclip" && req.method === "POST") {
|
|
const method = String(req.body?.method ?? "").slice(0, 100);
|
|
const tool = method === "tools/call" ? String(req.body?.params?.name ?? "").slice(0, 100) : undefined;
|
|
res.on("finish", () => record("mcp", { method, ...(tool ? { tool } : {}), status: res.statusCode }));
|
|
}
|
|
next();
|
|
});
|
|
app.get("/", (_req, res) => res.type("text").send("Disposable Paperclip Dot lab. Authenticated MCP endpoint: /mcp/paperclip. No production data."));
|
|
app.get("/mcp-connect/:id", async (req, res) => {
|
|
const id = requestIdSchema.parse(req.params.id);
|
|
const redirect = redirects.get(id);
|
|
if (redirect) { res.redirect(303, redirect); return; }
|
|
const request = await oauth.describeRequest(id, actor, null);
|
|
if (pending.size >= 20 && !pending.has(id)) { res.sendStatus(429); return; }
|
|
pending.set(id, request);
|
|
res.setHeader("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'");
|
|
res.type("html").send(`<!doctype html><meta charset="utf-8"><meta http-equiv="refresh" content="3"><title>Paperclip Dot lab connection</title><h1>Connect the disposable Dot lab</h1><p>Waiting for the local lab operator to approve this request.</p><p>Client: ${escape(request.clientName)}. Return destination: ${escape(request.redirectOrigin)}.</p><p>This lab contains one test task and no existing Paperclip data. It expires when the lab closes.</p><p>Request: ${escape(id)}</p>`);
|
|
});
|
|
app.use(publicMcpIngressRoutes(oauth, createPublicMcpExecutor(db, oauth, api), service, bridge));
|
|
const errors: express.ErrorRequestHandler = (_error, _req, res, _next) => { res.status(400).json({ error: "Lab request unavailable" }); };
|
|
app.use(errors);
|
|
control.use((req, res, next) => {
|
|
const supplied = Buffer.from(req.headers.authorization ?? "");
|
|
const expected = Buffer.from(`Bearer ${controlToken}`);
|
|
if (supplied.length !== expected.length || !timingSafeEqual(supplied, expected)) { res.sendStatus(401); return; }
|
|
next();
|
|
});
|
|
control.get("/status", async (_req, res) => res.json({ companyId: company.id, taskId: task.id, connected: !!native, queued,
|
|
pending: [...pending.values()], reports, transcript, records,
|
|
subscriptions: await db.select({ id: mcpEventSubscriptions.id, name: mcpEventSubscriptions.name, stoppedAt: mcpEventSubscriptions.stoppedAt }).from(mcpEventSubscriptions),
|
|
deliveries: await db.select({ id: mcpEventDeliveries.id, outcome: mcpEventDeliveries.outcome, attempts: mcpEventDeliveries.attempts }).from(mcpEventDeliveries),
|
|
}));
|
|
// Explicit local operator approval, never a public auto-consent route.
|
|
control.post("/approve", async (req, res) => {
|
|
assert(!grantId, "This lab permits only one connection");
|
|
const requestId = requestIdSchema.parse(req.body.requestId);
|
|
assert(pending.has(requestId), "Visit the public consent page first");
|
|
const consent = await oauth.consent(requestId, actor, { decision: "approve", companyId: company.id, allowWrites: true });
|
|
const grants = await oauth.listConnections(userId); assert.equal(grants.length, 1);
|
|
grantId = grants[0]!.id;
|
|
const identity = { companyId: company.id, agentId: randomUUID(), issueId: task.id, runId: randomUUID(), sessionId: randomUUID() };
|
|
const driver = new DotHarnessDriver({ identity, principal: { companyId: company.id, grantId }, expiresAt: Date.now() + 2 * 3600_000,
|
|
assertAuthority: async () => { assert(active); await oauth.authorizeGrant(grantId!); },
|
|
tools: [{ name: "save_report", description: "Save a short harmless report in this disposable lab only", inputSchema: { type: "object", properties: { body: { type: "string", maxLength: 2000 } }, required: ["body"], additionalProperties: false } }],
|
|
executeTool: async ({ name, arguments: args }) => { assert.equal(name, "save_report"); const { body } = z.object({ body: z.string().min(1).max(2000) }).strict().parse(args); reports.push(body); record("report_saved", { body }); return { saved: true, reportNumber: reports.length }; },
|
|
publish: async assignment => { await db.insert(activityLog).values({ companyId: company.id, actorType: "agent", actorId: identity.agentId, action: "dot.work_available", entityType: "issue", entityId: task.id, details: { ...assignment } }); record("assignment_published", { ...assignment }); },
|
|
});
|
|
unregister = bridge.register(driver, grantId);
|
|
native = await new HarnessDriverBackend(driver).openSession({ identity });
|
|
void (async () => { for await (const event of native!.events()) { transcript.push(event); record("runner_event", { event }); } })().catch(() => record("runner_stream_error"));
|
|
redirects.set(requestId, consent.redirectUrl); pending.delete(requestId);
|
|
record("connection_approved"); res.json({ connected: true, companyId: company.id, taskId: task.id });
|
|
});
|
|
control.post("/queue", async (_req, res) => {
|
|
assert(native && !queued, "Connect first; this lab permits one task");
|
|
const subscriptions = await db.select().from(mcpEventSubscriptions);
|
|
assert(subscriptions.some(s => s.grantId === grantId && s.name === "paperclip.dot.work_available" && !s.stoppedAt && s.expiresAt > new Date()), "Dot must establish its event subscription first");
|
|
queued = true;
|
|
await native.startTurn({ message: { role: "user", text: "Live integration test: compute 17 + 25. Call the projected save_report tool with body 'Dot received the Paperclip event. 17 + 25 = 42.' Then finish with schema paperclip.run_result.v1, reportedWorkDisposition 'done', summary stating the saved result, completionClaim {contractRevision:'dot-live-v1',objectiveSatisfied:true,criteria:[],remainingWork:[]}, and empty arrays for evidence, verification, attentionRequests, artifacts. Do not use any other apps, private data, external messages, or additional tasks." } });
|
|
await service!.tick(); res.json({ queued: true });
|
|
});
|
|
control.post("/stop", (_req, res) => { res.json({ stopping: true }); stopLab(); });
|
|
control.use(errors);
|
|
await listen(http, port); await listen(admin, port + 1); service.start();
|
|
await writeFile(join(scratch, "control.json"), JSON.stringify({ origin, controlOrigin: `http://127.0.0.1:${port + 1}`, token: controlToken, companyId: company.id, taskId: task.id }), { mode: 0o600 });
|
|
console.log(`LIVE LAB READY ${origin}/mcp/paperclip`);
|
|
console.log(`Local control file: ${join(scratch, "control.json")}`);
|
|
console.log(`Standing instruction: Subscribe to paperclip.dot.work_available for companyId ${company.id}, taskId ${task.id}. When an event arrives, check paperclip_dot_inbox; read, accept, execute and finish its assignment. Do not poll. Wait for the event before taking work.`);
|
|
process.once("SIGINT", stopLab); process.once("SIGTERM", stopLab);
|
|
const timer = setTimeout(stopLab, 2 * 3600_000); timer.unref();
|
|
await stopped; clearTimeout(timer);
|
|
if (grantId) await oauth.revokeConnection(grantId, userId);
|
|
} finally {
|
|
active = false; unregister?.(); await service?.stop(); await close(http); await close(admin);
|
|
await database?.cleanup(); await rm(scratch, { recursive: true, force: true });
|
|
}
|