cora-code 0.17.2

CLI-first AI code review — BYOK, diff/scan/branch, pre-commit hooks
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#[allow(dead_code)]
/// Grouping strategies for bundling files into LLM-friendly groups.
use std::collections::HashMap;

use tracing::debug;

use crate::engine::scanner::FileEntry;

use super::types::{BundlingConfig, FileGroup, GroupingKey, GroupingStrategy, LanguageFamily};

/// Group files into `FileGroup`s based on the configuration.
///
/// This is the main entry point for the bundling engine. It dispatches to
/// the appropriate strategy and returns a vector of groups.
pub fn group_files(files: &[FileEntry], config: &BundlingConfig) -> Vec<FileGroup> {
    if files.is_empty() {
        return Vec::new();
    }

    match config.strategy {
        GroupingStrategy::Smart => group_smart(files, config),
        GroupingStrategy::Flat => group_flat(files, config),
    }
}

/// Smart grouping: coalesce by directory and language within character limits.
///
/// Algorithm:
/// 1. Compute a `GroupingKey` (directory × language_family) for each file.
/// 2. Sort files by key so related files are adjacent.
/// 3. Greedily pack files into groups, preferring to keep the same key together.
/// 4. When a group is full (char or file limit), start a new one.
fn group_smart(files: &[FileEntry], config: &BundlingConfig) -> Vec<FileGroup> {
    debug!(total_files = files.len(), "smart grouping files");

    let max_chars = config.max_chars_per_group;
    let max_files = config.max_files_per_group;

    // Build grouping keys for each file
    let mut keyed: Vec<(GroupingKey, &FileEntry)> = files
        .iter()
        .map(|f| {
            let ext = f
                .path
                .rsplit_once('.')
                .map(|(_, e)| e)
                .filter(|e| !e.contains('/'));
            let key = if config.coalesce_by_directory || config.coalesce_by_language {
                GroupingKey::from_file(&f.path, ext)
            } else {
                GroupingKey {
                    directory: String::new(),
                    language: LanguageFamily::Other,
                }
            };
            (key, f)
        })
        .collect();

    // Sort by key to cluster related files
    keyed.sort_by(|a, b| {
        if config.coalesce_by_directory && config.coalesce_by_language {
            a.0.directory
                .cmp(&b.0.directory)
                .then_with(|| a.0.language.cmp(&b.0.language))
                .then_with(|| a.1.path.cmp(&b.1.path))
        } else if config.coalesce_by_directory {
            a.0.directory
                .cmp(&b.0.directory)
                .then_with(|| a.1.path.cmp(&b.1.path))
        } else if config.coalesce_by_language {
            a.0.language
                .cmp(&b.0.language)
                .then_with(|| a.1.path.cmp(&b.1.path))
        } else {
            a.1.path.cmp(&b.1.path)
        }
    });

    // Greedy packing with coalescing preference
    let mut groups: Vec<FileGroup> = Vec::new();
    let mut current_group = FileGroup::new();
    let mut current_key: Option<GroupingKey> = None;

    for (key, file) in keyed {
        let fits = current_group.would_fit(file, max_chars, max_files);

        // If file doesn't fit or we're switching to a very different key, consider
        // starting a new group. But only split if we have enough files in the current
        // group to justify it (avoid trivial groups).
        let should_split = !fits
            || (current_key.is_some()
                && current_key.as_ref() != Some(&key)
                && current_group.len() >= 2
                && would_exceed_threshold(&current_group, file, max_chars));

        if should_split && !current_group.is_empty() {
            groups.push(std::mem::replace(&mut current_group, FileGroup::new()));
            let _ = current_key.take();
        }

        current_group.push(file.clone());
        current_key = Some(key);
    }

    if !current_group.is_empty() {
        groups.push(current_group);
    }

    debug!(groups = groups.len(), "smart grouping complete");

    groups
}

/// Check if adding a file would push the group past a "comfort threshold"
/// (75% of max_chars), suggesting it's a good split point.
fn would_exceed_threshold(group: &FileGroup, file: &FileEntry, max_chars: usize) -> bool {
    let threshold = (max_chars as f64 * 0.75) as usize;
    let cost = FileGroup::file_cost(file);
    group.total_chars + cost > threshold
}

/// Flat grouping: simple first-fit batching by character count.
///
/// This is the legacy behavior — equivalent to the old `batch_files` function
/// in scanner.rs. Files are processed in order, packed greedily until limits
/// are hit, then a new group starts.
fn group_flat(files: &[FileEntry], config: &BundlingConfig) -> Vec<FileGroup> {
    debug!(total_files = files.len(), "flat grouping files");

    let max_chars = config.max_chars_per_group;
    let max_files = config.max_files_per_group;

    let mut groups: Vec<FileGroup> = Vec::new();
    let mut current_group = FileGroup::new();

    for file in files {
        if !current_group.would_fit(file, max_chars, max_files) && !current_group.is_empty() {
            groups.push(std::mem::replace(&mut current_group, FileGroup::new()));
        }
        current_group.push(file.clone());
    }

    if !current_group.is_empty() {
        groups.push(current_group);
    }

    debug!(groups = groups.len(), "flat grouping complete");

    groups
}

/// Group files by a specific key dimension (directory or language only).
///
/// This is a helper for more fine-grained grouping when the user only wants
/// one dimension of coalescing.
pub fn group_by_dimension(
    files: &[FileEntry],
    config: &BundlingConfig,
    dimension: GroupingDimension,
) -> Vec<FileGroup> {
    let max_chars = config.max_chars_per_group;
    let max_files = config.max_files_per_group;

    // Bucket files by the chosen dimension
    let mut buckets: HashMap<String, Vec<&FileEntry>> = HashMap::new();

    for file in files {
        let bucket_key = match dimension {
            GroupingDimension::Directory => file
                .path
                .rsplit_once('/')
                .or_else(|| file.path.rsplit_once('\\'))
                .map(|(dir, _)| dir.to_string())
                .unwrap_or_else(|| ".".to_string()),
            GroupingDimension::Language => file
                .path
                .rsplit_once('.')
                .map(|(_, ext)| LanguageFamily::from_extension(ext).to_string())
                .unwrap_or_else(|| "other".to_string()),
        };
        buckets.entry(bucket_key).or_default().push(file);
    }

    // Sort bucket keys for deterministic output
    let mut bucket_keys: Vec<String> = buckets.keys().cloned().collect();
    bucket_keys.sort();

    // Pack each bucket into groups respecting limits
    let mut groups: Vec<FileGroup> = Vec::new();

    for key in bucket_keys {
        let bucket_files = &buckets[&key];
        let mut current_group = FileGroup::new();

        for file in bucket_files {
            if !current_group.would_fit(file, max_chars, max_files) && !current_group.is_empty() {
                groups.push(std::mem::replace(&mut current_group, FileGroup::new()));
            }
            current_group.push((*file).clone());
        }

        if !current_group.is_empty() {
            groups.push(current_group);
        }
    }

    groups
}

/// Dimension to group files by.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GroupingDimension {
    /// Group by parent directory.
    Directory,
    /// Group by language family.
    Language,
}

/// Merge small groups that could fit together.
///
/// After initial grouping, some groups may be very small (1-2 files).
/// This post-processing step merges adjacent small groups if their
/// combined size stays within limits.
pub fn merge_small_groups(
    mut groups: Vec<FileGroup>,
    config: &BundlingConfig,
    min_group_size: usize,
) -> Vec<FileGroup> {
    if groups.len() <= 1 {
        return groups;
    }

    let max_chars = config.max_chars_per_group;
    let max_files = config.max_files_per_group;

    let mut merged: Vec<FileGroup> = Vec::new();
    let mut accumulator = groups.remove(0);

    for group in groups {
        // Merge if both groups are small and combined fits
        if accumulator.len() < min_group_size
            && group.len() < min_group_size
            && accumulator.len() + group.len() <= max_files
            && accumulator.total_chars + group.total_chars <= max_chars
        {
            for file in group.files {
                accumulator.push(file);
            }
        } else {
            merged.push(std::mem::replace(&mut accumulator, group));
        }
    }

    if !accumulator.is_empty() {
        merged.push(accumulator);
    }

    merged
}

/// Collect statistics about the grouping result.
#[derive(Debug, Clone)]
pub struct GroupingStats {
    /// Total number of groups.
    pub group_count: usize,
    /// Total number of files across all groups.
    pub total_files: usize,
    /// Total characters across all groups.
    pub total_chars: usize,
    /// Average files per group.
    pub avg_files_per_group: f64,
    /// Average characters per group.
    pub avg_chars_per_group: f64,
    /// Number of groups with only 1 file.
    pub single_file_groups: usize,
}

impl GroupingStats {
    /// Compute stats from a list of groups.
    pub fn from_groups(groups: &[FileGroup]) -> Self {
        let group_count = groups.len();
        let total_files: usize = groups.iter().map(|g| g.len()).sum();
        let total_chars: usize = groups.iter().map(|g| g.total_chars).sum();
        let single_file_groups = groups.iter().filter(|g| g.len() == 1).count();

        Self {
            group_count,
            total_files,
            total_chars,
            avg_files_per_group: if group_count > 0 {
                total_files as f64 / group_count as f64
            } else {
                0.0
            },
            avg_chars_per_group: if group_count > 0 {
                total_chars as f64 / group_count as f64
            } else {
                0.0
            },
            single_file_groups,
        }
    }
}

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

    fn make_file(path: &str, content_len: usize) -> FileEntry {
        FileEntry {
            path: path.to_string(),
            content: "x".repeat(content_len),
            lines: content_len,
        }
    }

    fn default_config() -> BundlingConfig {
        BundlingConfig::default()
    }

    // ─── group_files dispatch ───

    #[test]
    fn group_files_empty() {
        let groups = group_files(&[], &default_config());
        assert!(groups.is_empty());
    }

    // ─── Flat grouping ───

    #[test]
    fn flat_grouping_basic() {
        let files = vec![
            make_file("a.rs", 100),
            make_file("b.rs", 100),
            make_file("c.rs", 100),
        ];
        let config = BundlingConfig {
            max_chars_per_group: 250, // fits ~2 files per group
            max_files_per_group: 100,
            strategy: GroupingStrategy::Flat,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        assert!(groups.len() >= 2, "should split into at least 2 groups");
        assert_eq!(
            groups.iter().map(|g| g.len()).sum::<usize>(),
            3,
            "all files should be assigned"
        );
    }

    #[test]
    fn flat_grouping_all_fit() {
        let files = vec![make_file("a.rs", 10), make_file("b.rs", 10)];
        let config = BundlingConfig {
            strategy: GroupingStrategy::Flat,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].len(), 2);
    }

    #[test]
    fn flat_grouping_preserves_order() {
        let files = vec![
            make_file("a.rs", 10),
            make_file("b.rs", 10),
            make_file("c.rs", 10),
        ];
        let config = BundlingConfig {
            strategy: GroupingStrategy::Flat,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].files[0].path, "a.rs");
        assert_eq!(groups[0].files[1].path, "b.rs");
        assert_eq!(groups[0].files[2].path, "c.rs");
    }

    #[test]
    fn flat_grouping_max_files_limit() {
        let files = vec![
            make_file("a.rs", 10),
            make_file("b.rs", 10),
            make_file("c.rs", 10),
            make_file("d.rs", 10),
        ];
        let config = BundlingConfig {
            strategy: GroupingStrategy::Flat,
            max_files_per_group: 2,
            max_chars_per_group: 1_000_000,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        assert_eq!(groups.len(), 2);
        assert_eq!(groups[0].len(), 2);
        assert_eq!(groups[1].len(), 2);
    }

    // ─── Smart grouping ───

    #[test]
    fn smart_grouping_clusters_same_directory() {
        let files = vec![
            make_file("src/engine/mod.rs", 100),
            make_file("src/engine/types.rs", 100),
            make_file("src/config/mod.rs", 100),
            make_file("tests/integration.rs", 100),
        ];
        let config = BundlingConfig {
            strategy: GroupingStrategy::Smart,
            coalesce_by_directory: true,
            coalesce_by_language: false,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        // Should cluster src/engine files together and src/config + tests may be separate
        assert_eq!(
            groups.iter().map(|g| g.len()).sum::<usize>(),
            4,
            "all files assigned"
        );
    }

    #[test]
    fn smart_grouping_clusters_same_language() {
        let files = vec![
            make_file("src/main.rs", 100),
            make_file("lib/api.py", 100),
            make_file("src/lib.rs", 100),
            make_file("scripts/deploy.py", 100),
        ];
        let config = BundlingConfig {
            strategy: GroupingStrategy::Smart,
            coalesce_by_directory: false,
            coalesce_by_language: true,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        // Rust files should cluster together, Python files together
        assert_eq!(
            groups.iter().map(|g| g.len()).sum::<usize>(),
            4,
            "all files assigned"
        );
    }

    #[test]
    fn smart_grouping_respects_char_limit() {
        let files = vec![
            make_file("src/a.rs", 10_000),
            make_file("src/b.rs", 10_000),
            make_file("src/c.rs", 10_000),
        ];
        let config = BundlingConfig {
            strategy: GroupingStrategy::Smart,
            max_chars_per_group: 15_000,
            max_files_per_group: 100,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        assert!(groups.len() >= 2, "should split into at least 2 groups");
    }

    // ─── merge_small_groups ───

    #[test]
    fn merge_small_groups_basic() {
        let g1 = {
            let mut g = FileGroup::new();
            g.push(make_file("a.rs", 10));
            g
        };
        let g2 = {
            let mut g = FileGroup::new();
            g.push(make_file("b.rs", 10));
            g
        };
        let g3 = {
            let mut g = FileGroup::new();
            g.push(make_file("c.rs", 10));
            g
        };

        let merged = merge_small_groups(vec![g1, g2, g3], &default_config(), 2);
        // All groups have 1 file (< min_group_size=2), so they should be merged
        assert!(merged.len() <= 2, "should merge small groups");
        assert_eq!(
            merged.iter().map(|g| g.len()).sum::<usize>(),
            3,
            "no files lost"
        );
    }

    #[test]
    fn merge_small_groups_respects_limits() {
        let g1 = {
            let mut g = FileGroup::new();
            g.push(make_file("a.rs", 30_000));
            g
        };
        let g2 = {
            let mut g = FileGroup::new();
            g.push(make_file("b.rs", 30_000));
            g
        };

        let config = BundlingConfig {
            max_chars_per_group: 50_000,
            ..Default::default()
        };
        let merged = merge_small_groups(vec![g1, g2], &config, 2);
        // 30k + 30k = 60k > 50k, should not merge
        assert_eq!(merged.len(), 2);
    }

    // ─── group_by_dimension ───

    #[test]
    fn group_by_directory() {
        let files = vec![
            make_file("src/engine/mod.rs", 10),
            make_file("src/engine/types.rs", 10),
            make_file("src/config/mod.rs", 10),
        ];
        let groups = group_by_dimension(&files, &default_config(), GroupingDimension::Directory);
        // Should have at least 2 groups: src/engine and src/config
        assert!(groups.len() >= 2);
    }

    #[test]
    fn group_by_language() {
        let files = vec![
            make_file("src/main.rs", 10),
            make_file("src/app.py", 10),
            make_file("src/lib.rs", 10),
        ];
        let groups = group_by_dimension(&files, &default_config(), GroupingDimension::Language);
        // Should have at least 2 groups: Rust and Python
        assert!(groups.len() >= 2);
    }

    // ─── GroupingStats ───

    #[test]
    fn grouping_stats_empty() {
        let stats = GroupingStats::from_groups(&[]);
        assert_eq!(stats.group_count, 0);
        assert_eq!(stats.total_files, 0);
        assert_eq!(stats.total_chars, 0);
        assert_eq!(stats.single_file_groups, 0);
    }

    #[test]
    fn grouping_stats_computed() {
        let g1 = {
            let mut g = FileGroup::new();
            g.push(make_file("a.rs", 100));
            g.push(make_file("b.rs", 100));
            g
        };
        let g2 = {
            let mut g = FileGroup::new();
            g.push(make_file("c.rs", 50));
            g
        };

        let stats = GroupingStats::from_groups(&[g1, g2]);
        assert_eq!(stats.group_count, 2);
        assert_eq!(stats.total_files, 3);
        assert_eq!(stats.single_file_groups, 1);
        assert!((stats.avg_files_per_group - 1.5).abs() < 0.01);
    }

    // ─── Smart grouping with both coalesce off ───

    #[test]
    fn smart_grouping_no_coalesce_is_like_flat() {
        let files = vec![
            make_file("z.rs", 10),
            make_file("a.rs", 10),
            make_file("m.rs", 10),
        ];
        let config = BundlingConfig {
            strategy: GroupingStrategy::Smart,
            coalesce_by_directory: false,
            coalesce_by_language: false,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        assert_eq!(groups.len(), 1);
        // Without coalescing, files are sorted by path
        assert_eq!(groups[0].files[0].path, "a.rs");
        assert_eq!(groups[0].files[1].path, "m.rs");
        assert_eq!(groups[0].files[2].path, "z.rs");
    }

    // ─── Single large file ───

    #[test]
    fn single_large_file_gets_own_group() {
        let files = vec![make_file("huge.rs", 200_000)];
        let config = BundlingConfig {
            strategy: GroupingStrategy::Smart,
            max_chars_per_group: 50_000,
            max_files_per_group: 10,
            ..Default::default()
        };

        let groups = group_files(&files, &config);
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].len(), 1);
    }
}