Files
PaperClipAI/server
20ffa6b0a3 fix(prompt): reference the exact plan revision on resumed turns (#17769)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Each heartbeat run gives the agent task Markdown, and a task can
have a revisioned plan document
> - A resumed session gets a compact variant of that Markdown, because
the session already received the assignment brief
> - The compact variant already leaves out the issue description, but it
still repeats the full plan body on every resumed wake
> - A long plan therefore grows every resumed prompt, although the
session already has the plan
> - This pull request keeps the exact plan document and revision pointer
in the compact variant and drops the body
> - The benefit is smaller resumed prompts without a weaker plan
contract: the agent reads that exact revision when it does not have it

## Linked Issues or Issue Description

No public issue exists for this. I searched open and closed issues and
pull requests for the task plan in resumed prompts and for changes to
`server/src/services/heartbeat/task-markdown.ts` and `task-context.ts`.
Related prompt-size work: #17584 (skip static notes on resumed ACPX
sessions), #17342 and #16645 (bound the execution continuation).

**What existing behavior does this improve?**

`buildPaperclipTaskMarkdown` with `includeDescription: false` builds the
compact task Markdown for resumed sessions. It still writes the full
`Task plan document …` body in a fenced block on every resumed wake.

**Subsystem affected**

Heartbeat task context
(`server/src/services/heartbeat/task-markdown.ts`,
`server/src/services/heartbeat/task-context.ts`).

**Current behavior**

Every resumed wake repeats the full plan body, even when the plan did
not change and the session already holds it.

**Proposed behavior**

The compact variants keep the line `Task plan document <id>, revision
<n> (<revisionId>):` and the work-mode rule. In place of the body, they
tell the agent to read that exact revision through `GET
/api/issues/{id}/documents/plan` (or its `/revisions` list) when the
session does not have it, and not to use a later draft or infer
approval. The full variants do not change.

**Reason and benefit**

Resumed prompts stay smaller. A changed plan still shows a new revision
id, so the agent can see that it must read the new revision.

## What Changed

- `buildPaperclipTaskMarkdown` accepts `includePlanBody`. When it is
`false`, the plan section keeps the document and revision pointer and
replaces the fenced body with the read instruction.
- `prepareHeartbeatTaskContext` passes `includePlanBody: false` only for
`paperclipTaskMarkdownCompact` and
`paperclipTaskMarkdownAssignmentCompact`. Fresh attempts and
assignment-shaped wakes use the full variants and still get the body.
- A quarantined low-trust plan seen by a higher-trust agent keeps its
withheld-body notice in the compact variants instead of the read
instruction, because the issue document routes return the stored body
without the quarantine filter.
- Add unit tests for the compact plan section in all work modes, for a
changed revision and for rejected-plan rules.
- Add a database-backed test that runs a real heartbeat with a plan
document and checks all four task Markdown variants. Another one checks
all four variants for a quarantined plan.

## Verification

On the head rebased onto `master` `7e8901aa60` (the later rebase onto
`7cd5558e41` did not change any file this PR touches):

- `npx vitest run src/__tests__/heartbeat-context-summary.test.ts
src/__tests__/heartbeat-task-context.test.ts` in `server`: 2 files, 70
tests passed (with embedded PostgreSQL).
- Without the source change, the two new plan tests fail (checked before
the rebase). Without the quarantine change, the quarantined-plan test
fails.
- `pnpm --filter @paperclipai/server typecheck` (all layers, including
the test trees): no errors. `pnpm check:tokens` and `pnpm
check:module-boundaries` pass.

## Risks

- A resumed session that lost the plan from its context must call the
API to read it. The prompt names the exact endpoint and revision.
- Approval and work-mode rules do not change. The rejected-plan text
stays in the compact variant.

> 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

AI-assisted with Claude Code and OpenAI Codex coding agents; exact model
versions are not listed.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [ ] 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 (none
needed: no documented behavior or setup 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

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: a-vasyliev <1255946+a-vasyliev@users.noreply.github.com>
2026-12-05 02:24:03 -08:00
..