Skip to content

fix(memory): reject unusable embedding vectors - #5875

Open
Winoren wants to merge 1 commit into
Tencent:mainfrom
Winoren:fix/memory-reject-invalid-embeddings
Open

Winoren wants to merge 1 commit into
Tencent:mainfrom
Winoren:fix/memory-reject-invalid-embeddings

Conversation

@Winoren

@Winoren Winoren commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Description

Long-term memory trusts the embedding provider's returned vector before using it for both persistence and recall. A provider response containing NaN or infinity is written into the portable blob; PostgreSQL can then reject the same value when casting it to halfvec, while SQLite retains a vector that cosine similarity cannot compare. An all-zero vector is likewise not a usable cosine vector but was persisted and repeatedly treated as embedding-complete.

Validate the vector once at the model boundary. Empty, non-finite and zero-norm results now follow the existing best-effort failure path: no embedding is stored or queried, lexical recall remains available, and write-side backfill can retry later. Valid vectors and model/dimension scoping are unchanged.

Prepared and verified with Codex assistance. No independent human review is claimed.

Type of Change

  • 🐛 Bug fix
  • 🧪 Test

Related Issue

Source-discovered; no associated issue claimed. Open and closed PRs were searched for duplicate work against the current default branch.

Testing

Linux amd64 with Go 1.26.0 and serialized low-memory compilation:

  • Added a table-driven persistence regression for NaN, positive infinity, negative infinity and zero-norm vectors.
  • Against the unmodified production implementation, the new regression failed all 4 subtests: each invalid vector was present in memory_item_embeddings.
  • go test -p=1 ./internal/application/service/memory -run '^(TestInvalidEmbeddingsAreNotPersisted|TestEmbeddingRoundTripsAndScoresItself|TestARewordedMemoryIsStillFound|TestRecallDegradesToLexicalWhenEmbeddingFails|TestMismatchedVectorsScoreZero)$' -count=1 — passed.
  • go test -p=1 ./internal/application/service/memory -count=1 — passed.
  • git diff --check origin/main...HEAD — passed.

The host's default go executable is not a working Go toolchain, so the repository's required Go 1.26.0 toolchain was used directly. Full-repository tests and local golangci-lint were not run; the remote App and Go Lint workflows remain the final gates.

Checklist

  • git diff --check origin/main...HEAD passes
  • Changed source files are formatted
  • Targeted tests for the changed package pass
  • Diff-scoped lint passes where applicable (not available locally; remote Go Lint is the gate)
  • Full-repository checks were run (scope and environment are documented above)
  • Self-reviewed the complete patch with Codex
  • Added regression coverage
  • Updated related documentation (no public API or configuration change)
  • Breaking changes are clearly called out above (none)

Screenshots / Recordings

Not applicable; backend memory-vector validation.

This branch has not been deployed

No deployments
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.

1 participant