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
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
use super::*;

#[tokio::test]
async fn rendered_report_ack_avoids_repetition_and_allows_followup_tools() {
    use crate::agentic::scheduled::{SessionStepLedger, StepLedger};
    let ledger = SessionStepLedger::default();
    let server = MockServer::start().await;
    let round = Arc::new(AtomicUsize::new(0));
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .respond_with(ScriptedOpenAi {
            round: round.clone(),
            script: vec![
                serde_json::json!({"tool_calls": [{"id": "report", "function": {
                    "name": "render_report", "arguments": "{\"title\":\"Findings\",\"body\":\"REPORT_BODY_CANARY\"}"
                }}]}),
                serde_json::json!({"tool_calls": [{"id": "plan", "function": {
                    "name": "update_plan", "arguments": "{\"plan\":[{\"step\":\"Verify the requested change\"}]}"
                }}]}),
                serde_json::json!({"content": "The verification plan is ready."}),
            ],
        })
        .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;
    c.step_ledger = Some(&ledger);
    let (reply, _, _, _) = chat_complete(c, &mut NoMcp).await.expect("dispatch");
    assert_eq!(
        crate::agentic::model_explanation(&reply),
        "The verification plan is ready."
    );
    assert_eq!(
        ledger.count(),
        1,
        "the follow-up tool actually updated the plan"
    );
    let requests = server.received_requests().await.unwrap();
    let ack = requests
        .iter()
        .filter_map(|request| serde_json::from_slice::<serde_json::Value>(&request.body).ok())
        .find_map(|body| {
            body["messages"].as_array()?.iter().find_map(|message| {
                (message["tool_call_id"] == "report")
                    .then(|| message["content"].as_str().map(str::to_string))
                    .flatten()
            })
        })
        .expect("the next inference receives the report acknowledgement");
    assert!(ack.contains("already visible"), "{ack}");
    assert!(ack.contains("Do not repeat"), "{ack}");
    assert!(ack.contains("unfinished requested work"), "{ack}");
    assert!(
        !ack.contains("REPORT_BODY_CANARY"),
        "the ack must stay small"
    );
}

/// Like [`run_openai_script`] but with a configured narrate-then-stop
/// rescue budget (`[tui] narration_nudge_cap`, lever L3).
async fn run_openai_script_with_cap(
    script: Vec<serde_json::Value>,
    narration_nudge_cap: usize,
) -> (String, usize) {
    let _settings = default_loop_settings();
    let server = MockServer::start().await;
    let round = Arc::new(AtomicUsize::new(0));
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .respond_with(ScriptedOpenAi {
            round: round.clone(),
            script,
        })
        .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;
    c.narration_nudge_cap = narration_nudge_cap;
    let (reply, _s, _u, _h) = chat_complete(c, &mut NoMcp).await.expect("dispatch");
    (reply, round.load(Ordering::SeqCst))
}

#[test]
fn strip_trailing_nudge_keeps_only_the_current_correction() {
    // #1158: successive nudges must REPLACE, not pile up — otherwise the
    // model's own accumulated dithering drives the "genuinely finished"
    // defense loop. After stripping, the tail nudge pair is gone; a
    // non-nudge tail is untouched.
    use serde_json::json;
    let guidance = format!("{} act now", compress::LOOP_GUIDANCE_PREFIX);
    let mut msgs = vec![
        json!({"role": "user", "content": "do the thing"}),
        json!({"role": "assistant", "content": "Let me start."}),
        json!({"role": "user", "content": guidance.clone()}),
    ];
    strip_trailing_nudge_exchange(&mut msgs);
    assert_eq!(msgs.len(), 1, "the narration + nudge pair is removed");
    assert_eq!(msgs[0]["content"], "do the thing");

    let mut clean = vec![
        json!({"role": "user", "content": "do the thing"}),
        json!({"role": "assistant", "content": "here is the answer"}),
    ];
    let before = clean.clone();
    strip_trailing_nudge_exchange(&mut clean);
    assert_eq!(clean, before, "a real answer tail is never stripped");

    let mut loop_msgs = vec![json!({"role": "user", "content": "fix it"})];
    for i in 0..3 {
        strip_trailing_nudge_exchange(&mut loop_msgs);
        loop_msgs.push(json!({"role": "assistant", "content": format!("narration {i}")}));
        loop_msgs.push(json!({"role": "user", "content": guidance.clone()}));
    }
    assert_eq!(
        loop_msgs.len(),
        3,
        "user task + exactly one (narration, nudge) pair — not three"
    );
}

#[tokio::test]
async fn narrated_intent_with_no_tool_call_nudges_and_continues() {
    // The model narrates intent to act but calls no tool. Instead of ending
    // the turn (the bug), the loop nudges and runs another round, returning
    // the post-nudge answer.
    let (reply, rounds) = run_openai_script(vec![
        serde_json::json!({ "content": "Let me edit the file now." }),
        serde_json::json!({ "content": "All done — the edit is complete." }),
    ])
    .await;
    assert_eq!(rounds, 2, "must run a second round after the nudge");
    assert!(
        reply.contains("complete"),
        "returns the post-nudge answer: {reply}"
    );
    assert!(
        !reply.contains("Let me edit"),
        "must not return the narration: {reply}"
    );
}

#[tokio::test]
async fn narration_auto_continue_is_bounded_by_the_cap() {
    // The model narrates intent EVERY round. The cap (1) allows exactly one
    // nudge, then the narration is accepted as the final answer — no loop.
    let (reply, rounds) = run_openai_script(vec![
        serde_json::json!({ "content": "Let me keep editing now." }),
        serde_json::json!({ "content": "Let me keep editing now." }),
        serde_json::json!({ "content": "Let me keep editing now." }),
    ])
    .await;
    assert_eq!(
        rounds, 2,
        "exactly one nudge (cap=1), then accept, got {rounds}"
    );
    assert!(
        reply.contains("editing"),
        "narration accepted as final: {reply}"
    );
}

#[tokio::test]
async fn narration_nudge_cap_two_allows_a_second_escalated_rescue() {
    // Lever L3: with `narration_nudge_cap = 2` a chronic narrator gets TWO
    // rescues (the second escalated), and the post-rescue answer is
    // returned; a genuine recovery on round 3 proves the extra budget is
    // what converts the stall.
    let (reply, rounds) = run_openai_script_with_cap(
        vec![
            serde_json::json!({ "content": "Let me keep editing now." }),
            serde_json::json!({ "content": "Let me keep editing now." }),
            serde_json::json!({ "content": "All done — the edit is complete." }),
        ],
        2,
    )
    .await;
    assert_eq!(
        rounds, 3,
        "two nudges (cap=2) before the recovery, got {rounds}"
    );
    assert!(
        reply.contains("complete"),
        "returns the post-nudge answer: {reply}"
    );
}

#[tokio::test]
async fn narration_nudge_cap_two_still_accepts_after_exhaustion() {
    // The raised cap is still a cap: a model that narrates through both
    // rescues has its third narration accepted as the final answer.
    let (reply, rounds) = run_openai_script_with_cap(
        vec![
            serde_json::json!({ "content": "Let me keep editing now." }),
            serde_json::json!({ "content": "Let me keep editing now." }),
            serde_json::json!({ "content": "Let me keep editing now." }),
            serde_json::json!({ "content": "Let me keep editing now." }),
        ],
        2,
    )
    .await;
    assert_eq!(rounds, 3, "two nudges, then accept, got {rounds}");
    assert!(
        reply.contains("editing"),
        "narration accepted as final: {reply}"
    );
}

#[test]
fn escalated_narration_nudge_names_attempt_cap_and_active_step() {
    use crate::agentic::scheduled::{SessionStepLedger, StepLedger};

    let ledger = SessionStepLedger::default();
    ledger.restore(&PlanSnapshot {
        target: None,
        steps: vec![
            Step {
                description: "inspect".to_string(),
                status: StepStatus::Done,
            },
            Step {
                description: "fix conflict markers".to_string(),
                status: StepStatus::Active,
            },
        ],
    });
    let text = escalated_narration_action_nudge(2, 3, Some(&ledger as &dyn StepLedger));
    assert!(text.contains("Reminder 2/3"), "{text}");
    assert!(text.contains("fix conflict markers"), "{text}");
    assert!(text.contains("tool call"), "{text}");

    // No ledger: no step clause, the demand still stands.
    let bare = escalated_narration_action_nudge(2, 2, None);
    assert!(bare.contains("Reminder 2/2"), "{bare}");
    assert!(!bare.contains("Active step"), "{bare}");
}

#[tokio::test]
async fn accepted_narration_reports_cap_exhausted_end_reason() {
    // The acceptance-forensics record: a narration that exhausts the
    // rescue budget ends the turn with a visible reason instead of
    // masquerading as a normal completion.
    let server = MockServer::start().await;
    let round = Arc::new(AtomicUsize::new(0));
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .respond_with(ScriptedOpenAi {
            round: round.clone(),
            script: vec![
                serde_json::json!({ "content": "Let me keep editing now." }),
                serde_json::json!({ "content": "Let me keep editing now." }),
            ],
        })
        .mount(&server)
        .await;
    let messages = msgs();
    let caveats = Caveats::top();
    let uri = server.uri();
    let mut end_reason: Option<crate::TurnEndReason> = None;
    let mut c = ctx(&uri, &messages, &caveats);
    c.kind = BackendKind::Openai;
    c.end_reason = Some(&mut end_reason);
    let _ = chat_complete(c, &mut NoMcp).await.expect("dispatch");
    assert_eq!(
        end_reason,
        Some(crate::TurnEndReason::NarrationCapExhausted)
    );
}

/// Real loopback HTTP and temporary files ground the classifier regression:
/// a recovered turn can report actual reads, while a further promise after
/// those same reads still exhausts the unchanged narration budget.
#[tokio::test]
async fn completed_check_report_after_tool_reads_keeps_completed_end_reason() {
    let report = "Both operations executed and returned real results this turn.\n\n\
        Git operations check: state.txt returned STATE_CANARY; git-check.txt returned GIT_CANARY.\n\n\
        The state and git check results are above. Stopping here.";
    let promise = "Let me check the current working directory and available git operations.";
    for (answer, expected) in [
        (promise, crate::TurnEndReason::NarrationCapExhausted),
        (report, crate::TurnEndReason::Completed),
    ] {
        let workspace = tempfile::tempdir().unwrap();
        std::fs::write(workspace.path().join("state.txt"), "STATE_CANARY").unwrap();
        std::fs::write(workspace.path().join("git-check.txt"), "GIT_CANARY").unwrap();
        let server = MockServer::start().await;
        let round = Arc::new(AtomicUsize::new(0));
        Mock::given(method("POST"))
            .and(path("/v1/chat/completions"))
            .respond_with(ScriptedOpenAi {
                round: round.clone(),
                script: vec![
                    serde_json::json!({"content": promise}),
                    serde_json::json!({"tool_calls": [
                        {"id": "state", "function": {"name": "read_file", "arguments": "{\"path\":\"state.txt\"}"}},
                        {"id": "git_check", "function": {"name": "read_file", "arguments": "{\"path\":\"git-check.txt\"}"}}
                    ]}),
                    serde_json::json!({"content": answer}),
                ],
            })
            .mount(&server)
            .await;
        let task = "Execute two read_file calls for state.txt and git-check.txt, report the results, and stop.";
        assert!(crate::classifiers::user_turn_invites_action(task));
        let messages = vec![MemMessage::system("you are a test"), MemMessage::user(task)];
        let caveats = Caveats::top();
        let uri = server.uri();
        let mut end_reason = None;
        let mut c = ctx(&uri, &messages, &caveats);
        c.kind = BackendKind::Openai;
        c.task = task;
        c.workspace = workspace.path().to_str().unwrap();
        c.end_reason = Some(&mut end_reason);
        let (reply, _, _, _) = chat_complete(c, &mut NoMcp).await.unwrap();
        assert_eq!(
            round.load(Ordering::SeqCst),
            3,
            "one rescue, actual reads, final reply"
        );
        assert_eq!(crate::agentic::model_explanation(&reply), answer);
        let requests = server.received_requests().await.unwrap();
        let recovery = body_json(&requests[1]);
        let guidance = recovery["messages"].as_array().unwrap().last().unwrap()["content"]
            .as_str()
            .unwrap();
        assert!(
            guidance.contains(compress::LOOP_GUIDANCE_PREFIX),
            "{guidance}"
        );
        let final_request = body_json(&requests[2]);
        let tool_results: Vec<_> = final_request["messages"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|message| message["role"] == "tool")
            .filter_map(|message| message["content"].as_str())
            .collect();
        assert_eq!(tool_results.len(), 2, "{tool_results:?}");
        for canary in ["STATE_CANARY", "GIT_CANARY"] {
            assert!(
                tool_results.iter().any(|result| result.contains(canary)),
                "{tool_results:?}"
            );
        }
        assert_eq!(end_reason, Some(expected), "{answer}");
    }
}

#[tokio::test]
async fn explanation_completion_retries_a_promise_without_restricting_session_authority() {
    // The branch-count incident ended after eighteen reads with only "Let me
    // check...". A response style must not turn that promise into an answer or
    // remove the operator's existing tool authority.
    let server = MockServer::start().await;
    let round = Arc::new(AtomicUsize::new(0));
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .respond_with(ScriptedOpenAi {
            round: round.clone(),
            script: vec![
                serde_json::json!({"tool_calls": [{"id": "read_evidence", "function": {
                    "name": "tool_search", "arguments": "{\"query\":\"read_file\"}"
                }}]}),
                serde_json::json!({ "content": "Let me check the local branches and the top-level remote remotes to be complete." }),
                serde_json::json!({ "content": "Do you mean local branches or open pull requests?" }),
            ],
        })
        .mount(&server)
        .await;
    let messages = msgs();
    let caveats = Caveats::top();
    let uri = server.uri();
    let mut end_reason: Option<crate::TurnEndReason> = None;
    let mut c = ctx(&uri, &messages, &caveats);
    c.kind = BackendKind::Openai;
    c.prompt_disposition = PromptDisposition::Explain;
    c.task = "Can you tell me how many branches are open in this repo?";
    c.action_nudges = false;
    c.end_reason = Some(&mut end_reason);
    let (reply, _, _, _) = chat_complete(c, &mut NoMcp).await.expect("dispatch");
    assert_eq!(
        crate::agentic::model_explanation(&reply),
        "Do you mean local branches or open pull requests?"
    );
    assert_eq!(
        round.load(Ordering::SeqCst),
        3,
        "one evidence read, promise, clarification"
    );
    let requests = server.received_requests().await.unwrap();
    let recovery = body_json(&requests[2]);
    let guidance = recovery["messages"].as_array().unwrap().last().unwrap()["content"]
        .as_str()
        .unwrap();
    assert!(
        guidance.contains("answer") && guidance.contains("read-only"),
        "{guidance}"
    );
    assert!(
        !guidance.contains("edit_file") && !guidance.contains("write_file"),
        "{guidance}"
    );
    for request in requests {
        let body = body_json(&request);
        let names: Vec<_> = body["tools"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|tool| tool["function"]["name"].as_str())
            .collect();
        assert!(
            names.contains(&"write_file") && names.contains(&"run_command"),
            "{names:?}"
        );
    }
    assert_eq!(
        end_reason,
        Some(crate::TurnEndReason::Completed),
        "a concrete clarification completes the turn"
    );
    // The footer stays clean too — the ⚠ never renders for Completed.
    let metrics = crate::TurnMetrics {
        end_reason,
        ..Default::default()
    };
    assert!(
        !metrics.display_line().contains('⚠'),
        "no warning may render: {}",
        metrics.display_line()
    );
}

#[tokio::test]
async fn readonly_completion_repeated_promise_hands_back_unresolved_with_bounded_retry() {
    let promise = "Let me check the current implementation and identify any gaps.";
    for max_rounds in [1, 8] {
        let server = MockServer::start().await;
        let round = Arc::new(AtomicUsize::new(0));
        Mock::given(method("POST"))
            .and(path("/v1/chat/completions"))
            .respond_with(ScriptedOpenAi {
                round: round.clone(),
                script: vec![serde_json::json!({"content": promise})],
            })
            .mount(&server)
            .await;
        let messages = msgs();
        let caveats = Caveats::top();
        let uri = server.uri();
        let mut end_reason = None;
        let mut c = ctx(&uri, &messages, &caveats);
        c.kind = BackendKind::Openai;
        c.prompt_disposition = PromptDisposition::Research;
        c.max_tool_rounds = max_rounds;
        c.end_reason = Some(&mut end_reason);
        let (reply, streamed, _, _) = chat_complete(c, &mut NoMcp).await.unwrap();
        assert!(
            reply.contains("unresolved") && reply.contains("continue"),
            "{reply}"
        );
        assert!(
            reply.contains(promise),
            "never discard the candidate: {reply}"
        );
        assert!(reply.contains("appears unfinished"), "{reply}");
        assert!(!streamed, "the caller must print the unresolved handoff");
        assert_ne!(end_reason, Some(crate::TurnEndReason::Completed));
        assert_eq!(round.load(Ordering::SeqCst), max_rounds.min(2));
    }
}

#[tokio::test]
async fn readonly_completion_accepts_answers_questions_and_blockers_without_retry() {
    for answer in [
        "There are three local branches.",
        "Do you mean local branches or open pull requests?",
        "Should I check the current implementation and identify any gaps?",
        "I cannot determine the remote count from the available evidence.",
        "I cannot check the current implementation because access is unavailable.",
        "There are three local branches. Let me check the current implementation and identify any gaps.",
        "Here is a summary of what I found across the tool calls.",
        "I found the issue: there is an extra closing brace causing a syntax error. I need to remove this stray brace.",
        "Current blocker: the for loop needs a one line fix. Next steps needed: fix the iteration type error in lib.rs.",
    ] {
        let server = MockServer::start().await;
        let round = Arc::new(AtomicUsize::new(0));
        Mock::given(method("POST"))
            .and(path("/v1/chat/completions"))
            .respond_with(ScriptedOpenAi {
                round: round.clone(),
                script: vec![serde_json::json!({"content": answer})],
            })
            .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;
        c.prompt_disposition = PromptDisposition::Explain;
        let (reply, _, _, _) = chat_complete(c, &mut NoMcp).await.unwrap();
        assert_eq!(crate::agentic::model_explanation(&reply), answer);
        assert_eq!(round.load(Ordering::SeqCst), 1);
    }
}

/// Grounds mocked exhausted-response retention in the real finalizer and a
/// temporary workspace, proving retained text still crosses disclosure.
#[test]
fn readonly_completion_handoff_preserves_the_disclosure_boundary() {
    let workspace = tempfile::tempdir().unwrap();
    let canary = "NEWT-CANARY-completion-7f3a9c2b1d";
    let mut filter = crate::ocap::DisclosureFilter::new();
    filter.register(canary);
    let candidate = format!("Let me check the current implementation: {canary}");
    let reply = readonly_completion_handoff(
        &candidate,
        &mut None,
        false,
        workspace.path().to_str().unwrap(),
        &crate::Scope::All,
        &capability_check::Evidence::default(),
        Some(&filter),
        &claim_check::TurnClaims::capture(
            workspace.path().to_str().unwrap(),
            &crate::Scope::All,
            None,
        ),
        &self_verify::VerificationLedger::default(),
    );
    assert!(reply.contains("Let me check the current implementation"));
    assert!(reply.contains("[REDACTED]"), "{reply}");
    assert!(!filter.leaks(&reply), "{reply}");
}

#[test]
fn readonly_completion_shared_guard_preserves_authority_and_bounds_recovery() {
    let classifier = crate::NudgeClassifier::builtin();
    let promise = "Let me check the current implementation and identify any gaps.";
    for disposition in [PromptDisposition::Explain, PromptDisposition::Research] {
        assert!(readonly_completion_pending(
            disposition,
            &classifier,
            promise
        ));
        assert!(!readonly_completion_pending(disposition, &classifier,
            "Recommended next action if session resumes: fix duplicate functions, clean up broken tests, read lib.rs, then wire the progressive dispatch. The build is currently broken and that is the blocker for further progress."));
    }
    for disposition in [
        PromptDisposition::Act,
        PromptDisposition::Ask,
        PromptDisposition::Plan,
    ] {
        assert!(!readonly_completion_pending(
            disposition,
            &classifier,
            promise
        ));
    }
    let mut retried = false;
    let mut messages = vec![serde_json::json!({"role": "user", "content": "Count the branches?"})];
    assert!(!retry_readonly_completion(
        &mut messages,
        promise,
        None,
        &mut retried,
        false
    ));
    assert!(!retried, "no retry was spent without room to continue");
    assert!(retry_readonly_completion(
        &mut messages,
        promise,
        None,
        &mut retried,
        true
    ));
    let after_retry = messages.clone();
    assert!(!retry_readonly_completion(
        &mut messages,
        promise,
        None,
        &mut retried,
        true
    ));
    assert_eq!(
        messages, after_retry,
        "a persistent promise cannot replenish its retry"
    );
}

#[tokio::test]
async fn readonly_completion_ollama_and_responses_recover_with_the_same_readonly_guidance() {
    for responses in [false, true] {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(move |request: &Request| {
                let body = body_json(request);
                let text = if body.to_string().contains("Your reply promised another step") {
                    "There are three local branches."
                } else {
                    "Let me check the current implementation and identify any gaps."
                };
                let reply = if responses {
                    serde_json::json!({"status": "completed", "output": [{
                        "type": "reasoning", "id": "rs_probe", "summary": [], "encrypted_content": "opaque"
                    }, {
                        "type": "message", "role": "assistant", "content": [{"type": "output_text", "text": text}]
                    }]})
                } else {
                    serde_json::json!({"message": {"role": "assistant", "content": text}, "done": true})
                };
                ResponseTemplate::new(200).set_body_json(reply)
            })
            .mount(&server).await;
        let messages = msgs();
        let caveats = Caveats::top();
        let uri = server.uri();
        let mut c = ctx(&uri, &messages, &caveats);
        c.prompt_disposition = PromptDisposition::Explain;
        c.action_nudges = false;
        let (reply, _, _, _) = if responses {
            openai_responses_complete(c, &mut NoMcp).await
        } else {
            chat_complete(c, &mut NoMcp).await
        }
        .unwrap();
        assert_eq!(
            crate::agentic::model_explanation(&reply),
            "There are three local branches."
        );
        let requests = server.received_requests().await.unwrap();
        // #2372: no display reissue on either wire.
        assert_eq!(requests.len(), 2);
        if responses {
            let second = body_json(&requests[1]);
            assert!(
                second["input"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .any(|item| item["id"] == "rs_probe" && item["encrypted_content"] == "opaque"),
                "the retry must preserve Responses reasoning items: {second}"
            );
        }
    }
}

#[tokio::test]
async fn genuine_completion_reports_completed_end_reason() {
    let server = MockServer::start().await;
    let round = Arc::new(AtomicUsize::new(0));
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .respond_with(ScriptedOpenAi {
            round: round.clone(),
            script: vec![serde_json::json!({ "content": "The capital of France is Paris." })],
        })
        .mount(&server)
        .await;
    let messages = msgs();
    let caveats = Caveats::top();
    let uri = server.uri();
    let mut end_reason: Option<crate::TurnEndReason> = None;
    let mut c = ctx(&uri, &messages, &caveats);
    c.kind = BackendKind::Openai;
    c.end_reason = Some(&mut end_reason);
    let _ = chat_complete(c, &mut NoMcp).await.expect("dispatch");
    assert_eq!(end_reason, Some(crate::TurnEndReason::Completed));
}

#[tokio::test]
async fn narration_nudge_reaches_the_wire_tagged_as_loop_guidance() {
    // The rescue nudge must arrive tagged so the compaction pipeline can
    // keep it (and the model's echo of it) out of later summaries.
    let server = MockServer::start().await;
    let saw_tag = Arc::new(AtomicBool::new(false));

    struct TagProbe {
        saw_tag: Arc<AtomicBool>,
    }
    impl Respond for TagProbe {
        fn respond(&self, req: &Request) -> ResponseTemplate {
            let body = body_json(req);
            let tagged = body["messages"].as_array().is_some_and(|ms| {
                ms.iter().any(|m| {
                    m["role"] == "user"
                        && m["content"]
                            .as_str()
                            .is_some_and(|c| c.starts_with(compress::LOOP_GUIDANCE_PREFIX))
                })
            });
            if tagged {
                self.saw_tag.store(true, Ordering::SeqCst);
                return ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "choices": [{ "message": { "content": "All done — edit complete." } }]
                }));
            }
            ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "choices": [{ "message": { "content": "Let me edit the file now." } }]
            }))
        }
    }
    Mock::given(method("POST"))
        .and(path("/v1/chat/completions"))
        .respond_with(TagProbe {
            saw_tag: saw_tag.clone(),
        })
        .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, _s, _u, _h) = chat_complete(c, &mut NoMcp).await.expect("dispatch");
    assert!(
        saw_tag.load(Ordering::SeqCst),
        "the narration nudge must carry LOOP_GUIDANCE_PREFIX on the wire"
    );
    let requests = server.received_requests().await.unwrap();
    let retry = body_json(&requests[1])["messages"].to_string();
    assert_eq!(retry.matches("Guidance is advisory").count(), 1, "{retry}");
    assert!(
        retry.contains("Honor stop or report-only requests"),
        "{retry}"
    );
    assert!(reply.contains("complete"), "{reply}");
}

#[tokio::test]
async fn ollama_loop_honors_cap_two_and_escalates_the_second_nudge() {
    let _settings = default_loop_settings();
    // Ollama-path parity for lever L3 (the macro chain is separate code
    // from the OpenAI inline chain): with narration_nudge_cap = 2 the
    // first rescue carries the [loop-guidance]-tagged generic corrective
    // and the SECOND carries the escalated "Reminder 2/2" variant — both
    // observed on the wire — before the model recovers.
    let server = MockServer::start().await;
    let saw_first = Arc::new(AtomicBool::new(false));
    let saw_escalated = Arc::new(AtomicBool::new(false));

    struct EscalationProbe {
        saw_first: Arc<AtomicBool>,
        saw_escalated: Arc<AtomicBool>,
    }
    impl Respond for EscalationProbe {
        fn respond(&self, req: &Request) -> ResponseTemplate {
            let body = body_json(req);
            let has = |needle: &str| {
                body["messages"].as_array().is_some_and(|ms| {
                    ms.iter()
                        .any(|m| m["content"].as_str().is_some_and(|c| c.contains(needle)))
                })
            };
            if has("Reminder 2/2") {
                self.saw_escalated.store(true, Ordering::SeqCst);
                return ResponseTemplate::new(200).set_body_json(serde_json::json!({
                    "message": { "content": "All done — the edit is complete." }
                }));
            }
            if has(compress::LOOP_GUIDANCE_PREFIX) {
                self.saw_first.store(true, Ordering::SeqCst);
            }
            ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "message": { "content": "Let me keep editing now." }
            }))
        }
    }
    Mock::given(method("POST"))
        .and(path("/api/chat"))
        .respond_with(EscalationProbe {
            saw_first: saw_first.clone(),
            saw_escalated: saw_escalated.clone(),
        })
        .mount(&server)
        .await;

    let messages = msgs();
    let caveats = Caveats::top();
    let uri = server.uri();
    let mut c = ctx(&uri, &messages, &caveats);
    c.kind = BackendKind::Ollama;
    c.narration_nudge_cap = 2;
    let (reply, _s, _u, _h) = chat_complete(c, &mut NoMcp).await.expect("dispatch");
    assert!(
        saw_first.load(Ordering::SeqCst),
        "the first nudge must reach the Ollama wire tagged [loop-guidance]"
    );
    assert!(
        saw_escalated.load(Ordering::SeqCst),
        "the second nudge must be the escalated Reminder 2/2 variant"
    );
    let requests = server.received_requests().await.unwrap();
    let retry = body_json(&requests[1])["messages"].to_string();
    assert_eq!(retry.matches("Guidance is advisory").count(), 1, "{retry}");
    assert!(
        retry.contains("Honor stop or report-only requests"),
        "{retry}"
    );
    assert!(reply.contains("complete"), "{reply}");
}

#[tokio::test]
async fn genuine_final_answer_is_not_nudged() {
    // No prior tool call and no intent-to-act cue → a real answer returns
    // immediately, un-nudged (no wasted round).
    let (reply, rounds) = run_openai_script(vec![
        serde_json::json!({ "content": "The capital of France is Paris." }),
    ])
    .await;
    assert_eq!(
        rounds, 1,
        "a plain final answer is not nudged, got {rounds}"
    );
    assert!(reply.contains("Paris"), "returns the answer: {reply}");
}

#[tokio::test]
async fn final_answer_after_a_tool_call_is_not_nudged() {
    // The normal "act, then conclude" turn: a tool call, then a cue-less
    // final answer. The rescue must NOT fire (no intent cue) — else every
    // ordinary tool-using turn would waste a round.
    let (reply, rounds) = run_openai_script(vec![
        serde_json::json!({
            "content": null,
            "tool_calls": [{
                "id": "c1", "type": "function",
                "function": { "name": "definitely_not_a_real_tool", "arguments": "{}" }
            }]
        }),
        serde_json::json!({ "content": "The files were examined; everything checks out." }),
    ])
    .await;
    assert_eq!(
        rounds, 2,
        "tool call (r0) then final answer (r1) — no extra round, got {rounds}"
    );
    assert!(
        reply.contains("checks out"),
        "returns the final answer as-is: {reply}"
    );
}

#[tokio::test]
async fn observed_fix_intent_after_a_tool_call_nudges_and_continues() {
    // Live repro: after a read-only observation, the model identified the
    // exact edit but stopped on prose instead of calling the edit tool.
    let (reply, rounds) = run_openai_script(vec![
        serde_json::json!({
            "content": null,
            "tool_calls": [{
                "id": "c1", "type": "function",
                "function": { "name": "definitely_not_a_real_tool", "arguments": "{}" }
            }]
        }),
        serde_json::json!({
            "content": "I found the issue - there's an extra closing brace } on line 809 of help_sections.rs that's causing a syntax error. I need to remove this stray brace."
        }),
        serde_json::json!({ "content": "The stray brace is removed and the compile error is fixed." }),
    ])
    .await;
    assert_eq!(
        rounds, 3,
        "tool call, narrated edit intent, then post-nudge answer; got {rounds}"
    );
    assert!(
        reply.contains("compile error is fixed"),
        "returns the post-nudge answer: {reply}"
    );
    assert!(
        !reply.contains("I need to remove"),
        "must not stop on the narrated edit intent: {reply}"
    );
}

#[test]
fn looks_like_intent_to_act_separates_narration_from_final_answers() {
    // Real repro narrations that ended a turn — must read as intent-to-act.
    assert!(looks_like_intent_to_act(
        "Now I have everything I need. Let me make both edits now."
    ));
    assert!(looks_like_intent_to_act(
        "Now I'll add the --home flag to the Cli struct."
    ));
    assert!(looks_like_intent_to_act("Let me keep editing now."));
    assert!(looks_like_intent_to_act(
        "I'm going to edit the config file."
    ));
    assert!(looks_like_intent_to_act(
        "Let me understand what was already done on this branch and compare it with the issue requirements."
    ));
    assert!(looks_like_intent_to_act(
        "Let me check the current implementation and identify any gaps."
    ));
    assert!(looks_like_intent_to_act(
        "The help section logic itself has no tests yet.\n\nLet me commit this first step, then move on:"
    ));
    assert!(looks_like_intent_to_act(
        "Plan is current — no update needed. Continuing with step 2: inserting the progressive dispatch into lib.rs."
    ));
    assert!(looks_like_intent_to_act(
        "I found the issue - there's an extra closing brace } on line 809 of help_sections.rs that's causing a syntax error. I need to remove this stray brace."
    ));
    // Genuine sign-offs / answers — must NOT be nudged.
    assert!(!looks_like_intent_to_act("The capital of France is Paris."));
    assert!(!looks_like_intent_to_act(
        "I have finished editing the file and the tests pass."
    ));
    assert!(!looks_like_intent_to_act(
        "Here is a summary of what I found across the tool calls."
    ));
    // Borrowed-cue sign-off ("let me know" + a verb) — must NOT be nudged.
    assert!(!looks_like_intent_to_act(
        "Done. Let me know if you want any further changes."
    ));
    // A long narration whose 400-byte tail cut lands mid-multibyte-glyph
    // (each `…` is 3 bytes; 200 of them puts the cut at byte 211, not a char
    // boundary) must not panic the slice — and still classify as intent.
    let multibyte = format!("{}let me edit", "…".repeat(200));
    assert!(looks_like_intent_to_act(&multibyte));
}

/// #2625: when the model follows the post-render nudge and prefixes a corrected
/// table with the prescribed "Correction: … — supersedes the report above" form,
/// the harness returns that reply unchanged — the correction statement is not
/// stripped or collapsed. The companion unit test (in report.rs) asserts the nudge
/// text carries the instruction; this test asserts the reply path honours it.
#[tokio::test]
async fn explicit_correction_statement_passes_through_to_caller() {
    let correction_reply = "Correction: I listed files shortest to longest, not longest \
        to shortest — supersedes the report above\n\n\
        | File | Lines |\n|------|-------|\n| gamma.rs | 300 |\n| beta.rs | 200 |\n| alpha.rs | 100 |";
    let (reply, _rounds) = run_openai_script(vec![
        // Round 1: render_report with a table
        serde_json::json!({"tool_calls": [{"id": "rpt", "function": {
            "name": "render_report",
            "arguments": "{\"title\":\"Top files\",\"body\":\"| File | Lines |\\n|------|-------|\\n| alpha.rs | 100 |\\n| beta.rs | 200 |\\n| gamma.rs | 300 |\"}"
        }}]}),
        // Round 2: final reply uses the correction format
        serde_json::json!({ "content": correction_reply }),
    ])
    .await;
    assert!(
        reply.contains("supersedes the report above"),
        "correction statement must reach the caller: {reply}"
    );
    assert!(
        reply.contains("Correction:"),
        "correction prefix must be present in the returned reply: {reply}"
    );
}