aidaemon-tools 0.26.1

Ready-made tools for aidaemon agents (files, search, sandboxed shell, web, checklist); implementation crate of the aidaemon SDK, no stability promise (use it through aidaemon-sdk)
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
//! Tests of `write_file` as the daemon assembles it (its policy and wording in
//! `fs::test_support`), on the unconfined local backend.

use crate::OutcomeTool as Tool;
use crate::semantics::DurableReplayDecision;
use serde_json::json;

use crate::fs::test_support::WriteFileTool;

mod durable_replay_tests {
    use super::*;
    use crate::semantics::ToolSemantics;

    #[tokio::test]
    async fn overwrite_receipt_reconciles_exact_current_file_state() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("result.md");
        let arguments = json!({
            "path": path.to_string_lossy(),
            "content": "expected content\n"
        })
        .to_string();
        let tool = WriteFileTool::new(crate::testing::active_execution_backend());

        assert!(matches!(
            tool.durable_replay_decision(&arguments).await,
            DurableReplayDecision::Reexecute { .. }
        ));

        std::fs::write(&path, "expected content\n").unwrap();
        assert_eq!(
            tool.durable_replay_decision(&arguments).await,
            DurableReplayDecision::Replay
        );

        std::fs::write(&path, "concurrent content\n").unwrap();
        assert!(matches!(
            tool.durable_replay_decision(&arguments).await,
            DurableReplayDecision::Block { .. }
        ));
    }

    #[tokio::test]
    async fn append_receipt_is_never_automatically_reexecuted() {
        let directory = tempfile::tempdir().unwrap();
        let arguments = json!({
            "path": directory.path().join("result.md").to_string_lossy(),
            "content": "one chunk\n",
            "mode": "append"
        })
        .to_string();

        assert_eq!(
            WriteFileTool::new(crate::testing::active_execution_backend())
                .durable_replay_decision(&arguments)
                .await,
            DurableReplayDecision::Replay
        );
    }
}

mod tests {
    use super::*;
    use crate::semantics::ToolSemantics;
    use crate::testing::ToolCalls as _;

    #[test]
    fn git_config_targets_are_configuration_egress() {
        let tool = WriteFileTool::new(crate::testing::active_execution_backend());
        let ann = |path: &str| {
            tool.call_annotations_in(
                &json!({"path": path}).to_string(),
                &crate::semantics::TrustRoots::new(&[]),
            )
        };
        let config = ann("/tmp/aidaemon-test/repo/.git/config");
        assert!(config.egress && !config.read_only);
        assert!(ann("/tmp/aidaemon-test/repo/.gitattributes").egress);
        let plain = ann("/tmp/aidaemon-test/repo/src/lib.rs");
        assert!(!plain.egress && !plain.read_only);
    }

    #[test]
    fn test_schema_has_required_fields() {
        let tool = WriteFileTool::new(crate::testing::active_execution_backend());
        let schema = tool.schema();
        assert_eq!(schema["name"], "write_file");
        assert!(!schema["description"].as_str().unwrap().is_empty());
        assert!(schema["parameters"]["properties"]["path"].is_object());
        assert!(schema["parameters"]["properties"]["content"].is_object());
    }

    #[tokio::test]
    async fn write_file_allows_unrelated_project_config_toml() {
        let dir = tempfile::tempdir().unwrap();
        let config = dir.path().join("config.toml");
        std::fs::write(&config, "title = \"a\"\n").unwrap();
        let args =
            json!({"path": config.to_str().unwrap(), "content": "title = \"b\"\n"}).to_string();
        WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await
            .unwrap();
        let args = json!({"path": config.to_str().unwrap()}).to_string();
        let read =
            crate::fs::test_support::ReadFileTool::new(crate::testing::active_execution_backend())
                .call(&args)
                .await
                .unwrap();
        assert!(read.contains("title = \"b\""), "{read}");
    }

    #[tokio::test]
    async fn write_file_refuses_the_daemons_own_config() {
        let dir = tempfile::tempdir().unwrap();
        let config = dir.path().join("daemon-config.toml");
        std::fs::write(&config, "[provider]\nname = \"synthetic\"\n").unwrap();
        let paths = crate::path_class::DaemonPaths::new(vec![config.clone()]);
        let args = json!({"path": config.to_str().unwrap(), "content": "x = 1"}).to_string();
        let err = crate::fs::test_support::guarded_call_protecting(
            paths,
            &WriteFileTool::new(crate::testing::active_execution_backend()),
            &args,
        )
        .await
        .unwrap_err()
        .to_string();
        assert!(err.contains("sensitive"), "got: {err}");
        assert!(
            std::fs::read_to_string(&config)
                .unwrap()
                .contains("synthetic")
        );
    }

    #[tokio::test]
    async fn test_write_new_file() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("new_file.txt");
        let args = json!({
            "path": file_path.to_str().unwrap(),
            "content": "Hello, world!"
        })
        .to_string();

        let result = WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await
            .unwrap();
        assert!(result.contains("Created"));
        assert!(result.contains("13 bytes"));

        let content = tokio::fs::read_to_string(&file_path).await.unwrap();
        assert_eq!(content, "Hello, world!");
    }

    #[tokio::test]
    async fn test_write_overwrite_with_backup() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("existing.txt");
        tokio::fs::write(&file_path, "old content").await.unwrap();

        let args = json!({
            "path": file_path.to_str().unwrap(),
            "content": "new content"
        })
        .to_string();

        let result = WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await
            .unwrap();
        assert!(result.contains("Updated"));
        assert!(result.contains("backup saved"));

        let content = tokio::fs::read_to_string(&file_path).await.unwrap();
        assert_eq!(content, "new content");

        // The backup lives under the daemon's data dir, not beside the file.
        let backup = crate::fs_utils::local_backup_path(
            &crate::fs::test_support::file_backup_root().unwrap(),
            &file_path,
        )
        .unwrap();
        let backup_content = tokio::fs::read_to_string(&backup).await.unwrap();
        assert_eq!(backup_content, "old content");
        assert!(!file_path.with_extension("txt.bak").exists());
        let _ = tokio::fs::remove_file(&backup).await;
    }

    #[tokio::test]
    async fn test_write_sensitive_path_blocked() {
        let args = json!({
            "path": "/tmp/.ssh/test_key",
            "content": "secret"
        })
        .to_string();

        let result = crate::fs::test_support::guarded_call(
            &WriteFileTool::new(crate::testing::active_execution_backend()),
            &args,
        )
        .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("sensitive"));
    }

    #[tokio::test]
    async fn test_write_create_dirs() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("a").join("b").join("file.txt");
        let args = json!({
            "path": file_path.to_str().unwrap(),
            "content": "nested",
            "create_dirs": true
        })
        .to_string();

        let result = WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await
            .unwrap();
        assert!(result.contains("Created"));
    }

    #[tokio::test]
    async fn test_write_auto_creates_parent_dirs_by_default() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("auto_created").join("file.txt");
        let args = json!({
            "path": file_path.to_str().unwrap(),
            "content": "hello"
        })
        .to_string();

        let result = WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await
            .unwrap();
        assert!(result.contains("Created"));
        let content = tokio::fs::read_to_string(&file_path).await.unwrap();
        assert_eq!(content, "hello");
    }

    #[tokio::test]
    async fn test_write_no_parent_dir_when_create_dirs_false() {
        let args = json!({
            "path": "/tmp/nonexistent_dir_12345/file.txt",
            "content": "hello",
            "create_dirs": false
        })
        .to_string();

        let result = WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Parent directory"));
    }

    #[test]
    fn test_write_file_capabilities_match_no_approval_tool_guidance() {
        let caps = WriteFileTool::new(crate::testing::active_execution_backend()).capabilities();
        assert!(
            !caps.needs_approval,
            "write_file is documented as a dedicated file tool that does not require approval"
        );
        assert!(!caps.high_impact_write);
    }

    #[tokio::test]
    async fn test_append_creates_new_file_with_hint() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("appended.txt");
        let args = json!({
            "path": file_path.to_str().unwrap(),
            "content": "chunk one\n",
            "mode": "append"
        })
        .to_string();

        let result = WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await
            .unwrap();
        assert!(result.contains("Appended"), "result was: {result}");
        // Continuation hint must tell the model how to add more.
        assert!(result.contains("mode=\"append\""), "result was: {result}");

        let content = tokio::fs::read_to_string(&file_path).await.unwrap();
        assert_eq!(content, "chunk one\n");
    }

    #[tokio::test]
    async fn test_append_preserves_existing_and_no_backup() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("doc.txt");
        tokio::fs::write(&file_path, "first\n").await.unwrap();

        let args = json!({
            "path": file_path.to_str().unwrap(),
            "content": "second\n",
            "mode": "append"
        })
        .to_string();
        WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await
            .unwrap();

        let content = tokio::fs::read_to_string(&file_path).await.unwrap();
        assert_eq!(content, "first\nsecond\n");

        // Append must NOT create a backup (it is additive).
        let backup = file_path.with_extension("txt.bak");
        assert!(!backup.exists(), "append must not create a backup");
    }

    #[tokio::test]
    async fn test_invalid_mode_errors_without_writing() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("untouched.txt");
        let args = json!({
            "path": file_path.to_str().unwrap(),
            "content": "x",
            "mode": "garbage"
        })
        .to_string();

        let result = WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid mode"));
        assert!(!file_path.exists(), "invalid mode must not write the file");
    }

    #[tokio::test]
    async fn test_append_sensitive_path_blocked() {
        let args = json!({
            "path": "/tmp/.ssh/test_key",
            "content": "secret",
            "mode": "append"
        })
        .to_string();

        let result = crate::fs::test_support::guarded_call(
            &WriteFileTool::new(crate::testing::active_execution_backend()),
            &args,
        )
        .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("sensitive"));
    }

    #[tokio::test]
    async fn test_explicit_overwrite_mode_matches_default() {
        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("ow.txt");
        tokio::fs::write(&file_path, "old").await.unwrap();
        let args = json!({
            "path": file_path.to_str().unwrap(),
            "content": "new",
            "mode": "overwrite"
        })
        .to_string();

        let result = WriteFileTool::new(crate::testing::active_execution_backend())
            .call(&args)
            .await
            .unwrap();
        assert!(result.contains("Updated"));
        let content = tokio::fs::read_to_string(&file_path).await.unwrap();
        assert_eq!(content, "new");
    }

    #[test]
    fn declares_write_of_the_absolute_path() {
        let dir = format!("/tmp/aidaemon-test/{}", uuid::Uuid::new_v4());
        let path = format!("{dir}/notes/../acme.md");
        for key in ["path", "file_path"] {
            let effects = WriteFileTool::new(crate::testing::active_execution_backend())
                .call_effects(&json!({key: path}).to_string());
            assert!(effects.complete, "{key}");
            assert_eq!(
                effects.keys,
                vec![
                    crate::command_effects::path_effect(
                        aidaemon_core::EffectClass::Write,
                        &format!("{dir}/acme.md"),
                        None
                    )
                    .unwrap()
                ]
            );
        }
        // A relative path has no known working directory here.
        let relative = WriteFileTool::new(crate::testing::active_execution_backend())
            .call_effects(&json!({"path": "acme.md"}).to_string());
        assert_eq!(relative, aidaemon_core::CallEffects::unknown());
    }
}

mod persistence_tests {
    use super::*;
    use crate::fs::test_support::EditFileTool;
    use crate::semantics::{ApprovalCall, OwnerApprovalNeed, ToolCallContext, ToolSemantics};

    /// A write into a shell start-up file, a launch agent, a git hook or the
    /// agent's own instructions asks the owner; an ordinary write does not
    /// judge (the policy's other rules decide).
    #[tokio::test]
    async fn writes_that_plant_persistence_ask_the_owner() {
        let directory = tempfile::tempdir().unwrap();
        let home = directory.path();
        let write = WriteFileTool::new(crate::testing::active_execution_backend());
        let edit = EditFileTool::new(crate::testing::active_execution_backend());
        let facts = ToolCallContext::default();
        for target in [
            home.join(".zshrc"),
            home.join("Library/LaunchAgents/com.example.synthetic.plist"),
            home.join("repo/.git/hooks/pre-commit"),
            home.join(".aidaemon/AGENTS.md"),
        ] {
            let args = json!({"path": target.to_string_lossy(), "content": "x"});
            let need = write
                .owner_approval_request(&ApprovalCall::as_sent(&args), &facts)
                .await;
            assert!(
                matches!(need, OwnerApprovalNeed::Ask(_)),
                "{target:?}: {need:?}"
            );
            let edit_args = json!({
                "path": target.to_string_lossy(),
                "old_text": "a",
                "new_text": "b"
            });
            let need = edit
                .owner_approval_request(&ApprovalCall::as_sent(&edit_args), &facts)
                .await;
            assert!(matches!(need, OwnerApprovalNeed::Ask(_)), "edit {target:?}");
        }
        let ordinary = json!({"path": home.join("notes.md").to_string_lossy(), "content": "x"});
        assert_eq!(
            write
                .owner_approval_request(&ApprovalCall::as_sent(&ordinary), &facts)
                .await,
            OwnerApprovalNeed::Unjudged
        );
    }
}