[GH-ISSUE #2999] [Issue]: listCustomRunsForChat truncates to a fixed 200-row window of ALL agent runs before filtering out built-ins, hiding custom outputs #1573

Closed
opened 2026-10-21 05:39:28 +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/2999

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
listCustomRunsForChat fetches the 200 most-recent successful runs (built-in AND custom), THEN filters out built-ins, THEN slices to the requested limit. In a chat where built-in agents run every turn, the 200-row window can be entirely consumed by built-in runs, pushing older custom-agent runs outside the window. Those custom runs are then never returned even though the requested limit (default 50) was not reached. Since this endpoint backs the UI for viewing/editing custom-agent outputs, the user loses access to editable custom outputs that still exist in the DB.

How to reproduce (try it yourself)

  1. Enable several built-in agents that run every turn plus one custom agent that runs occasionally.
  2. Generate >200 total agent runs in the chat (built-ins dominate).
  3. GET /api/agents/runs/:chatId/custom.

You'll see: older custom-agent runs that fall outside the most-recent-200 mixed window are omitted from the response despite being well under the limit of 50.
Expected: up to limit custom runs are returned regardless of how many built-in runs precede them.

Where it is
packages/server/src/services/storage/agents.storage.ts:367-382 (listCustomRunsForChat).

What's happening
The 200-row LIMIT is applied to the unfiltered mixed result set; the built-in filter and final slice happen in JS afterward, so a burst of built-in runs evicts custom runs before they are ever considered.

Suggested fix
Keep the JS-side built-in filter (it works on both storage backends) but remove the premature SQL row cap so the cap is applied AFTER filtering, with an early break once enough custom rows are collected:

async listCustomRunsForChat(chatId: string, limit = 50) {
  const normalizedLimit = Math.max(1, Math.min(limit, 200));
  const rows = await db
    .select()
    .from(agentRuns)
    .innerJoin(agentConfigs, eq(agentRuns.agentConfigId, agentConfigs.id))
    .where(and(eq(agentRuns.chatId, chatId), eq(agentRuns.success, "true")))
    .orderBy(desc(agentRuns.createdAt));

  const customRows: typeof rows = [];
  for (const row of rows) {
    if (isBuiltInAgentType(row.agent_configs.type)) continue;
    customRows.push(row);
    if (customRows.length >= normalizedLimit) break;
  }
  return customRows.map((row) => serializeRunWithConfig(row));
}

Regression avoided: do NOT exclude built-ins via SQL NOT IN/notInArray (the primary suggested fix) — the project's DEFAULT storage backend is a custom file-native query emulator whose WHERE evaluator does not implement not in, so the clause would silently fail to filter and the "custom" list would leak built-in runs (which the PATCH /runs/:runId endpoint then 403-rejects). The JS-filter-after-cap approach is backend-agnostic, and the unbounded per-chat scan it introduces already exists as an accepted pattern in the same file (getEchoMessages), bounded by the early break.


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/2999 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `listCustomRunsForChat` fetches the 200 most-recent successful runs (built-in AND custom), THEN filters out built-ins, THEN slices to the requested limit. In a chat where built-in agents run every turn, the 200-row window can be entirely consumed by built-in runs, pushing older custom-agent runs outside the window. Those custom runs are then never returned even though the requested limit (default 50) was not reached. Since this endpoint backs the UI for viewing/editing custom-agent outputs, the user loses access to editable custom outputs that still exist in the DB. **How to reproduce (try it yourself)** 1. Enable several built-in agents that run every turn plus one custom agent that runs occasionally. 2. Generate >200 total agent runs in the chat (built-ins dominate). 3. GET /api/agents/runs/:chatId/custom. You'll see: older custom-agent runs that fall outside the most-recent-200 mixed window are omitted from the response despite being well under the limit of 50. Expected: up to `limit` custom runs are returned regardless of how many built-in runs precede them. **Where it is** `packages/server/src/services/storage/agents.storage.ts:367-382` (`listCustomRunsForChat`). **What's happening** The 200-row LIMIT is applied to the unfiltered mixed result set; the built-in filter and final slice happen in JS afterward, so a burst of built-in runs evicts custom runs before they are ever considered. **Suggested fix** Keep the JS-side built-in filter (it works on both storage backends) but remove the premature SQL row cap so the cap is applied AFTER filtering, with an early break once enough custom rows are collected: ```ts async listCustomRunsForChat(chatId: string, limit = 50) { const normalizedLimit = Math.max(1, Math.min(limit, 200)); const rows = await db .select() .from(agentRuns) .innerJoin(agentConfigs, eq(agentRuns.agentConfigId, agentConfigs.id)) .where(and(eq(agentRuns.chatId, chatId), eq(agentRuns.success, "true"))) .orderBy(desc(agentRuns.createdAt)); const customRows: typeof rows = []; for (const row of rows) { if (isBuiltInAgentType(row.agent_configs.type)) continue; customRows.push(row); if (customRows.length >= normalizedLimit) break; } return customRows.map((row) => serializeRunWithConfig(row)); } ``` Regression avoided: do NOT exclude built-ins via SQL `NOT IN`/`notInArray` (the primary suggested fix) — the project's DEFAULT storage backend is a custom file-native query emulator whose WHERE evaluator does not implement `not in`, so the clause would silently fail to filter and the "custom" list would leak built-in runs (which the PATCH /runs/:runId endpoint then 403-rejects). The JS-filter-after-cap approach is backend-agnostic, and the unbounded per-chat scan it introduces already exists as an accepted pattern in the same file (getEchoMessages), bounded by the early break. --- *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 #3004.

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