mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 19:08:33 +01:00
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>
This commit is contained in:
3 files changed
+53
-8
No files matched your search
@@ -1358,6 +1358,35 @@ describe("PaperclipRunnerToolAuthority", () => {
|
||||
).rejects.toThrow("paperclip_runner_tool_idempotency_conflict");
|
||||
});
|
||||
|
||||
it("honors the revision limit and returns a read too large to journal as a small named tool error", async () => {
|
||||
// A Sentry case document accumulates many large revisions. Its full
|
||||
// history once exceeded the PRP command bound, so the run disconnected
|
||||
// and restart recovery reran the same read and failed the run.
|
||||
const authority = new PaperclipRunnerToolAuthority(db, { companyId, agentId, issueId, runId });
|
||||
let baseRevisionId: string | null = null;
|
||||
for (let revision = 1; revision <= 5; revision += 1) {
|
||||
const written = await authority.execute({ tool: "write_document", callId: `history-${revision}`, arguments: {
|
||||
idempotencyKey: `history-${revision}`, key: "large-history", title: "Case", baseRevisionId,
|
||||
body: `Revision ${revision}\n${"evidence ".repeat(18_000)}`, changeSummary: `Revision ${revision}`,
|
||||
} }) as { document: { latestRevisionId: string } };
|
||||
baseRevisionId = written.document.latestRevisionId;
|
||||
}
|
||||
|
||||
const latest = await authority.execute({ tool: "list_document_revisions", callId: "latest-revision",
|
||||
arguments: { key: "large-history", limit: 1 } }) as { revisions: Array<{ revisionNumber: number }> };
|
||||
expect(latest.revisions.map(revision => revision.revisionNumber)).toEqual([5]);
|
||||
const recent = await authority.execute({ tool: "list_document_revisions", callId: "recent-revisions",
|
||||
arguments: { key: "large-history", limit: 2 } }) as { revisions: Array<{ revisionNumber: number }> };
|
||||
expect(recent.revisions.map(revision => revision.revisionNumber)).toEqual([5, 4]);
|
||||
|
||||
const oversized = { tool: "list_document_revisions", callId: "whole-history", arguments: { key: "large-history" } };
|
||||
await expect(authority.execute(oversized)).rejects.toThrow(/^paperclip_runner_read_result_too_large: The list_document_revisions result is \d+ bytes/);
|
||||
const recovered = await authority.recover(oversized);
|
||||
expect(recovered).toMatchObject({ isError: true, result: { success: false,
|
||||
contentItems: [{ type: "inputText", text: expect.stringContaining("paperclip_runner_read_result_too_large") }] } });
|
||||
expect(Buffer.byteLength(JSON.stringify(recovered))).toBeLessThan(1_000);
|
||||
});
|
||||
|
||||
it("settles a write_document a restart orphaned from its transactional receipt, applying it at most once", async () => {
|
||||
// A restart can cut a write_document callback after the runner journaled
|
||||
// its input but before any result was saved. The revision and its receipt
|
||||
|
||||
@@ -204,7 +204,18 @@ function nativeToolAllowedInMode(operationId: string, workMode: string): boolean
|
||||
const RECOVERABLE_READ_ERRORS = new Set([
|
||||
"paperclip_runner_document_not_found", "paperclip_runner_agent_not_found",
|
||||
"paperclip_runner_approval_not_found", "paperclip_runner_tool_input_invalid",
|
||||
"paperclip_runner_read_result_too_large",
|
||||
]);
|
||||
// A tool result travels as one durable PRP command. A document read larger than
|
||||
// that bound cannot be journaled: it would disconnect the run, and restart
|
||||
// recovery would read it again and fail the run. Return a small named error.
|
||||
function boundedDocumentRead<T>(tool: string, result: T): T {
|
||||
const bytes = Buffer.byteLength(JSON.stringify(result) ?? "");
|
||||
if (bytes <= RUNNER_TOOL_RESULT_MAX_BYTES) return result;
|
||||
throw new Error(`paperclip_runner_read_result_too_large: The ${tool} result is ${bytes} bytes, above the `
|
||||
+ `${RUNNER_TOOL_RESULT_MAX_BYTES}-byte limit for one tool result.`
|
||||
+ (tool === "list_document_revisions" ? " Request fewer revisions with a smaller limit, such as limit: 1." : ""));
|
||||
}
|
||||
const idSimilarity = (left: string, right: string) => {
|
||||
const a = left.toLowerCase(), b = right.toLowerCase();
|
||||
let prefix = 0;
|
||||
@@ -1125,21 +1136,24 @@ export class PaperclipRunnerToolAuthority {
|
||||
};
|
||||
}
|
||||
case "list_documents":
|
||||
return { documents: await this.#visibleMuseDocuments(context.issue,
|
||||
await documentService(this.db).listIssueDocuments(this.binding.issueId)) };
|
||||
return boundedDocumentRead(call.tool, { documents: await this.#visibleMuseDocuments(context.issue,
|
||||
await documentService(this.db).listIssueDocuments(this.binding.issueId)) });
|
||||
case "read_document": {
|
||||
const document = await documentService(this.db).getIssueDocumentByKey(this.binding.issueId, requiredString(input.key));
|
||||
if (!document) throw new Error("paperclip_runner_document_not_found");
|
||||
const [visible] = await this.#visibleMuseDocuments(context.issue, [document]);
|
||||
return { document: visible };
|
||||
return boundedDocumentRead(call.tool, { document: visible });
|
||||
}
|
||||
case "list_document_revisions": {
|
||||
const key = requiredString(input.key);
|
||||
const service = documentService(this.db);
|
||||
const revisions = await service.listIssueDocumentRevisions(this.binding.issueId, key);
|
||||
if (!this.binding.museRuntime || revisions.length === 0) return { revisions };
|
||||
// Honor the advertised limit (default 50, maximum 200), newest first.
|
||||
const revisions = await service.listIssueDocumentRevisions(this.binding.issueId, key,
|
||||
{ limit: boundedLimit(input.limit, 50, 200) });
|
||||
if (!this.binding.museRuntime || revisions.length === 0) return boundedDocumentRead(call.tool, { revisions });
|
||||
const document = await service.getIssueDocumentByKey(this.binding.issueId, key);
|
||||
return { revisions: await this.#projectMuseDocumentContent(context.issue, revisions, document ? [document] : []) };
|
||||
return boundedDocumentRead(call.tool,
|
||||
{ revisions: await this.#projectMuseDocumentContent(context.issue, revisions, document ? [document] : []) });
|
||||
}
|
||||
case "write_document": return this.#writeDocument(input);
|
||||
case "list_agents":
|
||||
|
||||
Reference in new issue
Block a user