| 336 | * only that file's speedup. |
| 337 | */ |
| 338 | export function tryKernelExtract( |
| 339 | filePath: string, |
| 340 | source: string, |
| 341 | language: Language |
| 342 | ): ExtractionResult | null { |
| 343 | if (!kernelRoutes(language)) return null; |
| 344 | const kernel = getKernel(); |
| 345 | if (!kernel) return null; |
| 346 | if (takeDeferredPreParse(filePath, source, language) !== null) return null; // already deferred |
| 347 | const timedOut = takeTimedOut(filePath, source, language); |
| 348 | if (timedOut !== null) return parseTimeoutResult(filePath, timedOut); |
| 349 | const t0 = Date.now(); |
| 350 | const pre = preParsedSource(filePath, source, language); |
| 351 | const budgetMs = indexParseBudgetMs(pre.length); |
| 352 | try { |
| 353 | const buffers = kernel.extractFile(filePath, pre, language, budgetMs); |
| 354 | const result = decodeExtractBuffers(buffers, filePath, language); |
| 355 | if (keepsReExportsAsWritten(source, pre, language)) restoreReExportSignatures(result, source); |
| 356 | POST_PASSES[language]?.(result, source); |
| 357 | // Read beside the tables, as TreeSitterExtractor does (a CommonJS `require`). |
| 358 | result.unresolvedReferences.push(...commonJsRequireRefs(filePath, source, language)); |
| 359 | result.durationMs = Date.now() - t0; |
| 360 | return result; |
| 361 | } catch (err) { |
| 362 | const message = err instanceof Error ? err.message : String(err); |
| 363 | // `defer:` is the kernel's expected-routing signal (files with parse |
| 364 | // errors take the wasm path — its error RECOVERY is the canonical one; |
| 365 | // recovery differs between UTF-8 and UTF-16 parsing). Silent by design. |
| 366 | if (message.includes('defer:')) { |
| 367 | deferSlot = { filePath, source, language, pre }; |
| 368 | return null; |
| 369 | } |
| 370 | // A parse that ran past its budget is the file's result: the wasm path |
| 371 | // runs the same tree-sitter and would spend the budget again. |
| 372 | if (message.startsWith('timeout:')) { |
| 373 | timeoutSlot = { filePath, source, language, budgetMs }; |
| 374 | return parseTimeoutResult(filePath, budgetMs); |
| 375 | } |
| 376 | if (!warned.has(language)) { |
| 377 | warned.add(language); |
| 378 | process.stderr.write( |
| 379 | `[codegraph-kernel] ${language} extraction failed (${message}) — falling back to the wasm path\n` |
| 380 | ); |
| 381 | } |
| 382 | return null; |
| 383 | } |
| 384 | } |