[GH-ISSUE #4721] [Issue]: Chat Settings drawer's pageSize-100 observer hijacks the shared chat-messages query options while open #2592

Closed
opened 2026-10-21 05:50:56 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Aug 7, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4721

Originally assigned to: @kolacheee on GitHub.

Summary

ChatSettingsDrawer's secret-plot reader mounts a useChatMessages observer with a hardcoded pageSize: 100 on the same React Query key the chat transcript uses with the user's messagesPerPage (and the tracker panel uses with 20). In React Query v5 the last observer to set options owns the shared query's queryFn and getNextPageParam closures, so while the drawer is open the transcript's pagination runs with the wrong page size — fetching 100-row pages regardless of the user's setting and mis-evaluating the has-more-history check.

Expected behavior

Opening Chat Settings has no effect on the transcript's message pagination: refetches fetch messagesPerPage-sized pages, and "Load More" stays available whenever older history exists.

Actual behavior

While the drawer is open (and briefly after closing, until a surviving observer re-renders and rewrites the shared options), the shared messages query's options belong to whichever observer rendered last — typically the drawer, which renders after ChatArea in the tree:

  • Any refetch of the transcript (post-generation refresh, invalidation) fetches with limit=100 instead of the user's page size. ChatArea then discards the surplus — its defensive trimNewestLoadedMessagePage effect cuts pages[0] back down to messagesPerPage, throwing away up to 80 fetched rows per refetch.
  • getNextPageParam evaluates lastPage.length < pageSize with pageSize = 100 against pages that were fetched at 20 rows, returns undefined, and hasNextPage goes false — "Load More" disappears while older history still exists.
  • Note the drawer's observer overwrites the shared options even when its own enabled gate is false — React Query observers set options regardless of enabled; the flag only gates fetching. So the mis-share applies whenever the drawer is open at all, not only when the Director secret-plot feature is active.

Code locations (current staging):

  • packages/client/src/components/chat/ChatSettingsDrawer.tsx:1650 — secretPlotMessagesQuery = useChatMessages(chat.id, 100, open && directorActive && supportsNarrativeDirectorSecretPlot && narrativeDirectorSecretPlotEnabled)
  • packages/client/src/hooks/use-chats.ts:270 — useChatMessages keys on chatKeys.messages(chatId) only; pageSize lives in the queryFn/getNextPageParam closures, not the key
  • packages/client/src/components/chat/ChatArea.tsx:749 — transcript observer at messagesPerPage; packages/client/src/features/tracker-panel/hooks/use-tracker-panel-model.ts:74 — third observer at 20

Steps to reproduce

  1. Set Messages-per-page to 20 and open a chat with well over 100 messages; scroll back a few pages so multiple 20-row pages are cached and "Load More" is visible.
  2. Open Chat Settings (leave it open) and trigger any transcript refetch — send a message and let the reply complete, or edit chat metadata that invalidates messages.
  3. Watch DevTools Network: the refetch drains limit=100 pages instead of 20, and "Load More" can vanish although older history exists. Close the drawer and force another refetch: behavior returns to 20 after the transcript re-renders.

Environment

  • Marinara Engine version: staging (fcf26892d) — mechanics unchanged on current main
  • Install type: source
  • OS + version: Windows 11 Pro (any)
  • Browser or app shell: any (React Query v5 semantics)

Logs, screenshots, or video

Found by code analysis during the #4703 investigation (two independent review passes against the installed @tanstack/query-core@5.100.9 confirmed the last-observer-wins options behavior); the pagination consequences follow directly from getNextPageParam's lastPage.length < pageSize gate. To be explicit: this is not what caused the reporter's traffic in #4703 (that was a third-party personal extension issuing its own fetches) — it is a latent mis-share the investigation surfaced.

Additional context

The codebase already documents the correct pattern for exactly this trap: useChatMessagePeek (use-chats.ts:303-309) deliberately uses a separate query key including its limit, with a comment explaining that the shared messages key "ignores page size." The natural fix is to move the secret-plot reader onto its own key the same way (it only ever wants the newest ~100 messages as one page — it never paginates), leaving the shared infinite query with a single page-size owner per surface pair (transcript + tracker both derive from settings/20-row pages).

Happy to take this one — it's adjacent to the #4703 amplifier work in PR #4719.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Aug 7, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/4721 Originally assigned to: @kolacheee on GitHub. ## Summary `ChatSettingsDrawer`'s secret-plot reader mounts a `useChatMessages` observer with a hardcoded `pageSize: 100` on the **same React Query key** the chat transcript uses with the user's `messagesPerPage` (and the tracker panel uses with 20). In React Query v5 the last observer to set options owns the shared query's `queryFn` and `getNextPageParam` closures, so while the drawer is open the transcript's pagination runs with the wrong page size — fetching 100-row pages regardless of the user's setting and mis-evaluating the has-more-history check. ## Expected behavior Opening Chat Settings has no effect on the transcript's message pagination: refetches fetch `messagesPerPage`-sized pages, and "Load More" stays available whenever older history exists. ## Actual behavior While the drawer is open (and briefly after closing, until a surviving observer re-renders and rewrites the shared options), the shared messages query's options belong to whichever observer rendered last — typically the drawer, which renders after `ChatArea` in the tree: - Any refetch of the transcript (post-generation refresh, invalidation) fetches with `limit=100` instead of the user's page size. `ChatArea` then discards the surplus — its defensive `trimNewestLoadedMessagePage` effect cuts `pages[0]` back down to `messagesPerPage`, throwing away up to 80 fetched rows per refetch. - `getNextPageParam` evaluates `lastPage.length < pageSize` with `pageSize = 100` against pages that were fetched at 20 rows, returns `undefined`, and `hasNextPage` goes false — **"Load More" disappears while older history still exists**. - Note the drawer's observer overwrites the shared options even when its own `enabled` gate is false — React Query observers set options regardless of `enabled`; the flag only gates fetching. So the mis-share applies whenever the drawer is open at all, not only when the Director secret-plot feature is active. Code locations (current staging): - `packages/client/src/components/chat/ChatSettingsDrawer.tsx:1650` — `secretPlotMessagesQuery = useChatMessages(chat.id, 100, open && directorActive && supportsNarrativeDirectorSecretPlot && narrativeDirectorSecretPlotEnabled)` - `packages/client/src/hooks/use-chats.ts:270` — `useChatMessages` keys on `chatKeys.messages(chatId)` only; `pageSize` lives in the `queryFn`/`getNextPageParam` closures, not the key - `packages/client/src/components/chat/ChatArea.tsx:749` — transcript observer at `messagesPerPage`; `packages/client/src/features/tracker-panel/hooks/use-tracker-panel-model.ts:74` — third observer at 20 ## Steps to reproduce 1. Set Messages-per-page to 20 and open a chat with well over 100 messages; scroll back a few pages so multiple 20-row pages are cached and "Load More" is visible. 2. Open Chat Settings (leave it open) and trigger any transcript refetch — send a message and let the reply complete, or edit chat metadata that invalidates messages. 3. Watch DevTools Network: the refetch drains `limit=100` pages instead of 20, and "Load More" can vanish although older history exists. Close the drawer and force another refetch: behavior returns to 20 after the transcript re-renders. ## Environment - Marinara Engine version: staging (fcf26892d) — mechanics unchanged on current `main` - Install type: source - OS + version: Windows 11 Pro (any) - Browser or app shell: any (React Query v5 semantics) ## Logs, screenshots, or video Found by code analysis during the #4703 investigation (two independent review passes against the installed `@tanstack/query-core@5.100.9` confirmed the last-observer-wins options behavior); the pagination consequences follow directly from `getNextPageParam`'s `lastPage.length < pageSize` gate. To be explicit: this is **not** what caused the reporter's traffic in #4703 (that was a third-party personal extension issuing its own fetches) — it is a latent mis-share the investigation surfaced. ## Additional context The codebase already documents the correct pattern for exactly this trap: `useChatMessagePeek` (`use-chats.ts:303-309`) deliberately uses a **separate query key including its limit**, with a comment explaining that the shared messages key "ignores page size." The natural fix is to move the secret-plot reader onto its own key the same way (it only ever wants the newest ~100 messages as one page — it never paginates), leaving the shared infinite query with a single page-size owner per surface pair (transcript + tracker both derive from settings/20-row pages). Happy to take this one — it's adjacent to the #4703 amplifier work in PR #4719. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:50:56 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#2592
No description provided.