Skip to content

Studio: compact long chats on self-hosted connections to the window the server reports - #12975

Merged
mahiatlinux merged 2 commits into
unslothai:mainfrom
oobabooga:worktree-served-context-window
Oct 8, 2026
Merged

mahiatlinux merged 2 commits into
unslothai:mainfrom
oobabooga:worktree-served-context-window

Conversation

@oobabooga

Copy link
Copy Markdown
Member

With Auto-compact on, a long chat on a llama.cpp, vLLM or Custom connection is never compacted. Studio sends the whole history every turn until the server refuses it. llama-server answers request (10613 tokens) exceeds the available context size (4096 tokens), or, when the prompt still fits, the reply stops early with finish_reason: length.

This was reported on Reddit by a user running Qwen Next on a llama.cpp server over a LAN connection (1wzf52a) and by a user running NInfer (1wu6h0w). #12567 fixed the misleading Max Tokens message in the second report, but not the missing compaction.

Root cause

  • apiCompactionRequestFields sends context_overflow, compaction_threshold and context_window only when resolveModelCatalogEntry knows the model's window. Otherwise it sends nothing.
  • The catalog cannot know it for these connections. The bundled snapshot has no custom, llama_cpp or vllm namespace, and the live catalog is fetched only for OpenRouter. The window is also not a property of the model: it is whatever the server was started with (llama-server -c/-np, vLLM --max-model-len).
  • _proxy_to_external_provider runs _fit_external_context, the oldest-turn fit added in Studio: compact long chats on API models too #12786, only when compaction_threshold is present, so it never runs for these connections.

Fix

  • Frontend: with Auto-compact on and no catalogued window, apiCompactionRequestFields now sends context_overflow: "truncate_oldest" on its own.

  • Backend: when a request asks for truncate_oldest with no compaction_threshold, and the provider does not compact on its own side, _proxy_to_external_provider takes the window from the request's context_window, or else from the server. It uses three quarters of that window as the threshold, the same rule the frontend uses. The existing fit then runs unchanged, on both the plain path and the Studio tool loop.

  • ExternalProviderClient.served_context_window reads the window for llama_cpp, vllm and custom connections from GET /models:

    • llama-server's meta.n_ctx, the per-slot runtime window (not meta.n_ctx_train);
    • max_model_len, which vLLM, SGLang and NInfer report;
    • for a vLLM or SGLang LoRA adapter, the window of its parent entry;
    • for llama-server builds before meta.n_ctx (added between b9000 and b9500), /props default_generation_settings.n_ctx, with ?model= so the read also works in router mode.

    Both reads share one 5 s deadline. The result is cached for 60 s per base URL, API key digest and model, and a server that reports no window is cached too. A refused connection is not cached, so a server that is down or restarting is read again on the next turn. A server restarted with a smaller window is still fitted to the cached one for up to 60 s, so a turn in that period can hit the server's limit as on main.

Behavior that stays as on main:

  • Auto-compact off: the request is unchanged and the server is not read. The probe needs an explicit truncate_oldest, so the UNSLOTH_CONTEXT_OVERFLOW server default cannot turn compaction on behind the switch.
  • Other providers: hosted providers, Ollama, and any model the catalog already covers behave as before.
  • Servers that report no window: the whole chat is forwarded as before. This includes LM Studio behind a Custom connection, which publishes its loaded context length on its native REST API (loaded_instances[].config.context_length on /api/v1/models), not on the OpenAI-compatible /models read here.
  • How compaction works: it drops the oldest complete turns and keeps the system prompt, the same as other API models since Studio: compact long chats on API models too #12786. There is no summary and no middle truncation.
  • Managed NPU runtime: untouched.

Testing

Live, with this branch's Studio in front of a real llama-server (gemma-3-1b-it Q4_K_M, -c 8192 -np 2 --no-kv-unified). The server reported meta.n_ctx 4096 and meta.n_ctx_train 32768. The chat was a 22-message, roughly 10.6k-token history sent through /v1/chat/completions with each provider type. Auto-compact off sends the same request main sends for these connections.

Connection type Auto-compact off Auto-compact on
llama_cpp exceed_context_size_error (10613 > 4096) Read 4096, compacted at 3072, dropped 16 messages, replied 2 + 2 = 4
custom same error same as above
vllm same error same as above

After the llama-server process was stopped and started again, the next request compacted normally.

  • 12 new tests in test_external_auto_compaction.py cover:

    • llama-server meta.n_ctx (and that n_ctx_train is not used), vLLM max_model_len through a Custom connection, a LoRA adapter, the /props?model= fallback, a request's own window, and the Studio tool loop;
    • a server that reports nothing, Auto-compact off, a hosted provider, a rejected key next to a valid one, a refused connection, and a stalled server.

    The six positive tests fail with main's route. The file passes 25/25, and together with the seven adjacent provider and compaction test files: 146 passed, 42 skipped.

  • auto-compaction-settings.test.ts and model-catalog.test.ts: 39 passed. The new test checks that custom, llama_cpp and vllm have no catalogued window and still send truncate_oldest. tsc for the app and test configs and eslint on the changed files passed. The full frontend suite has 12 failures, and the same 12 fail with main's versions of the two changed files.

  • The Python files were formatted with the repository's pinned ruff 0.6.9.

Not tested

  • A real vLLM, SGLang or NInfer server, or an older llama-server that lacks meta.n_ctx. Those are covered by fake servers built from the upstream response shapes.
  • llama.cpp router mode.
  • Clicking through the chat UI. The live run called the API directly, and the frontend change is the pure function covered by its test.
  • The probe adds up to 5 s before the first token on a cache miss when the server is slow to answer /models. Locally it answered in milliseconds.

@oobabooga

Copy link
Copy Markdown
Member Author

Reached Codex review convergence at oobabooga#343.

@mahiatlinux

Copy link
Copy Markdown
Contributor

Review and verification evidence

Expected: with Auto-compact enabled and no catalogued model window, a self-hosted connection should read the server's active context window and drop complete oldest turns before forwarding an oversized chat. Auto-compact off, hosted providers, and servers that report no window should keep their previous behavior.

Observed A/B behavior:

  • Merge base 0e8fc1ab9f2db29a004c04a7e4a0bb2d524eb088: the control forwarded all 26 messages and failed the claimed compaction assertion.
  • Original source head fcb5896f76cd6c415d9c1d525cb042a569ad85c6: the same control compacted the chat and emitted context_truncated metadata.
  • Prospective source merge b1b42d9675bf9272880a166f6827cecdcf477351: the focused backend and frontend suites passed.
  • Final mirror head 034e8748c6eebf7b2cbd9042bc6736f1505bdbc8: adds only the required comment cleanup; its affected source trees match the verified repaired source tree.
  • Final source head 60ce4058d0cb7388ba0d989ea0acb97e212c9e85: the same comment-only cleanup was landed through the mirror workflow. Codex completed its review on the equivalent mirror tree with no major issues and no inline findings.

Executed checks:

  • pytest tests/test_external_auto_compaction.py: 25 passed on the original head, prospective merge, and final repaired source tree.
  • Adjacent backend provider, compaction, overflow, and tool-loop suites: 528 passed in 889.47 s.
  • Frontend auto-compaction-settings.test.ts plus model-catalog.test.ts: 39 passed on the original head, prospective merge, and final repaired source tree.
  • Negative mutation: restoring the old no-window {} result made the new frontend regression test fail for custom, proving the test detects the missing request field.
  • npm run typecheck: passed. ESLint on both changed TypeScript files: passed.
  • Repository ruff 0.6.9 formatter and ruff checks on the touched Python files: passed.
  • Comment-only gate: Python AST and TypeScript printer output unchanged across five cleaned files.
  • Source CI before the cleanup push: 66 successful and 4 skipped checks. CodeQL alert 1371 was dismissed as a false positive because SHA-256 only partitions an in-process API-key cache and is not password storage or authentication.

Environment: Linux 7.0.0-34-generic x86_64; CPython 3.13.14 in disposable uv environments; Node 26.8.2; uv 0.12.1. Contributor code ran with the host credential store hidden. No browser or GPU behavior is involved in this API and pure-function change.

Primary response-shape checks: llama.cpp documents the runtime n_ctx and router /props?model= behavior in its server source and server README; vLLM documents max_model_len as the serving request limit in its context extension guide.

@mahiatlinux mahiatlinux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Ready to merge, all manually checked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants