mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-25 14:53:34 +00:00
feat: extend lifecycle hooks with declarative bundles (#176)
* feat: add bundled and declarative hook bundle loading * fix: load plugin hooks only for active extensions * fix: avoid duplicate plugin hook registration * security: harden outbound webhook hooks * fix: pin webhook DNS resolutions for outbound hooks * fix: block IPv4-mapped local webhook targets * style: format webhook hardening changes for CI * fix: pass HookRegistry to ExtensionManager in AppBuilder After merging main (which extracted AppBuilder from main.rs in #198), the ExtensionManager::new() call in app.rs was missing the `hooks` parameter that PR #176 added. This moves HookRegistry creation before init_extensions() and threads it through, matching the existing pattern in main.rs. Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Illia Polosukhin <[email protected]> Co-authored-by: Claude Opus 4.6 <[email protected]>
This commit is contained in:
co-authored by
Illia Polosukhin
Claude Opus 4.6
parent
e42b1e5ec1
commit
e87d7bd066
+6
-6
@@ -278,7 +278,7 @@ This document tracks feature parity between IronClaw (Rust implementation) and O
|
||||
| Auth plugins | ✅ | ❌ | |
|
||||
| Memory plugins | ✅ | ❌ | Custom backends |
|
||||
| Tool plugins | ✅ | ✅ | WASM tools |
|
||||
| Hook plugins | ✅ | ❌ | |
|
||||
| Hook plugins | ✅ | ✅ | Declarative hooks from extension capabilities |
|
||||
| Provider plugins | ✅ | ❌ | |
|
||||
| Plugin CLI (`install`, `list`) | ✅ | ✅ | `tool` subcommand |
|
||||
| ClawHub registry | ✅ | ❌ | Discovery |
|
||||
@@ -421,10 +421,10 @@ This document tracks feature parity between IronClaw (Rust implementation) and O
|
||||
| `transcribeAudio` hook | ✅ | ❌ | P3 | |
|
||||
| `transformResponse` hook | ✅ | ✅ | P2 | |
|
||||
| `llm_input`/`llm_output` hooks | ✅ | ❌ | P3 | LLM payload inspection |
|
||||
| Bundled hooks | ✅ | ❌ | P2 | |
|
||||
| Plugin hooks | ✅ | ❌ | P3 | |
|
||||
| Workspace hooks | ✅ | ❌ | P2 | Inline code |
|
||||
| Outbound webhooks | ✅ | ❌ | P2 | |
|
||||
| Bundled hooks | ✅ | ✅ | P2 | Audit + declarative rule/webhook hooks |
|
||||
| Plugin hooks | ✅ | ✅ | P3 | Registered from WASM `capabilities.json` |
|
||||
| Workspace hooks | ✅ | ✅ | P2 | `hooks/hooks.json` and `hooks/*.hook.json` |
|
||||
| Outbound webhooks | ✅ | ✅ | P2 | Fire-and-forget lifecycle event delivery |
|
||||
| Heartbeat system | ✅ | ✅ | - | Periodic execution |
|
||||
| Gmail pub/sub | ✅ | ❌ | P3 | |
|
||||
|
||||
@@ -528,7 +528,7 @@ This document tracks feature parity between IronClaw (Rust implementation) and O
|
||||
- ✅ Telegram channel (WASM, DM pairing, caption, /start)
|
||||
- ❌ WhatsApp channel
|
||||
- ✅ Multi-provider failover (`FailoverProvider` with retryable error classification)
|
||||
- ✅ Hooks system (beforeInbound, beforeToolCall, beforeOutbound, onSessionStart, onSessionEnd, transformResponse)
|
||||
- ✅ Hooks system (core lifecycle hooks + bundled/plugin/workspace hooks + outbound webhooks)
|
||||
|
||||
### P2 - Medium Priority
|
||||
- ❌ Media handling (images, PDFs)
|
||||
|
||||
+7
-2
@@ -473,6 +473,7 @@ impl AppBuilder {
|
||||
pub async fn init_extensions(
|
||||
&self,
|
||||
tools: &Arc<ToolRegistry>,
|
||||
hooks: &Arc<HookRegistry>,
|
||||
) -> Result<
|
||||
(
|
||||
Arc<McpSessionManager>,
|
||||
@@ -661,6 +662,7 @@ impl AppBuilder {
|
||||
Arc::clone(&mcp_session_manager),
|
||||
Arc::clone(secrets),
|
||||
Arc::clone(tools),
|
||||
Some(Arc::clone(hooks)),
|
||||
wasm_tool_runtime.clone(),
|
||||
self.config.wasm.tools_dir.clone(),
|
||||
self.config.channels.wasm_channels_dir.clone(),
|
||||
@@ -697,8 +699,12 @@ impl AppBuilder {
|
||||
|
||||
let (llm, cheap_llm) = self.init_llm()?;
|
||||
let (safety, tools, embeddings, workspace) = self.init_tools(&llm).await?;
|
||||
|
||||
// Create hook registry early so runtime extension activation can register hooks.
|
||||
let hooks = Arc::new(HookRegistry::new());
|
||||
|
||||
let (mcp_session_manager, wasm_tool_runtime, extension_manager) =
|
||||
self.init_extensions(&tools).await?;
|
||||
self.init_extensions(&tools, &hooks).await?;
|
||||
|
||||
// Seed workspace and backfill embeddings
|
||||
if let Some(ref ws) = workspace {
|
||||
@@ -741,7 +747,6 @@ impl AppBuilder {
|
||||
};
|
||||
|
||||
let context_manager = Arc::new(ContextManager::new(self.config.agent.max_parallel_jobs));
|
||||
let hooks = Arc::new(HookRegistry::new());
|
||||
let cost_guard = Arc::new(crate::agent::cost_guard::CostGuard::new(
|
||||
crate::agent::cost_guard::CostGuardConfig {
|
||||
max_cost_per_day_cents: self.config.agent.max_cost_per_day_cents,
|
||||
|
||||
@@ -16,6 +16,7 @@ use crate::extensions::{
|
||||
ActivateResult, AuthResult, ExtensionError, ExtensionKind, ExtensionSource, InstallResult,
|
||||
InstalledExtension, RegistryEntry, ResultSource, SearchResult,
|
||||
};
|
||||
use crate::hooks::HookRegistry;
|
||||
use crate::secrets::{CreateSecretParams, SecretsStore};
|
||||
use crate::tools::ToolRegistry;
|
||||
use crate::tools::mcp::McpClient;
|
||||
@@ -52,6 +53,7 @@ pub struct ExtensionManager {
|
||||
// Shared
|
||||
secrets: Arc<dyn SecretsStore + Send + Sync>,
|
||||
tool_registry: Arc<ToolRegistry>,
|
||||
hooks: Option<Arc<HookRegistry>>,
|
||||
pending_auth: RwLock<HashMap<String, PendingAuth>>,
|
||||
/// Tunnel URL for remote OAuth callbacks (used in future iterations).
|
||||
_tunnel_url: Option<String>,
|
||||
@@ -66,6 +68,7 @@ impl ExtensionManager {
|
||||
mcp_session_manager: Arc<McpSessionManager>,
|
||||
secrets: Arc<dyn SecretsStore + Send + Sync>,
|
||||
tool_registry: Arc<ToolRegistry>,
|
||||
hooks: Option<Arc<HookRegistry>>,
|
||||
wasm_tool_runtime: Option<Arc<WasmToolRuntime>>,
|
||||
wasm_tools_dir: PathBuf,
|
||||
wasm_channels_dir: PathBuf,
|
||||
@@ -83,6 +86,7 @@ impl ExtensionManager {
|
||||
wasm_channels_dir,
|
||||
secrets,
|
||||
tool_registry,
|
||||
hooks,
|
||||
pending_auth: RwLock::new(HashMap::new()),
|
||||
_tunnel_url: tunnel_url,
|
||||
user_id,
|
||||
@@ -320,6 +324,21 @@ impl ExtensionManager {
|
||||
// Unregister from tool registry
|
||||
self.tool_registry.unregister(name).await;
|
||||
|
||||
// Unregister hooks registered from this plugin source.
|
||||
let removed_hooks = self
|
||||
.unregister_hook_prefix(&format!("plugin.tool:{}::", name))
|
||||
.await
|
||||
+ self
|
||||
.unregister_hook_prefix(&format!("plugin.dev_tool:{}::", name))
|
||||
.await;
|
||||
if removed_hooks > 0 {
|
||||
tracing::info!(
|
||||
extension = name,
|
||||
removed_hooks = removed_hooks,
|
||||
"Removed plugin hooks for WASM tool"
|
||||
);
|
||||
}
|
||||
|
||||
// Delete files
|
||||
let wasm_path = self.wasm_tools_dir.join(format!("{}.wasm", name));
|
||||
let cap_path = self
|
||||
@@ -969,6 +988,34 @@ impl ExtensionManager {
|
||||
.await
|
||||
.map_err(|e| ExtensionError::ActivationFailed(e.to_string()))?;
|
||||
|
||||
if let Some(ref hooks) = self.hooks
|
||||
&& let Some(cap_path) = cap_path_option
|
||||
{
|
||||
let source = format!("plugin.tool:{}", name);
|
||||
let registration =
|
||||
crate::hooks::bootstrap::register_plugin_bundle_from_capabilities_file(
|
||||
hooks, &source, cap_path,
|
||||
)
|
||||
.await;
|
||||
|
||||
if registration.total_registered() > 0 {
|
||||
tracing::info!(
|
||||
extension = name,
|
||||
hooks = registration.hooks,
|
||||
outbound_webhooks = registration.outbound_webhooks,
|
||||
"Registered plugin hooks for activated WASM tool"
|
||||
);
|
||||
}
|
||||
|
||||
if registration.errors > 0 {
|
||||
tracing::warn!(
|
||||
extension = name,
|
||||
errors = registration.errors,
|
||||
"Some plugin hooks failed to register"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!("Activated WASM tool '{}'", name);
|
||||
|
||||
Ok(ActivateResult {
|
||||
@@ -1008,6 +1055,21 @@ impl ExtensionManager {
|
||||
let mut pending = self.pending_auth.write().await;
|
||||
pending.retain(|_, auth| auth.created_at.elapsed() < std::time::Duration::from_secs(300));
|
||||
}
|
||||
|
||||
async fn unregister_hook_prefix(&self, prefix: &str) -> usize {
|
||||
let Some(ref hooks) = self.hooks else {
|
||||
return 0;
|
||||
};
|
||||
|
||||
let names = hooks.list().await;
|
||||
let mut removed = 0;
|
||||
for hook_name in names {
|
||||
if hook_name.starts_with(prefix) && hooks.unregister(&hook_name).await {
|
||||
removed += 1;
|
||||
}
|
||||
}
|
||||
removed
|
||||
}
|
||||
}
|
||||
|
||||
/// Infer the extension kind from a URL.
|
||||
|
||||
@@ -0,0 +1,378 @@
|
||||
//! Hook bootstrap helpers for loading bundled, plugin, and workspace hooks.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::channels::wasm::discover_channels;
|
||||
use crate::hooks::bundled::{
|
||||
HookBundleConfig, HookRegistrationSummary, register_bundle, register_bundled_hooks,
|
||||
};
|
||||
use crate::hooks::registry::HookRegistry;
|
||||
use crate::tools::wasm::{discover_dev_tools, discover_tools};
|
||||
use crate::workspace::Workspace;
|
||||
|
||||
/// Summary of hook bootstrap work done at startup.
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub struct HookBootstrapSummary {
|
||||
/// Number of bundled built-in hooks registered.
|
||||
pub bundled_hooks: usize,
|
||||
/// Number of plugin-provided rule hooks registered.
|
||||
pub plugin_hooks: usize,
|
||||
/// Number of workspace-provided rule hooks registered.
|
||||
pub workspace_hooks: usize,
|
||||
/// Number of outbound webhook hooks registered.
|
||||
pub outbound_webhooks: usize,
|
||||
/// Number of invalid hook configs skipped.
|
||||
pub errors: usize,
|
||||
}
|
||||
|
||||
impl HookBootstrapSummary {
|
||||
/// Total number of hooks registered across all categories.
|
||||
pub fn total_hooks(&self) -> usize {
|
||||
self.bundled_hooks + self.plugin_hooks + self.workspace_hooks + self.outbound_webhooks
|
||||
}
|
||||
}
|
||||
|
||||
/// Register bundled hooks, then load plugin and workspace hook bundles.
|
||||
pub async fn bootstrap_hooks(
|
||||
registry: &Arc<HookRegistry>,
|
||||
workspace: Option<&Arc<Workspace>>,
|
||||
wasm_tools_dir: &Path,
|
||||
wasm_channels_dir: &Path,
|
||||
active_tool_names: &[String],
|
||||
active_channel_names: &[String],
|
||||
dev_loaded_tool_names: &[String],
|
||||
) -> HookBootstrapSummary {
|
||||
let mut summary = HookBootstrapSummary::default();
|
||||
|
||||
let bundled = register_bundled_hooks(registry).await;
|
||||
summary.bundled_hooks += bundled.hooks;
|
||||
summary.outbound_webhooks += bundled.outbound_webhooks;
|
||||
summary.errors += bundled.errors;
|
||||
|
||||
let plugin = register_plugin_bundles(
|
||||
registry,
|
||||
wasm_tools_dir,
|
||||
wasm_channels_dir,
|
||||
active_tool_names,
|
||||
active_channel_names,
|
||||
dev_loaded_tool_names,
|
||||
)
|
||||
.await;
|
||||
summary.plugin_hooks += plugin.hooks;
|
||||
summary.outbound_webhooks += plugin.outbound_webhooks;
|
||||
summary.errors += plugin.errors;
|
||||
|
||||
if let Some(workspace) = workspace {
|
||||
let workspace_loaded = register_workspace_bundles(registry, workspace).await;
|
||||
summary.workspace_hooks += workspace_loaded.hooks;
|
||||
summary.outbound_webhooks += workspace_loaded.outbound_webhooks;
|
||||
summary.errors += workspace_loaded.errors;
|
||||
}
|
||||
|
||||
summary
|
||||
}
|
||||
|
||||
async fn register_plugin_bundles(
|
||||
registry: &Arc<HookRegistry>,
|
||||
wasm_tools_dir: &Path,
|
||||
wasm_channels_dir: &Path,
|
||||
active_tool_names: &[String],
|
||||
active_channel_names: &[String],
|
||||
dev_loaded_tool_names: &[String],
|
||||
) -> HookRegistrationSummary {
|
||||
let mut summary = HookRegistrationSummary::default();
|
||||
let files = collect_plugin_capability_files(
|
||||
wasm_tools_dir,
|
||||
wasm_channels_dir,
|
||||
active_tool_names,
|
||||
active_channel_names,
|
||||
dev_loaded_tool_names,
|
||||
)
|
||||
.await;
|
||||
|
||||
for (source, path) in files {
|
||||
let registered =
|
||||
register_plugin_bundle_from_capabilities_file(registry, &source, &path).await;
|
||||
summary.merge(registered);
|
||||
}
|
||||
|
||||
summary
|
||||
}
|
||||
|
||||
/// Register a plugin hook bundle from a single capabilities file.
|
||||
///
|
||||
/// This is used by startup bootstrap and by runtime extension activation.
|
||||
pub async fn register_plugin_bundle_from_capabilities_file(
|
||||
registry: &Arc<HookRegistry>,
|
||||
source: &str,
|
||||
path: &Path,
|
||||
) -> HookRegistrationSummary {
|
||||
match load_plugin_bundle_from_capabilities_file(path).await {
|
||||
Ok(Some(bundle)) => register_bundle(registry, source, bundle).await,
|
||||
Ok(None) => HookRegistrationSummary::default(),
|
||||
Err(err) => {
|
||||
tracing::warn!(
|
||||
source = source,
|
||||
path = %path.display(),
|
||||
error = %err,
|
||||
"Skipping plugin hook bundle"
|
||||
);
|
||||
HookRegistrationSummary {
|
||||
hooks: 0,
|
||||
outbound_webhooks: 0,
|
||||
errors: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn collect_plugin_capability_files(
|
||||
wasm_tools_dir: &Path,
|
||||
wasm_channels_dir: &Path,
|
||||
active_tool_names: &[String],
|
||||
active_channel_names: &[String],
|
||||
dev_loaded_tool_names: &[String],
|
||||
) -> Vec<(String, PathBuf)> {
|
||||
let mut files: Vec<(String, PathBuf)> = Vec::new();
|
||||
let mut seen: HashSet<String> = HashSet::new();
|
||||
let active_tools: HashSet<&str> = active_tool_names.iter().map(String::as_str).collect();
|
||||
let active_channels: HashSet<&str> = active_channel_names.iter().map(String::as_str).collect();
|
||||
let dev_loaded_tools: HashSet<&str> =
|
||||
dev_loaded_tool_names.iter().map(String::as_str).collect();
|
||||
|
||||
if wasm_tools_dir.exists() {
|
||||
match discover_tools(wasm_tools_dir).await {
|
||||
Ok(tools) => {
|
||||
for (name, tool) in tools {
|
||||
if let Some(path) = tool.capabilities_path
|
||||
&& active_tools.contains(name.as_str())
|
||||
&& !dev_loaded_tools.contains(name.as_str())
|
||||
{
|
||||
insert_unique(&mut files, &mut seen, format!("plugin.tool:{}", name), path);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::warn!(
|
||||
path = %wasm_tools_dir.display(),
|
||||
error = %err,
|
||||
"Failed to discover WASM tool capabilities for plugin hooks"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match discover_dev_tools().await {
|
||||
Ok(dev_tools) => {
|
||||
for (name, tool) in dev_tools {
|
||||
if let Some(path) = tool.capabilities_path
|
||||
&& active_tools.contains(name.as_str())
|
||||
&& dev_loaded_tools.contains(name.as_str())
|
||||
{
|
||||
insert_unique(
|
||||
&mut files,
|
||||
&mut seen,
|
||||
format!("plugin.dev_tool:{}", name),
|
||||
path,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::debug!(error = %err, "No dev tool capabilities discovered for plugin hooks");
|
||||
}
|
||||
}
|
||||
|
||||
if wasm_channels_dir.exists() {
|
||||
match discover_channels(wasm_channels_dir).await {
|
||||
Ok(channels) => {
|
||||
for (name, channel) in channels {
|
||||
if let Some(path) = channel.capabilities_path
|
||||
&& active_channels.contains(name.as_str())
|
||||
{
|
||||
insert_unique(
|
||||
&mut files,
|
||||
&mut seen,
|
||||
format!("plugin.channel:{}", name),
|
||||
path,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::warn!(
|
||||
path = %wasm_channels_dir.display(),
|
||||
error = %err,
|
||||
"Failed to discover WASM channel capabilities for plugin hooks"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
files.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
files
|
||||
}
|
||||
|
||||
fn insert_unique(
|
||||
files: &mut Vec<(String, PathBuf)>,
|
||||
seen: &mut HashSet<String>,
|
||||
source: String,
|
||||
path: PathBuf,
|
||||
) {
|
||||
let key = path.to_string_lossy().to_string();
|
||||
if seen.insert(key) {
|
||||
files.push((source, path));
|
||||
}
|
||||
}
|
||||
|
||||
async fn load_plugin_bundle_from_capabilities_file(
|
||||
path: &Path,
|
||||
) -> Result<Option<HookBundleConfig>, String> {
|
||||
let bytes = tokio::fs::read(path)
|
||||
.await
|
||||
.map_err(|e| format!("read failed: {e}"))?;
|
||||
|
||||
let value: serde_json::Value =
|
||||
serde_json::from_slice(&bytes).map_err(|e| format!("invalid JSON: {e}"))?;
|
||||
|
||||
let Some(hooks_value) = extract_hooks_section(&value) else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
HookBundleConfig::from_value(hooks_value)
|
||||
.map(Some)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn extract_hooks_section(root: &serde_json::Value) -> Option<&serde_json::Value> {
|
||||
root.get("hooks")
|
||||
.or_else(|| root.get("capabilities").and_then(|c| c.get("hooks")))
|
||||
}
|
||||
|
||||
async fn register_workspace_bundles(
|
||||
registry: &Arc<HookRegistry>,
|
||||
workspace: &Arc<Workspace>,
|
||||
) -> HookRegistrationSummary {
|
||||
let mut summary = HookRegistrationSummary::default();
|
||||
|
||||
let paths = match workspace.list_all().await {
|
||||
Ok(paths) => paths,
|
||||
Err(err) => {
|
||||
summary.errors += 1;
|
||||
tracing::warn!(error = %err, "Failed to list workspace paths for hooks");
|
||||
return summary;
|
||||
}
|
||||
};
|
||||
|
||||
let mut hook_paths: Vec<String> = paths
|
||||
.into_iter()
|
||||
.filter(|path| is_workspace_hook_file(path))
|
||||
.collect();
|
||||
hook_paths.sort();
|
||||
|
||||
for path in hook_paths {
|
||||
let doc = match workspace.read(&path).await {
|
||||
Ok(doc) => doc,
|
||||
Err(err) => {
|
||||
summary.errors += 1;
|
||||
tracing::warn!(path = %path, error = %err, "Skipping unreadable workspace hook file");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let parsed: serde_json::Value = match serde_json::from_str(&doc.content) {
|
||||
Ok(value) => value,
|
||||
Err(err) => {
|
||||
summary.errors += 1;
|
||||
tracing::warn!(path = %path, error = %err, "Workspace hook file is not valid JSON");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let bundle = match parse_workspace_bundle(&parsed) {
|
||||
Ok(bundle) => bundle,
|
||||
Err(err) => {
|
||||
summary.errors += 1;
|
||||
tracing::warn!(path = %path, error = %err, "Skipping invalid workspace hook bundle");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let source = format!("workspace:{}", path);
|
||||
let registered = register_bundle(registry, &source, bundle).await;
|
||||
summary.merge(registered);
|
||||
}
|
||||
|
||||
summary
|
||||
}
|
||||
|
||||
fn parse_workspace_bundle(value: &serde_json::Value) -> Result<HookBundleConfig, String> {
|
||||
if let Some(nested) = value.get("hooks") {
|
||||
HookBundleConfig::from_value(nested).map_err(|e| e.to_string())
|
||||
} else {
|
||||
HookBundleConfig::from_value(value).map_err(|e| e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn is_workspace_hook_file(path: &str) -> bool {
|
||||
path == "hooks/hooks.json" || (path.starts_with("hooks/") && path.ends_with(".hook.json"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_extract_hooks_section_from_tool_caps() {
|
||||
let value = serde_json::json!({
|
||||
"http": {"allowlist": []},
|
||||
"hooks": {"rules": []}
|
||||
});
|
||||
|
||||
let extracted = extract_hooks_section(&value).unwrap();
|
||||
assert!(extracted.get("rules").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_hooks_section_from_channel_caps() {
|
||||
let value = serde_json::json!({
|
||||
"type": "channel",
|
||||
"capabilities": {
|
||||
"hooks": {
|
||||
"rules": []
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let extracted = extract_hooks_section(&value).unwrap();
|
||||
assert!(extracted.get("rules").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_workspace_hook_file_filter() {
|
||||
assert!(is_workspace_hook_file("hooks/hooks.json"));
|
||||
assert!(is_workspace_hook_file("hooks/redact.hook.json"));
|
||||
assert!(!is_workspace_hook_file("hooks/readme.md"));
|
||||
assert!(!is_workspace_hook_file("MEMORY.md"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_workspace_bundle_wrapped_hooks() {
|
||||
let value = serde_json::json!({
|
||||
"hooks": {
|
||||
"rules": [
|
||||
{
|
||||
"name": "append-bang",
|
||||
"points": ["beforeInbound"],
|
||||
"append": "!"
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
|
||||
let bundle = parse_workspace_bundle(&value).unwrap();
|
||||
assert_eq!(bundle.rules.len(), 1);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+20
-3
@@ -3,9 +3,11 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Points in the agent lifecycle where hooks can be attached.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum HookPoint {
|
||||
/// Before processing an inbound user message.
|
||||
BeforeInbound,
|
||||
@@ -21,8 +23,22 @@ pub enum HookPoint {
|
||||
TransformResponse,
|
||||
}
|
||||
|
||||
impl HookPoint {
|
||||
/// Human-readable hook point identifier.
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
HookPoint::BeforeInbound => "beforeInbound",
|
||||
HookPoint::BeforeToolCall => "beforeToolCall",
|
||||
HookPoint::BeforeOutbound => "beforeOutbound",
|
||||
HookPoint::OnSessionStart => "onSessionStart",
|
||||
HookPoint::OnSessionEnd => "onSessionEnd",
|
||||
HookPoint::TransformResponse => "transformResponse",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Contextual data carried with each hook invocation.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum HookEvent {
|
||||
/// An inbound user message about to be processed.
|
||||
Inbound {
|
||||
@@ -133,7 +149,8 @@ impl HookOutcome {
|
||||
}
|
||||
|
||||
/// How to handle hook execution failures.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum HookFailureMode {
|
||||
/// On error/timeout, continue processing as if the hook returned `ok()`.
|
||||
FailOpen,
|
||||
|
||||
@@ -12,8 +12,14 @@
|
||||
//! Hooks are executed in priority order (lower number = higher priority).
|
||||
//! Each hook can pass through, modify content, or reject the event.
|
||||
|
||||
pub mod bootstrap;
|
||||
pub mod bundled;
|
||||
pub mod hook;
|
||||
pub mod registry;
|
||||
|
||||
pub use bootstrap::{HookBootstrapSummary, bootstrap_hooks};
|
||||
pub use bundled::{
|
||||
HookBundleConfig, HookRegistrationSummary, register_bundle, register_bundled_hooks,
|
||||
};
|
||||
pub use hook::{Hook, HookContext, HookError, HookEvent, HookFailureMode, HookOutcome, HookPoint};
|
||||
pub use registry::HookRegistry;
|
||||
|
||||
+54
-1
@@ -39,7 +39,22 @@ impl HookRegistry {
|
||||
/// Lower priority number = runs first.
|
||||
pub async fn register_with_priority(&self, hook: Arc<dyn Hook>, priority: u32) {
|
||||
let mut hooks = self.hooks.write().await;
|
||||
hooks.push(HookEntry { hook, priority });
|
||||
let hook_name = hook.name().to_string();
|
||||
|
||||
if let Some(existing) = hooks
|
||||
.iter_mut()
|
||||
.find(|entry| entry.hook.name() == hook_name)
|
||||
{
|
||||
tracing::warn!(
|
||||
hook = %hook_name,
|
||||
"Replacing existing hook registration with same name"
|
||||
);
|
||||
existing.hook = hook;
|
||||
existing.priority = priority;
|
||||
} else {
|
||||
hooks.push(HookEntry { hook, priority });
|
||||
}
|
||||
|
||||
hooks.sort_by_key(|e| e.priority);
|
||||
}
|
||||
|
||||
@@ -346,6 +361,44 @@ mod tests {
|
||||
assert_eq!(names, vec!["hook-a", "hook-b"]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_register_duplicate_name_replaces_existing() {
|
||||
let registry = HookRegistry::new();
|
||||
|
||||
registry
|
||||
.register_with_priority(
|
||||
Arc::new(ModifyHook {
|
||||
name: "dup".into(),
|
||||
suffix: "-A".into(),
|
||||
points: vec![HookPoint::BeforeInbound],
|
||||
}),
|
||||
100,
|
||||
)
|
||||
.await;
|
||||
|
||||
registry
|
||||
.register_with_priority(
|
||||
Arc::new(ModifyHook {
|
||||
name: "dup".into(),
|
||||
suffix: "-B".into(),
|
||||
points: vec![HookPoint::BeforeInbound],
|
||||
}),
|
||||
10,
|
||||
)
|
||||
.await;
|
||||
|
||||
let names = registry.list().await;
|
||||
assert_eq!(names, vec!["dup"]);
|
||||
|
||||
let result = registry.run(&test_event()).await.unwrap();
|
||||
match result {
|
||||
HookOutcome::Continue {
|
||||
modified: Some(value),
|
||||
} => assert_eq!(value, "hello-B"),
|
||||
other => panic!("expected modified output, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_priority_ordering() {
|
||||
let registry = HookRegistry::new();
|
||||
|
||||
+34
-4
@@ -23,7 +23,7 @@ use ironclaw::{
|
||||
config::Config,
|
||||
context::ContextManager,
|
||||
extensions::ExtensionManager,
|
||||
hooks::HookRegistry,
|
||||
hooks::{HookRegistry, bootstrap_hooks},
|
||||
llm::{
|
||||
CachedProvider, CircuitBreakerConfig, CircuitBreakerProvider, CooldownConfig,
|
||||
FailoverProvider, LlmProvider, ResponseCacheConfig, SessionConfig,
|
||||
@@ -746,6 +746,9 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
let mcp_session_manager = Arc::new(McpSessionManager::new());
|
||||
|
||||
// Create hook registry early so runtime extension activation can register hooks.
|
||||
let hooks = Arc::new(HookRegistry::new());
|
||||
|
||||
// Create WASM tool runtime (sync, just builds the wasmtime engine)
|
||||
let wasm_tool_runtime: Option<Arc<WasmToolRuntime>> =
|
||||
if config.wasm.enabled && config.wasm.tools_dir.exists() {
|
||||
@@ -763,6 +766,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
// Load WASM tools and MCP servers concurrently.
|
||||
// Both register into the shared ToolRegistry (RwLock-based) so concurrent writes are safe.
|
||||
let wasm_tools_future = async {
|
||||
let mut dev_loaded_tool_names: Vec<String> = Vec::new();
|
||||
|
||||
if let Some(ref runtime) = wasm_tool_runtime {
|
||||
let mut loader = WasmToolLoader::new(Arc::clone(runtime), Arc::clone(&tools));
|
||||
if let Some(ref secrets) = secrets_store {
|
||||
@@ -791,6 +796,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
// Load dev tools from build artifacts (overrides installed if newer)
|
||||
match load_dev_tools(&loader, &config.wasm.tools_dir).await {
|
||||
Ok(results) => {
|
||||
dev_loaded_tool_names.extend(results.loaded.iter().cloned());
|
||||
if !results.loaded.is_empty() {
|
||||
tracing::info!(
|
||||
"Loaded {} dev WASM tools from build artifacts",
|
||||
@@ -803,6 +809,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dev_loaded_tool_names
|
||||
};
|
||||
|
||||
let mcp_servers_future = async {
|
||||
@@ -908,7 +916,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
};
|
||||
|
||||
tokio::join!(wasm_tools_future, mcp_servers_future);
|
||||
let (dev_loaded_tool_names, _) = tokio::join!(wasm_tools_future, mcp_servers_future);
|
||||
|
||||
// Create extension manager for in-chat discovery/install/auth/activate
|
||||
let extension_manager = if let Some(ref secrets) = secrets_store {
|
||||
@@ -916,6 +924,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
Arc::clone(&mcp_session_manager),
|
||||
Arc::clone(secrets),
|
||||
Arc::clone(&tools),
|
||||
Some(Arc::clone(&hooks)),
|
||||
wasm_tool_runtime.clone(),
|
||||
config.wasm.tools_dir.clone(),
|
||||
config.channels.wasm_channels_dir.clone(),
|
||||
@@ -1013,6 +1022,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
// Initialize channel manager
|
||||
let mut channels = ChannelManager::new();
|
||||
let mut channel_names: Vec<String> = Vec::new();
|
||||
let mut loaded_wasm_channel_names: Vec<String> = Vec::new();
|
||||
|
||||
if let Some(repl) = repl_channel {
|
||||
channels.add(Box::new(repl));
|
||||
@@ -1045,6 +1055,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
|
||||
for loaded in results.loaded {
|
||||
let channel_name = loaded.name().to_string();
|
||||
loaded_wasm_channel_names.push(channel_name.clone());
|
||||
tracing::info!("Loaded WASM channel: {}", channel_name);
|
||||
|
||||
let secret_name = loaded.webhook_secret_name();
|
||||
@@ -1270,8 +1281,27 @@ async fn main() -> anyhow::Result<()> {
|
||||
// Create context manager (shared between job tools and agent)
|
||||
let context_manager = Arc::new(ContextManager::new(config.agent.max_parallel_jobs));
|
||||
|
||||
// Create hook registry
|
||||
let hooks = Arc::new(HookRegistry::new());
|
||||
// Register bundled/plugin/workspace hooks.
|
||||
let active_tool_names = tools.list().await;
|
||||
|
||||
let hook_bootstrap = bootstrap_hooks(
|
||||
&hooks,
|
||||
workspace.as_ref(),
|
||||
&config.wasm.tools_dir,
|
||||
&config.channels.wasm_channels_dir,
|
||||
&active_tool_names,
|
||||
&loaded_wasm_channel_names,
|
||||
&dev_loaded_tool_names,
|
||||
)
|
||||
.await;
|
||||
tracing::info!(
|
||||
bundled = hook_bootstrap.bundled_hooks,
|
||||
plugin = hook_bootstrap.plugin_hooks,
|
||||
workspace = hook_bootstrap.workspace_hooks,
|
||||
outbound_webhooks = hook_bootstrap.outbound_webhooks,
|
||||
errors = hook_bootstrap.errors,
|
||||
"Lifecycle hooks initialized"
|
||||
);
|
||||
|
||||
// Create session manager (shared between agent and web gateway)
|
||||
let session_manager = Arc::new(SessionManager::new().with_hooks(hooks.clone()));
|
||||
|
||||
@@ -570,6 +570,7 @@ mod tests {
|
||||
Arc::new(InMemorySecretsStore::new(crypto)),
|
||||
Arc::new(ToolRegistry::new()),
|
||||
None,
|
||||
None,
|
||||
std::path::PathBuf::from("/tmp/ironclaw-test-tools"),
|
||||
std::path::PathBuf::from("/tmp/ironclaw-test-channels"),
|
||||
None,
|
||||
|
||||
Reference in New Issue
Block a user