reflect: Ollama's context-overflow 400 is not recognised as an overflow, and `REFLECT_MAX_CONTEXT_TOKENS` ignores `LLM_OLLAMA_NUM_CTX`

Aperta
#7,260 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
56/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
ollama, python
Ambito
ai, backend

Direzione di ricerca

Start by reading _is_context_overflow_error in engine/reflect/agent.py and trace where the reflect context-token budget is configured from HINDSIGHT_API_LLM_OLLAMA_NUM_CTX and HINDSIGHT_API_REFLECT_MAX_CONTEXT_TOKENS. Check the existing overflow and budget tests before changing behavior. Done means Ollama’s reported wording triggers final synthesis, and the default budget respects the configured context size when the override is unset.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

core
Bug Description

With the Ollama provider, a mental-model refresh whose reflect loop outgrows the model's context fails outright
instead of falling back to a final synthesis from the evidence already gathered, and the page is then paused. Two
things combine:

  1. _is_context_overflow_error (engine/reflect/agent.py) looks for phrases such as context_length_exceeded,
    maximum context length and prompt is too long. Ollama words the error as "the prompt is longer than the context
    length currently available to the model", which matches none of them, so the branch that forces final synthesis on
    an overflow is skipped and the 400 ends the refresh.
  2. HINDSIGHT_API_REFLECT_MAX_CONTEXT_TOKENS keeps its default of 100,000 even when
    HINDSIGHT_API_LLM_OLLAMA_NUM_CTX tells Hindsight the model has 32,768. The budget is the threshold at which the
    loop forces synthesis, so with these settings the loop keeps adding evidence long after the prompt has outgrown the
    context Hindsight itself asked Ollama for.
Environment
  • hindsight-api 1.3.0 (through hindsight-embed) on macOS; both points are unchanged in v1.3.1 and on main.
  • HINDSIGHT_API_LLM_PROVIDER=ollama, HINDSIGHT_API_LLM_OLLAMA_NUM_CTX=32768,
    HINDSIGHT_API_REFLECT_MAX_COMPLETION_TOKENS=8192; HINDSIGHT_API_REFLECT_MAX_CONTEXT_TOKENS not set.
  • Ollama 0.40.19 with qwen3.8:27b (Q4_K_M); Ollama's server log shows n_ctx = 32768 on every load.
Actual Behavior

In the API logs we keep (2026-11-04 to 2026-12-04), 9 refresh_mental_model tasks failed with this error, one on
2026-11-25, seven on 2026-11-29 and one on 2026-11-30:

Task execution failed: refresh_mental_model, error: UpstreamHTTPError: HTTP 400 from http://127.0.0.1:11434/api/chat: {"error":"the prompt is longer than the context length currently available to the model; shorten the prompt, adjust the context length in settings, or use a model with a longer context length"}

The same 400 was logged twice more by reflect.agent as "closing done call failed, using the standalone prompt";
that fallback re-sends a prompt sized from the same budget. Each failed refresh set last_refresh_failed_at, so those
pages were left paused (they show in paused_mental_models since #6375).

Expected Behavior
  • Ollama's wording counts as a context overflow, so the refresh synthesizes from what it has, as it would on
    OpenAI's context_length_exceeded.
  • With REFLECT_MAX_CONTEXT_TOKENS unset and LLM_OLLAMA_NUM_CTX set, the default follows the context: for example
    num_ctx - reflect_max_completion_tokens, less a margin, since the budget is an o200k_base estimate of the
    messages that leaves out the tool schemas sent with each request (and with no completion cap set, some fixed share
    of num_ctx). Without num_ctx, Ollama 0.40.19 reports a loaded model's context as context_length in
    GET /api/ps; a startup warning when the reflect budget exceeds it would at least make the mismatch visible.
Workaround

Setting HINDSIGHT_API_REFLECT_MAX_CONTEXT_TOKENS=24576 (32,768 - 8,192) has held since 2026-12-04 14:56: to 2026-12-05 08:49, 41 mental-model refreshes started (38 of them on 12-05) and none failed on the context.

Lingua principale
Python
Stelle
45.2k
Fork
5.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vectorize-io/hindsight

Tutte le issue di vectorize-io/hindsight

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.