newt-core 0.8.0

Newt-Agent core types, errors, and the NeMoCode-style tier router
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
use super::*;

/// #2372: the probe's plain answer is ACCEPTED and returned. Any second
/// (`stream: true`) request would stream DIFFERENT text, so a display reissue
/// shows up as an extra request and as `Hello world` in the reply.
struct StreamHappyResponder;
impl Respond for StreamHappyResponder {
    fn respond(&self, req: &Request) -> ResponseTemplate {
        if is_stream(req) {
            ndjson(&[
                serde_json::json!({"message": {"content": "Hello "}, "done": false}),
                serde_json::json!({
                    "message": {"content": "world"}, "done": true,
                    "prompt_eval_count": 7, "eval_count": 3
                }),
            ])
        } else {
            ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "message": {"content": "probe answer"},
                "prompt_eval_count": 5, "eval_count": 2,
            }))
        }
    }
}

#[tokio::test]
async fn ollama_returns_the_accepted_probe_answer_without_a_reissue() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(StreamHappyResponder)
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let (reply, streamed, usage, hallu) =
        chat_complete(ctx(&server.uri(), &messages, &caveats), &mut NoMcp)
            .await
            .expect("chat_complete should succeed");

    assert_eq!(
        crate::agentic::model_explanation(&reply),
        "probe answer",
        "the gated answer, never a second generation"
    );
    assert!(!streamed, "nothing was streamed to a display");
    assert_eq!(
        server.received_requests().await.unwrap().len(),
        1,
        "one generation request"
    );
    let u = usage.expect("the probe's usage");
    assert_eq!((u.input_tokens, u.output_tokens), (5, 2));
    assert_eq!(hallu, 0);
}

/// One Ollama answer with `content` (and optionally native `thinking`),
/// returned through a real turn. Returns the reply and every reasoning body
/// the fold retained. Callers hold `GlobalSettingsGuard` with
/// `NEWT_THINKING=fold`, so a missing fold cannot pass by being switched off.
async fn ollama_answer(
    content: &str,
    thinking: Option<&str>,
    leading_reasoning: bool,
    color: bool,
) -> (String, Vec<String>) {
    let server = MockServer::start().await;
    let mut message = serde_json::json!({"content": content});
    if let Some(thinking) = thinking {
        message["thinking"] = serde_json::json!(thinking);
    }
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "message": message, "prompt_eval_count": 5, "eval_count": 2,
        })))
        .mount(&server)
        .await;
    let messages = msgs();
    let caveats = Caveats::top();
    let uri = server.uri();
    let recorder = Arc::new(FoldRecorder::default());
    let mut c = ctx(&uri, &messages, &caveats);
    c.emits_leading_reasoning = leading_reasoning;
    c.color = color;
    c.completed_spill_renderer = Some(recorder.clone() as Arc<dyn CompletedSpillRenderer>);
    let (reply, _, _, _) = chat_complete(c, &mut NoMcp).await.expect("dispatch");
    assert_eq!(server.received_requests().await.unwrap().len(), 1);
    let folded = recorder.0.lock().unwrap().clone();
    (crate::agentic::model_explanation(&reply).to_owned(), folded)
}

fn thinking_folded() -> crate::test_guard::GlobalSettingsGuard {
    let guard = crate::test_guard::GlobalSettingsGuard::acquire();
    crate::process_env::set_var("NEWT_THINKING", "fold");
    guard
}

/// #2372: the accepted Ollama answer follows the display stream's reasoning
/// policy, and what it strips is what it folds. An undeclared backend keeps a
/// lone `</think>` as answer text; only a backend declaring the leading shape
/// (#384, #528) has it stripped; an inline `<think>` block never reaches the
/// answer either way.
#[tokio::test]
async fn the_ollama_answer_follows_the_declared_reasoning_policy() {
    let _settings = thinking_folded();
    let undeclared = "End the block with `</think>` and then answer.";
    assert_eq!(
        ollama_answer(undeclared, None, false, true).await,
        (undeclared.to_string(), vec![])
    );
    let (reply, folded) = ollama_answer("plan</think>Done.", None, true, true).await;
    assert_eq!(reply, "Done.");
    assert!(
        folded.iter().any(|body| body.contains("plan")),
        "{folded:?}"
    );
    let (reply, folded) = ollama_answer("plan</think>Use the <think> tag", None, true, true).await;
    assert!(
        !reply.contains("plan") && !reply.contains("</think>"),
        "{reply:?}"
    );
    assert!(
        folded.iter().any(|body| body.contains("plan")),
        "{folded:?}"
    );
    for leading in [false, true] {
        let (reply, folded) = ollama_answer("<think>x</think>Done.", None, leading, true).await;
        assert_eq!(reply, "Done.");
        assert!(folded.iter().any(|body| body.contains('x')), "{folded:?}");
    }
}

/// Records what the reasoning fold retains.
#[derive(Default)]
struct FoldRecorder(std::sync::Mutex<Vec<String>>);
impl CompletedSpillRenderer for FoldRecorder {
    fn retain_completed(&self, output: &str) -> Option<u64> {
        self.0.lock().unwrap().push(output.to_string());
        Some(1)
    }
    fn render_completed(&self, _output: &str, _width: usize, _max_height: usize) -> usize {
        0
    }
    fn is_active(&self) -> bool {
        false
    }
    fn erase(&self) {}
    fn discard(&self) {}
}

/// #2372: Ollama's native `thinking` channel is folded like the OpenAI wire's
/// `reasoning_content`; together with an inline block both are folded and
/// neither reaches the answer; whitespace-only thinking is not reasoning.
#[tokio::test]
async fn ollama_native_thinking_is_folded_with_the_accepted_answer() {
    let _settings = thinking_folded();
    let (reply, folded) = ollama_answer("Done.", Some("plan the change"), false, true).await;
    assert_eq!(crate::agentic::model_explanation(&reply), "Done.");
    assert!(
        folded.iter().any(|body| body.contains("plan the change")),
        "{folded:?}"
    );

    let (reply, folded) = ollama_answer(
        "<think>inline step</think>Done.",
        Some("native plan"),
        false,
        true,
    )
    .await;
    assert_eq!(reply, "Done.");
    assert!(
        folded
            .iter()
            .any(|body| body.contains("native plan") && body.contains("inline step")),
        "both reasoning sources fold: {folded:?}"
    );

    let (reply, folded) = ollama_answer("<think>x</think>Done.", Some("   "), false, true).await;
    assert_eq!(reply, "Done.");
    assert!(
        folded.iter().all(|body| !body.trim().is_empty()),
        "{folded:?}"
    );
    assert!(folded.iter().any(|body| body.contains('x')), "{folded:?}");
    assert_eq!(
        ollama_answer("Done.", Some("  \n "), false, true).await,
        ("Done.".to_string(), vec![])
    );
}

/// #2372: headless and piped runs print no reasoning, as the display stream's
/// spinner never did off a colour terminal.
#[tokio::test]
async fn ollama_reasoning_is_not_folded_off_a_colour_terminal() {
    let _settings = thinking_folded();
    assert_eq!(
        ollama_answer("<think>x</think>Done.", Some("native plan"), false, false).await,
        ("Done.".to_string(), vec![])
    );
}

struct EmptyStreamResponder;
impl Respond for EmptyStreamResponder {
    fn respond(&self, req: &Request) -> ResponseTemplate {
        if is_stream(req) {
            ndjson(&[serde_json::json!({"message": {"content": ""}, "done": true})])
        } else {
            ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "message": {"content": "probe says hi"},
                "prompt_eval_count": 5, "eval_count": 2,
            }))
        }
    }
}

#[tokio::test]
async fn empty_stream_falls_back_to_probe_content() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(EmptyStreamResponder)
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let (reply, streamed, usage, _) =
        chat_complete(ctx(&server.uri(), &messages, &caveats), &mut NoMcp)
            .await
            .expect("chat_complete should succeed");

    assert_eq!(crate::agentic::model_explanation(&reply), "probe says hi");
    assert!(!streamed, "fallback content was never streamed");
    assert_eq!(usage.unwrap().input_tokens, 5);
}

/// Regression for the DGX wedge: the non-streamed probe said "Let me verify
/// by looking...", then the streaming re-issue returned no tokens. The probe
/// fallback must still go through the no-tool nudge gate instead of ending
/// the turn and forcing the operator to type "continue".
struct EmptyStreamPendingActionResponder {
    probes: Arc<AtomicUsize>,
}
impl Respond for EmptyStreamPendingActionResponder {
    fn respond(&self, req: &Request) -> ResponseTemplate {
        if is_stream(req) {
            return ndjson(&[serde_json::json!({
                "message": {"content": ""},
                "done": true,
                "prompt_eval_count": 6,
                "eval_count": 0
            })]);
        }
        let probe = self.probes.fetch_add(1, Ordering::SeqCst);
        let content = if probe == 0 {
            "Now I understand the issue. Let me verify by looking at format_rollup_detail."
        } else {
            "Verified after the automatic continue."
        };
        ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "message": {"content": content},
            "prompt_eval_count": 5 + probe as u32,
            "eval_count": 2,
        }))
    }
}

#[tokio::test]
async fn empty_stream_probe_fallback_pending_action_nudges_and_continues() {
    let server = MockServer::start().await;
    let probes = Arc::new(AtomicUsize::new(0));
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(EmptyStreamPendingActionResponder {
            probes: probes.clone(),
        })
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let (reply, streamed, _usage, _) =
        chat_complete(ctx(&server.uri(), &messages, &caveats), &mut NoMcp)
            .await
            .expect("chat_complete should auto-continue after pending probe fallback");

    assert_eq!(
        probes.load(Ordering::SeqCst),
        2,
        "the nudge ran a second probe"
    );
    assert_eq!(
        crate::agentic::model_explanation(&reply),
        "Verified after the automatic continue."
    );
    assert!(!streamed, "the second answer also came from probe fallback");
    assert!(
        !reply.contains("Let me verify"),
        "must not return the pending-action narration"
    );
}

/// Probe AND stream both empty, with no safe-context hint → the loop gives
/// the explicit empty-response diagnostic instead of silence.
struct AllEmptyResponder;
impl Respond for AllEmptyResponder {
    fn respond(&self, req: &Request) -> ResponseTemplate {
        if is_stream(req) {
            ndjson(&[serde_json::json!({"message": {"content": ""}, "done": true})])
        } else {
            ResponseTemplate::new(200)
                .set_body_json(serde_json::json!({"message": {"content": ""}}))
        }
    }
}

#[tokio::test]
async fn fully_empty_response_yields_diagnostic_message() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(AllEmptyResponder)
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let (reply, streamed, _, _) =
        chat_complete(ctx(&server.uri(), &messages, &caveats), &mut NoMcp)
            .await
            .expect("chat_complete should succeed");

    assert!(
        reply.contains("model returned an empty response"),
        "got: {reply}"
    );
    assert!(reply.contains("newt doctor"), "points at diagnostics");
    assert!(!streamed);
}

struct SuspiciousEmptyThenRecover {
    probes: Arc<AtomicUsize>,
    saw_nudge: Arc<AtomicBool>,
}
impl Respond for SuspiciousEmptyThenRecover {
    fn respond(&self, req: &Request) -> ResponseTemplate {
        if is_stream(req) {
            if self.probes.load(Ordering::SeqCst) <= 1 {
                ndjson(&[serde_json::json!({
                    "message": {"content": ""},
                    "done": true,
                    "prompt_eval_count": 9,
                    "eval_count": 4
                })])
            } else {
                ndjson(&[
                    serde_json::json!({"message": {"content": "recovered "}, "done": false}),
                    serde_json::json!({
                        "message": {"content": "after empty retry"},
                        "done": true,
                        "prompt_eval_count": 5,
                        "eval_count": 3
                    }),
                ])
            }
        } else {
            let body = body_json(req);
            if body["messages"].as_array().into_iter().flatten().any(|m| {
                m["content"]
                    .as_str()
                    .unwrap_or("")
                    .contains("no assistant-visible content")
            }) {
                self.saw_nudge.store(true, Ordering::SeqCst);
            }
            let n = self.probes.fetch_add(1, Ordering::SeqCst) + 1;
            if n == 1 {
                ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "message": {
                        "content": "",
                        "thinking": "I know what to do but did not emit final text."
                    },
                    "prompt_eval_count": 10,
                    "eval_count": 2559,
                }))
            } else {
                ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "message": {"content": "recovered after empty retry"},
                    "prompt_eval_count": 5,
                    "eval_count": 3,
                }))
            }
        }
    }
}

#[tokio::test]
async fn suspicious_empty_generated_output_retries_with_nudge() {
    let server = MockServer::start().await;
    let probes = Arc::new(AtomicUsize::new(0));
    let saw_nudge = Arc::new(AtomicBool::new(false));
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(SuspiciousEmptyThenRecover {
            probes: probes.clone(),
            saw_nudge: saw_nudge.clone(),
        })
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let (reply, streamed, usage, _) =
        chat_complete(ctx(&server.uri(), &messages, &caveats), &mut NoMcp)
            .await
            .expect("chat_complete should succeed");

    assert_eq!(
        crate::agentic::model_explanation(&reply),
        "recovered after empty retry"
    );
    assert!(!streamed, "the host renders the accepted answer (#2372)");
    assert_eq!(probes.load(Ordering::SeqCst), 2);
    assert!(saw_nudge.load(Ordering::SeqCst));
    assert_eq!(
        usage
            .expect("usage survives suspicious retry")
            .output_tokens,
        2_559 + 3,
        "usage from the suspicious empty round must be preserved"
    );
}

struct SuspiciousEmptyTwiceThenRecover {
    probes: Arc<AtomicUsize>,
    saw_strong_nudge: Arc<AtomicBool>,
}
impl Respond for SuspiciousEmptyTwiceThenRecover {
    fn respond(&self, req: &Request) -> ResponseTemplate {
        if is_stream(req) {
            if self.probes.load(Ordering::SeqCst) <= 2 {
                ndjson(&[serde_json::json!({
                    "message": {"content": ""},
                    "done": true,
                    "prompt_eval_count": 9,
                    "eval_count": 4
                })])
            } else {
                ndjson(&[serde_json::json!({
                    "message": {"content": "recovered after strong hidden-only nudge"},
                    "done": true,
                    "prompt_eval_count": 5,
                    "eval_count": 3
                })])
            }
        } else {
            let body = body_json(req);
            if body["messages"].as_array().into_iter().flatten().any(|m| {
                m["content"]
                    .as_str()
                    .unwrap_or("")
                    .contains("Hidden thinking is not an action")
            }) {
                self.saw_strong_nudge.store(true, Ordering::SeqCst);
            }
            let n = self.probes.fetch_add(1, Ordering::SeqCst) + 1;
            if n <= 2 {
                ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "message": {
                        "content": "",
                        "thinking": "I know the next action but did not emit it."
                    },
                    "prompt_eval_count": 10,
                    "eval_count": 2559,
                }))
            } else {
                ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "message": {"content": "recovered after strong hidden-only nudge"},
                    "prompt_eval_count": 5,
                    "eval_count": 3,
                }))
            }
        }
    }
}

#[tokio::test]
async fn repeated_thinking_only_gets_stronger_second_nudge() {
    // Two hidden-only probe rounds race the process-global `no_progress` brake
    // (`crate::initiative::installed_no_progress()`): unguarded, this test can
    // observe a `stop_after` a sibling test (e.g. `loop_controls::run_brake_loop`)
    // installed under `GlobalSettingsGuard` for its own duration, stopping the
    // turn after 2 idle rounds before the third (recovering) probe runs.
    let _settings = crate::test_guard::GlobalSettingsGuard::acquire();
    let server = MockServer::start().await;
    let probes = Arc::new(AtomicUsize::new(0));
    let saw_strong_nudge = Arc::new(AtomicBool::new(false));
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(SuspiciousEmptyTwiceThenRecover {
            probes: probes.clone(),
            saw_strong_nudge: saw_strong_nudge.clone(),
        })
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let (reply, streamed, _, _) =
        chat_complete(ctx(&server.uri(), &messages, &caveats), &mut NoMcp)
            .await
            .expect("second hidden-only nudge should recover the turn");

    assert_eq!(
        crate::agentic::model_explanation(&reply),
        "recovered after strong hidden-only nudge"
    );
    assert!(!streamed, "the host renders the accepted answer (#2372)");
    assert_eq!(probes.load(Ordering::SeqCst), 3);
    assert!(saw_strong_nudge.load(Ordering::SeqCst));
}

struct SuspiciousEmptyStaysEmpty;
impl Respond for SuspiciousEmptyStaysEmpty {
    fn respond(&self, req: &Request) -> ResponseTemplate {
        if is_stream(req) {
            ndjson(&[serde_json::json!({
                "message": {"content": ""},
                "done": true,
                "prompt_eval_count": 9,
                "eval_count": 4
            })])
        } else {
            ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "message": {
                    "content": "",
                    "reasoning_content": "internal-only response"
                },
                "prompt_eval_count": 10,
                "eval_count": 12,
            }))
        }
    }
}

#[tokio::test]
async fn suspicious_empty_generated_output_reports_targeted_diagnostic() {
    // Two suspicious-empty retry rounds race the process-global `no_progress`
    // brake (`crate::initiative::installed_no_progress()`): unguarded, this
    // test can observe a `stop_after` a sibling test (e.g.
    // `loop_controls::run_brake_loop`) installed under `GlobalSettingsGuard`
    // for its own duration, stopping the turn after 2 idle rounds before the
    // retry cap is reached and the targeted diagnostic is produced.
    let _settings = crate::test_guard::GlobalSettingsGuard::acquire();
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(SuspiciousEmptyStaysEmpty)
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let uri = server.uri();
    let mut c = ctx(&uri, &messages, &caveats);
    c.trace = true;
    let (reply, streamed, _, _) = chat_complete(c, &mut NoMcp)
        .await
        .expect("chat_complete should succeed");

    assert!(reply.contains("generated output tokens"), "got: {reply}");
    assert!(
        reply.contains("reasoning_content"),
        "diagnostic should name the non-content field: {reply}"
    );
    assert!(reply.contains("--trace"), "points at trace diagnostics");
    assert!(!streamed);
}

#[tokio::test]
async fn openai_empty_content_yields_diagnostic_message() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "choices": [{"message": {"content": ""}}]
        })))
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let uri = server.uri();
    let mut c = ctx(&uri, &messages, &caveats);
    c.kind = BackendKind::Openai;
    let (reply, _, _, _) = chat_complete(c, &mut NoMcp).await.expect("should succeed");
    assert!(
        reply.contains("model returned an empty response"),
        "got: {reply}"
    );
}