nexo-core 0.1.2

Agent runtime: event bus, sessions, plugin trait, heartbeat, A2A delegation.
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
//! Phase 9.2 follow-up — non-blocking per-tool rate limiter.
//!
//! Token bucket keyed by `(agent, tool)`. The LLM loop calls
//! `try_acquire` before invoking a handler; denial surfaces as an
//! `outcome="rate_limited"` turn result so the model can pick a
//! different tool. No sleep-based backpressure here — the agent loop
//! must stay live.
//!
//! Phase 82.7 — config types unified with the yaml-side `nexo-config`
//! (`ToolRateLimitsConfig` + `ToolRateLimitSpec`) so a single shape
//! crosses operator → runtime without translation. Re-exported as
//! `ToolRateLimitConfig` for back-compat with pre-82.7 callers.
use dashmap::DashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
use tokio::sync::Mutex;

pub use nexo_config::types::agents::{ToolRateLimitSpec, ToolRateLimitsConfig};

/// Phase 9.2 alias — `ToolRateLimitSpec` is the canonical name in
/// `nexo-config` (yaml shape); this alias preserves the older
/// `ToolRateLimitConfig` symbol for downstream re-exports without
/// duplicating the definition.
pub type ToolRateLimitConfig = ToolRateLimitSpec;

/// Default cap on concurrent buckets. With a triple key
/// `(agent, binding_id, tool)` cardinality stays low (operators
/// rarely declare more than ~10 patterns × ~5 bindings × ~50
/// tools per agent), so 10k is generous.
pub const DEFAULT_MAX_BUCKETS: usize = 10_000;

/// Bucket key triple: `(agent, Option<binding_id>, tool)`.
/// `None` binding_id encodes the legacy single-tenant path so
/// pre-Phase-82.7 callers see identical semantics.
type BucketKey = (String, Option<String>, String);

pub struct ToolRateLimiter {
    /// Sorted (pattern, config) pairs; `_default` always last.
    patterns: Vec<(String, ToolRateLimitConfig)>,
    default: Option<ToolRateLimitConfig>,
    buckets: DashMap<BucketKey, TokenBucket>,
    /// Phase 82.7 — keys evicted by LRU whose config had
    /// `essential_deny_on_miss=true`. The next allocation for
    /// such a key consumes the entry and returns false ONCE,
    /// then proceeds normally. Mirrors claude-code-leak's
    /// `policyLimits::isPolicyAllowed` ESSENTIAL deny-on-miss
    /// pattern adapted to LRU eviction.
    evicted_essential: DashMap<BucketKey, ()>,
    max_buckets: usize,
    /// Monotonic counter stamped on every `try_acquire` so we can
    /// pick the stalest bucket without taking any lock. Mirror
    /// `SenderRateLimiter::clock`.
    clock: AtomicU64,
}
struct TokenBucket {
    capacity: u64,
    rate_per_sec: f64,
    /// Phase 82.7 — `true` when config opts in to fail-closed on
    /// LRU eviction. Pinned in the bucket so `enforce_cap` can
    /// flag the key for `evicted_essential` insertion before
    /// removing.
    essential_deny_on_miss: bool,
    tokens: AtomicU64,
    last_refill: Mutex<Instant>,
    last_touch: AtomicU64,
}
impl TokenBucket {
    fn new(cfg: &ToolRateLimitConfig, touch: u64) -> Self {
        let capacity = cfg.effective_burst();
        Self {
            capacity,
            rate_per_sec: cfg.rps.max(0.0),
            essential_deny_on_miss: cfg.essential_deny_on_miss,
            tokens: AtomicU64::new(capacity),
            last_refill: Mutex::new(Instant::now()),
            last_touch: AtomicU64::new(touch),
        }
    }
    /// Atomically try to subtract one token. Refills lazily from elapsed
    /// time since `last_refill`. Returns true if allowed.
    async fn try_acquire(&self, touch: u64) -> bool {
        self.last_touch.store(touch, Ordering::Relaxed);
        let now = Instant::now();
        // Refill step under lock; keep critical section small.
        {
            let mut last = self.last_refill.lock().await;
            let elapsed = now.duration_since(*last).as_secs_f64();
            if elapsed > 0.0 && self.rate_per_sec > 0.0 {
                let add = (elapsed * self.rate_per_sec).floor() as u64;
                if add > 0 {
                    let current = self.tokens.load(Ordering::Relaxed);
                    let new = current.saturating_add(add).min(self.capacity);
                    self.tokens.store(new, Ordering::Relaxed);
                    // Advance last_refill proportionally to tokens added
                    // to avoid starvation from rounding loss.
                    let consumed_secs = add as f64 / self.rate_per_sec;
                    *last += std::time::Duration::from_secs_f64(consumed_secs);
                }
            }
        }
        let mut current = self.tokens.load(Ordering::Acquire);
        loop {
            if current == 0 {
                return false;
            }
            match self.tokens.compare_exchange(
                current,
                current - 1,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => return true,
                Err(actual) => current = actual,
            }
        }
    }
}
impl ToolRateLimiter {
    pub fn new(cfg: ToolRateLimitsConfig) -> Self {
        Self::with_cap(cfg, DEFAULT_MAX_BUCKETS)
    }

    /// Build with a custom bucket cap. Production callers use
    /// `new`; tests cover eviction behaviour with smaller caps.
    pub fn with_cap(cfg: ToolRateLimitsConfig, max_buckets: usize) -> Self {
        let mut sorted: Vec<(String, ToolRateLimitConfig)> = cfg
            .patterns
            .iter()
            .filter(|(k, _)| k.as_str() != "_default")
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        // Deterministic order (alpha) so tests are stable.
        sorted.sort_by(|a, b| a.0.cmp(&b.0));
        let default = cfg.patterns.get("_default").cloned();
        Self {
            patterns: sorted,
            default,
            buckets: DashMap::new(),
            evicted_essential: DashMap::new(),
            max_buckets,
            clock: AtomicU64::new(0),
        }
    }

    /// Resolve the effective config for a given tool name from the
    /// global pattern set. Per-binding overrides bypass this via
    /// `resolve_in_override`.
    fn resolve_global(&self, tool: &str) -> Option<&ToolRateLimitConfig> {
        for (pat, cfg) in &self.patterns {
            if glob_matches(pat, tool) {
                return Some(cfg);
            }
        }
        self.default.as_ref()
    }

    /// Resolve from a per-binding override map (Phase 82.7). First
    /// glob match wins, then `_default`, else `None` (= unlimited
    /// for this tool on this binding — does NOT fall back to
    /// global, per spec semantic "per-binding fully replaces").
    fn resolve_in_override<'a>(
        &self,
        tool: &str,
        over: &'a ToolRateLimitsConfig,
    ) -> Option<ToolRateLimitConfig> {
        // Iterate in deterministic alpha order to mirror global
        // behaviour. Skip `_default` until last.
        let mut explicit: Vec<(&String, &ToolRateLimitSpec)> = over
            .patterns
            .iter()
            .filter(|(k, _)| k.as_str() != "_default")
            .collect();
        explicit.sort_by(|a, b| a.0.cmp(b.0));
        for (pat, cfg) in explicit {
            if glob_matches(pat, tool) {
                return Some(cfg.clone());
            }
        }
        over.patterns.get("_default").cloned()
    }

    /// Phase 9.2 back-compat — single-tenant entry point with no
    /// binding scope. Equivalent to
    /// `try_acquire_with_binding(agent, None, tool, None)`.
    pub async fn try_acquire(&self, agent: &str, tool: &str) -> bool {
        self.try_acquire_with_binding(agent, None, tool, None).await
    }

    /// Phase 82.7 — canonical entry point.
    ///
    /// Resolution priority:
    /// 1. `per_binding_override` is `Some` → look up tool in that
    ///    map only (no fall-through to global). `binding_id` is
    ///    stamped into the bucket key so each binding gets its
    ///    own bucket cardinality.
    /// 2. Else → look up in global `self.patterns` /
    ///    `self.default`. Bucket key uses `None` binding scope
    ///    (legacy single-tenant cell).
    /// 3. No match → unlimited (return `true` without bucket
    ///    allocation).
    pub async fn try_acquire_with_binding(
        &self,
        agent: &str,
        binding_id: Option<&str>,
        tool: &str,
        per_binding_override: Option<&ToolRateLimitsConfig>,
    ) -> bool {
        let (cfg, scoped_binding) = match per_binding_override {
            Some(over) => match self.resolve_in_override(tool, over) {
                Some(cfg) => (cfg, binding_id.map(String::from)),
                None => return true, // override + no match → unlimited (no global fallthrough)
            },
            None => match self.resolve_global(tool) {
                Some(cfg) => (cfg.clone(), None),
                None => return true,
            },
        };

        let key: BucketKey = (agent.to_string(), scoped_binding, tool.to_string());
        let touch = self.clock.fetch_add(1, Ordering::Relaxed);

        // Phase 82.7 — fail-closed deny-on-miss for keys that
        // were LRU-evicted while their config had
        // essential_deny_on_miss. Consume the entry and deny
        // ONCE; the next call allocates a fresh bucket.
        if self.evicted_essential.remove(&key).is_some() {
            return false;
        }

        if !self.buckets.contains_key(&key) {
            self.enforce_cap();
        }
        let entry = self
            .buckets
            .entry(key)
            .or_insert_with(|| TokenBucket::new(&cfg, touch));
        entry.value().try_acquire(touch).await
    }

    /// LRU eviction — drops the stalest bucket(s) until under
    /// `max_buckets`. Stalest is determined by `last_touch`. If
    /// the evicted bucket's config had `essential_deny_on_miss`,
    /// stamps the key into `evicted_essential` so the next
    /// allocation denies once before reallocating.
    fn enforce_cap(&self) {
        if self.max_buckets == 0 {
            return;
        }
        while self.buckets.len() >= self.max_buckets {
            let oldest = self
                .buckets
                .iter()
                .min_by_key(|e| e.value().last_touch.load(Ordering::Relaxed))
                .map(|e| (e.key().clone(), e.value().essential_deny_on_miss));
            match oldest {
                Some((k, was_essential)) => {
                    if self.buckets.remove(&k).is_some() {
                        if was_essential {
                            self.evicted_essential.insert(k.clone(), ());
                        }
                        tracing::debug!(
                            evicted_agent = %k.0,
                            evicted_binding = ?k.1,
                            evicted_tool = %k.2,
                            essential = was_essential,
                            cap = self.max_buckets,
                            "tool rate-limit bucket cap reached; evicted stalest"
                        );
                    }
                }
                None => break,
            }
        }
    }

    /// Active bucket count — useful for telemetry / metrics. Not
    /// part of the hot path.
    pub fn active_buckets(&self) -> usize {
        self.buckets.len()
    }

    /// Phase 82.7 — drop every bucket belonging to `agent`. Used
    /// by admin RPC delete-agent path (Phase 82.10) so
    /// `(agent, *, *)` cells don't leak after an operator removes
    /// the agent. Iteration is O(N) — only fires on admin ops, not
    /// in the hot path.
    pub fn drop_buckets_for_agent(&self, agent: &str) {
        let to_remove: Vec<BucketKey> = self
            .buckets
            .iter()
            .filter_map(|e| {
                if e.key().0 == agent {
                    Some(e.key().clone())
                } else {
                    None
                }
            })
            .collect();
        for k in to_remove {
            self.buckets.remove(&k);
            self.evicted_essential.remove(&k);
        }
    }
}
/// Minimal `*` glob: `foo*bar` matches any string starting `foo` and
/// ending `bar`. `*` alone matches anything.
pub fn glob_matches(pattern: &str, s: &str) -> bool {
    if pattern == "*" {
        return true;
    }
    if !pattern.contains('*') {
        return pattern == s;
    }
    let parts: Vec<&str> = pattern.split('*').collect();
    let mut pos = 0usize;
    for (idx, part) in parts.iter().enumerate() {
        if part.is_empty() {
            continue;
        }
        if idx == 0 {
            if !s[pos..].starts_with(part) {
                return false;
            }
            pos += part.len();
        } else if idx == parts.len() - 1 && !pattern.ends_with('*') {
            return s[pos..].ends_with(part);
        } else {
            match s[pos..].find(part) {
                Some(i) => pos += i + part.len(),
                None => return false,
            }
        }
    }
    true
}
#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    #[test]
    fn glob_matches_exact_and_wildcards() {
        assert!(glob_matches("foo", "foo"));
        assert!(!glob_matches("foo", "bar"));
        assert!(glob_matches("*", "anything"));
        assert!(glob_matches("foo*", "foobar"));
        assert!(!glob_matches("foo*", "barfoo"));
        assert!(glob_matches("*bar", "foobar"));
        assert!(glob_matches("foo*bar", "foobazbar"));
        assert!(glob_matches("*mid*", "leftmidright"));
        assert!(!glob_matches("foo*bar", "foobaz"));
    }
    #[tokio::test]
    async fn token_bucket_burst_then_refill() {
        let cfg = ToolRateLimitConfig {
            rps: 10.0,
            burst: 3,
            essential_deny_on_miss: false,
        };
        let b = TokenBucket::new(&cfg, 0);
        assert!(b.try_acquire(1).await);
        assert!(b.try_acquire(2).await);
        assert!(b.try_acquire(3).await);
        assert!(!b.try_acquire(4).await);
        // Wait ~150ms; rate 10/s → ~1 token refilled.
        tokio::time::sleep(std::time::Duration::from_millis(150)).await;
        assert!(b.try_acquire(5).await);
    }
    #[tokio::test]
    async fn limiter_no_config_always_allows() {
        let rl = ToolRateLimiter::new(ToolRateLimitsConfig::default());
        for _ in 0..100 {
            assert!(rl.try_acquire("kate", "any_tool").await);
        }
    }
    #[tokio::test]
    async fn limiter_selects_first_matching_pattern() {
        let mut patterns = HashMap::new();
        patterns.insert(
            "memory_*".to_string(),
            ToolRateLimitConfig {
                rps: 1.0,
                burst: 1,
                essential_deny_on_miss: false,
            },
        );
        patterns.insert(
            "_default".to_string(),
            ToolRateLimitConfig {
                rps: 100.0,
                burst: 100,
                essential_deny_on_miss: false,
            },
        );
        let rl = ToolRateLimiter::new(ToolRateLimitsConfig { patterns });
        // memory_recall uses memory_* → burst=1
        assert!(rl.try_acquire("kate", "memory_recall").await);
        assert!(!rl.try_acquire("kate", "memory_recall").await);
        // mcp_fs_read hits _default → burst=100
        for _ in 0..50 {
            assert!(rl.try_acquire("kate", "mcp_fs_read").await);
        }
    }
    #[tokio::test]
    async fn limiter_keys_per_agent_tool_independently() {
        let mut patterns = HashMap::new();
        patterns.insert(
            "mcp_*".to_string(),
            ToolRateLimitConfig {
                rps: 1.0,
                burst: 1,
                essential_deny_on_miss: false,
            },
        );
        let rl = ToolRateLimiter::new(ToolRateLimitsConfig { patterns });
        assert!(rl.try_acquire("a", "mcp_fs_read").await);
        assert!(rl.try_acquire("b", "mcp_fs_read").await);
        assert!(rl.try_acquire("a", "mcp_fs_write").await);
        assert!(!rl.try_acquire("a", "mcp_fs_read").await);
    }

    /// Phase 82.7 — per-binding override allocates an independent
    /// bucket so binding A's tight cap does not affect binding B
    /// (which falls back to "no override → unlimited" because its
    /// `per_binding_override` is `None`).
    #[tokio::test]
    async fn per_binding_override_uses_independent_bucket() {
        let rl = ToolRateLimiter::new(ToolRateLimitsConfig::default());

        let mut over_a_patterns = HashMap::new();
        over_a_patterns.insert(
            "marketing_*".into(),
            ToolRateLimitSpec {
                rps: 1.0,
                burst: 1,
                essential_deny_on_miss: false,
            },
        );
        let over_a = ToolRateLimitsConfig {
            patterns: over_a_patterns,
        };

        // Binding A: cap 1, second call denied.
        assert!(
            rl.try_acquire_with_binding("ana", Some("wa:free"), "marketing_send", Some(&over_a))
                .await
        );
        assert!(
            !rl.try_acquire_with_binding("ana", Some("wa:free"), "marketing_send", Some(&over_a))
                .await
        );

        // Binding B: no override → unlimited (separate bucket).
        for _ in 0..50 {
            assert!(
                rl.try_acquire_with_binding("ana", Some("wa:enterprise"), "marketing_send", None)
                    .await
            );
        }
    }

    /// Phase 82.7 — per-binding override fully replaces global
    /// (no fall-through). Tool not in override map → unlimited
    /// even if global pattern would catch it.
    #[tokio::test]
    async fn per_binding_no_match_falls_to_unlimited_no_global_fallthrough() {
        let mut global_patterns = HashMap::new();
        global_patterns.insert(
            "_default".into(),
            ToolRateLimitSpec {
                rps: 1.0,
                burst: 1,
                essential_deny_on_miss: false,
            },
        );
        let rl = ToolRateLimiter::new(ToolRateLimitsConfig {
            patterns: global_patterns,
        });

        let mut over_patterns = HashMap::new();
        over_patterns.insert(
            "marketing_*".into(),
            ToolRateLimitSpec {
                rps: 1.0,
                burst: 1,
                essential_deny_on_miss: false,
            },
        );
        let over = ToolRateLimitsConfig {
            patterns: over_patterns,
        };

        // `read_state` does NOT match override pattern; per-binding
        // semantic = unlimited (NOT fall back to global `_default`).
        for _ in 0..50 {
            assert!(
                rl.try_acquire_with_binding("ana", Some("wa:free"), "read_state", Some(&over))
                    .await
            );
        }
    }

    /// Phase 82.7 — `drop_buckets_for_agent` removes only the
    /// target agent's entries, leaving other agents intact.
    #[tokio::test]
    async fn drop_buckets_for_agent_clears_only_target() {
        let mut patterns = HashMap::new();
        patterns.insert(
            "*".into(),
            ToolRateLimitSpec {
                rps: 1.0,
                burst: 1,
                essential_deny_on_miss: false,
            },
        );
        let rl = ToolRateLimiter::new(ToolRateLimitsConfig { patterns });
        rl.try_acquire("ana", "tool_a").await;
        rl.try_acquire("bob", "tool_a").await;
        assert_eq!(rl.active_buckets(), 2);

        rl.drop_buckets_for_agent("ana");
        assert_eq!(rl.active_buckets(), 1);
        // bob's bucket persists — second acquire still denied (cap 1).
        assert!(!rl.try_acquire("bob", "tool_a").await);
        // ana's bucket cleared — fresh allocation, single token allowed.
        assert!(rl.try_acquire("ana", "tool_a").await);
    }

    /// Phase 82.7 — LRU eviction drops the stalest bucket once
    /// `max_buckets` is reached.
    #[tokio::test]
    async fn lru_eviction_drops_stalest_bucket_at_cap() {
        let mut patterns = HashMap::new();
        patterns.insert(
            "*".into(),
            ToolRateLimitSpec {
                rps: 100.0,
                burst: 10,
                essential_deny_on_miss: false,
            },
        );
        // Cap 2 — third unique key triggers eviction.
        let rl = ToolRateLimiter::with_cap(ToolRateLimitsConfig { patterns }, 2);
        rl.try_acquire("a1", "tool_a").await;
        rl.try_acquire("a2", "tool_a").await;
        assert_eq!(rl.active_buckets(), 2);

        // Touch a2 again to make a1 the stalest.
        rl.try_acquire("a2", "tool_a").await;
        // Allocate third key → a1 evicted.
        rl.try_acquire("a3", "tool_a").await;
        assert_eq!(rl.active_buckets(), 2);
        // Re-acquiring a1 allocates fresh bucket (full burst).
        assert!(rl.try_acquire("a1", "tool_a").await);
    }

    /// Phase 82.7 — `essential_deny_on_miss` makes the next
    /// allocation fail-closed when the bucket was evicted under
    /// LRU pressure. After consuming the deny token, subsequent
    /// allocations proceed normally.
    #[tokio::test]
    async fn essential_deny_on_miss_blocks_after_eviction() {
        let mut patterns = HashMap::new();
        patterns.insert(
            "*".into(),
            ToolRateLimitSpec {
                rps: 100.0,
                burst: 10,
                essential_deny_on_miss: true,
            },
        );
        let rl = ToolRateLimiter::with_cap(ToolRateLimitsConfig { patterns }, 2);
        // Allocate 2 buckets (cap full).
        rl.try_acquire("a1", "marketing_send").await;
        rl.try_acquire("a2", "marketing_send").await;
        // Touch a2 so a1 is stalest.
        rl.try_acquire("a2", "marketing_send").await;
        // Allocate third → a1 evicted with essential flag.
        rl.try_acquire("a3", "marketing_send").await;

        // a1 was essential → next allocation MUST deny once.
        assert!(!rl.try_acquire("a1", "marketing_send").await);
        // Subsequent calls allocate fresh bucket and proceed.
        assert!(rl.try_acquire("a1", "marketing_send").await);
    }

    /// Phase 82.7 — back-compat wrapper preserves pre-82.7
    /// `try_acquire(agent, tool)` semantics: bucket key uses
    /// `None` binding scope (legacy single-tenant cell).
    #[tokio::test]
    async fn legacy_try_acquire_keeps_pre_82_7_behavior() {
        let mut patterns = HashMap::new();
        patterns.insert(
            "tool_a".into(),
            ToolRateLimitSpec {
                rps: 1.0,
                burst: 1,
                essential_deny_on_miss: false,
            },
        );
        let rl = ToolRateLimiter::new(ToolRateLimitsConfig { patterns });
        assert!(rl.try_acquire("ana", "tool_a").await);
        // Wrapper hits same bucket as binding-aware None/None.
        assert!(
            !rl.try_acquire_with_binding("ana", None, "tool_a", None)
                .await
        );
    }
}