rho-coding-agent 3.6.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! News on watched pull requests as the model reads it ([`prompt`]) and as
//! clients show it ([`News::row`]).

use rho_protocol::{
    display::{
        NotificationCard, NotificationDelivery, NotificationItem, NotificationOutcome,
        NotificationPreview, NotificationSubject, NotificationTone, NotificationVisibility,
    },
    PullRequestId,
};

use super::{
    record::NEWS_PER_SESSION,
    state::{Change, CheckRef, CheckTally, End, Failure, Gate, News, Remark, Reviewed},
    watch::{COMMENT_WAKE_LIMIT, READ_FAILURE_LIMIT},
};
use crate::{
    display_transcript::NotificationRow, notification_info::notification_item,
    tools::notification_format::join_budgeted_sections,
};

/// The most one wake's text may take: the budget process and subagent
/// notifications have (`tools/process/notify.rs`, `tools/agent_output.rs`).
pub(crate) const MODEL_NOTIFICATION_BYTES: usize = 16 * 1024;

/// Items one change lists before `and N more`, and characters of a comment
/// it quotes: t3's `LISTED_ITEMS` and `SNIPPET_LENGTH`
/// (`pullRequestWatch.ts:14-15`).
const LISTED_ITEMS: usize = 10;
const SNIPPET_CHARS: usize = 200;

/// What a wake ends with while any watch it tells of is still on. Short,
/// since every wake repeats it; the no-polling line stays because agents
/// otherwise wait in the shell (t3's `evals/mcp-tool-use` cases).
const CLOSING: &str = "The watch stays on. Act on what needs it, then end your turn; rho wakes you on the next change. Don't watch again or poll (no sleep, `gh pr checks --watch` or background waits). Call unwatch_pull_request before you hand the work back.";

/// The text of one wake for a session's queued `news`, oldest first: each
/// pull request's changes and end, then [`CLOSING`] once if any watch is
/// still on. `dropped` counts older news the queue let go (see
/// [`NEWS_PER_SESSION`]); the text opens by saying so when it isn't 0. The
/// text fits [`MODEL_NOTIFICATION_BYTES`], and says how many updates it
/// left out to fit.
pub(crate) fn prompt(news: &[News], dropped: u32) -> String {
    let preface = if dropped == 0 {
        String::new()
    } else {
        let updates = plural(dropped as usize, "update", "updates");
        format!(
            "rho dropped {dropped} older {updates} for this session (it keeps {NEWS_PER_SESSION}). Read each pull request's current state.\n\n"
        )
    };
    let closing = if news.iter().any(|news| !news.is_end()) {
        format!("\n\n{CLOSING}")
    } else {
        String::new()
    };
    let budget = MODEL_NOTIFICATION_BYTES.saturating_sub(preface.len() + closing.len());
    let body = join_budgeted_sections(news.iter().map(section), "\n\n", budget, omitted);
    format!("{preface}{body}{closing}")
}

/// What [`prompt`] says in place of the last `count` updates when they
/// don't fit.
fn omitted(count: usize) -> String {
    let updates = plural(count, "update", "updates");
    format!(
        "... {count} more pull request {updates} left out to fit {} KiB; read their current state with gh.",
        MODEL_NOTIFICATION_BYTES / 1024
    )
}

/// One pull request's part of a wake.
fn section(news: &News) -> String {
    let label = news.pr.to_string();
    let mut lines = Vec::new();
    if !news.changes.is_empty() {
        lines.push(format!("Update on {label} ({}):", news.url));
        lines.extend(change_lines(news));
    }
    if let Some(end) = &news.end {
        lines.push(end_line(end, &label, &news.url));
    }
    lines.join("\n")
}

impl News {
    /// Transcript card for this news: what happened and which pull request
    /// up front, the head beneath, the same lines the model reads as the
    /// body. The tone is that of its most pressing item. Typed with one item
    /// per change, in the body's order: new commits, each change, the end,
    /// each in its own tone.
    pub(crate) fn row(&self) -> NotificationRow {
        let subject = NotificationSubject::PullRequest {
            id: PullRequestId::from(&self.pr),
            url: self.url.clone(),
        };
        let item = |outcome, text: String| {
            notification_item(outcome, tone(outcome), subject.clone(), text)
        };
        let commit = on_head(self);
        let pushed = self.pushed.then(|| {
            let head = self.head.as_deref().map_or("", short);
            item(
                NotificationOutcome::NewCommits,
                format!("New commits: the head is now {head}"),
            )
        });
        let changes = self.changes.iter().map(|change| {
            let (outcome, text) = change_item(change, &commit);
            item(outcome, text)
        });
        let end = self.end.as_ref().map(|end| {
            let (outcome, text) = end_item(end);
            item(outcome, text)
        });
        let items: Vec<_> = pushed.into_iter().chain(changes).chain(end).collect();
        NotificationRow::typed(self.card(&items), subject, items)
    }

    /// The card, toned as the most pressing of `items`.
    fn card(&self, items: &[NotificationItem]) -> NotificationCard {
        let label = self.pr.to_string();
        let mut outcomes: Vec<&str> = self.changes.iter().map(change_summary).collect();
        outcomes.extend(self.end.as_ref().map(end_summary));
        let tone = items
            .iter()
            .map(|item| item.tone)
            .max_by_key(|tone| pressing(*tone))
            .unwrap_or(NotificationTone::Neutral);
        let mut body = change_lines(self);
        body.extend(
            self.end
                .as_ref()
                .map(|end| end_line(end, &label, &self.url)),
        );
        let mut details = vec![format!("pull request: {}", self.url)];
        details.extend(self.head.as_ref().map(|head| format!("head: {head}")));
        NotificationCard {
            title: format!("{} · {label}", capitalized(&outcomes.join(", "))),
            sender: "github".into(),
            recipient: "session".into(),
            delivery: NotificationDelivery::Received,
            tone,
            preview: NotificationPreview::Truncated,
            visibility: NotificationVisibility::Conversation,
            reference: Some(label),
            subtitle: self
                .head
                .as_deref()
                .map(|head| format!("head {}", short(head))),
            body: body.join("\n"),
            output: None,
            details,
        }
    }
}

/// A commit's first seven characters, as GitHub shows it.
fn short(oid: &str) -> &str {
    oid.get(..7).unwrap_or(oid)
}

fn plural<'a>(count: usize, one: &'a str, many: &'a str) -> &'a str {
    if count == 1 {
        one
    } else {
        many
    }
}

fn capitalized(text: &str) -> String {
    let mut chars = text.chars();
    chars.next().map_or_else(String::new, |first| {
        first.to_uppercase().chain(chars).collect()
    })
}

/// Which head `news`'s checks ran on, as " on <short oid>", if it knows.
fn on_head(news: &News) -> String {
    news.head
        .as_deref()
        .map(|head| format!(" on {}", short(head)))
        .unwrap_or_default()
}

/// The lines of `news`'s changes, as a Markdown list.
fn change_lines(news: &News) -> Vec<String> {
    let commit = on_head(news);
    let mut lines = Vec::new();
    if news.pushed {
        let head = news.head.as_deref().map_or("", short);
        lines.push(format!(
            "- New commits: the head is now {head}, so earlier check results no longer apply."
        ));
    }
    for change in &news.changes {
        match change {
            Change::ChecksFailed { failed } => {
                lines.push(format!("- Checks failed{commit}:"));
                lines.extend(listed(failed, check_line));
                lines.extend(news.checks.as_ref().map(tally_line));
            }
            Change::ChecksPassed { count, gate } => {
                let required = match gate {
                    Gate::Required => "required ",
                    Gate::All => "",
                };
                let checks = plural(*count as usize, "check", "checks");
                lines.push(format!("- All {count} {required}{checks} passed{commit}."));
            }
            Change::ChecksFinished { passed, failing } if failing.is_empty() => {
                lines.push(format!(
                    "- Every check has finished{commit}: {passed} passed, none failing."
                ));
            }
            Change::ChecksFinished { passed, failing } => {
                lines.push(format!(
                    "- Every check has finished{commit}: {passed} passed, {} still failing:",
                    failing.len()
                ));
                lines.extend(listed(failing, check_line));
            }
            Change::Remarks { remarks } => {
                let comments = plural(remarks.len(), "comment", "comments");
                lines.push(format!("- {} new {comments}:", remarks.len()));
                lines.extend(listed(remarks, remark_line));
            }
            Change::Conflicting => {
                lines.push("- The branch now conflicts with its base branch.".to_owned());
            }
        }
    }
    lines
}

/// `- Checks now: 10 passed, 2 failed, 2 running, 1 workflow queued.`:
/// where CI stands as failures are told, so the agent can tell acting now
/// from waiting.
fn tally_line(tally: &CheckTally) -> String {
    let CheckTally {
        passed,
        running,
        failed,
        unread,
        queued_workflows,
    } = *tally;
    let unread = if unread == 0 {
        String::new()
    } else {
        format!(", {unread} not read")
    };
    let queued = match queued_workflows {
        0 => String::new(),
        queued => format!(
            ", {queued} {} queued",
            plural(queued as usize, "workflow", "workflows")
        ),
    };
    format!("- Checks now: {passed} passed, {failed} failed, {running} running{unread}{queued}.")
}

/// The first [`LISTED_ITEMS`] of `items` as sub-items, and how many more.
fn listed<T>(items: &[T], line: fn(&T) -> String) -> Vec<String> {
    let mut lines: Vec<String> = items.iter().take(LISTED_ITEMS).map(line).collect();
    if items.len() > LISTED_ITEMS {
        lines.push(format!("  - and {} more", items.len() - LISTED_ITEMS));
    }
    lines
}

fn check_line(check: &CheckRef) -> String {
    let how = match check.failure {
        Failure::Failed => "",
        Failure::Cancelled => " (cancelled)",
    };
    let url = check
        .url
        .as_deref()
        .map(|url| format!(" {url}"))
        .unwrap_or_default();
    format!("  - {}{how}{url}", check.name)
}

/// `alice (MEMBER) on src/lib.rs: "quote" url`: who, their standing in the
/// repository, where, what a review decided, and what they wrote.
fn remark_line(remark: &Remark) -> String {
    let association = remark
        .association
        .as_deref()
        .map(|association| format!(" ({association})"))
        .unwrap_or_default();
    let place = remark
        .path
        .as_deref()
        .map(|path| format!(" on {path}"))
        .unwrap_or_default();
    let verdict = match remark.verdict {
        None | Some(Reviewed::Commented) => "",
        Some(Reviewed::Approved) => " approved",
        Some(Reviewed::ChangesRequested) => " requested changes",
        Some(Reviewed::Dismissed) => " (review dismissed)",
    };
    let quote = snippet(&remark.body);
    let said = if quote.is_empty() {
        String::new()
    } else {
        format!(": \"{quote}\"")
    };
    format!(
        "  - {}{association}{place}{verdict}{said} {}",
        remark.author, remark.url
    )
}

fn end_line(end: &End, label: &str, url: &str) -> String {
    match end {
        End::Merged { by, commit, base } => {
            let by = by
                .as_deref()
                .map(|by| format!(" by {by}"))
                .unwrap_or_default();
            let commit = commit
                .as_deref()
                .map(|commit| format!(" as {}", short(commit)))
                .unwrap_or_default();
            format!(
                "Pull request {label} ({url}) was merged into {base}{by}{commit}, so rho stopped watching it."
            )
        }
        End::Closed => format!(
            "Pull request {label} ({url}) was closed without merging, so rho stopped watching it. If it is reopened, call watch_pull_request to watch it again."
        ),
        End::CommentLoop => format!(
            "rho stopped watching pull request {label} ({url}) after {COMMENT_WAKE_LIMIT} comment-only updates in a row (limit {COMMENT_WAKE_LIMIT}). Call watch_pull_request to watch it again."
        ),
        End::Unreadable(why) => format!(
            "rho stopped watching pull request {label} ({url}) after {READ_FAILURE_LIMIT} failed reads in a row (limit {READ_FAILURE_LIMIT}): {}. Check it yourself, then call watch_pull_request to watch it again.",
            why.trim_end_matches('.')
        ),
    }
}

/// One change's item: its outcome, and the change in one line. Checks that
/// all finished with some failing are failed checks.
fn change_item(change: &Change, commit: &str) -> (NotificationOutcome, String) {
    match change {
        Change::ChecksFailed { failed } => (
            NotificationOutcome::ChecksFailed,
            format!("Checks failed{commit}: {}", check_names(failed)),
        ),
        Change::ChecksPassed { count, gate } => {
            let required = match gate {
                Gate::Required => "required ",
                Gate::All => "",
            };
            let checks = plural(*count as usize, "check", "checks");
            (
                NotificationOutcome::ChecksPassed,
                format!("All {count} {required}{checks} passed{commit}"),
            )
        }
        Change::ChecksFinished { passed, failing } if failing.is_empty() => (
            NotificationOutcome::ChecksFinished,
            format!("Every check has finished{commit}: {passed} passed"),
        ),
        Change::ChecksFinished { passed, failing } => (
            NotificationOutcome::ChecksFailed,
            format!(
                "Every check has finished{commit}: {passed} passed, {} still failing: {}",
                failing.len(),
                check_names(failing)
            ),
        ),
        Change::Remarks { remarks } => {
            let comments = plural(remarks.len(), "comment", "comments");
            (
                NotificationOutcome::NewComments,
                format!("{} new {comments}", remarks.len()),
            )
        }
        Change::Conflicting => (
            NotificationOutcome::MergeConflict,
            "The branch conflicts with its base branch".to_owned(),
        ),
    }
}

/// The first [`LISTED_ITEMS`] of `checks` by name, and how many more.
fn check_names(checks: &[CheckRef]) -> String {
    let names: Vec<&str> = checks
        .iter()
        .take(LISTED_ITEMS)
        .map(|check| check.name.as_str())
        .collect();
    let more = checks
        .len()
        .checked_sub(LISTED_ITEMS)
        .filter(|more| *more > 0)
        .map(|more| format!(" and {more} more"))
        .unwrap_or_default();
    format!("{}{more}", names.join(", "))
}

/// The end's item: its outcome, and the end in one line.
fn end_item(end: &End) -> (NotificationOutcome, String) {
    match end {
        End::Merged { by, base, .. } => {
            let by = by
                .as_deref()
                .map(|by| format!(" by {by}"))
                .unwrap_or_default();
            (
                NotificationOutcome::Merged,
                format!("Merged into {base}{by}"),
            )
        }
        End::Closed => (
            NotificationOutcome::Closed,
            "Closed without merging".to_owned(),
        ),
        End::CommentLoop => (
            NotificationOutcome::WatchStopped,
            format!("Stopped watching after {COMMENT_WAKE_LIMIT} comment-only updates in a row"),
        ),
        // GitHub's refusal is kept as it said it, which may run to several
        // lines; an item is one.
        End::Unreadable(why) => (
            NotificationOutcome::Unreadable,
            format!(
                "Stopped watching after {READ_FAILURE_LIMIT} failed reads: {}",
                why.split_whitespace()
                    .collect::<Vec<_>>()
                    .join(" ")
                    .trim_end_matches('.')
            ),
        ),
    }
}

fn change_summary(change: &Change) -> &'static str {
    match change {
        Change::ChecksFailed { .. } => "checks failed",
        Change::ChecksPassed { .. } => "checks passed",
        Change::ChecksFinished { .. } => "checks finished",
        Change::Remarks { .. } => "new comments",
        Change::Conflicting => "merge conflict",
    }
}

fn end_summary(end: &End) -> &'static str {
    match end {
        End::Merged { .. } => "merged",
        End::Closed => "closed",
        End::CommentLoop => "stopped watching (comment loop)",
        End::Unreadable(_) => "stopped watching (unreadable)",
    }
}

/// The color of an item that ended as `outcome`. New commits alone are
/// neutral.
fn tone(outcome: NotificationOutcome) -> NotificationTone {
    use NotificationOutcome as Outcome;
    match outcome {
        Outcome::ChecksFailed | Outcome::MergeConflict => NotificationTone::Error,
        Outcome::ChecksPassed | Outcome::ChecksFinished | Outcome::Merged => {
            NotificationTone::Success
        }
        Outcome::NewComments => NotificationTone::Accent,
        Outcome::Closed | Outcome::WatchStopped | Outcome::Unreadable => NotificationTone::Warning,
        // `NewCommits`, and outcomes news has none of.
        _ => NotificationTone::Neutral,
    }
}

/// Which of a card's tones wins: the one that most needs a look.
fn pressing(tone: NotificationTone) -> u8 {
    match tone {
        NotificationTone::Error => 4,
        NotificationTone::Warning => 3,
        NotificationTone::Accent => 2,
        NotificationTone::Success => 1,
        // Neutral, and tones this crate doesn't produce.
        _ => 0,
    }
}

/// A comment's opening for the model: HTML comments and tags dropped from
/// its prose (GitHub renders them, so they are presentation, not what was
/// said), Markdown links, quote markers, headings and bold markers reduced
/// to their text so they don't spend the cut, code kept as written,
/// whitespace collapsed, and cut to [`SNIPPET_CHARS`]. After t3's
/// `snippet` (`pullRequestWatch.ts`).
fn snippet(body: &str) -> String {
    let text = without_markup(&without_html_comments(body));
    let text = text.split_whitespace().collect::<Vec<_>>().join(" ");
    if text.chars().count() <= SNIPPET_CHARS {
        return text;
    }
    let cut: String = text.chars().take(SNIPPET_CHARS - 3).collect();
    format!("{cut}...")
}

/// `body` with each closed `<!-- ... -->` replaced by a space.
fn without_html_comments(body: &str) -> String {
    let mut text = String::with_capacity(body.len());
    let mut rest = body;
    while let Some(start) = rest.find("<!--") {
        let Some(end) = rest[start + 4..].find("-->") else {
            break;
        };
        text.push_str(&rest[..start]);
        text.push(' ');
        rest = &rest[start + 4 + end + 3..];
    }
    text.push_str(rest);
    text
}

/// `body` with HTML tags dropped from its Markdown prose. Fenced code
/// blocks, and indented ones after a blank line, pass through untouched.
fn without_markup(body: &str) -> String {
    let mut parts: Vec<String> = Vec::new();
    let mut prose: Vec<&str> = Vec::new();
    let mut fence: Option<&str> = None;
    let mut after_text = false;
    for line in body.split('\n') {
        let code = if let Some(marker) = fence {
            let closer = line.trim();
            let mark = marker.chars().next();
            if closer.len() >= marker.len() && closer.chars().all(|ch| Some(ch) == mark) {
                fence = None;
            }
            true
        } else if let Some(marker) = fence_opener(line) {
            fence = Some(marker);
            true
        } else {
            // After text, an indent continues the paragraph.
            !after_text && is_indented_code(line)
        };
        if code {
            if !prose.is_empty() {
                parts.push(plain_prose(&without_tags(&prose.join("\n"))));
                prose.clear();
            }
            parts.push(line.to_owned());
            after_text = false;
        } else {
            prose.push(line);
            after_text = !line.trim().is_empty();
        }
    }
    if !prose.is_empty() {
        parts.push(plain_prose(&without_tags(&prose.join("\n"))));
    }
    parts.join("\n")
}

/// The fence a line opens a code block with: three or more backticks or
/// tildes after at most three spaces. A backtick fence's info string has no
/// backtick, or it is inline code.
fn fence_opener(line: &str) -> Option<&str> {
    let indent = line.len() - line.trim_start_matches(' ').len();
    if indent > 3 {
        return None;
    }
    let rest = &line[indent..];
    let mark = rest.chars().next().filter(|ch| matches!(ch, '`' | '~'))?;
    let run = rest.len() - rest.trim_start_matches(mark).len();
    let (marker, info) = rest.split_at(run);
    (run >= 3 && !(mark == '`' && info.contains('`'))).then_some(marker)
}

/// Whether a line is indented code: four spaces, or a tab after at most
/// three, before some text.
fn is_indented_code(line: &str) -> bool {
    let spaces = line.len() - line.trim_start_matches(' ').len();
    let indented = spaces >= 4 || line[spaces..].starts_with('\t');
    indented && !line.trim().is_empty()
}

/// HTML elements GitHub renders in a comment, by name. Lowercase only:
/// `Promise<T>` is a type argument.
const TAG_NAMES: &[&str] = &[
    "a",
    "abbr",
    "b",
    "blockquote",
    "br",
    "caption",
    "cite",
    "code",
    "dd",
    "del",
    "details",
    "div",
    "dl",
    "dt",
    "em",
    "h1",
    "h2",
    "h3",
    "h4",
    "h5",
    "h6",
    "hr",
    "i",
    "img",
    "ins",
    "kbd",
    "li",
    "mark",
    "ol",
    "p",
    "picture",
    "pre",
    "q",
    "s",
    "samp",
    "small",
    "source",
    "span",
    "strike",
    "strong",
    "sub",
    "summary",
    "sup",
    "table",
    "tbody",
    "td",
    "tfoot",
    "th",
    "thead",
    "tr",
    "tt",
    "ul",
    "var",
];

/// `prose` with its HTML tags replaced by spaces. Code spans stay as
/// written. An opener glued to an identifier is a type argument
/// (`Vec<code>`), so it and its closer stay too.
fn without_tags(prose: &str) -> String {
    let bytes = prose.as_bytes();
    let mut text = String::with_capacity(prose.len());
    let mut kept: Vec<&str> = Vec::new();
    let (mut from, mut at) = (0, 0);
    while at < bytes.len() {
        match bytes[at] {
            b'`' => {
                let ticks = run_of(bytes, at, b'`');
                at = code_span_end(bytes, at + ticks, ticks).unwrap_or(at + ticks);
            }
            b'<' => {
                let Some(tag) = tag_at(&prose[at..]) else {
                    at += 1;
                    continue;
                };
                let end = at + tag.len;
                let glued = at.checked_sub(1).is_some_and(|before| {
                    bytes[before].is_ascii_alphanumeric() || bytes[before] == b'_'
                });
                let keep = if tag.closing {
                    let opener = kept.iter().position(|name| *name == tag.name);
                    opener.map(|opener| kept.swap_remove(opener)).is_some()
                } else if glued {
                    kept.push(tag.name);
                    true
                } else {
                    false
                };
                if !keep {
                    text.push_str(&prose[from..at]);
                    text.push(' ');
                    from = end;
                }
                at = end;
            }
            _ => at += 1,
        }
    }
    text.push_str(&prose[from..]);
    text
}

/// Markdown `prose` as its words: each line without its `>` quote markers
/// and `#` heading marks, `**` and `__` dropped, and each `[text](target)`
/// link or `![alt](target)` image as its text.
fn plain_prose(prose: &str) -> String {
    prose
        .split('\n')
        .map(|line| {
            let line = line.trim_start_matches(|ch: char| ch == '>' || ch.is_whitespace());
            let line = line.trim_start_matches('#').trim_start();
            without_link_targets(&line.replace("**", "").replace("__", ""))
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// `text` with each `[label](target)` as `label`, and an image's `!` too.
/// Brackets not followed by a closed `(target)` on the line stay.
fn without_link_targets(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    while let Some(open) = rest.find('[') {
        let link = rest[open + 1..].find("](").and_then(|label_end| {
            let label = &rest[open + 1..open + 1 + label_end];
            let target = &rest[open + 1 + label_end + 2..];
            let target_end = target.find(')')?;
            (!label.contains('[') && !target[..target_end].contains(char::is_whitespace))
                .then_some((label, open + 1 + label_end + 2 + target_end + 1))
        });
        match link {
            Some((label, end)) => {
                out.push_str(rest[..open].strip_suffix('!').unwrap_or(&rest[..open]));
                out.push_str(label);
                rest = &rest[end..];
            }
            None => {
                out.push_str(&rest[..=open]);
                rest = &rest[open + 1..];
            }
        }
    }
    out.push_str(rest);
    out
}

/// How many `byte`s in a row start at `at`.
fn run_of(bytes: &[u8], at: usize, byte: u8) -> usize {
    bytes[at..].iter().take_while(|&&next| next == byte).count()
}

/// Where a code span opened by `ticks` backticks ends, searching from
/// `from`: after the next run of exactly as many, before any blank line.
fn code_span_end(bytes: &[u8], from: usize, ticks: usize) -> Option<usize> {
    let mut at = from;
    while at < bytes.len() {
        match bytes[at] {
            b'`' => {
                let run = run_of(bytes, at, b'`');
                if run == ticks {
                    return Some(at + run);
                }
                at += run;
            }
            b'\n' => {
                let blank = bytes[at + 1..]
                    .iter()
                    .find(|&&next| !matches!(next, b' ' | b'\t' | b'\r'))
                    .is_some_and(|&next| next == b'\n');
                if blank {
                    return None;
                }
                at += 1;
            }
            _ => at += 1,
        }
    }
    None
}

/// An HTML tag at the start of `text`.
struct Tag {
    name: &'static str,
    closing: bool,
    /// Its length in bytes, through its `>`.
    len: usize,
}

/// The tag `text` starts with, if it names one of [`TAG_NAMES`] and closes
/// before the next `<`, quoted attribute values aside.
fn tag_at(text: &str) -> Option<Tag> {
    let after = text.strip_prefix('<')?;
    let closing = after.starts_with('/');
    let named = if closing { &after[1..] } else { after };
    let name_len = named.bytes().take_while(u8::is_ascii_alphanumeric).count();
    let name = TAG_NAMES
        .iter()
        .copied()
        .find(|known| *known == &named[..name_len])?;
    let rest = &named[name_len..];
    if !rest.starts_with(|ch: char| ch.is_whitespace() || ch == '/' || ch == '>') {
        return None;
    }
    let bytes = rest.as_bytes();
    let mut at = 0;
    loop {
        match *bytes.get(at)? {
            b'>' => break,
            b'<' => return None,
            quote @ (b'"' | b'\'') => {
                let close = rest[at + 1..].find(char::from(quote))?;
                at += close + 2;
            }
            _ => at += 1,
        }
    }
    Some(Tag {
        name,
        closing,
        len: text.len() - rest.len() + at + 1,
    })
}

#[cfg(test)]
#[path = "news_tests.rs"]
mod tests;