Skip to content

fix: prevent test data from leaking into production DB#320

Merged
BYK merged 2 commits into
mainfrom
fix/test-db-leak-guard
May 14, 2026
Merged

fix: prevent test data from leaking into production DB#320
BYK merged 2 commits into
mainfrom
fix/test-db-leak-guard

Conversation

@BYK
Copy link
Copy Markdown
Owner

@BYK BYK commented May 14, 2026

Summary

  • Add ensureProject() guard that rejects synthetic test paths (/test/...) when LORE_DB_PATH is unset (production DB), preventing test data leaks when test files are run directly with bun run instead of bun test
  • Harden bunfig.toml with explicit NODE_ENV=test in [test.env] as belt-and-suspenders
  • Expose lore data delete project <path|id> via CLI so leaked test projects can be cleaned up

Context

A test project /test/ltm/debug-forsession leaked into the production DB. Root cause: running a test file directly with bun run (for debugging) bypasses the bunfig.toml preload that sets LORE_DB_PATH to a temp DB. The existing NODE_ENV=test guard in db() doesn't help because NODE_ENV isn't set when the test runner isn't involved.

The new guard in ensureProject() catches this by detecting that paths starting with /test/ are synthetic test paths that should never exist in the production DB. No real project path on any OS starts with /test/.

Changes

File Change
packages/core/src/db.ts Guard in ensureProject() — rejects /test/ paths when LORE_DB_PATH unset
bunfig.toml Add [test.env] with NODE_ENV = "test"
packages/core/bunfig.toml Add [test.env] with NODE_ENV = "test"
packages/gateway/src/cli/data.ts Add project type to lore data delete, update help text
packages/core/test/db.test.ts 3 tests for the guard

Testing

  • All 1404 tests pass, 0 failures
  • Typecheck passes for all 4 packages
  • After merging, run lore data delete project /test/ltm/debug-forsession to clean up the leaked project

BYK added 2 commits May 14, 2026 19:27
Add ensureProject() guard that rejects synthetic test paths (/test/...)
when LORE_DB_PATH is unset (production DB). This catches the case where
a test file is run directly with `bun run` instead of `bun test`,
bypassing the bunfig.toml preload that sets up the temp test DB.

Also:
- Harden bunfig.toml with explicit NODE_ENV=test in [test.env]
- Expose `lore data delete project <path|id>` via CLI for cleanup
- Add unit tests for the guard
…, comments

- Fix misleading test comment about ensureProject not being called
- Add boundary test: /testing/... and /test (no slash) don't trigger guard
- Validate project existence before confirmation prompt in CLI delete
- Show project name and counts in confirmation prompt
- Clarify LORE_DB_PATH assumption in guard comment
@BYK BYK merged commit ab289f3 into main May 14, 2026
7 checks passed
@BYK BYK deleted the fix/test-db-leak-guard branch May 14, 2026 19:40
@craft-deployer craft-deployer Bot mentioned this pull request May 14, 2026
6 tasks
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