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
//! Hook integration helpers for agent lifecycle and loop points.
use crate::agent::{Agent, AgentLlmClient, AgentSessionStore, AgentToolDispatcher};
use crate::error::AgentError;
use crate::event::AgentEvent;
use crate::hooks::{
HookDecision, HookEngineError, HookExecutionReport, HookFailureReason, HookInvocation,
};
#[cfg(target_arch = "wasm32")]
use crate::tokio;
use tokio::sync::mpsc;
impl<C, T, S> Agent<C, T, S>
where
C: AgentLlmClient + ?Sized + 'static,
T: AgentToolDispatcher + ?Sized + 'static,
S: AgentSessionStore + ?Sized + 'static,
{
pub(super) async fn execute_hooks(
&self,
invocation: HookInvocation,
event_tx: Option<&mpsc::Sender<AgentEvent>>,
) -> Result<HookExecutionReport, AgentError> {
let Some(hook_engine) = &self.hook_engine else {
return Ok(HookExecutionReport::empty());
};
let report = match hook_engine
.execute(invocation.clone(), Some(&self.hook_run_overrides))
.await
{
Ok(report) => report,
Err(err) => {
self.emit_hook_engine_error(&invocation, event_tx, &err)
.await;
return Err(Self::map_hook_engine_error(err));
}
};
// `HookStarted` means execution actually began. The engine reports the
// hook ids it truly started in `report.started` — foreground entries it
// ran (a deny short-circuit leaves later entries absent) and background
// entries it acquired a permit for and spawned (a saturated queue leaves
// skipped entries absent). Emitting from `matching_hooks()` instead would
// fire `HookStarted` for hooks that never began.
for hook_id in &report.started {
crate::event_tap::tap_emit(
&self.event_tap,
event_tx,
AgentEvent::HookStarted {
hook_id: hook_id.clone(),
point: invocation.point,
},
)
.await;
}
for outcome in &report.outcomes {
if let Some(reason) = &outcome.failure_reason {
crate::event_tap::tap_emit(
&self.event_tap,
event_tx,
AgentEvent::HookFailed {
hook_id: outcome.hook_id.clone(),
point: outcome.point,
reason: reason.clone(),
},
)
.await;
} else {
crate::event_tap::tap_emit(
&self.event_tap,
event_tx,
AgentEvent::HookCompleted {
hook_id: outcome.hook_id.clone(),
point: outcome.point,
duration_ms: outcome.duration_ms.unwrap_or(0),
},
)
.await;
}
}
if let Some(HookDecision::Deny {
hook_id,
reason_code,
message,
payload,
}) = &report.decision
{
crate::event_tap::tap_emit(
&self.event_tap,
event_tx,
AgentEvent::HookDenied {
hook_id: hook_id.clone(),
point: invocation.point,
reason_code: *reason_code,
message: message.clone(),
payload: payload.clone(),
},
)
.await;
}
Ok(report)
}
fn map_hook_engine_error(err: HookEngineError) -> AgentError {
err.into_agent_error()
}
async fn emit_hook_engine_error(
&self,
invocation: &HookInvocation,
event_tx: Option<&mpsc::Sender<AgentEvent>>,
err: &HookEngineError,
) {
if let Some(hook_id) = err.hook_id() {
// A `HookEngineError` carrying a hook_id means that hook actually
// began executing (Timeout / adapter-runtime ExecutionFailed both
// originate from `execute_one` after start; pre-start config breaks
// surface as `InvalidConfiguration`, hook_id == None). The partial
// `HookExecutionReport` — including the id pushed into `started` —
// is discarded when `execute()` returns `Err`, so emit the
// `HookStarted` here, before the terminal `HookFailed`, to preserve
// the invariant that every terminal hook event is preceded by its
// start (observability for timed-out / hard-failed hooks).
crate::event_tap::tap_emit(
&self.event_tap,
event_tx,
AgentEvent::HookStarted {
hook_id: hook_id.clone(),
point: invocation.point,
},
)
.await;
crate::event_tap::tap_emit(
&self.event_tap,
event_tx,
AgentEvent::HookFailed {
hook_id: hook_id.clone(),
point: invocation.point,
reason: HookFailureReason::from_engine_error(err),
},
)
.await;
}
}
}