atman-runtime 1.12.1

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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;
use std::time::Duration;

use atman_runtime::error::RuntimeError;
use atman_runtime::event::{Event, EventSink, FlowRunId};
use atman_runtime::flow_authority::EffectiveAuthority;
use atman_runtime::flow_workspace::FlowWorkspaceService;
use atman_runtime::git_workspace::{WorkspaceManager, WorkspaceState};
use atman_runtime::provider::ProviderRegistry;
use atman_runtime::task_registry::{TaskFilter, TaskRegistry, TaskStatus};
use atman_runtime::tool::{BoxFut, Tier, Tool, ToolArgs, ToolCtx, ToolRegistry, ToolResult};
use atman_runtime::tools::agent_ctrl::{
    AgentKill, AgentSpawn, AgentStatus, FlowRegistry, FlowRunStatus,
};
use atman_runtime::value::Value;

struct LifecycleProbe;

impl Tool for LifecycleProbe {
    fn name(&self) -> &str {
        "test.lifecycle_probe"
    }

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

    fn call<'a>(&'a self, args: ToolArgs, ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let action = match args.named("action") {
                Some(Value::Str(action)) => action.as_str(),
                _ => return Err(RuntimeError::ToolFailed("missing action".into())),
            };
            match action {
                "cwd" => Ok(Value::Str(ctx.resolve_cwd(None)?.display().to_string())),
                "dirty" => {
                    let path = ctx.resolve_path(Path::new("dirty.txt"))?;
                    std::fs::write(path, "inspect me\n")
                        .map_err(|error| RuntimeError::ToolFailed(error.to_string()))?;
                    Ok(Value::Str("dirty".into()))
                }
                "error" => Err(RuntimeError::ToolFailed("probe failed".into())),
                "wait" => {
                    ctx.cancel.cancelled().await;
                    Err(RuntimeError::Cancelled("probe cancelled".into()))
                }
                "break_cleanup" => {
                    let binding = ctx.workspace.as_ref().expect("managed workspace binding");
                    let registry_path = binding.repository_root.join(".atman/workspaces.json");
                    let registry_bytes = std::fs::read(&registry_path)
                        .map_err(|error| RuntimeError::ToolFailed(error.to_string()))?;
                    let mut registry: serde_json::Value =
                        serde_json::from_slice(&registry_bytes)
                            .map_err(|error| RuntimeError::ToolFailed(error.to_string()))?;
                    let workspaces = registry["workspaces"]
                        .as_array_mut()
                        .expect("workspace registry array");
                    let record = workspaces
                        .iter_mut()
                        .find(|record| record["id"] == binding.workspace_id)
                        .expect("workspace record");
                    record["owner_session"] = serde_json::Value::String("other-session".into());
                    let registry_bytes = serde_json::to_vec_pretty(&registry)
                        .map_err(|error| RuntimeError::ToolFailed(error.to_string()))?;
                    std::fs::write(registry_path, registry_bytes)
                        .map_err(|error| RuntimeError::ToolFailed(error.to_string()))?;
                    Ok(Value::Str("cleanup will fail".into()))
                }
                other => Err(RuntimeError::ToolFailed(format!(
                    "unknown probe action {other}"
                ))),
            }
        })
    }
}

struct Setup {
    _repo: tempfile::TempDir,
    ctx: ToolCtx,
    flow_ref: String,
    flows: Arc<FlowRegistry>,
    tasks: TaskRegistry,
    events: EventSink,
}

impl Setup {
    fn new(with_service: bool, with_session: bool) -> Self {
        let repo = git_repo();
        let flow_path = repo.path().join("workspace_flow.at");
        std::fs::write(
            &flow_path,
            r#"flow lifecycle(action: string) -> string {
    result = test.lifecycle_probe(action: action)
    return result
}
"#,
        )
        .unwrap();

        let flows = Arc::new(FlowRegistry::new());
        let tasks = TaskRegistry::new();
        let events = EventSink::new();
        let tools = ToolRegistry::new();
        tools.register(Arc::new(AgentSpawn));
        tools.register(Arc::new(AgentStatus));
        tools.register(Arc::new(AgentKill));
        tools.register(Arc::new(LifecycleProbe));

        let root_run_id = FlowRunId::now();
        let root_identity = flows
            .register_root(
                "session-one".into(),
                root_run_id.clone(),
                EffectiveAuthority::root(&Default::default(), false, None),
            )
            .unwrap();
        let broker = atman_runtime::permission::PermissionBroker::shared(Arc::clone(&flows));
        let mut ctx = ToolCtx::new()
            .with_registry(Arc::new(tools))
            .with_providers(Arc::new(ProviderRegistry::new()))
            .with_flow_registry(Arc::clone(&flows))
            .with_permission_broker(broker)
            .with_approval(Arc::new(atman_runtime::session::ApprovalRegistry::new()))
            .with_trust(atman_runtime::trust::TrustConfig::default())
            .with_task_registry(tasks.clone())
            .with_events(events.clone());
        ctx.flow_run_id = Some(root_run_id);
        ctx.flow_identity = Some(root_identity);
        if with_session {
            ctx = ctx.with_session_id("session-one");
        }
        if with_service {
            let service = FlowWorkspaceService::new(repo.path(), None, "generation-one").unwrap();
            ctx = ctx.with_flow_workspace_service(Arc::new(service));
        }

        Self {
            flow_ref: format!("{}@lifecycle", flow_path.display()),
            _repo: repo,
            ctx,
            flows,
            tasks,
            events,
        }
    }

    async fn spawn(&self, workspace: Option<&str>, action: &str) -> Value {
        self.spawn_with_extra(workspace, action, Vec::new()).await
    }

    async fn spawn_with_extra(
        &self,
        workspace: Option<&str>,
        action: &str,
        extra: Vec<(String, Value)>,
    ) -> Value {
        let mut named = vec![
            ("flow".into(), Value::Str(self.flow_ref.clone())),
            (
                "arguments".into(),
                Value::Struct(vec![("action".into(), Value::Str(action.into()))]),
            ),
        ];
        if let Some(workspace) = workspace {
            named.push(("workspace".into(), Value::Str(workspace.into())));
        }
        named.extend(extra);
        named.push((
            "spawn_token".into(),
            Value::Str(
                self.flows
                    .issue_spawn_permit(self.ctx.flow_identity.as_ref().unwrap()),
            ),
        ));
        AgentSpawn
            .call(
                ToolArgs {
                    positional: vec![],
                    named,
                },
                &self.ctx,
            )
            .await
            .unwrap()
    }
}

fn git(cwd: &Path, args: &[&str]) {
    git_output(cwd, args);
}

fn git_output(cwd: &Path, args: &[&str]) -> String {
    let output = Command::new("git")
        .args(args)
        .current_dir(cwd)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "git {} failed: {}",
        args.join(" "),
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8(output.stdout).unwrap().trim().to_owned()
}

fn git_repo() -> tempfile::TempDir {
    let repo = tempfile::tempdir().unwrap();
    git(repo.path(), &["init", "-q"]);
    git(repo.path(), &["config", "user.name", "Atman Test"]);
    git(
        repo.path(),
        &["config", "user.email", "atman@example.invalid"],
    );
    git(repo.path(), &["config", "commit.gpgsign", "false"]);
    git(repo.path(), &["config", "tag.gpgsign", "false"]);
    std::fs::write(repo.path().join("README.md"), "committed\n").unwrap();
    git(repo.path(), &["add", "README.md"]);
    git(repo.path(), &["commit", "-q", "-m", "initial"]);
    repo
}

fn field<'a>(value: &'a Value, name: &str) -> Option<&'a Value> {
    match value {
        Value::Struct(fields) => fields
            .iter()
            .find(|(key, _)| key == name)
            .map(|(_, value)| value),
        _ => None,
    }
}

fn string_field(value: &Value, name: &str) -> String {
    match field(value, name) {
        Some(Value::Str(value)) => value.clone(),
        other => panic!("expected string field {name}, got {other:?}"),
    }
}

async fn wait_for_terminal(setup: &Setup, handle: &str) -> FlowRunStatus {
    for _ in 0..200 {
        let status = setup
            .flows
            .lookup(handle)
            .unwrap()
            .status
            .lock()
            .unwrap()
            .clone();
        if !status.is_running() {
            return status;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    panic!("flow {handle} did not finish");
}

async fn status(setup: &Setup, handle: &str) -> Value {
    AgentStatus
        .call(
            ToolArgs {
                positional: vec![],
                named: vec![("handle".into(), Value::Str(handle.into()))],
            },
            &setup.ctx,
        )
        .await
        .unwrap()
}

#[tokio::test]
async fn none_policy_preserves_existing_spawn_behavior() {
    let setup = Setup::new(false, false);
    let spawned = setup.spawn(None, "cwd").await;
    let handle = string_field(&spawned, "handle");
    assert!(field(&spawned, "workspace_id").is_none());

    assert!(matches!(
        wait_for_terminal(&setup, &handle).await,
        FlowRunStatus::Ok { .. }
    ));
    assert!(setup.flows.lookup(&handle).unwrap().workspace.is_none());
    assert_eq!(
        setup.tasks.lookup_by_handle(&handle).unwrap().workspace_id,
        None
    );
}

#[tokio::test]
async fn managed_allocation_failures_do_not_register_flow_or_task() {
    for setup in [Setup::new(false, true), Setup::new(true, false)] {
        let error = AgentSpawn
            .call(
                ToolArgs {
                    positional: vec![],
                    named: vec![
                        ("flow".into(), Value::Str(setup.flow_ref.clone())),
                        ("workspace".into(), Value::Str("auto".into())),
                        (
                            "spawn_token".into(),
                            Value::Str(
                                setup
                                    .flows
                                    .issue_spawn_permit(setup.ctx.flow_identity.as_ref().unwrap()),
                            ),
                        ),
                    ],
                },
                &setup.ctx,
            )
            .await
            .unwrap_err();
        assert!(
            error
                .to_string()
                .contains("workspace service is unavailable")
                || error
                    .to_string()
                    .contains("requires a non-empty session id"),
            "unexpected allocation error: {error}"
        );
        assert!(setup.flows.is_empty());
        assert!(setup.tasks.list(&TaskFilter::all()).is_empty());
    }
}

#[tokio::test]
async fn flow_source_failure_happens_before_workspace_allocation() {
    let setup = Setup::new(true, true);
    let missing_flow = setup._repo.path().join("missing.at");

    let error = AgentSpawn
        .call(
            ToolArgs {
                positional: vec![],
                named: vec![
                    (
                        "flow".into(),
                        Value::Str(format!("{}@missing", missing_flow.display())),
                    ),
                    ("workspace".into(), Value::Str("auto".into())),
                    (
                        "spawn_token".into(),
                        Value::Str(
                            setup
                                .flows
                                .issue_spawn_permit(setup.ctx.flow_identity.as_ref().unwrap()),
                        ),
                    ),
                ],
            },
            &setup.ctx,
        )
        .await
        .unwrap_err();

    assert!(error.to_string().contains("missing.at"));
    assert!(setup.flows.is_empty());
    assert!(setup.tasks.list(&TaskFilter::all()).is_empty());
    let records = WorkspaceManager::at(setup._repo.path(), None)
        .unwrap()
        .list()
        .unwrap();
    assert!(records.is_empty());
}

#[tokio::test]
async fn auto_child_uses_managed_cwd_and_releases_clean_workspace() {
    let setup = Setup::new(true, true);
    let process_cwd = std::env::current_dir().unwrap();
    let spawned = setup.spawn(Some("auto"), "cwd").await;
    let handle = string_field(&spawned, "handle");
    let workspace_id = string_field(&spawned, "workspace_id");
    let workspace_path = PathBuf::from(string_field(&spawned, "workspace_path"));
    assert_eq!(string_field(&spawned, "workspace_state"), "active");

    let final_status = wait_for_terminal(&setup, &handle).await;
    match final_status {
        FlowRunStatus::Ok { final_text, .. } => {
            assert_eq!(PathBuf::from(final_text), workspace_path)
        }
        other => panic!("expected ok flow, got {other:?}"),
    }
    assert_eq!(std::env::current_dir().unwrap(), process_cwd);
    assert!(!workspace_path.exists());

    let projected = status(&setup, &handle).await;
    assert_eq!(string_field(&projected, "workspace_id"), workspace_id);
    assert_eq!(string_field(&projected, "workspace_state"), "released");
    assert!(field(&projected, "cleanup_error").is_none());

    let task = setup.tasks.lookup_by_handle(&handle).unwrap();
    assert_eq!(task.workspace_id.as_deref(), Some(workspace_id.as_str()));
    assert_eq!(task.status, TaskStatus::Ok);
}

#[tokio::test]
async fn nested_async_spawn_branches_from_parent_workspace_head() {
    let mut setup = Setup::new(true, true);
    let manager = WorkspaceManager::at(setup._repo.path(), None).unwrap();
    let main_head = git_output(setup._repo.path(), &["rev-parse", "HEAD"]);
    let parent_record = manager
        .create_managed(
            "parent",
            "session-one",
            "parent-flow",
            "generation-one",
            true,
            &main_head,
        )
        .unwrap();
    std::fs::write(
        parent_record.worktree_path.join("parent-marker.txt"),
        "from parent\n",
    )
    .unwrap();
    git(&parent_record.worktree_path, &["add", "parent-marker.txt"]);
    git(
        &parent_record.worktree_path,
        &["commit", "-q", "-m", "parent marker"],
    );
    let parent_head = git_output(&parent_record.worktree_path, &["rev-parse", "HEAD"]);
    setup.ctx = setup.ctx.clone().with_workspace(parent_record.binding());

    let spawned = setup.spawn(Some("retain"), "cwd").await;
    let handle = string_field(&spawned, "handle");
    let child_id = string_field(&spawned, "workspace_id");
    let child_path = PathBuf::from(string_field(&spawned, "workspace_path"));
    match wait_for_terminal(&setup, &handle).await {
        FlowRunStatus::Ok { final_text, .. } => assert_eq!(PathBuf::from(final_text), child_path),
        other => panic!("expected ok flow, got {other:?}"),
    }

    let child_record = manager.get(&child_id).unwrap();
    assert_eq!(
        child_record.allocation_base.as_deref(),
        Some(parent_head.as_str())
    );
    assert_eq!(git_output(&child_path, &["rev-parse", "HEAD"]), parent_head);
    assert_eq!(
        std::fs::read_to_string(child_path.join("parent-marker.txt")).unwrap(),
        "from parent\n"
    );
    assert_eq!(
        git_output(setup._repo.path(), &["rev-parse", "HEAD"]),
        main_head
    );
}

#[tokio::test]
async fn dirty_and_retain_policies_preserve_workspaces() {
    for (policy, action, expected) in [
        ("auto", "dirty", WorkspaceState::Dirty),
        ("retain", "cwd", WorkspaceState::Retained),
    ] {
        let setup = Setup::new(true, true);
        let spawned = setup.spawn(Some(policy), action).await;
        let handle = string_field(&spawned, "handle");
        let workspace_id = string_field(&spawned, "workspace_id");
        let workspace_path = PathBuf::from(string_field(&spawned, "workspace_path"));

        assert!(matches!(
            wait_for_terminal(&setup, &handle).await,
            FlowRunStatus::Ok { .. }
        ));
        assert!(workspace_path.exists());
        let projected = status(&setup, &handle).await;
        assert_eq!(
            string_field(&projected, "workspace_state"),
            expected.as_str()
        );
        let manager = WorkspaceManager::at(setup._repo.path(), None).unwrap();
        assert_eq!(
            manager.get(&workspace_id).unwrap().lifecycle_state(),
            expected
        );
    }
}

#[tokio::test]
async fn child_error_still_finalizes_clean_workspace() {
    let setup = Setup::new(true, true);
    let spawned = setup.spawn(Some("auto"), "error").await;
    let handle = string_field(&spawned, "handle");
    let workspace_path = PathBuf::from(string_field(&spawned, "workspace_path"));

    match wait_for_terminal(&setup, &handle).await {
        FlowRunStatus::Err { message, .. } => assert!(message.contains("probe failed")),
        other => panic!("expected error flow, got {other:?}"),
    }
    assert!(!workspace_path.exists());
    let projected = status(&setup, &handle).await;
    assert_eq!(string_field(&projected, "workspace_state"), "released");
    assert_eq!(
        setup.tasks.lookup_by_handle(&handle).unwrap().status,
        TaskStatus::Err
    );
}

#[tokio::test]
async fn kill_maps_flow_and_task_status_and_finalizes_workspace() {
    let setup = Setup::new(true, true);
    let spawned = setup.spawn(Some("auto"), "wait").await;
    let handle = string_field(&spawned, "handle");
    let workspace_path = PathBuf::from(string_field(&spawned, "workspace_path"));

    AgentKill
        .call(
            ToolArgs {
                positional: vec![],
                named: vec![("handle".into(), Value::Str(handle.clone()))],
            },
            &setup.ctx,
        )
        .await
        .unwrap();

    assert!(matches!(
        wait_for_terminal(&setup, &handle).await,
        FlowRunStatus::Killed { .. }
    ));
    assert!(!workspace_path.exists());
    let projected = status(&setup, &handle).await;
    assert_eq!(string_field(&projected, "workspace_state"), "released");
    assert_eq!(
        setup.tasks.lookup_by_handle(&handle).unwrap().status,
        TaskStatus::Killed
    );
}

#[tokio::test]
async fn cleanup_failure_is_projected_without_changing_flow_result() {
    let setup = Setup::new(true, true);
    let spawned = setup.spawn(Some("auto"), "break_cleanup").await;
    let handle = string_field(&spawned, "handle");
    let workspace_path = PathBuf::from(string_field(&spawned, "workspace_path"));

    assert!(matches!(
        wait_for_terminal(&setup, &handle).await,
        FlowRunStatus::Ok { .. }
    ));
    assert!(workspace_path.exists());
    let projected = status(&setup, &handle).await;
    assert_eq!(string_field(&projected, "workspace_state"), "active");
    assert!(!string_field(&projected, "cleanup_error").is_empty());

    let workspace_id = string_field(&projected, "workspace_id");
    let manager = WorkspaceManager::at(setup._repo.path(), None).unwrap();
    assert_eq!(
        manager.get(&workspace_id).unwrap().lifecycle_state(),
        WorkspaceState::Active
    );
    let lifecycle = setup
        .events
        .snapshot()
        .into_iter()
        .find(|event| {
            matches!(
                event,
                Event::WorkspaceLifecycle {
                    workspace_id: event_workspace_id,
                    ..
                } if event_workspace_id == &workspace_id
            )
        })
        .expect("workspace lifecycle event");
    let Event::WorkspaceLifecycle {
        state,
        cleanup_error,
        ..
    } = lifecycle
    else {
        unreachable!()
    };
    assert_eq!(state, "active");
    assert!(cleanup_error.is_some_and(|error| !error.is_empty()));
    assert_eq!(
        setup.tasks.lookup_by_handle(&handle).unwrap().status,
        TaskStatus::Ok
    );
}