trusty-mpm 0.7.0

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Telegram-specific rendering of [`CommandResult`].
//!
//! Why: the executor returns a structured, UI-agnostic [`CommandResult`]; the
//! Telegram bot must turn that into HTML message text and, for the session
//! list, an inline keyboard. Keeping the rendering pure (no network, no
//! teloxide runtime) makes it unit-testable.
//! What: [`TelegramFormatter::format`] produces the HTML body and
//! [`TelegramFormatter::keyboard_for`] the optional inline keyboard.
//! Test: `cargo test -p trusty-mpm-telegram` covers each variant's rendering.

use crate::client::{CommandResult, DiscoveredProjectSummary};
use teloxide::types::{InlineKeyboardButton, InlineKeyboardMarkup};

#[cfg(test)]
mod tests;

/// How many characters of a session id to show in chat output.
///
/// Why: full UUIDs are unreadable on a phone; the first 8 chars disambiguate in
/// practice while keeping messages compact.
const SHORT_ID_LEN: usize = 8;

/// Renders [`CommandResult`]s into Telegram HTML messages and keyboards.
///
/// Why: the bot's message handler stays thin โ€” it executes a command and hands
/// the result here for presentation.
/// What: a stateless formatter; both methods are associated functions.
/// Test: the `format_*` tests in `tests.rs`.
pub struct TelegramFormatter;

impl TelegramFormatter {
    /// Render a [`CommandResult`] into an HTML message body.
    ///
    /// Why: every command's reply text is produced here so presentation is
    /// consistent and testable.
    /// What: matches each variant and returns an HTML-formatted string suitable
    /// for teloxide's `ParseMode::Html`.
    /// Test: `format_sessions_*`, `pair_code_command_formats_correctly`, etc.
    pub fn format(result: &CommandResult) -> String {
        match result {
            CommandResult::Sessions(sessions) => {
                if sessions.is_empty() {
                    return "No active sessions.".to_string();
                }
                let mut text = String::from("<b>trusty-mpm sessions</b>\n");
                for s in sessions {
                    let dot = if s.status.eq_ignore_ascii_case("active") {
                        "๐ŸŸข"
                    } else {
                        "๐Ÿ”ด"
                    };
                    text.push_str(&format!(
                        "\n{dot} <code>{}</code> โ€” {}\n  ๐Ÿ“ <code>{}</code>\n",
                        short_id(&s.id),
                        s.status,
                        s.workdir,
                    ));
                }
                text
            }
            CommandResult::SessionDetail { id, events, .. } => {
                if events.is_empty() {
                    format!("Session {id}: no recent events")
                } else {
                    let lines = events
                        .iter()
                        .map(|e| format!("โ€ข {e}"))
                        .collect::<Vec<_>>()
                        .join("\n");
                    format!("<b>Session {}</b>\n{lines}", short_id(id))
                }
            }
            CommandResult::OverseerStatus {
                enabled,
                handler,
                decisions,
            } => format!(
                "<b>Overseer Status</b>\nHandler: <code>{handler}</code>\nEnabled: {}\n\
                 Recent decisions: allow ({}), block ({}), flag ({})",
                if *enabled { "โœ…" } else { "โŒ" },
                decisions.allow,
                decisions.block,
                decisions.flag,
            ),
            CommandResult::TmuxSessions(sessions) => {
                if sessions.is_empty() {
                    return "No tmux sessions found.".to_string();
                }
                let lines = sessions
                    .iter()
                    .map(|s| {
                        let tag = if s.managed {
                            "๐ŸŸข managed"
                        } else {
                            "โšช external"
                        };
                        format!("โ€ข <code>{}</code> โ€” {tag}", s.name)
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                format!("<b>tmux sessions</b>\n{lines}")
            }
            CommandResult::DiscoveredProjects(projects) => format_discovered_projects(projects),
            CommandResult::Adopted { session } => {
                format!("โœ… Adopted tmux session <code>{session}</code> for oversight")
            }
            CommandResult::Discovered { count } => {
                if *count == 0 {
                    "๐Ÿ” No new Claude Code tmux sessions found".to_string()
                } else {
                    format!("๐Ÿ” Discovered and adopted {count} Claude Code tmux session(s)")
                }
            }
            CommandResult::ProjectRegistered { path } => {
                format!("โœ… Registered project <code>{path}</code>")
            }
            CommandResult::ConfigAnalysis {
                project,
                recommendations,
            } => {
                if recommendations.is_empty() {
                    format!(
                        "<b>Claude config</b> for <code>{project}</code>\n\
                         No recommendations โ€” config looks healthy."
                    )
                } else {
                    let lines = recommendations
                        .iter()
                        .map(|r| format!("โ€ข {}", r.message))
                        .collect::<Vec<_>>()
                        .join("\n");
                    format!("<b>Claude config</b> for <code>{project}</code>\n{lines}")
                }
            }
            CommandResult::Snapshot { session, output } => {
                let tail: Vec<&str> = output.lines().rev().take(50).collect();
                let lines = tail.into_iter().rev().collect::<Vec<_>>().join("\n");
                if lines.is_empty() {
                    format!("Session <code>{session}</code>: empty pane")
                } else {
                    format!(
                        "<b>Snapshot: {session}</b>\n<pre>{}</pre>",
                        html_escape(&lines)
                    )
                }
            }
            CommandResult::Killed { session_id } => {
                format!("๐Ÿ—‘๏ธ Session {} killed", short_id(session_id))
            }
            CommandResult::CommandSent { session, output } => {
                if output.trim().is_empty() {
                    format!("๐Ÿ“จ Sent to <code>{session}</code> โ€” no output captured")
                } else {
                    format!("<b>๐Ÿ“จ {session}</b>\n<pre>{}</pre>", html_escape(output))
                }
            }
            CommandResult::ChatReply { reply } => html_escape(reply),
            CommandResult::Approved { session_id } => {
                format!("โœ… Permission approved for session {session_id}")
            }
            CommandResult::Denied { session_id } => {
                format!("โŒ Permission denied for session {session_id}")
            }
            CommandResult::PairCode {
                code,
                expires_in_seconds,
            } => format!(
                "<b>Pairing code:</b> <code>{code}</code>\n\
                 Expires in {} minutes\n\nSend to your bot: <code>/pair {code}</code>",
                expires_in_seconds / 60,
            ),
            CommandResult::PairSuccess { chat_info } => {
                format!(
                    "โœ… Successfully paired! This chat ({chat_info}) is now registered for alerts."
                )
            }
            CommandResult::PairState { paired } => {
                if *paired {
                    "โœ… Bot is paired with this daemon. Type /help to see commands.".to_string()
                } else {
                    "๐Ÿ‘‹ Welcome to trusty-mpm bot! To pair this bot with your daemon, run \
                     `tm pair` on your server, then send the code with /pair <code>"
                        .to_string()
                }
            }
            CommandResult::AlertSubscriptions(lines) => {
                format!("<b>Alert subscription</b>\n{}", lines.join("\n"))
            }
            CommandResult::Doctor(report) => format_doctor_report(report),
            CommandResult::SessionStarted {
                session,
                workdir,
                deployed,
            } => {
                let mode = if *deployed {
                    "launched (framework deployed)"
                } else {
                    "connected (no deployment)"
                };
                format!("โœ… Session <b>{session}</b> {mode}\n<code>{workdir}</code>")
            }
            CommandResult::Help(text) => text.clone(),
            CommandResult::Error(msg) => format!("โŒ {msg}"),
        }
    }

    /// Build the inline keyboard for a [`CommandResult`], if it warrants one.
    ///
    /// Why: several lists decorate their rows with action buttons โ€” `/sessions`
    /// gets `[Status] [Approve] [Deny]`, `/projects` gets a `[Set Active]`
    /// button per project, and `/tmux` gets an `[Adopt]` button for each
    /// unmanaged session.
    /// What: returns one button row per item for the list variants above,
    /// `None` for every other variant. Callback data is `verb:arg` so the
    /// callback handler can route the tap.
    /// Test: `keyboard_for_sessions_has_rows`, `keyboard_for_projects_has_rows`,
    /// `keyboard_for_tmux_adopts_external`, `keyboard_for_help_is_none`.
    pub fn keyboard_for(result: &CommandResult) -> Option<InlineKeyboardMarkup> {
        match result {
            CommandResult::Sessions(sessions) if !sessions.is_empty() => {
                let rows: Vec<Vec<InlineKeyboardButton>> = sessions
                    .iter()
                    .map(|s| {
                        vec![
                            InlineKeyboardButton::callback("๐Ÿ“‹ Status", format!("status:{}", s.id)),
                            InlineKeyboardButton::callback(
                                "โœ… Approve",
                                format!("approve:{}", s.id),
                            ),
                            InlineKeyboardButton::callback("โŒ Deny", format!("deny:{}", s.id)),
                        ]
                    })
                    .collect();
                Some(InlineKeyboardMarkup::new(rows))
            }
            CommandResult::DiscoveredProjects(projects) if !projects.is_empty() => {
                let rows: Vec<Vec<InlineKeyboardButton>> = projects
                    .iter()
                    .filter(|p| callback_fits(&p.path))
                    .map(|p| {
                        vec![InlineKeyboardButton::callback(
                            format!("๐Ÿ“ Set Active โ€” {}", project_basename(&p.path)),
                            format!("setproj:{}", p.path),
                        )]
                    })
                    .collect();
                if rows.is_empty() {
                    None
                } else {
                    Some(InlineKeyboardMarkup::new(rows))
                }
            }
            CommandResult::TmuxSessions(sessions) => {
                let rows: Vec<Vec<InlineKeyboardButton>> = sessions
                    .iter()
                    .filter(|s| !s.managed && callback_fits(&s.name))
                    .map(|s| {
                        vec![InlineKeyboardButton::callback(
                            format!("โž• Adopt โ€” {}", s.name),
                            format!("adopt:{}", s.name),
                        )]
                    })
                    .collect();
                if rows.is_empty() {
                    None
                } else {
                    Some(InlineKeyboardMarkup::new(rows))
                }
            }
            _ => None,
        }
    }
}

/// Render a discovered-project list as a Telegram HTML message body.
///
/// Why: the `/projects` command lists projects mined from `~/.claude/projects/`;
/// keeping the rendering as a free function lets it be unit-tested and reused.
/// What: returns a placeholder line when the list is empty, otherwise one line
/// per project showing the path, recorded session count, and last-used date.
/// Test: `format_discovered_projects_lists_each`, `format_discovered_projects_empty`.
pub fn format_discovered_projects(projects: &[DiscoveredProjectSummary]) -> String {
    if projects.is_empty() {
        return "No projects discovered in Claude Code config.".to_string();
    }
    let mut text = String::from("<b>Discovered projects</b>\n");
    for p in projects {
        let last = p
            .last_session
            .as_deref()
            .map(|s| s.split('T').next().unwrap_or(s).to_string())
            .unwrap_or_else(|| "never".to_string());
        text.push_str(&format!(
            "\n๐Ÿ“ <code>{}</code>\n  {} session(s) ยท last used {last}\n",
            p.path, p.session_count,
        ));
    }
    text
}

/// Render a [`DoctorReport`] as a Telegram HTML message body.
///
/// Why: the `/doctor` command's diagnostic must be readable on a phone โ€” one
/// emoji-tagged line per check plus an overall verdict.
/// What: returns a heading, one `<icon> <name> โ€” <message>` line per check, and
/// a final overall-status line.
/// Test: `format_doctor_report_lists_each_check`.
fn format_doctor_report(report: &crate::client::DoctorReport) -> String {
    let mut text = String::from("<b>trusty-mpm doctor</b>\n");
    for check in &report.checks {
        text.push_str(&format!(
            "\n{} <b>{}</b> โ€” {}",
            status_icon(check.status),
            html_escape(&check.name),
            html_escape(&check.message),
        ));
    }
    text.push_str(&format!(
        "\n\n{} <b>overall: {}</b>",
        status_icon(report.overall),
        status_word(report.overall),
    ));
    text
}

/// The emoji icon for a doctor [`CheckStatus`](crate::core::doctor::CheckStatus).
///
/// Why: the `/doctor` message marks each check with a glanceable status symbol.
/// What: `Ok โ†’ โœ…`, `Warn โ†’ โš ๏ธ`, `Fail โ†’ โŒ`.
/// Test: covered by `format_doctor_report_lists_each_check`.
fn status_icon(status: crate::core::doctor::CheckStatus) -> &'static str {
    use crate::core::doctor::CheckStatus;
    match status {
        CheckStatus::Ok => "โœ…",
        CheckStatus::Warn => "โš ๏ธ",
        CheckStatus::Fail => "โŒ",
    }
}

/// A one-word label for a doctor [`CheckStatus`](crate::core::doctor::CheckStatus).
///
/// Why: the overall verdict reads better with a word than a bare icon.
/// What: `Ok โ†’ "healthy"`, `Warn โ†’ "warnings"`, `Fail โ†’ "failed"`.
/// Test: covered by `format_doctor_report_lists_each_check`.
fn status_word(status: crate::core::doctor::CheckStatus) -> &'static str {
    use crate::core::doctor::CheckStatus;
    match status {
        CheckStatus::Ok => "healthy",
        CheckStatus::Warn => "warnings",
        CheckStatus::Fail => "failed",
    }
}

/// True when a string fits within Telegram's 64-byte callback-data budget.
///
/// Why: Telegram rejects inline-keyboard callback data over 64 bytes; a `verb:`
/// prefix (โ‰ค8 bytes) plus the argument must stay under the limit, so a button
/// is omitted rather than crashing the bot when the argument is too long.
/// What: returns true when `arg`'s byte length leaves room for the prefix.
/// Test: covered by `keyboard_for_projects_has_rows`.
fn callback_fits(arg: &str) -> bool {
    arg.len() <= 55
}

/// Extract the final path component for a compact button label.
///
/// Why: a full project path is too long for an inline-keyboard button label;
/// the directory name alone identifies the project at a glance.
/// What: returns the last `/`-separated component, or the whole string when it
/// has no separator.
/// Test: covered by `keyboard_for_projects_has_rows`.
fn project_basename(path: &str) -> &str {
    path.rsplit('/')
        .next()
        .filter(|s| !s.is_empty())
        .unwrap_or(path)
}

/// Shorten a session id for compact chat display.
///
/// Why: full UUIDs do not fit comfortably on a phone screen.
/// What: returns the first [`SHORT_ID_LEN`] chars plus an ellipsis, or the id
/// unchanged when already short.
/// Test: `short_id_truncates_long_ids`.
fn short_id(id: &str) -> String {
    if id.len() > SHORT_ID_LEN {
        format!("{}โ€ฆ", &id[..SHORT_ID_LEN])
    } else {
        id.to_string()
    }
}

/// Escape the three HTML-significant characters for teloxide's HTML parse mode.
///
/// Why: snapshot output is arbitrary terminal text; un-escaped `<`/`>`/`&`
/// would break the message or be silently dropped by Telegram.
/// What: replaces `&`, `<`, `>` with their HTML entities.
/// Test: covered indirectly by the snapshot formatting test.
pub fn html_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}