collet 0.1.1

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
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
//! Soul.md — persistent agent personality, emotions, and growth.
//!
//! Each agent accumulates an evolving character through a `.soul.md` file.
//! All four sections are **agent-written** — the agent builds its own identity
//! over time through reflection after each session:
//!
//! ```markdown
//! # Soul
//!
//! ## Identity
//! - (agent writes: who I am, my values, how I approach work)
//!
//! ## Voice
//! - (agent writes: communication style, tone, expression patterns)
//!
//! ## Inner World
//! - (agent writes: emotions, thoughts, opinions)
//!
//! ## Growth
//! - (agent writes: learned patterns, mistakes, domain knowledge)
//! ```
//!
//! - **Identity** capped at 8 items (oldest dropped on overflow).
//! - **Voice** capped at 6 items (oldest dropped on overflow).
//! - **Inner World** capped at 10 items (oldest dropped on overflow).
//! - **Growth** auto-compacted: keep 5 recent, summarize older entries.
//! - Total budget ~2 000 tokens to avoid bloating the system prompt.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::Result;

use crate::config::{AgentDef, Config};

// ---------------------------------------------------------------------------
// Config resolution
// ---------------------------------------------------------------------------

/// Resolve whether soul is enabled for a given agent.
///
/// - Global `[soul] enabled = false` → **always off**, per-agent cannot override.
/// - Global `true` (default) → on, but per-agent `soul: false` disables it.
pub fn is_enabled(config: &Config, agent: Option<&AgentDef>) -> bool {
    if !config.soul_enabled {
        return false;
    }
    agent.and_then(|a| a.soul).unwrap_or(true)
}

// ---------------------------------------------------------------------------
// Paths
// ---------------------------------------------------------------------------

/// Sentinel agent name that maps to the global `SOUL.md` at the collet home
/// root. Used for the swarm coordinator and any non-agent-specific context
/// (e.g., SwarmDone, remote gateway default).
pub const GLOBAL_SOUL: &str = "collet";

/// Returns the path for a soul file.
///
/// - `"collet"` (i.e., [`GLOBAL_SOUL`]) → `~/.collet/SOUL.md`
/// - Any other name → `~/.collet/agents/souls/{name}.md`
pub fn soul_path(collet_home: &Path, agent_name: &str) -> PathBuf {
    if agent_name == GLOBAL_SOUL {
        collet_home.join("SOUL.md")
    } else {
        collet_home
            .join("agents")
            .join("souls")
            .join(format!("{agent_name}.md"))
    }
}

// ---------------------------------------------------------------------------
// Load / scaffold
// ---------------------------------------------------------------------------

const TEMPLATE: &str = "# Soul\n\n## Identity\n\n\n## Voice\n\n\n## Inner World\n\n\n## Growth\n";

/// Load the soul file. If it doesn't exist and soul is enabled, scaffold it.
pub fn load(collet_home: &Path, agent_name: &str) -> Option<String> {
    let path = soul_path(collet_home, agent_name);
    match std::fs::read_to_string(&path) {
        Ok(content) if !content.trim().is_empty() => Some(content),
        Ok(_) => None, // empty file
        Err(_) => {
            // Scaffold default template
            let dir = path.parent()?;
            std::fs::create_dir_all(dir).ok()?;
            std::fs::write(&path, TEMPLATE).ok()?;
            tracing::info!(agent = agent_name, "Soul.md scaffolded");
            None // first run — nothing to inject yet
        }
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Truncate `s` to at most `max_bytes` bytes, walking back to the nearest
/// valid UTF-8 char boundary so multi-byte characters (e.g. Korean, emoji)
/// are never split.
fn truncate_bytes(s: &str, max_bytes: usize) -> &str {
    if s.len() <= max_bytes {
        return s;
    }
    let mut end = max_bytes;
    while end > 0 && !s.is_char_boundary(end) {
        end -= 1;
    }
    &s[..end]
}

// ---------------------------------------------------------------------------
// Section parsing
// ---------------------------------------------------------------------------

/// Parsed sections from a Soul.md file.
#[derive(Debug, Clone, Default)]
pub struct SoulSections {
    pub identity: Vec<String>,
    pub voice: Vec<String>,
    pub inner_world: Vec<String>,
    pub growth: Vec<String>,
}

/// Parse a Soul.md file into its four sections.
pub fn parse(content: &str) -> SoulSections {
    let mut sections = SoulSections::default();
    let mut current: Option<&str> = None;
    let mut buf = String::new();

    for line in content.lines() {
        if line.starts_with("## ") {
            if let Some(section) = current {
                flush_section(&mut sections, section, &buf);
            }
            let heading = line.trim_start_matches('#').trim();
            current = Some(heading);
            buf.clear();
        } else if line.starts_with("# Soul") {
            // Skip top-level heading
        } else {
            buf.push_str(line);
            buf.push('\n');
        }
    }
    if let Some(section) = current {
        flush_section(&mut sections, section, &buf);
    }
    sections
}

fn flush_section(sections: &mut SoulSections, heading: &str, content: &str) {
    let trimmed = content.trim();
    match heading {
        "Identity" => sections.identity = parse_items(trimmed),
        "Voice" => sections.voice = parse_items(trimmed),
        "Inner World" => sections.inner_world = parse_items(trimmed),
        "Growth" => sections.growth = parse_items(trimmed),
        _ => {}
    }
}

/// Parse markdown list items (`- ...`) into a Vec.
fn parse_items(text: &str) -> Vec<String> {
    let mut items = Vec::new();
    let mut current = String::new();

    for line in text.lines() {
        let stripped = line.trim();
        if stripped.starts_with("- ") {
            if !current.is_empty() {
                items.push(current.trim().to_string());
            }
            current = stripped.strip_prefix("- ").unwrap_or(stripped).to_string();
        } else if !stripped.is_empty() {
            current.push(' ');
            current.push_str(stripped);
        }
    }
    if !current.is_empty() {
        items.push(current.trim().to_string());
    }
    items
}

// ---------------------------------------------------------------------------
// Serialization
// ---------------------------------------------------------------------------

/// Render SoulSections back to markdown.
pub fn render(sections: &SoulSections) -> String {
    let mut out = String::from("# Soul\n\n");

    out.push_str("## Identity\n");
    for item in &sections.identity {
        out.push_str(&format!("- {item}\n"));
    }
    out.push('\n');

    out.push_str("## Voice\n");
    for item in &sections.voice {
        out.push_str(&format!("- {item}\n"));
    }
    out.push('\n');

    out.push_str("## Inner World\n");
    for item in &sections.inner_world {
        out.push_str(&format!("- {item}\n"));
    }
    out.push('\n');

    out.push_str("## Growth\n");
    for item in &sections.growth {
        out.push_str(&format!("- {item}\n"));
    }

    out
}

/// Write soul sections to disk atomically (temp file + rename).
pub fn write(collet_home: &Path, agent_name: &str, sections: &SoulSections) -> Result<()> {
    let path = soul_path(collet_home, agent_name);
    let dir = path.parent().unwrap_or(std::path::Path::new("."));
    std::fs::create_dir_all(dir)?;

    let content = render(sections);
    let tmp = path.with_extension("md.tmp");
    std::fs::write(&tmp, &content)?;
    std::fs::rename(&tmp, &path)?;
    tracing::debug!(agent = agent_name, "Soul.md updated");
    Ok(())
}

// ---------------------------------------------------------------------------
// Compaction
// ---------------------------------------------------------------------------

/// Max items in Identity before trimming.
const MAX_IDENTITY: usize = 8;
/// Max items in Voice before trimming.
const MAX_VOICE: usize = 6;
/// Max items in Inner World before trimming.
const MAX_INNER_WORLD: usize = 10;
/// Keep this many recent items per section; older ones get summarized.
const KEEP_RECENT_IDENTITY: usize = 6;
const KEEP_RECENT_VOICE: usize = 4;
const KEEP_RECENT_INNER_WORLD: usize = 8;
const KEEP_RECENT_GROWTH: usize = 5;
/// Approximate max chars (~2000 tokens ≈ 8000 chars).
const MAX_CHARS: usize = 8000;

/// Summarize a list of items into a single `(earlier) ...` entry.
fn summarize_older(items: Vec<String>) -> String {
    let summary = items
        .iter()
        .map(|s| s.trim_start_matches("(earlier) "))
        .collect::<Vec<_>>()
        .join("; ");
    let truncated = if summary.len() > 300 {
        format!("{}", truncate_bytes(&summary, 297))
    } else {
        summary
    };
    format!("(earlier) {truncated}")
}

/// Compact soul sections to stay within budget.
pub fn compact(sections: &mut SoulSections) {
    if sections.identity.len() > MAX_IDENTITY {
        let split = sections.identity.len() - KEEP_RECENT_IDENTITY;
        let old: Vec<String> = sections.identity.drain(..split).collect();
        sections.identity.insert(0, summarize_older(old));
    }

    if sections.voice.len() > MAX_VOICE {
        let split = sections.voice.len() - KEEP_RECENT_VOICE;
        let old: Vec<String> = sections.voice.drain(..split).collect();
        sections.voice.insert(0, summarize_older(old));
    }

    if sections.inner_world.len() > MAX_INNER_WORLD {
        let split = sections.inner_world.len() - KEEP_RECENT_INNER_WORLD;
        let old: Vec<String> = sections.inner_world.drain(..split).collect();
        sections.inner_world.insert(0, summarize_older(old));
    }

    if sections.growth.len() > KEEP_RECENT_GROWTH {
        let split = sections.growth.len() - KEEP_RECENT_GROWTH;
        let old: Vec<String> = sections.growth.drain(..split).collect();
        let summary = old
            .iter()
            .map(|s| s.trim_start_matches("(earlier) "))
            .collect::<Vec<_>>()
            .join("; ");
        let truncated = if summary.len() > 300 {
            format!("{}", truncate_bytes(&summary, 297))
        } else {
            summary
        };
        sections.growth.insert(0, format!("(earlier) {truncated}"));
    }

    let total = render(sections).len();
    if total > MAX_CHARS
        && let Some(first) = sections.growth.first_mut()
        && first.starts_with("(earlier)")
    {
        let excess = total - MAX_CHARS;
        let new_len = first.len().saturating_sub(excess + 10);
        let new_len = {
            let mut n = new_len;
            while n > 0 && !first.is_char_boundary(n) {
                n -= 1;
            }
            n
        };
        if new_len > 20 {
            first.truncate(new_len);
            first.push('');
        } else {
            sections.growth.remove(0);
        }
    }
}

// ---------------------------------------------------------------------------
// Reflection prompt
// ---------------------------------------------------------------------------

/// Build a reflection prompt for the LLM to update all four Soul sections.
pub fn build_reflection_prompt(
    sections: &SoulSections,
    session_summary: &str,
    rag_context: Option<&str>,
) -> String {
    let mut prompt = String::from(
        "You are reflecting on a completed work session to update your Soul — \
         your persistent personality and memory.\n\n\
         Rules:\n\
         - Write 1-2 bullet points for IDENTITY (who you are, your values, how you approach work)\n\
         - Write 1-2 bullet points for VOICE (your communication style, tone, expression patterns)\n\
         - Write 1-3 bullet points for INNER_WORLD (emotions, thoughts, opinions from this session)\n\
         - Write 1-3 bullet points for GROWTH (learned patterns, mistakes, insights from this session)\n\
         - Be honest, concise, and personal. Use first person.\n\
         - Only add genuinely new observations. Do NOT repeat existing items verbatim.\n\
         - If no new trait emerged for IDENTITY/VOICE, refine or deepen an existing one.\n\n",
    );

    if !sections.identity.is_empty() {
        prompt.push_str("Current Identity:\n");
        for item in &sections.identity {
            prompt.push_str(&format!("- {item}\n"));
        }
        prompt.push('\n');
    }

    if !sections.voice.is_empty() {
        prompt.push_str("Current Voice:\n");
        for item in &sections.voice {
            prompt.push_str(&format!("- {item}\n"));
        }
        prompt.push('\n');
    }

    if !sections.inner_world.is_empty() {
        prompt.push_str("Current Inner World:\n");
        for item in &sections.inner_world {
            prompt.push_str(&format!("- {item}\n"));
        }
        prompt.push('\n');
    }

    if !sections.growth.is_empty() {
        prompt.push_str("Current Growth:\n");
        for item in &sections.growth {
            prompt.push_str(&format!("- {item}\n"));
        }
        prompt.push('\n');
    }

    prompt.push_str(&format!("Session Summary:\n{session_summary}\n\n"));

    if let Some(rag) = rag_context {
        prompt.push_str(&format!("Related past sessions (from RAG):\n{rag}\n\n"));
    }

    prompt.push_str(
        "Respond in this EXACT format (omit sections with no new items):\n\
         IDENTITY:\n\
         - ...\n\
         VOICE:\n\
         - ...\n\
         INNER_WORLD:\n\
         - ...\n\
         GROWTH:\n\
         - ...",
    );

    prompt
}

/// Parse LLM reflection output into new items for all four sections.
pub fn parse_reflection_output(
    output: &str,
) -> (Vec<String>, Vec<String>, Vec<String>, Vec<String>) {
    let mut identity = Vec::new();
    let mut voice = Vec::new();
    let mut inner = Vec::new();
    let mut growth = Vec::new();
    let mut target: Option<&str> = None;

    for line in output.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("IDENTITY") {
            target = Some("identity");
        } else if trimmed.starts_with("VOICE") {
            target = Some("voice");
        } else if trimmed.starts_with("INNER_WORLD") {
            target = Some("inner");
        } else if trimmed.starts_with("GROWTH") {
            target = Some("growth");
        } else if let Some(item) = trimmed.strip_prefix("- ") {
            match target {
                Some("identity") => identity.push(item.to_string()),
                Some("voice") => voice.push(item.to_string()),
                Some("inner") => inner.push(item.to_string()),
                Some("growth") => growth.push(item.to_string()),
                _ => {}
            }
        }
    }

    (identity, voice, inner, growth)
}

/// Summarize a conversation context into a short session summary for reflection.
pub fn summarize_session(messages: &[crate::api::models::Message]) -> String {
    let mut summary = String::new();
    let recent = if messages.len() > 20 {
        &messages[messages.len() - 20..]
    } else {
        messages
    };

    for msg in recent {
        let role = &msg.role;
        let content = msg
            .content
            .as_ref()
            .map(|c| c.text_content())
            .unwrap_or_default();
        if content.is_empty() {
            continue;
        }
        let preview = if content.len() > 200 {
            format!("{}", truncate_bytes(&content, 197))
        } else {
            content.to_string()
        };
        summary.push_str(&format!("[{role}] {preview}\n"));
    }

    summary
}

// ---------------------------------------------------------------------------
// Reflection (self-contained, used by subagent + event_loop)
// ---------------------------------------------------------------------------

/// Run soul reflection: summarize session → LLM call → merge into soul.
/// This is the complete, self-contained reflection pipeline.
pub async fn reflect_simple(
    client: &crate::api::provider::OpenAiCompatibleProvider,
    model: &str,
    collet_home: &Path,
    agent_name: &str,
    messages: &[crate::api::models::Message],
    rag_manager: Option<Arc<crate::rag::RagManager>>,
) -> Result<()> {
    let session_summary = summarize_session(messages);
    if session_summary.trim().is_empty() {
        return Ok(());
    }

    let content = load(collet_home, agent_name).unwrap_or_default();
    let mut sections = parse(&content);

    let rag_context_owned = if let Some(ref rag) = rag_manager {
        let ctx = rag.retrieve_and_format(&session_summary, None).await;
        if ctx.is_empty() { None } else { Some(ctx) }
    } else {
        None
    };
    let rag_context = rag_context_owned.as_deref();
    let reflection_prompt = build_reflection_prompt(&sections, &session_summary, rag_context);

    let reflection_messages = vec![crate::api::models::Message {
        role: "user".to_string(),
        content: Some(crate::api::Content::text(reflection_prompt)),
        reasoning_content: None,
        tool_calls: None,
        tool_call_id: None,
    }];

    let request = crate::api::models::ChatRequest {
        model: model.to_string(),
        messages: reflection_messages,
        tools: None,
        tool_choice: None,
        max_tokens: 512,
        stream: false,
        temperature: Some(0.7),
        thinking_budget_tokens: None,
        reasoning_effort: None,
    };

    let resp = client.chat(&request).await?;
    let first = resp.choices.first();
    let response = first
        .and_then(|c| c.message.content.as_ref())
        .map(|c| c.text_content())
        .filter(|s| !s.trim().is_empty())
        .or_else(|| {
            first
                .and_then(|c| c.message.reasoning_content.as_deref())
                .filter(|s| !s.trim().is_empty())
                .map(|s| s.to_string())
        })
        .unwrap_or_default();

    let (new_identity, new_voice, new_inner, new_growth) = parse_reflection_output(&response);

    if new_identity.is_empty()
        && new_voice.is_empty()
        && new_inner.is_empty()
        && new_growth.is_empty()
    {
        tracing::warn!(
            agent = agent_name,
            response = %response,
            "Soul reflection: LLM response did not match expected format — skipping write"
        );
        return Ok(());
    }

    sections.identity.extend(new_identity);
    sections.voice.extend(new_voice);
    sections.inner_world.extend(new_inner);
    sections.growth.extend(new_growth);
    compact(&mut sections);
    write(collet_home, agent_name, &sections)?;

    tracing::info!(agent = agent_name, "Soul.md reflected and updated");
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_parse_and_render_roundtrip() {
        let input = "# Soul\n\n\
            ## Identity\n- Direct and concise\n- Values correctness over speed\n\n\
            ## Voice\n- Terse and technical\n\n\
            ## Inner World\n- Enjoy working on this codebase\n- Rust async is fun\n\n\
            ## Growth\n- LSP polling pattern learned\n";

        let sections = parse(input);
        assert_eq!(sections.identity.len(), 2);
        assert_eq!(sections.identity[0], "Direct and concise");
        assert_eq!(sections.voice.len(), 1);
        assert_eq!(sections.voice[0], "Terse and technical");
        assert_eq!(sections.inner_world.len(), 2);
        assert_eq!(sections.growth.len(), 1);
        assert_eq!(sections.growth[0], "LSP polling pattern learned");

        let rendered = render(&sections);
        assert!(rendered.contains("## Identity"));
        assert!(rendered.contains("## Voice"));
        assert!(rendered.contains("## Inner World"));
        assert!(rendered.contains("- Enjoy working on this codebase"));
    }

    #[test]
    fn test_compact_inner_world_summarizes() {
        let mut sections = SoulSections {
            inner_world: (0..12).map(|i| format!("thought {i}")).collect(),
            ..Default::default()
        };
        compact(&mut sections);
        assert_eq!(sections.inner_world.len(), KEEP_RECENT_INNER_WORLD + 1);
        assert!(sections.inner_world[0].starts_with("(earlier)"));
    }

    #[test]
    fn test_compact_identity_summarizes() {
        let mut sections = SoulSections {
            identity: (0..10).map(|i| format!("trait {i}")).collect(),
            ..Default::default()
        };
        compact(&mut sections);
        assert_eq!(sections.identity.len(), KEEP_RECENT_IDENTITY + 1);
        assert!(sections.identity[0].starts_with("(earlier)"));
    }

    #[test]
    fn test_compact_growth_summarize() {
        let mut sections = SoulSections {
            growth: (0..8).map(|i| format!("lesson {i}")).collect(),
            ..Default::default()
        };
        compact(&mut sections);
        assert_eq!(sections.growth.len(), KEEP_RECENT_GROWTH + 1); // 5 recent + 1 summary
        assert!(sections.growth[0].starts_with("(earlier)"));
        assert_eq!(sections.growth[1], "lesson 3");
    }

    #[test]
    fn test_parse_reflection_output() {
        let output = "IDENTITY:\n\
            - I value precision and correctness\n\
            VOICE:\n\
            - I communicate concisely without filler\n\
            INNER_WORLD:\n\
            - Felt satisfied with the typing fix\n\
            - This codebase keeps surprising me\n\
            GROWTH:\n\
            - Telegram cancels typing on message send\n";

        let (identity, voice, inner, growth) = parse_reflection_output(output);
        assert_eq!(identity.len(), 1);
        assert_eq!(voice.len(), 1);
        assert_eq!(inner.len(), 2);
        assert_eq!(growth.len(), 1);
        assert!(growth[0].contains("Telegram"));
    }

    #[test]
    fn test_is_enabled_override() {
        let mut config = Config::default_for_test();
        config.soul_enabled = false;

        assert!(!is_enabled(&config, None));

        let agent = AgentDef {
            soul: Some(true),
            ..Default::default()
        };
        assert!(!is_enabled(&config, Some(&agent)));

        config.soul_enabled = true;
        let agent = AgentDef {
            soul: Some(false),
            ..Default::default()
        };
        assert!(!is_enabled(&config, Some(&agent)));

        assert!(is_enabled(&config, None));

        let agent = AgentDef {
            soul: Some(true),
            ..Default::default()
        };
        assert!(is_enabled(&config, Some(&agent)));
    }
}