Skip to content

fix(agent): fix Ollama context-window truncation and strengthen integration tests - #1447

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1443-ollama-context-window
Jun 22, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1443-ollama-context-window

Conversation

@malibio

@malibio malibio commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Closes #1443

Summary

  • Adds num_ctx to OllamaOptions and sets it from model info on every tool-bearing request, eliminating the 4096-token default context truncation
  • Fixes OllamaShowResponse field name (modelinfo → model_info) so serde correctly deserializes the Ollama /api/show response
  • Fixes context key lookup to try family-specific keys (gemma4.context_length, llama.context_length, etc.) before falling back; default fallback changed from 4096 to 32768
  • Caches context window in OnceCell<u32> to avoid a per-request /api/show round-trip
  • Strengthens test_ollama_inference_with_tools to assert non-empty response
  • Adds test_ollama_model_info_context_window_detection (pins serde field name + key lookup)
  • Adds test_ollama_inference_with_production_prompt (full system prompt + 14 tools, guards against silent truncation)

Test plan

  • bun run test:all passes (140 frontend + 957 Rust unit tests, no new failures)
  • Integration tests skip gracefully when Ollama is not running
  • When Ollama is running with gemma4:e4b, all three new tests pass

🤖 Generated with Claude Code

Michael Libio and others added 2 commits June 22, 2026 17:10
…ration tests (closes #1443)

Three compounding bugs caused Ollama to silently truncate at 4096 tokens:
1. `OllamaOptions` was missing `num_ctx` — Ollama defaulted to 4096 regardless of model capacity.
   Now fetches context window from /api/show and sets num_ctx on all tool-bearing requests.
2. `OllamaShowResponse` deserialised field `modelinfo` but the Ollama API returns `model_info` —
   fixed with `#[serde(rename = "model_info")]`.
3. Context key lookup only tried `llm.context_length` but Gemma 4 uses `gemma4.context_length`.
   Fixed by trying a list of known family-specific keys, then falling back to any key ending in
   `.context_length`. Default fallback changed from 4096 to 32768.

Integration test changes:
- `test_ollama_inference_with_tools`: asserts non-empty response (tool call OR text token);
  empty responses are now treated as inference failures, not valid outcomes.
- New `test_ollama_model_info_context_window_detection`: asserts context_window > 4096,
  pinning the serde field name and key lookup correctness.
- New `test_ollama_inference_with_production_prompt`: sends a realistic production system prompt
  (~1,900 tokens) plus all 14 production tools against LocalAgentService, asserts non-empty
  response and completion_tokens > 1 (guards against silent truncation).

Co-Authored-By: Claude <[email protected]>
…inline helper

- Add OnceCell<u32> to OllamaInferenceEngine to cache the context window fetched
  from /api/show; eliminates the per-request /api/show round-trip on multi-turn
  conversations (was one HTTP call per generate() invocation with tools).
- Remove redundant #[serde(rename = "model_info")] — serde's default snake_case
  mapping already handles this; the attribute implied the JSON key differs from
  the Rust field name, which is misleading.
- Inline completion_tokens_indicate_real_response (was a one-liner called once);
  added an explanatory comment so the > 1 threshold is self-documenting.

Skipped: production tool definition helper — the two call sites serve genuinely
different purposes (full production schemas vs. minimal stubs) and a shared
helper would obscure that distinction without reducing maintenance burden.

Co-Authored-By: Claude <[email protected]>
@malibio
malibio merged commit 13f92c8 into main Jun 22, 2026
@malibio
malibio deleted the worktree-issue-1443-ollama-context-window branch June 22, 2026 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ollama integration tests don't catch context-window truncation causing empty responses

1 participant