diff --git a/scripts/lib/agent-proximity/graph.js b/scripts/lib/agent-proximity/graph.js index 3d4c42ad6..f854e05a8 100644 --- a/scripts/lib/agent-proximity/graph.js +++ b/scripts/lib/agent-proximity/graph.js @@ -16,8 +16,27 @@ const fs = require('fs'); const path = require('path'); -const SOURCE_EXTENSIONS = ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx']; +const SOURCE_EXTENSIONS = ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.mts', '.cts']; const RESOLVE_EXTENSIONS = ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.json']; +const TYPESCRIPT_SOURCE_EXTENSIONS = { + '.js': ['.ts', '.tsx', '.d.ts'], + '.jsx': ['.tsx', '.d.ts'], + '.mjs': ['.mts', '.d.mts'], + '.cjs': ['.cts', '.d.cts'], +}; + +function resolutionCandidates(target, pathApi) { + const extension = pathApi.extname(target); + const sourceExtensions = TYPESCRIPT_SOURCE_EXTENSIONS[extension] || []; + // NodeNext source imports name the emitted JS file, which may not exist yet. + // Keep literal files first, then recover the corresponding TypeScript source. + return [ + target, + ...sourceExtensions.map(ext => target.slice(0, -extension.length) + ext), + ...RESOLVE_EXTENSIONS.map(ext => target + ext), + ...RESOLVE_EXTENSIONS.map(ext => pathApi.join(target, 'index' + ext)), + ]; +} function toRepoRel(repoRoot, absPath) { return path.relative(repoRoot, absPath).split(path.sep).join('/'); @@ -53,9 +72,7 @@ function extractRelativeSpecifiers(source) { function resolveSpecifier(repoRoot, fromFile, spec) { const baseDir = path.dirname(path.join(repoRoot, fromFile)); const target = path.resolve(baseDir, spec); - const candidates = [target]; - for (const ext of RESOLVE_EXTENSIONS) candidates.push(target + ext); - for (const ext of RESOLVE_EXTENSIONS) candidates.push(path.join(target, 'index' + ext)); + const candidates = resolutionCandidates(target, path); for (const cand of candidates) { try { if (fs.existsSync(cand) && fs.statSync(cand).isFile()) { @@ -117,9 +134,7 @@ function buildDependencyGraphFromSources(sources = {}) { const tryResolve = (fromFile, spec) => { const base = path.posix.dirname(fromFile); const target = path.posix.normalize(path.posix.join(base, spec)); - const candidates = [target]; - for (const ext of RESOLVE_EXTENSIONS) candidates.push(target + ext); - for (const ext of RESOLVE_EXTENSIONS) candidates.push(path.posix.join(target, 'index' + ext)); + const candidates = resolutionCandidates(target, path.posix); return candidates.find(c => fileSet.has(c)) || null; }; for (const file of fileList) { diff --git a/tests/lib/agent-proximity.test.js b/tests/lib/agent-proximity.test.js index 7359057c6..cb3d6cd08 100644 --- a/tests/lib/agent-proximity.test.js +++ b/tests/lib/agent-proximity.test.js @@ -5,9 +5,12 @@ const assert = require('assert'); const { spawnSync } = require('child_process'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); const { treeDistance, lineRangeOverlap, graphDistance, collisionRisk, advise, closureRate } = require('../../scripts/lib/agent-proximity/distance'); -const { buildDependencyGraphFromSources, extractRelativeSpecifiers } = require('../../scripts/lib/agent-proximity/graph'); +const { buildDependencyGraph, buildDependencyGraphFromSources, extractRelativeSpecifiers } = require('../../scripts/lib/agent-proximity/graph'); const { scanAirspace, embedAgents } = require('../../scripts/lib/agent-proximity'); let passed = 0; @@ -122,6 +125,47 @@ test('extractRelativeSpecifiers ignores bare (node_modules) specifiers', () => { const specs = extractRelativeSpecifiers("require('fs'); require('./local'); import x from 'lodash';"); assert.deepStrictEqual(specs, ['./local']); }); +test('TypeScript runtime specifiers resolve to source files in both graph builders', () => { + const sources = { + 'apps/web/page.ts': "import { value } from '../../packages/core/util.js';\nimport View from './view.jsx';", + 'apps/web/view.tsx': 'export default function View() {}', + 'packages/core/util.ts': 'export const value = 1;', + 'packages/core/esm.mts': "export { value } from './util.js';", + 'packages/core/cjs.cts': "const esm = require('./esm.mjs');", + 'apps/web/main.ts': "import esm from '../../packages/core/esm.mjs';\nimport cjs from '../../packages/core/cjs.cjs';", + }; + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-ts-graph-')); + try { + for (const [file, source] of Object.entries(sources)) { + const target = path.join(root, file); + fs.mkdirSync(path.dirname(target), { recursive: true }); + fs.writeFileSync(target, source); + } + for (const graph of [buildDependencyGraphFromSources(sources), buildDependencyGraph(root, Object.keys(sources))]) { + assert.deepStrictEqual(graph.adjacency['apps/web/page.ts'], ['packages/core/util.ts', 'apps/web/view.tsx']); + assert.deepStrictEqual(graph.adjacency['apps/web/main.ts'], ['packages/core/esm.mts', 'packages/core/cjs.cts']); + assert.deepStrictEqual(graph.adjacency['packages/core/esm.mts'], ['packages/core/util.ts']); + assert.deepStrictEqual(graph.adjacency['packages/core/cjs.cts'], ['packages/core/esm.mts']); + assert.ok(collisionRisk( + { files: [{ path: 'apps/web/page.ts' }] }, + { files: [{ path: 'packages/core/util.ts' }] }, + graph + ).channels.dependency > 0, 'source edits must retain their dependency risk'); + } + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } +}); +test('TypeScript source fallback preserves literal files and unrelated extensions', () => { + const graph = buildDependencyGraphFromSources({ + 'main.ts': "import './ready.js'; import './missing.js'; import './styles.css'; import './types.js';", + 'ready.js': 'export {};', + 'ready.ts': 'export {};', + 'styles.ts': 'export {};', + 'types.d.ts': 'export interface Options {}', + }); + assert.deepStrictEqual(graph.adjacency['main.ts'], ['ready.js', 'types.d.ts']); +}); test('graphDistance: direct edge is 1, two hops is 2, unreachable is Infinity', () => { const g = { adjacency: { 'a.js': ['b.js'], 'b.js': ['c.js'], 'c.js': [], 'z.js': [] } }; assert.strictEqual(graphDistance(g, 'a.js', 'b.js'), 1);