Skip to content

fix: import dedup checks remote DB when LORE_REMOTE_URL is set#338

Merged
BYK merged 1 commit into
mainfrom
fix/import-remote-dedup
May 15, 2026
Merged

fix: import dedup checks remote DB when LORE_REMOTE_URL is set#338
BYK merged 1 commit into
mainfrom
fix/import-remote-dedup

Conversation

@BYK
Copy link
Copy Markdown
Owner

@BYK BYK commented May 15, 2026

Summary

  • Fix split-brain bug where lore import with LORE_REMOTE_URL checked the local DB for dedup but wrote import records to the remote DB, causing duplicate imports on every run
  • Skip ensureProject() in remote mode to avoid creating phantom local project records

Problem

When LORE_REMOTE_URL is set:

  1. isImported() queries the local import_history table → always empty
  2. All sessions detected as "new" → sent to remote for extraction
  3. remotePost("/api/v1/import/record") writes to the remote DB
  4. Next run: local DB still empty → same sessions re-detected → double extraction

This means every lore import in remote mode re-imports everything, wasting LLM calls and creating duplicate knowledge entries.

Fix

  1. Query remote import history: Before filtering sessions, fetch GET /api/v1/import/history?git_remote=... from the remote gateway and check against it instead of the local DB
  2. Skip local ensureProject(): In remote mode, don't create a phantom project record in the local DB (detection/readChunks don't need it)
  3. Record locally too: After the remote record succeeds, also call local recordImport() as belt-and-suspenders — if the user later runs without LORE_REMOTE_URL, the local DB already has the records

Files Changed

File Change
packages/gateway/src/cli/import.ts Restructure commandImport() to fetch remote history for dedup; skip local ensureProject() in remote mode; record imports locally after remote

The import command's idempotency check (isImported) was querying the
local SQLite DB, but import records were written to the remote gateway.
This caused duplicate imports on every run in remote mode.

Fix:
- Fetch import history from remote gateway via GET /api/v1/import/history
  and check against it during session filtering
- Skip ensureProject() in remote mode to avoid creating phantom local
  project records
- Also record imports locally after remote record succeeds (belt-and-
  suspenders: remote is source of truth, local prevents re-detection
  if user later runs without LORE_REMOTE_URL)
@BYK BYK merged commit 7737dbd into main May 15, 2026
7 checks passed
@BYK BYK deleted the fix/import-remote-dedup branch May 15, 2026 13:10
BYK added a commit that referenced this pull request May 15, 2026
## Summary

Self-review follow-up for PR #338. Hardens error handling in the remote
import path and cleans up code duplication.

## Fixes

**Critical:**
- **C2**: Only swallow 400/404 on remote history fetch (project not yet
known to remote). Server errors (500), auth failures (401), and network
errors now propagate instead of silently skipping dedup — which could
cause duplicate imports
- **C3**: Wrap `remotePost` for `/api/v1/import/record` in try/catch.
Previously a single record failure would abort the entire import, losing
all progress for remaining agents/sessions
- **M5**: Wrap `remotePost` for `/api/v1/import/extract` in try/catch.
On extraction failure, logs error and continues to next agent instead of
crashing

**Cleanup:**
- **M3**: Replace hand-rolled git remote resolution with
`projectQueryParams()` from `remote.ts` (eliminates code duplication)
- Remove redundant dynamic `import("@loreai/core")` for `exportLoreFile`
— already statically imported
- Clarify `ensureProject` skip comment: local project creation from
belt-and-suspenders `recordImport()` is intentional, not a bug

## Files Changed

| File | Change |
|---|---|
| `packages/gateway/src/cli/import.ts` | Error handling, code cleanup |
This was referenced May 15, 2026
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