ai-slop 0.1.2

Lints bug reports, commit messages, changelogs, and other outbound text for AI slop
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
//! Output schema, deterministic ordering, escaping, and the emit-time span
//! invariant. A mapping bug becomes `instrumentation_error`, never a wrong
//! finding.

use crate::engine::{CompiledPolicy, Hit};
use crate::extract::{Doc, RegionKind};
use crate::input::Prepared;
use crate::policy::{Lifecycle, Tier};
use crate::{AnalysisError, Config, Stance, WaiverAuthority};
use serde::Serialize;
use serde_json::value::RawValue;
use std::ops::Range;

pub const SNIPPET_CAP: usize = 200;

#[derive(Serialize, Clone, Debug)]
pub struct SpanOut {
    pub start: usize,
    pub end: usize,
}

#[derive(Serialize, Clone, Debug)]
pub struct Suggestion {
    pub start: usize,
    pub end: usize,
    pub replace_with: String,
}

#[derive(Serialize, Debug)]
pub struct Finding {
    pub rule_id: String,
    pub family: String,
    pub state: String,
    pub lifecycle: String,
    pub waived: bool,
    pub spans: Vec<SpanOut>,
    pub view: String,
    pub snippet: Box<RawValue>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub section: Option<String>,
    pub provenance: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub suggestion: Option<Suggestion>,
    pub message: String,
}

#[derive(Serialize, Debug)]
pub struct PolicyRef {
    pub version: String,
    pub digest: String,
}

#[derive(Serialize, Debug)]
pub struct ArtifactRef {
    pub sha256: String,
    pub bytes: usize,
    pub encoding: String,
    pub input_format: String,
    pub profile: String,
}

#[derive(Serialize, Debug)]
pub struct ExcludedOut {
    pub start: usize,
    pub end: usize,
    pub reason: String,
}

#[derive(Serialize, Debug)]
pub struct Segmentation {
    pub prose_bytes: usize,
    pub excluded: Vec<ExcludedOut>,
}

#[derive(Serialize, Debug)]
pub struct SectionOut {
    pub title: String,
    pub level: u32,
    pub start: usize,
    pub end: usize,
}

#[derive(Serialize, Debug)]
pub struct Coverage {
    pub sections: Vec<SectionOut>,
    pub segmentation: Segmentation,
    pub notes: Vec<String>,
}

#[derive(Serialize, Debug)]
pub struct Report {
    pub schema_version: &'static str,
    pub tool_version: &'static str,
    pub policy: PolicyRef,
    pub artifact: ArtifactRef,
    pub result_state: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
    pub findings: Vec<Finding>,
    pub coverage: Coverage,
}

impl Report {
    /// Stable exit code with precedence 30 > 40 > 10 > 20 > 0. The error
    /// states are produced by the binary from `AnalysisError`, so a report
    /// only ever maps to 10, 20, or 0.
    pub fn exit_code(&self) -> i32 {
        match self.result_state.as_str() {
            "instrumentation_error" => 30,
            "unsupported_input" => 40,
            "violations_present" => 10,
            "candidates_present" => 20,
            _ => 0,
        }
    }
}

/// JSON string literal with every C0 and C1 control rendered as `\uXXXX`.
pub fn escape_json_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            c if (c as u32) < 0x20 || ((c as u32) >= 0x7F && (c as u32) <= 0x9F) => {
                out.push_str(&format!("\\u{:04X}", c as u32));
            }
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// Trigger fidelity: does the reported source span still carry the `trigger`
/// the pattern matched? Two independent reconstructions of what the span renders
/// to are checked, and EITHER carrying the trigger passes.
///
/// `raw_slice` is the raw source bytes re-rendered through `render_key`
/// (entity/escape decode, zero-width and HTML-markup removal, NFC, whitespace-
/// run folding, ASCII case-folding); it covers non-norm hits (raw/code/link/
/// heading scans) whose span never went through a mapping. `norm_text` is the
/// norm text the source span overlaps, assembled by `source_span_norm_text` with
/// every exclusion (inline and fenced code, HTML markup, link URLs, autolinks)
/// and mapping (entities, escapes, softbreaks, owned content) applied — Identity
/// segments CLIPPED to their actual source-overlap sub-slice, Mapped segments
/// contributing their whole norm text. It is computed for every hit (norm or
/// not); the clip is what stops a span displaced onto one byte of a
/// trigger-bearing paragraph from inheriting the paragraph's text and passing.
///
/// The test is CONTAINMENT, not equality: a Mapped segment expands to its whole
/// source range, so a legitimate span surrounds the trigger. A span no
/// reconstruction carries is a mapping bug. An empty trigger key (all
/// markup/whitespace) is unverifiable and accepted.
///
/// `decoded` is the third reconstruction, for hits found in a
/// DECODED link destination: the parser-decoded text of the region the span
/// maps into, carried on the hit. It is checked by direct containment — the
/// exact pulldown decode, no entity-table dependence — because the raw
/// spelling may hide the trigger behind references OUTSIDE the crate's
/// enumerated table (`&lowbar;`, `&period;`), which `render_key` cannot
/// resolve; routing those spans through the render_key bridge aborted the
/// whole report (exit 30) on exactly the entity-hidden tracking URLs the
/// decoded scan exists to catch. Like the norm-text reconstruction, the
/// engine binds `decoded` to the hit's own region, so it vouches only for
/// the span it maps to. Empty when the hit is not from the decoded pass.
fn slice_carries_trigger(raw_slice: &str, norm_text: &str, decoded: &str, trigger: &str) -> bool {
    if !decoded.is_empty() && decoded.contains(trigger) {
        return true;
    }
    let want = crate::views::render_key(trigger);
    if want.is_empty() {
        return true;
    }
    crate::views::render_key(raw_slice).contains(&want)
        || crate::views::render_key(norm_text).contains(&want)
}

fn snippet_raw(src: &str, span: &Range<usize>) -> Result<Box<RawValue>, AnalysisError> {
    let full = src.get(span.clone()).ok_or_else(|| {
        AnalysisError::Instrumentation(format!(
            "span {}..{} does not index the payload on a char boundary",
            span.start, span.end
        ))
    })?;
    // The span invariant: the emitted bytes are exactly the source slice.
    if full.as_bytes() != &src.as_bytes()[span.start..span.end] {
        return Err(AnalysisError::Instrumentation(format!(
            "span invariant failed at {}..{}",
            span.start, span.end
        )));
    }
    let mut cap = full.len().min(SNIPPET_CAP);
    while cap > 0 && !full.is_char_boundary(cap) {
        cap -= 1;
    }
    let escaped = escape_json_string(&full[..cap]);
    RawValue::from_string(escaped)
        .map_err(|e| AnalysisError::Instrumentation(format!("snippet escape: {e}")))
}

fn section_for(doc: &Doc, offset: usize) -> Option<String> {
    doc.sections
        .iter()
        .filter(|s| s.range.start <= offset && offset < s.range.end)
        .max_by_key(|s| (s.level, s.range.start))
        .map(|s| s.title.clone())
}

fn suggestion_for(rule_id: &str, span: &Range<usize>) -> Option<Suggestion> {
    let replace_with = match rule_id {
        "SLOP-M001" => ", ",
        "SLOP-M002" => ".",
        "SLOP-M003" | "SLOP-M006" | "SLOP-T001" | "SLOP-I001" => "",
        _ => return None,
    };
    Some(Suggestion {
        start: span.start,
        end: span.end,
        replace_with: replace_with.to_string(),
    })
}

struct WaiverDecision {
    waived: bool,
    notes: Vec<String>,
}

fn waiver_decision(
    cp: &CompiledPolicy,
    config: &Config,
    rule_id: &str,
    human_only: bool,
    span: &Range<usize>,
) -> WaiverDecision {
    let mut notes = Vec::new();
    for w in &config.waivers {
        if w.rule_id != rule_id {
            continue;
        }
        if cp.pkg.rule_by_id(&w.rule_id).is_none() {
            notes.push(format!("waiver for unknown rule {} ignored", w.rule_id));
            continue;
        }
        if let Some(ws) = &w.span {
            if !(ws.start <= span.start && ws.end >= span.end) {
                continue;
            }
        }
        if let (Some(expires), Some(now)) = (&w.expires, config.now_unix) {
            match crate::waiver::parse_rfc3339(expires) {
                Some(t) if t < now => {
                    notes.push(format!("expired waiver for {} ignored", w.rule_id));
                    continue;
                }
                None => {
                    notes.push(format!(
                        "waiver for {} has an unreadable expiry and is ignored",
                        w.rule_id
                    ));
                    continue;
                }
                _ => {}
            }
        }
        // The authority floor is the single shared decision in
        // `waiver::floor_allows`; the interactive path and `verify` cannot
        // drift because both consult it. A waiver is human-privileged only
        // when it names the recognized human signer; an absent or
        // unrecognized `signer_kind` is untrusted and gets at most agent
        // privilege.
        let authority = config
            .deployment
            .waiver_authority
            .unwrap_or(WaiverAuthority::Human);
        if let Err(reason) =
            crate::waiver::floor_allows(w.signer_kind.as_deref(), rule_id, human_only, authority)
        {
            notes.push(reason);
            continue;
        }
        return WaiverDecision {
            waived: true,
            notes,
        };
    }
    WaiverDecision {
        waived: false,
        notes,
    }
}

pub fn assemble(
    cp: &CompiledPolicy,
    prepared: &Prepared,
    doc: &Doc,
    norm: &crate::views::NormView,
    config: &Config,
    hits: Vec<Hit>,
) -> Result<Report, AnalysisError> {
    let src = prepared.text.as_str();
    let mut notes: Vec<String> = Vec::new();
    if prepared.bom_stripped {
        notes.push("leading BOM stripped; offsets index the post-BOM payload".to_string());
    }
    if prepared.mixed_line_endings {
        notes.push("mixed line endings".to_string());
    }

    let mut findings: Vec<Finding> = Vec::new();
    let mut adversarial = false;

    for hit in &hits {
        let rule = &cp.pkg.rules[hit.rule];
        if rule.lifecycle == Lifecycle::Deprecated {
            continue;
        }
        let stance = rule.stance(config.profile, hit.field);
        if stance == Stance::Off {
            continue;
        }
        let mut tier = rule.tier;
        let mut lifecycle = rule.lifecycle;
        let mut provenance = "author".to_string();

        if hit.force_hint {
            tier = Tier::CoverageHint;
            lifecycle = Lifecycle::Advisory;
        } else if hit.force_candidate && tier == Tier::Violation {
            tier = Tier::Candidate;
        }
        if stance == Stance::Relax {
            match tier {
                Tier::Violation => tier = Tier::Candidate,
                Tier::Candidate => lifecycle = Lifecycle::Advisory,
                Tier::CoverageHint => {}
            }
        }
        if hit.quoted
            && tier == Tier::Violation
            && cp.pkg.quotation_downgrade.iter().any(|id| id == &rule.id)
        {
            tier = Tier::Candidate;
            provenance = "claimed-quotation".to_string();
        }
        if config.deployment.demote.iter().any(|id| id == &rule.id) {
            lifecycle = Lifecycle::Advisory;
        }
        if rule.id == "SLOP-J001" {
            adversarial = true;
        }

        let decision = waiver_decision(cp, config, &rule.id, rule.human_only_waiver, &hit.span);
        notes.extend(decision.notes);

        let snippet = snippet_raw(src, &hit.span)?;
        // Trigger fidelity: the reported source slice, re-rendered through
        // the same view transforms, must still carry the trigger the pattern
        // matched. The comparison is whitespace-run-folded and case-folded via
        // `render_key`, and by CONTAINMENT rather than equality — a Mapped
        // segment expands `to_source` to its whole source range, so the slice
        // legitimately surrounds the trigger (softbreaks, entities, escapes,
        // inline-tag fusion, owned content). A slice that does not carry the
        // trigger is a mapping bug: fail closed as instrumentation rather than
        // emit a finding at the wrong bytes.
        if let Some(trigger) = &hit.trigger {
            if !slice_carries_trigger(
                &src[hit.span.clone()],
                &norm.source_span_norm_text(&hit.span),
                hit.decoded.as_deref().unwrap_or(""),
                trigger,
            ) {
                return Err(AnalysisError::Instrumentation(format!(
                    "trigger-fidelity: source slice {}..{} does not render to the matched trigger",
                    hit.span.start, hit.span.end
                )));
            }
        }
        let suggestion = if config.suggest && !hit.force_hint {
            suggestion_for(&rule.id, &hit.span)
        } else {
            None
        };
        findings.push(Finding {
            rule_id: rule.id.clone(),
            family: rule.family.clone(),
            state: tier.as_str().to_string(),
            lifecycle: lifecycle.as_str().to_string(),
            waived: decision.waived,
            spans: vec![SpanOut {
                start: hit.span.start,
                end: hit.span.end,
            }],
            view: rule.view.as_str().to_string(),
            snippet,
            section: section_for(doc, hit.span.start),
            provenance,
            suggestion,
            message: rule.name.replace('-', " "),
        });
    }

    // Deterministic ordering, then merge duplicates of the same rule at the
    // same span.
    findings.sort_by(|a, b| {
        (a.spans[0].start, a.spans[0].end, a.rule_id.as_str()).cmp(&(
            b.spans[0].start,
            b.spans[0].end,
            b.rule_id.as_str(),
        ))
    });
    findings.dedup_by(|a, b| {
        a.rule_id == b.rule_id
            && a.spans[0].start == b.spans[0].start
            && a.spans[0].end == b.spans[0].end
            && a.state == b.state
    });

    if findings.len() > config.limits.max_findings {
        return Err(AnalysisError::Instrumentation(format!(
            "{} findings exceed the {} limit",
            findings.len(),
            config.limits.max_findings
        )));
    }

    if adversarial {
        notes.push(
            "injection pattern present: adversarial mode, all candidates require human adjudication"
                .to_string(),
        );
    }

    let mut has_violation = false;
    let mut has_candidate = false;
    for f in &findings {
        if f.waived || f.lifecycle != "blocking" {
            continue;
        }
        match f.state.as_str() {
            "violation" => has_violation = true,
            "candidate" => has_candidate = true,
            _ => {}
        }
    }
    let result_state = if has_violation {
        "violations_present"
    } else if has_candidate {
        "candidates_present"
    } else {
        "no_findings"
    };

    let prose_bytes: usize = doc
        .regions
        .iter()
        .filter(|r| r.kind == RegionKind::Prose)
        .map(|r| r.range.len())
        .sum();
    let excluded = doc
        .regions
        .iter()
        .filter(|r| r.kind != RegionKind::Prose)
        .map(|r| ExcludedOut {
            start: r.range.start,
            end: r.range.end,
            reason: r.kind.reason().to_string(),
        })
        .collect();
    let sections = doc
        .sections
        .iter()
        .map(|s| SectionOut {
            title: s.title.clone(),
            level: s.level,
            start: s.range.start,
            end: s.range.end,
        })
        .collect();

    let note = if config.suggest {
        Some(
            "applying any suggestion changes the artifact hash and invalidates any approval"
                .to_string(),
        )
    } else {
        None
    };

    Ok(Report {
        schema_version: crate::SCHEMA_VERSION,
        tool_version: crate::TOOL_VERSION,
        policy: PolicyRef {
            version: cp.pkg.version.clone(),
            digest: cp.pkg.digest.clone(),
        },
        artifact: ArtifactRef {
            sha256: prepared.sha256.clone(),
            bytes: prepared.original_len,
            encoding: "utf-8".to_string(),
            input_format: config.input_format.as_str().to_string(),
            profile: config.profile.as_str().to_string(),
        },
        result_state: result_state.to_string(),
        note,
        findings,
        coverage: Coverage {
            sections,
            segmentation: Segmentation {
                prose_bytes,
                excluded,
            },
            notes,
        },
    })
}

#[cfg(test)]
mod trigger_fidelity_tests {
    use super::slice_carries_trigger;

    // The raw-slice reconstruction alone verifies these structured divergences
    // (empty norm-text arg, as a non-norm hit would supply).
    #[test]
    fn raw_slice_legitimate_divergences_verify() {
        // Softbreak: norm folds "\n" to a space; the source keeps the newline.
        assert!(slice_carries_trigger("not\njust", "", "", "not just"));
        // Enumerated entity: to_source expands the Mapped em-dash to `&mdash;`.
        assert!(slice_carries_trigger("a&mdash;b", "", "", "a\u{2014}b"));
        // Backslash escape resolved in the norm view.
        assert!(slice_carries_trigger(r"foo\*bar", "", "", "foo*bar"));
        // Inline-tag fusion: the norm reads "delve", the slice carries markup.
        assert!(slice_carries_trigger("de<b></b>lve", "", "", "delve"));
        // Element-boundary space kept across inline markup.
        assert!(slice_carries_trigger(
            "game <i>changer</i>",
            "",
            "",
            "game changer"
        ));
        // Case-folding.
        assert!(slice_carries_trigger("DELVE", "", "", "delve"));
        // Whole-source expansion of a Mapped (owned) segment.
        assert!(slice_carries_trigger(
            "A comprehensive tapestry to delve into",
            "",
            "",
            "delve"
        ));
    }

    // An INVERSION of an earlier premise. Without the barrier, the norm view
    // fused "de"+"lve" across the excluded inline-code gap into "delve", and
    // the norm-text reconstruction certified that fusion as a legitimate
    // finding. That fusion WAS the SLOP-A001 false positive — the reader sees
    // "de x lve", never "delve" — so the inline-code barrier interposes U+FFFD
    // in the norm view, and the pipeline supplies "de\u{FFFD}lve" (not
    // "delve") as the span's norm text. A hit assembled across inline code can
    // no longer exist, and were one ever produced, trigger fidelity fails it
    // closed.
    #[test]
    fn norm_text_reconstruction_respects_inline_code_barrier() {
        // The barrier norm text does not carry the trigger: fail closed.
        assert!(!slice_carries_trigger(
            "de`x`lve",
            "de\u{FFFD}lve",
            "",
            "delve"
        ));
        // A gross desync still fails BOTH reconstructions.
        assert!(!slice_carries_trigger("de`x`lve", "parser", "", "delve"));
        // The norm-text path itself still works where the norm LEGITIMATELY
        // diverges from the raw slice: a decoded entity's norm text verifies
        // a trigger the raw bytes don't spell.
        assert!(slice_carries_trigger("x", "a\u{2014}b", "", "a\u{2014}b"));
    }

    // A span that points at the wrong bytes must fail closed on both paths.
    #[test]
    fn corrupted_span_fails_closed() {
        assert!(!slice_carries_trigger("The parser reads", "", "", "delve"));
        // Off-by-one into a neighbouring word.
        assert!(!slice_carries_trigger("elve", "", "", "delve"));
        assert!(!slice_carries_trigger("", "", "", "delve"));
    }

    // A hit from the decoded-destination pass verifies against
    // the PARSER-decoded text — full HTML5 entity semantics — where the
    // render_key bridge (enumerated entity table only) cannot resolve the
    // raw spelling. Routed through render_key instead, this shape would
    // abort the whole report (exit 30).
    #[test]
    fn decoded_destination_reconstruction_verifies_outside_entity_table() {
        // Raw slice spells `&lowbar;`; render_key leaves it literal, so the
        // raw path fails — the decoded text carries the trigger.
        assert!(slice_carries_trigger(
            "https://e/?utm&lowbar;source=chatgpt",
            "",
            "https://e/?utm_source=chatgpt",
            "utm_source=chatgpt"
        ));
        // A desynced decoded text still fails closed.
        assert!(!slice_carries_trigger(
            "https://e/?utm&lowbar;source=chatgpt",
            "",
            "https://e/?clean",
            "utm_source=chatgpt"
        ));
    }
}