Skip to content

fix(vector_store): sync the facade mirror on backend update_vectors - #1847

Merged
Sameer6305 merged 3 commits into
semantica-agi:mainfrom
drakeo338:claude/1833-fix
Oct 5, 2026
Merged

Sameer6305 merged 3 commits into
semantica-agi:mainfrom
drakeo338:claude/1833-fix

Conversation

@drakeo338

Copy link
Copy Markdown
Contributor

Description

On the backend path, update_vectors() returned straight from the backend delegate and left self.vectors / self.metadata stale, so a later save() wrote the old vectors. It now refreshes the facade mirror for known ids, the same way delete_vectors() already does.

This PR was written with AI assistance (Claude Code). I reviewed and tested the change.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Performance improvement
  • Code refactoring

Related Issues

Fixes #1833

Changes Made

  • VectorStore.update_vectors: after delegating to the backend, update mirrored vectors and metadata under _inmemory_lock.
  • Regression test test_update_vectors_syncs_backend_mirror_before_save.

Testing

  • Tested locally
  • Added tests for new functionality
  • Package builds successfully (python -m build)

Test Commands

# Build the package
# (not run)

# Optional: Run your own tests
pytest tests/vector_store/test_vector_store.py   # 15 passed, 1 skipped

# Optional: Format code
# (not run; change follows surrounding style)

The new test fails with the source change reverted.

Documentation

  • Updated relevant documentation
  • Added code examples if applicable
  • Updated API reference if adding new APIs
  • Updated cookbook if adding new examples
  • No documentation changes needed

Breaking Changes

Breaking Changes: No

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • Package builds successfully

Additional Notes

Ids not already in the mirror are left alone, matching the in-memory path.

update_vectors returned straight from the backend delegate, leaving
self.vectors and self.metadata stale, so save() persisted the
pre-update values. Refresh the mirror for known ids, as delete_vectors
already does on the backend path.

Fixes semantica-agi#1833
@qodo-code-review

Copy link
Copy Markdown

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Keep VectorStore’s mirror current after backend vector updates

🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Refresh mirrored vectors and metadata after backend updates so save() persists current values.
• Add a regression test covering the mirror and saved JSON.
Diagram

graph TD
  U["Update request"] --> F["VectorStore facade"] --> B["Backend store"]
  B -->|returns result| F
  F -->|sync known IDs| M["Facade mirror"] -->|save| J[("store_data.json")]
Loading
High-Level Assessment

Synchronizing the facade after delegation matches the existing store and delete behavior. Reading updated values back from each backend would add calls and require read support that not every backend exposes.

Files changed (2) +49 / -2

Bug fix (1) +14 / -2
vector_store.pySynchronize mirrored data after backend updates +14/-2

Synchronize mirrored data after backend updates

• After either backend update method returns, update vectors and supplied metadata for IDs already present in the facade mirror under its lock. Preserve the backend’s return value so subsequent save() calls serialize the updated data.

semantica/vector_store/vector_store.py

Tests (1) +35 / -0
test_vector_store.pyCover backend update persistence regression +35/-0

Cover backend update persistence regression

• Add a fake-backend test that updates a vector and its metadata, checks the facade mirror, then verifies both updated values in saved JSON.

tests/vector_store/test_vector_store.py

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (1) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. Failed updates overwrite saved vectors ✓ Resolved
Description
VectorStore.update_vectors() changes the facade mirror without checking whether the backend
returned False. If a backend rejects an update by returning False, a later save() writes the
requested vector and metadata even though the backend kept the originals.
Code

semantica/vector_store/vector_store.py[R865-868]

+            with self._inmemory_lock:
+                for vec_id, new_vec in zip(vector_ids, new_vectors):
+                    if vec_id in self.vectors:
+                        self.vectors[vec_id] = new_vec
Evidence
The changed code assigns the backend result but updates both mirrors before returning it. The save
path serializes those mirrors, so a False result does not prevent the requested values from being
persisted.

Synchronize the facade mirror after backend vector updates
semantica/vector_store/vector_store.py[855-873]
semantica/vector_store/vector_store.py[646-655]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The facade changes its mirror even when the backend reports that an update failed.
## Fix Focus Areas
- semantica/vector_store/vector_store.py[857-873]
- tests/vector_store/test_vector_store.py[305-337]
## Recommended Fix
Check the backend result before changing the mirror, preserve that result for the caller, and add a regression test in which the backend returns `False`.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Missing backend rows enter saved data 📎 Requirement gap ≡ Correctness
Description
VectorStore.update_vectors() replaces every locally mirrored ID after a SQL backend returns
True, without confirming that the backend updated a row. If load() restores a mirror into a
store connected to an empty SQLite or PostgreSQL database, or a backend row is later removed, an
unmatched update can change the vector and metadata persisted by save() even though no
corresponding backend row changed.
Code

semantica/vector_store/vector_store.py[R866-870]

+                for vec_id, new_vec in zip(vector_ids, new_vectors):
+                    if vec_id in self.vectors:
+                        self.vectors[vec_id] = new_vec
+                if metadata:
+                    for vec_id, meta in zip(vector_ids, metadata):
Evidence
load() populates the facade dictionaries, while both SQL backends execute UPDATE statements and
return True without checking affected-row counts or whether the requested IDs exist. The facade’s
mirror loop checks only local membership, and save() serializes that local data, so an ID missing
from the backend can still acquire a new saved value.

Synchronize the facade mirror after backend vector updates
semantica/vector_store/vector_store.py[865-873]
semantica/vector_store/sqlite_vec_store.py[515-538]
semantica/vector_store/pgvector_store.py[550-576]
semantica/vector_store/vector_store.py[646-655]
semantica/vector_store/vector_store.py[695-704]
semantica/vector_store/vector_store.py[865-872]
semantica/vector_store/sqlite_vec_store.py[525-537]
semantica/vector_store/pgvector_store.py[568-590]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
SQL backends can report success for updates that match no row, causing the facade to mirror and save values that the backend did not store.
## Fix Focus Areas
- semantica/vector_store/vector_store.py[854-873]
- semantica/vector_store/sqlite_vec_store.py[515-538]
- semantica/vector_store/pgvector_store.py[550-590]
## Recommended Fix
Have the SQL backends detect unmatched IDs and report or reject them, then update the facade mirror only for IDs confirmed updated. Test a loaded mirror whose IDs are absent from the connected backend.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

3. Saves can contain mismatched updates 🐞 Bug ☼ Reliability
Description
update_vectors() writes mirrored vectors and metadata in separate loops, while save() reads both
dictionaries without acquiring _inmemory_lock. If a save runs during a backend update, it can
serialize a new vector with its old metadata; it can also serialize the old mirror after the backend
update has completed but before synchronization starts.
Code

semantica/vector_store/vector_store.py[R865-868]

+            with self._inmemory_lock:
+                for vec_id, new_vec in zip(vector_ids, new_vectors):
+                    if vec_id in self.vectors:
+                        self.vectors[vec_id] = new_vec
Evidence
The added backend path changes vectors before metadata under a lock, but save neither takes that
lock nor snapshots the dictionaries together.

semantica/vector_store/vector_store.py[854-873]
semantica/vector_store/vector_store.py[643-661]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A concurrent save can read the facade mirror partway through a backend update and persist inconsistent vector and metadata values.
## Fix Focus Areas
- semantica/vector_store/vector_store.py[854-873]
- semantica/vector_store/vector_store.py[643-661]
## Recommended Fix
Coordinate backend update synchronization with save, and take a snapshot of both mirror dictionaries under the same lock before serializing them. Ensure a save cannot snapshot the mirror between completion of a backend update and its mirror synchronization.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can turn on the rule miner and Qodo learns your standards from review history

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Comment thread semantica/vector_store/vector_store.py Resolved
Comment thread semantica/vector_store/vector_store.py Resolved
Comment thread semantica/vector_store/vector_store.py Resolved
If the backend update returned False it had kept its old values, but
update_vectors still wrote the new ones into self.vectors and
self.metadata, so a later save() persisted an update that never
happened. Return the backend result before touching the mirror.
@drakeo338

Copy link
Copy Markdown
Contributor Author

Finding 1 fixed in d6d369e: the mirror only changes when the backend reports success, with a test for a backend that returns False. 2 and 3 are about the SQL backends and save locking in general, outside this fix.

@pkupt

pkupt commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

Reviewed against #1833. update_vectors now captures the backend result, returns as-is when the backend reports False, and otherwise updates the mirror under _inmemory_lock, so save() persists the new values. That is the same shape as delete_vectors on the backend branch, and the inmemory branch is untouched.

One thing before this can merge: the head has no CI. gh pr checks 1847 shows only GitGuardian, and build / security-scan / verify never ran on d6d369e1, so a rebase or a push to the branch is needed to get a real signal.

On if result is False: it treats only an explicit boolean False as a rejection, so a backend returning None or another falsy value on failure would still have its mirror updated. Not a blocker for the backends in tree, but worth widening if new backends land.

The branch is also behind main. Nothing else from me.

@Sameer6305
Sameer6305 self-requested a review October 5, 2026 07:45

@Sameer6305 Sameer6305 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved. The fix correctly addresses #1833, the regression coverage is appropriate, focused tests pass, CI is green, and the remaining review findings were addressed/resolved. No merge-blocking correctness, security, compatibility, or concurrency issues found.

@Sameer6305
Sameer6305 merged commit a948fd0 into semantica-agi:main Oct 5, 2026
12 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(vector-store): update_vectors() does not update the facade mirror for backend stores, causing save() to persist stale data

3 participants