hol-cigar 0.14.2

Open-source context engineering protocol for AI agents: governed retrieval, bounded multi-agent handoffs, MCP, provenance, recoverable effects, and replay.
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
726
727
728
729
730
731
732
733
//! Bounded admission and round-robin dispatch for one broker owner.
//!
//! Registration/host admission must remain private to the host. Authentication here only admits
//! work to a queue; the broker must recheck the current grant, scope and evidence at dispatch.
//! Jobs are non-preemptive. Fairness is between dispatch opportunities, not equal CPU time.

use super::authentication::{ClientHello, ClientProof, ServerKey, ServerProof, grant_id};
use super::{BrokerCredential, BrokerError, MAX_LIFETIME_MS, deadline};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, VecDeque};
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use std::time::{Duration, Instant};

const QUEUED: u8 = 0;
const DISPATCHED: u8 = 1;
const FINISHED: u8 = 2;
const CANCELLED: u8 = 3;

/// Separate global budgets for agents and for the host control channel.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct QueueLimits {
    /// Maximum registered agent lanes.
    pub max_agents: usize,
    /// Aggregate queued agent jobs, excluding the executing job.
    pub max_agent_jobs: usize,
    /// Aggregate encoded bytes of queued agent work.
    pub max_agent_bytes: usize,
    /// Host jobs have their own capacity, unavailable to agents.
    pub max_host_jobs: usize,
    /// Separate encoded-byte capacity for host commands.
    pub max_host_bytes: usize,
    /// Largest accepted queue wait in milliseconds.
    pub max_wait_ms: u64,
}

impl Default for QueueLimits {
    fn default() -> Self {
        Self {
            max_agents: 128,
            max_agent_jobs: 1024,
            max_agent_bytes: 32 * 1024 * 1024,
            max_host_jobs: 8,
            max_host_bytes: 32 * 1024 * 1024,
            max_wait_ms: 30_000,
        }
    }
}

/// Host-assigned per-agent queue allowance. Connections do not each get a separate allowance.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct AgentQueueLimits {
    /// Maximum queued jobs across all of the grant's connections.
    pub max_jobs: usize,
    /// Maximum encoded queued bytes across the grant's connections.
    pub max_bytes: usize,
}

impl Default for AgentQueueLimits {
    fn default() -> Self {
        Self {
            max_jobs: 8,
            max_bytes: 4 * 1024 * 1024,
        }
    }
}

/// One request's cancellation state, shared with its connection handler.
#[derive(Clone)]
pub struct Cancellation(Arc<AtomicU8>);

impl Cancellation {
    /// Returns true only when this operation definitely cannot start in the future.
    /// False means dispatch may have begun/completed; never infer mutation failure or retry.
    #[must_use]
    pub fn cancel(&self) -> bool {
        match self
            .0
            .compare_exchange(QUEUED, CANCELLED, Ordering::AcqRel, Ordering::Acquire)
        {
            Ok(_) | Err(CANCELLED) => true,
            Err(_) => false,
        }
    }
}

/// Dispatch status. Only `Ready` permits calling the broker's operation handler.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DispatchStatus {
    /// The operation has crossed the dispatch boundary; cancellation is now uncertain.
    Ready,
    /// Deadline expired before dispatch; no operation handler may run.
    Expired,
    /// Cancellation or lane revocation occurred before dispatch; no handler may run.
    Cancelled,
}

struct Queued<T> {
    payload: T,
    bytes: usize,
    enqueued: Instant,
    expires: Instant,
    cancellation: Cancellation,
}

/// A dequeued job, including queue-delay telemetry and definitive pre-dispatch failures.
pub struct Scheduled<T> {
    payload: Option<T>,
    status: DispatchStatus,
    delay: Duration,
    cancellation: Cancellation,
}

impl<T> Scheduled<T> {
    /// Definitive scheduling disposition. Expired/cancelled work must only receive a failure reply.
    #[must_use]
    pub const fn status(&self) -> DispatchStatus {
        self.status
    }

    /// Time spent waiting, including time behind other agents' bounded operations.
    #[must_use]
    pub const fn queue_delay(&self) -> Duration {
        self.delay
    }

    /// Take the job for execution or a failure reply. Does not change its scheduling disposition.
    pub fn take(&mut self) -> Option<T> {
        self.payload.take()
    }
}

impl<T> Drop for Scheduled<T> {
    fn drop(&mut self) {
        let _ = self.cancellation.0.compare_exchange(
            DISPATCHED,
            FINISHED,
            Ordering::AcqRel,
            Ordering::Acquire,
        );
    }
}

struct Lane<T> {
    agent: String,
    key: ServerKey,
    limits: AgentQueueLimits,
    expires: Instant,
    jobs: VecDeque<Queued<T>>,
    bytes: usize,
}

/// Host-private scheduler. Keep behind one owner/mutex; synchronization is deliberately external.
pub struct BrokerScheduler<T> {
    epoch: String,
    limits: QueueLimits,
    lanes: BTreeMap<String, Lane<T>>,
    active: VecDeque<String>,
    host: VecDeque<Queued<T>>,
    host_bytes: usize,
    host_turn: bool,
}

impl<T> BrokerScheduler<T> {
    /// Create an empty scheduler tied to exactly one broker epoch.
    pub fn new(epoch: &str, limits: QueueLimits) -> Result<Self, BrokerError> {
        if epoch.len() != 64
            || !epoch.bytes().all(|byte| byte.is_ascii_hexdigit())
            || limits.max_agents == 0
            || limits.max_agents > 128
            || limits.max_agent_jobs < 2
            || limits.max_agent_jobs > 65_536
            || limits.max_agent_bytes < 2
            || limits.max_agent_bytes > 1024 * 1024 * 1024
            || limits.max_host_jobs == 0
            || limits.max_host_jobs > 128
            || limits.max_host_bytes == 0
            || limits.max_host_bytes > 1024 * 1024 * 1024
            || limits.max_wait_ms == 0
            || limits.max_wait_ms > MAX_LIFETIME_MS
        {
            return Err(BrokerError::InvalidInput);
        }
        Ok(Self {
            epoch: epoch.into(),
            limits,
            lanes: BTreeMap::new(),
            active: VecDeque::new(),
            host: VecDeque::new(),
            host_bytes: 0,
            host_turn: true,
        })
    }

    /// Validate a lane's limits before creating/replacing the corresponding broker grant.
    /// One agent can never reserve the entire global agent queue.
    pub fn validate_lane_limits(&self, limits: AgentQueueLimits) -> Result<(), BrokerError> {
        if limits.max_jobs == 0
            || limits.max_jobs > self.limits.max_agent_jobs / 2
            || limits.max_bytes == 0
            || limits.max_bytes > self.limits.max_agent_bytes / 2
        {
            return Err(BrokerError::InvalidInput);
        }
        Ok(())
    }

    /// Register an already-created broker grant; replacing an agent cancels its old queued jobs.
    /// Return cancelled payloads so the transport can send definitive failure responses.
    pub fn host_register(
        &mut self,
        agent: &str,
        credential: &BrokerCredential,
        limits: AgentQueueLimits,
        lease_ms: u64,
    ) -> Result<Vec<Scheduled<T>>, BrokerError> {
        self.validate_lane_limits(limits)?;
        if !crate::graph::valid_id(agent) || lease_ms == 0 || lease_ms > MAX_LIFETIME_MS {
            return Err(BrokerError::InvalidInput);
        }
        let key = self.credential_key(credential)?;
        let server_key = ServerKey::new(credential)?;
        let previous = self
            .lanes
            .iter()
            .find(|(_, lane)| lane.agent == agent)
            .map(|(key, _)| key.clone());
        if self.lanes.contains_key(&key)
            || (previous.is_none() && self.lanes.len() >= self.limits.max_agents)
        {
            return Err(BrokerError::Quota);
        }
        let expires = deadline(lease_ms)?;
        let cancelled = previous.map_or_else(Vec::new, |key| self.remove_lane(&key));
        self.lanes.insert(
            key,
            Lane {
                agent: agent.into(),
                key: server_key,
                limits,
                expires,
                jobs: VecDeque::new(),
                bytes: 0,
            },
        );
        Ok(cancelled)
    }

    /// Prove the current broker to a client before it transmits any context. The transport must
    /// retain this exact proof for this connection and require a fresh client proof in response.
    pub fn server_proof(&self, hello: &ClientHello) -> Result<ServerProof, BrokerError> {
        self.handshake_lane(hello)?.key.prove(hello)
    }

    /// Complete this connection's challenge before reading its command body. Recheck revocation
    /// and expiry; the resulting credential stays inside the broker, never on the agent wire.
    pub fn authenticate_client(
        &self,
        hello: &ClientHello,
        server: &ServerProof,
        client: &ClientProof,
    ) -> Result<BrokerCredential, BrokerError> {
        let credential = self
            .handshake_lane(hello)?
            .key
            .verify_client(hello, server, client)?;
        if grant_id(&credential)? != hello.grant_id {
            return Err(BrokerError::AccessDenied);
        }
        Ok(credential)
    }

    fn handshake_lane(&self, hello: &ClientHello) -> Result<&Lane<T>, BrokerError> {
        if !hello.valid() || hello.epoch != self.epoch {
            return Err(BrokerError::AccessDenied);
        }
        self.lanes
            .get(&hello.grant_id)
            .filter(|lane| lane.expires > Instant::now())
            .ok_or(BrokerError::AccessDenied)
    }

    /// Revoke queue admission and return cancelled work for failure replies. The broker's grant
    /// must also be revoked; queue registration alone is never source/reviewer authority.
    pub fn host_revoke(&mut self, agent: &str) -> Vec<Scheduled<T>> {
        let key = self
            .lanes
            .iter()
            .find(|(_, lane)| lane.agent == agent)
            .map(|(key, _)| key.clone());
        key.map_or_else(Vec::new, |key| self.remove_lane(&key))
    }

    /// Admit one authenticated job after the transport has bounded and measured its encoded frame.
    /// `encoded_bytes` must be the transport's actual byte count, never a client-supplied estimate.
    pub fn enqueue_agent(
        &mut self,
        credential: &BrokerCredential,
        payload: T,
        encoded_bytes: usize,
        wait_ms: u64,
    ) -> Result<Cancellation, BrokerError> {
        self.validate_job(encoded_bytes, wait_ms)?;
        let key = self.credential_key(credential)?;
        let lane = self
            .lanes
            .get(&key)
            .filter(|lane| lane.expires > Instant::now())
            .ok_or(BrokerError::AccessDenied)?;
        let (jobs, bytes) = self.lanes.values().fold((0, 0), |(jobs, bytes), lane| {
            (jobs + lane.jobs.len(), bytes + lane.bytes)
        });
        if jobs >= self.limits.max_agent_jobs
            || encoded_bytes > self.limits.max_agent_bytes - bytes
            || lane.jobs.len() >= lane.limits.max_jobs
            || encoded_bytes > lane.limits.max_bytes - lane.bytes
        {
            return Err(BrokerError::Quota);
        }
        let expires = deadline(wait_ms)?.min(lane.expires);
        let cancellation = Cancellation(Arc::new(AtomicU8::new(QUEUED)));
        if let Some(lane) = self.lanes.get_mut(&key) {
            if lane.jobs.is_empty() {
                self.active.push_back(key);
            }
            lane.jobs.push_back(Queued {
                payload,
                bytes: encoded_bytes,
                enqueued: Instant::now(),
                expires,
                cancellation: cancellation.clone(),
            });
            lane.bytes += encoded_bytes;
        }
        Ok(cancellation)
    }

    /// Admit a private host job, using capacity unavailable to any agent.
    pub fn enqueue_host(
        &mut self,
        payload: T,
        encoded_bytes: usize,
        wait_ms: u64,
    ) -> Result<Cancellation, BrokerError> {
        self.validate_job(encoded_bytes, wait_ms)?;
        if self.host.len() >= self.limits.max_host_jobs
            || encoded_bytes > self.limits.max_host_bytes - self.host_bytes
        {
            return Err(BrokerError::Quota);
        }
        let cancellation = Cancellation(Arc::new(AtomicU8::new(QUEUED)));
        self.host.push_back(Queued {
            payload,
            bytes: encoded_bytes,
            enqueued: Instant::now(),
            expires: deadline(wait_ms)?,
            cancellation: cancellation.clone(),
        });
        self.host_bytes += encoded_bytes;
        Ok(cancellation)
    }

    /// Pop one dispatch opportunity. When both have work, the host and agent classes alternate;
    /// agents rotate once per opportunity. Expired/cancelled jobs still receive a failure reply.
    pub fn pop(&mut self) -> Option<Scheduled<T>> {
        let queued = if !self.host.is_empty() && (self.host_turn || self.active.is_empty()) {
            self.host_turn = false;
            let queued = self.host.pop_front()?;
            self.host_bytes -= queued.bytes;
            queued
        } else {
            let key = self.active.pop_front()?;
            self.host_turn = true;
            let lane = self.lanes.get_mut(&key)?;
            let queued = lane.jobs.pop_front()?;
            lane.bytes -= queued.bytes;
            if !lane.jobs.is_empty() {
                self.active.push_back(key);
            }
            queued
        };
        Some(dispatch(queued))
    }

    /// Total currently queued jobs, for host telemetry/wakeup logic only.
    #[must_use]
    pub fn len(&self) -> usize {
        self.host.len()
            + self
                .lanes
                .values()
                .map(|lane| lane.jobs.len())
                .sum::<usize>()
    }

    /// Whether dispatch would find no work.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Prune expired grants and return their definitively cancelled jobs for failure replies.
    pub fn host_prune(&mut self) -> Vec<Scheduled<T>> {
        let now = Instant::now();
        let keys = self
            .lanes
            .iter()
            .filter(|(_, lane)| lane.expires <= now)
            .map(|(key, _)| key.clone())
            .collect::<Vec<_>>();
        keys.into_iter()
            .flat_map(|key| self.remove_lane(&key))
            .collect()
    }

    /// Cancel all waiting jobs after the runtime has stopped admission. Return their payloads
    /// for definitive failure replies; no returned operation has crossed the dispatch boundary.
    pub fn host_drain(&mut self) -> Vec<Scheduled<T>> {
        let keys = self.lanes.keys().cloned().collect::<Vec<_>>();
        let mut cancelled = keys
            .into_iter()
            .flat_map(|key| self.remove_lane(&key))
            .collect::<Vec<_>>();
        while let Some(job) = self.host.pop_front() {
            let _ = job.cancellation.cancel();
            cancelled.push(dispatch(job));
        }
        self.host_bytes = 0;
        cancelled
    }

    fn validate_job(&self, bytes: usize, wait_ms: u64) -> Result<(), BrokerError> {
        if bytes == 0 || wait_ms == 0 || wait_ms > self.limits.max_wait_ms {
            return Err(BrokerError::InvalidInput);
        }
        Ok(())
    }

    fn credential_key(&self, credential: &BrokerCredential) -> Result<String, BrokerError> {
        if credential.epoch != self.epoch {
            return Err(BrokerError::AccessDenied);
        }
        grant_id(credential)
    }

    fn remove_lane(&mut self, key: &str) -> Vec<Scheduled<T>> {
        self.active.retain(|active| active != key);
        self.lanes.remove(key).map_or_else(Vec::new, |lane| {
            lane.jobs
                .into_iter()
                .map(|job| {
                    let _ = job.cancellation.cancel();
                    dispatch(job)
                })
                .collect()
        })
    }
}

impl<T> Drop for BrokerScheduler<T> {
    fn drop(&mut self) {
        for job in self
            .host
            .iter()
            .chain(self.lanes.values().flat_map(|lane| &lane.jobs))
        {
            let _ = job.cancellation.cancel();
        }
    }
}

fn dispatch<T>(queued: Queued<T>) -> Scheduled<T> {
    let now = Instant::now();
    let status = if queued.cancellation.0.load(Ordering::Acquire) == CANCELLED {
        DispatchStatus::Cancelled
    } else if queued.expires <= now {
        let _ = queued.cancellation.cancel();
        DispatchStatus::Expired
    } else if queued
        .cancellation
        .0
        .compare_exchange(QUEUED, DISPATCHED, Ordering::AcqRel, Ordering::Acquire)
        .is_ok()
    {
        DispatchStatus::Ready
    } else {
        DispatchStatus::Cancelled
    };
    Scheduled {
        payload: Some(queued.payload),
        status,
        delay: now.saturating_duration_since(queued.enqueued),
        cancellation: queued.cancellation,
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::indexing_slicing)]
    use super::*;

    fn credential(i: usize) -> BrokerCredential {
        BrokerCredential {
            epoch: "e".repeat(64),
            secret: format!("{i:064x}"),
        }
    }

    fn queue<T>() -> BrokerScheduler<T> {
        BrokerScheduler::new(&"e".repeat(64), QueueLimits::default()).unwrap()
    }

    fn register<T>(queue: &mut BrokerScheduler<T>, i: usize) {
        queue
            .host_register(
                &format!("agent-{i}"),
                &credential(i),
                AgentQueueLimits::default(),
                60_000,
            )
            .unwrap();
    }

    #[test]
    fn twelve_busy_agents_receive_one_turn_each_and_host_capacity_is_separate() {
        let mut queue = queue();
        for agent in 0..12 {
            register(&mut queue, agent);
            for job in 0..8 {
                queue
                    .enqueue_agent(&credential(agent), (agent, job), 100, 30_000)
                    .unwrap();
            }
            assert!(matches!(
                queue.enqueue_agent(&credential(agent), (agent, 9), 100, 30_000),
                Err(BrokerError::Quota)
            ));
        }
        for i in 0..8 {
            queue.enqueue_host((100, i), 100, 30_000).unwrap();
        }
        let mut agent_order = Vec::new();
        let mut host_count = 0;
        while let Some(mut job) = queue.pop() {
            assert_eq!(job.status(), DispatchStatus::Ready);
            let (agent, number) = job.take().unwrap();
            if agent == 100 {
                host_count += 1;
                assert!(agent_order.len() < 8);
            } else {
                agent_order.push((agent, number));
            }
        }
        assert_eq!(host_count, 8);
        let expected = (0..8)
            .flat_map(|job| (0..12).map(move |agent| (agent, job)))
            .collect::<Vec<_>>();
        assert_eq!(agent_order, expected);
        assert!(queue.is_empty());
    }

    #[test]
    fn global_agent_saturation_preserves_host_and_cancellation_boundaries() {
        let limits = QueueLimits {
            max_agent_jobs: 4,
            max_agent_bytes: 100,
            max_host_bytes: 10,
            ..QueueLimits::default()
        };
        let mut queue = BrokerScheduler::new(&"e".repeat(64), limits).unwrap();
        for agent in 0..2 {
            queue
                .host_register(
                    &format!("a{agent}"),
                    &credential(agent),
                    AgentQueueLimits {
                        max_jobs: 2,
                        max_bytes: 50,
                    },
                    60_000,
                )
                .unwrap();
            queue
                .enqueue_agent(&credential(agent), agent, 50, 30_000)
                .unwrap();
        }
        assert!(matches!(
            queue.enqueue_agent(&credential(0), 3, 1, 30_000),
            Err(BrokerError::Quota)
        ));
        let host = queue.enqueue_host(100, 10, 30_000).unwrap();
        assert!(host.cancel());
        let job = queue.pop().unwrap();
        assert_eq!(job.status(), DispatchStatus::Cancelled);
        assert!(host.cancel());
        drop(job);
        assert!(host.cancel());
        let agent = queue.pop().unwrap();
        assert_eq!(agent.status(), DispatchStatus::Ready);
        assert!(!agent.cancellation.cancel());
        let completed = agent.cancellation.clone();
        drop(agent);
        assert!(!completed.cancel());
    }

    #[test]
    fn expiration_and_redefinition_never_dispatch_previous_grant_jobs() {
        let mut queue = queue();
        register(&mut queue, 1);
        let token = queue.enqueue_agent(&credential(1), 1, 10, 30_000).unwrap();
        let key = queue.credential_key(&credential(1)).unwrap();
        queue
            .lanes
            .get_mut(&key)
            .unwrap()
            .jobs
            .front_mut()
            .unwrap()
            .expires = Instant::now();
        assert_eq!(queue.pop().unwrap().status(), DispatchStatus::Expired);
        assert!(token.cancel());
        let token = queue.enqueue_agent(&credential(1), 2, 10, 30_000).unwrap();
        let replaced = queue
            .host_register(
                "agent-1",
                &credential(2),
                AgentQueueLimits::default(),
                60_000,
            )
            .unwrap();
        assert_eq!(replaced.len(), 1);
        assert_eq!(replaced[0].status(), DispatchStatus::Cancelled);
        assert!(token.cancel());
        assert!(matches!(
            queue.enqueue_agent(&credential(1), 3, 10, 30_000),
            Err(BrokerError::AccessDenied)
        ));
        queue.enqueue_agent(&credential(2), 4, 10, 30_000).unwrap();
        assert_eq!(queue.pop().unwrap().status(), DispatchStatus::Ready);
    }

    #[test]
    fn forged_wrong_epoch_and_unregistered_credentials_cannot_get_a_lane() {
        let mut queue = queue();
        register(&mut queue, 1);
        assert!(matches!(
            queue.enqueue_agent(&credential(9), (), 1, 1),
            Err(BrokerError::AccessDenied)
        ));
        let mut stale = credential(1);
        stale.epoch = "0".repeat(64);
        assert!(matches!(
            queue.enqueue_agent(&stale, (), 1, 1),
            Err(BrokerError::AccessDenied)
        ));
        assert!(queue.is_empty());
        let key = queue.credential_key(&credential(1)).unwrap();
        queue.lanes.get_mut(&key).unwrap().expires = Instant::now();
        assert!(matches!(
            queue.enqueue_agent(&credential(1), (), 1, 1),
            Err(BrokerError::AccessDenied)
        ));
        queue.host_prune();
        assert!(queue.lanes.is_empty());
    }

    #[test]
    fn dispatch_and_cancellation_race_has_only_one_winner() {
        for _ in 0..100 {
            let mut queue = queue();
            register(&mut queue, 1);
            let token = queue.enqueue_agent(&credential(1), (), 1, 30_000).unwrap();
            let barrier = Arc::new(std::sync::Barrier::new(2));
            let other = Arc::clone(&barrier);
            let cancellation = std::thread::spawn(move || {
                other.wait();
                token.cancel()
            });
            barrier.wait();
            let job = queue.pop().unwrap();
            let cancelled = cancellation.join().unwrap();
            assert_eq!(cancelled, job.status() == DispatchStatus::Cancelled);
        }
    }

    #[test]
    fn dropping_queue_proves_waiting_jobs_never_started() {
        let mut queue = queue();
        register(&mut queue, 1);
        let token = queue.enqueue_agent(&credential(1), (), 1, 30_000).unwrap();
        drop(queue);
        assert!(token.cancel());
    }

    #[test]
    fn handshake_rechecks_expiry_revocation_and_grant_replacement() {
        let mut queue = queue::<()>();
        register(&mut queue, 1);
        let hello = ClientHello::new(&credential(1)).unwrap();
        let server = queue.server_proof(&hello).unwrap();
        let client = hello.verify_server(&credential(1), &server).unwrap();
        assert_eq!(
            queue
                .authenticate_client(&hello, &server, &client)
                .unwrap()
                .secret,
            credential(1).secret
        );
        queue.lanes.get_mut(&hello.grant_id).unwrap().expires = Instant::now();
        assert!(queue.server_proof(&hello).is_err());
        assert!(queue.authenticate_client(&hello, &server, &client).is_err());
        queue.host_revoke("agent-1");
        assert!(queue.authenticate_client(&hello, &server, &client).is_err());
        queue
            .host_register(
                "agent-1",
                &credential(2),
                AgentQueueLimits::default(),
                60_000,
            )
            .unwrap();
        assert!(queue.authenticate_client(&hello, &server, &client).is_err());
        let next = ClientHello::new(&credential(2)).unwrap();
        let server = queue.server_proof(&next).unwrap();
        let client = next.verify_server(&credential(2), &server).unwrap();
        assert!(queue.authenticate_client(&next, &server, &client).is_ok());
        let other_epoch =
            BrokerScheduler::<()>::new(&"a".repeat(64), QueueLimits::default()).unwrap();
        assert!(other_epoch.server_proof(&next).is_err());
    }
}