Skip to content

The open-source app everyone uses to manage agents at work

16.2 KB
Raw Blame
/** Provider-neutral semantic completion tools and their strict model-facing schemas. */
export const PRP_COMPLETION_TOOL_NAME = "paperclip_finish" as const;
export const PRP_BLOCK_TOOL_NAME = "paperclip_block" as const;
/** Native Runner guidance; legacy adapters use their own skill/API completion paths. */
export const PRP_COMPLETION_TOOL_DESCRIPTION =
  "Report completed work (done), work requiring review (needs_review), or an explicit wait (yielded with response_wake for a response, monitor after set_task_monitor confirms a schedule on this task, or omit continuation to use the saved repository-preparation continuation). Use the current completion contract and supporting evidence. If rejected, correct the report and retry. After acceptance, read the returned outcome; do not claim completion while gated. Explain any required approval with its supplied link and action, then write the final response and end the turn without further tool calls.";
export const PRP_BLOCK_TOOL_DESCRIPTION =
  "Report work that cannot continue because of a concrete blocker. Routine repository preparation is not a blocker: use paperclip_finish with yielded and omit continuation to resume through the saved workspace continuation. Identify the blocker, its owner, and the action needed to unblock it; use the current completion contract and supporting evidence. If rejected, correct the report and retry. After acceptance, read the returned outcome, explain the blocker and any required action in the final response, and end the turn without further tool calls.";
export const PRP_SEMANTIC_TOOL_NAMES = [
  PRP_COMPLETION_TOOL_NAME,
  PRP_BLOCK_TOOL_NAME,
] as const;

export const PRP_VERIFICATION_REASON_CODES = [
  "environment_unavailable",
  "tool_unavailable",
  "dependency_missing",
  "permission_denied",
  "credential_missing",
  "policy_restricted",
  "external_service_unavailable",
  "budget_exhausted",
  "other",
] as const;

export const PRP_ACTIONABLE_ATTENTION_KINDS = [
  "review",
  "human_input",
  "approval",
  "credential",
  "external_action",
  "agent_handoff",
] as const;

export const PRP_ATTENTION_OWNER_CLASSES = [
  "human",
  "agent",
  "external_system",
] as const;

const completionClaimSchema = {
  type: "object",
  additionalProperties: false,
  required: ["contractRevision", "objectiveSatisfied", "criteria", "remainingWork"],
  properties: {
    contractRevision: { type: "string", minLength: 1, description: "Use the current turn completion.revision (or completionContract.revision on the first turn), never a previous turn’s revision. On stale-revision feedback, reassess the current request and correct the report without repeating completed work." },
    objectiveSatisfied: { type: "boolean" },
    criteria: {
      type: "array",
      items: {
        type: "object",
        additionalProperties: false,
        required: ["criterionId", "status", "evidenceRefs"],
        properties: {
          criterionId: { type: "string", minLength: 1 },
          status: { enum: ["satisfied", "not_satisfied", "unknown"] },
          evidenceRefs: { type: "array", items: { type: "string" } },
        },
      },
    },
    remainingWork: {
      type: "array",
      items: {
        type: "object",
        additionalProperties: false,
        required: ["description", "blocksCompletion"],
        properties: {
          description: { type: "string", minLength: 1 },
          blocksCompletion: { type: "boolean" },
        },
      },
    },
  },
} as const;

const evidenceSchema = {
  type: "array",
  items: {
    type: "object",
    additionalProperties: false,
    required: ["ref"],
    properties: { ref: { type: "string", minLength: 1 } },
  },
} as const;

const verificationSchema = {
  type: "array",
  description:
    "Checks used to verify the work. Use failed only when the check actually ran and found a defect in the work. If a check could not run or complete because the environment, tool, dependency, permission, credential, policy, external service, or budget was unavailable, use not_run with reasonCode even when the attempted command exited non-zero.",
  items: {
    type: "object",
    additionalProperties: false,
    required: ["commandOrCheck", "status"],
    properties: {
      commandOrCheck: { type: "string", minLength: 1 },
      status: {
        enum: ["passed", "failed", "not_run"],
        description:
          "passed: the check ran and succeeded. failed: the check ran to a meaningful verdict and found the work incorrect. not_run: the check had no meaningful verdict because it was not attempted or an environment/tool/dependency/permission/credential/policy/service/budget limitation prevented it from completing; command launch or setup failures are not_run.",
      },
      reasonCode: {
        enum: [...PRP_VERIFICATION_REASON_CODES],
        description: "Required for not_run; identify why the check had no meaningful verdict.",
      },
      detail: { type: "string" },
      artifactRef: { type: "string", minLength: 1 },
    },
    allOf: [{
      if: { properties: { status: { const: "not_run" } }, required: ["status"] },
      then: { required: ["reasonCode"] },
    }],
  },
} as const;

const attentionRequestsSchema = {
  type: "array",
  items: {
    type: "object",
    additionalProperties: false,
    required: ["kind", "summary", "ownerClass"],
    properties: {
      kind: { enum: [...PRP_ACTIONABLE_ATTENTION_KINDS] },
      summary: { type: "string", minLength: 1 },
      ownerClass: { enum: [...PRP_ATTENTION_OWNER_CLASSES] },
      targetAgentId: { type: "string", minLength: 1 },
    },
    allOf: [{
      if: { properties: { ownerClass: { const: "agent" } }, required: ["ownerClass"] },
      then: { required: ["targetAgentId"] },
    }],
  },
} as const;

const artifactsSchema = {
  type: "array",
  items: {
    type: "object",
    additionalProperties: false,
    required: ["kind", "ref"],
    properties: {
      kind: { type: "string", minLength: 1 },
      ref: { type: "string", minLength: 1 },
    },
  },
} as const;

const waitContinuationSchema = {
  type: "object",
  description:
    "Required when reportedWorkDisposition is yielded. Use response_wake for a response wait, or monitor only after a real monitor is persisted on the current task. Include kind, summary, and a stable idempotencyKey.",
  additionalProperties: false,
  required: ["kind", "summary", "idempotencyKey"],
  properties: {
    kind: { type: "string", enum: ["response_wake", "monitor", "case_handoff"] },
    summary: {
      type: "string",
      minLength: 1,
      description:
        "Internal control-plane reason to wait for the next response. Keep routine waiting and continuation bookkeeping here, not in the top-level user-facing summary. This field is not the answer to the user's request.",
    },
    idempotencyKey: { type: "string", minLength: 1 },
    handoffId: { type: "string", minLength: 1, description: "Exact server-issued handoff ID; required for case_handoff." },
  },
  allOf: [{ if: { properties: { kind: { const: "case_handoff" } }, required: ["kind"] }, then: { required: ["handoffId"] } }],
} as const;

const commonResultProperties = {
  schema: { type: "string", const: "paperclip.run_result.v1" },
  summary: {
    type: "string",
    minLength: 1,
    description:
      "The complete user-facing answer for this turn. Do not replace the requested answer with routine work/control bookkeeping. Include the requested result and any genuine actionable failure, limitation, or required user action. Unless explicitly requested, omit routine preparation, unconfirmed-delivery, and wait/review status; put the response-wake reason in continuation.summary. Never claim delivery without a confirmed receipt.",
  },
  completionClaim: completionClaimSchema,
  evidence: evidenceSchema,
  verification: verificationSchema,
  attentionRequests: attentionRequestsSchema,
  artifacts: artifactsSchema,
} as const;

export const PRP_COMPLETION_RESULT_OUTPUT_SCHEMA = {
  type: "object",
  additionalProperties: false,
  required: [
    "reportedWorkDisposition",
    "summary",
    "completionClaim",
    "evidence",
    "verification",
    "attentionRequests",
    "artifacts",
  ],
  properties: {
    ...commonResultProperties,
    reportedWorkDisposition: { enum: ["done", "needs_review", "yielded"] },
    continuation: waitContinuationSchema,
  },
  allOf: [
    {
      if: { properties: { reportedWorkDisposition: { const: "done" } }, required: ["reportedWorkDisposition"] },
      then: { properties: { attentionRequests: { maxItems: 0 } } },
    },
    {
      if: { properties: { reportedWorkDisposition: { const: "needs_review" } }, required: ["reportedWorkDisposition"] },
      then: { properties: { attentionRequests: { minItems: 1 } } },
    },
    {
      if: { properties: { reportedWorkDisposition: { const: "yielded" } }, required: ["reportedWorkDisposition"] },
      then: { required: ["continuation"] },
      else: { not: { required: ["continuation"] } },
    },
  ],
} as const;

export const PRP_BLOCK_RESULT_OUTPUT_SCHEMA = {
  type: "object",
  additionalProperties: false,
  required: [
    "reportedWorkDisposition",
    "summary",
    "completionClaim",
    "evidence",
    "verification",
    "attentionRequests",
    "artifacts",
    "blocker",
  ],
  properties: {
    ...commonResultProperties,
    reportedWorkDisposition: { type: "string", const: "blocked" },
    attentionRequests: { type: "array", maxItems: 0, items: attentionRequestsSchema.items },
    blocker: {
      type: "object",
      additionalProperties: false,
      required: ["reasonCode", "owner", "unblockAction", "scope"],
      properties: {
        reasonCode: { type: "string", minLength: 1 },
        owner: {
          type: "object",
          additionalProperties: false,
          required: ["kind", "name"],
          properties: {
            kind: { enum: ["agent", "user", "system", "external"] },
            name: { type: "string", minLength: 1 },
          },
        },
        unblockAction: { type: "string", minLength: 1 },
        scope: { enum: ["current_track", "task_wide"] },
      },
    },
  },
} as const;

const providerVerificationCompatibilitySchema = {
  type: "array",
  description:
    "Checks used to verify the work. Use failed only when the check actually ran and found a defect in the work. If a check could not run or complete because the environment, tool, dependency, permission, credential, policy, external service, or budget was unavailable, use not_run with reasonCode even when the attempted command exited non-zero.",
  items: {
    type: "object",
    additionalProperties: true,
    required: ["status"],
    properties: {
      commandOrCheck: { type: "string", minLength: 1 },
      command: { type: "string", minLength: 1 },
      status: {
        enum: ["passed", "failed", "not_run", "blocked", "skipped", "pass", "success", "succeeded", "fail"],
        description:
          "passed: the check ran and succeeded. failed: the check ran to a meaningful verdict and found the work incorrect. not_run: no meaningful verdict because the check was not attempted or could not complete. Prefer not_run over legacy blocked/skipped, and include reasonCode for any unavailable check.",
      },
      reasonCode: {
        enum: [...PRP_VERIFICATION_REASON_CODES],
        description: "Required for not_run; identify why the check had no meaningful verdict.",
      },
      detail: { type: "string" },
      result: { type: "string" },
      cwd: { type: "string" },
      artifactRef: {
        type: ["string", "null"],
        description: "Reference to a real verification artifact. Omit or use null when no artifact exists; empty values are normalized away.",
      },
    },
  },
} as const;

const providerCompletionClaimCompatibilitySchema = {
  ...completionClaimSchema,
  properties: {
    ...completionClaimSchema.properties,
    criteria: {
      ...completionClaimSchema.properties.criteria,
      items: {
        ...completionClaimSchema.properties.criteria.items,
        properties: {
          ...completionClaimSchema.properties.criteria.items.properties,
          status: {
            enum: [
              "satisfied",
              "not_satisfied",
              "unknown",
              "pass",
              "passed",
              "complete",
              "completed",
              "fail",
              "failed",
              "incomplete",
            ],
            description:
              "Use satisfied when the criterion is met, not_satisfied when it is not met, or unknown when no verdict is available. Common provider aliases are accepted and normalized.",
          },
        },
      },
    },
  },
} as const;

const providerCommonResultProperties = {
  ...commonResultProperties,
  schema: {
    enum: [
      "paperclip.run_result.v1",
      "paperclip_finish",
      "paperclip_paperclip_finish",
      "paperclip_finish.v1",
    ],
  },
  completionClaim: providerCompletionClaimCompatibilitySchema,
} as const;

const providerAttentionCompatibilitySchema = {
  type: "array",
  items: {
    type: "object",
    additionalProperties: true,
    required: ["kind", "summary"],
    properties: {
      kind: { type: "string", minLength: 1 },
      summary: { type: "string", minLength: 1 },
      ownerClass: { enum: [...PRP_ATTENTION_OWNER_CLASSES] },
      targetAgentId: { type: "string", minLength: 1 },
      requestedCapability: { type: "string", minLength: 1 },
      requiredAuthority: { type: "string", minLength: 1 },
      target: { type: "object", additionalProperties: true },
    },
  },
} as const;

/**
 * Provider-facing compatibility schemas. Canonical PRP validation still uses
 * the strict schemas above after legacy aliases have been normalized.
 */
export const PRP_COMPLETION_RESULT_PROVIDER_INPUT_SCHEMA = {
  type: "object",
  additionalProperties: true,
  required: [
    "reportedWorkDisposition",
    "summary",
    "completionClaim",
    "evidence",
    "verification",
  ],
  properties: {
    ...providerCommonResultProperties,
    reportedWorkDisposition: { enum: ["done", "needs_review", "yielded", "completed"] },
    verification: providerVerificationCompatibilitySchema,
    attentionRequests: providerAttentionCompatibilitySchema,
    // Responses-compatible gateways can require every declared property in
    // tool calls. Give non-yielding results an explicit absence value instead
    // of forcing callers to invent a response-wake continuation.
    continuation: {
      ...waitContinuationSchema,
      type: ["object", "null"],
      description:
        "For yielded, omit or use null to reuse this task's eligible saved monitor or repository-preparation continuation; the controller reads current task state. Otherwise provide a real response_wake or monitor with summary and idempotencyKey. Use null or omit for done, completed, or needs_review.",
    },
  },
  // Keep the provider-facing root a concrete object. Codex code-mode renders a
  // root allOf containing only an if/then constraint as `args: unknown`, hiding
  // every required field from the model. This equivalent direct conditional
  // preserves validation without obscuring the object-shaped tool signature.
  if: { properties: { reportedWorkDisposition: { const: "yielded" } }, required: ["reportedWorkDisposition"] },
  then: { properties: { continuation: { type: ["object", "null"] } } },
  else: { properties: { continuation: { type: "null" } } },
} as const;

export const PRP_COMPLETION_RESULT_BRIDGE_INPUT_SCHEMA = {
  ...PRP_COMPLETION_RESULT_PROVIDER_INPUT_SCHEMA,
  properties: {
    ...PRP_COMPLETION_RESULT_PROVIDER_INPUT_SCHEMA.properties,
    continuation: {
      ...PRP_COMPLETION_RESULT_PROVIDER_INPUT_SCHEMA.properties.continuation,
      properties: {
        ...waitContinuationSchema.properties,
        kind: { type: "string", enum: [...waitContinuationSchema.properties.kind.enum, "same_agent"] },
      },
    },
  },
} as const;

export const PRP_BLOCK_RESULT_PROVIDER_INPUT_SCHEMA = {
  type: "object",
  additionalProperties: true,
  required: [
    "reportedWorkDisposition",
    "summary",
    "completionClaim",
    "evidence",
    "verification",
    "blocker",
  ],
  properties: {
    ...providerCommonResultProperties,
    reportedWorkDisposition: { type: "string", const: "blocked" },
    verification: providerVerificationCompatibilitySchema,
    attentionRequests: providerAttentionCompatibilitySchema,
    blocker: PRP_BLOCK_RESULT_OUTPUT_SCHEMA.properties.blocker,
  },
} as const;