omni-dev 0.43.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, Datadog, Gmail, and Drive.
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
//! Source comment markers that exclude or tolerate a *region* of a file in
//! `coverage diff`.
//!
//! `--ignore-filename-regex` (and its `coverage.yaml` twin) drops a **whole
//! file** from both reports, but the noise it exists to silence is almost always
//! narrower than a file: one function gated on a *runtime* CPU-feature check is
//! compiled into the denominator on every run yet executed only on a host that
//! has the instruction, so it flips whenever the baseline and head runs draw
//! different runner CPUs. Excluding the file hides far more real coverage than
//! noise.
//!
//! Naming the region in config is not an option either: line numbers are
//! invalidated by every edit above them, and function *extents* are absent from
//! the lcov `FN:` records (start line + mangled symbol only). So the region is
//! delimited in the source itself, and each revision's own source is scanned —
//! head from the worktree, base from the base blob — which means no line number
//! is ever stored, and a region that moves, grows, or disappears between base
//! and head is handled by construction.
//!
//! A region is opened by a comment naming a kind and a mandatory reason, and
//! closed by an `end` comment; there is also a single-line form. **The syntax is
//! documented, with examples, in `docs/coverage.md`** — deliberately not here:
//! see [`INTRODUCER`] for why this file must not contain a literal marker.
//!
//! Two kinds, differing in what they do to the *reports*:
//!
//! - [`MarkerKind::Ignore`] — the lines are removed from **both** reports before
//!   any analysis, so they cannot move any number. The scoped twin of
//!   `ignore-filename-regex`.
//! - [`MarkerKind::Tolerate`] — the lines stay, so the percentage stays honest;
//!   only the *delta signals* are masked, by scoring each tolerated head line
//!   with its baseline hit status.
//!
//! Matching is a **plain substring** on any line, so it works in any comment
//! syntax and never depends on parsing the host language. A marker inside a
//! string literal is therefore matched too — accepted, and documented.

use std::collections::BTreeSet;

use anyhow::{bail, Result};

/// The literal that introduces every marker.
///
/// Assembled with `concat!` rather than written out, because omni-dev measures
/// its own coverage: this file is in its own report, so a contiguous introducer
/// anywhere in this source — a doc example, a test fixture — would be scanned as
/// a real marker, and the deliberately-malformed fixtures below would fail
/// omni-dev's own `coverage diff` run outright. `self_source_contains_no_literal_introducer`
/// pins that invariant; put examples in `docs/coverage.md`, which is not a
/// source file and is never scanned.
///
/// A file that does not contain this substring anywhere cannot carry a marker,
/// and so cannot raise a marker error either — which is what makes the
/// whole-file short-circuit in [`scan`] exact rather than merely fast.
pub const INTRODUCER: &str = concat!("omni-dev", ": coverage");

/// What a marked region does to the reports.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum MarkerKind {
    /// Remove the region's lines from both reports before analysis.
    Ignore,
    /// Keep the region's lines, but mask coverage *flips* on them.
    Tolerate,
}

impl MarkerKind {
    /// The lowercase keyword used in the marker and in rendered output.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Ignore => "ignore",
            Self::Tolerate => "tolerate",
        }
    }
}

/// One marked region, as a 1-based inclusive line span.
///
/// Both marker lines are inside the span. They are comments, so they are never
/// executable and never appear in a coverage report — including them costs
/// nothing and keeps the span identical to what a reader sees in the source.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Region {
    /// Whether the region is ignored or tolerated.
    pub kind: MarkerKind,
    /// First line of the region (the opening marker's own line).
    pub start: u32,
    /// Last line of the region (the `end` marker's line, or `start` for the
    /// single-line form).
    pub end: u32,
    /// The mandatory reason text explaining why the region is silenced.
    pub reason: String,
}

/// The regions of one file, plus the line sets they expand to.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FileMarkers {
    /// Every line covered by an [`MarkerKind::Ignore`] region.
    pub ignored: BTreeSet<u32>,
    /// Every line covered by a [`MarkerKind::Tolerate`] region.
    pub tolerated: BTreeSet<u32>,
    /// The regions themselves, in source order, for reporting.
    pub regions: Vec<Region>,
}

impl FileMarkers {
    /// Expands `regions` into the per-line sets.
    pub fn new(regions: Vec<Region>) -> Self {
        let mut ignored = BTreeSet::new();
        let mut tolerated = BTreeSet::new();
        for region in &regions {
            let set = match region.kind {
                MarkerKind::Ignore => &mut ignored,
                MarkerKind::Tolerate => &mut tolerated,
            };
            set.extend(region.start..=region.end);
        }
        Self {
            ignored,
            tolerated,
            regions,
        }
    }

    /// Whether this file carries no markers at all.
    pub fn is_empty(&self) -> bool {
        self.regions.is_empty()
    }
}

/// A marker keyword, its kind, and whether it is the single-line form. Ordered
/// longest-first so `ignore-line` is never read as `ignore` plus trailing junk.
const KEYWORDS: &[(&str, MarkerKind, bool)] = &[
    ("ignore-line", MarkerKind::Ignore, true),
    ("tolerate-line", MarkerKind::Tolerate, true),
    ("ignore", MarkerKind::Ignore, false),
    ("tolerate", MarkerKind::Tolerate, false),
];

/// A region start that has not been closed yet.
struct Open {
    kind: MarkerKind,
    start: u32,
    reason: String,
}

/// Scans `text` for coverage markers, returning the regions in source order.
///
/// `path` is used only to build error messages. Every malformed marker is a hard
/// error naming `path:line` rather than a silent skip: a marker that does not
/// take effect is worse than one that does not exist, because its author
/// believes the noise is silenced.
pub fn scan(path: &str, text: &str) -> Result<Vec<Region>> {
    // A file with no introducer cannot produce a region *or* an error, so this
    // short-circuit changes nothing but the cost of the common case.
    if !text.contains(INTRODUCER) {
        return Ok(Vec::new());
    }

    let mut regions = Vec::new();
    let mut open: Option<Open> = None;

    for (index, raw) in text.lines().enumerate() {
        let line = u32::try_from(index + 1).unwrap_or(u32::MAX);
        let Some(rest) = raw.trim_end_matches('\r').split(INTRODUCER).nth(1) else {
            continue;
        };
        let rest = rest.trim_start();

        if let Some(tail) = strip_keyword(rest, "end") {
            if !tail.trim().is_empty() {
                bail!(
                    "{path}:{line}: unexpected text after `{INTRODUCER} end`: `{}`",
                    tail.trim()
                );
            }
            let Some(open) = open.take() else {
                bail!("{path}:{line}: `{INTRODUCER} end` without a matching region start");
            };
            regions.push(Region {
                kind: open.kind,
                start: open.start,
                end: line,
                reason: open.reason,
            });
            continue;
        }

        let Some((keyword, kind, single)) = KEYWORDS
            .iter()
            .find(|(keyword, _, _)| strip_keyword(rest, keyword).is_some())
            .copied()
        else {
            bail!(
                "{path}:{line}: unrecognised coverage marker `{INTRODUCER} {}` \
                 (expected `ignore`, `tolerate`, `ignore-line`, `tolerate-line`, or `end`)",
                rest.split_whitespace().next().unwrap_or("")
            );
        };
        // `strip_keyword` just succeeded for this keyword.
        let tail = strip_keyword(rest, keyword).unwrap_or("");
        let reason = parse_reason(path, line, keyword, tail)?;

        if single {
            regions.push(Region {
                kind,
                start: line,
                end: line,
                reason,
            });
            continue;
        }

        if let Some(previous) = &open {
            bail!(
                "{path}:{line}: nested coverage region; the `{}` region opened at line {} is \
                 still open (regions may not overlap)",
                previous.kind.as_str(),
                previous.start
            );
        }
        open = Some(Open {
            kind,
            start: line,
            reason,
        });
    }

    if let Some(open) = open {
        bail!(
            "{path}:{}: unterminated `{INTRODUCER} {}` region (add `{INTRODUCER} end`)",
            open.start,
            open.kind.as_str()
        );
    }

    Ok(regions)
}

/// Strips `keyword` from the front of `rest`, requiring it to be followed by
/// whitespace or end-of-line so `ignore` never matches the prefix of
/// `ignore-line`.
fn strip_keyword<'a>(rest: &'a str, keyword: &str) -> Option<&'a str> {
    let tail = rest.strip_prefix(keyword)?;
    if tail.is_empty() || tail.starts_with(|c: char| c.is_whitespace()) {
        Some(tail)
    } else {
        None
    }
}

/// Extracts the mandatory `reason="…"` from a marker's tail.
///
/// The reason is required because silencing must be explained at the site: a
/// bare marker tells a later reader nothing about whether the noise it hides is
/// still real.
fn parse_reason(path: &str, line: u32, keyword: &str, tail: &str) -> Result<String> {
    let tail = tail.trim();
    let Some(after) = tail.split_once("reason=\"").map(|(_, after)| after) else {
        bail!(
            "{path}:{line}: `{INTRODUCER} {keyword}` needs a reason \
             (write `{INTRODUCER} {keyword} reason=\"why this is silenced\"`)"
        );
    };
    let Some((reason, _)) = after.split_once('"') else {
        bail!("{path}:{line}: unterminated `reason=\"\"` (missing closing quote)");
    };
    let reason = reason.trim();
    if reason.is_empty() {
        bail!("{path}:{line}: `reason=\"\"` is empty; explain why the region is silenced");
    }
    Ok(reason.to_string())
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    /// Builds a marker line. Every fixture goes through this rather than writing
    /// the introducer out, so this file's own source stays marker-free — see
    /// [`INTRODUCER`] and `self_source_contains_no_literal_introducer`.
    fn mark(comment: &str, rest: &str) -> String {
        format!("{comment} {INTRODUCER} {rest}")
    }

    /// Joins fixture lines into a file body with a trailing newline.
    fn file(lines: &[String]) -> String {
        format!("{}\n", lines.join("\n"))
    }

    /// A `//`-commented marker line, the common case.
    fn rs(rest: &str) -> String {
        mark("//", rest)
    }

    /// Scans and unwraps, for the cases that must succeed.
    fn ok(text: &str) -> Vec<Region> {
        scan("src/a.rs", text).unwrap()
    }

    /// Scans and returns the error message, for the cases that must fail.
    fn err(text: &str) -> String {
        scan("src/a.rs", text).unwrap_err().to_string()
    }

    fn region(kind: MarkerKind, start: u32, end: u32, reason: &str) -> Region {
        Region {
            kind,
            start,
            end,
            reason: reason.to_string(),
        }
    }

    /// The guard that keeps omni-dev able to measure its own coverage. A literal
    /// introducer anywhere in this file would be scanned as a real marker when
    /// omni-dev runs `coverage diff` on itself, and the malformed fixtures below
    /// would then fail that run. Examples belong in `docs/coverage.md`.
    #[test]
    fn self_source_contains_no_literal_introducer() {
        let source = include_str!("markers.rs");
        assert!(
            !source.contains(INTRODUCER),
            "src/coverage/markers.rs must not contain a literal marker introducer; \
             build fixtures with `mark()` and put examples in docs/coverage.md"
        );
    }

    #[test]
    fn file_without_the_introducer_yields_nothing() {
        assert!(ok("fn a() {}\n// ordinary comment\n").is_empty());
    }

    #[test]
    fn scans_an_ignore_region() {
        let text = file(&[
            "fn a() {}".to_string(),
            rs("ignore reason=\"CPU-gated\""),
            "fn b() {}".to_string(),
            rs("end"),
            "fn c() {}".to_string(),
        ]);
        assert_eq!(
            ok(&text),
            vec![region(MarkerKind::Ignore, 2, 4, "CPU-gated")]
        );
    }

    #[test]
    fn scans_a_tolerate_region() {
        let text = file(&[
            rs("tolerate reason=\"avx512f arm\""),
            "fn b() {}".to_string(),
            rs("end"),
        ]);
        assert_eq!(
            ok(&text),
            vec![region(MarkerKind::Tolerate, 1, 3, "avx512f arm")]
        );
    }

    #[test]
    fn scans_both_single_line_forms() {
        let text = file(&[
            rs("ignore-line reason=\"one off\""),
            rs("tolerate-line reason=\"flaky\""),
        ]);
        assert_eq!(
            ok(&text),
            vec![
                region(MarkerKind::Ignore, 1, 1, "one off"),
                region(MarkerKind::Tolerate, 2, 2, "flaky"),
            ]
        );
    }

    /// `ignore-line` must not be read as `ignore` with trailing junk — the
    /// keyword table is longest-first *and* `strip_keyword` requires a word
    /// boundary, so either alone would be enough.
    #[test]
    fn single_line_keyword_wins_over_its_prefix() {
        let text = file(&[rs("ignore-line reason=\"one off\"")]);
        let regions = ok(&text);
        assert_eq!(regions.len(), 1);
        assert_eq!(regions[0].end, 1, "must not open a region");
    }

    #[test]
    fn regions_may_repeat_within_a_file() {
        let text = file(&[
            rs("ignore reason=\"a\""),
            "x".to_string(),
            rs("end"),
            "y".to_string(),
            rs("tolerate reason=\"b\""),
            "z".to_string(),
            rs("end"),
        ]);
        assert_eq!(
            ok(&text),
            vec![
                region(MarkerKind::Ignore, 1, 3, "a"),
                region(MarkerKind::Tolerate, 5, 7, "b"),
            ]
        );
    }

    /// Matching is a plain substring, so any comment syntax works.
    #[test]
    fn any_comment_syntax_matches() {
        let text = file(&[
            mark("#", "ignore-line reason=\"shell\""),
            mark("<!--", "ignore-line reason=\"html\" -->"),
        ]);
        assert_eq!(ok(&text).len(), 2);
    }

    /// The flip side of a plain substring match: a marker inside a *string
    /// literal* is matched too, because nothing here parses the host language.
    /// Documented behaviour, not an oversight.
    #[test]
    fn marker_inside_a_string_literal_is_matched() {
        let text = file(&[mark("let s = '", "ignore-line reason=\"in a literal\"';")]);
        assert_eq!(ok(&text).len(), 1);
    }

    #[test]
    fn crlf_line_endings_are_handled() {
        let lines = [rs("ignore reason=\"crlf\""), "x".to_string(), rs("end")];
        let text = format!("{}\r\n", lines.join("\r\n"));
        assert_eq!(ok(&text), vec![region(MarkerKind::Ignore, 1, 3, "crlf")]);
    }

    #[test]
    fn marker_on_the_last_line_without_a_trailing_newline() {
        let text = format!("x\n{}", rs("ignore-line reason=\"last\""));
        assert_eq!(ok(&text), vec![region(MarkerKind::Ignore, 2, 2, "last")]);
    }

    #[test]
    fn reason_is_mandatory() {
        let text = file(&[rs("ignore"), "x".to_string(), rs("end")]);
        let message = err(&text);
        assert!(message.contains("src/a.rs:1"), "{message}");
        assert!(message.contains("needs a reason"), "{message}");
    }

    #[test]
    fn reason_must_not_be_empty() {
        let message = err(&file(&[rs("ignore-line reason=\"\"")]));
        assert!(message.contains("is empty"), "{message}");
    }

    #[test]
    fn reason_quote_must_be_closed() {
        let message = err(&file(&[rs("ignore-line reason=\"unclosed")]));
        assert!(message.contains("unterminated `reason"), "{message}");
    }

    #[test]
    fn nested_regions_are_rejected() {
        let text = file(&[
            rs("ignore reason=\"a\""),
            rs("tolerate reason=\"b\""),
            rs("end"),
        ]);
        let message = err(&text);
        assert!(message.contains("src/a.rs:2"), "{message}");
        assert!(message.contains("nested"), "{message}");
        assert!(message.contains("opened at line 1"), "{message}");
    }

    #[test]
    fn stray_end_is_rejected() {
        let message = err(&file(&["x".to_string(), rs("end")]));
        assert!(message.contains("src/a.rs:2"), "{message}");
        assert!(
            message.contains("without a matching region start"),
            "{message}"
        );
    }

    /// An unterminated region must never widen silently to end-of-file: that
    /// would silence an unbounded amount of code its author never looked at.
    #[test]
    fn unterminated_region_is_rejected() {
        let text = file(&[rs("ignore reason=\"a\""), "x".to_string(), "y".to_string()]);
        let message = err(&text);
        assert!(message.contains("src/a.rs:1"), "{message}");
        assert!(message.contains("unterminated"), "{message}");
    }

    #[test]
    fn unknown_keyword_is_rejected() {
        let message = err(&file(&[rs("skip reason=\"a\"")]));
        assert!(message.contains("unrecognised"), "{message}");
        assert!(message.contains("skip"), "{message}");
    }

    #[test]
    fn text_after_end_is_rejected() {
        let text = file(&[rs("ignore reason=\"a\""), rs("end reason=\"b\"")]);
        let message = err(&text);
        assert!(message.contains("unexpected text after"), "{message}");
    }

    #[test]
    fn file_markers_expand_regions_to_line_sets() {
        let markers = FileMarkers::new(vec![
            region(MarkerKind::Ignore, 2, 4, "a"),
            region(MarkerKind::Tolerate, 7, 7, "b"),
        ]);
        assert_eq!(markers.ignored, BTreeSet::from([2, 3, 4]));
        assert_eq!(markers.tolerated, BTreeSet::from([7]));
        assert!(!markers.is_empty());
        assert!(FileMarkers::default().is_empty());
    }
}