talahon 2.0.1

Talon — automated AI SOC analyst (host orchestrator)
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
//! The host orchestrator.
//!
//! [`Orchestrator`] owns the host's in-memory state (registered lanes,
//! session ids, message cursors) and implements
//! every host-side callback trait:
//!
//! - `talon_core::channel::ChannelHost` — inbound messages, lane
//!   registration, session reset, queue status ([`host`]);
//! - `talon_ipc::IpcHost` — authorized container requests ([`host`]);
//! - `talon_scheduler::LaneProcessor` / `TaskExecutor` / `GroupDirectory`
//!   — lane jobs and scheduled tasks ([`host`], [`tasks`]);
//! - `talon_router::SessionCommandDeps` — `/new`, `/compact` ([`commands`]).
//!
//! The message pipeline lives in [`messages`], agent runs (sessions, LLM
//! tokens, snapshots) in [`run`].

pub mod commands;
pub mod host;
pub mod messages;
pub mod run;
pub mod tasks;

#[cfg(test)]
mod tests;

use std::collections::{BTreeMap, HashMap, HashSet};
use std::os::fd::{AsFd, BorrowedFd};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError, RwLock};
use std::time::Duration;

use talon_core::audit::SharedAuditSink;
use talon_core::channel::Channel;
use talon_core::config::TalonConfig;
use talon_core::paths::group_folder_path;
use talon_core::protocol::SessionUsage;
use talon_core::types::{ChatMetadata, NewMessage, RegisteredGroup};
use talon_core::{Error, Result};
use talon_db::Db;
use talon_scheduler::{GroupQueue, TaskScheduler};
use talon_security::redact::Redactor;
use tokio::sync::Notify;
use tokio::time::Instant;

use crate::agent::{ContainerLauncher, LlmAccess};
use crate::allowlist::AllowlistSource;
use crate::output_limits::OutputLimits;
use crate::safe_fs;

/// Router-state key of the global "seen" cursor.
pub const LAST_TIMESTAMP_KEY: &str = "last_timestamp";
/// Router-state key of the per-lane agent cursors (JSON object).
pub const LAST_AGENT_TIMESTAMP_KEY: &str = "last_agent_timestamp";
/// Per-group memory file bootstrapped from the `main` / `global` template.
const CLAUDE_MD: &str = "CLAUDE.md";
/// Largest `CLAUDE.md` template the host copies.
const MAX_TEMPLATE_BYTES: u64 = 256 * 1024;

/// Tunables that are not part of `TalonConfig`.
#[derive(Debug, Clone)]
pub struct Settings {
    /// Close a scheduled task's container this long after its result.
    pub task_close_delay: Duration,
    /// Close a `/compact` container this long after it finished.
    pub command_close_delay: Duration,
    /// Added to the run's lifetime when sizing the LLM token TTL.
    pub token_ttl_margin: Duration,
    /// Shutdown grace for running lane jobs.
    pub shutdown_grace: Duration,
    /// Caps on agent output (MYTH-07).
    pub output_limits: OutputLimits,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            task_close_delay: Duration::from_secs(10),
            command_close_delay: Duration::from_secs(10),
            token_ttl_margin: Duration::from_secs(5 * 60),
            shutdown_grace: Duration::from_secs(10),
            output_limits: OutputLimits::default(),
        }
    }
}

/// Everything [`Orchestrator::new`] needs.
pub struct OrchestratorDeps {
    /// Resolved host configuration.
    pub config: Arc<TalonConfig>,
    /// Database handle.
    pub db: Db,
    /// The lane queue (processor is set by the caller afterwards).
    pub queue: GroupQueue,
    /// Container launcher.
    pub launcher: Arc<dyn ContainerLauncher>,
    /// LLM credentials; `None` when vLLM is not configured.
    pub llm: Option<Arc<dyn LlmAccess>>,
    /// Sender allowlist.
    pub allowlist: AllowlistSource,
    /// Redactor for outbound text and stored error messages.
    pub redactor: Arc<Redactor>,
    /// Audit sink.
    pub audit: SharedAuditSink,
    /// Tunables.
    pub settings: Settings,
}

/// Mutable orchestrator state.
#[derive(Debug, Default)]
pub(crate) struct State {
    /// Registered lanes (persisted + in-memory), by JID.
    pub(crate) groups: HashMap<String, RegisteredGroup>,
    /// JIDs whose registration lives in the database.
    pub(crate) persisted: HashSet<String>,
    /// Session key -> session id.
    pub(crate) sessions: HashMap<String, String>,
    /// High-water mark of messages seen by the message loop.
    pub(crate) last_timestamp: String,
    /// Per-lane cursor: newest message handed to the agent.
    pub(crate) last_agent_timestamp: BTreeMap<String, String>,
    /// Lanes with a running message container: when its LLM token expires.
    pub(crate) token_deadlines: HashMap<String, Instant>,
    /// Lanes with a running message container: its LLM token and TTL, so a
    /// piped follow-up can extend the token instead of recycling the run.
    pub(crate) run_tokens: HashMap<String, (String, Duration)>,
}

/// The host orchestrator. Share it as `Arc<Orchestrator>`.
pub struct Orchestrator {
    pub(crate) config: Arc<TalonConfig>,
    pub(crate) settings: Settings,
    pub(crate) db: Db,
    pub(crate) queue: GroupQueue,
    pub(crate) launcher: Arc<dyn ContainerLauncher>,
    pub(crate) llm: Option<Arc<dyn LlmAccess>>,
    pub(crate) allowlist: AllowlistSource,
    pub(crate) redactor: Arc<Redactor>,
    pub(crate) audit: SharedAuditSink,
    pub(crate) state: Mutex<State>,
    channels: RwLock<Vec<Arc<dyn Channel>>>,
    scheduler: OnceLock<Arc<TaskScheduler>>,
    pub(crate) wake: Notify,
    accepting: AtomicBool,
    pub(crate) loop_running: AtomicBool,
}

impl std::fmt::Debug for Orchestrator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Orchestrator")
            .field("assistant", &self.config.assistant_name)
            .field("queue", &self.queue)
            .finish_non_exhaustive()
    }
}

impl Orchestrator {
    /// Build the orchestrator and load its state from the database.
    ///
    /// # Errors
    /// Database read failures.
    pub fn new(deps: OrchestratorDeps) -> Result<Arc<Self>> {
        let state = load_state(&deps.db)?;
        tracing::info!(group_count = state.groups.len(), "State loaded");
        Ok(Arc::new(Self {
            config: deps.config,
            settings: deps.settings,
            db: deps.db,
            queue: deps.queue,
            launcher: deps.launcher,
            llm: deps.llm,
            allowlist: deps.allowlist,
            redactor: deps.redactor,
            audit: deps.audit,
            state: Mutex::new(state),
            channels: RwLock::new(Vec::new()),
            scheduler: OnceLock::new(),
            wake: Notify::new(),
            accepting: AtomicBool::new(true),
            loop_running: AtomicBool::new(false),
        }))
    }

    pub(crate) fn lock(&self) -> MutexGuard<'_, State> {
        // Critical sections never call foreign code; keep serving after a
        // panic elsewhere rather than cascading.
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// The host configuration.
    pub fn config(&self) -> &TalonConfig {
        &self.config
    }

    /// The lane queue.
    pub fn queue(&self) -> &GroupQueue {
        &self.queue
    }

    /// The database handle.
    pub fn db(&self) -> &Db {
        &self.db
    }

    /// Install the connected channels (they need the host to be built
    /// first, hence the late binding).
    pub fn set_channels(&self, channels: Vec<Arc<dyn Channel>>) {
        *self
            .channels
            .write()
            .unwrap_or_else(PoisonError::into_inner) = channels;
    }

    /// Snapshot of the channels.
    pub fn channels(&self) -> Vec<Arc<dyn Channel>> {
        self.channels
            .read()
            .unwrap_or_else(PoisonError::into_inner)
            .clone()
    }

    /// The first channel owning `jid`.
    pub fn find_channel(&self, jid: &str) -> Option<Arc<dyn Channel>> {
        let channels = self.channels.read().unwrap_or_else(PoisonError::into_inner);
        talon_router::find_channel(&channels, jid).cloned()
    }

    /// Install the task scheduler (built after the orchestrator because the
    /// orchestrator is its executor).
    pub fn set_scheduler(&self, scheduler: Arc<TaskScheduler>) {
        if self.scheduler.set(scheduler).is_err() {
            tracing::warn!("task scheduler already installed; ignoring");
        }
    }

    /// The task scheduler, once installed.
    pub fn scheduler(&self) -> Option<&Arc<TaskScheduler>> {
        self.scheduler.get()
    }

    /// Wake the scheduler (tasks changed out of band).
    pub fn wake_scheduler(&self) {
        if let Some(s) = self.scheduler.get() {
            s.wake();
        }
    }

    /// Wake the message loop.
    pub fn wake_message_loop(&self) {
        self.wake.notify_one();
    }

    /// Stop starting new work (shutdown). Inbound messages are still stored
    /// so they are recovered on the next start.
    pub fn stop_accepting(&self) {
        self.accepting.store(false, Ordering::SeqCst);
        self.wake.notify_one();
    }

    /// Whether new work is accepted.
    pub fn is_accepting(&self) -> bool {
        self.accepting.load(Ordering::SeqCst)
    }

    /// Snapshot of all registered lanes keyed by JID.
    pub fn groups_snapshot(&self) -> HashMap<String, RegisteredGroup> {
        self.lock().groups.clone()
    }

    /// The lane registered for `jid`.
    pub fn group(&self, jid: &str) -> Option<RegisteredGroup> {
        self.lock().groups.get(jid).cloned()
    }

    /// Current session id stored for `key`.
    pub fn session_for_key(&self, key: &str) -> Option<String> {
        self.lock().sessions.get(key).cloned()
    }

    // ---- persistence -----------------------------------------------------

    /// Persist both cursors. Failures are logged.
    pub(crate) fn save_state(&self) {
        let (last, agent) = {
            let st = self.lock();
            (
                st.last_timestamp.clone(),
                serde_json::to_string(&st.last_agent_timestamp),
            )
        };
        if let Err(e) = self.db.set_router_state(LAST_TIMESTAMP_KEY, &last) {
            tracing::error!(error = %e, "failed to save last_timestamp");
        }
        match agent {
            Ok(json) => {
                if let Err(e) = self.db.set_router_state(LAST_AGENT_TIMESTAMP_KEY, &json) {
                    tracing::error!(error = %e, "failed to save last_agent_timestamp");
                }
            }
            Err(e) => tracing::error!(error = %e, "failed to encode last_agent_timestamp"),
        }
    }

    /// The lane's agent cursor without recovery (`''` when unset).
    pub fn cursor(&self, jid: &str) -> String {
        self.lock()
            .last_agent_timestamp
            .get(jid)
            .cloned()
            .unwrap_or_default()
    }

    /// Set the lane's agent cursor and persist.
    pub(crate) fn set_cursor(&self, jid: &str, ts: &str) {
        self.lock()
            .last_agent_timestamp
            .insert(jid.to_string(), ts.to_string());
        self.save_state();
    }

    /// The lane's cursor, recovering it from the last bot reply when it is
    /// missing (new lane, corrupted state, restart).
    pub(crate) fn get_or_recover_cursor(&self, jid: &str) -> String {
        let existing = self.cursor(jid);
        if !existing.is_empty() {
            return existing;
        }
        match self
            .db
            .get_last_bot_message_timestamp(jid, &self.config.assistant_name)
        {
            Ok(Some(bot_ts)) if !bot_ts.is_empty() => {
                tracing::info!(chat_jid = jid, recovered_from = %bot_ts, "Recovered message cursor from last bot reply");
                self.set_cursor(jid, &bot_ts);
                bot_ts
            }
            Ok(_) => String::new(),
            Err(e) => {
                tracing::warn!(chat_jid = jid, error = %e, "cursor recovery failed");
                String::new()
            }
        }
    }

    /// Record a lane's session. Ephemeral lanes keep nothing.
    pub(crate) fn persist_session(
        &self,
        group: &RegisteredGroup,
        session_id: &str,
        fingerprint: &str,
        usage: Option<&SessionUsage>,
    ) {
        if !group.should_persist_session() || session_id.is_empty() {
            return;
        }
        let key = group.session_key().to_string();
        self.lock()
            .sessions
            .insert(key.clone(), session_id.to_string());
        if let Err(e) = self.db.set_session(&key, session_id, Some(fingerprint)) {
            tracing::error!(group = %group.name, error = %e, "failed to persist session");
            return;
        }
        // Advisory: a bad usage write never fails the turn.
        let context = usage.map(SessionUsage::context_size).unwrap_or(0);
        if context > 0 {
            if let Err(e) = self.db.record_session_usage(&key, context) {
                tracing::warn!(group = %group.name, error = %e, "Failed to record session usage");
            }
        }
    }

    /// Drop a lane's session in memory *and* in the database.
    pub(crate) fn clear_session_for(&self, group: &RegisteredGroup) {
        let key = group.session_key().to_string();
        let had = self.lock().sessions.remove(&key).is_some();
        if let Err(e) = self.db.delete_session(&key) {
            tracing::error!(group = %group.name, error = %e, "failed to delete session");
        }
        if had {
            self.audit.record(talon_core::audit::event(
                "session.cleared",
                &group.folder,
                Some(&key),
                "info",
                serde_json::json!({ "group": group.name }),
            ));
        }
    }

    /// Store an inbound message, creating a minimal chat row if the
    /// channel never reported the chat (foreign key).
    pub(crate) fn store_message(&self, msg: &NewMessage) {
        if self.db.store_message(msg).is_ok() {
            return;
        }
        let meta = ChatMetadata {
            chat_jid: msg.chat_jid.clone(),
            timestamp: msg.timestamp.clone(),
            name: None,
            channel: None,
            is_group: None,
        };
        let result = self
            .db
            .store_chat_metadata(&meta)
            .and_then(|()| self.db.store_message(msg));
        if let Err(e) = result {
            tracing::error!(chat_jid = %msg.chat_jid, error = %e, "failed to store message");
        }
    }

    /// Register (or update) a lane.
    ///
    /// - The folder must be valid (else the registration is refused).
    /// - `persist`: write to `registered_groups`; when another JID already
    ///   owns the folder (lanes sharing a folder, e.g. `http:copilot:*`) the
    ///   lane stays in memory.
    /// - `create_template`: create `logs/` and copy the `CLAUDE.md` template
    ///   (used for IPC registrations).
    pub(crate) fn register_group_inner(
        &self,
        jid: &str,
        group: RegisteredGroup,
        persist: bool,
        create_template: bool,
    ) -> Result<()> {
        let group_dir = match group_folder_path(&self.config.groups_dir, &group.folder) {
            Ok(dir) => dir,
            Err(e) => {
                tracing::warn!(jid, folder = %group.folder, error = %e, "Rejecting group registration with invalid folder");
                return Err(e);
            }
        };
        for key in [group.session_key.as_deref(), group.ipc_key.as_deref()]
            .into_iter()
            .flatten()
        {
            talon_core::paths::assert_valid_group_folder(key)?;
        }

        let mut persisted = false;
        if persist {
            match self.db.set_registered_group(jid, &group) {
                Ok(()) => persisted = true,
                Err(Error::InvalidInput(reason)) => {
                    tracing::debug!(jid, %reason, "lane shares its folder; keeping it in memory");
                }
                Err(e) => {
                    tracing::error!(jid, error = %e, "failed to persist group registration");
                    return Err(e);
                }
            }
        }
        {
            let mut st = self.lock();
            if persisted {
                st.persisted.insert(jid.to_string());
            }
            st.groups.insert(jid.to_string(), group.clone());
        }

        self.prepare_group_dir(&group_dir, &group, create_template);
        tracing::info!(jid, name = %group.name, folder = %group.folder, persisted, "Group registered");
        Ok(())
    }

    /// Create `groups/<folder>/logs` and, with `create_template`, bootstrap
    /// `CLAUDE.md`. Group folders are agent-writable, so everything below
    /// `groups/` is handled descriptor-relatively without following
    /// symlinks (MYTH-10): a planted `groups/<folder>` or `logs` symlink is
    /// refused instead of redirecting the host's writes.
    fn prepare_group_dir(&self, group_dir: &Path, group: &RegisteredGroup, create_template: bool) {
        let groups_dir = &self.config.groups_dir;
        let prepared = std::fs::create_dir_all(groups_dir)
            .and_then(|()| safe_fs::open_root(groups_dir))
            .and_then(|groups| {
                let dir = safe_fs::ensure_dir_at(groups.as_fd(), &group.folder)?;
                Ok((groups, dir))
            });
        let (groups, dir) = match prepared {
            Ok(fds) => fds,
            Err(e) => {
                tracing::warn!(folder = %group.folder, dir = %group_dir.display(), error = %e, "cannot create group folder");
                return;
            }
        };
        if let Err(e) = safe_fs::ensure_dir_at(dir.as_fd(), "logs") {
            tracing::warn!(folder = %group.folder, error = %e, "cannot create group logs folder");
        }
        if create_template {
            self.install_claude_md(groups.as_fd(), dir.as_fd(), group);
        }
    }

    /// Copy the `CLAUDE.md` template into a new group folder. Never follows or replaces an existing entry (the folder is
    /// agent-writable, so a planted symlink must not redirect the write:
    /// the file is created with `O_CREAT | O_EXCL | O_NOFOLLOW`), and the
    /// template itself (`groups/main` is writable by the main agent) is
    /// read only when it is a regular, size-capped file.
    fn install_claude_md(
        &self,
        groups: BorrowedFd<'_>,
        dir: BorrowedFd<'_>,
        group: &RegisteredGroup,
    ) {
        match safe_fs::entry_exists_at(dir, CLAUDE_MD) {
            Ok(false) => {}
            Ok(true) => return,
            Err(e) => {
                tracing::warn!(folder = %group.folder, error = %e, "cannot inspect CLAUDE.md");
                return;
            }
        }
        let template_folder = if group.is_main { "main" } else { "global" };
        let Some(mut content) = read_template(groups, template_folder) else {
            return;
        };
        let name = &self.config.assistant_name;
        if name != "Talon" {
            content = content
                .lines()
                .map(|l| {
                    if l == "# Talon" {
                        format!("# {name}")
                    } else {
                        l.to_string()
                    }
                })
                .collect::<Vec<_>>()
                .join("\n")
                + if content.ends_with('\n') { "\n" } else { "" };
            content = content.replace("You are Talon", &format!("You are {name}"));
        }
        match safe_fs::create_new_at(dir, CLAUDE_MD, content.as_bytes()) {
            Ok(()) => tracing::info!(folder = %group.folder, "Created CLAUDE.md from template"),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
                tracing::debug!(folder = %group.folder, "CLAUDE.md appeared concurrently; kept");
            }
            Err(e) => tracing::warn!(folder = %group.folder, error = %e, "cannot create CLAUDE.md"),
        }
    }

    /// Drop an in-memory lane (never a persisted one).
    pub(crate) fn unregister_lane(&self, jid: &str) -> bool {
        {
            let mut st = self.lock();
            if st.persisted.contains(jid) || st.groups.remove(jid).is_none() {
                return false;
            }
            st.last_agent_timestamp.remove(jid);
        }
        self.save_state();
        true
    }

    /// Redact, format for the owning channel's dialect and send. Returns
    /// whether anything was sent (empty after stripping `<internal>`).
    ///
    /// # Errors
    /// No connected channel owns `jid`, or the channel failed.
    pub async fn send_outbound(&self, jid: &str, raw: &str) -> Result<bool> {
        let redacted = self.redactor.redact(raw);
        let channels = self.channels();
        talon_router::send_formatted(&channels, jid, &redacted).await
    }
}

/// Read `groups/<template_folder>/CLAUDE.md`: neither the folder nor the
/// file may be a symlink, the file must be a regular, size-capped UTF-8
/// file (a FIFO cannot block the open). Missing templates are silent.
fn read_template(groups: BorrowedFd<'_>, template_folder: &str) -> Option<String> {
    let folder = match safe_fs::open_dir_at(groups, template_folder) {
        Ok(fd) => fd,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return None,
        Err(e) => {
            tracing::warn!(template = template_folder, error = %e, "unsafe CLAUDE.md template folder; not copied");
            return None;
        }
    };
    let file = match safe_fs::open_regular_at(folder.as_fd(), CLAUDE_MD, MAX_TEMPLATE_BYTES) {
        Ok((file, _)) => file,
        Err(safe_fs::OpenRefusal::Missing) => return None,
        Err(safe_fs::OpenRefusal::Rejected(reason)) => {
            tracing::warn!(template = template_folder, %reason, "unsafe CLAUDE.md template; not copied");
            return None;
        }
    };
    match safe_fs::read_capped(file, MAX_TEMPLATE_BYTES).map(String::from_utf8) {
        Ok(Ok(text)) => Some(text),
        Ok(Err(_)) => {
            tracing::warn!(
                template = template_folder,
                "CLAUDE.md template is not UTF-8; not copied"
            );
            None
        }
        Err(e) => {
            tracing::warn!(template = template_folder, error = %e, "cannot read CLAUDE.md template");
            None
        }
    }
}

/// Load cursors, sessions and registered groups from the database.
fn load_state(db: &Db) -> Result<State> {
    let last_timestamp = db.get_router_state(LAST_TIMESTAMP_KEY)?.unwrap_or_default();
    let last_agent_timestamp = match db.get_router_state(LAST_AGENT_TIMESTAMP_KEY)? {
        None => BTreeMap::new(),
        Some(raw) if raw.trim().is_empty() => BTreeMap::new(),
        Some(raw) => match serde_json::from_str::<BTreeMap<String, String>>(&raw) {
            Ok(map) => map,
            Err(_) => {
                tracing::warn!("Corrupted last_agent_timestamp in DB, resetting");
                BTreeMap::new()
            }
        },
    };
    let sessions = db.get_all_sessions()?;
    let groups = db.get_all_registered_groups()?;
    let persisted = groups.keys().cloned().collect();
    Ok(State {
        groups,
        persisted,
        sessions,
        last_timestamp,
        last_agent_timestamp,
        token_deadlines: HashMap::new(),
        run_tokens: HashMap::new(),
    })
}