[GH-ISSUE #2643] [Issue]: multi-pass knowledge retrieval double-injects facts when a middle chunk returns empty #1303

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

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Low — correctness. In the chunked (multi-pass) knowledge-retrieval path, the post-loop fallback uses extractions.length < chunks.length as the proxy for "final consolidation missing." But a chunk can be absent from extractions[] simply because it returned "No relevant information found." When exactly one non-final chunk is empty, the fallback concatenates the raw partial extractions plus the final consolidated extraction (which already merged those partials), double-injecting the same facts.

Expected behavior

When the final consolidation pass produces output, only the consolidated text should be injected. Raw per-chunk partials should be concatenated only when the consolidation pass itself produced nothing.

Actual behavior

packages/server/src/services/agents/knowledge-retrieval.ts — an empty chunk is filtered at line 104 and never pushed (105). The final chunk runs with _previousExtractions, and the agent prompt (agent-executor.ts:1757–1768) instructs the model to "Consolidate them into a single, coherent summary along with any new relevant information from the current chunk" — so the final extraction already incorporates the earlier partials. The post-loop branch:

if (extractions.length < chunks.length) {
  const combined = extractions.filter(Boolean).join("\n\n");
  return { ... data: { text: combined } ... };
}

is entered whenever any non-final chunk was empty even though consolidation succeeded — so combined is the raw partials concatenated with the consolidation that already contains them, double-injecting the facts.

Steps to reproduce

  1. Use a knowledge source large enough to split into 3+ chunks (exceeds sourceContextBudget).
  2. Arrange for a non-final chunk to yield "No relevant information found." while the others (including the final consolidation pass) produce text.
  3. Observe the injected context_injection text contains the same facts twice.

Environment

  • Marinara Engine version: staging @ 3dc062e2
  • Install type: source
  • OS + version: n/a (static source audit)
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above.

Additional context

Fix sketch: track consolidation success with an explicit boolean rather than inferring it from extractions.length vs chunks.length. When the final consolidation pass produced output, return only the consolidated text; fall back to concatenating raw partials only when the consolidation pass itself produced nothing.

Regression notes: not a regression — knowledge-retrieval.ts is byte-identical on staging and main; this is a long-standing shared-code bug. Effect is wasted prompt tokens / redundant context, not data loss. Found via static audit.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 19, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2643 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Low — correctness.** In the chunked (multi-pass) knowledge-retrieval path, the post-loop fallback uses `extractions.length < chunks.length` as the proxy for "final consolidation missing." But a chunk can be absent from `extractions[]` simply because it returned `"No relevant information found."` When exactly one *non-final* chunk is empty, the fallback concatenates the raw partial extractions **plus** the final consolidated extraction (which already merged those partials), double-injecting the same facts. ## Expected behavior When the final consolidation pass produces output, only the consolidated text should be injected. Raw per-chunk partials should be concatenated only when the consolidation pass itself produced nothing. ## Actual behavior `packages/server/src/services/agents/knowledge-retrieval.ts` — an empty chunk is filtered at line 104 and never pushed (105). The final chunk runs with `_previousExtractions`, and the agent prompt (`agent-executor.ts:1757–1768`) instructs the model to "Consolidate them into a single, coherent summary along with any new relevant information from the current chunk" — so the final extraction already incorporates the earlier partials. The post-loop branch: ```ts if (extractions.length < chunks.length) { const combined = extractions.filter(Boolean).join("\n\n"); return { ... data: { text: combined } ... }; } ``` is entered whenever any non-final chunk was empty even though consolidation succeeded — so `combined` is the raw partials concatenated with the consolidation that already contains them, double-injecting the facts. ## Steps to reproduce 1. Use a knowledge source large enough to split into 3+ chunks (exceeds `sourceContextBudget`). 2. Arrange for a non-final chunk to yield `"No relevant information found."` while the others (including the final consolidation pass) produce text. 3. Observe the injected `context_injection` text contains the same facts twice. ## Environment - Marinara Engine version: `staging @ 3dc062e2` - Install type: source - OS + version: n/a (static source audit) - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. ## Additional context Fix sketch: track consolidation success with an explicit boolean rather than inferring it from `extractions.length` vs `chunks.length`. When the final consolidation pass produced output, return only the consolidated text; fall back to concatenating raw partials only when the consolidation pass itself produced nothing. Regression notes: not a regression — `knowledge-retrieval.ts` is byte-identical on `staging` and `main`; this is a long-standing shared-code bug. Effect is wasted prompt tokens / redundant context, not data loss. Found via static audit. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:31:11 +07:00
  • closed this issue
  • added the
    server
    bug
    labels
Author
Owner

@Romuromylus commented on GitHub (Jun 19, 2026):

Sibling of #2642 — both live in the knowledge-retrieval path (knowledge-retrieval.ts / knowledge-sources.routes.ts). Grouped with #2639, #2640, #2641 into one agents-subsystem hardening PR.

<!-- gh-comment-id:4751269662 --> @Romuromylus commented on GitHub (Jun 19, 2026): Sibling of #2642 — both live in the knowledge-retrieval path (`knowledge-retrieval.ts` / `knowledge-sources.routes.ts`). Grouped with #2639, #2640, #2641 into one agents-subsystem hardening PR.
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#1303
No description provided.