oxios-kernel 1.23.0

Oxios kernel: supervisor, event bus, state store
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
//! Persona manager: coordinates persona-aware execution.
//!
//! The PersonaManager manages persona lifecycle and provides
//! the active persona for orchestrator and agent runtime.
//!
//! RFC-039 completion: the manager owns an optional `StateStore` and a
//! reseed callback so that **every** `set_active` call — from the HTTP API,
//! the `PersonaTool`, the Gateway, or the delete handler — automatically
//! persists to disk and re-seeds the intent engine.  There is one code path;
//! callers cannot accidentally skip persistence or re-seeding.

use std::sync::Arc;

use anyhow::Result;
use parking_lot::RwLock;

use super::store::PersonaStore;
use super::{Persona, default_personas};
use crate::state_store::StateStore;

/// Manages persona lifecycle and coordinates persona-aware execution.
pub struct PersonaManager {
    store: PersonaStore,
    active_persona_id: RwLock<Option<String>>,
    /// Optional shared `StateStore` for automatic persistence.
    /// When `None`, persistence calls are no-ops (tests, ephemeral runs).
    state_store: Option<Arc<StateStore>>,
    /// Callback invoked after a successful `set_active` to re-seed the
    /// intent engine's `system_prompt`.  Stored on the manager (not
    /// `PersonaApi`) so ephemeral `PersonaApi` instances in `PersonaTool`
    /// cannot lose it.
    #[allow(clippy::type_complexity)]
    reseed_callback: RwLock<Option<Arc<dyn Fn(Option<String>) + Send + Sync>>>,
}

impl std::fmt::Debug for PersonaManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PersonaManager")
            .field("active_persona_id", &self.active_persona_id.read())
            .field("state_store", &self.state_store.is_some())
            .field("reseed_callback", &self.reseed_callback.read().is_some())
            .finish()
    }
}

impl PersonaManager {
    /// Creates a new persona manager with default personas.
    pub fn new() -> Self {
        let store = PersonaStore::new();
        let manager = Self {
            store,
            active_persona_id: RwLock::new(None),
            state_store: None,
            reseed_callback: RwLock::new(None),
        };
        manager.create_default_personas();
        manager
    }

    /// Creates a new persona manager, optionally loading from existing data.
    pub fn with_defaults(personas: Vec<Persona>) -> Self {
        let store = PersonaStore::new();
        store.load_from_slice(&personas);
        let this = Self {
            store,
            active_persona_id: RwLock::new(None),
            state_store: None,
            reseed_callback: RwLock::new(None),
        };
        // Set the first enabled persona as active by default.
        if let Some(first) = this.store.list_enabled().into_iter().next() {
            *this.active_persona_id.write() = Some(first.id);
        }
        this
    }

    /// Builder: attach a shared `StateStore`.
    pub fn with_state_store(mut self, store: Arc<StateStore>) -> Self {
        self.state_store = Some(store);
        self
    }

    /// Set the callback that re-seeds the intent engine's system_prompt.
    /// Called automatically by `set_active`.
    pub fn set_reseed_callback(&self, cb: Option<Arc<dyn Fn(Option<String>) + Send + Sync>>) {
        *self.reseed_callback.write() = cb;
    }

    /// Returns the current active persona, if any.
    pub fn get_active_persona(&self) -> Option<Persona> {
        let active_id = self.active_persona_id.read().clone();
        active_id.and_then(|id| self.store.get(&id))
    }

    /// Returns the system prompt for the active persona.
    /// Falls back to a default prompt if no active persona.
    pub fn active_system_prompt(&self) -> String {
        self.get_active_persona()
            .map(|p| p.system_prompt.clone())
            .unwrap_or_else(|| {
                "You are a helpful AI assistant that follows the Ouroboros methodology: \
                 specify before you build, evaluate before you ship."
                    .to_string()
            })
    }

    /// Creates the default personas (Dev, Review, Research, Architect, Mentor,
    /// Ops, Security, Writer, Planner).
    pub fn create_default_personas(&self) {
        let defaults = default_personas();
        for persona in defaults {
            // Only register if not already present.
            if self.store.get(&persona.id).is_none() {
                self.store.register(persona);
            }
        }
        // Set first persona as active if none is set.
        {
            let mut active = self.active_persona_id.write();
            if active.is_none() {
                *active = Some("dev".to_string());
            }
        }
        tracing::info!("Default personas initialized");
    }

    /// Returns the first enabled persona, for wiring into OuroborosEngine.
    pub fn first_enabled(&self) -> Option<Persona> {
        self.store.list_enabled().into_iter().next()
    }

    /// Returns the persona store for direct access.
    pub fn store(&self) -> &PersonaStore {
        &self.store
    }

    /// Returns the ID of the active persona.
    pub fn active_persona_id(&self) -> Option<String> {
        self.active_persona_id.read().clone()
    }

    // ── RFC-039 ────────────────────────────────────────────────────────────

    /// Active persona 의 우선순위 결정:
    ///   1. StateStore `index.json` 의 `active_persona_id` (enabled 면 적용)
    ///   2. `PersonaConfig.default_persona_id` (enabled 면 적용)
    ///   3. store 의 첫 번째 enabled
    ///   4. None
    ///
    /// `&self` — `Arc<PersonaManager>` 뒤에서 호출됨.
    pub fn apply_config(&self, cfg: &crate::config::PersonaConfig) {
        // 우선순위 1: 기존 active_persona_id 가 enabled 면 유지
        // (load_from_state_store 가 이미 StateStore 의 active_persona_id 를 박았음).
        if let Some(id) = self.active_persona_id()
            && self.store.get(&id).map(|p| p.enabled).unwrap_or(false)
        {
            return;
        }
        // 우선순위 2: config.default_persona_id
        if let Some(id) = cfg.default_persona_id.as_ref()
            && self.store.get(id).map(|p| p.enabled).unwrap_or(false)
        {
            *self.active_persona_id.write() = Some(id.clone());
            return;
        }
        // 우선순위 3: 첫 번째 enabled
        if let Some(p) = self.store.list_enabled().into_iter().next() {
            *self.active_persona_id.write() = Some(p.id);
        }
    }

    /// StateStore 에서 페르소나 + active_persona_id 를 로드.
    /// 손상·부재 시 silent fallback 하지 않고 `Result::Err` 로 전파.
    /// 호출자는 defaults 가 이미 new() 로 박혀 있음을 알고 있어야 함.
    pub async fn load_from_state_store(&self, store: &StateStore) -> Result<()> {
        let snap = crate::persona::persistence::load_from_state_store(store)
            .await?
            .ok_or_else(|| anyhow::anyhow!("persona: no snapshot present"))?;
        // store 가 이미 기본 페르소나를 들고 있어도 디스크가 우선.
        for p in &snap.personas {
            self.store.register(p.clone());
        }
        if let Some(active) = snap.active_persona_id
            && snap.personas.iter().any(|p| p.id == active && p.enabled)
        {
            *self.active_persona_id.write() = Some(active);
        }
        Ok(())
    }

    /// StateStore 에 페르소나 + active_persona_id 를 저장.
    /// `state_store` 가 설정되지 않은 경우 no-op (`Ok(())`).
    /// 메모리 상태는 유지, IO 실패는 `Result::Err` 로 전파.
    pub async fn persist(&self) -> Result<()> {
        let Some(ref store) = self.state_store else {
            return Ok(());
        };
        let snapshot = crate::persona::persistence::PersonaSnapshot {
            schema_version: 1,
            active_persona_id: self.active_persona_id(),
            personas: self.store.list_all(),
        };
        crate::persona::persistence::save_to_state_store(store, &snapshot).await
    }

    /// 글로벌 활성 페르소나 변경.
    ///
    /// 단일 진리 경로: 슬롯 변경 → persist (내부 StateStore) → reseed (callback).
    /// 모든 호출자 (HTTP, PersonaTool, Gateway, delete handler) 가 이 메서드를
    /// 거치므로 영속화 누락이나 intent engine 미재시드가 발생하지 않는다.
    ///
    /// 새 system_prompt 를 `Ok(Some(prompt))` 로 반환.
    ///
    /// `&self` — interior mutability (Arc 뒤 호출 가능).
    pub async fn set_active(&self, id: &str) -> Result<Option<String>> {
        let persona = self
            .store
            .get(id)
            .ok_or_else(|| anyhow::anyhow!("Persona '{id}' not found"))?;
        if !persona.enabled {
            anyhow::bail!("Persona '{id}' is disabled");
        }
        *self.active_persona_id.write() = Some(id.to_string());
        tracing::info!(persona_id = %id, name = %persona.name, "Active persona set");

        // Persist (no-op if no state_store).
        if let Err(e) = self.persist().await {
            tracing::warn!(error = %e, "persona set_active: persist failed");
        }

        // Re-seed intent engine if callback is set.
        let prompt = persona.system_prompt.clone();
        if let Some(ref cb) = *self.reseed_callback.read() {
            cb(Some(prompt.clone()));
        }
        Ok(Some(prompt))
    }
}

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

impl Clone for PersonaManager {
    fn clone(&self) -> Self {
        let personas: Vec<Persona> = self.store.list_all();
        let store = PersonaStore::new();
        store.load_from_slice(&personas);
        Self {
            store,
            active_persona_id: RwLock::new(self.active_persona_id.read().clone()),
            // Arc clone — shares the same underlying store/callback.
            state_store: self.state_store.clone(),
            reseed_callback: RwLock::new(self.reseed_callback.read().clone()),
        }
    }
}

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

    fn make_store() -> Arc<StateStore> {
        let dir = tempfile::tempdir().unwrap();
        Arc::new(StateStore::new(dir.keep()).unwrap())
    }

    #[tokio::test]
    async fn test_load_from_state_store_round_trip() {
        let store = make_store();
        let pm = PersonaManager::new().with_state_store(store.clone());
        // Create a custom persona, persist, then load into a fresh manager.
        let custom = Persona {
            id: "custom-1".to_string(),
            name: "Custom".to_string(),
            role: "custom".to_string(),
            description: "Custom test persona".to_string(),
            system_prompt: "You are Custom.".to_string(),
            enabled: true,
            model: None,
            personality_traits: vec![],
        };
        pm.store().register(custom);
        pm.set_active("custom-1").await.unwrap();
        pm.persist().await.unwrap();

        // Fresh manager — load from disk.
        let pm2 = PersonaManager::new().with_state_store(store.clone());
        pm2.load_from_state_store(&store).await.unwrap();
        assert!(pm2.store().get("custom-1").is_some());
        assert_eq!(pm2.active_persona_id(), Some("custom-1".to_string()));
        // Defaults should also still be present (new() created them).
        assert!(pm2.store().get("dev").is_some());
    }

    #[tokio::test]
    async fn test_load_no_file_is_ok() {
        let store = make_store();
        let pm = PersonaManager::new().with_state_store(store.clone());
        let result = pm.load_from_state_store(&store).await;
        // No file → Err (we require a snapshot). But defaults from new() remain.
        assert!(result.is_err());
        assert!(pm.store().get("dev").is_some());
    }

    #[tokio::test]
    async fn test_apply_config_default_persona_id() {
        let pm = PersonaManager::new();
        // Clear active so apply_config must pick from config.
        *pm.active_persona_id.write() = None;
        let cfg = PersonaConfig {
            default_persona_id: Some("review".to_string()),
        };
        pm.apply_config(&cfg);
        assert_eq!(pm.active_persona_id(), Some("review".to_string()));
    }

    #[tokio::test]
    async fn test_apply_config_falls_back_to_first_enabled() {
        let pm = PersonaManager::new();
        *pm.active_persona_id.write() = None;
        let cfg = PersonaConfig {
            default_persona_id: None,
        };
        pm.apply_config(&cfg);
        // First enabled persona is "dev" (order: dev, review, research).
        assert_eq!(pm.active_persona_id(), Some("dev".to_string()));
    }

    #[tokio::test]
    async fn test_apply_config_skips_disabled_default() {
        let pm = PersonaManager::new();
        *pm.active_persona_id.write() = None;
        // "review" is enabled by default; disable it.
        pm.store().set_enabled("review", false).unwrap();
        let cfg = PersonaConfig {
            default_persona_id: Some("review".to_string()),
        };
        pm.apply_config(&cfg);
        // review is disabled → fall back to first enabled = dev.
        assert_eq!(pm.active_persona_id(), Some("dev".to_string()));
    }

    #[tokio::test]
    async fn test_apply_config_keeps_existing_active() {
        let pm = PersonaManager::new();
        pm.set_active("research").await.unwrap();
        let cfg = PersonaConfig {
            default_persona_id: Some("dev".to_string()),
        };
        pm.apply_config(&cfg);
        // Existing active (research, enabled) wins over config default.
        assert_eq!(pm.active_persona_id(), Some("research".to_string()));
    }

    #[tokio::test]
    async fn test_set_active_rejects_disabled() {
        let pm = PersonaManager::new();
        pm.store().set_enabled("review", false).unwrap();
        let result = pm.set_active("review").await;
        assert!(result.is_err());
        assert_eq!(pm.active_persona_id(), Some("dev".to_string()));
    }

    #[tokio::test]
    async fn test_set_active_rejects_unknown_id() {
        let pm = PersonaManager::new();
        let result = pm.set_active("nonexistent").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_set_active_returns_system_prompt() {
        let pm = PersonaManager::new();
        let prompt = pm.set_active("review").await.unwrap();
        assert!(prompt.is_some());
        assert!(prompt.unwrap().contains("Review"));
    }

    #[tokio::test]
    async fn test_set_active_fires_reseed_callback() {
        let pm = PersonaManager::new();
        let received = Arc::new(parking_lot::Mutex::new(None::<String>));
        let received_cb = received.clone();
        pm.set_reseed_callback(Some(Arc::new(move |prompt| {
            *received_cb.lock() = prompt;
        })));
        pm.set_active("review").await.unwrap();
        assert!(received.lock().as_ref().unwrap().contains("Review"));
    }

    #[tokio::test]
    async fn test_set_active_no_callback_still_works() {
        let pm = PersonaManager::new();
        // No callback set — should not panic.
        let result = pm.set_active("research").await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_set_active_persists_when_store_set() {
        let store = make_store();
        let pm = PersonaManager::new().with_state_store(store.clone());
        pm.set_active("research").await.unwrap();

        // Fresh manager loads the persisted active.
        let pm2 = PersonaManager::new().with_state_store(store.clone());
        pm2.load_from_state_store(&store).await.unwrap();
        assert_eq!(pm2.active_persona_id(), Some("research".to_string()));
    }

    #[tokio::test]
    async fn test_set_active_no_persist_without_store() {
        let pm = PersonaManager::new();
        // No state_store — persist is no-op, should succeed.
        pm.set_active("research").await.unwrap();
        assert_eq!(pm.active_persona_id(), Some("research".to_string()));
    }

    #[tokio::test]
    async fn test_persist_round_trip_preserves_active() {
        let store = make_store();
        let pm = PersonaManager::new().with_state_store(store.clone());
        pm.set_active("research").await.unwrap();
        pm.persist().await.unwrap();

        let pm2 = PersonaManager::new().with_state_store(store.clone());
        pm2.load_from_state_store(&store).await.unwrap();
        assert_eq!(pm2.active_persona_id(), Some("research".to_string()));
    }

    #[tokio::test]
    async fn test_clone_preserves_state_store_and_callback() {
        let store = make_store();
        let pm = PersonaManager::new().with_state_store(store.clone());
        let received = Arc::new(parking_lot::Mutex::new(None::<String>));
        let received_cb = received.clone();
        pm.set_reseed_callback(Some(Arc::new(move |prompt| {
            *received_cb.lock() = prompt;
        })));

        let cloned = pm.clone();
        // Cloned manager should persist and reseed via the shared Arcs.
        cloned.set_active("research").await.unwrap();
        assert!(received.lock().is_some());
    }
}