Skip to main content

mail4agent_messenger_shell/
machine.rs

1//! Web machine client: one process for every bot session on this machine.
2//!
3//! This is not the homeserver, and it is not the node CLI
4//! ([`crate::OpenedStore::connect_node_from_env`]). Discovery uses
5//! [`AGENTS_DIR_ENV`] only when that variable names a directory. The home
6//! Grok Bot directory is not opened by default, and neither is its gateway
7//! file. Each child folder is a Grok Bot agent id and `profile.json`
8//! carries the display name. The mail session id is that agent id, unless
9//! [`SESSION_IDS_ENV`] maps the agent to an existing session id. The host
10//! may still pass a list, or this process reads [`SESSIONS_DIR_ENV`] when
11//! no agents directory was named. A session record is the bot display name, the mail session id,
12//! and the Grok Bot agent id when this session has one. A routine URL or a
13//! bearer in the file is refused.
14//!
15//! Wake routines. Every bot here is server-hosted, so only the bot itself
16//! can put a routine on the backend, with its own `UpdateRoutine`. The
17//! bot's nick, the routine's name, and its folder id are one string
18//! (`privet-mir`): the nick already follows the host slug rule, so
19//! [`crate::routine_folder_id`] leaves it unchanged. A homeserver that
20//! still enforces the older `[A-Za-z0-9_]` nick rule refuses such a nick
21//! at register (400) until it is redeployed; users already registered under
22//! underscore nicks are left as they are. Its backend id is
23//! `stableAutomationId(agentId, folderId)`. The local gateway hands out a
24//! webhook key only for a routine it finds locally, and mints it for that
25//! same id, so [`MachineClient::from_env`] keeps a disabled local mirror
26//! (webhook trigger, same folder) per bot through `createAgentAutomation`
27//! and then calls `getAutomationWebhookCredential`. A null key means the
28//! bot has not created its routine yet: logged, retried by
29//! [`MachineClient::poll_agent_directory`], and never answered with another
30//! routine. A mirror that lands in any other folder is deleted again.
31//! [`SKIP_NICKS_ENV`] lists bots to leave alone. A ready URL and key stay
32//! in memory and in the session's keychain file ([`WAKE_KEYCHAIN_FILE`],
33//! mode 0600, under the sealed store root); they are never logged. When a
34//! bot first becomes ready, this client POSTs `kind=peer_joined` once to
35//! every other ready bot's webhook ([`crate::post_routine_json`]); URLs
36//! and keys stay out of the log. A record with no agent id gets no wake.
37//! One URL is not shared across sessions.
38//! [`ensure_agent_webhook_routines`] is the same path without opening
39//! sealed stores. [`crate::LEADER_SOCK_ENV`] is not this path. The node
40//! CLI does not create a routine.
41//!
42//! Each session still seals under [`crate::session_store_dir`]. Olm pickles
43//! are not shared. While [`MachineClient::set_local_delivery`] is set, the
44//! existing drive performs requests against an in-process bus instead of
45//! the homeserver. Turn it off to reach a session that is not in the list.
46//! The bus is called from that drive. It is not a second sync loop.
47//! Room text from the homeserver is not a second sync loop either.
48//! [`MachineClient::open`] opens one socket for every session it
49//! registered. The homeserver pushes an event down that socket. This
50//! process delivers it to that session. A routine POST is the later
51//! drive, and only for a session that has its own webhook.
52//! [`MachineClient::tick`] is that loop step; the `m4a-web-client` binary
53//! runs it.
54//!
55//! Wake without a gateway agent. A session record may name `routine_file`, a path to a file of mode
56//! 0600 that the owner placed and that holds `{"url": "...", "key": "..."}`, the routine's webhook.
57//! The client reads it itself when it opens; the URL and the key are not logged, not shown in
58//! `Debug`, and never given to the agent, and a file that others can read is refused. The record
59//! itself cannot carry a URL or a key. A session with an `agent_id` and a gateway
60//! (`M4A_GATEWAY_FILE`, `{"port","scheme","token"}`) still gets its webhook created and read
61//! through the gateway as before; the gateway needs the Grok Bot agent id and is not used without
62//! one. A session with neither has no wake: its mail arrives and is read on demand.
63//!
64//! Session bearers. Each session logs in by signature with its own vaulted identity; the
65//! bearer lives in memory only and is never stored or handed in.
66
67//! the device. With [`KEYCHAIN_DIR_ENV`] set, [`MachineClient::from_env`]
68
69use std::fs::File;
70use std::collections::{HashMap, HashSet};
71use std::path::{Path, PathBuf};
72use std::sync::Arc;
73use zeroize::Zeroizing;
74
75use crate::ipc::{SendListener, SendStream};
76pub(crate) use crate::local_bus::{ensure_server_name, LocalBus};
77use crate::{
78    clip_public, register_session, DeviceId, OpenedStore,
79    SessionConfig, SessionWake, ShellError, HOMESERVER_URL_ENV, STORE_ROOT_ENV,
80};
81
82/// Directory of session records. Each `*.json` file is `bot_name`,
83/// `session_id`, and an optional `agent_id`. Used when no agents directory
84/// is available. Unset means the host passed the list to
85/// [`MachineClient::open`] instead.
86pub const SESSIONS_DIR_ENV: &str = "M4A_SESSIONS_DIR";
87
88/// Live Grok Bot agents on this machine. Each child folder name is an
89/// agent id; `profile.json` has the display `name`. [`MachineClient::from_env`]
90/// prefers this over [`SESSIONS_DIR_ENV`] when the directory exists.
91pub const AGENTS_DIR_ENV: &str = "M4A_AGENTS_DIR";
92
93/// Default agents directory, relative to the user's home (`$HOME` or
94/// `%USERPROFILE%`). The webhook command uses it when [`AGENTS_DIR_ENV`]
95/// is unset and this path is a directory. The web client does not.
96pub const DEFAULT_AGENTS_DIR: &str = "agent-data/agents";
97
98/// `<home>/<rel>`, with the home taken from the environment; a bare relative path when none is set.
99pub(crate) fn under_home(rel: &str) -> PathBuf {
100    match std::env::var("HOME").or_else(|_| std::env::var("USERPROFILE")) {
101        Ok(h) if !h.is_empty() => PathBuf::from(h).join(rel),
102        _ => PathBuf::from(rel),
103    }
104}
105
106/// Optional rescan period in seconds for [`MachineClient::poll_agent_directory`].
107/// Unset or `0` means the caller decides when to poll; open still scans once.
108pub const AGENT_RESCAN_SECS_ENV: &str = "M4A_AGENT_RESCAN_SECS";
109
110/// One bot session the host says lives on this machine.
111///
112/// `routine_url` and `routine_bearer` are optional and stay in memory.
113/// They are not part of a session record on disk.
114pub struct HostSession {
115    /// Display name the host already shows (`Alice`, `Привет мир`).
116    pub bot_name: String,
117    /// Session id the host already assigned. This is the mail session, not
118    /// the Grok Bot agent id.
119    pub session_id: String,
120    /// Grok Bot agent id for [`createAgentAutomation`]. Absent means this
121    /// session does not get a webhook routine.
122    pub agent_id: Option<String>,
123    /// Routine URL for this session, if the host has one. Not a file.
124    pub routine_url: Option<String>,
125    /// Bearer for that routine POST. Memory only.
126    pub routine_bearer: Option<String>,
127    /// The operator's one-time invite code, needed only until this session's identity is
128    /// enrolled. Read by the client, never given to the agent.
129    pub invite: Option<String>,
130    /// Path of an owner-placed file (mode 0600) with the routine's webhook `{"url","key"}`. The
131    /// client reads it itself at open; the values never go to the agent or the log.
132    pub routine_file: Option<PathBuf>,
133    /// `server` tier (default) or `matrix`.
134    pub tier: m4a_agent::BackendKind,
135}
136
137impl HostSession {
138    /// A session with no routine and no stored device bearer.
139    pub fn new(bot_name: impl Into<String>, session_id: impl Into<String>) -> Self {
140        Self {
141            bot_name: bot_name.into(),
142            session_id: session_id.into(),
143            agent_id: None,
144            routine_url: None,
145            routine_bearer: None,
146            invite: None,
147            routine_file: None,
148            tier: m4a_agent::BackendKind::Server,
149        }
150    }
151
152    /// A routine webhook the owner put in a file (see [`Self::routine_file`]).
153    pub fn with_routine_file(mut self, path: impl Into<PathBuf>) -> Self {
154        self.routine_file = Some(path.into());
155        self
156    }
157
158    pub(crate) fn config(&self, url: &str, store_root: &Path) -> Result<SessionConfig, ShellError> {
159        Ok(SessionConfig::new_identity(url, self.tier, &self.session_id, store_root, self.invite.clone())?.with_nick_request(&self.bot_name))
160    }
161
162    /// The operator's invite for this session's first login.
163    pub fn with_invite(mut self, invite: impl Into<String>) -> Self {
164        self.invite = Some(invite.into());
165        self
166    }
167
168    /// Which tier this session logs in on.
169    pub fn with_tier(mut self, tier: m4a_agent::BackendKind) -> Self {
170        self.tier = tier;
171        self
172    }
173
174    /// Attaches a routine target. The bearer is kept only as this value.
175    pub fn with_routine(mut self, url: impl Into<String>, bearer: Option<String>) -> Self {
176        self.routine_url = Some(url.into());
177        self.routine_bearer = bearer.filter(|token| !token.is_empty());
178        self
179    }
180
181}
182
183impl std::fmt::Debug for HostSession {
184    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185        f.debug_struct("HostSession")
186            .field("bot_name", &self.bot_name)
187            .field("session_id", &self.session_id)
188            .field("agent_id", &self.agent_id)
189            .field("routine_url", &self.routine_url.as_ref().map(|_| "[set]"))
190            .field(
191                "routine_bearer",
192                &self.routine_bearer.as_ref().map(|_| "[redacted]"),
193            )
194            .finish()
195    }
196}
197
198#[derive(serde::Deserialize)]
199#[serde(deny_unknown_fields)]
200struct SessionFile {
201    bot_name: String,
202    session_id: String,
203    /// Grok Bot agent id. Missing or blank skips routine creation.
204    #[serde(default)]
205    agent_id: Option<String>,
206    /// Path of a file (mode 0600) that holds the routine's webhook `{"url","key"}`. Only the path
207    /// is in the record; the secret is in the file, which the owner places.
208    #[serde(default)]
209    routine_file: Option<String>,
210}
211
212/// Reads `*.json` session records from `dir`. A file that carries anything
213/// besides `bot_name`, `session_id`, and `agent_id` is refused, so a
214/// webhook URL or a bearer cannot ride along in a world-readable record.
215pub fn load_session_records(dir: &Path) -> Result<Vec<HostSession>, ShellError> {
216    if !dir.is_dir() {
217        return Err(ShellError::SessionList(
218            "session directory is missing".to_string(),
219        ));
220    }
221    let mut paths = Vec::new();
222    for entry in std::fs::read_dir(dir)? {
223        let entry = entry?;
224        let path = entry.path();
225        if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
226            continue;
227        }
228        paths.push(path);
229    }
230    paths.sort();
231    if paths.is_empty() {
232        return Err(ShellError::SessionList(
233            "session directory has no records".to_string(),
234        ));
235    }
236    let mut out = Vec::with_capacity(paths.len());
237    for path in paths {
238        let text = std::fs::read_to_string(&path)
239            .map_err(|err| ShellError::SessionList(clip_public(err.to_string())))?;
240        let file: SessionFile = serde_json::from_str(&text)
241            .map_err(|err| ShellError::SessionList(clip_public(err.to_string())))?;
242        let mut session = HostSession::new(file.bot_name, file.session_id);
243        session.agent_id = file
244            .agent_id
245            .map(|id| id.trim().to_string())
246            .filter(|id| !id.is_empty());
247        session.routine_file = file.routine_file.map(|p| PathBuf::from(p.trim())).filter(|p| !p.as_os_str().is_empty());
248        out.push(session);
249    }
250    Ok(out)
251}
252
253#[derive(serde::Deserialize)]
254struct AgentProfileFile {
255    name: String,
256}
257
258/// Reads each agent folder under `dir`. Folder name is the agent id and the
259/// mail session id. `profile.json` supplies the display name. Folders
260/// without a usable profile are skipped. This does not hardcode agent ids.
261pub fn load_agents_dir(dir: &Path) -> Result<Vec<HostSession>, ShellError> {
262    if !dir.is_dir() {
263        return Err(ShellError::SessionList(
264            "agents directory is missing".to_string(),
265        ));
266    }
267    let mut ids = Vec::new();
268    for entry in std::fs::read_dir(dir)? {
269        let entry = entry?;
270        if !entry.file_type()?.is_dir() {
271            continue;
272        }
273        let name = entry.file_name();
274        let Some(id) = name.to_str() else {
275            continue;
276        };
277        if id.is_empty() || id.starts_with('.') {
278            continue;
279        }
280        ids.push(id.to_string());
281    }
282    ids.sort();
283    if ids.is_empty() {
284        return Err(ShellError::SessionList(
285            "agents directory has no agents".to_string(),
286        ));
287    }
288    let mut out = Vec::with_capacity(ids.len());
289    for id in ids {
290        let profile_path = dir.join(&id).join("profile.json");
291        if !profile_path.is_file() {
292            continue;
293        }
294        let text = std::fs::read_to_string(&profile_path)
295            .map_err(|err| ShellError::SessionList(clip_public(err.to_string())))?;
296        let profile: AgentProfileFile = serde_json::from_str(&text)
297            .map_err(|err| ShellError::SessionList(clip_public(err.to_string())))?;
298        let bot_name = profile.name.trim().to_string();
299        if bot_name.is_empty() {
300            continue;
301        }
302        let mut session = HostSession::new(bot_name, id.clone());
303        session.agent_id = Some(id);
304        out.push(session);
305    }
306    if out.is_empty() {
307        return Err(ShellError::SessionList(
308            "agents directory has no profiles".to_string(),
309        ));
310    }
311    Ok(out)
312}
313
314/// Agents directory for the web client. Only [`AGENTS_DIR_ENV`], and only
315/// when that path is a directory. An unset variable does not fall back to
316/// the home Grok Bot directory: that directory belongs to another product
317/// on the same box.
318fn explicit_agents_dir(mut get: impl FnMut(&str) -> Option<String>) -> Option<PathBuf> {
319    let path = PathBuf::from(get(AGENTS_DIR_ENV)?);
320    path.is_dir().then_some(path)
321}
322
323/// Resolves the agents directory from the environment lookup. Prefers
324/// [`AGENTS_DIR_ENV`], then [`DEFAULT_AGENTS_DIR`] when that path is a
325/// directory. The webhook command uses this. The web client does not.
326fn resolve_agents_dir(mut get: impl FnMut(&str) -> Option<String>) -> Option<PathBuf> {
327    if let Some(path) = get(AGENTS_DIR_ENV).filter(|value| !value.is_empty()) {
328        let path = PathBuf::from(path);
329        if path.is_dir() {
330            return Some(path);
331        }
332        return None;
333    }
334    let default = under_home(DEFAULT_AGENTS_DIR);
335    if default.is_dir() {
336        Some(default)
337    } else {
338        None
339    }
340}
341
342/// Session records plus one wake per session that has an agent id.
343///
344/// `get` is the process environment on [`MachineClient::from_env`]. A
345/// shared [`crate::ROUTINE_URL_ENV`] is not copied onto every session.
346/// [`GATEWAY_FILE_ENV`] names the gateway file. When that lookup is empty,
347/// this does not look for a gateway, so a test that did not point at one
348/// does not create a mirror. The json files are not rewritten.
349fn load_web_sessions(
350    dir: &Path,
351    mut get: impl FnMut(&str) -> Option<String>,
352) -> Result<Vec<HostSession>, ShellError> {
353    let sessions = load_session_records(dir)?;
354    let mut sessions = drop_skipped(sessions, &skip_nicks(&mut get));
355    attach_routines_from_env(&mut sessions, &mut get)?;
356    Ok(sessions)
357}
358
359/// [`load_agents_dir`] minus [`SKIP_NICKS_ENV`], plus one wake per agent.
360fn load_web_agents(
361    dir: &Path,
362    mut get: impl FnMut(&str) -> Option<String>,
363) -> Result<Vec<HostSession>, ShellError> {
364    let sessions = load_agents_dir(dir)?;
365    let mut sessions = drop_skipped(sessions, &skip_nicks(&mut get));
366    apply_session_ids(
367        &mut sessions,
368        &parse_session_ids(get(SESSION_IDS_ENV).as_deref().unwrap_or("")),
369    );
370    attach_routines_from_env(&mut sessions, &mut get)?;
371    Ok(sessions)
372}
373
374fn attach_routines_from_env(
375    sessions: &mut [HostSession],
376    get: &mut impl FnMut(&str) -> Option<String>,
377) -> Result<(), ShellError> {
378    let store_root = get(STORE_ROOT_ENV).map(PathBuf::from);
379    let Some(path) = get(GATEWAY_FILE_ENV).filter(|value| !value.is_empty()) else {
380        return Ok(());
381    };
382    let token = get(GATEWAY_TOKEN_ENV).filter(|value| !value.is_empty());
383    attach_webhook_routines(
384        sessions,
385        Path::new(&path),
386        token.as_deref(),
387        store_root.as_deref(),
388    )?;
389    Ok(())
390}
391
392/// Sessions in `found`, minus skipped nicks, with session-id aliases
393/// applied, that no open session holds (by store dir or nick). Each comes
394/// with its nick.
395fn unopened_agent_sessions(
396    found: Vec<HostSession>,
397    skip: &[String],
398    aliases: &[(String, String)],
399    store_root: &Path,
400    held: &[(PathBuf, Option<String>)],
401) -> Vec<(HostSession, String)> {
402    let mut sessions = drop_skipped(found, skip);
403    apply_session_ids(&mut sessions, aliases);
404    sessions
405        .into_iter()
406        .filter_map(|session| {
407            let nick = routine_name_for(&session)?;
408            let dir = crate::session_store_dir(store_root, &session.session_id);
409            let taken = held.iter().any(|(held_dir, held_nick)| {
410                *held_dir == dir
411                    || held_nick
412                        .as_deref()
413                        .is_some_and(|held| held.eq_ignore_ascii_case(&nick))
414            });
415            (!taken).then_some((session, nick))
416        })
417        .collect()
418}
419
420/// Comma-separated `agent_id=session_id` pairs. An agent listed here uses
421/// that mail session id instead of its agent id, for a bot whose session
422/// was registered before agent-id sessions. Machine-specific, so it lives
423/// in the environment.
424pub const SESSION_IDS_ENV: &str = "M4A_SESSION_IDS";
425
426pub(crate) fn parse_session_ids(raw: &str) -> Vec<(String, String)> {
427    raw.split(',')
428        .filter_map(|pair| {
429            let (agent, session) = pair.split_once('=')?;
430            let (agent, session) = (agent.trim(), session.trim());
431            (!agent.is_empty() && !session.is_empty())
432                .then(|| (agent.to_string(), session.to_string()))
433        })
434        .collect()
435}
436
437pub(crate) fn apply_session_ids(sessions: &mut [HostSession], aliases: &[(String, String)]) {
438    for session in sessions.iter_mut() {
439        let Some(agent_id) = session.agent_id.as_deref() else {
440            continue;
441        };
442        if let Some((_, alias)) = aliases.iter().find(|(agent, _)| agent == agent_id) {
443            session.session_id = alias.clone();
444        }
445    }
446}
447
448/// Comma-separated nicks this client leaves alone: no session, no mirror,
449/// no credential call. Machine-specific, so it lives in the environment
450/// and not in source.
451pub const SKIP_NICKS_ENV: &str = "M4A_SKIP_NICKS";
452
453fn skip_nicks(get: &mut impl FnMut(&str) -> Option<String>) -> Vec<String> {
454    parse_skip_nicks(get(SKIP_NICKS_ENV).as_deref().unwrap_or(""))
455}
456
457fn parse_skip_nicks(raw: &str) -> Vec<String> {
458    raw.split(',')
459        .map(|item| item.trim().to_ascii_lowercase())
460        .filter(|item| !item.is_empty())
461        .collect()
462}
463
464fn drop_skipped(sessions: Vec<HostSession>, skip: &[String]) -> Vec<HostSession> {
465    if skip.is_empty() {
466        return sessions;
467    }
468    sessions
469        .into_iter()
470        .filter(|session| match routine_name_for(session) {
471            Some(nick) => !skip.iter().any(|item| item.eq_ignore_ascii_case(&nick)),
472            None => true,
473        })
474        .collect()
475}
476
477/// Where one bot's wake stands after [`ensure_agent_webhook_routines`].
478/// Carries no URL and no key.
479#[derive(Debug, Clone, PartialEq, Eq)]
480pub enum WakeStatus {
481    /// Local mirror present and the gateway returned a URL and a key.
482    Ready,
483    /// Local mirror present, key null: the bot has not created its own
484    /// routine with this folder id yet. Retried on the next poll. No
485    /// second routine is made.
486    AwaitingBackend,
487    /// Nothing usable. The text names the reason, never a secret.
488    Failed(String),
489}
490
491/// One bot's wake routine, by nick and folder id.
492#[derive(Debug, Clone, PartialEq, Eq)]
493pub struct RoutineReport {
494    /// Grok Bot agent id.
495    pub agent_id: String,
496    /// Bot nick.
497    pub nick: String,
498    /// Routine name and folder id: [`crate::routine_folder_id`] of the
499    /// nick, which equals the nick.
500    pub folder_id: String,
501    /// Outcome.
502    pub status: WakeStatus,
503}
504
505/// Knobs for [`ensure_agent_webhook_routines`]. All machine-specific, so
506/// they come from the environment ([`WakeOptions::from_lookup`]).
507#[derive(Debug, Clone, Default)]
508pub struct WakeOptions {
509    /// Nicks to leave alone ([`SKIP_NICKS_ENV`]).
510    pub skip_nicks: Vec<String>,
511    /// Sealed store root for the per-session keychain file.
512    pub store_root: Option<PathBuf>,
513    /// Write the bootstrap note into the profile description of a bot that
514    /// has no routine yet ([`PROFILE_NOTE_ENV`]). Off unless asked for.
515    pub profile_note: bool,
516    /// `agent_id -> session_id` overrides ([`SESSION_IDS_ENV`]).
517    pub session_ids: Vec<(String, String)>,
518}
519
520impl WakeOptions {
521    /// [`SKIP_NICKS_ENV`], [`STORE_ROOT_ENV`], [`PROFILE_NOTE_ENV`].
522    pub fn from_lookup(mut get: impl FnMut(&str) -> Option<String>) -> Self {
523        Self {
524            skip_nicks: skip_nicks(&mut get),
525            store_root: get(STORE_ROOT_ENV).map(PathBuf::from),
526            profile_note: get(PROFILE_NOTE_ENV).as_deref() == Some("1"),
527            session_ids: parse_session_ids(get(SESSION_IDS_ENV).as_deref().unwrap_or("")),
528        }
529    }
530}
531
532/// Ensures each agent's local mirror (webhook trigger, disabled, folder =
533/// [`crate::routine_folder_id`] of the nick) and asks the gateway for its
534/// credential. Bots whose nick is in `skip_nicks` are not touched. Does
535/// not open sealed stores, does not register on the homeserver, and does
536/// not log or return URLs or keys. When `store_root` is set, a ready URL
537/// and key are written to that session's keychain file. With
538/// `profile_note`, a bot still waiting gets the bootstrap note.
539pub fn ensure_agent_webhook_routines(
540    agents_dir: &Path,
541    gateway_file: &Path,
542    token_override: Option<&str>,
543    options: &WakeOptions,
544) -> Result<Vec<RoutineReport>, ShellError> {
545    let store_root = options.store_root.as_deref();
546    let mut sessions = drop_skipped(load_agents_dir(agents_dir)?, &options.skip_nicks);
547    apply_session_ids(&mut sessions, &options.session_ids);
548    let Some(gate) = open_gateway(gateway_file, token_override)? else {
549        return Err(ShellError::Gateway("gateway file is missing".to_string()));
550    };
551    let mut reports = Vec::new();
552    for session in &sessions {
553        let Some(agent_id) = session.agent_id.as_deref().filter(|id| !id.is_empty()) else {
554            continue;
555        };
556        let Some(nick) = routine_name_for(session) else {
557            continue;
558        };
559        let outcome = ensure_wake(&gate, agent_id, &nick);
560        let folder_id = crate::nick::routine_folder_id(&nick).unwrap_or_default();
561        if let (WakeOutcome::Ready { url, key }, Some(root)) = (&outcome, store_root) {
562            save_wake(root, &session.session_id, &folder_id, url, key);
563        }
564        log_outcome(&nick, &folder_id, &outcome);
565        if options.profile_note && matches!(outcome, WakeOutcome::AwaitingBackend) {
566            note_profile(&gate, agents_dir, agent_id, &nick);
567        }
568        reports.push(RoutineReport {
569            agent_id: agent_id.to_string(),
570            nick,
571            folder_id,
572            status: outcome.status(),
573        });
574    }
575    Ok(reports)
576}
577
578/// [`ensure_agent_webhook_routines`] using the host gateway file, the
579/// agents directory from the environment (or [`DEFAULT_AGENTS_DIR`]),
580/// [`SKIP_NICKS_ENV`], and [`STORE_ROOT_ENV`] when set.
581pub fn ensure_agent_webhook_routines_from_env() -> Result<Vec<RoutineReport>, ShellError> {
582    let mut get = |key: &str| std::env::var(key).ok().filter(|v| !v.is_empty());
583    let agents_dir = resolve_agents_dir(&mut get)
584        .ok_or_else(|| ShellError::SessionList("agents directory is missing".to_string()))?;
585    let gateway = get(GATEWAY_FILE_ENV).unwrap_or_else(|| DEFAULT_GATEWAY_FILE.to_string());
586    let token = get(GATEWAY_TOKEN_ENV);
587    let options = WakeOptions::from_lookup(&mut get);
588    ensure_agent_webhook_routines(&agents_dir, Path::new(&gateway), token.as_deref(), &options)
589}
590
591/// Set to `1` to write the bootstrap note into the profile description of
592/// each bot that has no routine yet, through the gateway's `updateAgent`
593/// (the host pushes profile edits to the server copy of the bot). The note
594/// asks the bot to create its own routine named
595/// [`crate::routine_folder_id`] of its nick on its next turn. Off by default: it edits a description the owner wrote.
596pub const PROFILE_NOTE_ENV: &str = "M4A_BOOTSTRAP_PROFILE_NOTE";
597
598const NOTE_OPEN: &str = "<!-- mail4agent:wake -->";
599const NOTE_CLOSE: &str = "<!-- /mail4agent:wake -->";
600/// Host limit on a profile description.
601const DESCRIPTION_MAX: usize = 20_000;
602
603/// The bootstrap note for routine name `routine` (already hyphenated). No
604/// host name, no key, no agent id.
605fn wake_note(routine: &str) -> String {
606    format!(
607        "{NOTE_OPEN}\nmail4agent wake: keep exactly one routine named \"{routine}\" with a webhook trigger, enabled. If it is missing, create it with UpdateRoutine: name \"{routine}\", trigger webhook, prompt \"A mail4agent room message woke this routine. The webhook JSON has body, from, event_id, and nick. Read that message.\" Do not rename it and do not create a second one.\n{NOTE_CLOSE}"
608    )
609}
610
611/// `description` with the note for `routine` in place: an older note block is
612/// replaced, otherwise the note is appended after a blank line. `None`
613/// when the description already carries exactly this note.
614fn with_wake_note(description: &str, routine: &str) -> Option<String> {
615    let note = wake_note(routine);
616    if description.contains(&note) {
617        return None;
618    }
619    if let (Some(start), Some(end)) = (description.find(NOTE_OPEN), description.find(NOTE_CLOSE)) {
620        if end > start {
621            let mut out = String::with_capacity(description.len() + note.len());
622            out.push_str(&description[..start]);
623            out.push_str(&note);
624            out.push_str(&description[end + NOTE_CLOSE.len()..]);
625            return Some(out);
626        }
627    }
628    let base = description.trim_end();
629    Some(if base.is_empty() {
630        note
631    } else {
632        format!("{base}\n\n{note}")
633    })
634}
635
636/// Writes the note into one bot's profile description through the
637/// gateway. Reads the rest of the profile from `profile.json` so name,
638/// title, and avatar are sent back unchanged. Failures are logged only.
639fn note_profile(gate: &GatewayConn, agents_dir: &Path, agent_id: &str, nick: &str) {
640    match ensure_profile_note(gate, agents_dir, agent_id, nick) {
641        Ok(true) => eprintln!("mail4agent: wake {nick}: bootstrap note written to the profile"),
642        Ok(false) => {}
643        Err(err) => eprintln!("mail4agent: wake {nick}: bootstrap note not written ({err})"),
644    }
645}
646
647fn ensure_profile_note(
648    gate: &GatewayConn,
649    agents_dir: &Path,
650    agent_id: &str,
651    nick: &str,
652) -> Result<bool, ShellError> {
653    let text = std::fs::read_to_string(agents_dir.join(agent_id).join("profile.json"))
654        .map_err(|_| ShellError::Gateway("profile is unreadable".to_string()))?;
655    let profile: serde_json::Value = serde_json::from_str(&text)
656        .map_err(|_| ShellError::Gateway("profile is unreadable".to_string()))?;
657    let field = |key: &str| profile.get(key).and_then(|value| value.as_str());
658    let Some(name) = field("name").filter(|name| !name.trim().is_empty()) else {
659        return Err(ShellError::Gateway("profile has no name".to_string()));
660    };
661    let routine = crate::nick::routine_folder_id(nick)
662        .ok_or_else(|| ShellError::Gateway("nick has no routine name".to_string()))?;
663    let Some(description) = with_wake_note(field("description").unwrap_or(""), &routine) else {
664        return Ok(false);
665    };
666    if description.chars().count() > DESCRIPTION_MAX {
667        return Err(ShellError::Gateway(
668            "description would be too long".to_string(),
669        ));
670    }
671    let mut body = serde_json::json!({ "name": name, "description": description });
672    for key in ["title", "avatarShape", "avatarColor"] {
673        if let Some(value) = field(key) {
674            body[key] = serde_json::Value::String(value.to_string());
675        }
676    }
677    let (status, _) = gateway_post(
678        gate,
679        "updateAgent",
680        &serde_json::json!({ "id": agent_id, "profile": body }),
681    )?;
682    if status != 200 {
683        return Err(ShellError::Gateway(format!(
684            "gateway profile status {status}"
685        )));
686    }
687    Ok(true)
688}
689
690/// Gateway file the host already runs. [`GATEWAY_FILE_ENV`] overrides it.
691/// The listener is loopback; the `host` field in the file is not used.
692const DEFAULT_GATEWAY_FILE: &str = "agent-data/gateway.json";
693
694/// Path of the gateway file. Unset on [`MachineClient::from_env`] means
695/// the web client does not open a gateway file. The webhook command still
696/// defaults to [`DEFAULT_GATEWAY_FILE`] when this is unset.
697pub const GATEWAY_FILE_ENV: &str = "M4A_GATEWAY_FILE";
698
699/// Gateway bearer. When set, this replaces the token in the gateway file.
700/// It is never written to disk.
701pub const GATEWAY_TOKEN_ENV: &str = "M4A_GATEWAY_TOKEN";
702
703/// Saved prompt of the local mirror. The mirror is disabled and never runs:
704/// the backend routine the bot made itself is the one the webhook wakes.
705/// No host name and no key.
706const MIRROR_ROUTINE_PROMPT: &str = "mail4agent wake mirror. Disabled on purpose: it only lets the local gateway hand out the webhook key for the routine with this folder id, which the bot creates itself with UpdateRoutine.";
707
708/// Per-session keychain file under the session's sealed directory
709/// ([`crate::session_store_dir`]). Mode 0600. Holds the folder id, the
710/// webhook URL, and its key. Outside any repository; never logged.
711pub const WAKE_KEYCHAIN_FILE: &str = "routine-wake.json";
712
713#[derive(serde::Deserialize)]
714struct GatewayFile {
715    port: u16,
716    scheme: String,
717    #[serde(default)]
718    token: Option<String>,
719}
720
721#[derive(serde::Deserialize)]
722struct AutomationCard {
723    id: String,
724    name: String,
725    #[serde(default)]
726    trigger: Option<serde_json::Value>,
727}
728
729impl AutomationCard {
730    /// Whether the card fires on a webhook. A card without a trigger field
731    /// is not judged here; the credential call refuses it if it is not.
732    fn is_webhook(&self) -> bool {
733        fn has_webhook(value: &serde_json::Value) -> bool {
734            match value {
735                serde_json::Value::Array(items) => items.iter().any(has_webhook),
736                serde_json::Value::Object(map) => {
737                    map.get("type").and_then(|kind| kind.as_str()) == Some("webhook")
738                        || map
739                            .values()
740                            .any(|inner| inner.is_array() && has_webhook(inner))
741                }
742                _ => false,
743            }
744        }
745        self.trigger.as_ref().map(has_webhook).unwrap_or(true)
746    }
747}
748
749#[derive(serde::Deserialize)]
750struct WebhookCredential {
751    url: String,
752    key: Option<String>,
753}
754
755#[derive(serde::Serialize, serde::Deserialize)]
756#[serde(deny_unknown_fields)]
757struct StoredWake {
758    folder_id: String,
759    url: String,
760    key: String,
761}
762
763/// The bot's nick ([`crate::nick_from_display_name`] of the display name,
764/// so `Alice` -> `alice`, `Привет мир` -> `privet-mir`).
765/// The routine name and folder are the same string.
766/// `None` when no nick derives.
767fn routine_name_for(session: &HostSession) -> Option<String> {
768    crate::nick::nick_from_display_name(session.bot_name.trim()).ok()
769}
770
771struct GatewayConn {
772    client: reqwest::blocking::Client,
773    base: String,
774    token: String,
775}
776
777fn open_gateway(
778    gateway_file: &Path,
779    token_override: Option<&str>,
780) -> Result<Option<GatewayConn>, ShellError> {
781    if !gateway_file.is_file() {
782        return Ok(None);
783    }
784    let text = std::fs::read_to_string(gateway_file)
785        .map_err(|_| ShellError::Gateway("gateway file is unreadable".to_string()))?;
786    let file: GatewayFile = serde_json::from_str(&text)
787        .map_err(|_| ShellError::Gateway("gateway file is unreadable".to_string()))?;
788    let scheme = file.scheme.trim();
789    if scheme != "http" && scheme != "https" {
790        return Err(ShellError::Gateway(
791            "gateway scheme is not http or https".to_string(),
792        ));
793    }
794    if file.port == 0 {
795        return Err(ShellError::Gateway("gateway port is unset".to_string()));
796    }
797    let token = token_override
798        .map(str::trim)
799        .filter(|value| !value.is_empty())
800        .map(str::to_string)
801        .or(file.token.filter(|value| !value.trim().is_empty()));
802    let Some(token) = token else {
803        return Err(ShellError::Gateway("gateway token is unset".to_string()));
804    };
805    // The gateway listens on loopback only. Do not use the file's host.
806    let base = format!("{scheme}://127.0.0.1:{}", file.port);
807    Ok(Some(GatewayConn {
808        client: gateway_client()?,
809        base,
810        token,
811    }))
812}
813
814/// Result of [`ensure_wake`] for one agent. Holds the secret only in
815/// memory, on its way to the session and the keychain file.
816enum WakeOutcome {
817    Ready { url: String, key: String },
818    AwaitingBackend,
819    Failed(String),
820}
821
822impl WakeOutcome {
823    fn status(&self) -> WakeStatus {
824        match self {
825            WakeOutcome::Ready { .. } => WakeStatus::Ready,
826            WakeOutcome::AwaitingBackend => WakeStatus::AwaitingBackend,
827            WakeOutcome::Failed(reason) => WakeStatus::Failed(reason.clone()),
828        }
829    }
830}
831
832fn log_outcome(nick: &str, folder_id: &str, outcome: &WakeOutcome) {
833    match outcome {
834        WakeOutcome::Ready { .. } => eprintln!("mail4agent: wake {nick} ({folder_id}): ready"),
835        WakeOutcome::AwaitingBackend => eprintln!(
836            "mail4agent: wake {nick} ({folder_id}): no key yet; the bot has not created its own routine with this folder id; will retry"
837        ),
838        WakeOutcome::Failed(reason) => {
839            eprintln!("mail4agent: wake {nick} ({folder_id}): {reason}")
840        }
841    }
842}
843
844/// One agent: make sure the disabled local mirror exists in exactly the
845/// folder the bot's own routine uses, then read the credential.
846///
847/// The gateway mints a key only for a routine it can find locally, and it
848/// mints it for `stableAutomationId(agentId, folderId)`, which is the
849/// backend id of the bot's own routine with that folder id. A null key
850/// means that backend routine does not exist yet. This never creates a
851/// second routine: a mirror that lands in another folder (`-2`) is deleted
852/// again and reported.
853fn ensure_wake(gate: &GatewayConn, agent_id: &str, nick: &str) -> WakeOutcome {
854    let Some(folder) = crate::nick::routine_folder_id(nick) else {
855        return WakeOutcome::Failed("nick has no routine folder id".to_string());
856    };
857    let before = match list_agent_automations(gate, agent_id) {
858        Ok(cards) => cards,
859        Err(err) => return WakeOutcome::Failed(err.to_string()),
860    };
861    match before.iter().find(|card| card.id == folder) {
862        Some(card) if !card.is_webhook() => {
863            return WakeOutcome::Failed(
864                "a routine in this folder is not webhook-triggered; left as is".to_string(),
865            );
866        }
867        Some(_) => {}
868        None => {
869            let after = match create_mirror_routine(gate, agent_id, &folder) {
870                Ok(cards) => cards,
871                Err(err) => return WakeOutcome::Failed(err.to_string()),
872            };
873            if !after.iter().any(|card| card.id == folder) {
874                for stray in after.iter().filter(|card| {
875                    card.name == folder && !before.iter().any(|old| old.id == card.id)
876                }) {
877                    let _ = delete_agent_automation(gate, agent_id, &stray.id);
878                }
879                return WakeOutcome::Failed(
880                    "the mirror did not land in the expected folder; removed it".to_string(),
881                );
882            }
883        }
884    }
885    match read_webhook_credential(gate, agent_id, &folder) {
886        Ok(CredentialRead::Ready { url, key }) => WakeOutcome::Ready { url, key },
887        Ok(CredentialRead::MintFailed) => WakeOutcome::AwaitingBackend,
888        Ok(CredentialRead::Missing) => {
889            WakeOutcome::Failed("the mirror is gone from the gateway".to_string())
890        }
891        Err(err) => WakeOutcome::Failed(err.to_string()),
892    }
893}
894
895/// Sets each session's wake. With a gateway: [`ensure_wake`], and a ready
896/// URL and key go into the session and, when `store_root` is set, into the
897/// keychain file. Without a gateway file: the keychain file, if it holds a
898/// wake for this nick's folder. A session with no agent id is left alone.
899fn attach_webhook_routines(
900    sessions: &mut [HostSession],
901    gateway_file: &Path,
902    token_override: Option<&str>,
903    store_root: Option<&Path>,
904) -> Result<(), ShellError> {
905    let gate = open_gateway(gateway_file, token_override)?;
906    for session in sessions.iter_mut() {
907        if session.routine_url.is_some() && session.routine_bearer.is_some() {
908            continue;
909        }
910        // The gateway id is the Grok Bot agent, not the mail session id.
911        let Some(agent_id) = session.agent_id.clone().filter(|id| !id.is_empty()) else {
912            continue;
913        };
914        let Some(nick) = routine_name_for(session) else {
915            continue;
916        };
917        let Some(folder) = crate::nick::routine_folder_id(&nick) else {
918            continue;
919        };
920        let Some(gate) = gate.as_ref() else {
921            if let Some(root) = store_root {
922                if let Some((url, key)) = load_wake(root, &session.session_id, &folder) {
923                    session.routine_url = Some(url);
924                    session.routine_bearer = Some(key);
925                }
926            }
927            continue;
928        };
929        let outcome = ensure_wake(gate, &agent_id, &nick);
930        log_outcome(&nick, &folder, &outcome);
931        match outcome {
932            WakeOutcome::Ready { url, key } => {
933                if let Some(root) = store_root {
934                    save_wake(root, &session.session_id, &folder, &url, &key);
935                }
936                session.routine_url = Some(url);
937                session.routine_bearer = Some(key);
938            }
939            WakeOutcome::AwaitingBackend => {
940                // A key kept from before is not trusted once the gateway
941                // says the routine has none.
942                if let Some(root) = store_root {
943                    clear_wake(root, &session.session_id);
944                }
945            }
946            WakeOutcome::Failed(_) => {}
947        }
948    }
949    Ok(())
950}
951
952#[derive(serde::Deserialize)]
953#[serde(deny_unknown_fields)]
954struct RoutineFile {
955    url: String,
956    key: String,
957}
958
959/// Reads the owner-placed routine file. The error text names the problem, never a value.
960fn read_routine_file(path: &Path) -> Result<(String, String), String> {
961    #[cfg(unix)]
962    {
963        use std::os::unix::fs::PermissionsExt;
964        let meta = std::fs::metadata(path).map_err(|_| "the routine file cannot be read".to_string())?;
965        if meta.permissions().mode() & 0o077 != 0 {
966            return Err("the routine file must be mode 0600 (owner only)".to_string());
967        }
968    }
969    let text = std::fs::read_to_string(path).map_err(|_| "the routine file cannot be read".to_string())?;
970    let file: RoutineFile = serde_json::from_str(&text).map_err(|_| "the routine file is not {\"url\",\"key\"} json".to_string())?;
971    let (url, key) = (file.url.trim().to_string(), file.key.trim().to_string());
972    if key.is_empty() || !crate::webhook::url_ok(&url) {
973        return Err("the routine file has no usable url and key".to_string());
974    }
975    Ok((url, key))
976}
977
978/// Gives every session that names a `routine_file` its wake from that file. A session that already
979/// holds a URL and a bearer is left alone. A refusal is logged without values and leaves the
980/// session without a wake (messages still arrive and can be read).
981fn attach_routine_files(sessions: &mut [HostSession]) {
982    for session in sessions.iter_mut() {
983        let Some(path) = session.routine_file.clone() else { continue };
984        if session.routine_url.is_some() && session.routine_bearer.is_some() {
985            continue;
986        }
987        let label = routine_name_for(session).unwrap_or_else(|| session.session_id.clone());
988        match read_routine_file(&path) {
989            Ok((url, key)) => {
990                session.routine_url = Some(url);
991                session.routine_bearer = Some(key);
992                eprintln!("mail4agent: wake {label}: routine file read");
993            }
994            Err(reason) => eprintln!("mail4agent: wake {label}: {reason}"),
995        }
996    }
997}
998
999/// Lock file in each sealed session directory. [`MachineClient::open`]
1000/// holds an exclusive lock on it for as long as the client lives, so a
1001/// second process (another client, or `m4a-send` opening the store itself)
1002/// cannot write the same Olm state at the same time.
1003pub const STORE_LOCK_FILE: &str = ".lock";
1004
1005fn now_ms() -> i64 {
1006    std::time::SystemTime::now()
1007        .duration_since(std::time::UNIX_EPOCH)
1008        .map(|elapsed| elapsed.as_millis() as i64)
1009        .unwrap_or(0)
1010}
1011
1012pub(crate) fn lock_store(dir: &Path) -> Result<File, ShellError> {
1013    std::fs::create_dir_all(dir)?;
1014    let file = std::fs::OpenOptions::new()
1015        .create(true)
1016        .truncate(false)
1017        .write(true)
1018        .open(dir.join(STORE_LOCK_FILE))?;
1019    match file.try_lock() {
1020        Ok(()) => Ok(file),
1021        Err(_) => Err(ShellError::SessionList(
1022            "session store is in use by another process".to_string(),
1023        )),
1024    }
1025}
1026
1027/// Writes `bytes` to `path` atomically, file 0600, parent created 0700.
1028fn write_secret_file(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
1029    let dir = path
1030        .parent()
1031        .ok_or_else(|| std::io::Error::other("no parent"))?;
1032    let mut builder = std::fs::DirBuilder::new();
1033    builder.recursive(true);
1034    #[cfg(unix)]
1035    {
1036        use std::os::unix::fs::DirBuilderExt;
1037        builder.mode(0o700);
1038    }
1039    builder.create(dir)?;
1040    let name = path
1041        .file_name()
1042        .and_then(|name| name.to_str())
1043        .unwrap_or("secret");
1044    let tmp = dir.join(format!("{name}.tmp"));
1045    let mut options = std::fs::OpenOptions::new();
1046    options.write(true).create(true).truncate(true);
1047    #[cfg(unix)]
1048    {
1049        use std::os::unix::fs::OpenOptionsExt;
1050        options.mode(0o600);
1051    }
1052    let mut file = options.open(&tmp)?;
1053    std::io::Write::write_all(&mut file, bytes)?;
1054    file.sync_all()?;
1055    drop(file);
1056    std::fs::rename(&tmp, path)
1057}
1058
1059fn keychain_path(store_root: &Path, session_id: &str) -> PathBuf {
1060    crate::session_store_dir(store_root, session_id).join(WAKE_KEYCHAIN_FILE)
1061}
1062
1063/// Writes the wake atomically with mode 0600. A failure is logged without
1064/// the values and does not stop the client: the gateway still has the key.
1065fn save_wake(store_root: &Path, session_id: &str, folder_id: &str, url: &str, key: &str) {
1066    let stored = StoredWake {
1067        folder_id: folder_id.to_string(),
1068        url: url.to_string(),
1069        key: key.to_string(),
1070    };
1071    let result = serde_json::to_vec(&stored)
1072        .map_err(std::io::Error::other)
1073        .and_then(|bytes| write_secret_file(&keychain_path(store_root, session_id), &bytes));
1074    if result.is_err() {
1075        eprintln!("mail4agent: wake keychain write failed for folder {folder_id}");
1076    }
1077}
1078
1079fn load_wake(store_root: &Path, session_id: &str, folder_id: &str) -> Option<(String, String)> {
1080    let text = std::fs::read_to_string(keychain_path(store_root, session_id)).ok()?;
1081    let stored: StoredWake = serde_json::from_str(&text).ok()?;
1082    if stored.folder_id != folder_id || stored.url.is_empty() || stored.key.is_empty() {
1083        return None;
1084    }
1085    Some((stored.url, stored.key))
1086}
1087
1088fn clear_wake(store_root: &Path, session_id: &str) {
1089    let _ = std::fs::remove_file(keychain_path(store_root, session_id));
1090}
1091
1092fn list_agent_automations(
1093    gate: &GatewayConn,
1094    agent_id: &str,
1095) -> Result<Vec<AutomationCard>, ShellError> {
1096    let (status, body) = gateway_post(
1097        gate,
1098        "getAgentAutomations",
1099        &serde_json::json!({ "id": agent_id }),
1100    )?;
1101    if status != 200 {
1102        return Err(ShellError::Gateway(format!("gateway list status {status}")));
1103    }
1104    serde_json::from_slice(&body)
1105        .map_err(|_| ShellError::Gateway("gateway list was not understood".to_string()))
1106}
1107
1108enum CredentialRead {
1109    Ready {
1110        url: String,
1111        key: String,
1112    },
1113    /// The mirror exists and the gateway did not return a key.
1114    MintFailed,
1115    Missing,
1116}
1117
1118fn gateway_client() -> Result<reqwest::blocking::Client, ShellError> {
1119    reqwest::blocking::Client::builder()
1120        .timeout(std::time::Duration::from_secs(8))
1121        .redirect(reqwest::redirect::Policy::none())
1122        .http1_only()
1123        .build()
1124        .map_err(|_| ShellError::Gateway("gateway client could not start".to_string()))
1125}
1126
1127fn gateway_post(
1128    gate: &GatewayConn,
1129    method: &str,
1130    body: &serde_json::Value,
1131) -> Result<(u16, Vec<u8>), ShellError> {
1132    let url = format!("{}/api/{method}", gate.base);
1133    let authorization = crate::bearer_header(&gate.token).map_err(|_| {
1134        ShellError::Gateway("gateway token is not a single header value".to_string())
1135    })?;
1136    let bytes = serde_json::to_vec(body)
1137        .map_err(|_| ShellError::Gateway("gateway request was not json".to_string()))?;
1138    let response = gate
1139        .client
1140        .post(&url)
1141        .header(reqwest::header::AUTHORIZATION, authorization)
1142        .header(reqwest::header::CONTENT_TYPE, "application/json")
1143        .body(bytes)
1144        .send()
1145        .map_err(|_| ShellError::Gateway("gateway request failed".to_string()))?;
1146    let status = response.status().as_u16();
1147    let body = response
1148        .bytes()
1149        .map_err(|_| ShellError::Gateway("gateway response failed".to_string()))?;
1150    Ok((status, body.to_vec()))
1151}
1152
1153fn read_webhook_credential(
1154    gate: &GatewayConn,
1155    agent_id: &str,
1156    automation_id: &str,
1157) -> Result<CredentialRead, ShellError> {
1158    let (status, body) = gateway_post(
1159        gate,
1160        "getAutomationWebhookCredential",
1161        &serde_json::json!({
1162            "id": agent_id,
1163            "automationId": automation_id,
1164        }),
1165    )?;
1166    if status == 200 {
1167        let parsed: WebhookCredential = serde_json::from_slice(&body).map_err(|_| {
1168            ShellError::Gateway("gateway credential was not understood".to_string())
1169        })?;
1170        let key = parsed.key.filter(|key| !key.is_empty());
1171        if parsed.url.is_empty() {
1172            return Err(ShellError::Gateway(
1173                "gateway credential was not understood".to_string(),
1174            ));
1175        }
1176        return Ok(match key {
1177            Some(key) => CredentialRead::Ready {
1178                url: parsed.url,
1179                key,
1180            },
1181            None => CredentialRead::MintFailed,
1182        });
1183    }
1184    if credential_is_missing(status, &body) {
1185        return Ok(CredentialRead::Missing);
1186    }
1187    Err(ShellError::Gateway(format!(
1188        "gateway credential status {status}"
1189    )))
1190}
1191
1192/// A missing routine is not a 200. The gateway reports that as 404 or as
1193/// 500 with its not-found error.
1194fn credential_is_missing(status: u16, body: &[u8]) -> bool {
1195    if status == 404 {
1196        return true;
1197    }
1198    if status != 500 {
1199        return false;
1200    }
1201    std::str::from_utf8(body)
1202        .map(|text| text.contains("Automation not found"))
1203        .unwrap_or(false)
1204}
1205
1206/// `routine` is the routine name, equal to its folder id.
1207fn create_mirror_routine(
1208    gate: &GatewayConn,
1209    agent_id: &str,
1210    routine: &str,
1211) -> Result<Vec<AutomationCard>, ShellError> {
1212    let (status, body) = gateway_post(
1213        gate,
1214        "createAgentAutomation",
1215        &serde_json::json!({
1216            "id": agent_id,
1217            "spec": {
1218                "name": routine,
1219                "prompt": MIRROR_ROUTINE_PROMPT,
1220                "trigger": { "type": "webhook" },
1221                "isEnabled": false,
1222            },
1223        }),
1224    )?;
1225    if status != 200 {
1226        return Err(ShellError::Gateway(format!(
1227            "gateway create status {status}"
1228        )));
1229    }
1230    serde_json::from_slice(&body)
1231        .map_err(|_| ShellError::Gateway("gateway create was not understood".to_string()))
1232}
1233
1234fn delete_agent_automation(
1235    gate: &GatewayConn,
1236    agent_id: &str,
1237    automation_id: &str,
1238) -> Result<(), ShellError> {
1239    let (status, _) = gateway_post(
1240        gate,
1241        "deleteAgentAutomation",
1242        &serde_json::json!({ "id": agent_id, "automationId": automation_id }),
1243    )?;
1244    if status != 200 {
1245        return Err(ShellError::Gateway(format!(
1246            "gateway delete status {status}"
1247        )));
1248    }
1249    Ok(())
1250}
1251
1252pub(crate) struct Prepared {
1253    pub(crate) config: SessionConfig,
1254    pub(crate) nick: String,
1255    pub(crate) user_id: String,
1256    pub(crate) device_id: DeviceId,
1257    pub(crate) bearer: Zeroizing<String>,
1258    pub(crate) backend: Arc<dyn m4a_agent::Backend>,
1259    pub(crate) routine_url: Option<String>,
1260    pub(crate) routine_bearer: Option<String>,
1261}
1262
1263/// The sessions on one machine, and the in-process bus they use when the
1264/// peer is one of them.
1265pub struct MachineClient {
1266    /// Product-session mode: one push socket per session (a handshake is vouched for one identity).
1267    product_mode: bool,
1268    sessions: Vec<OpenedStore>,
1269    bus: Arc<LocalBus>,
1270    push: m4a_agent::engine::PushLink,
1271    /// When set, [`Self::poll_agent_directory`] rescans this folder for new
1272    /// bots and creates their webhook routines. Session stores already open
1273    /// are left alone; a new process picks up new sealed stores.
1274    agents_dir: Option<PathBuf>,
1275    gateway_file: Option<PathBuf>,
1276    gateway_token: Option<String>,
1277    last_agent_poll: std::time::Instant,
1278    agent_rescan_secs: u64,
1279    /// Root for per-session keychain files ([`WAKE_KEYCHAIN_FILE`]).
1280    store_root: Option<PathBuf>,
1281    /// [`SKIP_NICKS_ENV`] as read at open.
1282    skip_nicks: Vec<String>,
1283    /// [`PROFILE_NOTE_ENV`] as read at open.
1284    profile_note: bool,
1285    /// [`SESSION_IDS_ENV`] as read at open.
1286    session_ids: Vec<(String, String)>,
1287    /// When every session was last driven without a push.
1288    last_full_drive: std::time::Instant,
1289    /// Open sessions whose bot has no key yet. Retried on each poll.
1290    pending_wakes: Vec<PendingWake>,
1291    /// Agent ids whose wake is already set on an open session or reported
1292    /// ready. Not asked again.
1293    ready_agents: HashSet<String>,
1294    /// Exclusive locks on every open session directory ([`STORE_LOCK_FILE`]).
1295    _locks: Vec<File>,
1296    /// Local socket `m4a-send` writes to ([`MachineClient::listen_for_sends`]).
1297    send_listener: Option<SendListener>,
1298    send_sock: Option<PathBuf>,
1299    /// Sends waiting for the peer to join the DM.
1300    send_queue: Vec<PendingSend>,
1301    /// Homeserver the sessions registered on; a bot found by
1302    /// [`Self::poll_agent_directory`] registers there too.
1303    homeserver_url: String,
1304    /// Next local-bus user row for a session opened after start.
1305    bus_next_user: i64,
1306    /// Session ids whose late open failed, and when. Retried after
1307    /// [`LATE_OPEN_RETRY_SECS`].
1308    failed_opens: HashMap<String, std::time::Instant>,
1309}
1310
1311/// How long [`MachineClient::poll_agent_directory`] waits before trying
1312/// again to open a newly found bot's session after a failure.
1313const LATE_OPEN_RETRY_SECS: u64 = 60;
1314
1315/// One `m4a-send` request still in progress.
1316struct PendingSend {
1317    stream: SendStream,
1318    request: crate::SendRequest,
1319    started: std::time::Instant,
1320    room: Option<String>,
1321    peer: Option<String>,
1322}
1323
1324/// How long a send waits for the recipient to join a new DM.
1325const SEND_JOIN_WAIT_SECS: u64 = 120;
1326
1327impl Drop for MachineClient {
1328    fn drop(&mut self) {
1329        if let Some(path) = self.send_sock.take() {
1330            let _ = std::fs::remove_file(path);
1331        }
1332    }
1333}
1334
1335/// What one [`MachineClient::tick`] did. Nicks, event ids, room ids, and
1336/// error texts only.
1337#[derive(Debug, Default)]
1338pub struct TickReport {
1339    /// (nick, event id) for each event the push socket delivered.
1340    pub pushed: Vec<(String, String)>,
1341    /// (nick, room id) for each DM invite joined.
1342    pub joined: Vec<(String, String)>,
1343    /// (nick, error) for each session whose drive failed.
1344    pub errors: Vec<(String, String)>,
1345    /// (from nick, to nick, answer) for each `m4a-send` request finished.
1346    pub sent: Vec<(String, String, crate::SendReply)>,
1347    /// `(nick, text)` peer key-change alerts (M3).
1348    pub alerts: Vec<(String, String)>,
1349}
1350
1351/// An open session still waiting for its bot's own routine.
1352struct PendingWake {
1353    store_dir: PathBuf,
1354    agent_id: String,
1355}
1356
1357impl MachineClient {
1358    /// Registers `sessions` and opens each sealed store under `store_root`.
1359    ///
1360    /// The host built `sessions`. This does not look for bot processes.
1361    /// The first drive publishes public keys to the homeserver and mirrors
1362    /// them onto the in-process bus. A long-poll left by that drive is
1363    /// dropped so it is not a second sync loop.
1364    pub fn open(
1365        homeserver_url: &str,
1366        store_root: &Path,
1367        sessions: Vec<HostSession>,
1368    ) -> Result<Self, ShellError> {
1369        Self::open_with(homeserver_url, store_root, sessions, false)
1370    }
1371
1372    /// [`Self::open`]; with `lenient`, a session that fails to register or
1373    /// whose store is locked is logged and left out instead of failing the
1374    /// whole open (at least one session must open).
1375    fn open_with(
1376        homeserver_url: &str,
1377        store_root: &Path,
1378        sessions: Vec<HostSession>,
1379        lenient: bool,
1380    ) -> Result<Self, ShellError> {
1381        let mut sessions = sessions;
1382        attach_routine_files(&mut sessions);
1383        if sessions.is_empty() {
1384            return Err(ShellError::SessionList("session list is empty".to_string()));
1385        }
1386        let mut prepared = Vec::with_capacity(sessions.len());
1387        let mut seen_ids = Vec::new();
1388        let mut seen_nicks = Vec::new();
1389        let mut locks = Vec::new();
1390        for session in sessions {
1391            let config = session.config(homeserver_url, store_root)?;
1392            if seen_ids.iter().any(|id: &String| id == config.session_id()) {
1393                return Err(ShellError::SessionList("duplicate session id".to_string()));
1394            }
1395            seen_ids.push(config.session_id().to_string());
1396            let lock = match lock_store(&config.store_dir()) {
1397                Ok(lock) => lock,
1398                Err(err) if lenient => {
1399                    eprintln!("mail4agent: session {} left out: {err}", config.session_id());
1400                    continue;
1401                }
1402                Err(err) => return Err(err),
1403            };
1404            let registered = match register_session(&config) {
1405                Ok(registered) => registered,
1406                Err(err) if lenient => {
1407                    eprintln!("mail4agent: session {} left out: {err}", config.session_id());
1408                    continue;
1409                }
1410                Err(err) => return Err(err),
1411            };
1412            if seen_nicks.iter().any(|nick: &String| nick.eq_ignore_ascii_case(&registered.nick)) {
1413                return Err(ShellError::SessionList("duplicate nick".to_string()));
1414            }
1415            seen_nicks.push(registered.nick.clone());
1416            locks.push(lock);
1417            prepared.push(Prepared {
1418                config,
1419                nick: registered.nick,
1420                user_id: registered.user_id,
1421                device_id: registered.device_id,
1422                bearer: registered.bearer,
1423                backend: registered.backend,
1424                routine_url: session.routine_url,
1425                routine_bearer: session.routine_bearer,
1426            });
1427        }
1428        if prepared.is_empty() {
1429            return Err(ShellError::SessionList(
1430                "no session could be opened".to_string(),
1431            ));
1432        }
1433        let server_name = server_name_of(&prepared[0].user_id)?;
1434        for item in &prepared[1..] {
1435            if server_name_of(&item.user_id)? != server_name {
1436                return Err(ShellError::SessionList(
1437                    "sessions disagree on the homeserver name".to_string(),
1438                ));
1439            }
1440        }
1441        ensure_server_name(server_name)?;
1442        let bus = Arc::new(LocalBus::open(&prepared)?);
1443        let peers: Vec<(String, String)> = prepared
1444            .iter()
1445            .map(|item| (item.nick.to_string(), item.user_id.clone()))
1446            .collect();
1447        let mut opened = Vec::with_capacity(prepared.len());
1448        for item in &prepared {
1449            let server_name = server_name_of(&item.user_id)?;
1450            let mut store = OpenedStore::open(
1451                &item.config.store_dir(),
1452                item.config.session_id(),
1453                item.device_id.clone(),
1454                &item.user_id,
1455                server_name,
1456                Arc::clone(&item.backend),
1457                item.bearer.as_str(),
1458            )?;
1459            store.set_registered_nick(item.nick.to_string());
1460            store.attach_bus(Arc::clone(&bus));
1461            store.set_local_peers(
1462                peers
1463                    .iter()
1464                    .filter(|(nick, _)| *nick != item.nick)
1465                    .cloned()
1466                    .collect(),
1467            );
1468            store.set_wake(SessionWake {
1469                routine_url: item.routine_url.clone(),
1470                routine_bearer: item.routine_bearer.clone(),
1471                leader_sock: None,
1472                leader_cwd: None,
1473            });
1474            if item.routine_url.is_none() {
1475                attach_detected_chain(&mut store, &item.config, &item.nick);
1476            }
1477            store.drive(1_000, false)?;
1478            store.abandon_inflight_sync(1_000)?;
1479            opened.push(store);
1480        }
1481        let tokens: Vec<String> = prepared
1482            .iter()
1483            .map(|item| item.bearer.as_str().to_string())
1484            .collect();
1485        let product_mode = true;
1486        let push = m4a_agent::engine::PushLink::open(&prepared[0].config.homeserver_url, prepared[0].backend.keep_prefix(), tokens, product_mode)?;
1487        Ok(Self {
1488            product_mode,
1489            sessions: opened,
1490            bus,
1491            push,
1492            agents_dir: None,
1493            gateway_file: None,
1494            gateway_token: None,
1495            last_agent_poll: std::time::Instant::now(),
1496            agent_rescan_secs: 0,
1497            store_root: None,
1498            skip_nicks: Vec::new(),
1499            profile_note: false,
1500            session_ids: Vec::new(),
1501            last_full_drive: std::time::Instant::now(),
1502            pending_wakes: Vec::new(),
1503            ready_agents: HashSet::new(),
1504            _locks: locks,
1505            send_listener: None,
1506            send_sock: None,
1507            send_queue: Vec::new(),
1508            homeserver_url: homeserver_url.to_string(),
1509            bus_next_user: prepared.len() as i64 + 1,
1510            failed_opens: HashMap::new(),
1511        })
1512    }
1513
1514    /// [`load_session_records`] then [`Self::open`]. Records have no bearer
1515    /// and no routine URL; those stay unset.
1516    pub fn open_session_dir(
1517        homeserver_url: &str,
1518        store_root: &Path,
1519        sessions_dir: &Path,
1520    ) -> Result<Self, ShellError> {
1521        let sessions = load_session_records(sessions_dir)?;
1522        Self::open(homeserver_url, store_root, sessions)
1523    }
1524
1525    /// Web machine client open path.
1526    ///
1527    /// [`HOMESERVER_URL_ENV`], [`STORE_ROOT_ENV`]. Discovery uses
1528    /// [`AGENTS_DIR_ENV`] only when it names a directory; otherwise
1529    /// [`SESSIONS_DIR_ENV`]. The home Grok Bot directory is not a default.
1530    /// A webhook routine is read only when [`GATEWAY_FILE_ENV`] is set.
1531    /// The token is the file's token, or [`GATEWAY_TOKEN_ENV`] when the
1532    /// host injected one. A missing file leaves routines unset and does
1533    /// not fail this open. URLs and keys are not written back.
1534    /// [`crate::LEADER_SOCK_ENV`] is not read.
1535    pub fn from_env() -> Result<Self, ShellError> {
1536        Self::from_env_filtered(None)
1537    }
1538
1539    /// [`Self::from_env`] with only the session whose nick is `nick`: the
1540    /// path `m4a-send` takes when no client is running. Does not listen
1541    /// for sends.
1542    pub fn from_env_for(nick: &str) -> Result<Self, ShellError> {
1543        Self::from_env_filtered(Some(nick))
1544    }
1545
1546    fn from_env_filtered(only: Option<&str>) -> Result<Self, ShellError> {
1547        let homeserver_url = std::env::var(HOMESERVER_URL_ENV)
1548            .ok()
1549            .filter(|value| !value.is_empty())
1550            .ok_or(ShellError::HomeserverUrl)?;
1551        let store_root = std::env::var(STORE_ROOT_ENV)
1552            .ok()
1553            .filter(|value| !value.is_empty())
1554            .ok_or(ShellError::StoreRoot)?;
1555        let mut get = |key: &str| -> Option<String> {
1556            std::env::var(key).ok().filter(|value| !value.is_empty())
1557        };
1558        let agents_dir = explicit_agents_dir(&mut get);
1559        let (sessions, agents_dir) = if let Some(dir) = agents_dir {
1560            (load_web_agents(&dir, &mut get)?, Some(dir))
1561        } else {
1562            let sessions_dir = get(SESSIONS_DIR_ENV)
1563                .ok_or_else(|| ShellError::SessionList("session directory is unset".to_string()))?;
1564            (load_web_sessions(Path::new(&sessions_dir), &mut get)?, None)
1565        };
1566        let sessions = match only {
1567            Some(nick) => {
1568                let needle = crate::nick::lookup_nick(nick)?;
1569                let kept: Vec<HostSession> = sessions
1570                    .into_iter()
1571                    .filter(|session| {
1572                        routine_name_for(session)
1573                            .is_some_and(|name| name.eq_ignore_ascii_case(&needle))
1574                    })
1575                    .collect();
1576                if kept.is_empty() {
1577                    return Err(ShellError::UnknownNick);
1578                }
1579                kept
1580            }
1581            None => sessions,
1582        };
1583        let gateway_file = get(GATEWAY_FILE_ENV).map(PathBuf::from);
1584        let gateway_token = get(GATEWAY_TOKEN_ENV);
1585        let agent_rescan_secs = get(AGENT_RESCAN_SECS_ENV)
1586            .and_then(|value| value.parse::<u64>().ok())
1587            .unwrap_or(0);
1588        let mut pending_wakes = Vec::new();
1589        let mut ready_agents = HashSet::new();
1590        for session in &sessions {
1591            let Some(agent_id) = session.agent_id.clone() else {
1592                continue;
1593            };
1594            if session.routine_url.is_some() && session.routine_bearer.is_some() {
1595                ready_agents.insert(agent_id);
1596            } else {
1597                pending_wakes.push(PendingWake {
1598                    store_dir: crate::session_store_dir(
1599                        Path::new(&store_root),
1600                        &session.session_id,
1601                    ),
1602                    agent_id,
1603                });
1604            }
1605        }
1606        let options = WakeOptions::from_lookup(&mut get);
1607        let mut client = Self::open_with(&homeserver_url, Path::new(&store_root), sessions, true)?;
1608        client.store_root = Some(PathBuf::from(&store_root));
1609        client.skip_nicks = options.skip_nicks;
1610        client.profile_note = options.profile_note;
1611        client.session_ids = options.session_ids;
1612        client.pending_wakes = pending_wakes;
1613        client.ready_agents = ready_agents;
1614        client.agents_dir = agents_dir;
1615        client.gateway_file = gateway_file;
1616        client.gateway_token = gateway_token;
1617        client.agent_rescan_secs = agent_rescan_secs;
1618        client.last_agent_poll = std::time::Instant::now();
1619        Ok(client)
1620    }
1621
1622    /// Rescans the agents directory when this client was opened from one.
1623    ///
1624    /// For every bot not skipped and not ready yet: ensures the local
1625    /// mirror and asks for the credential again. A bot that just created
1626    /// its own routine turns ready here; when that bot has an open session,
1627    /// its wake is set in place and saved to the keychain file. A bot that
1628    /// appeared after open gets its mirror now; its sealed store opens with
1629    /// the next process. When [`AGENT_RESCAN_SECS_ENV`] is set and greater
1630    /// than zero, returns without scanning until that many seconds have
1631    /// passed since the last poll. Reports carry no URL and no key.
1632    pub fn poll_agent_directory(&mut self) -> Result<Vec<RoutineReport>, ShellError> {
1633        let Some(agents_dir) = self.agents_dir.clone() else {
1634            return Ok(Vec::new());
1635        };
1636        if self.agent_rescan_secs > 0 {
1637            let elapsed = self.last_agent_poll.elapsed().as_secs();
1638            if elapsed < self.agent_rescan_secs {
1639                return Ok(Vec::new());
1640            }
1641        }
1642        self.last_agent_poll = std::time::Instant::now();
1643        self.open_new_agent_sessions(&agents_dir);
1644        let gateway = self
1645            .gateway_file
1646            .clone()
1647            .unwrap_or_else(|| under_home(DEFAULT_GATEWAY_FILE));
1648        let Some(gate) = open_gateway(&gateway, self.gateway_token.as_deref())? else {
1649            return Ok(Vec::new());
1650        };
1651        let mut sessions = drop_skipped(load_agents_dir(&agents_dir)?, &self.skip_nicks);
1652        apply_session_ids(&mut sessions, &self.session_ids);
1653        let mut reports = Vec::new();
1654        for session in &sessions {
1655            let Some(agent_id) = session.agent_id.clone() else {
1656                continue;
1657            };
1658            let Some(nick) = routine_name_for(session) else {
1659                continue;
1660            };
1661            let folder_id = crate::nick::routine_folder_id(&nick).unwrap_or_default();
1662            if self.ready_agents.contains(&agent_id) {
1663                reports.push(RoutineReport {
1664                    agent_id,
1665                    nick,
1666                    folder_id,
1667                    status: WakeStatus::Ready,
1668                });
1669                continue;
1670            }
1671            let outcome = ensure_wake(&gate, &agent_id, &nick);
1672            log_outcome(&nick, &folder_id, &outcome);
1673            if self.profile_note && matches!(outcome, WakeOutcome::AwaitingBackend) {
1674                note_profile(&gate, &agents_dir, &agent_id, &nick);
1675            }
1676            if let WakeOutcome::Ready { url, key } = &outcome {
1677                if let Some(root) = &self.store_root {
1678                    save_wake(root, &session.session_id, &folder_id, url, key);
1679                }
1680                if let Some(index) = self
1681                    .pending_wakes
1682                    .iter()
1683                    .position(|pending| pending.agent_id == agent_id)
1684                {
1685                    let pending = self.pending_wakes.remove(index);
1686                    if let Some(store) = self
1687                        .sessions
1688                        .iter_mut()
1689                        .find(|store| store.store_dir() == pending.store_dir)
1690                    {
1691                        store.set_wake(SessionWake {
1692                            routine_url: Some(url.clone()),
1693                            routine_bearer: Some(key.clone()),
1694                            leader_sock: None,
1695                            leader_cwd: None,
1696                        });
1697                    }
1698                }
1699                self.ready_agents.insert(agent_id.clone());
1700                if let Some(user_id) = self
1701                    .sessions
1702                    .iter()
1703                    .find(|store| {
1704                        store
1705                            .nick()
1706                            .map(|n| n.eq_ignore_ascii_case(&nick))
1707                            .unwrap_or(false)
1708                    })
1709                    .map(|store| store.user_id().to_string())
1710                {
1711                    self.announce_peer_joined(&nick, &user_id);
1712                }
1713            }
1714            reports.push(RoutineReport {
1715                agent_id,
1716                nick,
1717                folder_id,
1718                status: outcome.status(),
1719            });
1720        }
1721        Ok(reports)
1722    }
1723
1724    /// Registers and opens a session for every bot in `agents_dir` that is
1725    /// not skipped and has no open session yet, the same way open does:
1726    /// nick = slug, sealed store under the store root, device bearer to the
1727    /// keychain dir. A failure is logged and retried after
1728    /// [`LATE_OPEN_RETRY_SECS`].
1729    fn open_new_agent_sessions(&mut self, agents_dir: &Path) {
1730        let Some(store_root) = self.store_root.clone() else {
1731            return;
1732        };
1733        let found = match load_agents_dir(agents_dir) {
1734            Ok(found) => found,
1735            Err(err) => {
1736                eprintln!("mail4agent: agent rescan: {err}");
1737                return;
1738            }
1739        };
1740        let held: Vec<(PathBuf, Option<String>)> = self
1741            .sessions
1742            .iter()
1743            .map(|store| {
1744                (
1745                    store.store_dir().to_path_buf(),
1746                    store.nick().map(str::to_string),
1747                )
1748            })
1749            .collect();
1750        let fresh = unopened_agent_sessions(
1751            found,
1752            &self.skip_nicks,
1753            &self.session_ids,
1754            &store_root,
1755            &held,
1756        );
1757        for (session, nick) in fresh {
1758            if let Some(failed_at) = self.failed_opens.get(&session.session_id) {
1759                if failed_at.elapsed().as_secs() < LATE_OPEN_RETRY_SECS {
1760                    continue;
1761                }
1762            }
1763            let session_id = session.session_id.clone();
1764            match self.open_late_session(&store_root, session, &nick) {
1765                Ok(()) => {
1766                    self.failed_opens.remove(&session_id);
1767                    println!("mail4agent: session {nick} registered and open");
1768                }
1769                Err(err) => {
1770                    eprintln!("mail4agent: session {nick} not opened: {err}");
1771                    self.failed_opens
1772                        .insert(session_id, std::time::Instant::now());
1773                }
1774            }
1775        }
1776    }
1777
1778    fn open_late_session(
1779        &mut self,
1780        store_root: &Path,
1781        mut session: HostSession,
1782        nick: &str,
1783    ) -> Result<(), ShellError> {
1784        if session.routine_url.is_none() || session.routine_bearer.is_none() {
1785            if let Some(folder) = crate::nick::routine_folder_id(nick) {
1786                if let Some((url, key)) = load_wake(store_root, &session.session_id, &folder) {
1787                    session.routine_url = Some(url);
1788                    session.routine_bearer = Some(key);
1789                }
1790            }
1791        }
1792        let config = session.config(&self.homeserver_url, store_root)?;
1793        let lock = lock_store(&config.store_dir())?;
1794        let registered = register_session(&config)?;
1795        if let Some(first) = self.sessions.first() {
1796            if server_name_of(&registered.user_id)? != server_name_of(first.user_id())? {
1797                return Err(ShellError::SessionList(
1798                    "sessions disagree on the homeserver name".to_string(),
1799                ));
1800            }
1801        }
1802        let item = Prepared {
1803            config,
1804            nick: registered.nick,
1805            user_id: registered.user_id,
1806            device_id: registered.device_id,
1807            bearer: registered.bearer,
1808                backend: registered.backend,
1809            routine_url: session.routine_url.clone(),
1810            routine_bearer: session.routine_bearer.clone(),
1811        };
1812        self.bus.seed(self.bus_next_user, &item)?;
1813        self.bus_next_user += 1;
1814        let mut store = OpenedStore::open(
1815            &item.config.store_dir(),
1816            item.config.session_id(),
1817            item.device_id.clone(),
1818            &item.user_id,
1819            server_name_of(&item.user_id)?,
1820            Arc::clone(&item.backend),
1821            item.bearer.as_str(),
1822        )?;
1823        store.set_registered_nick(item.nick.to_string());
1824        store.attach_bus(Arc::clone(&self.bus));
1825        store.set_wake(SessionWake {
1826            routine_url: item.routine_url.clone(),
1827            routine_bearer: item.routine_bearer.clone(),
1828            leader_sock: None,
1829            leader_cwd: None,
1830        });
1831        if item.routine_url.is_none() {
1832            attach_detected_chain(&mut store, &item.config, &item.nick);
1833        }
1834        store.drive(1_000, false)?;
1835        store.abandon_inflight_sync(1_000)?;
1836        self.sessions.push(store);
1837        self._locks.push(lock);
1838        self.refresh_local_peers();
1839        let tokens: Vec<String> = self
1840            .sessions
1841            .iter()
1842            .map(|store| store.device_bearer().to_string())
1843            .collect();
1844        match m4a_agent::engine::PushLink::open(&self.homeserver_url, self.sessions.first().is_some_and(|s| s.keep_prefix()), tokens, self.product_mode) {
1845            Ok(push) => {
1846                let old = std::mem::replace(&mut self.push, push);
1847                for (recipient, event) in old.drain() {
1848                    if let Some(store) = self
1849                        .sessions
1850                        .iter_mut()
1851                        .find(|store| store.user_id() == recipient)
1852                    {
1853                        store.record_push(event);
1854                    }
1855                }
1856            }
1857            Err(err) => {
1858                eprintln!("mail4agent: push socket not reopened for {nick}: {err}");
1859            }
1860        }
1861        if let Some(agent_id) = session.agent_id.clone() {
1862            if item.routine_url.is_some() && item.routine_bearer.is_some() {
1863                self.ready_agents.insert(agent_id);
1864                self.announce_peer_joined(nick, &item.user_id);
1865            } else {
1866                // Ask the gateway again so the wake lands on this session.
1867                self.ready_agents.remove(&agent_id);
1868                if !self.pending_wakes.iter().any(|p| p.agent_id == agent_id) {
1869                    self.pending_wakes.push(PendingWake {
1870                        store_dir: item.config.store_dir(),
1871                        agent_id,
1872                    });
1873                }
1874            }
1875        }
1876        Ok(())
1877    }
1878
1879    /// Every open session sees every other one as a local peer.
1880    fn refresh_local_peers(&mut self) {
1881        let peers: Vec<(String, String)> = self
1882            .sessions
1883            .iter()
1884            .filter_map(|store| Some((store.nick()?.to_string(), store.user_id().to_string())))
1885            .collect();
1886        for store in self.sessions.iter_mut() {
1887            let own = store.nick().unwrap_or("").to_string();
1888            store.set_local_peers(
1889                peers
1890                    .iter()
1891                    .filter(|(nick, _)| *nick != own)
1892                    .cloned()
1893                    .collect(),
1894            );
1895        }
1896    }
1897
1898    /// POSTs `kind=peer_joined` once to every other ready bot's webhook.
1899    /// Called when `joined_nick` first becomes ready. Failures are logged
1900    /// without the URL or key.
1901    fn announce_peer_joined(&self, joined_nick: &str, joined_user_id: &str) {
1902        let body = serde_json::json!({
1903            "kind": "peer_joined",
1904            "nick": joined_nick,
1905            "user_id": joined_user_id,
1906        });
1907        for store in &self.sessions {
1908            let Some(nick) = store.nick() else {
1909                continue;
1910            };
1911            if nick.eq_ignore_ascii_case(joined_nick) {
1912                continue;
1913            }
1914            if !store.has_routine() {
1915                continue;
1916            }
1917            let Some((url, bearer)) = store.routine_target() else {
1918                continue;
1919            };
1920            match crate::post_routine_json(&url, &body, bearer.as_deref()) {
1921                Ok(()) => {
1922                    eprintln!("mail4agent: peer_joined {joined_nick} -> {nick} status=200")
1923                }
1924                Err(err) => {
1925                    eprintln!("mail4agent: peer_joined {joined_nick} -> {nick}: {err}")
1926                }
1927            }
1928        }
1929    }
1930
1931    /// Agent ids of open sessions that still have no wake.
1932    pub fn pending_wake_agents(&self) -> Vec<String> {
1933        self.pending_wakes
1934            .iter()
1935            .map(|pending| pending.agent_id.clone())
1936            .collect()
1937    }
1938
1939    /// While `enabled`, drive does not call the homeserver. Requests are
1940    /// answered by the in-process bus. Turn this on for an exchange whose
1941    /// peer is a session [`Self::open`] holds, and off again before reaching
1942    /// a session that exists only on the homeserver.
1943    ///
1944    /// A public channel is not given a separate transport: with this off,
1945    /// create and send use the homeserver, which is what a public channel
1946    /// and a group with a remote member already do. An encrypted direct
1947    /// room between two local sessions uses the bus while this is on, and
1948    /// the room stays encrypted.
1949    pub fn set_local_delivery(&self, enabled: bool) {
1950        self.bus.set_local_only(enabled);
1951    }
1952
1953    /// Homeserver calls counted across every session since [`Self::open`].
1954    /// In-process bus calls are not included. Registration before the first
1955    /// drive is not included either.
1956    pub fn homeserver_hits(&self) -> u64 {
1957        self.bus.hits()
1958    }
1959
1960    /// The open session whose nick matches `name_or_nick`.
1961    pub fn session_mut(&mut self, name_or_nick: &str) -> Result<&mut OpenedStore, ShellError> {
1962        let needle = crate::nick::lookup_nick(name_or_nick)?;
1963        self.sessions
1964            .iter_mut()
1965            .find(|store| {
1966                store
1967                    .nick()
1968                    .is_some_and(|nick| nick.eq_ignore_ascii_case(&needle))
1969            })
1970            .ok_or(ShellError::UnknownNick)
1971    }
1972
1973    /// Sealed directory for that nick.
1974    pub fn store_dir(&self, name_or_nick: &str) -> Result<PathBuf, ShellError> {
1975        let needle = crate::nick::lookup_nick(name_or_nick)?;
1976        self.sessions
1977            .iter()
1978            .find(|store| {
1979                store
1980                    .nick()
1981                    .is_some_and(|nick| nick.eq_ignore_ascii_case(&needle))
1982            })
1983            .map(|store| store.store_dir().to_path_buf())
1984            .ok_or(ShellError::UnknownNick)
1985    }
1986
1987    /// Hands pushed room text to the session named by `recipient`.
1988    /// Another session on this client does not receive it. This does not
1989    /// start `/sync` and it does not post a routine.
1990    pub fn deliver_pushed(&mut self) -> usize {
1991        let batch = self.push.drain();
1992        let mut delivered = 0;
1993        for (recipient, event) in batch {
1994            let Some(session) = self
1995                .sessions
1996                .iter_mut()
1997                .find(|store| store.user_id() == recipient)
1998            else {
1999                continue;
2000            };
2001            session.record_push(event);
2002            delivered += 1;
2003        }
2004        delivered
2005    }
2006
2007    /// One step of the long-running client loop.
2008    ///
2009    /// Drains the push socket and hands each pushed event to its session,
2010    /// then drives exactly those sessions (waiting for the `/sync` the push
2011    /// announced), which decrypts the text and POSTs the wake to that
2012    /// session's own routine. Every `full_drive_secs` it also drives every
2013    /// session once, which is how DM invites get joined
2014    /// ([`OpenedStore::accept_direct_invites`]) and how a missed push is
2015    /// caught up. Errors are returned per session, without secrets.
2016    pub fn tick(&mut self, now_ms: i64, full_drive_secs: u64) -> TickReport {
2017        let mut report = TickReport::default();
2018        let mut due: Vec<usize> = Vec::new();
2019        for (recipient, event) in self.push.drain() {
2020            let Some(index) = self
2021                .sessions
2022                .iter()
2023                .position(|store| store.user_id() == recipient)
2024            else {
2025                continue;
2026            };
2027            let nick = self.sessions[index].nick().unwrap_or("").to_string();
2028            report.pushed.push((nick, event.event_id.clone()));
2029            self.sessions[index].record_push(event);
2030            if !due.contains(&index) {
2031                due.push(index);
2032            }
2033        }
2034        let full = self.last_full_drive.elapsed().as_secs() >= full_drive_secs;
2035        if full {
2036            self.last_full_drive = std::time::Instant::now();
2037        }
2038        for index in 0..self.sessions.len() {
2039            let pushed = due.contains(&index);
2040            if !pushed && !full {
2041                continue;
2042            }
2043            let store = &mut self.sessions[index];
2044            let nick = store.nick().unwrap_or("").to_string();
2045            let drove = store.drive(now_ms, pushed);
2046            for text in store.take_security_alerts() {
2047                report.alerts.push((nick.clone(), text));
2048            }
2049            if let Err(err) = drove {
2050                report.errors.push((nick.clone(), err.to_string()));
2051                continue;
2052            }
2053            match store.accept_direct_invites(now_ms) {
2054                Ok(joined) => {
2055                    for room in joined {
2056                        report.joined.push((nick.clone(), room));
2057                    }
2058                }
2059                Err(err) => report.errors.push((nick, err.to_string())),
2060            }
2061        }
2062        self.serve_sends(now_ms, &mut report);
2063        report
2064    }
2065
2066    /// Listens on `path` for `m4a-send` requests (one JSON line each, see
2067    /// [`crate::SendRequest`]). A stale socket file is replaced; a live
2068    /// listener is refused. Unix is mode 0600. Windows is loopback TCP and
2069    /// the file holds `127.0.0.1:{port}`. The file is removed when the
2070    /// client drops. [`Self::tick`] answers requests: the `as` session
2071    /// opens (or reuses) the encrypted DM with `to`, waits up to two
2072    /// minutes for `to` to join, and sends.
2073    pub fn listen_for_sends(&mut self, path: &Path) -> Result<(), ShellError> {
2074        let listener = SendListener::bind(path).map_err(|err| {
2075            if err.kind() == std::io::ErrorKind::AlreadyExists {
2076                ShellError::SessionList(
2077                    "another client already listens on the send socket".to_string(),
2078                )
2079            } else {
2080                ShellError::Io(err)
2081            }
2082        })?;
2083        listener.set_nonblocking(true)?;
2084        self.send_listener = Some(listener);
2085        self.send_sock = Some(path.to_path_buf());
2086        Ok(())
2087    }
2088
2089    /// [`Self::listen_for_sends`] on [`crate::SEND_SOCK_ENV`], or
2090    /// [`crate::DEFAULT_SOCK_NAME`] under the store root. Returns the path.
2091    pub fn listen_for_sends_from_env(&mut self) -> Result<PathBuf, ShellError> {
2092        let root = self.store_root.clone().ok_or(ShellError::StoreRoot)?;
2093        let path = crate::send_sock_path(
2094            |key| std::env::var(key).ok().filter(|value| !value.is_empty()),
2095            &root,
2096        );
2097        self.listen_for_sends(&path)?;
2098        Ok(path)
2099    }
2100
2101    /// Sends `text` from session `as_nick` to `to` in their encrypted DM,
2102    /// driving until `to` has joined (up to `wait`). The direct path of
2103    /// `m4a-send` when no client is running.
2104    pub fn send_blocking(
2105        &mut self,
2106        as_nick: &str,
2107        to: &str,
2108        text: &str,
2109        wait: std::time::Duration,
2110    ) -> crate::SendReply {
2111        let started = std::time::Instant::now();
2112        let mut room = None;
2113        let mut peer = None;
2114        loop {
2115            let now = now_ms();
2116            match self.try_send(as_nick, to, text, now, &mut room, &mut peer) {
2117                Some(reply) => return reply,
2118                None if started.elapsed() >= wait => {
2119                    return crate::SendReply {
2120                        room,
2121                        ..crate::SendReply::failed(format!("{to} has not joined the DM yet"))
2122                    }
2123                }
2124                None => {
2125                    if let Ok(store) = self.session_mut(as_nick) {
2126                        let _ = store.drive(now, false);
2127                    }
2128                    std::thread::sleep(std::time::Duration::from_millis(500));
2129                }
2130            }
2131        }
2132    }
2133
2134    /// One attempt. `None` means the DM exists but `to` has not joined yet.
2135    fn try_send(
2136        &mut self,
2137        as_nick: &str,
2138        to: &str,
2139        text: &str,
2140        now_ms: i64,
2141        room: &mut Option<String>,
2142        peer: &mut Option<String>,
2143    ) -> Option<crate::SendReply> {
2144        let store = match self.session_mut(as_nick) {
2145            Ok(store) => store,
2146            Err(_) => {
2147                return Some(crate::SendReply::failed(format!(
2148                    "{as_nick} is not a session on this client"
2149                )))
2150            }
2151        };
2152        if peer.is_none() {
2153            // Local peers first, then the homeserver user directory
2154            // (find_nick); the directory answer may need another drive.
2155            let mut last_err = None;
2156            for attempt in 0..3 {
2157                if attempt > 0 {
2158                    let _ = store.drive(now_ms, false);
2159                }
2160                match store.find_nick(to, now_ms) {
2161                    Ok(found) => {
2162                        *peer = Some(found.user_id);
2163                        last_err = None;
2164                        break;
2165                    }
2166                    Err(err) => last_err = Some(err),
2167                }
2168            }
2169            if let Some(err) = last_err {
2170                return Some(crate::SendReply::failed(format!("find {to}: {err}")));
2171            }
2172        }
2173        if room.is_none() {
2174            match store.ensure_dm(to, now_ms) {
2175                Ok(room_id) => *room = Some(room_id),
2176                Err(err) => return Some(crate::SendReply::failed(format!("open DM: {err}"))),
2177            }
2178        }
2179        let (room_id, peer_id) = (room.clone()?, peer.clone()?);
2180        if !store.member_joined(&room_id, &peer_id) {
2181            return None;
2182        }
2183        match store.write_to_nick(to, text, now_ms) {
2184            Ok(room_id) => {
2185                let event_id = store
2186                    .texts()
2187                    .into_iter()
2188                    .rev()
2189                    .find(|row| row.room_id == room_id && row.body == text)
2190                    .and_then(|row| row.event_id);
2191                Some(crate::SendReply {
2192                    ok: true,
2193                    room: Some(room_id),
2194                    event_id,
2195                    error: None,
2196                })
2197            }
2198            Err(err) => Some(crate::SendReply {
2199                room: Some(room_id),
2200                ..crate::SendReply::failed(format!("send: {err}"))
2201            }),
2202        }
2203    }
2204
2205    fn serve_sends(&mut self, now_ms: i64, report: &mut TickReport) {
2206        let mut cmds: Vec<(crate::ipc::SendStream, crate::CmdRequest)> = Vec::new();
2207        if let Some(listener) = &self.send_listener {
2208            loop {
2209                match listener.accept() {
2210                    Ok(mut stream) => match crate::send::read_incoming(&mut stream) {
2211                        Ok(crate::send::Incoming::Send(request)) => self.send_queue.push(PendingSend {
2212                            stream,
2213                            request,
2214                            started: std::time::Instant::now(),
2215                            room: None,
2216                            peer: None,
2217                        }),
2218                        Ok(crate::send::Incoming::Cmd(cmd)) => cmds.push((stream, cmd)),
2219                        Err(err) => {
2220                            crate::send::write_reply(&mut stream, &crate::SendReply::failed(err))
2221                        }
2222                    },
2223                    Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => break,
2224                    Err(_) => break,
2225                }
2226            }
2227        }
2228        for (mut stream, cmd) in cmds {
2229            let reply = match self.sessions.iter_mut().find(|store| {
2230                store.nick().is_some_and(|n| n.eq_ignore_ascii_case(cmd.as_nick.trim()))
2231            }) {
2232                Some(store) => store.run_command(&cmd, now_ms),
2233                None => crate::CmdReply::failed(format!("{} is not a session on this client", cmd.as_nick)),
2234            };
2235            crate::send::write_cmd_reply(&mut stream, &reply);
2236        }
2237        let queue = std::mem::take(&mut self.send_queue);
2238        for mut pending in queue {
2239            let (as_nick, to, text) = (
2240                pending.request.as_nick.clone(),
2241                pending.request.to.clone(),
2242                pending.request.text.clone(),
2243            );
2244            let outcome = self.try_send(
2245                &as_nick,
2246                &to,
2247                &text,
2248                now_ms,
2249                &mut pending.room,
2250                &mut pending.peer,
2251            );
2252            let reply = match outcome {
2253                Some(reply) => reply,
2254                None if pending.started.elapsed().as_secs() >= SEND_JOIN_WAIT_SECS => {
2255                    crate::SendReply {
2256                        room: pending.room.clone(),
2257                        ..crate::SendReply::failed(format!("{to} has not joined the DM yet"))
2258                    }
2259                }
2260                None => {
2261                    self.send_queue.push(pending);
2262                    continue;
2263                }
2264            };
2265            crate::send::write_reply(&mut pending.stream, &reply);
2266            report.sent.push((as_nick, to, reply));
2267        }
2268    }
2269
2270    /// Every routine POST attempted by every session, as (nick, attempt).
2271    /// Event ids and HTTP statuses only.
2272    pub fn wake_log(&self) -> Vec<(String, crate::WakeAttempt)> {
2273        self.sessions
2274            .iter()
2275            .flat_map(|store| {
2276                let nick = store.nick().unwrap_or("").to_string();
2277                store
2278                    .wake_log()
2279                    .iter()
2280                    .cloned()
2281                    .map(move |attempt| (nick.clone(), attempt))
2282            })
2283            .collect()
2284    }
2285
2286    /// Whether `name_or_nick` is a session this client holds.
2287    pub fn holds(&self, name_or_nick: &str) -> bool {
2288        let Ok(needle) = crate::nick::lookup_nick(name_or_nick) else {
2289            return false;
2290        };
2291        self.sessions.iter().any(|store| {
2292            store
2293                .nick()
2294                .is_some_and(|nick| nick.eq_ignore_ascii_case(&needle))
2295        })
2296    }
2297}
2298
2299fn server_name_of(mxid: &str) -> Result<&str, ShellError> {
2300    mxid.split_once(':')
2301        .map(|(_, server)| server)
2302        .filter(|server| !server.is_empty())
2303        .ok_or_else(|| ShellError::Register("user id has no server".to_string()))
2304}
2305
2306
2307/// A web session without a webhook routine, on a detected vendor host
2308/// (Claude web container, Codex cloud, Cursor cloud): install the
2309/// provider wake chain (hooks in the open session first, last-resort
2310/// spawn only when headless). Nothing changes on the Grok Bot box or for
2311/// a session that has a routine.
2312fn attach_detected_chain(store: &mut OpenedStore, config: &SessionConfig, nick: &str) {
2313    if let Some((session, chain)) = crate::provider::chain::detected_web_chain(
2314        config.session_id(),
2315        nick,
2316        &config.store_root,
2317    ) {
2318        store.set_wake_chain(session, chain);
2319    }
2320}
2321
2322#[cfg(test)]
2323mod tests {
2324    #[cfg(unix)]
2325    #[test]
2326    fn a_routine_file_gives_a_session_without_an_agent_id_its_wake_and_leaks_nothing() {
2327        use std::os::unix::fs::PermissionsExt;
2328        let dir = tempfile::tempdir().expect("dir");
2329        let file = dir.path().join("wake.json");
2330        let write = |text: &str, mode: u32| {
2331            std::fs::write(&file, text).expect("write");
2332            std::fs::set_permissions(&file, std::fs::Permissions::from_mode(mode)).expect("mode");
2333        };
2334        let mut set = vec![HostSession::new("Courier", "web-courier").with_routine_file(&file)];
2335
2336        // Owner-only file with a good pair: the session gets URL and bearer.
2337        write(r#"{"url":"http://127.0.0.1:9/hook","key":"k-secret"}"#, 0o600);
2338        attach_routine_files(&mut set);
2339        assert_eq!(set[0].routine_url.as_deref(), Some("http://127.0.0.1:9/hook"));
2340        assert_eq!(set[0].routine_bearer.as_deref(), Some("k-secret"));
2341        assert!(!format!("{:?}", set[0]).contains("k-secret"), "Debug must not show the key");
2342
2343        // A file others can read, a file of another shape, a blank key: no wake, and the error names no value.
2344        for (text, mode) in [(r#"{"url":"http://127.0.0.1:9/hook","key":"k-secret"}"#, 0o644), (r#"{"url":"http://127.0.0.1:9/hook","key":"k-secret","extra":1}"#, 0o600), (r#"{"url":"http://127.0.0.1:9/hook","key":" "}"#, 0o600), (r#"{"url":"not a url","key":"k-secret"}"#, 0o600)] {
2345            write(text, mode);
2346            let err = read_routine_file(&file).expect_err("refused");
2347            assert!(!err.contains("k-secret") && !err.contains("127.0.0.1"), "{err}");
2348            let mut one = vec![HostSession::new("Courier", "web-courier").with_routine_file(&file)];
2349            attach_routine_files(&mut one);
2350            assert!(one[0].routine_url.is_none() && one[0].routine_bearer.is_none());
2351        }
2352        // A session that already holds a wake keeps it.
2353        write(r#"{"url":"http://127.0.0.1:9/other","key":"k2"}"#, 0o600);
2354        let mut held = vec![HostSession::new("Courier", "web-courier").with_routine_file(&file).with_routine("http://127.0.0.1:9/mine", Some("mine".into()))];
2355        attach_routine_files(&mut held);
2356        assert_eq!(held[0].routine_url.as_deref(), Some("http://127.0.0.1:9/mine"));
2357    }
2358
2359    #[test]
2360    fn a_session_record_may_name_a_routine_file_but_never_carry_the_secret() {
2361        let dir = tempfile::tempdir().expect("dir");
2362        std::fs::write(dir.path().join("a.json"), r#"{"bot_name":"Courier","session_id":"web-courier","routine_file":"/some/where/wake.json"}"#).expect("write");
2363        let loaded = load_session_records(dir.path()).expect("records");
2364        assert_eq!(loaded[0].routine_file.as_deref(), Some(Path::new("/some/where/wake.json")));
2365        assert!(loaded[0].routine_url.is_none());
2366        std::fs::write(dir.path().join("a.json"), r#"{"bot_name":"Courier","session_id":"web-courier","routine_url":"http://127.0.0.1:9/h"}"#).expect("write");
2367        assert!(load_session_records(dir.path()).is_err(), "a URL in the record is still refused");
2368    }
2369
2370    use super::*;
2371
2372    #[test]
2373    fn web_client_agents_dir_is_only_the_env_path() {
2374        assert!(explicit_agents_dir(|_| None).is_none());
2375        let missing = std::env::temp_dir().join(format!(
2376            "m4a-missing-agents-{}",
2377            std::process::id()
2378        ));
2379        let _ = std::fs::remove_dir_all(&missing);
2380        assert!(explicit_agents_dir(|key| {
2381            (key == AGENTS_DIR_ENV).then(|| missing.display().to_string())
2382        })
2383        .is_none());
2384        let present = std::env::temp_dir().join(format!(
2385            "m4a-present-agents-{}-{}",
2386            std::process::id(),
2387            std::time::SystemTime::now()
2388                .duration_since(std::time::UNIX_EPOCH)
2389                .expect("clock")
2390                .as_nanos()
2391        ));
2392        std::fs::create_dir_all(&present).expect("dir");
2393        let got = explicit_agents_dir(|key| {
2394            (key == AGENTS_DIR_ENV).then(|| present.display().to_string())
2395        });
2396        assert_eq!(got.as_deref(), Some(present.as_path()));
2397        let _ = std::fs::remove_dir_all(&present);
2398    }
2399
2400    #[test]
2401    fn rescan_opens_only_bots_without_a_session() {
2402        let root = Path::new("/tmp/m4a-rescan-test");
2403        let agent = |name: &str, id: &str| {
2404            let mut session = HostSession::new(name, id);
2405            session.agent_id = Some(id.to_string());
2406            session
2407        };
2408        let found = vec![
2409            agent("alice", "a-hatch"),
2410            agent("m4a-proba2", "a-proba2"),
2411            agent("skipme", "a-skip"),
2412            agent("aliased", "a-alias"),
2413        ];
2414        let held = vec![
2415            (crate::session_store_dir(root, "a-hatch"), Some("alice".to_string())),
2416            (crate::session_store_dir(root, "old-alias"), None),
2417        ];
2418        let fresh = unopened_agent_sessions(
2419            found,
2420            &["skipme".to_string()],
2421            &[("a-alias".to_string(), "old-alias".to_string())],
2422            root,
2423            &held,
2424        );
2425        let nicks: Vec<&str> = fresh.iter().map(|(_, nick)| nick.as_str()).collect();
2426        assert_eq!(nicks, vec!["m4a-proba2"]);
2427        assert_eq!(fresh[0].0.session_id, "a-proba2");
2428    }
2429
2430    #[test]
2431    fn session_directory_is_name_and_id_only() {
2432        let dir = std::env::temp_dir().join(format!(
2433            "m4a-sessions-{}-{}",
2434            std::process::id(),
2435            std::time::SystemTime::now()
2436                .duration_since(std::time::UNIX_EPOCH)
2437                .expect("clock")
2438                .as_nanos()
2439        ));
2440        std::fs::create_dir_all(&dir).expect("dir");
2441        std::fs::write(
2442            dir.join("alice.json"),
2443            r#"{"bot_name":"Alice","session_id":"web-alice"}"#,
2444        )
2445        .expect("write");
2446        std::fs::write(
2447            dir.join("chief.json"),
2448            "{\"bot_name\":\"Привет мир\",\"session_id\":\"web-chief\"}",
2449        )
2450        .expect("write");
2451        let loaded = load_session_records(&dir).expect("records");
2452        assert_eq!(loaded.len(), 2);
2453        assert_eq!(loaded[1].bot_name, "Привет мир");
2454        assert_eq!(loaded[0].bot_name, "Alice");
2455        assert!(loaded[1].agent_id.is_none());
2456        assert!(loaded[0].agent_id.is_none());
2457        assert!(loaded[1].routine_url.is_none());
2458        assert!(loaded[1].routine_bearer.is_none());
2459        assert!(loaded[1].invite.is_none());
2460
2461        std::fs::write(
2462            dir.join("leaked.json"),
2463            r#"{"bot_name":"Courier","session_id":"web-courier","routine_url":"http://127.0.0.1/hook","routine_bearer":"not-a-file"}"#,
2464        )
2465        .expect("write");
2466        let refused = load_session_records(&dir).expect_err("webhook file");
2467        let text = refused.to_string();
2468        assert!(!text.contains("not-a-file"));
2469        assert!(!text.contains("127.0.0.1/hook"));
2470        let _ = std::fs::remove_dir_all(&dir);
2471    }
2472
2473    #[test]
2474    fn web_client_does_not_copy_one_routine_onto_every_session_and_node_cli_refuses_it() {
2475        let dir = std::env::temp_dir().join(format!(
2476            "m4a-split-{}-{}",
2477            std::process::id(),
2478            std::time::SystemTime::now()
2479                .duration_since(std::time::UNIX_EPOCH)
2480                .expect("clock")
2481                .as_nanos()
2482        ));
2483        std::fs::create_dir_all(&dir).expect("dir");
2484        let record = dir.join("alice.json");
2485        let body = r#"{"bot_name":"Alice","session_id":"web-alice"}"#;
2486        std::fs::write(&record, body).expect("write");
2487        let routine = "http://127.0.0.1:9/routine";
2488        let bearer = "host-injected-bearer";
2489        let sessions = load_web_sessions(&dir, |key| match key {
2490            crate::ROUTINE_URL_ENV => Some(routine.to_string()),
2491            crate::ROUTINE_BEARER_ENV => Some(bearer.to_string()),
2492            crate::LEADER_SOCK_ENV => Some("/tmp/leader.sock".to_string()),
2493            _ => None,
2494        })
2495        .expect("web sessions");
2496        assert_eq!(sessions.len(), 1);
2497        assert!(sessions[0].routine_url.is_none());
2498        assert!(sessions[0].routine_bearer.is_none());
2499        assert_eq!(std::fs::read_to_string(&record).expect("reread"), body);
2500        let wake = crate::SessionWake::web_from_lookup(|key| match key {
2501            crate::ROUTINE_URL_ENV => Some(routine.to_string()),
2502            crate::LEADER_SOCK_ENV => Some("/tmp/leader.sock".to_string()),
2503            _ => None,
2504        });
2505        assert!(wake.leader_sock.is_none());
2506        assert!(wake.leader_cwd.is_none());
2507
2508        std::fs::write(
2509            dir.join("leaked.json"),
2510            r#"{"bot_name":"Courier","session_id":"web-courier","routine_url":"http://127.0.0.1:9/from-file","routine_bearer":"file-bearer"}"#,
2511        )
2512        .expect("leak");
2513        let refused = load_web_sessions(&dir, |_| None).expect_err("json routine");
2514        let text = refused.to_string();
2515        assert!(!text.contains("from-file"));
2516        assert!(!text.contains("file-bearer"));
2517
2518        let node = crate::OpenedStore::connect_node_from_lookup(
2519            |key| match key {
2520                crate::ROUTINE_URL_ENV => Some(routine.to_string()),
2521                crate::ROUTINE_BEARER_ENV => Some(bearer.to_string()),
2522                crate::LEADER_SOCK_ENV => Some("/tmp/leader.sock".to_string()),
2523                _ => None,
2524            },
2525            None,
2526        );
2527        let err = match node {
2528            Ok(_) => panic!("node cli accepted a routine url"),
2529            Err(err) => err,
2530        };
2531        assert!(matches!(err, crate::ShellError::NodeRoutine));
2532        let text = err.to_string();
2533        assert!(!text.contains(routine));
2534        assert!(!text.contains(bearer));
2535        assert!(!text.contains("leader.sock"));
2536
2537        let node = crate::SessionWake::node_from_lookup(|key| match key {
2538            crate::LEADER_SOCK_ENV => Some("/tmp/node-leader.sock".to_string()),
2539            crate::LEADER_CWD_ENV => Some("/tmp/node".to_string()),
2540            _ => None,
2541        })
2542        .expect("node leader");
2543        assert!(node.routine_url.is_none());
2544        assert!(node.routine_bearer.is_none());
2545        assert_eq!(
2546            node.leader_sock.as_deref(),
2547            Some(std::path::Path::new("/tmp/node-leader.sock"))
2548        );
2549        assert_eq!(node.leader_cwd.as_deref(), Some("/tmp/node"));
2550        let _ = std::fs::remove_dir_all(&dir);
2551    }
2552
2553    #[test]
2554    fn mirrors_follow_the_nick_folder_and_keys_wait_for_the_bot_routine() {
2555        use std::collections::HashSet;
2556        use std::io::{Read, Write};
2557        use std::net::TcpListener;
2558        use std::sync::atomic::{AtomicBool, Ordering};
2559        use std::sync::{Arc, Mutex};
2560        use std::thread;
2561
2562        struct Card {
2563            agent: String,
2564            id: String,
2565            name: String,
2566            trigger: &'static str,
2567            enabled: bool,
2568        }
2569        #[derive(Default)]
2570        struct Gateway {
2571            creates: usize,
2572            deletes: usize,
2573            cards: Vec<Card>,
2574            // (agent, folder) pairs whose routine the bot created itself.
2575            backend: HashSet<(String, String)>,
2576            // Agents whose next mirror lands in `<folder>-2`.
2577            clash: HashSet<String>,
2578            agents_seen: HashSet<String>,
2579            profiles: Vec<(String, serde_json::Value)>,
2580        }
2581
2582        fn read_http(sock: &mut std::net::TcpStream) -> Option<(String, Vec<u8>)> {
2583            let _ = sock.set_read_timeout(Some(std::time::Duration::from_secs(2)));
2584            let mut buf = Vec::new();
2585            let mut tmp = [0u8; 2048];
2586            loop {
2587                let n = sock.read(&mut tmp).unwrap_or(0);
2588                if n == 0 {
2589                    return None;
2590                }
2591                buf.extend_from_slice(&tmp[..n]);
2592                let Some(end) = buf.windows(4).position(|w| w == b"\r\n\r\n") else {
2593                    continue;
2594                };
2595                let headers = String::from_utf8_lossy(&buf[..end]).to_string();
2596                let length = headers
2597                    .lines()
2598                    .find_map(|line| {
2599                        let (name, value) = line.split_once(':')?;
2600                        name.eq_ignore_ascii_case("content-length")
2601                            .then(|| value.trim().parse::<usize>().ok())
2602                            .flatten()
2603                    })
2604                    .unwrap_or(0);
2605                if buf.len() >= end + 4 + length {
2606                    return Some((headers, buf[end + 4..end + 4 + length].to_vec()));
2607                }
2608            }
2609        }
2610        fn reply(status: &str, body: &serde_json::Value) -> Vec<u8> {
2611            let payload = serde_json::to_vec(body).expect("json");
2612            let mut out = format!(
2613                "HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
2614                payload.len()
2615            )
2616            .into_bytes();
2617            out.extend(payload);
2618            out
2619        }
2620        fn cards_of(gate: &Gateway, agent: &str) -> serde_json::Value {
2621            serde_json::Value::Array(
2622                gate.cards
2623                    .iter()
2624                    .filter(|card| card.agent == agent)
2625                    .map(|card| {
2626                        serde_json::json!({
2627                            "id": card.id,
2628                            "name": card.name,
2629                            "trigger": {"type": card.trigger},
2630                            "isEnabled": card.enabled,
2631                        })
2632                    })
2633                    .collect(),
2634            )
2635        }
2636
2637        let listener = TcpListener::bind("127.0.0.1:0").expect("bind");
2638        listener.set_nonblocking(true).expect("nonblocking");
2639        let port = listener.local_addr().expect("addr").port();
2640        let state = Arc::new(Mutex::new(Gateway::default()));
2641        {
2642            let mut gate = state.lock().expect("gate");
2643            gate.backend.insert(("agent-h".into(), "alice".into()));
2644            gate.clash.insert("agent-x".into());
2645            gate.cards.push(Card {
2646                agent: "agent-k".into(),
2647                id: "cron-bot".into(),
2648                name: "cron_bot".into(),
2649                trigger: "cron",
2650                enabled: true,
2651            });
2652        }
2653        let shared = Arc::clone(&state);
2654        let token = "gw-test-token";
2655        let done = Arc::new(AtomicBool::new(false));
2656        let flag = Arc::clone(&done);
2657        let server = thread::spawn(move || {
2658            while !flag.load(Ordering::Relaxed) {
2659                let (mut sock, _) = match listener.accept() {
2660                    Ok(pair) => pair,
2661                    Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
2662                        thread::sleep(std::time::Duration::from_millis(5));
2663                        continue;
2664                    }
2665                    Err(_) => break,
2666                };
2667                let _ = sock.set_nonblocking(false);
2668                let Some((headers, body)) = read_http(&mut sock) else {
2669                    continue;
2670                };
2671                let lower = headers.to_ascii_lowercase();
2672                let authorized = lower.contains(&format!("authorization: bearer {token}"));
2673                let request: serde_json::Value = serde_json::from_slice(&body).unwrap_or_default();
2674                let agent = request["id"].as_str().unwrap_or("").to_string();
2675                let mut gate = shared.lock().expect("gate");
2676                gate.agents_seen.insert(agent.clone());
2677                let response = if !authorized {
2678                    reply("401 Unauthorized", &serde_json::json!({}))
2679                } else if lower.starts_with("post /api/getagentautomations ") {
2680                    reply("200 OK", &cards_of(&gate, &agent))
2681                } else if lower.starts_with("post /api/createagentautomation ") {
2682                    let spec = &request["spec"];
2683                    assert_eq!(spec["trigger"]["type"], "webhook");
2684                    assert_eq!(spec["isEnabled"], false, "mirror must be disabled");
2685                    let name = spec["name"].as_str().unwrap_or("").to_string();
2686                    let prompt = spec["prompt"].as_str().unwrap_or("");
2687                    assert!(!prompt.is_empty());
2688                    assert!(!prompt.contains("http"));
2689                    gate.creates += 1;
2690                    let folder = crate::nick::routine_folder_id(&name).expect("slug");
2691                    let id = if gate.clash.contains(&agent) {
2692                        format!("{folder}-2")
2693                    } else {
2694                        folder
2695                    };
2696                    gate.cards.push(Card {
2697                        agent: agent.clone(),
2698                        id,
2699                        name,
2700                        trigger: "webhook",
2701                        enabled: false,
2702                    });
2703                    reply("200 OK", &cards_of(&gate, &agent))
2704                } else if lower.starts_with("post /api/updateagent ") {
2705                    gate.profiles
2706                        .push((agent.clone(), request["profile"].clone()));
2707                    reply("200 OK", &serde_json::json!({"id": agent}))
2708                } else if lower.starts_with("post /api/deleteagentautomation ") {
2709                    let id = request["automationId"].as_str().unwrap_or("");
2710                    gate.deletes += 1;
2711                    gate.cards
2712                        .retain(|card| !(card.agent == agent && card.id == id));
2713                    reply("200 OK", &cards_of(&gate, &agent))
2714                } else if lower.starts_with("post /api/getautomationwebhookcredential ") {
2715                    let id = request["automationId"].as_str().unwrap_or("").to_string();
2716                    let local = gate
2717                        .cards
2718                        .iter()
2719                        .find(|card| card.agent == agent && card.id == id);
2720                    match local {
2721                        None => reply(
2722                            "500 Internal Server Error",
2723                            &serde_json::json!({"error": format!("Automation not found: {id}")}),
2724                        ),
2725                        Some(card) if card.trigger != "webhook" => reply(
2726                            "500 Internal Server Error",
2727                            &serde_json::json!({"error": "Automation is not webhook-triggered"}),
2728                        ),
2729                        Some(_) => {
2730                            let minted = gate.backend.contains(&(agent.clone(), id.clone()));
2731                            reply(
2732                                "200 OK",
2733                                &serde_json::json!({
2734                                    "url": format!("https://backend.invalid/automations/webhook/{agent}-{id}"),
2735                                    "key": minted.then(|| format!("key-{agent}-{id}")),
2736                                }),
2737                            )
2738                        }
2739                    }
2740                } else {
2741                    reply("404 Not Found", &serde_json::json!({}))
2742                };
2743                drop(gate);
2744                let _ = sock.write_all(&response);
2745            }
2746        });
2747
2748        let dir = std::env::temp_dir().join(format!(
2749            "m4a-mirror-{}-{}",
2750            std::process::id(),
2751            std::time::SystemTime::now()
2752                .duration_since(std::time::UNIX_EPOCH)
2753                .expect("clock")
2754                .as_nanos()
2755        ));
2756        let agents = dir.join("agents");
2757        let store_root = dir.join("stores");
2758        let describe = |id: &str| {
2759            if id == "agent-c" {
2760                "about the chief"
2761            } else {
2762                ""
2763            }
2764        };
2765        for (id, name) in [
2766            ("agent-h", "Alice"),
2767            ("agent-c", "Привет мир"),
2768            ("agent-s", "Свой браузер"),
2769            ("agent-x", "Clash Bot"),
2770            ("agent-k", "Cron Bot"),
2771        ] {
2772            std::fs::create_dir_all(agents.join(id)).expect("agent dir");
2773            std::fs::write(
2774                agents.join(id).join("profile.json"),
2775                serde_json::to_vec(&serde_json::json!({"name": name, "description": describe(id)}))
2776                    .expect("profile"),
2777            )
2778            .expect("profile");
2779        }
2780        let gateway = dir.join("gateway-file.json");
2781        std::fs::write(
2782            &gateway,
2783            format!(r#"{{"host":"192.0.2.1","port":{port},"scheme":"http","token":"{token}"}}"#),
2784        )
2785        .expect("gateway");
2786        let skip = parse_skip_nicks(" svoi-brauzer , ");
2787        assert_eq!(skip, vec!["svoi-brauzer".to_string()]);
2788        let options = WakeOptions {
2789            skip_nicks: skip.clone(),
2790            store_root: Some(store_root.clone()),
2791            profile_note: false,
2792            session_ids: Vec::new(),
2793        };
2794
2795        let status_of = |reports: &[RoutineReport], agent: &str| {
2796            reports
2797                .iter()
2798                .find(|report| report.agent_id == agent)
2799                .map(|report| report.status.clone())
2800        };
2801        let first =
2802            ensure_agent_webhook_routines(&agents, &gateway, None, &options).expect("first pass");
2803        assert_eq!(status_of(&first, "agent-h"), Some(WakeStatus::Ready));
2804        assert_eq!(
2805            status_of(&first, "agent-c"),
2806            Some(WakeStatus::AwaitingBackend)
2807        );
2808        assert!(matches!(
2809            status_of(&first, "agent-x"),
2810            Some(WakeStatus::Failed(_))
2811        ));
2812        assert!(matches!(
2813            status_of(&first, "agent-k"),
2814            Some(WakeStatus::Failed(_))
2815        ));
2816        assert_eq!(status_of(&first, "agent-s"), None);
2817        let chief = first
2818            .iter()
2819            .find(|r| r.agent_id == "agent-c")
2820            .expect("chief");
2821        assert_eq!(chief.nick, "privet-mir");
2822        assert_eq!(chief.folder_id, "privet-mir");
2823        let shown = format!("{first:?}");
2824        assert!(!shown.contains("key-"));
2825        assert!(!shown.contains("automations/webhook"));
2826        {
2827            let gate = state.lock().expect("gate");
2828            // alice, privet-mir, clash-bot. Not the skipped bot,
2829            // not the folder that already holds a cron routine.
2830            assert_eq!(gate.creates, 3);
2831            assert_eq!(gate.deletes, 1);
2832            assert!(!gate.agents_seen.contains("agent-s"));
2833            assert!(gate.cards.iter().all(|card| card.agent != "agent-x"));
2834            let cron = gate
2835                .cards
2836                .iter()
2837                .find(|card| card.agent == "agent-k")
2838                .expect("cron");
2839            assert_eq!(cron.trigger, "cron");
2840            assert!(cron.enabled);
2841            let mirror = gate
2842                .cards
2843                .iter()
2844                .find(|card| card.agent == "agent-h")
2845                .expect("mirror");
2846            assert_eq!(
2847                (mirror.id.as_str(), mirror.name.as_str()),
2848                ("alice", "alice")
2849            );
2850            let chief_mirror = gate
2851                .cards
2852                .iter()
2853                .find(|card| card.agent == "agent-c")
2854                .expect("chief mirror");
2855            // Nick == routine name == folder id, all with hyphens.
2856            assert_eq!(chief_mirror.name, "privet-mir");
2857            assert_eq!(chief_mirror.id, "privet-mir");
2858            assert!(!mirror.enabled);
2859        }
2860        // Keychain: only the ready bot, mode 0600, its folder recorded.
2861        let alice_file = keychain_path(&store_root, "agent-h");
2862        let stored: serde_json::Value =
2863            serde_json::from_slice(&std::fs::read(&alice_file).expect("keychain"))
2864                .expect("keychain json");
2865        assert_eq!(stored["folder_id"], "alice");
2866        assert_eq!(stored["key"], "key-agent-h-alice");
2867        #[cfg(unix)]
2868        {
2869            use std::os::unix::fs::PermissionsExt;
2870            let mode = std::fs::metadata(&alice_file)
2871                .expect("meta")
2872                .permissions()
2873                .mode();
2874            assert_eq!(mode & 0o777, 0o600);
2875        }
2876        assert!(!keychain_path(&store_root, "agent-c").exists());
2877
2878        // Second pass: nothing new is created; the chief still waits.
2879        let second =
2880            ensure_agent_webhook_routines(&agents, &gateway, None, &options).expect("second pass");
2881        assert_eq!(
2882            status_of(&second, "agent-c"),
2883            Some(WakeStatus::AwaitingBackend)
2884        );
2885        assert_eq!(
2886            state.lock().expect("gate").creates,
2887            4,
2888            "only the clash bot retries"
2889        );
2890        assert!(state.lock().expect("gate").profiles.is_empty());
2891        // Opt-in bootstrap note: only the waiting bot's profile is edited,
2892        // name kept, original description kept, note appended.
2893        let noted = WakeOptions {
2894            profile_note: true,
2895            ..options.clone()
2896        };
2897        ensure_agent_webhook_routines(&agents, &gateway, None, &noted).expect("note pass");
2898        {
2899            let gate = state.lock().expect("gate");
2900            assert_eq!(gate.profiles.len(), 1);
2901            let (agent, profile) = &gate.profiles[0];
2902            assert_eq!(agent, "agent-c");
2903            assert_eq!(profile["name"], "Привет мир");
2904            let description = profile["description"].as_str().unwrap_or("");
2905            assert!(description.starts_with("about the chief"));
2906            assert!(description.contains("routine named \"privet-mir\""));
2907            assert!(!description.contains("http"));
2908        }
2909        state.lock().expect("gate").creates = 4;
2910        // The chief creates its own routine; the next pass picks the key up
2911        // without another mirror.
2912        state
2913            .lock()
2914            .expect("gate")
2915            .backend
2916            .insert(("agent-c".into(), "privet-mir".into()));
2917        let third =
2918            ensure_agent_webhook_routines(&agents, &gateway, None, &options).expect("third pass");
2919        assert_eq!(status_of(&third, "agent-c"), Some(WakeStatus::Ready));
2920        assert_eq!(state.lock().expect("gate").creates, 5);
2921        assert!(keychain_path(&store_root, "agent-c").exists());
2922
2923        // The env path: the skipped bot is not a session at all.
2924        let gateway_path = gateway.display().to_string();
2925        let root_text = store_root.display().to_string();
2926        let sessions = load_web_agents(&agents, |key| match key {
2927            GATEWAY_FILE_ENV => Some(gateway_path.clone()),
2928            SKIP_NICKS_ENV => Some("svoi-brauzer".to_string()),
2929            crate::STORE_ROOT_ENV => Some(root_text.clone()),
2930            _ => None,
2931        })
2932        .expect("web agents");
2933        assert_eq!(sessions.len(), 4);
2934        let alice = sessions
2935            .iter()
2936            .find(|s| s.session_id == "agent-h")
2937            .expect("h");
2938        assert!(alice
2939            .routine_url
2940            .as_deref()
2941            .unwrap_or("")
2942            .starts_with("https://"));
2943        assert!(alice.routine_bearer.is_some());
2944        assert!(!format!("{alice:?}").contains("key-"));
2945
2946        // No gateway file: the keychain answers, but only for the same folder.
2947        let mut offline = vec![HostSession::new("Alice", "agent-h")];
2948        offline[0].agent_id = Some("agent-h".to_string());
2949        let mut renamed = HostSession::new("Alice Two", "agent-h");
2950        renamed.agent_id = Some("agent-h".to_string());
2951        offline.push(renamed);
2952        attach_webhook_routines(
2953            &mut offline,
2954            &dir.join("absent.json"),
2955            None,
2956            Some(&store_root),
2957        )
2958        .expect("offline");
2959        assert_eq!(
2960            offline[0].routine_bearer.as_deref(),
2961            Some("key-agent-h-alice")
2962        );
2963        assert!(offline[1].routine_url.is_none());
2964        let mut bare = vec![HostSession::new("Alice", "agent-h")];
2965        attach_webhook_routines(&mut bare, &dir.join("absent.json"), None, None).expect("bare");
2966        assert!(bare[0].routine_url.is_none());
2967
2968        done.store(true, Ordering::Relaxed);
2969        let _ = server.join();
2970        let _ = std::fs::remove_dir_all(&dir);
2971    }
2972
2973    #[test]
2974    fn wake_note_is_appended_once_and_replaced_in_place() {
2975        let first = with_wake_note("Runs the alice.", "alice").expect("added");
2976        assert!(first.starts_with("Runs the alice.\n\n<!-- mail4agent:wake -->"));
2977        assert!(first.ends_with("<!-- /mail4agent:wake -->"));
2978        assert!(with_wake_note(&first, "alice").is_none());
2979        let renamed = with_wake_note(&first, "alice-two").expect("replaced");
2980        assert_eq!(renamed.matches("<!-- mail4agent:wake -->").count(), 1);
2981        assert!(renamed.contains("\"alice-two\""));
2982        assert!(!renamed.contains("\"alice\""));
2983        assert!(renamed.starts_with("Runs the alice."));
2984        assert_eq!(with_wake_note("", "carol").expect("empty"), wake_note("carol"));
2985    }
2986
2987    #[test]
2988    fn agents_directory_becomes_sessions_without_hardcoded_ids() {
2989        let dir = std::env::temp_dir().join(format!(
2990            "m4a-agents-{}-{}",
2991            std::process::id(),
2992            std::time::SystemTime::now()
2993                .duration_since(std::time::UNIX_EPOCH)
2994                .expect("clock")
2995                .as_nanos()
2996        ));
2997        let alice = dir.join("agent-alice");
2998        let chief = dir.join("agent-chief");
2999        std::fs::create_dir_all(&alice).expect("dir");
3000        std::fs::create_dir_all(&chief).expect("dir");
3001        std::fs::write(
3002            alice.join("profile.json"),
3003            r#"{"name":"Alice","description":"x"}"#,
3004        )
3005        .expect("profile");
3006        std::fs::write(
3007            chief.join("profile.json"),
3008            concat!(
3009                "{\"name\":\"",
3010                "Привет мир",
3011                "\",\"description\":\"x\"}"
3012            ),
3013        )
3014        .expect("profile");
3015        std::fs::write(dir.join("active-agent.json"), "{}").expect("skip file");
3016        let loaded = load_agents_dir(&dir).expect("agents");
3017        assert_eq!(loaded.len(), 2);
3018        assert_eq!(loaded[1].session_id, "agent-chief");
3019        assert_eq!(loaded[1].agent_id.as_deref(), Some("agent-chief"));
3020        assert_eq!(loaded[1].bot_name, "Привет мир");
3021        assert_eq!(
3022            routine_name_for(&loaded[1]).as_deref(),
3023            Some("privet-mir")
3024        );
3025        assert_ne!(
3026            routine_name_for(&loaded[1]).as_deref(),
3027            Some(loaded[1].session_id.as_str())
3028        );
3029        assert_eq!(loaded[0].bot_name, "Alice");
3030        assert_eq!(routine_name_for(&loaded[0]).as_deref(), Some("alice"));
3031        assert!(loaded.iter().all(|session| session.routine_url.is_none()));
3032        let _ = std::fs::remove_dir_all(&dir);
3033    }
3034}