mahbot 0.6.5

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Shared agent-session rendering layer — one view-model ("ledger") plus
//! shared components used by both the Sessions page (full historical
//! transcript) and the Running Agents page (live compact projection). The
//! compact view is a projection of the same ledger, never a second data
//! structure.

use std::borrow::Cow;
use std::ops::Range;

use iced::widget::markdown;

use crate::ChatMessage;
use crate::agent::registry::RunningTool;
use crate::providers::plaintext_for_display;
use crate::session::{DecodedNativeHistoryMessage, decode_native_history_message};
use crate::util::none_if_empty;

use super::markdown_breaks;
use super::media_markers;

pub(crate) mod preview;
mod render;

// Shared components re-exported for the Sessions page (full view) and the
// Running Agents page (compact projection).
pub(crate) use render::{
    MAX_TOOL_TOOLTIP_WIDTH, ToolBlockView, collapse_control_chars, tool_block, truncate_at_boundary,
};

/// One entry of the flat session ledger: a regular message (system/user/
/// assistant, optionally carrying a thinking block) or one assistant
/// tool-call round (one assistant message's calls plus their
/// tool_call_id-matched results).
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum SessionEntry {
    Message {
        role: crate::ChatRole,
        /// Visible body text (thinking extracted). None = empty.
        content: Option<String>,
        /// Thinking text: the decoded Reasoning when present, else a literal
        /// [thinking] block extracted from the content.
        thinking: Option<String>,
    },
    ToolRound {
        /// Assistant text content that appeared alongside the calls.
        narration: Option<String>,
        /// The assistant message's decoded long Reasoning block.
        reasoning: Option<String>,
        calls: Vec<ToolCallEntry>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ToolCallEntry {
    pub tool_call_id: String,
    /// Full unscrubbed args pairs (RunningTool flattening).
    pub tool: RunningTool,
    /// tool_call_id-matched result content.
    pub result: Option<String>,
}

/// Split a decoded assistant message into (thinking, visible body).
/// The decoded Reasoning wins; otherwise a literal `[thinking]...[/thinking]`
/// pair is extracted from the content, panic-safely: the closer is searched
/// only after the opener (historical slice-panic regression — see tests).
fn split_thinking(
    content: Option<String>,
    reasoning: Option<String>,
) -> (Option<String>, Option<String>) {
    if let Some(reasoning) = reasoning
        && !reasoning.is_empty()
    {
        return (Some(reasoning), content.and_then(|c| none_if_empty(&c)));
    }
    let Some(content) = content else {
        return (None, None);
    };
    if let Some(thinking_start) = content.find("[thinking]") {
        let body_start = thinking_start + "[thinking]".len();
        if let Some(rel_end) = content[body_start..].find("[/thinking]") {
            let end = body_start + rel_end;
            let thinking = content[body_start..end].trim().to_string();
            let after = content[end + "[/thinking]".len()..].trim().to_string();
            return (Some(thinking), none_if_empty(&after));
        }
    }
    (None, none_if_empty(&content))
}

/// [`build_ledger_with_sources`]'s entries alone, for callers that never need
/// the stored-row mapping.
pub(crate) fn build_ledger(messages: &[ChatMessage]) -> Vec<SessionEntry> {
    build_ledger_with_sources(messages).0
}

/// Decode the native history messages into the flat session ledger, plus a
/// parallel `sources` vec mapping each entry back to the message row that
/// CREATED it: the assistant tool-call frame for a [`SessionEntry::ToolRound`],
/// the message row itself for a [`SessionEntry::Message`].
///
/// Pure and deterministic: each assistant message becomes a regular message
/// (with an optional thinking block) or a tool round, and each tool result
/// attaches to the first unmatched call of the nearest preceding tool round;
/// stray results render like a regular message — so `sources` is NOT the
/// identity.
///
/// Contract: `sources` is strictly increasing, and cutting stored rows from
/// `sources[i]` on removes exactly the entries `i..` — the Sessions page's
/// truncate relies on both.
pub(crate) fn build_ledger_with_sources(
    messages: &[ChatMessage],
) -> (Vec<SessionEntry>, Vec<usize>) {
    let mut entries: Vec<SessionEntry> = Vec::new();
    let mut sources: Vec<usize> = Vec::new();
    for (index, message) in messages.iter().enumerate() {
        let before = entries.len();
        match decode_native_history_message(message) {
            Some(DecodedNativeHistoryMessage::Assistant {
                content,
                tool_calls,
                reasoning,
            }) => {
                if let Some(calls) = tool_calls.filter(|calls| !calls.is_empty()) {
                    entries.push(SessionEntry::ToolRound {
                        narration: content.filter(|c| !c.is_empty()),
                        reasoning: plaintext_for_display(reasoning.as_ref()),
                        calls: calls
                            .iter()
                            .map(|call| ToolCallEntry {
                                tool_call_id: call.id.clone(),
                                tool: RunningTool::from_tool_call(call),
                                result: None,
                            })
                            .collect(),
                    });
                } else {
                    // No (or an empty) tool-call list: a regular assistant
                    // message, possibly carrying a thinking block.
                    let (thinking, body) =
                        split_thinking(content, plaintext_for_display(reasoning.as_ref()));
                    entries.push(SessionEntry::Message {
                        role: message.role,
                        content: body,
                        thinking,
                    });
                }
            }
            Some(DecodedNativeHistoryMessage::ToolResult {
                tool_call_id,
                content,
            }) => {
                let mut remaining = Some(content);
                let mut attached = false;
                for entry in entries.iter_mut().rev() {
                    if let SessionEntry::ToolRound { calls, .. } = entry
                        && let Some(call) = calls
                            .iter_mut()
                            .find(|c| c.tool_call_id == tool_call_id && c.result.is_none())
                    {
                        call.result = remaining.take();
                        attached = true;
                        break;
                    }
                }
                if !attached {
                    entries.push(SessionEntry::Message {
                        role: message.role,
                        content: remaining,
                        thinking: None,
                    });
                }
            }
            _ => {
                entries.push(SessionEntry::Message {
                    role: message.role,
                    content: Some(message.content.clone()),
                    thinking: None,
                });
            }
        }
        // A message produces at most one entry, so the delta is that entry's
        // source row.
        if entries.len() != before {
            sources.push(index);
        }
    }
    (entries, sources)
}

/// Per-entry markdown parse for bubble bodies: Message → its content,
/// ToolRound → its narration. None = nothing to render.
pub(crate) fn parse_entry_bodies(entries: &[SessionEntry]) -> Vec<Option<Vec<markdown::Item>>> {
    entries
        .iter()
        .map(|entry| {
            let display = match entry {
                SessionEntry::Message { content, .. } => content.as_deref()?,
                SessionEntry::ToolRound { narration, .. } => narration.as_deref()?,
            };
            let processed = media_markers::preprocess(display);
            let escaped = escape_html_blocks(&processed);
            let with_breaks = markdown_breaks::hard_breaks(&escaped);
            Some(markdown::parse(&with_breaks).collect())
        })
        .collect()
}

/// The reasoning promoted into a tool round's narration slot, if any: the
/// assistant text content is empty (whitespace-only counts as empty) while a
/// decoded Reasoning block is present. Render-only signal — the ledger itself
/// is untouched, empty content stays empty in the history and in the model
/// replay. Used by both the Sessions page (narration body) and the Running
/// Agents page (trace-group label fallback).
pub(crate) fn promoted_reasoning<'a>(
    narration: Option<&'a str>,
    reasoning: Option<&'a str>,
) -> Option<&'a str> {
    match reasoning {
        Some(reasoning) if narration.is_none_or(|n| n.trim().is_empty()) => Some(reasoning),
        _ => None,
    }
}

/// Escape `<` → `&lt;` only inside the regions the markdown parser classifies
/// as HTML (block-level `Event::Html` plus inline `Event::InlineHtml`), so
/// angle-bracket tag blocks (`<system-notification>`, `<analyze-tool-result>`,
/// `<timestamp>` …) render as visible literal text instead of being dropped
/// wholesale.
///
/// This is deliberately a `<`-only escape: `util::escape_html` is not reused
/// (it also escapes `>` — breaking blockquotes — and `&`, which would
/// double-encode existing entities).
fn escape_html_blocks(text: &str) -> Cow<'_, str> {
    if !text.contains('<') {
        return Cow::Borrowed(text);
    }
    let mut ranges: Vec<Range<usize>> = Vec::new();
    for (event, range) in
        pulldown_cmark::Parser::new_ext(text, markdown_breaks::iced_markdown_options())
            .into_offset_iter()
    {
        match event {
            pulldown_cmark::Event::Html(_) | pulldown_cmark::Event::InlineHtml(_) => {
                ranges.push(range);
            }
            _ => {}
        }
    }
    if ranges.is_empty() {
        return Cow::Borrowed(text);
    }
    // Events arrive in document order with disjoint ranges; merge overlapping
    // or adjacent ones so multi-line HTML blocks collapse into single
    // replacement regions.
    ranges.sort_by_key(|r| (r.start, r.end));
    let mut merged: Vec<Range<usize>> = Vec::with_capacity(ranges.len());
    for range in ranges {
        if let Some(last) = merged.last_mut() {
            if range.start <= last.end {
                last.end = last.end.max(range.end);
                continue;
            }
        }
        merged.push(range);
    }
    let mut out = String::with_capacity(text.len() + merged.len() * 4);
    let mut pos = 0;
    for range in &merged {
        out.push_str(&text[pos..range.start]);
        out.push_str(&text[range.clone()].replace('<', "&lt;"));
        pos = range.end;
    }
    out.push_str(&text[pos..]);
    Cow::Owned(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ChatRole;

    #[test]
    fn split_thinking_reversed_markers_never_panics() {
        // Regression: a literal "[/thinking]" before "[thinking]" previously
        // sliced `content[body_start..end]` with `end < body_start`
        // ("byte range starts at X but ends at Y"), aborting the GUI. The
        // closer is searched only after the opener, so this input resolves to
        // plain body text instead of crashing.
        let (thinking, body) =
            split_thinking(Some("[/thinking] before [thinking]".to_string()), None);
        assert_eq!(thinking, None);
        assert_eq!(body.as_deref(), Some("[/thinking] before [thinking]"));
    }

    #[test]
    fn split_thinking_extracts_first_pair_only() {
        let (thinking, body) = split_thinking(
            Some(
                "intro [thinking] inner [/thinking] mid [thinking] later [/thinking] tail"
                    .to_string(),
            ),
            None,
        );
        assert_eq!(thinking.as_deref(), Some("inner"));
        // Only the first pair is consumed; a later literal block stays visible
        // body text.
        assert_eq!(
            body.as_deref(),
            Some("mid [thinking] later [/thinking] tail")
        );
    }

    #[test]
    fn split_thinking_decoded_reasoning_wins() {
        let (thinking, body) = split_thinking(
            Some("[thinking] literal [/thinking]".to_string()),
            Some("decoded reasoning".to_string()),
        );
        assert_eq!(thinking.as_deref(), Some("decoded reasoning"));
        assert_eq!(body.as_deref(), Some("[thinking] literal [/thinking]"));
    }

    #[test]
    fn empty_tool_call_list_never_forms_a_tool_round() {
        let messages = vec![ChatMessage {
            role: ChatRole::Assistant,
            content: r#"{"content":"hello","tool_calls":[]}"#.to_string(),
        }];
        let entries = build_ledger(&messages);
        match &entries[..] {
            [SessionEntry::Message { content, .. }] => {
                assert_eq!(content.as_deref(), Some("hello"));
            }
            other => panic!("expected a single Message entry, got {other:?}"),
        }
    }

    #[test]
    fn ledger_sources_track_the_source_row_of_each_entry() {
        // The tool result attaches to its round instead of adding an entry, so
        // the entry→message map is not the identity.
        let messages = vec![
            ChatMessage::user("go"),
            assistant_tool_call(Some("checking"), None),
            ChatMessage::tool_result("call_1", "result"),
            ChatMessage::user("next"),
        ];
        let (_entries, sources) = build_ledger_with_sources(&messages);
        assert_eq!(sources, vec![0, 1, 3]);
    }

    /// Build an assistant tool-call message with an optional decoded `content`
    /// (null when `None`) and an optional `reasoning` field, so the ledger's
    /// `ToolRound` carries the promoted-reasoning signal under test.
    fn assistant_tool_call(content: Option<&str>, reasoning: Option<&str>) -> ChatMessage {
        let calls = vec![serde_json::json!({
            "id": "call_1",
            "name": "read",
            "arguments": serde_json::to_string(&serde_json::json!({"path": "a.rs"})).unwrap(),
        })];
        let mut body = serde_json::json!({ "content": content, "tool_calls": calls });
        if let Some(reasoning) = reasoning {
            body["reasoning"] = serde_json::json!(reasoning);
        }
        ChatMessage::assistant(body.to_string())
    }

    #[test]
    fn promoted_reasoning_textless_round_with_reasoning() {
        let messages = vec![assistant_tool_call(None, Some("Checking the file first"))];
        let entries = build_ledger(&messages);
        match &entries[..] {
            [
                SessionEntry::ToolRound {
                    narration,
                    reasoning,
                    ..
                },
            ] => {
                assert_eq!(narration.as_deref(), None);
                assert_eq!(reasoning.as_deref(), Some("Checking the file first"));
                assert_eq!(
                    promoted_reasoning(narration.as_deref(), reasoning.as_deref()),
                    Some("Checking the file first"),
                    "null content + reasoning promotes"
                );
            }
            other => panic!("expected a single ToolRound entry, got {other:?}"),
        }
    }

    #[test]
    fn promoted_reasoning_visible_content_does_not_promote() {
        let messages = vec![assistant_tool_call(
            Some("Doing it"),
            Some("Reasoning here"),
        )];
        let entries = build_ledger(&messages);
        match &entries[..] {
            [
                SessionEntry::ToolRound {
                    narration,
                    reasoning,
                    ..
                },
            ] => {
                assert_eq!(narration.as_deref(), Some("Doing it"));
                assert!(
                    promoted_reasoning(narration.as_deref(), reasoning.as_deref()).is_none(),
                    "visible content blocks promotion"
                );
            }
            other => panic!("expected a single ToolRound entry, got {other:?}"),
        }
    }

    #[test]
    fn promoted_reasoning_whitespace_content_counts_as_empty() {
        let messages = vec![assistant_tool_call(Some("   "), Some("Reasoning here"))];
        let entries = build_ledger(&messages);
        match &entries[..] {
            [
                SessionEntry::ToolRound {
                    narration,
                    reasoning,
                    ..
                },
            ] => {
                assert_eq!(narration.as_deref(), Some("   "));
                assert!(
                    promoted_reasoning(narration.as_deref(), reasoning.as_deref()).is_some(),
                    "whitespace-only content counts as empty"
                );
            }
            other => panic!("expected a single ToolRound entry, got {other:?}"),
        }
    }

    #[test]
    fn promoted_reasoning_without_reasoning_does_not_promote() {
        let messages = vec![assistant_tool_call(None, None)];
        let entries = build_ledger(&messages);
        match &entries[..] {
            [
                SessionEntry::ToolRound {
                    narration,
                    reasoning,
                    ..
                },
            ] => {
                assert_eq!(narration.as_deref(), None);
                assert_eq!(reasoning.as_deref(), None);
                assert!(
                    promoted_reasoning(narration.as_deref(), reasoning.as_deref()).is_none(),
                    "no reasoning → no promotion"
                );
            }
            other => panic!("expected a single ToolRound entry, got {other:?}"),
        }
    }
}