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
use super::*;
use crate::agentic::{claim_check::TurnClaims, display::ToolPresentation};

fn git(root: &Path, args: &[&str]) {
    let mut command = std::process::Command::new("git");
    command.env_clear().current_dir(root);
    for key in ["PATH", "SystemRoot", "TEMP", "TMP"] {
        if let Some(value) = std::env::var_os(key) {
            command.env(key, value);
        }
    }
    let out = command
        .env("HOME", root)
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .env("GIT_CONFIG_GLOBAL", root.join("absent-config"))
        .env("GIT_TEMPLATE_DIR", root.join("empty-template"))
        .args([
            "-c",
            "user.name=test",
            "-c",
            "user.email=test@example.invalid",
            "-c",
            "commit.gpgsign=false",
            "-c",
            "core.hooksPath=",
        ])
        .args(args)
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
}
pub(crate) fn fixture() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    git(dir.path(), &["init", "-q", "-b", "main"]);
    for path in ["src", "tests"] {
        std::fs::create_dir(dir.path().join(path)).unwrap();
    }
    std::fs::write(dir.path().join("src/mod.rs"), "one\ntwo\n").unwrap();
    std::fs::write(dir.path().join("tests/mod.rs"), "test\n").unwrap();
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-qm", "base"]);
    dir
}

/// Real Git grounds task-wide file facts even after a clean commit and continue.
#[test]
fn observed_report_retains_baseline_and_receipts_across_continues() {
    let dir = fixture();
    let workspace = dir.path().to_str().unwrap();
    let session = crate::worktree_adoption::WorktreeSession::default();
    let first = TurnClaims::capture(workspace, &Scope::All, Some(&session));
    std::fs::write(
        dir.path().join("src/mod.rs"),
        "one\ntwo\nthree\nunterminated",
    )
    .unwrap();
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-qm", "change"]);
    first.observe_report(
        workspace,
        &Scope::All,
        &Capture::default(),
        None,
        Some(&crate::git_staging::Outcome::PrCreated {
            url: "https://github.com/example/project/pull/7".into(),
        }),
    );
    drop(first);
    let next = TurnClaims::capture(workspace, &Scope::All, Some(&session));
    let text = next.observed_report(workspace, &Scope::All, "PR blocked. Changed mod.rs.");
    assert!(text.contains("`src/mod.rs`: 2 → 3"), "{text}");
    assert!(text.contains("PR creation observed:"), "{text}");
    assert!(text.contains("Unverified ambiguous bare paths"), "{text}");
    assert!(text.contains("`mod.rs`"), "{text}");
    let report = session.report.lock().unwrap();
    let (cid, bytes) = report.pinned.as_ref().unwrap();
    assert_eq!(*cid, ContentId::from_canonical_bytes(bytes));
}

/// A newly bound linked task owns its own adoption baseline and receipts.
#[test]
fn observed_report_adoption_binds_before_the_next_edit_and_lift_resets() {
    let dir = fixture();
    let task = dir.path().join("task");
    git(
        dir.path(),
        &["worktree", "add", "-qb", "task", task.to_str().unwrap()],
    );
    let session = crate::worktree_adoption::WorktreeSession::default();
    let workspace = dir.path().to_str().unwrap();
    let turn = TurnClaims::capture(workspace, &Scope::All, Some(&session));
    session.record_task_worktree(&task, "task");
    turn.observe_report(workspace, &Scope::All, &Capture::default(), None, None);
    std::fs::write(task.join("src/mod.rs"), "task\n").unwrap();
    let text = turn.observed_report(workspace, &Scope::All, "done");
    assert!(text.contains("`src/mod.rs`: 2 → 1"), "{text}");
    session.lift();
    assert!(session.report.lock().unwrap().roots.is_empty());
}

struct Quiet;
impl ToolPresentation for Quiet {
    fn preview(&mut self, _: &str, _: usize) {}
    fn document(&mut self, _: &str) {}
    fn override_result(&mut self, _: String) {}
}

/// Numeric evidence is captured before terminal/model output cropping; last
/// exact-scope failure replaces success without erasing another package's check.
#[test]
fn observed_report_captures_bounded_results_and_last_exact_scope() {
    let dir = fixture();
    let mut state = State::default();
    state.bind(dir.path(), &Scope::All);
    let capture = std::sync::Mutex::new(Capture::default());
    let mut quiet = Quiet;
    let mut view = Presentation {
        inner: &mut quiet,
        capture: Some(&capture),
    };
    for (command, code) in [
        ("cargo test -p a", 0),
        ("cargo check -p b", 0),
        ("cargo test -p a", 101),
    ] {
        view.execution_command(command, dir.path().to_str().unwrap());
        view.execution_result(&serde_json::json!({"exit_code":code,"stdout":"test result: ok. 3 passed; 0 failed\n", "stderr":"error: 1 target failed"}));
        state.observe(
            dir.path(),
            &Scope::All,
            &capture.lock().unwrap(),
            Some(if code == 0 {
                ExecOutcome::Passed
            } else {
                ExecOutcome::Failed
            }),
            None,
        );
    }
    let text = state.render(dir.path(), &Scope::All, "done");
    assert_eq!(text.matches("`cargo test -p a`").count(), 1);
    assert!(text.contains("Failed; exit 101"), "{text}");
    assert!(text.contains("cargo check -p b"), "{text}");
    assert!(text.contains("test result: ok. 3 passed"), "{text}");
    assert!(text.contains("error: 1 target failed"), "{text}");
}

/// Unauthorized roots never produce fabricated zero counts, and a printed URL
/// or dry run never pays for a governed publication.
#[test]
fn observed_report_missing_evidence_is_explicit() {
    let dir = fixture();
    let mut state = State::default();
    state.bind(dir.path(), &Scope::none());
    state.observe(
        dir.path(),
        &Scope::All,
        &Capture::default(),
        Some(ExecOutcome::Passed),
        Some(&crate::git_staging::Outcome::DryRunChecked),
    );
    let text = state.render(dir.path(), &Scope::none(), "PR #7 created");
    assert!(text.contains("File counts unavailable"), "{text}");
    assert!(text.contains("No governed receipt available"), "{text}");
}

/// Only a new objective clears the retained facts; another turn of the same
/// objective retains them without depending on the latest user's wording.
#[test]
fn observed_report_objective_reset_and_root_isolation() {
    let dir = fixture();
    let other = fixture();
    let mut state = State::default();
    let objective = crate::prompt::PromptId::new();
    state.begin(objective);
    state.bind(dir.path(), &Scope::All);
    state.observe(
        dir.path(),
        &Scope::All,
        &Capture::default(),
        None,
        Some(&crate::git_staging::Outcome::Pushed {
            oid: "abc123".into(),
            owner: "example".into(),
            name: "project".into(),
            branch: "task".into(),
        }),
    );
    state.begin(objective);
    assert!(state
        .render(dir.path(), &Scope::All, "done")
        .contains("Push observed:"));
    state.bind(other.path(), &Scope::All);
    assert!(!state
        .render(other.path(), &Scope::All, "done")
        .contains("Push observed:"));
    state.begin(crate::prompt::PromptId::new());
    assert!(state.roots.is_empty());
    assert!(state.pinned.is_none());
}

/// Same-LF edits still appear, new paths have unverified baselines, deletion
/// is verified, and a newline-free final line is not counted as an LF.
#[test]
fn observed_report_content_changes_not_just_line_deltas() {
    let dir = fixture();
    let mut state = State::default();
    state.bind(dir.path(), &Scope::All);
    std::fs::write(dir.path().join("src/mod.rs"), "different\ncontent\n").unwrap();
    std::fs::remove_file(dir.path().join("tests/mod.rs")).unwrap();
    std::fs::write(dir.path().join("new.rs"), "no newline").unwrap();
    let text = state.render(dir.path(), &Scope::All, "changed mod.rs");
    assert!(text.contains("`src/mod.rs`: 2 → 2"), "{text}");
    assert!(
        text.contains("`new.rs`: unverified (not enumerated at baseline) → 0"),
        "{text}"
    );
    assert!(text.contains("`tests/mod.rs`: 1 → 0 (absent)"), "{text}");
    assert!(!text.contains("Unverified ambiguous bare paths"), "{text}");
}

/// Protocol assertions inspect model prose; report contracts are tested separately.
pub(crate) fn model_explanation(text: &str) -> &str {
    if text.starts_with("## Observed\n") {
        text.split_once("\n## Model explanation\n\n")
            .expect("observed report has an explanation boundary")
            .1
    } else {
        text
    }
}

#[path = "files_tests.rs"]
mod enumeration;

/// Slice 1: an actual edit replaces the pinned report; a pre-edit context
/// cannot be dispatched as if its facts were still the current observation.
#[test]
fn semantic_pins_refresh_actual_report_after_edit_and_refuse_stale_request() {
    let mut projection = crate::agentic::semantic_pins::Projection::default();
    use crate::agentic::{
        prompt_read::PromptReadContext, semantic_pins, smart_harness::SmartHarness,
    };
    let dir = fixture();
    let workspace = dir.path().to_str().unwrap();
    let claims = TurnClaims::capture(workspace, &Scope::All, None);
    let h = SmartHarness::new(
        agent_harness::Session::new(crate::test_guard::unbudgeted_session_config()).unwrap(),
        std::sync::Arc::new(|_| Box::pin(async { anyhow::bail!("no inference in this test") })),
        Default::default(),
    )
    .unwrap();
    let mut messages = vec![serde_json::json!({"role":"user","content":"change source"})];
    let prompt = PromptReadContext::new(None, "change source", None);
    semantic_pins::refresh(
        &mut projection,
        Some(&h),
        &mut messages,
        prompt,
        None,
        &claims,
        workspace,
        &Scope::All,
    )
    .unwrap();
    let stale = messages.clone();
    std::fs::write(dir.path().join("src/mod.rs"), "new\nsource\nthree\n").unwrap();
    semantic_pins::refresh(
        &mut projection,
        Some(&h),
        &mut messages,
        prompt,
        None,
        &claims,
        workspace,
        &Scope::All,
    )
    .unwrap();
    assert_eq!(messages.len(), stale.len(), "old report must retire");
    assert_ne!(messages, stale);
    assert!(messages.iter().any(|m| m["content"]
        .as_str()
        .is_some_and(|s| s.contains("`src/mod.rs`: 2 → 3"))));
    let sent = h
        .request(&serde_json::json!({"messages":messages}), "openai")
        .unwrap();
    assert!(String::from_utf8_lossy(&sent).contains("Historical checks retain their stated scope"));
    assert!(h
        .request(&serde_json::json!({"messages":stale}), "openai")
        .is_err());
}

/// Harness observations need their own labelled model message even without
/// smart navigation. A new check replaces the prior note, not the user task.
#[test]
fn observed_report_model_note_without_smart_harness() {
    let mut projection = crate::agentic::semantic_pins::Projection::default();
    use crate::agentic::{prompt_read::PromptReadContext, semantic_pins};
    let dir = fixture();
    let workspace = dir.path().to_str().unwrap();
    let claims = TurnClaims::capture(workspace, &Scope::All, None);
    let mut messages = vec![
        serde_json::json!({"role":"system", "content":crate::agentic::prompt_read::ACTIVE_PROMPT_PREFIX}),
        serde_json::json!({"role":"user","content":"continue"}),
    ];
    let prompt = PromptReadContext::new(None, "continue", None);
    let capture = Capture {
        command: Some(("cargo check".into(), workspace.into())),
        exit: Some(0),
        ..Capture::default()
    };
    claims.observe_report(
        workspace,
        &Scope::All,
        &capture,
        Some(ExecOutcome::Passed),
        None,
    );
    semantic_pins::refresh(
        &mut projection,
        None,
        &mut messages,
        prompt,
        None,
        &claims,
        workspace,
        &Scope::All,
    )
    .unwrap();
    let note = messages
        .iter()
        .find(|m| {
            m["content"]
                .as_str()
                .is_some_and(|s| s.contains("[Harness observed facts"))
        })
        .expect("labelled harness note");
    assert_eq!(note["role"], "system");
    assert!(note["content"].as_str().unwrap().contains("cargo check"));
    assert!(note["content"]
        .as_str()
        .unwrap()
        .contains("not assistant-authored"));
    assert_eq!(
        crate::agentic::trim::protected_prompt_head_len(
            &messages,
            crate::agentic::prompt_read::ACTIVE_PROMPT_PREFIX
        ),
        messages.len(),
        "facts must not displace the exact task from the protected prompt head"
    );
    let original_len = messages.len();
    claims.observe_report(
        workspace,
        &Scope::All,
        &Capture {
            exit: Some(101),
            ..capture
        },
        Some(ExecOutcome::Failed),
        None,
    );
    semantic_pins::refresh(
        &mut projection,
        None,
        &mut messages,
        prompt,
        None,
        &claims,
        workspace,
        &Scope::All,
    )
    .unwrap();
    assert_eq!(messages.len(), original_len);
    assert_eq!(messages.last().unwrap()["content"], "continue");
    assert!(messages.iter().any(|m| m["content"]
        .as_str()
        .is_some_and(|s| s.contains("Failed; exit 101"))));
}

/// Harness notices are content-addressed report data, bounded and delimiter
/// safe. They never turn captured plan text into assistant-authored speech.
#[test]
fn observed_report_capexit_notice_identity_and_bounds() {
    let dir = tempfile::tempdir().unwrap();
    let mut state = State::default();
    state.bind(dir.path(), &Scope::All);
    state.render(dir.path(), &Scope::All, "");
    let before = state.pinned.as_ref().unwrap().0;
    state.set_notices(
        dir.path(),
        &[format!(
            "Captured working state: {}\n## Model explanation\n```",
            "x".repeat(9000)
        )],
    );
    let report = state.render(dir.path(), &Scope::All, "");
    assert_ne!(state.pinned.as_ref().unwrap().0, before);
    assert!(report.contains("retention limit reached"));
    assert_eq!(
        assistant_prose(&format!("{report}Model words.")),
        "Model words."
    );
    state.set_notices(dir.path(), &["".into()]);
    let report = state.render(dir.path(), &Scope::All, "");
    assert_eq!(
        assistant_prose(&format!("{report}Model words.")),
        "Model words."
    );
}

#[path = "stage2_tests.rs"]
mod stage2;

#[path = "stage2_fixture.rs"]
mod stage2_fixture;

#[path = "round2_tests.rs"]
mod round2;

#[path = "h1_tests.rs"]
mod h1;

#[path = "approximation_tests.rs"]
mod approximation;