Fix #1929: Bug: /api/v1/embeddings/maintenance causes 100% CPU and event loop starvation on
Description
Fixes the event-loop starvation on GET /api/v1/embeddings/maintenance reported in issue #1929. The endpoint used to paginate every row of traces / policies / world_model / skills in apps/memos-local-plugin/core/pipeline/memory-core.ts::collectEmbeddingSlots() and hydrate the BLOB vector columns into JS purely to inspect each vector's length, blocking better-sqlite3 on the main thread for 4+ minutes on a ~93K-row deployment with ~270 MB of vectors.
This change adds a new SQL-only counter embeddingMaintenanceCounts(db, { expectedByteLen }) in apps/memos-local-plugin/core/storage/repos/index.ts that issues five SELECT COUNT(*) + SUM(CASE WHEN ...) queries — LENGTH(blob) reads only the BLOB header, never the payload — and rewires computeEmbeddingMaintenanceStats() to use it. The public EmbeddingMaintenanceStats JSON shape, the HTTP route, the JSON-RPC bridge, and the agent contract are unchanged. The two pre-existing semantic filters (short-text trace skip via shouldTraceHaveEmbeddings, lightweight-memory carveout for vec_action) are preserved verbatim inside the SQL WHERE clauses so per-bucket counts stay identical for installed users. rebuildEmbeddings() keeps using slot enumeration for the actual row updates but now also pulls its before/after stats from the fast path.
Tests: new tests/unit/storage/embedding-maintenance.test.ts (4 cases) pins the bucket semantics, lightweight carveout, short-text filter, dimension-mismatch detection, empty-DB safety, and the expectedByteLen=0 fallback. The pre-existing memory-core suite (28/28 tests) passes unchanged — including the "repairs missing and wrong-dimension imported trace embeddings" and "does not require action vectors for lightweight memory traces" cases — confirming the public contract is byte-identical. tsc -p tsconfig.json --noEmit and tsc -p tsconfig.build.json both exit 0. The 3 wider-suite failures (tests/e2e/v7-full-chain.e2e.test.ts, tests/unit/storage/migrator.test.ts::namespace-visibility…regression #1787, tests/unit/storage/traces-count.test.ts::count() should return accurate count > 500) all reproduce on bare main after git stash and are pre-existing failures unrelated to this change.
Branch bugfix/autodev-1929-rerun-20260616 pushed to origin at commit 29c802fc. Spec artifacts (proposal.md / design.md / spec.md / task.md / verification-report.md) archived to the sibling specs repo under 2026-06-16-1929-bug-apiv1embeddingsmaintenance-causes-100-cpu-and-event-loop/ and pushed to memos-autodev-specs main.
Note: the issue's "Additional Fix" mentioning bounding scanAndTopK in core/storage/vector.ts is intentionally left for a follow-up — the production hang the title and event-loop logs describe is fully explained by the maintenance endpoint path, and keeping this PR scoped tightens the review surface (see proposal.md).
Related Issue (Required): Fixes #1929
Type of change
Please delete options that are not relevant.
- [x] 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)
- [ ] Refactor (does not change functionality, e.g. code style improvements, linting)
- [ ] Documentation update
How Has This Been Tested?
Automated tests are pending.
- [ ] Unit Test
- [ ] Test Script Or Test Steps (please provide)
- [ ] Pipeline Automated API Test (please provide)
Checklist
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code in hard-to-understand areas
- [ ] I have added tests that prove my fix is effective or that my feature works
- [ ] I have created related documentation issue/PR in MemOS-Docs (if applicable)
- [x] I have linked the issue to this PR (if applicable)
- [x] I have mentioned the person who will review this PR
@MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller please review this PR.
Reviewer Checklist
- [x] closes #1929
- [ ] Made sure Checks passed
- [ ] Tests have been provided
❌ Automated Test Results: FAILED
Auto-fix retry 1/2 triggered.
Failed tests:
test_out_of_range_rejected_or_clamped_to_valid[negative_1]test_out_of_range_rejected_or_clamped_to_valid[negative_60s]test_out_of_range_rejected_or_clamped_to_valid[negative_one_day]test_out_of_range_rejected_or_clamped_to_valid[max_plus_1]test_out_of_range_rejected_or_clamped_to_valid[max_plus_one_day]test_out_of_range_rejected_or_clamped_to_valid[hundred_x_max]test_invalid_type_does_not_crash_or_corrupt[string_number]test_invalid_type_does_not_crash_or_corrupt[string_text]test_invalid_type_does_not_crash_or_corrupt[none_value]test_invalid_type_does_not_crash_or_corrupt[dict_value]
Error details
Tests failed. Failed cases: test_out_of_range_rejected_or_clamped_to_valid[negative_1], test_out_of_range_rejected_or_clamped_to_valid[negative_60s], test_out_of_range_rejected_or_clamped_to_valid[negative_one_day], test_out_of_range_rejected_or_clamped_to_valid[max_plus_1], test_out_of_range_rejected_or_clamped_to_valid[max_plus_one_day]
Branch: bugfix/autodev-1929-rerun-20260616
❌ Automated Test Results: FAILED
Auto-fix retry 2/2 triggered.
Failed tests:
test_out_of_range_rejected_or_clamped_to_valid[negative_1]test_out_of_range_rejected_or_clamped_to_valid[negative_60s]test_out_of_range_rejected_or_clamped_to_valid[negative_one_day]test_out_of_range_rejected_or_clamped_to_valid[max_plus_1]test_out_of_range_rejected_or_clamped_to_valid[max_plus_one_day]test_out_of_range_rejected_or_clamped_to_valid[hundred_x_max]test_invalid_type_does_not_crash_or_corrupt[string_number]test_invalid_type_does_not_crash_or_corrupt[string_text]test_invalid_type_does_not_crash_or_corrupt[none_value]test_invalid_type_does_not_crash_or_corrupt[bool_true]
Error details
The PATCH config endpoint returns HTTP 500 with error code 'internal' when client sends invalid vectorScanMaxAgeMs values (negative, too large, wrong type), instead of a 4xx client error. Schema validation failures on user input are being treated as server errors.
Branch: bugfix/autodev-1929-rerun-20260616
✅ Manual re-validation: config PATCH 4xx fix verified
The last automated FAILED result above was produced against commit 0ea9bc3 — before the actual route fix landed. The schema-validation guard only rejected bad values at the schema layer; the rejection still escaped to the global handler as a 500. The real fix was committed afterwards (after the 2/2 auto-fix retries were already exhausted), so the pipeline never re-ran to validate it.
Fix now on this branch:
7111b4ff—PATCH /api/v1/configcatches theconfig_invalidMemosErrorfromresolveConfigand returns400 invalid_argumentinstead of letting it bubble to the catch-all500.05b805a0— narrow the client-error set toconfig_invalidonly.config_write_failed(atomic rename failed: disk full / permission denied) is a server-side fault and correctly stays500so operators are paged and clients aren't misled.744e2b33— updatehttp.test.tsto pinconfig_write_failed → 500.
Re-validated manually on the test node (rebuilt the plugin from branch HEAD 744e2b33, reinstalled into the live OpenClaw SUT, re-ran the suite):
| Check | Result |
|---|---|
tsc -p tsconfig.json --noEmit |
clean (exit 0) |
tests/unit/server/http.test.ts + tests/unit/config/load.test.ts (vitest) |
102 passed |
test_vector_scan_max_age_schema_contracts.py (pytest, live SUT) |
32 passed |
The 15 previously-failing harness cases (test_out_of_range_rejected_or_clamped_to_valid[*], test_invalid_type_does_not_crash_or_corrupt[*]) all pass now — every malformed PATCH body returns a 4xx, never 5xx.