Skip to content

Studio: search earlier turns for the user's question, not the tool loop's own notices - #16204

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
debojyoti-dev:fix/conversation-recall-query-16187
Nov 1, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
debojyoti-dev:fix/conversation-recall-query-16187

Conversation

@debojyoti-dev

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

Copy link
Copy Markdown
Contributor

Fixes #16187

What was wrong

In the second screenshot the turn runs 13 rounds of 2 tool calls, reaches the 25-call cap, and the context is compacted on the final "give your answer now" pass. That pass runs with tool_choice: "none", so search_conversation can't be called there; the only way back to earlier turns is the automatic recall that runs when a fit evicts something.

That recall took its query from the last user-role message on the loop's live branch. The loop adds its own user-role notices to that branch (for example "an identical call had already completed successfully…" or the "You have used all available tool calls…" nudge when it rides a user turn). So the archive was searched for the notice text instead of the user's question, nothing relevant came back, and the model answered that it had no earlier context. These notices are long enough that the existing thin-query fallback ("continue" and the like) doesn't catch them.

The change

  • build_conversation_recall takes query_messages, the client's own transcript, and reads the query (and the thin-query anchor) from it. The live branch is still what filters the results, so tool results this request produced stay recallable.
  • _archive_and_recall passes it through (only when given, so existing callers are unchanged).
  • The llama.cpp tool loop passes _request_branch on its per-iteration fit and on the final pass; the MLX / managed-engine loop (compact_chat_context) passes its request_branch.

Tests

  • test_recall_searches_the_users_question_not_the_loops_own_notices drives the real llama.cpp tool loop: repeated calls leave a duplicate-call notice as the last user turn, the final pass compacts, and the recall query must be the user's question. Before the fix it searched for the notice.
  • Two unit tests in test_conversation_recall_injection.py cover query_messages in build_conversation_recall and _archive_and_recall.
  • test_mlx_context_compaction.py: the existing re-prompt loop test also checks the query source.

All three new tests fail on main and pass with the change.

Ran on Windows 11 (no GPU needed), compared against a clean copy of main:

  • test_conversation_recall_injection.py 81 passed, test_mlx_context_compaction.py 20 passed, test_conversation_archive.py 187 passed, test_external_conversation_archive.py 11 passed, test_conversation_search_safetensors_loop.py 11 passed, test_conversation_recall_session.py 8 passed, test_context_overflow_truncation.py 78 passed.
  • test_checkpoint_compaction.py: 168 passed, 1 failed, 14 errors, identical on clean main (/proc/self/fd and a Windows-only parametrize setup error).
  • test_llama_cpp_tool_loop.py: the targeted tests pass; the full file hangs on this machine in a textual-fallback per-turn-limit test on clean main as well, so I couldn't get a full-file run here. CI should cover it.

I don't have a Mac, so the MLX loop is covered only by the stubbed tests above.

External provider path

The API provider loop (_fit_external_context) already searched for the user's question, but it filtered the recall to the client's request only. Tool results this turn produced and a fit evicted were archived and then filtered out, so the model still answered that it had no findings. The recall now filters on the loop's own conversation and takes its query from the client's request (query_messages), matching the llama.cpp loop. test_a_tool_result_this_turn_evicted_is_recalled_for_the_users_question fails without this and passes with it.

…op's own notices (unslothai#16187)

When a long tool turn compacts on a later pass, the automatic recall took
its query from the last user-role message on the live branch. The loop adds
its own user-role notices there (an identical call already ran, the tool
call budget is spent), so recall searched the archive for that notice and
the final answer pass, which cannot call search_conversation, got nothing
back and told the user it had no earlier context.

build_conversation_recall now takes query_messages, the client's own
transcript, for the query; the live branch still filters what is returned.
The llama.cpp loop (iteration fits and the final pass) and the MLX /
managed-engine loop pass their request branch.
@danielhanchen danielhanchen self-assigned this Nov 1, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #16204, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#674.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror bbce9c4995 (this PR's 295cc13b5)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: bbce9c4995

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror bbce9c4995 (this PR's 295cc13b5).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] search_conversation tool totally disappeared

2 participants