ralph-workflow 0.7.18

PROMPT-driven multi-agent orchestrator for git repos
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! JavaScript and TypeScript review guidelines
//!
//! Contains guidelines for JavaScript/TypeScript projects including React, Vue, Angular,
//! Node.js backends (Express, Fastify, `NestJS`), and SSR frameworks (Next.js, Nuxt).

use super::base::ReviewGuidelines;
use crate::language_detector::ProjectStack;

/// Add JavaScript-specific guidelines to the review
pub fn add_javascript_guidelines(
    guidelines: ReviewGuidelines,
    stack: &ProjectStack,
) -> ReviewGuidelines {
    let base = ReviewGuidelines {
        quality_checks: guidelines
            .quality_checks
            .into_iter()
            .chain([
                "Use const/let, never var".to_string(),
                "Handle Promise rejections".to_string(),
                "Use async/await over raw Promises".to_string(),
                "Avoid deeply nested callbacks".to_string(),
            ])
            .collect(),
        security_checks: guidelines
            .security_checks
            .into_iter()
            .chain([
                "Sanitize user input before DOM insertion".to_string(),
                "Use Content Security Policy headers".to_string(),
                "Validate data from external APIs".to_string(),
                "Check for prototype pollution vulnerabilities".to_string(),
            ])
            .collect(),
        performance_checks: guidelines
            .performance_checks
            .into_iter()
            .chain([
                "Debounce/throttle frequent event handlers".to_string(),
                "Use appropriate data structures".to_string(),
                "Minimize DOM manipulation".to_string(),
            ])
            .collect(),
        anti_patterns: guidelines
            .anti_patterns
            .into_iter()
            .chain([
                "Avoid == for comparisons (use ===)".to_string(),
                "Don't mutate function arguments".to_string(),
                "Avoid synchronous I/O in Node.js".to_string(),
            ])
            .collect(),
        ..guidelines
    };

    let with_frontend = if stack.frameworks.iter().any(|f| f == "React" || f == "Vue") {
        add_frontend_guidelines(base)
    } else {
        base
    };

    add_framework_guidelines(with_frontend, stack)
}

pub fn add_typescript_guidelines(
    guidelines: ReviewGuidelines,
    stack: &ProjectStack,
) -> ReviewGuidelines {
    let guidelines = add_javascript_guidelines(guidelines, stack);

    ReviewGuidelines {
        quality_checks: guidelines
            .quality_checks
            .into_iter()
            .chain([
                "Use strict TypeScript mode".to_string(),
                "Prefer interfaces over type aliases for objects".to_string(),
                "Use explicit return types for public functions".to_string(),
                "Avoid 'any' type; use 'unknown' if needed".to_string(),
            ])
            .collect(),
        idioms: guidelines
            .idioms
            .into_iter()
            .chain([
                "Use union types for discriminated unions".to_string(),
                "Leverage type inference where clear".to_string(),
                "Use generics appropriately".to_string(),
            ])
            .collect(),
        anti_patterns: guidelines
            .anti_patterns
            .into_iter()
            .chain([
                "Don't use 'as' casts to bypass type checking".to_string(),
                "Avoid non-null assertions (!) without justification".to_string(),
            ])
            .collect(),
        ..guidelines
    }
}

fn add_frontend_guidelines(guidelines: ReviewGuidelines) -> ReviewGuidelines {
    ReviewGuidelines {
        quality_checks: guidelines
            .quality_checks
            .into_iter()
            .chain([
                "Components are properly modularized".to_string(),
                "State management is predictable".to_string(),
                "Accessibility (a11y) is considered".to_string(),
            ])
            .collect(),
        performance_checks: guidelines
            .performance_checks
            .into_iter()
            .chain([
                "Avoid unnecessary re-renders".to_string(),
                "Use lazy loading for large components".to_string(),
                "Optimize bundle size".to_string(),
            ])
            .collect(),
        ..guidelines
    }
}

fn add_framework_guidelines(
    guidelines: ReviewGuidelines,
    stack: &ProjectStack,
) -> ReviewGuidelines {
    stack
        .frameworks
        .iter()
        .fold(guidelines, |acc, framework| match framework.as_str() {
            "React" => add_react_guidelines(acc),
            "Vue" => add_vue_guidelines(acc),
            "Angular" => add_angular_guidelines(acc),
            "Express" | "Fastify" | "NestJS" => add_node_backend_guidelines(acc),
            "Next.js" | "Nuxt" => add_ssr_framework_guidelines(acc),
            _ => acc,
        })
}

fn add_react_guidelines(guidelines: ReviewGuidelines) -> ReviewGuidelines {
    ReviewGuidelines {
        quality_checks: guidelines
            .quality_checks
            .into_iter()
            .chain([
                "Use hooks correctly (rules of hooks)".to_string(),
                "Properly manage component lifecycle".to_string(),
                "Use React.memo for expensive renders".to_string(),
            ])
            .collect(),
        anti_patterns: guidelines
            .anti_patterns
            .into_iter()
            .chain([
                "Avoid prop drilling (use context or state management)".to_string(),
                "Don't mutate state directly".to_string(),
                "Avoid inline functions in render".to_string(),
            ])
            .collect(),
        ..guidelines
    }
}

fn add_vue_guidelines(guidelines: ReviewGuidelines) -> ReviewGuidelines {
    ReviewGuidelines {
        quality_checks: guidelines
            .quality_checks
            .into_iter()
            .chain([
                "Use Composition API for complex logic".to_string(),
                "Follow Vue style guide".to_string(),
                "Use computed properties appropriately".to_string(),
            ])
            .collect(),
        anti_patterns: guidelines
            .anti_patterns
            .into_iter()
            .chain([
                "Avoid watchers when computed works".to_string(),
                "Don't directly mutate props".to_string(),
            ])
            .collect(),
        ..guidelines
    }
}

fn add_angular_guidelines(guidelines: ReviewGuidelines) -> ReviewGuidelines {
    ReviewGuidelines {
        quality_checks: guidelines
            .quality_checks
            .into_iter()
            .chain([
                "Use OnPush change detection where possible".to_string(),
                "Follow Angular style guide".to_string(),
                "Use RxJS operators effectively".to_string(),
            ])
            .collect(),
        security_checks: guidelines
            .security_checks
            .into_iter()
            .chain(["Use Angular's built-in sanitization".to_string()])
            .collect(),
        anti_patterns: guidelines
            .anti_patterns
            .into_iter()
            .chain([
                "Avoid subscribing without unsubscribing".to_string(),
                "Don't use any type".to_string(),
            ])
            .collect(),
        ..guidelines
    }
}

fn add_node_backend_guidelines(guidelines: ReviewGuidelines) -> ReviewGuidelines {
    ReviewGuidelines {
        quality_checks: guidelines
            .quality_checks
            .into_iter()
            .chain([
                "Use middleware pattern effectively".to_string(),
                "Handle errors in middleware".to_string(),
                "Use environment variables for config".to_string(),
            ])
            .collect(),
        security_checks: guidelines
            .security_checks
            .into_iter()
            .chain([
                "Use helmet for security headers".to_string(),
                "Implement rate limiting".to_string(),
                "Validate request body schema".to_string(),
            ])
            .collect(),
        ..guidelines
    }
}

fn add_ssr_framework_guidelines(guidelines: ReviewGuidelines) -> ReviewGuidelines {
    ReviewGuidelines {
        quality_checks: guidelines
            .quality_checks
            .into_iter()
            .chain([
                "Use appropriate rendering strategy (SSR/SSG/ISR)".to_string(),
                "Handle hydration correctly".to_string(),
                "Optimize for Core Web Vitals".to_string(),
            ])
            .collect(),
        performance_checks: guidelines
            .performance_checks
            .into_iter()
            .chain([
                "Minimize client-side JavaScript".to_string(),
                "Use image optimization".to_string(),
            ])
            .collect(),
        ..guidelines
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_javascript_guidelines() {
        let stack = ProjectStack {
            primary_language: "JavaScript".to_string(),
            secondary_languages: vec![],
            frameworks: vec![],
            has_tests: false,
            test_framework: None,
            package_manager: Some("Bun".to_string()),
        };

        let guidelines = add_javascript_guidelines(ReviewGuidelines::default(), &stack);

        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("const/let")));
        assert!(guidelines.anti_patterns.iter().any(|c| c.contains("===")));
    }

    #[test]
    fn test_typescript_react_guidelines() {
        let stack = ProjectStack {
            primary_language: "TypeScript".to_string(),
            secondary_languages: vec!["JavaScript".to_string()],
            frameworks: vec!["React".to_string(), "Next.js".to_string()],
            has_tests: true,
            test_framework: Some("Jest".to_string()),
            package_manager: Some("Bun".to_string()),
        };

        let guidelines = add_typescript_guidelines(ReviewGuidelines::default(), &stack);

        // Should have TypeScript checks
        assert!(guidelines.quality_checks.iter().any(|c| c.contains("any")));
        // Should have React checks
        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("hooks")));
        // Should have Next.js checks
        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("SSR") || c.contains("rendering")));
    }

    #[test]
    fn test_vue_guidelines() {
        let stack = ProjectStack {
            primary_language: "JavaScript".to_string(),
            secondary_languages: vec![],
            frameworks: vec!["Vue".to_string()],
            has_tests: false,
            test_framework: None,
            package_manager: Some("Bun".to_string()),
        };

        let guidelines = add_javascript_guidelines(ReviewGuidelines::default(), &stack);

        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("Composition API")));
    }

    #[test]
    fn test_angular_guidelines() {
        let stack = ProjectStack {
            primary_language: "TypeScript".to_string(),
            secondary_languages: vec![],
            frameworks: vec!["Angular".to_string()],
            has_tests: false,
            test_framework: None,
            package_manager: Some("Bun".to_string()),
        };

        let guidelines = add_typescript_guidelines(ReviewGuidelines::default(), &stack);

        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("OnPush") || c.contains("RxJS")));
    }

    #[test]
    fn test_express_guidelines() {
        let stack = ProjectStack {
            primary_language: "JavaScript".to_string(),
            secondary_languages: vec![],
            frameworks: vec!["Express".to_string()],
            has_tests: false,
            test_framework: None,
            package_manager: Some("Bun".to_string()),
        };

        let guidelines = add_javascript_guidelines(ReviewGuidelines::default(), &stack);

        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("middleware")));
        assert!(guidelines
            .security_checks
            .iter()
            .any(|c| c.contains("helmet")));
    }

    #[test]
    fn test_nextjs_guidelines() {
        let stack = ProjectStack {
            primary_language: "TypeScript".to_string(),
            secondary_languages: vec!["JavaScript".to_string()],
            frameworks: vec!["Next.js".to_string()],
            has_tests: true,
            test_framework: Some("Jest".to_string()),
            package_manager: Some("Bun".to_string()),
        };

        let guidelines = add_typescript_guidelines(ReviewGuidelines::default(), &stack);

        // Should have SSR framework guidelines
        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("SSR") || c.contains("rendering") || c.contains("hydration")));
    }

    #[test]
    fn test_multiple_frameworks_combines_guidelines() {
        let stack = ProjectStack {
            primary_language: "TypeScript".to_string(),
            secondary_languages: vec!["JavaScript".to_string()],
            frameworks: vec!["React".to_string(), "Express".to_string()],
            has_tests: true,
            test_framework: Some("Jest".to_string()),
            package_manager: Some("Bun".to_string()),
        };

        let guidelines = add_typescript_guidelines(ReviewGuidelines::default(), &stack);

        // Should have React-specific checks
        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("hooks")));

        // Should have Express-specific checks
        assert!(guidelines
            .quality_checks
            .iter()
            .any(|c| c.contains("middleware")));
    }
}