Files
PaperClipAI/server/src/__tests__/heartbeat-context-summary.test.ts
DottaandClaude Fable 5 b996b71a38 Deduplicate wake-payload issue descriptions and compact resume deltas (#10216)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Heartbeat wake payloads and the task-context markdown are the two
channels that deliver an issue's brief into an agent's prompt
> - #10151 fixed wake-prompt-only adapter lanes waking without the issue
description by adding it to the structured wake payload
> - That left the description delivered twice per prompt on lanes that
also inject the task-context markdown, and re-delivered in full on every
resume wake, permanently bloating persistent-session context
> - This pull request makes the task markdown the single description
carrier on lanes that use it, and omits the description from
non-assignment resume deltas on all lanes while keeping it for
assignment-shaped and recovery wakes
> - The benefit is that every lane receives the brief exactly once when
it needs it, and long-lived sessions stop re-paying the full brief in
tokens on every wake

## Linked Issues or Issue Description

Refs #10151

Related prior work: #2883, #8402 (earlier description-delivery attempts
referenced by #10151). I searched the PR list for open work on
wake-payload description handling and found none besides the merged
#10151.

**Bug:** After #10151, adapters that inject the `Paperclip task context`
markdown (ACPX engine lanes, claude-local CLI, hermes server and
gateway) receive the issue description twice in a single prompt — once
in the wake prompt's `Issue description:` block and once in the task
markdown. Separately, resume deltas re-send the full description (up to
12k characters) on every wake even though the persistent session already
received it.

**Expected behavior:** The description appears exactly once per prompt
on every lane, and resume deltas only carry it when the resuming session
may not have seen the brief (assignment-shaped or recovery wakes),
leaving an explicit fetch breadcrumb otherwise.

**Reproduction:** Wake a claude-local or ACPX agent on an issue with a
description and inspect the assembled prompt: the description text
appears in both the wake-payload block and the task-context block. Wake
the same session again via a comment: the full description is present
again in the resume delta.

**Affected version:** Current `master` (with #10151 merged).

**Deployment mode:** Adapter-backed heartbeat execution, local and
sandboxed lanes.

## What Changed

- `renderPaperclipWakePrompt` accepts `suppressIssueDescription`; the
four task-markdown lanes pass it so the task markdown stays the single,
uncapped description carrier there.
- Non-assignment resume deltas omit the description and emit `- issue
description: omitted from this resume delta; fetch the issue if you need
the latest brief`. Assignment-shaped reasons (`issue_assigned`,
`issue_reopened_via_comment`, `issue_recovery_action_restored`,
`issue_tree_restored`) and recovery wakes still deliver the full brief.
- `buildPaperclipTaskMarkdown` gains `includeDescription`; the server
now also publishes `context.paperclipTaskMarkdownCompact` (description
stripped, directives and wake comment kept), and the new
`selectPaperclipTaskMarkdown` helper picks the right variant under the
same resume rules, falling back to the full markdown when no compact
variant exists (version skew safety).
- The wake prompt's description block now carries the same user-authored
trust framing the task markdown already had.

## Verification

- `npx vitest run packages/adapter-utils/src/server-utils.test.ts
packages/adapters/claude-local/src/server/acp.test.ts
packages/adapters/codex-local/src/server/acp.test.ts
server/src/__tests__/heartbeat-context-summary.test.ts` — 137 tests
passed, including new coverage for suppression, resume omission plus
breadcrumb, assignment-shaped resume inclusion, compact-variant
building, variant selection, and an end-to-end ACPX prompt-assembly test
asserting the description appears exactly once on fresh wakes and not at
all on comment resumes.
- `npx vitest run` in `packages/adapters/hermes` — 59 tests passed,
including a gateway execute-level test asserting the brief is sent
exactly once on fresh runs and not re-sent on stable-session resumes.
- `tsc --noEmit` in `packages/adapter-utils`,
`packages/adapters/claude-local`, `packages/adapters/hermes` — clean;
`server` matches the `master` baseline exactly (pre-existing plugin-sdk
resolution errors only, none in touched files).
- Pre-existing failures confirmed identical on clean `master`:
claude-local `execute.remote.test.ts` / `test.probe.test.ts`,
adapter-utils `mcp-isolation.integration.test.ts` (requires a newer
local Claude CLI).

## Risks

- Behavioral shift, prompt-only: a resumed session woken by a comment on
an issue it never handled (rare — assignment wakes normally precede
comment wakes) would not get the inline description; the breadcrumb plus
the standard issue-fetch path covers it.
- Additive context key (`paperclipTaskMarkdownCompact`); older adapters
ignore it and newer adapters fall back to the full markdown when it is
absent, so mixed-version deployments degrade to current behavior.
- No schema, migration, or API changes; the structured wake-payload JSON
shape is unchanged.
- Known follow-up deliberately out of scope: openclaw embeds the raw
wake-payload JSON (which still contains the description) in prompt text
for machine parsing. The hermes-gateway lane is handled: it detects
stable-session resumes (issue/agent session-key strategy plus a stored
prior session id), compacts the task markdown, and omits the description
from its prompt-embedded JSON copy.

> This is a focused correctness/efficiency fix to existing wake plumbing
and does not overlap with planned roadmap feature work.

## Model Used

- Anthropic Claude Fable 5 (`claude-fable-5`), extended thinking
enabled, with repository tool use, shell execution, and local test
execution via Claude Code.

## 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)
- [ ] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
(execution-workspace branch, same convention as merged #10202)
- [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
(code-level docs; no user-facing docs affected)
- [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

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 16:35:07 -05:00

302 lines
9.6 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("Create child issues from the approved plan only");
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("Create child issues from the approved plan only");
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("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" }],
});
});
});
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();
});
});