terraphim_orchestrator 1.16.34

AI Dark Factory orchestrator wiring spawner, router, supervisor into a reconciliation loop
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
use handlebars::Handlebars;
use std::collections::HashMap;
use std::path::Path;
use terraphim_types::persona::{PersonaDefinition, PersonaLoadError};
use tracing::{info, warn};

#[cfg(test)]
use terraphim_types::persona::{CharacteristicDef, SfiaSkillDef};

/// Registry for loading and accessing persona definitions.
/// Stores personas with case-insensitive lookup.
#[derive(Debug, Clone)]
pub struct PersonaRegistry {
    personas: HashMap<String, PersonaDefinition>,
}

impl PersonaRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self {
            personas: HashMap::new(),
        }
    }

    /// Load all persona TOML files from a directory.
    ///
    /// Reads all `*.toml` files from the given directory. For each file,
    /// attempts to parse it as a PersonaDefinition. If parsing fails,
    /// a warning is logged and the file is skipped.
    ///
    /// Returns an error if the directory does not exist or cannot be read.
    pub fn load_from_dir(dir: &Path) -> Result<Self, PersonaLoadError> {
        if !dir.exists() {
            return Err(PersonaLoadError::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("Persona directory not found: {}", dir.display()),
            )));
        }

        if !dir.is_dir() {
            return Err(PersonaLoadError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                format!("Not a directory: {}", dir.display()),
            )));
        }

        let mut registry = Self::new();

        for entry in std::fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.extension().map(|e| e == "toml").unwrap_or(false) {
                match PersonaDefinition::from_file(&path) {
                    Ok(persona) => {
                        info!(name = %persona.agent_name, path = %path.display(), "loaded persona");
                        registry.insert(persona);
                    }
                    Err(e) => {
                        warn!(path = %path.display(), error = %e, "failed to load persona file, skipping");
                    }
                }
            }
        }

        info!(count = registry.len(), dir = %dir.display(), "persona registry loaded");
        Ok(registry)
    }

    /// Get a persona by name (case-insensitive lookup).
    pub fn get(&self, name: &str) -> Option<&PersonaDefinition> {
        self.personas.get(&name.to_lowercase())
    }

    /// Get the number of personas in the registry.
    pub fn len(&self) -> usize {
        self.personas.len()
    }

    /// Check if the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.personas.is_empty()
    }

    /// Insert a persona into the registry.
    /// Uses lowercase key for case-insensitive lookup.
    pub fn insert(&mut self, persona: PersonaDefinition) {
        let key = persona.agent_name.to_lowercase();
        self.personas.insert(key, persona);
    }

    /// Get a list of all persona names in the registry.
    pub fn persona_names(&self) -> Vec<&str> {
        self.personas
            .values()
            .map(|p| p.agent_name.as_str())
            .collect()
    }
}

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

const DEFAULT_TEMPLATE: &str = include_str!("../data/metaprompt-template.hbs");
const TEMPLATE_NAME: &str = "metaprompt";

/// Error type for metaprompt rendering operations.
#[derive(Debug, thiserror::Error)]
pub enum MetapromptRenderError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("Template compilation error: {0}")]
    Template(String),
    #[error("Template render error: {0}")]
    Render(String),
}

/// Renderer for persona metaprompts using Handlebars templates.
///
/// The renderer uses strict mode and expects all template variables
/// to be present in the PersonaDefinition. A default template is
/// embedded at compile time, but a custom template can be loaded
/// from a file.
#[derive(Debug)]
pub struct MetapromptRenderer {
    handlebars: Handlebars<'static>,
}

impl MetapromptRenderer {
    /// Create a new renderer with the default embedded template.
    pub fn new() -> Result<Self, MetapromptRenderError> {
        let mut handlebars = Handlebars::new();
        handlebars.set_strict_mode(true);

        handlebars
            .register_template_string(TEMPLATE_NAME, DEFAULT_TEMPLATE)
            .map_err(|e| MetapromptRenderError::Template(e.to_string()))?;

        Ok(Self { handlebars })
    }

    /// Create a new renderer from a custom template file.
    ///
    /// The file should be a valid Handlebars template that can
    /// render a PersonaDefinition.
    pub fn from_template_file(path: &Path) -> Result<Self, MetapromptRenderError> {
        let template_str = std::fs::read_to_string(path)?;

        let mut handlebars = Handlebars::new();
        handlebars.set_strict_mode(true);

        handlebars
            .register_template_string(TEMPLATE_NAME, &template_str)
            .map_err(|e| MetapromptRenderError::Template(e.to_string()))?;

        Ok(Self { handlebars })
    }

    /// Render a persona into a metaprompt preamble.
    ///
    /// Returns the rendered metaprompt string using the configured
    /// Handlebars template and the persona's data.
    pub fn render(&self, persona: &PersonaDefinition) -> Result<String, MetapromptRenderError> {
        self.handlebars
            .render(TEMPLATE_NAME, persona)
            .map_err(|e| MetapromptRenderError::Render(e.to_string()))
    }

    /// Compose a full prompt with metapreamble and task.
    ///
    /// On render success, returns: "{preamble}\n\n---\n\n## Current Task\n\n{task}"
    /// On render failure, logs a warning and returns the task unchanged.
    pub fn compose_prompt(&self, persona: &PersonaDefinition, task: &str) -> String {
        match self.render(persona) {
            Ok(preamble) => {
                format!("{}\n\n---\n\n## Current Task\n\n{}", preamble, task)
            }
            Err(e) => {
                warn!(
                    agent = %persona.agent_name,
                    error = %e,
                    "metaprompt render failed, returning task without preamble"
                );
                task.to_string()
            }
        }
    }
}

impl Default for MetapromptRenderer {
    fn default() -> Self {
        Self::new().expect("default template should always compile")
    }
}

/// Create a test persona for use in tests.
#[cfg(test)]
pub fn test_persona() -> PersonaDefinition {
    PersonaDefinition {
        agent_name: "TestAgent".to_string(),
        role_name: "Test Engineer".to_string(),
        name_origin: "From testing".to_string(),
        vibe: "Thorough, methodical".to_string(),
        symbol: "Checkmark".to_string(),
        core_characteristics: vec![CharacteristicDef {
            name: "Thorough".to_string(),
            description: "checks everything twice".to_string(),
        }],
        speech_style: "Precise and factual.".to_string(),
        terraphim_nature: "Adapted to testing environments.".to_string(),
        sfia_title: "Test Engineer".to_string(),
        primary_level: 4,
        guiding_phrase: "Enable".to_string(),
        level_essence: "Works autonomously under general direction.".to_string(),
        sfia_skills: vec![SfiaSkillDef {
            code: "TEST".to_string(),
            name: "Testing".to_string(),
            level: 4,
            description: "Designs and executes test plans.".to_string(),
        }],
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::Serialize;
    use std::io::Write;
    use tempfile::TempDir;

    #[test]
    fn test_registry_new_is_empty() {
        let registry = PersonaRegistry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn test_registry_insert_and_get() {
        let mut registry = PersonaRegistry::new();
        let persona = test_persona();

        registry.insert(persona);

        assert!(!registry.is_empty());
        assert_eq!(registry.len(), 1);
        assert!(registry.get("TestAgent").is_some());
        assert_eq!(registry.get("TestAgent").unwrap().agent_name, "TestAgent");
    }

    #[test]
    fn test_registry_get_case_insensitive() {
        let mut registry = PersonaRegistry::new();
        let persona = test_persona();

        registry.insert(persona);

        // All these should resolve to the same persona
        assert!(registry.get("vigil").is_none()); // vigil doesn't exist

        // But for our test persona, case variations should work
        assert!(registry.get("TestAgent").is_some());
        assert!(registry.get("testagent").is_some());
        assert!(registry.get("TESTAGENT").is_some());
        assert!(registry.get("TestAGENT").is_some());
    }

    #[test]
    fn test_registry_load_from_dir() {
        let temp_dir = TempDir::new().unwrap();

        // Create test TOML files
        let persona1 = r#"
agent_name = "Vigil"
role_name = "Test Role 1"
name_origin = "Test"
vibe = "Test"
symbol = "T"
core_characteristics = []
speech_style = "Test"
terraphim_nature = "Test"
sfia_title = "Test"
primary_level = 4
guiding_phrase = "Test"
level_essence = "Test"
sfia_skills = []
"#;

        let persona2 = r#"
agent_name = "Sentinel"
role_name = "Test Role 2"
name_origin = "Test"
vibe = "Test"
symbol = "S"
core_characteristics = []
speech_style = "Test"
terraphim_nature = "Test"
sfia_title = "Test"
primary_level = 3
guiding_phrase = "Test"
level_essence = "Test"
sfia_skills = []
"#;

        let mut file1 = std::fs::File::create(temp_dir.path().join("vigil.toml")).unwrap();
        file1.write_all(persona1.as_bytes()).unwrap();

        let mut file2 = std::fs::File::create(temp_dir.path().join("sentinel.toml")).unwrap();
        file2.write_all(persona2.as_bytes()).unwrap();

        // Create a non-toml file (should be ignored)
        let mut file3 = std::fs::File::create(temp_dir.path().join("readme.txt")).unwrap();
        file3.write_all(b"This is not a persona").unwrap();

        let registry = PersonaRegistry::load_from_dir(temp_dir.path()).unwrap();

        assert_eq!(registry.len(), 2);
        assert!(registry.get("vigil").is_some());
        assert!(registry.get("sentinel").is_some());
        assert!(registry.get("Vigil").is_some()); // case-insensitive
        assert!(registry.get("SENTINEL").is_some()); // case-insensitive
    }

    #[test]
    fn test_registry_load_missing_dir() {
        let result = PersonaRegistry::load_from_dir(Path::new("/nonexistent/path/12345"));
        assert!(result.is_err());

        // Verify it's the right error type
        match result {
            Err(PersonaLoadError::Io(e)) => {
                assert_eq!(e.kind(), std::io::ErrorKind::NotFound);
            }
            _ => panic!("Expected Io error with NotFound kind"),
        }
    }

    #[test]
    fn test_renderer_default_template() {
        let renderer = MetapromptRenderer::new();
        assert!(renderer.is_ok());
    }

    #[test]
    fn test_renderer_render_persona() {
        let renderer = MetapromptRenderer::new().unwrap();
        let persona = test_persona();

        let result = renderer.render(&persona);
        assert!(result.is_ok());

        let rendered = result.unwrap();
        assert!(rendered.contains(&persona.agent_name));
        assert!(rendered.contains(&persona.role_name));
        assert!(rendered.contains(&persona.sfia_skills[0].code));
        assert!(rendered.contains(&persona.sfia_skills[0].name));
    }

    #[test]
    fn test_renderer_compose_prompt() {
        let renderer = MetapromptRenderer::new().unwrap();
        let persona = test_persona();
        let task = "Write some tests for the new feature";

        let prompt = renderer.compose_prompt(&persona, task);

        // Should contain the separator
        assert!(prompt.contains("---"));
        // Should contain the task section header
        assert!(prompt.contains("## Current Task"));
        // Should contain the task verbatim
        assert!(prompt.contains(task));
        // Should contain the preamble (from rendering the persona)
        assert!(prompt.contains(&persona.agent_name));
    }

    #[test]
    fn test_renderer_compose_prompt_contains_task() {
        let renderer = MetapromptRenderer::new().unwrap();
        let persona = test_persona();
        let task = "This is the specific task to accomplish";

        let prompt = renderer.compose_prompt(&persona, task);

        // Verify task appears after the final separator
        // The prompt contains "## Current Task" followed by the task
        assert!(prompt.contains("## Current Task"));
        assert!(prompt.contains(task));

        // Verify task appears at the end of the prompt
        assert!(prompt.ends_with(task));
    }

    #[test]
    fn test_renderer_strict_mode_missing_field() {
        let renderer = MetapromptRenderer::new().unwrap();

        // Create a minimal persona that's missing required fields
        #[derive(Serialize)]
        struct IncompletePersona {
            agent_name: String,
        }

        let incomplete = IncompletePersona {
            agent_name: "Incomplete".to_string(),
        };

        // Try to render with the incomplete persona
        // This should fail because the template expects many fields
        let result: Result<String, MetapromptRenderError> = renderer
            .handlebars
            .render(TEMPLATE_NAME, &incomplete)
            .map_err(|e| MetapromptRenderError::Render(e.to_string()));

        assert!(result.is_err());
    }

    #[test]
    fn test_renderer_from_template_file() {
        let temp_dir = TempDir::new().unwrap();
        let template_path = temp_dir.path().join("custom.hbs");

        let custom_template = "Hello {{agent_name}}, you are a {{role_name}}!";
        std::fs::write(&template_path, custom_template).unwrap();

        let renderer = MetapromptRenderer::from_template_file(&template_path).unwrap();
        let persona = test_persona();

        let result = renderer.render(&persona).unwrap();
        assert!(result.contains(&persona.agent_name));
        assert!(result.contains(&persona.role_name));
    }

    #[test]
    fn test_persona_names_returns_all_names() {
        let mut registry = PersonaRegistry::new();

        let mut persona1 = test_persona();
        persona1.agent_name = "Alpha".to_string();
        registry.insert(persona1);

        let mut persona2 = test_persona();
        persona2.agent_name = "Beta".to_string();
        registry.insert(persona2);

        let names = registry.persona_names();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"Alpha"));
        assert!(names.contains(&"Beta"));
    }

    #[test]
    fn test_compose_prompt_fallback_on_render_failure() {
        let renderer = MetapromptRenderer::new().unwrap();
        let task = "Do the thing";

        let broken = PersonaDefinition {
            agent_name: "Broken".to_string(),
            ..test_persona() // Take valid fields from test_persona
        };

        // This should succeed because test_persona has all required fields
        let prompt = renderer.compose_prompt(&broken, task);
        assert!(prompt.contains(task));

        // Verify it contains the separator (meaning render succeeded)
        assert!(prompt.contains("---"));
    }

    #[test]
    fn test_registry_insert_overwrites_existing() {
        let mut registry = PersonaRegistry::new();

        let mut persona1 = test_persona();
        persona1.agent_name = "SameName".to_string();
        persona1.role_name = "Role1".to_string();
        registry.insert(persona1);

        let mut persona2 = test_persona();
        persona2.agent_name = "SAMENAME".to_string(); // Different case, same key
        persona2.role_name = "Role2".to_string();
        registry.insert(persona2);

        // Should only have one entry (the second one)
        assert_eq!(registry.len(), 1);
        assert_eq!(registry.get("samename").unwrap().role_name, "Role2");
    }
}