sashiko 0.2.4

Agentic code review system for Linux kernel
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
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
// Copyright 2026 The Sashiko Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::events::Event;
use crate::utils::redact_secret;
use anyhow::{Result, anyhow};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::process::Stdio;
use tokio::process::Command;
use tokio::sync::mpsc;
use tokio::time::{Duration, interval};
use tracing::{error, info, warn};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FetchRequest {
    pub repo_url: Option<String>,
    pub commit_hash: String,
    pub mr_url: Option<String>,
    pub mr_title: Option<String>,
    pub mr_number: Option<i64>,
}

pub struct FetchAgent {
    repo_path: PathBuf,
    rx: mpsc::Receiver<FetchRequest>,
    main_tx: mpsc::Sender<Event>,
    #[allow(clippy::type_complexity)]
    mr_metadata: HashMap<String, (Option<String>, Option<String>, Option<i64>)>,
    gitlab_token: Option<String>,
}

impl FetchAgent {
    pub fn new(
        repo_path: PathBuf,
        main_tx: mpsc::Sender<Event>,
        gitlab_token: Option<String>,
    ) -> (Self, mpsc::Sender<FetchRequest>) {
        let (tx, rx) = mpsc::channel(100);
        (
            Self {
                repo_path,
                rx,
                main_tx,
                mr_metadata: HashMap::new(),
                gitlab_token,
            },
            tx,
        )
    }

    pub async fn run(mut self) {
        info!("FetchAgent started");
        let mut queue: HashMap<Option<String>, HashSet<String>> = HashMap::new();
        let mut ticker = interval(Duration::from_secs(10));

        loop {
            tokio::select! {
                Some(req) = self.rx.recv() => {
                    if req.mr_url.is_some() || req.mr_title.is_some() || req.mr_number.is_some() {
                        self.mr_metadata.insert(
                            req.commit_hash.clone(),
                            (req.mr_url.clone(), req.mr_title.clone(), req.mr_number)
                        );
                    }
                    queue.entry(req.repo_url)
                        .or_default()
                        .insert(req.commit_hash);
                }
                _ = ticker.tick() => {
                    if !queue.is_empty() {
                        self.process_queue(&mut queue).await;
                    }
                }
            }
        }
    }

    async fn process_queue(&self, queue: &mut HashMap<Option<String>, HashSet<String>>) {
        info!("Processing fetch queue with {} repos", queue.len());

        for (url_opt, commits) in queue.drain() {
            if commits.is_empty() {
                continue;
            }

            let commit_list: Vec<String> = commits.into_iter().collect();
            let url_display = url_opt.as_deref().unwrap_or("local");

            info!(
                "Processing {} commits for remote {}",
                commit_list.len(),
                url_display
            );

            // For ranges (base..head), we need to check both endpoints individually
            let mut commits_to_check = Vec::new();
            for commit_or_range in &commit_list {
                if commit_or_range.contains("..") {
                    let parts: Vec<&str> = commit_or_range.split("..").collect();
                    if parts.len() == 2 {
                        commits_to_check.push(parts[0].to_string());
                        commits_to_check.push(parts[1].to_string());
                    }
                } else {
                    commits_to_check.push(commit_or_range.clone());
                }
            }

            let mut missing_commits = Vec::new();
            for commit in &commits_to_check {
                if !self.is_present(commit).await {
                    missing_commits.push(commit.clone());
                }
            }

            if missing_commits.is_empty() {
                info!(
                    "All commits present locally, skipping fetch for {}",
                    url_display
                );
            } else if let Some(url) = url_opt {
                // Remote fetch logic
                let remote_name = self.get_remote_name(&url);

                // Check if repo is local (same as self.repo_path)
                let is_local = {
                    let url_path = PathBuf::from(&url);
                    if let (Ok(canon_url), Ok(canon_repo)) = (
                        std::fs::canonicalize(&url_path),
                        std::fs::canonicalize(&self.repo_path),
                    ) {
                        canon_url == canon_repo
                    } else {
                        false
                    }
                };

                if is_local {
                    warn!(
                        "Repository is local but commits are missing: {:?}. Cannot fetch.",
                        missing_commits
                    );
                    // Do not continue here; let it fall through to Step 3 where it will fail individually
                } else {
                    if let Err(e) = self.ensure_remote(&remote_name, &url).await {
                        error!("Failed to ensure remote {}: {}", url, e);
                        for commit in &missing_commits {
                            let _ = self
                                .main_tx
                                .send(Event::IngestionFailed {
                                    article_id: commit.clone(),
                                    error: format!("Failed to set up remote {}: {}", url, e),
                                })
                                .await;
                        }
                        continue;
                    }

                    // 1. Try optimistic fetch (fetch specific commits)
                    if let Err(e) = self.fetch_commits(&remote_name, &missing_commits).await {
                        warn!(
                            "Optimistic fetch failed for {}: {}. Falling back to full fetch.",
                            url, e
                        );
                        // 2. Fallback: Fetch everything (heads)
                        if let Err(e) = self.fetch_all(&remote_name).await {
                            error!("Full fetch failed for {}: {}", url, e);
                            for commit in &missing_commits {
                                let _ = self
                                    .main_tx
                                    .send(Event::IngestionFailed {
                                        article_id: commit.clone(),
                                        error: format!("Failed to fetch from {}: {}", url, e),
                                    })
                                    .await;
                            }
                            continue;
                        }
                    }
                }
            } else {
                // Local repo, but commits are missing
                warn!(
                    "Local repository missing commits: {:?}. Cannot fetch.",
                    missing_commits
                );
            }

            // 3. Process each commit or range
            for commit_or_range in commit_list {
                if commit_or_range.contains("..") {
                    // It's a range
                    let range = &commit_or_range;

                    let shas = match crate::git_ops::resolve_git_range(&self.repo_path, range).await
                    {
                        Ok(shas) => shas,
                        Err(e) => {
                            let _ = self
                                .main_tx
                                .send(Event::IngestionFailed {
                                    article_id: range.clone(),
                                    error: format!("Failed to resolve git range: {}", e),
                                })
                                .await;
                            continue;
                        }
                    };
                    let count = shas.len() as u32;

                    // Process each SHA
                    let (mr_url, mr_title, mr_number) = self
                        .mr_metadata
                        .get(range)
                        .cloned()
                        .unwrap_or((None, None, None));

                    let article_id = if let Some(number) = mr_number {
                        format!("mr-{}-{}", number, range)
                    } else {
                        range.to_string()
                    };

                    for (i, sha) in shas.iter().enumerate() {
                        match self
                            .extract_patch(
                                sha,
                                &article_id,
                                (i + 1) as u32,
                                count,
                                mr_url.as_ref(),
                                mr_title.as_ref(),
                                mr_number,
                            )
                            .await
                        {
                            Ok(mut event) => {
                                if let Event::PatchSubmitted {
                                    ref mut message_id, ..
                                } = event
                                {
                                    *message_id = sha.clone();
                                }
                                if let Err(e) = self.main_tx.send(event).await {
                                    error!("Failed to send PatchSubmitted event: {}", e);
                                }
                            }
                            Err(e) => {
                                error!(
                                    "Failed to extract patch {} from range {}: {}",
                                    sha, range, e
                                );
                            }
                        }
                    }
                    info!("Successfully submitted remote range {}", range);
                } else {
                    // Single commit
                    let full_sha = match self.resolve_sha(&commit_or_range).await {
                        Ok(sha) => sha,
                        Err(e) => {
                            let _ = self
                                .main_tx
                                .send(Event::IngestionFailed {
                                    article_id: commit_or_range.clone(),
                                    error: format!("Failed to resolve SHA: {}", e),
                                })
                                .await;
                            continue;
                        }
                    };

                    let (mr_url, mr_title, mr_number) = self
                        .mr_metadata
                        .get(&commit_or_range)
                        .cloned()
                        .unwrap_or((None, None, None));

                    let article_id = if let Some(number) = mr_number {
                        format!("mr-{}-{}", number, &commit_or_range)
                    } else {
                        commit_or_range.clone()
                    };

                    match self
                        .extract_patch(
                            &full_sha,
                            &article_id,
                            1,
                            1,
                            mr_url.as_ref(),
                            mr_title.as_ref(),
                            mr_number,
                        )
                        .await
                    {
                        Ok(mut event) => {
                            if let Event::PatchSubmitted {
                                ref mut message_id, ..
                            } = event
                            {
                                *message_id = full_sha.clone();
                            }
                            if let Err(e) = self.main_tx.send(event).await {
                                error!("Failed to send PatchSubmitted event: {}", e);
                            } else {
                                info!("Successfully submitted remote patch {}", commit_or_range);
                            }
                        }
                        Err(e) => {
                            error!("Failed to extract patch {}: {}", commit_or_range, e);
                            let _ = self
                                .main_tx
                                .send(Event::IngestionFailed {
                                    article_id: commit_or_range,
                                    error: format!("Failed to extract patch: {}", e),
                                })
                                .await;
                        }
                    }
                }
            }
        }
    }

    fn get_remote_name(&self, url: &str) -> String {
        // Use a hash of the URL to ensure safe and unique remote names
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        url.hash(&mut hasher);
        format!("fetcher-{:x}", hasher.finish())
    }

    async fn ensure_remote(&self, name: &str, url: &str) -> Result<()> {
        // Inject GitLab token if available
        let authenticated_url = if let Some(token) = &self.gitlab_token {
            if url.contains("gitlab.com") && url.starts_with("https://") {
                url.replace("https://", &format!("https://oauth2:{}@", token))
            } else {
                url.to_string()
            }
        } else {
            url.to_string()
        };

        // Check if remote exists
        let status = Command::new("git")
            .current_dir(&self.repo_path)
            .args(["-c", "safe.bareRepository=all"])
            .args(["remote", "get-url", name])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .await?;

        if status.success() {
            let output = Command::new("git")
                .current_dir(&self.repo_path)
                .args(["-c", "safe.bareRepository=all"])
                .args(["remote", "get-url", name])
                .output()
                .await?;
            let current_url = String::from_utf8_lossy(&output.stdout).trim().to_string();

            if current_url != authenticated_url {
                info!(
                    "Updating remote {} from {} to {}",
                    name,
                    redact_secret(&current_url),
                    redact_secret(&authenticated_url)
                );
                Command::new("git")
                    .current_dir(&self.repo_path)
                    .args(["-c", "safe.bareRepository=all"])
                    .args(["remote", "set-url", name, &authenticated_url])
                    .output()
                    .await?;
            }
        } else {
            info!(
                "Adding remote {} -> {}",
                name,
                redact_secret(&authenticated_url)
            );
            let output = Command::new("git")
                .current_dir(&self.repo_path)
                .args(["-c", "safe.bareRepository=all"])
                .args(["remote", "add", name, &authenticated_url])
                .output()
                .await?;

            if !output.status.success() {
                return Err(anyhow!(
                    "Failed to add remote: {}",
                    String::from_utf8_lossy(&output.stderr).trim()
                ));
            }
        }
        Ok(())
    }

    async fn fetch_commits(&self, remote: &str, commits: &[String]) -> Result<()> {
        let mut cmd = Command::new("git");
        cmd.current_dir(&self.repo_path)
            .args(crate::git_ops::GIT_PROTOCOL_RESTRICTIONS)
            .arg("fetch")
            .arg(remote);

        for commit in commits {
            cmd.arg(commit);
        }

        let output = cmd.output().await?;
        if !output.status.success() {
            return Err(anyhow!(
                "Fetch failed: {}",
                String::from_utf8_lossy(&output.stderr).trim()
            ));
        }
        Ok(())
    }

    async fn fetch_all(&self, remote: &str) -> Result<()> {
        let output = Command::new("git")
            .current_dir(&self.repo_path)
            .args(crate::git_ops::GIT_PROTOCOL_RESTRICTIONS)
            .args(["fetch", remote])
            .output()
            .await?;

        if !output.status.success() {
            return Err(anyhow!(
                "Fetch all failed: {}",
                String::from_utf8_lossy(&output.stderr).trim()
            ));
        }
        Ok(())
    }

    async fn is_present(&self, commit_or_range: &str) -> bool {
        let mut args = vec!["-c", "safe.bareRepository=all"];
        let arg_str: String;

        if let Some((start, end)) = commit_or_range.split_once("..") {
            arg_str = format!("{}^{{commit}}..{}^{{commit}}", start, end);
            args.extend(["rev-list", "-n", "1", &arg_str]);
        } else {
            arg_str = format!("{}^{{commit}}", commit_or_range);
            args.extend(["rev-parse", "--verify", &arg_str]);
        };

        let output = Command::new("git")
            .current_dir(&self.repo_path)
            .args(&args)
            .output()
            .await;

        match output {
            Ok(s) => {
                let success = s.status.success();
                if success {
                    info!("is_present: {} is present", commit_or_range);
                } else {
                    info!(
                        "is_present: {} is missing or not a commit. stderr: {}",
                        commit_or_range,
                        String::from_utf8_lossy(&s.stderr)
                    );
                }
                success
            }
            Err(e) => {
                error!("is_present: {} check failed: {}", commit_or_range, e);
                false
            }
        }
    }

    async fn resolve_sha(&self, commit: &str) -> Result<String> {
        let output = Command::new("git")
            .current_dir(&self.repo_path)
            .args(["-c", "safe.bareRepository=all"])
            .args(["rev-parse", "--verify", commit])
            .output()
            .await?;

        if !output.status.success() {
            return Err(anyhow!(
                "Failed to resolve SHA for {}: {}",
                commit,
                String::from_utf8_lossy(&output.stderr).trim()
            ));
        }
        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    }

    #[allow(clippy::too_many_arguments)]
    async fn extract_patch(
        &self,
        commit: &str,
        article_id: &str,
        index: u32,
        total: u32,
        mr_url: Option<&String>,
        mr_title: Option<&String>,
        mr_number: Option<i64>,
    ) -> Result<Event> {
        let meta = crate::git_ops::extract_patch_metadata(&self.repo_path, commit).await?;

        Ok(Event::PatchSubmitted {
            group: "git-fetch".to_string(),
            article_id: article_id.to_string(),
            message_id: String::new(), // Set by caller
            subject: meta.subject,
            author: meta.author,
            message: meta.message,
            diff: meta.diff,
            base_commit: meta.base_commit,
            timestamp: meta.timestamp,
            index,
            total,
            mr_url: mr_url.cloned(),
            mr_title: mr_title.cloned(),
            mr_number,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;

    #[tokio::test]
    async fn test_fetch_agent_lifecycle() {
        let (tx, _rx) = mpsc::channel(1);
        let repo_path = PathBuf::from("/tmp");
        let (_agent, _sender) = FetchAgent::new(repo_path, tx, None);
    }

    #[tokio::test]
    async fn test_extract_patch_parsing() -> Result<()> {
        let temp_dir = tempfile::tempdir()?;
        let repo_path = temp_dir.path().to_path_buf();

        // Setup dummy repo
        Command::new("git")
            .current_dir(&repo_path)
            .arg("init")
            .output()
            .await?;
        Command::new("git")
            .current_dir(&repo_path)
            .args(["config", "user.name", "Test User"])
            .output()
            .await?;
        Command::new("git")
            .current_dir(&repo_path)
            .args(["config", "user.email", "test@example.com"])
            .output()
            .await?;

        let file_path = repo_path.join("file.txt");
        let mut file = File::create(&file_path)?;
        writeln!(file, "content")?;

        Command::new("git")
            .current_dir(&repo_path)
            .args(["add", "."])
            .output()
            .await?;
        Command::new("git")
            .current_dir(&repo_path)
            .args(["commit", "-m", "Subject Line\n\nBody Line"])
            .output()
            .await?;

        let (tx, _rx) = mpsc::channel(1);
        let (agent, _) = FetchAgent::new(repo_path.clone(), tx, None);

        let output = Command::new("git")
            .current_dir(&repo_path)
            .args(["rev-parse", "HEAD"])
            .output()
            .await?;
        let head = String::from_utf8(output.stdout)?.trim().to_string();

        let event = agent
            .extract_patch(&head, &head, 1, 1, None, None, None)
            .await?;

        match event {
            Event::PatchSubmitted {
                subject,
                author,
                message,
                diff,
                article_id,
                ..
            } => {
                assert_eq!(subject, "Subject Line");
                assert_eq!(author, "Test User <test@example.com>");
                assert!(message.contains("Body Line"));
                assert!(diff.contains("diff --git"));
                assert_eq!(article_id, head);
            }
            _ => panic!("Wrong event type"),
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_is_present_with_tree_sha() -> Result<()> {
        let temp_dir = tempfile::tempdir()?;
        let repo_path = temp_dir.path().to_path_buf();

        // Setup dummy repo
        Command::new("git")
            .current_dir(&repo_path)
            .arg("init")
            .output()
            .await?;
        Command::new("git")
            .current_dir(&repo_path)
            .args(["config", "user.name", "Test User"])
            .output()
            .await?;
        Command::new("git")
            .current_dir(&repo_path)
            .args(["config", "user.email", "test@example.com"])
            .output()
            .await?;

        let file_path = repo_path.join("file.txt");
        let mut file = File::create(&file_path)?;
        writeln!(file, "content")?;

        Command::new("git")
            .current_dir(&repo_path)
            .args(["add", "."])
            .output()
            .await?;
        Command::new("git")
            .current_dir(&repo_path)
            .args(["commit", "-m", "Subject Line"])
            .output()
            .await?;

        let (tx, _rx) = mpsc::channel(1);
        let (agent, _) = FetchAgent::new(repo_path.clone(), tx, None);

        let output = Command::new("git")
            .current_dir(&repo_path)
            .args(["rev-parse", "HEAD^{tree}"])
            .output()
            .await?;
        let tree_sha = String::from_utf8(output.stdout)?.trim().to_string();

        assert!(
            !agent.is_present(&tree_sha).await,
            "Tree SHA should not be considered a present commit"
        );

        let output = Command::new("git")
            .current_dir(&repo_path)
            .args(["rev-parse", "HEAD"])
            .output()
            .await?;
        let commit_sha = String::from_utf8(output.stdout)?.trim().to_string();

        assert!(
            agent.is_present(&commit_sha).await,
            "Commit SHA should be considered present"
        );

        Ok(())
    }
}