apollo-agent 0.5.0

Local-first Rust AI agent runtime — Telegram-first, trait-driven, SurrealDB + RocksDB state layer.
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
//! Build runner — orchestrates cargo build/test/run cycles.
//!
//! Wraps `cargo build` and `cargo test` with structured error parsing, retry,
//! and timeout handling. Uses `tokio::process::Command` with the same
//! environment scrubbing as the shell tool so secrets never reach the child.
//!
//! Designed to be driven by the agent loop (via `BuildRunnerTool`) or by the
//! autonomous loop's validation step, replacing the bare `sh -c` test runner
//! with one that returns compiler diagnostics in a structured form the model
//! can act on directly.

use std::path::PathBuf;
use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::tools::child_proc;

/// Maximum retries before giving up. A retry only makes sense when the build
/// failed for a transient reason (a stale lock, a race with another cargo
/// invocation); compiler errors are deterministic and retrying them just
/// burns time. The runner still retries unconditionally because the cost of a
/// false retry is one wasted build, while the cost of not retrying a transient
/// failure is a spurious red.
const DEFAULT_MAX_RETRIES: usize = 2;

/// Upper bound on a single cargo invocation. A release build of a large
/// workspace can take minutes; an hour is well past any legitimate single
/// compile and signals a stuck process.
const DEFAULT_TIMEOUT_SECS: u64 = 1800;

/// Configuration for the build runner.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BuildRunnerConfig {
    /// Maximum number of retries on a failed build/test.
    pub max_retries: usize,
    /// Per-invocation timeout in seconds.
    pub timeout_secs: u64,
    /// Extra arguments appended to every `cargo` invocation
    /// (e.g. `["--release"]`).
    #[serde(default)]
    pub extra_args: Vec<String>,
    /// Package filter passed as `cargo <cmd> -p <name>`. Empty = whole workspace.
    #[serde(default)]
    pub package: String,
}

impl Default for BuildRunnerConfig {
    fn default() -> Self {
        Self {
            max_retries: DEFAULT_MAX_RETRIES,
            timeout_secs: DEFAULT_TIMEOUT_SECS,
            extra_args: Vec::new(),
            package: String::new(),
        }
    }
}

/// Severity of a parsed compiler diagnostic.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DiagnosticSeverity {
    Error,
    Warning,
    Note,
}

/// A single parsed compiler diagnostic.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompileError {
    pub severity: DiagnosticSeverity,
    /// Rust error code when present (e.g. `E0308`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
    /// The headline message (first line of the diagnostic).
    pub message: String,
    /// Source file, when the diagnostic carries a `--> path:line:col` span.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub line: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub column: Option<u32>,
}

/// Result of a single build or test invocation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BuildResult {
    pub success: bool,
    pub exit_code: i32,
    /// Which attempt this is (1-based); >1 means a retry fired.
    pub attempt: usize,
    pub errors: Vec<CompileError>,
    pub warnings: Vec<CompileError>,
    /// Raw combined stdout+stderr, truncated for size.
    pub raw_output: String,
    /// `cargo build`, `cargo test`, etc.
    pub command: String,
}

impl BuildResult {
    /// A compact, model-friendly summary of the diagnostics.
    pub fn summary(&self) -> String {
        if self.success {
            return format!("{} succeeded (attempt {})", self.command, self.attempt);
        }
        let mut out = format!(
            "{} failed (exit {}, attempt {})\n",
            self.command, self.exit_code, self.attempt
        );
        if !self.errors.is_empty() {
            out.push_str(&format!("--- {} error(s) ---\n", self.errors.len()));
            for e in &self.errors {
                out.push_str(&format_error(e));
                out.push('\n');
            }
        }
        if !self.warnings.is_empty() {
            out.push_str(&format!("--- {} warning(s) ---\n", self.warnings.len()));
            for w in &self.warnings {
                out.push_str(&format_error(w));
                out.push('\n');
            }
        }
        out
    }
}

/// The build/run orchestrator.
pub struct BuildRunner {
    workspace: PathBuf,
    config: BuildRunnerConfig,
}

impl BuildRunner {
    pub fn new(workspace: PathBuf, config: BuildRunnerConfig) -> Self {
        Self { workspace, config }
    }

    pub fn with_workspace(mut self, workspace: PathBuf) -> Self {
        self.workspace = workspace;
        self
    }

    /// Run `cargo build` with retries. Returns the last attempt's result.
    pub async fn run_build(&self) -> BuildResult {
        self.run_with_retry("build").await
    }

    /// Run `cargo test` with retries. Returns the last attempt's result.
    pub async fn run_test(&self) -> BuildResult {
        self.run_with_retry("test").await
    }

    /// Run `cargo build` then `cargo test`. Stops after the build if it
    /// fails — testing a broken compile is wasted time.
    pub async fn run_cycle(&self) -> (BuildResult, Option<BuildResult>) {
        let build = self.run_build().await;
        if !build.success {
            return (build, None);
        }
        let test = self.run_test().await;
        (build, Some(test))
    }

    /// Run a cargo subcommand, retrying up to `max_retries` times on failure.
    async fn run_with_retry(&self, subcommand: &str) -> BuildResult {
        let max_attempts = self.config.max_retries.saturating_add(1);
        let mut last = BuildResult {
            success: false,
            exit_code: -1,
            attempt: 0,
            errors: Vec::new(),
            warnings: Vec::new(),
            raw_output: String::new(),
            command: format!("cargo {}", subcommand),
        };

        for attempt in 1..=max_attempts {
            let result = self.run_once(subcommand, attempt).await;
            if result.success {
                return result;
            }
            tracing::warn!(
                "[build_runner] {} attempt {} failed (exit {})",
                subcommand,
                attempt,
                result.exit_code
            );
            last = result;
        }
        last
    }

    /// A single cargo invocation with timeout and output parsing.
    async fn run_once(&self, subcommand: &str, attempt: usize) -> BuildResult {
        let mut cmd = tokio::process::Command::new("cargo");
        cmd.arg(subcommand)
            .current_dir(&self.workspace)
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .kill_on_drop(true);
        if !self.config.package.is_empty() {
            cmd.arg("-p").arg(&self.config.package);
        }
        for arg in &self.config.extra_args {
            cmd.arg(arg);
        }
        child_proc::scrub(&mut cmd);

        let mut child = match cmd.spawn() {
            Ok(c) => c,
            Err(e) => {
                return BuildResult {
                    success: false,
                    exit_code: -1,
                    attempt,
                    errors: vec![CompileError {
                        severity: DiagnosticSeverity::Error,
                        code: None,
                        message: format!("failed to spawn cargo: {e}"),
                        file: None,
                        line: None,
                        column: None,
                    }],
                    warnings: Vec::new(),
                    raw_output: format!("failed to spawn cargo: {e}"),
                    command: format!("cargo {}", subcommand),
                };
            }
        };

        let timeout = Duration::from_secs(self.config.timeout_secs);
        let output = match child_proc::wait_with_timeout(&mut child, timeout).await {
            Ok(Some(o)) => o,
            Ok(None) => {
                return BuildResult {
                    success: false,
                    exit_code: -1,
                    attempt,
                    errors: vec![CompileError {
                        severity: DiagnosticSeverity::Error,
                        code: None,
                        message: format!(
                            "cargo {} timed out after {}s",
                            subcommand,
                            timeout.as_secs()
                        ),
                        file: None,
                        line: None,
                        column: None,
                    }],
                    warnings: Vec::new(),
                    raw_output: format!("timed out after {}s", timeout.as_secs()),
                    command: format!("cargo {}", subcommand),
                };
            }
            Err(e) => {
                return BuildResult {
                    success: false,
                    exit_code: -1,
                    attempt,
                    errors: vec![CompileError {
                        severity: DiagnosticSeverity::Error,
                        code: None,
                        message: format!("cargo {} io error: {e}", subcommand),
                        file: None,
                        line: None,
                        column: None,
                    }],
                    warnings: Vec::new(),
                    raw_output: format!("io error: {e}"),
                    command: format!("cargo {}", subcommand),
                };
            }
        };

        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        let combined = if stdout.is_empty() && !stderr.is_empty() {
            stderr.to_string()
        } else if !stderr.is_empty() {
            format!("{}\n{}", stdout, stderr)
        } else {
            stdout.to_string()
        };

        let (errors, warnings) = parse_diagnostics(&combined);
        let exit_code = output.status.code().unwrap_or(-1);
        let success = output.status.success();

        let raw = match crate::text::truncate_chars_counted(&combined, 20_000) {
            Some((head, dropped)) => format!("{}...\n[truncated {} chars]", head, dropped),
            None => combined,
        };

        BuildResult {
            success,
            exit_code,
            attempt,
            errors,
            warnings,
            raw_output: raw,
            command: format!("cargo {}", subcommand),
        }
    }
}

/// Format a single diagnostic for the summary.
fn format_error(e: &CompileError) -> String {
    let sev = match e.severity {
        DiagnosticSeverity::Error => "error",
        DiagnosticSeverity::Warning => "warning",
        DiagnosticSeverity::Note => "note",
    };
    let code = e
        .code
        .as_ref()
        .map(|c| format!("[{}]", c))
        .unwrap_or_default();
    let loc = match (&e.file, e.line, e.column) {
        (Some(f), Some(l), Some(c)) => format!(" {}:{}:{}", f, l, c),
        (Some(f), Some(l), None) => format!(" {}:{}", f, l),
        _ => String::new(),
    };
    format!("{}{}: {}{}", sev, code, e.message, loc)
}

/// Parse cargo/rustc diagnostic output into structured errors and warnings.
///
/// Recognises the standard rustc format:
///
/// ```text
/// error[E0308]: mismatched types
///   --> src/main.rs:10:5
/// ```
///
/// and the `warning:` / `note:` variants. Lines without a `-->` span still
/// produce a diagnostic (with `file`/`line`/`column` set to `None`) so the
/// caller sees the headline message.
pub fn parse_diagnostics(output: &str) -> (Vec<CompileError>, Vec<CompileError>) {
    let mut errors = Vec::new();
    let mut warnings = Vec::new();

    let lines: Vec<&str> = output.lines().collect();
    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];

        if let Some((severity, rest)) = parse_diagnostic_header(line) {
            let (code, message) = parse_code_and_message(rest);
            // Look ahead for a `--> path:line:col` span.
            let (file, line_no, col) = look_ahead_for_span(&lines, i + 1);
            let diag = CompileError {
                severity: severity.clone(),
                code,
                message,
                file,
                line: line_no,
                column: col,
            };
            match severity {
                DiagnosticSeverity::Error => errors.push(diag),
                DiagnosticSeverity::Warning => warnings.push(diag),
                DiagnosticSeverity::Note => {}
            }
        }
        i += 1;
    }

    (errors, warnings)
}

/// Detect `error[...]:`, `warning:`, or `note:` headers.
///
/// Returns the severity and the remainder *after* the severity keyword,
/// including any `[CODE]` prefix, so `parse_code_and_message` can extract it.
fn parse_diagnostic_header(line: &str) -> Option<(DiagnosticSeverity, &str)> {
    let trimmed = line.trim_start();
    if let Some(rest) = trimmed.strip_prefix("error") {
        // `error:` or `error[E0308]: ...` — hand the rest (including the
        // code bracket) to parse_code_and_message.
        return Some((DiagnosticSeverity::Error, rest));
    }
    if let Some(rest) = trimmed.strip_prefix("warning") {
        return Some((DiagnosticSeverity::Warning, rest));
    }
    None
}

/// Split `[E0308]: mismatched types` into `(Some("E0308"), "mismatched types")`
/// when a code is present, otherwise `(None, message)` after stripping the
/// leading `: ` separator.
fn parse_code_and_message(rest: &str) -> (Option<String>, String) {
    if rest.starts_with('[') {
        if let Some(idx) = rest.find(']') {
            let code = rest[1..idx].to_string();
            let after = &rest[idx + 1..];
            let msg = after.strip_prefix(':').unwrap_or(after).trim_start();
            return (Some(code), msg.to_string());
        }
    }
    // `: could not compile` → strip the leading colon.
    let msg = rest.strip_prefix(':').unwrap_or(rest).trim_start();
    (None, msg.to_string())
}

/// Scan forward from `start` for a `--> path:line:col` span line.
fn look_ahead_for_span(lines: &[&str], start: usize) -> (Option<String>, Option<u32>, Option<u32>) {
    for line in &lines[start..] {
        let trimmed = line.trim_start();
        if let Some(rest) = trimmed.strip_prefix("-->") {
            return parse_span(rest.trim());
        }
        // A new diagnostic header before a span means this one had none.
        if trimmed.starts_with("error") || trimmed.starts_with("warning") {
            return (None, None, None);
        }
    }
    (None, None, None)
}

/// Parse `src/main.rs:10:5` into file/line/column.
fn parse_span(span: &str) -> (Option<String>, Option<u32>, Option<u32>) {
    // The span may carry a trailing note like `src/main.rs:10:5:13:20` or
    // `src/main.rs:10:5`. Take the first three colon-separated parts.
    let parts: Vec<&str> = span.splitn(3, ':').collect();
    if parts.is_empty() {
        return (None, None, None);
    }
    let file = Some(parts[0].to_string());
    let line = parts.get(1).and_then(|s| s.parse().ok());
    let column = parts.get(2).and_then(|s| s.parse().ok());
    (file, line, column)
}

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

    const SAMPLE_OUTPUT: &str = "\
   Compiling apollo v0.4.0 (/Users/undivisible/projects/apollo)
error[E0308]: mismatched types
  --> src/main.rs:10:5
   |
10 |     let x: i32 = \"hello\";
   |                 ^^^^^^^ expected `i32`, found `&str`

warning: unused variable: `y`
  --> src/main.rs:5:9
   |
5  |     let y = 1;
   |         ^^^

error[E0425]: cannot find value `z` in this scope
  --> src/main.rs:12:13
   |
12 |     println!(\"{}\", z);
   |                     ^ not found in this scope
";

    #[test]
    fn parses_errors_and_warnings() {
        let (errors, warnings) = parse_diagnostics(SAMPLE_OUTPUT);
        assert_eq!(errors.len(), 2);
        assert_eq!(warnings.len(), 1);

        assert_eq!(errors[0].code.as_deref(), Some("E0308"));
        assert_eq!(errors[0].message, "mismatched types");
        assert_eq!(errors[0].file.as_deref(), Some("src/main.rs"));
        assert_eq!(errors[0].line, Some(10));
        assert_eq!(errors[0].column, Some(5));

        assert_eq!(errors[1].code.as_deref(), Some("E0425"));
        assert_eq!(errors[1].file.as_deref(), Some("src/main.rs"));
        assert_eq!(errors[1].line, Some(12));
        assert_eq!(errors[1].column, Some(13));

        assert_eq!(warnings[0].severity, DiagnosticSeverity::Warning);
        assert_eq!(warnings[0].message, "unused variable: `y`");
        assert_eq!(warnings[0].line, Some(5));
    }

    #[test]
    fn handles_error_without_code() {
        let output = "error: could not compile `foo` due to 1 previous error";
        let (errors, _) = parse_diagnostics(output);
        assert_eq!(errors.len(), 1);
        assert!(errors[0].code.is_none());
        assert!(errors[0].file.is_none());
        assert!(errors[0].line.is_none());
    }

    #[test]
    fn handles_error_without_span() {
        let output = "error[E0308]: mismatched types\nnote: run with `RUST_BACKTRACE=1`";
        let (errors, _) = parse_diagnostics(output);
        assert_eq!(errors.len(), 1);
        assert!(errors[0].file.is_none());
        assert!(errors[0].line.is_none());
    }

    #[test]
    fn summary_lists_errors() {
        let (errors, warnings) = parse_diagnostics(SAMPLE_OUTPUT);
        let result = BuildResult {
            success: false,
            exit_code: 101,
            attempt: 1,
            errors,
            warnings,
            raw_output: String::new(),
            command: "cargo build".to_string(),
        };
        let s = result.summary();
        assert!(s.contains("cargo build failed"));
        assert!(s.contains("2 error(s)"));
        assert!(s.contains("1 warning(s)"));
        assert!(s.contains("E0308"));
        assert!(s.contains("src/main.rs:10:5"));
    }

    #[test]
    fn summary_for_success() {
        let result = BuildResult {
            success: true,
            exit_code: 0,
            attempt: 1,
            errors: Vec::new(),
            warnings: Vec::new(),
            raw_output: String::new(),
            command: "cargo test".to_string(),
        };
        assert_eq!(result.summary(), "cargo test succeeded (attempt 1)");
    }

    #[test]
    fn config_defaults_are_sane() {
        let c = BuildRunnerConfig::default();
        assert_eq!(c.max_retries, DEFAULT_MAX_RETRIES);
        assert_eq!(c.timeout_secs, DEFAULT_TIMEOUT_SECS);
        assert!(c.extra_args.is_empty());
        assert!(c.package.is_empty());
    }

    #[tokio::test]
    async fn run_build_on_nonexistent_workspace_reports_spawn_error() {
        let runner = BuildRunner::new(
            PathBuf::from("/nonexistent/path/that/does/not/exist"),
            BuildRunnerConfig {
                max_retries: 0,
                ..BuildRunnerConfig::default()
            },
        );
        let result = runner.run_build().await;
        assert!(!result.success);
        assert_eq!(result.attempt, 1);
        assert!(!result.errors.is_empty());
    }
}