Skip to content

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

Open
Sycun wants to merge 7 commits into
AIPentest:mainfrom
Sycun:feat/local-model-context-profile
Open

Sycun wants to merge 7 commits into
AIPentest:mainfrom
Sycun:feat/local-model-context-profile

Conversation

@Sycun

@Sycun Sycun commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

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.

Sycun added 7 commits October 21, 2026 00:12
- Probe Ollama, llama.cpp, LM Studio and vLLM runtime context windows, including reverse-proxy subpaths
- Add context_window and context_profile=local: clamp budgets, compaction triggers, tool-output limits and always-visible tools
- Count tool schemas in the soft input budget and apply a 10% token estimate margin for local tokenizers
- Prefer max_tokens for local requests across agent, attack-chain, FOFA, workflow and HITL paths, and honor channel extra_request_fields
- Recognize local context-overflow errors and retry with aggressive compaction
The warning was computed after the runtime config had already been clamped,
so configured and effective values were identical and the clamp warning was lost.
- Surface llama.cpp parallel slots via context_slots and a warning instead of treating n_ctx as the per-request window
- Pass max_completion_tokens into detection previews and warn when the output cap is clamped
- Clamp the effective budget to the window and use the assumed local window when clamping output
- Overwrite a stale window on explicit auto-detect so the preview matches the saved value
- Keep the budget-warning test repeatable and document failover/budget semantics
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.

1 participant