Skip to main content

aion_core/
activity_event.rs

1//! The `ActivityEvent` envelope for the agent-observability real-time channel.
2//!
3//! This module defines the *typed contract* for a live, durable, per-`(workflow, activity,
4//! attempt)` transcript of what an agent harness is doing inside an activity: its messages,
5//! tool calls, tool results, progress, stop reasons, and (ephemeral) token deltas. It is the
6//! sibling of [`crate::cluster_event`] — a **non-replay real-time DTO** that crosses the
7//! Rust -> TypeScript boundary via `ts-rs` into the ops-console generated bindings.
8//!
9//! The wire shapes live in `aion-core` (not `aion-server` / the SDK) for the same reason the
10//! cluster events do: only this leaf crate depends on `ts-rs`, so this is the single place a
11//! Rust type can cross into the ops console's generated union. The `aion-integrations` SDK
12//! re-exports these neutral types; the worker-side per-harness adapter is the single point that
13//! maps a harness's native events into this envelope.
14//!
15//! # Harness neutrality (LOCKED)
16//!
17//! Every type in this module is **harness-neutral**: it names no agent harness, no transport,
18//! and no wire protocol. There is no `Norn`, no JSON-RPC, and no stdio concept here. A harness
19//! is integrated by mapping its native events into these shapes in the worker-side adapter,
20//! never by editing this module. [`ActivityEventKind::Raw`] is the passthrough fallback that
21//! makes the harness-agnostic path possible (and forward-compatible when a harness emits a
22//! shape the adapter does not yet classify).
23//!
24//! # Observability, never replay
25//!
26//! An `ActivityEvent` is an observability record. It is **never** part of the workflow replay
27//! log: the replay-authoritative output of an activity is its single terminal result, not its
28//! transcript. These types deliberately carry no behaviour and no engine coupling — they are
29//! pure data.
30//!
31//! # `u64` precision across the TS boundary
32//!
33//! The ts-rs config exports every `u64` as TS `number` (`with_large_int("number")`), which
34//! truncates above `2^53`. [`ActivityEvent::worker_seq`] and [`ActivityEvent::store_seq`] are
35//! `u64`. This is the *same* accepted ceiling that already applies to [`crate::EventEnvelope::seq`]
36//! and the cluster channel's sequence fields; the transcript sequence follows the established
37//! project convention rather than a divergent string encoding.
38
39use chrono::{DateTime, Utc};
40use serde::{Deserialize, Serialize};
41use uuid::Uuid;
42
43use crate::ids::{ActivityId, WorkflowId};
44
45/// The role a conversational message is attributed to.
46///
47/// Harness-neutral: the worker-side adapter maps a harness's native speaker attribution onto
48/// these roles. `Tool` covers a tool/function participant turn where the harness models one.
49#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Copy, Debug, PartialEq, Eq)]
50#[serde(tag = "role")]
51pub enum MessageRole {
52    /// A turn attributed to the operator / end user.
53    User,
54    /// A turn produced by the agent (model output).
55    Assistant,
56    /// A system / instruction turn.
57    System,
58    /// A turn attributed to a tool or function participant.
59    Tool,
60}
61
62/// A fine-grained progress signal within an activity attempt.
63///
64/// Harness-neutral projection of the incremental, non-terminal signals a harness can emit
65/// (streaming text/thinking fragments, tool-call argument streaming, usage estimates). It is a
66/// tagged union so a harness advertises only the progress shapes it actually produces; anything
67/// unclassifiable falls through to [`ActivityEventKind::Raw`] instead.
68#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Debug, PartialEq)]
69#[serde(tag = "detail")]
70pub enum ProgressDetail {
71    /// A running estimate of resource usage for the attempt so far.
72    UsageEstimate {
73        /// Estimated input (prompt) tokens consumed so far, when the harness reports it.
74        input_tokens: Option<u64>,
75        /// Estimated output (completion) tokens produced so far, when the harness reports it.
76        output_tokens: Option<u64>,
77    },
78    /// A free-form, human-readable progress note the adapter could not model more precisely.
79    Note {
80        /// The progress note text.
81        text: String,
82    },
83}
84
85/// Why an agent run reached a terminal boundary.
86///
87/// Harness-neutral projection of a harness's native stop/finish reason. `Other` carries the
88/// harness's raw reason label for reasons this neutral set does not enumerate, so no stop reason
89/// is ever silently dropped.
90#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Debug, PartialEq, Eq)]
91#[serde(tag = "stop")]
92pub enum StopKind {
93    /// The agent completed its turn normally (produced its result).
94    EndTurn,
95    /// The agent stopped to await a tool result before continuing.
96    ToolUse,
97    /// The run hit a configured resource limit (tokens / turns / time).
98    LimitReached,
99    /// The run was cancelled (e.g. by an intervention or shutdown).
100    Cancelled,
101    /// The run stopped because of an error.
102    Error {
103        /// Human-readable error description.
104        message: String,
105    },
106    /// A stop reason this neutral set does not enumerate; carries the harness's raw label.
107    Other {
108        /// The harness's raw stop-reason label.
109        reason: String,
110    },
111}
112
113/// The payload of an [`ActivityEvent`] — the classified kind of transcript signal.
114///
115/// **Kinds are LOCKED:** `Message`, `ToolCall`, `ToolResult`, `Progress`, `Stop`, `Raw`, plus
116/// `Delta` carried on the same channel but flagged ephemeral (forwarded to the WS, never
117/// persisted). [`Self::Raw`] is the passthrough fallback — critical for the harness-agnostic
118/// path and for forward-compat when a harness adds an event shape the adapter does not yet map.
119#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Debug, PartialEq)]
120#[serde(tag = "kind")]
121pub enum ActivityEventKind {
122    /// A complete conversational message (assistant text/thinking, an operator turn, etc.).
123    Message {
124        /// Who the message is attributed to.
125        role: MessageRole,
126        /// The message text.
127        text: String,
128    },
129    /// The agent invoked a tool/function with structured input.
130    ToolCall {
131        /// The tool/function name.
132        tool: String,
133        /// Correlation id linking this call to its eventual [`Self::ToolResult`].
134        call_id: String,
135        /// The structured tool input.
136        #[ts(type = "unknown")]
137        input: serde_json::Value,
138    },
139    /// A tool/function returned a result for a prior [`Self::ToolCall`].
140    ToolResult {
141        /// Correlation id matching the originating [`Self::ToolCall`].
142        call_id: String,
143        /// The structured tool output.
144        #[ts(type = "unknown")]
145        output: serde_json::Value,
146        /// Whether the tool reported an error result.
147        is_error: bool,
148    },
149    /// A fine-grained, non-terminal progress signal.
150    Progress {
151        /// The progress detail.
152        detail: ProgressDetail,
153    },
154    /// The agent run reached a terminal boundary.
155    Stop {
156        /// Why the run stopped.
157        reason: StopKind,
158    },
159    /// Passthrough fallback for an unmapped or other-harness line.
160    ///
161    /// Carries the source label the adapter observed and the raw value verbatim, so nothing is
162    /// ever silently dropped and the harness-agnostic path stays lossless.
163    Raw {
164        /// A label identifying where the raw value came from (adapter-defined).
165        source: String,
166        /// The raw value, passed through verbatim.
167        #[ts(type = "unknown")]
168        value: serde_json::Value,
169    },
170    /// An ephemeral token delta — forwarded to the WS only, **never persisted**.
171    ///
172    /// Always carried with [`ActivityEvent::ephemeral`] set to `true`.
173    Delta {
174        /// The id of the message this fragment belongs to.
175        message_id: String,
176        /// The incremental text fragment.
177        text_fragment: String,
178    },
179}
180
181/// A live transcript event for one `(workflow, activity, attempt)` produced by an agent harness.
182///
183/// Streamed to the ops console in real time and persisted to a durable observability keyspace
184/// (except [`Self::ephemeral`] events). It is **never** mixed into workflow replay history — the
185/// activity's single terminal result is the replay-authoritative output, not this transcript.
186///
187/// # Ordering
188///
189/// [`Self::emitted_at`] and [`Self::worker_seq`] are best-effort producer-side ordering hints.
190/// [`Self::store_seq`] is assigned by the server at durable-commit time and is `None` until the
191/// event has been persisted — an unpersisted (e.g. ephemeral, or in-flight) event carries no
192/// store sequence.
193#[derive(Serialize, Deserialize, ts_rs::TS, Clone, Debug, PartialEq)]
194pub struct ActivityEvent {
195    /// The workflow this activity belongs to.
196    pub workflow_id: WorkflowId,
197    /// The activity within the workflow.
198    pub activity_id: ActivityId,
199    /// The attempt number of the activity this event was produced during.
200    pub attempt: u32,
201    /// Sub-identity of the agent that produced this event — REQUIRED for multi-agent
202    /// attribution (a single activity attempt may run several agents).
203    pub agent_id: Uuid,
204    /// The role/label of the producing agent (e.g. an orchestrator vs a sub-agent).
205    pub agent_role: String,
206    /// Producer-clock instant the event was emitted (best-effort ordering hint).
207    pub emitted_at: DateTime<Utc>,
208    /// Worker-local best-effort monotonic sequence.
209    ///
210    /// Exported to TypeScript as `number`; see the module docs for the accepted `2^53` ceiling.
211    pub worker_seq: u64,
212    /// Server-stamped monotonic sequence assigned at durable-commit time; `None` until the event
213    /// is persisted (ephemeral events are never persisted and always carry `None`).
214    ///
215    /// Exported to TypeScript as `number`; see the module docs for the accepted `2^53` ceiling.
216    pub store_seq: Option<u64>,
217    /// When `true`, this event is WS-forward-only and is **never persisted** (token deltas).
218    pub ephemeral: bool,
219    /// The classified payload of this event.
220    pub kind: ActivityEventKind,
221}
222
223#[cfg(test)]
224mod tests {
225    use chrono::{DateTime, Utc};
226    use serde::de::DeserializeOwned;
227    use serde_json::json;
228    use uuid::Uuid;
229
230    use super::{
231        ActivityEvent, ActivityEventKind, MessageRole, ProgressDetail, StopKind, WorkflowId,
232    };
233    use crate::ids::ActivityId;
234
235    fn fixed_time() -> DateTime<Utc> {
236        DateTime::from_timestamp(1_700_000_000, 0).unwrap_or_default()
237    }
238
239    fn round_trip<T>(value: &T) -> Result<T, serde_json::Error>
240    where
241        T: DeserializeOwned + serde::Serialize,
242    {
243        let json = serde_json::to_string(value)?;
244        serde_json::from_str::<T>(&json)
245    }
246
247    fn envelope(kind: ActivityEventKind, ephemeral: bool, store_seq: Option<u64>) -> ActivityEvent {
248        ActivityEvent {
249            workflow_id: WorkflowId::new(Uuid::nil()),
250            activity_id: ActivityId::from_sequence_position(7),
251            attempt: 2,
252            agent_id: Uuid::nil(),
253            agent_role: "orchestrator".to_owned(),
254            emitted_at: fixed_time(),
255            worker_seq: 42,
256            store_seq,
257            ephemeral,
258            kind,
259        }
260    }
261
262    #[test]
263    fn envelope_round_trips_through_json() -> Result<(), Box<dyn std::error::Error>> {
264        let event = envelope(
265            ActivityEventKind::Message {
266                role: MessageRole::Assistant,
267                text: "hello".to_owned(),
268            },
269            false,
270            Some(9),
271        );
272        let decoded = round_trip(&event)?;
273        assert_eq!(event, decoded);
274        Ok(())
275    }
276
277    #[test]
278    fn every_event_kind_round_trips() -> Result<(), Box<dyn std::error::Error>> {
279        let kinds = vec![
280            ActivityEventKind::Message {
281                role: MessageRole::User,
282                text: "steer".to_owned(),
283            },
284            ActivityEventKind::ToolCall {
285                tool: "read_file".to_owned(),
286                call_id: "call-1".to_owned(),
287                input: json!({ "path": "/tmp/x" }),
288            },
289            ActivityEventKind::ToolResult {
290                call_id: "call-1".to_owned(),
291                output: json!({ "bytes": 12 }),
292                is_error: false,
293            },
294            ActivityEventKind::Progress {
295                detail: ProgressDetail::UsageEstimate {
296                    input_tokens: Some(100),
297                    output_tokens: None,
298                },
299            },
300            ActivityEventKind::Progress {
301                detail: ProgressDetail::Note {
302                    text: "thinking".to_owned(),
303                },
304            },
305            ActivityEventKind::Stop {
306                reason: StopKind::EndTurn,
307            },
308            ActivityEventKind::Stop {
309                reason: StopKind::Error {
310                    message: "boom".to_owned(),
311                },
312            },
313            ActivityEventKind::Stop {
314                reason: StopKind::Other {
315                    reason: "custom".to_owned(),
316                },
317            },
318            ActivityEventKind::Raw {
319                source: "unknown-harness".to_owned(),
320                value: json!({ "anything": [1, 2, 3] }),
321            },
322        ];
323        for kind in kinds {
324            let event = envelope(kind, false, None);
325            let decoded = round_trip(&event)?;
326            assert_eq!(event, decoded);
327        }
328        Ok(())
329    }
330
331    #[test]
332    fn ephemeral_delta_round_trips_without_store_seq() -> Result<(), Box<dyn std::error::Error>> {
333        let event = envelope(
334            ActivityEventKind::Delta {
335                message_id: "msg-1".to_owned(),
336                text_fragment: "wor".to_owned(),
337            },
338            true,
339            None,
340        );
341        let decoded = round_trip(&event)?;
342        assert!(decoded.ephemeral);
343        assert_eq!(decoded.store_seq, None);
344        assert_eq!(event, decoded);
345        Ok(())
346    }
347}