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
use serde::{Deserialize, Serialize};
use crate::hook::{HookFailure, HookPoint};
use crate::limits::LimitKind;
use crate::outcome::RunStatus;
use crate::provider::{ModelRequest, Usage};
use crate::receipt::Receipt;
use crate::state::{Interruption, RunId};
use crate::stuck::StuckSignal;
/// Everything observable about a run, in order. One stream feeds UIs,
/// tracing exporters, stores and eval assertions.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
#[non_exhaustive]
pub enum Event {
RunStarted {
session_id: String,
},
RunResumed,
/// This run was started by a tool call of `parent_run`.
RunLinked {
parent_run: RunId,
},
TurnStarted {
turn: u32,
},
/// Streamed model text, before the turn's `ModelResponded`.
TextDelta {
turn: u32,
text: String,
},
/// The run continued after a crash or restart.
Recovered {
reran: Vec<String>,
indeterminate: Vec<String>,
},
/// A [`crate::Failover`] chain moved to its next provider during this
/// turn's model call.
ProviderSwitched {
turn: u32,
switch: crate::ProviderSwitch,
},
/// The model is about to be called; pairs with `ModelResponded`, or
/// with `ModelRetried` or `Finished` if the call fails. Content is not
/// journaled: see [`EventSink::observe_request`].
ModelRequested {
turn: u32,
model: String,
messages: usize,
tools: usize,
},
ModelResponded {
turn: u32,
usage: Usage,
text: String,
tool_calls: usize,
/// Why the reply may be incomplete, in the provider's words.
#[serde(default, skip_serializing_if = "Option::is_none")]
note: Option<String>,
},
ToolStarted {
call_id: String,
tool: String,
},
ToolFinished {
receipt: Receipt,
},
Stuck {
signal: StuckSignal,
},
StopRejected {
message: String,
},
/// A hook made a decision other than letting things proceed. Together
/// with `HookFailed` this says which hook did what, and why.
HookDecided {
point: HookPoint,
hook: String,
decision: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
reason: Option<String>,
},
/// A hook panicked, timed out or was cancelled, and gave no answer.
/// See [`crate::Hook`] for what the run does instead.
HookFailed {
point: HookPoint,
hook: String,
failure: HookFailure,
},
/// A hook answered in the model's place, or supplied the response of a
/// failed call; the model's own reply was not used.
ModelAnswered {
turn: u32,
hook: String,
},
/// An `after_model` hook rejected this turn's response.
ModelRejected {
turn: u32,
message: String,
},
/// The model call failed and an `on_model_error` hook asked for a retry.
ModelRetried {
turn: u32,
error: String,
after_ms: u64,
},
/// An `on_limit` hook raised a limit; `amount` is in the limit's unit.
LimitExtended {
limit: LimitKind,
amount: u64,
},
/// An `edit_transcript` hook replaced the stored transcript.
TranscriptEdited {
turn: u32,
before: usize,
after: usize,
},
/// An `edit_transcript` hook's edit was malformed and was not applied.
TranscriptEditRefused {
turn: u32,
reason: String,
},
Interrupted {
interruption: Interruption,
},
Finished {
status: RunStatus,
usage: Usage,
turns: u32,
/// How long the run took, summed across resumes, on the runtime's
/// clock (see [`crate::RunOutcome::elapsed`]).
#[serde(default)]
elapsed_ms: u64,
},
}
/// Receives events synchronously as they happen. Keep it cheap; forward to a
/// channel if the consumer is slow. Events carry no timestamps: a sink is
/// called the moment each happens, so it stamps them with whatever clock it
/// keeps (receipts carry each call's `duration_ms`, and
/// [`Event::Finished`] the run's `elapsed_ms`).
pub trait EventSink: Send + Sync {
fn emit(&self, run_id: RunId, event: &Event);
/// The exact request about to go to the model, after every hook has
/// shaped it. Prompts are not events, so they are never journaled; a
/// sink that exports them takes on deciding what may leave.
fn observe_request(&self, _run_id: RunId, _request: &ModelRequest) {}
}
impl<F> EventSink for F
where
F: Fn(RunId, &Event) + Send + Sync,
{
fn emit(&self, run_id: RunId, event: &Event) {
self(run_id, event)
}
}