horus 0.6.9

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
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
use super::*;

struct PanickingTool;

struct ApprovalRequiredTool;

struct InterruptibleTool {
    name: &'static str,
    interruptible: bool,
}

impl Tool for InterruptibleTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: self.name.into(),
            description: String::new(),
            parameters: serde_json::json!({}),
        }
    }

    fn interrupt_on_active_input(&self) -> bool {
        self.interruptible
    }

    fn call<'a>(
        &'a self,
        _context: ToolContext,
        _arguments: Value,
    ) -> BoxFuture<'a, Result<String>> {
        Box::pin(async { Ok(String::new()) })
    }
}

impl Tool for PanickingTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "panicking".into(),
            description: String::new(),
            parameters: serde_json::json!({}),
        }
    }

    fn execution_mode(&self) -> ExecutionMode {
        ExecutionMode::Parallel
    }

    fn call<'a>(
        &'a self,
        _context: ToolContext,
        _arguments: Value,
    ) -> BoxFuture<'a, Result<String>> {
        panic!("intentional tool panic")
    }
}

impl Tool for ApprovalRequiredTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "approval_required".into(),
            description: String::new(),
            parameters: serde_json::json!({}),
        }
    }

    fn approval(&self) -> ApprovalRequirement {
        ApprovalRequirement::Always
    }

    fn call<'a>(
        &'a self,
        _context: ToolContext,
        _arguments: Value,
    ) -> BoxFuture<'a, Result<String>> {
        Box::pin(async { Ok("executed".into()) })
    }
}

#[tokio::test]
async fn parallel_tool_panic_preserves_call_identity() {
    let mut catalog = Catalog::default();
    catalog
        .register(Arc::new(PanickingTool))
        .expect("register tool");
    let calls = [ToolCall {
        call_id: "call-1".into(),
        name: "panicking".into(),
        arguments: serde_json::json!({}),
    }];
    let backend =
        Arc::new(crate::backend::sandbox::local::LocalSandbox::new(".").expect("local sandbox"));
    let sandbox = Arc::new(crate::backend::sandbox::Sandbox::new(
        backend,
        crate::backend::sandbox::ApprovalPolicy::Ask,
    ));
    let permissions = SandboxPermissions::restore(
        "session",
        crate::backend::sandbox::NetworkAccess::Denied,
        Vec::new(),
    );

    assert_eq!(
        execute_batch(&catalog, &calls, sandbox, &permissions).await,
        vec![ToolResult {
            call_id: "call-1".into(),
            name: "panicking".into(),
            output: "tool panicked".into(),
            is_error: true,
        }]
    );
}

#[tokio::test]
async fn approval_required_handler_cannot_run_without_exact_call_authority() {
    let mut catalog = Catalog::default();
    catalog
        .register(Arc::new(ApprovalRequiredTool))
        .expect("register tool");
    let calls = [ToolCall {
        call_id: "blocked".into(),
        name: "approval_required".into(),
        arguments: serde_json::json!({}),
    }];
    let sandbox = Arc::new(crate::backend::sandbox::Sandbox::new(
        Arc::new(crate::backend::sandbox::local::LocalSandbox::new(".").expect("sandbox")),
        crate::backend::sandbox::ApprovalPolicy::Ask,
    ));
    let permissions = SandboxPermissions::restore(
        "session",
        crate::backend::sandbox::NetworkAccess::Allowed,
        ["different-call".into()],
    );

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

    assert!(result.is_error);
    assert_eq!(result.output, "tool call is not authorized to mutate state");
}

#[test]
fn only_wholly_interruptible_batches_stop_for_active_input() {
    let mut catalog = Catalog::default();
    for (name, interruptible) in [("wait", true), ("write", false)] {
        catalog
            .register(Arc::new(InterruptibleTool {
                name,
                interruptible,
            }))
            .expect("register tool");
    }
    let call = |name: &str| ToolCall {
        call_id: name.into(),
        name: name.into(),
        arguments: serde_json::json!({}),
    };

    assert!(catalog.interrupts_on_active_input(&[call("wait")]));
    assert!(!catalog.interrupts_on_active_input(&[call("wait"), call("write"),]));
    assert!(!catalog.interrupts_on_active_input(&[]));
}

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 {
        sandbox: Arc::new(Sandbox::new(
            Arc::new(
                crate::backend::sandbox::local::LocalSandbox::new(workspace.path())
                    .expect("local sandbox"),
            ),
            crate::backend::sandbox::ApprovalPolicy::Ask,
        )),
        permissions: SandboxPermissions::restore(
            "session",
            crate::backend::sandbox::NetworkAccess::Denied,
            ["patch".into()],
        )
        .for_call("patch"),
    };

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

#[tokio::test]
async fn apply_patch_returns_a_unified_diff_after_writing() {
    let workspace = tempfile::tempdir().expect("workspace");
    let path = workspace.path().join("note.txt");
    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::NetworkAccess::Denied,
        ["call-1".into(), "call-2".into()],
    );

    let result = execute_batch(
        &catalog,
        &[ToolCall {
            call_id: "call-1".into(),
            name: "apply_patch".into(),
            arguments: serde_json::json!({
                "path": "note.txt",
                "patch": "--- ignored.txt\n+++ ignored.txt\n@@ -1,3 +1,3 @@\n first\n-old\n+new\n last\n"
            }),
        }],
        Arc::clone(&sandbox),
        &permissions,
    )
    .await
    .pop()
    .expect("tool result");

    assert!(!result.is_error, "{}", result.output);
    let patch = diffy::Patch::from_str(&result.output).expect("unified diff");
    assert_eq!(
        (patch.original(), patch.modified()),
        (Some("note.txt"), Some("note.txt"))
    );
    assert!(result.output.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 = execute_batch(
        &catalog,
        &[ToolCall {
            call_id: "call-2".into(),
            name: "apply_patch".into(),
            arguments: serde_json::json!({
                "path": "note.txt",
                "patch": "--- ignored.txt\n+++ ignored.txt\n@@ -1,3 +1,3 @@\n first\n-new\n+new\n last\n"
            }),
        }],
        sandbox,
        &permissions,
    )
    .await
    .pop()
    .expect("no-op result");
    assert!(no_op.is_error);
    assert_eq!(
        no_op.output,
        "tool error: Patch rejected: patch applies but makes no changes."
    );
    assert_eq!(
        std::fs::read_to_string(workspace.path().join("note.txt")).expect("read unchanged file"),
        "first\nnew\nlast\n"
    );
}

#[tokio::test]
async fn apply_patch_reports_failed_context_and_nearest_line() {
    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!(
            "--- ignored\n+++ ignored\n@@ -432,2 +432,2 @@\n {context}\n-expected next line\n+replacement\n"
        ),
    )
    .await;

    assert_eq!(
        error,
        "tool error: Patch rejected: no hunks matched the file.\n\
         Failed hunk starts with context:\n\
         \"Project Prometheus is a cloud-hosted modeling workspace.\"\n\
         The nearest match is at line 438."
    );
}

#[tokio::test]
async fn apply_patch_reports_malformed_hunk_header_counts() {
    let content = "line\n".repeat(441);
    let error = rejected_patch(
        &content,
        "--- ignored\n+++ ignored\n@@ -432,3 +432,4 @@\n line\n-old\n+new\n",
    )
    .await;

    assert_eq!(
        error,
        "tool error: Patch rejected: malformed unified diff.\n\
         Reason: error parsing patch: Hunk header does not match hunk.\n\
         Expected hunk header format: @@ -start[,count] +start[,count] @@\n\
         Received: @@ -432,3 +432,4 @@\n\
         Actual file has 441 lines."
    );
}

#[tokio::test]
async fn apply_patch_rejects_input_without_hunks_as_malformed() {
    let error = rejected_patch("line\n", "@@-1 +1@@\n-old\n+new\n").await;

    assert_eq!(
        error,
        "tool error: Patch rejected: malformed unified diff.\n\
         Reason: no hunk headers were found.\n\
         Expected hunk header format: @@ -start[,count] +start[,count] @@\n\
         Received: @@-1 +1@@\n\
         Actual file has 1 lines."
    );
}

#[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).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 {
        sandbox,
        permissions: SandboxPermissions::restore(
            "session",
            crate::backend::sandbox::NetworkAccess::Denied,
            ["patch".into()],
        )
        .for_call("patch"),
    };

    let error = ApplyPatch
        .call(
            context,
            serde_json::json!({
                "path": "full.txt",
                "patch": "--- ignored\n+++ ignored\n@@ -0,0 +1 @@\n+y\n"
            }),
        )
        .await
        .expect_err("oversized result");

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

#[test]
fn tools_do_not_claim_footer_space() {
    assert!(Tools::coding().frontend().widgets.is_empty());
}

#[test]
fn coding_renderer_preserves_patch_diff_blocks() {
    let diff = "--- a/note.txt\n+++ b/note.txt\n@@ -1 +1 @@\n-old\n+new\n";
    let block = Tools::coding()
        .render(
            &EventMsg::ToolCallEnd(crate::protocol::ToolCallEndEvent {
                turn_id: "turn".into(),
                call_id: "call".into(),
                name: "apply_patch".into(),
                output: diff.into(),
                is_error: false,
            }),
            "session",
        )
        .expect("patch rendering");

    assert_eq!(block.format, FrontendBlockFormat::UnifiedDiff);
    assert!(!block.append);
    assert_eq!(block.text, diff);
}

#[test]
fn coding_renderer_groups_read_lifecycle() {
    let tools = Tools::coding();
    let begin = tools
        .render(
            &EventMsg::ToolCallBegin(crate::protocol::ToolCallBeginEvent {
                turn_id: "turn".into(),
                call_id: "call".into(),
                name: "read_file".into(),
                arguments: serde_json::json!({"path": "note.txt"}),
            }),
            "session",
        )
        .expect("read begin rendering");
    let end = tools
        .render(
            &EventMsg::ToolCallEnd(crate::protocol::ToolCallEndEvent {
                turn_id: "turn".into(),
                call_id: "call".into(),
                name: "read_file".into(),
                output: "contents".into(),
                is_error: false,
            }),
            "session",
        )
        .expect("read end rendering");

    assert_eq!(begin.group.as_deref(), Some("read:turn"));
    assert_eq!(end.group.as_deref(), Some("read:turn"));
}

#[test]
fn generic_tool_renderer_does_not_infer_coding_presentation() {
    let diff = "--- a/note.txt\n+++ b/note.txt\n@@ -1 +1 @@\n-old\n+new\n";
    let block = render_tool_event(
        &EventMsg::ToolCallEnd(crate::protocol::ToolCallEndEvent {
            turn_id: "turn".into(),
            call_id: "call".into(),
            name: "load_skill".into(),
            output: diff.into(),
            is_error: false,
        }),
        |name| name == "load_skill",
        |_, _| String::new(),
    )
    .expect("generic rendering");

    assert_eq!(block.format, FrontendBlockFormat::PlainText);
    assert_eq!(block.group, None);
    assert!(block.append);
}

#[test]
fn only_starting_a_background_command_requires_approval() {
    let mut catalog = Catalog::default();
    catalog
        .register(Arc::new(StartCommand))
        .expect("start command");
    catalog
        .register(Arc::new(PollCommand))
        .expect("poll command");
    catalog
        .register(Arc::new(StopCommand))
        .expect("stop command");

    assert!(catalog.requires_approval("start_command"));
    assert!(!catalog.requires_approval("poll_command"));
    assert!(!catalog.requires_approval("stop_command"));
}

#[test]
fn background_output_remains_valid_json_at_its_limit() {
    let rendered = background_output(BackgroundCommandPoll {
        status: crate::backend::sandbox::BackgroundCommandStatus::Running,
        exit_code: None,
        stdout: "\0".repeat(6_000),
        stderr: String::new(),
        truncated: false,
        error: Some("\0".repeat(512)),
    });

    assert!(rendered.len() <= MAX_TOOL_OUTPUT_BYTES);
    let value: Value = serde_json::from_str(&rendered).expect("valid JSON");
    assert_eq!(value["status"], "running");
}