Repository navigation
Studio: compact long chats on self-hosted connections to the window the server reports - #12975
Conversation
…he server reports
|
Reached Codex review convergence at oobabooga#343. |
Review and verification evidenceExpected: 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:
Executed checks:
Environment: Linux 7.0.0-34-generic x86_64; CPython 3.13.14 in disposable Primary response-shape checks: llama.cpp documents the runtime |
mahiatlinux
left a comment
There was a problem hiding this comment.
LGTM! Ready to merge, all manually checked.
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 withfinish_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
apiCompactionRequestFieldssendscontext_overflow,compaction_thresholdandcontext_windowonly whenresolveModelCatalogEntryknows the model's window. Otherwise it sends nothing.custom,llama_cpporvllmnamespace, 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_providerruns_fit_external_context, the oldest-turn fit added in Studio: compact long chats on API models too #12786, only whencompaction_thresholdis present, so it never runs for these connections.Fix
Frontend: with Auto-compact on and no catalogued window,
apiCompactionRequestFieldsnow sendscontext_overflow: "truncate_oldest"on its own.Backend: when a request asks for
truncate_oldestwith nocompaction_threshold, and the provider does not compact on its own side,_proxy_to_external_providertakes the window from the request'scontext_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_windowreads the window forllama_cpp,vllmandcustomconnections fromGET /models:meta.n_ctx, the per-slot runtime window (notmeta.n_ctx_train);max_model_len, which vLLM, SGLang and NInfer report;parententry;meta.n_ctx(added between b9000 and b9500),/propsdefault_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:
truncate_oldest, so theUNSLOTH_CONTEXT_OVERFLOWserver default cannot turn compaction on behind the switch.loaded_instances[].config.context_lengthon/api/v1/models), not on the OpenAI-compatible/modelsread here.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 reportedmeta.n_ctx4096 andmeta.n_ctx_train32768. The chat was a 22-message, roughly 10.6k-token history sent through/v1/chat/completionswith each provider type. Auto-compact off sends the same request main sends for these connections.llama_cppexceed_context_size_error(10613 > 4096)2 + 2 = 4customvllmAfter the llama-server process was stopped and started again, the next request compacted normally.
12 new tests in
test_external_auto_compaction.pycover:meta.n_ctx(and thatn_ctx_trainis not used), vLLMmax_model_lenthrough a Custom connection, a LoRA adapter, the/props?model=fallback, a request's own window, and the Studio tool loop;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.tsandmodel-catalog.test.ts: 39 passed. The new test checks thatcustom,llama_cppandvllmhave no catalogued window and still sendtruncate_oldest.tscfor the app and test configs andeslinton 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
meta.n_ctx. Those are covered by fake servers built from the upstream response shapes./models. Locally it answered in milliseconds.