mlua-swarm 0.25.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
//! `OperatorSessionStore` — persistence for Operator login-flow sessions.
//!
//! One row per minted `POST /v1/operators` session: the sid / bearer token /
//! capability manifest / mint time / 記名. This is the record that
//! lets a single-server restart keep every logged-in Operator logged in —
//! the sibling stores (task / run / replay / trace / …) already persist,
//! and `RunRecord.operator_sid` persists a *pointer* into this session
//! space, so leaving the sessions themselves process-volatile stranded
//! every restored run pin on a `404 unknown sid` after restart.
//!
//! Deliberately **not** persisted: the WS adapter state (`tx` sender,
//! `pending` oneshot map). Both are process-lifetime objects with no
//! meaningful serialized form — an empty rebuild on the client's next WS
//! connect (the existing reconnect path) is the correct restoration.
//!
//! Current scope:
//!
//! - [`InMemoryOperatorSessionStore`] — process-volatile default.
//! - [`SqliteOperatorSessionStore`] — file-backed persistence via
//!   `rusqlite-isle` (same shape as [`crate::store::task::SqliteTaskStore`]).

use crate::types::SessionId;
use crate::AgentProviderManifest;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Mutex;
use thiserror::Error;

/// How many [`ObservedAssignment`] entries one session retains.
///
/// **D2** says the observed part is appended to on every `Assign` and has
/// no delete path; it does not say the row grows forever. A session that
/// re-acquires in a loop would otherwise put an unbounded column behind
/// every `GET /v1/operators`, so the log is a ring: over the cap, the
/// **oldest** entry goes. Nothing a reader has is deleted by an API — the
/// oldest fact simply ages out, and
/// [`OperatorSessionRecord::observed_total`] stays monotone so the reader
/// can see that it did.
///
/// The value is set for the thing the log is read for: telling apart the
/// handful of Runs a driver is currently juggling in one repo. 32 is well
/// past that.
///
/// # The size that count multiplies
///
/// A depth is only half of a size, and this ring is rewritten whole to
/// `observed_json` on every `Assign` and returned for up to
/// `OPERATORS_LIST_MAX_LIMIT` sessions per `GET /v1/operators` read. So
/// every field of an entry has a ceiling — [`TASK_METADATA_MAX_BYTES`] for
/// the JSON bag, [`OBSERVED_TEXT_MAX_BYTES`] for each of the three
/// caller-supplied strings — and the product is what a reader is actually
/// promised: at most `32 × (4096 + 3 × 1024)` ≈ 224 KiB of variable
/// content per session, whatever the launch put in them.
///
/// That number is the claim, and it is stated rather than asserted because
/// the alternative was the shape this used to have: a depth with a
/// reassuring adjective ("still small enough to serialize whole") in front
/// of four fields, one of which — `goal` — had no bound at all, so the
/// sentence was true only of launches that happened to be small.
pub const OBSERVED_CAP: usize = 32;

/// Serialized-size ceiling for a recorded [`ObservedAssignment::task_metadata`].
///
/// `task_metadata` is an arbitrary caller-supplied JSON bag, so it is the
/// one observed field with no natural size. Above this it is dropped and
/// [`ObservedAssignment::task_metadata_omitted`] says so — an omission the
/// reader can see beats a session row that inherits someone's payload
/// 32 times over.
pub const TASK_METADATA_MAX_BYTES: usize = 4096;

/// Byte ceiling for each of the three caller-supplied strings on an
/// [`ObservedAssignment`] — `goal`, `project_root` and `work_dir`.
///
/// The same rule [`TASK_METADATA_MAX_BYTES`] applies to the fourth
/// caller-supplied field, differing in what it does when the ceiling is
/// hit: these three are **cut, not dropped**. A JSON bag half-carried is
/// not a JSON bag, but a goal's opening clause and a path's leading
/// components are exactly what the observed part is read for — telling two
/// of a driver's Runs apart — so keeping the prefix keeps the field doing
/// its job. [`ObservedAssignment::text_truncated`] says a cut happened, and
/// the value itself ends in `…`, so a reader is never handed a shortened
/// path that reads like a whole one.
///
/// 1 KiB is generous for both shapes: a goal is a sentence, and a path that
/// long is already past what most filesystems will hand out.
pub const OBSERVED_TEXT_MAX_BYTES: usize = 1024;

pub mod inmemory;
pub mod sqlite;
pub use inmemory::InMemoryOperatorSessionStore;
pub use sqlite::SqliteOperatorSessionStore;

// ──────────────────────────────────────────────────────────────────────────
// OperatorSessionRecord
// ──────────────────────────────────────────────────────────────────────────

/// One persisted Operator login-flow session.
///
/// Field-for-field the durable subset of the server's `LoginSession` —
/// everything except the process-lifetime WS adapter state, which is
/// rebuilt empty on reconnect.
///
/// # The bearer token is never stored
///
/// [`token_digest`](Self::token_digest) holds
/// `hex(SHA-256(bearer))` — the same fingerprint shape the `/v1/sessions`
/// path already keys its store by, for the same reason ("the sid handed to
/// the client is the token nonce itself (a bearer secret), so the server
/// never uses it as a map key"; see `mse_server::SessionStore`). Every
/// consumer of this record only ever *compares* a presented bearer
/// ([`verify_bearer`](Self::verify_bearer)), so nothing downstream needs
/// the plaintext — it exists only inside `POST /v1/operators`, between
/// minting and the mint response.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OperatorSessionRecord {
    /// Server-minted session id (`S-<hex>`).
    pub sid: SessionId,
    /// `hex(SHA-256(bearer))` of the auth token required on the WS upgrade
    /// and admin routes. Derive with [`Self::digest_of`]; compare with
    /// [`Self::verify_bearer`]. The plaintext bearer is deliberately absent
    /// (see the type doc).
    pub token_digest: String,
    /// Provider-owned effective capability manifest submitted at join.
    pub capability_manifest: Option<AgentProviderManifest>,
    /// Unix epoch seconds when `POST /v1/operators` minted this session.
    pub joined_at_secs: u64,
    /// The **confirmed part** of this session's 記名 (model §4.2, **D1**):
    /// roughly 50 characters the joining AI wrote about what it is working
    /// on, fixed at join and never rewritten afterwards.
    ///
    /// It is what the observed part cannot supply. Two drivers in the same
    /// worktree produce the same `project_root` / `work_dir` and can hold
    /// Runs of the same Blueprint; the sentence one of them wrote at join
    /// exists only in that conversation, which is what makes it an
    /// identifier (§4.2: 観測部分だけでは足りない).
    ///
    /// `None` = the session joined without one. Kept as an absence rather
    /// than an empty string so a reader can tell "nothing was written" from
    /// "something was written and it was blank" — `POST /v1/operators` does
    /// not reject a missing `desc` (unlike **A9** on the assignment side,
    /// **D1-D5** name no `400`), so the absence is a real and readable
    /// state.
    ///
    /// **D4**: nothing matches on this. It is read by humans and AIs to
    /// tell sessions apart, never by the server to decide identity.
    ///
    /// Additive with `#[serde(default)]` — rows persisted before the 記名
    /// existed decode as `None`.
    #[serde(default)]
    pub desc: Option<String>,
    /// The **observed part** of this session's 記名 (model §4.2, **D2**):
    /// one entry per seat this session was assigned, appended by the server
    /// at each `Assign` and never removed by any API.
    ///
    /// Oldest first. Bounded by [`OBSERVED_CAP`] and de-duplicated per
    /// `(run_id, slot)` — see [`Self::record_observed`].
    ///
    /// Additive with `#[serde(default)]`.
    #[serde(default)]
    pub observed: Vec<ObservedAssignment>,
    /// How many `Assign`s have been recorded onto [`Self::observed`] over
    /// this session's life, including the ones the ring has since dropped
    /// and the re-assignments folded into an existing entry.
    ///
    /// Monotone. `observed_total > observed.len()` is the visible signal
    /// that the reader is looking at a window rather than the whole
    /// history.
    ///
    /// Additive with `#[serde(default)]`.
    #[serde(default)]
    pub observed_total: u64,
}

/// One `Assign` as the assigned Operator session observed it — the
/// per-entry shape of the 記名's observed part (model §4.2's second row:
/// 担当した Run と goal / `project_root` / `work_dir` / `task_metadata` /
/// 最終活動時刻).
///
/// # Every field is what the server could actually read
///
/// The three path-ish fields come from the Task row's `task_input_spec`
/// (the persisted `TaskInputSpec` the launch was given, which is also what
/// `TaskInputMiddleware` is later built from), and `goal` from the same
/// row. A launch that carried no Task-level input leaves all three `None`,
/// and nothing is substituted for them: an invented `project_root` would
/// be read as a fact about where the work is happening.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ObservedAssignment {
    /// The Run whose seat was taken.
    pub run_id: String,
    /// Which Blueprint-declared Operator seat (`Run.current`'s key).
    pub slot: String,
    /// The owning Task's human-facing goal, when the Task row could be
    /// read. `None` = the read failed; it is not a claim that the Task has
    /// no goal (the field is not optional on the Task row).
    ///
    /// Cut to [`OBSERVED_TEXT_MAX_BYTES`] — see [`Self::text_truncated`].
    #[serde(default)]
    pub goal: Option<String>,
    /// Task-level project root, from the launch's `TaskInputSpec`. Cut to
    /// [`OBSERVED_TEXT_MAX_BYTES`] — see [`Self::text_truncated`].
    #[serde(default)]
    pub project_root: Option<String>,
    /// Task-level working directory, from the same spec. Cut to
    /// [`OBSERVED_TEXT_MAX_BYTES`] — see [`Self::text_truncated`].
    #[serde(default)]
    pub work_dir: Option<String>,
    /// Task-level metadata bag, from the same spec. Dropped when it
    /// serializes above [`TASK_METADATA_MAX_BYTES`] — see
    /// [`Self::task_metadata_omitted`].
    #[serde(default)]
    pub task_metadata: Option<serde_json::Value>,
    /// `true` when [`Self::task_metadata`] was present but too large to
    /// carry, so a reader does not read the `null` as "the launch supplied
    /// none".
    #[serde(default)]
    pub task_metadata_omitted: bool,
    /// `true` when at least one of [`Self::goal`] / [`Self::project_root`] /
    /// [`Self::work_dir`] was longer than [`OBSERVED_TEXT_MAX_BYTES`] and
    /// was cut to fit.
    ///
    /// One flag for the three because it answers the one question a cut
    /// raises — "is what I am reading the whole value?" — and the cut
    /// values name themselves: each ends in `…`. A flag per field would
    /// only restate that.
    ///
    /// Additive with `#[serde(default)]`: rows written before the ceiling
    /// existed decode as `false`, which is what they were — nothing had
    /// been cut.
    #[serde(default)]
    pub text_truncated: bool,
    /// Unix epoch seconds of the `Assign` this entry records — the session's
    /// last activity when this is its newest entry
    /// ([`OperatorSessionRecord::last_activity_secs`]).
    pub at_secs: u64,
}

impl ObservedAssignment {
    /// Build an entry, applying the [`TASK_METADATA_MAX_BYTES`] bound to
    /// `task_metadata` and the [`OBSERVED_TEXT_MAX_BYTES`] bound to each of
    /// the three caller-supplied strings.
    ///
    /// This is the only constructor callers use, which is what makes the
    /// bounds a property of the type rather than a rule a call site has to
    /// remember: every entry that reaches the ring came through here.
    ///
    /// A metadata value that will not even serialize is treated as an
    /// oversized one (dropped, flagged) rather than as absent — the failure
    /// is about carrying it, not about having it.
    pub fn new(
        run_id: String,
        slot: String,
        goal: Option<String>,
        project_root: Option<String>,
        work_dir: Option<String>,
        task_metadata: Option<serde_json::Value>,
        at_secs: u64,
    ) -> Self {
        let (task_metadata, task_metadata_omitted) = match task_metadata {
            None => (None, false),
            Some(value) => match serde_json::to_string(&value) {
                Ok(text) if text.len() <= TASK_METADATA_MAX_BYTES => (Some(value), false),
                _ => (None, true),
            },
        };
        let mut text_truncated = false;
        let goal = cap_text(goal, &mut text_truncated);
        let project_root = cap_text(project_root, &mut text_truncated);
        let work_dir = cap_text(work_dir, &mut text_truncated);
        Self {
            run_id,
            slot,
            goal,
            project_root,
            work_dir,
            task_metadata,
            task_metadata_omitted,
            text_truncated,
            at_secs,
        }
    }
}

/// Cut `value` to the longest prefix that fits in
/// [`OBSERVED_TEXT_MAX_BYTES`] and mark `truncated`, or hand it back
/// untouched when it already fits.
///
/// The cut lands on a `char` boundary — a byte-sliced `String` would not
/// be one — and the result carries a trailing `…` so the shortening is
/// visible in the value and not only in the flag. That marker is why the
/// output can exceed the ceiling by its own 3 bytes: the bound is on what
/// a caller can put in, not on the notation this adds.
fn cap_text(value: Option<String>, truncated: &mut bool) -> Option<String> {
    let text = value?;
    if text.len() <= OBSERVED_TEXT_MAX_BYTES {
        return Some(text);
    }
    let mut end = OBSERVED_TEXT_MAX_BYTES;
    while end > 0 && !text.is_char_boundary(end) {
        end -= 1;
    }
    *truncated = true;
    let mut cut = String::with_capacity(end + ''.len_utf8());
    cut.push_str(&text[..end]);
    cut.push('');
    Some(cut)
}

impl OperatorSessionRecord {
    /// Append one `Assign` to the observed part (**D2**).
    ///
    /// Two shaping rules, both about keeping the log readable rather than
    /// about deleting anything:
    ///
    /// - **One entry per `(run_id, slot)`.** Re-acquiring a seat this
    ///   session already holds is the same fact with a newer timestamp, so
    ///   the existing entry is replaced and moved to the newest position
    ///   instead of accumulating a row per acquire. A driver that
    ///   re-acquires after every reconnect would otherwise fill the whole
    ///   window with one Run.
    /// - **Newest [`OBSERVED_CAP`] kept.** Past the cap the oldest entry is
    ///   dropped.
    ///
    /// [`Self::observed_total`] counts every call regardless, so a reader
    /// can tell that folding or dropping happened.
    pub fn record_observed(&mut self, entry: ObservedAssignment) {
        self.observed_total = self.observed_total.saturating_add(1);
        if let Some(pos) = self
            .observed
            .iter()
            .position(|e| e.run_id == entry.run_id && e.slot == entry.slot)
        {
            self.observed.remove(pos);
        }
        self.observed.push(entry);
        while self.observed.len() > OBSERVED_CAP {
            self.observed.remove(0);
        }
    }

    /// When this session was last seen doing something — the newest
    /// [`ObservedAssignment::at_secs`], or [`Self::joined_at_secs`] for a
    /// session that has never been assigned anything.
    ///
    /// **D5**'s default ordering key. Derived rather than stored: a
    /// separate column would be a second thing to keep in step with the
    /// log, and the ring only ever drops entries older than the newest one,
    /// so the derivation cannot go stale.
    pub fn last_activity_secs(&self) -> u64 {
        self.observed
            .iter()
            .map(|e| e.at_secs)
            .max()
            .unwrap_or(0)
            .max(self.joined_at_secs)
    }

    /// Digest a plaintext bearer into the at-rest shape
    /// ([`Self::token_digest`]).
    ///
    /// Callers mint a bearer with
    /// [`operator_bearer_token`](crate::types::operator_bearer_token) and
    /// keep the plaintext only long enough to answer the mint request.
    pub fn digest_of(bearer: &str) -> String {
        crate::types::token_fingerprint(bearer)
    }

    /// Constant-time check of a presented bearer against
    /// [`Self::token_digest`].
    ///
    /// The comparison runs over the two digests (fixed-width hex), so it
    /// carries no timing signal about the bearer itself.
    pub fn verify_bearer(&self, bearer: &str) -> bool {
        crate::types::ct_eq(
            self.token_digest.as_bytes(),
            Self::digest_of(bearer).as_bytes(),
        )
    }
}

/// Errors surfaced by an [`OperatorSessionStore`] implementation.
#[derive(Debug, Error)]
pub enum OperatorSessionStoreError {
    /// No session exists for the given sid.
    #[error("operator session not found: {0}")]
    NotFound(SessionId),

    /// Backend-specific failure not covered by the other variants.
    #[error("other: {0}")]
    Other(String),
}

// ──────────────────────────────────────────────────────────────────────────
// OperatorSessionStore trait
// ──────────────────────────────────────────────────────────────────────────

/// Persistence interface for Operator login-flow sessions.
///
/// Write-through contract on the server side: `POST /v1/operators` calls
/// [`put`](Self::put) before answering the mint, teardown (`DELETE
/// /v1/operators/:sid`) calls [`delete`](Self::delete), and a fresh boot
/// calls [`list`](Self::list) once to rehydrate its in-memory session map.
#[async_trait]
pub trait OperatorSessionStore: Send + Sync {
    /// Backend name — for diagnostics/logging.
    fn name(&self) -> &str;

    /// Insert or replace the row for `record.sid`. Upsert semantics: sids
    /// are freshly minted so a same-sid overwrite only happens on a
    /// deliberate re-put of the same session.
    async fn put(&self, record: OperatorSessionRecord) -> Result<(), OperatorSessionStoreError>;

    /// Delete the row for `sid`. `NotFound` when no such row exists.
    async fn delete(&self, sid: &SessionId) -> Result<(), OperatorSessionStoreError>;

    /// List the sessions this store can decode, ascending by
    /// `joined_at_secs` (mint order, stable for deterministic rehydration).
    ///
    /// # Contract: per row, not all-or-nothing
    ///
    /// A backend that decodes at-rest bytes back into
    /// [`OperatorSessionRecord`] **must not** let one undecodable row fail
    /// the whole call. Such a row is skipped and reported with a
    /// `tracing::warn!` naming the row and the field that failed; the
    /// intact rows are still returned. An `Err` from this method therefore
    /// means the *backend* failed (the file is unreadable, the connection
    /// is gone) — never that one stored session went bad.
    ///
    /// This matters because the sole caller is boot-time rehydration, and
    /// its own error path is fatal: an `Err` here takes `mse serve` down
    /// and every healthy session with it. Undecodable rows are reachable
    /// in practice — an older build could persist shapes a newer one
    /// rejects (`sid: "op-<uuid>"` predates the `S-<hex>` shape) — so
    /// all-or-nothing decoding means one stale row bricks the boot.
    ///
    /// Skipping the row rather than defaulting the field is deliberate: a
    /// session restored minus a field it was minted with would come back
    /// claiming something other than what it is, and would fail later,
    /// elsewhere, and quietly. Dropping it is the observable choice.
    ///
    /// # Backends that never decode
    ///
    /// [`InMemoryOperatorSessionStore`] holds live
    /// [`OperatorSessionRecord`]s, so no row of its can be undecodable and
    /// it never skips anything. That is consistent with the contract, not
    /// an exemption from it: "the sessions this store can decode" is every
    /// session it holds.
    async fn list(&self) -> Result<Vec<OperatorSessionRecord>, OperatorSessionStoreError>;
}

// ──────────────────────────────────────────────────────────────────────────
// Shared inner state used by the InMemory backend.
// ──────────────────────────────────────────────────────────────────────────

#[derive(Default)]
pub(crate) struct Inner {
    /// Insertion order — used as a stable tie-break under `list()`.
    pub(crate) order: Vec<SessionId>,
    pub(crate) records: HashMap<SessionId, OperatorSessionRecord>,
}

pub(crate) type SharedInner = Mutex<Inner>;

// ──────────────────────────────────────────────────────────────────────────
// tests — the 記名 shaping rules (D1 / D2 / D5)
// ──────────────────────────────────────────────────────────────────────────

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

    fn record() -> OperatorSessionRecord {
        OperatorSessionRecord {
            sid: SessionId::parse("S-1").expect("a well-formed sid"),
            token_digest: OperatorSessionRecord::digest_of("bearer"),
            capability_manifest: None,
            joined_at_secs: 100,
            desc: None,
            observed: Vec::new(),
            observed_total: 0,
        }
    }

    fn entry(run: &str, slot: &str, at_secs: u64) -> ObservedAssignment {
        ObservedAssignment::new(
            run.to_string(),
            slot.to_string(),
            Some("resolve issue #10".to_string()),
            Some("/repo".to_string()),
            Some("/repo/.worktrees/topic".to_string()),
            Some(json!({"issue": 10})),
            at_secs,
        )
    }

    /// Re-taking a seat this session already holds refreshes the one entry
    /// instead of adding a second, and moves it to the newest position.
    #[test]
    fn re_assigning_the_same_seat_folds_into_one_entry() {
        let mut r = record();
        r.record_observed(entry("R-a", "phase-a-op", 110));
        r.record_observed(entry("R-b", "phase-a-op", 120));
        r.record_observed(entry("R-a", "phase-a-op", 130));

        let seen: Vec<(&str, u64)> = r
            .observed
            .iter()
            .map(|e| (e.run_id.as_str(), e.at_secs))
            .collect();
        assert_eq!(seen, vec![("R-b", 120), ("R-a", 130)]);
        assert_eq!(
            r.observed_total, 3,
            "the fold is not a deletion: the count still says three Assigns happened"
        );
    }

    /// The same Run in a different seat is a different fact.
    #[test]
    fn the_same_run_in_another_seat_is_its_own_entry() {
        let mut r = record();
        r.record_observed(entry("R-a", "phase-a-op", 110));
        r.record_observed(entry("R-a", "phase-b-op", 111));
        assert_eq!(r.observed.len(), 2);
    }

    /// Past the cap the oldest entry ages out; the counter keeps saying how
    /// many there really were.
    #[test]
    fn the_log_is_a_ring_bounded_by_the_cap() {
        let mut r = record();
        for i in 0..(OBSERVED_CAP + 5) {
            r.record_observed(entry(&format!("R-{i}"), "phase-a-op", 200 + i as u64));
        }
        assert_eq!(r.observed.len(), OBSERVED_CAP);
        assert_eq!(r.observed[0].run_id, "R-5", "the oldest five aged out");
        assert_eq!(r.observed_total, (OBSERVED_CAP + 5) as u64);
    }

    /// D5's ordering key: the newest activity, falling back to the join.
    #[test]
    fn last_activity_falls_back_to_the_join_time() {
        let mut r = record();
        assert_eq!(r.last_activity_secs(), 100);
        r.record_observed(entry("R-a", "phase-a-op", 140));
        assert_eq!(r.last_activity_secs(), 140);
    }

    /// An oversized metadata bag is dropped *and flagged*, so the `null` is
    /// not read as "the launch supplied none".
    #[test]
    fn oversized_task_metadata_is_dropped_and_flagged() {
        let big = json!({ "blob": "x".repeat(TASK_METADATA_MAX_BYTES) });
        let e = ObservedAssignment::new(
            "R-a".to_string(),
            "phase-a-op".to_string(),
            None,
            None,
            None,
            Some(big),
            1,
        );
        assert!(e.task_metadata.is_none());
        assert!(e.task_metadata_omitted);

        let small = ObservedAssignment::new(
            "R-a".to_string(),
            "phase-a-op".to_string(),
            None,
            None,
            None,
            None,
            1,
        );
        assert!(!small.task_metadata_omitted, "absent is not omitted");
    }

    /// The bound [`OBSERVED_CAP`]'s doc multiplies by 32 has to exist for
    /// every field, not only for the JSON bag. `goal` is the one a caller
    /// controls with no natural size, and it used to be copied verbatim.
    #[test]
    fn an_oversized_goal_is_cut_and_flagged() {
        let e = ObservedAssignment::new(
            "R-a".to_string(),
            "phase-a-op".to_string(),
            Some("g".repeat(OBSERVED_TEXT_MAX_BYTES * 4)),
            Some("/repo".to_string()),
            None,
            None,
            1,
        );
        let goal = e.goal.as_deref().expect("the prefix is kept, not dropped");
        assert!(
            goal.len() <= OBSERVED_TEXT_MAX_BYTES + ''.len_utf8(),
            "a goal must not enter the ring longer than the ceiling (+ the marker), got {}",
            goal.len()
        );
        assert!(goal.ends_with(''), "the cut names itself in the value");
        assert!(e.text_truncated, "and in the flag");
        assert_eq!(
            e.project_root.as_deref(),
            Some("/repo"),
            "a field that fits is untouched"
        );
    }

    /// The cut lands on a `char` boundary — a multi-byte goal must not be
    /// sliced through the middle of one.
    #[test]
    fn the_cut_lands_on_a_char_boundary() {
        // 3 bytes each, so the ceiling falls inside a character.
        let text = "".repeat(OBSERVED_TEXT_MAX_BYTES);
        let e = ObservedAssignment::new(
            "R-a".to_string(),
            "phase-a-op".to_string(),
            Some(text),
            None,
            None,
            None,
            1,
        );
        let goal = e.goal.as_deref().expect("kept");
        assert!(e.text_truncated);
        assert!(
            goal.trim_end_matches('').chars().all(|c| c == ''),
            "the prefix is whole characters"
        );
    }

    /// Nothing is flagged when nothing was cut — the flag is a report, not
    /// a default.
    #[test]
    fn a_short_entry_is_not_flagged() {
        let e = entry("R-a", "phase-a-op", 1);
        assert!(!e.text_truncated);
        assert_eq!(e.goal.as_deref(), Some("resolve issue #10"));
    }
}