[GH-ISSUE #3012] [Issue]: append_chat_summary grows persisted summaryEntries unbounded; only the compiled summary is byte-capped #1584

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

Every append_chat_summary call adds a new entry to metadata.summaryEntries via appendChatSummaryEntryToMetadata, which does sortChatSummaryEntries([...entries, entry]) with NO cap on entry count and no pruning. The compiled summary string is byte-limited (to 64 KB), so the prompt-injected text stays bounded, but the underlying summaryEntries array — persisted in chat metadata and re-serialized to JSON on every metadata write — accumulates forever. An agent that calls append_chat_summary on most turns will steadily inflate the chat row's metadata blob with entries that no longer contribute to the already byte-capped compiled summary, growing JSON parse/stringify cost on every subsequent patchMetadata for that chat.

How to reproduce (try it yourself)

  1. Enable an agent with append_chat_summary and have it append summary text every turn over a long chat.
  2. Inspect metadata.summaryEntries.

You'll see: the array grows by one per append with no upper bound, while the compiled summary saturates at ~64 KB and discards older entries' contribution. Each subsequent patchMetadata (any metadata write for the chat) parses/stringifies the ever-larger array.

Expected: the persisted entries array is bounded (mirroring the 64 KB compile cap) so metadata size and per-write JSON cost stay bounded.

Where it is

Writer at packages/server/src/services/tools/tool-executor.ts:536-549; the unbounded append at packages/shared/src/utils/chat-summary-entries.ts:267-283 (appendChatSummaryEntryToMetadata); persistence (JSON.stringify on every write) at packages/server/src/services/storage/chats.storage.ts:467-489.

What's happening

appendChatSummaryEntryToMetadata appends to the entries array with no maximum-count or total-bytes pruning; the only size guard (compileChatSummaryEntries → 64 KB) is applied to the derived compiled string, not to the persisted source array. The append path has no back-pressure on the stored entry list.

Suggested fix

Cap the count of automated summary entries inside the shared appendChatSummaryEntryToMetadata so all unbounded callers (the agent tool plus the auto-rolling-summary route) are fixed at once, while preserving user-authored and user-disabled content.

In packages/shared/src/utils/chat-summary-entries.ts:

  1. Add export const MAX_AUTOMATED_CHAT_SUMMARY_ENTRIES = 200;.
  2. Add a prune helper that drops the OLDEST automated entries first (the sorted array is oldest-to-newest), never touching manual/legacy entries and never an automated entry that hid messages (hiddenMessageIds), since this side-effect-free helper cannot unhide them the way the delete route does:
function pruneAutomatedChatSummaryEntries(entries: ChatSummaryEntry[]): ChatSummaryEntry[] {
  let automatedCount = 0;
  for (const entry of entries) if (entry.origin === "automated") automatedCount += 1;
  let removable = automatedCount - MAX_AUTOMATED_CHAT_SUMMARY_ENTRIES;
  if (removable <= 0) return entries;
  const pruned: ChatSummaryEntry[] = [];
  for (const entry of entries) {
    if (removable > 0 && entry.origin === "automated" && !(entry.hiddenMessageIds && entry.hiddenMessageIds.length > 0)) {
      removable -= 1;
      continue;
    }
    pruned.push(entry);
  }
  return pruned;
}
  1. Apply it after the sort in appendChatSummaryEntryToMetadata:
const nextEntries = pruneAutomatedChatSummaryEntries(sortChatSummaryEntries([...entries, entry]));
return { entry, entries: nextEntries, summary: compileChatSummaryEntries(nextEntries) };

Regression avoided: the original suggestion ("prune entries whose content no longer survives compilation" / "drop oldest disabled") would delete user-disabled entries — but summaryEntries is user-facing (the SummaryPopover renders disabled entries via a "show inactive" toggle and lets the user re-enable/edit/delete them), and disabled entries never survive compilation BY DESIGN. It would also break message-hiding consistency if it pruned an entry carrying hiddenMessageIds (leaving messages hidden with no entry to justify them; only the delete route unhides first). And the cap belongs in the shared helper, not just the tool path — three of its four callers produce unbounded automated entries (the tool, the automated chats.routes path, and the auto-rolling-summary generate.routes path); the fourth is user-initiated manual. The corrected fix caps ONLY automated entries, preserves all manual/legacy and disabled entries, and never prunes an automated entry that hid messages.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3012 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** Every `append_chat_summary` call adds a new entry to `metadata.summaryEntries` via `appendChatSummaryEntryToMetadata`, which does `sortChatSummaryEntries([...entries, entry])` with NO cap on entry count and no pruning. The compiled `summary` string is byte-limited (to 64 KB), so the prompt-injected text stays bounded, but the underlying `summaryEntries` array — persisted in chat metadata and re-serialized to JSON on every metadata write — accumulates forever. An agent that calls `append_chat_summary` on most turns will steadily inflate the chat row's metadata blob with entries that no longer contribute to the already byte-capped compiled summary, growing JSON parse/stringify cost on every subsequent `patchMetadata` for that chat. **How to reproduce (try it yourself)** 1. Enable an agent with `append_chat_summary` and have it append summary text every turn over a long chat. 2. Inspect `metadata.summaryEntries`. You'll see: the array grows by one per append with no upper bound, while the compiled `summary` saturates at ~64 KB and discards older entries' contribution. Each subsequent `patchMetadata` (any metadata write for the chat) parses/stringifies the ever-larger array. Expected: the persisted entries array is bounded (mirroring the 64 KB compile cap) so metadata size and per-write JSON cost stay bounded. **Where it is** Writer at `packages/server/src/services/tools/tool-executor.ts:536-549`; the unbounded append at `packages/shared/src/utils/chat-summary-entries.ts:267-283` (`appendChatSummaryEntryToMetadata`); persistence (JSON.stringify on every write) at `packages/server/src/services/storage/chats.storage.ts:467-489`. **What's happening** `appendChatSummaryEntryToMetadata` appends to the entries array with no maximum-count or total-bytes pruning; the only size guard (`compileChatSummaryEntries` → 64 KB) is applied to the derived compiled string, not to the persisted source array. The append path has no back-pressure on the stored entry list. **Suggested fix** Cap the count of `automated` summary entries inside the shared `appendChatSummaryEntryToMetadata` so all unbounded callers (the agent tool plus the auto-rolling-summary route) are fixed at once, while preserving user-authored and user-disabled content. In `packages/shared/src/utils/chat-summary-entries.ts`: 1. Add `export const MAX_AUTOMATED_CHAT_SUMMARY_ENTRIES = 200;`. 2. Add a prune helper that drops the OLDEST `automated` entries first (the sorted array is oldest-to-newest), never touching manual/legacy entries and never an automated entry that hid messages (`hiddenMessageIds`), since this side-effect-free helper cannot unhide them the way the delete route does: ```ts function pruneAutomatedChatSummaryEntries(entries: ChatSummaryEntry[]): ChatSummaryEntry[] { let automatedCount = 0; for (const entry of entries) if (entry.origin === "automated") automatedCount += 1; let removable = automatedCount - MAX_AUTOMATED_CHAT_SUMMARY_ENTRIES; if (removable <= 0) return entries; const pruned: ChatSummaryEntry[] = []; for (const entry of entries) { if (removable > 0 && entry.origin === "automated" && !(entry.hiddenMessageIds && entry.hiddenMessageIds.length > 0)) { removable -= 1; continue; } pruned.push(entry); } return pruned; } ``` 3. Apply it after the sort in `appendChatSummaryEntryToMetadata`: ```ts const nextEntries = pruneAutomatedChatSummaryEntries(sortChatSummaryEntries([...entries, entry])); return { entry, entries: nextEntries, summary: compileChatSummaryEntries(nextEntries) }; ``` *Regression avoided:* the original suggestion ("prune entries whose content no longer survives compilation" / "drop oldest disabled") would delete user-disabled entries — but `summaryEntries` is user-facing (the SummaryPopover renders disabled entries via a "show inactive" toggle and lets the user re-enable/edit/delete them), and disabled entries never survive compilation BY DESIGN. It would also break message-hiding consistency if it pruned an entry carrying `hiddenMessageIds` (leaving messages hidden with no entry to justify them; only the delete route unhides first). And the cap belongs in the shared helper, not just the tool path — three of its four callers produce unbounded automated entries (the tool, the automated `chats.routes` path, and the auto-rolling-summary `generate.routes` path); the fourth is user-initiated `manual`. The corrected fix caps ONLY automated entries, preserves all manual/legacy and disabled entries, and never prunes an automated entry that hid messages. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in merged staging PR #3018.

<!-- gh-comment-id:4833018537 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3018.
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#1584
No description provided.