Skip to content

Studio: let a cloud connection drop whole oldest turns instead of summarizing - #18211

Merged
danielhanchen merged 24 commits into
unslothai:mainfrom
debojyoti-dev:fix/cloud-compaction-evict-mode-18189
Nov 18, 2026
Merged

danielhanchen merged 24 commits into
unslothai:mainfrom
debojyoti-dev:fix/cloud-compaction-evict-mode-18189

Conversation

@debojyoti-dev

@debojyoti-dev debojyoti-dev commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #18189

What changes for users

A connection (Settings > Connections > edit) gains When the chat is full, next to "Auto-compact after":

  • Summarize older turns (default, unchanged): the summary call added in Studio: summarize long cloud chats instead of dropping their oldest turns #17743.
  • Drop oldest turns: no summary call. Once the chat passes the trigger, just enough of the oldest whole turns leave the prompt on each message to fit. This is exactly what cloud chats did before Studio: summarize long cloud chats instead of dropping their oldest turns #17743: the dropped turns go to the thread's archive and are recalled inline, and search_conversation finds them.
  • Reset to latest turn: no summary call. Once the chat passes the trigger, everything before the latest turn leaves the prompt at once and stays out for the following turns, the checkpoint policy local models use. The prompt prefix stays stable between resets, so provider prompt caching keeps working; older turns come back through search_conversation and inline recall.

The thread shows "Context compacted" instead of "Earlier messages summarized" for both drop modes.

Summarize older turns Drop oldest turns Reset to latest turn
Extra model call yes, once per compaction no no
What stays in the prompt summary + recent turns as many recent turns as fit the latest turn (plus pinned instructions)
Compact chat / /compact summarizes now not applied (the chat only drops turns once it is full) starts a checkpoint, kept with auto-compact off
Provider refuses a prompt as too long summarize and resend once evict below the refused size and resend once evict below the refused size and resend once

With 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 nullable llm_providers.compaction_mode column (added by the existing schema migration), ProviderCreate / ProviderUpdate / ProviderResponse, create/update/restore in routes/providers.py. The values match the existing context_policy names.
  • compaction_mode request field ([x-unsloth]) on ChatCompletionRequest. 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_context with can_reset=False and replays_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 /compact epochs 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" /compact epoch is applied.
  • Frontend: compactionMode on the connection config, synced from the backend, saved by the dialog (editing back to the default clears it to null).

Testing

  • Drop oldest turns matches the pre-Studio: summarize long cloud chats instead of dropping their oldest turns #17743 cloud fit byte for byte. The same five chats went through the old tree (8fc3ae284, the parent of Studio: summarize long cloud chats instead of dropping their oldest turns #17743's merge) and through this branch with compaction_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, /compact epochs 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".
  • Overflow retries (both drop modes): keep the reply cap the window allows, evict every older turn when the latest turn is larger than half the refused prompt, drop an older exchange inside one tool turn, handle a latest turn that dominates the refused prompt, and keep a whole attachment when dropping turns is enough. A thread switched from Reset to latest turn to Drop oldest turns no longer replays its old checkpoint, and Reset to latest turn resets under a rolling process default. Each of these tests fails on the commit before its fix.
  • Backend compaction, provider, external-provider, archive, recall, attachment and overflow suites: 516 passed, 42 skipped.
  • Frontend: node scripts/typecheck.mjs --tests clean; full npm test 16652 passed, 1 skipped, 0 failed.

Follow-up fixes

  • With auto-compact off, the chat now still sends the drop mode, so a /compact checkpoint stays in force on later turns (before, the next turn forwarded the whole chat).
  • An overflow refusal now evicts to half of the prompt that was refused, not half of the unfitted transcript, and the tool loop keeps that bound on later passes (before, the retry could be as large as the refused prompt and fail again).
  • Compact chat with tools off starts a checkpoint, decided by the model's tool support as on managed engines; overflow refits use the same gate.
  • A replayed boundary with context_overflow: "error" no longer turns threshold eviction back on.

Connection form, base vs this PR

Connection form before and after: the "When the chat is full" field and its three options

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.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T17:38:23.521017Z a3d2056 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 18, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

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]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…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.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

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]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 914ce7e948

ℹ️ 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".

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]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 18, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread studio/backend/routes/inference.py Outdated
Comment on lines +30794 to +30796
_evicting_payload = payload.model_copy(
update = {
"compaction_threshold": payload.context_window or _EXTERNAL_COMPACTION_THRESHOLD_MAX

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…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.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…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]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

# Conflicts:
#	studio/backend/routes/inference.py

[skip ci]

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment on lines +30915 to +30916
if truncation and truncation.get("dropped_messages") and truncation.get("fits"):
break

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…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]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Later continuations kept rebuilding the payload, dropping any overflow bound a
retry had tightened.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread studio/backend/routes/inference.py Outdated
Comment on lines +31241 to +31243
_evicting_payload = _first_payload = payload.model_copy(
update = {"context_policy": payload.compaction_mode}
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: a3d2056817

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 692106c into unslothai:main Nov 18, 2026
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.

[Bug] API/provider connections have lost the same compaction policy as local models (evict whole turns + searchable archive)

2 participants