Skip to content

[TEST] bugfix/ladybug — support knowledgeIds scoping and optimize query execution - #135

Open
bharatsachya wants to merge 2 commits into
pre-releasefrom
fix/ladybug
Open

bharatsachya wants to merge 2 commits into
pre-releasefrom
fix/ladybug

Conversation

@bharatsachya

@bharatsachya bharatsachya commented Jun 9, 2026

Copy link
Copy Markdown
Contributor
  • Extracted Schema Initialization: Moved the database schema table definitions (nodeTables, relTables) and the ensureSchema function out of client.ts and into a new, dedicated schema module schema.ts.
  • Added knowledgeIds Scoping for Ladybug Searches:
    • Updated search builders in cypherBuilders.ts and keyword lookup queries in keywordLookup.ts to filter using knowledgeId IN $knowledgeIds when an array of allowed knowledgebase IDs is provided.
    • Guided parameter mapping in smartSearch.ts so that the $knowledgeIds parameter is only bound when the query uses the filter condition, preventing engine errors from unused parameters.
  • Optimized Connection Closing: Improved closeLadybug in client.ts to clear the prepared statements cache (preparedCache) and properly close and await the release of native DB and Connection handles. This resolves issues with locked database files on disk.
  • Added Uncached Query Execution (_runCypherOnce):
    • Introduced _runCypherOnce in client.ts to execute unique/one-off query statements without caching their compiled plans.
    • Refactored bulkUpsertFiles in files.ts to run COPY File FROM '<temp path>' commands via _runCypherOnce. Since the temporary paths are unique per invocation, this prevents memory leaks caused by caching infinite unique prepared statements.
  • Updated Package Documentation: Updated README.md to describe the implementation status of read-side search (now native via LadybugDB) and documented exports setKnowledgeBranchInGraph and vacuumOrphanEntities.

Why

  • Search Scoping: The ConceptGraphStrategy enrichment utilizes Neo4j by querying with a knowledgeIds array scope (defaulting to the current knowledge base ID, with knowledgeId set to null). Without supporting this allowlist scoping in LadybugDB's query builder, search operations would either run completely unscoped (across all repos) or fail, resulting in incorrect results or exceptions.
  • Resource Cleanup and Locked Files: If cached prepared statements from a previous connection are not cleared when closing the client, trying to reconnect and reuse cached statements causes crashes since they references a destroyed connection handle. Failing to close the native handles properly keeps a lock on the database file on disk, which blocks subsequent reads/writes or reconnects.
  • Preventing Prepared Statement Cache Bloat & Memory Leaks: Compiling and caching prepared statements with changing values directly in the Cypher query (such as the temporary parquet file paths in COPY File FROM ...) leaks memory. By running them through _runCypherOnce, the statements are compiled, executed, and garbage-collected without populating the preparedCache.

How to test

  1. Static Checks:
    • Run typechecking to verify no TypeScript compilation/strictness violations exist:
      bun run typecheck
    • Run linter and formatter:
      bun run lint
  2. Database Connection Cleanup & File Lock Verification:
    • Run the ingestion suite or start/stop the server. Ensure that upon connection close, there are no locked file errors on disk (indicating native DB/Connection handles are successfully closed).
  3. Scoping Validation:
    • Execute a search/lookup scoped by knowledgeIds with LadybugDB active as the graph provider. Ensure results are filtered correctly to only match target knowledge bases and that the query engine does not reject parameters.
  4. Memory Stability:
    • Run multiple bulk file ingestion passes and verify that the preparedCache does not bloat or contain unique COPY File FROM query templates.

Screenshots

Backend-only changes. No UI was affected.

@bharatsachya bharatsachya added the enhancement New feature or request label Jun 9, 2026
@bharatsachya

Copy link
Copy Markdown
Contributor Author

Against #136

@bharatsachya
bharatsachya requested a review from Dead-Bytes June 9, 2026 07:48
@bharatsachya bharatsachya changed the title [TEST] improvement/ladybug-query-speed — fix prepared statement memory leak in ingestion` [TEST] bugfix/ladybug — support knowledgeIds scoping and optimize query execution Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant