mental_models search state is not maintained and reconciliation still targets reflections
Bug description
mental_models has backend-specific searchable derived state, but its write paths and runtime reconciliation no longer maintain that state consistently.
This is independent from, but a prerequisite to fully fixing, #3268: a backend-aware query can stop the HTTP 500 while still returning empty or stale lexical results.
Root causes
On current main:
MemoryEngine.create_mental_model()writesembeddingbut never writessearch_vector. This leaves VChordbm25vectorrowsNULLbecause that column is not generated.update_mental_model()re-embeds only whencontentchanges and builds that embedding from the incoming name (or an empty string), not the canonical stored name + content document. It does not updatesearch_vector; name-only updates do not refresh either derived representation.clear_mental_model()clearscontentbut leaves the old embedding and lexical state searchable.rename_knowledge_node()changesknowledge_pages.nameonly; for page nodes the backingmental_models.nameremains stale.ensure_text_search_extension()still checks and rebuildsreflections, although migrationt5o6p7q8r9s0renamed that table and its indexes tomental_models. The current table is silently skipped, and the failure guidance still tells operators to delete fromreflections.- The original native column is generated, but the runtime reconciliation path recreates native
search_vectoras a plaintsvector, so the same write-path omission also matters after reconciliation.
Expected behavior
Mental-model search should use one canonical document (name + content) and maintain its embedding and backend-specific lexical projection together whenever that document changes.
Knowledge Page renames should keep the visible page name and backing mental-model name synchronized atomically.
Minimal fix boundary
- Add one backend-aware mental-model search-document expression/writer at the engine/database boundary; do not hard-code a specific backend in Knowledge Pages code.
- Use the canonical stored
nameandcontentfor create, content-only update, name-only update, refresh, and clear. - For native and VChord, write
search_vectorin the same transaction as the canonical row update. For backends that index base columns directly (pg_textsearch,pgroonga,pg_search), leave the dummy column alone. - Make page rename update
knowledge_pages.nameand its backingmental_models.namein one transaction. - Change runtime text-search reconciliation from
reflectionstomental_models, align index names/expressions, and backfill missing derived state only when the configured backend already matches the physical column/index type. - Keep the existing fail-closed rule for populated backend switches. This issue must not turn startup reconciliation into an in-place backend migrator.
- No API or client schema change.
Acceptance coverage
- Create, content-only update, name-only update, clear, and page rename keep canonical content, embedding, and lexical state consistent.
- VChord rows no longer remain
NULLafter normal writes. - Native behavior remains correct for both generated and reconciled plain
tsvectorcolumns. - Base-column backends do not write meaningless dummy
search_vectorvalues. - Page rename rolls back both names on a uniqueness failure.
- Existing populated backend-mismatch tests remain fail-closed.
- Regression coverage demonstrates that a subsequent backend-aware Knowledge Page search can retrieve current names/content rather than stale or empty projections.
Related
- #3268: Knowledge Page search hard-codes native PostgreSQL FTS operators.
Triaged against current main: most of this was resolved by #3318 (native_inline=False search_vector writes on create/update/clear) and #3335 (PGroonga + shared mental_models_text_document()), both of which merged the same day this was filed.
- Root cause 1 (
create_mental_modelomitssearch_vector) — fixed: the INSERT now writes it viapg_search_vector_expr(..., native_inline=False)for VChord. - Root cause 2 (
update_mental_modelre-embeds from the wrong document / never touchessearch_vector) — fixed: re-tokenizes when name/content change, embedding built off-connection. - Root cause 3 (
clear_mental_modelleaves stale lexical state) — fixed: re-tokenizes from the name after clearing content. - Root cause 5 (reconciliation still targets
reflections) — fixed:ensure_text_search_extensionnow checksmental_modelsand handles thereflections→mental_modelsrename transition. - Root cause 6 (reconciled native column omission) — covered by the same write-path fix.
The one thing still outstanding is root cause 4: rename_knowledge_node updates knowledge_pages.name only, leaving the backing mental_models.name stale — so the page's searchable document (name + content) keeps the old name after a rename, and the two names aren't updated atomically.
Scoping this issue down to just the rename sync; fix coming in a dedicated PR.