Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(analyze): prevent exponential import regex backtracking

Rudy Celekli153c7bcd0437
diff --git a/v3/@claude-flow/cli/__tests__/ruvector/graph-analyzer.test.ts b/v3/@claude-flow/cli/__tests__/ruvector/graph-analyzer.test.ts
index 343882c0c9..63d4995d35 100644
--- a/v3/@claude-flow/cli/__tests__/ruvector/graph-analyzer.test.ts
+++ b/v3/@claude-flow/cli/__tests__/ruvector/graph-analyzer.test.ts
@@ -25,8 +25,10 @@ import {
type GraphAnalysisResult,
} from '../../src/ruvector/graph-analyzer.js';
import { mkdir, writeFile, rm } from 'fs/promises';
+import { spawnSync } from 'child_process';
import { join } from 'path';
import { tmpdir } from 'os';
+import { fileURLToPath } from 'url';
// Mock the @ruvector/wasm module
vi.mock('@ruvector/wasm', () => ({
@@ -93,6 +95,36 @@ export const b = 'value';
expect(nodeA?.imports.length).toBeGreaterThan(0);
});
+ it('does not spend seconds backtracking on prose containing import', async () => {
+ await writeFile(join(testDir, 'source.ts'), `
+// A plain sentence: import apple berry cherry delta echo foxtrot golf.
+import main, { helper } from './helper';
+export { main, helper };
+`);
+ await writeFile(join(testDir, 'helper.ts'), 'export const helper = 1; export default helper;\n');
+
+ // Run the parser in a child: a regex regression can block the event loop,
+ // so a normal Vitest timeout cannot interrupt it.
+ const tsx = fileURLToPath(new URL('../../node_modules/tsx/dist/cli.mjs', import.meta.url));
+ const source = fileURLToPath(new URL('../../src/ruvector/graph-analyzer.ts', import.meta.url));
+ const probe = `import(${JSON.stringify(source)}).then(async ({ buildDependencyGraph }) => {
+ const graph = await buildDependencyGraph(${JSON.stringify(testDir)}, { skipCache: true });
+ console.log(JSON.stringify({ imports: graph.nodes.get('source.ts')?.imports,
+ edges: graph.edges.map(edge => [edge.source, edge.target]) }));
+ }).catch(error => { console.error(error); process.exitCode = 1; });`;
+ const result = spawnSync(process.execPath, [tsx, '--eval', probe], {
+ cwd: testDir,
+ encoding: 'utf8',
+ timeout: 2000,
+ });
+
+ expect(result.error).toBeUndefined();
+ expect(result.status, result.stderr).toBe(0);
+ const graph = JSON.parse(result.stdout);
+ expect(graph.imports).toContain('./helper');
+ expect(graph.edges).toContainEqual(['source.ts', 'helper.ts']);
+ });
+
it('should handle empty directory', async () => {
const graph = await buildDependencyGraph(testDir);
expect(graph.nodes.size).toBe(0);
diff --git a/v3/@claude-flow/cli/src/ruvector/graph-analyzer.ts b/v3/@claude-flow/cli/src/ruvector/graph-analyzer.ts
index 83f29123ff..b6dde4ebb2 100644
--- a/v3/@claude-flow/cli/src/ruvector/graph-analyzer.ts
+++ b/v3/@claude-flow/cli/src/ruvector/graph-analyzer.ts
@@ -219,7 +219,10 @@ function extractImports(content: string, _filePath: string): Array<{ path: strin
const imports: Array<{ path: string; type: GraphEdge['type'] }> = [];
// ES6 import statements
- const esImportRegex = /import\s+(?:(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s*,?\s*)*\s*from\s*['"]([^'"]+)['"]/g;
+ // Keep the import clause's shape finite. Repeating an optional separator
+ // around alternatives lets ordinary prose such as "import one two three ..."
+ // backtrack exponentially when no `from` clause follows.
+ const esImportRegex = /\bimport\s+(?:type\s+)?(?:[\w$]+\s*,\s*)?(?:\{[^}]*\}|\*\s+as\s+[\w$]+|[\w$]+)\s+from\s*['"]([^'"]+)['"]/g;
let match: RegExpExecArray | null;
while ((match = esImportRegex.exec(content)) !== null) {