apr-cli 0.4.13

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

    // =========================================================================
    // Additional ChatConfig tests
    // =========================================================================

    #[test]
    fn test_chat_config_extreme_values() {
        let config = ChatConfig {
            temperature: 0.0,
            top_p: 0.0,
            max_tokens: 1,
            system: None,
            inspect: false,
            force_cpu: false,
            trace: false,
            trace_output: None,
        };
        assert_eq!(config.temperature, 0.0);
        assert_eq!(config.top_p, 0.0);
        assert_eq!(config.max_tokens, 1);
    }

    #[test]
    fn test_chat_config_high_temp() {
        let config = ChatConfig {
            temperature: 2.0,
            top_p: 1.0,
            max_tokens: 4096,
            system: Some("Creative mode".to_string()),
            inspect: true,
            force_cpu: true,
            trace: true,
            trace_output: Some(PathBuf::from("/tmp/creative_trace.json")),
        };
        assert_eq!(config.temperature, 2.0);
        assert_eq!(config.max_tokens, 4096);
    }

    #[test]
    fn test_chat_config_inspect_mode() {
        let config = ChatConfig {
            inspect: true,
            ..Default::default()
        };
        assert!(config.inspect);
    }

    // =========================================================================
    // Additional clean_chat_response edge cases
    // =========================================================================

    #[test]
    fn test_clean_chat_response_deeply_nested_markers() {
        let raw = "<|im_start|><|im_start|>assistant\nTest<|im_end|><|im_end|>";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Test");
    }

    #[test]
    fn test_clean_chat_response_escaped_newlines() {
        let raw = "Line1\\nLine2";
        let cleaned = clean_chat_response(raw);
        // Should preserve escaped newlines
        assert!(cleaned.contains("\\n") || cleaned.contains('\n'));
    }

    #[test]
    fn test_clean_chat_response_tabs() {
        let raw = "Column1\tColumn2";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Column1\tColumn2");
    }

    #[test]
    fn test_clean_chat_response_markdown_headers() {
        let raw = "# Header 1\n## Header 2\n### Header 3";
        let cleaned = clean_chat_response(raw);
        assert!(cleaned.contains("# Header 1"));
        assert!(cleaned.contains("## Header 2"));
    }

    #[test]
    fn test_clean_chat_response_math_expression() {
        let raw = "E = mc²";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "E = mc²");
    }

    #[test]
    fn test_clean_chat_response_url() {
        let raw = "Visit https://example.com for more info";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Visit https://example.com for more info");
    }

    // =========================================================================
    // ModelFormat additional tests
    // =========================================================================

    #[test]
    fn test_model_format_all_variants() {
        // Ensure all variants exist
        let _apr = ModelFormat::Apr;
        let _gguf = ModelFormat::Gguf;
        let _st = ModelFormat::SafeTensors;
        let _demo = ModelFormat::Demo;
    }

    #[test]
    fn test_detect_format_double_extension() {
        let path = Path::new("/models/model.tar.gz");
        // Should detect based on last extension
        assert_eq!(detect_format(path), ModelFormat::Demo);
    }

    #[test]
    fn test_detect_format_hidden_file() {
        let path = Path::new("/models/.hidden.gguf");
        assert_eq!(detect_format(path), ModelFormat::Gguf);
    }

    #[test]
    fn test_detect_format_dot_in_name() {
        let path = Path::new("/models/model.v2.0.safetensors");
        assert_eq!(detect_format(path), ModelFormat::SafeTensors);
    }

    // =========================================================================
    // print_welcome_banner smoke tests (all format branches + config combos)
    // =========================================================================

    #[test]
    fn test_print_welcome_banner_apr_format() {
        let path = Path::new("/models/qwen2-instruct.apr");
        let config = ChatConfig::default();
        // Should not panic; exercises ModelFormat::Apr branch
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_gguf_format() {
        let path = Path::new("/models/qwen2-instruct.gguf");
        let config = ChatConfig::default();
        // Should not panic; exercises ModelFormat::Gguf branch
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_safetensors_format() {
        let path = Path::new("/models/model.safetensors");
        let config = ChatConfig::default();
        // Should not panic; exercises ModelFormat::SafeTensors branch
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_demo_format() {
        let path = Path::new("/models/model.bin");
        let config = ChatConfig::default();
        // Should not panic; exercises ModelFormat::Demo branch
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_with_system_prompt() {
        let path = Path::new("/models/test.apr");
        let config = ChatConfig {
            system: Some("You are a helpful assistant.".to_string()),
            ..Default::default()
        };
        // Exercises the system prompt display branch
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_with_inspect_mode() {
        let path = Path::new("/models/test.gguf");
        let config = ChatConfig {
            inspect: true,
            ..Default::default()
        };
        // Exercises the inspect mode display branch
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_with_system_and_inspect() {
        let path = Path::new("/models/model.safetensors");
        let config = ChatConfig {
            system: Some("Be concise".to_string()),
            inspect: true,
            ..Default::default()
        };
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_template_chatml() {
        // "qwen" in filename triggers ChatML template detection
        let path = Path::new("/models/qwen2-0.5b.apr");
        let config = ChatConfig::default();
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_template_llama2() {
        // "llama" in filename triggers LLaMA2 template detection
        let path = Path::new("/models/llama-2-7b.gguf");
        let config = ChatConfig::default();
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_template_mistral() {
        // "mistral" in filename triggers Mistral template detection
        let path = Path::new("/models/mistral-7b.gguf");
        let config = ChatConfig::default();
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_template_phi() {
        // "phi-" in filename triggers Phi template detection
        let path = Path::new("/models/phi-3.safetensors");
        let config = ChatConfig::default();
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_template_alpaca() {
        // "alpaca" in filename triggers Alpaca template detection
        let path = Path::new("/models/alpaca-7b.gguf");
        let config = ChatConfig::default();
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_template_raw_fallback() {
        // Unknown model name falls back to Raw template
        let path = Path::new("/models/unknown-model.gguf");
        let config = ChatConfig::default();
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_no_extension() {
        // No extension -> Demo format
        let path = Path::new("/models/modelfile");
        let config = ChatConfig::default();
        print_welcome_banner(path, &config);
    }

    #[test]
    fn test_print_welcome_banner_no_stem() {
        // File with no stem (just extension)
        let path = Path::new("/models/.apr");
        let config = ChatConfig::default();
        print_welcome_banner(path, &config);
    }

    // =========================================================================
    // clean_chat_response: new-turn detection branches
    // =========================================================================

    #[test]
    fn test_clean_chat_response_what_question_cutoff() {
        let raw = "42\nWhat is the meaning of life?";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "42");
    }

    #[test]
    fn test_clean_chat_response_how_question_cutoff() {
        let raw = "Done.\nHow do you feel about that?";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Done.");
    }

    #[test]
    fn test_clean_chat_response_why_question_cutoff() {
        let raw = "Because reasons.\nWhy did the chicken cross the road?";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Because reasons.");
    }

    #[test]
    fn test_clean_chat_response_can_question_cutoff() {
        let raw = "Yes.\nCan you elaborate on that?";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Yes.");
    }

    #[test]
    fn test_clean_chat_response_im_start_in_rest_not_cutoff_after_stripping() {
        // Note: <|im_start|> markers are stripped BEFORE new-turn detection,
        // so the rest becomes "user\nNext question?" which doesn't match
        // any cutoff pattern (Suggest/What/How/Why/Can/Human:/<|im_start|>)
        let raw = "Answer here.\n<|im_start|>user\nNext question?";
        let cleaned = clean_chat_response(raw);
        // After marker stripping: "Answer here.\nuser\nNext question?"
        // "user" doesn't match cutoff patterns, so full text is preserved
        assert_eq!(cleaned, "Answer here.\nuser\nNext question?");
    }

    #[test]
    fn test_clean_chat_response_raw_im_start_text_in_rest() {
        // If the raw text literally has "<|im_start|>" that WASN'T stripped
        // (e.g., doubled markers), test the cutoff pattern
        // Actually, all <|im_start|> are stripped first, so this tests
        // that the check works on the already-cleaned text
        let raw = "Answer.\nWhat is next?";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Answer.");
    }

    #[test]
    fn test_clean_chat_response_suggest_cutoff() {
        // Already covered above, but this tests with different first-line content
        let raw = "Rust is great!\nSuggest some alternatives";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Rust is great!");
    }

    #[test]
    fn test_clean_chat_response_no_cutoff_normal_continuation() {
        // Lines that start with lowercase should NOT be cut off
        let raw = "First line\nsecond line continues";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "First line\nsecond line continues");
    }

    #[test]
    fn test_clean_chat_response_no_cutoff_numbered_continuation() {
        let raw = "Steps:\n1. Do this\n2. Do that";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Steps:\n1. Do this\n2. Do that");
    }

    // =========================================================================
    // clean_chat_response: BPE artifact edge cases
    // =========================================================================

    #[test]
    fn test_clean_chat_response_multiple_bpe_spaces() {
        // Multiple Ġ should collapse to single space after cleaning
        let raw = "HelloĠĠworld";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello world");
    }

    #[test]
    fn test_clean_chat_response_bpe_newline_u010a() {
        // U+010A character (Ċ) is newline in some BPE tokenizers
        let raw = "LineAĊLineB";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "LineA\nLineB");
    }

    #[test]
    fn test_clean_chat_response_both_bpe_artifacts() {
        // Mix of Ġ and Ċ
        let raw = "HelloĠworldĊnewĠline";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello world\nnew line");
    }

    #[test]
    fn test_clean_chat_response_literal_g_with_dot() {
        // Literal "Ġ" string (not the Unicode character) should also be replaced
        let raw = "Hello\u{0120}there";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hello there");
    }

    // =========================================================================
    // clean_chat_response: punctuation normalization edge cases
    // =========================================================================

    #[test]
    fn test_clean_chat_response_exactly_three_exclamation() {
        let raw = "Wow!!!";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Wow!!!");
    }

    #[test]
    fn test_clean_chat_response_exactly_two_exclamation() {
        let raw = "Wow!!";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Wow!!");
    }

    #[test]
    fn test_clean_chat_response_single_exclamation() {
        let raw = "Wow!";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Wow!");
    }

    #[test]
    fn test_clean_chat_response_mixed_punctuation_not_collapsed() {
        // Mixed punctuation (different chars) should NOT be collapsed
        let raw = "Really!?!?";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Really!?!?");
    }

    #[test]
    fn test_clean_chat_response_dots_exactly_three() {
        let raw = "Hmm...";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Hmm...");
    }

    #[test]
    fn test_clean_chat_response_dots_many() {
        let raw = "Wait..........";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Wait...");
    }

    #[test]
    fn test_clean_chat_response_questions_exactly_three() {
        let raw = "What???";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "What???");
    }

    #[test]
    fn test_clean_chat_response_questions_many() {
        let raw = "What????????";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "What???");
    }

    // =========================================================================
    // clean_chat_response: ChatML marker variations
    // =========================================================================

    #[test]
    fn test_clean_chat_response_im_start_alone() {
        let raw = "<|im_start|>Some text";
        let cleaned = clean_chat_response(raw);
        assert_eq!(cleaned, "Some text");
    }