khive-pack-brain 0.2.0

Brain pack — event-driven auto-tuning via meta-fold (ADR-064)
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
pub mod event;
pub mod fold;
pub mod state;
pub mod tunable;

use std::sync::Mutex;

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{json, Value};

use khive_fold::{Fold, FoldContext};
use khive_runtime::pack::PackRuntime;
use khive_runtime::{DispatchHook, KhiveRuntime, RuntimeError, VerbRegistry};
use khive_storage::event::{Event, EventFilter};
use khive_storage::types::PageRequest;
use khive_types::{Pack, VerbDef};

use crate::fold::EventFold;
use crate::state::BrainState;

const ENTITY_CACHE_CAPACITY: usize = 10_000;

pub struct BrainPack {
    runtime: KhiveRuntime,
    state: Mutex<BrainState>,
    fold: EventFold,
}

impl Pack for BrainPack {
    const NAME: &'static str = "brain";
    const NOTE_KINDS: &'static [&'static str] = &[];
    const ENTITY_KINDS: &'static [&'static str] = &[];
    const VERBS: &'static [VerbDef] = &BRAIN_VERBS;
    const REQUIRES: &'static [&'static str] = &["kg"];
}

static BRAIN_VERBS: [VerbDef; 5] = [
    VerbDef {
        name: "brain.state",
        description: "Return current BrainState snapshot for inspection",
    },
    VerbDef {
        name: "brain.config",
        description: "Return projected config for a named pack parameter",
    },
    VerbDef {
        name: "brain.events",
        description: "List recent brain-relevant events for debugging",
    },
    VerbDef {
        name: "brain.reset",
        description: "Reset posteriors to priors (preserves event history)",
    },
    VerbDef {
        name: "brain.emit",
        description: "Manually emit a feedback event for a specific entity",
    },
];

impl BrainPack {
    pub fn new(runtime: KhiveRuntime) -> Self {
        let fold = EventFold::new(ENTITY_CACHE_CAPACITY);
        let ctx = FoldContext::new();
        let state = fold.initial(&ctx);
        Self {
            runtime,
            state: Mutex::new(state),
            fold,
        }
    }

    async fn handle_state(&self, _params: Value) -> Result<Value, RuntimeError> {
        let state = self.state.lock().unwrap();
        let snapshot = state.to_snapshot();
        serde_json::to_value(&snapshot).map_err(|e| RuntimeError::InvalidInput(e.to_string()))
    }

    /// Public snapshot of the current `BrainState`.
    ///
    /// Equivalent to dispatching the `brain.state` verb but callable directly
    /// when you hold an `Arc<BrainPack>` (e.g. a test that registered the pack
    /// as a `DispatchHook` and wants to verify posteriors updated).
    pub fn snapshot(&self) -> crate::state::BrainStateSnapshot {
        self.state.lock().unwrap().to_snapshot()
    }

    async fn handle_config(&self, params: Value) -> Result<Value, RuntimeError> {
        #[derive(Deserialize)]
        struct ConfigParams {
            parameter: Option<String>,
        }
        let p: ConfigParams = serde_json::from_value(params)
            .map_err(|e| RuntimeError::InvalidInput(e.to_string()))?;

        let state = self.state.lock().unwrap();
        match p.parameter {
            Some(key) => {
                let posterior = state
                    .parameters
                    .get(&key)
                    .ok_or_else(|| RuntimeError::NotFound(format!("parameter {key:?}")))?;
                Ok(json!({
                    "parameter": key,
                    "mean": posterior.mean(),
                    "variance": posterior.variance(),
                    "ess": posterior.effective_sample_size(),
                    "alpha": posterior.alpha,
                    "beta": posterior.beta,
                }))
            }
            None => {
                let configs: serde_json::Map<String, Value> = state
                    .parameters
                    .iter()
                    .map(|(k, p)| {
                        (
                            k.clone(),
                            json!({
                                "mean": p.mean(),
                                "variance": p.variance(),
                                "ess": p.effective_sample_size(),
                            }),
                        )
                    })
                    .collect();
                Ok(Value::Object(configs))
            }
        }
    }

    async fn handle_events(&self, params: Value) -> Result<Value, RuntimeError> {
        #[derive(Deserialize)]
        struct EventsParams {
            namespace: Option<String>,
            limit: Option<u32>,
        }
        let p: EventsParams = serde_json::from_value(params)
            .map_err(|e| RuntimeError::InvalidInput(e.to_string()))?;

        let limit = p.limit.unwrap_or(20).min(100);
        let ns = self.runtime.ns(p.namespace.as_deref()).to_string();

        let store = self.runtime.events(p.namespace.as_deref())?;
        let filter = EventFilter {
            verbs: vec![
                "recall".into(),
                "search".into(),
                "brain.emit".into(),
                "get".into(),
                "remember".into(),
            ],
            namespaces: vec![ns],
            ..EventFilter::default()
        };
        let page = store
            .query_events(filter, PageRequest { offset: 0, limit })
            .await
            .map_err(|e| RuntimeError::InvalidInput(e.to_string()))?;

        let events: Vec<Value> = page
            .items
            .iter()
            .map(|e| {
                json!({
                    "id": e.id.to_string(),
                    "verb": e.verb,
                    "outcome": e.outcome,
                    "target_id": e.target_id.map(|t| t.to_string()),
                    "duration_us": e.duration_us,
                    "created_at": e.created_at,
                })
            })
            .collect();

        Ok(json!({
            "count": events.len(),
            "events": events,
        }))
    }

    async fn handle_reset(&self, _params: Value) -> Result<Value, RuntimeError> {
        let mut state = self.state.lock().unwrap();
        state.reset_posteriors();
        Ok(json!({
            "reset": true,
            "exploration_epoch": state.exploration_epoch,
        }))
    }

    async fn handle_emit(&self, params: Value) -> Result<Value, RuntimeError> {
        #[derive(Deserialize)]
        struct EmitParams {
            target_id: String,
            signal: String,
            namespace: Option<String>,
        }
        let p: EmitParams = serde_json::from_value(params)
            .map_err(|e| RuntimeError::InvalidInput(e.to_string()))?;

        let target: uuid::Uuid = p
            .target_id
            .parse()
            .map_err(|e| RuntimeError::InvalidInput(format!("invalid target_id: {e}")))?;

        let signal = match p.signal.as_str() {
            "useful" => "useful",
            "not_useful" => "not_useful",
            "wrong" => "wrong",
            other => {
                return Err(RuntimeError::InvalidInput(format!(
                    "unknown signal {other:?}; valid: useful | not_useful | wrong"
                )))
            }
        };

        let event = khive_storage::event::Event::new(
            self.runtime.ns(p.namespace.as_deref()).to_string(),
            "brain.emit",
            khive_types::SubstrateKind::Event,
            "brain",
        )
        .with_target(target)
        .with_data(json!({"signal": signal}));

        let store = self.runtime.events(p.namespace.as_deref())?;
        store
            .append_event(event.clone())
            .await
            .map_err(|e| RuntimeError::InvalidInput(e.to_string()))?;

        // Update brain state from this event
        let ctx = FoldContext::new();
        let mut state = self.state.lock().unwrap();
        let current = std::mem::replace(
            &mut *state,
            BrainState::new(std::collections::HashMap::new(), 0),
        );
        *state = self.fold.step(current, &event, &ctx);

        Ok(json!({
            "emitted": true,
            "event_id": event.id.to_string(),
            "signal": signal,
            "target_id": target.to_string(),
        }))
    }
}

// ── ADR-063: inventory self-registration ─────────────────────────────────────

struct BrainPackFactory;

impl khive_runtime::PackFactory for BrainPackFactory {
    fn name(&self) -> &'static str {
        "brain"
    }

    fn requires(&self) -> &'static [&'static str] {
        &["kg"]
    }

    fn create(&self, runtime: KhiveRuntime) -> Box<dyn PackRuntime> {
        Box::new(BrainPack::new(runtime))
    }
}

inventory::submit! { khive_runtime::PackRegistration(&BrainPackFactory) }

#[async_trait]
impl PackRuntime for BrainPack {
    fn name(&self) -> &str {
        <BrainPack as Pack>::NAME
    }

    fn note_kinds(&self) -> &'static [&'static str] {
        <BrainPack as Pack>::NOTE_KINDS
    }

    fn entity_kinds(&self) -> &'static [&'static str] {
        <BrainPack as Pack>::ENTITY_KINDS
    }

    fn verbs(&self) -> &'static [VerbDef] {
        &BRAIN_VERBS
    }

    fn requires(&self) -> &'static [&'static str] {
        <BrainPack as Pack>::REQUIRES
    }

    async fn dispatch(
        &self,
        verb: &str,
        params: Value,
        _registry: &VerbRegistry,
    ) -> Result<Value, RuntimeError> {
        match verb {
            "brain.state" => self.handle_state(params).await,
            "brain.config" => self.handle_config(params).await,
            "brain.events" => self.handle_events(params).await,
            "brain.reset" => self.handle_reset(params).await,
            "brain.emit" => self.handle_emit(params).await,
            _ => Err(RuntimeError::InvalidInput(format!(
                "brain pack does not handle verb {verb:?}"
            ))),
        }
    }
}

/// `BrainPack` as a post-dispatch hook (Issue #158).
///
/// When registered via `VerbRegistryBuilder::with_dispatch_hook`, every
/// successful verb dispatch calls `on_dispatch` with a synthesized `Event`.
/// The event is fed into `EventFold::step`, updating the brain's posteriors
/// in real time — no polling required.
///
/// This is opt-in: the hook must be explicitly registered. Registries that do
/// not load the brain pack are unaffected.
#[async_trait]
impl DispatchHook for BrainPack {
    async fn on_dispatch(&self, event: &Event) {
        let ctx = FoldContext::new();
        let mut state = self.state.lock().unwrap();
        // Replace state with fold result. BrainState is not Clone, so we
        // use mem::replace with a sentinel and immediately overwrite.
        let current = std::mem::replace(
            &mut *state,
            BrainState::new(std::collections::HashMap::new(), 0),
        );
        *state = self.fold.step(current, event, &ctx);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use khive_runtime::VerbRegistryBuilder;
    use serde_json::json;

    fn make_pack() -> BrainPack {
        let rt = KhiveRuntime::memory().expect("in-memory runtime");
        BrainPack::new(rt)
    }

    fn empty_registry() -> VerbRegistry {
        VerbRegistryBuilder::new()
            .build()
            .expect("empty registry builds successfully")
    }

    #[tokio::test]
    async fn dispatch_unknown_verb_returns_invalid_input() {
        let pack = make_pack();
        let registry = empty_registry();
        let err = pack
            .dispatch("brain.unknown", json!({}), &registry)
            .await
            .unwrap_err();
        if let RuntimeError::InvalidInput(msg) = &err {
            assert!(
                msg.contains("brain.unknown"),
                "expected verb name in error: {msg}"
            );
        } else {
            panic!("expected InvalidInput, got {err:?}");
        }
    }

    #[tokio::test]
    async fn dispatch_reset_returns_true_and_increments_epoch() {
        let pack = make_pack();
        let registry = empty_registry();
        let result = pack
            .dispatch("brain.reset", json!({}), &registry)
            .await
            .unwrap();
        assert_eq!(result["reset"], json!(true));
        assert_eq!(result["exploration_epoch"], json!(1u64));
    }

    #[tokio::test]
    async fn dispatch_emit_invalid_signal_returns_invalid_input() {
        let pack = make_pack();
        let registry = empty_registry();
        let target = "00000000-0000-0000-0000-000000000001";
        let err = pack
            .dispatch(
                "brain.emit",
                json!({"target_id": target, "signal": "bad_signal"}),
                &registry,
            )
            .await
            .unwrap_err();
        if let RuntimeError::InvalidInput(msg) = &err {
            assert!(
                msg.contains("bad_signal"),
                "expected signal name in error: {msg}"
            );
            assert!(
                msg.contains("valid"),
                "expected hint about valid values: {msg}"
            );
        } else {
            panic!("expected InvalidInput, got {err:?}");
        }
    }

    #[tokio::test]
    async fn dispatch_state_returns_snapshot_fields() {
        let pack = make_pack();
        let registry = empty_registry();
        let result = pack
            .dispatch("brain.state", json!({}), &registry)
            .await
            .unwrap();
        assert!(result.get("total_events").is_some(), "missing total_events");
        assert!(
            result.get("exploration_epoch").is_some(),
            "missing exploration_epoch"
        );
        assert!(result.get("parameters").is_some(), "missing parameters");
    }
}