Files
PaperClipAI/server
7ce7d99f8a fix(runner): honor document revision limit and bound oversized document reads (#23352)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Native Runner agents read task documents through first-party tools
such as `list_document_revisions`.
> - Each tool result travels to the runner as one durable PRP command. A
command has a bound of about 522 KB.
> - `list_document_revisions` advertises `limit` (default 50, maximum
200), but the server ignored it and returned every revision.
> - A case document with 55 revisions of about 70 KB each gave a result
of about 3.8 MB. The journal refused it with
`command_payload_too_large`. The run disconnected. Restart recovery read
the same revisions again, and the run failed with
`native_session_recovery_failed`.
> - This pull request honors the advertised limit and returns a small
named tool error when a document read is too large for one tool result.
> - The benefit is that a large document history no longer fails the
run. The agent gets a clear error and can ask for fewer revisions.

## Linked Issues or Issue Description

Refs #20727 (classifies the oversized-command failure as a named
terminal; this PR removes one cause of it)

**What happened?**
A native Runner agent called `list_document_revisions` with `{ "key":
"sentry-case", "limit": 1 }`. The server ignored `limit` and returned
all 55 revisions (about 70 KB each). The result was too large for one
durable PRP command. The run disconnected. Recovery read the same result
again and failed the run with `native_session_recovery_failed: Error:
Durable PRP command journal bound exceeded: command_payload_too_large.`

**Steps to reproduce**
1. Write a task document 5 times with bodies of about 160 KB each.
2. As a native Runner agent, call `list_document_revisions` with `limit:
1`.
3. The result contains all 5 revisions (about 800 KB), above the
per-command bound.

**Expected behavior**
The tool returns only the requested number of revisions, newest first. A
document read that cannot fit in one tool result returns a small named
tool error. It does not fail the run.

## What Changed

- `documentService.listIssueDocumentRevisions` accepts an optional
`limit`. It keeps the newest revisions first. The HTTP route does not
pass a limit, so its behavior does not change.
- The native Runner `list_document_revisions` tool honors the advertised
`limit` (default 50, maximum 200).
- The native Runner `list_documents`, `read_document` and
`list_document_revisions` tools return
`paperclip_runner_read_result_too_large` when their result is above the
500,000-byte tool-result bound (`RUNNER_TOOL_RESULT_MAX_BYTES`). The
message tells the agent to request fewer revisions.
- `paperclip_runner_read_result_too_large` is a recoverable read error.
Restart recovery returns it as a small `isError` tool result and does
not fail the run.

## Verification

- Red: without the fix, the new test fails. `limit: 1` returned
revisions `[5, 4, 3, 2, 1]`.
- Green: `cd server && npx vitest run
src/services/native-runtime/paperclip-runner-tool-authority.test.ts`.
All 50 tests pass.
- The new test checks that `limit: 1` and `limit: 2` return the newest
revisions. It also checks that an oversized history gives the named
error from `execute`, and that `recover` returns an `isError` result
smaller than 1 KB.
- `pnpm verify` asked for broader verification (exit 2): its
dependency-graph worker was stopped by local memory pressure. Required
CI supplies typecheck, build and the broad test suites.

## Risks

- Low risk. The change is limited to three native Runner document read
tools.
- An agent that called `list_document_revisions` without `limit` on a
document with more than 50 revisions now gets the newest 50. Before this
change it got all of them. The advertised schema already documented a
default of 50.
- A document read above 500,000 bytes now returns a named tool error.
Before this change, such a read could not be journaled and the run
failed.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) through Claude Code, with tool use
and code execution.

## 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
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] 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

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

Co-authored-by: devinfoley <139239+devinfoley@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 08:46:42 +00:00
..