atman-runtime 1.5.0

atman flow execution runtime: evaluator, tool dispatch, provider dispatch, executor, memory stores
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
492
493
494
495
496
497
498
499
500
501
use std::path::PathBuf;

use crate::error::RuntimeError;
use crate::hunk::EditProposal;
use crate::tool::{BoxFut, Tier, Tool, ToolArgs, ToolCtx, ToolResult};
use crate::value::Value;

pub struct FsEdit;

impl Tool for FsEdit {
    fn name(&self) -> &str {
        "hunk.plan_edit"
    }

    fn tier(&self) -> Tier {
        Tier::Zero
    }

    fn description(&self) -> Option<&str> {
        Some(
            "Compute a hunk-level EditProposal for replacing a file with new content. \
             Nothing is written; feed the proposal into hunk.review or hunk.apply. \
             For straightforward str_replace edits, prefer fs.edit instead.",
        )
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "path": {"type": "string", "description": "File to edit."},
                "new_content": {"type": "string", "description": "Proposed replacement content."}
            },
            "required": ["path", "new_content"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let path = extract_path(&args, "path", 0)?;
            let new_content = extract_string(&args, "new_content", 1)?;
            let original = tokio::fs::read_to_string(&path).await.map_err(|e| {
                RuntimeError::ToolFailed(format!("fs.edit({}): {e}", path.display()))
            })?;
            let proposal = EditProposal::compute(path, original, new_content);
            Ok(Value::EditProposal(Box::new(proposal)))
        })
    }
}

pub struct HunkReview;

impl Tool for HunkReview {
    fn name(&self) -> &str {
        "hunk.review"
    }

    fn tier(&self) -> Tier {
        Tier::One
    }

    fn approval_level(&self, _args: &ToolArgs, _ctx: &ToolCtx) -> crate::tool::ApprovalLevel {
        crate::tool::ApprovalLevel::Auto
    }

    fn description(&self) -> Option<&str> {
        Some(
            "Present an EditProposal to a human reviewer (or auto-approve if no resolver is \
             configured). Returns a struct with mode = auto|resolved and a hunks id list \
             the caller should pass to hunk.apply.",
        )
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "proposal": {"description": "EditProposal value from hunk.plan_edit."},
                "timeout_secs": {"type": "integer", "description": "Seconds to wait for a reviewer answer (default 300)."}
            },
            "required": ["proposal"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let proposal = extract_proposal(&args)?;
            let timeout_secs = match args.named("timeout_secs") {
                Some(Value::Int(n)) if *n > 0 => *n as u64,
                _ => 300,
            };
            let default_selection: Vec<u32> = proposal.hunks.iter().map(|h| h.id).collect();
            let Some(resolver) = ctx.prompt_resolver.clone() else {
                return Ok(Value::Struct(vec![
                    ("mode".into(), Value::Str("auto".into())),
                    (
                        "hunks".into(),
                        Value::List(
                            default_selection
                                .into_iter()
                                .map(|id| Value::Int(id as i64))
                                .collect(),
                        ),
                    ),
                ]));
            };
            let id = crate::rendezvous::PromptId::now();
            let payload = hunk_review_payload(&proposal);
            let answer = crate::rendezvous::await_prompt_with_payload(
                &resolver,
                id,
                "hunk_selection",
                payload,
                std::time::Duration::from_secs(timeout_secs),
            )
            .await?;
            let selection = parse_answer_hunk_ids(&answer, &default_selection)?;
            Ok(Value::Struct(vec![
                ("mode".into(), Value::Str("resolved".into())),
                ("prompt_id".into(), Value::Str(id.to_string())),
                (
                    "hunks".into(),
                    Value::List(
                        selection
                            .into_iter()
                            .map(|id| Value::Int(id as i64))
                            .collect(),
                    ),
                ),
            ]))
        })
    }
}

fn hunk_review_payload(proposal: &EditProposal) -> serde_json::Value {
    let hunks: Vec<serde_json::Value> = proposal
        .hunks
        .iter()
        .map(|h| {
            let mut diff = String::new();
            for line in &h.lines {
                match line {
                    crate::hunk::HunkLine::Add { text } => {
                        diff.push('+');
                        diff.push_str(text);
                        if !text.ends_with('\n') {
                            diff.push('\n');
                        }
                    }
                    crate::hunk::HunkLine::Delete { text } => {
                        diff.push('-');
                        diff.push_str(text);
                        if !text.ends_with('\n') {
                            diff.push('\n');
                        }
                    }
                    crate::hunk::HunkLine::Context { text } => {
                        diff.push(' ');
                        diff.push_str(text);
                        if !text.ends_with('\n') {
                            diff.push('\n');
                        }
                    }
                }
            }
            serde_json::json!({
                "id": h.id,
                "old_start": h.old_start,
                "old_len": h.old_len,
                "new_start": h.new_start,
                "new_len": h.new_len,
                "unified_diff": diff,
            })
        })
        .collect();
    serde_json::json!({
        "path": proposal.path.display().to_string(),
        "hunks": hunks,
        "options": ["all", "none", "select"],
    })
}

fn parse_answer_hunk_ids(
    answer: &serde_json::Value,
    default: &[u32],
) -> Result<Vec<u32>, RuntimeError> {
    if answer.is_null() {
        return Ok(default.to_vec());
    }
    if let Some(s) = answer.as_str() {
        match s {
            "all" => return Ok(default.to_vec()),
            "none" => return Ok(Vec::new()),
            other => {
                return Err(RuntimeError::ToolFailed(format!(
                    "hunk.review answer: unknown string `{other}`"
                )));
            }
        }
    }
    if let Some(hunks) = answer.get("hunks").and_then(|v| v.as_array()) {
        let mut ids = Vec::with_capacity(hunks.len());
        for h in hunks {
            let n = h.as_u64().ok_or_else(|| {
                RuntimeError::ToolFailed(format!("hunk.review answer: hunk id not u64: {h:?}"))
            })?;
            ids.push(n as u32);
        }
        return Ok(ids);
    }
    Err(RuntimeError::ToolFailed(format!(
        "hunk.review answer: unrecognized shape: {answer:?}"
    )))
}

pub struct HunkApply;

impl Tool for HunkApply {
    fn name(&self) -> &str {
        "hunk.apply"
    }

    fn tier(&self) -> Tier {
        Tier::Two
    }

    fn description(&self) -> Option<&str> {
        Some(
            "Apply selected hunks from an EditProposal to disk. `hunks` is a list of hunk ids \
             (usually from a hunk.review result). Returns which ids were applied vs skipped.",
        )
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "proposal": {"description": "EditProposal value from hunk.plan_edit."},
                "hunks": {
                    "type": "array",
                    "items": {"type": "integer"},
                    "description": "Hunk ids to apply. Omit or pass \"all\" to apply everything."
                }
            },
            "required": ["proposal"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let proposal = extract_proposal(&args)?;
            let selection = resolve_selection(&args, &proposal)?;
            let applied = proposal
                .apply_selected(&selection)
                .map_err(|e| RuntimeError::ToolFailed(format!("hunk.apply: {e}")))?;
            tokio::fs::write(&proposal.path, applied.as_bytes())
                .await
                .map_err(|e| {
                    RuntimeError::ToolFailed(format!(
                        "hunk.apply write {}: {e}",
                        proposal.path.display()
                    ))
                })?;
            let all_ids: Vec<u32> = proposal.hunks.iter().map(|h| h.id).collect();
            let skipped: Vec<Value> = all_ids
                .iter()
                .filter(|id| !selection.contains(id))
                .map(|id| Value::Int(*id as i64))
                .collect();
            let applied_ids: Vec<Value> =
                selection.iter().map(|id| Value::Int(*id as i64)).collect();
            Ok(Value::Struct(vec![
                ("status".into(), Value::Str("applied".into())),
                ("path".into(), Value::Path(proposal.path.clone())),
                ("applied_hunks".into(), Value::List(applied_ids)),
                ("skipped_hunks".into(), Value::List(skipped)),
                ("total_hunks".into(), Value::Int(all_ids.len() as i64)),
            ]))
        })
    }
}

fn resolve_selection(args: &ToolArgs, proposal: &EditProposal) -> Result<Vec<u32>, RuntimeError> {
    let value = args
        .named("hunks")
        .cloned()
        .or_else(|| args.positional(1).ok().cloned())
        .unwrap_or(Value::Str("all".into()));
    match value {
        Value::Str(s) => match s.as_str() {
            "all" => Ok(proposal.hunks.iter().map(|h| h.id).collect()),
            "none" => Ok(Vec::new()),
            other => Err(RuntimeError::ToolFailed(format!(
                "hunk.apply: unknown selection string `{other}` (want `all` | `none` | [1,3,...])"
            ))),
        },
        Value::List(items) => {
            let mut out = Vec::with_capacity(items.len());
            for item in items {
                match item {
                    Value::Int(n) if n > 0 => out.push(n as u32),
                    other => {
                        return Err(RuntimeError::TypeMismatch {
                            expected: "positive int (hunk id)".into(),
                            actual: other.kind_name().into(),
                        });
                    }
                }
            }
            Ok(out)
        }
        other => Err(RuntimeError::TypeMismatch {
            expected: "`all` | `none` | list of int (hunk ids)".into(),
            actual: other.kind_name().into(),
        }),
    }
}

fn extract_proposal(args: &ToolArgs) -> Result<EditProposal, RuntimeError> {
    let value = match args.named("proposal") {
        Some(v) => v,
        None => args.positional(0)?,
    };
    match value {
        Value::EditProposal(p) => Ok((**p).clone()),
        other => Err(RuntimeError::TypeMismatch {
            expected: "edit_proposal".into(),
            actual: other.kind_name().into(),
        }),
    }
}

fn extract_string(args: &ToolArgs, name: &str, pos: usize) -> Result<String, RuntimeError> {
    let value = match args.named(name) {
        Some(v) => v,
        None => args.positional(pos)?,
    };
    match value {
        Value::Str(s) => Ok(s.clone()),
        other => Err(RuntimeError::TypeMismatch {
            expected: "string".into(),
            actual: other.kind_name().into(),
        }),
    }
}

fn extract_path(args: &ToolArgs, name: &str, pos: usize) -> Result<PathBuf, RuntimeError> {
    let value = match args.named(name) {
        Some(v) => v,
        None => args.positional(pos)?,
    };
    match value {
        Value::Path(p) => Ok(p.clone()),
        Value::Str(s) => Ok(PathBuf::from(s)),
        other => Err(RuntimeError::TypeMismatch {
            expected: "path".into(),
            actual: other.kind_name().into(),
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn fs_edit_returns_edit_proposal_with_hunks() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("f.txt");
        std::fs::write(&path, "a\nb\nc\n").unwrap();
        let ctx = ToolCtx::new();
        let args = ToolArgs {
            positional: vec![Value::Path(path.clone()), Value::Str("a\nB\nc\n".into())],
            named: vec![],
        };
        let v = FsEdit.call(args, &ctx).await.unwrap();
        let Value::EditProposal(p) = v else {
            panic!("expected EditProposal");
        };
        assert_eq!(p.hunks.len(), 1);
        assert_eq!(p.original, "a\nb\nc\n");
        assert_eq!(p.proposed, "a\nB\nc\n");
    }

    #[tokio::test]
    async fn hunk_apply_all_writes_full_proposed() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("f.txt");
        std::fs::write(&path, "a\nb\nc\n").unwrap();
        let ctx = ToolCtx::new();
        let proposal = FsEdit
            .call(
                ToolArgs {
                    positional: vec![Value::Path(path.clone()), Value::Str("a\nB\nc\n".into())],
                    named: vec![],
                },
                &ctx,
            )
            .await
            .unwrap();
        let apply_args = ToolArgs {
            positional: vec![proposal, Value::Str("all".into())],
            named: vec![],
        };
        let out = HunkApply.call(apply_args, &ctx).await.unwrap();
        let Value::Struct(fields) = out else {
            panic!("expected struct");
        };
        assert!(matches!(
            fields.iter().find(|(k, _)| k == "status").unwrap().1,
            Value::Str(ref s) if s == "applied"
        ));
        let on_disk = std::fs::read_to_string(&path).unwrap();
        assert_eq!(on_disk, "a\nB\nc\n");
    }

    #[tokio::test]
    async fn hunk_apply_none_leaves_file_untouched() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("f.txt");
        std::fs::write(&path, "a\nb\nc\n").unwrap();
        let ctx = ToolCtx::new();
        let proposal = FsEdit
            .call(
                ToolArgs {
                    positional: vec![Value::Path(path.clone()), Value::Str("a\nB\nc\n".into())],
                    named: vec![],
                },
                &ctx,
            )
            .await
            .unwrap();
        let apply_args = ToolArgs {
            positional: vec![proposal],
            named: vec![("hunks".into(), Value::Str("none".into()))],
        };
        HunkApply.call(apply_args, &ctx).await.unwrap();
        let on_disk = std::fs::read_to_string(&path).unwrap();
        assert_eq!(on_disk, "a\nb\nc\n");
    }

    #[tokio::test]
    async fn hunk_apply_with_id_list_writes_only_selected() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("f.txt");
        let original: String = (0..20).map(|i| format!("l{i}\n")).collect();
        std::fs::write(&path, &original).unwrap();
        let mut proposed = original.clone();
        proposed = proposed.replace("l3\n", "L3\n");
        proposed = proposed.replace("l15\n", "L15\n");
        let ctx = ToolCtx::new();
        let proposal_v = FsEdit
            .call(
                ToolArgs {
                    positional: vec![Value::Path(path.clone()), Value::Str(proposed.clone())],
                    named: vec![],
                },
                &ctx,
            )
            .await
            .unwrap();
        let apply_args = ToolArgs {
            positional: vec![proposal_v],
            named: vec![("hunks".into(), Value::List(vec![Value::Int(1)]))],
        };
        let out = HunkApply.call(apply_args, &ctx).await.unwrap();
        let Value::Struct(fields) = out else {
            panic!("expected struct");
        };
        let f = |k: &str| fields.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone());
        assert!(matches!(f("total_hunks"), Some(Value::Int(2))));
        let on_disk = std::fs::read_to_string(&path).unwrap();
        assert!(on_disk.contains("L3\n"));
        assert!(!on_disk.contains("L15\n"));
        assert!(on_disk.contains("l15\n"));
    }

    #[tokio::test]
    async fn hunk_apply_rejects_unknown_selection_string() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("f.txt");
        std::fs::write(&path, "a\n").unwrap();
        let ctx = ToolCtx::new();
        let proposal = FsEdit
            .call(
                ToolArgs {
                    positional: vec![Value::Path(path), Value::Str("b\n".into())],
                    named: vec![],
                },
                &ctx,
            )
            .await
            .unwrap();
        let args = ToolArgs {
            positional: vec![proposal, Value::Str("some".into())],
            named: vec![],
        };
        let err = HunkApply.call(args, &ctx).await.unwrap_err();
        assert!(format!("{err}").contains("unknown selection"));
    }
}