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): classify actual diff content and commit subjects

Rudy Celekliea7bc48a9e0d
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
new file mode 100644
index 0000000000..6ff2f7038f
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/analyze-diff-content-4122.test.ts
@@ -0,0 +1,94 @@
+import { afterEach, describe, expect, it } from 'vitest';
+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';
+
+const file = (path: string, additions = 1): DiffFile => ({
+ path, status: 'modified', additions, deletions: 0, hunks: 1, binary: false,
+});
+
+describe('diff classification uses source-bound evidence', () => {
+ afterEach(() => clearDiffCache());
+
+ it('does not classify unrelated path substrings as bugfixes or tests', () => {
+ for (const path of ['src/debug/logger.ts', 'src/dispatcher.ts', 'src/inspector.ts']) {
+ expect(classifyDiff([file(path)]).primary, path).toBe('unknown');
+ }
+ });
+
+ it('recognizes non-JavaScript test files', () => {
+ expect(classifyDiff([file('tests/schema.test.sql')]).primary).toBe('test');
+ });
+
+ it('does not infer security, API, or config risk from substrings inside unrelated names', () => {
+ for (const path of ['src/author/profile.ts', 'src/rapid-sort.ts', 'src/envelope.ts']) {
+ const changed = file(path);
+ expect(assessFileRisk(changed).risk, path).toBe('low');
+ expect(suggestReviewers([changed], [assessFileRisk(changed)]), path).toEqual(['developer']);
+ }
+ expect(assessFileRisk(file('src/auth/profile.ts')).risk).toBe('high');
+ expect(assessFileRisk(file('src/api/client.ts')).risk).toBe('low');
+ expect(suggestReviewers([file('src/api/client.ts')], []).includes('api-owner')).toBe(true);
+ });
+
+ it('uses changed lines and the one-commit subject, without treating Error as a fix', () => {
+ const patch = `diff --git a/src/debug/logger.ts b/src/debug/logger.ts
+--- a/src/debug/logger.ts
++++ b/src/debug/logger.ts
+@@ -0,0 +1 @@
++throw new Error('missing logger');
+`;
+ const files = [file('src/debug/logger.ts')];
+ expect(classifyDiff(files, patch).primary).toBe('unknown');
+ const result = classifyDiff(files, patch, 'feat(logger): add debug logger');
+ expect(result.primary).toBe('feature');
+ expect(result.confidence).toBeGreaterThanOrEqual(0.85);
+ expect(classifyDiff(files, patch, 'fix(logger): implement rollback').primary).toBe('bugfix');
+ });
+
+ it('uses real added code and does not reuse a stale classification for the same hunk', () => {
+ const additions = Array.from({ length: 12 }, (_, i) => `+export function helper${i}() { return ${i}; }`).join('\n');
+ const featurePatch = `diff --git a/src/a.ts b/src/a.ts
+--- a/src/a.ts
++++ b/src/a.ts
+@@ -0,0 +1,12 @@
+${additions}
+`;
+ const bugfixPatch = featurePatch.replace(additions,
+ Array.from({ length: 12 }, () => '+// fix bug in existing behavior').join('\n'));
+ const files = [file('src/a.ts', 12)];
+ expect(classifyDiff(files, featurePatch).primary).toBe('feature');
+ expect(classifyDiff(files, bugfixPatch).primary).toBe('bugfix');
+ });
+
+ 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();
+ const git = (...args: string[]) => execFileSync('git', ['-C', repo, ...args], { stdio: 'pipe' });
+ try {
+ git('init', '-q');
+ git('config', 'user.name', 'Ruflo Test');
+ git('config', 'user.email', '[email protected]');
+ mkdirSync(join(repo, 'src'));
+ writeFileSync(join(repo, 'src', 'debug.ts'), 'export const before = true;\n');
+ git('add', '.');
+ git('commit', '-qm', 'initial');
+ writeFileSync(join(repo, 'src', 'debug.ts'), "throw new Error('missing logger');\n");
+ git('add', '.');
+ git('commit', '-qm', 'feat(logger): add debug logger');
+ const parent = git('rev-parse', 'HEAD^').toString().trim();
+ const head = git('rev-parse', 'HEAD').toString().trim();
+ process.chdir(repo);
+ clearDiffCache();
+ const result = await analyzeDiff({ ref: `${parent}..${head}`, skipCache: true });
+ expect(result.files).toHaveLength(1);
+ expect(result.classification.primary).toBe('feature');
+ } finally {
+ process.chdir(originalCwd);
+ clearDiffCache();
+ rmSync(repo, { recursive: true, force: true });
+ }
+ });
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/analyze-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/analyze-tools.ts
index 1981cc1a8e..bf5217f42d 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/analyze-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/analyze-tools.ts
@@ -9,7 +9,6 @@ import {
analyzeDiff,
assessFileRisk,
assessOverallRisk,
- classifyDiff,
suggestReviewers,
getGitDiffNumstat,
type DiffFile,
@@ -22,7 +21,7 @@ import {
*/
export const analyzeDiffTool: MCPTool = {
name: 'analyze_diff',
- description: 'Analyze git diff for change risk assessment and classification Use when native `git diff` / `grep` / static analysis is wrong because you want LLM-graded change classification, reviewer recommendations, or risk scoring. For literal-text inspection, native tools are fine.',
+ description: 'Heuristically analyze git diff paths, changed lines, and one-commit subjects for risk and change classification.',
category: 'analyze',
tags: ['diff', 'risk', 'classification', 'git'],
inputSchema: {
@@ -98,7 +97,7 @@ export const analyzeDiffTool: MCPTool = {
*/
export const diffRiskTool: MCPTool = {
name: 'analyze_diff-risk',
- description: 'Quick risk assessment for git diff Use when native `git diff` / `grep` / static analysis is wrong because you want LLM-graded change classification, reviewer recommendations, or risk scoring. For literal-text inspection, native tools are fine.',
+ description: 'Heuristically assess git diff risk from changed file paths, sizes, and status.',
category: 'analyze',
tags: ['diff', 'risk', 'git'],
inputSchema: {
@@ -141,7 +140,7 @@ export const diffRiskTool: MCPTool = {
*/
export const diffClassifyTool: MCPTool = {
name: 'analyze_diff-classify',
- description: 'Classify git diff change type Use when native `git diff` / `grep` / static analysis is wrong because you want LLM-graded change classification, reviewer recommendations, or risk scoring. For literal-text inspection, native tools are fine.',
+ description: 'Heuristically classify a git diff from changed lines, file paths, and a single-commit subject when available.',
category: 'analyze',
tags: ['diff', 'classification', 'git'],
inputSchema: {
@@ -159,13 +158,12 @@ export const diffClassifyTool: MCPTool = {
const ref = (params.ref as string) || 'HEAD';
try {
- const files = getGitDiffNumstat(ref);
- const classification = classifyDiff(files);
+ const result = await analyzeDiff({ ref });
return {
ref,
- classification,
- files: files.length,
+ classification: result.classification,
+ files: result.files.length,
};
} catch (error) {
return {
@@ -232,7 +230,7 @@ export const diffReviewersTool: MCPTool = {
*/
export const fileRiskTool: MCPTool = {
name: 'analyze_file-risk',
- description: 'Assess risk for a specific file change Use when native `git diff` / `grep` / static analysis is wrong because you want LLM-graded change classification, reviewer recommendations, or risk scoring. For literal-text inspection, native tools are fine.',
+ description: 'Heuristically assess risk for a file path and optional change metadata.',
category: 'analyze',
tags: ['file', 'risk'],
inputSchema: {
@@ -289,7 +287,7 @@ export const fileRiskTool: MCPTool = {
*/
export const diffStatsTool: MCPTool = {
name: 'analyze_diff-stats',
- description: 'Get quick statistics for git diff Use when native `git diff` / `grep` / static analysis is wrong because you want LLM-graded change classification, reviewer recommendations, or risk scoring. For literal-text inspection, native tools are fine.',
+ description: 'Count changed files, additions, and deletions in a git diff.',
category: 'analyze',
tags: ['diff', 'stats', 'git'],
inputSchema: {
diff --git a/v3/@claude-flow/cli/src/ruvector/diff-classifier.ts b/v3/@claude-flow/cli/src/ruvector/diff-classifier.ts
index ca71f450ad..5e74308ac8 100644
--- a/v3/@claude-flow/cli/src/ruvector/diff-classifier.ts
+++ b/v3/@claude-flow/cli/src/ruvector/diff-classifier.ts
@@ -3,6 +3,7 @@
*/
import { execFileSync, execFile } from 'node:child_process';
+import { createHash } from 'node:crypto';
import { promisify } from 'node:util';
export interface DiffClassifierConfig {
@@ -64,7 +65,7 @@ const DEFAULT_CONFIG: DiffClassifierConfig = {
minConfidence: 0.5,
};
-const CLASSIFICATION_PATTERNS: Record<string, RegExp[]> = {
+const COMMIT_CLASSIFICATION_PATTERNS: Record<string, RegExp[]> = {
feature: [/^feat/, /add.*feature/, /implement/, /new.*functionality/i],
bugfix: [/^fix/, /bug/, /patch/, /resolve.*issue/i, /hotfix/i],
refactor: [/^refactor/, /restructure/, /reorganize/, /cleanup/i, /rename/i],
@@ -74,11 +75,35 @@ const CLASSIFICATION_PATTERNS: Record<string, RegExp[]> = {
style: [/^style/, /format/, /lint/, /prettier/, /eslint/],
};
+// File paths and commit subjects are different signals. Prefixes such as
+// "fix" and words such as "bug" are useful in subjects, but match unrelated
+// paths like src/debug/logger.ts and src/dispatcher.ts.
+const PATH_CLASSIFICATION_PATTERNS: Record<string, RegExp[]> = {
+ docs: [/(^|\/)docs?\//i, /(^|\/)readme(?:\.[^/]*)?$/i, /\.mdx?$/i],
+ test: [/(^|\/)(?:__tests__|tests?|specs?)\//i, /\.(?:test|spec)\.[^/]+$/i],
+ config: [/(^|\/)(?:configs?\/|config(?:\.[^/]*)?$)/i, /\.config\.[^/]+$/i,
+ /(^|\/)(?:package|tsconfig)(?:\.[^/]*)?\.json$/i, /(^|\/)\.env(?:\.[^/]*)?$/i],
+ style: [/\.(?:css|scss|sass|less)$/i],
+};
+
const IMPACT_KEYWORDS: Record<string, number> = {
security: 3, auth: 3, payment: 3, database: 2, api: 2, core: 2,
util: 1, helper: 1, test: 0, mock: 0, fixture: 0,
};
+function pathHasTerm(path: string, term: string): boolean {
+ const tokens = path.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase().split(/[^a-z0-9]+/);
+ const aliases: Record<string, string[]> = {
+ auth: ['auth', 'authentication', 'authorization', 'authenticator', 'oauth'],
+ util: ['util', 'utils', 'utility', 'utilities'],
+ test: ['test', 'tests'],
+ mock: ['mock', 'mocks'],
+ fixture: ['fixture', 'fixtures'],
+ config: ['config', 'configuration'],
+ };
+ return (aliases[term] ?? [term]).some(value => tokens.includes(value));
+}
+
export class DiffClassifier {
private config: DiffClassifierConfig;
private ruvectorEngine: unknown = null;
@@ -129,7 +154,15 @@ export class DiffClassifier {
classifyCommitMessage(message: string): DiffClassification['primary'] {
const lowerMessage = message.toLowerCase();
- for (const [type, patterns] of Object.entries(CLASSIFICATION_PATTERNS)) {
+ const conventional = lowerMessage.match(/^([a-z]+)(?:\([^)]*\))?!?:/);
+ const conventionalTypes: Record<string, DiffClassification['primary']> = {
+ feat: 'feature', feature: 'feature', fix: 'bugfix', refactor: 'refactor',
+ docs: 'docs', doc: 'docs', test: 'test', config: 'config', style: 'style',
+ };
+ if (conventional && conventionalTypes[conventional[1]]) {
+ return conventionalTypes[conventional[1]];
+ }
+ for (const [type, patterns] of Object.entries(COMMIT_CLASSIFICATION_PATTERNS)) {
for (const pattern of patterns) {
if (pattern.test(lowerMessage)) return type as DiffClassification['primary'];
}
@@ -187,17 +220,22 @@ export class DiffClassifier {
}
private getCacheKey(path: string, hunks: DiffHunk[]): string {
- const hunkSummary = hunks.map(h => h.oldStart + ':' + h.newStart).join(',');
- return path + ':' + hunkSummary;
+ const digest = createHash('sha256');
+ digest.update(path);
+ for (const hunk of hunks) {
+ digest.update(`${hunk.oldStart}:${hunk.newStart}:`);
+ for (const change of hunk.changes) digest.update(`${change.type}:${change.content}\n`);
+ }
+ return digest.digest('hex');
}
private determinePrimaryClassification(path: string, hunks: DiffHunk[]): DiffClassification['primary'] {
- for (const [type, patterns] of Object.entries(CLASSIFICATION_PATTERNS)) {
+ for (const [type, patterns] of Object.entries(PATH_CLASSIFICATION_PATTERNS)) {
for (const pattern of patterns) { if (pattern.test(path)) return type as DiffClassification['primary']; }
}
- const allContent = hunks.flatMap(h => h.changes.map(c => c.content)).join('\n').toLowerCase();
+ const allContent = hunks.flatMap(h => h.changes.filter(c => c.type !== 'context').map(c => c.content)).join('\n').toLowerCase();
if (/function|class|interface|type\s+\w+/.test(allContent) && hunks.some(h => h.changes.filter(c => c.type === 'add').length > 10)) return 'feature';
- if (/fix|bug|issue|error|exception/.test(allContent)) return 'bugfix';
+ if (/\b(?:fix(?:ed|es)?|bug|issue|exception)\b/.test(allContent)) return 'bugfix';
if (this.config.detectRefactoring && this.isRefactoring(hunks)) return 'refactor';
return 'unknown';
}
@@ -211,7 +249,7 @@ export class DiffClassifier {
private determineSecondaryClassifications(path: string, hunks: DiffHunk[], primary: DiffClassification['primary']): string[] {
const secondary: string[] = [];
- for (const [type, patterns] of Object.entries(CLASSIFICATION_PATTERNS)) {
+ for (const [type, patterns] of Object.entries(PATH_CLASSIFICATION_PATTERNS)) {
if (type === primary) continue;
for (const pattern of patterns) { if (pattern.test(path)) { secondary.push(type); break; } }
}
@@ -220,7 +258,7 @@ export class DiffClassifier {
private calculateConfidence(path: string, hunks: DiffHunk[], primary: DiffClassification['primary']): number {
let confidence = 0.5;
- for (const patterns of Object.values(CLASSIFICATION_PATTERNS)) { for (const pattern of patterns) { if (pattern.test(path)) { confidence += 0.2; break; } } }
+ for (const patterns of Object.values(PATH_CLASSIFICATION_PATTERNS)) { for (const pattern of patterns) { if (pattern.test(path)) { confidence += 0.2; break; } } }
const totalChanges = hunks.reduce((sum, h) => sum + h.changes.length, 0);
if (totalChanges > 10) confidence += 0.1;
if (totalChanges > 50) confidence += 0.1;
@@ -230,8 +268,7 @@ export class DiffClassifier {
private determineImpactLevel(path: string, hunks: DiffHunk[]): DiffClassification['impactLevel'] {
let score = 0;
- const lowerPath = path.toLowerCase();
- for (const [keyword, weight] of Object.entries(IMPACT_KEYWORDS)) { if (lowerPath.includes(keyword)) score = Math.max(score, weight); }
+ for (const [keyword, weight] of Object.entries(IMPACT_KEYWORDS)) { if (pathHasTerm(path, keyword)) score = Math.max(score, weight); }
const totalChanges = hunks.reduce((sum, h) => sum + h.changes.filter(c => c.type !== 'context').length, 0);
if (totalChanges > 100) score = Math.max(score, 2);
if (totalChanges > 300) score = Math.max(score, 3);
@@ -246,8 +283,8 @@ export class DiffClassifier {
const typeReviewers: Record<string, string[]> = { feature: ['tech-lead', 'product-owner'], bugfix: ['qa-engineer', 'developer'], refactor: ['senior-developer', 'architect'], docs: ['tech-writer', 'developer'], test: ['qa-engineer', 'developer'], config: ['devops', 'tech-lead'], style: ['developer'], unknown: ['developer'] };
reviewers.push(...(typeReviewers[primary] || typeReviewers.unknown));
if (impact === 'critical' || impact === 'high') reviewers.push('security-reviewer');
- if (/security|auth/.test(path)) reviewers.push('security-team');
- if (/database|migration/.test(path)) reviewers.push('dba');
+ if (pathHasTerm(path, 'security') || pathHasTerm(path, 'auth')) reviewers.push('security-team');
+ if (pathHasTerm(path, 'database') || pathHasTerm(path, 'migration')) reviewers.push('dba');
return [...new Set(reviewers)].slice(0, 4);
}
@@ -256,8 +293,8 @@ export class DiffClassifier {
if (primary !== 'test') strategies.push('unit-tests');
if (primary === 'feature') strategies.push('integration-tests');
if (impact === 'high' || impact === 'critical') { strategies.push('regression-tests'); strategies.push('e2e-tests'); }
- if (/api|endpoint|route|handler/.test(path)) strategies.push('api-contract-tests');
- if (/security|auth|crypto/.test(path)) strategies.push('security-audit');
+ if (['api', 'endpoint', 'route', 'handler'].some(term => pathHasTerm(path, term))) strategies.push('api-contract-tests');
+ if (['security', 'auth', 'crypto'].some(term => pathHasTerm(path, term))) strategies.push('security-audit');
return strategies.slice(0, 5);
}
@@ -267,9 +304,9 @@ export class DiffClassifier {
if (totalChanges > 200) risks.push('Large change set - increased review time needed');
if (impact === 'critical') risks.push('Critical system component - requires careful review');
if (impact === 'high') risks.push('High-impact area - monitor after deployment');
- if (/security|auth/.test(path)) risks.push('Security-sensitive code');
- if (/database|migration/.test(path)) risks.push('Database changes - ensure backup strategy');
- if (/config|env/.test(path)) risks.push('Configuration changes - verify all environments');
+ if (pathHasTerm(path, 'security') || pathHasTerm(path, 'auth')) risks.push('Security-sensitive code');
+ if (pathHasTerm(path, 'database') || pathHasTerm(path, 'migration')) risks.push('Database changes - ensure backup strategy');
+ if (pathHasTerm(path, 'config') || pathHasTerm(path, 'env')) risks.push('Configuration changes - verify all environments');
const allContent = hunks.flatMap(h => h.changes.map(c => c.content)).join('\n');
if (/TODO|FIXME|HACK/.test(allContent)) risks.push('Contains TODO/FIXME comments');
if (/password|secret|key|token/i.test(allContent)) risks.push('Potential secrets in code');
@@ -557,24 +594,23 @@ export function assessFileRisk(file: DiffFile): FileRisk {
}
// Path-based risk
- const lowerPath = file.path.toLowerCase();
- if (/security|auth|crypto|password/.test(lowerPath)) {
+ if (['security', 'auth', 'crypto', 'password'].some(term => pathHasTerm(file.path, term))) {
score += 40;
reasons.push('Security-sensitive file');
}
- if (/payment|billing|transaction/.test(lowerPath)) {
+ if (['payment', 'billing', 'transaction'].some(term => pathHasTerm(file.path, term))) {
score += 35;
reasons.push('Payment-related file');
}
- if (/database|migration|schema/.test(lowerPath)) {
+ if (['database', 'migration', 'schema'].some(term => pathHasTerm(file.path, term))) {
score += 25;
reasons.push('Database-related file');
}
- if (/core|main|index/.test(lowerPath)) {
+ if (['core', 'main', 'index'].some(term => pathHasTerm(file.path, term))) {
score += 15;
reasons.push('Core module');
}
- if (/config|env|settings/.test(lowerPath)) {
+ if (['config', 'env', 'settings'].some(term => pathHasTerm(file.path, term))) {
score += 20;
reasons.push('Configuration file');
}
@@ -634,20 +670,71 @@ function getClassifier(): DiffClassifier {
return classifierInstance;
}
-/**
- * Classify a diff based on files (uses singleton classifier)
- */
-export function classifyDiff(files: DiffFile[]): DiffClassification {
+/** Classify file metadata with the actual patch and, for one-commit ranges, its subject. */
+export function classifyDiff(files: DiffFile[], patch = '', commitSubject = ''): DiffClassification {
const classifier = getClassifier();
- const fileDiffs: FileDiff[] = files.map(f => ({
- path: f.path,
- hunks: [],
- additions: f.additions,
- deletions: f.deletions,
- classification: classifier['classifyFile'](f.path, []),
- }));
-
- return classifier['computeOverallClassification'](fileDiffs);
+ const parsed = new Map(classifier.parseDiff(patch).map(file => [file.path, file]));
+ const fileDiffs: FileDiff[] = files.map(f => {
+ const patchFile = parsed.get(f.path);
+ const hunks = patchFile?.hunks ?? [];
+ return {
+ path: f.path,
+ hunks,
+ additions: f.additions,
+ deletions: f.deletions,
+ classification: patchFile?.classification ?? classifier['classifyFile'](f.path, hunks),
+ };
+ });
+
+ const overall = classifier['computeOverallClassification'](fileDiffs);
+ if (fileDiffs.length > 0 && commitSubject) {
+ const subjectType = classifier.classifyCommitMessage(commitSubject);
+ if (subjectType !== 'unknown') {
+ if (overall.primary !== subjectType && overall.primary !== 'unknown') {
+ overall.secondary = [overall.primary, ...overall.secondary].filter((type, index, all) =>
+ type !== subjectType && all.indexOf(type) === index).slice(0, 3);
+ }
+ overall.primary = subjectType;
+ overall.confidence = Math.max(overall.confidence, 0.85);
+ }
+ }
+ return overall;
+}
+
+function getSingleCommitSubjectSync(ref: string): string {
+ if (!ref.includes('..')) return '';
+ try {
+ const output = execFileSync('git', ['log', '-2', '--format=%s', ref],
+ { encoding: 'utf-8', maxBuffer: 64 * 1024 });
+ const subjects = output.trim().split('\n').filter(Boolean);
+ return subjects.length === 1 ? subjects[0] : '';
+ } catch { return ''; }
+}
+
+function getDiffContextSync(ref: string): { patch: string; subject: string } {
+ validateGitRef(ref);
+ let patch = '';
+ try {
+ patch = execFileSync('git', ['diff', '--no-ext-diff', '--no-textconv', '--unified=0', ref],
+ { encoding: 'utf-8', maxBuffer: 10 * 1024 * 1024 });
+ } catch { /* A large or unavailable patch falls back to path-only classification. */ }
+ return { patch, subject: getSingleCommitSubjectSync(ref) };
+}
+
+async function getDiffContextAsync(ref: string): Promise<{ patch: string; subject: string }> {
+ validateGitRef(ref);
+ const execFileAsync = promisify(execFile);
+ const patchPromise = execFileAsync('git', ['diff', '--no-ext-diff', '--no-textconv', '--unified=0', ref],
+ { maxBuffer: 10 * 1024 * 1024 }).then(({ stdout }) => stdout).catch(() => '');
+ const subjectPromise = ref.includes('..')
+ ? execFileAsync('git', ['log', '-2', '--format=%s', ref], { maxBuffer: 64 * 1024 })
+ .then(({ stdout }) => {
+ const subjects = stdout.trim().split('\n').filter(Boolean);
+ return subjects.length === 1 ? subjects[0] : '';
+ }).catch(() => '')
+ : Promise.resolve('');
+ const [patch, subject] = await Promise.all([patchPromise, subjectPromise]);
+ return { patch, subject };
}
/**
@@ -657,15 +744,14 @@ export function suggestReviewers(files: DiffFile[], fileRisks: FileRisk[]): stri
const reviewers = new Set<string>();
for (const file of files) {
- const lowerPath = file.path.toLowerCase();
-
- if (/security|auth|crypto/.test(lowerPath)) reviewers.add('security-team');
- if (/database|migration/.test(lowerPath)) reviewers.add('dba');
- if (/api|endpoint|route/.test(lowerPath)) reviewers.add('api-owner');
- if (/test|spec/.test(lowerPath)) reviewers.add('qa-engineer');
- if (/config|deploy|ci/.test(lowerPath)) reviewers.add('devops');
- if (/ui|component|style/.test(lowerPath)) reviewers.add('frontend-lead');
- if (/model|service|repository/.test(lowerPath)) reviewers.add('backend-lead');
+ const hasAny = (...terms: string[]) => terms.some(term => pathHasTerm(file.path, term));
+ if (hasAny('security', 'auth', 'crypto')) reviewers.add('security-team');
+ if (hasAny('database', 'migration')) reviewers.add('dba');
+ if (hasAny('api', 'endpoint', 'route')) reviewers.add('api-owner');
+ if (hasAny('test', 'spec')) reviewers.add('qa-engineer');
+ if (hasAny('config', 'deploy', 'ci')) reviewers.add('devops');
+ if (hasAny('ui', 'component', 'style')) reviewers.add('frontend-lead');
+ if (hasAny('model', 'service', 'repository')) reviewers.add('backend-lead');
}
// Add based on risk
@@ -706,7 +792,7 @@ export async function analyzeDiff(options: {
}
// Use async git diff for non-blocking operation
- const files = await getGitDiffNumstatAsync(ref);
+ const [files, context] = await Promise.all([getGitDiffNumstatAsync(ref), getDiffContextAsync(ref)]);
// Parallel file risk assessment for large diffs
const fileRisks = files.length > 20
@@ -714,7 +800,7 @@ export async function analyzeDiff(options: {
: files.map(assessFileRisk);
const risk = assessOverallRisk(files, fileRisks);
- const classification = classifyDiff(files);
+ const classification = classifyDiff(files, context.patch, context.subject);
const recommendedReviewers = suggestReviewers(files, fileRisks);
const totalAdditions = files.reduce((sum, f) => sum + f.additions, 0);
@@ -753,9 +839,10 @@ export function analyzeDiffSync(options: {
}
const files = getGitDiffNumstat(ref);
+ const context = getDiffContextSync(ref);
const fileRisks = files.map(assessFileRisk);
const risk = assessOverallRisk(files, fileRisks);
- const classification = classifyDiff(files);
+ const classification = classifyDiff(files, context.patch, context.subject);
const recommendedReviewers = suggestReviewers(files, fileRisks);
const totalAdditions = files.reduce((sum, f) => sum + f.additions, 0);