Repository navigation
Studio: add Chat settings to disable or ease GGUF auto-compaction - #9774
mahiatlinux merged 48 commits into
Conversation
Local GGUF chats always opted into truncate_oldest, then compacted at the prompt budget with a 25% extra trim (or a full checkpoint reset). Settings now let you turn that off, switch to a sliding window, and choose how much extra room a rolling compaction takes. Compaction still keys off the configured context length, not free VRAM.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 53cdb3e071
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Send checkpoint/error overflow fields from Studio so Chat settings override UNSLOTH_CONTEXT_POLICY and UNSLOTH_CONTEXT_OVERFLOW. Refuse checkpoint-origin sticky boundaries when a request forces rolling, and route search_conversation from the same effective policy as the fitter.
|
The direction here is right, but chat-adapter.ts re-implements the same branching that ggufCompactionRequestFields already covers, so the tested helper is never called on the real request path. Could you have the adapter spread that helper instead? |
|
Done in 0d87970 — |
Keep both the Chat compaction request overrides and main's tool_choice parameter on generate_chat_completion_with_tools.
|
Fixed and pushed in Findings and fixes
Regression coverage
Validation
The fixes are split across eight focused commits, plus merge commits needed to bring the branch onto current |
|
@codex review |
…n-settings-7bf8 Keep main's checkpoint-recall helper on the GGUF tool-loop gate, and pass the request into _checkpoint_needs_search so Chat compaction settings still override the process context policy.
… moves Source-inspection contracts still pointed at openai_chat_completions after unslothai#9187 split produce_openai_chat_completions out, and at strings that unslothai#7880 and unslothai#9683 moved. Point them at the live sites and defer PEP 604 unions in the new kv-cache estimate tests.
tests/studio/test_pip_cache_naming.py was already listed at line 141 of .github/workflows/workflow-trigger-lint.yml, so the Workflow guard suites step collected it twice. The trailing newline it also removed goes back.
fit_rolling_context gated the 5% floor on each eviction bite on `headroom > 0`, but keeps_boundary = False zeroes the headroom for every threadless API request and incognito chat. Those callers send no new field, so they were handed the "no extra trim" behaviour anyway: on a 201-message transcript at context_length 1100 eviction fell from 12 messages to 2. Gate it on the requested ratio instead, so only a caller that actually asked for no extra trim gives the floor up.
_sticky_compaction_boundary rejected any boundary whose recorded policy differed from the process policy, in both directions. The rolling-to-checkpoint direction is only unsafe where fit_checkpoint_context would read the boundary as an epoch already in force and skip the reset's recall, and that needs _can_reset_epoch. Threads it refuses -- a template that cannot render tools, --disable-tools, no conversation archive -- compact through fit_rolling_context and record a rolling boundary, so on a checkpoint-default server every later turn discarded it and the boundary slid again. Measured on a stored boundary of 6: base restored 6, this branch returned 0. Each call site now computes _can_reset_epoch once and hands the same value to the boundary read and to the fit.
refreshContextUsage forwards the marker, but ChatCountTokensRequest only carried it through extra = "allow", which coerces nothing. A client sending the JSON string "false" reached _only_studio_tool_history as a truthy value and moved the count onto the Unsloth tool path the completion never takes, so the context-usage bar priced a different prompt. The model's own bypass_permissions field is declared for the same reason.
Renaming _only_studio_memory_tool_history to _only_studio_tool_history replaced its docstring with three lines that say what the marker is and no longer say why the strict route exists: a replayed tool pair read as a client contract sends the turn to the llama-server passthrough, which runs no context fit, so the epoch, the carried-forward block and the automatic recall are gone one turn after the compaction that created them. Put that back, and match the sentence case the rest of the file and the api.ts field comments use.
Review and fixesReviewed the branch at Environment: macOS 27.0.0, Python 3.13 in a per-worktree What was found1. A rolling boundary was discarded on every turn for threads that cannot start an epoch ( The
Measured on a stored boundary of 6 with
Each of the three call sites now computes 2. The eviction bite changed for requests that sent no new field (
Measured on a 201-message transcript with
The floor is now gated on the requested ratio, so only a caller that actually picked "no extra trim" gives it up. The zero-headroom behaviour the PR added is unchanged where it was asked for: 3.
4. A duplicate entry in
5. The renamed tool-history helper lost the constraint it documented (
VerificationEach fix ships with a regression test, and each test was mutation-checked by reverting only the source change:
Suites run:
Not covered
|
|
@codex review |
3a0ce7e to
ecc7384
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ecc7384830
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A request that may not reset still takes the checkpoint REPLAY branch in _fit_context under a checkpoint policy, so the rolling-origin boundary the previous commit restored reached fit_checkpoint_context and read as an epoch already in force. The fit returned checkpoint_started false, and _archive_and_recall reads that as "already recalled" and injected nothing. Measured at context_length 1800 with a stored boundary of 30: the reply lost the archived turns the rolling fallback would have pulled back. _sticky_compaction_state now returns the depth and whether a checkpoint reset recorded it, mixed provenance across surviving rows counting as rolling since min(boundaries) may pick either row. _fit_context takes that as sticky_is_checkpoint and enters the replay branch only for a real epoch; otherwise the count stays on the rolling fitter, which reports no checkpoint_started and still recalls.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a41dff2cf6
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Phase one of fit_rolling_context re-applies the saved boundary before anything else and returns as soon as the result fits, so an already-compacted chat that moved from 25% extra room down to 10%, 5% or none replayed the deeper cut and never reached the phase that reads the new ratio. Lowering the setting did nothing at all, and the history the client had re-sent stayed dropped. The persisted truncation now carries boundary_headroom_ratio beside boundary_messages and boundary_anchor, written through one _boundary_metadata helper so the three cannot drift apart. _sticky_compaction_state refuses a rolling boundary recorded under a larger ratio than this request wants; a larger request needs no refusal, since phase two moves the boundary out by itself. Rows saved before the ratio existed carry no key and replay as they did, and a checkpoint reset ignores headroom so its depth is never refused over it.
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 07f9db9fcc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
The previous commit only refused a boundary recorded under MORE headroom than the request asked for, on the reasoning that phase two would widen a shallower one by itself. It does not: phase one re-applies the saved count and phase two runs only when the result still does not fit, so a shallower boundary that already fits is just as inert. Measured on a 121-message transcript at context_length 1600, a stored boundary of 86 gave dropped 86 under both 0.05 and 0.25, so raising the setting did nothing either. Compare for equality instead. With the 5%-origin boundary of 86 above, a request at 0.05 still replays it and a request at 0.25 recomputes to 94.
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
mahiatlinux
left a comment
There was a problem hiding this comment.
All reviewed and fixed, LGTM. https://github.com/unslothai/unsloth/actions/runs/33053837580
Eight fix commits on top of the original work, Codex converged clean on b790abb8c ("Didn't find any major issues"), and all ten review threads are resolved.
CI on b790abb8c: 41 passing, 6 failing. Every one of the six is a pre-existing main breakage that reproduces with this branch absent, at the identical job and step:
| Failing check | Step | Reproduced on main |
|---|---|---|
| Frontend build + bundle sanity | Browser smoke for ANSI tool output | 33044821662, 33041836046, 33040513019 |
| Repo tests (CPU) | Repo tests (CPU, auto-discovered) | 33052756239, 33052084858, 33049388996 |
| Core (HF=default + TRL=default) | unsloth_zoo @ main — full pytest (CPU) | 33049388896 |
| Core (HF=4.57.6 + TRL<1) | unsloth_zoo @ main — full pytest (CPU) | 33049388896 |
| Core (HF=latest + TRL=latest) | unsloth_zoo @ main — full pytest (CPU) | 33049388896 |
| Chat UI, API, Update and GGUF inference | Assert llama.cpp loads on this macOS | 33044821603, 33041836104 |
The frontend one is a real bug and worth its own fix: the ANSI smoke dies on Cannot access 'CHAT_GPU_MEMORY_MODE_KEY' before initialization, a module cycle reached through the @/features/chat barrel. Same error text on a main-only run. Frontend CI last passed on main at fd31302d1 and has been red on every commit since 3fd9dc0e8 / 54367e591, the latter being this PR's own merge base. The three Core legs fail against unsloth_zoo @ main, a moving external dependency, and all three fail on main where only two fail here. main additionally fails (Python 3.13) :: Backend tests, which passes on this branch.
None of the six is caused by this PR, and none is fixable from inside it without mixing an unrelated main repair into a feature change.
What the eight commits fixed
Backend correctness, each with a regression test that was mutation-checked by reverting only the source change:
- A rolling boundary was discarded every turn for threads that cannot start an epoch.
_sticky_compaction_boundaryrejected any boundary whose recorded policy differed from the process policy, in both directions._can_reset_epochis false whenever the model's template cannot render tools, the tool loop is off, or the archive is unavailable, and those threads compact throughfit_rolling_contextand record a rolling boundary, so on a checkpoint-default server the boundary slid again on every reply. Measured on a stored boundary of 6: base restored 6, the branch returned 0. - A rolling boundary reached the checkpoint replay branch. A request that may not reset still takes that branch, so the boundary restored by (1) was read as an epoch already in force:
fit_checkpoint_contextreturnedcheckpoint_startedfalse and_archive_and_recallinjected nothing. Measured atcontext_length=1800with a boundary of 30._sticky_compaction_statenow reports provenance and the branch fires only for a real epoch. - Changing the extra-trim ratio did nothing on an already-compacted chat. Phase one of
fit_rolling_contextre-applies the saved count and stops once the result fits, so the phase that reads the new ratio never ran. Measured on a 121-message transcript atcontext_length=1600: a stored boundary of 86 gavedropped=86under both 0.05 and 0.25. The truncation now carriesboundary_headroom_ratioand a changed ratio forces a refit. Both directions, after Codex correctly pointed out my first attempt only handled decreases. - The eviction bite changed for requests that sent no new field. The 5% floor on each bite was gated on
headroom > 0, butkeeps_boundary = Falsezeroes the headroom for every threadless API request and incognito chat. Measured atcontext_length=1100: eviction fell from 12 messages to 2 for a caller sending nothing new, contradicting "API clients are unchanged". Gated on the requested ratio instead. studio_tool_historywas undeclared onChatCountTokensRequest. It arrived throughextra = "allow", which coerces nothing, so the JSON string"false"read as truthy and moved the count onto the Unsloth tool path the completion never takes.- A duplicate
tests/studio/test_pip_cache_naming.pyin.github/workflows/workflow-trigger-lint.yml, already listed at line 141, plus a removed trailing newline. Unrelated to this PR; reverted to the base file. - The renamed tool-history helper lost the constraint it documented, and its docstring was restored along with the sentence case the rest of the file uses.
Verification
test_checkpoint_compaction.py,test_context_overflow_truncation.py,test_chat_settings_payload.py,test_conversation_recall_injection.py: 291 passed. The single failure,test_the_reachability_probe_closes_the_connection_it_opens, reads/proc/self/fdand fails identically on the base commit; a macOS-only environment limit.- Whole
studio/backend/testssuite, unmodified head versus this branch on the same interpreter and dependency set: failure sets compare equal name for name across four runs. The ~248 common failures are this laptop missing torch, transformers, peft and torchao. - Frontend:
npm test5653 passed, 0 failed.npm run typecheckclean.npm run i18n:check:strictreports 0 missing keys across all 12 locales. ruff check studio/backendclean. Merges cleanly into currentmain.
Not covered
- No GPU here, so no live GGUF chat drove a real compaction end to end. The fits, boundary reads and request shapes are covered by unit tests against the real functions.
- No UI screenshots; the two new Chat settings rows render from existing
SettingsRow,SwitchandSelectprimitives. - Multi-turn behaviour of a policy switch in a real session, as opposed to per-fit, is untested on either tree.
) * Studio: add Chat settings to disable or ease GGUF auto-compaction Local GGUF chats always opted into truncate_oldest, then compacted at the prompt budget with a 25% extra trim (or a full checkpoint reset). Settings now let you turn that off, switch to a sliding window, and choose how much extra room a rolling compaction takes. Compaction still keys off the configured context length, not free VRAM. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Studio: honor Chat compaction settings over process defaults Send checkpoint/error overflow fields from Studio so Chat settings override UNSLOTH_CONTEXT_POLICY and UNSLOTH_CONTEXT_OVERFLOW. Refuse checkpoint-origin sticky boundaries when a request forces rolling, and route search_conversation from the same effective policy as the fitter. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * honor zero compaction headroom * test zero-headroom rolling compaction * preserve server compaction policy by default * test server policy inheritance * mark studio-owned tool history * keep studio tool history on the fitted path * isolate studio tool history provenance * test compaction after local tool use * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * complete locale settings translations * fix sanitized catalog guard coverage * run pip cache guard on workflow changes * set utf-8 for pip cache guard reads * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * mark studio tool history in token counts * reset boundaries when context policy changes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * mark studio tool history from replayed calls Hosted server builtins such as web_search are dropped during OpenAI serialization, so ownership must be derived from the calls that actually go on the wire. Mixed hosted-plus-local history can then keep the studio_tool_history marker and stay on the compaction path. * align prune contract test with token-count history builder The send and count paths both prune through pruneOutboundHistory into survivingMessages; the source contract still expected the old outboundMessages name on the count path. * align sticky and token-count harnesses with current compaction APIs Recall fixtures store rolling-shaped truncation records, so pin CONTEXT_POLICY to rolling when faking studio_db. Recount and prune harnesses follow the renamed token-count builder, a tighter setActiveThreadId slice, and the send-path prune call rather than the earlier count-path copy. * test(studio): follow main's chat-completions extract and VRAM/keyless moves Source-inspection contracts still pointed at openai_chat_completions after #9187 split produce_openai_chat_completions out, and at strings that #7880 and #9683 moved. Point them at the live sites and defer PEP 604 unions in the new kv-cache estimate tests. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * drop the duplicate workflow guard entry this pr added tests/studio/test_pip_cache_naming.py was already listed at line 141 of .github/workflows/workflow-trigger-lint.yml, so the Workflow guard suites step collected it twice. The trailing newline it also removed goes back. * keep the eviction bite for requests that chose no compaction setting fit_rolling_context gated the 5% floor on each eviction bite on `headroom > 0`, but keeps_boundary = False zeroes the headroom for every threadless API request and incognito chat. Those callers send no new field, so they were handed the "no extra trim" behaviour anyway: on a 201-message transcript at context_length 1100 eviction fell from 12 messages to 2. Gate it on the requested ratio instead, so only a caller that actually asked for no extra trim gives the floor up. * replay a rolling boundary where the fit cannot start an epoch _sticky_compaction_boundary rejected any boundary whose recorded policy differed from the process policy, in both directions. The rolling-to-checkpoint direction is only unsafe where fit_checkpoint_context would read the boundary as an epoch already in force and skip the reset's recall, and that needs _can_reset_epoch. Threads it refuses -- a template that cannot render tools, --disable-tools, no conversation archive -- compact through fit_rolling_context and record a rolling boundary, so on a checkpoint-default server every later turn discarded it and the boundary slid again. Measured on a stored boundary of 6: base restored 6, this branch returned 0. Each call site now computes _can_reset_epoch once and hands the same value to the boundary read and to the fit. * declare studio_tool_history on the token-count request refreshContextUsage forwards the marker, but ChatCountTokensRequest only carried it through extra = "allow", which coerces nothing. A client sending the JSON string "false" reached _only_studio_tool_history as a truthy value and moved the count onto the Unsloth tool path the completion never takes, so the context-usage bar priced a different prompt. The model's own bypass_permissions field is declared for the same reason. * restore the constraint the renamed tool-history helper documented Renaming _only_studio_memory_tool_history to _only_studio_tool_history replaced its docstring with three lines that say what the marker is and no longer say why the strict route exists: a replayed tool pair read as a client contract sends the turn to the llama-server passthrough, which runs no context fit, so the epoch, the carried-forward block and the automatic recall are gone one turn after the compaction that created them. Put that back, and match the sentence case the rest of the file and the api.ts field comments use. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * keep a rolling boundary off the checkpoint replay branch A request that may not reset still takes the checkpoint REPLAY branch in _fit_context under a checkpoint policy, so the rolling-origin boundary the previous commit restored reached fit_checkpoint_context and read as an epoch already in force. The fit returned checkpoint_started false, and _archive_and_recall reads that as "already recalled" and injected nothing. Measured at context_length 1800 with a stored boundary of 30: the reply lost the archived turns the rolling fallback would have pulled back. _sticky_compaction_state now returns the depth and whether a checkpoint reset recorded it, mixed provenance across surviving rows counting as rolling since min(boundaries) may pick either row. _fit_context takes that as sticky_is_checkpoint and enters the replay branch only for a real epoch; otherwise the count stays on the rolling fitter, which reports no checkpoint_started and still recalls. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * discard a boundary cut with more extra trim than the request asks for Phase one of fit_rolling_context re-applies the saved boundary before anything else and returns as soon as the result fits, so an already-compacted chat that moved from 25% extra room down to 10%, 5% or none replayed the deeper cut and never reached the phase that reads the new ratio. Lowering the setting did nothing at all, and the history the client had re-sent stayed dropped. The persisted truncation now carries boundary_headroom_ratio beside boundary_messages and boundary_anchor, written through one _boundary_metadata helper so the three cannot drift apart. _sticky_compaction_state refuses a rolling boundary recorded under a larger ratio than this request wants; a larger request needs no refusal, since phase two moves the boundary out by itself. Rows saved before the ratio existed carry no key and replay as they did, and a checkpoint reset ignores headroom so its depth is never refused over it. * refuse a replayed boundary on any change of extra trim, not just a cut The previous commit only refused a boundary recorded under MORE headroom than the request asked for, on the reasoning that phase two would widen a shallower one by itself. It does not: phase one re-applies the saved count and phase two runs only when the result still does not fit, so a shallower boundary that already fits is just as inert. Measured on a 121-message transcript at context_length 1600, a stored boundary of 86 gave dropped 86 under both 0.05 and 0.25, so raising the setting did nothing either. Compare for equality instead. With the 5%-origin boundary of 86 above, a request at 0.05 still replays it and a request at 0.25 recomputes to 94. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: mahiatlinux <[email protected]>
Summary
Fixes #9671.
Local GGUF chats always sent
context_overflow: "truncate_oldest", so Studio compacted at the configured context length (not free VRAM), then either reset the conversation (checkpoint, the default) or trimmed extra history (~25% of the prompt budget) so the boundary could stay put.This adds Chat settings to control that, without changing VRAM estimation:
context_overflow, so a full window errors instead of silently dropping history.Checkpoint vs sliding window is a per-request
context_policyoverride ofUNSLOTH_CONTEXT_POLICY. The extra trim is a per-requestcompaction_headroom_ratiooverride ofROLLING_COMPACTION_HEADROOM_RATIO. Unset requests keep process defaults, so API clients are unchanged.Tests
tests/auto-compaction-settings.test.ts,tests/rolling-context-window.test.ts, settings search / chat-settings payload round-trip testscontext_policy=rollingforces the sliding window even when checkpoint is the process default, Chat settings payload bounds