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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
//! Configurable lightweight classifiers.
//!
//! Classifier drop-ins live under `~/.newt/classifiers/` (or
//! `$NEWT_CONFIG_DIR/classifiers/`). The first shipped classifier is the
//! `NudgeClassifier`, used by the agentic loop to distinguish final answers
//! from no-tool-call stall shapes such as "I am about to continue" narrations
//! and stale-plan findings summaries.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::config::Config;

const NUDGE_CLASSIFIER_FILE: &str = "nudge.toml";
const BUNDLED_NUDGE_CLASSIFIER: &str = include_str!("classifiers/nudge.toml");

/// A nudge class the agentic loop knows how to act on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NudgeClass {
    /// The assistant is announcing an action it has not actually performed.
    PendingAction,
    /// A bare promise to gather evidence, with no answer or substantive finding.
    /// A refinement of PendingAction; ordinary action-pressure behavior is shared.
    DeferredAnswer,
    /// The assistant discovered prerequisite work that should revise the plan.
    PlanUpdate,
    /// The assistant appears to be giving a real final answer.
    FinalAnswer,
    /// No configured class was close enough to trust.
    Unknown,
}

/// Result of a nudge classifier pass.
#[derive(Debug, Clone, PartialEq)]
pub struct NudgeClassification {
    pub class: NudgeClass,
    pub score: f32,
}

impl NudgeClassification {
    pub fn is_pending_action(&self) -> bool {
        matches!(
            self.class,
            NudgeClass::PendingAction | NudgeClass::DeferredAnswer | NudgeClass::PlanUpdate
        )
    }

    pub fn is_plan_update(&self) -> bool {
        self.class == NudgeClass::PlanUpdate
    }
}

/// Config file shape for `~/.newt/classifiers/nudge.toml`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NudgeClassifierConfig {
    /// Schema version. Currently informational; future versions can migrate.
    #[serde(default = "default_classifier_version")]
    pub version: u32,
    /// Minimum similarity score required before a class is trusted.
    #[serde(default = "default_min_score")]
    pub min_score: f32,
    /// Minimum gap between the winning class and runner-up.
    #[serde(default = "default_min_margin")]
    pub min_margin: f32,
    /// Class definitions keyed by canonical class names:
    /// `pending_action`, `deferred_answer`, `plan_update`, `final_answer`.
    #[serde(default)]
    pub classes: BTreeMap<String, NudgeClassConfig>,
}

/// One nudge class: many input matchers share one output nudge.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct NudgeClassConfig {
    /// Prototype phrases that classify model output into this class.
    #[serde(default)]
    pub matchers: Vec<String>,
    /// Model-facing corrective direction sent when this class triggers an
    /// auto-continue path. Empty means "do not send a class-specific nudge".
    #[serde(default)]
    pub nudge: String,
}

impl Default for NudgeClassifierConfig {
    fn default() -> Self {
        bundled_nudge_config()
    }
}

fn default_classifier_version() -> u32 {
    1
}

fn default_min_score() -> f32 {
    0.28
}

fn default_min_margin() -> f32 {
    0.03
}

fn bundled_nudge_config() -> NudgeClassifierConfig {
    toml::from_str(BUNDLED_NUDGE_CLASSIFIER).expect("bundled nudge classifier template is valid")
}

fn bundled_nudge_classes() -> BTreeMap<String, NudgeClassConfig> {
    bundled_nudge_config().classes
}

impl NudgeClassifierConfig {
    /// Load only this config file. Missing files are treated as defaults; malformed
    /// files return an error so callers that explicitly asked to load can surface it.
    pub fn load_file(path: &Path) -> anyhow::Result<Self> {
        match std::fs::read_to_string(path) {
            Ok(text) => {
                let cfg: Self = toml::from_str(&text)?;
                Ok(cfg.with_builtin_fallbacks())
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
            Err(e) => Err(e.into()),
        }
    }

    /// Load from a classifier directory, e.g. `~/.newt/classifiers`.
    pub fn load_from_dir(dir: &Path) -> anyhow::Result<Self> {
        Self::load_file(&dir.join(NUDGE_CLASSIFIER_FILE))
    }

    fn with_builtin_fallbacks(mut self) -> Self {
        let builtins = bundled_nudge_classes();
        for (class, bundled) in builtins {
            self.classes
                .entry(class)
                .and_modify(|configured| {
                    if configured.matchers.is_empty() {
                        configured.matchers = bundled.matchers.clone();
                    }
                    if configured.nudge.trim().is_empty() {
                        configured.nudge = bundled.nudge.clone();
                    }
                })
                .or_insert(bundled);
        }
        if self.min_score <= 0.0 {
            self.min_score = default_min_score();
        }
        if self.min_margin < 0.0 {
            self.min_margin = default_min_margin();
        }
        self
    }
}

/// Configurable classifier for no-tool-call nudge decisions.
#[derive(Debug, Clone)]
pub struct NudgeClassifier {
    cfg: NudgeClassifierConfig,
}

impl Default for NudgeClassifier {
    fn default() -> Self {
        Self::builtin()
    }
}

impl NudgeClassifier {
    pub fn builtin() -> Self {
        Self {
            cfg: NudgeClassifierConfig::default(),
        }
    }

    pub fn from_config(cfg: NudgeClassifierConfig) -> Self {
        Self {
            cfg: cfg.with_builtin_fallbacks(),
        }
    }

    pub fn load_from_dir(dir: &Path) -> anyhow::Result<Self> {
        Ok(Self::from_config(NudgeClassifierConfig::load_from_dir(
            dir,
        )?))
    }

    /// Load the user nudge classifier from `~/.newt/classifiers/nudge.toml`.
    /// Unit tests use built-ins by default so local user config cannot make the
    /// harness tests nondeterministic; explicit load_from_dir tests still cover
    /// the file format.
    pub fn load_default() -> Self {
        #[cfg(test)]
        {
            Self::builtin()
        }
        #[cfg(not(test))]
        {
            let Some(dir) = classifier_config_dir() else {
                return Self::builtin();
            };
            match Self::load_from_dir(&dir) {
                Ok(classifier) => classifier,
                Err(e) => {
                    tracing::warn!(
                        path = %dir.join(NUDGE_CLASSIFIER_FILE).display(),
                        error = %e,
                        "failed to load nudge classifier config; using built-ins"
                    );
                    Self::builtin()
                }
            }
        }
    }

    pub fn classify(&self, text: &str) -> NudgeClassification {
        let query = tokens(text);
        if query.is_empty() {
            return NudgeClassification {
                class: NudgeClass::Unknown,
                score: 0.0,
            };
        }

        let query_words: Vec<_> = words(text).collect();
        let opening = &query_words[..query_words.len().min(3)];
        let deferred_prototypes: Vec<_> = self
            .cfg
            .classes
            .iter()
            .filter(|(name, _)| parse_nudge_class(name) == Some(NudgeClass::DeferredAnswer))
            .flat_map(|(_, cfg)| &cfg.matchers)
            .map(|example| tokens(example))
            .collect();
        let class_specificity = |class| {
            self.cfg
                .classes
                .iter()
                .filter(|(name, _)| parse_nudge_class(name) == Some(class))
                .flat_map(|(_, cfg)| &cfg.matchers)
                .filter(|example| {
                    class != NudgeClass::DeferredAnswer
                        || words(example).take(3).eq(opening.iter().cloned())
                })
                .map(|example| tokens(example))
                .filter(|prototype| {
                    class != NudgeClass::PendingAction || !deferred_prototypes.contains(prototype)
                })
                .map(|prototype| jaccard(&query, &prototype))
                .fold(0.0_f32, f32::max)
        };
        let mut scored: Vec<(NudgeClass, f32)> = self
            .cfg
            .classes
            .iter()
            .filter_map(|(class, class_cfg)| {
                let class = parse_nudge_class(class)?;
                // A subtype never competes with or expands the configured
                // coarse corpus or its ambiguity margin.
                if class == NudgeClass::DeferredAnswer {
                    return None;
                }
                let best = class_cfg
                    .matchers
                    .iter()
                    .map(|example| {
                        let prototype = tokens(example);
                        if class == NudgeClass::PendingAction {
                            // Boost recall only for a configured opening phrase;
                            // shared nouns alone still use whole-reply similarity.
                            let phrase: Vec<_> = words(example).take(3).collect();
                            if phrase.is_empty()
                                || !query_words.windows(phrase.len()).any(|part| part == phrase)
                            {
                                return jaccard(&query, &prototype);
                            }
                        }
                        prototype_similarity(&query, &prototype)
                    })
                    .fold(0.0_f32, f32::max);
                Some((class, best))
            })
            .collect();
        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        let Some((class, score)) = scored.first().copied() else {
            return NudgeClassification {
                class: NudgeClass::Unknown,
                score: 0.0,
            };
        };
        let runner_up = scored.get(1).map(|(_, s)| *s).unwrap_or(0.0);
        if score < self.cfg.min_score || (score - runner_up) < self.cfg.min_margin {
            return NudgeClassification {
                class: NudgeClass::Unknown,
                score,
            };
        }
        // Refine by specificity, not the broad prototype-recall score: a short
        // "Let me edit the file" prototype shares generic words with evidence
        // promises. Jaccard accounts for the whole reply and preserves concrete
        // findings. The subtype also requires its configured opening words:
        // negated blockers and answers followed by a promise stay broad.
        // Ambiguous cases and questions keep the broad class; as in prompt
        // intake, a question mark conservatively preserves a real ask.
        let class = if class == NudgeClass::PendingAction
            && !text.contains('?')
            && class_specificity(NudgeClass::DeferredAnswer)
                > class_specificity(NudgeClass::PendingAction) + self.cfg.min_margin
        {
            NudgeClass::DeferredAnswer
        } else {
            class
        };
        NudgeClassification { class, score }
    }

    pub fn is_pending_action(&self, text: &str) -> bool {
        self.classify(text).is_pending_action()
    }

    pub fn direction_for(&self, class: NudgeClass) -> Option<&str> {
        self.cfg
            .classes
            .get(nudge_class_key(class))
            .map(|class_cfg| class_cfg.nudge.as_str())
            .map(str::trim)
            .filter(|direction| !direction.is_empty())
            .or_else(|| match class {
                NudgeClass::DeferredAnswer => self.direction_for(NudgeClass::PendingAction),
                _ => None,
            })
    }
}

/// The classifier config root: `$NEWT_CONFIG_DIR/classifiers` or
/// `~/.newt/classifiers`.
pub fn classifier_config_dir() -> Option<PathBuf> {
    Config::user_config_dir().map(|dir| dir.join("classifiers"))
}

fn parse_nudge_class(s: &str) -> Option<NudgeClass> {
    match s.trim().to_ascii_lowercase().replace('-', "_").as_str() {
        "pending_action" | "continue" | "continuation" => Some(NudgeClass::PendingAction),
        "deferred_answer" => Some(NudgeClass::DeferredAnswer),
        "plan_update" | "update_plan" | "stale_plan" | "replan" => Some(NudgeClass::PlanUpdate),
        "final_answer" | "final" | "done" => Some(NudgeClass::FinalAnswer),
        _ => None,
    }
}

fn nudge_class_key(class: NudgeClass) -> &'static str {
    match class {
        NudgeClass::PendingAction => "pending_action",
        NudgeClass::DeferredAnswer => "deferred_answer",
        NudgeClass::PlanUpdate => "plan_update",
        NudgeClass::FinalAnswer => "final_answer",
        NudgeClass::Unknown => "unknown",
    }
}

fn words(s: &str) -> impl Iterator<Item = String> + '_ {
    s.split(|c: char| !c.is_alphanumeric())
        .filter(|raw| !raw.is_empty())
        .map(str::to_ascii_lowercase)
}

fn tokens(s: &str) -> BTreeSet<String> {
    words(s).filter(|t| t.len() >= 3).collect()
}

fn jaccard(a: &BTreeSet<String>, b: &BTreeSet<String>) -> f32 {
    if a.is_empty() || b.is_empty() {
        return 0.0;
    }
    let intersection = a.intersection(b).count() as f32;
    let union = a.union(b).count() as f32;
    if union == 0.0 {
        0.0
    } else {
        intersection / union
    }
}

fn prototype_similarity(query: &BTreeSet<String>, prototype: &BTreeSet<String>) -> f32 {
    if query.is_empty() || prototype.is_empty() {
        return 0.0;
    }
    let overlap = query.intersection(prototype).count() as f32;
    let prototype_recall = overlap / prototype.len() as f32;
    jaccard(query, prototype).max(prototype_recall)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn unfinished_summary_and_tool_promises_are_pending_actions() {
        let classifier = NudgeClassifier::builtin();
        for text in [
            "This remains a large file. Let me summarize the current state and what could be improved:",
            "Let me check the current working directory and available git operations:",
        ] {
            assert!(classifier.is_pending_action(text), "{text}: {:?}", classifier.classify(text));
        }
        assert!(!classifier.is_pending_action("The files were examined; everything checks out."));
    }

    #[test]
    fn pending_handoffs_preserve_action_and_object_paraphrases() {
        let classifier = NudgeClassifier::builtin();
        for text in [
            "Next steps: replace the redundant wrapper, then run the check.",
            "Next steps: remove the duplicate helper, then run cargo check.",
        ] {
            assert!(
                classifier.is_pending_action(text),
                "{text}: {:?}",
                classifier.classify(text)
            );
        }
    }

    #[test]
    fn embedded_verification_promises_preserve_action_paraphrases() {
        let classifier = NudgeClassifier::builtin();
        for text in [
            "The issue is understood. Let me inspect the code before running the check.",
            "Now I understand the issue. Let me verify by looking at format_rollup_detail.",
        ] {
            assert!(
                classifier.is_pending_action(text),
                "{text}: {:?}",
                classifier.classify(text)
            );
        }
    }

    #[test]
    fn reports_and_partial_answers_do_not_become_recovery_promises() {
        let classifier = NudgeClassifier::builtin();
        for text in [
            "partial answer before the break",
            "First I checked the build and verified the result. The work is complete; the tests pass.",
            "Let me summarize the completed work: the file was edited, the tests passed, and the requested result is ready.",
            "The next steps are complete: the duplicate helper was removed and the check passed.",
        ] {
            assert!(!classifier.is_pending_action(text), "{text}: {:?}", classifier.classify(text));
        }
    }

    #[test]
    fn completed_check_report_is_not_a_deferred_promise() {
        let report = "Both operations executed and returned real results this turn.

## Restart Smoke Check

| Check | Result |
|---|---|
| `scrybe__state` (read-only) | MCP reachable — live state returned: active tab `report.md` (not dirty), view `both`, theme `default`, vim off, wrap on. **4 open tabs** now (the original dashboard, the project board note, the companion DRAFT, and v2). |
| `git rev-parse --is-inside-work-tree` | **`true`** — exit 0; `/workspace/project` is a valid work tree, so the `git` executable and filesystem grants are intact after restart. |

Both permanent grants (MCP and exec/fs) survived the restart. Nothing opened, edited, tested, queried, or sent. Stopping here.";
        let classifier = NudgeClassifier::builtin();
        assert!(
            !classifier.is_pending_action(report),
            "shared check/state/git nouns are not a promise: {:?}",
            classifier.classify(report)
        );
    }

    #[test]
    fn nudge_classifier_builtin_matches_known_phrases() {
        let classifier = NudgeClassifier::builtin();
        assert!(classifier.is_pending_action(
            "Plan is current — no update needed. Continuing with step 2: inserting the progressive dispatch into lib.rs."
        ));
        assert!(classifier.is_pending_action(
            "\
Summary

Progress so far: Step 2 is in progress.

Current blocker: The for loop needs to iterate over the slice directly.
The fix is to use for entry in page.entries.iter().

Next steps needed:
1. Fix the iteration type error in lib.rs.
2. Add tests.
3. Run just check."
        ));
        assert!(classifier.is_pending_action("Now I'll add the --home flag to the Cli struct."));
        assert!(classifier.is_pending_action("Let me keep editing now."));
        assert!(classifier.is_pending_action(
            "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."
        ));
        assert!(classifier.is_pending_action(
            "I have two issues: duplicate topic_has_rollups and a stray brace. Let me fix both — read around 490 to see what needs removing, then verify with a build check."
        ));
        assert!(!classifier.is_pending_action("The capital of France is Paris."));
        assert!(!classifier.is_pending_action("Done. Let me know if you want any further changes."));
        assert!(!classifier.is_pending_action(
            "The duplicate helper definitions and stray brace were removed, and the build check passed."
        ));
        assert!(
            classifier
                .direction_for(NudgeClass::PendingAction)
                .is_some_and(|d| d.contains("emit the tool call now")),
            "bundled classifier should carry output direction text"
        );
    }

    #[test]
    fn nudge_classifier_refines_evidence_promises_without_changing_action_corpus() {
        let classifier = NudgeClassifier::builtin();
        for key in ["pending_action", "deferred_answer"] {
            for text in &classifier.cfg.classes[key].matchers {
                assert!(classifier.is_pending_action(text), "{key}: {text}");
            }
        }
        assert_eq!(classifier.classify(
            "Let me check the local branches and the top-level remote remotes to be complete."
        ).class, NudgeClass::DeferredAnswer);
        for text in [
            "Let me check the current working directory and available git operations.",
            "Let me summarize the current state and what could be improved.",
            "Let me verify the code before I give the answer.",
        ] {
            assert_eq!(
                classifier.classify(text).class,
                NudgeClass::DeferredAnswer,
                "an evidence-only promise must remain recoverable on read-only turns: {text}"
            );
        }
        for text in [
            "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.",
            "Next I will add the tests.",
        ] {
            assert_eq!(classifier.classify(text).class, NudgeClass::PendingAction, "{text}");
        }
        assert_eq!(
            classifier.direction_for(NudgeClass::DeferredAnswer),
            classifier.direction_for(NudgeClass::PendingAction)
        );
        let mut config = NudgeClassifierConfig::default();
        config.classes.get_mut("deferred_answer").unwrap().matchers =
            vec!["Checking the ledger comes before answering your question.".to_string()];
        config
            .classes
            .get_mut("pending_action")
            .unwrap()
            .matchers
            .push("Checking the ledger comes before answering your question.".to_string());
        assert_eq!(
            NudgeClassifier::from_config(config)
                .classify("Checking the ledger comes before answering your question.")
                .class,
            NudgeClass::DeferredAnswer,
            "refinement uses the same configurable data"
        );
    }

    #[test]
    fn nudge_classifier_refinement_requires_the_configured_opening() {
        let classifier = NudgeClassifier::builtin();
        for text in [
            "There are three local branches. Let me check the current implementation and identify any gaps.",
            "Let me be clear: I cannot check the current implementation because access is unavailable.",
            "Do not let me check the current implementation and identify any gaps.",
            "The earlier reply said 'Let me check the current implementation and identify any gaps.'",
        ] {
            assert_ne!(classifier.classify(text).class, NudgeClass::DeferredAnswer, "{text}");
        }
    }

    #[test]
    fn nudge_classifier_refines_legacy_defaults() {
        let mut legacy = NudgeClassifierConfig::default();
        let evidence = legacy.classes.remove("deferred_answer").unwrap().matchers;
        let pending = &mut legacy.classes.get_mut("pending_action").unwrap().matchers;
        for example in &evidence {
            if !pending.contains(example) {
                pending.push(example.clone());
            }
        }
        // An old copied template has the evidence prototypes in PendingAction,
        // no subtype class, and may use different casing or punctuation.
        pending.push(evidence[0].to_uppercase());
        let classifier = NudgeClassifier::from_config(legacy.clone());
        for example in &evidence {
            assert_eq!(
                classifier.classify(example).class,
                NudgeClass::DeferredAnswer,
                "old copied default: {example}"
            );
        }
    }

    #[test]
    fn nudge_classifier_preserves_custom_coarse_corpus() {
        let mut legacy = NudgeClassifierConfig::default();
        let evidence = legacy.classes.remove("deferred_answer").unwrap().matchers;
        legacy.classes.get_mut("pending_action").unwrap().matchers =
            vec!["Proceeding with the patch by editing the target file.".to_string()];
        let before = NudgeClassifier {
            cfg: legacy.clone(),
        };
        let after = NudgeClassifier::from_config(legacy);
        for example in evidence {
            let previous = before.classify(&example);
            let mut current = after.classify(&example);
            if current.class == NudgeClass::DeferredAnswer {
                current.class = NudgeClass::PendingAction;
            }
            assert_eq!(
                current, previous,
                "subtype fallback must not expand a configured action corpus"
            );
        }
    }

    #[test]
    fn pending_action_recall_preserves_custom_exact_and_paraphrased_promises() {
        let mut config = NudgeClassifierConfig::default();
        config.classes.get_mut("pending_action").unwrap().matchers =
            vec!["Proceeding with the patch by editing the target file.".to_string()];
        let classifier = NudgeClassifier::from_config(config);
        for text in [
            "Proceeding with the patch by editing the target file.",
            "I am editing the target file for this patch.",
            "The location is confirmed. Proceeding with the patch by editing the target file.",
        ] {
            assert!(
                classifier.is_pending_action(text),
                "{text}: {:?}",
                classifier.classify(text)
            );
        }
    }

    #[test]
    fn nudge_classifier_separates_plan_update_summaries_from_final_summaries() {
        let classifier = NudgeClassifier::builtin();
        let findings = "\
Summary of Findings

Across the tool calls, I observed two issues in newt-tui/src/help_sections.rs:
1. Duplicate function
2. Stray closing brace

Current Status

The build is broken due to these syntax errors. The plan was at step 2, but we need to fix the immediate compilation issues first.

Next Steps Required

To continue, I would need to remove the duplicate function using edit_file, remove the stray brace using edit_file, verify cargo check, then proceed with step 2 of the plan.

However, I've reached the tool-call limit and cannot make these edits now.";

        let classified = classifier.classify(findings);
        assert_eq!(classified.class, NudgeClass::PlanUpdate);
        assert!(classified.is_pending_action());
        assert!(classified.is_plan_update());

        let resume_handoff = "\
Summary

I reached the tool-call limit. Current state of newt-tui/src/help_sections.rs:
duplicate topic_has_rollups and rollup_page_for_topic definitions need to be removed.

Recommended next action if session resumes:
1. Fix the duplicate functions.
2. Clean up the broken test block.
3. Read lib.rs and wire the progressive dispatch.

The build is currently broken due to the duplicate definitions — that's the blocker for any further progress.";
        assert_eq!(
            classifier.classify(resume_handoff).class,
            NudgeClass::PlanUpdate
        );

        let final_summary =
            classifier.classify("Here is a summary of what I found across the tool calls.");
        assert_eq!(final_summary.class, NudgeClass::FinalAnswer);
    }

    #[test]
    fn nudge_classifier_loads_dropin_from_classifiers_dir() {
        let dir = tempfile::tempdir().unwrap();
        let classifiers = dir.path().join("classifiers");
        std::fs::create_dir_all(&classifiers).unwrap();
        std::fs::write(
            classifiers.join(NUDGE_CLASSIFIER_FILE),
            r#"
version = 1
min_score = 0.20
min_margin = 0.01

[classes.pending_action]
matchers = ["Proceeding with the patch by editing the target file."]
nudge = "Call the edit tool now."

[classes.final_answer]
matchers = ["No changes are needed."]
"#,
        )
        .unwrap();

        let classifier = NudgeClassifier::load_from_dir(&classifiers).unwrap();
        assert!(
            classifier.is_pending_action("Proceeding with the patch by editing the target file.")
        );
        assert!(!classifier.is_pending_action("No changes are needed."));
        assert_eq!(
            classifier.direction_for(NudgeClass::PendingAction),
            Some("Call the edit tool now.")
        );
        assert!(
            classifier
                .direction_for(NudgeClass::PlanUpdate)
                .is_some_and(|d| d.contains("Update the plan first")),
            "partial user configs still inherit bundled nudges"
        );
    }

    #[test]
    fn classifier_dir_is_under_user_config_dir() {
        let dir = classifier_config_dir().unwrap_or_else(|| PathBuf::from(".newt/classifiers"));
        assert!(dir.ends_with("classifiers"));
    }
}

/// Does this USER turn invite harness action-pressure at all? (#1152/#1162)
///
/// The nudge classifier reads only the ASSISTANT's words, so it cannot tell
/// "dodging work" from "answering a question" — a model answering *"give me
/// your top 5 improvements"* classifies identically to one narrating instead
/// of fixing a bug. This gate supplies the missing half: action-pressure
/// nudges (narration rescue, workflow repair steering, pending-plan pushes)
/// fire ONLY when the user's turn actually asked for actions.
///
/// Rules (conservative — unknown shapes default to TODAY's behavior, true):
/// - Social acknowledgments ("thanks!", "excellent!", "lgtm") → false.
/// - Reporting/analysis imperatives ("give me…", "show…", "explain…",
///   "review…", "what do you think…") → false: narration IS the deliverable.
/// - Questions default to false UNLESS they name a mutating action
///   ("can you push the branch?" → true).
/// - Everything else → true.
pub fn user_turn_invites_action(task: &str) -> bool {
    let t = task.trim().trim_end_matches(['!', '.', ' ']).to_lowercase();
    if t.is_empty() {
        return true; // continuation/unknown — keep today's behavior
    }
    // Social acknowledgment: short, and led by a pleasantry.
    const SOCIAL: &[&str] = &[
        "thanks",
        "thank you",
        "ok",
        "okay",
        "great",
        "excellent",
        "nice",
        "cool",
        "awesome",
        "perfect",
        "good",
        "lgtm",
        "sounds good",
        "got it",
        "you're welcome",
        "no worries",
    ];
    if t.len() <= 60 && SOCIAL.iter().any(|w| t.starts_with(w)) {
        return false;
    }
    // Judge the LEADING verb (after politeness prefixes) — a verb buried in a
    // descriptive phrase ("…to make X better") is not the ask's deliverable.
    let mut lead = t.as_str();
    for prefix in [
        "please ",
        "can you ",
        "could you ",
        "would you ",
        "will you ",
        "now ",
    ] {
        if let Some(rest) = lead.strip_prefix(prefix) {
            lead = rest;
        }
    }
    // Leading verbs whose deliverable is a MUTATION — invite action even
    // inside a question ("can you push…?").
    const MUTATING: &[&str] = &[
        "fix",
        "push",
        "commit",
        "edit",
        "create",
        "make",
        "write",
        "build",
        "run ",
        "install",
        "deploy",
        "merge",
        "delete",
        "remove",
        "refactor",
        "implement",
        "add ",
        "apply",
        "rebase",
        "rename",
        "move",
        "update",
        "patch",
        "revert",
        "release",
        "publish",
        "start",
        "stop",
        "restart",
        "clean",
        "format",
        "test ",
    ];
    let leads_mutating = MUTATING.iter().any(|v| lead.starts_with(v));
    // Leading verbs whose deliverable is TEXT — narration is the answer.
    const REPORTING: &[&str] = &[
        "give me",
        "show",
        "list",
        "tell me",
        "explain",
        "summarize",
        "summarise",
        "describe",
        "analyze",
        "analyse",
        "review",
        "compare",
        "what",
        "which",
        "why",
        "how",
        "who",
        "where",
        "assess",
        "evaluate",
        "your top",
        "your take",
        "thoughts on",
        "read ",
    ];
    let leads_reporting = REPORTING.iter().any(|v| lead.starts_with(v));
    if leads_reporting {
        return false;
    }
    if t.ends_with('?') {
        // A question invites action only when it LEADS with a mutation.
        return leads_mutating;
    }
    true
}

#[cfg(test)]
mod invites_action_tests {
    use super::user_turn_invites_action;

    #[test]
    fn transcript_cases_from_the_2026_07_14_session() {
        // The misfires (#1152/#1162) — none of these may invite action-pressure:
        assert!(!user_turn_invites_action("excellent! Thank you!"));
        assert!(!user_turn_invites_action("thank you!"));
        assert!(!user_turn_invites_action(
            "Give me your top 5 improvements to make LLM effectiveness better inside this harness please?"
        ));
        assert!(!user_turn_invites_action(
            "what do you think of the MCP integration story?"
        ));
        assert!(!user_turn_invites_action(
            "review the OCAP confinement model"
        ));
        // Genuine action asks — pressure stays available:
        assert!(user_turn_invites_action(
            "Make a branch for me to examine with your commits on them please."
        ));
        assert!(user_turn_invites_action(
            "can you push the branch to origin for me?"
        ));
        assert!(user_turn_invites_action(
            "fix the failing test in backend_probe"
        ));
        // Unknown/imperative default = today's behavior:
        assert!(user_turn_invites_action("continue"));
        assert!(user_turn_invites_action(""));
    }
}

/// Phrase table for [`is_bare_continuation`]: operator nudges that carry no
/// objective of their own. Knowledge is data (three Cs) — extend here, not in
/// logic. Deliberately conservative: ambiguous acknowledgments ("ok", "good")
/// are NOT continuations; a fresh prompt is always the safe default.
const BARE_CONTINUATION_PHRASES: &[&str] = &[
    "continue",
    "please continue",
    "continue please",
    "yes continue",
    "keep going",
    "go on",
    "carry on",
    "resume",
    "proceed",
    "go ahead",
    "next",
    "finish",
    "finish up",
    "keep at it",
    "don't stop",
    "onward",
    // #2332 / #2283: retry nudges after a failed or capped turn.
    "retry",
    "try again",
    "try now",
    // What is left of "let's go" / "let's land it" once `prompt_intake`'s
    // collaborative opener is stripped: a nudge, not a proposal. The other
    // consumer, `chat.rs`'s `upgrade_origin_for_interrupted_objective`, then
    // links a bare "go" to the interrupted objective — the intended reading
    // there too.
    "go",
    "land it",
    "ship it",
    "do it",
];

/// Is this operator input a BARE continuation — a nudge like "continue" /
/// "keep going" / "1: proceed" that carries no objective of its own?
///
/// bug/steering-regressions iteration #2: when a round cap interrupts an
/// objective mid-flight, the operator's natural reply is a bare nudge. Minting
/// that nudge as a FRESH prompt makes it the active operator prompt — the
/// compression-immune card then protects the word "continue" while the real
/// task drifts into the summarizable middle (observed live 2026-07-27: the
/// agent re-read an 11.7k-line file from line 1 with zero goal). Chat re-links
/// a bare continuation to the interrupted objective's lineage instead, so
/// fix #1's authority walk keeps the REAL task active.
///
/// Conservative by construction: multi-line input, anything longer than a
/// short nudge, or any phrase not in the table is NOT a continuation — a
/// substantive new ask must never be silently chained to a stale objective.
/// A leading decision ordinal ("1: proceed", "2. continue") is stripped so
/// decision-surface replies match.
pub fn is_bare_continuation(input: &str) -> bool {
    let t = input.trim();
    if t.is_empty() || t.contains('\n') || t.chars().count() > 48 {
        return false;
    }
    // A trailing `?` too: "try now?" asks to resume, it does not ask a question
    // of its own (#2332). Only table phrases match, so "what should I try now?"
    // stays fresh.
    let t = t.trim_end_matches(['.', '!', '?', '…', ' ']).to_lowercase();
    // Strip one leading decision ordinal: "1:", "2.", "3)" — the shape the
    // decision-surface asks for ("reply using an explicit ordinal").
    let t = match t.split_once([':', '.', ')']) {
        Some((head, rest)) if !head.is_empty() && head.chars().all(|c| c.is_ascii_digit()) => {
            rest.trim().to_string()
        }
        _ => t,
    };
    BARE_CONTINUATION_PHRASES.iter().any(|p| *p == t)
}

#[cfg(test)]
mod bare_continuation_tests {
    use super::is_bare_continuation;

    #[test]
    fn bare_nudges_match() {
        for input in [
            "continue",
            "Continue.",
            "  keep going  ",
            "go ahead!",
            "resume",
            "1: proceed",
            "2. continue",
            "don't stop",
            // #2332: the recorded retry nudges, including question-shaped ones.
            "retry",
            "retry?",
            "try again",
            "try again?",
            "try now?",
            "continue?",
            // The remainder of a collaborative opener ("let's go").
            "go",
            "land it",
            "ship it",
            "do it",
        ] {
            assert!(is_bare_continuation(input), "{input:?} must link");
        }
    }

    #[test]
    fn substantive_or_ambiguous_input_stays_fresh() {
        for input in [
            "",
            "ok",
            "thanks",
            "continue with the newt-tui refactor instead",
            "extract the next largest module and open a PR",
            "continue\nand also fix the tests",
            "proceed to delete the production database",
            "retry the build with --release",
            "try again tomorrow?",
            "what should I try now?",
        ] {
            assert!(!is_bare_continuation(input), "{input:?} must stay fresh");
        }
    }
}