Skip to content

refactor(memory): migrate vector storage to resilient v2 - #1960

Merged
yyhhyyyyyy merged 10 commits into
devfrom
refactor/memory-vector-store-v2
Jul 13, 2026
Merged

refactor(memory): migrate vector storage to resilient v2#1960
yyhhyyyyyy merged 10 commits into
devfrom
refactor/memory-vector-store-v2

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR introduces Memory Vector Store V2 to eliminate persistent HNSW corruption risks and make migration, publication, recovery, and quarantined cleanup converge safely across restarts.

Changes

  • Remove HNSW indexes and runtime VSS dependency from the active V2 path.
  • Add V2 schema, format_version, embedding identity, dimensions, and row-count validation.
  • Publish stores through checkpointed staging and atomic rename.
  • Preserve compatible WAL-free V1 vectors through read-only keyset migration.
  • Rebuild stale, incompatible, or V1-with-WAL stores from the SQLite source of truth.
  • Use a progress-refreshed migration deadline and a single INSERT-only transaction.
  • Classify DuckDB fatal errors and convert recovery failures into terminal FTS-only states.
  • Make marker recovery files-first and marker-last without rebuilding until marker removal succeeds.
  • Handle Windows EBUSY, orphan WAL, post-commit failure, and cleanup failure without retry loops.
  • Defer quarantined clear and agent cleanup safely until restart.
  • Surface cleanupPendingRestart in the Settings UI.
  • Add unit, native DuckDB/VSS, crash-recovery, performance, and Windows handle regression coverage.

Summary by CodeRabbit

  • New Features

    • Introduced a resilient Memory Vector Store v2 with deterministic v1→v2 migration and recovery.
    • Uses exact vector search for improved reliability (no persistent ANN indexing).
    • Agent deletion and memory clearing now report when cleanup is deferred until the next restart.
    • Added user-facing notifications for deferred cleanup, including localized messaging.
  • Bug Fixes

    • Prevented indefinite hangs by quarantining unusable native storage and bounding failure behaviors.
  • Documentation

    • Added/expanded architecture and incident specs for migration, recovery, and hardening.
  • Tests

    • Expanded native, crash-recovery, quarantine, migration, and Windows coverage, plus added CI smoke checks for native VSS.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 97032c87-e6b6-4728-87b3-2d351e9349ee

📥 Commits

Reviewing files that changed from the base of the PR and between 97f15e1 and b9c023c.

📒 Files selected for processing (9)
  • docs/architecture/memory-vector-store-v2/plan.md
  • docs/architecture/memory-vector-store-v2/tasks.md
  • docs/issues/memory-vector-store-corruption-hang/spec.md
  • src/main/presenter/memoryPresenter/index.ts
  • src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts
  • src/main/presenter/memoryPresenter/services/managementService.ts
  • test/main/presenter/memory/embeddingPipeline.test.ts
  • test/main/presenter/memory/managementService.test.ts
  • test/main/presenter/memory/serviceTestSupport.ts
🚧 Files skipped from review as they are similar to previous changes (6)
  • docs/architecture/memory-vector-store-v2/tasks.md
  • test/main/presenter/memory/serviceTestSupport.ts
  • src/main/presenter/memoryPresenter/index.ts
  • docs/architecture/memory-vector-store-v2/plan.md
  • src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts
  • docs/issues/memory-vector-store-corruption-hang/spec.md

📝 Walkthrough

Walkthrough

The PR introduces Memory Vector Store Format v2 with exact scans, staged publication, legacy migration, quarantine recovery, and native crash validation. Cleanup APIs now return restart-pending status, which propagates through routes, clients, UI notifications, and localized strings.

Changes

Memory vector store v2 and migration

Layer / File(s) Summary
V2 format and migration flow
docs/architecture/memory-vector-store-v2/*, src/main/presenter/memoryPresenter/infra/*
Defines plain-table exact scans, legacy migration, fenced reads, staged publication, metadata validation, WAL recovery, and quarantine markers.
Lease quarantine and cleanup propagation
src/main/presenter/memoryPresenter/infra/vectorStoreManager.ts, src/main/presenter/memoryPresenter/index.ts, src/main/presenter/index.ts, src/main/presenter/memoryPresenter/services/*
Quarantines fatal stores, blocks lease admission, defers unsafe cleanup, and reports restart-pending cleanup through deletion and memory clearing.
Native recovery validation
test/main/presenter/memoryVectorStore.test.ts, test/main/presenter/memoryVectorStoreV2Native.test.ts, test/main/presenter/memory/legacyV1Reader.test.ts, test/fixtures/memory-vector-store-v2/*
Tests schema behavior, migration, VSS loading, crash boundaries, WAL recovery, and Windows locking semantics.
Cleanup APIs and UI
src/shared/contracts/routes/*, src/main/routes/*, src/renderer/api/*, src/renderer/settings/components/*, src/renderer/src/i18n/*
Extends cleanup response payloads and displays localized restart-cleanup notifications.
Native CI gates
.github/workflows/*
Installs or verifies DuckDB VSS and runs native memory validation with VSS required.

Estimated code review effort: 5 (Critical) | ~120 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly captures the main change: migrating memory vector storage to a resilient v2 implementation.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/memory-vector-store-v2

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts (1)

843-861: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Remove abandoned agents from embeddingWarmupAgents.

abandonAgent clears the warmup promise map but never removes agentId from embeddingWarmupAgents. getAgentInFlight can therefore continue discovering the shared warmup as work belonging to the deleted agent, defeating the abandon path and retaining stale agent associations. Remove the ID from each set and delete empty sets.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts` around lines
843 - 861, Update abandonAgent to remove agentId from every set in
embeddingWarmupAgents, deleting any sets left empty after removal. Keep the
existing cleanup behavior intact and ensure no stale agent association remains
discoverable through getAgentInFlight.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/architecture/memory-vector-store-v2/plan.md`:
- Line 34: In the authoritative store description, replace “cleanup afterwards”
with the American-English “cleanup afterward,” preserving the surrounding
wording.

In `@docs/issues/memory-vector-store-corruption-hang/spec.md`:
- Around line 30-34: Update the fenced log block in the issue specification to
declare the text language on its opening fence, preserving the existing log
content unchanged.

In `@src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts`:
- Line 48: Update runReindex to inspect the VectorStoreCleanupDisposition
returned by resetAgentStore and immediately stop when it is 'pending-restart'.
Ensure no embeddings are drained, emitted, or used to warm the store after a
deferred reset, while preserving normal reindexing for other dispositions.

In `@src/main/presenter/memoryPresenter/ports.ts`:
- Line 378: The markVectorStoreQuarantined contract in
src/main/presenter/memoryPresenter/ports.ts at lines 378-378 must remain
required. Update the default factory wiring in
src/main/presenter/memoryPresenter/index.ts at lines 142-143 to always provide
this callback, using an explicit no-op for test adapters that do not persist
quarantine markers.

In `@src/main/presenter/memoryPresenter/services/managementService.ts`:
- Around line 645-670: Update clearMemories so VectorStoreQuarantineMarkerError
from resetAgentStore does not bypass post-clear bookkeeping: after clearByAgent
commits removals, still emit the change and clear consolidation cooldown when
applicable, then rethrow the marker error. Ensure the caller’s diagnostics
cleanup also executes before propagation, while preserving existing handling for
non-marker reset failures and return behavior for successful clears.

---

Outside diff comments:
In `@src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts`:
- Around line 843-861: Update abandonAgent to remove agentId from every set in
embeddingWarmupAgents, deleting any sets left empty after removal. Keep the
existing cleanup behavior intact and ensure no stale agent association remains
discoverable through getAgentInFlight.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 04832f2d-04b2-4ffc-aa6f-3edbcdb2851c

📥 Commits

Reviewing files that changed from the base of the PR and between a93d573 and 97f15e1.

📒 Files selected for processing (65)
  • .github/workflows/prcheck.yml
  • .github/workflows/windows-arm64-e2e.yml
  • docs/architecture/memory-vector-store-v2/plan.md
  • docs/architecture/memory-vector-store-v2/spec.md
  • docs/architecture/memory-vector-store-v2/tasks.md
  • docs/issues/memory-vector-store-corruption-hang/spec.md
  • src/main/presenter/configPresenter/index.ts
  • src/main/presenter/index.ts
  • src/main/presenter/memoryPresenter/domain/types.ts
  • src/main/presenter/memoryPresenter/index.ts
  • src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts
  • src/main/presenter/memoryPresenter/infra/legacyV1Reader.ts
  • src/main/presenter/memoryPresenter/infra/legacyVssLoader.ts
  • src/main/presenter/memoryPresenter/infra/memoryVectorStore.ts
  • src/main/presenter/memoryPresenter/infra/memoryVectorStoreFormat.ts
  • src/main/presenter/memoryPresenter/infra/vectorStoreErrors.ts
  • src/main/presenter/memoryPresenter/infra/vectorStoreManager.ts
  • src/main/presenter/memoryPresenter/ports.ts
  • src/main/presenter/memoryPresenter/runtimeConstants.ts
  • src/main/presenter/memoryPresenter/services/managementService.ts
  • src/main/presenter/memoryPresenter/types.ts
  • src/main/routes/config/configRouteHandler.ts
  • src/main/routes/index.ts
  • src/renderer/api/ConfigClient.ts
  • src/renderer/api/MemoryClient.ts
  • src/renderer/settings/components/DeepChatAgentsSettings.vue
  • src/renderer/settings/components/MemoryDiagnosticsPanel.vue
  • src/renderer/src/i18n/da-DK/settings.json
  • src/renderer/src/i18n/de-DE/settings.json
  • src/renderer/src/i18n/en-US/settings.json
  • src/renderer/src/i18n/es-ES/settings.json
  • src/renderer/src/i18n/fa-IR/settings.json
  • src/renderer/src/i18n/fr-FR/settings.json
  • src/renderer/src/i18n/he-IL/settings.json
  • src/renderer/src/i18n/id-ID/settings.json
  • src/renderer/src/i18n/it-IT/settings.json
  • src/renderer/src/i18n/ja-JP/settings.json
  • src/renderer/src/i18n/ko-KR/settings.json
  • src/renderer/src/i18n/ms-MY/settings.json
  • src/renderer/src/i18n/pl-PL/settings.json
  • src/renderer/src/i18n/pt-BR/settings.json
  • src/renderer/src/i18n/ru-RU/settings.json
  • src/renderer/src/i18n/tr-TR/settings.json
  • src/renderer/src/i18n/vi-VN/settings.json
  • src/renderer/src/i18n/zh-CN/settings.json
  • src/renderer/src/i18n/zh-HK/settings.json
  • src/renderer/src/i18n/zh-TW/settings.json
  • src/shared/contracts/routes/config.routes.ts
  • src/shared/contracts/routes/memory.routes.ts
  • src/shared/types/presenters/core.presenter.d.ts
  • test/fixtures/memory-vector-store-v2/crash-worker.mjs
  • test/main/presenter/configPresenter/deprecatedProviderCleanup.test.ts
  • test/main/presenter/fakes/memoryFakes.ts
  • test/main/presenter/fakes/memoryPresenterTestAdapter.ts
  • test/main/presenter/memory/legacyV1Reader.test.ts
  • test/main/presenter/memory/managementService.test.ts
  • test/main/presenter/memory/serviceTestSupport.ts
  • test/main/presenter/memory/vectorStoreManager.test.ts
  • test/main/presenter/memoryVectorStore.test.ts
  • test/main/presenter/memoryVectorStoreV2Native.test.ts
  • test/main/routes/dispatcher.test.ts
  • test/memory-test-scope.json
  • test/renderer/api/clients.test.ts
  • test/renderer/components/DeepChatAgentsSettings.test.ts
  • test/renderer/components/MemoryDiagnosticsPanel.test.ts

Comment thread docs/architecture/memory-vector-store-v2/plan.md Outdated
Comment thread docs/issues/memory-vector-store-corruption-hang/spec.md Outdated
Comment thread src/main/presenter/memoryPresenter/infra/embeddingPipeline.ts
Comment thread src/main/presenter/memoryPresenter/ports.ts
Comment thread src/main/presenter/memoryPresenter/services/managementService.ts
@yyhhyyyyyy
yyhhyyyyyy merged commit 1357792 into dev Jul 13, 2026
4 checks passed
@yyhhyyyyyy
yyhhyyyyyy deleted the refactor/memory-vector-store-v2 branch July 13, 2026 13:12
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