session_core/request_layer.rs
1//! A layer over every lane's host requests, set once by a composition root whose root agent is not the session
2//! actor's (`ultron mcp`, whose root is Claude Code): it may answer a request itself (refuse an API that root cannot
3//! serve, such as `schedule.*` with nothing to fire it) or pass it on and see the answer (which root a spawn's work is
4//! charged to). Without one set, requests pass through unchanged.
5
6use kernel::{HostHandler, Signal};
7use serde_json::{Map, Value};
8use std::sync::{Arc, OnceLock};
9
10/// Passes the request (with this payload) on to the session's own handler.
11pub type Next<'a> = &'a dyn Fn(Map<String, Value>) -> Result<Value, String>;
12
13/// `(lane, kind, payload, next)`: answer the request, or call `next` and return (or inspect) what it answers.
14pub type RequestLayer = Arc<dyn Fn(&str, &str, Map<String, Value>, Next<'_>) -> Result<Value, String> + Send + Sync>;
15
16/// The outermost handler of a lane's requests: the stack's layer when one is set (read at each request, so a layer
17/// set after a kernel started still applies), else the inner handler.
18pub(crate) struct Layered {
19 pub inner: Arc<dyn HostHandler>,
20 pub layer: Arc<OnceLock<RequestLayer>>,
21 pub lane: String,
22}
23
24impl HostHandler for Layered {
25 fn handle(&self, kind: &str, payload: Map<String, Value>, signal: &Signal) -> Result<Value, String> {
26 match self.layer.get() {
27 Some(layer) => layer(&self.lane, kind, payload, &|payload| {
28 self.inner.handle(kind, payload, signal)
29 }),
30 None => self.inner.handle(kind, payload, signal),
31 }
32 }
33}