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
//! Core trait and handle types for the managed agent runtime.
//!
//! The [`ManagedAgentRuntime`] trait defines the full lifecycle interface for
//! managed agents: create agents from declarative definitions, start sessions,
//! send/receive events, pause/resume/interrupt, and archive.
//!
//! # Architecture
//!
//! The runtime is a **library**, not a service. The platform hosts it.
//! This trait is provider-agnostic — it behaves identically for Gemini, OpenAI,
//! Anthropic, Ollama, and OpenAI-compatible providers.
//!
//! # Example
//!
//! ```rust,ignore
//! use adk_managed::runtime::{ManagedAgentRuntime, AgentHandle, SessionHandle};
//! use adk_managed::types::ManagedAgentDef;
//!
//! async fn example(runtime: &dyn ManagedAgentRuntime) {
//! let def = ManagedAgentDef::default();
//! let agent = runtime.create(def).await.unwrap();
//! let session = runtime.start_session(&agent, &ManagedOwner::new("app", "user").unwrap(), None).await.unwrap();
//! let status = runtime.status(&session).await.unwrap();
//! println!("Session status: {status:?}");
//! }
//! ```
use HashMap;
use async_trait;
use BoxStream;
use ;
use crate;
// ─── Handle Types ────────────────────────────────────────────────────────────
/// Opaque agent handle.
///
/// The platform assigns the user-facing `agt_` prefixed ID; the runtime uses
/// this internal handle for lookups. The inner string is an implementation detail.
///
/// # Example
///
/// ```
/// use adk_managed::runtime::AgentHandle;
///
/// let handle = AgentHandle("agent_abc123".to_string());
/// assert_eq!(handle.0, "agent_abc123");
/// ```
;
/// Opaque session handle.
///
/// Identifies an active or archived session within the runtime. The inner string
/// is an implementation detail assigned by the runtime on session creation.
///
/// # Example
///
/// ```
/// use adk_managed::runtime::SessionHandle;
///
/// let handle = SessionHandle("session_xyz789".to_string());
/// assert_eq!(handle.0, "session_xyz789");
/// ```
;
/// Who a managed session belongs to.
///
/// Every managed session was previously persisted under the constants `managed` /
/// `managed_user`, so all sessions shared one logical namespace: session lookup, memory, and
/// deletion could not be scoped to a caller, and audit could not attribute a session to
/// anyone. The runtime needs this at creation because the underlying `SessionService` is
/// addressed by app and user, and substituting constants to satisfy that contract is what
/// erased the caller.
///
/// # Example
///
/// ```rust
/// use adk_managed::ManagedOwner;
///
/// let owner = ManagedOwner::new("support-console", "user-42")?;
/// assert_eq!(owner.app_name(), "support-console");
///
/// // Blank components are rejected, since they would silently re-create a shared namespace.
/// assert!(ManagedOwner::new("", "user-42").is_err());
/// # Ok::<(), adk_managed::types::RuntimeError>(())
/// ```
// ─── EnvironmentConfig ───────────────────────────────────────────────────────
/// Optional environment configuration for a session.
///
/// Provides environment variables and working directory context that the agent
/// session can use during execution (e.g., for sandbox or tool execution).
///
/// # Example
///
/// ```
/// use adk_managed::runtime::EnvironmentConfig;
///
/// let env = EnvironmentConfig {
/// env_vars: [("API_KEY".to_string(), "secret".to_string())].into(),
/// working_dir: Some("/workspace".to_string()),
/// };
/// assert_eq!(env.env_vars.get("API_KEY").unwrap(), "secret");
/// ```
// ─── ManagedAgentRuntime Trait ───────────────────────────────────────────────
/// The central async trait defining the managed agent lifecycle.
///
/// Implementations of this trait encapsulate the full agent lifecycle:
/// creating agents from declarative definitions, starting durable sessions,
/// sending/receiving events, pausing/resuming, interrupting, and archiving.
///
/// The trait is provider-agnostic: it takes a [`ManagedAgentDef`] with a
/// [`ModelRef`](crate::types::ModelRef) and behaves identically regardless
/// of which LLM provider powers the agent.
///
/// # Implementors
///
/// - `DefaultManagedAgentRuntime` — the default implementation
/// composed from `Runner` + pluggable `SessionService` + optional sandbox/memory.
///
/// # Design Notes
///
/// - All methods return `Result<_, RuntimeError>` for structured error handling.
/// - `stream_events` returns a `BoxStream` for SSE-compatible event delivery.
/// - `from_seq` on `stream_events` enables `Last-Event-ID` reconnection.
/// - The runtime is `Send + Sync` for use across async task boundaries.