battlecommand-forge 0.2.0

Quality-first AI coding army: single Rust binary that generates production-grade projects via a 9-stage TDD pipeline with a complexity-scaled quality gate (up to 9.2/10)
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
//! Self-improving memory system.
//!
//! After successful missions: distill learnings + save few-shot examples.
//! Before missions: load relevant context from learnings + examples.

use anyhow::Result;
use std::fs;
use std::path::{Path, PathBuf};

use crate::llm::LlmClient;

const LEARNINGS_FILE: &str = ".battlecommand/learnings.md";
const EXAMPLES_DIR: &str = ".battlecommand/examples";
const CONTEXT_FILE: &str = ".battlecommand/context.md";
const FAILURES_FILE: &str = ".battlecommand/failure_patterns.md";
const MAX_EXAMPLES: usize = 100;
const MAX_FAILURE_PATTERNS: usize = 30;

/// Load relevant context for a mission prompt.
/// Combines: context.md + matching learnings + relevant few-shot examples.
pub fn load_context(prompt: &str) -> String {
    let mut context = String::new();

    // Always load context.md
    if let Ok(ctx) = fs::read_to_string(CONTEXT_FILE) {
        context.push_str(&ctx);
        context.push_str("\n\n");
    }

    // Search learnings for relevant entries
    if let Ok(learnings) = fs::read_to_string(LEARNINGS_FILE) {
        let relevant = find_relevant_sections(&learnings, prompt, 3);
        if !relevant.is_empty() {
            context.push_str("## Relevant Learnings from Past Missions\n\n");
            context.push_str(&relevant);
            context.push_str("\n\n");
        }
    }

    // Find relevant few-shot examples
    let examples = find_relevant_examples(prompt, 2);
    if !examples.is_empty() {
        context.push_str("## Reference Examples (follow this style)\n\n");
        for (name, content) in &examples {
            context.push_str(&format!("### Example: {}\n```\n{}\n```\n\n", name, content));
        }
    }

    context
}

/// Save a learning after a successful mission.
pub async fn distill_and_save(
    llm: &LlmClient,
    prompt: &str,
    code_summary: &str,
    score: f32,
) -> Result<()> {
    // Distill the mission into a learning
    let system = "You are a knowledge distiller. Extract the key patterns, decisions, and \
                  pitfalls from this successful mission into 2-3 bullet points. \
                  Focus on reusable patterns, not specifics. Output ONLY bullet points.";
    let user_prompt = format!(
        "Mission: {}\nScore: {:.1}/10\nCode summary:\n{}",
        prompt, score, code_summary
    );

    let learning = llm
        .generate("DISTILL", system, &user_prompt)
        .await
        .unwrap_or_else(|_| format!("- Completed: {}", prompt));

    // Append to learnings file
    let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M");
    let entry = format!(
        "\n## [{}] {}\nScore: {:.1}/10\n{}\n",
        timestamp,
        prompt.chars().take(80).collect::<String>(),
        score,
        learning.trim()
    );

    let mut existing = fs::read_to_string(LEARNINGS_FILE).unwrap_or_default();
    existing.push_str(&entry);
    fs::write(LEARNINGS_FILE, existing)?;

    Ok(())
}

/// Save successful output files as a few-shot example.
pub fn save_example(prompt: &str, output_dir: &Path, language: &str) -> Result<()> {
    let examples_dir = Path::new(EXAMPLES_DIR);
    fs::create_dir_all(examples_dir)?;

    // Create example directory name
    let slug: String = prompt
        .to_lowercase()
        .chars()
        .filter(|c| c.is_alphanumeric() || *c == ' ')
        .collect::<String>()
        .split_whitespace()
        .take(4)
        .collect::<Vec<_>>()
        .join("_");

    let example_dir = examples_dir.join(format!("{}_{}", language, slug));
    fs::create_dir_all(&example_dir)?;

    // Copy key source files (not __init__.py, not boilerplate)
    for entry in walkdir_source_files(output_dir, language)? {
        let relative = entry.strip_prefix(output_dir).unwrap_or(&entry);
        let dest = example_dir.join(relative);
        if let Some(parent) = dest.parent() {
            fs::create_dir_all(parent)?;
        }
        let _ = fs::copy(&entry, &dest);
    }

    // Enforce max examples limit
    enforce_example_limit(examples_dir)?;

    Ok(())
}

/// Find sections in learnings.md that match the prompt keywords.
fn find_relevant_sections(learnings: &str, prompt: &str, max: usize) -> String {
    let lowered = prompt.to_lowercase();
    let keywords: Vec<&str> = lowered.split_whitespace().filter(|w| w.len() > 3).collect();

    let mut sections: Vec<(usize, &str)> = Vec::new();
    let mut current_section = String::new();
    let mut section_start = 0;

    for (i, line) in learnings.lines().enumerate() {
        if line.starts_with("## ") {
            if !current_section.is_empty() {
                let score = keywords
                    .iter()
                    .filter(|k| current_section.to_lowercase().contains(*k))
                    .count();
                if score > 0 {
                    let start = section_start;
                    let end = i;
                    let section_text = learnings
                        .lines()
                        .skip(start)
                        .take(end - start)
                        .collect::<Vec<_>>()
                        .join("\n");
                    sections.push((score, Box::leak(section_text.into_boxed_str())));
                }
            }
            current_section = line.to_string();
            section_start = i;
        } else {
            current_section.push('\n');
            current_section.push_str(line);
        }
    }

    sections.sort_by(|a, b| b.0.cmp(&a.0));
    sections
        .into_iter()
        .take(max)
        .map(|(_, s)| s)
        .collect::<Vec<_>>()
        .join("\n\n")
}

/// Find relevant few-shot examples by matching directory names to prompt keywords.
fn find_relevant_examples(prompt: &str, max: usize) -> Vec<(String, String)> {
    let examples_dir = Path::new(EXAMPLES_DIR);
    if !examples_dir.exists() {
        return vec![];
    }

    let keywords: Vec<String> = prompt
        .to_lowercase()
        .split_whitespace()
        .filter(|w| w.len() > 3)
        .map(String::from)
        .collect();

    let mut matches: Vec<(usize, String, String)> = Vec::new();

    if let Ok(entries) = fs::read_dir(examples_dir) {
        for entry in entries.flatten() {
            if !entry.path().is_dir() {
                continue;
            }
            let name = entry.file_name().to_string_lossy().to_string();
            let score = keywords
                .iter()
                .filter(|k| name.contains(k.as_str()))
                .count();

            if score > 0 {
                // Read the main source file from the example
                let content = read_example_summary(&entry.path());
                if !content.is_empty() {
                    matches.push((score, name, content));
                }
            }
        }
    }

    matches.sort_by(|a, b| b.0.cmp(&a.0));
    matches
        .into_iter()
        .take(max)
        .map(|(_, name, content)| (name, content))
        .collect()
}

/// Read a summary of an example (first source file, truncated).
fn read_example_summary(dir: &Path) -> String {
    let extensions = ["py", "ts", "js", "rs", "go"];
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_file() {
                if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                    if extensions.contains(&ext) {
                        if let Ok(content) = fs::read_to_string(&path) {
                            // Truncate to first 50 lines
                            return content.lines().take(50).collect::<Vec<_>>().join("\n");
                        }
                    }
                }
            }
        }
    }
    String::new()
}

/// Walk directory for source files (skip __init__.py, boilerplate).
fn walkdir_source_files(dir: &Path, language: &str) -> Result<Vec<PathBuf>> {
    let source_exts: Vec<&str> = match language {
        "python" => vec!["py"],
        "typescript" => vec!["ts", "tsx"],
        "javascript" => vec!["js", "jsx"],
        "rust" => vec!["rs"],
        "go" => vec!["go"],
        _ => vec!["py"],
    };

    let mut files = Vec::new();
    if dir.is_dir() {
        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_dir() {
                files.extend(walkdir_source_files(&path, language)?);
            } else if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                if source_exts.contains(&ext) {
                    let name = path.file_name().unwrap_or_default().to_string_lossy();
                    if name != "__init__.py" {
                        files.push(path);
                    }
                }
            }
        }
    }
    Ok(files)
}

/// Remove oldest examples if we exceed the limit.
fn enforce_example_limit(examples_dir: &Path) -> Result<()> {
    let mut entries: Vec<_> = fs::read_dir(examples_dir)?
        .flatten()
        .filter(|e| e.path().is_dir())
        .collect();

    if entries.len() <= MAX_EXAMPLES {
        return Ok(());
    }

    // Sort by modification time (oldest first)
    entries.sort_by_key(|e| {
        e.metadata()
            .and_then(|m| m.modified())
            .unwrap_or(std::time::SystemTime::UNIX_EPOCH)
    });

    // Remove oldest
    let to_remove = entries.len() - MAX_EXAMPLES;
    for entry in entries.into_iter().take(to_remove) {
        let _ = fs::remove_dir_all(entry.path());
    }

    Ok(())
}

/// Save failure patterns from a failed mission for future runs.
/// Extracts error categories from verifier output and stores them.
pub fn save_failure_patterns(language: &str, errors: &[String], score: f32) {
    if errors.is_empty() {
        return;
    }

    let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M");
    let mut entry = format!("\n## [{}] {} (score: {:.1})\n", timestamp, language, score);
    for err in errors.iter().take(10) {
        // Normalize errors into reusable patterns
        let pattern = normalize_error_pattern(err);
        if !pattern.is_empty() {
            entry.push_str(&format!("- {}\n", pattern));
        }
    }

    let mut existing = fs::read_to_string(FAILURES_FILE).unwrap_or_default();
    existing.push_str(&entry);

    // Trim to max patterns (keep most recent)
    let sections: Vec<&str> = existing.split("\n## ").collect();
    if sections.len() > MAX_FAILURE_PATTERNS {
        let kept: String = sections
            .iter()
            .skip(sections.len() - MAX_FAILURE_PATTERNS)
            .map(|s| format!("\n## {}", s))
            .collect();
        let _ = fs::write(FAILURES_FILE, kept.trim_start());
    } else {
        let _ = fs::write(FAILURES_FILE, &existing);
    }
}

/// Load failure patterns relevant to a language.
pub fn load_failure_patterns(language: &str) -> String {
    let content = match fs::read_to_string(FAILURES_FILE) {
        Ok(c) => c,
        Err(_) => return String::new(),
    };

    let mut patterns: Vec<String> = Vec::new();
    let mut in_matching_section = false;

    for line in content.lines() {
        if line.starts_with("## ") {
            in_matching_section = line.to_lowercase().contains(language);
        } else if in_matching_section && line.starts_with("- ") {
            let pattern = line[2..].trim().to_string();
            if !patterns.contains(&pattern) {
                patterns.push(pattern);
            }
        }
    }

    if patterns.is_empty() {
        return String::new();
    }

    // Deduplicate and limit
    patterns.truncate(15);
    let mut result =
        String::from("## Patterns from previous failed runs (DO NOT repeat these mistakes):\n");
    for p in &patterns {
        result.push_str(&format!("- {}\n", p));
    }
    result
}

/// Normalize an error message into a reusable pattern.
fn normalize_error_pattern(error: &str) -> String {
    let lower = error.to_lowercase();

    // Python-specific normalizations
    if lower.contains("modulenotfounderror") || lower.contains("importerror") {
        if lower.contains("pydantic") && lower.contains("basesettings") {
            return "Use pydantic_settings.BaseSettings, not pydantic.BaseSettings (Pydantic v2)"
                .to_string();
        }
        if lower.contains("no module named") {
            return format!(
                "Missing import: {}",
                error
                    .split("named")
                    .last()
                    .unwrap_or("")
                    .trim()
                    .trim_matches('\'')
            );
        }
    }
    if lower.contains("nameerror") {
        return format!(
            "Undefined name: {}",
            error
                .split("name")
                .last()
                .unwrap_or("")
                .trim()
                .trim_matches('\'')
        );
    }
    if lower.contains("attributeerror") {
        return format!("Wrong attribute/method: {}", error.trim());
    }
    if lower.contains("syntax error") || lower.contains("syntaxerror") {
        return "Python syntax error in generated code".to_string();
    }
    if lower.contains("hardcoded secret") || lower.contains("hardcoded") {
        return "Hardcoded secrets — use environment variables".to_string();
    }

    // Generic: keep short errors as-is
    if error.len() < 100 {
        error.trim().to_string()
    } else {
        error.trim().chars().take(100).collect::<String>()
    }
}

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

    #[test]
    fn test_load_context_missing_files() {
        // Should not panic even if files don't exist
        let ctx = load_context("build something");
        // May or may not have content depending on if .battlecommand exists
        let _ = ctx; // just verify it doesn't panic
    }

    #[test]
    fn test_find_relevant_examples_empty() {
        let examples = find_relevant_examples("nonexistent prompt xyz", 3);
        // May be empty if no examples dir exists
        assert!(examples.len() <= 3);
    }
}