a3s-code-core 8.5.3

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
//! Internal tool invocation gateway shared by agent runs, orchestrators, and
//! explicit session host calls.

use super::{ToolCapabilities, ToolContext, ToolRegistry, ToolResult};
use async_trait::async_trait;
use serde_json::Value;
use std::sync::{Arc, Weak};

/// Policy for explicit host control-plane tool calls.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum HostDirectPolicy {
    /// The host has already authorized the invocation, so model-facing
    /// permission and HITL gates are bypassed. Lifecycle hooks, budgets,
    /// queue/timeout, cancellation, recursive-invocation protection, and
    /// output sanitization still apply.
    TrustedControlPlane,
    /// Re-enter the same permission and HITL gate used by model and nested
    /// invocations before executing the host-requested tool.
    GovernedControlPlane,
}

/// Identifies which runtime path requested a tool invocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum InvocationOrigin {
    /// A tool call emitted directly by the model.
    Agent,
    /// A tool call emitted by an orchestrator such as `batch` or `program`.
    Nested,
    /// A private implementation step owned by a built-in runtime.
    ///
    /// The enclosing public tool has already crossed the permission boundary,
    /// so this origin skips a duplicate permission/HITL decision for the
    /// implementation tool itself. Pre-tool hooks, request evidence, budgets,
    /// cancellation, recursion guards, and every tool called by that
    /// implementation remain governed normally.
    RuntimeInternal,
    /// An explicit control-plane call made through `AgentSession::tool` or a
    /// typed direct-tool helper.
    HostDirect(HostDirectPolicy),
    /// A nested call made by a built-in control-plane orchestrator that was
    /// itself invoked directly by the host.
    ///
    /// This origin cannot be constructed through the public
    /// [`super::InvocationRuntime`]. Keeping it distinct prevents arbitrary
    /// custom tools and model sub-runs from amplifying one trusted top-level
    /// call into unrestricted nested authority.
    HostDirectNested(HostDirectPolicy),
}

impl InvocationOrigin {
    pub(crate) fn is_nested(self) -> bool {
        matches!(
            self,
            Self::Nested | Self::RuntimeInternal | Self::HostDirectNested(_)
        )
    }
}

/// Terminal outcome of a governed tool invocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ToolInvocationTerminal {
    Completed,
    Rejected,
    Cancelled,
    Failed,
}

/// Lifecycle states for one governed tool invocation.
///
/// The state machine is intentionally independent of any concrete tool
/// backend. Built-ins, MCP, Flow, and Use Runtime Tasks all cross the same
/// admission and terminal boundary in the scoped invoker.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ToolInvocationState {
    Created,
    Admitted,
    GatePending,
    Running,
    Terminal(ToolInvocationTerminal),
}

/// Internal state machine for one invocation's ownership and settlement.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ToolInvocationLifecycle {
    state: ToolInvocationState,
}

impl ToolInvocationLifecycle {
    pub(crate) const fn new() -> Self {
        Self {
            state: ToolInvocationState::Created,
        }
    }

    pub(crate) const fn state(self) -> ToolInvocationState {
        self.state
    }

    pub(crate) fn transition(&mut self, next: ToolInvocationState) -> Result<(), &'static str> {
        let allowed = matches!(
            (self.state, next),
            (ToolInvocationState::Created, ToolInvocationState::Admitted)
                | (
                    ToolInvocationState::Created,
                    ToolInvocationState::Terminal(_)
                )
                | (
                    ToolInvocationState::Admitted,
                    ToolInvocationState::GatePending
                )
                | (
                    ToolInvocationState::Admitted,
                    ToolInvocationState::Terminal(_)
                )
                | (
                    ToolInvocationState::GatePending,
                    ToolInvocationState::Running
                )
                | (
                    ToolInvocationState::GatePending,
                    ToolInvocationState::Terminal(_)
                )
                | (
                    ToolInvocationState::Running,
                    ToolInvocationState::Terminal(_)
                )
        );
        if !allowed {
            return Err("invalid tool invocation lifecycle transition");
        }
        self.state = next;
        Ok(())
    }

    pub(crate) fn terminal(&mut self, outcome: ToolInvocationTerminal) {
        debug_assert!(self
            .transition(ToolInvocationState::Terminal(outcome))
            .is_ok());
    }
}

/// Owned invocation data so calls can be dispatched across async tasks.
#[derive(Debug, Clone)]
pub(crate) struct ToolInvocation {
    pub(crate) id: String,
    pub(crate) name: String,
    pub(crate) args: Value,
    pub(crate) origin: InvocationOrigin,
    pub(crate) recent_tools: Vec<String>,
}

impl ToolInvocation {
    pub(crate) fn agent(
        id: impl Into<String>,
        name: impl Into<String>,
        args: Value,
        recent_tools: Vec<String>,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            args,
            origin: InvocationOrigin::Agent,
            recent_tools,
        }
    }

    pub(crate) fn nested(name: impl Into<String>, args: Value) -> Self {
        let name = name.into();
        Self {
            id: format!("nested-{name}-{}", uuid::Uuid::new_v4()),
            name,
            args,
            origin: InvocationOrigin::Nested,
            recent_tools: Vec::new(),
        }
    }

    pub(crate) fn runtime_internal(name: impl Into<String>, args: Value) -> Self {
        let name = name.into();
        Self {
            id: format!("runtime-internal-{name}-{}", uuid::Uuid::new_v4()),
            name,
            args,
            origin: InvocationOrigin::RuntimeInternal,
            recent_tools: Vec::new(),
        }
    }

    pub(crate) fn host_direct(id: impl Into<String>, name: impl Into<String>, args: Value) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            args,
            origin: InvocationOrigin::HostDirect(HostDirectPolicy::TrustedControlPlane),
            recent_tools: Vec::new(),
        }
    }

    pub(crate) fn host_governed(
        id: impl Into<String>,
        name: impl Into<String>,
        args: Value,
    ) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            args,
            origin: InvocationOrigin::HostDirect(HostDirectPolicy::GovernedControlPlane),
            recent_tools: Vec::new(),
        }
    }

    pub(crate) fn host_direct_nested(
        name: impl Into<String>,
        args: Value,
        policy: HostDirectPolicy,
    ) -> Self {
        let name = name.into();
        Self {
            id: format!("nested-{name}-{}", uuid::Uuid::new_v4()),
            name,
            args,
            origin: InvocationOrigin::HostDirectNested(policy),
            recent_tools: Vec::new(),
        }
    }

    /// Derive a replay-stable identity for this logical tool request.
    ///
    /// The transport `id` is intentionally excluded: providers and nested
    /// orchestrators may assign a fresh delivery id while retrying the same
    /// name/arguments at the same scope. The caller still owns the ledger that
    /// decides whether this identity is currently claimable or completed.
    pub(crate) fn idempotency_identity(
        &self,
        scope: Option<&str>,
    ) -> Result<
        crate::execution_identity::ExecutionIdentityV1,
        crate::execution_identity::ExecutionIdentityError,
    > {
        let origin = match self.origin {
            InvocationOrigin::Agent => "agent",
            InvocationOrigin::Nested => "nested",
            InvocationOrigin::RuntimeInternal => "runtime_internal",
            InvocationOrigin::HostDirect(HostDirectPolicy::TrustedControlPlane) => {
                "host_direct_trusted"
            }
            InvocationOrigin::HostDirect(HostDirectPolicy::GovernedControlPlane) => {
                "host_direct_governed"
            }
            InvocationOrigin::HostDirectNested(HostDirectPolicy::TrustedControlPlane) => {
                "host_direct_nested_trusted"
            }
            InvocationOrigin::HostDirectNested(HostDirectPolicy::GovernedControlPlane) => {
                "host_direct_nested_governed"
            }
        };
        crate::execution_identity::ExecutionIdentityV1::derive(
            crate::execution_identity::TOOL_INVOCATION_IDENTITY_DOMAIN_V1,
            &serde_json::json!({
                "scope": scope,
                "origin": origin,
                "name": self.name,
                "args": self.args,
                "recent_tools": self.recent_tools,
            }),
        )
    }
}

/// Execution boundary used by orchestrator tools instead of a raw registry.
#[async_trait]
pub(crate) trait ToolInvoker: Send + Sync {
    async fn invoke(&self, invocation: ToolInvocation, ctx: &ToolContext) -> ToolResult;

    fn available_tools(&self) -> Vec<String>;

    fn capabilities(&self, _name: &str, _args: &Value) -> Option<ToolCapabilities> {
        None
    }
}

/// Ungoverned adapter retained only for standalone low-level registry usage.
///
/// Agent runs and `AgentSession` host-direct calls install a scoped governed
/// invoker in [`ToolContext`], which takes precedence over this adapter inside
/// orchestrator tools.
struct RegistryToolInvoker {
    registry: RegistryOwnership,
}

enum RegistryOwnership {
    Standalone(Arc<ToolRegistry>),
    RegistryBound(Weak<ToolRegistry>),
}

impl RegistryOwnership {
    fn resolve(&self) -> Option<Arc<ToolRegistry>> {
        match self {
            Self::Standalone(registry) => Some(Arc::clone(registry)),
            Self::RegistryBound(registry) => registry.upgrade(),
        }
    }
}

#[async_trait]
impl ToolInvoker for RegistryToolInvoker {
    async fn invoke(&self, invocation: ToolInvocation, ctx: &ToolContext) -> ToolResult {
        let Some(registry) = self.registry.resolve() else {
            return ToolResult::error(&invocation.name, "Tool registry is closed".to_string());
        };
        let invocation_ctx = match ctx.enter_tool_invocation(&invocation.name) {
            Ok(ctx) => ctx,
            Err(message) => return ToolResult::error(&invocation.name, message),
        };

        match registry
            .execute_with_context(&invocation.name, &invocation.args, &invocation_ctx)
            .await
        {
            Ok(result) => result,
            Err(error) => {
                ToolResult::error(&invocation.name, format!("Tool execution error: {error}"))
            }
        }
    }

    fn available_tools(&self) -> Vec<String> {
        self.registry
            .resolve()
            .map_or_else(Vec::new, |registry| registry.list())
    }

    fn capabilities(&self, name: &str, args: &Value) -> Option<ToolCapabilities> {
        self.registry
            .resolve()
            .and_then(|registry| registry.capabilities(name, args))
    }
}

pub(crate) fn registry_tool_invoker(registry: Arc<ToolRegistry>) -> Arc<dyn ToolInvoker> {
    Arc::new(RegistryToolInvoker {
        registry: RegistryOwnership::Standalone(registry),
    })
}

/// Construct an invoker installed into `registry` itself.
///
/// Its back-reference is weak so `registry -> tool -> invoker -> registry`
/// cannot retain a closed executor and its session-owned resources.
pub(crate) fn registry_bound_tool_invoker(registry: Arc<ToolRegistry>) -> Arc<dyn ToolInvoker> {
    Arc::new(RegistryToolInvoker {
        registry: RegistryOwnership::RegistryBound(Arc::downgrade(&registry)),
    })
}

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

    #[test]
    fn standalone_invoker_retains_its_registry() {
        let registry = Arc::new(ToolRegistry::new(PathBuf::from("standalone-registry-test")));
        let lifetime = Arc::downgrade(&registry);
        let invoker = registry_tool_invoker(registry.clone());

        drop(registry);

        assert!(lifetime.upgrade().is_some());
        assert!(invoker.available_tools().is_empty());
    }

    #[test]
    fn tool_identity_is_stable_across_delivery_ids() {
        let first = ToolInvocation::agent(
            "delivery-a",
            "read",
            serde_json::json!({"path": "src/lib.rs"}),
            vec!["batch".to_string()],
        );
        let second = ToolInvocation::agent(
            "delivery-b",
            "read",
            serde_json::json!({"path": "src/lib.rs"}),
            vec!["batch".to_string()],
        );

        assert_eq!(
            first.idempotency_identity(Some("session-1")).unwrap(),
            second.idempotency_identity(Some("session-1")).unwrap()
        );
        assert_ne!(
            first.idempotency_identity(Some("session-1")).unwrap(),
            first.idempotency_identity(Some("session-2")).unwrap()
        );
    }

    #[test]
    fn registry_bound_invoker_does_not_retain_a_closed_registry() {
        let registry = Arc::new(ToolRegistry::new(PathBuf::from("registry-cycle-test")));
        let lifetime = Arc::downgrade(&registry);
        let invoker = registry_bound_tool_invoker(registry.clone());

        drop(registry);

        assert!(lifetime.upgrade().is_none());
        assert!(invoker.available_tools().is_empty());
        assert!(invoker
            .capabilities("read", &serde_json::json!({}))
            .is_none());
    }

    #[test]
    fn lifecycle_accepts_one_terminal_transition_and_rejects_late_work() {
        let mut lifecycle = ToolInvocationLifecycle::new();
        assert_eq!(lifecycle.state(), ToolInvocationState::Created);
        lifecycle.transition(ToolInvocationState::Admitted).unwrap();
        lifecycle
            .transition(ToolInvocationState::GatePending)
            .unwrap();
        lifecycle.transition(ToolInvocationState::Running).unwrap();
        lifecycle.terminal(ToolInvocationTerminal::Completed);
        assert_eq!(
            lifecycle.state(),
            ToolInvocationState::Terminal(ToolInvocationTerminal::Completed)
        );
        assert!(lifecycle.transition(ToolInvocationState::Running).is_err());
    }

    #[test]
    fn lifecycle_allows_rejection_before_execution() {
        let mut lifecycle = ToolInvocationLifecycle::new();
        lifecycle.transition(ToolInvocationState::Admitted).unwrap();
        lifecycle
            .transition(ToolInvocationState::GatePending)
            .unwrap();
        lifecycle.terminal(ToolInvocationTerminal::Rejected);
        assert_eq!(
            lifecycle.state(),
            ToolInvocationState::Terminal(ToolInvocationTerminal::Rejected)
        );
    }
}