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

Method extractNodeCode

src/context/index.ts:1422–1450  ·  view source on GitHub ↗

* Extract code from a node's source file

(node: Node)

Source from the content-addressed store, hash-verified

1420 * Extract code from a node's source file
1421 */
1422 private async extractNodeCode(node: Node): Promise<string | null> {
1423 // SECURITY (#383): a config-leaf node's on-disk line is `key = <secret>`.
1424 // Return the KEY only — never read the value off disk. This closes the
1425 // includeCode / buildContext code-block path, mirroring the explore source
1426 // renderer; an agent that genuinely needs a value can read the file itself.
1427 if (isConfigLeafNode(node)) {
1428 return node.signature || node.qualifiedName || node.name;
1429 }
1430
1431 const filePath = validatePathWithinRoot(this.projectRoot, node.filePath);
1432
1433 if (!filePath || !fs.existsSync(filePath)) {
1434 return null;
1435 }
1436
1437 try {
1438 const content = fs.readFileSync(filePath, 'utf-8');
1439 const lines = content.split('\n');
1440
1441 // Extract lines (1-indexed to 0-indexed)
1442 const startIdx = Math.max(0, node.startLine - 1);
1443 const endIdx = Math.min(lines.length, node.endLine);
1444
1445 return lines.slice(startIdx, endIdx).join('\n');
1446 } catch (error) {
1447 logDebug('Failed to extract code from node', { nodeId: node.id, filePath: node.filePath, error: String(error) });
1448 return null;
1449 }
1450 }
1451
1452 /**
1453 * Get entry points from a subgraph (the root nodes)

Callers 2

getCodeMethod · 0.95
extractCodeBlocksMethod · 0.95

Calls 4

isConfigLeafNodeFunction · 0.90
validatePathWithinRootFunction · 0.90
logDebugFunction · 0.90
joinMethod · 0.45

Tested by

no test coverage detected