mobius 0.15.36

A small, modular Rust framework for building coding agents
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
use super::*;

async fn rejected_patch(content: &str, patch: &str) -> String {
    let workspace = tempfile::tempdir().expect("workspace");
    std::fs::write(workspace.path().join("note.txt"), content).expect("write fixture");
    let context = ToolContext::new(
        Arc::new(Sandbox::new(
            Arc::new(
                crate::backend::sandbox::local::LocalSandbox::new(workspace.path())
                    .expect("local sandbox"),
            ),
            crate::backend::sandbox::ApprovalPolicy::Ask,
        )),
        SandboxPermissions::restore(
            "session",
            crate::backend::sandbox::SandboxMode::WorkspaceWrite,
            crate::backend::sandbox::NetworkAccess::Denied,
            ["patch".into()],
        )
        .for_call("patch"),
        "turn",
    );

    ApplyPatch
        .call(context, serde_json::json!({"patch": patch}))
        .await
        .expect_err("patch rejection")
        .to_string()
}

#[test]
fn apply_patch_accepts_only_one_patch_document_argument() {
    let parameters = ApplyPatch.definition().parameters;

    assert_eq!(parameters["required"], serde_json::json!(["patch"]));
    assert!(parameters["properties"].get("patch").is_some());
    assert!(parameters["properties"].get("path").is_none());
    assert!(
        serde_json::from_value::<ApplyPatchArgs>(serde_json::json!({
            "path": "note.txt",
            "patch": "invalid"
        }))
        .is_err()
    );
}

#[test]
fn apply_patch_preserves_crlf_line_endings() {
    let patch = parse_patch_document(
        "*** Begin Patch\n*** Update File: note.txt\n@@\n-old\n+new\n*** End Patch\n",
    )
    .expect("patch document");

    assert_eq!(
        apply_patch_document("first\r\nold\r\nlast\r\n", &patch).expect("applied patch"),
        "first\r\nnew\r\nlast\r\n"
    );
}

#[tokio::test]
async fn apply_patch_edits_an_absolute_workspace_path() {
    let workspace = tempfile::tempdir().expect("workspace");
    let workspace_root = std::fs::canonicalize(workspace.path()).expect("canonical workspace");
    let path = workspace_root.join("note.txt");
    let patch_path = path.to_str().expect("UTF-8 workspace path");
    std::fs::write(&path, "first\nold\nlast\n").expect("write fixture");
    let mut catalog = Catalog::default();
    catalog
        .register(Arc::new(ApplyPatch))
        .expect("register patch tool");
    let sandbox = Arc::new(Sandbox::new(
        Arc::new(
            crate::backend::sandbox::local::LocalSandbox::new(workspace.path())
                .expect("local sandbox"),
        ),
        crate::backend::sandbox::ApprovalPolicy::Ask,
    ));
    let permissions = SandboxPermissions::restore(
        "session",
        crate::backend::sandbox::SandboxMode::WorkspaceWrite,
        crate::backend::sandbox::NetworkAccess::Denied,
        ["call-1".into(), "call-2".into()],
    );
    let calls = finalize_and_bind(
        &mut catalog,
        &[ToolCall {
            call_id: "call-1".into(),
            name: "apply_patch".into(),
            arguments: serde_json::json!({
                "patch": format!("*** Begin Patch\n*** Update File: {patch_path}\n@@\n first\n-old\n+new\n last\n*** End Patch\n")
            }),
        }],
    );

    let result = execute_batch(&catalog, &calls, Arc::clone(&sandbox), &permissions, "turn")
        .await
        .pop()
        .expect("tool result");

    assert!(!result.is_error, "{}", result.output.text());
    let output = result.output.text();
    let patch = diffy::Patch::from_str(&output).expect("unified diff");
    assert_eq!(
        (patch.original(), patch.modified()),
        (Some(patch_path), Some(patch_path))
    );
    assert!(result.output.text().contains("-old\n+new\n"));
    assert_eq!(
        diffy::apply("first\nold\nlast\n", &patch).expect("apply generated patch"),
        "first\nnew\nlast\n"
    );
    assert_eq!(
        std::fs::read_to_string(&path).expect("read edited file"),
        "first\nnew\nlast\n"
    );

    let no_op_call = catalog
        .bind_call(
            ToolCall {
                call_id: "call-2".into(),
                name: "apply_patch".into(),
                arguments: serde_json::json!({
                    "patch": format!("*** Begin Patch\n*** Update File: {patch_path}\n@@\n first\n-new\n+new\n last\n*** End Patch\n")
                }),
            },
            &BTreeSet::new(),
            &BTreeSet::new(),
        )
        .expect("bind no-op call");
    let no_op = execute_batch(&catalog, &[no_op_call], sandbox, &permissions, "turn")
        .await
        .pop()
        .expect("no-op result");
    assert!(no_op.is_error);
    assert_eq!(
        no_op.output.text(),
        "tool error: patch rejected: patch applies but makes no changes"
    );
    assert_eq!(
        std::fs::read_to_string(&path).expect("read unchanged file"),
        "first\nnew\nlast\n"
    );
}

#[tokio::test]
async fn apply_patch_uses_the_active_sandbox_policy() {
    let workspace = tempfile::tempdir().expect("workspace");
    let outside = tempfile::tempdir().expect("outside");
    let path = outside.path().join("note.txt");
    let patch_path = path.to_str().expect("UTF-8 outside path");
    std::fs::write(&path, "old\n").expect("write fixture");
    let sandbox = Arc::new(Sandbox::new(
        Arc::new(
            crate::backend::sandbox::local::LocalSandbox::new(workspace.path())
                .expect("local sandbox"),
        ),
        crate::backend::sandbox::ApprovalPolicy::Ask,
    ));
    let patch = serde_json::json!({
        "patch": format!("*** Begin Patch\n*** Update File: {patch_path}\n@@\n-old\n+new\n*** End Patch\n")
    });

    let workspace_error = ApplyPatch
        .call(
            ToolContext::new(
                Arc::clone(&sandbox),
                SandboxPermissions::restore(
                    "session",
                    crate::backend::sandbox::SandboxMode::WorkspaceWrite,
                    crate::backend::sandbox::NetworkAccess::Denied,
                    ["patch".into()],
                )
                .for_call("patch"),
                "turn",
            ),
            patch.clone(),
        )
        .await
        .expect_err("workspace policy must reject outside path");
    assert!(workspace_error.to_string().contains(patch_path));
    assert_eq!(
        std::fs::read_to_string(&path).expect("unchanged file"),
        "old\n"
    );

    ApplyPatch
        .call(
            ToolContext::new(
                sandbox,
                SandboxPermissions::restore(
                    "session",
                    crate::backend::sandbox::SandboxMode::DangerFullAccess,
                    crate::backend::sandbox::NetworkAccess::Allowed,
                    ["patch".into()],
                )
                .for_call("patch"),
                "turn",
            ),
            patch,
        )
        .await
        .expect("full access patch");

    assert_eq!(
        std::fs::read_to_string(path).expect("patched file"),
        "new\n"
    );
}

#[tokio::test]
async fn apply_patch_reports_failed_context() {
    let context = "Project Prometheus is a cloud-hosted modeling workspace.";
    let content = format!("{}{context}\nactual next line\n", "padding\n".repeat(437));
    let error = rejected_patch(
        &content,
        &format!(
            "*** Begin Patch\n*** Update File: note.txt\n@@\n {context}\n-expected next line\n+replacement\n*** End Patch\n"
        ),
    )
    .await;

    assert!(
        error.contains("Patch rejected: no hunks matched the file"),
        "{error}"
    );
    assert!(error.contains(context));
}

#[tokio::test]
async fn apply_patch_supports_context_headers_and_multiple_changes() {
    let workspace = tempfile::tempdir().expect("workspace");
    let path = workspace.path().join("runtime.rs");
    let content = "    fn first\ncomment\nold one\nmiddle\nsection\ncomment\nold two\nlast\n";
    std::fs::write(&path, content).expect("write fixture");
    let context = ToolContext::new(
        Arc::new(Sandbox::new(
            Arc::new(
                crate::backend::sandbox::local::LocalSandbox::new(workspace.path())
                    .expect("local sandbox"),
            ),
            crate::backend::sandbox::ApprovalPolicy::Ask,
        )),
        SandboxPermissions::restore(
            "session",
            crate::backend::sandbox::SandboxMode::WorkspaceWrite,
            crate::backend::sandbox::NetworkAccess::Denied,
            ["patch".into()],
        )
        .for_call("patch"),
        "turn",
    );

    ApplyPatch
        .call(
            context,
            serde_json::json!({
                "patch": "*** Begin Patch\n*** Update File: runtime.rs\n@@ fn first\n-old one\n+new one\n@@ section\n-old two\n+new two\n last\n*** End of File\n*** End Patch\n"
            }),
        )
        .await
        .expect("applied patch");

    assert_eq!(
        std::fs::read_to_string(path).expect("read patched file"),
        "    fn first\ncomment\nnew one\nmiddle\nsection\ncomment\nnew two\nlast\n"
    );
}

#[test]
fn apply_patch_keeps_changes_in_document_order() {
    let patch = parse_patch_document(
        "*** Begin Patch\n*** Update File: note.txt\n@@\n-A\n+a\n@@\n-B\n+b\n*** End Patch\n",
    )
    .expect("patch document");

    assert_eq!(
        apply_patch_document("B\nA\nB\n", &patch).expect("applied patch"),
        "B\na\nb\n"
    );
}

#[test]
fn apply_patch_context_only_changes_advance_document_order() {
    let patch = parse_patch_document(
        "*** Begin Patch\n*** Update File: note.txt\n@@\n A\n@@\n-B\n+b\n*** End Patch\n",
    )
    .expect("patch document");

    assert_eq!(
        apply_patch_document("B\nA\nB\n", &patch).expect("applied patch"),
        "B\nA\nb\n"
    );
}

#[test]
fn apply_patch_end_of_file_changes_the_final_duplicate() {
    let patch = parse_patch_document(
        "*** Begin Patch\n*** Update File: note.txt\n@@\n-old\n+new\n*** End of File\n\n*** End Patch\n",
    )
    .expect("patch document");

    assert_eq!(
        apply_patch_document("old\nmiddle\nold\n", &patch).expect("applied patch"),
        "old\nmiddle\nnew\n"
    );
}

#[test]
fn apply_patch_matches_the_whole_change_body() {
    let patch = parse_patch_document(
        "*** Begin Patch\n*** Update File: note.txt\n@@\n-old\n+new\n one\n two\n three\n four\n*** End Patch\n",
    )
    .expect("patch document");

    assert_eq!(
        apply_patch_document(
            "old\none\ntwo\nthree\nwrong\nmiddle\nold\none\ntwo\nthree\nfour\n",
            &patch,
        )
        .expect("applied patch"),
        "old\none\ntwo\nthree\nwrong\nmiddle\nnew\none\ntwo\nthree\nfour\n"
    );
}

#[tokio::test]
async fn apply_patch_rejects_unwrapped_and_multiple_file_documents() {
    let unwrapped = rejected_patch("line\n", "@@ -1 +1 @@\n-old\n+new\n").await;
    let multiple = rejected_patch(
        "line\n",
        "*** Begin Patch\n*** Update File: note.txt\n-line\n+new\n*** Update File: other.txt\n-old\n+new\n*** End Patch\n",
    )
    .await;

    assert!(unwrapped.contains("missing `*** Begin Patch`"));
    assert!(multiple.contains("only one existing-file `*** Update File` operation is supported"));
}

#[test]
fn apply_patch_rejects_pathological_fuzzy_matching() {
    let content = "same\n".repeat(20_000);
    let mut patch = String::from("--- ignored\n+++ ignored\n@@ -1,1000 +1,1000 @@\n");
    patch.push_str(&" same\n".repeat(999));
    patch.push_str("-same\n+changed\n");
    let patch = Patch::from_str(&patch).expect("patch");

    assert!(validate_patch_complexity(&content, &patch, &mut 0).is_err());
}

#[test]
fn apply_patch_bounds_matching_work_across_changes() {
    let content = "same\n".repeat(2_000);
    let patch =
        Patch::from_str("--- ignored\n+++ ignored\n@@ -1 +1 @@\n-same\n+changed\n").expect("patch");
    let mut work = 0;

    assert!(validate_patch_complexity(&content, &patch, &mut work).is_ok());
    assert!(
        (0..MAX_PATCH_MATCH_WORK)
            .any(|_| validate_patch_complexity(&content, &patch, &mut work).is_err())
    );
}

#[tokio::test]
async fn apply_patch_cannot_make_a_file_unreadable() {
    let workspace = tempfile::tempdir().expect("workspace");
    let path = workspace.path().join("full.txt");
    std::fs::write(&path, "x".repeat(crate::backend::sandbox::MAX_FILE_BYTES))
        .expect("write fixture");
    let sandbox = Arc::new(Sandbox::new(
        Arc::new(
            crate::backend::sandbox::local::LocalSandbox::new(workspace.path())
                .expect("local sandbox"),
        ),
        crate::backend::sandbox::ApprovalPolicy::Ask,
    ));
    let context = ToolContext::new(
        sandbox,
        SandboxPermissions::restore(
            "session",
            crate::backend::sandbox::SandboxMode::WorkspaceWrite,
            crate::backend::sandbox::NetworkAccess::Denied,
            ["patch".into()],
        )
        .for_call("patch"),
        "turn",
    );

    let error = ApplyPatch
        .call(
            context,
            serde_json::json!({
                "patch": "*** Begin Patch\n*** Update File: full.txt\n@@\n+y\n*** End Patch\n"
            }),
        )
        .await
        .expect_err("oversized result");

    assert!(error.to_string().contains("write limit"));
    assert_eq!(
        std::fs::metadata(path).expect("metadata").len(),
        1024 * 1024
    );
}