mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-25 09:41:04 +01:00
## Thinking Path > - Paperclip coordinates AI agents and their runs. > - Environment plugins own remote capacity. > - A plugin needs a typed API to start and stop its installed Runner. > - Core must derive authority from the saved lease. > - A lost reply or controller restart must not lose cleanup authority. > - This PR adds the worker contract and the host task service. > - Native execution and Runner changes are separate PRs. ## Linked Issues or Issue Description **Subsystem affected** Plugin SDK and environment lease services. **Current behavior** Environment plugins have no typed Runner task hook. Core cannot dispatch these tasks or recover their credential handoff through a saved lease. **Proposed behavior** Add validated task operations, worker negotiation, company and project checks, retained-session authorization, and durable cleanup. Keep credentials out of lease metadata. **Additional context** This PR is the first part of a smaller series. Runner installed-home support is in #16977. Native execution and setup are in #16978. Both are stacked drafts. This PR does not enable native plugin execution, change default environments, add a route, or add a database migration. ## What Changed - Add `supportsTasks` and `onEnvironmentTask` with seven typed operations. - Validate worker requests and responses. Reject shell commands and file transfer operations. - Pin dispatch and cleanup to the saved company, lease, plugin, and driver. - Authorize projects and retained sessions. Validate the current run separately from the retained PRP identity. - Save dispatch before the provider call. Fence admission during cleanup. Require observed process termination. - Save account writeback authority on the lease. Recover Codex refreshes through the existing account and secret services. - Document the task API. Keep the Runner, heartbeat, setup, workspace, and provider changes in later PRs. ## Verification - Full `pnpm -r typecheck` and `pnpm build` passed on the separated API scope. - 201 focused tests passed: SDK worker dispatch, task validation, PostgreSQL task lineage, credential recovery, and existing environment cleanup. - The lineage test covers a third run, repeated same-run replacements, a stale submitting run, and exact termination receipts. - Initial commands hit sandbox socket/IPC restrictions. The permitted runs executed PostgreSQL and passed. A typecheck attempt lacked Cargo in PATH; the run with the installed toolchain passed. - The earlier combined implementation had a full local run with 22,286 passing tests, 120 skips, and eight failed assertions. Task fixture failures were fixed before this split. Remaining host Git and temporary-path assertions are recorded in the previous PR revision. This split does not claim a fresh full local suite pass. - The standard Greptile review scored this API head 5/5 with no unresolved threads. CI is green. The single Apex review also scored this head 5/5 with no actionable findings. - Fresh API CI lost a self-hosted runner during server shard 9. GitHub reported lost communication, with no failed test step. The affected jobs passed on retry without code changes. - CI on the prior combined head failed an unchanged workspace dev-server readiness case. No unrelated test fix is included here. Fresh CI is required for this smaller head. - This scope does not run a provider or a Cloud test. The user owns deployment and the intermediate Cloud test. ## Risks - The API is opt-in. Native plugin execution requires the later integrations. - Providers must make task submission idempotent and prove descendant termination. - A failed status read or credential export keeps the lease available for cleanup. - Codex refresh writeback requires the same active grant, secret reference, and session epoch. Revocation or account replacement discards the old refresh. ## Model Used OpenAI GPT-6 through Codex. Used source inspection, code execution, tests, and GitHub tools. The exact served model ID and context-window size are not exposed in this session. ## 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 local checks and recorded failures and their results above - [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>
460 lines
17 KiB
TypeScript
460 lines
17 KiB
TypeScript
import { PassThrough } from "node:stream";
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
PLUGIN_RPC_ERROR_CODES,
|
|
createRequest,
|
|
isJsonRpcErrorResponse,
|
|
isJsonRpcSuccessResponse,
|
|
parseMessage,
|
|
serializeMessage,
|
|
} from "../../../packages/plugins/sdk/src/protocol.js";
|
|
import type { PluginEnvironmentTaskParams } from "../../../packages/plugins/sdk/src/environment-tasks.js";
|
|
import { definePlugin } from "../../../packages/plugins/sdk/src/define-plugin.js";
|
|
import { startWorkerRpcHost } from "../../../packages/plugins/sdk/src/worker-rpc-host.js";
|
|
import { pluginManifestV1Schema, type PaperclipPluginManifestV1 } from "@paperclipai/shared";
|
|
import { pluginCapabilityValidator } from "../services/plugin-capability-validator.js";
|
|
|
|
const baseManifest: PaperclipPluginManifestV1 = {
|
|
id: "test.environment-driver",
|
|
apiVersion: 1,
|
|
version: "1.0.0",
|
|
displayName: "Environment Driver",
|
|
description: "Test environment driver plugin",
|
|
author: "Paperclip",
|
|
categories: ["automation"],
|
|
capabilities: ["environment.drivers.register"],
|
|
entrypoints: { worker: "dist/worker.js" },
|
|
environmentDrivers: [
|
|
{
|
|
driverKey: "fake-plugin",
|
|
displayName: "Fake plugin",
|
|
supportsInteractiveSetup: true,
|
|
interactiveSetupConnectionTypes: ["ssh"],
|
|
supportsTemplateCapture: true,
|
|
templateRefKind: "snapshot",
|
|
supportsTemplateDelete: true,
|
|
configSchema: {
|
|
type: "object",
|
|
properties: {
|
|
template: { type: "string" },
|
|
},
|
|
required: ["template"],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
describe("plugin environment driver seam", () => {
|
|
it("validates environment driver manifest declarations", () => {
|
|
expect(pluginManifestV1Schema.safeParse(baseManifest).success).toBe(true);
|
|
|
|
const missingCapability = pluginManifestV1Schema.safeParse({
|
|
...baseManifest,
|
|
capabilities: ["http.outbound"],
|
|
});
|
|
expect(missingCapability.success).toBe(false);
|
|
expect(JSON.stringify(missingCapability.error?.issues)).toContain(
|
|
"environment.drivers.register",
|
|
);
|
|
|
|
const duplicateDriver = pluginManifestV1Schema.safeParse({
|
|
...baseManifest,
|
|
environmentDrivers: [
|
|
baseManifest.environmentDrivers![0],
|
|
{ ...baseManifest.environmentDrivers![0], displayName: "Duplicate" },
|
|
],
|
|
});
|
|
expect(duplicateDriver.success).toBe(false);
|
|
expect(JSON.stringify(duplicateDriver.error?.issues)).toContain(
|
|
"Duplicate environment driver keys",
|
|
);
|
|
});
|
|
|
|
it("enforces environment driver capability requirements", () => {
|
|
const validator = pluginCapabilityValidator();
|
|
expect(validator.getRequiredCapabilities("environment.acquireLease")).toEqual([
|
|
"environment.drivers.register",
|
|
]);
|
|
expect(validator.getRequiredCapabilities("environment.captureTemplate")).toEqual([
|
|
"environment.drivers.register",
|
|
]);
|
|
expect(validator.checkOperation(baseManifest, "environment.execute").allowed).toBe(true);
|
|
|
|
const withoutCapability = {
|
|
...baseManifest,
|
|
capabilities: ["http.outbound"],
|
|
} satisfies PaperclipPluginManifestV1;
|
|
|
|
expect(validator.checkOperation(withoutCapability, "environment.execute")).toMatchObject({
|
|
allowed: false,
|
|
missing: ["environment.drivers.register"],
|
|
});
|
|
expect(validator.validateManifestCapabilities(withoutCapability)).toMatchObject({
|
|
allowed: false,
|
|
missing: ["environment.drivers.register"],
|
|
});
|
|
});
|
|
|
|
it("negotiates and validates plugin-provided Runner execution RPCs", async () => {
|
|
const calls: unknown[] = [];
|
|
const plugin = definePlugin({
|
|
async setup() {},
|
|
async onEnvironmentTask(input) {
|
|
calls.push(input);
|
|
return { kind: "accepted", taskId: input.taskId };
|
|
},
|
|
});
|
|
const stdin = new PassThrough();
|
|
const stdout = new PassThrough();
|
|
const host = startWorkerRpcHost({ plugin, stdin, stdout });
|
|
const responses: unknown[] = [];
|
|
stdout.on("data", chunk => {
|
|
for (const line of String(chunk).split("\n").filter(Boolean)) responses.push(parseMessage(line));
|
|
});
|
|
try {
|
|
const manifest = { ...baseManifest, environmentDrivers: [{ ...baseManifest.environmentDrivers![0], supportsTasks: true }] };
|
|
expect(pluginManifestV1Schema.parse(manifest).environmentDrivers![0].supportsTasks).toBe(true);
|
|
stdin.write(serializeMessage(createRequest("initialize", {
|
|
manifest, config: {}, instanceInfo: { instanceId: "instance-1", hostVersion: "1.0.0" }, apiVersion: 1,
|
|
}, 1)));
|
|
await waitForResponses(responses, 1);
|
|
const initialized = responses[0];
|
|
expect(isJsonRpcSuccessResponse(initialized)).toBe(true);
|
|
if (!isJsonRpcSuccessResponse(initialized)) return;
|
|
expect(initialized.result.supportedMethods).toContain("environmentTask");
|
|
const projectIds = ["10000000-0000-4000-8000-000000000001", "10000000-0000-4000-8000-000000000002"];
|
|
const params: PluginEnvironmentTaskParams = { driverKey: "fake-plugin", companyId: "company", environmentId: "environment", config: {},
|
|
taskId: "attempt", runId: "run", agentId: "agent", projectIds, lease: { providerLeaseId: "attempt" },
|
|
operation: { kind: "submit", projectIds, bootstrapTicket: "transient-test-ticket", runner: {
|
|
protocolMin: 1, protocolMax: 2, harness: "codex", runnerId: "runner", leaseId: "lease", runId: "run", sessionId: "session:part.1", turnId: "turn", itemId: "item",
|
|
} } };
|
|
|
|
stdin.write(serializeMessage(createRequest("environmentTask", params, 2)));
|
|
await waitForResponses(responses, 2);
|
|
expect(responses[1]).toMatchObject({ result: { kind: "accepted", taskId: "attempt" } });
|
|
expect(calls[0]).toMatchObject({ projectIds, operation: { kind: "submit", projectIds } });
|
|
stdin.write(serializeMessage(createRequest("environmentTask", { ...params, operation: { kind: "status" } }, 3)));
|
|
await waitForResponses(responses, 3);
|
|
expect(isJsonRpcErrorResponse(responses[2])).toBe(true); // Wrong response kind.
|
|
stdin.write(serializeMessage(createRequest("environmentTask", { ...params, operation: { kind: "shell", command: "echo test" } }, 4)));
|
|
await waitForResponses(responses, 4);
|
|
expect(isJsonRpcErrorResponse(responses[3])).toBe(true);
|
|
expect(calls).toHaveLength(2); // Invalid operation did not reach the plugin.
|
|
} finally { host.stop(); }
|
|
});
|
|
|
|
it("dispatches environment driver worker hooks and reports support", async () => {
|
|
const plugin = definePlugin({
|
|
async setup() {},
|
|
async onEnvironmentProbe(params) {
|
|
return {
|
|
ok: true,
|
|
summary: `probed ${params.driverKey}`,
|
|
metadata: { environmentId: params.environmentId },
|
|
};
|
|
},
|
|
async onEnvironmentStartInteractiveSetup(params) {
|
|
return {
|
|
providerLeaseId: `setup-${params.sessionId}`,
|
|
status: "waiting_for_user",
|
|
connectionSummary: {
|
|
type: "ssh",
|
|
username: "sandbox",
|
|
hostRedacted: true,
|
|
portRedacted: true,
|
|
},
|
|
connectionPayload: null,
|
|
metadata: { environmentId: params.environmentId },
|
|
};
|
|
},
|
|
});
|
|
|
|
const stdin = new PassThrough();
|
|
const stdout = new PassThrough();
|
|
const host = startWorkerRpcHost({ plugin, stdin, stdout });
|
|
const responses: unknown[] = [];
|
|
stdout.on("data", (chunk) => {
|
|
const lines = String(chunk).split("\n").filter(Boolean);
|
|
for (const line of lines) {
|
|
responses.push(parseMessage(line));
|
|
}
|
|
});
|
|
|
|
stdin.write(serializeMessage(createRequest("initialize", {
|
|
manifest: baseManifest,
|
|
config: {},
|
|
instanceInfo: { instanceId: "instance-1", hostVersion: "1.0.0" },
|
|
apiVersion: 1,
|
|
}, 1)));
|
|
await waitForResponses(responses, 1);
|
|
|
|
const initializeResponse = responses[0];
|
|
expect(isJsonRpcSuccessResponse(initializeResponse)).toBe(true);
|
|
if (!isJsonRpcSuccessResponse(initializeResponse)) return;
|
|
expect(initializeResponse.result.supportedMethods).toContain("environmentProbe");
|
|
expect(initializeResponse.result.supportedMethods).toContain("environmentStartInteractiveSetup");
|
|
|
|
stdin.write(serializeMessage(createRequest("environmentProbe", {
|
|
driverKey: "fake-plugin",
|
|
companyId: "company-1",
|
|
environmentId: "environment-1",
|
|
config: { template: "base" },
|
|
}, 2)));
|
|
await waitForResponses(responses, 2);
|
|
|
|
const probeResponse = responses[1];
|
|
expect(isJsonRpcSuccessResponse(probeResponse)).toBe(true);
|
|
if (!isJsonRpcSuccessResponse(probeResponse)) return;
|
|
expect(probeResponse.result).toMatchObject({
|
|
ok: true,
|
|
summary: "probed fake-plugin",
|
|
metadata: { environmentId: "environment-1" },
|
|
});
|
|
|
|
stdin.write(serializeMessage(createRequest("environmentStartInteractiveSetup", {
|
|
driverKey: "fake-plugin",
|
|
companyId: "company-1",
|
|
environmentId: "environment-1",
|
|
sessionId: "session-1",
|
|
config: { template: "base" },
|
|
}, 3)));
|
|
await waitForResponses(responses, 3);
|
|
|
|
const setupResponse = responses[2];
|
|
expect(isJsonRpcSuccessResponse(setupResponse)).toBe(true);
|
|
if (!isJsonRpcSuccessResponse(setupResponse)) return;
|
|
expect(setupResponse.result).toMatchObject({
|
|
providerLeaseId: "setup-session-1",
|
|
status: "waiting_for_user",
|
|
connectionSummary: {
|
|
type: "ssh",
|
|
hostRedacted: true,
|
|
portRedacted: true,
|
|
},
|
|
});
|
|
|
|
stdin.write(serializeMessage(createRequest("environmentExecute", {
|
|
driverKey: "fake-plugin",
|
|
companyId: "company-1",
|
|
environmentId: "environment-1",
|
|
config: { template: "base" },
|
|
lease: { providerLeaseId: "lease-1" },
|
|
command: "echo",
|
|
}, 4)));
|
|
await waitForResponses(responses, 4);
|
|
|
|
const executeResponse = responses[3];
|
|
expect(isJsonRpcErrorResponse(executeResponse)).toBe(true);
|
|
if (!isJsonRpcErrorResponse(executeResponse)) return;
|
|
expect(executeResponse.error.code).toBe(PLUGIN_RPC_ERROR_CODES.METHOD_NOT_IMPLEMENTED);
|
|
expect(executeResponse.error.message).toContain("environmentExecute");
|
|
|
|
host.stop();
|
|
});
|
|
});
|
|
|
|
const objectReferenceManifest: PaperclipPluginManifestV1 = {
|
|
id: "test.external-object-provider",
|
|
apiVersion: 1,
|
|
version: "1.0.0",
|
|
displayName: "External Object Provider",
|
|
description: "Test external object provider plugin",
|
|
author: "Paperclip",
|
|
categories: ["connector"],
|
|
capabilities: ["external.objects.detect", "external.objects.read"],
|
|
entrypoints: { worker: "dist/worker.js" },
|
|
objectReferences: [
|
|
{
|
|
providerKey: "mocktracker",
|
|
displayName: "Mock Tracker",
|
|
objectTypes: ["ticket"],
|
|
urlPatterns: ["https://mock.example/tickets/:id"],
|
|
refreshPolicy: { defaultTtlSeconds: 300, staleAfterSeconds: 1800 },
|
|
},
|
|
],
|
|
};
|
|
|
|
describe("plugin external object provider seam", () => {
|
|
it("validates provider manifest declarations and capabilities", () => {
|
|
expect(pluginManifestV1Schema.safeParse(objectReferenceManifest).success).toBe(true);
|
|
|
|
const missingCapability = pluginManifestV1Schema.safeParse({
|
|
...objectReferenceManifest,
|
|
capabilities: ["external.objects.detect"],
|
|
});
|
|
expect(missingCapability.success).toBe(false);
|
|
expect(JSON.stringify(missingCapability.error?.issues)).toContain("external.objects.read");
|
|
|
|
const duplicateProvider = pluginManifestV1Schema.safeParse({
|
|
...objectReferenceManifest,
|
|
objectReferences: [
|
|
objectReferenceManifest.objectReferences![0],
|
|
{ ...objectReferenceManifest.objectReferences![0], displayName: "Duplicate" },
|
|
],
|
|
});
|
|
expect(duplicateProvider.success).toBe(false);
|
|
expect(JSON.stringify(duplicateProvider.error?.issues)).toContain(
|
|
"Duplicate object reference provider keys",
|
|
);
|
|
});
|
|
|
|
it("enforces provider capability requirements", () => {
|
|
const validator = pluginCapabilityValidator();
|
|
expect(validator.getRequiredCapabilities("external.objects.detect")).toEqual([
|
|
"external.objects.detect",
|
|
]);
|
|
expect(validator.getRequiredCapabilities("external.objects.read")).toEqual([
|
|
"external.objects.read",
|
|
]);
|
|
expect(validator.checkOperation(objectReferenceManifest, "external.objects.read").allowed).toBe(true);
|
|
|
|
const withoutCapability = {
|
|
...objectReferenceManifest,
|
|
capabilities: ["external.objects.detect"],
|
|
} satisfies PaperclipPluginManifestV1;
|
|
|
|
expect(validator.checkOperation(withoutCapability, "external.objects.read")).toMatchObject({
|
|
allowed: false,
|
|
missing: ["external.objects.read"],
|
|
});
|
|
expect(validator.validateManifestCapabilities(withoutCapability)).toMatchObject({
|
|
allowed: false,
|
|
missing: ["external.objects.read"],
|
|
});
|
|
});
|
|
|
|
it("dispatches provider detection and resolution worker hooks", async () => {
|
|
const plugin = definePlugin({
|
|
async setup() {},
|
|
async onDetectExternalObjects(params) {
|
|
return {
|
|
detections: params.urls.map((url) => ({
|
|
urlIdentityHash: url.canonicalIdentityHash,
|
|
providerKey: "mocktracker",
|
|
objectType: "ticket",
|
|
externalId: "MOCK-123",
|
|
displayTitle: "Mock ticket",
|
|
confidence: "exact",
|
|
})),
|
|
};
|
|
},
|
|
async onResolveExternalObject(params) {
|
|
return {
|
|
ok: true,
|
|
snapshot: {
|
|
displayTitle: `Resolved ${params.externalId}`,
|
|
statusKey: "ready",
|
|
statusLabel: "Ready",
|
|
statusCategory: "succeeded",
|
|
statusTone: "success",
|
|
isTerminal: true,
|
|
ttlSeconds: 600,
|
|
},
|
|
};
|
|
},
|
|
});
|
|
|
|
const stdin = new PassThrough();
|
|
const stdout = new PassThrough();
|
|
const host = startWorkerRpcHost({ plugin, stdin, stdout });
|
|
const responses: unknown[] = [];
|
|
stdout.on("data", (chunk) => {
|
|
const lines = String(chunk).split("\n").filter(Boolean);
|
|
for (const line of lines) {
|
|
responses.push(parseMessage(line));
|
|
}
|
|
});
|
|
|
|
stdin.write(serializeMessage(createRequest("initialize", {
|
|
manifest: objectReferenceManifest,
|
|
config: {},
|
|
instanceInfo: { instanceId: "instance-1", hostVersion: "1.0.0" },
|
|
apiVersion: 1,
|
|
}, 1)));
|
|
await waitForResponses(responses, 1);
|
|
|
|
const initializeResponse = responses[0];
|
|
expect(isJsonRpcSuccessResponse(initializeResponse)).toBe(true);
|
|
if (!isJsonRpcSuccessResponse(initializeResponse)) return;
|
|
expect(initializeResponse.result.supportedMethods).toContain("detectExternalObjects");
|
|
expect(initializeResponse.result.supportedMethods).toContain("resolveExternalObject");
|
|
|
|
stdin.write(serializeMessage(createRequest("detectExternalObjects", {
|
|
companyId: "company-1",
|
|
urls: [{
|
|
sanitizedCanonicalUrl: "https://mock.example/tickets/123",
|
|
sanitizedDisplayUrl: "https://mock.example/tickets/123",
|
|
canonicalIdentityHash: "hash-123",
|
|
canonicalIdentity: { scheme: "https", host: "mock.example", path: "/tickets/123" },
|
|
redactedMatchedText: "https://mock.example/tickets/123",
|
|
}],
|
|
sourceContext: {
|
|
companyId: "company-1",
|
|
sourceIssueId: "issue-1",
|
|
sourceKind: "description",
|
|
sourceRecordId: null,
|
|
documentKey: null,
|
|
propertyKey: null,
|
|
},
|
|
}, 2)));
|
|
await waitForResponses(responses, 2);
|
|
|
|
const detectResponse = responses[1];
|
|
expect(isJsonRpcSuccessResponse(detectResponse)).toBe(true);
|
|
if (!isJsonRpcSuccessResponse(detectResponse)) return;
|
|
expect(detectResponse.result.detections[0]).toMatchObject({
|
|
providerKey: "mocktracker",
|
|
objectType: "ticket",
|
|
externalId: "MOCK-123",
|
|
});
|
|
|
|
stdin.write(serializeMessage(createRequest("resolveExternalObject", {
|
|
companyId: "company-1",
|
|
providerKey: "mocktracker",
|
|
objectType: "ticket",
|
|
externalId: "MOCK-123",
|
|
object: {
|
|
id: "object-1",
|
|
companyId: "company-1",
|
|
providerKey: "mocktracker",
|
|
objectType: "ticket",
|
|
externalId: "MOCK-123",
|
|
sanitizedCanonicalUrl: "https://mock.example/tickets/123",
|
|
canonicalIdentityHash: "hash-123",
|
|
displayTitle: "Mock ticket",
|
|
statusKey: null,
|
|
statusLabel: null,
|
|
statusCategory: "unknown",
|
|
statusTone: "neutral",
|
|
liveness: "unknown",
|
|
isTerminal: false,
|
|
data: {},
|
|
remoteVersion: null,
|
|
etag: null,
|
|
},
|
|
}, 3)));
|
|
await waitForResponses(responses, 3);
|
|
|
|
const resolveResponse = responses[2];
|
|
expect(isJsonRpcSuccessResponse(resolveResponse)).toBe(true);
|
|
if (!isJsonRpcSuccessResponse(resolveResponse)) return;
|
|
expect(resolveResponse.result).toMatchObject({
|
|
ok: true,
|
|
snapshot: {
|
|
statusCategory: "succeeded",
|
|
statusTone: "success",
|
|
},
|
|
});
|
|
|
|
host.stop();
|
|
});
|
|
});
|
|
|
|
async function waitForResponses(responses: unknown[], count: number): Promise<void> {
|
|
const deadline = Date.now() + 1_000;
|
|
while (responses.length < count && Date.now() < deadline) {
|
|
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
}
|
|
expect(responses.length).toBeGreaterThanOrEqual(count);
|
|
}
|