mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-21 08:22:20 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The runner package already provides the production protocol and execution spine. > - Contributors still need stable SDK surfaces, deterministic test tools, and local inspection tools. > - Those surfaces share generated contracts and must change as one package boundary. > - This pull request adds the package-local SDK, labs, examples, and drift checks. > - The benefit is a reviewable developer platform that does not change application execution selection. ## Linked Issues or Issue Description **Subsystem affected** `packages/paperclip-runner` — runner SDK, conformance tools, and developer tooling. **Problem or motivation** The production runner spine is present, but package consumers cannot build deterministic integrations, inspect sessions, or verify provider-neutral behavior through supported surfaces. **Proposed solution** Add browser, React, standalone, live-session, scenario, conformance, and evaluation surfaces. Add generated contract inventories and package-local verification scripts. Keep production application routing unchanged. **Alternatives considered** We considered splitting each generated catalog, SDK surface, and demo into separate pull requests. Those changes share exports, fixtures, and drift gates. Splitting them would create intermediate package states that do not build. **Roadmap alignment** No overlapping item appears in `ROADMAP.md`. This work extends the runner package that is already on `master`. ## What Changed - Add browser, React, standalone, live-session, and issue-thread SDK surfaces. - Add deterministic mock control-plane, scenario, conformance, replay, and evaluation tools. - Add bounded Codex, OpenCode, and ACPX development transports and fixtures. - Keep deferred managed-provider execution fail-closed. Persisted compatibility data remains readable. - Add generated capability inventories with their source files and drift checks. - Add examples, package documentation, browser checks, and clean-consumer checks. - Preserve the reviewed protocol bounds, replay compatibility aliases, process environment isolation, and semantic redaction limits. - Update the ACPX package patch that the existing workspace patch registry already tracks. - Do not change `pnpm-lock.yaml`, repository workflows, server runtime selection, or the application UI. ## Verification GitHub Actions is the verification authority for this pull request. The repository CI, package TypeScript and Rust checks, package tests, generated-output drift checks, browser checks, security scans, and Greptile review must pass on the exact head. Local test suites were not run because this series uses parallel GitHub Actions for verification. ## Risks This is a large greenfield package change. The main risks are public export drift, generated-output drift, and optional React consumer compatibility. Package boundary checks, clean-consumer checks, and browser tests cover those risks. Production adapter selection and server execution are outside this pull request. ## Stack 1. **This PR:** runner SDK and developer tooling. 2. [Codex production server integration](https://github.com/paperclipai/paperclip/pull/12616). 3. [Provider-neutral task-thread UI](https://github.com/paperclipai/paperclip/pull/12617). ## Model Used OpenAI Codex, GPT-5, high-reasoning mode, 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 described the issue in-PR following the feature request template - [x] I have not referenced internal/instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal Paperclip ticket id - [ ] 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 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
261 lines
9.8 KiB
TypeScript
261 lines
9.8 KiB
TypeScript
import type { CapabilityIssueThreadSnapshot } from "../../../src/issue-thread/types";
|
|
import type { CapabilityDevtoolsSnapshot } from "../../../src/devtools";
|
|
import type { CapabilityJsonValue } from "../../../src/mock-core/capability-control-plane-types";
|
|
import {
|
|
CAPABILITY_TURN_STREAM_ACCEPT,
|
|
CapabilityTurnStreamError,
|
|
readCapabilityTurnStream,
|
|
} from "../../../src/live/turn-stream";
|
|
|
|
/**
|
|
* Browser client for the package session server.
|
|
*
|
|
* Every response is a server-projected `CapabilityIssueThreadSnapshot`. The client
|
|
* posts intents and renders what comes back; it never patches the snapshot
|
|
* locally, which is what keeps policy and state authority on the server
|
|
* (contract §11).
|
|
*
|
|
* A turn is the one route that answers with many of those projections instead
|
|
* of one: `send` consumes NDJSON frames as the provider produces them, hands
|
|
* each interim view to `onFrame`, and resolves with the settled payload. The
|
|
* authority rule is unchanged — every frame is still a server projection, and
|
|
* the settled one is final.
|
|
*/
|
|
|
|
const BASE = "/api/capability/ui";
|
|
|
|
/**
|
|
* Every route is session-scoped and mediated by the per-browser capability
|
|
* cookie the server sets, so the cookie has to ride along. Stated rather than
|
|
* left to the default so a future change to this client cannot silently drop
|
|
* the capability and fall back to session-id-only access (track 7U).
|
|
*/
|
|
const CREDENTIALS = "same-origin" as const;
|
|
|
|
export interface CapabilityCleanRoomIdentity {
|
|
token: string;
|
|
sequence: number;
|
|
companyId: string;
|
|
actorId: string;
|
|
taskId: string;
|
|
identifier: string;
|
|
}
|
|
|
|
export interface CapabilityHarnessConfiguration {
|
|
provider: "codex" | "opencode" | "acpx";
|
|
model: string | null;
|
|
acpxAgent?: "claude" | "codex";
|
|
lifecyclePolicy:
|
|
| { mode: "per_turn"; idleTimeoutMs: null }
|
|
| { mode: "warm"; idleTimeoutMs: number };
|
|
}
|
|
|
|
export interface CapabilityLiveResponse {
|
|
sessionId: string;
|
|
view: CapabilityIssueThreadSnapshot;
|
|
surface?: "issue" | "cleanroom";
|
|
/** Present on the clean-room surface so the UI can show which tenant is live. */
|
|
identity?: CapabilityCleanRoomIdentity;
|
|
limits?: { maxTurns: number; maxMessageBytes: number };
|
|
turns?: number;
|
|
configuration?: CapabilityHarnessConfiguration;
|
|
runtime?: {
|
|
providerSessionId: string | null;
|
|
driverSessionId?: string | null;
|
|
runnerPid: number | null;
|
|
providerPid: number | null;
|
|
sidecarPid?: number | null;
|
|
agentPid?: number | null;
|
|
providerVersion?: string | null;
|
|
agentServerVersion?: string | null;
|
|
agentRuntimeVersion?: string | null;
|
|
acpProtocolVersion?: number | null;
|
|
executionKind: "local_process" | "remote_service";
|
|
status: string;
|
|
};
|
|
}
|
|
|
|
export interface CapabilityToolTestResponse extends CapabilityLiveResponse {
|
|
toolResult: CapabilityJsonValue;
|
|
toolTurnId: string;
|
|
}
|
|
|
|
/**
|
|
* The server names its own failures. Surfacing the code lets the clean room say
|
|
* "this chat hit its turn limit" instead of "500", and — more importantly —
|
|
* lets a failure to start real Codex read as a failure rather than quietly
|
|
* degrading into a fixture.
|
|
*/
|
|
export class CapabilityLiveError extends Error {
|
|
constructor(
|
|
readonly code: string,
|
|
message: string,
|
|
) {
|
|
super(message);
|
|
this.name = "CapabilityLiveError";
|
|
}
|
|
}
|
|
|
|
async function readError(response: Response, path: string): Promise<CapabilityLiveError> {
|
|
try {
|
|
const body = (await response.json()) as { error?: string; message?: string };
|
|
return new CapabilityLiveError(
|
|
body.error ?? `http_${response.status}`,
|
|
body.message ?? `${path} failed with ${response.status}`,
|
|
);
|
|
} catch {
|
|
return new CapabilityLiveError(`http_${response.status}`, `${path} failed with ${response.status}`);
|
|
}
|
|
}
|
|
|
|
async function post<T = CapabilityLiveResponse>(path: string, body: unknown): Promise<T> {
|
|
const response = await fetch(`${BASE}${path}`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
credentials: CREDENTIALS,
|
|
});
|
|
if (!response.ok) throw await readError(response, path);
|
|
return (await response.json()) as T;
|
|
}
|
|
|
|
/** Interim projection delivered while the turn is still running. */
|
|
export type CapabilityTurnFrameHandler = (view: CapabilityIssueThreadSnapshot) => void;
|
|
|
|
export interface CapabilitySendOptions {
|
|
onFrame?: CapabilityTurnFrameHandler;
|
|
/** Abort the turn stream — used when the surface is torn down or rotated. */
|
|
signal?: AbortSignal;
|
|
}
|
|
|
|
async function postTurnStream(
|
|
path: string,
|
|
body: unknown,
|
|
options: CapabilitySendOptions,
|
|
): Promise<CapabilityLiveResponse> {
|
|
const response = await fetch(`${BASE}${path}`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json", accept: CAPABILITY_TURN_STREAM_ACCEPT },
|
|
body: JSON.stringify(body),
|
|
credentials: CREDENTIALS,
|
|
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
});
|
|
// Admission failures still answer with a JSON body and a real status code,
|
|
// because they are decided before the first frame is written.
|
|
if (!response.ok) throw await readError(response, path);
|
|
try {
|
|
return await readCapabilityTurnStream<CapabilityIssueThreadSnapshot, CapabilityLiveResponse>(
|
|
response,
|
|
(frame) => options.onFrame?.(frame.view),
|
|
);
|
|
} catch (cause) {
|
|
if (cause instanceof CapabilityTurnStreamError) {
|
|
throw new CapabilityLiveError(cause.code, cause.message);
|
|
}
|
|
throw cause;
|
|
}
|
|
}
|
|
|
|
export const capabilityLiveClient = {
|
|
async devtools(sessionId: string): Promise<CapabilityDevtoolsSnapshot> {
|
|
const response = await fetch(`${BASE}/devtools?sessionId=${encodeURIComponent(sessionId)}`, {
|
|
credentials: CREDENTIALS,
|
|
});
|
|
if (!response.ok) throw await readError(response, "/devtools");
|
|
return (await response.json()) as CapabilityDevtoolsSnapshot;
|
|
},
|
|
fork(sessionId: string, revision: number): Promise<CapabilityLiveResponse> {
|
|
return post("/devtools/fork", { sessionId, revision });
|
|
},
|
|
invokeTool(
|
|
sessionId: string,
|
|
operationId: string,
|
|
input: CapabilityJsonValue,
|
|
): Promise<CapabilityToolTestResponse> {
|
|
return post("/tool", { sessionId, operationId, input }) as Promise<CapabilityToolTestResponse>;
|
|
},
|
|
async load(sessionId: string | null): Promise<CapabilityLiveResponse> {
|
|
const query = sessionId === null ? "" : `?sessionId=${encodeURIComponent(sessionId)}`;
|
|
const response = await fetch(`${BASE}/session${query}`, { credentials: CREDENTIALS });
|
|
if (!response.ok) throw await readError(response, "/session");
|
|
return (await response.json()) as CapabilityLiveResponse;
|
|
},
|
|
create(scenario: string): Promise<CapabilityLiveResponse> {
|
|
return post("/session", { scenario });
|
|
},
|
|
/** Reconnect to a clean room, or open one when there is nothing to resume. */
|
|
async loadCleanRoom(sessionId: string | null, configuration: CapabilityHarnessConfiguration): Promise<CapabilityLiveResponse> {
|
|
const query = new URLSearchParams({ provider: configuration.provider });
|
|
if (sessionId !== null) query.set("sessionId", sessionId);
|
|
if (configuration.model !== null) query.set("model", configuration.model);
|
|
if (configuration.acpxAgent) query.set("acpxAgent", configuration.acpxAgent);
|
|
query.set("lifecycleMode", configuration.lifecyclePolicy.mode);
|
|
if (configuration.lifecyclePolicy.mode === "warm") {
|
|
query.set("idleTimeoutMs", String(configuration.lifecyclePolicy.idleTimeoutMs));
|
|
}
|
|
const response = await fetch(`${BASE}/cleanroom/session?${query.toString()}`, { credentials: CREDENTIALS });
|
|
if (!response.ok) throw await readError(response, "/cleanroom/session");
|
|
return (await response.json()) as CapabilityLiveResponse;
|
|
},
|
|
/** `New chat`: retire the current room and mint a new mock tenant. */
|
|
newCleanRoom(sessionId: string | null, configuration: CapabilityHarnessConfiguration): Promise<CapabilityLiveResponse> {
|
|
return post("/cleanroom/session", { ...(sessionId === null ? {} : { sessionId }), ...configuration });
|
|
},
|
|
/**
|
|
* Runs one turn. `onFrame` fires for every interim projection the server
|
|
* writes while the POST is open; the resolved value is the settled payload.
|
|
*/
|
|
send(
|
|
sessionId: string,
|
|
message: string,
|
|
options: CapabilitySendOptions = {},
|
|
): Promise<CapabilityLiveResponse> {
|
|
return postTurnStream("/message", { sessionId, message }, options);
|
|
},
|
|
stop(sessionId: string): Promise<CapabilityLiveResponse> {
|
|
return post("/interrupt", { sessionId });
|
|
},
|
|
reset(sessionId: string): Promise<CapabilityLiveResponse> {
|
|
return post("/reset", { sessionId });
|
|
},
|
|
reconnect(sessionId: string): Promise<CapabilityLiveResponse> {
|
|
return post("/reconnect", { sessionId });
|
|
},
|
|
respond(
|
|
sessionId: string,
|
|
interactionId: string,
|
|
outcome: string,
|
|
result: unknown,
|
|
): Promise<CapabilityLiveResponse> {
|
|
return post("/interaction", { sessionId, interactionId, outcome, result });
|
|
},
|
|
};
|
|
|
|
const SESSION_STORAGE_KEY = "paperclip-runner.capability.session";
|
|
/**
|
|
* The clean room keeps its own key. Sharing one would let a scenario session id
|
|
* be handed to the clean-room route (and the reverse) after a refresh, which is
|
|
* exactly the cross-surface bleed the isolation criterion forbids.
|
|
*/
|
|
const CLEAN_ROOM_STORAGE_KEY = "paperclip-runner.capability.cleanroom.session";
|
|
|
|
function storageKey(surface: "issue" | "cleanroom"): string {
|
|
return surface === "cleanroom" ? CLEAN_ROOM_STORAGE_KEY : SESSION_STORAGE_KEY;
|
|
}
|
|
|
|
export function rememberSession(sessionId: string, surface: "issue" | "cleanroom" = "issue"): void {
|
|
try {
|
|
window.localStorage.setItem(storageKey(surface), sessionId);
|
|
} catch {
|
|
// Refresh restore falls back to a fresh session when storage is blocked.
|
|
}
|
|
}
|
|
|
|
export function recallSession(surface: "issue" | "cleanroom" = "issue"): string | null {
|
|
try {
|
|
return window.localStorage.getItem(storageKey(surface));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|