Emit structured tracing fields and synchronously commit the captured event through the caller's service-owned sink. Logging failure preserves dispatch. # Examples ``` use atomic_agent::hook_log::{record_result, HookContext}; let context = HookContext { session_id: Some("child-session".into()), parent_session_id: Some("parent-session".into()), }; let result = record_result(&context, "opencode", "
(
context: &HookContext,
agent: &str,
display_name: &str,
verb: &str,
result: Result<T, E>,
sink: impl FnOnce(&SessionLogEvent) -> Result<(), String>,
)
| 132 | /// assert_eq!(result.unwrap(), 42); |
| 133 | /// ``` |
| 134 | pub fn record_result<T, E: std::fmt::Display>( |
| 135 | context: &HookContext, |
| 136 | agent: &str, |
| 137 | display_name: &str, |
| 138 | verb: &str, |
| 139 | result: Result<T, E>, |
| 140 | sink: impl FnOnce(&SessionLogEvent) -> Result<(), String>, |
| 141 | ) -> Result<T, E> { |
| 142 | let captured = Arc::new(Mutex::new(None)); |
| 143 | let subscriber = tracing_subscriber::registry().with(HookLogLayer(captured.clone())); |
| 144 | let event_id = uuid::Uuid::new_v4().to_string(); |
| 145 | let at = chrono::Utc::now().to_rfc3339(); |
| 146 | let detail = result |
| 147 | .as_ref() |
| 148 | .err() |
| 149 | .map(ToString::to_string) |
| 150 | .unwrap_or_default(); |
| 151 | let outcome = if result.is_ok() { "ok" } else { "error" }; |
| 152 | tracing::subscriber::with_default(subscriber, || { |
| 153 | tracing::info!(target: "atomic::hook", |
| 154 | event_id = event_id.as_str(), at = at.as_str(), agent, display_name, verb, outcome, |
| 155 | session_id = context.session_id.as_deref().unwrap_or(""), |
| 156 | parent_session_id = context.parent_session_id.as_deref().unwrap_or(""), |
| 157 | detail = detail.as_str(), "hook dispatch completed"); |
| 158 | }); |
| 159 | let event = captured.lock().unwrap().take(); |
| 160 | if let Some(event) = event { |
| 161 | if let Err(error) = sink(&event) { |
| 162 | log::warn!("failed to persist hook log: {error}"); |
| 163 | } |
| 164 | } |
| 165 | result |
| 166 | } |
| 167 | |
| 168 | /// Durable transport-outage spool. Each event has its own immutable file, so |
| 169 | /// independent processes never share a read-modify-write critical section. |