#367

feat: optimize local model context handling (32K-256K)

OpenSycunwants to merge intofrom

Summary

Optimizes how the agent uses local OpenAI-compatible models whose effective context is usually 32K-256K, while keeping cloud behavior unchanged unless the new opt-in profile is enabled.

  • Runtime window probing: new internal/modelmeta probes Ollama /api/ps + /api/show, llama.cpp /props, LM Studio /api/v0/models, then vLLM/OpenAI-compatible /v1/models. Reverse-proxy subpaths (e.g. https://host/ollama/v1) are supported, and runtime-vs-model-maximum uncertainty is surfaced as warnings.
  • Context profile: new per-channel context_window and context_profile: local. Local clamps the shared budget to 60% of the window (max 128K), keeps output reserve bounded, and tightens summarization triggers, tool-output truncation/clearing, user-message/ledger caps and plan step results. It only tightens explicit middleware settings.
  • Tool schema budget: the soft input budget now counts tool JSON schemas, so tool_search decisions reflect the real request size; local keeps a small always-visible core set and lets backend builtins enter the dynamic pool.
  • Local compatibility: local requests use max_tokens instead of max_completion_tokens across the agent, attack-chain, FOFA, workflow-draft and HITL paths; channel-level reasoning.extra_request_fields is now merged into those auxiliary payloads.
  • Safer estimation and recovery: local token estimates carry a 10% margin for tokenizer differences (CJK), and context-overflow detection now recognizes llama.cpp (exceeds the available context size), LM Studio, vLLM (max_model_len) and related messages so aggressive compaction retry can run.
  • Settings UI: context-window field with auto-detect, local profile selector, effective-budget preview, warning display, and save/validation for ai.channels.
  • Docs: bilingual configuration guide covering runtime window, Ollama num_ctx, KV cache/prefix caching, thinking models and minimum viable windows; config.example.yaml, CHANGELOG and all three i18n files updated.

Verification

  • go test ./... passes (full suite), go vet ./... passes, gofmt clean.
  • New unit/integration coverage: model probing incl. reverse-proxy prefix, budget math, middleware tightening/idempotency, tool-schema budget, local max_tokens for both model factories and summarization, overflow error classification, channel validation, and detect-context response preview.
  • End-to-end against a fake Ollama endpoint: detected 32768 and 131072 runtime windows; local profile previewed effective budgets 16384 and 78643; saved YAML round-tripped context_window/context_profile; invalid profile and negative window were rejected with 400; settings page rendered the detected result and saved the channel.

Notes

  • context_profile is opt-in; auto preserves existing behavior.
  • Ollama's /v1 endpoint ignores num_ctx; the docs explain the required OLLAMA_CONTEXT_LENGTH/Modelfile setup and why oversized prompts drop the system prompt first.