Skip to content

fix(knowledge): reject invalid embeddings before caching - #1073

Merged
dovvnloading merged 7 commits into
mainfrom
agent/validate-knowledge-embeddings-1037
Nov 25, 2026
Merged

dovvnloading merged 7 commits into
mainfrom
agent/validate-knowledge-embeddings-1037

Conversation

@dovvnloading

Copy link
Copy Markdown
Owner

Summary

Fixes #1037. A provider can currently return vectors with invalid shape, incompatible dimensions, nonfinite float32 values, or zero norm; the cache marks them as embedded, preventing an ordinary retry. Validate each full batch before committing it, enforce a consistent model-wide dimension under SQLite's write lock, and provide an explicit model-cache repair procedure for existing stale rows.

What Changed

  • Validate embedding inputs (including queries and connection tests) after float32 conversion; reject the complete malformed batch without marking chunks done.
  • Check cached dimensions on the first write of each indexing pass and enforce the dimension under a write lock on subsequent batches, including across workspace collections. Keep previously committed valid batches.
  • Use bounded float64 cosine scoring to avoid float32 overflow/underflow, with clear errors for invalid cached vectors rather than synthetic scores; retain the existing keyword fallback.
  • Add an explicit offline, model-scoped reset procedure in docs/KNOWLEDGE_RETRIEVAL.md. It only deletes derived embeddings, not documents, chunks, keyword indexes or another model's cache.
  • Add parametrized regression coverage for malformed shapes/types, nonfinite and zero vectors, overflow/underflow, inconsistent dimensions, atomic retry, legacy cache repair, connection tests, keyword fallback and extreme cosine values. Update two preexisting fake-provider fixtures that returned zero embeddings.

Why

A transient invalid provider response should not permanently poison the semantic index. A changed model dimension should require an explicit cache rebuild rather than silently mixing incompatible vector spaces. Repair is intentionally opt-in so a transient query failure cannot trigger a costly or unapproved rebuild.

Validation

  • App launches
  • Relevant workflow was exercised manually
  • python -m compileall -q . passes (run from the repo root - matches CI)
  • python -m pytest -q passes (run from the repo root - matches CI)
  • cd web_ui && npm run check passes (matches CI)

Passed an isolated Python smoke check using the proposed validation/write/reset logic with NumPy 2.3.5 and a minimal SQLite schema: 12 malformed input cases, cosine scoring at both float32 extremes, mixed-batch rollback, mismatch between batches, legacy mixed-cache rejection, model-scoped reset and retry. This is not the repository test suite: the execution environment could not resolve github.com to clone the repository. The added regression tests require CI validation.

UI Notes

  • No screenshots needed

Additional Context

No new dependencies, schema migration, wire contract or UI changes. Model-wide cache recovery is deliberately a documented offline maintenance action; it does not automatically remove embeddings or change cloud consent. Main advanced during implementation; please let CI validate the PR against the current base.

@dovvnloading dovvnloading left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintainer review via ChatGPT — no blocking findings.

Reviewed all seven changed files against #1037: validation occurs on the actual float32 representation before writing a batch; SQLite BEGIN IMMEDIATE serializes model-wide dimension checks and writes; failed batches remain retryable; cosine math uses float64 and clamps scores; corrupt legacy vectors have an explicit model-scoped offline recovery path, with keyword fallback and cloud consent preserved. Regression tests cover malformed vectors, atomic retry, cross-collection dimensions, legacy repair and extreme values. The latest main comparison showed two intervening commits touching unrelated workflow UI files only.

Verified GitHub Actions run 45604990518 for this head: all four required checks succeeded (Python including pytest, frontend, build/install and POSIX); Playwright boot-smoke was skipped. Static review and CI verification only, no independent local/manual app test. The connected account is also the PR author, so recording a review comment rather than self-approval. No code changes requested.

@dovvnloading
dovvnloading merged commit 1b228ff into main Nov 25, 2026
5 checks passed
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(knowledge): invalid embedding vectors are persisted and permanently poison semantic search

1 participant