Skip to content

feat(chat): long attached files — the text is cut at 20,000 characters and pasted whole into an 8,192-token prompt; answer from retrieved chunks instead #22016

Open

Description

@mrveiss

Owner report (2026-11-22)

The chat "does not want to work with long files". The owner asked whether vectorization or a graph would help.

How an attachment reaches the model today (code)

  • Extraction: chat_workflow/attachment_extract.py:97 caches at most ATTACHMENT_MAX_CHARS_PER_FILE characters. That is 20,000 by default (chat_workflow/limits.py:49). The rest of the file is dropped.
  • Prompt: chat_workflow/attachment_context.py:173,212 pastes each file whole into the prompt, up to ATTACHMENT_MAX_CHARS_TOTAL (60,000 by default, limits.py:52).
  • Model window: the chat model's window is CHAT_NUM_CTX = 8192 tokens (autobot_shared/ssot_constants.py:195). 60,000 characters is roughly 15,000 tokens, before the system prompt and tool list are counted. So even within the caps, the prompt can exceed the window.
  • Unknown: what Ollama drops on overflow on this install (the start of the prompt, which holds the system instructions, or something else). A file longer than the cap loses its tail, and I did not verify whether the user is told.

Proposal

  1. Questions about a long file: retrieval.
  2. Whole-file tasks (summarise, translate, review all of it): retrieval does not cover them. Process the file in chunks and combine the partial results (map-reduce), showing progress.
  3. Graph: useful later, for entities and cross-references across many documents. It is not the first fix for one long file: building it costs a model pass over every chunk on the local model.
  4. Budget the prompt before sending it. Never exceed num_ctx, and tell the user when content was left out instead of dropping it silently.

Acceptance criteria

  • Established with evidence: what happens today to a file longer than the per-file cap, and to a prompt longer than num_ctx (what is dropped, and whether the user is told).
  • A question about a file larger than the chat window is answered from retrieved chunks. The answer cites the chunks it used, and nothing silently overflows the prompt.
  • A whole-file request on a long file processes every chunk, and the answer says so.
  • Retrieved attachment chunks are visible only to users who may see the attachment, with a test.
  • Host evidence: the owner's long-file case works on the local install.

Activity

  1. mrveiss commented on Nov 22, 2026

    @mrveiss
    OwnerAuthor

    Code finding (AC1), read against main a2384694b9.

    The main defect is not the 20,000-character cap. The prompt budget sizes attachments against a 32,768-token window, while the request is sent with num_ctx 8,192. As a result the existing budget never limits anything.

    • attachment_context.py:250-266 _window_char_budget calls ContextWindowManager.get_prompt_budget. That resolves the window from config/context_windows.yaml, which gives 32,768 for qwen3.5:9b.
    • The chat request is sent with ModelConfig.CHAT_NUM_CTX = 8,192 (manager.py:1936). Nothing in context_window_manager.py reads it.
    • The budget works out to 28,172 tokens, about 112k characters, which is above the 60,000-character total cap. So the window budget does not bind.
    • The system-prompt reserve is 500 tokens. resources/prompts/chat/system_prompt_simple.md alone is about 11 KB, roughly 2,750 tokens.
    • Result: one capped file (about 5k tokens), plus the system prompt (about 2.75k), plus history (up to 3k) already reaches the window.
    • The user is told the file was cut, by the truncated notice (attachment_context.py:74,232-234). The model is not told, and the notice does not say how much was used.
    • Overflow handling: feat(chat): context overflow protection — auto-summarize conversation when approaching model context limit #9043 (handle_message_completion) runs after the completion and attachments never pass through it. Nothing reads Ollama's prompt_eval_count, so an overflow is never detected.

    What Ollama drops on overflow can only be confirmed on the host: check the Ollama log for a "truncating input prompt" warning, and prompt_eval_count near 8,192.

    Plan, in 3 PRs:

    1. Make the budget read the real window. Reserve the measured system prompt and an output allowance. Check the prompt size at the send point. Make the notice state how much of the file was used, and tell the model the text is partial.
    2. Retrieval over attachment chunks: one Chroma collection filtered by session and file, reusing knowledge/document_ingest.py through an adapter, gated by the bug(chat): a file attached to a chat message never reaches the answer #20195 access checks and cited as file_content sources in fix(chat): answers carry proof of their steps; a respond turn stores one reply (#21976) #22012's shape.
    3. Whole-file tasks: map-reduce over every chunk, started by an explicit user action, reporting "N of N parts".

    The full plan is kept with the session; owner decisions are pending.

  2. mrveiss commented on Nov 22, 2026

    @mrveiss
    OwnerAuthor

    Owner decisions, 2026-11-22:

    1. Reply reserve: 2,048 tokens of the 8,192 window are reserved for the reply, and num_predict is set to match.
    2. Attachment sources: they store a location only (file, part, page). The excerpt is fetched through the access-checked file route when the user clicks it, and no file text is stored in the answer.
    3. Deleting a file: its chunks become unreachable at once. They are removed physically in the same human-approved cleanup that removes the file's bytes (Umbrella: SLM Data hygiene page — detect and safely clean up orphaned data across services #17038), never automatically.
    4. Whole-file tasks: started by a "Process the whole file" action on the long-file notice, not by a slash command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestphase:M2.4Tasks and company workflows

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions