hindsight icon indicating copy to clipboard operation
hindsight copied to clipboard

mental_models search state is not maintained and reconciliation still targets reflections

Open JiehoonKwak opened this issue 3 months ago • 1 comments

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:

  1. MemoryEngine.create_mental_model() writes embedding but never writes search_vector. This leaves VChord bm25vector rows NULL because that column is not generated.
  2. update_mental_model() re-embeds only when content changes and builds that embedding from the incoming name (or an empty string), not the canonical stored name + content document. It does not update search_vector; name-only updates do not refresh either derived representation.
  3. clear_mental_model() clears content but leaves the old embedding and lexical state searchable.
  4. rename_knowledge_node() changes knowledge_pages.name only; for page nodes the backing mental_models.name remains stale.
  5. ensure_text_search_extension() still checks and rebuilds reflections, although migration t5o6p7q8r9s0 renamed that table and its indexes to mental_models. The current table is silently skipped, and the failure guidance still tells operators to delete from reflections.
  6. The original native column is generated, but the runtime reconciliation path recreates native search_vector as a plain tsvector, 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 name and content for create, content-only update, name-only update, refresh, and clear.
  • For native and VChord, write search_vector in 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.name and its backing mental_models.name in one transaction.
  • Change runtime text-search reconciliation from reflections to mental_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 NULL after normal writes.
  • Native behavior remains correct for both generated and reconciled plain tsvector columns.
  • Base-column backends do not write meaningless dummy search_vector values.
  • 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.

JiehoonKwak avatar Aug 10 '26 07:08 JiehoonKwak

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_model omits search_vector) — fixed: the INSERT now writes it via pg_search_vector_expr(..., native_inline=False) for VChord.
  • Root cause 2 (update_mental_model re-embeds from the wrong document / never touches search_vector) — fixed: re-tokenizes when name/content change, embedding built off-connection.
  • Root cause 3 (clear_mental_model leaves stale lexical state) — fixed: re-tokenizes from the name after clearing content.
  • Root cause 5 (reconciliation still targets reflections) — fixed: ensure_text_search_extension now checks mental_models and handles the reflections→mental_models rename 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.

nicoloboschi avatar Aug 12 '26 05:08 nicoloboschi