Files
PaperClipAI/server/src/__tests__/heartbeat-context-summary.test.ts
T
Dotta 51ad751e0b feat(runner): integrate Codex native execution (#12616)
## Thinking Path

> - Paperclip is the open source control plane for teams of AI agents.
> - Agent runs currently use direct adapters and their established
finalization paths.
> - The new runner package needs one production integration before it
can execute a real provider through the server.
> - That integration must not change direct adapters or expose
unsupported providers.
> - The rollout must also preserve native runs that were already
recorded when the feature flag changes.
> - This pull request adds a default-off, Codex-only native execution
path and its authority boundary.
> - The benefit is a recoverable production vertical slice with explicit
compatibility guards.

## Linked Issues or Issue Description

**Subsystem affected**

Cross-cutting server orchestration and adapter selection.

**Problem or motivation**

The runner package exists, but the server cannot yet start and recover a
governed Codex run through it. A careless integration could also route
existing direct adapters into the native runtime or lose cancellation
and finalization state.

**Proposed solution**

Add a hidden `paperclip_runner` adapter for Codex. Keep it behind the
default-off instance flag. Bind native execution, resume, cancellation,
semantic tool authority, and finalization to the recorded company,
issue, run, and coordinator identities. Leave every direct adapter on
its existing path.

**Alternatives considered**

A multi-provider launch was rejected because only Codex has the complete
production bridge in this series. Replacing direct adapter execution was
rejected because the runner remains experimental.

**Roadmap alignment**

This work supports governed tool access, action attribution, and
self-healing runs. It keeps the integration narrow and default-off.

## What Changed

- Add the Codex-only native session executor and persisted resumption
path.
- Add run-scoped semantic tool projection, authorization, receipts, and
idempotency.
- Add audited native cancellation with durable issue and coordinator
binding.
- Add result fencing so a recorded result cannot reacquire the provider
and run twice.
- Reject fresh runner starts when the rollout flag is off while
preserving recorded native recovery.
- Keep direct adapters outside native status, cancellation, record
creation, and finalization.
- Add focused conformance, recovery, cancellation, status, portability,
and compatibility coverage.

## Verification

- GitHub Actions is the authoritative test environment for this large
stack.
- The PR policy and lightweight stack checks run while this is a middle
PR.
- The full required suite runs when this PR becomes the lowest unmerged
or top PR.
- Greptile will review this exact delta after the branch is pushed.

## Risks

- The main risk is routing a legacy adapter into native execution.
Runtime selection and heartbeat tests cover that boundary.
- The next risk is stale or cross-company cancellation. Durable binding
checks and transactional audit persistence cover it.
- The adapter remains hidden and default-off. Only Codex is admitted.
- There are no database migration, lockfile, or GitHub workflow changes
in this PR.

## Stack

1. [Runner package, SDK, and developer
tools](https://github.com/paperclipai/paperclip/pull/12608)
2. This PR: Codex production server integration
3. [Provider-neutral task-thread
UI](https://github.com/paperclipai/paperclip/pull/12617)

## Model Used

OpenAI Codex with GPT-5, extended reasoning, repository tools, and
parallel review agents.

## 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
2026-08-31 22:51:17 -05:00

381 lines
12 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
buildPaperclipTaskMarkdown,
mergeCoalescedContextSnapshot,
summarizeHeartbeatRunContextSnapshot,
summarizeHeartbeatRunListResultJson,
} from "../services/heartbeat.js";
describe("buildPaperclipTaskMarkdown", () => {
it("adds planning directives for assignment and comment task context", () => {
const assignment = buildPaperclipTaskMarkdown({
issue: {
id: "issue-1",
identifier: "PAP-3404",
title: "Plan first",
workMode: "planning",
description: null,
},
});
expect(assignment).toContain("- Work mode: \"planning\"");
expect(assignment).toContain("Make the plan only. Do not write code or perform implementation work.");
const commentWake = buildPaperclipTaskMarkdown({
issue: {
id: "issue-1",
identifier: "PAP-3404",
title: "Plan first",
workMode: "planning",
description: null,
},
wakeComment: {
id: "comment-1",
body: "Please revise the plan.",
},
});
expect(commentWake).toContain("Update the plan only. Do not write code or perform implementation work.");
const acceptedConfirmation = buildPaperclipTaskMarkdown({
issue: {
id: "issue-1",
identifier: "PAP-3404",
title: "Plan first",
workMode: "planning",
description: null,
},
interaction: {
kind: "request_confirmation",
status: "accepted",
},
});
expect(acceptedConfirmation).toContain(
"Implement the accepted plan on this issue when the work is small and cohesive.",
);
expect(acceptedConfirmation).not.toContain("Make the plan only.");
});
it("adds accepted-plan continuation guidance for standard-work issues when the wake is flagged as a plan continuation", () => {
const acceptedConfirmation = buildPaperclipTaskMarkdown({
issue: {
id: "issue-2",
identifier: "PAP-415",
title: "Implement the fix",
workMode: "standard",
description: null,
},
acceptedPlanContinuation: true,
});
expect(acceptedConfirmation).toContain("Accepted plan directive:");
expect(acceptedConfirmation).toContain(
"Implement the accepted plan on this issue when the work is small and cohesive.",
);
expect(acceptedConfirmation).not.toContain("- Work mode: \"planning\"");
});
it("adds answer-only guidance for ask-mode issues", () => {
const assignment = buildPaperclipTaskMarkdown({
issue: {
id: "issue-ask",
identifier: "PAP-416",
title: "Explain the tradeoff",
workMode: "ask",
description: null,
},
});
expect(assignment).toContain("- Work mode: \"ask\"");
expect(assignment).toContain("Ask mode directive:");
expect(assignment).toContain("Answer the question directly in the issue thread.");
expect(assignment).toContain("Do not write implementation code");
expect(assignment).toContain("do not produce an implementation plan");
});
it("adds dry-run containment guidance for skill-test issues", () => {
const assignment = buildPaperclipTaskMarkdown({
issue: {
id: "issue-skill-test",
identifier: "PAP-417",
title: "Test skill draft",
workMode: "skill_test",
description: null,
},
});
expect(assignment).toContain("- Work mode: \"skill_test\"");
expect(assignment).toContain("Skill test mode directive:");
expect(assignment).toContain("Make no durable changes outside this issue.");
expect(assignment).toContain("Write your final output as issue document `output`");
});
it("strips the description for the compact resume variant but keeps directives and the wake comment", () => {
const input = {
issue: {
id: "issue-1",
identifier: "PAP-3404",
title: "Ship the fix",
workMode: "standard",
description: "Full multi-paragraph brief that the session already received.",
},
wakeComment: {
id: "comment-1",
body: "Please also update the changelog.",
},
};
const full = buildPaperclipTaskMarkdown(input);
expect(full).toContain("Issue description:");
expect(full).toContain("Full multi-paragraph brief that the session already received.");
const compact = buildPaperclipTaskMarkdown({ ...input, includeDescription: false });
expect(compact).not.toContain("Issue description:");
expect(compact).not.toContain("Full multi-paragraph brief");
expect(compact).toContain("- Issue: \"PAP-3404\"");
expect(compact).toContain("Please also update the changelog.");
});
it("makes the latest wake comment the immediate follow-up request", () => {
const commentWake = buildPaperclipTaskMarkdown({
issue: {
id: "issue-follow-up",
identifier: "PAP-418",
title: "Original task",
workMode: "standard",
description: "Reply with the original answer.",
},
wakeComment: {
id: "comment-follow-up",
body: "Reply with the new answer instead.",
},
});
expect(commentWake).toContain("The latest wake comment is the immediate request for this run.");
expect(commentWake).toContain("Do not repeat an earlier requested output from the issue description");
expect(commentWake).toContain("Reply with the new answer instead.");
});
it("prefers ordinary comment planning guidance over stale accepted confirmation state", () => {
const commentWake = buildPaperclipTaskMarkdown({
issue: {
id: "issue-1",
identifier: "PAP-3404",
title: "Plan first",
workMode: "planning",
description: null,
},
wakeComment: {
id: "comment-1",
body: "Please revise the plan.",
},
interaction: {
kind: "request_confirmation",
status: "accepted",
},
});
expect(commentWake).toContain("Update the plan only. Do not write code or perform implementation work.");
expect(commentWake).not.toContain("Create child issues from the approved plan only");
});
});
describe("mergeCoalescedContextSnapshot", () => {
it("clears stale accepted-plan interaction state when merging a later ordinary comment wake", () => {
const merged = mergeCoalescedContextSnapshot(
{
issueId: "issue-1",
interactionId: "interaction-1",
interactionKind: "request_confirmation",
interactionStatus: "accepted",
continuationPolicy: "wake_assignee_on_accept",
checkboxSelection: {
prompt: "Delete selected files?",
selectedOptionIds: ["file-b"],
selectedOptions: [{ id: "file-b", label: "b.txt", description: "Generated build output" }],
},
wakeReason: "issue_commented",
},
{
issueId: "issue-1",
commentId: "comment-1",
wakeCommentId: "comment-1",
wakeReason: "issue_commented",
},
);
expect(merged.interactionId).toBeUndefined();
expect(merged.interactionKind).toBeUndefined();
expect(merged.interactionStatus).toBeUndefined();
expect(merged.continuationPolicy).toBeUndefined();
expect(merged.checkboxSelection).toBeUndefined();
expect(merged.commentId).toBe("comment-1");
expect(merged.wakeCommentId).toBe("comment-1");
});
it("preserves resolved interaction state for the interaction wake itself", () => {
const merged = mergeCoalescedContextSnapshot(
{
issueId: "issue-1",
},
{
issueId: "issue-1",
interactionId: "interaction-1",
interactionKind: "request_confirmation",
interactionStatus: "accepted",
continuationPolicy: "wake_assignee_on_accept",
checkboxSelection: {
prompt: "Delete selected files?",
selectedOptionIds: ["file-b"],
selectedOptions: [{ id: "file-b", label: "b.txt", description: "Generated build output" }],
},
wakeReason: "issue_commented",
},
);
expect(merged.interactionId).toBe("interaction-1");
expect(merged.interactionKind).toBe("request_confirmation");
expect(merged.interactionStatus).toBe("accepted");
expect(merged.continuationPolicy).toBe("wake_assignee_on_accept");
expect(merged.checkboxSelection).toEqual({
prompt: "Delete selected files?",
selectedOptionIds: ["file-b"],
selectedOptions: [{ id: "file-b", label: "b.txt", description: "Generated build output" }],
});
});
it("preserves a deferred interaction when a later comment joins its successor wake", () => {
const merged = mergeCoalescedContextSnapshot(
{
issueId: "issue-1",
interactionId: "interaction-1",
interactionKind: "request_confirmation",
interactionStatus: "accepted",
continuationPolicy: "wake_assignee_on_accept",
wakeReason: "issue_commented",
},
{
issueId: "issue-1",
commentId: "comment-1",
wakeCommentId: "comment-1",
wakeReason: "issue_commented",
},
{ preserveExistingInteractionContinuation: true },
);
expect(merged.interactionId).toBe("interaction-1");
expect(merged.interactionKind).toBe("request_confirmation");
expect(merged.interactionStatus).toBe("accepted");
expect(merged.continuationPolicy).toBe("wake_assignee_on_accept");
expect(merged.commentId).toBe("comment-1");
expect(merged.wakeCommentId).toBe("comment-1");
});
it("keeps a queued comment when an interaction joins its successor wake", () => {
const merged = mergeCoalescedContextSnapshot(
{
issueId: "issue-1",
commentId: "comment-1",
wakeCommentId: "comment-1",
wakeCommentIds: ["comment-1"],
wakeReason: "issue_commented",
},
{
issueId: "issue-1",
interactionId: "interaction-1",
interactionKind: "ask_user_questions",
interactionStatus: "answered",
continuationPolicy: "wake_assignee_on_accept",
wakeReason: "issue_commented",
},
{ preserveExistingInteractionContinuation: true },
);
expect(merged.interactionId).toBe("interaction-1");
expect(merged.interactionKind).toBe("ask_user_questions");
expect(merged.interactionStatus).toBe("answered");
expect(merged.commentId).toBe("comment-1");
expect(merged.wakeCommentId).toBe("comment-1");
expect(merged.wakeCommentIds).toEqual(["comment-1"]);
});
});
describe("summarizeHeartbeatRunContextSnapshot", () => {
it("keeps only the small retry/linking fields needed by the client", () => {
const summarized = summarizeHeartbeatRunContextSnapshot({
issueId: "issue-1",
taskId: "task-1",
taskKey: "PAP-1",
commentId: "comment-1",
wakeCommentId: "comment-2",
wakeReason: "retry_failed_run",
wakeSource: "on_demand",
wakeTriggerDetail: "manual",
paperclipWake: {
comments: [
{
body: "x".repeat(50_000),
},
],
},
executionStage: {
summary: "large nested object that should not be sent back in run lists",
},
});
expect(summarized).toEqual({
issueId: "issue-1",
taskId: "task-1",
taskKey: "PAP-1",
commentId: "comment-1",
wakeCommentId: "comment-2",
wakeReason: "retry_failed_run",
wakeSource: "on_demand",
wakeTriggerDetail: "manual",
});
});
it("returns null when no allowed fields are present", () => {
expect(
summarizeHeartbeatRunContextSnapshot({
paperclipWake: { comments: [{ body: "hello" }] },
}),
).toBeNull();
});
});
describe("summarizeHeartbeatRunListResultJson", () => {
it("keeps only summary fields and parses numeric cost aliases", () => {
expect(
summarizeHeartbeatRunListResultJson({
summary: "Completed the task",
result: "Updated three files",
message: "",
error: null,
totalCostUsd: "1.25",
costUsd: "0.75",
costUsdCamel: "0.5",
}),
).toEqual({
summary: "Completed the task",
result: "Updated three files",
total_cost_usd: 1.25,
cost_usd: 0.75,
costUsd: 0.5,
});
});
it("returns null when projected fields are empty", () => {
expect(
summarizeHeartbeatRunListResultJson({
summary: "",
result: null,
message: undefined,
error: " ",
totalCostUsd: "abc",
}),
).toBeNull();
});
});