mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 20:09:33 +01:00
## Thinking Path > - Paperclip manages AI agent runs and their tool authority. > - Runner supplies provider sessions with assigned MCP connections. > - The current native connection binding supports an HTTP gateway only. > - Execution hosts also have private local tools with stdio MCP interfaces. > - Those tools need session-local registration and safe concurrency. > - This pull request adds verified local stdio assignments beside the existing gateway. > - Providers receive the existing tool schemas and image content. ## Linked Issues or Issue Description **Problem or motivation** Runner can supply a native provider session with an HTTP MCP gateway, but cannot also assign a verified local stdio MCP server without persistent provider configuration. Execution hosts need a session-local transport for local MCP tools, coordination for calls sharing a resource, and native schema/image delivery. The pinned native Codex client drops MCP images when `structuredContent` is also present. **Proposed solution** Add general stdio MCP transport support using host-assigned executable digests, arguments, environment and a private Unix socket identifying the shared resource. Tool implementations supply their own schemas and responses; Runner forwards their native MCP schemas and content. Register the transport for the current provider session. Serialize calls across processes, keep unknown outcomes fenced, and authorize assigned native Codex tools without a new prompt while routing explicit tool confirmations through existing PRP permission requests. Retain the HTTP gateway and applicable permission policy. **Alternatives considered** Reimplementing tools or translating their actions into provider-specific APIs would tie the transport to individual tool implementations. Global provider config would leak session assignments. Shell commands alone do not deliver native MCP images or preserve MCP approval requests. **Roadmap alignment** This completes the existing Runner transport boundary. Related: #10690 (operator config for the legacy ACPX engine), #11605 (gateway delivery of installed stdio connections), #16486 (preconfigured provider homes). This change assigns local tools on the execution host and leaves those administration paths intact. ## What Changed - Add reserved, verified stdio bindings for native Codex and through the shared ACPX runtime. ACPX clients must support stdio MCP assignments. Read-only sessions omit the binding. No home/project config is rewritten. - Run the backend with only its assigned environment through the verified executable launch path. - Serialize all calls sharing a private Unix socket across sessions. Metadata remains responsive. Queued cancellation prevents execution; active calls remain fenced until acknowledgement. Unknown outcomes require a resource restart. - Authorize host-assigned native Codex tools without a new per-call prompt, matching existing execution-host shell utilities. Preserve explicit tool deny/prompt rules. Route requested MCP tool confirmations through one-time PRP approvals, bound to the active thread and turn. Keep shell escalation disabled and existing tool deny rules effective. ACPX adapters retain their applicable ACPX permission policy. - Preserve schemas and image blocks. For native Codex 0.160/0.162, retain structured metadata as MCP text on image-bearing results to avoid the client's image-loss bug. ACPX adapters receive the original response. - Document transport authority, cancellation and recovery limits. ## Verification - `pnpm -r typecheck` and `pnpm build` passed with pnpm 9.15.4 and Rust 1.97.1 on macOS arm64. - Before Apex, full `paperclip-runner-core` Rust tests passed. After the fixes, all 379 library tests and eight subprocess tests using a generic stdio MCP fixture pass. Those subprocess tests cover schema/image passthrough, assigned-only environment, executable drift, call serialization, cancellation and unknown outcomes. The native Codex suite passed 91 tests with two intentionally ignored; `receipt_limit_rejects_the_call_and_keeps_polling_when_interrupt_fails` timed out waiting for `turn.interrupted`, including an isolated retry. The unchanged pre-fix commit `80917b301a2309f7966a466cf0c198a2112be2d5` reproduces the same timeout in isolation; this local native-suite run is not claimed as fully passing. - All 252 native controller tests pass against the final narrowed approval branch. Focused TypeScript transport/permission suites passed (298 tests); final binding, read-only host, policy and adapter checks passed (105 tests). - **Provider-client smoke coverage:** pinned Codex 0.160.0 through native Runner and Claude SDK 0.3.286 used local stdio MCP servers and loopback model API fixtures without live model credentials. These clients define the smoke-test coverage; this is not an exhaustive adapter compatibility test. Image responses reached the next model request as typed image blocks. Native Codex checks also verified unattended execution without approval requests, explicit per-tool prompt denial without backend execution, disabled-tool rules, and stale-turn approval rejection. - Two Greptile concurrency findings were reproduced with failing subprocess tests and fixed: pending cancellation under output backpressure, and a ctime-based fence reset after chmod. Initial checks exposed Codex CLI option-scope and key-quoting errors, which were corrected. The existing ordinary elicitation regression also exposed an overly broad request branch; the final branch handles explicit tool approvals only. - `pnpm test:run` was attempted. The broad local server suite hit persistent embedded PostgreSQL startup failures and unrelated local Git fixture failures; it was stopped with the failure log retained. Those local results are not claimed as passing. Hosted Linux CI gates passed on the reviewed runtime commit, as recorded below. - First Apex review found three additional defects, now fixed with regressions: keep the resource-lock socket snapshot instead of sampling the socket twice; project native permission requests with durable turn/item IDs while retaining provider IDs in details; validate provider response conversion before marking a request as settling. The retry regression failed before the fix. Focused Codex request/adapter checks pass (23 tests), server permission projection passes (16 tests), and all 379 Runner library tests pass. The broader database-backed bridge suite was attempted but its local PostgreSQL setup timed out; 29 tests did not execute. This local failure is retained, not claimed as passing. - Runtime acceptance before the documentation clarification, on `bd4fccae001157beb5bc76ff17c1920a5db3105a`: standard Greptile 5/5, then Apex 5/5 with no unresolved threads; all 60 checks complete successfully or intentionally skip. Ten CI jobs were interrupted by the same runner shutdown, including one stale job that required cancellation before retry; the same-commit retry passed. Hosted Rust tests also pass, including the native suite with the local baseline timeout noted above. - `git diff --check`, map validation, and pre-push secret/PII scan passed. ## Risks - No live model account or deployment isolation qualification was run. The loopback fixture proves the actual vendor-client request format, not model interpretation. - Stdio MCP support uses the current host-assignment contract: each binding requires a verified executable and a private Unix socket identifying the serialized resource. Other platforms retain the HTTP path. No provider version was upgraded. - A lost acknowledgement blocks subsequent calls until the host restarts the assigned resource. Direct shell calls are outside this MCP fence and retain the existing shell policy. - Preconfigured Claude remains unattended and denies interactive approval requests. Host assignment authorizes native Codex local tools; it must be supplied only for sessions already authorized to use the resource. ## Model Used OpenAI Codex, GPT-6, with reasoning and repository/tool execution. The session does not expose a more specific model identifier or context-window size. ## 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 - [ ] 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 - [ ] 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 --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
646 lines
24 KiB
TypeScript
646 lines
24 KiB
TypeScript
import { isAcpxCanonicalInputMethod } from "../acpx/profile-extensions.js";
|
|
import type {
|
|
HarnessRuntimeRequest,
|
|
HarnessRuntimeRequestKind,
|
|
HarnessRuntimeRequestResolution,
|
|
PaperclipQuestion,
|
|
PaperclipQuestionResponse,
|
|
PaperclipQuestionSet,
|
|
} from "../../contracts/harness-driver.js";
|
|
import {
|
|
PAPERCLIP_QUESTION_SET_SCHEMA,
|
|
PAPERCLIP_RUNTIME_REQUEST_SCHEMA_V2,
|
|
parsePaperclipQuestionSet,
|
|
} from "../../contracts/harness-driver.js";
|
|
import { redactCodexDiagnostic } from "./app-server-transport.js";
|
|
|
|
export interface CodexQuestionResponseContext {
|
|
readonly kind: "codex_question_response_context";
|
|
}
|
|
|
|
const NATIVE_OPTION_VALUES = new WeakMap<
|
|
CodexQuestionResponseContext,
|
|
ReadonlyMap<string, ReadonlyMap<string, unknown>>
|
|
>();
|
|
|
|
/** Create one private response context for one provider runtime request. */
|
|
export function createCodexQuestionResponseContext(): CodexQuestionResponseContext {
|
|
const context = Object.freeze({
|
|
kind: "codex_question_response_context" as const,
|
|
});
|
|
NATIVE_OPTION_VALUES.set(context, new Map());
|
|
return context;
|
|
}
|
|
|
|
interface NormalizedQuestionOptions {
|
|
options: NonNullable<PaperclipQuestion["options"]>;
|
|
nativeValues: ReadonlyMap<string, unknown>;
|
|
}
|
|
|
|
function record(value: unknown): Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
? (value as Record<string, unknown>)
|
|
: {};
|
|
}
|
|
|
|
function text(value: unknown, fallback = ""): string {
|
|
return typeof value === "string" ? value : fallback;
|
|
}
|
|
|
|
function boundedText(
|
|
value: unknown,
|
|
fallback = "unknown",
|
|
maxCharacters = 1024,
|
|
): string {
|
|
const candidate = text(value, fallback);
|
|
return candidate.length <= maxCharacters
|
|
? candidate
|
|
: `${candidate.slice(0, maxCharacters)}...[truncated]`;
|
|
}
|
|
|
|
export function runtimeRequestKind(method: string, params?: Record<string, unknown>): HarnessRuntimeRequestKind | null {
|
|
if (method === "mcpServer/elicitation/request" && record(params?._meta).codex_approval_kind === "mcp_tool_call"
|
|
&& params?.mode === "form" && record(params.requestedSchema).type === "object"
|
|
&& Object.keys(record(record(params.requestedSchema).properties)).length === 0
|
|
&& Object.keys(record(params.requestedSchema)).sort().join(",") === "properties,type") return "permission_approval";
|
|
if (
|
|
method === "item/commandExecution/requestApproval" ||
|
|
method === "execCommandApproval"
|
|
) {
|
|
return "command_approval";
|
|
}
|
|
if (
|
|
method === "item/fileChange/requestApproval" ||
|
|
method === "applyPatchApproval"
|
|
) {
|
|
return "file_approval";
|
|
}
|
|
if (method === "item/permissions/requestApproval" || method === "session/request_permission")
|
|
return "permission_approval";
|
|
if (
|
|
method === "item/tool/requestUserInput" ||
|
|
method === "tool/requestUserInput"
|
|
) {
|
|
return "user_input";
|
|
}
|
|
if (method === "mcpServer/elicitation/request" || isAcpxCanonicalInputMethod(method)) return "elicitation";
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* During the v1 migration, input requests that predate structured form data
|
|
* remain opaque runtime requests. Once a provider supplies a native form,
|
|
* however, a malformed/unsupported form must fail closed instead of silently
|
|
* degrading back to the legacy textarea presentation.
|
|
*/
|
|
export function hasCodexQuestionForm(method: string, params: Record<string, unknown>): boolean {
|
|
if (isAcpxCanonicalInputMethod(method)) return "questionSet" in params;
|
|
if (method === "item/tool/requestUserInput" || method === "tool/requestUserInput") {
|
|
return "questions" in params;
|
|
}
|
|
if (method === "mcpServer/elicitation/request") {
|
|
return "requestedSchema" in params || "schema" in params;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
export function runtimeRequestPrompt(
|
|
kind: HarnessRuntimeRequestKind,
|
|
params: Record<string, unknown>,
|
|
): string {
|
|
const reason = text(params.reason, text(params.message));
|
|
if (reason.length > 0) return boundedText(redactCodexDiagnostic(reason));
|
|
const labels: Record<HarnessRuntimeRequestKind, string> = {
|
|
command_approval: "Codex requests approval to run a command.",
|
|
file_approval: "Codex requests approval to change files.",
|
|
permission_approval: "Codex requests additional runtime permissions.",
|
|
user_input: "Codex requests user input.",
|
|
elicitation: "A tool requests structured user input.",
|
|
};
|
|
return labels[kind];
|
|
}
|
|
|
|
function stableQuestionId(value: unknown, index: number): string {
|
|
const candidate = text(value).trim();
|
|
if (candidate.length > 160) {
|
|
throw new Error("Codex question identifier exceeds 160 characters");
|
|
}
|
|
return candidate.length > 0 ? candidate : `question-${index + 1}`;
|
|
}
|
|
|
|
function exactOptionLabel(value: unknown, index: number): string {
|
|
const label = text(value);
|
|
if (label.length > 1_000) {
|
|
throw new Error("Codex option label exceeds 1000 characters");
|
|
}
|
|
const redacted = redactCodexDiagnostic(label || `Option ${index + 1}`);
|
|
if (redacted.length > 1_000) {
|
|
throw new Error("Codex option label exceeds 1000 characters after redaction");
|
|
}
|
|
return redacted;
|
|
}
|
|
|
|
function exactQuestionText(
|
|
value: unknown,
|
|
field: string,
|
|
maxCharacters = 4_000,
|
|
): string {
|
|
const candidate = text(value);
|
|
if (candidate.length > maxCharacters) {
|
|
throw new Error(
|
|
`Codex ${field} exceeds ${maxCharacters} characters`,
|
|
);
|
|
}
|
|
return candidate;
|
|
}
|
|
|
|
function exactRedactedQuestionText(
|
|
value: unknown,
|
|
field: string,
|
|
maxCharacters = 4_000,
|
|
): string {
|
|
const redacted = redactCodexDiagnostic(
|
|
exactQuestionText(value, field, maxCharacters),
|
|
);
|
|
if (redacted.length <= maxCharacters) return redacted;
|
|
throw new Error(
|
|
`Codex ${field} exceeds ${maxCharacters} characters after redaction`,
|
|
);
|
|
}
|
|
|
|
function codexOptions(value: unknown): NormalizedQuestionOptions | undefined {
|
|
if (!Array.isArray(value)) return undefined;
|
|
if (value.length > 128) throw new Error("Codex question exceeds 128 options");
|
|
const explicitIds = value.map((rawOption, index) => {
|
|
const optionId = text(record(rawOption).id).trim();
|
|
return optionId.length > 0 ? stableQuestionId(optionId, index) : null;
|
|
});
|
|
const usedIds = new Set<string>();
|
|
for (const id of explicitIds) {
|
|
if (id === null) continue;
|
|
if (usedIds.has(id)) {
|
|
throw new Error("Codex option identifiers must be unique");
|
|
}
|
|
usedIds.add(id);
|
|
}
|
|
const nativeValues = new Map<string, unknown>();
|
|
const options = value.map((rawOption, index) => {
|
|
const option = record(rawOption);
|
|
const nativeLabel = text(
|
|
option.label,
|
|
text(option.value, text(rawOption, `Option ${index + 1}`)),
|
|
);
|
|
let id = explicitIds[index];
|
|
if (id === null || id === undefined) {
|
|
let generatedIndex = index + 1;
|
|
id = `option-${generatedIndex}`;
|
|
while (usedIds.has(id)) {
|
|
generatedIndex += 1;
|
|
id = `option-${generatedIndex}`;
|
|
}
|
|
usedIds.add(id);
|
|
}
|
|
nativeValues.set(
|
|
id,
|
|
structuredClone("value" in option ? option.value : nativeLabel),
|
|
);
|
|
return {
|
|
id,
|
|
label: exactOptionLabel(nativeLabel, index),
|
|
...(text(option.description).length > 0
|
|
? {
|
|
description: exactRedactedQuestionText(
|
|
option.description,
|
|
"option description",
|
|
),
|
|
}
|
|
: {}),
|
|
};
|
|
});
|
|
return { options, nativeValues };
|
|
}
|
|
|
|
function jsonSchemaOptions(schema: Record<string, unknown>): NormalizedQuestionOptions {
|
|
const values = Array.isArray(schema.enum)
|
|
? schema.enum
|
|
: Array.isArray(schema.oneOf)
|
|
? schema.oneOf.map((entry) => record(entry).const)
|
|
: [];
|
|
if (values.length > 128) throw new Error("Codex question exceeds 128 options");
|
|
const nativeValues = new Map<string, unknown>();
|
|
const options = values.map((value, index) => {
|
|
const oneOf = Array.isArray(schema.oneOf) ? record(schema.oneOf[index]) : {};
|
|
const id = `option-${index + 1}`;
|
|
nativeValues.set(id, structuredClone(value));
|
|
return {
|
|
id,
|
|
label: exactOptionLabel(
|
|
text(
|
|
oneOf.title,
|
|
typeof value === "string" ? value : JSON.stringify(value),
|
|
),
|
|
index,
|
|
),
|
|
...(text(oneOf.description).length > 0
|
|
? {
|
|
description: exactRedactedQuestionText(
|
|
text(oneOf.description),
|
|
"option description",
|
|
),
|
|
}
|
|
: {}),
|
|
};
|
|
});
|
|
return { options, nativeValues };
|
|
}
|
|
|
|
function retainNativeOptionValues(
|
|
questionSet: PaperclipQuestionSet,
|
|
nativeValues: ReadonlyMap<string, ReadonlyMap<string, unknown>>,
|
|
responseContext: CodexQuestionResponseContext,
|
|
): PaperclipQuestionSet {
|
|
if (!NATIVE_OPTION_VALUES.has(responseContext)) {
|
|
throw new Error("Codex question response context was not created by this adapter");
|
|
}
|
|
NATIVE_OPTION_VALUES.set(responseContext, nativeValues);
|
|
return questionSet;
|
|
}
|
|
|
|
/** Codex-native requests are converted once, before they enter PRP. */
|
|
export function normalizeCodexQuestionSet(
|
|
method: string,
|
|
params: Record<string, unknown>,
|
|
responseContext: CodexQuestionResponseContext,
|
|
): PaperclipQuestionSet | null {
|
|
if (isAcpxCanonicalInputMethod(method)) return parsePaperclipQuestionSet(params.questionSet);
|
|
if (method === "item/tool/requestUserInput" || method === "tool/requestUserInput") {
|
|
if (!Array.isArray(params.questions) || params.questions.length === 0) return null;
|
|
if (params.questions.length > 64) throw new Error("Codex question form exceeds 64 questions");
|
|
const explicitQuestionIds = params.questions.map((rawQuestion, index) => {
|
|
const questionId = text(record(rawQuestion).id).trim();
|
|
return questionId.length > 0 ? stableQuestionId(questionId, index) : null;
|
|
});
|
|
const usedQuestionIds = new Set<string>();
|
|
for (const id of explicitQuestionIds) {
|
|
if (id === null) continue;
|
|
if (usedQuestionIds.has(id)) {
|
|
throw new Error("Codex question identifiers must be unique");
|
|
}
|
|
usedQuestionIds.add(id);
|
|
}
|
|
const nativeValues = new Map<string, ReadonlyMap<string, unknown>>();
|
|
const questions = params.questions.map((rawQuestion, index): PaperclipQuestion => {
|
|
const question = record(rawQuestion);
|
|
const normalizedOptions = codexOptions(question.options);
|
|
let questionId = explicitQuestionIds[index];
|
|
if (questionId === null || questionId === undefined) {
|
|
let generatedIndex = index + 1;
|
|
questionId = `question-${generatedIndex}`;
|
|
while (usedQuestionIds.has(questionId)) {
|
|
generatedIndex += 1;
|
|
questionId = `question-${generatedIndex}`;
|
|
}
|
|
usedQuestionIds.add(questionId);
|
|
}
|
|
if (normalizedOptions !== undefined) {
|
|
nativeValues.set(questionId, normalizedOptions.nativeValues);
|
|
}
|
|
return {
|
|
id: questionId,
|
|
...(text(question.header).length > 0
|
|
? {
|
|
header: exactRedactedQuestionText(
|
|
text(question.header),
|
|
"question header",
|
|
1_000,
|
|
),
|
|
}
|
|
: {}),
|
|
prompt: exactRedactedQuestionText(
|
|
text(question.question, text(question.prompt, `Question ${index + 1}`)),
|
|
"question prompt",
|
|
),
|
|
...(text(question.description).length > 0
|
|
? {
|
|
helpText: exactRedactedQuestionText(
|
|
question.description,
|
|
"question description",
|
|
),
|
|
}
|
|
: {}),
|
|
// Codex requestUserInput questions do not normally declare requiredness.
|
|
// Do not invent a required constraint when the provider omitted one.
|
|
required: question.required === true,
|
|
answerMode: normalizedOptions && normalizedOptions.options.length > 0
|
|
? question.multiSelect === true || question.multiple === true ? "multi_select" : "single_select"
|
|
: "text",
|
|
...(normalizedOptions && normalizedOptions.options.length > 0
|
|
? { options: normalizedOptions.options }
|
|
: {}),
|
|
...(question.isOther === true || question.allowOther === true
|
|
? { customAnswer: { enabled: true, label: "Other", placeholder: "Enter another answer" } }
|
|
: {}),
|
|
...(!(normalizedOptions && normalizedOptions.options.length > 0) && (typeof question.minLength === "number" || typeof question.maxLength === "number")
|
|
? { textValidation: {
|
|
...(typeof question.minLength === "number" ? { minLength: question.minLength } : {}),
|
|
...(typeof question.maxLength === "number" ? { maxLength: question.maxLength } : {}),
|
|
} }
|
|
: {}),
|
|
};
|
|
});
|
|
return retainNativeOptionValues(parsePaperclipQuestionSet({
|
|
schema: PAPERCLIP_QUESTION_SET_SCHEMA,
|
|
title: exactRedactedQuestionText(
|
|
text(params.title, "Codex needs your input"),
|
|
"form title",
|
|
1_000,
|
|
),
|
|
...(text(params.description).length > 0
|
|
? {
|
|
description: exactRedactedQuestionText(
|
|
params.description,
|
|
"form description",
|
|
),
|
|
}
|
|
: {}),
|
|
submitLabel: exactRedactedQuestionText(
|
|
text(params.submitLabel, "Submit answers"),
|
|
"submit label",
|
|
1_000,
|
|
),
|
|
questions,
|
|
}), nativeValues, responseContext);
|
|
}
|
|
if (method !== "mcpServer/elicitation/request") return null;
|
|
const requestedSchema = record(params.requestedSchema ?? params.schema);
|
|
const properties = record(requestedSchema.properties);
|
|
const required = new Set(Array.isArray(requestedSchema.required) ? requestedSchema.required.filter((entry): entry is string => typeof entry === "string") : []);
|
|
const propertyEntries = Object.entries(properties);
|
|
if (propertyEntries.length > 64) throw new Error("Codex question form exceeds 64 questions");
|
|
const nativeValues = new Map<string, ReadonlyMap<string, unknown>>();
|
|
const questions = propertyEntries.map(([id, rawProperty]): PaperclipQuestion => {
|
|
const property = record(rawProperty);
|
|
const propertyType = text(property.type);
|
|
const itemSchema = record(property.items);
|
|
const selectSchema = propertyType === "array" ? itemSchema : property;
|
|
const normalizedOptions = propertyType === "boolean"
|
|
? {
|
|
options: [{ id: "true", label: "Yes" }, { id: "false", label: "No" }],
|
|
nativeValues: new Map<string, unknown>([["true", true], ["false", false]]),
|
|
}
|
|
: jsonSchemaOptions(selectSchema);
|
|
if (normalizedOptions.options.length > 0) {
|
|
nativeValues.set(id, normalizedOptions.nativeValues);
|
|
}
|
|
const answerMode: PaperclipQuestion["answerMode"] = propertyType === "array" && normalizedOptions.options.length > 0
|
|
? "multi_select"
|
|
: normalizedOptions.options.length > 0
|
|
? "single_select"
|
|
: "text";
|
|
const inputType = propertyType === "integer" ? "integer" : propertyType === "number" ? "number" : "text";
|
|
return {
|
|
id,
|
|
...(text(property.title).length > 0
|
|
? {
|
|
header: exactRedactedQuestionText(
|
|
text(property.title),
|
|
"question header",
|
|
1_000,
|
|
),
|
|
}
|
|
: {}),
|
|
prompt: exactRedactedQuestionText(
|
|
text(property.title, id),
|
|
"question prompt",
|
|
),
|
|
...(text(property.description).length > 0
|
|
? {
|
|
helpText: exactRedactedQuestionText(
|
|
text(property.description),
|
|
"question description",
|
|
),
|
|
}
|
|
: {}),
|
|
required: required.has(id),
|
|
answerMode,
|
|
...(normalizedOptions.options.length > 0 ? { options: normalizedOptions.options } : {}),
|
|
...(answerMode === "text" ? { textValidation: {
|
|
inputType,
|
|
...(typeof property.minLength === "number" ? { minLength: property.minLength } : {}),
|
|
...(typeof property.maxLength === "number" ? { maxLength: property.maxLength } : {}),
|
|
...(typeof property.minimum === "number" ? { minimum: property.minimum } : {}),
|
|
...(typeof property.maximum === "number" ? { maximum: property.maximum } : {}),
|
|
...(typeof property.pattern === "string" ? { pattern: property.pattern } : {}),
|
|
} } : {}),
|
|
};
|
|
});
|
|
if (questions.length === 0) return null;
|
|
return retainNativeOptionValues(parsePaperclipQuestionSet({
|
|
schema: PAPERCLIP_QUESTION_SET_SCHEMA,
|
|
title: "A tool needs your input",
|
|
...(text(params.message).length > 0
|
|
? {
|
|
description: exactRedactedQuestionText(
|
|
text(params.message),
|
|
"form description",
|
|
),
|
|
}
|
|
: {}),
|
|
submitLabel: "Submit",
|
|
questions,
|
|
}), nativeValues, responseContext);
|
|
}
|
|
|
|
export function runtimeRequestProtocolPayload(request: HarnessRuntimeRequest): Record<string, unknown> {
|
|
if (request.method === "session/request_permission") {
|
|
return { ...request, schema: PAPERCLIP_RUNTIME_REQUEST_SCHEMA_V2, type: "permission", choices: request.details.choices };
|
|
}
|
|
if (request.requestKind === "permission_approval" && request.method === "mcpServer/elicitation/request") {
|
|
return { schema: PAPERCLIP_RUNTIME_REQUEST_SCHEMA_V2, requestKind: "permission_approval", requestId: request.requestId,
|
|
type: "permission", status: request.status, prompt: request.prompt, choices: request.details.choices,
|
|
details: request.details, origin: request.origin, turnId: request.turnId, itemId: request.itemId };
|
|
}
|
|
if (request.input !== undefined) {
|
|
return {
|
|
schema: PAPERCLIP_RUNTIME_REQUEST_SCHEMA_V2,
|
|
requestKind: "runtime",
|
|
requestId: request.requestId,
|
|
type: "input",
|
|
status: request.status,
|
|
prompt: request.prompt,
|
|
input: structuredClone(request.input),
|
|
origin: structuredClone(request.origin),
|
|
turnId: request.turnId,
|
|
itemId: request.itemId,
|
|
};
|
|
}
|
|
return { ...request, type: request.method };
|
|
}
|
|
|
|
function canonicalCodexAnswers(
|
|
request: HarnessRuntimeRequest,
|
|
response: PaperclipQuestionResponse,
|
|
responseContext: CodexQuestionResponseContext,
|
|
): Record<string, { answers: string[] }> {
|
|
const result: Record<string, { answers: string[] }> = {};
|
|
for (const question of request.input?.questions ?? []) {
|
|
const answer = response.answers[question.id];
|
|
if (answer === undefined) continue;
|
|
const nativeValues = request.input === undefined
|
|
? undefined
|
|
: NATIVE_OPTION_VALUES.get(responseContext)?.get(question.id);
|
|
const labels = (answer.selectedOptionIds ?? []).map((optionId) => {
|
|
const value = nativeValues?.has(optionId)
|
|
? nativeValues.get(optionId)
|
|
: question.options?.find((option) => option.id === optionId)?.label;
|
|
if (typeof value === "string") return value;
|
|
const serialized = JSON.stringify(value);
|
|
if (serialized === undefined) {
|
|
throw new Error("Codex native option value must be JSON-serializable");
|
|
}
|
|
return serialized;
|
|
});
|
|
if (answer.text !== undefined) labels.push(answer.text);
|
|
if (answer.customText !== undefined) labels.push(answer.customText);
|
|
result[question.id] = { answers: labels };
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function jsonSchemaOptionValue(
|
|
schema: Record<string, unknown>,
|
|
optionId: string,
|
|
nativeValues: ReadonlyMap<string, unknown> | undefined,
|
|
): unknown {
|
|
if (nativeValues?.has(optionId)) return structuredClone(nativeValues.get(optionId));
|
|
if (optionId === "true") return true;
|
|
if (optionId === "false") return false;
|
|
const index = Number(optionId.match(/^option-(\d+)$/)?.[1] ?? "0") - 1;
|
|
if (index < 0) return optionId;
|
|
if (Array.isArray(schema.enum)) return schema.enum[index];
|
|
if (Array.isArray(schema.oneOf)) return record(schema.oneOf[index]).const;
|
|
return optionId;
|
|
}
|
|
|
|
function canonicalElicitationContent(
|
|
request: HarnessRuntimeRequest,
|
|
response: PaperclipQuestionResponse,
|
|
responseContext: CodexQuestionResponseContext,
|
|
): Record<string, unknown> {
|
|
const requestedSchema = record(request.details.requestedSchema ?? request.details.schema);
|
|
const properties = record(requestedSchema.properties);
|
|
const content: Record<string, unknown> = {};
|
|
for (const question of request.input?.questions ?? []) {
|
|
const answer = response.answers[question.id];
|
|
if (answer === undefined) continue;
|
|
const property = record(properties[question.id]);
|
|
const itemSchema = record(property.items);
|
|
const nativeValues = request.input === undefined
|
|
? undefined
|
|
: NATIVE_OPTION_VALUES.get(responseContext)?.get(question.id);
|
|
if (question.answerMode === "text") {
|
|
const value = answer.text ?? "";
|
|
content[question.id] = property.type === "integer" || property.type === "number" ? Number(value) : value;
|
|
} else if (question.answerMode === "multi_select") {
|
|
content[question.id] = (answer.selectedOptionIds ?? []).map((optionId) =>
|
|
jsonSchemaOptionValue(itemSchema, optionId, nativeValues));
|
|
} else {
|
|
const optionId = answer.selectedOptionIds?.[0];
|
|
if (optionId !== undefined) {
|
|
content[question.id] = jsonSchemaOptionValue(property, optionId, nativeValues);
|
|
}
|
|
else if (answer.customText !== undefined) content[question.id] = answer.customText;
|
|
}
|
|
}
|
|
return content;
|
|
}
|
|
|
|
/**
|
|
* Maps an already-validated resolution onto the provider's response shape.
|
|
* `parseHarnessRuntimeRequestResolution` is the only gate on shape, so every
|
|
* branch here answers a resolution the request kind actually accepts.
|
|
*/
|
|
export function runtimeRequestResponse(
|
|
request: HarnessRuntimeRequest,
|
|
resolution: HarnessRuntimeRequestResolution,
|
|
responseContext: CodexQuestionResponseContext,
|
|
): Record<string, unknown> {
|
|
// The durable transport sends this canonical resolution to the ACPX sidecar,
|
|
// which owns conversion back to the original provider form values.
|
|
if (isAcpxCanonicalInputMethod(request.method) || request.method === "session/request_permission") return structuredClone(resolution);
|
|
if (
|
|
request.requestKind === "command_approval" ||
|
|
request.requestKind === "file_approval"
|
|
) {
|
|
if (resolution.action === "submit") {
|
|
throw new Error(
|
|
`${request.requestKind} does not accept submitted form data`,
|
|
);
|
|
}
|
|
const decisions = {
|
|
accept: "accept",
|
|
accept_for_session: "acceptForSession",
|
|
decline: "decline",
|
|
cancel: "cancel",
|
|
} as const;
|
|
return { decision: decisions[resolution.action] };
|
|
}
|
|
if (request.requestKind === "permission_approval" && request.method === "mcpServer/elicitation/request") {
|
|
if (!["accept", "decline", "cancel"].includes(resolution.action)) throw new Error("MCP tool approval requires a one-time decision");
|
|
return { action: resolution.action, content: null, _meta: null };
|
|
}
|
|
if (request.requestKind === "permission_approval") {
|
|
if (resolution.action === "submit") {
|
|
throw new Error(
|
|
"permission approval does not accept submitted form data",
|
|
);
|
|
}
|
|
return {
|
|
permissions: {},
|
|
scope: resolution.action === "accept_for_session" ? "session" : "turn",
|
|
};
|
|
}
|
|
if (request.requestKind === "user_input") {
|
|
if (resolution.action === "submit" && "response" in resolution) {
|
|
return {
|
|
answers: canonicalCodexAnswers(
|
|
request,
|
|
resolution.response,
|
|
responseContext,
|
|
),
|
|
};
|
|
}
|
|
if (resolution.action !== "submit" || !("answers" in resolution)) {
|
|
// Declines and cancels are the only non-submit answers the validator
|
|
// lets through, and neither carries form data.
|
|
return { answers: {} };
|
|
}
|
|
return { answers: structuredClone(resolution.answers) };
|
|
}
|
|
if (resolution.action === "submit" && "response" in resolution) {
|
|
return {
|
|
action: "accept",
|
|
content: canonicalElicitationContent(
|
|
request,
|
|
resolution.response,
|
|
responseContext,
|
|
),
|
|
_meta: null,
|
|
};
|
|
}
|
|
if (resolution.action === "submit" && "content" in resolution) {
|
|
return {
|
|
action: "accept",
|
|
content: structuredClone(resolution.content),
|
|
_meta: null,
|
|
};
|
|
}
|
|
if (
|
|
resolution.action === "submit" ||
|
|
resolution.action === "accept_for_session"
|
|
) {
|
|
throw new Error("elicitation submissions require content");
|
|
}
|
|
return { action: resolution.action, content: null, _meta: null };
|
|
}
|