use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use kian_core::model::SessionId;
use super::failure::{
Cause, ClassDefault, Outcome, SpeechCode, grant_keyed_default, resolve_outcome,
};
use super::grants::Grant;
use super::ids::HookId;
use super::ports::SettleCtl;
use super::registry::{Entry, HookPayload, HookReply, Registry, global_registry};
use super::resolve;
use super::session;
use super::views::{
AdmitDraft, AdmitReceiptView, AdmitTarget, AdmitVerdict, FrameInfo, Origin, RecoveryView,
RequestMeta, ResponseView, SettleVerdict, SettleView, ToolCallView, ToolPostView, ToolVerdict,
};
pub const VETO_MARKER_PREFIX: &str = "[kian:hook-veto plugin=";
pub const DEFAULT_MAX_SETTLE_VETOS: u64 = 3;
pub fn veto_marker(plugin: &str, hook: &str, reason: &str) -> String {
format!(
"[kian:hook-veto plugin={plugin} hook={hook} reason={reason}] {reason} — address this before finishing."
)
}
pub fn veto_marker_prefix(plugin: &str) -> String {
format!("{VETO_MARKER_PREFIX}{plugin} ")
}
pub fn is_veto_marker(plugin: &str, synthetic: bool, content: &str) -> bool {
synthetic && content.starts_with(&veto_marker_prefix(plugin))
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub(crate) struct Discard {
pub plugin: String,
pub code: SpeechCode,
pub grant: Option<Grant>,
}
#[allow(dead_code)]
#[derive(Debug, Default, Clone)]
pub(crate) struct DispatchRecord {
pub invoked: usize,
pub winner: Option<String>,
pub reason: Option<String>,
pub skipped: usize,
pub discards: Vec<Discard>,
pub defaults: Vec<(String, ClassDefault)>,
}
impl DispatchRecord {
fn denied(&self) -> bool {
self.winner.is_some()
}
}
struct BusInner {
registry: Arc<Registry>,
next_dispatch_id: AtomicU64,
settle_failed_admits: Mutex<HashMap<(SessionId, String), u32>>,
sidecars: session::SharedRuntime,
}
#[derive(Clone)]
pub struct HookBus {
inner: Arc<BusInner>,
}
impl Default for HookBus {
fn default() -> Self {
Self::new()
}
}
impl HookBus {
pub fn new() -> Self {
Self::with_registry(Arc::clone(global_registry()))
}
pub fn with_registry(registry: Arc<Registry>) -> Self {
Self {
inner: Arc::new(BusInner {
registry,
next_dispatch_id: AtomicU64::new(0),
settle_failed_admits: Mutex::new(HashMap::new()),
sidecars: std::sync::RwLock::new(None),
}),
}
}
pub fn install_sidecars(
&self,
cfg: resolve::SidecarConfig,
manifests: std::collections::BTreeMap<String, super::sidecar::manifest::Manifest>,
) -> Result<(), String> {
let mut slot = self
.inner
.sidecars
.write()
.expect("kian-plugin: sidecar runtime lock poisoned");
if slot.is_some() {
return Err(
"sidecar runtime already installed — no live reload in v1 (§13.1); restart \
`kian serve` to change the set"
.to_string(),
);
}
*slot = Some(Arc::new(session::SidecarRuntime::new(cfg, manifests)));
Ok(())
}
pub fn session_begin(&self, frame: FrameInfo) {
let runtime = self.sidecar_runtime();
if let Some(runtime) = runtime {
runtime.drain_begin(&frame);
}
}
pub fn session_set_digest(&self, session: &SessionId) -> Option<String> {
self.sidecar_runtime()
.and_then(|rt| rt.recorded_digest(session))
}
pub fn session_quarantined(&self, session: &SessionId) -> bool {
self.sidecar_runtime()
.is_some_and(|rt| rt.is_quarantined(session))
}
fn sidecar_runtime(&self) -> Option<Arc<session::SidecarRuntime>> {
self.inner
.sidecars
.read()
.expect("kian-plugin: sidecar runtime lock poisoned")
.clone()
}
pub fn registry(&self) -> &Arc<Registry> {
&self.inner.registry
}
async fn dispatch(
&self,
hook: HookId,
payload: &HookPayload,
origin: Origin,
targets: usize,
) -> DispatchRecord {
self.dispatch_per(hook, payload.frame(), origin, targets, |_| payload.clone())
.await
}
async fn dispatch_per<F>(
&self,
hook: HookId,
frame: Option<&FrameInfo>,
origin: Origin,
targets: usize,
payload_for: F,
) -> DispatchRecord
where
F: Fn(&Entry) -> HookPayload,
{
let subscribers = self.inner.registry.snapshot(&hook, frame);
let mut record = DispatchRecord {
invoked: subscribers.len(),
..DispatchRecord::default()
};
let sidecars = self.sidecar_runtime();
let session_state = sidecars.as_ref().map(|rt| rt.state_for(frame));
let eligible = |subscriber: &Arc<Entry>| {
let sidecar = sidecars
.as_ref()
.is_some_and(|rt| rt.is_sidecar(subscriber.handler.plugin));
if sidecar && let Some(session::SessionState::Set(set)) = &session_state {
set.names.contains(subscriber.handler.plugin)
} else {
true
}
};
for (position, subscriber) in subscribers.iter().enumerate() {
let dispatch_id = format!(
"t0-{}",
self.inner.next_dispatch_id.fetch_add(1, Ordering::Relaxed)
);
let started = Instant::now();
let sidecar = sidecars
.as_ref()
.is_some_and(|rt| rt.is_sidecar(subscriber.handler.plugin));
if !eligible(subscriber) {
record.invoked -= 1;
continue;
}
let quarantined =
sidecar && matches!(session_state, Some(session::SessionState::Quarantined));
let payload = payload_for(subscriber);
let outcome = if quarantined {
Outcome::Default(grant_keyed_default(
&subscriber.handler.grants,
&hook,
origin,
Cause::Quarantine,
))
} else {
let reply: HookReply = (subscriber.f)(&payload).await;
resolve_outcome(&reply, &subscriber.handler.grants, &hook, origin)
};
let elapsed_ms = started.elapsed().as_millis() as u64;
let plugin = subscriber.handler.plugin;
match outcome {
Outcome::AsIfAbsent => {
audit_dispatch(&hook, plugin, frame, &dispatch_id, "observed", elapsed_ms);
}
Outcome::Denied { reason } => {
audit_dispatch(&hook, plugin, frame, &dispatch_id, "deny", elapsed_ms);
record.skipped = subscribers[position + 1..]
.iter()
.filter(|s| eligible(s))
.count();
audit_veto(
&hook,
plugin,
frame,
&dispatch_id,
&reason,
record.skipped,
targets,
);
record.winner = Some(plugin.to_string());
record.reason = Some(reason);
break;
}
Outcome::Discarded { code, grant } => {
audit_dispatch(&hook, plugin, frame, &dispatch_id, "discarded", elapsed_ms);
audit_discard(&hook, plugin, code, grant);
record.discards.push(Discard {
plugin: plugin.to_string(),
code,
grant,
});
}
Outcome::Default(class_default) => match class_default {
ClassDefault::Deny { cause } | ClassDefault::RefuseAdmission { cause } => {
let reason = format!("hook unavailable ({})", cause.as_str());
audit_dispatch(
&hook,
plugin,
frame,
&dispatch_id,
"default-deny",
elapsed_ms,
);
audit_default(&hook, plugin, Some(cause));
record.skipped = subscribers[position + 1..]
.iter()
.filter(|s| eligible(s))
.count();
audit_veto(
&hook,
plugin,
frame,
&dispatch_id,
&reason,
record.skipped,
targets,
);
record.winner = Some(plugin.to_string());
record.reason = Some(reason);
record.defaults.push((plugin.to_string(), class_default));
break;
}
ClassDefault::Abstain | ClassDefault::Lapse => {
audit_dispatch(
&hook,
plugin,
frame,
&dispatch_id,
"default-lapse",
elapsed_ms,
);
audit_default(&hook, plugin, None);
record.defaults.push((plugin.to_string(), class_default));
}
},
}
}
record
}
pub async fn admit(&self, draft: &AdmitDraft) -> AdmitVerdict {
let mut grid: Vec<(AdmitTarget, FrameInfo)> = if draft.frames.len() == draft.targets.len() {
draft
.targets
.iter()
.cloned()
.zip(draft.frames.iter().cloned())
.collect()
} else {
draft
.targets
.iter()
.map(|t| (t.clone(), FrameInfo::new(t.session)))
.collect()
};
grid.sort_by_key(|(target, _)| target.session);
let origin = draft.origin;
let mut stop: Option<(String, String)> = None;
for (_target, frame) in &grid {
if stop.is_some() {
continue;
}
let mut framed = draft.clone();
framed.frames = vec![frame.clone()];
let record = self
.dispatch(
HookId::AdmitPre,
&HookPayload::AdmitDraft(framed),
origin,
draft.targets.len(),
)
.await;
if record.denied() {
stop = record.winner.zip(record.reason);
}
}
match stop {
Some((plugin, reason)) => AdmitVerdict::Stop { plugin, reason },
None => AdmitVerdict::Next,
}
}
pub async fn admit_post(&self, receipt: &AdmitReceiptView) {
let _ = self
.dispatch(
HookId::AdmitPost,
&HookPayload::AdmitReceipt(receipt.clone()),
receipt.origin,
1,
)
.await;
}
pub async fn request(&self, meta: &RequestMeta) {
let _ = self
.dispatch(
HookId::TurnRequest,
&HookPayload::TurnRequest(meta.clone()),
Origin::Api,
1,
)
.await;
}
pub async fn response(&self, view: &ResponseView) {
let _ = self
.dispatch(
HookId::TurnResponse,
&HookPayload::TurnResponse(view.clone()),
Origin::Api,
1,
)
.await;
}
pub async fn tool_pre(&self, call: &ToolCallView) -> ToolVerdict {
let record = self
.dispatch(
HookId::ToolPre,
&HookPayload::ToolPre(call.clone()),
Origin::Api,
1,
)
.await;
match (record.winner, record.reason) {
(Some(plugin), Some(reason)) => ToolVerdict::Deny { plugin, reason },
_ => ToolVerdict::Allow,
}
}
pub async fn tool_post(&self, view: &ToolPostView) {
let _ = self
.dispatch(
HookId::ToolPost,
&HookPayload::ToolPost(view.clone()),
Origin::Api,
1,
)
.await;
}
pub async fn recover(&self, view: &RecoveryView) {
let _ = self
.dispatch(
HookId::RecoverDecided,
&HookPayload::Recovery(view.clone()),
Origin::Api,
1,
)
.await;
}
pub async fn settle(&self, view: &SettleView, ctl: &dyn SettleCtl) -> SettleVerdict {
let subscribers = self
.inner
.registry
.snapshot(&HookId::SettlePre, view.frame.as_ref());
let mut used_of: HashMap<&str, u64> = HashMap::with_capacity(subscribers.len());
let mut limit_of: HashMap<&str, u64> = HashMap::with_capacity(subscribers.len());
for subscriber in &subscribers {
let plugin = subscriber.handler.plugin;
let stream_used = ctl.veto_count(plugin);
let failed_admits = self.failed_settle_admits(view, plugin);
used_of.insert(plugin, stream_used.max(u64::from(failed_admits)));
let limit = self
.sidecar_runtime()
.map_or(DEFAULT_MAX_SETTLE_VETOS, |rt| {
rt.settle_veto_limit(view.frame.as_ref(), plugin)
});
limit_of.insert(plugin, limit);
}
let record = self
.dispatch_per(
HookId::SettlePre,
view.frame.as_ref(),
Origin::Api,
1,
|subscriber| {
let plugin = subscriber.handler.plugin;
let used = used_of.get(plugin).copied().unwrap_or(0);
let limit = limit_of
.get(plugin)
.copied()
.unwrap_or(DEFAULT_MAX_SETTLE_VETOS);
let mut per_plugin = view.clone();
per_plugin.vetoes_used = used;
per_plugin.vetoes_remaining = limit.saturating_sub(used);
HookPayload::Settle(per_plugin)
},
)
.await;
let (plugin, reason) = match (record.winner, record.reason) {
(Some(plugin), Some(reason)) => (plugin, reason),
_ => return SettleVerdict::Allow,
};
let used = used_of.get(plugin.as_str()).copied().unwrap_or(0);
let limit = limit_of
.get(plugin.as_str())
.copied()
.unwrap_or(DEFAULT_MAX_SETTLE_VETOS);
if used >= limit {
audit_override(&plugin, view, used, &reason);
return SettleVerdict::Override {
plugin,
reason,
vetoes_used: used,
};
}
let marker = veto_marker(&plugin, "settle/pre", &reason);
if let Err(error) = ctl.admit_synthetic(marker).await {
self.note_failed_settle_admit(view, &plugin);
let vetoes_used = used + 1;
audit_veto_admit_failed(&plugin, view, vetoes_used, &error, &reason);
return SettleVerdict::AdmitFailed {
plugin,
reason,
error,
vetoes_used,
};
}
SettleVerdict::Deny { plugin, reason }
}
fn failed_settle_admits(&self, view: &SettleView, plugin: &str) -> u32 {
let Some(frame) = view.frame.as_ref() else {
return 0;
};
self.inner
.settle_failed_admits
.lock()
.expect("kian-plugin: settle-attempts lock poisoned")
.get(&(frame.session, plugin.to_string()))
.copied()
.unwrap_or(0)
}
fn note_failed_settle_admit(&self, view: &SettleView, plugin: &str) {
let Some(frame) = view.frame.as_ref() else {
return;
};
*self
.inner
.settle_failed_admits
.lock()
.expect("kian-plugin: settle-attempts lock poisoned")
.entry((frame.session, plugin.to_string()))
.or_insert(0) += 1;
}
}
fn audit_dispatch(
hook: &HookId,
plugin: &str,
frame: Option<&FrameInfo>,
dispatch: &str,
outcome: &str,
elapsed_ms: u64,
) {
match frame {
Some(f) => {
tracing::info!(
target: "kian::hook",
hook = %hook,
plugin = plugin,
session = %f.session.0,
worker = %f.worker,
seq = f.seq,
epoch = ?f.epoch,
dispatch = dispatch,
outcome = outcome,
elapsed_ms = elapsed_ms,
"hook-dispatch"
);
}
None => {
tracing::info!(
target: "kian::hook",
hook = %hook,
plugin = plugin,
dispatch = dispatch,
outcome = outcome,
elapsed_ms = elapsed_ms,
"hook-dispatch"
);
}
}
}
fn audit_veto(
hook: &HookId,
plugin: &str,
frame: Option<&FrameInfo>,
dispatch: &str,
reason: &str,
skipped: usize,
targets: usize,
) {
let session = frame.map(|f| f.session.0.to_string()).unwrap_or_default();
tracing::info!(
target: "kian::hook",
hook = %hook,
plugin = plugin,
session = %session,
dispatch = dispatch,
reason = reason,
skipped = skipped,
winner = plugin,
targets = targets,
"hook-veto"
);
}
fn audit_default(hook: &HookId, plugin: &str, cause: Option<super::failure::Cause>) {
match cause {
Some(cause) => {
tracing::info!(
target: "kian::hook",
hook = %hook,
plugin = plugin,
reason = cause.as_str(),
"hook-default"
);
}
None => {
tracing::info!(
target: "kian::hook",
hook = %hook,
plugin = plugin,
"hook-default"
);
}
}
}
fn audit_discard(hook: &HookId, plugin: &str, code: SpeechCode, grant: Option<Grant>) {
let grant = grant.map(|g| g.as_str().to_string()).unwrap_or_default();
tracing::info!(
target: "kian::hook",
hook = %hook,
plugin = plugin,
code = code.as_str(),
grant = %grant,
outcome = "discarded",
"hook-discard"
);
}
fn audit_override(plugin: &str, view: &SettleView, vetoes: u64, reason: &str) {
let session = view
.frame
.as_ref()
.map(|f| f.session.0.to_string())
.unwrap_or_default();
tracing::info!(
target: "kian::hook",
hook = "settle/pre",
plugin = plugin,
session = %session,
vetoes = vetoes,
reason = reason,
"hook-override"
);
}
fn audit_veto_admit_failed(
plugin: &str,
view: &SettleView,
vetoes: u64,
error: &str,
reason: &str,
) {
let session = view
.frame
.as_ref()
.map(|f| f.session.0.to_string())
.unwrap_or_default();
tracing::info!(
target: "kian::hook",
hook = "settle/pre",
plugin = plugin,
session = %session,
vetoes = vetoes,
error = error,
reason = reason,
"hook-veto-admit-failed"
);
}
#[cfg(test)]
mod tests {
use std::sync::Mutex;
use futures::FutureExt;
use super::super::grants::GrantSet;
use super::super::registry::{HandlerId, SessionScope};
use super::*;
fn bus() -> HookBus {
HookBus::with_registry(Arc::new(Registry::new()))
}
fn tool_pre_handler(
bus: &HookBus,
plugin: &'static str,
grants: &[&str],
order: i32,
reply: HookReply,
log: Arc<Mutex<Vec<String>>>,
) -> crate::ctx::Disposer {
let handler = HandlerId {
plugin,
hook: HookId::ToolPre,
grants: GrantSet::from_names(grants).unwrap(),
order,
};
bus.registry().register(
handler,
Arc::new(move |_payload: &HookPayload| {
let log = Arc::clone(&log);
let reply = reply.clone();
async move {
log.lock().unwrap().push(plugin.to_string());
reply
}
.boxed()
}),
)
}
fn tool_pre_view(frame: Option<FrameInfo>) -> ToolCallView {
ToolCallView {
call_id: "call_1".to_string(),
name: "exec".to_string(),
iteration: 1,
arguments: serde_json::json!({"cmd": ["ls"]}),
arguments_bytes: 12,
arguments_sha256: "ab".to_string(),
arguments_truncated: false,
permission: super::super::views::PermissionView {
evaluated: "allow".to_string(),
policy: "default".to_string(),
matched: "exec".to_string(),
reason: None,
},
surface: "react".to_string(),
frame,
}
}
fn frame() -> Option<FrameInfo> {
Some(FrameInfo::new(
serde_json::from_str("\"0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b\"").unwrap(),
))
}
#[tokio::test]
async fn out_of_grant_reply_is_discarded_as_if_absent() {
let bus = bus();
let log = Arc::new(Mutex::new(Vec::new()));
let _keep = tool_pre_handler(
&bus,
"audit-only",
&["observe:tool"],
0,
HookReply::BailDeny {
reason: "I deny anyway".to_string(),
},
Arc::clone(&log),
);
let verdict = bus.tool_pre(&tool_pre_view(frame())).await;
assert_eq!(
verdict,
ToolVerdict::Allow,
"an out-of-grant deny is discarded — never a denial, never the class default"
);
assert_eq!(*log.lock().unwrap(), vec!["audit-only".to_string()]);
let record = bus
.dispatch(
HookId::ToolPre,
&HookPayload::ToolPre(tool_pre_view(frame())),
Origin::Api,
1,
)
.await;
assert_eq!(record.discards.len(), 1);
assert_eq!(record.discards[0].plugin, "audit-only");
assert_eq!(record.discards[0].code, SpeechCode::Unauthorized);
assert_eq!(record.discards[0].grant, Some(Grant::VetoTool));
}
#[tokio::test]
async fn wrong_mode_reply_is_discarded() {
let bus = bus();
let log = Arc::new(Mutex::new(Vec::new()));
let _keep = tool_pre_handler(
&bus,
"confused",
&["observe:tool", "veto:tool"],
0,
HookReply::Ack { note: None },
Arc::clone(&log),
);
let verdict = bus.tool_pre(&tool_pre_view(frame())).await;
assert_eq!(verdict, ToolVerdict::Allow);
assert_eq!(*log.lock().unwrap(), vec!["confused".to_string()]);
log.lock().unwrap().clear();
let record = bus
.dispatch(
HookId::ToolPre,
&HookPayload::ToolPre(tool_pre_view(frame())),
Origin::Api,
1,
)
.await;
assert_eq!(record.discards.len(), 1);
assert_eq!(record.discards[0].code, SpeechCode::Malformed);
assert_eq!(*log.lock().unwrap(), vec!["confused".to_string()]);
}
#[tokio::test]
async fn first_deny_wins_and_skips_rest() {
let bus = bus();
let log = Arc::new(Mutex::new(Vec::new()));
let _keep = tool_pre_handler(
&bus,
"gate",
&["observe:tool", "veto:tool"],
0,
HookReply::BailDeny {
reason: "blocked".to_string(),
},
Arc::clone(&log),
);
let _keep = tool_pre_handler(
&bus,
"observer",
&["observe:tool"],
1,
HookReply::BailNone,
Arc::clone(&log),
);
let verdict = bus.tool_pre(&tool_pre_view(frame())).await;
assert_eq!(
verdict,
ToolVerdict::Deny {
plugin: "gate".to_string(),
reason: "blocked".to_string()
}
);
assert_eq!(
*log.lock().unwrap(),
vec!["gate".to_string()],
"subscribers after the first deny are not consulted"
);
let record = bus
.dispatch(
HookId::ToolPre,
&HookPayload::ToolPre(tool_pre_view(frame())),
Origin::Api,
1,
)
.await;
assert_eq!(
record.skipped, 1,
"the skipped count names the not-consulted subscriber"
);
assert_eq!(record.winner.as_deref(), Some("gate"));
}
#[tokio::test]
async fn handler_order_tiebreak_is_declaration_index() {
let bus = bus();
let log = Arc::new(Mutex::new(Vec::new()));
let _keep = tool_pre_handler(
&bus,
"first-declared",
&["observe:tool"],
0,
HookReply::BailNone,
Arc::clone(&log),
);
let _keep = tool_pre_handler(
&bus,
"second-declared",
&["observe:tool"],
0,
HookReply::BailNone,
Arc::clone(&log),
);
let _keep = tool_pre_handler(
&bus,
"ordered-early",
&["observe:tool"],
-1,
HookReply::BailNone,
Arc::clone(&log),
);
bus.tool_pre(&tool_pre_view(frame())).await;
assert_eq!(
*log.lock().unwrap(),
vec![
"ordered-early".to_string(),
"first-declared".to_string(),
"second-declared".to_string(),
],
"order ascends; ties break by declaration index"
);
}
#[tokio::test]
async fn unreachable_veto_denies_at_any_order_position() {
let bus = bus();
let log = Arc::new(Mutex::new(Vec::new()));
let _keep = tool_pre_handler(
&bus,
"abstainer",
&["observe:tool", "veto:tool"],
0,
HookReply::BailNone,
Arc::clone(&log),
);
let _keep = tool_pre_handler(
&bus,
"wedged-gate",
&["observe:tool", "veto:tool"],
1,
HookReply::Err {
code: super::super::registry::ReplyErrCode::Deadline,
message: "over deadline".to_string(),
},
Arc::clone(&log),
);
let verdict = bus.tool_pre(&tool_pre_view(frame())).await;
assert_eq!(
verdict,
ToolVerdict::Deny {
plugin: "wedged-gate".to_string(),
reason: "hook unavailable (deadline)".to_string(),
},
"the fail-closed default's reason is the honest-error tail"
);
assert_eq!(
*log.lock().unwrap(),
vec!["abstainer".to_string(), "wedged-gate".to_string()]
);
}
#[tokio::test]
async fn scoped_handler_joins_only_its_session_dispatch() {
let bus = bus();
let hits = Arc::new(Mutex::new(0u32));
let scoped: SessionId =
serde_json::from_str("\"0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b\"").unwrap();
let other: SessionId =
serde_json::from_str("\"1f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b\"").unwrap();
let counter = Arc::clone(&hits);
let _keep = bus.registry().register_scoped(
SessionScope::Session(scoped),
HandlerId {
plugin: "scoped-gate",
hook: HookId::ToolPre,
grants: GrantSet::from_names(&["observe:tool", "veto:tool"]).unwrap(),
order: 0,
},
Arc::new(move |_payload: &HookPayload| {
let counter = Arc::clone(&counter);
async move {
*counter.lock().unwrap() += 1;
HookReply::BailNone
}
.boxed()
}),
);
bus.tool_pre(&tool_pre_view(Some(FrameInfo::new(other))))
.await;
assert_eq!(*hits.lock().unwrap(), 0, "not this session's handler");
bus.tool_pre(&tool_pre_view(Some(FrameInfo::new(scoped))))
.await;
assert_eq!(*hits.lock().unwrap(), 1);
}
mod sidecar_dispatch {
use super::*;
fn sidecar_manifest(name: &str) -> super::super::super::sidecar::manifest::Manifest {
let text = format!(
"[plugin]\nname = \"{name}\"\nversion = \"0.1.0\"\nabi = 1\n\
[process]\nargv = [\"/bin/cat\"]\nsha256 = \"{}\"\n\
[grants]\nhooks = [\"tool/pre\"]\ncapabilities = [\"observe:tool\"]\n\
[limits]\ndeadline_ms = 500\n",
"a".repeat(64)
);
let ctx = super::super::super::sidecar::ValidationCtx {
instance_key: name,
..Default::default()
};
super::super::super::sidecar::parse_manifest(&text, &ctx)
.expect("fixture manifest parses")
}
fn sidecar_cfg(server: &[&str]) -> resolve::SidecarConfig {
serde_json::from_value(serde_json::json!({
"plugins": {"gate": {"manifest": "/etc/kian/plugins/gate.toml"}},
"scopes": {"server": server}
}))
.unwrap()
}
fn installed_sidecars(bus: &HookBus, server: &[&str]) {
let mut manifests = std::collections::BTreeMap::new();
manifests.insert("gate".to_string(), sidecar_manifest("gate"));
bus.install_sidecars(sidecar_cfg(server), manifests)
.expect("boot-once install");
}
fn worker_frame(worker: &str, session: &str) -> Option<FrameInfo> {
let session: SessionId =
serde_json::from_str(&format!("\"{session}\"")).expect("valid uuid literal");
Some(FrameInfo {
session,
worker: worker.to_string(),
seq: 0,
epoch: Some(1),
})
}
#[tokio::test]
async fn mismatch_quarantines_sidecars_tier0_keeps_working() {
let bus = bus();
installed_sidecars(&bus, &["gate"]);
let log = Arc::new(Mutex::new(Vec::new()));
let _tier0 = tool_pre_handler(
&bus,
"watcher",
&["observe:tool"],
0,
HookReply::Ack { note: None },
Arc::clone(&log),
);
let _gate = tool_pre_handler(
&bus,
"gate",
&["observe:tool", "veto:tool"],
1,
HookReply::BailNone,
Arc::clone(&log),
);
let frame = worker_frame("", "0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b");
bus.session_begin(frame.clone().unwrap());
let verdict = bus.tool_pre(&tool_pre_view(frame.clone())).await;
assert_eq!(verdict, ToolVerdict::Allow);
assert_eq!(*log.lock().unwrap(), vec!["watcher", "gate"]);
bus.inner
.sidecars
.read()
.expect("sidecar runtime lock poisoned")
.as_ref()
.expect("runtime installed")
.reconfigure(sidecar_cfg(&[]));
bus.session_begin(frame.clone().unwrap());
assert!(bus.session_quarantined(&frame.as_ref().unwrap().session));
let verdict = bus.tool_pre(&tool_pre_view(frame.clone())).await;
assert_eq!(
verdict,
ToolVerdict::Deny {
plugin: "gate".to_string(),
reason: "hook unavailable (quarantine)".to_string(),
},
"the quarantined veto:tool holder fails closed without being consulted"
);
assert_eq!(
*log.lock().unwrap(),
vec!["watcher", "gate", "watcher"],
"the sidecar handler was NOT invoked; the tier-0 handler was"
);
}
#[tokio::test]
async fn per_worker_use_filters_the_sidecar_from_dispatch() {
let bus = bus();
let mut cfg = sidecar_cfg(&["gate"]);
cfg.scopes.worker.insert(
"node-zulu".to_string(),
resolve::WorkerScope {
use_: Some(vec![]),
limits: Default::default(),
},
);
let mut manifests = std::collections::BTreeMap::new();
manifests.insert("gate".to_string(), sidecar_manifest("gate"));
bus.install_sidecars(cfg, manifests).expect("install");
let log = Arc::new(Mutex::new(Vec::new()));
let _gate = tool_pre_handler(
&bus,
"gate",
&["observe:tool"],
0,
HookReply::BailNone,
Arc::clone(&log),
);
let zulu = worker_frame("node-zulu", "0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b");
bus.session_begin(zulu.clone().unwrap());
let verdict = bus.tool_pre(&tool_pre_view(zulu.clone())).await;
assert_eq!(verdict, ToolVerdict::Allow);
assert_eq!(
*log.lock().unwrap(),
Vec::<String>::new(),
"not in zulu's set"
);
let yankee = worker_frame("", "99999999-8888-7777-6666-555555555555");
bus.session_begin(yankee.clone().unwrap());
let verdict = bus.tool_pre(&tool_pre_view(yankee.clone())).await;
assert_eq!(verdict, ToolVerdict::Allow);
assert_eq!(
*log.lock().unwrap(),
vec!["gate"],
"in the server scope's set"
);
}
#[tokio::test]
async fn no_runtime_means_no_sidecar_behaviour() {
let bus = bus();
let session: SessionId =
serde_json::from_str("\"0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b\"").unwrap();
bus.session_begin(FrameInfo::new(session));
assert_eq!(bus.session_set_digest(&session), None);
assert!(!bus.session_quarantined(&session));
let log = Arc::new(Mutex::new(Vec::new()));
let _tier0 = tool_pre_handler(
&bus,
"watcher",
&["observe:tool"],
0,
HookReply::Ack { note: None },
Arc::clone(&log),
);
let verdict = bus
.tool_pre(&tool_pre_view(worker_frame(
"",
"0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b",
)))
.await;
assert_eq!(verdict, ToolVerdict::Allow);
assert_eq!(*log.lock().unwrap(), vec!["watcher"]);
}
#[tokio::test]
async fn second_install_is_refused() {
let bus = bus();
installed_sidecars(&bus, &["gate"]);
assert!(
bus.install_sidecars(sidecar_cfg(&["gate"]), Default::default())
.is_err()
);
}
}
fn admit_draft(origin: Origin, targets: &[&str]) -> AdmitDraft {
let targets: Vec<AdmitTarget> = targets
.iter()
.map(|&s| {
let session: SessionId =
serde_json::from_str(&format!("\"{s}\"")).expect("valid uuid literal");
AdmitTarget {
session,
router: false,
}
})
.collect();
let frames = targets.iter().map(|t| FrameInfo::new(t.session)).collect();
AdmitDraft {
origin,
targets,
input: super::super::views::AdmitInput {
role: "user".to_string(),
synthetic: false,
content_preview: "go".to_string(),
content_bytes: 2,
content_sha256: "ab".to_string(),
content_truncated: false,
inline_view: false,
},
channel: None,
admission: super::super::views::AdmissionInfo {
policy: "none".to_string(),
spilled: false,
},
frames,
}
}
fn admit_handler(
bus: &HookBus,
plugin: &'static str,
grants: &[&str],
reply: HookReply,
log: Arc<Mutex<Vec<String>>>,
) -> crate::ctx::Disposer {
let handler = HandlerId {
plugin,
hook: HookId::AdmitPre,
grants: GrantSet::from_names(grants).unwrap(),
order: 0,
};
bus.registry().register(
handler,
Arc::new(move |_payload: &HookPayload| {
let log = Arc::clone(&log);
let reply = reply.clone();
async move {
log.lock().unwrap().push(plugin.to_string());
reply
}
.boxed()
}),
)
}
#[tokio::test]
async fn unreachable_veto_admit_refuses_all_targets_at_channel_origin() {
let bus = bus();
let log = Arc::new(Mutex::new(Vec::new()));
let _keep = admit_handler(
&bus,
"border-guard",
&["observe:admit", "veto:admit"],
HookReply::Err {
code: super::super::registry::ReplyErrCode::Deadline,
message: "wedged".to_string(),
},
Arc::clone(&log),
);
let verdict = bus
.admit(&admit_draft(
Origin::Channel,
&[
"0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b",
"1f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b",
],
))
.await;
assert_eq!(
verdict,
AdmitVerdict::Stop {
plugin: "border-guard".to_string(),
reason: "hook unavailable (deadline)".to_string(),
},
"the fail-closed class default refuses the whole grid"
);
assert_eq!(
*log.lock().unwrap(),
vec!["border-guard".to_string()],
"the wedged holder was consulted exactly once — the second \
target's frame was never sent after the stop"
);
}
#[tokio::test]
async fn api_origin_admission_never_refuses() {
let bus = bus();
let log = Arc::new(Mutex::new(Vec::new()));
let _keep = admit_handler(
&bus,
"border-guard",
&["observe:admit", "veto:admit"],
HookReply::BailDeny {
reason: "I deny anyway".to_string(),
},
Arc::clone(&log),
);
let verdict = bus
.admit(&admit_draft(
Origin::Api,
&["0f9c2a11-3b4d-4e5f-8a6b-7c8d9e0f1a2b"],
))
.await;
assert_eq!(
verdict,
AdmitVerdict::Next,
"an API-origin deny is out-of-origin speech — discarded, never a stop"
);
assert_eq!(*log.lock().unwrap(), vec!["border-guard".to_string()]);
}
struct TestCtl {
counts: std::collections::HashMap<String, u64>,
admits: Arc<Mutex<Vec<String>>>,
}
impl SettleCtl for TestCtl {
fn admit_synthetic(
&self,
content: String,
) -> futures::future::BoxFuture<'static, Result<(), String>> {
self.admits.lock().unwrap().push(content);
Box::pin(async { Ok(()) })
}
fn veto_count(&self, plugin: &str) -> u64 {
self.counts.get(plugin).copied().unwrap_or(0)
}
}
fn settle_view(frame: Option<FrameInfo>) -> SettleView {
SettleView {
reason: "completed".to_string(),
turns: 1,
tool_calls: 0,
evidence: "none".to_string(),
vetoes_used: 0,
vetoes_remaining: 0,
episodes: 0,
frame,
}
}
fn settle_handler(
bus: &HookBus,
plugin: &'static str,
reply: HookReply,
seen: Arc<Mutex<Vec<(String, u64, u64)>>>,
) -> crate::ctx::Disposer {
let handler = HandlerId {
plugin,
hook: HookId::SettlePre,
grants: GrantSet::from_names(&["observe:settle", "veto:settle"]).unwrap(),
order: 0,
};
bus.registry().register(
handler,
Arc::new(move |payload: &HookPayload| {
let seen = Arc::clone(&seen);
let reply = reply.clone();
async move {
if let HookPayload::Settle(view) = payload {
seen.lock().unwrap().push((
plugin.to_string(),
view.vetoes_used,
view.vetoes_remaining,
));
}
reply
}
.boxed()
}),
)
}
fn sidecar_cfg_named(name: &str) -> resolve::SidecarConfig {
serde_json::from_value(serde_json::json!({
"plugins": {name: {"manifest": "/etc/kian/plugins/x.toml"}},
"scopes": {"server": [name]}
}))
.unwrap()
}
fn sidecar_manifest_with_veto_limit(
name: &str,
max_settle_vetoes: u64,
) -> super::super::sidecar::manifest::Manifest {
let text = format!(
"[plugin]\nname = \"{name}\"\nversion = \"0.1.0\"\nabi = 1\n\
[process]\nargv = [\"/bin/cat\"]\nsha256 = \"{}\"\n\
[grants]\nhooks = [\"settle/pre\"]\ncapabilities = [\"observe:settle\", \"veto:settle\"]\n\
[limits]\ndeadline_ms = 500\nmax_settle_vetoes = {max_settle_vetoes}\n",
"a".repeat(64)
);
let ctx = super::super::sidecar::ValidationCtx {
instance_key: name,
..Default::default()
};
super::super::sidecar::parse_manifest(&text, &ctx).expect("fixture manifest parses")
}
#[tokio::test]
async fn manifest_limit_zero_disables_the_gate_for_that_plugin() {
let bus = bus();
let mut manifests = std::collections::BTreeMap::new();
manifests.insert(
"gated".to_string(),
sidecar_manifest_with_veto_limit("gated", 0),
);
let cfg = sidecar_cfg_named("gated");
bus.install_sidecars(cfg, manifests).expect("install");
let seen: Arc<Mutex<Vec<(String, u64, u64)>>> = Arc::new(Mutex::new(Vec::new()));
let _keep = settle_handler(
&bus,
"gated",
HookReply::BailDeny {
reason: "not done".to_string(),
},
Arc::clone(&seen),
);
let admits = Arc::new(Mutex::new(Vec::new()));
let ctl = TestCtl {
counts: std::collections::HashMap::new(),
admits: Arc::clone(&admits),
};
let verdict = bus.settle(&settle_view(frame()), &ctl).await;
assert_eq!(
verdict,
SettleVerdict::Override {
plugin: "gated".to_string(),
reason: "not done".to_string(),
vetoes_used: 0,
},
"§6.1: 0 disables the gate outright — the deny is recorded, settlement proceeds"
);
assert!(
admits.lock().unwrap().is_empty(),
"a disabled gate admits no marker"
);
}
#[tokio::test]
async fn manifest_limit_wider_than_default_is_honored() {
let bus = bus();
let mut manifests = std::collections::BTreeMap::new();
manifests.insert(
"gated".to_string(),
sidecar_manifest_with_veto_limit("gated", 5),
);
let cfg = sidecar_cfg_named("gated");
bus.install_sidecars(cfg, manifests).expect("install");
let seen: Arc<Mutex<Vec<(String, u64, u64)>>> = Arc::new(Mutex::new(Vec::new()));
let _keep = settle_handler(
&bus,
"gated",
HookReply::BailDeny {
reason: "not done".to_string(),
},
Arc::clone(&seen),
);
let admits = Arc::new(Mutex::new(Vec::new()));
let mut counts = std::collections::HashMap::new();
counts.insert("gated".to_string(), 3);
let ctl = TestCtl {
counts,
admits: Arc::clone(&admits),
};
let verdict = bus.settle(&settle_view(frame()), &ctl).await;
assert!(matches!(verdict, SettleVerdict::Deny { .. }), "{verdict:?}");
assert_eq!(admits.lock().unwrap().len(), 1);
let mut counts = std::collections::HashMap::new();
counts.insert("gated".to_string(), 5);
let ctl = TestCtl {
counts,
admits: Arc::clone(&admits),
};
let verdict = bus.settle(&settle_view(frame()), &ctl).await;
assert!(
matches!(verdict, SettleVerdict::Override { vetoes_used: 5, .. }),
"exactly the manifest's 5, not the tier-0 3: {verdict:?}"
);
}
#[tokio::test]
async fn settle_budget_fields_are_per_plugin() {
let bus = bus();
let seen: Arc<Mutex<Vec<(String, u64, u64)>>> = Arc::new(Mutex::new(Vec::new()));
let _keep_a = settle_handler(&bus, "gate-a", HookReply::BailNone, Arc::clone(&seen));
let _keep_b = settle_handler(&bus, "gate-b", HookReply::BailNone, Arc::clone(&seen));
let mut counts = std::collections::HashMap::new();
counts.insert("gate-a".to_string(), 1);
counts.insert("gate-b".to_string(), 0);
let ctl = TestCtl {
counts,
admits: Arc::new(Mutex::new(Vec::new())),
};
let _ = bus.settle(&settle_view(frame()), &ctl).await;
let seen = seen.lock().unwrap().clone();
assert_eq!(
seen,
vec![("gate-a".to_string(), 1, 2), ("gate-b".to_string(), 0, 3),],
"gate-a reads its own 1-of-3; gate-b reads its own 0-of-3 — \
no shared constants"
);
}
#[test]
fn steer_minted_marker_matches_the_budget_predicate_regardless_of_role() {
let content = veto_marker("quality-gate", "settle/pre", "not done yet");
assert!(is_veto_marker("quality-gate", true, &content));
assert!(!is_veto_marker("quality", true, &content));
assert!(!is_veto_marker("quality-gate", false, &content));
}
}