apr-cli 0.64.0

CLI tool for APR model inspection, debugging, and operations
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

    /// Parse CLI args on a thread with 16 MB stack.
    /// Clap's parser for 34 subcommands exceeds the default test-thread
    /// stack in debug builds.
    fn parse_cli(args: Vec<&'static str>) -> Result<Cli, clap::error::Error> {
        std::thread::Builder::new()
            .stack_size(16 * 1024 * 1024)
            .spawn(move || Cli::try_parse_from(args))
            .expect("spawn thread")
            .join()
            .expect("join thread")
    }

    /// Test CLI parsing with clap's debug_assert
    #[test]
    fn test_cli_parsing_valid() {
        use clap::CommandFactory;
        std::thread::Builder::new()
            .stack_size(16 * 1024 * 1024)
            .spawn(|| Cli::command().debug_assert())
            .expect("spawn")
            .join()
            .expect("join");
    }

    /// `apr code -p "hi" --model X` must RUN model X.
    ///
    /// With `trailing_var_arg` the option landed in the prompt vector and the
    /// run silently fell back to auto-discovery — a different model than the
    /// one named, with no warning.
    #[test]
    fn test_parse_code_model_after_prompt_is_honoured() {
        let args = vec!["apr", "code", "-p", "hi", "--model", "/tmp/named.gguf"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Code {
                model,
                prompt,
                print,
                ..
            } => {
                assert!(print, "-p must still set print");
                assert_eq!(
                    model,
                    Some(PathBuf::from("/tmp/named.gguf")),
                    "--model written after the prompt must be honoured, not swallowed"
                );
                assert_eq!(
                    prompt,
                    vec!["hi".to_string()],
                    "the prompt must not absorb the option"
                );
            }
            _ => panic!("Expected Code command"),
        }
    }

    /// A misspelled option after the prompt must be a parse error, not silence.
    #[test]
    fn test_parse_code_unknown_option_after_prompt_is_rejected() {
        let args = vec!["apr", "code", "-p", "hi", "--totally-bogus-flag-xyz"];
        let err = parse_cli(args)
            .expect_err("an unknown option after the prompt must fail to parse");
        assert_eq!(
            err.kind(),
            clap::error::ErrorKind::UnknownArgument,
            "expected UnknownArgument, got {:?}",
            err.kind()
        );
    }

    /// Multi-word prompts must still collect into the positional vector.
    #[test]
    fn test_parse_code_multiword_prompt_still_collects() {
        let args = vec!["apr", "code", "-p", "fix", "the", "auth", "bug"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Code { prompt, .. } => {
                assert_eq!(prompt, vec!["fix", "the", "auth", "bug"]);
            }
            _ => panic!("Expected Code command"),
        }
    }

    /// Test parsing 'apr inspect' command
    #[test]
    fn test_parse_inspect_command() {
        let args = vec!["apr", "inspect", "model.apr"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Inspect { file, .. } => {
                assert_eq!(file, PathBuf::from("model.apr"));
            }
            _ => panic!("Expected Inspect command"),
        }
    }

    /// Test parsing 'apr inspect' with flags
    #[test]
    fn test_parse_inspect_with_flags() {
        let args = vec!["apr", "inspect", "model.apr", "--vocab", "--json"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Inspect {
                file, vocab, json, ..
            } => {
                assert_eq!(file, PathBuf::from("model.apr"));
                assert!(vocab);
                assert!(json);
            }
            _ => panic!("Expected Inspect command"),
        }
    }

    /// Test parsing 'apr serve run' command
    #[test]
    fn test_parse_serve_command() {
        let args = vec!["apr", "serve", "run", "model.apr", "--port", "3000"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Serve {
                command: ServeCommands::Run { ref file, port, .. },
            } => {
                assert_eq!(*file, PathBuf::from("model.apr"));
                assert_eq!(port, 3000);
            }
            _ => panic!("Expected Serve Run command"),
        }
    }

    /// Test parsing 'apr run' command
    #[test]
    fn test_parse_run_command() {
        let args = vec![
            "apr",
            "run",
            "hf://openai/whisper-tiny",
            "--prompt",
            "Hello",
            "--max-tokens",
            "64",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Run {
                source,
                prompt,
                max_tokens,
                ..
            } => {
                assert_eq!(source, "hf://openai/whisper-tiny");
                assert_eq!(prompt, Some("Hello".to_string()));
                assert_eq!(max_tokens, 64);
            }
            _ => panic!("Expected Run command"),
        }
    }

    /// Test parsing 'apr run --stream' flag (NDJSON token stream).
    #[test]
    fn test_parse_run_stream_flag() {
        let args = vec!["apr", "run", "model.gguf", "--prompt", "Hi", "--stream"];
        let cli = parse_cli(args).expect("Failed to parse --stream");
        match *cli.command {
            Commands::Run {
                source,
                stream,
                ..
            } => {
                assert_eq!(source, "model.gguf");
                assert!(stream, "--stream must set stream=true");
            }
            _ => panic!("Expected Run command"),
        }
    }

    /// Default for --stream is false when omitted.
    #[test]
    fn test_parse_run_stream_default_false() {
        let args = vec!["apr", "run", "model.gguf", "--prompt", "Hi"];
        let cli = parse_cli(args).expect("Failed to parse default");
        match *cli.command {
            Commands::Run { stream, .. } => assert!(!stream, "--stream defaults to false"),
            _ => panic!("Expected Run command"),
        }
    }

    /// Test parsing 'apr chat' command
    #[test]
    fn test_parse_chat_command() {
        let args = vec![
            "apr",
            "chat",
            "model.gguf",
            "--temperature",
            "0.5",
            "--top-p",
            "0.95",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Chat {
                file,
                temperature,
                top_p,
                ..
            }) => {
                assert_eq!(file, PathBuf::from("model.gguf"));
                assert!((temperature - 0.5).abs() < f32::EPSILON);
                assert!((top_p - 0.95).abs() < f32::EPSILON);
            }
            _ => panic!("Expected Chat command"),
        }
    }

    /// Test parsing 'apr validate' command with quality flag
    #[test]
    fn test_parse_validate_with_quality() {
        let args = vec!["apr", "validate", "model.apr", "--quality", "--strict"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Validate {
                file,
                quality,
                strict,
                ..
            } => {
                assert_eq!(file, PathBuf::from("model.apr"));
                assert!(quality);
                assert!(strict);
            }
            _ => panic!("Expected Validate command"),
        }
    }

    /// Test parsing 'apr diff' command
    #[test]
    fn test_parse_diff_command() {
        let args = vec!["apr", "diff", "model1.apr", "model2.apr", "--weights"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Diff {
                file1,
                file2,
                weights,
                ..
            } => {
                assert_eq!(file1, PathBuf::from("model1.apr"));
                assert_eq!(file2, PathBuf::from("model2.apr"));
                assert!(weights);
            }
            _ => panic!("Expected Diff command"),
        }
    }

    /// Test parsing 'apr bench' command
    #[test]
    fn test_parse_bench_command() {
        let args = vec![
            "apr",
            "bench",
            "model.gguf",
            "--warmup",
            "5",
            "--iterations",
            "10",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Bench {
                file,
                warmup,
                iterations,
                ..
            }) => {
                assert_eq!(file, PathBuf::from("model.gguf"));
                assert_eq!(warmup, 5);
                assert_eq!(iterations, 10);
            }
            _ => panic!("Expected Bench command"),
        }
    }

    /// Test parsing 'apr cbtop' command with CI flags
    #[test]
    fn test_parse_cbtop_ci_mode() {
        let args = vec![
            "apr",
            "cbtop",
            "--headless",
            "--ci",
            "--throughput",
            "100.0",
            "--brick-score",
            "90",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Cbtop {
                headless,
                ci,
                throughput,
                brick_score,
                ..
            }) => {
                assert!(headless);
                assert!(ci);
                assert_eq!(throughput, Some(100.0));
                assert_eq!(brick_score, Some(90));
            }
            _ => panic!("Expected Cbtop command"),
        }
    }

    /// #2397 finding 1: `--iterations 0` must be rejected where it is typed.
    /// Appending it to any `cbtop --ci` invocation used to turn the gate green
    /// because a brick with zero samples scores a perfect 100/A.
    #[test]
    fn test_parse_cbtop_rejects_zero_iterations() {
        let args = vec![
            "apr",
            "cbtop",
            "--headless",
            "--simulated",
            "--ci",
            "--iterations",
            "0",
        ];
        assert!(
            parse_cli(args).is_err(),
            "cbtop accepted --iterations 0 at parse time"
        );

        // One iteration is the smallest honest run and must still parse.
        let ok = vec!["apr", "cbtop", "--headless", "--iterations", "1"];
        let cli = parse_cli(ok).expect("--iterations 1 should parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Cbtop { iterations, .. }) => {
                assert_eq!(iterations, 1);
            }
            _ => panic!("Expected Cbtop command"),
        }
    }

    /// #2397 finding 4: `--json` and `--output` document "requires --headless",
    /// so the parser must enforce it. Without the constraint the flag was
    /// silently dropped and cbtop entered the interactive TUI instead.
    #[test]
    fn test_parse_cbtop_json_requires_headless() {
        assert!(
            parse_cli(vec!["apr", "cbtop", "--json"]).is_err(),
            "cbtop --json was accepted without --headless"
        );
        assert!(
            parse_cli(vec!["apr", "cbtop", "--output", "r.json"]).is_err(),
            "cbtop --output was accepted without --headless"
        );

        let cli = parse_cli(vec!["apr", "cbtop", "--headless", "--json"])
            .expect("--json --headless should parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Cbtop { headless, json, .. }) => {
                assert!(headless);
                assert!(json);
            }
            _ => panic!("Expected Cbtop command"),
        }
    }

    /// Test parsing 'apr qa' command
    #[test]
    fn test_parse_qa_command() {
        let args = vec![
            "apr",
            "qa",
            "model.gguf",
            "--assert-tps",
            "50.0",
            "--skip-ollama",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Qa {
                file,
                assert_tps,
                skip_ollama,
                ..
            }) => {
                assert_eq!(file, PathBuf::from("model.gguf"));
                assert_eq!(assert_tps, Some(50.0));
                assert!(skip_ollama);
            }
            _ => panic!("Expected Qa command"),
        }
    }

    /// Test global --verbose flag
    #[test]
    fn test_global_verbose_flag() {
        let args = vec!["apr", "--verbose", "inspect", "model.apr"];
        let cli = parse_cli(args).expect("Failed to parse");
        assert!(cli.verbose);
    }

    /// Test global --json flag
    #[test]
    fn test_global_json_flag() {
        let args = vec!["apr", "--json", "inspect", "model.apr"];
        let cli = parse_cli(args).expect("Failed to parse");
        assert!(cli.json);
    }

    /// Test parsing 'apr list' command (alias 'ls')
    #[test]
    fn test_parse_list_command() {
        let args = vec!["apr", "list"];
        let cli = parse_cli(args).expect("Failed to parse");
        assert!(matches!(*cli.command, Commands::List));
    }

    /// Test parsing 'apr ls' alias
    #[test]
    fn test_parse_ls_alias() {
        let args = vec!["apr", "ls"];
        let cli = parse_cli(args).expect("Failed to parse");
        assert!(matches!(*cli.command, Commands::List));
    }

    /// Test parsing 'apr rm' command (alias 'remove')
    #[test]
    fn test_parse_rm_command() {
        let args = vec!["apr", "rm", "model-name"];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Rm { model_ref } => {
                assert_eq!(model_ref, "model-name");
            }
            _ => panic!("Expected Rm command"),
        }
    }

    /// Test invalid command fails parsing
    #[test]
    fn test_invalid_command() {
        let args = vec!["apr", "invalid-command"];
        let result = parse_cli(args);
        assert!(result.is_err());
    }

    /// Test missing required argument fails
    #[test]
    fn test_missing_required_arg() {
        let args = vec!["apr", "inspect"]; // Missing FILE
        let result = parse_cli(args);
        assert!(result.is_err());
    }

    /// Test parsing 'apr merge' with multiple files and weights
    #[test]
    fn test_parse_merge_command() {
        let args = vec![
            "apr",
            "merge",
            "model1.apr",
            "model2.apr",
            "--strategy",
            "weighted",
            "--weights",
            "0.7,0.3",
            "-o",
            "merged.apr",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Merge {
                files,
                strategy,
                output,
                weights,
                ..
            } => {
                assert_eq!(files.len(), 2);
                assert_eq!(strategy, "weighted");
                assert_eq!(output, Some(PathBuf::from("merged.apr")));
                assert_eq!(weights, Some(vec![0.7, 0.3]));
            }
            _ => panic!("Expected Merge command"),
        }
    }

    /// Test parsing 'apr showcase' command
    #[test]
    fn test_parse_showcase_command() {
        let args = vec![
            "apr",
            "showcase",
            "--tier",
            "medium",
            "--gpu",
            "--auto-verify",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Tools(ToolCommands::Showcase {
                tier,
                gpu,
                auto_verify,
                ..
            })) => {
                assert_eq!(tier, "medium");
                assert!(gpu);
                assert!(auto_verify);
            }
            _ => panic!("Expected Showcase command"),
        }
    }

    /// Test parsing 'apr profile' with all options
    #[test]
    fn test_parse_profile_command() {
        let args = vec![
            "apr",
            "profile",
            "model.apr",
            "--granular",
            "--detect-naive",
            "--fail-on-naive",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Profile {
                file,
                granular,
                detect_naive,
                fail_on_naive,
                ..
            }) => {
                assert_eq!(file, PathBuf::from("model.apr"));
                assert!(granular);
                assert!(detect_naive);
                assert!(fail_on_naive);
            }
            _ => panic!("Expected Profile command"),
        }
    }

    /// Test parsing 'apr profile' with CI assertions (PMAT-192, GH-180)
    #[test]
    fn test_parse_profile_ci_mode() {
        let args = vec![
            "apr",
            "profile",
            "model.gguf",
            "--ci",
            "--assert-throughput",
            "100",
            "--assert-p99",
            "50",
            "--format",
            "json",
        ];
        let cli = parse_cli(args).expect("Failed to parse");
        match *cli.command {
            Commands::Extended(ExtendedCommands::Profile {
                file,
                ci,
                assert_throughput,
                assert_p99,
                format,
                ..
            }) => {
                assert_eq!(file, PathBuf::from("model.gguf"));
                assert!(ci);
                assert_eq!(assert_throughput, Some(100.0));
                assert_eq!(assert_p99, Some(50.0));
                assert_eq!(format, "json");
            }
            _ => panic!("Expected Profile command"),
        }
    }

    /// Dogfood 0.63.0 #2377 finding 10: `apr typical-p-lint --help` printed
    /// its only required flag with an empty description, so the (non-obvious,
    /// otherwise undocumented) observation schema had no route to the user.
    /// Every other command in the 16-command lint family documents its flag.
    ///
    /// A family-wide ratchet, not a one-line fix: a new `*-lint` command that
    /// forgets its flag doc turns this red.
    #[test]
    fn every_lint_command_documents_its_flags() {
        use clap::CommandFactory;
        let offenders = std::thread::Builder::new()
            .stack_size(16 * 1024 * 1024)
            .spawn(|| {
                let mut root = Cli::command();
                root.build();
                let mut bad: Vec<String> = Vec::new();
                for sub in root.get_subcommands() {
                    if !sub.get_name().ends_with("-lint") {
                        continue;
                    }
                    for arg in sub.get_arguments() {
                        if arg.get_id() == "help" || arg.get_id() == "version" {
                            continue;
                        }
                        let documented = arg
                            .get_help()
                            .is_some_and(|h| !h.to_string().trim().is_empty());
                        if !documented {
                            bad.push(format!("{} --{}", sub.get_name(), arg.get_id()));
                        }
                    }
                }
                bad
            })
            .expect("spawn")
            .join()
            .expect("join");
        assert!(
            offenders.is_empty(),
            "lint commands with an undocumented flag: {offenders:?}"
        );
    }

    /// FALSIFIER (#2394 finding 15): `apr tree --format <garbage>` must be
    /// rejected, not silently rendered as ascii.
    ///
    /// `apr tree model.apr --format bogusvalue` printed the ascii tree and
    /// exited 0 — no error, no warning. The dispatcher parsed the string with
    /// `.unwrap_or(TreeFormat::Ascii)`, so `--format josn` in a pipeline
    /// produced a tree where JSON was expected and every downstream check saw
    /// success. `TreeFormat::from_str` had always returned
    /// `Err("Unknown format: …")`; the error was thrown away one call up.
    #[test]
    fn test_tree_rejects_an_unknown_format_value() {
        let err = parse_cli(vec!["apr", "tree", "m.apr", "--format", "bogusvalue"])
            .expect_err("--format bogusvalue must be rejected");
        let msg = err.to_string();
        assert!(
            msg.contains("bogusvalue"),
            "the error must quote the value it rejected, got: {msg}"
        );
    }

    /// ...while every documented spelling still parses, so the guard rejects
    /// typos rather than rejecting everything.
    #[test]
    fn test_tree_accepts_every_documented_format() {
        for value in ["ascii", "text", "dot", "graphviz", "mermaid", "md", "json"] {
            let args: Vec<&'static str> = vec!["apr", "tree", "m.apr", "--format", value];
            assert!(
                parse_cli(args).is_ok(),
                "--format {value} is documented and must parse"
            );
        }
        // And the default (no --format at all) still works.
        assert!(parse_cli(vec!["apr", "tree", "m.apr"]).is_ok());
    }