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
use super::*;
impl AgentSession {
/// Install or replace a runtime budget guard. Takes effect on the
/// next `send` / `stream` call (the guard is consulted at agent-
/// loop build time, not on the live execution). Setting `None`
/// clears the override so `config.budget_guard` takes over again.
///
/// This is the entry point SDKs use to wire a host-supplied guard
/// after the session has already been constructed — useful when
/// the guard's transport (e.g. a JS callable) cannot live inside
/// the value-typed `SessionOptions`.
pub fn set_budget_guard(
&self,
guard: Option<Arc<dyn crate::budget::BudgetGuard>>,
) -> crate::error::Result<()> {
self.close_handle.mutate_immediate(|| {
let mut slot = self
.runtime_budget_guard
.lock()
.unwrap_or_else(|p| p.into_inner());
*slot = guard;
drop(slot);
// Delegated children own a pre-built TaskExecutor. Refresh its parent
// context so the next run cannot bypass a runtime-installed ledger.
// SkillTool likewise owns a child AgentConfig captured at registration
// time, so it must be refreshed from the same runtime source of truth.
self.refresh_task_delegation_tools();
self.refresh_skill_tools();
})
}
/// Return the currently-installed runtime budget guard, if any.
/// `None` means the loop falls back to `config.budget_guard`.
pub fn budget_guard(&self) -> Option<Arc<dyn crate::budget::BudgetGuard>> {
self.runtime_budget_guard
.lock()
.unwrap_or_else(|p| p.into_inner())
.clone()
}
/// Install or clear the host-owned live checkpoint export sink (SDK-CP1).
///
/// Takes effect on the next `send` / `stream` that opens a checkpoint
/// channel. Callback-backed SDK hosts use this after session construction
/// because value-typed `SessionOptions` cannot carry language callables.
pub fn set_session_checkpoint_export_sink(
&self,
sink: Option<Arc<dyn crate::session_checkpoint::SessionCheckpointExportSink>>,
) -> crate::error::Result<()> {
self.close_handle.mutate_immediate(|| {
let mut slot = self
.runtime_session_checkpoint_export_sink
.lock()
.unwrap_or_else(|p| p.into_inner());
*slot = sink;
})
}
/// Return the currently installed live checkpoint export sink, if any.
pub fn session_checkpoint_export_sink(
&self,
) -> Option<Arc<dyn crate::session_checkpoint::SessionCheckpointExportSink>> {
self.runtime_session_checkpoint_export_sink
.lock()
.unwrap_or_else(|p| p.into_inner())
.clone()
}
/// Return pending HITL tool confirmations for this session.
pub async fn pending_confirmations(&self) -> Vec<PendingConfirmationInfo> {
HitlControl::from_session(self)
.pending_confirmations()
.await
}
/// Resolve a pending HITL tool confirmation.
///
/// Returns `Ok(true)` when a pending confirmation was found and completed,
/// `Ok(false)` when the tool ID is not pending or HITL is not configured.
pub async fn confirm_tool_use(
&self,
tool_id: &str,
approved: bool,
reason: Option<String>,
) -> Result<bool> {
HitlControl::from_session(self)
.confirm_tool_use(tool_id, approved, reason)
.await
}
/// Cancel all pending HITL confirmations for this session.
pub async fn cancel_confirmations(&self) -> usize {
HitlControl::from_session(self).cancel_confirmations().await
}
/// Return structured verification reports recorded for this session.
pub fn verification_reports(&self) -> Vec<crate::verification::VerificationReport> {
VerificationRuntime::from_session(self).reports()
}
/// Return a structured summary of all verification reports recorded for this session.
pub fn verification_summary(&self) -> crate::verification::VerificationSummary {
VerificationRuntime::from_session(self).summary()
}
/// Return a concise human-readable verification summary for this session.
pub fn verification_summary_text(&self) -> String {
VerificationRuntime::from_session(self).summary_text()
}
/// Add externally produced verification reports to this session's completion evidence.
pub fn record_verification_reports(
&self,
reports: impl IntoIterator<Item = crate::verification::VerificationReport>,
) {
VerificationRuntime::from_session(self).record(reports);
}
/// Register a hook for lifecycle event interception.
pub fn register_hook(&self, hook: crate::hooks::Hook) -> crate::error::Result<()> {
self.close_handle.mutate_immediate(|| {
self.ensure_compatibility_name_available(
crate::capability::CapabilityKind::Hook,
&hook.id,
)?;
HookControl::from_session(self).register_hook(hook);
Ok(())
})?
}
/// Unregister a hook by ID.
pub fn unregister_hook(
&self,
hook_id: &str,
) -> crate::error::Result<Option<crate::hooks::Hook>> {
self.close_handle.mutate_immediate(|| {
self.ensure_compatibility_name_available(
crate::capability::CapabilityKind::Hook,
hook_id,
)?;
Ok(HookControl::from_session(self).unregister_hook(hook_id))
})?
}
/// Register a handler for a specific hook.
pub fn register_hook_handler(
&self,
hook_id: &str,
handler: Arc<dyn crate::hooks::HookHandler>,
) -> crate::error::Result<()> {
let mut pending_handler = Some(handler);
let retired = self.close_handle.mutate_immediate(|| {
self.ensure_compatibility_name_available(
crate::capability::CapabilityKind::Hook,
hook_id,
)?;
let handler = pending_handler.take().ok_or_else(|| {
crate::error::CodeError::Capability(
crate::capability::CapabilityRuntimeError::RuntimeValueInvalid {
kind: crate::capability::CapabilityKind::Hook,
public_name: hook_id.to_owned(),
message: "Hook handler mutation was already consumed".to_owned(),
},
)
})?;
Ok::<_, crate::error::CodeError>(
HookControl::from_session(self).register_hook_handler(hook_id, handler),
)
})??;
drop(retired);
Ok(())
}
/// Unregister a hook handler by hook ID.
pub fn unregister_hook_handler(&self, hook_id: &str) -> crate::error::Result<()> {
let retired = self.close_handle.mutate_immediate(|| {
self.ensure_compatibility_name_available(
crate::capability::CapabilityKind::Hook,
hook_id,
)?;
Ok::<_, crate::error::CodeError>(
HookControl::from_session(self).unregister_hook_handler(hook_id),
)
})??;
drop(retired);
Ok(())
}
/// Atomically add or replace a complete Hook definition and handler pair.
///
/// Official SDK bridges use this method so a newly admitted Run cannot
/// observe the definition without its matching callback. Passing `None`
/// intentionally registers an event-only Hook with no gating handler.
pub fn register_hook_registration(
&self,
hook: crate::hooks::Hook,
handler: Option<Arc<dyn crate::hooks::HookHandler>>,
) -> crate::error::Result<()> {
let mut pending_handler = Some(handler);
let retired = self.close_handle.mutate_immediate(|| {
self.ensure_compatibility_name_available(
crate::capability::CapabilityKind::Hook,
&hook.id,
)?;
let handler = pending_handler.take().ok_or_else(|| {
crate::error::CodeError::Capability(
crate::capability::CapabilityRuntimeError::RuntimeValueInvalid {
kind: crate::capability::CapabilityKind::Hook,
public_name: hook.id.clone(),
message: "Hook registration was already consumed".to_owned(),
},
)
})?;
Ok::<_, crate::error::CodeError>(
HookControl::from_session(self).register_hook_registration(hook, handler),
)
})??;
drop(retired);
Ok(())
}
/// Atomically remove a complete Hook definition and handler pair.
pub fn unregister_hook_registration(
&self,
hook_id: &str,
) -> crate::error::Result<Option<crate::hooks::Hook>> {
let (retired_hook, retired_handler) = self.close_handle.mutate_immediate(|| {
self.ensure_compatibility_name_available(
crate::capability::CapabilityKind::Hook,
hook_id,
)?;
Ok::<_, crate::error::CodeError>(
HookControl::from_session(self).unregister_hook_registration(hook_id),
)
})??;
let removed = retired_hook.as_deref().cloned();
drop((retired_hook, retired_handler));
Ok(removed)
}
/// Get the number of registered hooks.
pub fn hook_count(&self) -> usize {
HookControl::from_session(self).hook_count()
}
/// Run verification commands through the session's tool execution path.
pub async fn verify_commands(
&self,
subject: &str,
commands: &[crate::verification::VerificationCommand],
) -> Result<crate::verification::VerificationReport> {
VerificationRuntime::from_session(self)
.verify_commands(subject, commands)
.await
}
/// Return project-aware verification command presets for this workspace.
pub fn verification_presets(&self) -> Vec<crate::verification::VerificationPreset> {
VerificationRuntime::from_session(self).presets()
}
}