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
//! The host protocol every sandbox speaks: jobs in, `HostEvent`s out, call
//! results back in. Sandbox-independent call handling lives here.
use std::collections::HashMap;
use monty_types::{CallArgs, ExcType, MontyException, MontyObject};
use serde_json::Value;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use super::catalog::Catalog;
use super::convert::{object_ref_to_json, tool_text_to_monty};
use super::dispatch::{budget_exceeded, check_call};
use super::CodeModeLimits;
use crate::tools::ToolCallOutput;
/// Outcome of one nested tool call, as delivered back to the sandbox.
pub type CallOutcome = Result<ToolCallOutput, String>;
/// Event sent from a sandbox to the async host while a snippet runs.
#[derive(Debug)]
pub enum HostEvent {
/// The snippet called a catalog function; run it and send the result.
Call {
call_id: u32,
py_name: String,
mcp_name: String,
arguments: Value,
mutating: bool,
},
/// The snippet finished.
Done(Result<Completion, String>),
}
/// A finished snippet: its final value (`None` for a `None` result) and prints.
#[derive(Debug, Clone, PartialEq)]
pub struct Completion {
pub value: Option<Value>,
pub prints: String,
}
/// One `run_code` request.
pub struct Job {
pub code: String,
pub restart: bool,
pub events: UnboundedSender<HostEvent>,
pub results: UnboundedReceiver<(u32, CallOutcome)>,
}
/// A started sandbox scope (one REPL). Dropping it ends the scope.
pub trait SandboxSession: Send + Sync {
fn submit(&self, job: Job) -> Result<(), String>;
}
/// What the sandbox should do with a function call.
pub enum CallDecision {
/// Handed to the host; resume with a pending future.
Pending,
/// Raise this exception at the call site.
Raise(MontyException),
/// Not a catalog function: raise `NameError`.
NotFound,
}
pub fn exception(kind: ExcType, message: impl Into<String>) -> MontyException {
MontyException::new(kind, Some(message.into()))
}
/// Per-snippet call handling shared by all sandboxes.
pub struct HostLink<'a> {
catalog: &'a Catalog,
limits: &'a CodeModeLimits,
job: &'a mut Job,
nested_calls: usize,
suspensions: usize,
buffered: HashMap<u32, CallOutcome>,
}
impl<'a> HostLink<'a> {
pub fn new(catalog: &'a Catalog, limits: &'a CodeModeLimits, job: &'a mut Job) -> Self {
Self {
catalog,
limits,
job,
nested_calls: 0,
suspensions: 0,
buffered: HashMap::new(),
}
}
/// Counts a suspension; `Err` once the per-snippet budget is spent.
pub fn suspend(&mut self) -> Result<(), MontyException> {
self.suspensions += 1;
if self.suspensions > self.limits.max_suspensions {
return Err(exception(
ExcType::RuntimeError,
format!(
"suspension limit of {} exceeded",
self.limits.max_suspensions
),
));
}
Ok(())
}
/// The value for an undefined name: a catalog function, or `None`.
pub fn lookup(&self, name: &str) -> Option<MontyObject> {
self.catalog
.get(name)
.map(|entry| MontyObject::function(entry.py_name.clone(), entry.doc.clone()))
}
pub fn call(
&mut self,
name: &str,
is_method: bool,
args: &CallArgs,
call_id: u32,
) -> CallDecision {
if is_method {
return CallDecision::Raise(exception(
ExcType::TypeError,
format!("{name}: host objects are not supported"),
));
}
let Some(entry) = self.catalog.get(name) else {
return CallDecision::NotFound;
};
if self.nested_calls >= self.limits.max_tool_calls {
let rejection = budget_exceeded(self.limits.max_tool_calls);
return CallDecision::Raise(exception(rejection.kind, rejection.message));
}
let kwargs = args
.kwargs()
.map(|(key, value)| {
let key = match key.as_str() {
Some(text) => text.to_string(),
None => key.py_repr(),
};
(key, object_ref_to_json(value))
})
.collect();
let arguments = match check_call(entry, args.args().len(), kwargs) {
Ok(arguments) => arguments,
Err(rejection) => {
return CallDecision::Raise(exception(rejection.kind, rejection.message))
}
};
self.nested_calls += 1;
let sent = self.job.events.send(HostEvent::Call {
call_id,
py_name: entry.py_name.clone(),
mcp_name: entry.mcp_name.clone(),
arguments: Value::Object(arguments),
mutating: entry.mutating,
});
if sent.is_err() {
return CallDecision::Raise(exception(ExcType::RuntimeError, "code mode host stopped"));
}
CallDecision::Pending
}
/// Results already delivered for any of `pending`.
pub fn take_ready(
&mut self,
pending: &[u32],
) -> Vec<(u32, Result<MontyObject, MontyException>)> {
while let Ok((call_id, outcome)) = self.job.results.try_recv() {
self.buffered.insert(call_id, outcome);
}
pending
.iter()
.filter_map(|call_id| {
self.buffered
.remove(call_id)
.map(|outcome| (*call_id, outcome_to_value(outcome)))
})
.collect()
}
/// Blocks the current (non-async) thread for one more result.
pub fn wait_blocking(&mut self) -> Result<(), String> {
match self.job.results.blocking_recv() {
Some((call_id, outcome)) => {
self.buffered.insert(call_id, outcome);
Ok(())
}
None => Err("code mode host stopped".to_string()),
}
}
/// Awaits one more result.
pub async fn wait(&mut self) -> Result<(), String> {
match self.job.results.recv().await {
Some((call_id, outcome)) => {
self.buffered.insert(call_id, outcome);
Ok(())
}
None => Err("code mode host stopped".to_string()),
}
}
}
/// A nested call's outcome as a Python value or exception.
pub fn outcome_to_value(outcome: CallOutcome) -> Result<MontyObject, MontyException> {
match outcome {
Ok(output) if output.is_error => Err(exception(ExcType::RuntimeError, output.content)),
Ok(output) => Ok(tool_text_to_monty(&output.content)),
Err(message) => Err(exception(
ExcType::RuntimeError,
format!("tool error: {message}"),
)),
}
}