Fix #1929: Bug: /api/v1/embeddings/maintenance causes 100% CPU and event loop starvation on#1930
Closed
Memtensor-AI wants to merge 0 commit into
Closed
Fix #1929: Bug: /api/v1/embeddings/maintenance causes 100% CPU and event loop starvation on#1930Memtensor-AI wants to merge 0 commit into
Memtensor-AI wants to merge 0 commit into
Conversation
Collaborator
Author
❌ Automated Test Results: FAILED
Failed tests:
Error detailsBranch: |
7b6a229 to
258c3a3
Compare
Collaborator
Author
❌ Automated Test Results: FAILED
Failed tests:
Error detailsBranch: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes MemOS#1929 (100% CPU + event-loop starvation on
GET /api/v1/embeddings/maintenance) inside the TypeScript subprojectapps/memos-local-plugin. Root cause:computeEmbeddingMaintenanceStats()walked every row oftraces/policies/world_model/skillswith the full BLOB vector columns selected, then decoded each vector into aFloat32Arrayonly to read its.length. On a 93K-trace corpus that allocated ~270MB of heap per call and blocked the Node main thread for 4+ minutes.Primary fix: added
embeddingMaintenanceStats(expectedByteLength)to each of the four vector-bearing repos plus aRepos.embeddingMaintenanceStats(expectedDim)aggregator. The new path usesSELECT COUNT(*), SUM(CASE WHEN ...)withLENGTH(blob)for the dim check — no BLOB ever crosses the better-sqlite3 boundary. The trace path mirrors the existing eligibility filter (shouldTraceHaveEmbeddings) and the lightweight-memory exclusion onvec_actionin SQL usingLENGTH(TRIM(...))and the establishedinstr(tags_json, '"lightweight_memory"')pattern.computeEmbeddingMaintenanceStats()now takes the SQL fast path wheneverembedder.dimensions > 0; the legacy JS walk stays as a fallback for the "no embedder configured" case so dimension inference still works forrebuildEmbeddings.Secondary fix (opt-in): added
algorithm.retrieval.vectorScanMaxAgeMsconfig field (default0= disabled, range0..365d). When> 0, tier-2traces.searchByVectorpre-filters byts >= now - vectorScanMaxAgeMsat the SQL layer, bounding the per-turn brute-force scan reported as a separate 5-30s blocking issue. Older memories still surface via the FTS / pattern channels which already run in parallel.Verification:
tsc -p tsconfig.json --noEmitexits 0.vitest run tests/unit→ 1045 pass / 1 skip / 2 pre-existing failures (page-clamp + migrator schema drift, both reproduce on a clean base-branch checkout). Integration suite passes. New coverage: 4 SQL-COUNT tests intests/unit/storage/embedding-maintenance-stats.test.tsand 1 time-window test intests/unit/retrieval/tier2.test.ts. The 28 existing pipeline tests now exercise the SQL path end-to-end (test embedder advertisesdimensions=384), proving the SQL path returns identical counts to the JS-walk on the same seeded dataset.Related Issue (Required): Fixes #1929
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Automated tests are pending.
Checklist
@MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller please review this PR.
Reviewer Checklist