Repository navigation
Studio: keep turns an external provider's compaction drops searchable - #14731
danielhanchen merged 15 commits into
Conversation
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
083987d to
623e05f
Compare
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]
|
@codex review |
Codex Review SummaryThis 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. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| # 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( |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
…rites the latest turn [skip ci]
…oop's conversation [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 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, |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
|
Codex Review: Something went wrong. Try again later by commenting “@codex review”. ℹ️ 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". |
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]>
…as an archive [skip ci]
|
Codex Review: Didn't find any major issues. 🚀 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". |
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_conversationwas 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_conversationand the existing compaction nudge.How:
_fit_external_contextreserves recall room in the fit (_conversation_recall_reserve) and then calls_archive_and_recallwithstyle="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, sosearch_conversationis not in it on the first compacting turn._proxy_to_external_providerappends_apply_compaction_nudgeto the external tool-loop nudge, which is a no-op unlesssearch_conversationis in the catalogue. The fit stays rolling.Tests:
tests/test_external_conversation_archive.pycovers archiving on an evicting fit, the evicting fit recalling the document into the latest turn, finding a phone number from an evicted document viasearch_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.pyandtest_conversation_archive.pypass.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 reportedarchived_messages: 5, recalled_chunks: 2and 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