newt-tui 0.7.5

Newt-Agent TUI surfaces (ratatui): code mode + pilot mode
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
use super::*;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

/// F5: the loop summarizer's Ollama request must carry the same
/// `options.num_ctx` the main loop sends — without it Ollama silently
/// truncates the (typically largest-of-session) summary request at the
/// model's default window.
#[tokio::test(flavor = "multi_thread")]
async fn loop_summarizer_sends_num_ctx_to_ollama() {
    use std::sync::{Arc, Mutex};
    use wiremock::{Request, Respond};

    struct Capture {
        body: Arc<Mutex<Option<serde_json::Value>>>,
    }
    impl Respond for Capture {
        fn respond(&self, req: &Request) -> ResponseTemplate {
            *self.body.lock().unwrap() = serde_json::from_slice(&req.body).ok();
            ResponseTemplate::new(200)
                .set_body_json(serde_json::json!({"message": {"content": "SUM"}}))
        }
    }

    let server = MockServer::start().await;
    let body = Arc::new(Mutex::new(None));
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(Capture { body: body.clone() })
        .mount(&server)
        .await;

    let s = make_loop_summarizer(
        server.uri(),
        "test-model".into(),
        newt_core::BackendKind::Ollama,
        None,
        None,
        SummarizerOpts {
            num_ctx: Some(4_096),
            ..Default::default()
        },
    );
    let out = s("summarize the middle".into()).await.unwrap();
    assert_eq!(out, "SUM");
    let captured = body.lock().unwrap().clone().expect("request captured");
    assert_eq!(
        captured["options"]["num_ctx"], 4_096,
        "the summarizer request must cap Ollama's window like the main loop"
    );
    assert_eq!(
        captured["keep_alive"], "5m",
        "summary request carries keep_alive (24.1, mirrors the main loop)"
    );
    assert!(
        captured.get("tools").is_none(),
        "summarizer stays tools-disabled"
    );

    // No cap configured → no options key (model default, as before).
    let s_none = make_loop_summarizer(
        server.uri(),
        "test-model".into(),
        newt_core::BackendKind::Ollama,
        None,
        None,
        SummarizerOpts::default(),
    );
    s_none("summarize".into()).await.unwrap();
    let captured = body.lock().unwrap().clone().unwrap();
    assert!(captured.get("options").is_none());
}

/// Step 24.1 (#559): for Ollama, the summarizer warms the model
/// (POST /api/generate, model + keep_alive) BEFORE the summary request, so a
/// cold reload is absorbed off the (short) summary timeout.
#[tokio::test(flavor = "multi_thread")]
async fn loop_summarizer_warms_the_model_first() {
    use std::sync::{Arc, Mutex};
    use wiremock::{Request, Respond};

    struct WarmCapture {
        body: Arc<Mutex<Option<serde_json::Value>>>,
    }
    impl Respond for WarmCapture {
        fn respond(&self, req: &Request) -> ResponseTemplate {
            *self.body.lock().unwrap() = serde_json::from_slice(&req.body).ok();
            ResponseTemplate::new(200).set_body_json(serde_json::json!({"done": true}))
        }
    }

    let server = MockServer::start().await;
    let warm = Arc::new(Mutex::new(None));
    Mock::given(method("POST"))
        .and(path("/api/generate"))
        .respond_with(WarmCapture { body: warm.clone() })
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(serde_json::json!({"message": {"content": "SUM"}})),
        )
        .mount(&server)
        .await;

    let s = make_loop_summarizer(
        server.uri(),
        "test-model".into(),
        newt_core::BackendKind::Ollama,
        None,
        None,
        SummarizerOpts::default(),
    );
    let out = s("summarize".into()).await.unwrap();
    assert_eq!(out, "SUM");
    let warm_body = warm
        .lock()
        .unwrap()
        .clone()
        .expect("warm request was made before the summary");
    assert_eq!(warm_body["model"], "test-model", "warm targets the model");
    assert_eq!(warm_body["keep_alive"], "5m", "warm carries keep_alive");
}

/// Step 24.2 (#559): a transient summarizer failure is retried (with
/// backoff) before giving up to the static-marker fallback.
#[tokio::test(flavor = "multi_thread")]
async fn loop_summarizer_retries_then_succeeds() {
    use std::sync::{Arc, Mutex};
    use wiremock::{Request, Respond};

    struct Flaky {
        calls: Arc<Mutex<u32>>,
    }
    impl Respond for Flaky {
        fn respond(&self, _req: &Request) -> ResponseTemplate {
            let mut n = self.calls.lock().unwrap();
            *n += 1;
            if *n == 1 {
                ResponseTemplate::new(500) // first attempt fails
            } else {
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": {"content": "SUM"}}))
            }
        }
    }

    let server = MockServer::start().await;
    let calls = Arc::new(Mutex::new(0));
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(Flaky {
            calls: calls.clone(),
        })
        .mount(&server)
        .await;

    let s = make_loop_summarizer(
        server.uri(),
        "test-model".into(),
        newt_core::BackendKind::Ollama,
        None,
        None,
        SummarizerOpts {
            retries: 2,
            ..Default::default()
        },
    );
    let out = s("summarize".into()).await.unwrap();
    assert_eq!(out, "SUM");
    assert_eq!(*calls.lock().unwrap(), 2, "retried once after the 500");
}

/// Step 24.2: after exhausting retries the summarizer returns an error
/// (which the compression pipeline turns into the static marker).
#[tokio::test(flavor = "multi_thread")]
async fn loop_summarizer_gives_up_after_retries() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(ResponseTemplate::new(500))
        .mount(&server)
        .await;

    let s = make_loop_summarizer(
        server.uri(),
        "test-model".into(),
        newt_core::BackendKind::Ollama,
        None,
        None,
        SummarizerOpts {
            retries: 1,
            ..Default::default()
        },
    );
    let err = s("summarize".into()).await.unwrap_err();
    assert!(
        err.to_string().contains("summarizer endpoint 500"),
        "exhausted error surfaces the last failure: {err}"
    );
}

/// Step 24.3 (#559): when the primary model's attempts all fail, the summary
/// falls back to the configured secondary model (a rung above the static
/// marker) rather than failing outright.
#[tokio::test(flavor = "multi_thread")]
async fn loop_summarizer_falls_back_to_secondary_model() {
    use wiremock::{Request, Respond};

    struct ByModel;
    impl Respond for ByModel {
        fn respond(&self, req: &Request) -> ResponseTemplate {
            let body: serde_json::Value = serde_json::from_slice(&req.body).unwrap_or_default();
            if body["model"] == "fallback-model" {
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": {"content": "FB SUM"}}))
            } else {
                ResponseTemplate::new(500) // the primary model always fails
            }
        }
    }

    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(ByModel)
        .mount(&server)
        .await;

    let s = make_loop_summarizer(
        server.uri(),
        "test-model".into(),
        newt_core::BackendKind::Ollama,
        None,
        None,
        SummarizerOpts {
            retries: 0,
            fallback_model: Some("fallback-model".into()),
            ..Default::default()
        },
    );
    let out = s("summarize".into()).await.unwrap();
    assert_eq!(out, "FB SUM", "fell back to the secondary model");
}

/// With no explicit fallback configured, the summarizer must not spend
/// another live turn auto-picking an installed Ollama model. The compression
/// pipeline turns the surfaced primary error into the static marker.
#[tokio::test(flavor = "multi_thread")]
async fn loop_summarizer_does_not_auto_pick_fallback_when_unset() {
    use wiremock::{Request, Respond};

    struct ByModel;
    impl Respond for ByModel {
        fn respond(&self, req: &Request) -> ResponseTemplate {
            let body: serde_json::Value = serde_json::from_slice(&req.body).unwrap_or_default();
            // The installed small model would succeed, but it was not
            // explicitly configured as the summarizer fallback.
            if body["model"] == "nemotron-mini:4b" {
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": {"content": "UNCONFIGURED FB"}}))
            } else {
                ResponseTemplate::new(500) // the primary model always fails
            }
        }
    }

    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(ByModel)
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/api/tags"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "models": [{"name": "session-model:27b"}, {"name": "nemotron-mini:4b"}]
        })))
        .expect(0)
        .mount(&server)
        .await;

    let s = make_loop_summarizer(
        server.uri(),
        "session-model:27b".into(),
        newt_core::BackendKind::Ollama,
        None,
        None,
        SummarizerOpts {
            retries: 0,
            fallback_model: None,
            ..Default::default()
        },
    );
    let err = s("summarize".into()).await.unwrap_err();
    assert!(
        err.to_string().contains("summarizer endpoint 500"),
        "the primary error should surface for static-marker compression: {err}"
    );
}

/// Step 24.10 (#559): a `summarizer.toml` with its own backend overrides
/// every session backend field; an explicit key is used for the pinned host.
#[test]
fn resolve_summarizer_backend_overrides_when_set() {
    let sum_cfg = newt_core::SummarizerConfig {
        endpoint: Some("http://REDACTED-HOST:11434".into()),
        model: Some("qwen2.5-1.5b".into()),
        kind: Some(newt_core::BackendKind::Embedded),
        model_path: Some("/models/qwen.gguf".into()),
        ..Default::default()
    };
    let (url, model, kind, key, model_path) = super::resolve_summarizer_backend(
        &sum_cfg,
        "http://REDACTED-HOST:11434",
        "session-model:27b",
        newt_core::BackendKind::Ollama,
        &Some("session-key".into()),
        None, // override set ⇒ embedded lookup unused; keep hermetic
    );
    assert_eq!(url, "http://REDACTED-HOST:11434");
    assert_eq!(model, "qwen2.5-1.5b");
    assert_eq!(kind, newt_core::BackendKind::Embedded);
    // #661 group C: the GGUF path threads through for an embedded summarizer.
    assert_eq!(model_path.as_deref(), Some("/models/qwen.gguf"));
    // No key configured on the pinned endpoint → the session key is NOT
    // leaked to the different host.
    assert_eq!(key, None);
}

#[tokio::test]
async fn embedded_summarizer_without_a_model_fails_cleanly() {
    // #661 group C: kind=embedded with no model_path (or a build lacking the
    // `embedded` feature) yields a failing summarizer — the compressor then
    // degrades to the deterministic static marker (group D), never a panic.
    let s = make_loop_summarizer(
        "http://unused".into(),
        "qwen2.5-1.5b".into(),
        newt_core::BackendKind::Embedded,
        None,
        None, // no model_path
        SummarizerOpts::default(),
    );
    let out = s("summarize this".to_string()).await;
    assert!(
        out.is_err(),
        "an embedded summarizer with no model must fail (→ static marker), not panic"
    );
}

/// Step 24.10: an absent/default `summarizer.toml` reuses the session
/// backend verbatim (unchanged behavior), session key included.
#[test]
fn resolve_summarizer_backend_reuses_session_when_unset() {
    let sum_cfg = newt_core::SummarizerConfig::default();
    let (url, model, kind, key, _model_path) = super::resolve_summarizer_backend(
        &sum_cfg,
        "http://REDACTED-HOST:11434",
        "session-model:27b",
        newt_core::BackendKind::Ollama,
        &Some("session-key".into()),
        None, // no on-host model ⇒ deterministically degrade to session (hermetic)
    );
    assert_eq!(url, "http://REDACTED-HOST:11434");
    assert_eq!(model, "session-model:27b");
    assert_eq!(kind, newt_core::BackendKind::Ollama);
    assert_eq!(key.as_deref(), Some("session-key"));
}

/// Step 24.10: the timeout / retries / fallback knobs come from
/// `SummarizerConfig`; `keep_alive` falls back to `[tui].keep_alive`.
#[test]
fn summarizer_opts_reads_from_summarizer_config() {
    let sum_cfg = newt_core::SummarizerConfig {
        timeout_secs: 45,
        retries: 2,
        fallback_model: Some("nemotron-mini:4b".into()),
        ..Default::default()
    };
    let cfg = newt_core::Config::default();
    let opts = super::summarizer_opts(&sum_cfg, &cfg, Some(8192), false);
    assert_eq!(opts.timeout_secs, 45);
    assert_eq!(opts.retries, 2);
    assert_eq!(opts.fallback_model.as_deref(), Some("nemotron-mini:4b"));
    assert_eq!(opts.num_ctx, Some(8192));
    // No summarizer-specific keep_alive → inherits the [tui] default ("5m").
    assert_eq!(opts.keep_alive, "5m");
}

/// Step 24.7 (#559): the live retry/fallback notice text.
#[test]
fn summarizer_progress_message_text() {
    assert_eq!(
        super::retry_progress_msg(2, 3),
        "↻ summarizer retrying (attempt 2/3)…"
    );
    assert_eq!(
        super::fallback_progress_msg("qwen:0.5b"),
        "⚠ summarizer falling back to qwen:0.5b…"
    );
}

/// `docs/decisions/tty_widget_suite.md` §5 row 3: `newt_core::tty::Notice` must
/// reproduce all three summarizer notices **byte for byte** before any of them
/// is routed through it. These use the ONE-space convention (the
/// `agentic::display` family uses two), which is why `Notice` carries an
/// explicit gap rather than assuming either.
#[test]
fn notice_reproduces_the_summarizer_progress_bytes() {
    use newt_core::tty::{Level, Notice};

    assert_eq!(
        Notice::new(Level::Warn, "", "summarizer retrying (attempt 2/3)…").line(),
        super::retry_progress_msg(2, 3),
    );
    assert_eq!(
        Notice::new(Level::Warn, "", "summarizer falling back to qwen:0.5b…").line(),
        super::fallback_progress_msg("qwen:0.5b"),
    );
    let err = anyhow::anyhow!("connection refused");
    assert_eq!(
        Notice::new(
            Level::Warn,
            "",
            "summarizer failed (connection refused); using static compression marker…"
        )
        .line(),
        super::failure_progress_msg(&err),
    );
}

/// F5 mirror: OpenAI-compatible endpoints configure context server-side
/// — `num_ctx` must NOT leak into their request body.
#[tokio::test(flavor = "multi_thread")]
async fn loop_summarizer_omits_num_ctx_on_openai() {
    use std::sync::{Arc, Mutex};
    use wiremock::{Request, Respond};

    struct Capture {
        body: Arc<Mutex<Option<serde_json::Value>>>,
    }
    impl Respond for Capture {
        fn respond(&self, req: &Request) -> ResponseTemplate {
            *self.body.lock().unwrap() = serde_json::from_slice(&req.body).ok();
            ResponseTemplate::new(200)
                .set_body_json(serde_json::json!({"choices": [{"message": {"content": "SUM"}}]}))
        }
    }

    let server = MockServer::start().await;
    let body = Arc::new(Mutex::new(None));
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .respond_with(Capture { body: body.clone() })
        .mount(&server)
        .await;

    let s = make_loop_summarizer(
        server.uri(),
        "test-model".into(),
        newt_core::BackendKind::Openai,
        Some("sk-test".into()),
        None,
        SummarizerOpts {
            num_ctx: Some(4_096),
            ..Default::default()
        },
    );
    let out = s("summarize the middle".into()).await.unwrap();
    assert_eq!(out, "SUM");
    let captured = body.lock().unwrap().clone().expect("request captured");
    assert!(
        captured.get("options").is_none(),
        "num_ctx is Ollama-only; OpenAI windows are server-side"
    );
}

/// Step 18.5 (#247): the `Summarizing` provider rebased onto the shared
/// path — one over-budget sync drives exactly ONE call to the (mocked)
/// summarizer endpoint through the same `make_loop_summarizer` wiring the
/// loop uses, the request carries the shared pipeline's template, and the
/// resulting history entry carries the pipeline's compaction markers.
#[tokio::test(flavor = "multi_thread")]
async fn summarizing_provider_delegates_through_loop_summarizer() {
    use std::sync::{Arc, Mutex};
    use wiremock::{Request, Respond};

    struct Capture {
        bodies: Arc<Mutex<Vec<serde_json::Value>>>,
    }
    impl Respond for Capture {
        fn respond(&self, req: &Request) -> ResponseTemplate {
            self.bodies
                .lock()
                .unwrap()
                .push(serde_json::from_slice(&req.body).unwrap());
            ResponseTemplate::new(200)
                .set_body_json(serde_json::json!({"message": {"content": "WIRE SUMMARY"}}))
        }
    }

    let server = MockServer::start().await;
    let bodies = Arc::new(Mutex::new(Vec::new()));
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(Capture {
            bodies: bodies.clone(),
        })
        .mount(&server)
        .await;

    let mut memory = newt_core::MemoryManager::new();
    // Leave enough room for the irreducible active-prompt metadata + exact
    // user pair. A smaller authoritative budget must refuse compression
    // rather than summarize either half of that pair.
    memory.add_provider(
        newt_core::Summarizing::new(512).with_summarizer(make_loop_summarizer(
            server.uri(),
            "test-model".into(),
            newt_core::BackendKind::Ollama,
            None,
            None,
            SummarizerOpts {
                num_ctx: Some(512),
                ..Default::default()
            },
        )),
    );
    let metrics = |input_tokens: u32| newt_core::TurnMetrics {
        usage: Some(newt_core::TokenUsage {
            input_tokens,
            output_tokens: 9,
        }),
        ..Default::default()
    };
    let big = "x".repeat(200);
    for i in 0..5u32 {
        memory
            .sync_all(&format!("task {i}"), &big, &metrics(10 + i))
            .await;
    }
    assert!(bodies.lock().unwrap().is_empty(), "under budget — no calls");
    memory.sync_all("final task", &big, &metrics(600)).await;

    let bodies = bodies.lock().unwrap();
    assert_eq!(bodies.len(), 1, "exactly one summarizer call");
    let prompt = bodies[0]["messages"][0]["content"].as_str().unwrap();
    assert!(
        prompt.contains("## Conversation middle to summarise"),
        "must be the shared pipeline's request template"
    );
    drop(bodies);
    // The minted record carries the shared markers.
    let record = memory
        .take_compaction_record()
        .expect("compression minted a record");
    assert!(record.starts_with(newt_core::agentic::SUMMARY_PREFIX));
    assert!(record.contains("WIRE SUMMARY"));
    assert!(record.contains(newt_core::agentic::SUMMARY_END_MARKER));
}