Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

feat: add role classification to ingestion and CLI filtering

Joseph Mikhail77de518a76cd
diff --git a/core-ingestion/dist/index.d.ts b/core-ingestion/dist/index.d.ts
index 62c9325f..5e611570 100644
--- a/core-ingestion/dist/index.d.ts
+++ b/core-ingestion/dist/index.d.ts
@@ -1,4 +1,5 @@
import { SupportedLanguages } from './languages.js';
+import type { RoleClassification } from './role-classifier.js';
export interface ParsedEntity {
name: string;
kind: string;
@@ -18,6 +19,7 @@ export interface FileParseResult {
language: SupportedLanguages;
entities: ParsedEntity[];
relationships: ParsedRelationship[];
+ fileRole: RoleClassification;
}
/** Returns true if a grammar is installed for the given file's language. */
export declare function isGrammarSupported(filePath: string): boolean;
diff --git a/core-ingestion/dist/index.js b/core-ingestion/dist/index.js
index 7e45558c..adfb2758 100644
--- a/core-ingestion/dist/index.js
+++ b/core-ingestion/dist/index.js
@@ -27,6 +27,7 @@ import PHP from 'tree-sitter-php';
import Scala from 'tree-sitter-scala';
import { SupportedLanguages, languageFromPath } from './languages.js';
import { LANGUAGE_QUERIES } from './queries.js';
+import { classifyFileRole } from './role-classifier.js';
// ---------------------------------------------------------------------------
// Language → grammar map
// ---------------------------------------------------------------------------
@@ -327,7 +328,7 @@ export function parseFile(filePath, source) {
continue;
}
}
- return { filePath, language, entities, relationships };
+ return { filePath, language, entities, relationships, fileRole: classifyFileRole(filePath, source) };
}
catch {
return null;
diff --git a/core-ingestion/dist/patch-builder.js b/core-ingestion/dist/patch-builder.js
index 65a9ff57..5267f4bb 100644
--- a/core-ingestion/dist/patch-builder.js
+++ b/core-ingestion/dist/patch-builder.js
@@ -87,6 +87,9 @@ export function buildPatch(result, sourceHash, previousSourceHash) {
const id = nodeId(filePath, qk);
if (!seenNodeIds.has(id)) {
seenNodeIds.add(id);
+ const roleAttrs = e.kind === 'file'
+ ? { role: result.fileRole.role, role_confidence: result.fileRole.role_confidence, role_signals: result.fileRole.role_signals }
+ : { role: result.fileRole.role, role_source: 'inherited_from_file' };
ops.push({
type: 'UpsertNode',
id,
@@ -96,6 +99,7 @@ export function buildPatch(result, sourceHash, previousSourceHash) {
line_start: e.lineStart,
line_end: e.lineEnd,
language: e.language,
+ ...roleAttrs,
},
});
}
@@ -197,12 +201,15 @@ export function buildPatchWithResolution(result, sourceHash, resolvedEdges, prev
const id = nodeId(filePath, qk);
if (!seenNodeIds2.has(id)) {
seenNodeIds2.add(id);
+ const roleAttrs = e.kind === 'file'
+ ? { role: result.fileRole.role, role_confidence: result.fileRole.role_confidence, role_signals: result.fileRole.role_signals }
+ : { role: result.fileRole.role, role_source: 'inherited_from_file' };
ops.push({
type: 'UpsertNode',
id,
kind: e.kind,
name: e.name,
- attrs: { line_start: e.lineStart, line_end: e.lineEnd, language: e.language },
+ attrs: { line_start: e.lineStart, line_end: e.lineEnd, language: e.language, ...roleAttrs },
});
}
}
diff --git a/core-ingestion/dist/role-classifier.d.ts b/core-ingestion/dist/role-classifier.d.ts
new file mode 100644
index 00000000..3773ca06
--- /dev/null
+++ b/core-ingestion/dist/role-classifier.d.ts
@@ -0,0 +1,7 @@
+export type EntityRole = 'production' | 'test' | 'fixture' | 'generated' | 'external' | 'tooling';
+export interface RoleClassification {
+ role: EntityRole;
+ role_confidence: number;
+ role_signals: string[];
+}
+export declare function classifyFileRole(filePath: string, source?: string): RoleClassification;
diff --git a/core-ingestion/dist/role-classifier.js b/core-ingestion/dist/role-classifier.js
new file mode 100644
index 00000000..7310e77e
--- /dev/null
+++ b/core-ingestion/dist/role-classifier.js
@@ -0,0 +1,204 @@
+import * as nodePath from 'node:path';
+// ---------------------------------------------------------------------------
+// Signal tables
+// ---------------------------------------------------------------------------
+const TEST_PATH_PATTERNS = [
+ { pattern: /[/\\]__tests__[/\\]/i, signal: 'path:__tests__' },
+ { pattern: /[/\\]tests?[/\\]/i, signal: 'path:tests/' },
+ { pattern: /[/\\]spec[/\\]/i, signal: 'path:spec/' },
+ { pattern: /[/\\]src[/\\]test[/\\]/i, signal: 'path:src/test/' },
+ { pattern: /[/\\]test[/\\]unit[/\\]/i, signal: 'path:test/unit/' },
+ { pattern: /[/\\]test[/\\]integration[/\\]/i, signal: 'path:test/integration/' },
+];
+const TEST_FILE_PATTERNS = [
+ { pattern: /\.test\.[^.]+$/i, signal: 'filename:.test.' },
+ { pattern: /\.spec\.[^.]+$/i, signal: 'filename:.spec.' },
+ { pattern: /Test\.[^.]+$/, signal: 'filename:Test.' },
+ { pattern: /Spec\.[^.]+$/, signal: 'filename:Spec.' },
+ { pattern: /_test\.[^.]+$/i, signal: 'filename:_test.' },
+ { pattern: /_spec\.[^.]+$/i, signal: 'filename:_spec.' },
+];
+const TEST_IMPORT_PATTERNS = [
+ { pattern: /from ['"](?:jest|vitest|mocha|chai|sinon|jasmine|@testing-library)/, signal: 'import:js_test_framework' },
+ { pattern: /require\(['"](?:jest|vitest|mocha|chai|sinon|jasmine)['"]\)/, signal: 'import:js_test_framework_require' },
+ { pattern: /import\s+(?:unittest|pytest)/, signal: 'import:python_test_framework' },
+ { pattern: /from\s+(?:unittest|pytest)/, signal: 'import:python_test_framework_from' },
+ { pattern: /import\s+org\.junit/, signal: 'import:junit' },
+ { pattern: /import\s+org\.testng/, signal: 'import:testng' },
+ { pattern: /import\s+io\.mockk/, signal: 'import:mockk' },
+ { pattern: /import\s+org\.mockito/, signal: 'import:mockito' },
+ { pattern: /import\s+org\.scalatest/, signal: 'import:scalatest' },
+ { pattern: /import\s+munit/, signal: 'import:munit' },
+ { pattern: /import\s+zio\.test/, signal: 'import:zio_test' },
+ { pattern: /"testing"/, signal: 'import:go_testing' },
+ { pattern: /require\s+['"]rspec['"]/, signal: 'import:rspec' },
+ { pattern: /require\s+['"]minitest['"]/, signal: 'import:minitest' },
+];
+const FIXTURE_PATH_PATTERNS = [
+ { pattern: /[/\\]fixtures?[/\\]/i, signal: 'path:fixtures/' },
+ { pattern: /[/\\]__fixtures__[/\\]/i, signal: 'path:__fixtures__/' },
+ { pattern: /[/\\]test[/\\]resources[/\\]/i, signal: 'path:test/resources/' },
+ { pattern: /[/\\]testdata[/\\]/i, signal: 'path:testdata/' },
+];
+const FIXTURE_FILE_PATTERNS = [
+ { pattern: /fixture/i, signal: 'filename:fixture' },
+ { pattern: /\.mock\.[^.]+$/i, signal: 'filename:.mock.' },
+ { pattern: /stub/i, signal: 'filename:stub' },
+ { pattern: /fake/i, signal: 'filename:fake' },
+ { pattern: /sample/i, signal: 'filename:sample' },
+ { pattern: /seed/i, signal: 'filename:seed' },
+];
+const GENERATED_PATH_PATTERNS = [
+ { pattern: /[/\\]generated[/\\]/i, signal: 'path:generated/' },
+ { pattern: /[/\\]gen[/\\]/i, signal: 'path:gen/' },
+ { pattern: /[/\\]\.gen[/\\]/i, signal: 'path:.gen/' },
+ { pattern: /\.pb\.[^.]+$/, signal: 'filename:.pb. (protobuf)' },
+ { pattern: /\.gen\.[^.]+$/, signal: 'filename:.gen.' },
+ { pattern: /_generated\.[^.]+$/, signal: 'filename:_generated.' },
+];
+const GENERATED_SOURCE_PATTERNS = [
+ { pattern: /^\/\/ Code generated/m, signal: 'source_marker:code_generated' },
+ { pattern: /^\/\/ DO NOT EDIT/m, signal: 'source_marker:do_not_edit' },
+ { pattern: /^# Code generated/m, signal: 'source_marker:code_generated_hash' },
+ { pattern: /^# DO NOT EDIT/m, signal: 'source_marker:do_not_edit_hash' },
+ { pattern: /^\/\* Generated by/m, signal: 'source_marker:generated_by' },
+ { pattern: /^\/\* AUTO-GENERATED/im, signal: 'source_marker:auto_generated' },
+ { pattern: /^\/\/ AUTO-GENERATED/im, signal: 'source_marker:auto_generated_line' },
+];
+const EXTERNAL_PATH_PATTERNS = [
+ { pattern: /[/\\]vendor[/\\]/i, signal: 'path:vendor/' },
+ { pattern: /[/\\]third[-_]?party[/\\]/i, signal: 'path:third_party/' },
+ { pattern: /[/\\]extern[/\\]/i, signal: 'path:extern/' },
+];
+const TOOLING_PATH_PATTERNS = [
+ { pattern: /[/\\]scripts?[/\\]/i, signal: 'path:scripts/' },
+ { pattern: /[/\\]tools?[/\\]/i, signal: 'path:tools/' },
+ { pattern: /[/\\]ci[/\\]/i, signal: 'path:ci/' },
+ { pattern: /[/\\]\.github[/\\]/i, signal: 'path:.github/' },
+ { pattern: /[/\\]dev[/\\]/i, signal: 'path:dev/' },
+];
+const TOOLING_FILE_PATTERNS = [
+ { pattern: /^Makefile$/, signal: 'filename:Makefile' },
+ { pattern: /^Dockerfile[^/\\]*$/, signal: 'filename:Dockerfile' },
+ { pattern: /^docker-compose[^/\\]*\.yml$/i, signal: 'filename:docker-compose' },
+ { pattern: /\.config\.[^.]+$/i, signal: 'filename:.config.' },
+ { pattern: /^vite\.config\./i, signal: 'filename:vite.config' },
+ { pattern: /^webpack\.config\./i, signal: 'filename:webpack.config' },
+ { pattern: /^jest\.config\./i, signal: 'filename:jest.config' },
+ { pattern: /^babel\.config\./i, signal: 'filename:babel.config' },
+ { pattern: /^tsconfig[^/\\]*\.json$/i, signal: 'filename:tsconfig.json' },
+ { pattern: /^\.eslintrc/i, signal: 'filename:.eslintrc' },
+ { pattern: /^rollup\.config\./i, signal: 'filename:rollup.config' },
+ { pattern: /^build\.[^.]+$/i, signal: 'filename:build.' },
+ { pattern: /migrate\.[^.]+$/i, signal: 'filename:migrate.' },
+];
+// ---------------------------------------------------------------------------
+// Main classifier
+// ---------------------------------------------------------------------------
+export function classifyFileRole(filePath, source) {
+ const normalizedPath = filePath.replace(/\\/g, '/');
+ const fileName = nodePath.basename(filePath);
+ const signals = [];
+ // --- Test ---
+ let testScore = 0;
+ for (const { pattern, signal } of TEST_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) {
+ testScore += 0.8;
+ signals.push(signal);
+ break;
+ }
+ }
+ for (const { pattern, signal } of TEST_FILE_PATTERNS) {
+ if (pattern.test(fileName)) {
+ testScore += 0.9;
+ signals.push(signal);
+ break;
+ }
+ }
+ if (source) {
+ for (const { pattern, signal } of TEST_IMPORT_PATTERNS) {
+ if (pattern.test(source)) {
+ testScore += 0.5;
+ signals.push(signal);
+ break;
+ }
+ }
+ }
+ // --- Fixture ---
+ let fixtureScore = 0;
+ for (const { pattern, signal } of FIXTURE_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) {
+ fixtureScore += 0.8;
+ signals.push(signal);
+ break;
+ }
+ }
+ for (const { pattern, signal } of FIXTURE_FILE_PATTERNS) {
+ if (pattern.test(fileName)) {
+ fixtureScore += 0.6;
+ signals.push(signal);
+ break;
+ }
+ }
+ // --- Generated ---
+ let generatedScore = 0;
+ for (const { pattern, signal } of GENERATED_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) {
+ generatedScore += 0.7;
+ signals.push(signal);
+ break;
+ }
+ }
+ if (source) {
+ for (const { pattern, signal } of GENERATED_SOURCE_PATTERNS) {
+ if (pattern.test(source)) {
+ generatedScore += 0.9;
+ signals.push(signal);
+ break;
+ }
+ }
+ }
+ // --- External ---
+ let externalScore = 0;
+ for (const { pattern, signal } of EXTERNAL_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) {
+ externalScore += 0.9;
+ signals.push(signal);
+ break;
+ }
+ }
+ // --- Tooling ---
+ let toolingScore = 0;
+ for (const { pattern, signal } of TOOLING_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) {
+ toolingScore += 0.6;
+ signals.push(signal);
+ break;
+ }
+ }
+ for (const { pattern, signal } of TOOLING_FILE_PATTERNS) {
+ if (pattern.test(fileName)) {
+ toolingScore += 0.7;
+ signals.push(signal);
+ break;
+ }
+ }
+ // --- Pick winner ---
+ const scores = [
+ ['test', testScore],
+ ['fixture', fixtureScore],
+ ['generated', generatedScore],
+ ['external', externalScore],
+ ['tooling', toolingScore],
+ ];
+ scores.sort((a, b) => b[1] - a[1]);
+ const [topRole, topScore] = scores[0];
+ if (topScore >= 0.5) {
+ return {
+ role: topRole,
+ role_confidence: parseFloat(Math.min(topScore, 1.0).toFixed(2)),
+ role_signals: signals,
+ };
+ }
+ return { role: 'production', role_confidence: 0.5, role_signals: [] };
+}
diff --git a/core-ingestion/src/__tests__/__snapshots__/parseFile.test.ts.snap b/core-ingestion/src/__tests__/__snapshots__/parseFile.test.ts.snap
index 63c6ea01..ed6ed9e8 100644
--- a/core-ingestion/src/__tests__/__snapshots__/parseFile.test.ts.snap
+++ b/core-ingestion/src/__tests__/__snapshots__/parseFile.test.ts.snap
@@ -225,6 +225,11 @@ exports[`parseFile snapshots > snapshots Identifiers.scala 1`] = `
},
],
"filePath": "memory-layer/src/main/scala/ix/memory/model/Identifiers.scala",
+ "fileRole": {
+ "role": "production",
+ "role_confidence": 0.5,
+ "role_signals": [],
+ },
"language": "scala",
"relationships": [
{
@@ -803,6 +808,11 @@ exports[`parseFile snapshots > snapshots Node.scala 1`] = `
},
],
"filePath": "memory-layer/src/main/scala/ix/memory/model/Node.scala",
+ "fileRole": {
+ "role": "production",
+ "role_confidence": 0.5,
+ "role_signals": [],
+ },
"language": "scala",
"relationships": [
{
@@ -1200,6 +1210,11 @@ exports[`parseFile snapshots > snapshots doctor.ts 1`] = `
},
],
"filePath": "ix-cli/src/cli/commands/doctor.ts",
+ "fileRole": {
+ "role": "production",
+ "role_confidence": 0.5,
+ "role_signals": [],
+ },
"language": "typescript",
"relationships": [
{
diff --git a/core-ingestion/src/__tests__/resolveEdges.property.test.ts b/core-ingestion/src/__tests__/resolveEdges.property.test.ts
index 84af7bd1..ac34f372 100644
--- a/core-ingestion/src/__tests__/resolveEdges.property.test.ts
+++ b/core-ingestion/src/__tests__/resolveEdges.property.test.ts
@@ -4,6 +4,8 @@ import { describe, expect, it } from 'vitest';
import { resolveEdges, type FileParseResult, type ParsedEntity } from '../index.js';
import { SupportedLanguages } from '../languages.js';
+const defaultFileRole = { role: 'production' as const, role_confidence: 0.5, role_signals: [] };
+
const languages = [SupportedLanguages.TypeScript, SupportedLanguages.Scala] as const;
const symbolNames = ['run', 'helperFn', 'NodeKind', 'execute', 'transform'] as const;
@@ -39,6 +41,7 @@ describe('resolveEdges property invariants', () => {
relationships: def.includeCall
? [{ srcName: `caller${index}`, dstName: def.symbol, predicate: 'CALLS' }]
: [],
+ fileRole: defaultFileRole,
}));
const resolved = resolveEdges(results);
@@ -67,6 +70,7 @@ describe('resolveEdges property invariants', () => {
makeEntity('caller', SupportedLanguages.TypeScript),
],
relationships: [{ srcName: 'caller', dstName: 'missing', predicate: 'CALLS' }],
+ fileRole: defaultFileRole,
},
{
filePath: '/repo/other.scala',
@@ -76,6 +80,7 @@ describe('resolveEdges property invariants', () => {
makeEntity('missing', SupportedLanguages.Scala),
],
relationships: [],
+ fileRole: defaultFileRole,
},
]);
@@ -90,6 +95,7 @@ describe('resolveEdges property invariants', () => {
makeEntity('caller', SupportedLanguages.TypeScript),
],
relationships: [{ srcName: 'caller', dstName: 'run', predicate: 'CALLS' }],
+ fileRole: defaultFileRole,
},
{
filePath: '/repo/a.ts',
@@ -99,6 +105,7 @@ describe('resolveEdges property invariants', () => {
makeEntity('run', SupportedLanguages.TypeScript),
],
relationships: [],
+ fileRole: defaultFileRole,
},
{
filePath: '/repo/b.ts',
@@ -108,6 +115,7 @@ describe('resolveEdges property invariants', () => {
makeEntity('run', SupportedLanguages.TypeScript),
],
relationships: [],
+ fileRole: defaultFileRole,
},
]);
diff --git a/core-ingestion/src/__tests__/resolveEdges.test.ts b/core-ingestion/src/__tests__/resolveEdges.test.ts
index 4c949d1d..c6ee50a7 100644
--- a/core-ingestion/src/__tests__/resolveEdges.test.ts
+++ b/core-ingestion/src/__tests__/resolveEdges.test.ts
@@ -19,6 +19,8 @@ function entity(
};
}
+const defaultFileRole = { role: 'production' as const, role_confidence: 0.5, role_signals: [] };
+
function fileResult(
filePath: string,
language: SupportedLanguages,
@@ -33,6 +35,7 @@ function fileResult(
...entities,
],
relationships,
+ fileRole: defaultFileRole,
};
}
diff --git a/core-ingestion/src/__tests__/roleClassifier.test.ts b/core-ingestion/src/__tests__/roleClassifier.test.ts
new file mode 100644
index 00000000..10f613b8
--- /dev/null
+++ b/core-ingestion/src/__tests__/roleClassifier.test.ts
@@ -0,0 +1,89 @@
+import { describe, expect, it } from 'vitest';
+
+import { classifyFileRole } from '../role-classifier.js';
+
+describe('classifyFileRole', () => {
+ it('production fallback — normal source file', () => {
+ const result = classifyFileRole('src/auth.ts');
+ expect(result.role).toBe('production');
+ expect(result.role_confidence).toBe(0.5);
+ expect(result.role_signals).toHaveLength(0);
+ });
+
+ it('test by path — __tests__ directory', () => {
+ const result = classifyFileRole('src/__tests__/auth.test.ts');
+ expect(result.role).toBe('test');
+ });
+
+ it('test by filename — .spec. pattern', () => {
+ const result = classifyFileRole('AuthService.spec.ts');
+ expect(result.role).toBe('test');
+ });
+
+ it('test by import — vitest import in source', () => {
+ const source = `import { describe, it, expect } from 'vitest';\n\ndescribe('foo', () => {});`;
+ const result = classifyFileRole('src/someFile.ts', source);
+ expect(result.role).toBe('test');
+ });
+
+ it('fixture by path', () => {
+ const result = classifyFileRole('test/fixtures/user.json');
+ expect(result.role).toBe('fixture');
+ });
+
+ it('fixture by filename — mock pattern', () => {
+ const result = classifyFileRole('src/auth.mock.ts');
+ expect(result.role).toBe('fixture');
+ });
+
+ it('generated by source marker — // Code generated', () => {
+ const source = `// Code generated by protoc. DO NOT EDIT.\n\nexport const foo = 1;`;
+ const result = classifyFileRole('src/schema.ts', source);
+ expect(result.role).toBe('generated');
+ });
+
+ it('generated by path — /generated/ directory', () => {
+ const result = classifyFileRole('src/generated/schema.ts');
+ expect(result.role).toBe('generated');
+ });
+
+ it('external by path — vendor directory', () => {
+ const result = classifyFileRole('src/vendor/somelib/index.ts');
+ expect(result.role).toBe('external');
+ });
+
+ it('tooling by filename — jest.config.ts', () => {
+ const result = classifyFileRole('jest.config.ts');
+ expect(result.role).toBe('tooling');
+ });
+
+ it('confidence >= 0.8 for test files', () => {
+ const byPath = classifyFileRole('src/__tests__/foo.test.ts');
+ expect(byPath.role_confidence).toBeGreaterThanOrEqual(0.8);
+
+ const byFilename = classifyFileRole('Foo.spec.ts');
+ expect(byFilename.role_confidence).toBeGreaterThanOrEqual(0.8);
+ });
+
+ it('signals array is non-empty for non-production classifications', () => {
+ const test = classifyFileRole('src/__tests__/foo.test.ts');
+ expect(test.role).not.toBe('production');
+ expect(test.role_signals.length).toBeGreaterThan(0);
+
+ const fixture = classifyFileRole('test/fixtures/data.json');
+ expect(fixture.role).not.toBe('production');
+ expect(fixture.role_signals.length).toBeGreaterThan(0);
+
+ const generated = classifyFileRole('src/generated/types.ts');
+ expect(generated.role).not.toBe('production');
+ expect(generated.role_signals.length).toBeGreaterThan(0);
+
+ const external = classifyFileRole('src/vendor/lib/index.ts');
+ expect(external.role).not.toBe('production');
+ expect(external.role_signals.length).toBeGreaterThan(0);
+
+ const tooling = classifyFileRole('jest.config.ts');
+ expect(tooling.role).not.toBe('production');
+ expect(tooling.role_signals.length).toBeGreaterThan(0);
+ });
+});
diff --git a/core-ingestion/src/index.ts b/core-ingestion/src/index.ts
index 11299867..506c90ce 100644
--- a/core-ingestion/src/index.ts
+++ b/core-ingestion/src/index.ts
@@ -28,6 +28,8 @@ import Scala from 'tree-sitter-scala';
import { SupportedLanguages, languageFromPath } from './languages.js';
import { LANGUAGE_QUERIES } from './queries.js';
+import { classifyFileRole } from './role-classifier.js';
+import type { RoleClassification } from './role-classifier.js';
// ---------------------------------------------------------------------------
// Public types
@@ -54,6 +56,7 @@ export interface FileParseResult {
language: SupportedLanguages;
entities: ParsedEntity[];
relationships: ParsedRelationship[];
+ fileRole: RoleClassification;
}
// ---------------------------------------------------------------------------
@@ -386,7 +389,7 @@ export function parseFile(filePath: string, source: string): FileParseResult | n
}
}
- return { filePath, language, entities, relationships };
+ return { filePath, language, entities, relationships, fileRole: classifyFileRole(filePath, source) };
} catch {
return null;
}
diff --git a/core-ingestion/src/patch-builder.ts b/core-ingestion/src/patch-builder.ts
index f6d1e421..a31e225a 100644
--- a/core-ingestion/src/patch-builder.ts
+++ b/core-ingestion/src/patch-builder.ts
@@ -105,6 +105,9 @@ export function buildPatch(
const id = nodeId(filePath, qk);
if (!seenNodeIds.has(id)) {
seenNodeIds.add(id);
+ const roleAttrs = e.kind === 'file'
+ ? { role: result.fileRole.role, role_confidence: result.fileRole.role_confidence, role_signals: result.fileRole.role_signals }
+ : { role: result.fileRole.role, role_source: 'inherited_from_file' };
ops.push({
type: 'UpsertNode',
id,
@@ -114,6 +117,7 @@ export function buildPatch(
line_start: e.lineStart,
line_end: e.lineEnd,
language: e.language,
+ ...roleAttrs,
},
});
}
@@ -230,12 +234,15 @@ export function buildPatchWithResolution(
const id = nodeId(filePath, qk);
if (!seenNodeIds2.has(id)) {
seenNodeIds2.add(id);
+ const roleAttrs = e.kind === 'file'
+ ? { role: result.fileRole.role, role_confidence: result.fileRole.role_confidence, role_signals: result.fileRole.role_signals }
+ : { role: result.fileRole.role, role_source: 'inherited_from_file' };
ops.push({
type: 'UpsertNode',
id,
kind: e.kind,
name: e.name,
- attrs: { line_start: e.lineStart, line_end: e.lineEnd, language: e.language },
+ attrs: { line_start: e.lineStart, line_end: e.lineEnd, language: e.language, ...roleAttrs },
});
}
}
diff --git a/core-ingestion/src/role-classifier.ts b/core-ingestion/src/role-classifier.ts
new file mode 100644
index 00000000..01bdb093
--- /dev/null
+++ b/core-ingestion/src/role-classifier.ts
@@ -0,0 +1,193 @@
+import * as nodePath from 'node:path';
+
+export type EntityRole = 'production' | 'test' | 'fixture' | 'generated' | 'external' | 'tooling';
+
+export interface RoleClassification {
+ role: EntityRole;
+ role_confidence: number; // 0–1
+ role_signals: string[]; // human-readable signal names that fired
+}
+
+// ---------------------------------------------------------------------------
+// Signal tables
+// ---------------------------------------------------------------------------
+
+const TEST_PATH_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /[/\\]__tests__[/\\]/i, signal: 'path:__tests__' },
+ { pattern: /[/\\]tests?[/\\]/i, signal: 'path:tests/' },
+ { pattern: /[/\\]spec[/\\]/i, signal: 'path:spec/' },
+ { pattern: /[/\\]src[/\\]test[/\\]/i, signal: 'path:src/test/' },
+ { pattern: /[/\\]test[/\\]unit[/\\]/i, signal: 'path:test/unit/' },
+ { pattern: /[/\\]test[/\\]integration[/\\]/i,signal: 'path:test/integration/' },
+];
+
+const TEST_FILE_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /\.test\.[^.]+$/i, signal: 'filename:.test.' },
+ { pattern: /\.spec\.[^.]+$/i, signal: 'filename:.spec.' },
+ { pattern: /Test\.[^.]+$/, signal: 'filename:Test.' },
+ { pattern: /Spec\.[^.]+$/, signal: 'filename:Spec.' },
+ { pattern: /_test\.[^.]+$/i, signal: 'filename:_test.' },
+ { pattern: /_spec\.[^.]+$/i, signal: 'filename:_spec.' },
+];
+
+const TEST_IMPORT_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /from ['"](?:jest|vitest|mocha|chai|sinon|jasmine|@testing-library)/, signal: 'import:js_test_framework' },
+ { pattern: /require\(['"](?:jest|vitest|mocha|chai|sinon|jasmine)['"]\)/, signal: 'import:js_test_framework_require' },
+ { pattern: /import\s+(?:unittest|pytest)/, signal: 'import:python_test_framework' },
+ { pattern: /from\s+(?:unittest|pytest)/, signal: 'import:python_test_framework_from' },
+ { pattern: /import\s+org\.junit/, signal: 'import:junit' },
+ { pattern: /import\s+org\.testng/, signal: 'import:testng' },
+ { pattern: /import\s+io\.mockk/, signal: 'import:mockk' },
+ { pattern: /import\s+org\.mockito/, signal: 'import:mockito' },
+ { pattern: /import\s+org\.scalatest/, signal: 'import:scalatest' },
+ { pattern: /import\s+munit/, signal: 'import:munit' },
+ { pattern: /import\s+zio\.test/, signal: 'import:zio_test' },
+ { pattern: /"testing"/, signal: 'import:go_testing' },
+ { pattern: /require\s+['"]rspec['"]/, signal: 'import:rspec' },
+ { pattern: /require\s+['"]minitest['"]/, signal: 'import:minitest' },
+];
+
+const FIXTURE_PATH_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /[/\\]fixtures?[/\\]/i, signal: 'path:fixtures/' },
+ { pattern: /[/\\]__fixtures__[/\\]/i, signal: 'path:__fixtures__/' },
+ { pattern: /[/\\]test[/\\]resources[/\\]/i,signal: 'path:test/resources/' },
+ { pattern: /[/\\]testdata[/\\]/i, signal: 'path:testdata/' },
+];
+
+const FIXTURE_FILE_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /fixture/i, signal: 'filename:fixture' },
+ { pattern: /\.mock\.[^.]+$/i, signal: 'filename:.mock.' },
+ { pattern: /stub/i, signal: 'filename:stub' },
+ { pattern: /fake/i, signal: 'filename:fake' },
+ { pattern: /sample/i, signal: 'filename:sample' },
+ { pattern: /seed/i, signal: 'filename:seed' },
+];
+
+const GENERATED_PATH_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /[/\\]generated[/\\]/i, signal: 'path:generated/' },
+ { pattern: /[/\\]gen[/\\]/i, signal: 'path:gen/' },
+ { pattern: /[/\\]\.gen[/\\]/i, signal: 'path:.gen/' },
+ { pattern: /\.pb\.[^.]+$/, signal: 'filename:.pb. (protobuf)' },
+ { pattern: /\.gen\.[^.]+$/, signal: 'filename:.gen.' },
+ { pattern: /_generated\.[^.]+$/, signal: 'filename:_generated.' },
+];
+
+const GENERATED_SOURCE_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /^\/\/ Code generated/m, signal: 'source_marker:code_generated' },
+ { pattern: /^\/\/ DO NOT EDIT/m, signal: 'source_marker:do_not_edit' },
+ { pattern: /^# Code generated/m, signal: 'source_marker:code_generated_hash' },
+ { pattern: /^# DO NOT EDIT/m, signal: 'source_marker:do_not_edit_hash' },
+ { pattern: /^\/\* Generated by/m, signal: 'source_marker:generated_by' },
+ { pattern: /^\/\* AUTO-GENERATED/im, signal: 'source_marker:auto_generated' },
+ { pattern: /^\/\/ AUTO-GENERATED/im, signal: 'source_marker:auto_generated_line' },
+];
+
+const EXTERNAL_PATH_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /[/\\]vendor[/\\]/i, signal: 'path:vendor/' },
+ { pattern: /[/\\]third[-_]?party[/\\]/i, signal: 'path:third_party/' },
+ { pattern: /[/\\]extern[/\\]/i, signal: 'path:extern/' },
+];
+
+const TOOLING_PATH_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /[/\\]scripts?[/\\]/i, signal: 'path:scripts/' },
+ { pattern: /[/\\]tools?[/\\]/i, signal: 'path:tools/' },
+ { pattern: /[/\\]ci[/\\]/i, signal: 'path:ci/' },
+ { pattern: /[/\\]\.github[/\\]/i, signal: 'path:.github/' },
+ { pattern: /[/\\]dev[/\\]/i, signal: 'path:dev/' },
+];
+
+const TOOLING_FILE_PATTERNS: Array<{ pattern: RegExp; signal: string }> = [
+ { pattern: /^Makefile$/, signal: 'filename:Makefile' },
+ { pattern: /^Dockerfile[^/\\]*$/, signal: 'filename:Dockerfile' },
+ { pattern: /^docker-compose[^/\\]*\.yml$/i, signal: 'filename:docker-compose' },
+ { pattern: /\.config\.[^.]+$/i, signal: 'filename:.config.' },
+ { pattern: /^vite\.config\./i, signal: 'filename:vite.config' },
+ { pattern: /^webpack\.config\./i, signal: 'filename:webpack.config' },
+ { pattern: /^jest\.config\./i, signal: 'filename:jest.config' },
+ { pattern: /^babel\.config\./i, signal: 'filename:babel.config' },
+ { pattern: /^tsconfig[^/\\]*\.json$/i, signal: 'filename:tsconfig.json' },
+ { pattern: /^\.eslintrc/i, signal: 'filename:.eslintrc' },
+ { pattern: /^rollup\.config\./i, signal: 'filename:rollup.config' },
+ { pattern: /^build\.[^.]+$/i, signal: 'filename:build.' },
+ { pattern: /migrate\.[^.]+$/i, signal: 'filename:migrate.' },
+];
+
+// ---------------------------------------------------------------------------
+// Main classifier
+// ---------------------------------------------------------------------------
+
+export function classifyFileRole(filePath: string, source?: string): RoleClassification {
+ const normalizedPath = filePath.replace(/\\/g, '/');
+ const fileName = nodePath.basename(filePath);
+ const signals: string[] = [];
+
+ // --- Test ---
+ let testScore = 0;
+ for (const { pattern, signal } of TEST_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) { testScore += 0.8; signals.push(signal); break; }
+ }
+ for (const { pattern, signal } of TEST_FILE_PATTERNS) {
+ if (pattern.test(fileName)) { testScore += 0.9; signals.push(signal); break; }
+ }
+ if (source) {
+ for (const { pattern, signal } of TEST_IMPORT_PATTERNS) {
+ if (pattern.test(source)) { testScore += 0.5; signals.push(signal); break; }
+ }
+ }
+
+ // --- Fixture ---
+ let fixtureScore = 0;
+ for (const { pattern, signal } of FIXTURE_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) { fixtureScore += 0.8; signals.push(signal); break; }
+ }
+ for (const { pattern, signal } of FIXTURE_FILE_PATTERNS) {
+ if (pattern.test(fileName)) { fixtureScore += 0.6; signals.push(signal); break; }
+ }
+
+ // --- Generated ---
+ let generatedScore = 0;
+ for (const { pattern, signal } of GENERATED_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) { generatedScore += 0.7; signals.push(signal); break; }
+ }
+ if (source) {
+ for (const { pattern, signal } of GENERATED_SOURCE_PATTERNS) {
+ if (pattern.test(source)) { generatedScore += 0.9; signals.push(signal); break; }
+ }
+ }
+
+ // --- External ---
+ let externalScore = 0;
+ for (const { pattern, signal } of EXTERNAL_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) { externalScore += 0.9; signals.push(signal); break; }
+ }
+
+ // --- Tooling ---
+ let toolingScore = 0;
+ for (const { pattern, signal } of TOOLING_PATH_PATTERNS) {
+ if (pattern.test(normalizedPath)) { toolingScore += 0.6; signals.push(signal); break; }
+ }
+ for (const { pattern, signal } of TOOLING_FILE_PATTERNS) {
+ if (pattern.test(fileName)) { toolingScore += 0.7; signals.push(signal); break; }
+ }
+
+ // --- Pick winner ---
+ const scores: Array<[EntityRole, number]> = [
+ ['test', testScore],
+ ['fixture', fixtureScore],
+ ['generated', generatedScore],
+ ['external', externalScore],
+ ['tooling', toolingScore],
+ ];
+ scores.sort((a, b) => b[1] - a[1]);
+ const [topRole, topScore] = scores[0];
+
+ if (topScore >= 0.5) {
+ return {
+ role: topRole,
+ role_confidence: parseFloat(Math.min(topScore, 1.0).toFixed(2)),
+ role_signals: signals,
+ };
+ }
+
+ return { role: 'production', role_confidence: 0.5, role_signals: [] };
+}
diff --git a/ix-cli/src/cli/commands/depends.ts b/ix-cli/src/cli/commands/depends.ts
index 5e9ae1c7..0e298cd0 100644
--- a/ix-cli/src/cli/commands/depends.ts
+++ b/ix-cli/src/cli/commands/depends.ts
@@ -3,6 +3,7 @@ import chalk from "chalk";
import { IxClient } from "../../client/api.js";
import { getEndpoint } from "../config.js";
import { resolveFileOrEntity, printResolved, isRawId } from "../resolve.js";
+import { roleHint } from "../role-filter.js";
import { stderr } from "../stderr.js";
// ── Tree types ──────────────────────────────────────────────────────
@@ -148,13 +149,15 @@ export function registerDependsCommand(program: Command): void {
.option("--depth <n>", "Limit traversal depth (default: full tree)")
.option("--all", "Remove the 200-node cap and show the complete tree")
.option("--format <fmt>", "Output format (text|json)", "text")
+ .option("--include-tests", "Include test and fixture entities in results")
+ .option("--tests-only", "Show only test and fixture entities")
.addHelpText("after", `\nExamples:
ix depends verify_token
ix depends pickBest --format json
ix depends AuthProvider --depth 2
ix depends parser.py --kind file
ix depends NodeKind --pick 1 --all`)
- .action(async (symbol: string, opts: { kind?: string; path?: string; pick?: string; depth?: string; all?: boolean; format: string }) => {
+ .action(async (symbol: string, opts: { kind?: string; path?: string; pick?: string; depth?: string; all?: boolean; format: string; includeTests?: boolean; testsOnly?: boolean }) => {
const client = new IxClient(getEndpoint());
// Validate --pick
@@ -166,7 +169,13 @@ export function registerDependsCommand(program: Command): void {
}
}
- const resolveOpts = { kind: opts.kind, path: opts.path, pick: opts.pick ? parseInt(opts.pick, 10) : undefined };
+ const resolveOpts = {
+ kind: opts.kind,
+ path: opts.path,
+ pick: opts.pick ? parseInt(opts.pick, 10) : undefined,
+ includeTests: opts.includeTests,
+ testsOnly: opts.testsOnly,
+ };
const target = await resolveFileOrEntity(client, symbol, resolveOpts);
if (!target) return;
@@ -226,5 +235,9 @@ export function registerDependsCommand(program: Command): void {
}
console.log(chalk.dim(`\n ${nodesVisited} downstream dependents, depth ${maxDepthReached}`));
+
+ if (!opts.includeTests) {
+ console.log(chalk.dim('\nUse --include-tests to include test entities in tree.'));
+ }
});
}
diff --git a/ix-cli/src/cli/commands/search.ts b/ix-cli/src/cli/commands/search.ts
index d407b91b..d3b28777 100644
--- a/ix-cli/src/cli/commands/search.ts
+++ b/ix-cli/src/cli/commands/search.ts
@@ -1,8 +1,11 @@
import type { Command } from "commander";
+import chalk from "chalk";
import { IxClient } from "../../client/api.js";
import { getEndpoint } from "../config.js";
import { formatNodes } from "../format.js";
import { scoreCandidate } from "../resolve.js";
+import { applyRoleFilter, roleHint } from "../role-filter.js";
+import { stderr } from "../stderr.js";
/** Structural kinds that should rank higher than incidental matches. */
const STRUCTURAL_KINDS = new Set([
@@ -87,6 +90,8 @@ export function registerSearchCommand(program: Command): void {
.option("--path <path>", "Boost results from files matching this path substring")
.option("--as-of <rev>", "Search as of a specific revision")
.option("--format <fmt>", "Output format (text|json)", "text")
+ .option("--include-tests", "Include test and fixture entities in results")
+ .option("--tests-only", "Show only test and fixture entities")
.addHelpText("after", `\nRanking priority:
1. Exact name + exact kind match
2. Exact name + structural kind (class, function, etc.)
@@ -103,7 +108,7 @@ Examples:
ix search expand --path memory-layer
ix search "" --kind file --limit 50 --format json`)
.action(async (term: string, opts: {
- limit: string; kind?: string; language?: string; path?: string; asOf?: string; format: string
+ limit: string; kind?: string; language?: string; path?: string; asOf?: string; format: string; includeTests?: boolean; testsOnly?: boolean
}) => {
const client = new IxClient(getEndpoint());
const limit = parseInt(opts.limit, 10);
@@ -125,7 +130,13 @@ Examples:
scored.sort(searchSort);
- const trimmed = scored.slice(0, limit);
+ const { filtered: roleFiltered, hiddenTestCount } = applyRoleFilter(
+ scored.map(s => s.node),
+ { includeTests: opts.includeTests, testsOnly: opts.testsOnly },
+ );
+ // Re-wrap with scores for trimming
+ const roleFilteredScored = scored.filter(s => roleFiltered.includes(s.node));
+ const trimmed = roleFilteredScored.slice(0, limit);
const ranked = trimmed.map(s => s.node);
if (opts.format === "json") {
@@ -136,6 +147,12 @@ Examples:
message: "Results may be broad. Use --kind to filter and boost structural matches.",
});
}
+ if (hiddenTestCount > 0) {
+ diagnostics.push({
+ code: "test_candidates_hidden",
+ message: roleHint(hiddenTestCount)!,
+ });
+ }
console.log(JSON.stringify({
results: trimmed.map((s, i) => ({
id: s.node.id,
@@ -155,6 +172,8 @@ Examples:
}, null, 2));
} else {
formatNodes(ranked, opts.format);
+ const hint = roleHint(hiddenTestCount);
+ if (hint) stderr(chalk.dim(hint));
}
});
}
diff --git a/ix-cli/src/cli/resolve.ts b/ix-cli/src/cli/resolve.ts
index e818953f..f70c711d 100644
--- a/ix-cli/src/cli/resolve.ts
+++ b/ix-cli/src/cli/resolve.ts
@@ -2,6 +2,7 @@ import * as path from "node:path";
import chalk from "chalk";
import type { IxClient } from "../client/api.js";
import { stderr } from "./stderr.js";
+import { applyRoleFilter } from "./role-filter.js";
export type ResolutionMode = "exact" | "preferred-kind" | "scored" | "ambiguous" | "heuristic";
@@ -20,9 +21,9 @@ export interface AmbiguousResult {
}
export type ResolveResult =
- | { resolved: true; entity: ResolvedEntity }
- | { resolved: false; ambiguous: true; result: AmbiguousResult }
- | { resolved: false; ambiguous: false };
+ | { resolved: true; entity: ResolvedEntity; hiddenTestCount?: number }
+ | { resolved: false; ambiguous: true; result: AmbiguousResult; hiddenTestCount?: number }
+ | { resolved: false; ambiguous: false; hiddenTestCount?: number };
// ── Structural kind sets ──────────────────────────────────────────────────
@@ -100,7 +101,7 @@ export async function resolveEntity(
client: IxClient,
symbol: string,
preferredKinds: string[],
- opts?: { kind?: string; path?: string; pick?: number }
+ opts?: { kind?: string; path?: string; pick?: number; includeTests?: boolean; testsOnly?: boolean }
): Promise<ResolvedEntity | null> {
const result = await resolveEntityFull(client, symbol, preferredKinds, opts);
if (result.resolved) return result.entity;
@@ -121,7 +122,7 @@ export async function resolveEntityFull(
client: IxClient,
symbol: string,
preferredKinds: string[],
- opts?: { kind?: string; path?: string; pick?: number }
+ opts?: { kind?: string; path?: string; pick?: number; includeTests?: boolean; testsOnly?: boolean }
): Promise<ResolveResult> {
const kindFilter = opts?.kind;
const nodes = await client.search(symbol, { limit: 20, kind: kindFilter, nameOnly: true });
@@ -131,9 +132,12 @@ export async function resolveEntityFull(
return { resolved: false, ambiguous: false };
}
+ // Apply role filter before scoring
+ const { filtered: filteredNodes, hiddenTestCount } = applyRoleFilter(nodes, opts ?? {});
+
// ── Phase 1: Exact-name candidates ──────────────────────────────────
const symbolLower = symbol.toLowerCase();
- const exactName = nodes.filter((n: any) => {
+ const exactName = filteredNodes.filter((n: any) => {
const name = (n.name || n.attrs?.name || "").toLowerCase();
return name === symbolLower;
});
@@ -143,24 +147,22 @@ export async function resolveEntityFull(
const winner = pickBest(exactName, symbol, preferredKinds, opts);
if (winner) {
const picked = applyPick(winner, opts);
- if (picked) return picked;
- if (!winner.resolved) return winner; // ambiguous but no --pick
- return winner;
+ if (picked) return { ...picked, hiddenTestCount } as ResolveResult;
+ return { ...winner, hiddenTestCount } as ResolveResult;
}
}
// ── Phase 2: Fall back to all candidates ────────────────────────────
- const winner = pickBest(nodes, symbol, preferredKinds, opts);
+ const winner = pickBest(filteredNodes, symbol, preferredKinds, opts);
if (winner) {
const picked = applyPick(winner, opts);
- if (picked) return picked;
- if (!winner.resolved) return winner;
- return winner;
+ if (picked) return { ...picked, hiddenTestCount } as ResolveResult;
+ return { ...winner, hiddenTestCount } as ResolveResult;
}
// Nothing resolved at all
stderr(`No entity found matching "${symbol}".`);
- return { resolved: false, ambiguous: false };
+ return { resolved: false, ambiguous: false, hiddenTestCount };
}
/**
@@ -397,7 +399,7 @@ export function looksFileLike(target: string): boolean {
export async function resolveFileOrEntity(
client: IxClient,
target: string,
- opts?: { kind?: string; path?: string; pick?: number }
+ opts?: { kind?: string; path?: string; pick?: number; includeTests?: boolean; testsOnly?: boolean }
): Promise<ResolvedEntity | null> {
// 1. Raw UUID
if (isRawId(target)) {
diff --git a/ix-cli/src/cli/role-filter.ts b/ix-cli/src/cli/role-filter.ts
new file mode 100644
index 00000000..371cb26b
--- /dev/null
+++ b/ix-cli/src/cli/role-filter.ts
@@ -0,0 +1,48 @@
+export type EntityRole = 'production' | 'test' | 'fixture' | 'generated' | 'external' | 'tooling';
+
+export const TEST_ROLES = new Set<string>(['test', 'fixture']);
+
+export interface RoleFilterOpts {
+ includeTests?: boolean;
+ testsOnly?: boolean;
+}
+
+export function getNodeRole(node: any): string {
+ return node.attrs?.role ?? 'production';
+}
+
+export function isTestOrFixture(node: any): boolean {
+ return TEST_ROLES.has(getNodeRole(node));
+}
+
+/**
+ * Filter a list of nodes based on role filter opts.
+ * Returns the filtered list plus a count of how many test/fixture nodes were hidden.
+ */
+export function applyRoleFilter<T>(
+ nodes: T[],
+ opts: RoleFilterOpts,
+): { filtered: T[]; hiddenTestCount: number } {
+ if (opts.testsOnly) {
+ return { filtered: nodes.filter(n => isTestOrFixture(n as any)), hiddenTestCount: 0 };
+ }
+ if (opts.includeTests) {
+ return { filtered: nodes, hiddenTestCount: 0 };
+ }
+ const production = nodes.filter(n => !isTestOrFixture(n as any));
+ const tests = nodes.filter(n => isTestOrFixture(n as any));
+ if (production.length > 0) {
+ return { filtered: production, hiddenTestCount: tests.length };
+ }
+ // No production matches — show tests rather than returning nothing
+ return { filtered: tests, hiddenTestCount: 0 };
+}
+
+/**
+ * Returns a hint string if any test/fixture nodes were hidden, otherwise null.
+ */
+export function roleHint(hiddenTestCount: number): string | null {
+ if (hiddenTestCount === 0) return null;
+ const s = hiddenTestCount === 1 ? 'candidate' : 'candidates';
+ return `${hiddenTestCount} test/fixture ${s} hidden. Use --include-tests to include.`;
+}