| 354 | * only that file's speedup. |
| 355 | */ |
| 356 | export function tryKernelExtract( |
| 357 | filePath: string, |
| 358 | source: string, |
| 359 | language: Language |
| 360 | ): ExtractionResult | null { |
| 361 | if (!kernelRoutes(language)) return null; |
| 362 | const kernel = getKernel(); |
| 363 | if (!kernel) return null; |
| 364 | if (takeDeferredPreParse(filePath, source, language) !== null) return null; // already deferred |
| 365 | const timedOut = takeTimedOut(filePath, source, language); |
| 366 | if (timedOut !== null) return parseTimeoutResult(filePath, timedOut); |
| 367 | const t0 = Date.now(); |
| 368 | const pre = preParsedSource(filePath, source, language); |
| 369 | const budgetMs = indexParseBudgetMs(pre.length); |
| 370 | try { |
| 371 | const buffers = kernel.extractFile(filePath, pre, language, budgetMs); |
| 372 | if (storesStandIn(source, pre, language, buffers.arena)) { |
| 373 | deferSlot = { filePath, source, language, pre }; |
| 374 | return null; |
| 375 | } |
| 376 | const result = decodeExtractBuffers(buffers, filePath, language); |
| 377 | if (keepsReExportsAsWritten(source, pre, language)) restoreReExportSignatures(result, source); |
| 378 | POST_PASSES[language]?.(result, source); |
| 379 | // Read beside the tables, as TreeSitterExtractor does (a CommonJS `require`). |
| 380 | pushAll(result.unresolvedReferences, commonJsRequireRefs(filePath, source, language)); |
| 381 | result.durationMs = Date.now() - t0; |
| 382 | return result; |
| 383 | } catch (err) { |
| 384 | const message = err instanceof Error ? err.message : String(err); |
| 385 | // `defer:` is the kernel's expected-routing signal (files with parse |
| 386 | // errors take the wasm path — its error RECOVERY is the canonical one; |
| 387 | // recovery differs between UTF-8 and UTF-16 parsing). Silent by design. |
| 388 | if (message.includes('defer:')) { |
| 389 | deferSlot = { filePath, source, language, pre }; |
| 390 | return null; |
| 391 | } |
| 392 | // A parse that ran past its budget is the file's result: the wasm path |
| 393 | // runs the same tree-sitter and would spend the budget again. |
| 394 | if (message.startsWith('timeout:')) { |
| 395 | timeoutSlot = { filePath, source, language, budgetMs }; |
| 396 | return parseTimeoutResult(filePath, budgetMs); |
| 397 | } |
| 398 | if (!warned.has(language)) { |
| 399 | warned.add(language); |
| 400 | process.stderr.write( |
| 401 | `[codegraph-kernel] ${language} extraction failed (${message}) — falling back to the wasm path\n` |
| 402 | ); |
| 403 | } |
| 404 | return null; |
| 405 | } |
| 406 | } |