Skip to content

fix: log embedding failures and fix O(n²) chunk lookup in embedPage - #37

Closed
sharziki wants to merge 1 commit into
garrytan:masterfrom
sharziki:fix/silent-embed-failure-and-embed-perf
Closed

sharziki wants to merge 1 commit into
garrytan:masterfrom
sharziki:fix/silent-embed-failure-and-embed-perf

Conversation

@sharziki

Copy link
Copy Markdown
Contributor

Summary

  • Log embedding errors in importFromContent() instead of silently swallowing them
  • Replace O(n²) find() + indexOf() with Map lookup in embedPage(), matching the pattern already used by embedAll() in the same file

Details

1. Silent embedding failure (src/core/import-file.ts)

importFromContent() had a bare catch { /* non-fatal */ } that swallowed all embedding errors — API rate limits, auth failures, timeouts, network issues. Pages got imported without embeddings but the user had no indication why vector search was returning poor results.

Now logs:

Warning: embedding failed for my-page: 429 Too Many Requests

The error is still non-fatal (page import continues without embeddings), but at least the user knows what happened and can run gbrain embed --stale to fix it.

2. O(n²) chunk lookup (src/commands/embed.ts)

embedPage() used toEmbed.find() + toEmbed.indexOf() for every chunk to map embeddings back — two linear scans per chunk. Replaced with a single Map<number, Float32Array> lookup, matching the pattern embedAll() already uses 30 lines below in the same file.

Test plan

  • All 256 unit tests pass (bun test — 0 failures)
  • 9/9 import-file.test.ts tests pass

🤖 Generated with Claude Code

Two fixes:

1. importFromContent() silently swallowed all embedding errors with
   `catch { /* non-fatal */ }`, making it impossible to diagnose why
   vector search returned poor results. Now logs a warning with the
   error message.

2. embedPage() used find() + indexOf() per chunk to map embeddings
   back, resulting in O(n²) complexity. Replaced with a Map lookup
   matching the pattern already used by embedAll() in the same file.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@garrytan

Copy link
Copy Markdown
Owner

Included in fix wave PR #65 (v0.9.1). O(n²) chunk lookup fix landed. Clean, surgical fix. Thanks sharziki! 🙏

@garrytan garrytan closed this Apr 12, 2026
garrytan added a commit that referenced this pull request Oct 16, 2026
…nable/complete/disable under the source lock (#7 #27 #16 #17), restore marks read purge-mirror's fields (#8), backfill outside drift (#15), read-only drift scan (#24), bounded routine scans (#28), one stale-preparation code (#29), ambient source (#37), lifetime cut (#41)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
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.

2 participants