MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / tryKernelExtract

Function tryKernelExtract

src/extraction/kernel/index.ts:339–385  ·  view source on GitHub ↗
(
  filePath: string,
  source: string,
  language: Language
)

Source from the content-addressed store, hash-verified

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

Callers 15

extractFromSourceFunction · 0.90
extractFunction · 0.90
extractFunction · 0.90
extractFunction · 0.90
extractFunction · 0.90
extractFunction · 0.90
extractFunction · 0.90
assertParityFunction · 0.90
assertParityFunction · 0.90

Calls 15

getKernelFunction · 0.90
parseTimeoutResultFunction · 0.90
indexParseBudgetMsFunction · 0.90
decodeExtractBuffersFunction · 0.90
pushAllFunction · 0.90
commonJsRequireRefsFunction · 0.90
kernelRoutesFunction · 0.85
takeDeferredPreParseFunction · 0.85
takeTimedOutFunction · 0.85
preParsedSourceFunction · 0.85
keepsReExportsAsWrittenFunction · 0.85

Tested by 15

extractFunction · 0.72
extractFunction · 0.72
extractFunction · 0.72
extractFunction · 0.72
extractFunction · 0.72
extractFunction · 0.72
assertParityFunction · 0.72
assertParityFunction · 0.72
extractFunction · 0.72
extractFunction · 0.72
extractFunction · 0.72
extractFunction · 0.72