Skip to content

Conversation

@nekomeowww
Copy link
Member

@nekomeowww nekomeowww commented Dec 6, 2025

💻 Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 👷 build
  • ⚡️ perf
  • ✅ test
  • 📝 docs
  • 🔨 chore

🔗 Related Issue

🔀 Description of Change

🧪 How to Test

  • Tested locally
  • Added/updated tests
  • No tests needed

📸 Screenshots / Videos

Before After
... ...

📝 Additional Information

Summary by Sourcery

Optimize user memory-related database queries by adjusting indexes on user and user_memory identifiers.

Enhancements:

  • Replace vector-based indexes on user memories with a btree index on user_id to better support common query patterns.
  • Add btree indexes on user_id and user_memory_id across user memories contexts, preferences, identities, and experiences tables to improve lookup performance.
  • Update database migration metadata and schema snapshot to reflect the new index definitions.

@nekomeowww nekomeowww requested a review from arvinxx December 6, 2025 18:22
@vercel
Copy link

vercel bot commented Dec 6, 2025

@nekomeowww is attempting to deploy a commit to the LobeHub OSS Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Dec 6, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 6, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds missing btree indexes on user_id and user_memory_id across user memories tables while removing two unused HNSW vector indexes, and wires the changes into the SQL and schema metadata migrations.

Entity relationship diagram for updated user memories indexes

erDiagram
  user_memories {
    int id
    int user_id_indexed
    vector summary_vector_1024
    vector details_vector_1024
  }

  user_memories_contexts {
    int id
    int user_id_indexed
    int user_memory_id
    text type
    vector description_vector
  }

  user_memories_preferences {
    int id
    int user_id_indexed
    int user_memory_id_indexed
    vector conclusion_directives_vector
  }

  user_memories_identities {
    int id
    int user_id_indexed
    int user_memory_id_indexed
    text type
    vector description_vector
  }

  user_memories_experiences {
    int id
    int user_id_indexed
    int user_memory_id_indexed
    text type
    vector key_learning_vector
  }

  user_memories ||--o{ user_memories_contexts : user_memory_id
  user_memories ||--o{ user_memories_preferences : user_memory_id
  user_memories ||--o{ user_memories_identities : user_memory_id
  user_memories ||--o{ user_memories_experiences : user_memory_id

  user_memories ||--o{ user_memories_contexts : user_id
  user_memories ||--o{ user_memories_preferences : user_id
  user_memories ||--o{ user_memories_identities : user_id
  user_memories ||--o{ user_memories_experiences : user_id
Loading

Flow diagram for migration 0060 index changes

flowchart TD
  A[start_migration_0060] --> B[drop_index_user_memories_summary_vector_1024_index]
  B --> C[drop_index_user_memories_details_vector_1024_index]

  C --> D[create_index_user_memories_user_id_index_btree]
  D --> E[create_index_user_memories_contexts_user_id_index_btree]
  E --> F[create_index_user_memories_experiences_user_id_index_btree]
  F --> G[create_index_user_memories_experiences_user_memory_id_index_btree]
  G --> H[create_index_user_memories_identities_user_id_index_btree]
  H --> I[create_index_user_memories_identities_user_memory_id_index_btree]
  I --> J[create_index_user_memories_preferences_user_id_index_btree]
  J --> K[create_index_user_memories_preferences_user_memory_id_index_btree]
  K --> L[end_migration_0060]
Loading

File-Level Changes

Change Details Files
Switches primary performance focus on user memories from vector similarity indexes to standard btree indexes on user_id and user_memory_id.
  • Removes HNSW vector indexes on summaryVector1024 and detailsVector1024 in the userMemories table schema.
  • Adds a btree index on userId for the userMemories table in the schema definition.
  • Updates the database schema documentation to reflect the new index configuration.
packages/database/src/schemas/userMemories.ts
docs/development/database-schema.dbml
Adds missing user_id and user_memory_id btree indexes to related user memories tables for faster lookups and joins.
  • Adds userId index for userMemoriesContexts schema.
  • Adds userId and userMemoryId indexes for userMemoriesPreferences schema.
  • Adds userId and userMemoryId indexes for userMemoriesIdentities schema.
  • Adds userId and userMemoryId indexes for userMemoriesExperiences schema.
packages/database/src/schemas/userMemories.ts
docs/development/database-schema.dbml
Introduces a migration to drop old vector indexes and create the new user_id and user_memory_id indexes, and registers it in migration metadata.
  • Creates SQL migration 0060 to drop obsolete vector indexes and create the new btree indexes across user memories tables.
  • Registers migration 0060 in the migrations journal and core migrations manifest.
  • Adds a snapshot of the schema at migration 0060 for migration tooling.
packages/database/migrations/0060_add_user_memory_user_id_index.sql
packages/database/migrations/meta/_journal.json
packages/database/src/core/migrations.json
packages/database/migrations/meta/0060_snapshot.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gru-agent
Copy link
Contributor

gru-agent bot commented Dec 6, 2025

TestGru Assignment

Summary

Link CommitId Status Reason
Detail f5de615 🚫 Skipped No files need to be tested {"docs/development/database-schema.dbml":"File path does not match include patterns.","packages/database/migrations/0060_add_user_memory_user_id_index.sql":"File path does not match include patterns.","packages/database/migrations/meta/0060_snapshot.json":"File path does not match include patterns.","packages/database/migrations/meta/_journal.json":"File path does not match include patterns.","packages/database/src/core/migrations.json":"The code does not contain any functions or classes.","packages/database/src/schemas/userMemories.ts":"Can not find valuable test target.\nuserMemories: Out of scope - This export is a database table schema definition, not a function or class. Table schemas are typically validated through integration or migration tests, not unit tests.\nuserMemoriesContexts: Out of scope - This export is a database table schema definition, not a functio…"}

History Assignment

Tip

You can @gru-agent and leave your feedback. TestGru will make adjustments based on your input

@dosubot dosubot bot added the ⚡️ Performance Performance issue label Dec 6, 2025
@vercel
Copy link

vercel bot commented Dec 6, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
lobehub Ready Ready Preview Comment Dec 6, 2025 6:45pm

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • The migration drops user_memories_*_vector_1024_index without IF EXISTS; to make repeated or partially-applied migrations safer, consider using DROP INDEX IF EXISTS for these two vector indexes as well.
  • Given you now have separate user_id and type indexes on several tables, if common queries filter on both fields together it may be worth changing these to composite indexes (e.g. (user_id, type)) to better match the access patterns.
  • By fully removing the HNSW vector indexes from user_memories, any remaining vector similarity queries on that table will fall back to sequential scans; if those queries are still in use, consider whether a different indexing strategy or relocation of vector search is needed rather than a full removal.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The migration drops `user_memories_*_vector_1024_index` without `IF EXISTS`; to make repeated or partially-applied migrations safer, consider using `DROP INDEX IF EXISTS` for these two vector indexes as well.
- Given you now have separate `user_id` and `type` indexes on several tables, if common queries filter on both fields together it may be worth changing these to composite indexes (e.g. `(user_id, type)`) to better match the access patterns.
- By fully removing the HNSW vector indexes from `user_memories`, any remaining vector similarity queries on that table will fall back to sequential scans; if those queries are still in use, consider whether a different indexing strategy or relocation of vector search is needed rather than a full removal.

## Individual Comments

### Comment 1
<location> `packages/database/migrations/0060_add_user_memory_user_id_index.sql:1-2` </location>
<code_context>
+DROP INDEX "user_memories_summary_vector_1024_index";--> statement-breakpoint
+DROP INDEX "user_memories_details_vector_1024_index";--> statement-breakpoint
+CREATE INDEX IF NOT EXISTS "user_memories_user_id_index" ON "user_memories" USING btree ("user_id");--> statement-breakpoint
+CREATE INDEX IF NOT EXISTS "user_memories_contexts_user_id_index" ON "user_memories_contexts" USING btree ("user_id");--> statement-breakpoint
</code_context>

<issue_to_address>
**issue:** Use `IF EXISTS` for `DROP INDEX` to make the migration more robust across environments.

These `DROP INDEX` statements will error if the indexes were never created or were removed manually in some environments, causing the migration to fail. Using `DROP INDEX IF EXISTS` keeps the migration idempotent and safe on partially migrated or manually modified databases.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +1 to +2
DROP INDEX "user_memories_summary_vector_1024_index";--> statement-breakpoint
DROP INDEX "user_memories_details_vector_1024_index";--> statement-breakpoint
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: Use IF EXISTS for DROP INDEX to make the migration more robust across environments.

These DROP INDEX statements will error if the indexes were never created or were removed manually in some environments, causing the migration to fail. Using DROP INDEX IF EXISTS keeps the migration idempotent and safe on partially migrated or manually modified databases.

@codecov
Copy link

codecov bot commented Dec 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.60%. Comparing base (b09361f) to head (f5de615).
⚠️ Report is 41 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff            @@
##             next   #10643     +/-   ##
=========================================
  Coverage   80.60%   80.60%             
=========================================
  Files         977      977             
  Lines       66579    66579             
  Branches    10266     8802   -1464     
=========================================
  Hits        53668    53668             
  Misses      12911    12911             
Flag Coverage Δ
app 73.05% <ø> (ø)
database 98.32% <ø> (ø)
packages/agent-runtime 98.08% <ø> (ø)
packages/context-engine 91.61% <ø> (ø)
packages/conversation-flow 98.05% <ø> (ø)
packages/electron-server-ipc 93.76% <ø> (ø)
packages/file-loaders 92.21% <ø> (ø)
packages/model-bank 100.00% <ø> (ø)
packages/model-runtime 91.66% <ø> (ø)
packages/prompts 79.17% <ø> (ø)
packages/python-interpreter 96.50% <ø> (ø)
packages/utils 95.31% <ø> (ø)
packages/web-crawler 96.81% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Store 73.05% <ø> (ø)
Services 56.44% <ø> (ø)
Server 75.34% <ø> (ø)
Libs 38.57% <ø> (ø)
Utils 83.36% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ Performance Performance issue size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant