mobius 0.15.16

A small, modular Rust framework for building coding agents
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
//! Tool execution and result persistence.

use std::collections::BTreeSet;
use std::sync::Arc;

use super::Runner;
use super::SubmissionInbox;
use super::input::ActiveRoute;
use super::input::Wait;
use crate::Error;
use crate::Result;
use crate::backend::model::ToolCall;
use crate::backend::model::tool_output;
use crate::backend::sandbox::SandboxPermissions;
use crate::middleware::tools::{PreparedToolSet, ToolResult, execute_batch};
use crate::middleware::{PostToolUseContext, PreToolUseContext};
use crate::protocol::Event;
use crate::protocol::EventMsg;
use crate::protocol::ToolCallBeginEvent;
use crate::protocol::ToolCallEndEvent;

#[derive(Default)]
pub(super) struct ToolCompletion {
    pub(super) results: Vec<ToolResult>,
    pub(super) events: Vec<EventMsg>,
}

impl From<Vec<ToolResult>> for ToolCompletion {
    fn from(results: Vec<ToolResult>) -> Self {
        Self {
            results,
            events: Vec::new(),
        }
    }
}

impl Runner {
    pub(super) async fn prepare_tool_call(
        &self,
        turn_id: &str,
        call: &mut ToolCall,
        tools: &PreparedToolSet,
        events: &mut Vec<EventMsg>,
        input: &mut Vec<serde_json::Value>,
    ) -> Result<Option<ToolResult>> {
        if let Err(error) = self.catalog.bind_prepared(call.clone(), tools) {
            return Ok(Some(ToolResult::error(call, error.to_string())));
        }
        let mut context = PreToolUseContext {
            turn: self.runtime.turn_identity(turn_id),
            events,
            tools: &self.catalog,
            call,
            input: Vec::new(),
            denial: None,
        };
        self.config.middleware.pre_tool_use(&mut context).await?;
        let denial = context.denial().map(str::to_owned);
        input.append(&mut context.input);
        if let Some(reason) = denial {
            return Ok(Some(ToolResult::error(
                call,
                format!("tool call denied: {reason}"),
            )));
        }
        Ok(None)
    }

    pub(super) async fn post_tool_results(
        &self,
        turn_id: &str,
        calls: &[ToolCall],
        completion: &mut ToolCompletion,
    ) -> Result<()> {
        for result in &mut completion.results {
            let call = calls
                .iter()
                .find(|call| call.call_id == result.call_id)
                .ok_or_else(|| Error::Tool("tool result has no matching call".into()))?;
            if !result.handler_executed {
                continue;
            }
            let mut context = PostToolUseContext {
                turn: self.runtime.turn_identity(turn_id),
                call,
                events: &mut completion.events,
                tools: &self.catalog,
                result,
            };
            self.config.middleware.post_tool_use(&mut context).await?;
        }
        Ok(())
    }

    pub(super) async fn execute_tools(
        &mut self,
        inbox: &mut SubmissionInbox,
        submission_id: &str,
        turn_id: &str,
        calls: &[ToolCall],
        permissions: SandboxPermissions,
    ) -> Result<Wait<ToolCompletion>> {
        let tools = self.live_tools().await?;
        let (bound_calls, mut unavailable_results) = self.catalog.bind_live_batch(calls, &tools);
        let callable = bound_calls
            .iter()
            .map(|call| call.as_call().clone())
            .collect::<Vec<_>>();
        for call in &callable {
            self.emit(
                submission_id,
                EventMsg::ToolCallBegin(ToolCallBeginEvent {
                    turn_id: turn_id.to_string(),
                    call_id: call.call_id.clone(),
                    name: call.name.clone(),
                    arguments: call.arguments.clone(),
                }),
            )
            .await?;
        }
        let catalog = Arc::clone(&self.catalog);
        let cancel_on_input = catalog.cancels_on_input(&callable);
        let drained = self.drain_submissions(inbox, turn_id).await?;
        if let Some(submission_id) = drained.interrupted {
            return Ok(Wait::Interrupted { submission_id });
        }
        let mut input_changed = drained.input_changed;
        let messages_ready = self
            .config
            .middleware
            .messages_ready(&self.state.pending_messages, turn_id)?;
        if cancel_on_input && (input_changed || messages_ready) {
            let mut results = interrupted_results(
                &callable,
                "execution cancelled before start because newer input is ready",
            );
            results.append(&mut unavailable_results);
            return Ok(Wait::Ready {
                value: order_results(calls, results).into(),
                input_changed: true,
            });
        }
        let execution = execute_batch(
            &catalog,
            &bound_calls,
            Arc::clone(&self.config.sandbox),
            &permissions,
            turn_id,
        );
        tokio::pin!(execution);
        let mut executed = false;
        let results = loop {
            let drained = self.drain_submissions(inbox, turn_id).await?;
            input_changed |= drained.input_changed;
            if let Some(submission_id) = drained.interrupted {
                break Wait::Interrupted { submission_id };
            }
            if cancel_on_input && drained.input_changed {
                break Wait::Ready {
                    value: interrupted_results(
                        &callable,
                        "execution cancelled by newer input; result unknown",
                    ),
                    input_changed: true,
                };
            }
            tokio::select! {
                biased;
                results = &mut execution => {
                    let drained = self.drain_submissions(inbox, turn_id).await?;
                    input_changed |= drained.input_changed;
                    if let Some(submission_id) = drained.interrupted {
                        break Wait::Interrupted { submission_id };
                    }
                    if cancel_on_input && drained.input_changed {
                        break Wait::Ready {
                            value: interrupted_results(
                                &callable,
                                "execution cancelled by newer input; result unknown",
                            ),
                            input_changed: true,
                        };
                    }
                    executed = true;
                    break Wait::Ready { value: results, input_changed };
                }
                submission = inbox.recv() => {
                    let Some(submission) = submission else {
                        return Err(Error::Stopped("frontend disconnected".into()));
                    };
                    match self.route_active_submission(submission, turn_id, None).await? {
                        ActiveRoute::Continue {
                            input_changed: changed,
                        } => {
                            if changed {
                                if cancel_on_input {
                                    break Wait::Ready {
                                        value: interrupted_results(
                                            &callable,
                                            "execution cancelled by newer input; result unknown",
                                        ),
                                        input_changed: true,
                                    };
                                }
                                input_changed = true;
                            }
                        }
                        ActiveRoute::Interrupted { submission_id } => {
                            break Wait::Interrupted { submission_id };
                        }
                        ActiveRoute::Approval { .. } => {}
                    }
                }
            }
        };
        let (mut results, input_changed) = match results {
            Wait::Ready {
                value,
                input_changed,
            } => (value, input_changed),
            Wait::Interrupted { submission_id } => {
                return Ok(Wait::Interrupted { submission_id });
            }
        };
        results.append(&mut unavailable_results);
        results = order_results(calls, results);
        if !executed {
            return Ok(Wait::Ready {
                value: results.into(),
                input_changed,
            });
        }
        let mut completion = results.into();
        self.post_tool_results(turn_id, calls, &mut completion)
            .await?;
        Ok(Wait::Ready {
            value: completion,
            input_changed,
        })
    }

    pub(super) async fn persist_tool_results(
        &mut self,
        submission_id: &str,
        turn_id: &str,
        completion: impl Into<ToolCompletion>,
    ) -> Result<()> {
        let ToolCompletion {
            results,
            events: hook_events,
        } = completion.into();
        if results.is_empty() && hook_events.is_empty() {
            return Ok(());
        }
        let mut events = hook_events
            .into_iter()
            .map(|msg| Event {
                submission_id: Some(submission_id.to_string()),
                msg,
            })
            .collect::<Vec<_>>();
        events.extend(tool_result_events(submission_id, turn_id, &results));
        let pending_tools = self.state.pending_tools.clone();
        let active_execution = self.state.active_execution.clone();
        let context_len = self.state.context.len();
        let transcript_len = self.transcript_delta.len();
        self.append_tool_results(results)?;
        match self.persist_with_events(events, None).await {
            Ok(_) => Ok(()),
            Err(error) => {
                self.state.pending_tools = pending_tools;
                self.state.active_execution = active_execution;
                self.state.context.truncate(context_len);
                self.transcript_delta.truncate(transcript_len);
                Err(error)
            }
        }
    }

    pub(super) async fn complete_tool_step(
        &mut self,
        submission_id: &str,
        turn_id: &str,
        completion: ToolCompletion,
    ) -> Result<()> {
        let pending_approval = self.state.pending_approval.take();
        match self
            .persist_tool_results(submission_id, turn_id, completion)
            .await
        {
            Ok(()) => Ok(()),
            Err(error) => {
                self.state.pending_approval = pending_approval;
                Err(error)
            }
        }
    }

    pub(super) fn append_tool_results(&mut self, results: Vec<ToolResult>) -> Result<()> {
        let tool_calls = u64::try_from(results.len())
            .map_err(|_| Error::Checkpoint("execution tool-call count is unsupported".into()))?;
        let failed_tool_calls = u64::try_from(
            results.iter().filter(|result| result.is_error).count(),
        )
        .map_err(|_| Error::Checkpoint("execution failed-tool count is unsupported".into()))?;
        self.record_tools(tool_calls, failed_tool_calls)?;
        let completed = results
            .iter()
            .map(|result| result.call_id.as_str())
            .collect::<BTreeSet<_>>();
        self.state
            .pending_tools
            .retain(|call| !completed.contains(call.call_id.as_str()));
        for mut result in results {
            self.push_context(tool_output(
                &result.call_id,
                &result.output,
                result.is_error,
            ));
            self.extend_context(std::mem::take(&mut result.additional_input));
        }
        Ok(())
    }

    pub(super) fn finish_pending_tools(
        &mut self,
        submission_id: &str,
        turn_id: &str,
        reason: &str,
    ) -> Result<Vec<Event>> {
        let calls = std::mem::take(&mut self.state.pending_tools);
        if self.state.active_model_step.is_some() {
            self.extend_context(tool_call_inputs(&calls)?);
        }
        let results = interrupted_results(
            &calls,
            &format!("execution interrupted; result unknown: {reason}"),
        );
        if results.is_empty() {
            return Ok(Vec::new());
        }
        let events = tool_result_events(submission_id, turn_id, &results);
        self.append_tool_results(results)?;
        Ok(events)
    }
}

pub(super) fn tool_call_inputs(calls: &[ToolCall]) -> Result<Vec<serde_json::Value>> {
    calls
        .iter()
        .map(|call| {
            Ok(serde_json::json!({
                "type": "function_call",
                "call_id": call.call_id,
                "name": call.name,
                "arguments": serde_json::to_string(&call.arguments)?,
            }))
        })
        .collect()
}

fn tool_result_events(submission_id: &str, turn_id: &str, results: &[ToolResult]) -> Vec<Event> {
    let mut events = Vec::with_capacity(results.len() * 2);
    for result in results {
        events.push(Event {
            submission_id: Some(submission_id.to_string()),
            msg: EventMsg::ToolCallEnd(ToolCallEndEvent {
                turn_id: turn_id.to_string(),
                call_id: result.call_id.clone(),
                name: result.name.clone(),
                output: result.output.clone(),
                is_error: result.is_error,
            }),
        });
        events.extend(result.events.iter().cloned().map(|msg| Event {
            submission_id: Some(submission_id.to_string()),
            msg,
        }));
    }
    events
}

fn interrupted_results(calls: &[ToolCall], message: &str) -> Vec<ToolResult> {
    calls
        .iter()
        .map(|call| ToolResult::error(call, message))
        .collect()
}

pub(super) fn order_results(calls: &[ToolCall], results: Vec<ToolResult>) -> Vec<ToolResult> {
    let mut results = results
        .into_iter()
        .map(|result| (result.call_id.clone(), result))
        .collect::<std::collections::BTreeMap<_, _>>();
    calls
        .iter()
        .filter_map(|call| results.remove(&call.call_id))
        .collect()
}

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

    #[test]
    fn interrupted_results_do_not_claim_tools_were_denied() {
        let calls = [ToolCall {
            call_id: "call-1".into(),
            name: "write".into(),
            arguments: serde_json::json!({}),
        }];

        let results = interrupted_results(&calls, "execution interrupted; result unknown");

        assert_eq!(
            results[0].output.text(),
            "execution interrupted; result unknown"
        );
    }
}