pulpod 0.0.41

Pulpo daemon — manages agent sessions via tmux/Docker
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
/// Extract a PR/MR URL from terminal output.
///
/// Checks for:
/// - GitHub PR URLs: `https://github.com/{owner}/{repo}/pull/{number}`
/// - GitLab MR URLs: `https://gitlab.com/{path}/-/merge_requests/{number}`
/// - Bitbucket PR URLs: `https://bitbucket.org/{owner}/{repo}/pull-requests/{number}`
///
/// Returns the first match found, or `None`.
pub fn extract_pr_url(output: &str) -> Option<String> {
    // Strip ANSI escape codes for reliable matching
    let cleaned = strip_ansi(output);

    for line in cleaned.lines() {
        // GitHub PR
        if let Some(url) = find_url_with_path(line, "github.com", "/pull/") {
            return Some(url);
        }
        // GitLab MR
        if let Some(url) = find_url_with_path(line, "gitlab.com", "/-/merge_requests/") {
            return Some(url);
        }
        // Bitbucket PR
        if let Some(url) = find_url_with_path(line, "bitbucket.org", "/pull-requests/") {
            return Some(url);
        }
    }
    None
}

/// Extract a branch name from git push output.
///
/// Checks for:
/// - `remote: Create a pull request for 'branch-name'` (GitHub push output)
/// - `* [new branch]      branch-name -> branch-name` (git push output)
/// - `Branch 'branch-name' set up to track` (git push output)
///
/// Returns the first match found, or `None`.
pub fn extract_branch(output: &str) -> Option<String> {
    let cleaned = strip_ansi(output);

    for line in cleaned.lines() {
        let trimmed = line.trim();

        // Pattern: remote: Create a pull request for 'branch-name'
        if let Some(rest) = trimmed.strip_prefix("remote:") {
            let rest = rest.trim();
            let needle = "Create a pull request for '";
            if let Some(after) = rest.find(needle).map(|i| &rest[i + needle.len()..])
                && let Some(end) = after.find('\'')
            {
                let branch = &after[..end];
                if !branch.is_empty() {
                    return Some(branch.to_owned());
                }
            }
        }

        // Pattern: * [new branch]      branch-name -> branch-name
        if let Some(rest) = trimmed.strip_prefix("* [new branch]") {
            let rest = rest.trim();
            if let Some(arrow) = rest.find("->") {
                let branch = rest[..arrow].trim();
                if !branch.is_empty() {
                    return Some(branch.to_owned());
                }
            }
        }

        // Pattern: Branch 'branch-name' set up to track
        let needle = "Branch '";
        if let Some(after) = trimmed.find(needle).map(|i| &trimmed[i + needle.len()..])
            && let Some(end) = after.find('\'')
        {
            let rest_after_quote = &after[end..];
            if rest_after_quote.contains("set up to track") {
                let branch = &after[..end];
                if !branch.is_empty() {
                    return Some(branch.to_owned());
                }
            }
        }
    }
    None
}

/// Strip ANSI escape codes from a string.
fn strip_ansi(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let mut chars = input.chars();
    while let Some(ch) = chars.next() {
        if ch == '\x1b' {
            // Skip ESC [ ... final_byte sequence
            if let Some(next) = chars.next()
                && next == '['
            {
                // CSI sequence: consume until 0x40-0x7E
                for c in chars.by_ref() {
                    if ('\x40'..='\x7e').contains(&c) {
                        break;
                    }
                }
            }
            // else: skip the single char after ESC (e.g., ESC])
        } else {
            result.push(ch);
        }
    }
    result
}

/// Find a URL in a line that contains the given host and path segment.
/// Extracts from `https://` up to the first whitespace or end of line.
fn find_url_with_path(line: &str, host: &str, path_segment: &str) -> Option<String> {
    let prefix = format!("https://{host}");
    let mut search_from = 0;
    while search_from < line.len() {
        let haystack = &line[search_from..];
        let Some(start) = haystack.find(&prefix) else {
            break;
        };
        let abs_start = search_from + start;
        // Find end of URL (whitespace, end of line, or common delimiters)
        let url_start = &line[abs_start..];
        let end = url_start
            .find(|c: char| c.is_whitespace() || c == '"' || c == '>' || c == ')' || c == ']')
            .unwrap_or(url_start.len());
        let url = &url_start[..end];
        if url.contains(path_segment) {
            return Some(url.to_owned());
        }
        search_from = abs_start + end;
    }
    None
}

/// Patterns that indicate rate limiting in agent output.
/// Each entry is (pattern, human-readable label).
const RATE_LIMIT_PATTERNS: &[(&str, &str)] = &[
    ("rate limit", "Rate limited"),
    ("too many requests", "Rate limited: too many requests"),
    ("429", "Rate limited (429)"),
    ("capacity", "Rate limited: at capacity"),
    ("overloaded", "Rate limited: overloaded"),
    ("quota exceeded", "Rate limited: quota exceeded"),
    ("resource_exhausted", "Rate limited: resource exhausted"),
];

/// Detect rate limit warnings in agent output.
/// Returns a human-readable message if rate limiting is detected.
///
/// Checks the last 20 lines of output (after ANSI stripping) for known
/// rate limit patterns. Returns the first match found.
pub fn detect_rate_limit(output: &str) -> Option<String> {
    let cleaned = strip_ansi(output);
    let last_lines: Vec<&str> = cleaned.lines().rev().take(20).collect();

    for line in &last_lines {
        let lower = line.to_lowercase();
        for &(pattern, label) in RATE_LIMIT_PATTERNS {
            if lower.contains(pattern) {
                return Some(label.to_owned());
            }
        }
    }
    None
}

/// Patterns that indicate errors/failures in agent output.
/// Each entry is (pattern, human-readable label).
const ERROR_PATTERNS: &[(&str, &str)] = &[
    ("error[e", "Compile error"),
    ("panicked at", "Panic"),
    ("npm err!", "npm error"),
    ("typeerror:", "TypeError"),
    ("syntaxerror:", "SyntaxError"),
    ("referenceerror:", "ReferenceError"),
    ("build failed", "Build failed"),
    ("fatal:", "Fatal error"),
];

/// Detect compilation errors, test failures, and panics in agent output.
/// Checks the last 30 lines for known error patterns.
/// Returns a short label like "Compile error", "Test failed", etc.
pub fn detect_error(output: &str) -> Option<String> {
    let cleaned = strip_ansi(output);
    let last_lines: Vec<&str> = cleaned.lines().rev().take(30).collect();

    for line in &last_lines {
        let lower = line.to_lowercase();

        // Check for test failures with specific patterns (line-start for `error:`)
        let trimmed = lower.trim_start();
        if trimmed.starts_with("error:") {
            return Some("Compile error".to_owned());
        }

        // FAILED at line start or as standalone word for test failures
        if trimmed.starts_with("failed") || trimmed.contains("test failed") {
            return Some("Test failed".to_owned());
        }

        // FAIL with space (vitest/jest)
        if trimmed.starts_with("fail ") {
            return Some("Test failed".to_owned());
        }

        for &(pattern, label) in ERROR_PATTERNS {
            if lower.contains(pattern) {
                return Some(label.to_owned());
            }
        }
    }
    None
}

/// Extract token usage from agent output.
/// Looks for patterns in the last 50 lines.
/// Returns `(input_tokens, output_tokens)` if found.
pub fn extract_token_usage(output: &str) -> Option<(u64, u64)> {
    let cleaned = strip_ansi(output);
    let last_lines: Vec<&str> = cleaned.lines().rev().take(50).collect();

    let mut input_tokens: Option<u64> = None;
    let mut output_tokens: Option<u64> = None;
    let mut total_tokens: Option<u64> = None;

    for line in &last_lines {
        let lower = line.to_lowercase();

        // Try to extract input/output tokens
        if input_tokens.is_none() {
            if let Some(n) = extract_number_after(&lower, "input tokens:") {
                input_tokens = Some(n);
            } else if let Some(n) = extract_number_after(&lower, "input_tokens:") {
                input_tokens = Some(n);
            }
        }
        if output_tokens.is_none() {
            if let Some(n) = extract_number_after(&lower, "output tokens:") {
                output_tokens = Some(n);
            } else if let Some(n) = extract_number_after(&lower, "output_tokens:") {
                output_tokens = Some(n);
            }
        }
        if total_tokens.is_none() {
            if let Some(n) = extract_number_after(&lower, "total tokens:") {
                total_tokens = Some(n);
            } else if let Some(n) = extract_number_after(&lower, "total_tokens:") {
                total_tokens = Some(n);
            } else if let Some(n) = extract_number_after(&lower, "tokens used:") {
                total_tokens = Some(n);
            }
        }
    }

    // If we have input and output, return those
    if let (Some(inp), Some(out)) = (input_tokens, output_tokens) {
        return Some((inp, out));
    }

    // If we have total tokens, split roughly (we can't know the split, use total as input)
    if let Some(total) = total_tokens {
        return Some((total, 0));
    }

    None
}

/// Extract a number that follows a keyword in a line.
/// Handles comma-separated numbers like "12,345".
fn extract_number_after(line: &str, keyword: &str) -> Option<u64> {
    let idx = line.find(keyword)?;
    let rest = &line[idx + keyword.len()..];
    // Skip whitespace and find the number
    let trimmed = rest.trim_start();
    // Collect digits and commas
    let num_str: String = trimmed
        .chars()
        .take_while(|c| c.is_ascii_digit() || *c == ',')
        .filter(char::is_ascii_digit)
        .collect();
    if num_str.is_empty() {
        return None;
    }
    num_str.parse().ok()
}

/// Parse `git diff --shortstat` output into (files, insertions, deletions).
/// Input looks like: ` 3 files changed, 42 insertions(+), 7 deletions(-)`
/// Any of the three parts may be missing.
pub fn parse_git_shortstat(output: &str) -> (Option<u32>, Option<u32>, Option<u32>) {
    let trimmed = output.trim();
    if trimmed.is_empty() {
        return (None, None, None);
    }

    let mut files: Option<u32> = None;
    let mut insertions: Option<u32> = None;
    let mut deletions: Option<u32> = None;

    for part in trimmed.split(',') {
        let part = part.trim();
        if part.contains("file") {
            files = extract_leading_number(part);
        } else if part.contains("insertion") {
            insertions = extract_leading_number(part);
        } else if part.contains("deletion") {
            deletions = extract_leading_number(part);
        }
    }

    (files, insertions, deletions)
}

/// Extract the first number from a string.
fn extract_leading_number(s: &str) -> Option<u32> {
    let num_str: String = s
        .chars()
        .skip_while(|c| !c.is_ascii_digit())
        .take_while(char::is_ascii_digit)
        .collect();
    if num_str.is_empty() {
        return None;
    }
    num_str.parse().ok()
}

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

    // -- extract_pr_url tests --

    #[test]
    fn test_extract_github_pr_url() {
        let output = "remote: \nremote: Create a pull request:\nremote:   https://github.com/owner/repo/pull/42\nremote: \n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/owner/repo/pull/42".into())
        );
    }

    #[test]
    fn test_extract_gitlab_mr_url() {
        let output = "View merge request: https://gitlab.com/group/project/-/merge_requests/123\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://gitlab.com/group/project/-/merge_requests/123".into())
        );
    }

    #[test]
    fn test_extract_bitbucket_pr_url() {
        let output = "Create pull request: https://bitbucket.org/owner/repo/pull-requests/99\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://bitbucket.org/owner/repo/pull-requests/99".into())
        );
    }

    #[test]
    fn test_extract_pr_url_no_match() {
        let output = "$ cargo test\nrunning 42 tests\nall passed\n";
        assert_eq!(extract_pr_url(output), None);
    }

    #[test]
    fn test_extract_pr_url_multiple_returns_first() {
        let output =
            "First: https://github.com/a/b/pull/1\nSecond: https://github.com/c/d/pull/2\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/a/b/pull/1".into())
        );
    }

    #[test]
    fn test_extract_pr_url_with_ansi_codes() {
        let output = "\x1b[32mhttps://github.com/owner/repo/pull/42\x1b[0m\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/owner/repo/pull/42".into())
        );
    }

    #[test]
    fn test_extract_pr_url_github_not_pr_path() {
        let output = "See https://github.com/owner/repo/issues/42\n";
        assert_eq!(extract_pr_url(output), None);
    }

    #[test]
    fn test_extract_pr_url_gh_create_output() {
        // Real `gh pr create` output
        let output = "Creating pull request for feature-branch into main in owner/repo\n\nhttps://github.com/owner/repo/pull/7\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/owner/repo/pull/7".into())
        );
    }

    #[test]
    fn test_extract_pr_url_nested_gitlab_path() {
        let output = "https://gitlab.com/group/sub/project/-/merge_requests/5\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://gitlab.com/group/sub/project/-/merge_requests/5".into())
        );
    }

    // -- extract_branch tests --

    #[test]
    fn test_extract_branch_github_remote_message() {
        let output = "remote: \nremote: Create a pull request for 'feature/my-branch' on GitHub by visiting:\nremote:   https://github.com/owner/repo/pull/new/feature/my-branch\nremote: \n";
        assert_eq!(extract_branch(output), Some("feature/my-branch".into()));
    }

    #[test]
    fn test_extract_branch_new_branch_output() {
        let output = "To github.com:owner/repo.git\n * [new branch]      my-branch -> my-branch\n";
        assert_eq!(extract_branch(output), Some("my-branch".into()));
    }

    #[test]
    fn test_extract_branch_set_up_to_track() {
        let output = "Branch 'fix-bug' set up to track remote branch 'fix-bug' from 'origin'.\n";
        assert_eq!(extract_branch(output), Some("fix-bug".into()));
    }

    #[test]
    fn test_extract_branch_no_match() {
        let output = "$ cargo build\nCompiling pulpo v0.1.0\nFinished dev\n";
        assert_eq!(extract_branch(output), None);
    }

    #[test]
    fn test_extract_branch_with_ansi_codes() {
        let output = "\x1b[33mremote: Create a pull request for 'ansi-branch' on GitHub\x1b[0m\n";
        assert_eq!(extract_branch(output), Some("ansi-branch".into()));
    }

    #[test]
    fn test_extract_branch_multiple_returns_first() {
        let output = " * [new branch]      first-branch -> first-branch\nBranch 'second-branch' set up to track remote.\n";
        assert_eq!(extract_branch(output), Some("first-branch".into()));
    }

    // -- strip_ansi tests --

    #[test]
    fn test_strip_ansi_empty() {
        assert_eq!(strip_ansi(""), "");
    }

    #[test]
    fn test_strip_ansi_no_codes() {
        assert_eq!(strip_ansi("hello world"), "hello world");
    }

    #[test]
    fn test_strip_ansi_basic() {
        assert_eq!(strip_ansi("\x1b[32mgreen\x1b[0m"), "green");
    }

    #[test]
    fn test_strip_ansi_multiple() {
        assert_eq!(strip_ansi("\x1b[1m\x1b[31mred bold\x1b[0m"), "red bold");
    }

    // -- find_url_with_path tests --

    #[test]
    fn test_find_url_basic() {
        let url = find_url_with_path(
            "Visit https://github.com/a/b/pull/1 now",
            "github.com",
            "/pull/",
        );
        assert_eq!(url, Some("https://github.com/a/b/pull/1".into()));
    }

    #[test]
    fn test_find_url_no_match() {
        let url = find_url_with_path("https://github.com/a/b/issues/1", "github.com", "/pull/");
        assert_eq!(url, None);
    }

    #[test]
    fn test_find_url_at_end_of_line() {
        let url = find_url_with_path("https://github.com/a/b/pull/99", "github.com", "/pull/");
        assert_eq!(url, Some("https://github.com/a/b/pull/99".into()));
    }

    #[test]
    fn test_find_url_with_trailing_delimiter() {
        let url = find_url_with_path(
            "PR: \"https://github.com/a/b/pull/5\" done",
            "github.com",
            "/pull/",
        );
        assert_eq!(url, Some("https://github.com/a/b/pull/5".into()));
    }

    // -- detect_rate_limit tests --

    #[test]
    fn test_detect_rate_limit_basic() {
        let output = "Processing...\nError: Rate limit exceeded. Please wait.\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited".into()));
    }

    #[test]
    fn test_detect_rate_limit_429() {
        let output = "HTTP 429: please retry later\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited (429)".into()));
    }

    #[test]
    fn test_detect_rate_limit_too_many_requests() {
        let output = "Error: too many requests, please slow down\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited: too many requests".into()));
    }

    #[test]
    fn test_detect_rate_limit_capacity() {
        let output = "The API is at capacity right now.\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited: at capacity".into()));
    }

    #[test]
    fn test_detect_rate_limit_overloaded() {
        let output = "Service is overloaded. Retrying in 30s.\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited: overloaded".into()));
    }

    #[test]
    fn test_detect_rate_limit_quota_exceeded() {
        let output = "Error: Quota exceeded for project.\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited: quota exceeded".into()));
    }

    #[test]
    fn test_detect_rate_limit_resource_exhausted() {
        let output = "RESOURCE_EXHAUSTED: API limit reached.\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited: resource exhausted".into()));
    }

    #[test]
    fn test_detect_rate_limit_no_match() {
        let output = "$ cargo test\nrunning 42 tests\nall passed\n";
        assert!(detect_rate_limit(output).is_none());
    }

    #[test]
    fn test_detect_rate_limit_with_ansi() {
        let output = "\x1b[31mRate limit exceeded\x1b[0m\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited".into()));
    }

    #[test]
    fn test_detect_rate_limit_case_insensitive() {
        let output = "RATE LIMIT warning: slow down\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited".into()));
    }

    #[test]
    fn test_detect_rate_limit_only_last_20_lines() {
        // Put the rate limit message beyond the last 20 lines
        let mut output = String::from("Rate limit exceeded\n");
        for _ in 0..25 {
            output.push_str("normal output line\n");
        }
        assert!(detect_rate_limit(&output).is_none());
    }

    #[test]
    fn test_detect_rate_limit_empty() {
        assert!(detect_rate_limit("").is_none());
    }

    // -- PR URL edge cases --

    #[test]
    fn test_extract_pr_url_with_query_params() {
        let output = "https://github.com/owner/repo/pull/42?expand=1\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/owner/repo/pull/42?expand=1".into())
        );
    }

    #[test]
    fn test_extract_pr_url_with_fragment() {
        let output = "See https://github.com/owner/repo/pull/42#issuecomment-123\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/owner/repo/pull/42#issuecomment-123".into())
        );
    }

    #[test]
    fn test_extract_pr_url_surrounded_by_ansi_heavy() {
        // Multiple nested ANSI codes
        let output =
            "\x1b[1m\x1b[33m\x1b[4mhttps://github.com/owner/repo/pull/7\x1b[0m\x1b[0m\x1b[0m\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/owner/repo/pull/7".into())
        );
    }

    #[test]
    fn test_extract_pr_url_in_angle_brackets() {
        // Some terminals/tools wrap URLs in angle brackets
        let output = "PR created: <https://github.com/owner/repo/pull/10>\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/owner/repo/pull/10".into())
        );
    }

    #[test]
    fn test_extract_pr_url_in_parentheses() {
        let output = "See PR (https://github.com/owner/repo/pull/3)\n";
        assert_eq!(
            extract_pr_url(output),
            Some("https://github.com/owner/repo/pull/3".into())
        );
    }

    #[test]
    fn test_extract_pr_url_empty() {
        assert_eq!(extract_pr_url(""), None);
    }

    // -- Branch edge cases --

    #[test]
    fn test_extract_branch_with_deep_slashes() {
        let output = "remote: Create a pull request for 'feature/deep/nested/branch' on GitHub:\n";
        assert_eq!(
            extract_branch(output),
            Some("feature/deep/nested/branch".into())
        );
    }

    #[test]
    fn test_extract_branch_empty() {
        assert_eq!(extract_branch(""), None);
    }

    #[test]
    fn test_extract_branch_empty_quotes() {
        // Edge case: empty branch name in quotes
        let output = "remote: Create a pull request for '' on GitHub:\n";
        assert_eq!(extract_branch(output), None);
    }

    #[test]
    fn test_extract_branch_set_up_to_track_with_slashes() {
        let output =
            "Branch 'feature/auth/oauth2' set up to track remote branch 'feature/auth/oauth2'.\n";
        assert_eq!(extract_branch(output), Some("feature/auth/oauth2".into()));
    }

    // -- Rate limit edge cases --

    #[test]
    fn test_detect_rate_limit_429_in_url_is_false_positive() {
        // Known limitation: "429" in a URL will trigger a false positive
        let output = "See https://github.com/owner/repo/issues/429 for details\n";
        // This IS detected as rate limit — documenting the false positive
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited (429)".into()));
    }

    #[test]
    fn test_detect_rate_limit_mixed_case() {
        let output = "Too Many Requests - slow down\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited: too many requests".into()));
    }

    #[test]
    fn test_detect_rate_limit_overloaded_uppercase() {
        let output = "SERVICE OVERLOADED\n";
        let result = detect_rate_limit(output);
        assert_eq!(result, Some("Rate limited: overloaded".into()));
    }

    // -- strip_ansi edge cases --

    #[test]
    fn test_strip_ansi_non_csi_sequence() {
        // ESC followed by non-'[' character (like OSC: ESC])
        // The code skips ESC and the next char, then passes through the rest
        let input = "\x1b]some title\x07normal text";
        let result = strip_ansi(input);
        // ESC consumed, then next char ']' consumed (non-'[' path),
        // remaining "some title\x07normal text" passes through
        assert_eq!(result, "some title\x07normal text");
    }

    #[test]
    fn test_strip_ansi_esc_at_end() {
        // ESC at the very end of string
        let input = "hello\x1b";
        let result = strip_ansi(input);
        // ESC consumed, then chars.next() returns None, so nothing more consumed
        assert_eq!(result, "hello");
    }

    // -- find_url_with_path edge cases --

    #[test]
    fn test_find_url_multiple_urls_on_same_line() {
        let line = "Old: https://github.com/a/b/issues/1 New: https://github.com/a/b/pull/2";
        let url = find_url_with_path(line, "github.com", "/pull/");
        assert_eq!(url, Some("https://github.com/a/b/pull/2".into()));
    }

    #[test]
    fn test_find_url_in_square_brackets() {
        let line = "[https://github.com/a/b/pull/3]";
        let url = find_url_with_path(line, "github.com", "/pull/");
        assert_eq!(url, Some("https://github.com/a/b/pull/3".into()));
    }

    // -- detect_error tests --

    #[test]
    fn test_detect_error_rust_compiler() {
        let output = "Compiling my-crate v0.1.0\nerror[E0308]: mismatched types\n";
        assert_eq!(detect_error(output), Some("Compile error".into()));
    }

    #[test]
    fn test_detect_error_error_at_line_start() {
        let output = "running build\nerror: could not compile `foo`\n";
        assert_eq!(detect_error(output), Some("Compile error".into()));
    }

    #[test]
    fn test_detect_error_test_failed() {
        let output = "running 5 tests\nFAILED tests/integration.rs\n";
        assert_eq!(detect_error(output), Some("Test failed".into()));
    }

    #[test]
    fn test_detect_error_panic() {
        let output = "thread 'main' panicked at 'index out of bounds'\n";
        assert_eq!(detect_error(output), Some("Panic".into()));
    }

    #[test]
    fn test_detect_error_npm_err() {
        let output = "npm ERR! code ELIFECYCLE\nnpm ERR! errno 1\n";
        assert_eq!(detect_error(output), Some("npm error".into()));
    }

    #[test]
    fn test_detect_error_typeerror() {
        let output = "TypeError: Cannot read property 'foo' of undefined\n";
        assert_eq!(detect_error(output), Some("TypeError".into()));
    }

    #[test]
    fn test_detect_error_syntaxerror() {
        let output = "SyntaxError: Unexpected token }\n";
        assert_eq!(detect_error(output), Some("SyntaxError".into()));
    }

    #[test]
    fn test_detect_error_referenceerror() {
        let output = "ReferenceError: foo is not defined\n";
        assert_eq!(detect_error(output), Some("ReferenceError".into()));
    }

    #[test]
    fn test_detect_error_build_failed() {
        let output = "Build failed with 2 errors\n";
        assert_eq!(detect_error(output), Some("Build failed".into()));
    }

    #[test]
    fn test_detect_error_git_fatal() {
        let output = "fatal: not a git repository\n";
        assert_eq!(detect_error(output), Some("Fatal error".into()));
    }

    #[test]
    fn test_detect_error_vitest_fail() {
        let output = " FAIL  src/utils.test.ts > should work\n";
        assert_eq!(detect_error(output), Some("Test failed".into()));
    }

    #[test]
    fn test_detect_error_no_match() {
        let output = "$ cargo build\nCompiling my-crate v0.1.0\nFinished dev\n";
        assert!(detect_error(output).is_none());
    }

    #[test]
    fn test_detect_error_only_last_30_lines() {
        let mut output = String::from("error[E0308]: old error\n");
        for _ in 0..35 {
            output.push_str("normal output line\n");
        }
        assert!(detect_error(&output).is_none());
    }

    #[test]
    fn test_detect_error_with_ansi() {
        let output = "\x1b[31merror[E0308]: mismatched types\x1b[0m\n";
        assert_eq!(detect_error(output), Some("Compile error".into()));
    }

    #[test]
    fn test_detect_error_empty() {
        assert!(detect_error("").is_none());
    }

    #[test]
    fn test_detect_error_test_failed_keyword() {
        let output = "1 test failed out of 42\n";
        assert_eq!(detect_error(output), Some("Test failed".into()));
    }

    // -- extract_token_usage tests --

    #[test]
    fn test_extract_token_usage_input_output() {
        let output = "Input tokens: 1234\nOutput tokens: 5678\n";
        assert_eq!(extract_token_usage(output), Some((1234, 5678)));
    }

    #[test]
    fn test_extract_token_usage_underscore_format() {
        let output = "input_tokens: 1000\noutput_tokens: 2000\n";
        assert_eq!(extract_token_usage(output), Some((1000, 2000)));
    }

    #[test]
    fn test_extract_token_usage_total_only() {
        let output = "Total tokens: 12345\n";
        assert_eq!(extract_token_usage(output), Some((12345, 0)));
    }

    #[test]
    fn test_extract_token_usage_total_underscore() {
        let output = "total_tokens: 9999\n";
        assert_eq!(extract_token_usage(output), Some((9999, 0)));
    }

    #[test]
    fn test_extract_token_usage_tokens_used() {
        let output = "tokens used: 5000\n";
        assert_eq!(extract_token_usage(output), Some((5000, 0)));
    }

    #[test]
    fn test_extract_token_usage_with_commas() {
        let output = "Input tokens: 12,345\nOutput tokens: 67,890\n";
        assert_eq!(extract_token_usage(output), Some((12345, 67890)));
    }

    #[test]
    fn test_extract_token_usage_no_match() {
        let output = "$ cargo build\nCompiling...\nDone.\n";
        assert!(extract_token_usage(output).is_none());
    }

    #[test]
    fn test_extract_token_usage_empty() {
        assert!(extract_token_usage("").is_none());
    }

    #[test]
    fn test_extract_token_usage_only_last_50_lines() {
        let mut output = String::from("Total tokens: 9999\n");
        for _ in 0..55 {
            output.push_str("normal line\n");
        }
        assert!(extract_token_usage(&output).is_none());
    }

    #[test]
    fn test_extract_token_usage_with_ansi() {
        let output = "\x1b[33mInput tokens: 100\x1b[0m\n\x1b[33mOutput tokens: 200\x1b[0m\n";
        assert_eq!(extract_token_usage(output), Some((100, 200)));
    }

    // -- extract_number_after tests --

    #[test]
    fn test_extract_number_after_basic() {
        assert_eq!(extract_number_after("total: 42", "total:"), Some(42));
    }

    #[test]
    fn test_extract_number_after_with_commas() {
        assert_eq!(
            extract_number_after("count: 1,234,567", "count:"),
            Some(1_234_567)
        );
    }

    #[test]
    fn test_extract_number_after_not_found() {
        assert_eq!(extract_number_after("nothing here", "total:"), None);
    }

    #[test]
    fn test_extract_number_after_no_number() {
        assert_eq!(extract_number_after("total: abc", "total:"), None);
    }

    // -- parse_git_shortstat tests --

    #[test]
    fn test_parse_git_shortstat_full() {
        let output = " 3 files changed, 42 insertions(+), 7 deletions(-)";
        assert_eq!(parse_git_shortstat(output), (Some(3), Some(42), Some(7)));
    }

    #[test]
    fn test_parse_git_shortstat_insertions_only() {
        let output = " 1 file changed, 10 insertions(+)";
        assert_eq!(parse_git_shortstat(output), (Some(1), Some(10), None));
    }

    #[test]
    fn test_parse_git_shortstat_deletions_only() {
        let output = " 2 files changed, 5 deletions(-)";
        assert_eq!(parse_git_shortstat(output), (Some(2), None, Some(5)));
    }

    #[test]
    fn test_parse_git_shortstat_empty() {
        assert_eq!(parse_git_shortstat(""), (None, None, None));
    }

    #[test]
    fn test_parse_git_shortstat_whitespace() {
        assert_eq!(parse_git_shortstat("  \n  "), (None, None, None));
    }

    #[test]
    fn test_parse_git_shortstat_one_file() {
        let output = " 1 file changed, 1 insertion(+), 1 deletion(-)";
        assert_eq!(parse_git_shortstat(output), (Some(1), Some(1), Some(1)));
    }

    // -- extract_leading_number tests --

    #[test]
    fn test_extract_leading_number_basic() {
        assert_eq!(extract_leading_number("42 files changed"), Some(42));
    }

    #[test]
    fn test_extract_leading_number_empty() {
        assert_eq!(extract_leading_number("no numbers"), None);
    }

    #[test]
    fn test_extract_leading_number_with_prefix() {
        assert_eq!(extract_leading_number("abc 123 def"), Some(123));
    }
}