Files
PaperClipAI/packages/paperclip-runner/src/contracts/codex.ts
T
Dotta d8dfa27a1e Define native runner execution contracts (#12351)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The Paperclip Runner package gives providers one stable execution
boundary
> - Later runner backends need one validated input and result contract
> - Provider-specific shapes cannot leak into server orchestration
> - This pull request adds the provider-neutral native execution
contracts
> - It also keeps legacy input versions readable while it validates new
input strictly
> - The benefit is a stable boundary for later backend and server pull
requests

## Linked Issues or Issue Description

**Subsystem affected**

packages/paperclip-runner

**Problem or motivation**

The runner has protocol transport and Codex support, but later backends
do not have one public contract for execution input, runtime context,
completion output, and recovery state.

**Proposed solution**

Add versioned native execution input parsers, model envelopes,
completion result helpers, runtime context contracts, and durable
recovery types. Keep these changes package-local. Do not change server
runtime selection.

**Alternatives considered**

Each provider could define its own server-facing input. That would
couple server orchestration to provider details and make compatibility
harder to verify.

**Roadmap alignment**

This is part of the existing experimental Paperclip Runner rollout. It
does not enable a new production adapter.

## What Changed

- Add versioned native execution input contracts and strict parsers.
- Add provider-neutral runtime context and completion contracts.
- Add durable recovery and local runner types.
- Add structured question compatibility and parser tests.
- Export only the contracts that exist in this stack layer.

## Verification

- `pnpm --filter @paperclipai/paperclip-runner test:typescript`
- `pnpm --filter @paperclipai/paperclip-runner typecheck:typescript`
- `pnpm -r typecheck`
- Protocol generation and manifest checks pass.
- The branch changes 12 files relative to its declared base.

## Risks

The main risk is accepting malformed or future input as a supported
contract. The parsers reject unknown required versions and unknown
fields. The change is package-local and does not select or start the
experimental runner.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

OpenAI Codex, `gpt-5`, with agentic reasoning, 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 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 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
2026-08-29 19:53:41 -05:00

183 lines
6.2 KiB
TypeScript

import type {
PrpCapabilities,
PrpEvent,
PrpStructuredRunResult,
} from "../protocol/replay-contract.js";
import type { SessionSnapshot } from "../reducer/session-reducer.js";
import {
PRP_BLOCK_RESULT_OUTPUT_SCHEMA,
PRP_BLOCK_RESULT_PROVIDER_INPUT_SCHEMA,
PRP_BLOCK_TOOL_NAME,
PRP_COMPLETION_RESULT_OUTPUT_SCHEMA,
PRP_COMPLETION_RESULT_PROVIDER_INPUT_SCHEMA,
PRP_COMPLETION_TOOL_NAME,
PRP_SEMANTIC_TOOL_NAMES,
} from "./completion-result.js";
export const CODEX_TASK_ENVELOPE_SCHEMA = "paperclip.skillless_task.v1" as const;
export const CODEX_CODEX_PROTOCOL_VERSION = "v2" as const;
/** @deprecated Use the provider-neutral PRP completion contract exports. */
export const CODEX_COMPLETION_TOOL_NAME = PRP_COMPLETION_TOOL_NAME;
/** @deprecated Use the provider-neutral PRP completion contract exports. */
export const CODEX_BLOCK_TOOL_NAME = PRP_BLOCK_TOOL_NAME;
/** @deprecated Use the provider-neutral PRP completion contract exports. */
export const CODEX_SEMANTIC_TOOL_NAMES = PRP_SEMANTIC_TOOL_NAMES;
export const CODEX_SKILLLESS_BASE_INSTRUCTIONS =
"Complete only the supplied task envelope. Do not discover or invoke skills. Do not call a control-plane API. Return exactly one semantic completion result; use paperclip_finish when the work is done or needs review, and paperclip_block only when work cannot continue." as const;
export interface CodexTaskEnvelope {
schema: typeof CODEX_TASK_ENVELOPE_SCHEMA;
objective: string;
completionContract: {
revision: string;
criteria: Array<{ id: string; requirement: string }>;
};
constraints: string[];
expectedResultSchema: "paperclip.run_result.v1";
}
export interface CodexModelContextSnapshot {
protocolVersion: typeof CODEX_CODEX_PROTOCOL_VERSION;
codexVersion: string;
clientInfo: {
name: "paperclip-runner";
title: "Paperclip Runner";
version: string;
};
model: string;
modelProvider: string;
workingDirectory: string;
collaborationMode: "default" | "plan";
sandbox: unknown;
approvalPolicy: unknown;
baseInstructions: string;
instructionSources: string[];
instructionPolicy: {
skillInstructions: boolean;
appInstructions: false;
collaborationInstructions: boolean;
};
environmentKeys: string[];
dynamicToolNames: string[];
modelInputKinds: ["text"];
liveConsole?: {
conversationMode?: "task" | "direct";
runtimeRequestResolution: boolean;
goals: boolean;
threadLineage: boolean;
};
envelope: CodexTaskEnvelope;
}
export interface CodexRunMetadata {
schema: "paperclip.runner.codex.metadata.v1";
fixtureName: string;
identity: {
schema: "paperclip.prp.identity.v1";
companyId: string;
issueId: string;
runId: string;
environmentLeaseId: string;
runnerInstanceId: string;
normalizedSessionId: string;
driverSessionId?: string;
providerSessionId?: string;
};
capabilities: PrpCapabilities;
}
export interface CodexRunTrace {
schema: "paperclip.runner.codex.trace.v1";
metadata: CodexRunMetadata;
context: CodexModelContextSnapshot;
events: PrpEvent[];
proposedResult: unknown | null;
result: PrpStructuredRunResult | null;
resultDecision: CodexResultDecision;
liveSnapshot: SessionSnapshot;
replaySnapshot: SessionSnapshot;
diagnostics: string[];
assertions: {
exactlyOneTerminalResult: boolean;
proposalAccepted: boolean;
liveReplayParity: boolean;
stableIdentity: boolean;
sourceSequenceContinuous: boolean;
stableItemIdentity: boolean;
contextIsSkillless: boolean;
unrelatedSkillsAbsent: boolean;
credentialsAbsent: boolean;
};
}
export interface CodexResultValidationIssue {
code:
| "schema_validation"
| "contract_revision_mismatch"
| "unknown_criterion"
| "missing_criterion"
| "duplicate_criterion"
| "invalid_disposition";
path: string;
message: string;
}
export type CodexResultDecision =
| { status: "accepted"; result: PrpStructuredRunResult; issues: [] }
| { status: "rejected"; result: null; issues: CodexResultValidationIssue[] };
export function createCodexTaskEnvelope(input: {
objective: string;
contractRevision?: string;
criteria?: Array<{ id: string; requirement: string }>;
constraints?: string[];
}): CodexTaskEnvelope {
return {
schema: CODEX_TASK_ENVELOPE_SCHEMA,
objective: input.objective,
completionContract: {
revision: input.contractRevision ?? "codex-demo-v1",
criteria:
input.criteria ?? [{ id: "objective", requirement: "Complete the objective safely." }],
},
constraints: input.constraints ?? [
"Work only inside the supplied working directory.",
"Do not discover or invoke skills.",
"Do not call a control-plane API.",
"Return one semantic completion result.",
],
expectedResultSchema: "paperclip.run_result.v1",
};
}
/** @deprecated Use PRP_COMPLETION_RESULT_OUTPUT_SCHEMA. */
export const CODEX_RESULT_OUTPUT_SCHEMA = PRP_COMPLETION_RESULT_OUTPUT_SCHEMA;
/** @deprecated Use PRP_BLOCK_RESULT_OUTPUT_SCHEMA. */
export const CODEX_BLOCK_RESULT_OUTPUT_SCHEMA = PRP_BLOCK_RESULT_OUTPUT_SCHEMA;
export const CODEX_RESULT_PROVIDER_INPUT_SCHEMA = PRP_COMPLETION_RESULT_PROVIDER_INPUT_SCHEMA;
export const CODEX_BLOCK_RESULT_PROVIDER_INPUT_SCHEMA = PRP_BLOCK_RESULT_PROVIDER_INPUT_SCHEMA;
export function isSkilllessCodexContext(
context: CodexModelContextSnapshot,
options: { dynamicTools: boolean } = { dynamicTools: true },
): boolean {
const serialized = JSON.stringify(context).toLowerCase();
const expectedTools = options.dynamicTools ? [...CODEX_SEMANTIC_TOOL_NAMES] : [];
return (
context.instructionSources.length === 0 &&
context.baseInstructions === CODEX_SKILLLESS_BASE_INSTRUCTIONS &&
context.modelInputKinds.length === 1 &&
context.modelInputKinds[0] === "text" &&
context.dynamicToolNames.length === expectedTools.length &&
context.dynamicToolNames.every((name) =>
expectedTools.includes(name as (typeof expectedTools)[number]),
) &&
context.instructionPolicy.skillInstructions === false &&
context.instructionPolicy.appInstructions === false &&
!serialized.includes("paperclip_api_key") &&
!serialized.includes("authorization: bearer") &&
!serialized.includes("/api/issues/") &&
!serialized.includes('"type":"skill"')
);
}