rsclaw 2026.5.1

AI Agent Engine Compatible with OpenClaw
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
734
735
736
737
738
739
740
741
742
743
//! Agent registry — maps agent IDs to live `AgentHandle`s.
//!
//! The registry is built at gateway startup from `RuntimeConfig.agents`
//! and is shared (via `Arc`) across all concurrent requests.
//!
//! Internal storage uses `std::sync::RwLock` so that `insert_handle` can be
//! called from outside (e.g. `AgentSpawner`) without requiring `&mut self`.

use std::{
    collections::HashMap,
    sync::Arc,
    sync::atomic::{AtomicBool, AtomicUsize},
    time::Instant,
};

use anyhow::Result;
use tokio::sync::{RwLock, mpsc};

use crate::config::{runtime::RuntimeConfig, schema::AgentEntry};

/// Default maximum concurrent turns per agent when not configured.
const DEFAULT_MAX_CONCURRENT: u32 = 4;

// ---------------------------------------------------------------------------
// AgentHandle
// ---------------------------------------------------------------------------

/// The four kinds of agent in the system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub enum AgentKind {
    /// The default entry point. Cannot be deleted. `default: true` in config.
    Main,
    /// User-created persistent agent. Saved to config file, survives restarts.
    Named,
    /// LLM-spawned temporary agent (`persistent: false`). Lives in memory, gone on restart.
    Sub,
    /// One-shot task agent. Automatically destroyed after completion.
    Task,
}

/// A lightweight handle to a running agent.
///
/// The actual agent loop lives in a tokio task; communication happens
/// through channels. This struct is cheaply `Clone`-able.
#[derive(Clone)]
pub struct AgentHandle {
    pub id: String,
    pub kind: AgentKind,
    pub config: AgentEntry,
    /// Sender to the agent's message queue.
    pub tx: mpsc::Sender<AgentMessage>,
    /// Limits concurrent turns for this agent.
    pub concurrency: Arc<tokio::sync::Semaphore>,
    /// Shared live status for /btw parallel queries.
    pub live_status: Arc<RwLock<crate::agent::runtime::LiveStatus>>,
    /// Provider registry for direct LLM calls (used by /btw bypass).
    pub providers: Arc<crate::provider::registry::ProviderRegistry>,
    /// Per-session abort flags: session_key -> atomic abort flag.
    /// Uses std::sync::RwLock (not tokio) so it can be accessed in Drop impls.
    pub abort_flags: Arc<std::sync::RwLock<HashMap<String, Arc<AtomicBool>>>>,
    /// When this agent handle was created (for /status uptime).
    pub started_at: Instant,
    /// Number of active sessions (updated after each turn for /status).
    pub session_count: Arc<AtomicUsize>,
    /// Per-session context token stats, updated by normal conversation LLM calls only.
    pub session_tokens: Arc<std::sync::RwLock<HashMap<String, SessionTokens>>>,
    /// Total context tokens of the most recent turn (sys + tools + msgs + scratchpad).
    /// Updated by runtime for /status so the number matches what the LLM actually saw.
    pub last_ctx_tokens: Arc<AtomicUsize>,
    /// System prompt tokens of the most recent LLM call.
    pub last_sys_tokens: Arc<AtomicUsize>,
    /// Tool-definition tokens of the most recent LLM call.
    pub last_tools_tokens: Arc<AtomicUsize>,
    /// Message-history tokens of the most recent LLM call.
    pub last_msg_tokens: Arc<AtomicUsize>,
    /// Signal to clear all sessions (set by /clear bypass, consumed by runtime).
    pub clear_signal: Arc<AtomicBool>,
    /// Signal to start a new session (set by /new bypass, consumed by runtime).
    /// Unlike clear_signal, this increments the archive generation.
    pub new_session_signal: Arc<AtomicBool>,
    /// Signal to reset current session (set by /reset bypass, consumed by runtime).
    /// Clears session without summary or generation increment.
    pub reset_signal: Arc<AtomicBool>,
    /// Shared memory store for this agent (used by meditation heartbeat).
    pub memory: Option<Arc<tokio::sync::Mutex<crate::agent::memory::MemoryStore>>>,
    /// Context window in tokens for this agent's primary model. Resolved
    /// at construction time by looking up the model name (e.g.
    /// `brain/brain-model`) in the provider config's `contextWindow`
    /// field, with `agent.model.contextTokens` overriding when set.
    /// Used by /status; 0 when nothing matched and the caller should
    /// fall back to its own default.
    pub context_window: usize,
}

/// Per-session context token statistics.
#[derive(Debug, Clone, Default)]
pub struct SessionTokens {
    /// System prompt tokens.
    pub sys: usize,
    /// Tool definition tokens.
    pub tools: usize,
    /// Message history tokens.
    pub msgs: usize,
    /// Total context tokens (sys + tools + msgs).
    pub total: usize,
}

impl AgentHandle {
    /// Record context token stats for a conversation session.
    pub fn update_session_tokens(&self, session_key: &str, tokens: SessionTokens) {
        if let Ok(mut map) = self.session_tokens.write() {
            map.insert(session_key.to_owned(), tokens);
        }
    }

    /// Remove token stats when a session is cleared.
    pub fn remove_session_tokens(&self, session_key: &str) {
        if let Ok(mut map) = self.session_tokens.write() {
            map.remove(session_key);
        }
    }

    /// Unified status string used by all channels (desktop WS, feishu, telegram, etc.).
    pub fn format_status(&self) -> String {
        use crate::agent::prompt_builder::format_duration;

        let model = self.config.model.as_ref()
            .and_then(|m| m.primary.as_deref())
            .unwrap_or("default");
        let sessions = self.session_count.load(std::sync::atomic::Ordering::Relaxed);
        let uptime = format_duration(self.started_at.elapsed());
        let os = if cfg!(target_os = "macos") { "macOS" }
            else if cfg!(target_os = "linux") {
                if std::env::var("ANDROID_ROOT").is_ok() { "Android" } else { "Linux" }
            }
            else if cfg!(target_os = "windows") { "Windows" }
            else if cfg!(target_os = "ios") { "iOS" }
            else { "Unknown" };
        // Resolution chain (already done at AgentHandle construction):
        //   1. agent.model.contextTokens
        //   2. agents.defaults.contextTokens
        //   3. fallback 64000 below when both are unset (handle.context_window == 0).
        let ctx_limit = if self.context_window > 0 {
            self.context_window
        } else {
            64000
        };

        let mut ctx_lines = String::new();
        if let Ok(map) = self.session_tokens.read() {
            if map.is_empty() {
                ctx_lines.push_str("Context: (no sessions)\n");
            } else {
                for (key, t) in map.iter() {
                    let short_key = if key.len() > 20 { &key[..20] } else { key };
                    let pct = if ctx_limit > 0 { t.total * 100 / ctx_limit } else { 0 };
                    ctx_lines.push_str(&format!(
                        "Context [{short_key}]:\n\
                         \u{A0} system  ~{:.1}k\n\
                         \u{A0} tools   ~{:.1}k\n\
                         \u{A0} msgs    ~{:.1}k\n\
                         \u{A0} total   ~{:.1}k / {:.0}k ({}%)\n",
                        t.sys as f64 / 1000.0,
                        t.tools as f64 / 1000.0,
                        t.msgs as f64 / 1000.0,
                        t.total as f64 / 1000.0,
                        ctx_limit as f64 / 1000.0,
                        pct,
                    ));
                }
            }
        }

        format!(
            "Gateway: running\nOS: {os}\nModel: {model}\nSessions: {sessions}\n\
             {ctx_lines}\
             Uptime: {uptime}\nVersion: rsclaw {}",
            option_env!("RSCLAW_BUILD_VERSION").unwrap_or("dev")
        )
    }
}

/// An image attachment sent by the user.
#[derive(Debug, Clone)]
pub struct ImageAttachment {
    /// Base64-encoded data URI or URL.
    pub data: String,
    /// MIME type (e.g. "image/png", "image/jpeg").
    pub mime_type: String,
}

/// A file attachment sent by the user (raw bytes, not yet processed).
#[derive(Debug, Clone)]
pub struct FileAttachment {
    pub filename: String,
    pub data: Vec<u8>,
    pub mime_type: String,
}

/// A message delivered to an agent.
#[derive(Debug)]
pub struct AgentMessage {
    /// Session key (determines context isolation).
    pub session_key: String,
    /// The user's text.
    pub text: String,
    /// Channel this message arrived on (e.g. "telegram", "discord").
    pub channel: String,
    /// Multi-account tag (e.g. feishu account name) for outbound routing.
    /// `None` for single-account channels.
    pub account: Option<String>,
    /// Peer / user ID (used for session isolation).
    pub peer_id: String,
    /// Chat/conversation ID for replies (for platforms like Feishu where reply ID differs from user ID).
    /// If empty, defaults to peer_id.
    pub chat_id: String,
    /// One-shot sender for the agent's response.
    pub reply_tx: tokio::sync::oneshot::Sender<AgentReply>,
    /// External tool definitions forwarded from the OAI /v1/chat/completions
    /// caller. These are merged into the agent's tool list for the turn.
    pub extra_tools: Vec<crate::provider::ToolDef>,
    /// Image attachments (vision support). Empty for text-only messages.
    pub images: Vec<ImageAttachment>,
    /// File attachments (raw bytes). Empty for text-only messages.
    pub files: Vec<FileAttachment>,
}

/// Extract `[file:path]` references from user text, read the files from disk,
/// and return (cleaned_text, images, files).
///
/// Image files (jpg/png/gif/webp) become `ImageAttachment`; everything else
/// becomes `FileAttachment`. The `[file:...]` markers are removed from the
/// returned text.
pub fn extract_file_refs(text: &str) -> (String, Vec<ImageAttachment>, Vec<FileAttachment>) {
    use base64::Engine as _;
    static RE: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
        regex::Regex::new(r"\[file:([^\]]+)\]").expect("file ref regex")
    });

    let mut images = Vec::new();
    let mut files = Vec::new();

    for cap in RE.captures_iter(text) {
        let path_str = cap[1].trim();
        let path = std::path::Path::new(path_str);
        let Ok(data) = std::fs::read(path) else {
            tracing::warn!(path = path_str, "file ref: cannot read file");
            continue;
        };

        let lower = path_str.to_lowercase();
        let is_image = lower.ends_with(".jpg")
            || lower.ends_with(".jpeg")
            || lower.ends_with(".png")
            || lower.ends_with(".gif")
            || lower.ends_with(".webp");

        if is_image {
            let mime = if lower.ends_with(".png") {
                "image/png"
            } else if lower.ends_with(".gif") {
                "image/gif"
            } else if lower.ends_with(".webp") {
                "image/webp"
            } else {
                "image/jpeg"
            };
            let b64 = base64::engine::general_purpose::STANDARD.encode(&data);
            images.push(ImageAttachment {
                data: format!("data:{mime};base64,{b64}"),
                mime_type: mime.to_owned(),
            });
        } else {
            let filename = path
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_else(|| path_str.to_owned());
            let mime = match lower.rsplit('.').next() {
                Some("pdf") => "application/pdf",
                Some("docx") => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                Some("xlsx") => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                Some("pptx") => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
                Some("txt" | "md" | "rs" | "py" | "js" | "ts" | "json" | "toml" | "yaml" | "yml") => "text/plain",
                Some("mp4") => "video/mp4",
                Some("mp3") => "audio/mpeg",
                Some("wav") => "audio/wav",
                _ => "application/octet-stream",
            }.to_owned();
            files.push(FileAttachment {
                filename,
                data,
                mime_type: mime,
            });
        }
    }

    let cleaned = RE.replace_all(text, "").to_string();
    let cleaned = cleaned.trim().to_owned();
    (cleaned, images, files)
}

/// Deferred file analysis that the per-user worker should process after
/// sending the initial "analyzing..." reply.
#[derive(Debug)]
pub struct PendingAnalysis {
    /// The extracted text to send to the LLM for analysis.
    pub text: String,
    /// Session key for the follow-up agent call.
    pub session_key: String,
    /// Channel name (e.g. "telegram", "discord").
    pub channel: String,
    /// Peer / user ID.
    pub peer_id: String,
}

/// Reply from an agent.
#[derive(Debug)]
pub struct AgentReply {
    /// Final text output (may be empty if the agent used messaging tools).
    pub text: String,
    /// Whether the agent produced any output at all.
    pub is_empty: bool,
    /// If the LLM called an external (caller-provided) tool, this carries the
    /// tool_calls payload in OpenAI wire format so the OAI handler can return
    /// it.
    pub tool_calls: Option<Vec<serde_json::Value>>,
    /// Images to send (base64 data URIs or file paths).
    pub images: Vec<String>,
    /// File attachments: Vec<(filename, mime_type, file_path_or_url)>.
    pub files: Vec<(String, String, String)>,
    /// If set, the worker should send a follow-up LLM analysis after the
    /// immediate reply.
    pub pending_analysis: Option<PendingAnalysis>,
    /// True when the runtime's outer dispatcher should emit a `done=true`
    /// event_bus frame after this reply.
    ///
    /// Set to `true` for every reply path that bypasses `agent_loop` (preparse
    /// short-circuits, file-attachment short-circuits, `__DIRECT_REPLY__`,
    /// `/btw` side queries, disk-low responses, etc.) since those paths
    /// don't emit a streaming terminator themselves. `agent_loop` emits its
    /// own done frame internally for normal LLM turns and its own
    /// early-return paths (clear_signal, abort, max_iterations, error_streak,
    /// loop-detected), so it sets this to `false`.
    ///
    /// Without this flag, WS subscribers (the desktop chat UI) wait forever
    /// for the terminator and the input freezes.
    pub needs_outer_done_emit: bool,
}

// ---------------------------------------------------------------------------
// AgentRegistry
// ---------------------------------------------------------------------------

struct RegistryInner {
    agents: HashMap<String, Arc<AgentHandle>>,
    /// ID of the default agent (first with `default: true`, else first entry).
    default_id: Option<String>,
}

pub struct AgentRegistry {
    inner: std::sync::RwLock<RegistryInner>,
}

impl Default for AgentRegistry {
    fn default() -> Self {
        Self {
            inner: std::sync::RwLock::new(RegistryInner {
                agents: HashMap::new(),
                default_id: None,
            }),
        }
    }
}

impl AgentRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    /// Build a registry from `RuntimeConfig`. Does not spawn actual agent loops
    /// (those are started by the gateway in W6+).
    pub fn from_config(cfg: &RuntimeConfig) -> Self {
        let providers = Arc::new(crate::provider::registry::ProviderRegistry::new());
        let (registry, _) = Self::from_config_with_receivers(cfg, providers);
        registry
    }

    /// Build a registry AND return the mpsc receivers for each agent so the
    /// gateway can spawn real runtime tasks.
    pub fn from_config_with_receivers(
        cfg: &RuntimeConfig,
        providers: Arc<crate::provider::registry::ProviderRegistry>,
    ) -> (Self, HashMap<String, mpsc::Receiver<AgentMessage>>) {
        let registry = Self::new();
        let mut receivers = HashMap::new();

        // If agents.list is empty (e.g. openclaw.json with no explicit list),
        // synthesize a default agent from agents.defaults.
        let agent_list = if cfg.agents.list.is_empty() {
            let model = cfg.agents.defaults.model.clone();
            let workspace = cfg.agents.defaults.workspace.clone();
            // Inherit ACP configs from defaults
            let opencode = cfg.agents.defaults.opencode.clone();
            let claudecode = cfg.agents.defaults.claudecode.clone();
            let codex = cfg.agents.defaults.codex.clone();
            vec![crate::config::schema::AgentEntry {
                id: "main".to_owned(),
                default: Some(true),
                name: Some("Main Agent".to_owned()),
                workspace,
                model,
                flash_model: None,
                lane: None,
                lane_concurrency: None,
                group_chat: None,
                channels: None,
                commands: None,
                allowed_commands: None,
                opencode,
                claudecode,
                codex,
                agent_dir: None,
                system: None,
                temperature: None,
            }]
        } else {
            cfg.agents.list.clone()
        };

        let max_concurrent = cfg
            .agents
            .defaults
            .max_concurrent
            .unwrap_or(DEFAULT_MAX_CONCURRENT) as usize;

        {
            let mut inner = registry.inner.write().expect("agent registry lock poisoned");

            for entry in &agent_list {
                let (tx, rx) = mpsc::channel::<AgentMessage>(32);
                let permits = entry
                    .lane_concurrency
                    .map(|n| n as usize)
                    .unwrap_or(max_concurrent);
                let kind = if entry.default == Some(true) {
                    AgentKind::Main
                } else {
                    AgentKind::Named
                };
                // Context window resolution: per-agent model.contextTokens
                // wins, then agents.defaults.contextTokens. 0 = unset, the
                // /status formatter falls back to its own default.
                let context_window = entry
                    .model
                    .as_ref()
                    .and_then(|m| m.context_tokens)
                    .or(cfg.agents.defaults.context_tokens)
                    .unwrap_or(0) as usize;
                let handle = Arc::new(AgentHandle {
                    id: entry.id.clone(),
                    kind,
                    config: entry.clone(),
                    tx,
                    concurrency: Arc::new(tokio::sync::Semaphore::new(permits)),
                    live_status: Arc::new(
                        RwLock::new(crate::agent::runtime::LiveStatus::default()),
                    ),
                    providers: Arc::clone(&providers),
                    abort_flags: Arc::new(std::sync::RwLock::new(HashMap::new())),
                    started_at: Instant::now(),
                    session_count: Arc::new(AtomicUsize::new(0)),
                    session_tokens: Arc::new(std::sync::RwLock::new(HashMap::new())),
                    last_ctx_tokens: Arc::new(AtomicUsize::new(0)),
                    last_sys_tokens: Arc::new(AtomicUsize::new(0)),
                    last_tools_tokens: Arc::new(AtomicUsize::new(0)),
                    last_msg_tokens: Arc::new(AtomicUsize::new(0)),
                    clear_signal: Arc::new(AtomicBool::new(false)),
                    new_session_signal: Arc::new(AtomicBool::new(false)),
                    reset_signal: Arc::new(AtomicBool::new(false)),
                    memory: None, // populated by agent runtime after memory store opens
                    context_window,
                });
                inner.agents.insert(entry.id.clone(), handle);
                receivers.insert(entry.id.clone(), rx);

                if entry.default == Some(true) && inner.default_id.is_none() {
                    inner.default_id = Some(entry.id.clone());
                }
            }

            // If no explicit default, use the first agent.
            if inner.default_id.is_none()
                && let Some(id) = inner.agents.keys().next().cloned()
            {
                inner.default_id = Some(id);
            }
        }

        (registry, receivers)
    }

    /// Insert a dynamically spawned agent handle.
    pub fn insert_handle(&self, handle: Arc<AgentHandle>) {
        let mut inner = self.inner.write().expect("agent registry lock poisoned");
        inner.agents.insert(handle.id.clone(), handle);
    }

    /// Remove an agent handle by ID (used for task agents after completion).
    pub fn remove_handle(&self, id: &str) {
        let mut inner = self.inner.write().expect("agent registry lock poisoned");
        inner.agents.remove(id);
    }

    /// Look up an agent by ID.
    pub fn get(&self, id: &str) -> Result<Arc<AgentHandle>> {
        self.inner
            .read()
            .expect("agent registry lock poisoned")
            .agents
            .get(id)
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("agent not found: `{id}`"))
    }

    /// Return the default agent.
    pub fn default_agent(&self) -> Result<Arc<AgentHandle>> {
        let inner = self.inner.read().expect("agent registry lock poisoned");
        let id = inner
            .default_id
            .as_deref()
            .ok_or_else(|| anyhow::anyhow!("no default agent configured"))?
            .to_owned();
        drop(inner);
        self.get(&id)
    }

    /// Route a message to the correct agent based on channel binding.
    ///
    /// Routing priority:
    ///   1. Agents with `channels` containing this channel name.
    ///   2. Default agent (fallback).
    pub fn route(&self, channel: &str) -> Result<Arc<AgentHandle>> {
        self.route_account(channel, None)
    }

    /// Route a message to the correct agent for a specific channel account.
    ///
    /// Routing priority:
    ///   1. Agents with `channels` containing `"channel:account"` (exact match).
    ///   2. Agents with `channels` containing `"channel"` (bare channel match).
    ///   3. Default agent (fallback).
    pub fn route_account(&self, channel: &str, account: Option<&str>) -> Result<Arc<AgentHandle>> {
        let inner = self.inner.read().expect("agent registry lock poisoned");

        // Build the qualified key "channel:account" for exact matching.
        let qualified = account.map(|a| format!("{channel}:{a}"));

        // Find agents explicitly bound to this channel+account.
        // Prefer exact "channel:account" match over bare "channel".
        let mut exact: Vec<Arc<AgentHandle>> = Vec::new();
        let mut bare: Vec<Arc<AgentHandle>> = Vec::new();

        for a in inner.agents.values() {
            let Some(chs) = a.config.channels.as_ref() else {
                continue;
            };
            if let Some(q) = &qualified {
                if chs.iter().any(|c| c == q) {
                    exact.push(Arc::clone(a));
                    continue;
                }
            }
            if chs.iter().any(|c| c == channel) {
                bare.push(Arc::clone(a));
            }
        }

        let bound = if !exact.is_empty() { exact } else { bare };

        match bound.len() {
            0 => {
                let id = inner
                    .default_id
                    .as_deref()
                    .ok_or_else(|| anyhow::anyhow!("no default agent configured"))?
                    .to_owned();
                drop(inner);
                self.get(&id)
            }
            1 => Ok(Arc::clone(&bound[0])),
            _ => {
                // Multiple matches — use the one with the lowest-alphabetical ID as tie-break.
                let winner = bound
                    .into_iter()
                    .min_by_key(|a| a.id.clone())
                    .expect("non-empty");
                Ok(winner)
            }
        }
    }

    pub fn len(&self) -> usize {
        self.inner.read().expect("agent registry lock poisoned").agents.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.read().expect("agent registry lock poisoned").agents.is_empty()
    }

    pub fn all(&self) -> Vec<Arc<AgentHandle>> {
        self.inner
            .read()
            .expect("agent registry lock poisoned")
            .agents
            .values()
            .cloned()
            .collect()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{
        runtime::{
            AgentsRuntime, ChannelRuntime, ExtRuntime, GatewayRuntime, ModelRuntime, OpsRuntime,
            RuntimeConfig,
        },
        schema::{AgentEntry, BindMode, GatewayMode, ReloadMode, SessionConfig},
    };

    fn make_runtime(agents: Vec<AgentEntry>) -> RuntimeConfig {
        RuntimeConfig {
            gateway: GatewayRuntime {
                port: 18888,
                mode: GatewayMode::Local,
                bind: BindMode::Loopback,
                bind_address: None,
                reload: ReloadMode::Hybrid,
                auth_token: None,
                allow_tailscale: false,
                channel_health_check_minutes: 5,
                channel_stale_event_threshold_minutes: 30,
                channel_max_restarts_per_hour: 10,
                auth_token_configured: false,
                auth_token_is_plaintext: false,
                user_agent: None,
                language: None,
            },
            agents: AgentsRuntime {
                defaults: Default::default(),
                list: agents,
                bindings: vec![],
                external: vec![],
            },
            channel: ChannelRuntime {
                channels: Default::default(),
                session: SessionConfig {
                    dm_scope: None,
                    thread_bindings: None,
                    reset: None,
                    identity_links: None,
                    maintenance: None,
                },
            },
            model: ModelRuntime {
                models: None,
                auth: None,
            },
            ext: ExtRuntime {
                tools: None,
                skills: None,
                plugins: None,
                evolution: None,
            },
            ops: OpsRuntime {
                cron: None,
                hooks: None,
                sandbox: None,
                logging: None,
                secrets: None,
            },
            raw: Default::default(),
        }
    }

    fn entry(id: &str, default: bool, channels: Option<Vec<&str>>) -> AgentEntry {
        AgentEntry {
            id: id.to_owned(),
            default: if default { Some(true) } else { None },
            workspace: None,
            model: None,
            flash_model: None,
            lane: None,
            lane_concurrency: None,
            group_chat: None,
            channels: channels.map(|v| v.into_iter().map(str::to_owned).collect()),
            name: None,
            commands: None,
            allowed_commands: None,
            opencode: None,
            claudecode: None,
            codex: None,
            agent_dir: None,
            system: None,
            temperature: None,
        }
    }

    #[test]
    fn routes_to_default_when_no_binding() {
        let cfg = make_runtime(vec![
            entry("main", true, None),
            entry("telegram_bot", false, Some(vec!["telegram"])),
        ]);
        let reg = AgentRegistry::from_config(&cfg);
        let agent = reg.route("slack").expect("route");
        assert_eq!(agent.id, "main");
    }

    #[test]
    fn routes_to_bound_agent() {
        let cfg = make_runtime(vec![
            entry("main", true, None),
            entry("tgbot", false, Some(vec!["telegram"])),
        ]);
        let reg = AgentRegistry::from_config(&cfg);
        let agent = reg.route("telegram").expect("route");
        assert_eq!(agent.id, "tgbot");
    }

    #[test]
    fn get_by_id() {
        let cfg = make_runtime(vec![entry("alpha", true, None)]);
        let reg = AgentRegistry::from_config(&cfg);
        assert!(reg.get("alpha").is_ok());
        assert!(reg.get("nonexistent").is_err());
    }
}