Skip to content

📜 fix: Read a Child Thread's Messages a Page at a Time - #18424

Merged
danny-avila merged 2 commits into
devfrom
danny-avila/child-thread-history-paging
Nov 18, 2026
Merged

danny-avila merged 2 commits into
devfrom
danny-avila/child-thread-history-paging

Conversation

@danny-avila

@danny-avila danny-avila commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

GET /api/convos/:parentConversationId/subagents/:threadId/messages (added in #18188) read a child thread's entire stored history: no row limit, no cursor, no size bound. Since #18279 a card that previews a running child follows that thread, so the read happens for up to three children at a time without the reader asking for it. A long or heavy thread was materialized whole on the server and sent whole to the browser; an independent review reproduced 80 messages totalling 10 MiB from one request, limit=50 included, because the route never looked at it.

Every read of that route is now one page: the thread's newest rows, at most 50 of them or 2 MiB as stored, whichever is reached first. The page before it is read by a cursor, on request. Nothing is dropped: the followed pane shows a "Show earlier messages" control above the oldest row it holds and reads back a page at a time until the first row is in.

How it works

storage     getMessagesByCursor(filter, { limit, cursor, select, maxBytes })     # the existing cursor reader
              rows come off a database cursor newest first; the read stops before the row
              that would pass maxBytes (always at least one row), so nothing past the bound
              is loaded. nextCursor names the last row returned when more follow.

route       ?limit / ?cursor  → { messages (oldest first), earlier: cursor | null }
            neither           → the newest page as a bare list        # a client that predates paging
            a limit that is not a count, or a cursor that is not a page position → 400, nothing read
            limit is clamped to 50; the 2 MiB budget always applies

client      getSubagentThreadMessages(parent, thread, { cursor }) → { messages, earlier }
              a bare list from a server that predates paging is taken as the whole thread

useFollowChat
  read()          newest page. If it starts at a row already held, the rows before it stay:
                  they are the earlier pages the reader loaded. If it joins no held row, it
                  replaces the list and what precedes it is offered again, so there is no gap.
                  A follower's first read always replaces: rows another follower left in the
                  cache come without their cursor, so it starts from its own page.
  loadEarlier()   the page before the oldest row held, put ahead of the list
  hasEarlier · isLoadingEarlier · isEarlierError

FollowedThread    <EarlierMessages> above the thread, through the list's existing renderThread slot

The byte bound is enforced while reading rather than with $bsonSize or a window stage, so it does not depend on what the database engine supports. The two bounds are constants beside the thread view's own MAX_THREAD_MESSAGES: they cap a response and are paired with the client's page request, not something a deployment tunes.

Type of change

  • Bug fix
  • Tests / tooling / CI

Testing

Tested environments/configuration:

Chromium on the mock e2e lane, against this branch's own server: the whole subagent-activity.spec.ts (7 scenarios, including the retained-history ones that open long threads) and the new paging scenario twice. That scenario serves the fixture's real two rows as two pages of one, since the fixture cannot write a thread longer than a page; the server's own paging is covered by the storage and route tests below.

Automated tests:

  • packages/data-schemas/src/methods/message.cursor.spec.ts (4, real MongoDB in memory): a page stops before the row that would pass the budget and paging continues without skips; one row over the budget is returned alone with a cursor; the row limit still applies under a roomy budget; a thread that fits both bounds is read whole.
  • packages/api/src/agents/view.spec.ts (7 new): the default read is one bounded page as a list; a paged request returns { messages, earlier } oldest first; a cursor is passed through; limit=5000 is clamped; four malformed page requests answer 400 and read nothing.
  • packages/data-provider/specs/subagent-messages.spec.ts (3) and api-endpoints.spec.ts (1): the page request's URL, and a bare list taken as the whole thread.
  • packages/chat/src/react/__tests__/follow.spec.tsx (7): newest page and hasEarlier; each earlier page prepended until the first row; earlier rows kept across a re-read; a page that joins nothing replaces the list and offers the rest; a failed earlier read reported and retried; a plain list as the whole conversation; a recreated follower starting from its own newest page with the control back. Removing the join makes the third fail, and removing the first-read rule the last.
  • packages/chat/src/components/pane/__tests__/followed.spec.tsx (4): the control above the first row and the rows it adds; loading, disabled and not asked twice; the failure and Retry; nothing drawn for a thread that fits a page.
  • client/src/Providers/__tests__/SubagentsHostFollow.spec.tsx: the app passes the cursor through.
  • e2e/specs/mock/subagent-activity.spec.ts, new @scenario:subagent-panel-pages-child-history.
  • packages/chat npx jest (245 suites) and tsc; packages/data-provider, packages/data-schemas tsc; packages/api view.spec.ts and tsc filtered to the route; npm run build:api (declarations included); npm run static-checks -- --against origin/dev.

Risk / compatibility

  • An old client against a new server gets the newest page as a list and no control: it shows the last 50 rows of a longer thread until the page is reloaded onto the new client. A new client against an old server reads the whole thread, as before.
  • A single stored row larger than 2 MiB is still sent whole, as one page: message text is not truncated here.
  • useFollowChat is also changed by 🚦 feat: Drive a Followed Chat With Main Chat's ask #18413 (in review). Whichever merges second needs a rebase in follow.ts; the changes do not overlap in behaviour.
  • The conversation messages route main chat uses is not changed.

The child messages route read a thread's whole stored history, and a
card previewing a running child triggers that read without anyone
asking. Every read is now one page (50 rows or 2 MiB, newest first)
with a cursor for the page before it, the followed pane keeps the pages
it has read and offers the earlier ones, and a client or server that
predates paging still gets a list.
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

Head 24701ed.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T15:53:58.238240Z 8c08cca Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 24701edcdc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/chat/src/react/follow.ts Outdated
Comment on lines +277 to +279
if (joinsAt > 0) {
await chat.setMessages([...held.slice(0, joinsAt), ...page.messages]);
return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore the paging cursor when reusing cached history

If a reader loads an earlier page without reaching the beginning, then closes and reopens the child thread, the new follower reuses the messages in ReactQueryChatState but initializes earlier to null and hasEarlier to false. The newest page joins those cached rows here, so this early return preserves the messages without restoring their paging metadata. Consequently, “Show earlier messages” disappears and the remaining history becomes inaccessible. The same issue occurs when retry() recreates the effect. Persist the cursor alongside the retained history, or replace the cached list and initialize pagination from the fetched page when no cursor has been restored.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid, fixed in 8c08cca. A follower now joins a re-read onto the rows it holds only after it has placed a page of its own; its first read replaces whatever an earlier follower left in the cache and takes that page's cursor, so the control is back and the rest is reachable (re-read a page at a time rather than trusted from the cache). Test: "offers the earlier rows again to a follower that starts on rows another one left" (fails without the check: eight rows held, no control).

A follower recreated over rows another one left in the cache kept them
without knowing where they began, so the earlier-messages control was
gone. It now places its first page itself and takes that page's
cursor.
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

Head 8c08cca.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 8c08cca38e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila
danny-avila marked this pull request as ready for review November 18, 2026 16:07
@danny-avila
danny-avila merged commit cfe97ec into dev Nov 18, 2026
69 of 70 checks passed
@danny-avila
danny-avila deleted the danny-avila/child-thread-history-paging branch November 18, 2026 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant