Skip to content

Studio: keep turns an external provider's compaction drops searchable - #14731

Merged
danielhanchen merged 15 commits into
unslothai:mainfrom
debojyoti-dev:fix/compaction-recall-attachments-14708
Oct 23, 2026
Merged

danielhanchen merged 15 commits into
unslothai:mainfrom
debojyoti-dev:fix/compaction-recall-attachments-14708

Conversation

@debojyoti-dev

@debojyoti-dev debojyoti-dev commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Before: when a chat with an API-connected model (OpenRouter, a custom OpenAI-compatible server, etc.) hit auto-compaction, the oldest turns were trimmed but never written to the conversation archive. Documents attached in those turns were gone for good, search_conversation was never offered for the thread, and the model was not told the chat had been compacted, so it answered that it had no record of the document (the second screenshot in #14708).

After: turns the external fit drops are archived the same way the local paths already archive them, and the fit that drops them recalls the relevant ones inline into the latest user turn, as the local GGUF plain path does. So the question asked on the very turn the chat compacts gets the evicted document back, with tools on or off. On later turns, once the thread has an archive, the external tool loop also gets search_conversation and the existing compaction nudge.

How: _fit_external_context reserves recall room in the fit (_conversation_recall_reserve) and then calls _archive_and_recall with style="inline", which archives what was evicted and prefixes the retrieved turns to the latest user message. It never raises: an archive or embedder failure leaves the plain fitted chat. Inline rather than a forged tool exchange because the catalogue for this request is chosen before the fit runs, so search_conversation is not in it on the first compacting turn. _proxy_to_external_provider appends _apply_compaction_nudge to the external tool-loop nudge, which is a no-op unless search_conversation is in the catalogue. The fit stays rolling.

Tests: tests/test_external_conversation_archive.py covers archiving on an evicting fit, the evicting fit recalling the document into the latest turn, finding a phone number from an evicted document via search_conversation, no archive without a thread or when nothing is evicted, an archive failure still returning the fitted chat, and the nudge being present only when an archive exists. Four of them fail without this change. test_external_auto_compaction.py, test_conversation_recall_injection.py, test_openai_compaction.py, test_anthropic_compaction.py, test_manual_compaction.py and test_conversation_archive.py pass.

Live check (Studio backend, a custom OpenAI-compatible llama-server running Qwen3-0.6B as the provider, 4096-token window, a report with a phone number attached in turn 1, then filler turns until compaction, then "Was there a phone number on the medical document I attached at the start? Quote it exactly."): on main the compacting fit archived nothing and recalled nothing, and the model invented 123-456-7890; with this change the compacting fit reported archived_messages: 5, recalled_chunks: 2 and the answer was the exact +39 06 4912 7731.

Not covered here: text inside images is still not searchable, since the archive indexes text only.

Refs #14708

Auto-compaction for API-connected models trimmed the oldest turns but
never archived them, so attached documents in those turns were lost for
good and search_conversation was never offered for the thread. Archive
what the external fit evicts, the way the local paths already do, and
add the compaction nudge to the external tool loop so the model knows
earlier turns exist and searches for them instead of saying it has no
record.

Refs unslothai#14708
@debojyoti-dev
debojyoti-dev force-pushed the fix/compaction-recall-attachments-14708 branch from 083987d to 623e05f Compare October 22, 2026 15:32
The catalogue is chosen before the external fit runs, so on the turn that
first compacts the chat search_conversation is not offered yet and a question
about an evicted attachment was answered with no record of it. Recall inline
on every evicting fit, as the local plain path does, which also covers chats
with tools off and makes the compaction nudge's promise of automatic retrieval
true on this path.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T09:47:17.674224Z 537dbd5 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

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

ℹ️ 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
# The compaction nudge is not part of the general nudge: once the thread has an archive
# the catalogue carries search_conversation, and without this the model is never told
# that turns it cannot see exist, so it answers that it has no record of them.
_external_nudge = _apply_compaction_nudge(

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 Apply the compaction nudge in the Codex provider branch

For a tool-enabled archived thread sent through provider_type == "openai_codex", the earlier Codex branch selects search_conversation but builds _codex_nudge only with _build_tool_action_nudge at lines 30914-30920 and returns before this new generic-provider call is reached. If that request carries a branch with compacted turns omitted, the Codex model therefore receives the search tool without the instruction that hidden turns exist, preserving the exact “no record” behavior this change is intended to prevent; apply _apply_compaction_nudge to studio_tool_payloads before appending the Codex instructions as well.

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.

The openai_codex branch never fits or drops turns in Studio, so the branch it sends is complete and there is nothing hidden for the nudge to describe; out of scope for this PR.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@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: b1936184fd

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

recall = _archive_and_recall(
fitted,
messages,
branch_messages = branch or messages,

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 Preserve in-loop exchanges in the recall branch

When an external Studio-tool run grows enough that a later refit evicts an earlier assistant/tool exchange, branch is still _request_branch captured before the loop. _archive_and_recall passes this argument to conversation_archive.recall as its live-branch filter as well as to query selection, so the newly archived exchange is rejected as off-branch and the model cannot recover an earlier tool result. The fresh evidence beyond the fixed query-selection issue is that the same argument drives _document_on_live_branch; keep an accumulated branch of loop-created exchanges while still deriving the query from the client's latest user turn.

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 client branch is the right live-branch reference for the persisted turns this recall exists for, and it also drives the archive's ordering. Recovering a tool exchange that a mid-loop refit evicted within the same turn is a long-tail case (small window plus a long tool loop) that nothing on this path recalled before either.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

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

chatgpt-codex-connector[bot]

This comment was marked as resolved.

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 23, 2026
unslothai#14768)

When a chat is compacted, a turn with an attached document can be
numbered after every later turn in the archive. Recall then presents the
attachment as newer than a subsequent correction, under instructions
that later turns supersede earlier ones.

## Fix

Saved user messages keep attachment text separate from `content`, while
the frontend combines them in the request. `_as_wire` ignored
attachments, so position matching failed and assigned a number past the
end of the transcript. This change includes attachment content in that
projection and ignores request-only Python file-path notes when matching
positions.

Existing archive entries get corrected when the turn is evicted again,
without a migration or re-embedding.

## Validation

Live A/B with Qwen3-4B Q4_K_M at a 6,144-token context: a report
attached in turn 1 returned as turn 15 on main and turn 1 with this
change; a later correction remained turn 2. This held with and without
Python path notes. The model answered correctly in all four runs, so
this demonstrated corrected ordering, not improved answer accuracy.
Replaying a thread archived by main also corrected its existing entry
without creating a new document.

- 7 new archive tests fail on main and pass here.
- 18 backend test files covering archive, recall, compaction and the
refactor guard: 2,760 passed, 127 skipped.
- Repository formatting and `ruff check` are clean.

## Limitations

Oversized attachments shortened in the request still take the fallback
position. Branch selection still compares Python path notes verbatim.
Desktop, Windows and vision-model attachment flows were not tested.

Found while investigating unslothai#14708, which this does not resolve: images
remain unindexed, and whether the model invokes `search_conversation` is
unchanged. API-provider archiving is addressed separately in unslothai#14731.

---------

Co-authored-by: Daniel Han <[email protected]>
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 23, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 23, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 23, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 537dbd5e37

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

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.

2 participants