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
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
//! Review phase validation checks.
//!
//! This module contains pre-flight and post-flight validation logic for the review phase.
//! These checks verify that the environment is suitable for running the review agent
//! and help diagnose issues early.

use crate::agents::contains_glm_model;
use crate::common::domain_types::{AgentDirectoryEntryCount, AgentName, IssueFileSize, ModelName};
use crate::review_metrics::ReviewMetrics;
use crate::workspace::Workspace;
use std::path::Path;

/// Maximum number of files in .agent directory before warning about cleanup.
const MAX_AGENT_DIR_ENTRY_COUNT: usize = 1000;

/// Result of pre-flight validation
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PreflightResult {
    /// All checks passed
    Ok,
    /// Warning issued but can proceed
    Warning(String),
    /// Critical error that should halt execution
    Error(String),
}

/// Diagnostics emitted by [`pre_flight_review_check`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PreflightDiagnostic {
    ProblematicReviewer {
        agent: AgentName,
        model: Option<ModelName>,
    },
    GlmAgentDetected {
        agent: AgentName,
    },
    ExistingIssuesFile {
        size_bytes: IssueFileSize,
    },
    EmptyIssuesFile,
    IssuesFileReadFailure {
        error: String,
    },
    AgentDirectoryTooLarge {
        entry_count: AgentDirectoryEntryCount,
    },
    AgentDirectoryCreationFailed {
        error: String,
    },
    AgentDirectoryNotWritable {
        error: String,
    },
}

/// Wrapper that pairs a domain value with diagnostics that explain non-fatal decisions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WithDiagnostics<T, D> {
    pub value: T,
    pub diagnostics: Vec<D>,
}

impl<T, D> WithDiagnostics<T, D> {
    pub fn new(value: T) -> Self {
        Self {
            value,
            diagnostics: Vec::new(),
        }
    }
}

/// Result of post-flight validation
#[derive(Debug)]
pub enum PostflightResult {
    /// ISSUES.md found and valid
    Valid,
    /// ISSUES.md missing or empty
    Missing(String),
    /// ISSUES.md has unexpected format
    Malformed(String),
}

/// Run pre-flight validation checks before starting a review pass.
///
/// These checks verify that the environment is suitable for running
/// the review agent and return diagnostics that explain warnings or adjustments
/// instead of logging directly.
///
/// Uses workspace abstraction for file operations, enabling testing with
/// `MemoryWorkspace`.
pub fn pre_flight_review_check(
    workspace: &dyn Workspace,
    cycle: u32,
    reviewer_agent: &str,
    reviewer_model: Option<&str>,
) -> WithDiagnostics<PreflightResult, PreflightDiagnostic> {
    let agent_dir = Path::new(".agent");
    let issues_path = Path::new(".agent/ISSUES.md");

    // Each check returns Ok(non_fatal_diagnostics) to continue, or
    // Err(terminal_result_with_diagnostics) to halt.
    // We thread these checks, accumulating diagnostics along the way.

    // Check 0: Agent compatibility warning (non-blocking)
    let agent_diagnostics: Vec<PreflightDiagnostic> =
        check_agent_compatibility(reviewer_agent, reviewer_model);

    // Check 0.5: Existing ISSUES.md diagnostic (non-blocking)
    let issues_diagnostics: Vec<PreflightDiagnostic> =
        check_existing_issues_file(workspace, issues_path);

    // Accumulated non-fatal diagnostics so far.
    let preamble_diagnostics: Vec<PreflightDiagnostic> = agent_diagnostics
        .into_iter()
        .chain(issues_diagnostics)
        .collect();

    // Check 1: .agent directory must exist and be writable (may halt).
    check_agent_dir_writable(workspace, agent_dir, cycle, preamble_diagnostics)
        .and_then(|diagnostics_after_write| {
            // Check 2: .agent directory must not be too large (may halt).
            check_agent_dir_size(workspace, agent_dir, diagnostics_after_write)
        })
        .unwrap_or_else(|terminal| terminal)
}

/// Check 0 + 0.1: agent/model compatibility warnings — always non-fatal.
fn check_agent_compatibility(
    reviewer_agent: &str,
    reviewer_model: Option<&str>,
) -> Vec<PreflightDiagnostic> {
    let problematic: Option<PreflightDiagnostic> =
        is_problematic_prompt_target(reviewer_agent, reviewer_model).then(|| {
            PreflightDiagnostic::ProblematicReviewer {
                agent: AgentName::from(reviewer_agent),
                model: reviewer_model.map(ModelName::from),
            }
        });

    let glm: Option<PreflightDiagnostic> =
        contains_glm_model(reviewer_agent).then(|| PreflightDiagnostic::GlmAgentDetected {
            agent: AgentName::from(reviewer_agent),
        });

    problematic.into_iter().chain(glm).collect()
}

/// Check 0.5: existing ISSUES.md — always non-fatal.
fn check_existing_issues_file(
    workspace: &dyn Workspace,
    issues_path: &Path,
) -> Vec<PreflightDiagnostic> {
    if !workspace.exists(issues_path) {
        return vec![];
    }
    match workspace.read(issues_path) {
        Ok(content) if !content.is_empty() => vec![PreflightDiagnostic::ExistingIssuesFile {
            size_bytes: IssueFileSize::from(content.len()),
        }],
        Ok(_) => vec![PreflightDiagnostic::EmptyIssuesFile],
        Err(e) => vec![PreflightDiagnostic::IssuesFileReadFailure {
            error: e.to_string(),
        }],
    }
}

/// Check 1: ensure .agent exists and is writable.
///
/// Returns `Ok(diagnostics)` when the directory is ready, or
/// `Err(terminal)` when the run must halt.
fn check_agent_dir_writable(
    workspace: &dyn Workspace,
    agent_dir: &Path,
    cycle: u32,
    prior_diagnostics: Vec<PreflightDiagnostic>,
) -> Result<Vec<PreflightDiagnostic>, WithDiagnostics<PreflightResult, PreflightDiagnostic>> {
    // Ensure the directory exists.
    let dir_created_diagnostics: Vec<PreflightDiagnostic> = if !workspace.is_dir(agent_dir) {
        match workspace.create_dir_all(agent_dir) {
            Ok(()) => vec![],
            Err(e) => {
                let error_message = e.to_string();
                return Err(WithDiagnostics {
                    value: PreflightResult::Error(format!(
                        "Cannot create .agent directory: {error_message}. Check directory permissions."
                    )),
                    diagnostics: prior_diagnostics
                        .into_iter()
                        .chain(std::iter::once(
                            PreflightDiagnostic::AgentDirectoryCreationFailed {
                                error: error_message,
                            },
                        ))
                        .collect(),
                });
            }
        }
    } else {
        vec![]
    };

    // Test write by touching a temp file.
    let test_file = agent_dir.join(format!(".write_test_{cycle}"));
    match workspace.write(&test_file, "test") {
        Ok(()) => {
            let _ = workspace.remove(&test_file);
            Ok(prior_diagnostics
                .into_iter()
                .chain(dir_created_diagnostics)
                .collect())
        }
        Err(e) => {
            let error_message = e.to_string();
            Err(WithDiagnostics {
                value: PreflightResult::Error(format!(
                    ".agent directory is not writable: {error_message}. Check file permissions."
                )),
                diagnostics: prior_diagnostics
                    .into_iter()
                    .chain(dir_created_diagnostics)
                    .chain(std::iter::once(
                        PreflightDiagnostic::AgentDirectoryNotWritable {
                            error: error_message,
                        },
                    ))
                    .collect(),
            })
        }
    }
}

/// Check 2: ensure .agent directory is not too large.
///
/// Returns `Ok(diagnostics)` when the directory size is acceptable, or
/// `Err(terminal)` when the run should halt with a warning.
fn check_agent_dir_size(
    workspace: &dyn Workspace,
    agent_dir: &Path,
    prior_diagnostics: Vec<PreflightDiagnostic>,
) -> Result<
    WithDiagnostics<PreflightResult, PreflightDiagnostic>,
    WithDiagnostics<PreflightResult, PreflightDiagnostic>,
> {
    let too_large: Option<(usize, PreflightDiagnostic)> = workspace
        .read_dir(agent_dir)
        .ok()
        .filter(|entries| entries.len() > MAX_AGENT_DIR_ENTRY_COUNT)
        .map(|entries| {
            let count = entries.len();
            (
                count,
                PreflightDiagnostic::AgentDirectoryTooLarge {
                    entry_count: AgentDirectoryEntryCount::from(count),
                },
            )
        });

    match too_large {
        Some((_, diag)) => Err(WithDiagnostics {
            value: PreflightResult::Warning(
                "Large .agent directory detected. Review may be slow.".to_string(),
            ),
            diagnostics: prior_diagnostics
                .into_iter()
                .chain(std::iter::once(diag))
                .collect(),
        }),
        None => Ok(WithDiagnostics {
            value: PreflightResult::Ok,
            diagnostics: prior_diagnostics,
        }),
    }
}

/// Run post-flight validation after a review pass completes.
///
/// These checks verify that the review agent produced expected output.
///
/// Uses workspace abstraction for file operations, enabling testing with
/// `MemoryWorkspace`.
pub fn post_flight_review_check(
    workspace: &dyn Workspace,
    logger: &crate::logger::Logger,
    cycle: u32,
) -> PostflightResult {
    let issues_path = Path::new(".agent/ISSUES.md");

    // Check 1: Verify ISSUES.md exists
    if !workspace.exists(issues_path) {
        logger.warn(&format!(
            "Review cycle {cycle} completed but ISSUES.md was not created. \
             The agent may have failed or used a different output format."
        ));
        logger.info("Possible causes:");
        logger.info("  - Agent failed to write the file (permission/execution error)");
        logger.info("  - Agent used a different output filename or format");
        logger.info("  - Agent was interrupted during execution");
        return PostflightResult::Missing(
            "ISSUES.md not found after review. Agent may have failed.".to_string(),
        );
    }

    // Check 2: Verify ISSUES.md is not empty and log its size
    let file_size = match workspace.read(issues_path) {
        Ok(content) if content.is_empty() => {
            logger.warn(&format!("Review cycle {cycle} created an empty ISSUES.md."));
            logger.info("Possible causes:");
            logger.info("  - Agent reviewed but found no issues (should write 'No issues found.')");
            logger.info("  - Agent failed during file write");
            logger.info("  - Agent doesn't understand the expected output format");
            return PostflightResult::Missing("ISSUES.md is empty".to_string());
        }
        Ok(content) => {
            // Log the file size for debugging
            let size = content.len() as u64;
            logger.info(&format!("ISSUES.md created ({size} bytes)"));
            size
        }
        Err(e) => {
            logger.warn(&format!("Cannot read ISSUES.md: {e}"));
            return PostflightResult::Missing(format!("Cannot read ISSUES.md: {e}"));
        }
    };

    // Check 3: Verify ISSUES.md has valid structure
    match ReviewMetrics::from_issues_file_with_workspace(workspace) {
        Ok(metrics) => {
            // Track whether metrics were successfully parsed from the file
            let _parsed = metrics.issues_file_found;
            // Check if metrics indicate reasonable content
            if metrics.total_issues == 0 && !metrics.no_issues_declared {
                // Partial recovery: file has content but no parseable issues
                logger.warn(&format!(
                    "Review cycle {cycle} produced ISSUES.md ({file_size} bytes) but no parseable issues detected."
                ));
                logger.info("Content may be in unexpected format. The fix pass may still work.");
                logger.info(
                    "Consider checking .agent/ISSUES.md manually to see what the agent wrote.",
                );
                return PostflightResult::Malformed(
                    "ISSUES.md exists but no issues detected. Check format.".to_string(),
                );
            }

            // Log a summary of what was found
            if metrics.total_issues > 0 {
                logger.info(&format!(
                    "Review found {} issues ({} critical, {} high, {} medium, {} low)",
                    metrics.total_issues,
                    metrics.critical_issues,
                    metrics.high_issues,
                    metrics.medium_issues,
                    metrics.low_issues
                ));
                if metrics.resolved_issues > 0 {
                    logger.info(&format!(
                        "  {} issues already resolved",
                        metrics.resolved_issues
                    ));
                }
            } else if metrics.no_issues_declared {
                logger.info("Review declared no issues found.");
            }

            PostflightResult::Valid
        }
        Err(e) => {
            // Partial recovery: attempt to show what content we can
            logger.warn(&format!("Failed to parse ISSUES.md: {e}"));
            logger.info(&format!(
                "ISSUES.md has {file_size} bytes but failed to parse."
            ));
            logger.info("The file may be malformed or in an unexpected format.");
            logger.info(
                "Attempting partial recovery: fix pass will proceed but may have limited success.",
            );

            // Try to read first few lines to give user a hint
            if let Ok(content) = workspace.read(issues_path) {
                let preview: String = content.lines().take(5).collect::<Vec<_>>().join("\n");
                if !preview.is_empty() {
                    logger.info("ISSUES.md preview (first 5 lines):");
                    preview.lines().for_each(|line| {
                        logger.info(&format!("  {line}"));
                    });
                }
            }

            PostflightResult::Malformed(format!("Failed to parse ISSUES.md: {e}"))
        }
    }
}

/// Check if the given agent/model combination is a problematic prompt target.
///
/// Certain AI agents have known compatibility issues with complex structured prompts.
/// This function detects those agents for which alternative handling may be needed.
fn is_problematic_prompt_target(agent: &str, model_flag: Option<&str>) -> bool {
    contains_glm_model(agent) || model_flag.is_some_and(contains_glm_model)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::domain_types::{AgentName, IssueFileSize, ModelName};
    use crate::workspace::{DirEntry, MemoryWorkspace, Workspace};
    use std::io;
    use std::path::{Path, PathBuf};

    // -------------------------------------------------------------------------
    // Workspace stubs for failure injection
    // -------------------------------------------------------------------------

    /// Workspace that reports `.agent` as non-existent and fails `create_dir_all`,
    /// used to exercise the `AgentDirectoryCreationFailed` diagnostic path.
    struct AgentDirCreationFailingWorkspace {
        inner: MemoryWorkspace,
    }

    impl AgentDirCreationFailingWorkspace {
        fn new() -> Self {
            Self {
                inner: MemoryWorkspace::new_test(),
            }
        }
    }

    impl Workspace for AgentDirCreationFailingWorkspace {
        fn root(&self) -> &Path {
            self.inner.root()
        }

        fn read(&self, relative: &Path) -> io::Result<String> {
            self.inner.read(relative)
        }

        fn read_bytes(&self, relative: &Path) -> io::Result<Vec<u8>> {
            self.inner.read_bytes(relative)
        }

        fn write(&self, relative: &Path, content: &str) -> io::Result<()> {
            self.inner.write(relative, content)
        }

        fn write_bytes(&self, relative: &Path, content: &[u8]) -> io::Result<()> {
            self.inner.write_bytes(relative, content)
        }

        fn append_bytes(&self, relative: &Path, content: &[u8]) -> io::Result<()> {
            self.inner.append_bytes(relative, content)
        }

        fn exists(&self, relative: &Path) -> bool {
            self.inner.exists(relative)
        }

        fn is_file(&self, relative: &Path) -> bool {
            self.inner.is_file(relative)
        }

        /// Always reports `.agent` as absent so the creation path is triggered.
        fn is_dir(&self, _relative: &Path) -> bool {
            false
        }

        fn remove(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove(relative)
        }

        fn remove_if_exists(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_if_exists(relative)
        }

        fn remove_dir_all(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_dir_all(relative)
        }

        fn remove_dir_all_if_exists(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_dir_all_if_exists(relative)
        }

        /// Always fails so `AgentDirectoryCreationFailed` is emitted.
        fn create_dir_all(&self, _relative: &Path) -> io::Result<()> {
            Err(io::Error::new(
                io::ErrorKind::PermissionDenied,
                "simulated: cannot create .agent directory",
            ))
        }

        fn read_dir(&self, relative: &Path) -> io::Result<Vec<DirEntry>> {
            self.inner.read_dir(relative)
        }

        fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
            self.inner.rename(from, to)
        }

        fn write_atomic(&self, relative: &Path, content: &str) -> io::Result<()> {
            self.inner.write_atomic(relative, content)
        }

        fn set_readonly(&self, relative: &Path) -> io::Result<()> {
            self.inner.set_readonly(relative)
        }

        fn set_writable(&self, relative: &Path) -> io::Result<()> {
            self.inner.set_writable(relative)
        }
    }

    /// Workspace where `.agent` appears to exist but any write into it fails,
    /// used to exercise the `AgentDirectoryNotWritable` diagnostic path.
    struct AgentDirWriteFailingWorkspace {
        inner: MemoryWorkspace,
    }

    impl AgentDirWriteFailingWorkspace {
        fn new() -> Self {
            Self {
                inner: MemoryWorkspace::new_test().with_dir(".agent"),
            }
        }
    }

    impl Workspace for AgentDirWriteFailingWorkspace {
        fn root(&self) -> &Path {
            self.inner.root()
        }

        fn read(&self, relative: &Path) -> io::Result<String> {
            self.inner.read(relative)
        }

        fn read_bytes(&self, relative: &Path) -> io::Result<Vec<u8>> {
            self.inner.read_bytes(relative)
        }

        /// Fails for every path to simulate a read-only `.agent` directory.
        fn write(&self, _relative: &Path, _content: &str) -> io::Result<()> {
            Err(io::Error::new(
                io::ErrorKind::PermissionDenied,
                "simulated: .agent directory is not writable",
            ))
        }

        fn write_bytes(&self, relative: &Path, content: &[u8]) -> io::Result<()> {
            self.inner.write_bytes(relative, content)
        }

        fn append_bytes(&self, relative: &Path, content: &[u8]) -> io::Result<()> {
            self.inner.append_bytes(relative, content)
        }

        fn exists(&self, relative: &Path) -> bool {
            self.inner.exists(relative)
        }

        fn is_file(&self, relative: &Path) -> bool {
            self.inner.is_file(relative)
        }

        fn is_dir(&self, relative: &Path) -> bool {
            self.inner.is_dir(relative)
        }

        fn remove(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove(relative)
        }

        fn remove_if_exists(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_if_exists(relative)
        }

        fn remove_dir_all(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_dir_all(relative)
        }

        fn remove_dir_all_if_exists(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_dir_all_if_exists(relative)
        }

        fn create_dir_all(&self, relative: &Path) -> io::Result<()> {
            self.inner.create_dir_all(relative)
        }

        fn read_dir(&self, relative: &Path) -> io::Result<Vec<DirEntry>> {
            self.inner.read_dir(relative)
        }

        fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
            self.inner.rename(from, to)
        }

        fn write_atomic(&self, relative: &Path, content: &str) -> io::Result<()> {
            self.inner.write_atomic(relative, content)
        }

        fn set_readonly(&self, relative: &Path) -> io::Result<()> {
            self.inner.set_readonly(relative)
        }

        fn set_writable(&self, relative: &Path) -> io::Result<()> {
            self.inner.set_writable(relative)
        }
    }

    /// Workspace where a specific path appears to exist (via `exists`) but fails to read,
    /// used to exercise the `IssuesFileReadFailure` diagnostic path.
    struct IssuesFileReadFailingWorkspace {
        inner: MemoryWorkspace,
        failing_path: PathBuf,
    }

    impl IssuesFileReadFailingWorkspace {
        fn new(failing_path: &str) -> Self {
            Self {
                inner: MemoryWorkspace::new_test().with_dir(".agent"),
                failing_path: PathBuf::from(failing_path),
            }
        }
    }

    impl Workspace for IssuesFileReadFailingWorkspace {
        fn root(&self) -> &Path {
            self.inner.root()
        }

        /// Returns `Err` for the configured path to simulate a read failure.
        fn read(&self, relative: &Path) -> io::Result<String> {
            if relative == self.failing_path.as_path() {
                return Err(io::Error::new(
                    io::ErrorKind::PermissionDenied,
                    "simulated: cannot read ISSUES.md",
                ));
            }
            self.inner.read(relative)
        }

        fn read_bytes(&self, relative: &Path) -> io::Result<Vec<u8>> {
            self.inner.read_bytes(relative)
        }

        fn write(&self, relative: &Path, content: &str) -> io::Result<()> {
            self.inner.write(relative, content)
        }

        fn write_bytes(&self, relative: &Path, content: &[u8]) -> io::Result<()> {
            self.inner.write_bytes(relative, content)
        }

        fn append_bytes(&self, relative: &Path, content: &[u8]) -> io::Result<()> {
            self.inner.append_bytes(relative, content)
        }

        /// Reports the configured path as existing even though reads fail.
        fn exists(&self, relative: &Path) -> bool {
            relative == self.failing_path.as_path() || self.inner.exists(relative)
        }

        fn is_file(&self, relative: &Path) -> bool {
            self.inner.is_file(relative)
        }

        fn is_dir(&self, relative: &Path) -> bool {
            self.inner.is_dir(relative)
        }

        fn remove(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove(relative)
        }

        fn remove_if_exists(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_if_exists(relative)
        }

        fn remove_dir_all(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_dir_all(relative)
        }

        fn remove_dir_all_if_exists(&self, relative: &Path) -> io::Result<()> {
            self.inner.remove_dir_all_if_exists(relative)
        }

        fn create_dir_all(&self, relative: &Path) -> io::Result<()> {
            self.inner.create_dir_all(relative)
        }

        fn read_dir(&self, relative: &Path) -> io::Result<Vec<DirEntry>> {
            self.inner.read_dir(relative)
        }

        fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
            self.inner.rename(from, to)
        }

        fn write_atomic(&self, relative: &Path, content: &str) -> io::Result<()> {
            self.inner.write_atomic(relative, content)
        }

        fn set_readonly(&self, relative: &Path) -> io::Result<()> {
            self.inner.set_readonly(relative)
        }

        fn set_writable(&self, relative: &Path) -> io::Result<()> {
            self.inner.set_writable(relative)
        }
    }

    #[test]
    fn preflight_problematic_reviewer_carries_agent_name_newtype() {
        let workspace = MemoryWorkspace::new_test();
        let outcome = pre_flight_review_check(&workspace, 1, "glm-4", Some("glm-4-flash"));

        let found = outcome.diagnostics.iter().find_map(|diag| match diag {
            PreflightDiagnostic::ProblematicReviewer { agent, model } => Some((agent, model)),
            _ => None,
        });
        let (agent, model) = found.expect("ProblematicReviewer diagnostic expected");
        assert_eq!(agent, &AgentName::from("glm-4"));
        assert_eq!(model.as_ref(), Some(&ModelName::from("glm-4-flash")));
    }

    #[test]
    fn preflight_glm_agent_detected_carries_agent_name_newtype() {
        let workspace = MemoryWorkspace::new_test();
        let outcome = pre_flight_review_check(&workspace, 1, "glm-4", None);

        let found = outcome.diagnostics.iter().find_map(|diag| match diag {
            PreflightDiagnostic::GlmAgentDetected { agent } => Some(agent),
            _ => None,
        });
        let agent = found.expect("GlmAgentDetected diagnostic expected");
        assert_eq!(agent, &AgentName::from("glm-4"));
    }

    #[test]
    fn preflight_reports_existing_issues_diagnostic() {
        let content = "previous ISSUES.md contents";
        let workspace = MemoryWorkspace::new_test().with_file(".agent/ISSUES.md", content);

        let outcome = pre_flight_review_check(&workspace, 1, "reviewer", None);

        assert_eq!(outcome.value, PreflightResult::Ok);
        assert!(outcome.diagnostics.iter().any(|diag| matches!(
            diag,
            PreflightDiagnostic::ExistingIssuesFile { size_bytes } if *size_bytes == IssueFileSize::from(content.len())
        )));
    }

    #[test]
    fn preflight_warns_on_large_agent_directory() {
        let workspace = (0..=MAX_AGENT_DIR_ENTRY_COUNT)
            .fold(MemoryWorkspace::new_test(), |workspace, index| {
                workspace.with_file(&format!(".agent/extra_{index}.log"), "x")
            });

        let outcome = pre_flight_review_check(&workspace, 2, "reviewer", None);

        assert!(matches!(
            outcome.value,
            PreflightResult::Warning(message)
            if message == "Large .agent directory detected. Review may be slow."
        ));
        assert!(outcome.diagnostics.iter().any(|diag| matches!(
            diag,
            PreflightDiagnostic::AgentDirectoryTooLarge { entry_count }
            if entry_count.as_count() > MAX_AGENT_DIR_ENTRY_COUNT
        )));
    }

    // -------------------------------------------------------------------------
    // P12-diagnostics: `.value` shape and empty-diagnostics coverage
    // -------------------------------------------------------------------------

    /// Fully-valid input (non-problematic reviewer, no existing ISSUES.md,
    /// writable `.agent` directory) must produce `PreflightResult::Ok` with
    /// an empty diagnostics list.
    #[test]
    fn preflight_clean_env_produces_ok_with_empty_diagnostics() {
        let workspace = MemoryWorkspace::new_test().with_dir(".agent");

        let outcome = pre_flight_review_check(&workspace, 1, "claude", None);

        assert_eq!(
            outcome.value,
            PreflightResult::Ok,
            "expected Ok for fully-valid input"
        );
        assert!(
            outcome.diagnostics.is_empty(),
            "expected no diagnostics for fully-valid input, got: {:?}",
            outcome.diagnostics
        );
    }

    /// An empty ISSUES.md from a previous run must produce the `EmptyIssuesFile`
    /// diagnostic while still allowing the check to proceed (`Ok` result).
    #[test]
    fn preflight_empty_issues_file_produces_empty_issues_file_diagnostic() {
        let workspace = MemoryWorkspace::new_test().with_file(".agent/ISSUES.md", "");

        let outcome = pre_flight_review_check(&workspace, 1, "reviewer", None);

        assert_eq!(
            outcome.value,
            PreflightResult::Ok,
            "an empty ISSUES.md should not block the preflight"
        );
        assert!(
            outcome
                .diagnostics
                .iter()
                .any(|d| matches!(d, PreflightDiagnostic::EmptyIssuesFile)),
            "expected EmptyIssuesFile diagnostic, got: {:?}",
            outcome.diagnostics
        );
    }

    /// When ISSUES.md is reported as present but its read fails, the check must
    /// emit an `IssuesFileReadFailure` diagnostic with the underlying error message.
    #[test]
    fn preflight_issues_file_read_failure_produces_read_failure_diagnostic() {
        let workspace = IssuesFileReadFailingWorkspace::new(".agent/ISSUES.md");

        let outcome = pre_flight_review_check(&workspace, 1, "reviewer", None);

        let failure_msg = outcome.diagnostics.iter().find_map(|d| match d {
            PreflightDiagnostic::IssuesFileReadFailure { error } => Some(error.as_str()),
            _ => None,
        });
        assert!(
            failure_msg.is_some(),
            "expected IssuesFileReadFailure diagnostic, got: {:?}",
            outcome.diagnostics
        );
        assert!(
            failure_msg.unwrap().contains("simulated"),
            "diagnostic error message should contain the underlying cause"
        );
    }

    /// When the `.agent` directory cannot be created, the check must:
    /// - return `PreflightResult::Error` (not proceed),
    /// - include an `AgentDirectoryCreationFailed` diagnostic with the error message.
    #[test]
    fn preflight_agent_directory_creation_failed_produces_error_and_diagnostic() {
        let workspace = AgentDirCreationFailingWorkspace::new();

        let outcome = pre_flight_review_check(&workspace, 1, "reviewer", None);

        assert!(
            matches!(outcome.value, PreflightResult::Error(_)),
            "expected Error when .agent cannot be created, got: {:?}",
            outcome.value
        );
        let creation_error = outcome.diagnostics.iter().find_map(|d| match d {
            PreflightDiagnostic::AgentDirectoryCreationFailed { error } => Some(error.as_str()),
            _ => None,
        });
        assert!(
            creation_error.is_some(),
            "expected AgentDirectoryCreationFailed diagnostic, got: {:?}",
            outcome.diagnostics
        );
        assert!(
            creation_error.unwrap().contains("simulated"),
            "diagnostic should carry the underlying error message"
        );
    }

    /// When `.agent` exists but writing into it fails, the check must:
    /// - return `PreflightResult::Error`,
    /// - include an `AgentDirectoryNotWritable` diagnostic with the error message.
    #[test]
    fn preflight_agent_directory_not_writable_produces_error_and_diagnostic() {
        let workspace = AgentDirWriteFailingWorkspace::new();

        let outcome = pre_flight_review_check(&workspace, 1, "reviewer", None);

        assert!(
            matches!(outcome.value, PreflightResult::Error(_)),
            "expected Error when .agent is not writable, got: {:?}",
            outcome.value
        );
        let write_error = outcome.diagnostics.iter().find_map(|d| match d {
            PreflightDiagnostic::AgentDirectoryNotWritable { error } => Some(error.as_str()),
            _ => None,
        });
        assert!(
            write_error.is_some(),
            "expected AgentDirectoryNotWritable diagnostic, got: {:?}",
            outcome.diagnostics
        );
        assert!(
            write_error.unwrap().contains("simulated"),
            "diagnostic should carry the underlying error message"
        );
    }
}