everruns-core 0.9.0

Core agent abstractions for Everruns - agent loop, events, tools, LLM providers
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// User-defined lifecycle hooks: session and turn events.
//
// Two trait families bridge the four non-tool `HookEvent`s to runtime firing
// points (see `specs/user-hooks.md`):
//
// - `SessionLifecycleHook` — `session_start` / `session_end`. Advisory only;
//   a hook failure is logged and never aborts the session.
// - `TurnLifecycleHook` — `user_prompt_submit` / `turn_end`. `turn_end` is
//   advisory; `user_prompt_submit` can *block* (reject the inbound message and
//   abort the turn) and *mutate* (rewrite the user message text).
//
// As with the tool hooks, capability authors hand the runtime *data*
// (`UserHookSpec`); this module owns the spec -> `Arc<dyn …Hook>` translation
// so the central timeout/output/sandbox limits stay enforced. The adapters
// reuse the same `BashHookExecutor` + `BashHookDispatcher` as the pre/post
// tool adapters, so payload delivery, output parsing, and `on_error` policy
// are identical across every event.

use std::sync::Arc;

use async_trait::async_trait;
use serde_json::json;

use crate::hook_executor::{BashHookExecutor, ExecutorOpts, HookExecutor, HookPayload};
use crate::typed_id::{OrgId, SessionId, TurnId};
use crate::user_hook_types::{ExecutorSpec, HookEvent, HookId, HookOutcome, OnError, UserHookSpec};

// ============================================================================
// Context structs
// ============================================================================

/// Context available when a session-lifecycle hook fires. Lighter than
/// `ToolContext`: at session create/close there is no tool, no turn, and no
/// per-tool stores — only the session identity and (optionally) the agent.
#[derive(Debug, Clone)]
pub struct SessionHookContext {
    pub session_id: SessionId,
    pub org_id: Option<OrgId>,
    pub agent_id: Option<String>,
}

/// Context available when a turn-lifecycle hook fires.
#[derive(Debug, Clone)]
pub struct TurnHookContext {
    pub session_id: SessionId,
    pub turn_id: Option<TurnId>,
    pub org_id: Option<OrgId>,
    pub agent_id: Option<String>,
}

// ============================================================================
// Decisions
// ============================================================================

/// Outcome of a `user_prompt_submit` chain. Mirrors `PreToolUseDecision` but
/// over the user message text rather than a `ToolCall`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UserPromptDecision {
    /// Continue the turn with the (possibly rewritten) user message text.
    Continue { message: String },
    /// Reject the inbound message and abort the turn. `reason` is logged /
    /// audited; `user_message` (if any) is surfaced to the caller.
    Block {
        reason: String,
        user_message: Option<String>,
    },
}

// ============================================================================
// SessionLifecycleHook
// ============================================================================

/// Hook that fires on `session_start` or `session_end`. Advisory only — the
/// runtime calls `fire` and logs any failure; it never blocks the session.
#[async_trait]
pub trait SessionLifecycleHook: Send + Sync {
    /// Which event this hook is bound to (`SessionStart` or `SessionEnd`).
    fn event(&self) -> HookEvent;
    /// Stable id for logs.
    fn hook_id(&self) -> &HookId;
    /// Run the hook. The outcome is advisory; `Block`/`Mutate` are ignored
    /// (logged) since these events cannot block or mutate anything.
    async fn fire(&self, ctx: &SessionHookContext, data: serde_json::Value);
}

// ============================================================================
// TurnLifecycleHook
// ============================================================================

/// Hook that fires on `user_prompt_submit` (blockable/mutable) or `turn_end`
/// (advisory).
#[async_trait]
pub trait TurnLifecycleHook: Send + Sync {
    fn event(&self) -> HookEvent;
    fn hook_id(&self) -> &HookId;
    /// The spec's `on_error` policy, used by the `user_prompt_submit` chain
    /// runner to decide block-vs-continue on executor failure.
    fn on_error(&self) -> OnError;
    /// Run the hook against a payload. Returns the raw `HookOutcome`; chain
    /// runners interpret it per-event (block/mutate for `user_prompt_submit`,
    /// advisory for `turn_end`).
    async fn run(&self, ctx: &TurnHookContext, data: serde_json::Value) -> HookOutcome;
}

// ============================================================================
// Bash-backed adapters
// ============================================================================

/// Single adapter that implements both lifecycle traits over a bash executor.
/// The `event` discriminates which trait method the runtime drives; the
/// executor/limits/`on_error` handling is shared.
struct BashLifecycleHook {
    spec: UserHookSpec,
    executor: Arc<dyn HookExecutor>,
    opts: ExecutorOpts,
    hook_id: HookId,
}

impl BashLifecycleHook {
    fn new(spec: UserHookSpec, executor: Arc<dyn HookExecutor>, index: usize) -> Self {
        let opts = ExecutorOpts {
            timeout_ms: spec.timeout_ms,
            max_output_bytes: 64 * 1024,
        };
        let hook_id = spec.resolve_id(index);
        Self {
            spec,
            executor,
            opts,
            hook_id,
        }
    }

    fn payload(&self, event: HookEvent, data: serde_json::Value) -> HookPayload {
        HookPayload {
            event,
            hook_id: self.hook_id.clone(),
            // session_id/org_id/turn_id are filled by the chain runner from
            // the live context before dispatch; the nil default is overwritten
            // there. (Lifecycle contexts vary by event, so the runner owns it.)
            session_id: SessionId::from_uuid(uuid::Uuid::nil()),
            turn_id: None,
            org_id: None,
            agent_id: None,
            ts: chrono::Utc::now().to_rfc3339(),
            data,
        }
    }
}

#[async_trait]
impl SessionLifecycleHook for BashLifecycleHook {
    fn event(&self) -> HookEvent {
        self.spec.event
    }
    fn hook_id(&self) -> &HookId {
        &self.hook_id
    }
    async fn fire(&self, ctx: &SessionHookContext, data: serde_json::Value) {
        let mut payload = self.payload(self.spec.event, data);
        payload.session_id = ctx.session_id;
        payload.org_id = ctx.org_id;
        payload.agent_id = ctx.agent_id.clone();
        let outcome = self.executor.run(payload, &self.opts).await;
        match outcome {
            HookOutcome::Allow => {}
            HookOutcome::Mutate { .. } | HookOutcome::Block { .. } => {
                tracing::warn!(
                    hook_id = %self.hook_id.as_str(),
                    event = %self.spec.event.as_str(),
                    "lifecycle hook returned block/mutate on an advisory event; ignoring"
                );
            }
            HookOutcome::Error { message } => {
                // Advisory: log per on_error but never abort the session.
                tracing::warn!(
                    hook_id = %self.hook_id.as_str(),
                    event = %self.spec.event.as_str(),
                    on_error = ?self.spec.on_error,
                    message = %message,
                    "lifecycle hook errored (advisory event, continuing)"
                );
            }
        }
    }
}

#[async_trait]
impl TurnLifecycleHook for BashLifecycleHook {
    fn event(&self) -> HookEvent {
        self.spec.event
    }
    fn hook_id(&self) -> &HookId {
        &self.hook_id
    }
    fn on_error(&self) -> OnError {
        self.spec.on_error
    }
    async fn run(&self, ctx: &TurnHookContext, data: serde_json::Value) -> HookOutcome {
        let mut payload = self.payload(self.spec.event, data);
        payload.session_id = ctx.session_id;
        payload.turn_id = ctx.turn_id.map(|t| t.to_string());
        payload.org_id = ctx.org_id;
        payload.agent_id = ctx.agent_id.clone();
        self.executor.run(payload, &self.opts).await
    }
}

// ============================================================================
// Builders
// ============================================================================

fn build_bash_executor(
    spec: &UserHookSpec,
    dispatcher: Arc<dyn crate::hook_executor::BashHookDispatcher>,
) -> Arc<dyn HookExecutor> {
    match &spec.executor {
        ExecutorSpec::Bash { command, env } => Arc::new(BashHookExecutor::with_dispatcher(
            command.clone(),
            env.clone(),
            dispatcher,
        )),
    }
}

/// Build `SessionLifecycleHook` adapters for every spec whose event is
/// `event` (must be `SessionStart` or `SessionEnd`). Invalid specs are dropped
/// with a warning so one bad entry can't take down the chain.
pub fn build_session_lifecycle_hooks(
    specs: &[UserHookSpec],
    event: HookEvent,
    dispatcher: Arc<dyn crate::hook_executor::BashHookDispatcher>,
) -> Vec<Arc<dyn SessionLifecycleHook>> {
    debug_assert!(matches!(
        event,
        HookEvent::SessionStart | HookEvent::SessionEnd
    ));
    let mut out: Vec<Arc<dyn SessionLifecycleHook>> = Vec::new();
    for (index, spec) in specs.iter().enumerate() {
        if spec.event != event {
            continue;
        }
        if let Err(e) = spec.validate() {
            tracing::warn!(
                hook_id = %spec.resolve_id(index).as_str(),
                error = %e,
                "skipping invalid lifecycle hook spec"
            );
            continue;
        }
        let executor = build_bash_executor(spec, dispatcher.clone());
        out.push(Arc::new(BashLifecycleHook::new(
            spec.clone(),
            executor,
            index,
        )));
    }
    out
}

/// Build `TurnLifecycleHook` adapters for every spec whose event is `event`
/// (must be `UserPromptSubmit` or `TurnEnd`).
pub fn build_turn_lifecycle_hooks(
    specs: &[UserHookSpec],
    event: HookEvent,
    dispatcher: Arc<dyn crate::hook_executor::BashHookDispatcher>,
) -> Vec<Arc<dyn TurnLifecycleHook>> {
    debug_assert!(matches!(
        event,
        HookEvent::UserPromptSubmit | HookEvent::TurnEnd
    ));
    let mut out: Vec<Arc<dyn TurnLifecycleHook>> = Vec::new();
    for (index, spec) in specs.iter().enumerate() {
        if spec.event != event {
            continue;
        }
        if let Err(e) = spec.validate() {
            tracing::warn!(
                hook_id = %spec.resolve_id(index).as_str(),
                error = %e,
                "skipping invalid lifecycle hook spec"
            );
            continue;
        }
        let executor = build_bash_executor(spec, dispatcher.clone());
        out.push(Arc::new(BashLifecycleHook::new(
            spec.clone(),
            executor,
            index,
        )));
    }
    out
}

// ============================================================================
// Chain runners
// ============================================================================

/// Run every session-lifecycle hook in order. Advisory: failures are handled
/// inside each `fire` call; this never returns an error.
pub async fn run_session_lifecycle_hooks(
    hooks: &[Arc<dyn SessionLifecycleHook>],
    ctx: &SessionHookContext,
    data: serde_json::Value,
) {
    for hook in hooks {
        hook.fire(ctx, data.clone()).await;
    }
}

/// Run the `turn_end` chain. Advisory: each hook runs independently; block /
/// mutate outcomes are logged and ignored, errors are logged per `on_error`.
pub async fn run_turn_end_hooks(
    hooks: &[Arc<dyn TurnLifecycleHook>],
    ctx: &TurnHookContext,
    data: serde_json::Value,
) {
    for hook in hooks {
        match hook.run(ctx, data.clone()).await {
            HookOutcome::Allow => {}
            HookOutcome::Mutate { .. } | HookOutcome::Block { .. } => {
                tracing::warn!(
                    hook_id = %hook.hook_id().as_str(),
                    "turn_end hook returned block/mutate on an advisory event; ignoring"
                );
            }
            HookOutcome::Error { message } => {
                tracing::warn!(
                    hook_id = %hook.hook_id().as_str(),
                    message = %message,
                    "turn_end hook errored (advisory, continuing)"
                );
            }
        }
    }
}

/// Run the `user_prompt_submit` chain sequentially over the message text.
/// Each hook sees the previous hook's mutated text. The first `Block` wins and
/// aborts the chain; on executor error the spec's `on_error` policy applies
/// (`Block` -> block, `Warn`/`Allow` -> continue with current text).
pub async fn run_user_prompt_submit_hooks(
    hooks: &[Arc<dyn TurnLifecycleHook>],
    ctx: &TurnHookContext,
    mut message: String,
) -> UserPromptDecision {
    for hook in hooks {
        let data = json!({ "message": message });
        match hook.run(ctx, data).await {
            HookOutcome::Allow => {}
            HookOutcome::Mutate { patch, .. } => {
                if let Some(new_msg) = patch.get("message").and_then(|v| v.as_str()) {
                    message = new_msg.to_string();
                }
            }
            HookOutcome::Block {
                reason,
                user_message,
            } => {
                return UserPromptDecision::Block {
                    reason,
                    user_message,
                };
            }
            HookOutcome::Error { message: err } => match hook.on_error() {
                OnError::Block => {
                    return UserPromptDecision::Block {
                        reason: format!("hook {} errored: {err}", hook.hook_id().as_str()),
                        user_message: None,
                    };
                }
                OnError::Warn => {
                    tracing::warn!(
                        hook_id = %hook.hook_id().as_str(),
                        message = %err,
                        "user_prompt_submit hook errored"
                    );
                }
                OnError::Allow => {}
            },
        }
    }
    UserPromptDecision::Continue { message }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hook_executor::{BashExecOutput, BashHookDispatcher};
    use crate::user_hook_types::{HookMatcher, HookSource};
    use async_trait::async_trait;
    use std::collections::BTreeMap;

    /// Dispatcher that returns a canned stdout/exit so we exercise the full
    /// parse + decision path without a real sandbox.
    struct CannedDispatcher {
        stdout: String,
        exit_code: i32,
    }

    #[async_trait]
    impl BashHookDispatcher for CannedDispatcher {
        async fn dispatch(
            &self,
            _payload: &HookPayload,
            _command: &str,
            _extra_env: &BTreeMap<String, String>,
            _opts: &ExecutorOpts,
        ) -> Result<BashExecOutput, String> {
            Ok(BashExecOutput {
                exit_code: self.exit_code,
                stdout: self.stdout.clone(),
                stderr: String::new(),
            })
        }
    }

    fn spec(event: HookEvent, on_error: OnError) -> UserHookSpec {
        UserHookSpec {
            id: Some("t".into()),
            event,
            matcher: HookMatcher::default(),
            executor: ExecutorSpec::Bash {
                command: "true".into(),
                env: Default::default(),
            },
            timeout_ms: 5000,
            on_error,
            description: None,
            source: HookSource::UserConfig,
        }
    }

    fn dispatcher(stdout: &str, exit: i32) -> Arc<dyn BashHookDispatcher> {
        Arc::new(CannedDispatcher {
            stdout: stdout.into(),
            exit_code: exit,
        })
    }

    fn turn_ctx() -> TurnHookContext {
        TurnHookContext {
            session_id: SessionId::new(),
            turn_id: Some(TurnId::new()),
            org_id: None,
            agent_id: None,
        }
    }

    #[tokio::test]
    async fn user_prompt_submit_allow_passes_message_through() {
        let hooks = build_turn_lifecycle_hooks(
            &[spec(HookEvent::UserPromptSubmit, OnError::Warn)],
            HookEvent::UserPromptSubmit,
            dispatcher("", 0),
        );
        let decision = run_user_prompt_submit_hooks(&hooks, &turn_ctx(), "hello".into()).await;
        assert_eq!(
            decision,
            UserPromptDecision::Continue {
                message: "hello".into()
            }
        );
    }

    #[tokio::test]
    async fn user_prompt_submit_block_via_json() {
        let hooks = build_turn_lifecycle_hooks(
            &[spec(HookEvent::UserPromptSubmit, OnError::Warn)],
            HookEvent::UserPromptSubmit,
            dispatcher(
                r#"{"decision":"block","reason":"nope","user_message":"blocked"}"#,
                0,
            ),
        );
        let decision = run_user_prompt_submit_hooks(&hooks, &turn_ctx(), "hello".into()).await;
        match decision {
            UserPromptDecision::Block {
                reason,
                user_message,
            } => {
                assert_eq!(reason, "nope");
                assert_eq!(user_message.as_deref(), Some("blocked"));
            }
            other => panic!("expected Block, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn user_prompt_submit_block_via_nonzero_exit() {
        // Empty stdout + non-zero exit = block (git-hook fallback).
        let hooks = build_turn_lifecycle_hooks(
            &[spec(HookEvent::UserPromptSubmit, OnError::Warn)],
            HookEvent::UserPromptSubmit,
            dispatcher("", 1),
        );
        let decision = run_user_prompt_submit_hooks(&hooks, &turn_ctx(), "hello".into()).await;
        assert!(matches!(decision, UserPromptDecision::Block { .. }));
    }

    #[tokio::test]
    async fn user_prompt_submit_mutate_rewrites_message() {
        let hooks = build_turn_lifecycle_hooks(
            &[spec(HookEvent::UserPromptSubmit, OnError::Warn)],
            HookEvent::UserPromptSubmit,
            dispatcher(
                r#"{"decision":"mutate","patch":{"message":"rewritten"}}"#,
                0,
            ),
        );
        let decision = run_user_prompt_submit_hooks(&hooks, &turn_ctx(), "original".into()).await;
        assert_eq!(
            decision,
            UserPromptDecision::Continue {
                message: "rewritten".into()
            }
        );
    }

    #[tokio::test]
    async fn user_prompt_submit_error_with_on_error_block_blocks() {
        // Non-JSON stdout => executor Error; on_error=Block => Block.
        let hooks = build_turn_lifecycle_hooks(
            &[spec(HookEvent::UserPromptSubmit, OnError::Block)],
            HookEvent::UserPromptSubmit,
            dispatcher("not json at all", 0),
        );
        let decision = run_user_prompt_submit_hooks(&hooks, &turn_ctx(), "hello".into()).await;
        assert!(matches!(decision, UserPromptDecision::Block { .. }));
    }

    #[tokio::test]
    async fn user_prompt_submit_error_with_on_error_warn_continues() {
        let hooks = build_turn_lifecycle_hooks(
            &[spec(HookEvent::UserPromptSubmit, OnError::Warn)],
            HookEvent::UserPromptSubmit,
            dispatcher("not json", 0),
        );
        let decision = run_user_prompt_submit_hooks(&hooks, &turn_ctx(), "hello".into()).await;
        assert_eq!(
            decision,
            UserPromptDecision::Continue {
                message: "hello".into()
            }
        );
    }

    #[tokio::test]
    async fn user_prompt_submit_chain_threads_mutations_then_blocks() {
        // First hook rewrites, second hook blocks. Order preserved.
        let specs = [
            {
                let mut s = spec(HookEvent::UserPromptSubmit, OnError::Warn);
                s.id = Some("rewriter".into());
                s
            },
            {
                let mut s = spec(HookEvent::UserPromptSubmit, OnError::Warn);
                s.id = Some("blocker".into());
                s
            },
        ];
        // Both hooks share one dispatcher returning a block; the first hook's
        // mutate vs block is decided by stdout, so to test threading we build
        // them individually with different dispatchers.
        let rewriter = build_turn_lifecycle_hooks(
            &specs[..1],
            HookEvent::UserPromptSubmit,
            dispatcher(r#"{"decision":"mutate","patch":{"message":"step1"}}"#, 0),
        );
        let blocker = build_turn_lifecycle_hooks(
            &specs[1..],
            HookEvent::UserPromptSubmit,
            dispatcher(r#"{"decision":"block","reason":"stop"}"#, 0),
        );
        let mut chain = rewriter;
        chain.extend(blocker);
        let decision = run_user_prompt_submit_hooks(&chain, &turn_ctx(), "orig".into()).await;
        assert!(matches!(decision, UserPromptDecision::Block { .. }));
    }

    #[tokio::test]
    async fn turn_end_runs_advisory_and_ignores_block() {
        let hooks = build_turn_lifecycle_hooks(
            &[spec(HookEvent::TurnEnd, OnError::Warn)],
            HookEvent::TurnEnd,
            // Even a "block" decision must not abort anything here.
            dispatcher(r#"{"decision":"block","reason":"ignored"}"#, 0),
        );
        // Just assert it runs without panicking; advisory has no return value.
        run_turn_end_hooks(&hooks, &turn_ctx(), json!({"success": true})).await;
    }

    #[tokio::test]
    async fn session_lifecycle_runs_advisory() {
        let hooks = build_session_lifecycle_hooks(
            &[spec(HookEvent::SessionStart, OnError::Warn)],
            HookEvent::SessionStart,
            dispatcher("", 0),
        );
        let ctx = SessionHookContext {
            session_id: SessionId::new(),
            org_id: None,
            agent_id: Some("agt_x".into()),
        };
        run_session_lifecycle_hooks(&hooks, &ctx, json!({"agent_id": "agt_x"})).await;
    }

    #[tokio::test]
    async fn builders_filter_by_event() {
        let specs = vec![
            spec(HookEvent::SessionStart, OnError::Warn),
            spec(HookEvent::SessionEnd, OnError::Warn),
            spec(HookEvent::TurnEnd, OnError::Warn),
            spec(HookEvent::UserPromptSubmit, OnError::Warn),
        ];
        let d = dispatcher("", 0);
        assert_eq!(
            build_session_lifecycle_hooks(&specs, HookEvent::SessionStart, d.clone()).len(),
            1
        );
        assert_eq!(
            build_session_lifecycle_hooks(&specs, HookEvent::SessionEnd, d.clone()).len(),
            1
        );
        assert_eq!(
            build_turn_lifecycle_hooks(&specs, HookEvent::TurnEnd, d.clone()).len(),
            1
        );
        assert_eq!(
            build_turn_lifecycle_hooks(&specs, HookEvent::UserPromptSubmit, d).len(),
            1
        );
    }
}