Repository navigation
Studio: let a cloud connection drop whole oldest turns instead of summarizing - #18211
danielhanchen merged 24 commits into
Conversation
…marizing (unslothai#18189) A connection's new "When the chat is full" option chooses between the summary call (default, unchanged) and "Drop oldest turns", which evicts whole oldest turns into the thread's archive with no summary call, as a local model does. /compact on such a connection starts a checkpoint epoch, and a provider's overflow refusal evicts and resends without a summary.
…t off The chat adapter sent nothing with auto-compact off, so the backend never saw compaction_mode "evict" and forwarded the whole chat on the turn after /compact.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
The overflow refit halved the unfitted transcript, so a prompt the trigger had already shortened came back as large or larger and was refused again. It now evicts to half of the prompt that was sent, through the evicting fit, and keeps that bound for the tool loop's later passes. [skip ci]
…st turns connection Checkpoint eligibility on the plain path now follows the model's tool support, as on managed engines, instead of whether this request runs the tool loop. A replayed boundary with context_overflow "error" no longer re-enables threshold eviction.
The overflow refit on a Drop oldest turns connection now applies the same model capability check as the plain fit, so a model that never receives Studio tools evicts rolling instead of starting a checkpoint. [skip ci]
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
compaction_mode is now summary, rolling or checkpoint. Drop oldest turns maps to rolling, which calls the fit exactly as cloud chats did before summaries (can_reset and replays_manual off), and Reset to latest turn keeps the checkpoint policy. [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c4c9062c2
ℹ️ 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".
| _evicting_payload = payload.model_copy( | ||
| update = { | ||
| "compaction_threshold": payload.context_window or _EXTERNAL_COMPACTION_THRESHOLD_MAX |
There was a problem hiding this comment.
Replay saved epochs without enabling new automatic evictions
When a saved /compact epoch grows past the fallback budget—for example, a checkpoint request with context_overflow: "error" and a known context_window—substituting the window here still runs a normal evicting fit with new resets enabled. _fit_external_context reserves reply room and _fit_context can start another checkpoint, dropping turns added after the manual boundary even though automatic trimming is disabled. Use replay-only fitting in this case; another finite threshold does not preserve the error/auto-off policy.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: it needs Reset to latest turn, a /compact epoch, auto-compact off or the error policy, and a chat past the model's whole window. There the only alternative is the provider's too-long error, and the turns dropped are archived and recalled, so nothing is lost.
Instead of estimating what the fit keeps, try half the refused prompt and loosen in eighths until the fit drops turns and fits, so pinned turns, re-prompts and evictable tool exchanges are judged by the fit itself. [skip ci]
…efused prompt A latest turn larger than the eighths allow still got nothing dropped; the final attempt now budgets one token under the refused prompt.
…ript The fit shortens oversized attachments in place, so a tight first probe could leave every looser probe with a cut file and nothing to evict. [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
# Conflicts: # studio/backend/routes/inference.py [skip ci]
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 94b9a224c5
ℹ️ 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".
| if truncation and truncation.get("dropped_messages") and truncation.get("fits"): | ||
| break |
There was a problem hiding this comment.
Prefer whole-turn eviction before shortening the latest attachment
If an overflowed prompt contains a sandbox-backed attachment in the latest user turn and substantial older history, _fit_external_context can shorten that attachment to the first half-size probe and then drop the older turns. This makes fits true and exits the loop, even when a later, looser bound would preserve the complete attachment after evicting those same older turns. The model therefore unnecessarily loses part of the current user input. Try whole-turn-only fits before accepting a candidate that modifies a surviving attachment.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing this: the half-size bound is deliberate so later tool-loop passes stay well under the refused size, and a cut attachment keeps its start plus the sandbox path the python tool reads, as the normal fit does on a tight budget. Nothing is lost; this only tunes how much of the file stays inline on the overflow path.
…tion The fallback for an OpenAI/Azure deployment that rejects context_management went through the summary planner whatever the connection's mode; it now takes the same evicting fit a drop-mode connection uses elsewhere. [skip ci]
Later continuations kept rebuilding the payload, dropping any overflow bound a retry had tightened.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 65f1bf32cc
ℹ️ 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".
| _fit_active = _auto_summary or ( | ||
| managed is None and (_manual_summary_pending or _has_saved_summary) | ||
| ) | ||
| if _evict_only: |
There was a problem hiding this comment.
Replay fallback checkpoints with auto-compaction disabled
On a Responses deployment that rejects native compaction, /compact in checkpoint mode can create a local manual epoch through _compaction_fallback. On the next Studio turn with auto-compaction off, apiCompactionRequestFields sends only the mode, but _provider_compacts is recomputed as true, making _evict_only false and skipping this saved-epoch check. No fallback callback is installed either, because no threshold is sent. The full transcript is therefore forwarded, resurrecting the messages excluded by the manual checkpoint. Replay existing local epochs independently of whether this request attempts native compaction, or retain the deployment's rejected-capability result across requests.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Out of scope for this PR, so the fallback changes are reverted in the latest push: the new setting applies where Unsloth compacts the chat itself. A deployment that advertises native compaction keeps it, and if it refuses, falls back exactly as on main (a summary). Making the drop modes drive that fallback needs the refusal remembered across requests, which belongs in its own PR.
| _evicting_payload = _first_payload = payload.model_copy( | ||
| update = {"context_policy": payload.compaction_mode} | ||
| ) |
There was a problem hiding this comment.
Refit the raw transcript when entering a drop-mode fallback
When a Responses deployment rejects native compaction and the thread already has an Unsloth summary, this fallback receives messages that _plan_external_compaction has already summarized: _evict_only was false during the initial fit, so saved-summary replay remained enabled. Changing the policy here does not recover the original turns, and a summarized prompt below the trigger is returned unchanged. Consequently, switching either drop mode on continues sending the old lossy summary on requests without a Studio tool loop, contrary to the new modes' saved-summary behavior. Start the first drop-mode fallback from the unsummarized transcript, or suppress saved-summary replay for the requested drop modes.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Out of scope for this PR, so the fallback changes are reverted in the latest push: the new setting applies where Unsloth compacts the chat itself. A deployment that advertises native compaction keeps it, and if it refuses, falls back exactly as on main (a summary). Making the drop modes drive that fallback needs the refusal remembered across requests, which belongs in its own PR.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! 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". |
Fixes #18189
What changes for users
A connection (Settings > Connections > edit) gains When the chat is full, next to "Auto-compact after":
search_conversationfinds them.search_conversationand inline recall.The thread shows "Context compacted" instead of "Earlier messages summarized" for both drop modes.
/compactWith either drop mode, a summary saved for the thread before the switch is not replayed; the turns it covered are evicted like any others. The transcript is kept on disk in every mode, as before.
Anthropic and OpenAI models that compact on the provider's side keep doing so; the option only applies where Unsloth compacts the chat itself. A deployment that advertises native compaction but refuses it falls back exactly as on main (a summary).
How
compaction_mode("summary"|"rolling"|"checkpoint", null = summary) on the connection: new nullablellm_providers.compaction_modecolumn (added by the existing schema migration),ProviderCreate/ProviderUpdate/ProviderResponse, create/update/restore inroutes/providers.py. The values match the existingcontext_policynames.compaction_moderequest field ([x-unsloth]) onChatCompletionRequest. The chat adapter sends it from the connection for the two drop modes; the default sends nothing, so requests from other clients are unchanged.routes/inference.py: a"rolling"request calls_fit_external_contextwithcan_reset=Falseandreplays_manual=False, the exact arguments the cloud path used before Studio: summarize long cloud chats instead of dropping their oldest turns #17743. A"checkpoint"request takes the evicting fit the managed engines use (can_reset/replays_manual), so archive, recall and/compactepochs behave as they do locally. An overflow refit tries half of the refused prompt, loosens in eighths, then a bound one token under the refused prompt, each on its own copy of the transcript, until the fit drops turns and fits; the reply cap stays clamped to the window. With auto-compaction off, only a"checkpoint"/compactepoch is applied.compactionModeon the connection config, synced from the backend, saved by the dialog (editing back to the default clears it to null).Testing
8fc3ae284, the parent of Studio: summarize long cloud chats instead of dropping their oldest turns #17743's merge) and through this branch withcompaction_mode: "rolling", using the same proxy harness: three plain long chats (12, 16 and 30 turns at different triggers) and two archive-enabled chats with tools off and on. The provider received identical messages in all five, with the same dropped, archived and recalled counts. With an archive,"checkpoint"sent 2 messages where the old fit and"rolling"sent 6.studio/backend/tests/test_cloud_evict_compaction.py(31 tests): no summary call, whole-turn eviction, saved summary not replayed, archive + recall,/compactepochs and their replay with auto-compaction off, overflow refusals, connection storage, and two new ones: rolling keeps more turns than checkpoint on the same chat (both recall), and rolling never starts a checkpoint on/compact. Both new tests fail when"rolling"is routed like"checkpoint".node scripts/typecheck.mjs --testsclean; fullnpm test16652 passed, 1 skipped, 0 failed.Follow-up fixes
/compactcheckpoint stays in force on later turns (before, the next turn forwarded the whole chat).context_overflow: "error"no longer turns threshold eviction back on.Connection form, base vs this PR
The same saved OpenRouter connection on the merge base and on this branch, edited in Settings > Connections. The base form has no compaction field; this branch shows "When the chat is full" with Summarize older turns (default), Drop oldest turns and Reset to latest turn.