Skip to main content

a2a_protocol_server/metrics/
labels.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! The bounded label values the [`Metrics`](crate::metrics::Metrics) hooks report.
5
6/// How an executor's run ended, as [`Metrics::on_executor_run`](crate::metrics::Metrics::on_executor_run) reports it.
7///
8/// A bounded set, so a dashboard can list every value.
9pub mod executor_outcome {
10    /// The executor returned `Ok`.
11    pub const COMPLETED: &str = "completed";
12    /// It returned an error that is not a spent budget.
13    pub const ERROR: &str = "error";
14    /// A budget ran out: the executor timeout passed (the future was
15    /// dropped), or the caller's deadline or tokens did.
16    pub const BUDGET_EXHAUSTED: &str = "budget_exhausted";
17    /// The task was canceled or halted, or the server was shutting down,
18    /// when the executor returned.
19    pub const CANCELED: &str = "canceled";
20    /// The future ended without returning a result: the executor panicked,
21    /// or the run was dropped.
22    pub const ABORTED: &str = "aborted";
23}
24
25/// Operation labels passed to [`Metrics::on_persistence_error`](crate::metrics::Metrics::on_persistence_error).
26///
27/// Named constants rather than string literals at the call sites, so the set
28/// stays bounded and greppable — an operator building a dashboard needs to know
29/// every value this can take, and a typo at one call site would otherwise
30/// create a silent second series.
31pub mod persistence_operation {
32    /// Persisting a task status transition.
33    pub const STATUS_UPDATE: &str = "status_update";
34    /// Persisting parts appended to an existing artifact.
35    pub const ARTIFACT_APPEND: &str = "artifact_append";
36    /// Persisting a newly added artifact.
37    pub const ARTIFACT_PUSH: &str = "artifact_push";
38    /// Appending one event to a task's event log.
39    pub const EVENT_APPEND: &str = "event_append";
40    /// Persisting a whole-task snapshot event.
41    pub const TASK_SNAPSHOT: &str = "task_snapshot";
42    /// Persisting the failed state after an invalid transition was rejected.
43    pub const FAILED_STATE: &str = "failed_state";
44    /// Persisting an agent message appended to the task's history.
45    pub const HISTORY_APPEND: &str = "history_append";
46    /// Waiting for a task's event log to catch up with what was already
47    /// broadcast, on a resubscribe that asked to resume.
48    ///
49    /// Reported when the wait bounded by
50    /// [`HandlerLimits::subscribe_replay_catchup`](crate::handler::HandlerLimits::subscribe_replay_catchup)
51    /// expired with the log still short of the writer's position. The replay
52    /// was served with what the log held, so the subscriber is missing the
53    /// shortfall — this is the only signal that it happened.
54    pub const EVENT_LOG_CATCHUP: &str = "event_log_catchup";
55    /// Handing an event from the executor's queue to the persistence processor.
56    ///
57    /// Reported with `error_kind = "channel_closed"` when the processor is
58    /// gone and the event was dropped: the stream still serves live
59    /// subscribers, but the task's stored state stops advancing, and until
60    /// 0.12 a default build said nothing about it.
61    pub const QUEUE_HANDOFF: &str = "queue_handoff";
62    /// One PostgreSQL statement, run again after its connection was lost
63    /// (`store::pg_retry`); reported only through
64    /// [`Metrics::on_store_retry`](crate::metrics::Metrics::on_store_retry).
65    pub const STATEMENT: &str = "statement";
66    /// Reading a task's webhook list to deliver an event.
67    ///
68    /// Reported through [`Metrics::on_store_retry`](crate::metrics::Metrics::on_store_retry)
69    /// while the read is retried; when it gives up, the event's push is
70    /// reported as a `failed` delivery (`push_outcome::FAILED`).
71    pub const PUSH_CONFIG_LIST: &str = "push_config_list";
72    /// Writing a record to the audit log (the `audit` feature).
73    ///
74    /// A call or event whose record failed was served or processed unrecorded; the
75    /// gap shows in the chain only as a missing record, so this is how it
76    /// is noticed.
77    pub const AUDIT_APPEND: &str = "audit_append";
78}
79
80/// Error kinds passed to [`Metrics::on_persistence_error`](crate::metrics::Metrics::on_persistence_error) for
81/// [`persistence_operation::EVENT_LOG_CATCHUP`].
82pub mod event_log_catchup_error {
83    /// The bounded wait expired with the log still behind the position the
84    /// writer had already broadcast. The replay was served short.
85    pub const TIMED_OUT: &str = "timed_out";
86}
87
88/// Error kinds passed to [`Metrics::on_persistence_error`](crate::metrics::Metrics::on_persistence_error) for
89/// [`persistence_operation::EVENT_APPEND`].
90///
91/// # Why an append that raised nothing is reported here
92///
93/// An append is `INSERT ... ON CONFLICT (task_id, seq) DO NOTHING`, because
94/// `seq` is a *position*: writing one twice must leave one row. That is the
95/// safety property, and it is also a way to lose an event without an error.
96/// Two replicas each running their own in-process event queue number one
97/// task's events from the same place — nothing here hands out a
98/// database-backed lease — so replica B's event *n* lands on the position
99/// replica A already wrote, and the row that stays is A's. Until these labels
100/// existed the store returned `Ok(())` and nothing anywhere said so.
101///
102/// The first two kinds mean **an event the agent emitted is not in the log**,
103/// which is what [`Metrics::on_persistence_error`](crate::metrics::Metrics::on_persistence_error) is for; the third,
104/// [`HOLD_CEILING`](event_append_error::HOLD_CEILING), that a stream lost its
105/// copy of one the log held. A retried append of the
106/// *same* event is not reported here: see
107/// [`POSITION_CONFLICT`](event_append_error::POSITION_CONFLICT).
108pub mod event_append_error {
109    /// The position was already held, and the store could not establish that
110    /// the row already there is this same event.
111    ///
112    /// On a collision the store reads the stored payload back and compares it
113    /// with what it was asked to write. Identical bytes mean a replay of the
114    /// event already recorded, which loses nothing and is **not** counted.
115    /// This label covers the rest: the stored event is a different one, or the
116    /// comparison could not be made (the read-back failed, or the event would
117    /// not serialize). It errs towards reporting.
118    ///
119    /// A non-zero rate is the shape the design has no other defence against:
120    /// two writers numbering one task's log. Look for a second replica
121    /// serving the same task id.
122    pub const POSITION_CONFLICT: &str = "position_conflict";
123
124    /// The store held no such task, so there was nowhere to put the event.
125    ///
126    /// Only the two in-memory stores raise this: they keep the log on the task
127    /// entry, so eviction takes the log with it and the event processor can
128    /// outlive the sweep that removed the task it is still writing for. The
129    /// append stays a success on purpose — failing it would turn a swept task
130    /// into a failed agent — and this is the report that it happened.
131    pub const TASK_ABSENT: &str = "task_absent";
132
133    /// The append succeeded, and the log, at its held-events ceiling, dropped
134    /// its oldest event though an attached stream still owed it.
135    ///
136    /// Unlike the two above, the event was in the log; what is lost is one
137    /// stream's copy of it. Only the two in-memory stores raise this (since
138    /// 2026-10-20): an attached stream holds the events it owes past
139    /// [`max_events_per_task`](crate::store::TaskStoreConfig::max_events_per_task),
140    /// up to [`max_held_events_factor`](crate::store::TaskStoreConfig::max_held_events_factor)
141    /// times it. That stream ends with `streamLagged` after what it
142    /// delivered. A non-zero rate is a client reading far slower than its
143    /// task writes, or one that stopped reading and kept its connection.
144    pub const HOLD_CEILING: &str = "hold_ceiling";
145}
146
147/// Error kinds passed to [`Metrics::on_persistence_error`](crate::metrics::Metrics::on_persistence_error) for
148/// [`persistence_operation::QUEUE_HANDOFF`].
149pub mod queue_handoff_error {
150    /// The persistence channel's receiver was dropped; the event was not
151    /// persisted.
152    pub const CHANNEL_CLOSED: &str = "channel_closed";
153}
154
155/// Why a `SubscribeToTask` stream stopped waiting on a task's event log, as
156/// [`Metrics::on_subscribe_wake`](crate::metrics::Metrics::on_subscribe_wake)
157/// reports it (audit E-F25 (b), ADR 0057).
158pub mod subscribe_wake {
159    /// The store said an event was appended to the task's log
160    /// ([`LogWake::Notified`](crate::store::LogWake::Notified)).
161    pub const NOTIFICATION: &str = "notification";
162    /// The store lost the source of its notifications and has it again, so it
163    /// woke every stream to look
164    /// ([`LogWake::Resynced`](crate::store::LogWake::Resynced)). A rising
165    /// count is a database connection that keeps dropping.
166    pub const RESYNC: &str = "resync";
167    /// Nothing woke the stream before its timer ran out.
168    ///
169    /// On a store with notifications, a wait nothing was appended in, or a
170    /// notification that never arrived. (A store without them reports no wake
171    /// at all.)
172    pub const TIMEOUT: &str = "timeout";
173}
174
175/// The control plane's label values (audit H11; ADR 0038 section 4).
176///
177/// Each set is bounded, so a dashboard can list every value; no task, root,
178/// job or message id is ever one (ADR 0034 section 4).
179pub mod control {
180    /// Why a request was refused before any work, as
181    /// [`Metrics::on_refusal`](crate::metrics::Metrics::on_refusal) reports it.
182    pub mod refusal {
183        /// The server holds its maximum of concurrent streams (event queues).
184        pub const STREAM_CAP: &str = "stream_cap";
185        /// The tenant has its maximum of tasks in flight.
186        pub const TENANT_CAP: &str = "tenant_cap";
187        /// The server holds its maximum of push notification configs.
188        pub const PUSH_CONFIG_CAP: &str = "push_config_cap";
189        /// The idempotency store could not take the key now.
190        pub const IDEMPOTENCY_STORE: &str = "idempotency_store";
191        /// The task's event log could not be read to number the turn.
192        pub const EVENT_LOG: &str = "event_log";
193        /// The halt store could not be read.
194        pub const HALT_STORE: &str = "halt_store";
195    }
196
197    /// Task lease events, as
198    /// [`Metrics::on_lease`](crate::metrics::Metrics::on_lease) reports them.
199    pub mod lease {
200        /// A lease ran out with its task running; the task was stopped.
201        pub const LAPSED: &str = "lapsed";
202    }
203
204    /// What a halt covers.
205    pub mod halt_scope {
206        /// Every task.
207        pub const ALL: &str = "all";
208        /// One tenant's tasks.
209        pub const TENANT: &str = "tenant";
210        /// One task tree, from its root (a job; ADR 0034). The root is not a
211        /// label: it is unbounded.
212        pub const ROOT: &str = "root";
213    }
214
215    /// What was applied.
216    pub mod halt_action {
217        /// A halt.
218        pub const HALT: &str = "halt";
219        /// A resume.
220        pub const RESUME: &str = "resume";
221    }
222
223    /// How a steering message ended, as
224    /// [`Metrics::on_steering`](crate::metrics::Metrics::on_steering) reports
225    /// it (ADR 0036). Every value is one message.
226    pub mod steering {
227        /// Put in the inbox of the turn running on this replica.
228        pub const DELIVERED: &str = "delivered";
229        /// Stored for the replica that holds the turn's claim.
230        pub const HANDED_OFF: &str = "handed_off";
231        /// Answered from the message's idempotency key: not delivered again.
232        pub const REPLAYED: &str = "replayed";
233        /// Refused: the turn's inbox holds its capacity.
234        pub const FULL: &str = "full";
235        /// Refused: the turn ended, or its claim lapsed, before delivery.
236        pub const ENDED: &str = "ended";
237        /// Refused: larger than the configured message size.
238        pub const TOO_LARGE: &str = "too_large";
239        /// Refused: the job (or tenant, or server) is halted.
240        pub const HALTED: &str = "halted";
241        /// Accepted, and still unread when the turn ended: dropped with it.
242        pub const UNREAD: &str = "unread";
243        /// Taken from the store into a turn's inbox on this replica, after
244        /// another replica handed it off.
245        pub const RECEIVED: &str = "received";
246    }
247
248    /// Turn-claim events, as
249    /// [`Metrics::on_claim`](crate::metrics::Metrics::on_claim) reports them.
250    pub mod claim {
251        /// The reaper ended a task its replica left running (its claim had
252        /// expired).
253        pub const REAPED: &str = "reaped";
254        /// The reaper completed the ending of a task that was already
255        /// terminal in the store but whose replica stopped before it logged
256        /// the ending and told the webhooks (its claim had expired).
257        pub const FINISHED: &str = "finished";
258    }
259}