greentic-runner-host 1.2.0

Host runtime shim for Greentic runner: config, pack loading, activity handling
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
//! [`HttpRunOutcomeSink`]: posts one run-outcome event per flow turn to the
//! admin's per-unit ingest door.
//!
//! Wire contract (v3 adds `path` / `path_truncated`, see [`super::path`]):
//! greentic-designer
//! `docs/superpowers/specs/2026-09-25-deployed-run-audit-design.md` §3.1/§3.3
//! (v1) plus the additive v2 fields of the Operate → Audit v1.1 Slice B
//! contract (`docs/superpowers/specs/2026-09-26-operate-audit-v1-1-design.md`) —
//! `POST {admin}/api/v1/ingest/run-outcome`, bearer = the unit's
//! worker-usage token (the SAME staged `metering` credential the
//! `WorkerUsageMeter` presents; the admin takes tenant, env and unit from it).
//!
//! # Body
//!
//! JSON, `snake_case`, matching the worker-usage body's convention:
//!
//! ```json
//! {
//!   "event_id": "01J…",            // ULID, admin idempotency key
//!   "occurred_at": "2026-09-25T10:00:00.000Z",
//!   "tenant_slug": "acme", "deployment_id": "…", "bundle_id": "…", "revision_id": "…",
//!   "run_id": "01J…", "flow_id": "main",
//!   "kind": "flow" | "agentic",      // agentic is sticky across the run's turns
//!   "status": "in_progress" | "completed" | "technical_error",  // v2 never sends "agentic"
//!   "seq": 3,                       // v2: 1 on the run's first event, +1 per event
//!   "last_step": "ask_name",        // omitted when unknown
//!   "user_ref": "webchat:u-1",      // omitted when unknown
//!   "user_verified": false,
//!   "channel": "webchat",           // omitted when unknown
//!   "outcome_json": { … },          // omitted; ≤ 4 KB when present
//!   "error_code": "timeout",        // technical_error only
//!   "started_at": "2026-09-25T09:59:00.000Z",
//!   "wait_kind": "user_input" | "approval" | "processing",  // iff in_progress
//!   "response_due_at": "2026-09-26T09:59:00.000Z",          // iff wait_kind = user_input
//!   "worker_id": "pack.demo",       // agentic: agent id; flow: manifest pack_id
//!   "worker_name": "Demo",          // flow: manifest name; omitted otherwise
//!   "worker_version": "1.2.0",      // pack manifest version
//!   "error_ref": "01J…",            // iff technical_error
//!   "error": {                      // technical_error, when a chain is known
//!     "error_ref": "01J…", "error_code": "timeout",
//!     "safe_summary": "Node `crm` failed after 2 retries: timeout",
//!     "redacted_excerpt": "…",      // redacted, ≤ 64 KiB incl. "\n…[truncated]"
//!     "truncated": false, "node_id": "crm", "retry_count": 2
//!   },
//!   "path": ["greet", "crm", "ask_name"],  // v3: node ids run this turn, ≤ 32
//!   "path_truncated": true          // v3: only when the path was cut
//! }
//! ```
//!
//! `tenant_slug` rides for parity with the worker-usage body; the admin must
//! still take the tenant from the token and never from here. Every v2 field is
//! additive: an optional one is omitted rather than sent empty, and an
//! over-long `worker_*` label is omitted rather than truncated.
//!
//! # Failure
//!
//! Same policy as `greentic_aw_runtime::billing::WorkerUsageMeter`, restated
//! here rather than shared because that crate is an OPTIONAL dependency of
//! this one (behind `agentic-worker`) while a flow run is not: `record` spawns
//! the POST and returns; a delivery is dropped, never retried in place, and
//! never fails or delays the turn. `401`/`403` suspend the endpoint for
//! [`AUTH_SUSPENSION`], `429` for its `Retry-After` (capped), a transport
//! error or `5xx` for [`TRANSIENT_SUSPENSION`], and [`REJECTION_STREAK`]
//! consecutive `4xx` (an admin without this door) for a doubling backoff. At
//! most [`MAX_IN_FLIGHT`] POSTs run at once; past that an event is dropped
//! and counted.

use std::sync::Arc;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::time::{Duration, Instant};

use parking_lot::Mutex;
use secrecy::{ExposeSecret, SecretString};
use serde::Serialize;
use serde_json::Value;

use super::{ErrorExcerpt, RunKind, RunOutcome, RunOutcomeSink, RunStatus, WaitKind, now_rfc3339};

/// Budget for DNS + connect + TLS handshake.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(3);
/// Budget for the whole POST; strictly larger than [`CONNECT_TIMEOUT`].
const POST_TIMEOUT: Duration = Duration::from_secs(15);
/// How long a refused token (`401`/`403`) stops this sink asking.
const AUTH_SUSPENSION: Duration = Duration::from_secs(300);
/// Longest a `429`'s `Retry-After` is honoured.
const MAX_RETRY_AFTER: Duration = Duration::from_secs(300);
/// `429` with no readable `Retry-After`.
const DEFAULT_RETRY_AFTER: Duration = Duration::from_secs(60);
/// Pause after a transport error or a `5xx`.
const TRANSIENT_SUSPENSION: Duration = Duration::from_secs(30);
/// Consecutive `4xx` after which the endpoint is suspended.
const REJECTION_STREAK: u32 = 5;
/// First suspension after a streak of `4xx`; doubles per further streak.
const REJECTION_BASE: Duration = Duration::from_secs(60);
/// Ceiling on the `4xx` backoff.
const REJECTION_MAX: Duration = Duration::from_secs(1800);

/// Longest value accepted in any id / label field (the admin ingest doors'
/// `MAX_FIELD_LEN`). An over-long REQUIRED id drops the event; an over-long
/// optional label is omitted rather than truncated into a different value.
pub(crate) const MAX_FIELD_BYTES: usize = 256;

/// Largest serialised `outcome_json` sent. A bigger one is omitted.
pub(crate) const MAX_OUTCOME_JSON_BYTES: usize = 4096;

/// Outcome POSTs in flight at once, per sink.
pub(crate) const MAX_IN_FLIGHT: usize = 32;

fn sampled(count: u64) -> bool {
    count == 1 || count.is_multiple_of(100)
}

/// Where and as whom a unit's run outcomes are recorded. Every field is
/// required and is the value the embedding host resolved for ONE unit.
#[derive(Clone)]
pub struct RunOutcomeTarget {
    /// Full ingest URL (`{admin}/api/v1/ingest/run-outcome`). Must be `https`,
    /// or `http` on a loopback host.
    pub endpoint: String,
    /// The unit's worker-usage bearer. Only ever sent as a header.
    pub token: String,
    /// The workspace tenant the token belongs to.
    pub tenant_slug: String,
    /// The deployment the unit runs under.
    pub deployment_id: String,
    /// The deployed unit (the revision's `bundle_id`).
    pub bundle_id: String,
    /// The revision being served.
    pub revision_id: String,
}

impl std::fmt::Debug for RunOutcomeTarget {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RunOutcomeTarget")
            .field("endpoint", &self.endpoint)
            .field("token", &"<redacted>")
            .field("tenant_slug", &self.tenant_slug)
            .field("deployment_id", &self.deployment_id)
            .field("bundle_id", &self.bundle_id)
            .field("revision_id", &self.revision_id)
            .finish()
    }
}

/// Why a [`RunOutcomeTarget`] could not configure a sink. Never names the token.
#[derive(Debug, thiserror::Error)]
pub enum RunOutcomeSinkError {
    #[error("run outcome sink needs a non-blank `{0}`")]
    Blank(&'static str),
    #[error("run outcome `{0}` is longer than the admin accepts ({MAX_FIELD_BYTES} bytes)")]
    TooLong(&'static str),
    #[error(
        "run outcome endpoint `{0}` is not https and not loopback http; refusing to send \
         a bearer token in cleartext"
    )]
    UnsafeEndpoint(String),
    #[error("run outcome sink has no HTTP client: {0}")]
    Client(String),
}

/// The wire body. Identifiers, a status and a class — no field holds message
/// text.
#[derive(Clone, Debug, Serialize)]
pub(crate) struct RunOutcomeEvent {
    pub(crate) event_id: String,
    pub(crate) occurred_at: String,
    pub(crate) tenant_slug: String,
    pub(crate) deployment_id: String,
    pub(crate) bundle_id: String,
    pub(crate) revision_id: String,
    pub(crate) run_id: String,
    pub(crate) flow_id: String,
    pub(crate) kind: RunKind,
    pub(crate) status: RunStatus,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) last_step: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) user_ref: Option<String>,
    pub(crate) user_verified: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) channel: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) outcome_json: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) error_code: Option<String>,
    pub(crate) started_at: String,
    pub(crate) seq: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) wait_kind: Option<WaitKind>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) response_due_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) worker_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) worker_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) worker_version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) error_ref: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) error: Option<ErrorExcerpt>,
    /// v3: node ids executed this turn. Omitted when empty.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub(crate) path: Vec<String>,
    /// v3: sent only as `true`, and only beside a non-empty `path`.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub(crate) path_truncated: bool,
}

/// What one delivery attempt decided. `pub(crate)` for tests.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Delivery {
    Accepted,
    Rejected,
    Failed,
    Suspended,
}

/// Records a deployed unit's run outcomes at the admin's ingest door.
pub struct HttpRunOutcomeSink {
    inner: Arc<Inner>,
}

struct Inner {
    endpoint: String,
    token: SecretString,
    tenant_slug: String,
    deployment_id: String,
    bundle_id: String,
    revision_id: String,
    http: reqwest::Client,
    suspended_until: Mutex<Option<Instant>>,
    dropped_while_suspended: AtomicU64,
    in_flight: Arc<tokio::sync::Semaphore>,
    dropped_saturated: AtomicU64,
    skipped_invalid: AtomicU64,
    rejected_streak: AtomicU32,
}

impl std::fmt::Debug for HttpRunOutcomeSink {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HttpRunOutcomeSink")
            .field("endpoint", &self.inner.endpoint)
            .field("token", &"<redacted>")
            .field("tenant_slug", &self.inner.tenant_slug)
            .field("deployment_id", &self.inner.deployment_id)
            .field("bundle_id", &self.inner.bundle_id)
            .field("revision_id", &self.inner.revision_id)
            .finish()
    }
}

fn required(field: &'static str, value: &str) -> Result<String, RunOutcomeSinkError> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return Err(RunOutcomeSinkError::Blank(field));
    }
    Ok(trimmed.to_string())
}

fn required_id(field: &'static str, value: &str) -> Result<String, RunOutcomeSinkError> {
    let value = required(field, value)?;
    if value.len() > MAX_FIELD_BYTES {
        return Err(RunOutcomeSinkError::TooLong(field));
    }
    Ok(value)
}

/// `https` anywhere, or `http` to a loopback host.
fn endpoint_is_safe(endpoint: &str) -> bool {
    let Ok(url) = url::Url::parse(endpoint) else {
        return false;
    };
    match url.scheme() {
        "https" => url.host().is_some(),
        "http" => match url.host() {
            Some(url::Host::Domain(domain)) => domain.eq_ignore_ascii_case("localhost"),
            Some(url::Host::Ipv4(ip)) => ip.is_loopback(),
            Some(url::Host::Ipv6(ip)) => ip.is_loopback(),
            None => false,
        },
        _ => false,
    }
}

/// An optional label within the admin's field cap, else omitted.
fn bounded(value: Option<String>) -> Option<String> {
    value.filter(|v| !v.is_empty() && v.len() <= MAX_FIELD_BYTES)
}

/// The v3 `path` as sent. An id longer than [`MAX_FIELD_BYTES`] is dropped
/// rather than truncated into a different id, and the path is then reported
/// as cut; `path_truncated` never rides without a path.
/// Dropping an id can make its neighbours adjacent; equal neighbours are
/// collapsed again so the wire keeps the no-consecutive-duplicates rule.
fn wire_path(path: Vec<String>, truncated: bool) -> (Vec<String>, bool) {
    let before = path.len();
    let mut kept: Vec<String> = path
        .into_iter()
        .filter(|id| !id.is_empty() && id.len() <= MAX_FIELD_BYTES)
        .collect();
    let dropped = kept.len() != before;
    kept.dedup();
    let truncated = (truncated || dropped) && !kept.is_empty();
    (kept, truncated)
}

impl HttpRunOutcomeSink {
    /// Build a sink for one unit. Refuses a blank field and an endpoint that
    /// would carry the token in cleartext off this host.
    pub fn new(target: RunOutcomeTarget) -> Result<Self, RunOutcomeSinkError> {
        let endpoint = required("endpoint", &target.endpoint)?;
        let token = required("token", &target.token)?;
        let tenant_slug = required_id("tenant_slug", &target.tenant_slug)?;
        let deployment_id = required_id("deployment_id", &target.deployment_id)?;
        let bundle_id = required_id("bundle_id", &target.bundle_id)?;
        let revision_id = required_id("revision_id", &target.revision_id)?;
        if !endpoint_is_safe(&endpoint) {
            return Err(RunOutcomeSinkError::UnsafeEndpoint(endpoint));
        }
        let http = reqwest::Client::builder()
            .timeout(POST_TIMEOUT)
            .connect_timeout(CONNECT_TIMEOUT)
            .build()
            .map_err(|err| RunOutcomeSinkError::Client(err.to_string()))?;
        Ok(Self {
            inner: Arc::new(Inner {
                endpoint,
                token: SecretString::from(token),
                tenant_slug,
                deployment_id,
                bundle_id,
                revision_id,
                http,
                suspended_until: Mutex::new(None),
                dropped_while_suspended: AtomicU64::new(0),
                in_flight: Arc::new(tokio::sync::Semaphore::new(MAX_IN_FLIGHT)),
                dropped_saturated: AtomicU64::new(0),
                skipped_invalid: AtomicU64::new(0),
                rejected_streak: AtomicU32::new(0),
            }),
        })
    }

    /// Build the wire event. Refuses an event the admin would refuse (empty or
    /// over-long `run_id` / `flow_id`); ids are never truncated.
    pub(crate) fn build_event(&self, outcome: RunOutcome) -> Result<RunOutcomeEvent, &'static str> {
        let run_id = outcome.run_id.trim();
        if run_id.is_empty() || run_id.len() > MAX_FIELD_BYTES {
            return Err("run_id empty or longer than 256 bytes");
        }
        let flow_id = outcome.flow_id.trim();
        if flow_id.is_empty() || flow_id.len() > MAX_FIELD_BYTES {
            return Err("flow_id empty or longer than 256 bytes");
        }
        let (wait_kind, response_due_at) = match outcome.wait {
            Some(wait) => (Some(wait.kind), wait.response_due_at),
            None => (None, None),
        };
        let outcome_json = outcome.outcome_json.filter(|value| {
            serde_json::to_vec(value)
                .map(|bytes| bytes.len() <= MAX_OUTCOME_JSON_BYTES)
                .unwrap_or(false)
        });
        let (path, path_truncated) = wire_path(outcome.path, outcome.path_truncated);
        Ok(RunOutcomeEvent {
            event_id: ulid::Ulid::new().to_string(),
            occurred_at: now_rfc3339(),
            tenant_slug: self.inner.tenant_slug.clone(),
            deployment_id: self.inner.deployment_id.clone(),
            bundle_id: self.inner.bundle_id.clone(),
            revision_id: self.inner.revision_id.clone(),
            run_id: run_id.to_string(),
            flow_id: flow_id.to_string(),
            kind: outcome.kind,
            status: outcome.status,
            last_step: bounded(outcome.last_step),
            user_ref: bounded(outcome.user_ref),
            user_verified: outcome.user_verified,
            channel: bounded(outcome.channel),
            outcome_json,
            error_code: bounded(outcome.error_code),
            started_at: outcome.started_at,
            seq: outcome.seq,
            wait_kind,
            response_due_at,
            worker_id: bounded(outcome.worker.id),
            worker_name: bounded(outcome.worker.name),
            worker_version: bounded(outcome.worker.version),
            error_ref: outcome.error_ref,
            error: outcome.error,
            path,
            path_truncated,
        })
    }

    #[cfg(test)]
    pub(crate) async fn deliver(&self, event: RunOutcomeEvent) -> Delivery {
        self.inner.deliver(event).await
    }

    #[cfg(test)]
    pub(crate) fn suspended_for(&self) -> Option<Duration> {
        let until = (*self.inner.suspended_until.lock())?;
        until.checked_duration_since(Instant::now())
    }

    #[cfg(test)]
    pub(crate) fn dropped_saturated(&self) -> u64 {
        self.inner.dropped_saturated.load(Ordering::Relaxed)
    }

    #[cfg(test)]
    pub(crate) fn in_flight_semaphore(&self) -> Arc<tokio::sync::Semaphore> {
        Arc::clone(&self.inner.in_flight)
    }
}

impl Inner {
    fn is_suspended(&self) -> bool {
        self.suspended_until
            .lock()
            .is_some_and(|until| Instant::now() < until)
    }

    /// Suspend for `window`; a suspension already running is only extended,
    /// silently, so a burst of failures is one warning.
    fn suspend(&self, window: Duration, reason: &str) {
        let now = Instant::now();
        let already_suspended = {
            let mut guard = self.suspended_until.lock();
            let running = guard.is_some_and(|until| now < until);
            let until = now + window;
            *guard = Some(match *guard {
                Some(existing) if running && existing > until => existing,
                _ => until,
            });
            running
        };
        if already_suspended {
            return;
        }
        let dropped = self.dropped_while_suspended.swap(0, Ordering::Relaxed);
        tracing::warn!(
            endpoint = %self.endpoint,
            bundle_id = %self.bundle_id,
            reason,
            suspended_secs = window.as_secs(),
            dropped_previous_window = dropped,
            "run outcome reporting suspended; outcome events are dropped until it resumes"
        );
    }

    async fn deliver(&self, event: RunOutcomeEvent) -> Delivery {
        if self.is_suspended() {
            self.dropped_while_suspended.fetch_add(1, Ordering::Relaxed);
            return Delivery::Suspended;
        }
        let response = self
            .http
            .post(&self.endpoint)
            // The ONE place the token is used: a header, never URL/body/log.
            .bearer_auth(self.token.expose_secret())
            .json(&event)
            .send()
            .await;
        let response = match response {
            Ok(response) => response,
            Err(err) => {
                let kind = if err.is_timeout() {
                    "timeout"
                } else if err.is_connect() {
                    "connect failed"
                } else {
                    "transport error"
                };
                self.suspend(TRANSIENT_SUSPENSION, kind);
                return Delivery::Failed;
            }
        };
        let status = response.status();
        if status.is_success() {
            self.rejected_streak.store(0, Ordering::Relaxed);
            return Delivery::Accepted;
        }
        match status.as_u16() {
            401 | 403 => {
                self.suspend(AUTH_SUSPENSION, "token refused (401/403)");
                Delivery::Failed
            }
            429 => {
                let window = response
                    .headers()
                    .get(reqwest::header::RETRY_AFTER)
                    .and_then(|value| value.to_str().ok())
                    .and_then(|value| value.trim().parse::<u64>().ok())
                    .map(Duration::from_secs)
                    .unwrap_or(DEFAULT_RETRY_AFTER)
                    .min(MAX_RETRY_AFTER);
                self.suspend(window, "rate limited (429)");
                Delivery::Failed
            }
            code if (400..500).contains(&code) => {
                let streak = self.rejected_streak.fetch_add(1, Ordering::Relaxed) + 1;
                if streak == 1 {
                    tracing::warn!(
                        endpoint = %self.endpoint,
                        status = code,
                        "run outcome event refused by the admin; dropped"
                    );
                }
                if streak.is_multiple_of(REJECTION_STREAK) {
                    let doublings = (streak / REJECTION_STREAK).saturating_sub(1).min(16);
                    let window = REJECTION_BASE
                        .saturating_mul(1u32 << doublings)
                        .min(REJECTION_MAX);
                    self.suspend(
                        window,
                        "admin refused consecutive events (4xx); an admin older than this \
                         runtime may not have the run-outcome ingest door",
                    );
                }
                Delivery::Rejected
            }
            _ => {
                self.suspend(TRANSIENT_SUSPENSION, "admin answered 5xx");
                Delivery::Failed
            }
        }
    }
}

impl RunOutcomeSink for HttpRunOutcomeSink {
    fn record(&self, outcome: RunOutcome) {
        let event = match self.build_event(outcome) {
            Ok(event) => event,
            Err(reason) => {
                let skipped = self.inner.skipped_invalid.fetch_add(1, Ordering::Relaxed) + 1;
                if sampled(skipped) {
                    tracing::warn!(
                        bundle_id = %self.inner.bundle_id,
                        reason,
                        skipped_total = skipped,
                        "run outcome event not sent: the admin would refuse it"
                    );
                }
                return;
            }
        };
        let Ok(handle) = tokio::runtime::Handle::try_current() else {
            tracing::warn!(
                bundle_id = %self.inner.bundle_id,
                "run outcome event dropped: recorded outside a tokio runtime"
            );
            return;
        };
        let Ok(permit) = Arc::clone(&self.inner.in_flight).try_acquire_owned() else {
            let dropped = self.inner.dropped_saturated.fetch_add(1, Ordering::Relaxed) + 1;
            if sampled(dropped) {
                tracing::warn!(
                    bundle_id = %self.inner.bundle_id,
                    in_flight = MAX_IN_FLIGHT,
                    dropped_total = dropped,
                    "run outcome event dropped: too many outcome POSTs already in flight"
                );
            }
            return;
        };
        let inner = Arc::clone(&self.inner);
        handle.spawn(async move {
            inner.deliver(event).await;
            drop(permit);
        });
    }
}

#[cfg(test)]
#[path = "http_tests.rs"]
mod tests;