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): parse Git patches in linear time

Rudy Celeklib4c18a0e15aa
diff --git a/v3/@claude-flow/cli/__tests__/analyze-diff-content-4122.test.ts b/v3/@claude-flow/cli/__tests__/analyze-diff-content-4122.test.ts
index 6ff2f7038f..4e6b15ddcd 100644
--- a/v3/@claude-flow/cli/__tests__/analyze-diff-content-4122.test.ts
+++ b/v3/@claude-flow/cli/__tests__/analyze-diff-content-4122.test.ts
@@ -3,7 +3,7 @@ import { execFileSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
-import { analyzeDiff, assessFileRisk, classifyDiff, clearDiffCache, suggestReviewers, type DiffFile } from '../src/ruvector/diff-classifier.js';
+import { analyzeDiff, assessFileRisk, classifyDiff, clearDiffCache, DiffClassifier, suggestReviewers, type DiffFile } from '../src/ruvector/diff-classifier.js';
const file = (path: string, additions = 1): DiffFile => ({
path, status: 'modified', additions, deletions: 0, hunks: 1, binary: false,
@@ -63,6 +63,29 @@ ${additions}
expect(classifyDiff(files, bugfixPatch).primary).toBe('bugfix');
});
+ it('parses large path and malformed hunk headers without losing later hunks', () => {
+ const path = `src/${'a/'.repeat(3000)}component.ts`;
+ const malformedStart = '0'.repeat(3000);
+ const patch = `diff --git a/${path} b/${path}
+--- a/${path}
++++ b/${path}
+@@ -${malformedStart}x +1 @@
++ignored
+@@ -1 +1 @@
+-before
++after
+@@ -4 +4 @@
+-old
++new
+`;
+ const parsed = new DiffClassifier().parseDiff(patch);
+ expect(parsed).toHaveLength(1);
+ expect(parsed[0].path).toBe(path);
+ expect(parsed[0].hunks).toHaveLength(2);
+ expect(parsed[0].additions).toBe(2);
+ expect(parsed[0].deletions).toBe(2);
+ });
+
it('uses the commit subject in the real async analysis path for a one-commit range', async () => {
const repo = mkdtempSync(join(tmpdir(), 'ruflo-diff-content-'));
const originalCwd = process.cwd();
diff --git a/v3/@claude-flow/cli/src/ruvector/diff-classifier.ts b/v3/@claude-flow/cli/src/ruvector/diff-classifier.ts
index 5e74308ac8..a70296ebe3 100644
--- a/v3/@claude-flow/cli/src/ruvector/diff-classifier.ts
+++ b/v3/@claude-flow/cli/src/ruvector/diff-classifier.ts
@@ -129,9 +129,11 @@ export class DiffClassifier {
const files: FileDiff[] = [];
const fileBlocks = diffContent.split(/^diff --git/m).filter(Boolean);
for (const block of fileBlocks) {
- const pathMatch = block.match(/a\/(.+?)\s+b\/(.+)/);
- if (!pathMatch) continue;
- const path = pathMatch[2];
+ const headerEnd = block.indexOf('\n');
+ const header = block.slice(0, headerEnd < 0 ? undefined : headerEnd);
+ const destination = header.lastIndexOf(' b/');
+ if (!header.startsWith(' a/') || destination < 0) continue;
+ const path = header.slice(destination + 3);
const hunks = this.parseHunks(block);
const additions = hunks.reduce((sum, h) => sum + h.changes.filter(c => c.type === 'add').length, 0);
const deletions = hunks.reduce((sum, h) => sum + h.changes.filter(c => c.type === 'remove').length, 0);
@@ -178,16 +180,39 @@ export class DiffClassifier {
private parseHunks(block: string): DiffHunk[] {
const hunks: DiffHunk[] = [];
- const hunkMatches = block.matchAll(/@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@([^\n]*)\n([\s\S]*?)(?=@@|$)/g);
- for (const match of hunkMatches) {
- const oldStart = parseInt(match[1], 10);
- const oldLines = parseInt(match[2] || '1', 10);
- const newStart = parseInt(match[3], 10);
- const newLines = parseInt(match[4] || '1', 10);
- const content = match[6] || '';
- const changes = this.parseChanges(content, newStart);
- hunks.push({ oldStart, oldLines, newStart, newLines, content, changes });
+ const readRange = (value: string, prefix: string): [number, number] | null => {
+ if (!value.startsWith(prefix)) return null;
+ const parts = value.slice(1).split(',');
+ if (parts.length > 2 || parts.some(part => !/^\d+$/.test(part))) return null;
+ const start = Number(parts[0]);
+ const count = parts.length === 2 ? Number(parts[1]) : 1;
+ return Number.isSafeInteger(start) && Number.isSafeInteger(count) ? [start, count] : null;
+ };
+ let current: { oldStart: number; oldLines: number; newStart: number; newLines: number; lines: string[] } | null = null;
+ const finish = () => {
+ if (!current) return;
+ const content = current.lines.join('\n');
+ hunks.push({
+ oldStart: current.oldStart, oldLines: current.oldLines,
+ newStart: current.newStart, newLines: current.newLines,
+ content, changes: this.parseChanges(content, current.newStart),
+ });
+ };
+ for (const line of block.split('\n')) {
+ if (line.startsWith('@@ -')) {
+ const end = line.indexOf(' @@', 3);
+ const coordinates = end < 0 ? [] : line.slice(3, end).split(' ');
+ const oldRange = coordinates.length === 2 ? readRange(coordinates[0], '-') : null;
+ const newRange = coordinates.length === 2 ? readRange(coordinates[1], '+') : null;
+ if (oldRange && newRange) {
+ finish();
+ current = { oldStart: oldRange[0], oldLines: oldRange[1], newStart: newRange[0], newLines: newRange[1], lines: [] };
+ continue;
+ }
+ }
+ if (current) current.lines.push(line);
}
+ finish();
return hunks;
}