newt-agent 0.7.1

Newt-Agent — small, fast, local-first agentic coder (vi to Hermes's emacs)
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
725
726
727
728
729
730
//! Integration tests for `newt tunings` (show / export / import / reset).
//!
//! Every test redirects `HOME` to a private tempdir so the suite never reads
//! or writes the developer's real `~/.newt`. `Config::user_config_path`
//! resolves through `$HOME`, so the per-process env override is sufficient —
//! and because each test spawns its own `newt` process, there is no shared
//! mutable env state between parallel tests.

use assert_cmd::Command;
use predicates::prelude::*;

/// A fresh fake `$HOME` with an empty `.newt/` directory.
fn fake_home() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    std::fs::create_dir_all(dir.path().join(".newt")).unwrap();
    dir
}

/// Write `~/.newt/model-capabilities.json` inside the fake home.
fn write_caps(home: &tempfile::TempDir, caps: &serde_json::Value) {
    std::fs::write(
        home.path().join(".newt").join("model-capabilities.json"),
        serde_json::to_string_pretty(caps).unwrap(),
    )
    .unwrap();
}

/// Read `~/.newt/model-capabilities.json` back as JSON.
fn read_caps(home: &tempfile::TempDir) -> serde_json::Value {
    let data =
        std::fs::read_to_string(home.path().join(".newt").join("model-capabilities.json")).unwrap();
    serde_json::from_str(&data).unwrap()
}

/// Write `~/.newt/community-tunings.toml` inside the fake home.
fn write_community(home: &tempfile::TempDir, toml_text: &str) {
    std::fs::write(
        home.path().join(".newt").join("community-tunings.toml"),
        toml_text,
    )
    .unwrap();
}

/// `newt` with `HOME` pointed at the fake home.
fn newt(home: &tempfile::TempDir) -> Command {
    let mut cmd = Command::cargo_bin("newt").unwrap();
    cmd.env("HOME", home.path());
    cmd
}

/// A capabilities document with two tuned models.
fn two_model_caps() -> serde_json::Value {
    serde_json::json!({
        "alpha:7b": {
            "conformance": "full",
            "tested_date": "2026-06-01",
            "context_window": 32768,
            "safe_context": 24576,
            "tune_confidence": "high",
            "consecutive_ok": 5
        },
        "beta:13b": {
            "conformance": "partial",
            "context_window": 8192,
            "safe_context": 6144,
            "tune_confidence": "low",
            "consecutive_ok": 1
        }
    })
}

// ---------------------------------------------------------------------------
// CLI surface
// ---------------------------------------------------------------------------

#[test]
fn tunings_requires_subcommand() {
    let home = fake_home();
    newt(&home).arg("tunings").assert().failure();
}

// ---------------------------------------------------------------------------
// show
// ---------------------------------------------------------------------------

#[test]
fn show_without_data_prints_guidance() {
    let home = fake_home();
    newt(&home)
        .args(["tunings", "show"])
        .assert()
        .success()
        .stdout(predicate::str::contains("No tuning data found."))
        .stdout(predicate::str::contains("newt tunings import"));
}

#[test]
fn show_lists_empirical_models_with_k_formatting() {
    let home = fake_home();
    write_caps(&home, &two_model_caps());

    newt(&home)
        .args(["tunings", "show"])
        .assert()
        .success()
        .stdout(predicate::str::contains("Model"))
        .stdout(predicate::str::contains("alpha:7b"))
        .stdout(predicate::str::contains("32k"))
        .stdout(predicate::str::contains("24k"))
        .stdout(predicate::str::contains("high"))
        .stdout(predicate::str::contains("beta:13b"))
        .stdout(predicate::str::contains("8k"))
        .stdout(predicate::str::contains("empirical"));
}

#[test]
fn show_filters_to_requested_model() {
    let home = fake_home();
    write_caps(&home, &two_model_caps());

    newt(&home)
        .args(["tunings", "show", "alpha:7b"])
        .assert()
        .success()
        .stdout(predicate::str::contains("alpha:7b"))
        .stdout(predicate::str::contains("beta:13b").not());
}

#[test]
fn show_unknown_model_fails_with_message() {
    let home = fake_home();
    write_caps(&home, &two_model_caps());

    newt(&home)
        .args(["tunings", "show", "ghost:1b"])
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "no tuning data for model 'ghost:1b'",
        ));
}

#[test]
fn show_merges_community_profiles_as_community_source() {
    let home = fake_home();
    write_community(
        &home,
        r#"
[format]
version = "1"

[[profiles]]
model = "comm:7b"
context_window = 8192
safe_context = 6144
confidence = "medium"
"#,
    );

    newt(&home)
        .args(["tunings", "show"])
        .assert()
        .success()
        .stdout(predicate::str::contains("comm:7b"))
        .stdout(predicate::str::contains("8k"))
        .stdout(predicate::str::contains("medium"))
        .stdout(predicate::str::contains("community"))
        .stdout(predicate::str::contains("Community file:"));
}

#[test]
fn show_small_context_window_is_not_abbreviated() {
    let home = fake_home();
    write_caps(
        &home,
        &serde_json::json!({
            "tiny:1b": {
                "context_window": 512,
                "safe_context": 384,
                "tune_confidence": "low",
                "consecutive_ok": 1
            }
        }),
    );

    newt(&home)
        .args(["tunings", "show"])
        .assert()
        .success()
        .stdout(predicate::str::contains("512"))
        .stdout(predicate::str::contains("384"));
}

/// Phase 20 (docs/design/model-self-tuning.md): the learned calibration
/// ratio and the thinking-only quirk render as detail lines under the row —
/// and stay absent for models that never learned them.
#[test]
fn show_renders_calibration_ratio_and_thinking_quirk() {
    let home = fake_home();
    write_caps(
        &home,
        &serde_json::json!({
            "nemotron3:33b": {
                "context_window": 32768,
                "safe_context": 26214,
                "tune_confidence": "medium",
                "estimate_ratio": 1.29,
                "emits_thinking": true
            },
            "plain:7b": {
                "context_window": 8192,
                "safe_context": 6553
            }
        }),
    );

    newt(&home)
        .args(["tunings", "show"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "estimate calibration: x1.29 (chars/4 -> real)",
        ))
        .stdout(predicate::str::contains(
            "quirk: emits thinking-only responses",
        ))
        // Exactly one model carries each detail line.
        .stdout(predicate::str::contains("estimate calibration").count(1))
        .stdout(predicate::str::contains("quirk:").count(1));
}

/// Step 20.2 (docs/design/model-self-tuning.md §4.6): `newt tunings show`
/// flags stale and not-empirically-probed entries and points them at
/// `/probe window`.
///
/// - `stale:7b` has an ancient `tune_date` (well over 30 days) → the
///   "tuning N days old" hint.
/// - `undated:7b` has empirical tuning but no `tune_date` → also stale
///   (None ⇒ re-probe) → the "never dated" hint.
/// - `unprobed:7b` is freshly dated but below High confidence → the
///   "window not empirically probed" hint.
/// - `solid:7b` is freshly dated AND High confidence → no hint at all.
#[test]
fn show_flags_stale_and_unprobed_entries_with_probe_window_hint() {
    let home = fake_home();
    write_caps(
        &home,
        &serde_json::json!({
            "stale:7b": {
                "context_window": 8192,
                "safe_context": 6144,
                "tune_confidence": "high",
                "tune_date": "2020-01-01"
            },
            "undated:7b": {
                "context_window": 8192,
                "safe_context": 6144,
                "tune_confidence": "high"
            },
            "unprobed:7b": {
                "context_window": 8192,
                "safe_context": 6144,
                "tune_confidence": "low",
                "tune_date": "2099-01-01"
            },
            "solid:7b": {
                "context_window": 8192,
                "safe_context": 6144,
                "tune_confidence": "high",
                "tune_date": "2099-01-01"
            }
        }),
    );

    newt(&home)
        .args(["tunings", "show"])
        .assert()
        .success()
        // Stale (old date) and undated entries both point at /probe window.
        .stdout(predicate::str::contains("days old — run /probe window"))
        .stdout(predicate::str::contains("never dated — run /probe window"))
        // Below-High confidence on a fresh date → the unprobed hint.
        .stdout(predicate::str::contains(
            "window not empirically probed — run /probe window",
        ));
}

/// A freshly-dated High-confidence entry shows no staleness hint at all.
#[test]
fn show_omits_hint_for_fresh_high_confidence_entry() {
    let home = fake_home();
    write_caps(
        &home,
        &serde_json::json!({
            "solid:7b": {
                "context_window": 8192,
                "safe_context": 6144,
                "tune_confidence": "high",
                "tune_date": "2099-01-01"
            }
        }),
    );

    newt(&home)
        .args(["tunings", "show"])
        .assert()
        .success()
        .stdout(predicate::str::contains("/probe window").not());
}

/// Phase 20: reset clears the new learned fields too — the Refused bail
/// sends users here when a learned budget (or calibration) is poisoned.
#[test]
fn reset_clears_estimate_ratio_and_emits_thinking() {
    let home = fake_home();
    write_caps(
        &home,
        &serde_json::json!({
            "m:7b": {
                "conformance": "full",
                "safe_context": 6553,
                "estimate_ratio": 2.9,
                "emits_thinking": true
            }
        }),
    );

    newt(&home)
        .args(["tunings", "reset", "m:7b"])
        .assert()
        .success();

    let caps = read_caps(&home);
    assert!(caps["m:7b"].get("estimate_ratio").is_none());
    assert!(caps["m:7b"].get("emits_thinking").is_none());
    assert_eq!(caps["m:7b"]["conformance"], "full");
}

/// Phase 20: export carries the learned calibration ratio (additive v1 key).
#[test]
fn export_includes_estimate_ratio_when_learned() {
    let home = fake_home();
    write_caps(
        &home,
        &serde_json::json!({
            "calibrated:33b": {
                "context_window": 32768,
                "safe_context": 26214,
                "tune_confidence": "medium",
                "consecutive_ok": 2,
                "estimate_ratio": 1.25
            }
        }),
    );

    newt(&home)
        .args(["tunings", "export"])
        .assert()
        .success()
        .stdout(predicate::str::contains("model = \"calibrated:33b\""))
        .stdout(predicate::str::contains("estimate_ratio = 1.25"));
}

#[test]
fn show_prints_dash_for_missing_values() {
    let home = fake_home();
    write_community(
        &home,
        r#"
[format]
version = "1"

[[profiles]]
model = "bare:7b"
confidence = "none"
"#,
    );

    newt(&home)
        .args(["tunings", "show"])
        .assert()
        .success()
        .stdout(predicate::str::contains("bare:7b"))
        .stdout(predicate::str::contains(""));
}

// ---------------------------------------------------------------------------
// export
// ---------------------------------------------------------------------------

#[test]
fn export_prints_community_toml_to_stdout() {
    let home = fake_home();
    write_caps(&home, &two_model_caps());

    newt(&home)
        .args(["tunings", "export"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "# newt community model tuning profiles · format v1",
        ))
        .stdout(predicate::str::contains("[[profiles]]"))
        .stdout(predicate::str::contains("model = \"alpha:7b\""))
        .stdout(predicate::str::contains("context_window = 32768"))
        .stdout(predicate::str::contains("safe_context = 24576"))
        .stdout(predicate::str::contains("tune_source = \"empirical\""))
        .stdout(predicate::str::contains("confidence = \"high\""))
        .stdout(predicate::str::contains("data_points = 5"));
}

#[test]
fn export_writes_file_with_output_flag() {
    let home = fake_home();
    write_caps(&home, &two_model_caps());
    let out_path = home.path().join("shared.toml");

    newt(&home)
        .args(["tunings", "export", "--output"])
        .arg(&out_path)
        .assert()
        .success()
        .stdout(predicate::str::contains("Tunings exported to"));

    let exported = std::fs::read_to_string(&out_path).unwrap();
    assert!(exported.contains("[[profiles]]"));
    assert!(exported.contains("model = \"alpha:7b\""));
    assert!(exported.contains("model = \"beta:13b\""));
    // The export must round-trip as valid community TOML.
    let parsed: toml::Value = toml::from_str(&exported).unwrap();
    assert_eq!(
        parsed["profiles"].as_array().map(|a| a.len()),
        Some(2),
        "expected exactly two exported profiles"
    );
}

#[test]
fn export_without_data_prints_message() {
    let home = fake_home();
    newt(&home)
        .args(["tunings", "export"])
        .assert()
        .success()
        .stdout(predicate::str::contains("No tuning data to export."));
}

#[test]
fn export_skips_models_without_tuning_fields() {
    let home = fake_home();
    write_caps(
        &home,
        &serde_json::json!({
            "tuned:7b": {
                "context_window": 4096,
                "tune_confidence": "medium",
                "consecutive_ok": 3
            },
            "untuned:7b": {
                "conformance": "full",
                "tested_date": "2026-06-01"
            }
        }),
    );

    newt(&home)
        .args(["tunings", "export"])
        .assert()
        .success()
        .stdout(predicate::str::contains("model = \"tuned:7b\""))
        .stdout(predicate::str::contains("untuned:7b").not());
}

#[test]
fn export_includes_community_profiles_not_in_caps() {
    let home = fake_home();
    write_caps(
        &home,
        &serde_json::json!({
            "tuned:7b": {
                "context_window": 4096,
                "tune_confidence": "medium",
                "consecutive_ok": 3
            }
        }),
    );
    write_community(
        &home,
        r#"
[format]
version = "1"

[[profiles]]
model = "community-only:7b"
safe_context = 2048
tune_source = "community"
confidence = "low"
"#,
    );

    newt(&home)
        .args(["tunings", "export"])
        .assert()
        .success()
        .stdout(predicate::str::contains("model = \"tuned:7b\""))
        .stdout(predicate::str::contains("model = \"community-only:7b\""));
}

// ---------------------------------------------------------------------------
// import
// ---------------------------------------------------------------------------

#[test]
fn import_creates_community_file() {
    let home = fake_home();
    let incoming = home.path().join("incoming.toml");
    std::fs::write(
        &incoming,
        r#"
[format]
version = "1"

[[profiles]]
model = "shared:7b"
context_window = 16384
safe_context = 12288
confidence = "high"
"#,
    )
    .unwrap();

    newt(&home)
        .args(["tunings", "import"])
        .arg(&incoming)
        .assert()
        .success()
        .stdout(predicate::str::contains("Imported 1 profile(s)"))
        .stdout(predicate::str::contains("Saved to"));

    let saved =
        std::fs::read_to_string(home.path().join(".newt").join("community-tunings.toml")).unwrap();
    assert!(saved.contains("model = \"shared:7b\""));
    assert!(saved.contains("safe_context = 12288"));
}

#[test]
fn import_missing_file_fails() {
    let home = fake_home();
    newt(&home)
        .args(["tunings", "import", "/nonexistent/tunings.toml"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("cannot read"));
}

#[test]
fn import_invalid_toml_fails_with_parse_error() {
    let home = fake_home();
    let bad = home.path().join("bad.toml");
    std::fs::write(&bad, "this is { not toml").unwrap();

    newt(&home)
        .args(["tunings", "import"])
        .arg(&bad)
        .assert()
        .failure()
        .stderr(predicate::str::contains("TOML parse error"));
}

#[test]
fn import_higher_confidence_replaces_existing_profile() {
    let home = fake_home();
    write_community(
        &home,
        r#"
[format]
version = "1"

[[profiles]]
model = "m:7b"
safe_context = 4096
confidence = "low"
"#,
    );
    let incoming = home.path().join("incoming.toml");
    std::fs::write(
        &incoming,
        r#"
[[profiles]]
model = "m:7b"
safe_context = 8192
confidence = "high"
"#,
    )
    .unwrap();

    newt(&home)
        .args(["tunings", "import"])
        .arg(&incoming)
        .assert()
        .success();

    let saved =
        std::fs::read_to_string(home.path().join(".newt").join("community-tunings.toml")).unwrap();
    assert!(saved.contains("safe_context = 8192"));
    assert!(!saved.contains("safe_context = 4096"));
}

#[test]
fn import_lower_confidence_keeps_existing_profile() {
    let home = fake_home();
    write_community(
        &home,
        r#"
[format]
version = "1"

[[profiles]]
model = "m:7b"
safe_context = 8192
confidence = "high"
"#,
    );
    let incoming = home.path().join("incoming.toml");
    std::fs::write(
        &incoming,
        r#"
[[profiles]]
model = "m:7b"
safe_context = 2048
confidence = "low"
"#,
    )
    .unwrap();

    newt(&home)
        .args(["tunings", "import"])
        .arg(&incoming)
        .assert()
        .success();

    let saved =
        std::fs::read_to_string(home.path().join(".newt").join("community-tunings.toml")).unwrap();
    assert!(saved.contains("safe_context = 8192"));
    assert!(!saved.contains("safe_context = 2048"));
}

// ---------------------------------------------------------------------------
// reset
// ---------------------------------------------------------------------------

#[test]
fn reset_single_model_clears_only_tuning_keys() {
    let home = fake_home();
    write_caps(&home, &two_model_caps());

    newt(&home)
        .args(["tunings", "reset", "alpha:7b"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Reset tuning data for 'alpha:7b'.",
        ));

    let caps = read_caps(&home);
    let alpha = &caps["alpha:7b"];
    // Tuning keys are gone…
    assert!(alpha.get("context_window").is_none());
    assert!(alpha.get("safe_context").is_none());
    assert!(alpha.get("tune_confidence").is_none());
    assert!(alpha.get("consecutive_ok").is_none());
    // …but the base capability fields survive.
    assert_eq!(alpha["conformance"], "full");
    assert_eq!(alpha["tested_date"], "2026-06-01");
    // And the other model is untouched.
    assert_eq!(caps["beta:13b"]["context_window"], 8192);
}

#[test]
fn reset_unknown_model_fails() {
    let home = fake_home();
    write_caps(&home, &two_model_caps());

    newt(&home)
        .args(["tunings", "reset", "ghost:1b"])
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "model 'ghost:1b' not found in capabilities cache",
        ));
}

#[test]
fn reset_all_models_clears_every_entry() {
    let home = fake_home();
    write_caps(&home, &two_model_caps());

    newt(&home)
        .args(["tunings", "reset"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Reset tuning data for 2 model(s).",
        ));

    let caps = read_caps(&home);
    for model in ["alpha:7b", "beta:13b"] {
        assert!(caps[model].get("context_window").is_none(), "{model}");
        assert!(caps[model].get("tune_confidence").is_none(), "{model}");
    }
    // Base fields survive a full reset too.
    assert_eq!(caps["alpha:7b"]["conformance"], "full");
}

#[test]
fn reset_without_caps_file_succeeds_with_zero_models() {
    let home = fake_home();

    newt(&home)
        .args(["tunings", "reset"])
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Reset tuning data for 0 model(s).",
        ));

    // The (empty) capabilities file is written back.
    let caps = read_caps(&home);
    assert!(caps.as_object().unwrap().is_empty());
}