aidaemon-channels 0.29.0

Chat channel contract and the runner that connects channels to aidaemon agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Shared presentation rules for outbound chat text.
//!
//! Keep channel messages readable here instead of teaching every notification
//! producer how to lay out a mobile-friendly card.

use regex::{Captures, Regex};
use std::sync::LazyLock as Lazy;

const MAX_INLINE_STEP_CHARS: usize = 180;
const MAX_SCHEDULED_FAILURE_CHARS: usize = 900;

static URL_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"https?://[^\s<>]+").expect("valid outbound URL regex"));
static LOCAL_PATH_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(?m)(^|[\s(])((?:~/|/)[^\s,;]+)").expect("valid local path regex"));
/// The lead the daemon writes above a finished goal's results. The
/// notification heading says the same, so the presenter drops it.
pub const GOAL_COMPLETED_LEAD: &str = "Goal completed:";
/// The lead the daemon writes above a failed goal's summary.
pub const GOAL_FAILED_LEAD: &str = "Goal failed:";
/// The lead the daemon writes above a stalled goal's notice.
pub const GOAL_STALLED_LEAD: &str = "Goal stalled:";

/// The count line the daemon writes above a goal's task results.
pub fn task_count_line(done: usize, total: usize) -> String {
    format!("{done}/{total} tasks completed.")
}

/// Whether `line` is the daemon's own [`task_count_line`]. A machine format
/// the daemon writes, matched exactly, never a paraphrase.
fn is_task_count_line(line: &str) -> bool {
    let Some(counts) = line.strip_suffix(" tasks completed.") else {
        return false;
    };
    let Some((done, total)) = counts.split_once('/') else {
        return false;
    };
    done.parse::<usize>().is_ok() && total.parse::<usize>().is_ok()
}

/// Replace the daemon's internal markers (refusal prefixes, redaction
/// placeholders, file-inbox paths) with what the owner reads instead. The
/// last check on every outbound message: a marker that reaches it got past
/// the boundary that should have rendered it, so it is logged.
pub fn render_internal_markers(text: &str) -> String {
    let (rendered, found) = aidaemon_tools::internal_markers::render_for_owner(text);
    if !found.is_empty() {
        tracing::warn!(
            markers = ?found,
            "Replaced internal markers in an outbound chat message"
        );
    }
    rendered
}

static TASK_NUMBER_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(^|[\s(])#(\d{1,6})\b").expect("valid task-number regex"));

/// Replace `#<id>` where `id` is one of this chat's task numbers (`tasks`:
/// id and description, from the task registry) with a short label of the
/// task: the numbers are the daemon's bookkeeping, not something the owner
/// tracks. Any other `#<n>` (an issue, a heading) stays.
pub fn render_task_numbers(text: &str, tasks: &[(u64, String)]) -> String {
    if tasks.is_empty() || !text.contains('#') {
        return text.to_string();
    }
    let mut replaced = 0usize;
    let rendered = TASK_NUMBER_RE.replace_all(text, |caps: &Captures<'_>| {
        let lead = &caps[1];
        let label = caps[2]
            .parse::<u64>()
            .ok()
            .and_then(|id| tasks.iter().find(|(task, _)| *task == id))
            .map(|(_, description)| word_preview(description, 40));
        match label {
            Some(label) => {
                replaced += 1;
                format!("{lead}\"{label}\"")
            }
            None => caps[0].to_string(),
        }
    });
    if replaced > 0 {
        tracing::warn!(
            replaced,
            "Replaced task numbers with task labels in an outbound chat message"
        );
    }
    rendered.into_owned()
}

/// Prepare arbitrary agent-originated Markdown for a chat surface.
pub fn prepare_chat_message(text: &str) -> String {
    let cleaned = aidaemon_tools::sanitize::strip_leaked_control_markers(text);
    let cleaned = render_internal_markers(&cleaned);
    let mut lines = Vec::new();
    let mut previous_blank = false;

    for raw_line in cleaned.lines() {
        let line = raw_line.trim_end();
        let trimmed = line.trim();
        if trimmed.is_empty() {
            if !previous_blank && !lines.is_empty() {
                lines.push(String::new());
            }
            previous_blank = true;
            continue;
        }

        // Orchestration steps can contain the user's entire multi-paragraph
        // instruction. They are useful in the task ledger, not in a chat card.
        if is_verbose_step_echo(trimmed) {
            continue;
        }

        lines.push(style_known_label(line));
        previous_blank = false;
    }

    while lines.last().is_some_and(String::is_empty) {
        lines.pop();
    }
    lines.join("\n").trim().to_string()
}

/// Apply a consistent status card to queued notifications.
pub fn present_notification(notification_type: &str, message: &str) -> String {
    let prepared = prepare_chat_message(message);
    if prepared.is_empty() || has_status_heading(&prepared) {
        return prepared;
    }

    let heading = match notification_type {
        "completed" => "✅ **Completed**",
        "failed" => "❌ **Run failed**",
        "escalation"
        | "mandate_ask"
        | "mandate_reconciliation_required"
        | "mandate_objective_control_required" => "⚠️ **Action needed**",
        "stalled" => "⏸️ **Work paused**",
        "token_alert" => "⚠️ **Budget alert**",
        "evergreen_alert" => "⚠️ **Schedule needs attention**",
        "mandate_paused" | "mandate_stopped" => "⏸️ **Automation paused**",
        "mandate_review_failed" | "mandate_reconciliation" | "mandate_ask_timeout" => {
            "⚠️ **Automation update**"
        }
        "mandate_action" => "✅ **Automation update**",
        "mandate_alert" => "🚨 **Automation alert**",
        "node_monitor_alert" => "🚨 **Node alert**",
        "node_monitor_recovery" => "✅ **Node recovered**",
        "node_monitor_ended" | "node_monitor_suspended" => "⏸️ **Node monitoring**",
        "progress" | "status_update" => "🔄 **Update**",
        _ => "ℹ️ **Update**",
    };
    let body = trim_redundant_lead(notification_type, &prepared);
    if body.is_empty() {
        heading.to_string()
    } else {
        format!("{heading}\n\n{body}")
    }
}

/// Whether a scheduled run's work had ended when its card was written.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScheduledRunProgress {
    /// The run's work is over; the card reports its outcome.
    Finished,
    /// The run handed long work to the background: it is still going, and its
    /// result arrives later.
    StillRunning,
}

/// Render a recurring scheduled-run result as a compact mobile card.
///
/// Executor results deliberately retain detailed paths, commands, versions, and
/// verification evidence in the durable task ledger. A chat notification has a
/// different job: state the outcome, link the deliverable, and make the schedule
/// state obvious. Keeping that boundary here prevents raw execution transcripts
/// from becoming the default Telegram experience. The card claims only what
/// the run's typed state shows: never "complete" while its work still runs,
/// and no checks it did not report.
pub fn present_scheduled_run_notification(
    notification_type: &str,
    message: &str,
    recurring: bool,
    progress: ScheduledRunProgress,
) -> String {
    let prepared = prepare_chat_message(message);
    let links = presentable_urls(&prepared);
    let schedule_note = recurring.then_some("_It will run again on its schedule._");

    if notification_type == "completed" && progress == ScheduledRunProgress::StillRunning {
        return "⏳ **Scheduled run still going**\n\nI'll tell you when it's done.".to_string();
    }

    if notification_type == "completed" {
        let outcome = concise_scheduled_outcome(&prepared);
        let mut sections = vec!["✅ **Scheduled run complete**".to_string()];
        sections.push(if outcome.is_empty() {
            "It finished.".to_string()
        } else {
            outcome
        });

        for (index, (url, host)) in links.iter().take(2).enumerate() {
            let label = if links.len() == 1 {
                format!("Open {host}")
            } else {
                format!("Open result {} · {host}", index + 1)
            };
            sections.push(format!("🔗 [{label}]({url})"));
        }
        if let Some(schedule_note) = schedule_note {
            sections.push(schedule_note.to_string());
        }
        return sections.join("\n\n");
    }

    let heading = if notification_type == "failed" {
        "⚠️ **Scheduled run needs attention**"
    } else {
        "ℹ️ **Scheduled run update**"
    };
    let cleaned = shorten_local_paths(&prepared);
    let detail = truncate_chars(cleaned.trim(), MAX_SCHEDULED_FAILURE_CHARS);
    match (detail.is_empty(), schedule_note) {
        (true, Some(schedule_note)) => format!("{heading}\n\n{schedule_note}"),
        (true, None) => heading.to_string(),
        (false, Some(schedule_note)) => format!("{heading}\n\n{detail}\n\n{schedule_note}"),
        (false, None) => format!("{heading}\n\n{detail}"),
    }
}

fn presentable_urls(text: &str) -> Vec<(String, String)> {
    let mut urls = Vec::new();
    for found in URL_RE.find_iter(text) {
        let candidate = found
            .as_str()
            .trim_end_matches(['.', ',', ';', ':', ')', ']', '}']);
        let Ok(parsed) = url::Url::parse(candidate) else {
            continue;
        };
        let Some(host) = parsed.host_str() else {
            continue;
        };
        let private_host = host.eq_ignore_ascii_case("localhost")
            || host == "127.0.0.1"
            || host == "::1"
            || host.ends_with(".local");
        if private_host || urls.iter().any(|(existing, _)| existing == candidate) {
            continue;
        }
        urls.push((candidate.to_string(), host.to_string()));
    }
    urls
}

fn concise_scheduled_outcome(text: &str) -> String {
    let without_urls = URL_RE.replace_all(text, "");
    let shortened = shorten_local_paths(&without_urls);
    for line in shortened.lines().map(str::trim) {
        // The daemon's own structure around the outcome is not it: its
        // status headings, its goal leads, its task-count line and the bold
        // task headings the results summary writes.
        if line.is_empty()
            || has_status_heading(line)
            || is_daemon_goal_lead(line)
            || is_task_count_line(line)
            || (line.starts_with("**") && line.ends_with("**"))
        {
            continue;
        }
        let sentence = first_sentence(line);
        if !sentence.is_empty() {
            return truncate_chars(sentence, 320);
        }
    }
    String::new()
}

fn first_sentence(text: &str) -> &str {
    let bytes = text.as_bytes();
    for (index, byte) in bytes.iter().enumerate() {
        if matches!(byte, b'.' | b'!' | b'?')
            && bytes.get(index + 1).is_none_or(u8::is_ascii_whitespace)
        {
            return text[..=index].trim();
        }
    }
    text.trim()
}

fn shorten_local_paths(text: &str) -> String {
    LOCAL_PATH_RE
        .replace_all(text, |captures: &Captures<'_>| {
            let prefix = captures.get(1).map_or("", |m| m.as_str());
            let original = captures.get(2).map_or("", |m| m.as_str());
            let trimmed = original.trim_end_matches(['.', ')', ']', '}']);
            let suffix = &original[trimmed.len()..];
            let filename = std::path::Path::new(trimmed)
                .file_name()
                .and_then(|name| name.to_str())
                .filter(|name| !name.is_empty())
                .unwrap_or("local file");
            format!("{prefix}`{filename}`{suffix}")
        })
        .into_owned()
}

use aidaemon_tools::text::truncate_ellipsis as truncate_chars;

fn is_verbose_step_echo(line: &str) -> bool {
    line.strip_prefix("Step:")
        .or_else(|| line.strip_prefix("**Step:**"))
        .is_some_and(|step| step.trim().chars().count() > MAX_INLINE_STEP_CHARS)
}

fn style_known_label(line: &str) -> String {
    let trimmed = line.trim_start();
    if trimmed.starts_with("**") {
        return line.to_string();
    }
    for label in [
        "Blocked",
        "Step",
        "Needed to continue",
        "Needed",
        "Resume",
        "Live",
        "Source",
        "Verification",
        "Status",
        "Version",
        "ID",
    ] {
        let prefix = format!("{label}:");
        if let Some(value) = trimmed.strip_prefix(&prefix) {
            return format!("**{label}:**{}", value);
        }
    }
    line.to_string()
}

fn has_status_heading(message: &str) -> bool {
    let first = message.lines().next().unwrap_or_default().trim_start();
    [
        "✅ **",
        "❌ **",
        "⚠️ **",
        "⏸️ **",
        "🔄 **",
        "⏳ **",
        "🔁 **",
        "ℹ️ **",
    ]
    .iter()
    .any(|prefix| first.starts_with(prefix))
}

/// Whether `line` is exactly one of the daemon's goal leads.
fn is_daemon_goal_lead(line: &str) -> bool {
    [GOAL_COMPLETED_LEAD, GOAL_FAILED_LEAD, GOAL_STALLED_LEAD].contains(&line)
}

/// `message` without the daemon's own goal lead that repeats the heading the
/// presenter adds. Only the daemon's constants are recognized.
fn trim_redundant_lead<'a>(notification_type: &str, message: &'a str) -> &'a str {
    let lead = match notification_type {
        "completed" => GOAL_COMPLETED_LEAD,
        "failed" => GOAL_FAILED_LEAD,
        "stalled" => GOAL_STALLED_LEAD,
        _ => return message,
    };
    message.strip_prefix(lead).map(str::trim).unwrap_or(message)
}

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

    #[test]
    fn chat_presentation_removes_long_step_echo_and_styles_labels() {
        let prompt = "Create, read, write, run, deploy, and verify exactly one post. ".repeat(8);
        let input = format!(
            "The deploy failed.\nStep: {prompt}\nNeeded: Fix the build.\nResume: /work unblock 12345678 <resolution>"
        );

        let output = prepare_chat_message(&input);

        assert!(!output.contains("Create, read"));
        assert!(output.contains("**Needed:** Fix the build."));
        assert!(output.contains("**Resume:** /work unblock"));
    }

    #[test]
    fn notifications_get_one_consistent_heading() {
        assert_eq!(
            present_notification("completed", "Goal completed:\n\nPublished the post."),
            "✅ **Completed**\n\nPublished the post."
        );
        assert_eq!(
            present_notification("escalation", "⚠️ **Action needed**\n\nFix it."),
            "⚠️ **Action needed**\n\nFix it."
        );
    }

    #[test]
    fn long_bold_lines_are_kept_whatever_their_words() {
        let bold = format!(
            "**{}**",
            "Create, read, write, deploy, and verify exactly one post. ".repeat(8)
        );
        let output = prepare_chat_message(&format!("Published the post.\n\n{bold}\n\nHTTP 200."));
        assert!(output.contains("Create, read, write"), "{output}");
    }

    #[test]
    fn a_result_line_ending_in_a_colon_is_the_outcome_unless_the_daemon_wrote_it() {
        let output = present_scheduled_run_notification(
            "completed",
            &format!(
                "{GOAL_COMPLETED_LEAD}\n\n{}\n\nRemind me at 9:",
                task_count_line(1, 1)
            ),
            false,
            ScheduledRunProgress::Finished,
        );
        assert!(output.contains("Remind me at 9:"), "{output}");
        assert!(!output.contains("tasks completed"), "{output}");
        // A lead the daemon did not write is the message's own text.
        assert_eq!(
            present_notification("completed", "Completed: the backup"),
            "✅ **Completed**\n\nCompleted: the backup"
        );
    }

    #[test]
    fn scheduled_completion_is_compact_and_hides_local_path() {
        let input = "Published /Users/synthetic/projects/blog/src/content/posts/2026-08-12-example.md. \
                     Evidence was repository guidance and clean git status. npm run deploy built \
                     successfully with Vite 7.3.1 and uploaded 5 assets. Exact public URL \
                     https://blog.example.com/posts/example/ returned HTTP 200.";

        let output = present_scheduled_run_notification(
            "completed",
            input,
            true,
            ScheduledRunProgress::Finished,
        );

        assert!(output.starts_with("✅ **Scheduled run complete**"));
        assert!(output.contains("Published `2026-08-12-example.md`."));
        assert!(
            output.contains("[Open blog.example.com](https://blog.example.com/posts/example/)")
        );
        assert!(!output.contains("Checks"));
        assert!(output.contains("_It will run again on its schedule._"));
        assert!(!output.contains("/Users/synthetic"));
        assert!(!output.contains("Vite 7.3.1"));
        assert!(!output.contains("uploaded 5 assets"));
    }

    #[test]
    fn scheduled_completion_skips_lead_in_labels() {
        let input = "Goal completed:\n\n**Deliver the scheduled reminder now.**\nPrepared \
                     reminder: Check the oven now. No rescheduling.";

        let output = present_scheduled_run_notification(
            "completed",
            input,
            false,
            ScheduledRunProgress::Finished,
        );

        assert!(output.contains("Check the oven"), "{output}");
        assert!(!output.contains("Goal completed:"), "{output}");
    }

    #[test]
    fn scheduled_completion_skips_task_count_and_internal_urls() {
        let input = "2/2 tasks completed.\n\n**Publish the article**\nPublished the article. \
                     Previewed at http://localhost:4173/ and deployed to \
                     https://blog.example.com/posts/article/.";

        let output = present_scheduled_run_notification(
            "completed",
            input,
            true,
            ScheduledRunProgress::Finished,
        );

        assert!(output.contains("Published the article."));
        assert!(output.contains("https://blog.example.com/posts/article/"));
        assert!(!output.contains("localhost"));
        assert!(!output.contains("2/2 tasks"));
    }

    #[test]
    fn a_run_still_going_is_never_called_complete() {
        let input = "⏳ **Still on it**\n\nThis is taking a little longer, so it's running \
                     in the background now.";

        let output = present_scheduled_run_notification(
            "completed",
            input,
            true,
            ScheduledRunProgress::StillRunning,
        );

        assert_eq!(
            output,
            "⏳ **Scheduled run still going**\n\nI'll tell you when it's done."
        );
    }

    #[test]
    fn a_status_card_inside_the_result_is_not_taken_as_its_outcome() {
        let input = "⏳ **Still on it**\nPublished the weekly post.";

        let output = present_scheduled_run_notification(
            "completed",
            input,
            false,
            ScheduledRunProgress::Finished,
        );

        assert!(output.contains("Published the weekly post."), "{output}");
        assert!(!output.contains("Still on it"), "{output}");
    }

    #[test]
    fn scheduled_failure_keeps_actionable_detail_but_shortens_paths() {
        let input = "Deployment failed while reading /Users/synthetic/projects/blog/wrangler.toml.\n\nNeeded: refresh the deployment token.";

        let output = present_scheduled_run_notification(
            "failed",
            input,
            true,
            ScheduledRunProgress::Finished,
        );

        assert!(output.starts_with("⚠️ **Scheduled run needs attention**"));
        assert!(output.contains("`wrangler.toml`"));
        assert!(output.contains("refresh the deployment token"));
        assert!(!output.contains("/Users/synthetic"));
        assert!(output.contains("_It will run again on its schedule._"));
    }
}

/// `text` cut to at most `max_chars` characters (the ellipsis included) at
/// the last whole word, so a preview never ends mid-word. A long word (a
/// link, a path) is cut where it must be rather than dropped: backing up
/// to the space before it would give up more than a third of the room,
/// leaving "Open…" for "Open https://…".
pub fn word_preview(text: &str, max_chars: usize) -> String {
    let text = text.trim();
    if text.chars().count() <= max_chars {
        return text.to_string();
    }
    let budget = max_chars.saturating_sub(1);
    let head: String = text.chars().take(budget).collect();
    // Cut at a space only when the word after it was split.
    let next_is_break = text.chars().nth(budget).is_some_and(char::is_whitespace);
    let kept = if next_is_break {
        head.as_str()
    } else {
        head.rfind(char::is_whitespace)
            .map(|at| &head[..at])
            .filter(|kept| !kept.trim().is_empty())
            .filter(|kept| kept.chars().count() * 3 >= budget * 2)
            .unwrap_or(head.as_str())
    };
    format!("{}…", kept.trim_end())
}

/// The reply to a message that arrives while a task runs in its session.
/// A message that joins the work (`waits_for_own_turn` false) is taken in
/// at the running task's next step that can act on it (one offering tools)
/// when it is from the same sender (`TaskRegistry::take_steering_messages`);
/// one with attachments or read as unrelated to the work, or one no model
/// call answered, runs as its own turn once the current one ends, and this
/// notice is edited into [`queued_message_starting`] then.
pub fn queued_message_notice(waits_for_own_turn: bool) -> String {
    if waits_for_own_turn {
        "📥 Got it – I'll get to this right after what I'm doing now.".to_string()
    } else {
        "📥 Got it – I'll add this to what I'm doing now.".to_string()
    }
}

/// The reply to a message that arrives while the running work waits on an
/// approval: nothing reads it until the owner answers.
pub fn after_approval_notice() -> String {
    "📥 Got it – I'll read this once you answer the approval.".to_string()
}

/// What a queued message's notice says once its own turn starts.
pub fn queued_message_starting(text: &str) -> String {
    format!("▶️ Now on: \"{}\"", word_preview(text, 50))
}

#[cfg(test)]
mod owner_facing_tests {
    use super::{prepare_chat_message, render_task_numbers};

    #[test]
    fn this_chat_s_task_numbers_become_task_labels() {
        let tasks = vec![
            (12, "Install a WordPress demo site".to_string()),
            (14, "Install a Drupal demo site".to_string()),
        ];
        assert_eq!(
            render_task_numbers("Both #12 and #14 are done (#14 took longer).", &tasks),
            "Both \"Install a WordPress demo site\" and \"Install a Drupal demo site\" are \
             done (\"Install a Drupal demo site\" took longer)."
        );
        // Not one of this chat's tasks, or not a task reference: unchanged.
        assert_eq!(
            render_task_numbers("Fixed issue #7 and step#12.", &tasks),
            "Fixed issue #7 and step#12."
        );
        assert_eq!(render_task_numbers("Done #12.", &[]), "Done #12.");
    }

    #[test]
    fn a_reply_never_shows_the_daemon_s_markers() {
        let reply = prepare_chat_message(
            "I could not send it: [OWNER DECLINED] The file [REDACTED:File path] is still at \
             ~/.aidaemon/files/inbox/dd4dc47d_screenshot.png.",
        );
        assert_eq!(
            reply,
            "I could not send it: you declined that The file a file is still at screenshot.png."
        );
    }
}

#[cfg(test)]
mod queued_notice_tests {
    use super::{queued_message_notice, queued_message_starting};

    #[test]
    fn the_notice_is_short_and_says_what_will_happen() {
        assert_eq!(
            queued_message_notice(false),
            "📥 Got it – I'll add this to what I'm doing now."
        );
        assert_eq!(
            queued_message_notice(true),
            "📥 Got it – I'll get to this right after what I'm doing now."
        );
        assert_eq!(
            queued_message_starting(&"x".repeat(80)),
            format!("▶️ Now on: \"{}…\"", "x".repeat(49))
        );
    }

    #[test]
    fn a_preview_ends_on_a_whole_word() {
        use super::word_preview;
        let request = "Every day at 7:13 am, send me a one-line synthetic test reminder \
                       that says: daily-check beta.";
        assert_eq!(
            word_preview(request, 80),
            "Every day at 7:13 am, send me a one-line synthetic test reminder that says:…"
        );
        assert_eq!(word_preview("short request", 80), "short request");
        assert_eq!(
            word_preview(&"y".repeat(90), 10),
            format!("{}…", "y".repeat(9))
        );
        // A link is cut, not dropped.
        assert_eq!(
            word_preview(
                "Open https://the-synthetic-example-site.example.com/reports/2026/acme and summarize it",
                60
            ),
            "Open https://the-synthetic-example-site.example.com/reports…"
        );
        assert_eq!(
            queued_message_starting(request),
            "▶️ Now on: \"Every day at 7:13 am, send me a one-line…\""
        );
    }
}