Skip to content

Studio: keep compacted turns searchable when the archive embedder fails - #16718

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
debojyoti-dev:fix/search-conversation-without-embedder-16706
Nov 7, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
debojyoti-dev:fix/search-conversation-without-embedder-16706

Conversation

@debojyoti-dev

Copy link
Copy Markdown
Contributor

Fixes #16706 (also the cause behind #16187 and #16602).

What was wrong

search_conversation is only offered once something has been archived for the chat (has_archive), and archiving needs the embedding model. When that model could not run (a failed load, no llama-server for the embedding fallback, a download still pending), archive_turns logged conversation_archive.ingest_failed and wrote nothing, while the compaction fit had already dropped the turns. The chat then never got an archive, so:

  • search_conversation was never added to the tool list, on GGUF and API models alike;
  • the inline recall on the compaction turn found nothing, so the model answered as if the chat had just started ("There are no previous turns in this conversation").

What this changes

  • conversation_archive._archive_turns: when the embedder fails (an exception before any vectors came back, the embedder's own error after a partial pass, or EmbeddingModelDownloadRequiredError), the evicted turns that have no copy yet are stored for keyword search only (_archive_turns_lexically). They get the same document row, archive position and FTS5 rows as a normal copy, but no vectors.
  • Those copies are stamped vectors-dropped:none (the existing rag_db.VECTORS_DROPPED marker), so they match no embedder. Once the embedder works, the next eviction sees them as stale and re-embeds them in place, keeping their position. Dense search never returns them in the meantime.
  • store.add_lexical_chunks writes chunks + chunks_fts without a chunks_vec row.
  • A store failure after encoding (e.g. add_chunks failing) does not fall back, so it still archives nothing and is retried as before.
  • degraded() keeps its meaning (the dense write failed), so recall room and checkpoint resets behave as before.

Tests

New in tests/test_conversation_archive.py:

  • test_turns_evicted_while_the_embedder_fails_stay_searchable: with the embedder failing, the thread gets an archive, search_conversation finds both turns, and a later working embedder re-embeds them at the same positions. Fails on main (archive_turns returns 0).
  • test_a_failing_store_does_not_fall_back_to_keyword_copies.

Updated for the new behaviour: the pending-download, degraded and partial-batch tests now expect the keyword copies (and the pending-download test also recalls the turn evicted while the download was pending).

Run on Windows 11 (Ryzen 5 5600H, RTX 3050 4 GB), Python 3.12, with and without the change: test_conversation_archive.py, test_external_conversation_archive.py, test_conversation_recall_injection.py, test_conversation_recall_session.py, test_rag_store.py, test_rag_reembed.py, test_rag_retrieval.py, test_rag_vec_cache.py, test_rag_embedding_identity.py, test_checkpoint_compaction.py, test_llama_cpp_tool_loop.py, test_openai_auto_switch.py and the other suites that touch the archive (27 files). Same results on both apart from the two new tests; the few failures/errors there (test_checkpoint_compaction.py, one each in test_llama_cpp_tool_loop.py and test_view_image_tool.py) fail identically on clean main on this machine.

End to end: a real Studio server (run.py --api-only) with an OpenAI-compatible stub provider (vLLM type, 4k window), driving a 6-turn chat, two edits of the last message, a follow-up, and a turn where the model calls search_conversation:

embedder search_conversation offered after the first compaction recall on the compaction turn search returns evicted turns
main failing (RAG_EMBED_BACKEND=llama-server, no binary) never none not offered
this PR failing yes, every later turn and after edits yes yes
main working (sentence-transformers, CPU) yes yes yes
this PR working yes yes yes

Not tested here: a real llama.cpp chat (no llama-server on this machine) and macOS.

When the embedding model behind the conversation archive could not run (a
failed load, a missing llama-server, a download still pending), compaction
still dropped the old turns but archived nothing. The chat then never had
an archive, so search_conversation was never offered and the automatic
recall found nothing.

Those turns are now stored for keyword search instead, with a marker that
matches no embedder, so the next request with a working embedder
re-embeds them in place. A store failure still archives nothing, so it is
retried as before.

Fixes unslothai#16706
@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 commented Nov 6, 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-11-06T10:24:13.960752Z bfa6aef 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.

@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: 33216e3409

ℹ️ 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 +1115 to +1116
if store.documents_by_hash(conn, scope, digest):
continue

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 each evicted occurrence of a repeated turn

When the embedder is unavailable and identical turns occur at different positions, this unconditional hash check discards every occurrence after the first. For example, archiving set X to 1, set X to 2, then set X to 1 with identical assistant replies writes only two documents; recall consequently presents the intermediate value 2 as the latest update. The dense path explicitly preserves these occurrences using _write_budget and separate ordinals. Apply the same occurrence-aware, live-prompt-aware budget in both lexical duplicate checks rather than treating any existing copy as sufficient.

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.

Fixed in ec32fba: the keyword fallback now writes one copy per evicted occurrence (_write_budget, live-prompt aware) at each occurrence's seat, as the dense path does; test_keyword_copies_keep_every_evicted_occurrence_of_a_repeated_turn.

if dense_failed and not (cancel_check is not None and cancel_check()):
# Without any copy there is no archive, so search_conversation is never offered (#16706).
# degraded() stays set: dense recall is still down.
written += _archive_turns_lexically(thread_id, scope, groups, positions)

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 Poll cancellation while writing the lexical fallback

When the embedder fails on a long compacted history, a Chat Stop received after this call starts is ignored until every group has been chunked and committed: the helper receives no cancel_check and processes all groups without the dense path's cap. In an isolated check, setting cancellation after the first lexical write still allowed all 3,000 turns to be written. Because _archive_and_recall calls this synchronously, cancellation cannot interrupt the archival pass. Pass the callback through and poll it between groups.

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.

Fixed in ec32fba: cancel_check is passed through and polled before each turn (a 3000-turn fallback took 7.8 s here without it); test_chat_stop_interrupts_the_keyword_fallback.

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

ℹ️ 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 +1126 to +1127
if len(store.documents_by_hash(conn, scope, digest)) >= budget:
continue

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 Retire surplus keyword copies after a rewind

With the embedder still unavailable, archive X=1, X=2, X=1, rewind to the first two turns, and compact again. Both lexical duplicate checks simply skip existing copies, leaving archive ordinals [0, 1, 2] instead of [0, 1]. _document_on_live_branch accepts the removed repeat because its text matches the surviving first occurrence, so recall presents X=1 as the latest update, overriding the actual latest value X=2. Apply the dense path's surplus-copy and ordinal reconciliation before skipping retained keyword copies.

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.

Fixed in bfa6aef: a keyword copy that already meets its budget now goes through the dense pre-check's _restamp (re-seats, retires surplus copies after a rewind); test_a_rewind_retires_the_copy_the_conversation_no_longer_holds now also runs with the embedder failing.

Comment on lines +1126 to +1127
if len(store.documents_by_hash(conn, scope, digest)) >= budget:
continue

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 Widen the transcript span of existing keyword copies

When the embedder is unavailable and a previously archived tool exchange is re-evicted with an additional retrieval-result message, stripping retrieval leaves the same content hash. These duplicate checks skip the copy without calling _widen_span, so a copy saved with archive_messages=3 remains at 3 even when the new transcript span is 4. Reproducing the existing longer-span regression case with the embedder disabled makes _document_matches_one_run reject the otherwise matching turn, and branch-filtered recall returns nothing. Preserve the dense path's span widening on both lexical duplicate-skip paths.

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.

Fixed in bfa6aef: the same skip path calls _widen_span; test_the_same_text_over_a_longer_span_widens_the_stored_window now also runs with the embedder failing.

@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. Bravo.

Reviewed commit: bfa6aef56a

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

[Bug] search_conversation tool totally disappeared (3rd time)

2 participants