mlua-swarm 0.21.0

Swarm engine host built on mlua — long-running stateful runtime with Role/Verb gate, CapToken, 3-stage pipeline, and Middleware overlay.
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! `RunTraceStore` — the persisted per-Run trace rail (`TraceEvent`
//! stream) plus the normalized worker-stats types (`TokenUsage` /
//! [`WorkerStats`]) shared by the trace rail and the
//! [`crate::store::run::StepEntry`] step-stats extension.
//!
//! Two rails observe the same dispatch (issue: per-step run stats):
//!
//! - [`crate::store::run::StepEntry`] — the **terminal summary** of one
//!   dispatched step (write-once, appended by the dispatcher after the
//!   outcome is known; carries duration / usage / model / verdict).
//! - [`TraceEvent`] (this module) — the **in-flight stream** of what is
//!   happening (`core.step_dispatched`, `mw.long_hold_warn`, …),
//!   append-only, per-Run, ordered by `seq`.
//!
//! Everything here is purely observational: a failed append must never
//! fail the dispatch it observes (callers warn-and-swallow — the same
//! fail-open convention as `EngineDispatcher::dispatch`'s
//! `append_step_entry`). The naming is deliberately `Trace`, not `Log`:
//! in the Rust ecosystem "log" collides with the `log`/`tracing`
//! facade crates, and this rail is domain data, not process logging.
//!
//! Kinds are an **open set** of namespaced strings — writers may insert
//! new kinds without a schema migration. Current namespaces:
//!
//! - `core.*` — engine/dispatcher (`run_started` / `step_dispatched` /
//!   `step_completed` / `cancel_requested` / `run_finished`)
//! - `mw.*` — middleware (`long_hold_warn`, …)
//! - `worker.*` — adapter / worker self-reports
//! - `ext.*` — future external writers (Lua flow, enhance flow, tools)
//!
//! Layering invariant (future `mlua-swarm-trace` crate split): this
//! module must not depend on engine types — only `crate::types` ids and
//! serde values.

use crate::types::RunId;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use thiserror::Error;

pub mod sqlite;
pub use sqlite::SqliteRunTraceStore;

// ──────────────────────────────────────────────────────────────────────────
// Normalized worker stats (TokenUsage / WorkerStats)
// ──────────────────────────────────────────────────────────────────────────

/// Aggregated token usage for one worker attempt, normalized across
/// worker kinds (agent-block `agent.run` return / subprocess declared
/// mapping / operator self-report). Field names follow the Anthropic
/// wire convention (`input_tokens` / `output_tokens`) that agent-block
/// already normalizes OpenAI-style responses into.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct TokenUsage {
    /// Prompt-side tokens consumed, summed across the attempt's turns.
    pub input_tokens: u64,
    /// Completion-side tokens produced, summed across the attempt's turns.
    pub output_tokens: u64,
    /// `input + output` (kept explicit because some producers report a
    /// total that includes cache-read/creation tokens the two split
    /// fields don't cover).
    pub total_tokens: u64,
}

/// Normalized per-attempt worker statistics, reported by a worker
/// boundary (spawner fold site / result captor / `POST
/// /v1/worker/submit`) into the engine and folded into the terminal
/// [`crate::store::run::StepEntry`] by the dispatcher.
///
/// The three named fields are the **closed schema** the engine knows;
/// everything worker-kind-specific rides in [`Self::adapter_data`] as
/// raw JSON the engine never interprets (capped at
/// [`TRACE_PAYLOAD_CAP_BYTES`] on fold). Every field is optional —
/// absence must never block a dispatch.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct WorkerStats {
    /// Worker kind label (`"agent_block"` / `"subprocess"` /
    /// `"operator"` / …) — set by whichever boundary constructed the
    /// stats, since only that boundary knows its own kind.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub worker_kind: Option<String>,
    /// The model that served the attempt, when the boundary knows it
    /// (subprocess: the rendered `{model}` placeholder; operator:
    /// self-report; agent-block: spec-declared model if any).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    /// Normalized token usage, when the boundary can produce one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub usage: Option<TokenUsage>,
    /// Number of LLM turns the attempt ran (agent-block `num_turns`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub num_turns: Option<u32>,
    /// Worker-kind-specific raw payload (exit code, stderr tail, cache
    /// token detail, …). Observational only — the engine stores it
    /// verbatim (size-capped) and never branches on it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub adapter_data: Option<Value>,
}

impl WorkerStats {
    /// `true` when no field carries information — callers skip
    /// recording an all-empty stats value.
    pub fn is_empty(&self) -> bool {
        self.worker_kind.is_none()
            && self.model.is_none()
            && self.usage.is_none()
            && self.num_turns.is_none()
            && self.adapter_data.is_none()
    }
}

// ──────────────────────────────────────────────────────────────────────────
// TraceEvent / TraceQuery
// ──────────────────────────────────────────────────────────────────────────

/// Byte cap applied to [`TraceEvent::payload`] and
/// [`WorkerStats::adapter_data`] before persisting. Oversized values are
/// replaced by a truncation marker object (see [`cap_payload`]) — the
/// trace rail is an observability artifact, not a blob store.
pub const TRACE_PAYLOAD_CAP_BYTES: usize = 8 * 1024;

/// Default per-Run retention ceiling — appends beyond this many events
/// prune the oldest rows first.
pub const DEFAULT_TRACE_MAX_EVENTS_PER_RUN: usize = 10_000;

/// Default `list` page size when a query sets neither `limit` nor
/// `latest`.
pub const DEFAULT_TRACE_LIST_LIMIT: usize = 1_000;

/// Well-known `TraceEvent.kind` values written by the engine itself.
/// The kind axis is an open set — these constants exist so in-tree
/// writers and tests agree on spelling, not to constrain writers.
pub mod kind {
    /// A Run began dispatching (server-side, once per kick).
    pub const RUN_STARTED: &str = "core.run_started";
    /// The dispatcher is about to spawn a step's worker.
    pub const STEP_DISPATCHED: &str = "core.step_dispatched";
    /// A step reached its terminal outcome (payload carries the status
    /// label + timing summary; the authoritative record is the
    /// `StepEntry` appended in the same breath).
    pub const STEP_COMPLETED: &str = "core.step_completed";
    /// A Run reached its terminal status (payload: `{"status": ...}`).
    pub const RUN_FINISHED: &str = "core.run_finished";
    /// Cancellation was requested for the Run.
    pub const CANCEL_REQUESTED: &str = "core.cancel_requested";
    /// `LongHoldMiddleware` observed a completion above its threshold.
    pub const LONG_HOLD_WARN: &str = "mw.long_hold_warn";
    /// A worker reported a degradation (mirrors the `DegradationEntry`
    /// rail so the trace stream is self-contained).
    pub const WORKER_DEGRADATION: &str = "worker.degradation";
}

/// One persisted trace event — a member of a Run's append-only stream.
#[derive(Debug, Clone, Serialize, Deserialize, schemars::JsonSchema)]
pub struct TraceEvent {
    /// The Run this event belongs to.
    #[schemars(with = "String")]
    pub run_id: RunId,
    /// Per-Run monotonically increasing ordering key, assigned by the
    /// store at append time (1-based).
    pub seq: u64,
    /// Unix epoch milliseconds — when the event was recorded.
    pub ts_ms: i64,
    /// Namespaced kind string (open set; see the module doc).
    pub kind: String,
    /// The Blueprint step ref this event concerns, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub step_ref: Option<String>,
    /// The attempt number this event concerns, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub attempt: Option<u32>,
    /// Free-form JSON payload (capped at [`TRACE_PAYLOAD_CAP_BYTES`]).
    pub payload: Value,
}

/// The caller-supplied half of a [`TraceEvent`] — the store assigns
/// `seq` and `ts_ms` at append time.
#[derive(Debug, Clone)]
pub struct TraceEventDraft {
    /// Namespaced kind string (open set).
    pub kind: String,
    /// The Blueprint step ref this event concerns, if any.
    pub step_ref: Option<String>,
    /// The attempt number this event concerns, if any.
    pub attempt: Option<u32>,
    /// Free-form JSON payload — capped by the store on append.
    pub payload: Value,
}

/// Filter/paging parameters for [`RunTraceStore::list`]. All filters
/// AND together; `latest` and `after` are mutually exclusive with
/// `latest` winning (it answers "show me the tail" regardless of any
/// cursor the caller also carried).
#[derive(Debug, Clone, Default)]
pub struct TraceQuery {
    /// Forward-paging cursor: only events with `seq > after`.
    pub after: Option<u64>,
    /// Page size cap (defaults to [`DEFAULT_TRACE_LIST_LIMIT`]).
    pub limit: Option<usize>,
    /// Tail mode: return the LAST n matching events (still in ascending
    /// `seq` order). Takes precedence over `after`/`limit`.
    pub latest: Option<usize>,
    /// Kind filters — an event matches when its kind equals, or starts
    /// with, ANY entry (prefix match: `"mw."` matches every middleware
    /// kind). Empty = no kind filter.
    pub kinds: Vec<String>,
    /// Exact `step_ref` filter.
    pub step_ref: Option<String>,
    /// Exact `attempt` filter.
    pub attempt: Option<u32>,
}

impl TraceQuery {
    /// Does `event` pass this query's kind/step/attempt filters
    /// (paging axes excluded)?
    fn matches(&self, event: &TraceEvent) -> bool {
        if !self.kinds.is_empty()
            && !self
                .kinds
                .iter()
                .any(|k| event.kind == *k || event.kind.starts_with(k.as_str()))
        {
            return false;
        }
        if let Some(step_ref) = &self.step_ref {
            if event.step_ref.as_deref() != Some(step_ref.as_str()) {
                return false;
            }
        }
        if let Some(attempt) = self.attempt {
            if event.attempt != Some(attempt) {
                return false;
            }
        }
        true
    }

    /// Apply paging (latest wins over after/limit) to an ascending,
    /// already-filtered event list.
    fn page(&self, mut events: Vec<TraceEvent>) -> Vec<TraceEvent> {
        if let Some(n) = self.latest {
            let start = events.len().saturating_sub(n);
            return events.split_off(start);
        }
        if let Some(after) = self.after {
            events.retain(|e| e.seq > after);
        }
        let limit = self.limit.unwrap_or(DEFAULT_TRACE_LIST_LIMIT);
        events.truncate(limit);
        events
    }
}

/// Replace an oversized payload with a truncation marker carrying the
/// original size and a head excerpt, so a runaway writer cannot bloat
/// the trace store. Values at or under [`TRACE_PAYLOAD_CAP_BYTES`] pass
/// through unchanged.
pub fn cap_payload(payload: Value) -> Value {
    let serialized = payload.to_string();
    if serialized.len() <= TRACE_PAYLOAD_CAP_BYTES {
        return payload;
    }
    let head: String = serialized.chars().take(1024).collect();
    serde_json::json!({
        "truncated": true,
        "size_bytes": serialized.len(),
        "head": head,
    })
}

/// Errors surfaced by a [`RunTraceStore`] implementation.
#[derive(Debug, Error)]
pub enum TraceStoreError {
    /// Backend-specific failure.
    #[error("other: {0}")]
    Other(String),
}

// ──────────────────────────────────────────────────────────────────────────
// RunTraceStore trait
// ──────────────────────────────────────────────────────────────────────────

/// Persistence interface for the per-Run trace stream.
#[async_trait]
pub trait RunTraceStore: Send + Sync {
    /// Backend name — for diagnostics/logging.
    fn name(&self) -> &str;

    /// Append one event to `run_id`'s stream, assigning the next `seq`
    /// and stamping `ts_ms`. The store caps `draft.payload` via
    /// [`cap_payload`] and prunes the oldest rows beyond the per-Run
    /// retention ceiling. Appending to an unknown `run_id` is legal —
    /// the trace rail has no foreign-key coupling to `RunStore` (a
    /// trace writer must never fail because Run-row creation raced it).
    async fn append(
        &self,
        run_id: &RunId,
        draft: TraceEventDraft,
    ) -> Result<TraceEvent, TraceStoreError>;

    /// List `run_id`'s events matching `query`, ascending by `seq`.
    async fn list(
        &self,
        run_id: &RunId,
        query: &TraceQuery,
    ) -> Result<Vec<TraceEvent>, TraceStoreError>;

    /// Delete every event belonging to `run_id`, returning the number
    /// of deleted events. Deleting an unknown/empty Run is `Ok(0)`.
    async fn delete_run(&self, run_id: &RunId) -> Result<u64, TraceStoreError>;
}

// ──────────────────────────────────────────────────────────────────────────
// TraceHandle — the pervasive-insertion write port
// ──────────────────────────────────────────────────────────────────────────

/// A cheap, cloneable write handle binding one `run_id` to a
/// [`RunTraceStore`] — the single port through which the dispatcher,
/// middlewares (via `Engine::trace_handle`), server handlers, and any
/// future writer append trace events. Appends are **best-effort**: a
/// store failure is logged at `warn` and swallowed, never propagated
/// (fail-open, matching the `append_step_entry` convention).
#[derive(Clone)]
pub struct TraceHandle {
    run_id: RunId,
    store: Arc<dyn RunTraceStore>,
}

impl TraceHandle {
    /// Bind `run_id` to `store`.
    pub fn new(run_id: RunId, store: Arc<dyn RunTraceStore>) -> Self {
        Self { run_id, store }
    }

    /// The Run this handle appends into.
    pub fn run_id(&self) -> &RunId {
        &self.run_id
    }

    /// Best-effort append (see the struct doc). `kind` should follow
    /// the namespaced open-set convention (`core.*` / `mw.*` /
    /// `worker.*` / `ext.*`).
    pub async fn append(
        &self,
        kind: &str,
        step_ref: Option<&str>,
        attempt: Option<u32>,
        payload: Value,
    ) {
        let draft = TraceEventDraft {
            kind: kind.to_string(),
            step_ref: step_ref.map(str::to_string),
            attempt,
            payload,
        };
        if let Err(e) = self.store.append(&self.run_id, draft).await {
            tracing::warn!(
                run_id = %self.run_id,
                kind = kind,
                error = %e,
                "TraceHandle::append failed (swallowed — trace is observational)"
            );
        }
    }
}

impl std::fmt::Debug for TraceHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TraceHandle")
            .field("run_id", &self.run_id)
            .field("store", &self.store.name())
            .finish()
    }
}

// ──────────────────────────────────────────────────────────────────────────
// InMemoryRunTraceStore
// ──────────────────────────────────────────────────────────────────────────

#[derive(Default)]
struct TraceInner {
    /// Per-Run ascending event lists.
    events: HashMap<RunId, Vec<TraceEvent>>,
    /// Per-Run next `seq` — kept separately from `events.len()` because
    /// retention pruning removes head entries without recycling seqs.
    next_seq: HashMap<RunId, u64>,
}

/// Process-volatile [`RunTraceStore`] — the default when no persistent
/// backend is wired.
pub struct InMemoryRunTraceStore {
    inner: Mutex<TraceInner>,
    max_events_per_run: usize,
}

impl InMemoryRunTraceStore {
    /// Create an empty store with the default retention ceiling.
    pub fn new() -> Self {
        Self {
            inner: Mutex::new(TraceInner::default()),
            max_events_per_run: DEFAULT_TRACE_MAX_EVENTS_PER_RUN,
        }
    }

    /// Create an empty store with a custom per-Run retention ceiling
    /// (tests).
    pub fn with_max_events_per_run(max: usize) -> Self {
        Self {
            inner: Mutex::new(TraceInner::default()),
            max_events_per_run: max,
        }
    }
}

impl Default for InMemoryRunTraceStore {
    fn default() -> Self {
        Self::new()
    }
}

/// Unix epoch milliseconds now — trace events are sub-second dense, so
/// the store stamps millis (the coarser `now_unix` seconds stay on the
/// pre-existing `StepEntry.at` / `RunRecord` fields).
pub(crate) fn now_unix_ms() -> i64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0)
}

#[async_trait]
impl RunTraceStore for InMemoryRunTraceStore {
    fn name(&self) -> &str {
        "in-memory"
    }

    async fn append(
        &self,
        run_id: &RunId,
        draft: TraceEventDraft,
    ) -> Result<TraceEvent, TraceStoreError> {
        let mut inner = self.inner.lock().unwrap();
        let seq_slot = inner.next_seq.entry(run_id.clone()).or_insert(0);
        *seq_slot += 1;
        let event = TraceEvent {
            run_id: run_id.clone(),
            seq: *seq_slot,
            ts_ms: now_unix_ms(),
            kind: draft.kind,
            step_ref: draft.step_ref,
            attempt: draft.attempt,
            payload: cap_payload(draft.payload),
        };
        let list = inner.events.entry(run_id.clone()).or_default();
        list.push(event.clone());
        if list.len() > self.max_events_per_run {
            let overflow = list.len() - self.max_events_per_run;
            list.drain(..overflow);
        }
        Ok(event)
    }

    async fn list(
        &self,
        run_id: &RunId,
        query: &TraceQuery,
    ) -> Result<Vec<TraceEvent>, TraceStoreError> {
        let inner = self.inner.lock().unwrap();
        let events: Vec<TraceEvent> = inner
            .events
            .get(run_id)
            .map(|list| list.iter().filter(|e| query.matches(e)).cloned().collect())
            .unwrap_or_default();
        Ok(query.page(events))
    }

    async fn delete_run(&self, run_id: &RunId) -> Result<u64, TraceStoreError> {
        let mut inner = self.inner.lock().unwrap();
        inner.next_seq.remove(run_id);
        Ok(inner
            .events
            .remove(run_id)
            .map(|list| list.len() as u64)
            .unwrap_or(0))
    }
}

// ──────────────────────────────────────────────────────────────────────────
// tests
// ──────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn rid(s: &str) -> RunId {
        RunId::parse(s).unwrap()
    }

    fn draft(kind: &str, step_ref: Option<&str>, attempt: Option<u32>) -> TraceEventDraft {
        TraceEventDraft {
            kind: kind.to_string(),
            step_ref: step_ref.map(str::to_string),
            attempt,
            payload: json!({"k": kind}),
        }
    }

    #[tokio::test]
    async fn append_assigns_monotonic_seq_per_run() {
        let s = InMemoryRunTraceStore::new();
        let e1 = s
            .append(&rid("R-1"), draft("core.run_started", None, None))
            .await
            .unwrap();
        let e2 = s
            .append(
                &rid("R-1"),
                draft("core.step_dispatched", Some("w"), Some(1)),
            )
            .await
            .unwrap();
        let other = s
            .append(&rid("R-2"), draft("core.run_started", None, None))
            .await
            .unwrap();
        assert_eq!(e1.seq, 1);
        assert_eq!(e2.seq, 2);
        assert_eq!(other.seq, 1, "seq is per-Run, not global");
        assert!(e1.ts_ms > 0);
    }

    #[tokio::test]
    async fn list_filters_by_kind_prefix_step_and_attempt() {
        let s = InMemoryRunTraceStore::new();
        let r = rid("R-1");
        s.append(&r, draft("core.run_started", None, None))
            .await
            .unwrap();
        s.append(&r, draft("core.step_dispatched", Some("a"), Some(1)))
            .await
            .unwrap();
        s.append(&r, draft("mw.long_hold_warn", Some("a"), Some(1)))
            .await
            .unwrap();
        s.append(&r, draft("core.step_completed", Some("b"), Some(2)))
            .await
            .unwrap();

        let mw = s
            .list(
                &r,
                &TraceQuery {
                    kinds: vec!["mw.".into()],
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(mw.len(), 1);
        assert_eq!(mw[0].kind, "mw.long_hold_warn");

        let step_a = s
            .list(
                &r,
                &TraceQuery {
                    step_ref: Some("a".into()),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(step_a.len(), 2);

        let attempt2 = s
            .list(
                &r,
                &TraceQuery {
                    attempt: Some(2),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(attempt2.len(), 1);
        assert_eq!(attempt2[0].step_ref.as_deref(), Some("b"));
    }

    #[tokio::test]
    async fn list_paging_after_and_latest() {
        let s = InMemoryRunTraceStore::new();
        let r = rid("R-1");
        for i in 0..5 {
            s.append(&r, draft(&format!("core.e{i}"), None, None))
                .await
                .unwrap();
        }

        let after = s
            .list(
                &r,
                &TraceQuery {
                    after: Some(3),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(after.iter().map(|e| e.seq).collect::<Vec<_>>(), vec![4, 5]);

        let latest = s
            .list(
                &r,
                &TraceQuery {
                    latest: Some(2),
                    // latest must win over a cursor the caller also set.
                    after: Some(1),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(latest.iter().map(|e| e.seq).collect::<Vec<_>>(), vec![4, 5]);

        let limited = s
            .list(
                &r,
                &TraceQuery {
                    limit: Some(2),
                    ..Default::default()
                },
            )
            .await
            .unwrap();
        assert_eq!(
            limited.iter().map(|e| e.seq).collect::<Vec<_>>(),
            vec![1, 2]
        );
    }

    #[tokio::test]
    async fn retention_prunes_oldest_keeping_seq() {
        let s = InMemoryRunTraceStore::with_max_events_per_run(3);
        let r = rid("R-1");
        for i in 0..5 {
            s.append(&r, draft(&format!("core.e{i}"), None, None))
                .await
                .unwrap();
        }
        let all = s.list(&r, &TraceQuery::default()).await.unwrap();
        assert_eq!(all.iter().map(|e| e.seq).collect::<Vec<_>>(), vec![3, 4, 5]);
        // A later append keeps counting up — pruning never recycles seqs.
        let e6 = s.append(&r, draft("core.e5", None, None)).await.unwrap();
        assert_eq!(e6.seq, 6);
    }

    #[tokio::test]
    async fn delete_run_removes_stream() {
        let s = InMemoryRunTraceStore::new();
        let r = rid("R-1");
        s.append(&r, draft("core.run_started", None, None))
            .await
            .unwrap();
        s.append(&r, draft("core.run_finished", None, None))
            .await
            .unwrap();
        assert_eq!(s.delete_run(&r).await.unwrap(), 2);
        assert!(s.list(&r, &TraceQuery::default()).await.unwrap().is_empty());
        assert_eq!(s.delete_run(&r).await.unwrap(), 0, "double delete is Ok(0)");
    }

    #[tokio::test]
    async fn oversized_payload_is_truncated_with_marker() {
        let s = InMemoryRunTraceStore::new();
        let r = rid("R-1");
        let big = "x".repeat(TRACE_PAYLOAD_CAP_BYTES + 100);
        let e = s
            .append(
                &r,
                TraceEventDraft {
                    kind: "worker.output".into(),
                    step_ref: None,
                    attempt: None,
                    payload: json!({"blob": big}),
                },
            )
            .await
            .unwrap();
        assert_eq!(e.payload.get("truncated"), Some(&json!(true)));
        assert!(e.payload.get("size_bytes").is_some());
    }

    #[tokio::test]
    async fn trace_handle_appends_best_effort() {
        let store: Arc<dyn RunTraceStore> = Arc::new(InMemoryRunTraceStore::new());
        let handle = TraceHandle::new(rid("R-1"), store.clone());
        handle
            .append(kind::STEP_DISPATCHED, Some("w"), Some(1), json!({}))
            .await;
        let events = store
            .list(&rid("R-1"), &TraceQuery::default())
            .await
            .unwrap();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].kind, kind::STEP_DISPATCHED);
    }

    #[test]
    fn worker_stats_is_empty_reflects_fields() {
        assert!(WorkerStats::default().is_empty());
        let stats = WorkerStats {
            usage: Some(TokenUsage {
                input_tokens: 1,
                output_tokens: 2,
                total_tokens: 3,
            }),
            ..Default::default()
        };
        assert!(!stats.is_empty());
    }
}