newt-coder 0.6.2

Coder plugin for newt-agent: whole-file emit + server-side diff normalization
Documentation
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
//! End-to-end smoke for [`newt_coder::Coder`].
//!
//! Wires a real `MockBackend` (canned reply) through the full
//! orchestrator: `build_prompt` -> `complete` -> `normalize_emission`
//! -> apply. The mock replies with the S5 whole-file shape that the
//! 2026-05-29 bake-off (wf_ecc784ea-aa2) showed qwen3-coder:30b
//! produces; this verifies newt-coder lands the edit on the workspace
//! given that shape — closing failure mode T0b end-to-end.

use std::path::Path;
use std::sync::Arc;

use async_trait::async_trait;
use newt_coder::{Coder, CoderError};
use newt_core::router::Tier;
use newt_core::{Caveats, CountBound, Scope};
use newt_inference::backend::{ChatReply, ChatRequest, InferenceBackend};
use tempfile::TempDir;
use tests_common::MockBackend;

fn write_file(path: &Path, contents: &str) {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).unwrap();
    }
    std::fs::write(path, contents).unwrap();
}

#[tokio::test]
async fn coder_lands_whole_file_rename_end_to_end() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("src/lib.rs"), "pub fn greet() {}\n");

    // Canned reply in the S5 shape: rename greet -> hello.
    let canned = "FILE: src/lib.rs\npub fn hello() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;

    let coder = Coder::new(backend);
    let run = coder
        .run(
            tmp.path(),
            "Rename greet to hello in src/lib.rs",
            &Caveats::top(),
        )
        .await
        .unwrap();

    assert_eq!(run.emission_shape, "whole_files");
    assert_eq!(run.files_written, vec!["src/lib.rs".to_string()]);
    let content = std::fs::read_to_string(tmp.path().join("src/lib.rs")).unwrap();
    assert_eq!(content, "pub fn hello() {}");
}

#[tokio::test]
async fn coder_reports_prose_shape_when_model_emits_no_structure() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("src/lib.rs"), "pub fn greet() {}\n");

    // T0a-style reply: pure prose, no structure.
    let canned = "I've updated src/lib.rs to rename greet to hello.";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;

    let coder = Coder::new(backend);
    let run = coder
        .run(
            tmp.path(),
            "Rename greet to hello in src/lib.rs",
            &Caveats::top(),
        )
        .await
        .unwrap();

    assert_eq!(run.emission_shape, "prose");
    assert!(run.files_written.is_empty());
    // The workspace must be unchanged — prose is a no-op.
    let content = std::fs::read_to_string(tmp.path().join("src/lib.rs")).unwrap();
    assert_eq!(content, "pub fn greet() {}\n");
}

#[tokio::test]
async fn coder_applies_unified_diff_when_model_emits_one() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("src/lib.rs"), "pub fn greet() {}\n");

    // Legacy path: a real unified diff against the actual file.
    let canned = "\
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1 +1 @@
-pub fn greet() {}
+pub fn hello() {}
";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;

    let coder = Coder::new(backend);
    let run = coder
        .run(
            tmp.path(),
            "Rename greet to hello in src/lib.rs",
            &Caveats::top(),
        )
        .await
        .unwrap();

    assert_eq!(run.emission_shape, "unified_diff");
    // Diff path returns empty files_written (see Coder::apply docs).
    assert!(run.files_written.is_empty());
    let content = std::fs::read_to_string(tmp.path().join("src/lib.rs")).unwrap();
    assert_eq!(content, "pub fn hello() {}\n");
}

#[tokio::test]
async fn coder_writes_multi_file_emission() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("src/lib.rs"), "pub fn a() {}\n");
    write_file(&tmp.path().join("src/util.rs"), "pub fn b() {}\n");

    let canned = "\
FILE: src/lib.rs
pub fn a_renamed() {}
END-FILE

FILE: src/util.rs
pub fn b_renamed() {}
END-FILE
";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;

    let coder = Coder::new(backend);
    let run = coder
        .run(
            tmp.path(),
            "Rename functions in src/lib.rs and src/util.rs",
            &Caveats::top(),
        )
        .await
        .unwrap();

    assert_eq!(run.emission_shape, "whole_files");
    assert_eq!(run.files_written.len(), 2);
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("src/lib.rs")).unwrap(),
        "pub fn a_renamed() {}"
    );
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("src/util.rs")).unwrap(),
        "pub fn b_renamed() {}"
    );
}

#[tokio::test]
async fn coder_surfaces_model_id_in_run_outcome() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("a.rs"), "fn x() {}\n");

    let canned = "FILE: a.rs\nfn y() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;

    let coder = Coder::new(backend);
    let run = coder
        .run(tmp.path(), "rename in a.rs", &Caveats::top())
        .await
        .unwrap();

    // MockBackend::all_tiers names the model "<name>-model".
    assert_eq!(run.model_id, "mock-model");
}

// ── 35b: Caveats enforcement at Coder::run ─────────────────────────────────

/// A mock backend whose endpoint is configurable — lets the
/// `caveats.net` axis tests pretend the inference call is going to a
/// specific host.
#[derive(Debug, Clone)]
struct EndpointMock {
    endpoint: String,
    reply: String,
}

#[async_trait]
impl InferenceBackend for EndpointMock {
    fn name(&self) -> &str {
        "endpoint-mock"
    }
    fn model_id(&self) -> &str {
        "endpoint-mock-model"
    }
    fn supports_tier(&self, _t: Tier) -> bool {
        true
    }
    fn endpoint(&self) -> Option<&str> {
        Some(&self.endpoint)
    }
    async fn complete(&self, _req: ChatRequest) -> anyhow::Result<ChatReply> {
        Ok(ChatReply {
            content: self.reply.clone(),
            model_id: self.model_id().to_string(),
        })
    }
}

#[tokio::test]
async fn coder_run_top_caveats_preserves_existing_behavior() {
    // Back-compat: explicit Caveats::top() runs the same path as the
    // pre-35b API — everything succeeds. This is the safety net
    // protecting the existing call sites in newt-acp-worker (which
    // pass top while 35c is in flight).
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("src/lib.rs"), "fn x() {}\n");
    let canned = "FILE: src/lib.rs\nfn y() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let run = coder
        .run(tmp.path(), "rename x to y", &Caveats::top())
        .await
        .expect("top caveats must permit every dispatch");
    assert_eq!(run.emission_shape, "whole_files");
    assert_eq!(run.files_written, vec!["src/lib.rs".to_string()]);
}

#[tokio::test]
async fn coder_run_attenuated_fs_write_permits_allowed_path() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("src/lib.rs"), "fn x() {}\n");
    let canned = "FILE: src/lib.rs\nfn y() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let caveats = Caveats {
        fs_write: Scope::only(["src/lib.rs".to_string()]),
        ..Caveats::top()
    };
    let run = coder
        .run(tmp.path(), "rename x to y", &caveats)
        .await
        .expect("permitted fs_write must succeed");
    assert_eq!(run.files_written, vec!["src/lib.rs".to_string()]);
}

#[tokio::test]
async fn coder_run_attenuated_fs_write_denies_forbidden_path() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("src/lib.rs"), "fn x() {}\n");
    // Model targets src/lib.rs, but caveats only allow "other.rs".
    let canned = "FILE: src/lib.rs\nfn y() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let caveats = Caveats {
        fs_write: Scope::only(["other.rs".to_string()]),
        ..Caveats::top()
    };
    let err = coder
        .run(tmp.path(), "rename x to y", &caveats)
        .await
        .unwrap_err();
    match err {
        CoderError::CapabilityDenied { kind, target } => {
            assert_eq!(kind, "fs_write");
            assert_eq!(target, "src/lib.rs");
        }
        other => panic!("expected CapabilityDenied{{fs_write}}, got {other:?}"),
    }
    // The original file must be untouched.
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("src/lib.rs")).unwrap(),
        "fn x() {}\n"
    );
}

#[tokio::test]
async fn coder_run_attenuated_net_permits_allowed_host() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("a.rs"), "fn x() {}\n");
    let canned = "FILE: a.rs\nfn y() {}\nEND-FILE\n".to_string();
    let backend = Arc::new(EndpointMock {
        endpoint: "https://allowed.example.com/v1/chat".to_string(),
        reply: canned,
    }) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let caveats = Caveats {
        net: Scope::only(["allowed.example.com".to_string()]),
        ..Caveats::top()
    };
    let run = coder
        .run(tmp.path(), "rename x", &caveats)
        .await
        .expect("permitted net must succeed");
    assert_eq!(run.files_written, vec!["a.rs".to_string()]);
}

#[tokio::test]
async fn coder_run_attenuated_net_denies_forbidden_host() {
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("a.rs"), "fn x() {}\n");
    let backend = Arc::new(EndpointMock {
        endpoint: "http://evil.example.com:11434/api/chat".to_string(),
        reply: "FILE: a.rs\nfn y() {}\nEND-FILE\n".to_string(),
    }) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let caveats = Caveats {
        net: Scope::only(["allowed.example.com".to_string()]),
        ..Caveats::top()
    };
    let err = coder
        .run(tmp.path(), "rename x", &caveats)
        .await
        .unwrap_err();
    match err {
        CoderError::CapabilityDenied { kind, target } => {
            assert_eq!(kind, "net");
            assert_eq!(target, "evil.example.com");
        }
        other => panic!("expected CapabilityDenied{{net}}, got {other:?}"),
    }
}

#[tokio::test]
async fn coder_run_max_calls_zero_denies_first_call() {
    // AtMost(0) must refuse the very first inference call. This is
    // the strict-cap regression: the dispatch must not slip through
    // even once.
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("a.rs"), "fn x() {}\n");
    let canned = "FILE: a.rs\nfn y() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let caveats = Caveats {
        max_calls: CountBound::AtMost(0),
        ..Caveats::top()
    };
    let err = coder
        .run(tmp.path(), "rename x", &caveats)
        .await
        .unwrap_err();
    match err {
        CoderError::CapabilityDenied { kind, target } => {
            assert_eq!(kind, "max_calls");
            assert!(target.contains("#1"), "want turn label, got {target:?}");
        }
        other => panic!("expected CapabilityDenied{{max_calls}}, got {other:?}"),
    }
}

#[tokio::test]
async fn coder_run_max_calls_one_permits_single_turn() {
    // AtMost(1) must permit exactly one happy-path inference call.
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("a.rs"), "fn x() {}\n");
    let canned = "FILE: a.rs\nfn y() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let caveats = Caveats {
        max_calls: CountBound::AtMost(1),
        ..Caveats::top()
    };
    let run = coder
        .run(tmp.path(), "rename x", &caveats)
        .await
        .expect("AtMost(1) must permit the first turn");
    assert_eq!(run.files_written, vec!["a.rs".to_string()]);
}

#[tokio::test]
async fn coder_run_attenuated_fs_read_denies_workspace_file() {
    // The included file (a.rs) is outside the fs_read scope —
    // build_prompt would have *read* it, so the dispatch is denied
    // before the model ever sees it.
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("a.rs"), "fn x() {}\n");
    let canned = "FILE: a.rs\nfn y() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let caveats = Caveats {
        fs_read: Scope::only(["only-this.rs".to_string()]),
        ..Caveats::top()
    };
    let err = coder
        .run(tmp.path(), "rename x", &caveats)
        .await
        .unwrap_err();
    assert!(matches!(
        err,
        CoderError::CapabilityDenied {
            kind: "fs_read",
            ..
        }
    ));
}

#[tokio::test]
async fn coder_run_denial_carries_axis_and_target_context() {
    // The error must carry enough context for the arbiter scorecard:
    // the failing axis name AND the concrete target. Lock that contract
    // explicitly so future error-format changes can't silently drop it.
    let tmp = TempDir::new().unwrap();
    write_file(&tmp.path().join("a.rs"), "fn x() {}\n");
    let canned = "FILE: a.rs\nfn y() {}\nEND-FILE\n";
    let backend = Arc::new(MockBackend::all_tiers("mock", canned)) as Arc<dyn InferenceBackend>;
    let coder = Coder::new(backend);

    let caveats = Caveats {
        fs_write: Scope::only(["else.rs".to_string()]),
        ..Caveats::top()
    };
    let err = coder
        .run(tmp.path(), "rename x", &caveats)
        .await
        .unwrap_err();
    let rendered = err.to_string();
    assert!(rendered.contains("fs_write"), "missing axis: {rendered}");
    assert!(rendered.contains("a.rs"), "missing target: {rendered}");
}