Skip to content

feat(kb): switching the embedding model rebuilds into a new collection in the background and cuts over on completion #19379

Closed
2 of 2 issues completed

Description

@mrveiss

Part of #19373. Blocked by the resolver child.

  • Start: a switch request starts a background job that re-embeds every fact from the system of record (fact_store.iter_facts, Postgres) into a NEW collection named for the target model (autobot_memory stays as it is), with its embedding provenance stamped (autobot_shared/embedding_provenance.py).
  • Progress: done/total and errors are reported via a status endpoint.
  • During the build: search and writes keep using the old collection; facts written during the build are also queued into the new one.
  • Cutover, only on a completed build: the KB's vector store switches to the new collection and the resolver override is set to the new model, atomically from the reader's view.
  • Failure: a failed or cancelled build leaves the active model and collection unchanged, and says why.
  • The old collection is not touched (its deletion is the next child).

Acceptance criteria

  • Search answers from the old collection during the build (test).
  • Cutover happens only after a complete build; failure leaves everything as it was (tests).
  • Progress is visible through an endpoint.

Activity

  1. added
    enhancementNew feature or request
    phase:M1.4Wizard and SLM: choose a mode, apply it, default models work
    on Oct 23, 2026
  2. mrveiss commented on Oct 23, 2026

    @mrveiss
    OwnerAuthor

    Design constraint from the #19383 review. This is dormant until this issue lands.

    active_embedding_model() feeds the KB init, services.npu_client and the embedding-cache keys. Once set_embedding_model_override() is called, queries are embedded with the new model. The KB collection, however, still holds vectors from the old model, and their dimensions may differ.

    AC proposal for this issue:

    • this rebuild is the only caller of set_embedding_model_override();
    • it fails closed: the override is written only after the new collection is fully built and verified;
    • a guard test fails on any second caller of the setter.
  3. mrveiss commented on Oct 25, 2026

    @mrveiss
    OwnerAuthor

    Maintainer decisions (2026-10-25):

    1. Cutover only with zero failures. Any fact that fails to embed blocks the cutover. The job reports the failed fact ids, and the active model and collection stay as they were.
    2. Admins only start a switch (admin permission, like the other embedding settings endpoints). A switch is a full re-embed of every fact.

    Design notes from mapping the code (origin/main ~129148ebe9):

    • The KB's collection binding is several attributes: _async_chroma_collection, vector_store, _write_buffer (its flush_fn closes over the old store), the lazy vector_index, and the collection name (knowledge/base.py:395-416). A cutover must drain the write buffer and rebind all of them.
    • The KB singleton is per process, and Celery holds its own (knowledge_factory.py:23). So the active space has to be one persisted record (model + collection + dim + generation). Each process rebinds when the generation changes. The query-side model and the collection then come from the same record, so a search can't mix spaces.
    • fact_store.iter_facts deliberately excludes facts created after the walk starts, so writes during the build are recorded and replayed before cutover.
    • The new collection is created stamped with embedding provenance (cognition_seeder.py:178 pattern). The KB collection is unstamped today.
    • The rebuild stays the only caller of set_embedding_model_override(), pinned by a guard (the constraint from code review on PR refactor(kb): one runtime embedding-model resolver with a user override; readers routed through it; dead copies retired (#19377) #19383).
  4. added
    needs-ac-evidenceClosed by a merge keyword; awaits an AC evidence table comment (#19520)
    on Oct 28, 2026
  5. github-actions commented on Oct 28, 2026

    @github-actions
    Contributor

    Landed in train train-2026-10-28-86b (#19880, merge 0b473f2ed5) via #19641

  6. mrveiss commented on Nov 1, 2026

    @mrveiss
    OwnerAuthor

    Checked against main (f88cd37 or later): each row was read from the code, and the tests and paths named were confirmed to exist there. Tests were not run for this comment.

    AC Evidence
    Search answers from the old collection during the build (test). MET. autobot-backend/knowledge/embedding_switch.py _build writes only to a new store from _open_collection(kb, run.collection, ...). kb is not rebound until _cut_over. Test: autobot-backend/knowledge/embedding_switch_test.py::test_the_kb_stays_on_its_old_collection_while_the_build_runs; also autobot-backend/knowledge/embedding_switch_test.py::test_the_active_space_is_untouched_until_the_build_completes.
    Cutover happens only after a complete build; failure leaves everything as it was (tests). MET. run_switch returns via _finish(run, "failed", ...) if failed_ids or not converged ("cutover needs zero failures") before _cut_over. set_embedding_model_override is called only in _cut_over (:381), after the settle script. Tests: autobot-backend/knowledge/embedding_switch_test.py::test_a_complete_build_cuts_over_after_every_fact_is_in, autobot-backend/knowledge/embedding_switch_test.py::test_one_failed_fact_blocks_the_cutover, autobot-backend/knowledge/embedding_switch_test.py::test_a_cancel_stops_before_cutover, autobot-backend/knowledge/embedding_switch_test.py::test_a_build_past_its_deadline_fails_without_switching. Single-writer guard (from the comment's proposal): repo_tests/embedding_override_single_writer_19379_test.py.
    Progress is visible through an endpoint. MET. autobot-backend/api/embeddings.py:289 @router.get("/switch/status") get_embedding_switch_status, backed by switch_status(). The router is mounted at /embeddings (autobot-backend/initialization/router_registry/feature_routers.py:196). Also POST /switch (:259, admin only via check_admin_permission) and DELETE /switch (:302). Body bullets: autobot_memory is untouched (a new collection from target_collection), and the collection is stamped via provenance_to_metadata(EmbeddingProvenance(model, dim)).
  7. removed
    needs-ac-evidenceClosed by a merge keyword; awaits an AC evidence table comment (#19520)
    on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendenhancementNew feature or requestphase:M1.4Wizard and SLM: choose a mode, apply it, default models work

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions