[GH-ISSUE #5611] Remaining unscoped queries silently make lazy tables fully resident (defeats the chat memory cap) #3106

Closed
opened 2026-10-21 05:59:39 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @kolacheee on GitHub.

What happens

Since #5597/#5603, chat-scoped tables load per chat and evict past the memory cap — but a query the store cannot scope to specific chats triggers the safety fallback: the whole table becomes permanently resident for the process (the log says Lazy table <name> is now fully resident). A follow-up audit found the remaining queries that hit this, and the biggest one fires on essentially every Game-mode turn — meaning on game-heavy installs the memory cap for game_state_snapshots silently stops working the moment a tracker updates.

The sites (all verified on current staging; every one has the owning chatId already in scope or one call away)

Hot — fire per turn:

  • game-state.storage.ts:276 getByMessage(messageId, swipeIndex) — no chatId conjunct; ~16 callers across the generation loop, and updateByMessage (which takes chatId as a parameter) funnels through it twice. The correctly scoped sibling getByChatAndMessage already exists at :287.
  • game-state.storage.ts:340 create()'s dedupe delete — missing the chatId conjunct its sibling in game-engine-state.storage.ts:243 already has.
  • game-state.storage.ts:304 getCommittedForMessages — batch read per generation; both callers hold the chatId.
  • Eight bare-PK tracker writes in generate.routes.ts (:9175/:9216/:9274/:9334) and retry-agents-route.ts (:2933/:3062/:3123/:3161) — currently masked because getByMessage leases first; they must be scoped together with it or the lease just relocates.

Routine interactions:

  • chats.storage.ts deleteSwipe (:2177-2234) — nine unscoped statements across game_state_snapshots / spatial_context_snapshots / game_engine_state; msg.chatId is fetched at the top of the function and already used at :2245.
  • checkpoint.service.ts:148 and :158 — the captured-snapshot reads on every checkpoint create; input.chatId is validated against the result on the very next line, so the conjunct also enforces the existing cross-chat check.
  • gallery.storage.ts:35 listByFilePath — the sole caller builds the path as ${chatId}/${filename}, so the owner is literally embedded in the argument.
  • gallery.storage.ts:39/:60, game-scene-videos.storage.ts:28/:33 — id-only reads/removes whose warm callers hold the chatId.

Cold, id-only endpoints (need an optional param to carry the owner): gallery DELETE /:id, checkpoint DELETE /:id, workspace DELETE /context/:id, agents PATCH /runs/:runId, game-state.storage.ts:164 getById, capability-persistence.service.ts:273 spatial getById, chats.routes.ts:2398, and the defensive pruneToNewestAnchors list-delete.

Try it yourself (Termux or any install with MARINARA_MAX_RESIDENT_CHATS set)

Play one Game-mode turn with any tracker enabled, then check the log: [file-storage] Lazy table game_state_snapshots is now fully resident (unbounded access). From that point the table never evicts again for the process.

The fix

The same mechanical treatment ~20 write sites received in #5603: AND the owning chatId into each condition (the store's scope resolver ignores conjuncts it can't resolve, so this is behavior-preserving — it only tells the store which chat's rows to load). For the id-only endpoints, accept an optional chatId the client can start passing later; until then those few cold endpoints keep the lease, which this issue documents.

Deliberately out of scope (tracked separately): galleryFileHasReferences (cross-chat by design — needs a refcount/index, not a conjunct) and the boot-time gallery-recovery walk (its queries are perfectly scoped; the problem is that it visits every chat).

Part of the #5592 close-out.

Originally created by @kolacheee on GitHub (Aug 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5611 Originally assigned to: @kolacheee on GitHub. **What happens** Since #5597/#5603, chat-scoped tables load per chat and evict past the memory cap — but a query the store cannot scope to specific chats triggers the safety fallback: the **whole table becomes permanently resident** for the process (the log says `Lazy table <name> is now fully resident`). A follow-up audit found the remaining queries that hit this, and the biggest one fires on essentially every Game-mode turn — meaning on game-heavy installs the memory cap for `game_state_snapshots` silently stops working the moment a tracker updates. **The sites** (all verified on current staging; every one has the owning chatId already in scope or one call away) *Hot — fire per turn:* - `game-state.storage.ts:276` `getByMessage(messageId, swipeIndex)` — no chatId conjunct; ~16 callers across the generation loop, and `updateByMessage` (which **takes chatId as a parameter**) funnels through it twice. The correctly scoped sibling `getByChatAndMessage` already exists at `:287`. - `game-state.storage.ts:340` `create()`'s dedupe delete — missing the chatId conjunct its sibling in `game-engine-state.storage.ts:243` already has. - `game-state.storage.ts:304` `getCommittedForMessages` — batch read per generation; both callers hold the chatId. - Eight bare-PK tracker writes in `generate.routes.ts` (`:9175/:9216/:9274/:9334`) and `retry-agents-route.ts` (`:2933/:3062/:3123/:3161`) — currently masked *because* `getByMessage` leases first; they must be scoped together with it or the lease just relocates. *Routine interactions:* - `chats.storage.ts` `deleteSwipe` (`:2177-2234`) — nine unscoped statements across `game_state_snapshots` / `spatial_context_snapshots` / `game_engine_state`; `msg.chatId` is fetched at the top of the function and already used at `:2245`. - `checkpoint.service.ts:148` and `:158` — the captured-snapshot reads on every checkpoint create; `input.chatId` is validated against the result on the very next line, so the conjunct also enforces the existing cross-chat check. - `gallery.storage.ts:35` `listByFilePath` — the sole caller builds the path as `${chatId}/${filename}`, so the owner is literally embedded in the argument. - `gallery.storage.ts:39/:60`, `game-scene-videos.storage.ts:28/:33` — id-only reads/removes whose warm callers hold the chatId. *Cold, id-only endpoints (need an optional param to carry the owner):* gallery `DELETE /:id`, checkpoint `DELETE /:id`, workspace `DELETE /context/:id`, agents `PATCH /runs/:runId`, `game-state.storage.ts:164` `getById`, `capability-persistence.service.ts:273` spatial `getById`, `chats.routes.ts:2398`, and the defensive `pruneToNewestAnchors` list-delete. **Try it yourself** (Termux or any install with `MARINARA_MAX_RESIDENT_CHATS` set) Play one Game-mode turn with any tracker enabled, then check the log: `[file-storage] Lazy table game_state_snapshots is now fully resident (unbounded access)`. From that point the table never evicts again for the process. **The fix** The same mechanical treatment ~20 write sites received in #5603: AND the owning chatId into each condition (the store's scope resolver ignores conjuncts it can't resolve, so this is behavior-preserving — it only tells the store which chat's rows to load). For the id-only endpoints, accept an optional chatId the client can start passing later; until then those few cold endpoints keep the lease, which this issue documents. **Deliberately out of scope** (tracked separately): `galleryFileHasReferences` (cross-chat by design — needs a refcount/index, not a conjunct) and the boot-time gallery-recovery walk (its queries are perfectly scoped; the problem is that it visits every chat). Part of the #5592 close-out.
bsakaguchi 2026-10-21 05:59:39 +07:00
Author
Owner

@kolacheee commented on GitHub (Aug 29, 2026):

Taking this one (kolacheee, agent-assisted) — PR incoming with the conjunct-threading batch; #5612/#5613 stay open for the two design-shaped cases.

<!-- gh-comment-id:5459003991 --> @kolacheee commented on GitHub (Aug 29, 2026): Taking this one (kolacheee, agent-assisted) — PR incoming with the conjunct-threading batch; #5612/#5613 stay open for the two design-shaped cases.
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#3106
No description provided.