[GH-ISSUE #2617] [Issue]: Knowledge-retrieval source files (incl. PDFs) re-read and re-parsed from disk every generation turn (no cache) #1290

Closed
opened 2026-10-21 05:30:46 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — performance. When a knowledge-retrieval agent is configured with sourceFileIds, the generate pipeline re-reads and re-parses every source file from disk on every generation turn with no cache — a full readFile for text formats and a full PDFParse().getText() re-parse for PDFs. Uploads are bounded only by the global 256MB cap, so a large PDF means a multi-MB read plus a CPU-heavy parse on the request path every message, scaling linearly with file size/count and blocking the event loop on PDF parse.

Expected behavior

Extracted source text should be cached and reused across turns (extract once per file version, or at least once per chat session), not recomputed per message.

Actual behavior

// packages/server/src/routes/generate.routes.ts (~3900-3924)
for (const fileId of knowledgeRetrievalAgent.settings.sourceFileIds) {
  // ...
  material += await extractFileText(filePath); // re-reads + re-parses every turn
}
agentContext.memory._knowledgeRetrievalMaterial = material; // ~3923 — only assignment, per-request

extractFileText (knowledge-sources.routes.ts:78-107) does readFile() of the whole file for text formats and PDFParse().getText() for PDFs. There is no memoization: agentContext is built per-request (~3524) and _knowledgeRetrievalMaterial is assigned only at ~3923, consumed by shouldRunKR (~4217-4221, gated only by !input.regenerateMessageId, so normal turns re-extract every message). Upload size is bounded only by MAX_UPLOAD_BYTES = 256MB (app.ts:44/68) with no per-source extraction cap.

Steps to reproduce

  1. Configure a knowledge-retrieval agent with one or more uploaded source files (especially a large PDF).
  2. Chat normally for several turns.
  3. Observe each generated message re-reads + re-parses the source file(s) from disk (CPU/event-loop cost scaling with file size).

Environment

  • Marinara Engine version: staging @ 56f700cd (v2.0.0)
  • 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. Cache extracted text per file id keyed on (id, size, uploadedAt/mtime) and reuse across turns; invalidate on re-upload/delete. The upload route already stores size in meta. Optionally cap per-source extraction size and/or move PDF parsing off the request path. At minimum, extract once per chat session rather than per turn.

Regression notes. Not a regression — the identical logic exists on main (generate.routes.ts ~5554-5575) and extractFileText is byte-identical on both branches. Gated behind opt-in knowledge-retrieval config, hence medium not high.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 18, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2617 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — performance.** When a knowledge-retrieval agent is configured with `sourceFileIds`, the generate pipeline re-reads and re-parses every source file from disk on **every** generation turn with no cache — a full `readFile` for text formats and a full `PDFParse().getText()` re-parse for PDFs. Uploads are bounded only by the global 256MB cap, so a large PDF means a multi-MB read plus a CPU-heavy parse on the request path every message, scaling linearly with file size/count and blocking the event loop on PDF parse. ## Expected behavior Extracted source text should be cached and reused across turns (extract once per file version, or at least once per chat session), not recomputed per message. ## Actual behavior ```ts // packages/server/src/routes/generate.routes.ts (~3900-3924) for (const fileId of knowledgeRetrievalAgent.settings.sourceFileIds) { // ... material += await extractFileText(filePath); // re-reads + re-parses every turn } agentContext.memory._knowledgeRetrievalMaterial = material; // ~3923 — only assignment, per-request ``` `extractFileText` (`knowledge-sources.routes.ts:78-107`) does `readFile()` of the whole file for text formats and `PDFParse().getText()` for PDFs. There is no memoization: `agentContext` is built per-request (`~3524`) and `_knowledgeRetrievalMaterial` is assigned only at `~3923`, consumed by `shouldRunKR` (`~4217-4221`, gated only by `!input.regenerateMessageId`, so normal turns re-extract every message). Upload size is bounded only by `MAX_UPLOAD_BYTES = 256MB` (`app.ts:44/68`) with no per-source extraction cap. ## Steps to reproduce 1. Configure a knowledge-retrieval agent with one or more uploaded source files (especially a large PDF). 2. Chat normally for several turns. 3. Observe each generated message re-reads + re-parses the source file(s) from disk (CPU/event-loop cost scaling with file size). ## Environment - Marinara Engine version: `staging @ 56f700cd` (v2.0.0) - 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.** Cache extracted text per file id keyed on `(id, size, uploadedAt/mtime)` and reuse across turns; invalidate on re-upload/delete. The upload route already stores `size` in meta. Optionally cap per-source extraction size and/or move PDF parsing off the request path. At minimum, extract once per chat session rather than per turn. **Regression notes.** Not a regression — the identical logic exists on `main` (`generate.routes.ts` ~5554-5575) and `extractFileText` is byte-identical on both branches. Gated behind opt-in knowledge-retrieval config, hence medium not high. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:46 +07:00
  • closed this issue
  • added the
    server
    bug
    labels
Author
Owner

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

Related to #2621 — same knowledge-sources subsystem (per-turn extraction perf here, meta.json TOCTOU there).

<!-- gh-comment-id:4740874135 --> @Romuromylus commented on GitHub (Jun 18, 2026): Related to #2621 — same knowledge-sources subsystem (per-turn extraction perf here, meta.json TOCTOU there).
Author
Owner

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

Fixed in #2623, merged to staging (e1132e5b). Closing manually since the PR targeted staging rather than the default branch.

<!-- gh-comment-id:4742296201 --> @Romuromylus commented on GitHub (Jun 18, 2026): Fixed in #2623, merged to `staging` (e1132e5b). Closing manually since the PR targeted `staging` rather than the default branch.
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#1290
No description provided.