repotoire 0.9.0

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
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
//! AST-driven extraction of [`super::predict::Evidence`] for Python
//! command-injection call sites.
//!
//! # Why a separate module
//!
//! The scorer in [`super::predict`] takes plain data
//! ([`super::predict::Evidence`]) so it can be unit-tested without an
//! AST. This module's job is to populate that struct from a
//! `tree_sitter::Node` for a Python `call` expression.
//!
//! Splitting the two halves matches Phase 2a/2b's
//! `{insecure_crypto,path_traversal}::evidence` split.
//!
//! # What this module knows about
//!
//! - Walking up from the call node to the enclosing
//!   `function_definition` (for name + parameter list) and
//!   `class_definition` (informational).
//! - Detecting the `shell=` kwarg (using the existing
//!   `python_kwarg_truthy` helper with `unknown_default=true`, the
//!   same conservative default the existing `mod.rs` API matcher uses).
//! - Classifying the first positional argument's origin into one of
//!   `Literal` / `ConfigSource` / `RequestSource` / `Parameter` /
//!   `Unknown` (only meaningful for **string-form** calls like
//!   `os.system(cmd)`).
//! - Classifying `argv[0]` and detecting "all elements are literals"
//!   for **list-form** calls (`subprocess.run([...])`).
//! - Reading the source line for `# repotoire: command-static[<reason>]`
//!   or `# repotoire: command-user-controlled[<source>]` annotations.
//!
//! # What this module deliberately does NOT do
//!
//! - Does not look for evidence in non-Python languages. Phase 2d
//!   scope is Python-only per decisions doc D4.
//! - Does not follow data flow across statements. The first-arg-origin
//!   classification looks at the syntactic form of the argument: bare
//!   identifier matched against the enclosing function's parameters,
//!   attribute chain matched against config/request lexicons, string
//!   literal, or unknown. Documented v0 limitation in the decisions doc.
//! - Does not consult the graph for enclosing scope. AST walking is
//!   sufficient and avoids a detector → graph dependency.
//!
//! # Status (commit-by-commit context)
//!
//! Lands ahead of the integration commit in `mod.rs`. The
//! `#![allow(dead_code)]` below silences clippy until that wire-up
//! commit invokes `extract_python_evidence()` from
//! `build_dual_branch_python_finding`. The 23 unit tests at the
//! bottom of this module exercise the public surface end-to-end, so
//! the "never used" diagnostic is structurally misleading — every
//! function and helper is hit by at least one `#[test]`. The allow is
//! removed in the next commit.
#![allow(dead_code)]

use super::predict::{
    extract_command_static_reason, extract_command_user_controlled_source, matches_config_object,
    matches_request_object, Argv0Origin, Evidence, FirstArgOrigin,
};
use crate::detectors::security::ast_helpers::{
    collect_named_args, enclosing_python_function, node_text, python_function_param_names,
    python_kwarg_truthy,
};
use tree_sitter::Node;

/// Extract typed evidence from a Python command-injection call node.
///
/// `call_node` must be a `call` AST node whose function names a
/// command-execution API (e.g. `os.system`, `subprocess.run`).
/// `source` is the file's raw bytes. `lines` is the pre-split
/// source-line slice the scanner already builds; used only for
/// annotation lookup.
///
/// Never panics; missing fields produce `None`/`false`/`Unknown`/
/// defaults in the corresponding Evidence field.
pub(super) fn extract_python_evidence<'a>(
    call_node: Node<'a>,
    source: &'a [u8],
    lines: &[&str],
) -> Evidence {
    let mut ev = Evidence::default();

    // ── Enclosing function (for name + parameter list) and class. ──
    let enclosing_fn = enclosing_python_function(call_node);
    if let Some(fn_node) = enclosing_fn {
        if let Some(name_node) = fn_node.child_by_field_name("name") {
            if let Some(name) = node_text(name_node, source) {
                ev.enclosing_function = Some(name.to_string());
            }
        }
    }
    ev.enclosing_class = enclosing_python_class_name(call_node, source);

    let param_names: Vec<String> = enclosing_fn
        .map(|fn_node| python_function_param_names(fn_node, source))
        .unwrap_or_default();

    // ── Argument collection. ──
    let mut all_args: Vec<Node<'_>> = Vec::new();
    let mut positional_args: Vec<Node<'_>> = Vec::new();
    if let Some(args_node) = call_node.child_by_field_name("arguments") {
        all_args = collect_named_args(args_node);
        positional_args = all_args
            .iter()
            .filter(|a| a.kind() != "keyword_argument" && a.kind() != "comment")
            .copied()
            .collect();
    }

    // ── shell=True kwarg detection (matches mod.rs:1204 semantics). ──
    //
    // `unknown_default=true` means: if `shell=` is present but its value
    // is a non-literal expression (variable, function call, ...), treat
    // it as truthy. This is the conservative default the existing API
    // matcher uses for `shell=` and matches the predictor's intent: a
    // non-literal `shell=` value should not silently be treated as
    // False, since at runtime it might be True.
    ev.kw_shell_true = python_kwarg_truthy(&all_args, "shell", source, true);

    // ── First positional arg classification. ──
    //
    // The classification has two modes depending on whether the first
    // positional arg is a list/tuple (list-form call like
    // `subprocess.run([...])`) or anything else (string-form call like
    // `os.system(cmd)`).
    if let Some(first) = positional_args.first() {
        match first.kind() {
            "list" | "tuple" => {
                // List-form: populate argv0_origin and
                // argv_list_all_literals; leave first_arg_origin = None
                // (or Unknown) because the predictor handles the
                // list-form path through argv0_origin instead.
                let (argv0, all_literals) = classify_list_form_args(*first, source, &param_names);
                ev.argv0_origin = Some(argv0);
                ev.argv_list_all_literals = all_literals;
            }
            _ => {
                // String-form: classify the first arg's origin.
                ev.first_arg_origin = Some(classify_first_arg_origin(*first, source, &param_names));
            }
        }
    }

    // ── Source-line annotations. ──
    let line_idx = call_node.start_position().row;
    if let Some(line) = lines.get(line_idx) {
        ev.command_static_annotation = extract_command_static_reason(line);
        ev.command_user_controlled_annotation = extract_command_user_controlled_source(line);
    }

    ev
}

// ──────────────────────────────────────────────────────────���──────────────────
// First-arg classification (string-form calls)
// ─────────────────────────────────────────────────────────────────────────────

/// Classify a Python expression node into a [`FirstArgOrigin`].
///
/// Decisions, in priority order (mirrors `path_traversal/evidence.rs`):
///
/// 1. String literal (no f-string interpolation) → `Literal`.
/// 2. Bare identifier matching an enclosing-function parameter →
///    `Parameter { name }`.
/// 3. Attribute / call / subscript whose textual chain matches the
///    request lexicon → `RequestSource`.
/// 4. ... matching the config lexicon → `ConfigSource`.
/// 5. Anything else → `Unknown`.
fn classify_first_arg_origin(
    node: Node<'_>,
    source: &[u8],
    param_names: &[String],
) -> FirstArgOrigin {
    match node.kind() {
        "string" => {
            // Pure string literal unless it contains an interpolation.
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if child.kind() == "interpolation" {
                    return FirstArgOrigin::Unknown;
                }
            }
            FirstArgOrigin::Literal
        }
        "concatenated_string" => {
            // `"foo" "bar"` adjacent literal concat: Literal if all are
            // literals, Unknown otherwise.
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if classify_first_arg_origin(child, source, param_names) != FirstArgOrigin::Literal
                {
                    return FirstArgOrigin::Unknown;
                }
            }
            FirstArgOrigin::Literal
        }
        "identifier" => {
            let Some(name) = node_text(node, source) else {
                return FirstArgOrigin::Unknown;
            };
            if param_names.iter().any(|p| p == name) {
                FirstArgOrigin::Parameter {
                    name: name.to_string(),
                }
            } else {
                FirstArgOrigin::Unknown
            }
        }
        "attribute" | "call" | "subscript" => {
            let Some(text) = node_text(node, source) else {
                return FirstArgOrigin::Unknown;
            };
            if matches_request_object(text) {
                FirstArgOrigin::RequestSource
            } else if matches_config_object(text) {
                FirstArgOrigin::ConfigSource
            } else {
                FirstArgOrigin::Unknown
            }
        }
        "parenthesized_expression" => {
            for i in 0..node.named_child_count() {
                if let Some(c) = node.named_child(i) {
                    return classify_first_arg_origin(c, source, param_names);
                }
            }
            FirstArgOrigin::Unknown
        }
        _ => FirstArgOrigin::Unknown,
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// List-form classification
// ─────────────────────────────────────────────────────────────────────────────

/// Classify a Python `list` or `tuple` node passed as the first
/// positional argument to a command-exec API.
///
/// Returns `(argv0_origin, all_literals)`:
///
/// - `argv0_origin`: origin of the first **element** of the list. Note
///   that the existing detector at `command_injection/mod.rs` already
///   reclassifies the canonical shell-c shape (argv[0] of `sh`/`bash`/
///   ... + argv[1] of `-c`) to `PySubprocessShellC` before this point,
///   so we don't see `["sh", "-c", x]` here — we see the post-classified
///   API. The first element here is just argv[0] of an
///   `execve`-semantics list.
/// - `all_literals`: true if every element is a string literal (with
///   no f-string interpolation). The strongest Benign signal.
fn classify_list_form_args(
    list_node: Node<'_>,
    source: &[u8],
    param_names: &[String],
) -> (Argv0Origin, bool) {
    let mut elements: Vec<Node<'_>> = Vec::new();
    let mut cursor = list_node.walk();
    for child in list_node.named_children(&mut cursor) {
        // Skip `comment` and similar non-element named children if any.
        if child.kind() == "comment" {
            continue;
        }
        elements.push(child);
    }

    let all_literals = !elements.is_empty()
        && elements
            .iter()
            .all(|el| is_static_string_literal(*el, source));

    let argv0 = elements
        .first()
        .map(|el| classify_argv0(*el, source, param_names))
        .unwrap_or(Argv0Origin::Other);

    (argv0, all_literals)
}

/// True if `node` is a string literal with no interpolation. Mirrors
/// the conservative posture in `classify_first_arg_origin`.
///
/// `_source` is accepted for signature uniformity with the broader
/// "give me a node, give me the bytes" extractor convention used by
/// the other helpers in this module; the recursion itself only needs
/// node kinds, not bytes. clippy's `only_used_in_recursion` flagged
/// this; underscore-prefix preserves the calling convention without
/// suppressing the lint globally.
fn is_static_string_literal(node: Node<'_>, _source: &[u8]) -> bool {
    match node.kind() {
        "string" => {
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if child.kind() == "interpolation" {
                    return false;
                }
            }
            true
        }
        "concatenated_string" => {
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if !is_static_string_literal(child, _source) {
                    return false;
                }
            }
            true
        }
        _ => false,
    }
}

fn classify_argv0(node: Node<'_>, source: &[u8], param_names: &[String]) -> Argv0Origin {
    match node.kind() {
        "string" | "concatenated_string" => {
            if is_static_string_literal(node, source) {
                Argv0Origin::Literal
            } else {
                Argv0Origin::Other
            }
        }
        "identifier" => {
            let Some(name) = node_text(node, source) else {
                return Argv0Origin::Other;
            };
            if param_names.iter().any(|p| p == name) {
                Argv0Origin::Parameter {
                    name: name.to_string(),
                }
            } else {
                Argv0Origin::Other
            }
        }
        _ => Argv0Origin::Other,
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Enclosing-scope helper
// ─────────────────────────────────────────────────────────────────────────────

/// Walk up from `node` to the nearest enclosing `class_definition` and
/// return its name. Returns `None` at module level.
///
/// Same shape as Phase 2a/2b's `enclosing_python_class_name`. Hoisting
/// to `ast_helpers.rs` is the rule-of-three trigger; deferred to a
/// follow-up cleanup since the three copies (2a/2b/2d) are 8 lines
/// each and consolidation would force a forward-coupling parameter
/// (`source: &[u8]`) into a helper that previously didn't need it.
fn enclosing_python_class_name<'a>(node: Node<'a>, source: &'a [u8]) -> Option<String> {
    let mut cur = node.parent()?;
    loop {
        if cur.kind() == "class_definition" {
            let name = cur.child_by_field_name("name")?;
            return node_text(name, source).map(str::to_string);
        }
        if cur.kind() == "module" {
            return None;
        }
        cur = cur.parent()?;
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::detectors::ast_fingerprint::parse_root_ext;
    use crate::parsers::lightweight::Language;

    /// Parse `source` as Python and find the first `call` node whose
    /// function chain ends in the named attribute or identifier.
    fn first_call_with_attr<'tree>(
        tree: &'tree tree_sitter::Tree,
        source: &[u8],
        attr_name: &str,
    ) -> tree_sitter::Node<'tree> {
        fn walk<'a>(
            node: tree_sitter::Node<'a>,
            source: &[u8],
            attr_name: &str,
        ) -> Option<tree_sitter::Node<'a>> {
            if node.kind() == "call" {
                if let Some(func) = node.child_by_field_name("function") {
                    if func.kind() == "attribute" {
                        if let Some(attr) = func.child_by_field_name("attribute") {
                            if node_text(attr, source) == Some(attr_name) {
                                return Some(node);
                            }
                        }
                    } else if func.kind() == "identifier"
                        && node_text(func, source) == Some(attr_name)
                    {
                        return Some(node);
                    }
                }
            }
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if let Some(found) = walk(child, source, attr_name) {
                    return Some(found);
                }
            }
            None
        }
        walk(tree.root_node(), source, attr_name)
            .unwrap_or_else(|| panic!("no call ending in `.{attr_name}` in source"))
    }

    fn extract(source: &str, attr: &str) -> Evidence {
        let bytes = source.as_bytes();
        let tree = parse_root_ext(source, Language::Python, "py").expect("parse python");
        let lines: Vec<&str> = source.lines().collect();
        let call = first_call_with_attr(&tree, bytes, attr);
        extract_python_evidence(call, bytes, &lines)
    }

    // ── Enclosing scope ──

    #[test]
    fn extracts_enclosing_class_and_function() {
        let src = "
import subprocess
class CmdRunner:
    def run(self, name):
        return subprocess.run(['echo', name])
";
        let ev = extract(src, "run");
        assert_eq!(ev.enclosing_class.as_deref(), Some("CmdRunner"));
        assert_eq!(ev.enclosing_function.as_deref(), Some("run"));
    }

    #[test]
    fn module_level_call_has_no_enclosing_function() {
        let src = "import os\nos.system('date')\n";
        let ev = extract(src, "system");
        assert!(ev.enclosing_function.is_none());
        assert!(ev.enclosing_class.is_none());
    }

    // ── kw_shell_true ──

    #[test]
    fn shell_true_literal_detected() {
        let src = "
import subprocess
def run(cmd):
    return subprocess.run(cmd, shell=True)
";
        let ev = extract(src, "run");
        assert!(ev.kw_shell_true);
    }

    #[test]
    fn shell_false_literal_not_detected() {
        let src = "
import subprocess
def run(cmd):
    return subprocess.run(['echo', cmd], shell=False)
";
        let ev = extract(src, "run");
        assert!(!ev.kw_shell_true);
    }

    #[test]
    fn shell_absent_kwarg_not_detected() {
        let src = "
import subprocess
def run(cmd):
    return subprocess.run(['echo', cmd])
";
        let ev = extract(src, "run");
        assert!(!ev.kw_shell_true);
    }

    #[test]
    fn shell_non_literal_treated_as_truthy_conservative() {
        // `shell=enable_shell` where `enable_shell` is a variable —
        // conservative posture is to treat as truthy, mirroring
        // `mod.rs:1204` semantics (`unknown_default=true`).
        let src = "
import subprocess
def run(cmd, enable_shell):
    return subprocess.run(cmd, shell=enable_shell)
";
        let ev = extract(src, "run");
        assert!(
            ev.kw_shell_true,
            "non-literal shell= value should be treated as truthy"
        );
    }

    // ── First-arg origin (string-form) ──

    #[test]
    fn string_form_literal_first_arg() {
        let src = "
import os
def run():
    os.system('date')
";
        let ev = extract(src, "system");
        assert_eq!(ev.first_arg_origin, Some(FirstArgOrigin::Literal));
        assert!(ev.argv0_origin.is_none(), "string-form: argv0_origin None");
    }

    #[test]
    fn string_form_parameter_first_arg() {
        let src = "
import os
def run(cmd):
    os.system(cmd)
";
        let ev = extract(src, "system");
        match ev.first_arg_origin {
            Some(FirstArgOrigin::Parameter { ref name }) if name == "cmd" => {}
            other => panic!("expected Parameter {{ cmd }}, got {other:?}"),
        }
    }

    #[test]
    fn string_form_request_source_first_arg() {
        let src = "
import os
def view(request):
    os.system(request.GET['cmd'])
";
        let ev = extract(src, "system");
        assert_eq!(ev.first_arg_origin, Some(FirstArgOrigin::RequestSource));
    }

    #[test]
    fn string_form_config_source_first_arg() {
        let src = "
import os
def run():
    os.system(os.environ.get('CMD'))
";
        let ev = extract(src, "system");
        assert_eq!(ev.first_arg_origin, Some(FirstArgOrigin::ConfigSource));
    }

    #[test]
    fn string_form_fstring_interpolation_is_unknown() {
        let src = "
import os
def run(name):
    os.system(f'echo {name}')
";
        let ev = extract(src, "system");
        assert_eq!(ev.first_arg_origin, Some(FirstArgOrigin::Unknown));
    }

    // ── List-form: argv_list_all_literals ──

    #[test]
    fn list_form_all_static_literals() {
        let src = "
import subprocess
def run():
    subprocess.run(['git', 'status', '--porcelain'])
";
        let ev = extract(src, "run");
        assert!(ev.argv_list_all_literals);
        assert_eq!(ev.argv0_origin, Some(Argv0Origin::Literal));
        assert!(ev.first_arg_origin.is_none());
    }

    #[test]
    fn list_form_with_variable_element_not_all_literals() {
        let src = "
import subprocess
def run(branch):
    subprocess.run(['git', 'checkout', branch])
";
        let ev = extract(src, "run");
        assert!(!ev.argv_list_all_literals);
        assert_eq!(ev.argv0_origin, Some(Argv0Origin::Literal));
    }

    #[test]
    fn list_form_param_argv0() {
        let src = "
import subprocess
def run(binary, arg):
    subprocess.run([binary, arg])
";
        let ev = extract(src, "run");
        assert!(!ev.argv_list_all_literals);
        match ev.argv0_origin {
            Some(Argv0Origin::Parameter { ref name }) if name == "binary" => {}
            other => panic!("expected Parameter {{ binary }}, got {other:?}"),
        }
    }

    #[test]
    fn list_form_other_argv0() {
        let src = "
import subprocess
def run():
    subprocess.run([get_binary(), 'arg'])
";
        let ev = extract(src, "run");
        assert_eq!(ev.argv0_origin, Some(Argv0Origin::Other));
        assert!(!ev.argv_list_all_literals);
    }

    #[test]
    fn list_form_tuple_works_same_as_list() {
        let src = "
import subprocess
def run():
    subprocess.run(('git', 'status'))
";
        let ev = extract(src, "run");
        assert!(ev.argv_list_all_literals);
        assert_eq!(ev.argv0_origin, Some(Argv0Origin::Literal));
    }

    #[test]
    fn list_form_fstring_argv0_is_other() {
        let src = "
import subprocess
def run(x):
    subprocess.run([f'/bin/{x}', 'arg'])
";
        let ev = extract(src, "run");
        // f-string with interpolation is not a static literal.
        assert!(!ev.argv_list_all_literals);
        assert_eq!(ev.argv0_origin, Some(Argv0Origin::Other));
    }

    // ── Annotations (kind isolation) ──

    #[test]
    fn command_static_annotation_extracted() {
        let src = "
import subprocess
def run(branch):
    subprocess.run(['git', 'checkout', branch])  # repotoire: command-static[allowlisted]
";
        let ev = extract(src, "run");
        assert_eq!(ev.command_static_annotation.as_deref(), Some("allowlisted"));
        assert!(ev.command_user_controlled_annotation.is_none());
    }

    #[test]
    fn command_user_controlled_annotation_extracted() {
        let src = "
import subprocess
def run(branch):
    subprocess.run(['git', 'checkout', branch])  # repotoire: command-user-controlled[GET]
";
        let ev = extract(src, "run");
        assert_eq!(
            ev.command_user_controlled_annotation.as_deref(),
            Some("GET")
        );
        assert!(ev.command_static_annotation.is_none());
    }

    #[test]
    fn internal_path_annotation_ignored_by_command_extractor() {
        // The 2b annotation kind must NOT match the 2d extractor.
        let src = "
import subprocess
def run(p):
    subprocess.run(['cat', p])  # repotoire: internal-path[ok]
";
        let ev = extract(src, "run");
        assert!(ev.command_static_annotation.is_none());
        assert!(ev.command_user_controlled_annotation.is_none());
    }

    #[test]
    fn no_annotation_yields_none() {
        let src = "
import subprocess
def run(p):
    subprocess.run(['cat', p])
";
        let ev = extract(src, "run");
        assert!(ev.command_static_annotation.is_none());
        assert!(ev.command_user_controlled_annotation.is_none());
    }

    // ── Canonical worked example from decisions D1 ──

    #[test]
    fn worked_example_2_shell_true_param_interpolation() {
        // `subprocess.run(f"echo {name}", shell=True)` where `name` is
        // a function parameter. Evidence should fire `kw_shell_true`
        // (the f-string with interpolation classifies first_arg_origin
        // as Unknown, which is correct — the interpolated `name` is
        // captured by the existing arg_kind = Interpolated path).
        let src = "
import subprocess
def run(name):
    subprocess.run(f'echo {name}', shell=True)
";
        let ev = extract(src, "run");
        assert!(ev.kw_shell_true);
        assert_eq!(ev.first_arg_origin, Some(FirstArgOrigin::Unknown));
        assert!(ev.argv0_origin.is_none());
    }

    #[test]
    fn worked_example_4_mixed_list_literal_argv0() {
        // `subprocess.run(["xdg-open", url])` where `url` is a param.
        // Evidence should fire `argv0_origin = Literal` (not the
        // strongest signal but enough for Benign), NOT
        // `argv_list_all_literals` (because `url` is variable).
        let src = "
import subprocess
def open_url(url):
    subprocess.run(['xdg-open', url])
";
        let ev = extract(src, "run");
        assert_eq!(ev.argv0_origin, Some(Argv0Origin::Literal));
        assert!(!ev.argv_list_all_literals);
        assert!(!ev.kw_shell_true);
    }
}