pitboss 0.2.0

CLI that orchestrates coding agents (Claude Code and others) through a phased implementation plan, with automatic test/commit loops and a TUI dashboard
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
//! `AiderAgent` — production [`Agent`] that drives the `aider` CLI in
//! non-interactive (`--message`) mode and parses its plain-text stdout into
//! pitboss's [`AgentEvent`] / [`AgentOutcome`] vocabulary.
//!
//! ## How to install / configure `aider`
//!
//! Pitboss shells out to whatever `aider` binary is on `PATH` (or the path you
//! pass via `[agent.aider] binary` in `config.toml`). Install per Aider's
//! docs (`pip install aider-chat` or `pipx install aider-chat`) and configure
//! whichever `*_API_KEY` env var matches the active model before running
//! pitboss.
//!
//! Pitboss runs the agent under `--yes-always` so it never blocks on a
//! confirmation prompt, `--no-pretty --no-stream` so stdout is line-oriented
//! plain text suitable for parsing, and `--no-check-update
//! --no-show-model-warnings --analytics-disable` so the very first run on a
//! fresh machine doesn't stall on a one-time interactive prompt. Override via
//! `[agent.aider] extra_args = […]` if a workspace needs different defaults.
//!
//! ## Prompt assembly
//!
//! Aider has no separate system-prompt channel, so [`AgentRequest::system_prompt`]
//! and [`AgentRequest::user_prompt`] are concatenated — system first, blank line,
//! then user — and the whole payload is passed via `--message <body>`. We use
//! the inline `--message` flag rather than `--message-file` so there's no temp
//! file to clean up; the OS's `ARG_MAX` (≥256 KB on every platform pitboss
//! supports) is comfortably above the prompts the runner produces.
//!
//! ## Event mapping
//!
//! Aider's stdout is plain text — there is no structured/JSON output mode at
//! the time of writing. Every line is forwarded as an [`AgentEvent::Stdout`]
//! verbatim so the dashboard surfaces the run's narrative. In addition, three
//! prefix patterns are recognized as side-channel events:
//!
//! - `Applied edit to <path>` → [`AgentEvent::ToolUse`]`("edit")`. Mirrors the
//!   way [`super::claude_code`] emits Claude's `Edit` tool and
//!   [`super::codex`] emits `patch` — runs that touch files surface in the
//!   dashboard alongside other backends.
//! - `Commit <sha> <message>` → [`AgentEvent::ToolUse`]`("commit")`. Aider does
//!   its own `git commit` after applying edits unless `--no-auto-commits` is
//!   passed via `extra_args`; surfacing those commits as tool-use events
//!   keeps parity with the runner's other dashboards.
//! - `Tokens: <n> sent, <n> received` (with optional `k` / `M` suffixes) →
//!   folded into a running [`TokenUsage`] total. One [`AgentEvent::TokenDelta`]
//!   is emitted at the end so the runner doesn't double-count cross-turn
//!   reports.
//!
//! Errors: aider has no structured error event. A non-zero exit code or a
//! drained stderr tail produces a [`StopReason::Error`] formatted with the
//! same shape Codex/Claude use; the parsed text on stdout is left untouched.

use std::path::PathBuf;

use anyhow::Result;
use async_trait::async_trait;
use tokio::process::Command;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

use crate::state::TokenUsage;

use super::{
    subprocess::{self, SubprocessOutcome},
    Agent, AgentEvent, AgentOutcome, AgentRequest, StopReason,
};

/// Default binary name. Resolved against `PATH` by the OS.
const DEFAULT_BINARY: &str = "aider";

/// How many trailing stderr lines to attach to a [`StopReason::Error`] when
/// the process exits non-zero. Bounded so a chatty error doesn't flood the
/// runner log.
const ERROR_TAIL_LINES: usize = 8;

/// Production [`Agent`] that drives the `aider` CLI.
#[derive(Debug, Clone)]
pub struct AiderAgent {
    binary: PathBuf,
    extra_args: Vec<String>,
    model_override: Option<String>,
}

impl AiderAgent {
    /// Construct an agent that resolves `aider` from `PATH`.
    pub fn new() -> Self {
        Self {
            binary: PathBuf::from(DEFAULT_BINARY),
            extra_args: Vec::new(),
            model_override: None,
        }
    }

    /// Construct an agent that invokes a specific binary path. Useful for
    /// tests (point at a fixture script) and for users with a non-standard
    /// install location.
    pub fn with_binary(binary: impl Into<PathBuf>) -> Self {
        Self {
            binary: binary.into(),
            extra_args: Vec::new(),
            model_override: None,
        }
    }

    /// Append extra argv that gets spliced in just before the `--message`
    /// flag on every invocation. Mirrors `[agent.aider] extra_args` in
    /// `config.toml`. Use this to enable `--no-auto-commits`, scope the run
    /// to specific files (`--file path`), or pass any flag the default set
    /// doesn't cover.
    pub fn with_extra_args(mut self, args: Vec<String>) -> Self {
        self.extra_args = args;
        self
    }

    /// Override the model identifier with a value from `[agent.aider] model`.
    /// When set this beats the per-role model in [`AgentRequest::model`] —
    /// users who configure a backend-specific model expect it to be used for
    /// every dispatch through that backend.
    pub fn with_model_override(mut self, model: impl Into<String>) -> Self {
        self.model_override = Some(model.into());
        self
    }

    /// Path to the binary this agent will invoke.
    pub fn binary(&self) -> &PathBuf {
        &self.binary
    }
}

impl Default for AiderAgent {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Agent for AiderAgent {
    fn name(&self) -> &str {
        "aider"
    }

    async fn run(
        &self,
        req: AgentRequest,
        events: mpsc::Sender<AgentEvent>,
        cancel: CancellationToken,
    ) -> Result<AgentOutcome> {
        let log_path = req.log_path.clone();
        let cmd = self.build_command(&req);

        // Raw subprocess events flow through `raw_*` then a forwarder turns
        // each stdout line into the appropriate semantic AgentEvent. Stderr is
        // forwarded verbatim and tee'd to a buffer so we can quote it back in
        // the StopReason::Error message on a bad exit.
        let (raw_tx, mut raw_rx) = mpsc::channel::<AgentEvent>(64);
        let outbound = events.clone();
        let forwarder = tokio::spawn(async move {
            let mut tokens = TokenUsage::default();
            let mut stderr_tail: Vec<String> = Vec::new();
            while let Some(ev) = raw_rx.recv().await {
                match ev {
                    AgentEvent::Stdout(line) => {
                        handle_stdout_line(&line, &outbound, &mut tokens).await;
                    }
                    AgentEvent::Stderr(line) => {
                        push_tail(&mut stderr_tail, line.clone(), ERROR_TAIL_LINES);
                        let _ = outbound.send(AgentEvent::Stderr(line)).await;
                    }
                    other => {
                        let _ = outbound.send(other).await;
                    }
                }
            }
            // Emit one TokenDelta with the grand total at the end so the
            // runner's accumulator doesn't double-count interim "Tokens:"
            // reports the model prints across turns.
            if tokens.input > 0 || tokens.output > 0 {
                let _ = outbound.send(AgentEvent::TokenDelta(tokens.clone())).await;
            }
            ForwarderResult {
                tokens,
                stderr_tail,
            }
        });

        let sub_outcome: SubprocessOutcome =
            subprocess::run_logged(cmd, &log_path, raw_tx, cancel, req.timeout).await?;
        let ForwarderResult {
            mut tokens,
            stderr_tail,
        } = forwarder.await.unwrap_or(ForwarderResult {
            tokens: TokenUsage::default(),
            stderr_tail: Vec::new(),
        });
        // by_role isn't populated by the model itself — re-key once here so
        // the runner doesn't have to special-case Aider's outcome shape.
        if tokens.input > 0 || tokens.output > 0 {
            tokens
                .by_role
                .entry(req.role.as_str().to_string())
                .or_default();
            let entry = tokens
                .by_role
                .get_mut(req.role.as_str())
                .expect("just inserted");
            entry.input = tokens.input;
            entry.output = tokens.output;
        }

        let stop_reason = match sub_outcome.stop_reason {
            StopReason::Completed => {
                if sub_outcome.exit_code == 0 {
                    StopReason::Completed
                } else {
                    StopReason::Error(format_error_message(sub_outcome.exit_code, &stderr_tail))
                }
            }
            // Pass the terminator decided by the subprocess helper through
            // unchanged — timeout and cancel beat any error inferred from
            // partial output.
            other => other,
        };

        Ok(AgentOutcome {
            exit_code: sub_outcome.exit_code,
            stop_reason,
            tokens,
            log_path,
        })
    }
}

impl AiderAgent {
    fn build_command(&self, req: &AgentRequest) -> Command {
        let mut cmd = Command::new(&self.binary);
        cmd.current_dir(&req.workdir);
        if !req.env.is_empty() {
            cmd.envs(req.env.iter());
        }
        // Unattended-friendly defaults: no TTY-only formatting, no streaming
        // chunks (we want whole lines), auto-confirm every prompt, suppress
        // first-run / version-check / model-warning interactivity. Anything a
        // workspace needs to flip can be supplied via [`Self::with_extra_args`].
        cmd.args([
            "--no-pretty",
            "--no-stream",
            "--yes-always",
            "--no-check-update",
            "--no-show-model-warnings",
            "--analytics-disable",
        ]);
        let model = self.model_override.as_deref().unwrap_or(&req.model);
        cmd.args(["--model", model]);
        for arg in &self.extra_args {
            cmd.arg(arg);
        }
        cmd.arg("--message").arg(build_message_payload(req));
        cmd
    }
}

fn build_message_payload(req: &AgentRequest) -> String {
    let mut out = String::new();
    if !req.system_prompt.is_empty() {
        out.push_str(&req.system_prompt);
        out.push_str("\n\n");
    }
    out.push_str(&req.user_prompt);
    out
}

struct ForwarderResult {
    tokens: TokenUsage,
    stderr_tail: Vec<String>,
}

async fn handle_stdout_line(
    line: &str,
    outbound: &mpsc::Sender<AgentEvent>,
    tokens: &mut TokenUsage,
) {
    // Forward the raw line verbatim regardless — aider's plain-text output is
    // the user-facing narrative and the dashboard wants it all. The structured
    // events below are emitted *in addition* to the Stdout event so the runner
    // sees both.
    let _ = outbound.send(AgentEvent::Stdout(line.to_string())).await;

    let trimmed = line.trim_start();
    if let Some(rest) = trimmed.strip_prefix("Applied edit to ") {
        if !rest.trim().is_empty() {
            let _ = outbound.send(AgentEvent::ToolUse("edit".to_string())).await;
        }
        return;
    }
    if let Some(rest) = trimmed.strip_prefix("Commit ") {
        if !rest.trim().is_empty() {
            let _ = outbound
                .send(AgentEvent::ToolUse("commit".to_string()))
                .await;
        }
        return;
    }
    if let Some(rest) = trimmed.strip_prefix("Tokens:") {
        if let Some((sent, received)) = parse_token_report(rest) {
            // Aider prints a cumulative-per-turn total. The runner's outer
            // aggregator sums `TokenDelta`s, so we overwrite the running
            // counter here and emit a single delta at end-of-stream.
            tokens.input = sent;
            tokens.output = received;
        }
    }
}

/// Parse the body of an `Tokens: <sent> sent, <received> received` line.
///
/// Returns `(sent, received)` on success, or `None` if either count is missing
/// or unrecognized. Accepts plain integers (`800`), `k` suffix (`1.2k`), and
/// `M` suffix (`1.5M`); aider prints decimals with one fractional digit so we
/// support that. Whitespace and a trailing `.` are tolerated.
fn parse_token_report(rest: &str) -> Option<(u64, u64)> {
    let cleaned: String = rest
        .chars()
        .filter(|c| !matches!(c, '\u{1b}'))
        .collect::<String>();
    let normalized = cleaned.trim().trim_end_matches('.');
    // Pattern shape: "<sent> sent, <received> received"
    let (sent_part, rest_part) = normalized.split_once(" sent")?;
    let sent = parse_token_count(sent_part.trim())?;
    let received_part = rest_part
        .trim_start_matches(',')
        .trim_start()
        .split_once(" received")
        .map(|(n, _)| n)?;
    let received = parse_token_count(received_part.trim())?;
    Some((sent, received))
}

fn parse_token_count(raw: &str) -> Option<u64> {
    let raw = raw.trim();
    if raw.is_empty() {
        return None;
    }
    let (digits, multiplier) = match raw.as_bytes().last().copied() {
        Some(b'k') | Some(b'K') => (&raw[..raw.len() - 1], 1_000.0_f64),
        Some(b'm') | Some(b'M') => (&raw[..raw.len() - 1], 1_000_000.0_f64),
        _ => (raw, 1.0_f64),
    };
    let digits = digits.replace(',', "");
    let value: f64 = digits.parse().ok()?;
    if !value.is_finite() || value < 0.0 {
        return None;
    }
    Some((value * multiplier).round() as u64)
}

fn push_tail(buf: &mut Vec<String>, line: String, max: usize) {
    if buf.len() == max {
        buf.remove(0);
    }
    buf.push(line);
}

fn format_error_message(exit_code: i32, stderr_tail: &[String]) -> String {
    let mut out = format!("aider exited with code {}", exit_code);
    if !stderr_tail.is_empty() {
        out.push_str("\nstderr tail:\n");
        for line in stderr_tail {
            out.push_str(line);
            out.push('\n');
        }
    }
    out
}

#[cfg(all(test, unix))]
mod tests {
    use super::*;
    use crate::agent::Role;
    use std::path::PathBuf;
    use std::time::Duration;

    fn fixture_path(name: &str) -> PathBuf {
        let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        manifest
            .join("tests")
            .join("fixtures")
            .join("aider")
            .join(name)
    }

    fn req_with_log(log_path: PathBuf, timeout: Duration) -> AgentRequest {
        AgentRequest {
            role: Role::Implementer,
            model: "anthropic/sonnet-4.5".into(),
            system_prompt: "be brief".into(),
            user_prompt: "say hi".into(),
            workdir: std::env::temp_dir(),
            log_path,
            timeout,
            env: std::collections::HashMap::new(),
        }
    }

    async fn drain<T>(mut rx: mpsc::Receiver<T>) -> Vec<T> {
        let mut out = Vec::new();
        while let Some(v) = rx.recv().await {
            out.push(v);
        }
        out
    }

    #[tokio::test]
    async fn parses_edits_commit_and_token_report() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("run.log");
        let agent = AiderAgent::with_binary(fixture_path("fake-aider-success.sh"));
        let (tx, rx) = mpsc::channel(64);
        let cancel = CancellationToken::new();
        let outcome = agent
            .run(
                req_with_log(log.clone(), Duration::from_secs(5)),
                tx,
                cancel,
            )
            .await
            .unwrap();
        assert_eq!(outcome.stop_reason, StopReason::Completed);
        assert_eq!(outcome.exit_code, 0);

        let evs = drain(rx).await;
        let stdouts: Vec<&str> = evs
            .iter()
            .filter_map(|e| match e {
                AgentEvent::Stdout(s) => Some(s.as_str()),
                _ => None,
            })
            .collect();
        let tool_uses: Vec<&str> = evs
            .iter()
            .filter_map(|e| match e {
                AgentEvent::ToolUse(s) => Some(s.as_str()),
                _ => None,
            })
            .collect();
        let token_deltas: Vec<&TokenUsage> = evs
            .iter()
            .filter_map(|e| match e {
                AgentEvent::TokenDelta(t) => Some(t),
                _ => None,
            })
            .collect();

        // Plain-text narrative is forwarded verbatim. The exact framing lines
        // (banner, model line) are loose, so we just check the assistant
        // sentence reaches the dashboard.
        assert!(
            stdouts.iter().any(|s| s.contains("Hello from Aider")),
            "missing assistant text: {stdouts:?}"
        );
        // Two `Applied edit to` lines → two "edit" ToolUse events; one
        // `Commit ...` line → one "commit" ToolUse event, in document order.
        assert_eq!(tool_uses, vec!["edit", "edit", "commit"]);
        assert_eq!(token_deltas.len(), 1);
        let total = token_deltas[0];
        // From fixture: "Tokens: 1.2k sent, 800 received" → 1200 / 800.
        assert_eq!(total.input, 1200);
        assert_eq!(total.output, 800);

        // by_role re-keyed onto Role::Implementer at the agent level.
        assert_eq!(outcome.tokens.input, 1200);
        assert_eq!(outcome.tokens.output, 800);
        let role_usage = outcome
            .tokens
            .by_role
            .get("implementer")
            .expect("implementer role usage");
        assert_eq!(role_usage.input, 1200);
        assert_eq!(role_usage.output, 800);

        // Log file should contain the raw plain-text output for post-mortem.
        let log_text = std::fs::read_to_string(&log).unwrap();
        assert!(
            log_text.contains("Applied edit to src/foo.rs"),
            "{log_text}"
        );
        assert!(log_text.contains("Commit a1b2c3d"), "{log_text}");
    }

    #[tokio::test]
    async fn noop_run_emits_no_tool_use_but_token_delta_still_fires() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("run.log");
        let agent = AiderAgent::with_binary(fixture_path("fake-aider-noop.sh"));
        let (tx, rx) = mpsc::channel(64);
        let cancel = CancellationToken::new();
        let outcome = agent
            .run(req_with_log(log, Duration::from_secs(5)), tx, cancel)
            .await
            .unwrap();
        assert_eq!(outcome.stop_reason, StopReason::Completed);

        let evs = drain(rx).await;
        let tool_uses: Vec<&str> = evs
            .iter()
            .filter_map(|e| match e {
                AgentEvent::ToolUse(s) => Some(s.as_str()),
                _ => None,
            })
            .collect();
        assert!(
            tool_uses.is_empty(),
            "no-op run should produce no tool-use events, got {tool_uses:?}"
        );
        // Still expect a single TokenDelta with the small-but-nonzero counts.
        let token_deltas: Vec<&TokenUsage> = evs
            .iter()
            .filter_map(|e| match e {
                AgentEvent::TokenDelta(t) => Some(t),
                _ => None,
            })
            .collect();
        assert_eq!(token_deltas.len(), 1);
        assert_eq!(token_deltas[0].input, 320);
        assert_eq!(token_deltas[0].output, 45);
    }

    #[tokio::test]
    async fn nonzero_exit_maps_to_error_with_stderr_tail() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("run.log");
        let agent = AiderAgent::with_binary(fixture_path("fake-aider-crash.sh"));
        let (tx, _rx) = mpsc::channel(64);
        let cancel = CancellationToken::new();
        let outcome = agent
            .run(req_with_log(log, Duration::from_secs(5)), tx, cancel)
            .await
            .unwrap();
        match outcome.stop_reason {
            StopReason::Error(msg) => {
                assert!(msg.contains("exit"), "{msg}");
                assert!(
                    msg.contains("ANTHROPIC_API_KEY"),
                    "expected stderr tail in error message, got: {msg}"
                );
            }
            other => panic!("expected Error, got {other:?}"),
        }
        assert_eq!(outcome.exit_code, 1);
    }

    #[tokio::test]
    async fn cancellation_propagates_to_child_process() {
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("run.log");
        let agent = AiderAgent::with_binary(fixture_path("fake-aider-hang.sh"));
        let (tx, _rx) = mpsc::channel(64);
        let cancel = CancellationToken::new();
        let canceler = cancel.clone();
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(80)).await;
            canceler.cancel();
        });
        let outcome = agent
            .run(req_with_log(log, Duration::from_secs(30)), tx, cancel)
            .await
            .unwrap();
        assert_eq!(outcome.stop_reason, StopReason::Cancelled);
        assert_eq!(outcome.exit_code, -1);
    }

    #[tokio::test]
    async fn build_command_includes_required_flags_and_workdir() {
        let agent = AiderAgent::with_binary("/usr/local/bin/aider")
            .with_extra_args(vec![
                "--no-auto-commits".into(),
                "--map-tokens".into(),
                "0".into(),
            ])
            .with_model_override("anthropic/opus-4.5");
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("run.log");
        let req = AgentRequest {
            role: Role::Auditor,
            model: "ignored-because-override".into(),
            system_prompt: "system body".into(),
            user_prompt: "user body".into(),
            workdir: dir.path().to_path_buf(),
            log_path: log,
            timeout: Duration::from_secs(1),
            env: std::collections::HashMap::new(),
        };
        let cmd = agent.build_command(&req);
        let std_cmd = cmd.as_std();
        let args: Vec<String> = std_cmd
            .get_args()
            .map(|a| a.to_string_lossy().into_owned())
            .collect();
        // Unattended defaults are present.
        assert!(args.iter().any(|a| a == "--no-pretty"));
        assert!(args.iter().any(|a| a == "--no-stream"));
        assert!(args.iter().any(|a| a == "--yes-always"));
        assert!(args.iter().any(|a| a == "--no-check-update"));
        assert!(args.iter().any(|a| a == "--no-show-model-warnings"));
        assert!(args.iter().any(|a| a == "--analytics-disable"));
        // Model override beats AgentRequest::model.
        assert!(args
            .windows(2)
            .any(|w| w[0] == "--model" && w[1] == "anthropic/opus-4.5"));
        assert!(!args.iter().any(|a| a == "ignored-because-override"));
        // Extra args spliced in before --message, in declared order.
        assert!(args.iter().any(|a| a == "--no-auto-commits"));
        assert!(args
            .windows(2)
            .any(|w| w[0] == "--map-tokens" && w[1] == "0"));
        // The prompt body is concatenated system + blank line + user, passed
        // via the trailing `--message` flag.
        let msg_idx = args
            .iter()
            .position(|a| a == "--message")
            .expect("--message flag must be present");
        let body = &args[msg_idx + 1];
        assert!(body.starts_with("system body\n\n"));
        assert!(body.ends_with("user body"));
        assert_eq!(std_cmd.get_program(), "/usr/local/bin/aider");
        assert_eq!(std_cmd.get_current_dir(), Some(dir.path()));
    }

    #[tokio::test]
    async fn build_command_uses_request_model_when_no_override() {
        let agent = AiderAgent::with_binary("aider");
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("run.log");
        let req = AgentRequest {
            role: Role::Implementer,
            model: "anthropic/sonnet-4.5".into(),
            system_prompt: String::new(),
            user_prompt: "u".into(),
            workdir: dir.path().to_path_buf(),
            log_path: log,
            timeout: Duration::from_secs(1),
            env: std::collections::HashMap::new(),
        };
        let cmd = agent.build_command(&req);
        let args: Vec<String> = cmd
            .as_std()
            .get_args()
            .map(|a| a.to_string_lossy().into_owned())
            .collect();
        assert!(args
            .windows(2)
            .any(|w| w[0] == "--model" && w[1] == "anthropic/sonnet-4.5"));
    }

    #[test]
    fn build_message_payload_concatenates_system_and_user_with_blank_line() {
        let req = AgentRequest {
            role: Role::Implementer,
            model: "x".into(),
            system_prompt: "you are a careful engineer".into(),
            user_prompt: "implement phase 03".into(),
            workdir: std::env::temp_dir(),
            log_path: std::env::temp_dir().join("never.log"),
            timeout: Duration::from_secs(1),
            env: std::collections::HashMap::new(),
        };
        let payload = build_message_payload(&req);
        assert!(payload.starts_with("you are a careful engineer\n\n"));
        assert!(payload.contains("implement phase 03"));
    }

    #[test]
    fn build_message_payload_omits_system_when_empty() {
        let req = AgentRequest {
            role: Role::Implementer,
            model: "x".into(),
            system_prompt: String::new(),
            user_prompt: "just the user body".into(),
            workdir: std::env::temp_dir(),
            log_path: std::env::temp_dir().join("never.log"),
            timeout: Duration::from_secs(1),
            env: std::collections::HashMap::new(),
        };
        let payload = build_message_payload(&req);
        assert_eq!(payload, "just the user body");
    }

    #[test]
    fn parses_token_count_with_k_and_m_suffixes_and_decimals() {
        assert_eq!(parse_token_count("800"), Some(800));
        assert_eq!(parse_token_count("1.2k"), Some(1200));
        assert_eq!(parse_token_count("3K"), Some(3000));
        assert_eq!(parse_token_count("1.5M"), Some(1_500_000));
        assert_eq!(parse_token_count("2,400"), Some(2400));
        assert_eq!(parse_token_count(""), None);
        assert_eq!(parse_token_count("abc"), None);
    }

    #[test]
    fn parses_token_report_full_line() {
        // Body of the `Tokens:` line (parser strips the prefix before calling).
        let (s, r) = parse_token_report(" 1.2k sent, 800 received.").unwrap();
        assert_eq!(s, 1200);
        assert_eq!(r, 800);
        let (s, r) = parse_token_report(" 320 sent, 45 received.").unwrap();
        assert_eq!(s, 320);
        assert_eq!(r, 45);
        // Missing parts → None.
        assert!(parse_token_report(" garbage").is_none());
        assert!(parse_token_report(" 100 sent").is_none());
    }

    /// Real end-to-end test against the actual `aider` binary on PATH.
    /// Skipped unless `PITBOSS_REAL_AGENT_TESTS=1` so CI doesn't burn tokens.
    #[tokio::test]
    async fn real_aider_smoke_test() {
        if std::env::var("PITBOSS_REAL_AGENT_TESTS").ok().as_deref() != Some("1") {
            eprintln!("skipping real_aider_smoke_test (set PITBOSS_REAL_AGENT_TESTS=1 to run)");
            return;
        }
        let dir = tempfile::tempdir().unwrap();
        let log = dir.path().join("run.log");
        let agent =
            AiderAgent::new().with_extra_args(vec!["--no-auto-commits".into(), "--no-git".into()]);
        let (tx, _rx) = mpsc::channel(64);
        let cancel = CancellationToken::new();
        let req = AgentRequest {
            role: Role::Implementer,
            model: "anthropic/sonnet-4.5".into(),
            system_prompt: String::new(),
            user_prompt: "respond with the single word OK".into(),
            workdir: dir.path().to_path_buf(),
            log_path: log,
            timeout: Duration::from_secs(120),
            env: std::collections::HashMap::new(),
        };
        let outcome = agent.run(req, tx, cancel).await.unwrap();
        assert!(
            matches!(outcome.stop_reason, StopReason::Completed),
            "real aider run did not complete: {:?}",
            outcome.stop_reason
        );
        assert_eq!(outcome.exit_code, 0);
    }
}