Repository navigation
fix(agent): page postsListTool results - #2109
Open
giladresisi wants to merge 2 commits into
Open
giladresisi wants to merge 2 commits into
giladresisi wants to merge 2 commits into
Conversation
postsListTool returned every post in the window with full content and settings, and its description told the model to pass a wide window. On a large organization a single tool result grew past the model provider's 10 MB per-output limit and the agent chat failed with context_length_exceeded / "string too long". Results are now sorted by publish date then id and returned 50 per page, with an optional "page" input and additive "total", "page" and "hasMore" outputs. The description and the agent prompt explain paging and prefer narrow windows. getPosts is unchanged, the slice happens in the tool. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Strix Security ReviewNo security issues found. Updated for Reviewed by Strix |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
This was referenced Sep 21, 2026
The paging hint said to repeat only the dates, which would drop a "customer" filter on the following pages. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Closed
1 task
4 of 25 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix (backend, agent / MCP tool
postsListToolinlibraries/nestjs-libraries/src/chat/tools/posts.list.tool.ts). The tool now returns its results 50 posts per page instead of the whole window at once: it sorts the posts bypublishDatethenid, slices the requested page, and adds an optionalpageinput (non-negative integer, default 0) plus additivetotal,pageandhasMoreoutputs. The tool description and the agent prompt line inload.tools.service.tsno longer tell the model to "pass a wide window"; they explain paging and prefer narrow windows. The existing inputs (startDate,endDate,customer) and every existing item field are unchanged, a call withoutpagereturns the first page, andPostsService.getPosts/PostsRepository.getPostsare untouched (the calendar uses them).Why was this change needed?
A Cloud customer with many channels and roughly ten thousand posts reported that the AI agent "is constantly crashing". Sentry shows the agent chat (
/agents/new) failing withcontext_length_exceededandAI_APICallError: Invalid 'input[12].output': string too long. Expected a string with maximum length 10485760, but got a string with length 101152578, i.e. a single tool result of about 101 MB against the model provider's 10 MB per-output limit. The Sentry event does not name the tool;postsListToolis the suspected source because it is the only tool whose output grows with the organization's data: it returned every post in the window with full content and settings, uncapped, and both its description and the agent prompt told the model to pass a wide window (for example a year ahead).getPostsalso expands every recurring post into one item per occurrence inside the window, so a wide window multiplies the row count.Measured on production data (5.7k posts sample): a post is small (content avg 264 chars, p99 1.5 KB, max 2.6 KB; settings max 2.8 KB), so the size comes from the number of rows, not from heavy rows. That is why the fix is a row cap per response and no per-item truncation. A full page of 50 is about 25 KB.
Other information:
getPostshas noorderBy, so the tool sorts before slicing to keep pages stable (posts sharing a publish date are ordered by id).includeAttachments); whichever merges second needs a small conflict resolution.Testing done (local stack, real MCP endpoint, 120 draft posts seeded in one window, pairs of posts sharing the same publish date):
tools/call: pages 0 / 1 / 2 returned 50 / 50 / 20 posts,total120 on every page,hasMoretrue, true, false; page 3 returned an empty list withhasMorefalse. The 120 ids across the pages had no gaps or duplicates and were in order.pagereturned exactly page 0 with all previous item fields.page: -1andpage: 1.5are rejected by validation.ask_postiz): asked for the total and the last post of the window, it paged on its own and answered correctly (120, last post content and date).postsListToolcalls (pages 0, 1, 2) and reported 120 posts with the correct first and last post.QA
postsListToolwith that window and nopage: the result has 50 posts ordered by publish date,totalequals the number of posts in the window,pageis 0 andhasMoreis true; every item still has id, publishDate, state, content, settings, group, integrationId, platform, integrationNamepage1, 2, ...: each page continues where the previous ended with no repeated or missing ids, and the last page hashasMorefalsepagepast the end: emptyposts, sametotal,hasMorefalse. Call withpage-1: a validation error, no crashChecklist:
Put a "X" in the boxes below to indicate you have followed the checklist;
🤖 Generated with Claude Code