collet 0.1.0

Relentless agentic coding orchestrator with zero-drop agent loops
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
//! Skillforge — default evolution engine.
//!
//! LLM-driven workspace mutation with EGL gating.  Analyzes observation
//! batches and proposes mutations to the workspace: new or improved skills,
//! prompt refinements, and memory entries.
//!
//! Uses Collet's existing `OpenAiCompatibleProvider` for LLM calls.

use std::collections::HashMap;

use anyhow::Result;
use async_trait::async_trait;

use crate::api::models::{ChatRequest, Message};
use crate::api::provider::OpenAiCompatibleProvider;
use crate::evolution::config::EvolveConfig;
use crate::evolution::engine::EvolutionEngine;
use crate::evolution::history::EvolutionHistory;
use crate::evolution::trial::TrialRunner;
use crate::evolution::types::{Observation, StepResult};
use crate::evolution::workspace::AgentWorkspace;

/// Skillforge evolution engine — LLM-driven workspace mutation.
pub struct SkillforgeEngine {
    config: EvolveConfig,
    client: OpenAiCompatibleProvider,
    accept_history: Vec<bool>,
}

impl SkillforgeEngine {
    pub fn new(config: EvolveConfig, client: OpenAiCompatibleProvider) -> Self {
        Self {
            config,
            client,
            accept_history: Vec::new(),
        }
    }

    /// Build an analysis prompt from observations for the evolver LLM.
    fn build_analysis_prompt(
        &self,
        workspace: &AgentWorkspace,
        observations: &[Observation],
        history: &EvolutionHistory,
    ) -> String {
        let mut prompt = String::new();

        prompt.push_str("You are an evolution engine analyzing an AI agent's performance.\n");
        prompt.push_str(
            "Your task: identify failure patterns and propose targeted workspace mutations.\n\n",
        );

        // Current workspace state
        let sys_prompt = workspace.read_prompt().unwrap_or_default();
        if !sys_prompt.is_empty() {
            prompt.push_str("## System Prompt (current)\n```\n");
            prompt.push_str(truncate(&sys_prompt, 2000));
            prompt.push_str("\n```\n\n");
        }

        let skills = workspace.list_skills();
        if !skills.is_empty() {
            prompt.push_str("## Skills\n");
            for s in &skills {
                prompt.push_str(&format!("- **{}**: {}\n", s.name, s.description));
            }
            // Include content of first 3 skills for mutation context.
            for s in skills.iter().take(3) {
                if let Ok(content) = workspace.read_skill(&s.name)
                    && !content.is_empty()
                {
                    prompt.push_str(&format!(
                        "\n### Skill: {}\n```\n{}\n```\n",
                        s.name,
                        truncate(&content, 600)
                    ));
                }
            }
            prompt.push('\n');
        }

        // Prompt fragments in use.
        if let Ok(fragments) = workspace.list_fragments()
            && !fragments.is_empty()
        {
            prompt.push_str(&format!(
                "## Prompt Fragments: {} defined\n",
                fragments.len()
            ));
            // Read first fragment for context (e.g. "rules").
            if let Some(frag_name) = fragments.first()
                && let Ok(frag_content) = workspace.read_fragment(frag_name)
                && !frag_content.is_empty()
            {
                prompt.push_str(&format!(
                    "### Fragment `{frag_name}`\n```\n{}\n```\n",
                    truncate(&frag_content, 400)
                ));
            }
            prompt.push('\n');
        }

        // Recent episodic memories.
        if let Ok(episodic) = workspace.read_memories("episodic", 5)
            && !episodic.is_empty()
        {
            prompt.push_str(&format!(
                "## Episodic Memories: {} recent entries\n\n",
                episodic.len()
            ));
        }

        // Recent memories across all categories.
        if let Ok(memories) = workspace.read_all_memories(20)
            && !memories.is_empty()
        {
            prompt.push_str(&format!(
                "## Total Memories: {} entries across categories\n\n",
                memories.len()
            ));
        }

        // Evolution history entries.
        if let Ok(evo_history) = workspace.read_evolution_history()
            && !evo_history.is_empty()
        {
            prompt.push_str(&format!(
                "## Evolution History: {} cycles logged\n\n",
                evo_history.len()
            ));
        }

        // Evolution metrics.
        if let Ok(metrics) = workspace.read_evolution_metrics()
            && metrics != serde_json::json!({})
        {
            prompt.push_str("## Evolution Metrics: available\n\n");
        }

        let scores = history.get_score_curve();
        if !scores.is_empty() {
            let curve: Vec<String> = scores
                .iter()
                .enumerate()
                .map(|(i, s)| format!("Cycle {}: {:.3}", i + 1, s))
                .collect();
            prompt.push_str(&format!("## Score History\n{}\n\n", curve.join(", ")));

            // Show the system prompt at the best-scoring cycle as a reference.
            if let Some((best_idx, _)) = scores
                .iter()
                .enumerate()
                .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            {
                let best_label = format!("evo-{}", best_idx + 1);
                if let Ok(best_prompt) = history.read_file_at(&best_label, "prompt.md")
                    && !best_prompt.is_empty()
                {
                    prompt.push_str(&format!(
                        "## Best Prompt (cycle {})\n```\n{}\n```\n\n",
                        best_idx + 1,
                        truncate(&best_prompt, 1000)
                    ));
                }
            }
        }

        // Version tags and recent git log.
        if let Ok(versions) = history.list_versions()
            && !versions.is_empty()
        {
            prompt.push_str(&format!(
                "## Version Tags: {} total (e.g. {})\n\n",
                versions.len(),
                versions[0]
            ));
        }
        if let Ok(log) = history.get_version_log(3)
            && !log.is_empty()
        {
            prompt.push_str(&format!(
                "## Recent Version Log\n```\n{}\n```\n\n",
                truncate(&log, 400)
            ));
        }

        // Historical failure summary from observer logs.
        if let Ok(hist_failures) = history.get_observations(3, true)
            && !hist_failures.is_empty()
        {
            prompt.push_str(&format!(
                "## Recent Historical Failures (last 3 cycles): {}\n\n",
                hist_failures.len()
            ));
        }

        // Aggregate stats for context.
        if let Ok(stats) = history.get_summary_stats()
            && let Some(total) = stats.get("total_observations").and_then(|v| v.as_u64())
        {
            prompt.push_str(&format!(
                "## Aggregate Stats: {total} total observations\n\n"
            ));
        }

        // Observation batch summary
        let total = observations.len();
        let successes = observations.iter().filter(|o| o.feedback.success).count();
        let avg = if total > 0 {
            observations.iter().map(|o| o.feedback.score).sum::<f64>() / total as f64
        } else {
            0.0
        };

        prompt.push_str(&format!("## Current Batch\nTotal: {total}, Success: {successes}/{total}, Avg Score: {avg:.3}\n\n"));

        // Failed task details (most useful for mutation decisions)
        let failures: Vec<_> = observations
            .iter()
            .filter(|o| !o.feedback.success)
            .collect();
        if !failures.is_empty() {
            prompt.push_str("## Failures (most recent, up to 5)\n\n");
            for (i, obs) in failures.iter().enumerate().take(5) {
                prompt.push_str(&format!(
                    "### Failure {}\n- Task: {}\n- Output: {}\n- Feedback: {}\n\n",
                    i + 1,
                    truncate(&obs.task.input, 400),
                    truncate(&obs.trajectory.output, 400),
                    truncate(&obs.feedback.detail, 400),
                ));
            }
        }

        // Mutation constraints
        let mut allowed = Vec::new();
        if self.config.evolve_prompts {
            allowed.push("system prompt");
        }
        if self.config.evolve_skills {
            allowed.push("skills (create/modify/delete)");
        }
        if self.config.evolve_memory {
            allowed.push("memory entries");
        }
        if self.config.evolve_tools {
            allowed.push("tool definitions");
        }
        prompt.push_str(&format!("## Allowed Mutations\n{}\n\n", allowed.join(", ")));

        prompt.push_str(r#"## Output Format

Respond with a JSON object (and nothing else) following this schema:

```json
{
  "analysis": "Brief explanation of identified failure patterns",
  "mutations": {
    "prompt": "New system prompt content (omit key if no change)",
    "skills": {
      "skill-name": "Full SKILL.md content with YAML frontmatter (use \"__DELETE__\" to remove)"
    },
    "memories": [
      { "category": "episodic", "content": "...", "source": "evolution" }
    ]
  }
}
```

Focus on targeted, evidence-based mutations. If no mutation is warranted, return `{"analysis": "...", "mutations": {}}`.
"#);

        prompt
    }

    /// Call the evolver LLM and return the raw text response.
    async fn call_llm(&self, prompt: &str) -> Result<String> {
        let request = ChatRequest {
            model: self.config.evolver_model.clone(),
            messages: vec![
                Message {
                    role: "system".to_string(),
                    content: Some(crate::api::content::Content::Text(
                        "You are a precise evolution engine. Output only valid JSON.".to_string(),
                    )),
                    reasoning_content: None,
                    tool_calls: None,
                    tool_call_id: None,
                },
                Message {
                    role: "user".to_string(),
                    content: Some(crate::api::content::Content::Text(prompt.to_string())),
                    reasoning_content: None,
                    tool_calls: None,
                    tool_call_id: None,
                },
            ],
            tools: None,
            tool_choice: None,
            max_tokens: self.config.evolver_max_tokens,
            stream: false,
            temperature: Some(0.3),
            thinking_budget_tokens: None,
            reasoning_effort: None,
        };

        let response = self.client.chat(&request).await?;
        let text = response
            .choices
            .first()
            .and_then(|c| c.message.content.as_ref())
            .map(|c| match c {
                crate::api::content::Content::Text(t) => t.clone(),
                crate::api::content::Content::Parts(parts) => parts
                    .iter()
                    .filter_map(|p| match p {
                        crate::api::content::ContentPart::Text { text } => Some(text.clone()),
                        _ => None,
                    })
                    .collect::<Vec<_>>()
                    .join(""),
            })
            .unwrap_or_default();

        Ok(text)
    }

    /// Parse a JSON block from the LLM response (handles code-fenced JSON).
    fn parse_mutations(text: &str) -> serde_json::Value {
        // Strip ```json ... ``` fences if present
        let cleaned = if let Some(start) = text.find("```json") {
            let inner = &text[start + 7..];
            if let Some(end) = inner.find("```") {
                inner[..end].trim().to_string()
            } else {
                inner.trim().to_string()
            }
        } else if let Some(start) = text.find('{') {
            if let Some(end) = text.rfind('}') {
                text[start..=end].to_string()
            } else {
                String::new()
            }
        } else {
            String::new()
        };

        serde_json::from_str(&cleaned).unwrap_or(serde_json::json!({}))
    }

    /// Apply parsed mutations to the workspace.
    fn apply_mutations(
        &self,
        workspace: &AgentWorkspace,
        mutations: &serde_json::Value,
    ) -> Result<(bool, String)> {
        let mut mutated = false;
        let mut summaries: Vec<String> = Vec::new();

        let mutation_obj = mutations.get("mutations").unwrap_or(mutations);

        // Clear drafts at start of each mutation cycle so stale staged content
        // from a previous cycle does not accumulate.
        let _ = workspace.clear_drafts();

        // Apply prompt mutation
        if self.config.evolve_prompts
            && let Some(new_prompt) = mutation_obj.get("prompt").and_then(|v| v.as_str())
            && !new_prompt.is_empty()
        {
            workspace.write_prompt(new_prompt)?;
            mutated = true;
            summaries.push("updated system prompt".to_string());
        }

        // Apply prompt fragment mutations.
        if self.config.evolve_prompts
            && let Some(frags) = mutation_obj.get("fragments").and_then(|v| v.as_object())
        {
            for (name, content) in frags {
                if let Some(content_str) = content.as_str()
                    && !content_str.is_empty()
                {
                    workspace.write_fragment(name, content_str)?;
                    mutated = true;
                    summaries.push(format!("wrote fragment '{name}'"));
                }
            }
        }

        // Apply skill mutations
        if self.config.evolve_skills
            && let Some(skills) = mutation_obj.get("skills").and_then(|v| v.as_object())
        {
            for (name, content) in skills {
                if let Some(content_str) = content.as_str() {
                    if content_str == "__DELETE__" {
                        workspace.delete_skill(name)?;
                        summaries.push(format!("deleted skill '{name}'"));
                    } else {
                        // Stage the draft before committing to workspace.
                        let _ = workspace.write_draft(name, content_str);
                        workspace.write_skill(name, content_str)?;
                        summaries.push(format!("wrote skill '{name}'"));
                    }
                    mutated = true;
                }
            }
            // Log how many skills were staged as drafts.
            let draft_count = workspace.list_drafts().len();
            if draft_count > 0 {
                tracing::debug!(draft_count, "Staged skill drafts before applying mutations");
            }
        }

        // Apply memory mutations
        if self.config.evolve_memory
            && let Some(mems) = mutation_obj.get("memories").and_then(|v| v.as_array())
        {
            for entry in mems {
                let category = entry
                    .get("category")
                    .and_then(|v| v.as_str())
                    .unwrap_or("episodic");
                workspace.add_memory(entry, category)?;
                mutated = true;
            }
            if !mems.is_empty() {
                summaries.push(format!("added {} memory entries", mems.len()));
            }
        }

        let summary = if summaries.is_empty() {
            mutations
                .get("analysis")
                .and_then(|v| v.as_str())
                .unwrap_or("no mutations applied")
                .to_string()
        } else {
            format!(
                "{}{}",
                summaries.join("; "),
                mutations
                    .get("analysis")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
            )
        };

        Ok((mutated, summary))
    }
}

#[async_trait]
impl EvolutionEngine for SkillforgeEngine {
    async fn step(
        &mut self,
        workspace: &AgentWorkspace,
        observations: &[Observation],
        history: &EvolutionHistory,
        _trial: &TrialRunner,
    ) -> Result<StepResult> {
        tracing::info!(
            engine = self.name(),
            observations = observations.len(),
            "Running skillforge step"
        );

        let prompt = self.build_analysis_prompt(workspace, observations, history);
        let raw = self.call_llm(&prompt).await?;

        tracing::debug!(response_len = raw.len(), "LLM response received");

        let parsed = Self::parse_mutations(&raw);
        let (mutated, summary) = self.apply_mutations(workspace, &parsed)?;

        tracing::info!(mutated, summary = %summary, "Skillforge step complete");

        Ok(StepResult {
            mutated,
            summary,
            metadata: HashMap::new(),
        })
    }

    fn on_cycle_end(&mut self, accepted: bool, _score: f64) {
        self.accept_history.push(accepted);
    }

    fn name(&self) -> &str {
        "skillforge"
    }
}

fn truncate(s: &str, max_len: usize) -> &str {
    if s.len() <= max_len {
        s
    } else {
        let mut end = max_len;
        while end > 0 && !s.is_char_boundary(end) {
            end -= 1;
        }
        &s[..end]
    }
}