|
| 1 | +/** |
| 2 | + * Embedding Backfiller - Background worker for semantic embeddings |
| 3 | + * |
| 4 | + * What it does: |
| 5 | + * Continuously processes files with embedding_status='pending' from SQLite, |
| 6 | + * generating embeddings and upserting to LanceDB. Decouples semantic indexing |
| 7 | + * from the critical path so structural queries work immediately. |
| 8 | + * |
| 9 | + * Inputs: SQLite storage (source of truth for pending files) |
| 10 | + * Outputs: Embeddings written to LanceDB, files marked done/error |
| 11 | + * Constraints: Bounded concurrency, bounded memory (no unbounded queues) |
| 12 | + * Assumptions: Files are already parsed and in storage |
| 13 | + * Failure cases: File deleted, embedding provider down, vectorStore errors |
| 14 | + * |
| 15 | + * Design: |
| 16 | + * - Pull files in batches from SQLite (bounded memory, survives restart) |
| 17 | + * - Read file content, scrub secrets, chunk by symbols, embed via queue |
| 18 | + * - Upsert to vectorStore, mark file status (done/error) |
| 19 | + * - Bounded concurrency (N files in flight at once) |
| 20 | + * - Graceful error handling (log + mark error + continue, no crash) |
| 21 | + * - Clean start/stop for graceful shutdown |
| 22 | + * |
| 23 | + * Performance: Processes files at the rate the embedding provider allows |
| 24 | + * Concurrency: Async with configurable concurrency limit |
| 25 | + * Security: Scrubs secrets before embedding |
| 26 | + */ |
| 27 | + |
| 28 | +import { readFile } from 'node:fs/promises'; |
| 29 | +import { resolve } from 'node:path'; |
| 30 | +import { EventEmitter } from 'events'; |
| 31 | +import type { StorageProvider } from '../store/provider.js'; |
| 32 | +import type { EmbeddingQueue } from './embedding-queue.js'; |
| 33 | +import type { LanceDBVectorStore } from '../store/lance.js'; |
| 34 | +import { scrubSecrets } from '../security/scrubber.js'; |
| 35 | +import { chunkCodeForEmbedding } from '../llm/chunker.js'; |
| 36 | +import { parseFile } from './parser.js'; |
| 37 | +import { StoreError } from './errors.js'; |
| 38 | + |
| 39 | +export interface EmbeddingBackfillOptions { |
| 40 | + concurrency: number; |
| 41 | + batchSize: number; |
| 42 | + pollIntervalMs: number; |
| 43 | + workspaceRoot: string; |
| 44 | +} |
| 45 | + |
| 46 | +export interface EmbeddingBackfillStats { |
| 47 | + filesProcessed: number; |
| 48 | + filesErrored: number; |
| 49 | + chunksCreated: number; |
| 50 | + isRunning: boolean; |
| 51 | +} |
| 52 | + |
| 53 | +export class EmbeddingBackfiller extends EventEmitter { |
| 54 | + private storage: StorageProvider; |
| 55 | + private embeddingQueue: EmbeddingQueue; |
| 56 | + private vectorStore: LanceDBVectorStore; |
| 57 | + private options: EmbeddingBackfillOptions; |
| 58 | + private running = false; |
| 59 | + private inFlight = 0; |
| 60 | + private filesProcessed = 0; |
| 61 | + private filesErrored = 0; |
| 62 | + private chunksCreated = 0; |
| 63 | + private pollTimer: NodeJS.Timeout | null = null; |
| 64 | + |
| 65 | + constructor( |
| 66 | + storage: StorageProvider, |
| 67 | + embeddingQueue: EmbeddingQueue, |
| 68 | + vectorStore: LanceDBVectorStore, |
| 69 | + options: EmbeddingBackfillOptions |
| 70 | + ) { |
| 71 | + super(); |
| 72 | + this.storage = storage; |
| 73 | + this.embeddingQueue = embeddingQueue; |
| 74 | + this.vectorStore = vectorStore; |
| 75 | + this.options = options; |
| 76 | + } |
| 77 | + |
| 78 | + start(): void { |
| 79 | + if (this.running) { |
| 80 | + console.warn('EmbeddingBackfiller already running'); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + this.running = true; |
| 85 | + console.log( |
| 86 | + `EmbeddingBackfiller started (concurrency: ${this.options.concurrency}, ` + |
| 87 | + `batch: ${this.options.batchSize}, poll: ${this.options.pollIntervalMs}ms)` |
| 88 | + ); |
| 89 | + this.schedulePoll(); |
| 90 | + } |
| 91 | + |
| 92 | + async stop(): Promise<void> { |
| 93 | + if (!this.running) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + this.running = false; |
| 98 | + |
| 99 | + if (this.pollTimer) { |
| 100 | + clearTimeout(this.pollTimer); |
| 101 | + this.pollTimer = null; |
| 102 | + } |
| 103 | + |
| 104 | + // Wait for in-flight operations to complete |
| 105 | + while (this.inFlight > 0) { |
| 106 | + await this.sleep(100); |
| 107 | + } |
| 108 | + |
| 109 | + console.log('EmbeddingBackfiller stopped'); |
| 110 | + } |
| 111 | + |
| 112 | + getStats(): EmbeddingBackfillStats { |
| 113 | + return { |
| 114 | + filesProcessed: this.filesProcessed, |
| 115 | + filesErrored: this.filesErrored, |
| 116 | + chunksCreated: this.chunksCreated, |
| 117 | + isRunning: this.running, |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + private schedulePoll(): void { |
| 122 | + if (!this.running) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + this.pollTimer = setTimeout(() => { |
| 127 | + this.poll().catch((error) => { |
| 128 | + console.error('EmbeddingBackfiller poll failed:', error); |
| 129 | + }).finally(() => { |
| 130 | + this.schedulePoll(); |
| 131 | + }); |
| 132 | + }, this.options.pollIntervalMs); |
| 133 | + } |
| 134 | + |
| 135 | + private async poll(): Promise<void> { |
| 136 | + if (!this.running || this.inFlight >= this.options.concurrency) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + const availableSlots = this.options.concurrency - this.inFlight; |
| 141 | + const files = this.storage.listPendingEmbeddingFiles(availableSlots); |
| 142 | + |
| 143 | + if (files.length === 0) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + for (const file of files) { |
| 148 | + if (!this.running || this.inFlight >= this.options.concurrency) { |
| 149 | + break; |
| 150 | + } |
| 151 | + |
| 152 | + this.inFlight++; |
| 153 | + this.processFile(file.path, file.repositoryId) |
| 154 | + .catch((error) => { |
| 155 | + console.error(`EmbeddingBackfiller failed for ${file.path}:`, error); |
| 156 | + }) |
| 157 | + .finally(() => { |
| 158 | + this.inFlight--; |
| 159 | + }); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private async processFile(filePath: string, repositoryId: string): Promise<void> { |
| 164 | + try { |
| 165 | + // Read file content |
| 166 | + const absolutePath = resolve(this.options.workspaceRoot, filePath); |
| 167 | + let fileContent: string; |
| 168 | + try { |
| 169 | + fileContent = await readFile(absolutePath, 'utf-8'); |
| 170 | + } catch (error) { |
| 171 | + throw new StoreError( |
| 172 | + 'readFile', |
| 173 | + `File not readable: ${filePath}`, |
| 174 | + error as Error |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + // Scrub secrets |
| 179 | + const { scrubbed } = scrubSecrets(fileContent); |
| 180 | + |
| 181 | + // Parse file to get AST nodes for chunking |
| 182 | + let parsed; |
| 183 | + try { |
| 184 | + parsed = await parseFile(filePath, repositoryId, this.options.workspaceRoot); |
| 185 | + } catch (error) { |
| 186 | + throw new StoreError( |
| 187 | + 'parseFile', |
| 188 | + `Failed to parse ${filePath}`, |
| 189 | + error as Error |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + // Chunk code for embedding |
| 194 | + const chunks = chunkCodeForEmbedding(parsed, scrubbed); |
| 195 | + |
| 196 | + if (chunks.length === 0) { |
| 197 | + // No chunks to embed (e.g., file only has imports) |
| 198 | + this.storage.updateFileEmbeddingStatus(filePath, 'done'); |
| 199 | + this.filesProcessed++; |
| 200 | + this.emit('file:complete', filePath, 0); |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + // Embed chunks via the queue (with backpressure) |
| 205 | + const texts = chunks.map((chunk) => chunk.content); |
| 206 | + const embeddings = await this.embeddingQueue.embed(texts); |
| 207 | + |
| 208 | + // Attach embeddings to chunks |
| 209 | + const chunksWithEmbeddings = chunks.map((chunk, i) => ({ |
| 210 | + ...chunk, |
| 211 | + embedding: embeddings[i], |
| 212 | + })); |
| 213 | + |
| 214 | + // Upsert to vectorStore |
| 215 | + await this.vectorStore.upsertChunks(chunksWithEmbeddings); |
| 216 | + |
| 217 | + // Mark file as done |
| 218 | + this.storage.updateFileEmbeddingStatus(filePath, 'done'); |
| 219 | + this.filesProcessed++; |
| 220 | + this.chunksCreated += chunks.length; |
| 221 | + this.emit('file:complete', filePath, chunks.length); |
| 222 | + } catch (error) { |
| 223 | + // Mark file as error (do not retry in this implementation) |
| 224 | + this.storage.updateFileEmbeddingStatus(filePath, 'error'); |
| 225 | + this.filesErrored++; |
| 226 | + this.emit('file:error', filePath, error); |
| 227 | + console.warn(`EmbeddingBackfiller marked ${filePath} as error:`, error); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + private sleep(ms: number): Promise<void> { |
| 232 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 233 | + } |
| 234 | +} |
0 commit comments