a3s-code-core 8.5.4

A3S Code Core - Embeddable AI agent library with tool execution
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
use super::*;

#[cfg(feature = "dynamic-workflow")]
#[tokio::test]
async fn test_dynamic_workflow_parallel_explore_can_use_readonly_web_tools() {
    let dir = tempfile::tempdir().unwrap();
    let agent = Agent::from_config(test_config()).await.unwrap();
    let client = Arc::new(ScriptedStreamingClient::new(vec![
        scripted_tool_call_response(
            "fetch-1",
            "web_fetch",
            serde_json::json!({"url": "not-a-url"}),
        ),
        scripted_text_response("explore web fetch completed"),
        scripted_text_response("independent web review completed"),
    ]));
    let opts = SessionOptions::new()
        .with_llm_client(client)
        .with_max_parallel_tasks(1)
        .with_manual_delegation_enabled(true);
    let session = agent
        .session_async(dir.path().to_string_lossy().to_string(), Some(opts))
        .await
        .unwrap();
    session.register_dynamic_workflow_runtime().unwrap();

    let source = r#"
async function run(ctx, inputs) {
  if (inputs.kind === "workflow") {
    const fanout = inputs.step_outputs.web_research;
    if (fanout) {
      return { type: "complete", output: { fanout } };
    }
    return {
      type: "schedule_step",
      step_id: "web_research",
      step_name: "task",
      input: {
        tasks: [
          {
            agent: "explore",
            description: "Fetch web evidence",
            prompt: "Use web_fetch on the requested URL and summarize the result.",
            max_steps: 3,
          },
          {
            agent: "explore",
            description: "Review web evidence",
            prompt: "Return a short independent review.",
            max_steps: 1,
          },
        ],
      },
      retry: { max_attempts: 1, delay_ms: 0 },
    };
  }

  return { error: "task should run as a host step" };
}
"#;

    let (_rx, join) = session.tool_with_events(
        "dynamic_workflow",
        serde_json::json!({
            "source": source,
            "run_id": "test-dynamic-workflow-explore-web",
            "allowed_tools": [],
        }),
    );

    let result = tokio::time::timeout(std::time::Duration::from_secs(5), join)
        .await
        .unwrap()
        .unwrap()
        .unwrap();

    assert_eq!(
        result.exit_code, 0,
        "dynamic workflow output: {}",
        result.output
    );
    assert!(
        result.output.contains("explore web fetch completed"),
        "{}",
        result.output
    );
    assert!(
        !result.output.contains("Permission denied"),
        "web_fetch must be permitted for read-only explore research: {}",
        result.output
    );
}

#[cfg(feature = "dynamic-workflow")]
#[tokio::test]
async fn test_dynamic_workflow_parallel_deep_research_inherits_parent_permissions() {
    let dir = tempfile::tempdir().unwrap();
    let agent = Agent::from_config(test_config()).await.unwrap();
    let workspace_fs: Arc<dyn crate::workspace::WorkspaceFileSystem> =
        Arc::new(TestWorkspaceFs::default());
    let runner = Arc::new(TestWorkspaceRunner::default());
    let runner_backend: Arc<dyn crate::workspace::WorkspaceCommandRunner> = runner.clone();
    let services = crate::workspace::WorkspaceServices::builder(
        crate::workspace::WorkspaceRef::new(
            "deep-research-permission-inheritance",
            dir.path().to_string_lossy(),
        ),
        workspace_fs,
    )
    .command_runner(runner_backend)
    .build();
    let client = Arc::new(ScriptedStreamingClient::new(vec![
        scripted_tool_call_response(
            "bash-1",
            "bash",
            serde_json::json!({"command": "echo inherited-dynamic-workflow-deep-research"}),
        ),
        scripted_text_response("deep-research child bash completed"),
        scripted_text_response("deep-research control child completed"),
    ]));
    let policy = crate::permissions::PermissionPolicy::new().allow("bash(*)");
    let opts = SessionOptions::new()
        .with_llm_client(client)
        .with_permission_policy(policy)
        .with_workspace_backend(services)
        .with_max_parallel_tasks(1)
        .with_manual_delegation_enabled(true);
    let session = agent
        .session_async(dir.path().to_string_lossy().to_string(), Some(opts))
        .await
        .unwrap();
    session.register_dynamic_workflow_runtime().unwrap();

    let source = r#"
async function run(ctx, inputs) {
  if (inputs.kind === "workflow") {
    const fanout = inputs.step_outputs.deep_research;
    if (fanout) {
      return { type: "complete", output: { fanout } };
    }
    return {
      type: "schedule_step",
      step_id: "deep_research",
      step_name: "task",
      input: {
        tasks: [
          {
            agent: "deep-research",
            description: "Use inherited parent tool policy",
            prompt: "Run the harmless bash command requested by this regression test.",
            max_steps: 3,
          },
          {
            agent: "deep-research",
            description: "Verify inherited policy independently",
            prompt: "Return a short control result.",
            max_steps: 1,
          },
        ],
      },
      retry: { max_attempts: 1, delay_ms: 0 },
    };
  }

  return { error: "task should run as a host step" };
}
"#;

    let (_rx, join) = session.tool_with_events(
        "dynamic_workflow",
        serde_json::json!({
            "source": source,
            "run_id": "test-dynamic-workflow-deep-research-inherits-permissions",
            "allowed_tools": [],
        }),
    );

    let result = tokio::time::timeout(std::time::Duration::from_secs(15), join)
        .await
        .unwrap()
        .unwrap()
        .unwrap();

    assert_eq!(
        result.exit_code, 0,
        "dynamic workflow output: {}",
        result.output
    );
    assert!(
        result.output.contains("deep-research child bash completed"),
        "{}",
        result.output
    );
    assert!(
        !result.output.contains("Permission denied"),
        "deep-research must inherit the parent permission checker: {}",
        result.output
    );
    assert!(
        !result.output.contains("requires confirmation but no HITL"),
        "deep-research must inherit the parent confirmation context: {}",
        result.output
    );
    assert_eq!(
        runner.commands.read().unwrap().as_slice(),
        ["echo inherited-dynamic-workflow-deep-research"],
        "the child must execute through the parent workspace runner"
    );
}

#[cfg(feature = "dynamic-workflow")]
#[tokio::test]
async fn test_dynamic_workflow_parallel_deep_research_inherits_parent_write_permissions() {
    let dir = tempfile::tempdir().unwrap();
    let target = dir.path().join("child-evidence.txt");
    let agent = Agent::from_config(test_config()).await.unwrap();
    let client = Arc::new(ScriptedStreamingClient::new(vec![
        scripted_tool_call_response(
            "write-1",
            "write",
            serde_json::json!({
                "file_path": "child-evidence.txt",
                "content": "deep-research child inherited write permission\n"
            }),
        ),
        scripted_text_response("deep-research child write completed"),
        scripted_text_response("deep-research write control completed"),
    ]));
    let policy = crate::permissions::PermissionPolicy::new().allow("write(*)");
    let opts = SessionOptions::new()
        .with_llm_client(client)
        .with_permission_policy(policy)
        .with_max_parallel_tasks(1)
        .with_manual_delegation_enabled(true);
    let session = agent
        .session_async(dir.path().to_string_lossy().to_string(), Some(opts))
        .await
        .unwrap();
    session.register_dynamic_workflow_runtime().unwrap();

    let source = r#"
async function run(ctx, inputs) {
  if (inputs.kind === "workflow") {
    const fanout = inputs.step_outputs.deep_research;
    if (fanout) {
      return { type: "complete", output: { fanout } };
    }
    return {
      type: "schedule_step",
      step_id: "deep_research",
      step_name: "task",
      input: {
        tasks: [
          {
            agent: "deep-research",
            description: "Use inherited parent write policy",
            prompt: "Write the requested child evidence file.",
            max_steps: 3,
          },
          {
            agent: "deep-research",
            description: "Verify inherited write policy independently",
            prompt: "Return a short control result.",
            max_steps: 1,
          },
        ],
      },
      retry: { max_attempts: 1, delay_ms: 0 },
    };
  }

  return { error: "task should run as a host step" };
}
"#;

    let (_rx, join) = session.tool_with_events(
        "dynamic_workflow",
        serde_json::json!({
            "source": source,
            "run_id": "test-dynamic-workflow-deep-research-inherits-write-permissions",
            "allowed_tools": [],
        }),
    );

    let result = tokio::time::timeout(std::time::Duration::from_secs(5), join)
        .await
        .unwrap()
        .unwrap()
        .unwrap();

    assert_eq!(
        result.exit_code, 0,
        "dynamic workflow output: {}",
        result.output
    );
    assert!(
        result
            .output
            .contains("deep-research child write completed"),
        "{}",
        result.output
    );
    assert!(
        !result.output.contains("Permission denied"),
        "deep-research must inherit parent write permissions: {}",
        result.output
    );
    assert_eq!(
        std::fs::read_to_string(target).unwrap(),
        "deep-research child inherited write permission\n"
    );
}

/// `AgentSession::workflow()` must pre-wire a shared budget ledger and a stable,
/// session-derived root id (so phase checkpoints resume across runs).
#[tokio::test]
async fn test_session_workflow_is_prewired_with_budget_and_stable_root_id() {
    let agent = Agent::from_config(test_config()).await.unwrap();
    let session = agent
        .session_async("/tmp/test-workspace", None)
        .await
        .unwrap();

    let wf = session.workflow();
    // An uncapped workflow still owns a ledger (for snapshots / aggregation).
    let snap = wf
        .budget_snapshot()
        .expect("workflow is pre-wired with a shared budget ledger");
    assert_eq!(snap.limit_tokens, None);
    assert_eq!(snap.consumed_tokens, 0);
    assert!(
        wf.root_id().contains(session.id()),
        "root id is session-derived so phase checkpoints are stable across runs"
    );

    // A capped workflow records its hard ceiling.
    let capped = session.workflow_with_token_budget(Some(50_000));
    assert_eq!(capped.budget_snapshot().unwrap().limit_tokens, Some(50_000));
}

/// End-to-end: `session.workflow().agent(spec)` actually spawns a real child
/// agent loop through the wired executor and returns its output. Uses a static
/// mock LLM so the built-in `explore` agent finishes with that text.
#[tokio::test]
async fn test_session_workflow_runs_a_real_child_agent_step() {
    let agent = Agent::from_config(test_config()).await.unwrap();
    let opts = SessionOptions::new();
    let session = agent
        .build_session(
            "/tmp/test-workflow-e2e".into(),
            Arc::new(StaticStreamingClient::new("explored answer")),
            &opts,
        )
        .unwrap();

    let wf = session.workflow();
    let outcome = wf
        .agent(crate::orchestration::AgentStepSpec::new(
            "t1",
            "explore",
            "explore",
            "find the auth code",
        ))
        .await;

    assert!(outcome.success, "child step failed: {}", outcome.output);
    assert_eq!(outcome.agent, "explore");
    assert!(
        outcome.output.contains("explored answer"),
        "child agent returned the mock LLM output; got: {}",
        outcome.output
    );

    // The shared ledger recorded the child's token usage (proves the workflow
    // budget was installed as the child's budget guard).
    assert!(
        wf.budget_snapshot().unwrap().consumed_tokens > 0,
        "child LLM usage fed the shared workflow budget"
    );
}

#[tokio::test]
async fn test_session_workflow_scopes_explicit_host_tools_to_its_children() {
    let agent = Agent::from_config(test_config()).await.unwrap();
    let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let tool: Arc<dyn crate::tools::Tool> = Arc::new(CountingScopedWorkflowTool {
        calls: Arc::clone(&calls),
    });
    let client = Arc::new(ScriptedStreamingClient::new(vec![
        scripted_tool_call_response(
            "scoped-evidence-1",
            "scoped_workflow_evidence",
            serde_json::json!({}),
        ),
        scripted_text_response("scoped workflow evidence reviewed"),
    ]));
    let mut parent_permissions =
        crate::permissions::PermissionPolicy::new().allow("scoped_workflow_evidence(*)");
    parent_permissions.default_decision = crate::permissions::PermissionDecision::Deny;
    let mut worker_permissions =
        crate::permissions::PermissionPolicy::new().allow("scoped_workflow_evidence(*)");
    worker_permissions.default_decision = crate::permissions::PermissionDecision::Deny;
    let opts = SessionOptions::new()
        .with_llm_client(client)
        .with_permission_policy(parent_permissions)
        .with_worker_agent(
            crate::subagent::WorkerAgentSpec::custom(
                "scoped-evidence-reviewer",
                "Review only explicitly scoped host evidence.",
            )
            .with_permissions(worker_permissions)
            .with_max_steps(2),
        );
    let session = agent
        .session_async("/tmp/test-workflow-scoped-host-tools", Some(opts))
        .await
        .unwrap();

    let parent_before = session
        .tool("scoped_workflow_evidence", serde_json::json!({}))
        .await
        .unwrap();
    assert_ne!(
        parent_before.exit_code, 0,
        "a workflow-scoped tool must not be registered on its parent session"
    );

    let workflow = session.workflow_with_token_budget_and_tools(Some(1_024), vec![tool]);
    let outcome = workflow
        .agent(
            crate::orchestration::AgentStepSpec::new(
                "scoped-evidence-step",
                "scoped-evidence-reviewer",
                "Review scoped evidence",
                "Call the admitted evidence tool exactly once, then report completion.",
            )
            .with_max_steps(2),
        )
        .await;

    assert!(outcome.success, "child step failed: {}", outcome.output);
    assert_eq!(
        calls.load(std::sync::atomic::Ordering::SeqCst),
        1,
        "the explicit workflow child must execute the scoped tool once"
    );
    let parent_after = session
        .tool("scoped_workflow_evidence", serde_json::json!({}))
        .await
        .unwrap();
    assert_ne!(
        parent_after.exit_code, 0,
        "a workflow-scoped tool must not escape after child execution"
    );
}

#[tokio::test]
async fn test_session_workflow_parent_permission_can_deny_scoped_host_tool() {
    let agent = Agent::from_config(test_config()).await.unwrap();
    let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let tool: Arc<dyn crate::tools::Tool> = Arc::new(CountingScopedWorkflowTool {
        calls: Arc::clone(&calls),
    });
    let client = Arc::new(ScriptedStreamingClient::new(vec![
        scripted_tool_call_response(
            "scoped-evidence-denied-1",
            "scoped_workflow_evidence",
            serde_json::json!({}),
        ),
        scripted_text_response("permission boundary observed"),
    ]));
    let mut parent_permissions = crate::permissions::PermissionPolicy::new();
    parent_permissions.default_decision = crate::permissions::PermissionDecision::Deny;
    let mut worker_permissions =
        crate::permissions::PermissionPolicy::new().allow("scoped_workflow_evidence(*)");
    worker_permissions.default_decision = crate::permissions::PermissionDecision::Deny;
    let opts = SessionOptions::new()
        .with_llm_client(client)
        .with_permission_policy(parent_permissions)
        .with_worker_agent(
            crate::subagent::WorkerAgentSpec::custom(
                "scoped-evidence-reviewer-denied",
                "Review only explicitly scoped host evidence.",
            )
            .with_permissions(worker_permissions)
            .with_max_steps(2),
        );
    let session = agent
        .session_async(
            "/tmp/test-workflow-scoped-host-tool-parent-deny",
            Some(opts),
        )
        .await
        .unwrap();

    let workflow = session.workflow_with_token_budget_and_tools(Some(1_024), vec![tool]);
    let outcome = workflow
        .agent(
            crate::orchestration::AgentStepSpec::new(
                "scoped-evidence-denied-step",
                "scoped-evidence-reviewer-denied",
                "Review scoped evidence",
                "Attempt the admitted evidence tool exactly once, then report completion.",
            )
            .with_max_steps(2),
        )
        .await;

    assert!(
        outcome.success,
        "the child should observe the governed denial and finish: {}",
        outcome.output
    );
    assert_eq!(
        calls.load(std::sync::atomic::Ordering::SeqCst),
        0,
        "a child allow rule must not override its parent deny boundary"
    );
}

#[tokio::test]
async fn test_session_workflow_inherits_runtime_budget_guard() {
    let runtime_budget = Arc::new(DenyingBudgetGuard::default());
    let agent = Agent::from_config(test_config()).await.unwrap();
    let session = agent
        .build_session(
            "/tmp/test-workflow-runtime-budget".into(),
            Arc::new(StaticStreamingClient::new("must not reach the provider")),
            &SessionOptions::new(),
        )
        .unwrap();
    session
        .set_budget_guard(Some(
            Arc::clone(&runtime_budget) as Arc<dyn crate::budget::BudgetGuard>
        ))
        .unwrap();

    let outcome = session
        .workflow()
        .agent(crate::orchestration::AgentStepSpec::new(
            "runtime-budget-step",
            "explore",
            "budget check",
            "Return a short answer.",
        ))
        .await;

    assert!(!outcome.success, "runtime budget must deny workflow child");
    assert_eq!(
        runtime_budget
            .checks
            .load(std::sync::atomic::Ordering::SeqCst),
        1
    );
    assert_eq!(
        runtime_budget
            .llm_records
            .load(std::sync::atomic::Ordering::SeqCst),
        0
    );
}