ghostmode 0.1.1

Code Mode for Rust agents. The model writes a Python script that calls your tools, and ghostmode runs it in a sandbox.
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
//! The sandbox that answers each `execute` or `poll` call the caller passes in from the model.

use std::borrow::Cow;
use std::time::{Duration, Instant};
use std::{error, fmt, mem};

use futures_timer::Delay;
use futures_util::future::{self, BoxFuture, Either, FutureExt};
use hmac::{Hmac, KeyInit, Mac};
use serde::Serialize;
use serde_json::Value;
use sha2::Sha256;
use tracing::field::{self, Empty};
use tracing::{Instrument, Span};

use crate::backend::{Backend, Ending, Progress, Streams};
use crate::host::{self, Host, Tool};

mod output;
use self::output::{split_budget, to_text, truncate_text};

/// One Python session that runs the model's scripts in backend `B`, and the host whose tools
/// they call. Globals persist from one `execute` to the next, and one run is in flight at a time.
///
/// In-process `backend::Monty` runs the script on the thread that polls `execute` or `poll`,
/// while `backend::MontyPool` runs it in a worker process. A wait ends only while the script
/// waits on the host: a script that runs past it holds `execute` or `poll` until it calls a
/// tool or ends, and [`Limits::run_time`] bounds the script's own time, which in-process is
/// checked between steps. Its `Debug` output never shows the signing key.
pub struct Sandbox<H, B: Backend> {
    builder: Builder<H, B>,
    /// The session, or `None` once a backend call that held it was dropped, which loses it.
    state: Option<State<B>>,
    /// The id of the last run `execute` started, which is the suspended run's id while one is
    /// open.
    last_run_id: u64,
    /// Whether the session lost its globals since the last finished or failed status, so the
    /// next one says `restarted`.
    unreported_restart: bool,
}

/// Sets up a [`Sandbox`]. Made by [`Builder::new`], which names the backend. The built sandbox
/// keeps its builder and runs with its host, backend, waits, limits and signing key. Its `Debug`
/// output never shows the signing key.
pub struct Builder<H, B> {
    host: H,
    tools: Vec<Tool>,
    backend: B,
    execute_wait: Duration,
    poll_wait: Duration,
    limits: Limits,
    signing_key: Option<[u8; 32]>,
}

/// How many bytes the HMAC-SHA256 tag takes that a signing key appends to a dump.
const TAG_BYTES: usize = 32;

/// The format version that starts every dump. [`Builder::restore`] refuses any other version.
const DUMP_FORMAT_VERSION: u8 = 1;

/// How many bytes the header takes that starts a dump: the [format version](DUMP_FORMAT_VERSION),
/// one byte for whether the session owes the model a `restarted` status, then the last run id as
/// 8 little-endian bytes.
const HEADER_BYTES: usize = 10;

/// What a script may use before the sandbox stops it with a `failed` status. A stopped script
/// leaves the session's globals as it had set them, unless the backend lost them, which the next
/// status reports as `restarted`.
///
/// Start from [`Limits::default`] and set the fields you want:
///
/// ```
/// # use std::time::Duration;
/// let mut limits = ghostmode::Limits::default();
/// limits.run_time = Duration::from_secs(5);
/// limits.output_chars = 8_000;
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Limits {
    /// How long the script itself may run, summed over `execute` and every `poll` of its run
    /// and not counting time spent waiting on the host. In-process `backend::Monty` checks it
    /// between steps, so one long step, such as a huge multiplication, runs to its end first. The
    /// default is 30 s.
    pub run_time: Duration,
    /// The most memory the script may use. In-process this does not cap the script's total
    /// memory. It only refuses one value bigger than the limit, such as one huge string, with
    /// `MemoryError`, and a host's answer becomes a script value without being checked against it.
    /// A `backend::MontyPool` worker caps the script's total memory. The default is 256 MiB.
    pub memory_bytes: usize,
    /// How deep the script's calls may nest before it raises `RecursionError`. The default is
    /// 1,000.
    pub recursion_depth: usize,
    /// How many tool calls the script of one `execute` may make, over all its polls. The
    /// default is 100.
    pub calls_per_execute: usize,
    /// How long one tool call may wait for the host's answer. The default is unset, no limit.
    pub call_timeout: Option<Duration>,
    /// How long a run that `execute` or `poll` left `running` may wait for the next `execute` or
    /// `poll` before it expires. A run whose host has answered by then doesn't expire, and the
    /// next `poll` resumes it. The default is unset, no limit.
    pub idle_timeout: Option<Duration>,
    /// How many characters of output a status carries: a finished value or an error, together
    /// with what the script printed to stdout and stderr. Output over it keeps its head and
    /// tail around a marker such as `[… 60,000 of 100,000 characters truncated …]`, the marker
    /// counted inside it, and a value truncated this way becomes a string. A budget smaller than
    /// the marker keeps the marker alone. A value is counted as its JSON text, except a string,
    /// which is counted as its own text. The arguments of a failed status's calls get as many
    /// characters again, split evenly over `calls_per_execute` calls. The default is 40,000.
    pub output_chars: usize,
}

/// What `execute` or `poll` answers, from the sandbox to the caller. It serializes as the JSON
/// the caller sends to the model, such as `{"status": "running", "run_id": 1}`.
///
/// `Status` and each of its variants are `#[non_exhaustive]`, so a later release can add a
/// status or a field without a breaking change. A `match` on it needs a `_` arm.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(tag = "status", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Status {
    /// The script ended, and this is the value of its last expression.
    #[non_exhaustive]
    Finished {
        /// The value as JSON.
        value: Value,
        /// What the script printed to stdout, if it printed anything there. In
        /// `backend::MontyPool`, a print that would take the script's prints on stdout and stderr
        /// together past 10 MiB is cut at or before 10 MiB, every later print is dropped, and the
        /// script keeps running.
        #[serde(skip_serializing_if = "Option::is_none")]
        stdout: Option<String>,
        /// What the script printed to stderr with `print(..., file=sys.stderr)`, if anything.
        #[serde(skip_serializing_if = "Option::is_none")]
        stderr: Option<String>,
        /// Whether the session started fresh before this script, with no globals. Serialized
        /// only when true.
        #[serde(skip_serializing_if = "std::ops::Not::not")]
        restarted: bool,
    },
    /// A tool call was still unanswered when the wait ended. Poll this run id for the rest.
    #[non_exhaustive]
    Running {
        /// The id to poll.
        run_id: u64,
    },
    /// The script or the request failed.
    #[non_exhaustive]
    Failed {
        /// What went wrong, such as the script's traceback.
        error: String,
        /// Every tool call the host answered in the run, refused and failed ones too. Each
        /// call's arguments, keys and values both counted, are truncated to the call's share of
        /// [`Limits::output_chars`], which is `output_chars / calls_per_execute`, so the
        /// calls' arguments together stay within `output_chars`. Each argument, in the order of
        /// `args`, gets what is left of the share. The arguments past the share are dropped, and
        /// the call's JSON gains a `truncated_args` key next to `args`, which holds their JSON
        /// text truncated to what is left. Kept arguments from the end of `args` join the dropped
        /// ones until that text's marker fits. If no argument is kept and the share is smaller
        /// than the marker, `truncated_args` is the marker alone.
        calls: Vec<host::Call>,
        /// What the script printed to stdout before the failure, if it printed anything there.
        /// A print that would take the script's prints on stdout and stderr together past
        /// 10 MiB raises `MemoryError` in the script, and only the prints before it are kept. In
        /// `backend::MontyPool` that print is cut instead, as `Finished`'s `stdout` says.
        #[serde(skip_serializing_if = "Option::is_none")]
        stdout: Option<String>,
        /// What the script printed to stderr with `print(..., file=sys.stderr)` before the
        /// failure, if anything. The traceback is in `error`, not here.
        #[serde(skip_serializing_if = "Option::is_none")]
        stderr: Option<String>,
        /// Whether the session started fresh before this script, with no globals. Serialized
        /// only when true.
        #[serde(skip_serializing_if = "std::ops::Not::not")]
        restarted: bool,
    },
}

/// Why the caller's [`Sandbox::dump`] or [`Builder::restore`] failed.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// The session is held by the run with this id, which `execute` left running.
    RunOpen {
        /// The open run's id.
        run_id: u64,
    },
    /// The backend can't write the session as bytes, for this reason.
    Dump(Box<dyn error::Error + Send + Sync>),
    /// The bytes are too short to be a dump, start with another format version, or the backend
    /// can't load them as a session, for this reason.
    Restore(Box<dyn error::Error + Send + Sync>),
    /// The bytes don't end in the tag that the builder's signing key makes for them: the key
    /// didn't sign them, or they changed since.
    SignatureMismatch,
}

/// Where the sandbox's session is: idle between scripts, or held by a suspended run.
enum State<B: Backend> {
    Idle(B::Session),
    Suspended(Run<B>),
}

/// A script suspended at a tool call, with the host's answer still to be picked up.
struct Run<B: Backend> {
    answered_calls: Vec<host::Call>,
    call: host::Call,
    suspension: B::Suspension,
    /// The host's answer to `call`, or `None` once the call timeout passes first.
    answer: BoxFuture<'static, Option<Result<Value, host::Error>>>,
    idle_since: Instant,
}

impl Default for Limits {
    fn default() -> Self {
        Self {
            run_time: Duration::from_secs(30),
            memory_bytes: 256 * 1024 * 1024,
            recursion_depth: 1000,
            calls_per_execute: 100,
            call_timeout: None,
            idle_timeout: None,
            output_chars: 40_000,
        }
    }
}

/// Awaits the `Status` future `step` in an info span named `name`, which records the run id, the
/// status, the call count and the duration.
macro_rules! trace_step {
    ($name:literal, $step:expr) => {{
        let span = tracing::info_span!(
            $name,
            run_id = Empty,
            status = Empty,
            call_count = Empty,
            duration = Empty
        );
        let started = Instant::now();
        let status: Status = $step.instrument(span.clone()).await;
        record_end(&span, status.as_str(), started);
        status
    }};
}

impl<H: Host, B: Backend> Sandbox<H, B> {
    /// The tools that [`Builder::new`] read from the host, which are the tools scripts call.
    /// Pass them to [`crate::schema::mcp_definitions`] or [`crate::schema::openai_definitions`]
    /// for the definitions the model sees.
    pub fn tools(&self) -> &[Tool] {
        &self.builder.tools
    }

    /// Runs `code`, waiting up to the execute wait for its tool calls. A script that fails is a
    /// `failed` status, as is a request without `restart` while another run is still running. If
    /// a run waited past its idle timeout with no answer from the host, it expires and `execute`
    /// answers its `failed` status without running `code`. With `restart`, the session starts
    /// fresh before `code` runs, as [`Sandbox::reset`] does, so the script sees no globals and
    /// polling a run that was open answers `failed`. A session lost to a dropped `execute`,
    /// `poll`, `cancel` or `dump` future also starts fresh, and the next finished or failed
    /// status says `restarted`.
    pub async fn execute(&mut self, code: &str, restart: bool) -> Status {
        trace_step!("execute", async {
            if let Some(status) = self.expire_idle_run().await {
                return status;
            }
            if restart || self.state.is_none() {
                self.reset().await;
            }
            let session = match self.take_session() {
                Ok(session) => session,
                Err(error) => return Status::failed(error.to_string()),
            };
            self.last_run_id += 1;
            Span::current()
                .record("run_id", self.last_run_id)
                .record("call_count", 0);
            let progress = self
                .builder
                .backend
                .feed_start(session, code, &self.builder.tools)
                .await;
            if let Some(status) = self.suspend_or_end(Vec::new(), progress).await {
                return status;
            }
            self.wait_timeout(self.builder.execute_wait).await
        })
    }

    /// Continues the run with `run_id` that `execute` left running, and answers `running` again
    /// if the host still owes it an answer when the poll wait ends.
    pub async fn poll(&mut self, run_id: u64) -> Status {
        trace_step!("poll", async {
            Span::current().record("run_id", run_id);
            if !self.is_open_run(run_id) {
                return Status::failed(format!("no run {run_id}"));
            }
            match self.expire_idle_run().await {
                Some(status) => status,
                None => self.wait_timeout(self.builder.poll_wait).await,
            }
        })
    }

    /// Cancels the run with `run_id` that `execute` left running, and says whether that run was
    /// open. The session is idle again with the globals the script had set, unless the backend
    /// lost them, which the next status reports as `restarted`. The host's answer to the call the
    /// script waits on is dropped. Cancelling any other run changes nothing.
    pub async fn cancel(&mut self, run_id: u64) -> bool {
        let span = tracing::info_span!("cancel", run_id, status = Empty, duration = Empty);
        let started = Instant::now();
        let is_open = self.is_open_run(run_id);
        let Some(run) = self.take_run_if(|_| is_open) else {
            record_end(&span, "not_open", started);
            return false;
        };
        let message = format!("run {run_id} was cancelled");
        let ending = self.builder.backend.abandon(run.suspension, message).await;
        self.state = Some(State::Idle(ending.session));
        self.unreported_restart |= ending.lost_globals;
        record_end(&span, "cancelled", started);
        true
    }

    /// Replaces the session with an empty one, and marks the next finished or failed status
    /// `restarted`. An open run is dropped with the host's answer to its call, so polling it
    /// answers `failed`.
    pub async fn reset(&mut self) {
        self.state = Some(State::Idle(
            self.builder.backend.new_session(&self.builder.limits).await,
        ));
        self.unreported_restart = true;
    }

    /// The idle session as bytes that [`Builder::restore`] loads into a new sandbox, with the
    /// last run id and whether the next finished or failed status owes the model `restarted`.
    /// With a signing key, the bytes end in its HMAC-SHA256 tag. While a run is open this is
    /// [`Error::RunOpen`], and bytes the backend can't write are [`Error::Dump`]. A session lost
    /// to a dropped `execute`, `poll`, `cancel` or `dump` future starts fresh first.
    pub async fn dump(&mut self) -> Result<Vec<u8>, Error> {
        if self.state.is_none() {
            self.reset().await;
        }
        let mut session = self.take_session()?;
        let (dump, lost_globals) = self.builder.backend.dump(&mut session).await;
        self.state = Some(State::Idle(session));
        self.unreported_restart |= lost_globals;
        let mut bytes = vec![DUMP_FORMAT_VERSION, u8::from(self.unreported_restart)];
        bytes.extend(self.last_run_id.to_le_bytes());
        bytes.extend(dump.map_err(Error::Dump)?);
        if let Some(key) = self.builder.signing_key {
            let tag = compute_hmac(&key, &bytes).finalize().into_bytes();
            bytes.extend_from_slice(&tag);
        }
        Ok(bytes)
    }

    /// Takes the idle session out of the sandbox, whose state stays `None` until the session is
    /// put back. While a run is open this is [`Error::RunOpen`], and the run stays in the sandbox.
    fn take_session(&mut self) -> Result<B::Session, Error> {
        match self.state.take() {
            Some(State::Idle(session)) => Ok(session),
            suspended => {
                self.state = suspended;
                Err(Error::RunOpen {
                    run_id: self.last_run_id,
                })
            }
        }
    }

    /// Whether `run_id` is the id of the run `execute` left running.
    fn is_open_run(&self, run_id: u64) -> bool {
        matches!(self.state, Some(State::Suspended(_))) && run_id == self.last_run_id
    }

    /// Takes the suspended run out of the sandbox if `condition` holds for it.
    fn take_run_if(&mut self, condition: impl FnOnce(&Run<B>) -> bool) -> Option<Run<B>> {
        match self.state.take() {
            Some(State::Suspended(run)) if condition(&run) => Some(run),
            state => {
                self.state = state;
                None
            }
        }
    }

    /// Waits up to `wait` for the suspended run, answering its tool calls as the host answers
    /// them. The run stays suspended in the sandbox while it waits on the host, so dropping this
    /// future then loses nothing. Dropping it while the backend resumes or aborts the run loses
    /// the session.
    async fn wait_timeout(&mut self, wait: Duration) -> Status {
        let mut timer = Delay::new(wait);
        loop {
            let Some(State::Suspended(run)) = &mut self.state else {
                panic!("wait_timeout without a suspended run");
            };
            let answer = match future::select(&mut run.answer, &mut timer).await {
                Either::Left((answer, _)) => answer,
                Either::Right(_) => {
                    run.idle_since = Instant::now();
                    Span::current().record("call_count", run.answered_calls.len());
                    return Status::Running {
                        run_id: self.last_run_id,
                    };
                }
            };
            let Some(State::Suspended(run)) = self.state.take() else {
                panic!("wait_timeout without a suspended run");
            };
            let Run {
                mut answered_calls,
                call,
                suspension,
                ..
            } = run;
            let progress = match answer {
                Some(answer) => {
                    answered_calls.push(output::truncate_args(call, &self.builder.limits));
                    Span::current().record("call_count", answered_calls.len());
                    self.builder
                        .backend
                        .resume(suspension, answer, &self.builder.tools)
                        .await
                }
                None => {
                    let message = format!("{} passed the call timeout", call.tool_id);
                    let ending = self.builder.backend.abort(suspension, message).await;
                    return self.end_run(ending, answered_calls);
                }
            };
            if let Some(status) = self.suspend_or_end(answered_calls, progress).await {
                return status;
            }
        }
    }

    /// Suspends the last run at the tool call `progress` reached, or ends it with its status. A
    /// call past the call limit ends the run instead of reaching the host.
    async fn suspend_or_end(
        &mut self,
        answered_calls: Vec<host::Call>,
        progress: Progress<B>,
    ) -> Option<Status> {
        let call_limit = self.builder.limits.calls_per_execute;
        let ending = match progress {
            Progress::Call { call, suspension } if answered_calls.len() < call_limit => {
                let answer = self.call_host(&call);
                self.state = Some(State::Suspended(Run {
                    answered_calls,
                    call,
                    suspension,
                    answer,
                    idle_since: Instant::now(),
                }));
                return None;
            }
            Progress::Call { suspension, .. } => {
                let message = format!("the script reached the call limit of {call_limit}");
                self.builder.backend.abort(suspension, message).await
            }
            Progress::Ended(ending) => ending,
        };
        Some(self.end_run(ending, answered_calls))
    }

    /// Ends the suspended run if it has waited past the idle timeout without an answer from the
    /// host, and says it expired. A call past the call timeout counts as unanswered. A run whose
    /// host answered stays for the next wait to resume.
    async fn expire_idle_run(&mut self) -> Option<Status> {
        let idle_timeout = self.builder.limits.idle_timeout?;
        if let Some(State::Suspended(run)) = &mut self.state
            && let Some(answer) = (&mut run.answer).now_or_never()
        {
            let host_answered = answer.is_some();
            run.answer = future::ready(answer).boxed();
            if host_answered {
                return None;
            }
        }
        let run = self.take_run_if(|run| run.idle_since.elapsed() >= idle_timeout)?;
        Span::current().record("run_id", self.last_run_id);
        let message = format!("run {} expired", self.last_run_id);
        let ending = self.builder.backend.abandon(run.suspension, message).await;
        Some(self.end_run(ending, run.answered_calls))
    }

    /// Ends the run: the `ending`'s session is idle again, and the script's result and what it
    /// printed to stdout and stderr become the status. The result gets its share of
    /// [`Limits::output_chars`] against both streams together, and the two streams then share
    /// the rest. The status says `restarted` if the session lost its globals since the last
    /// finished or failed status, and the next status does if the backend lost them in this run.
    fn end_run(&mut self, ending: Ending<B>, answered_calls: Vec<host::Call>) -> Status {
        let Ending {
            result,
            streams,
            session,
            lost_globals,
        } = ending;
        self.state = Some(State::Idle(session));
        let result_text = match &result {
            Ok(value) => to_text(value),
            Err(error) => Cow::Borrowed(error.as_str()),
        };
        let result_chars = result_text.chars().count();
        let Streams { stdout, stderr } = streams;
        let (stdout_chars, stderr_chars) = (stdout.chars().count(), stderr.chars().count());
        let (result_share, streams_share) = split_budget(
            self.builder.limits.output_chars,
            result_chars,
            stdout_chars + stderr_chars,
        );
        let (stdout_share, stderr_share) = split_budget(streams_share, stdout_chars, stderr_chars);
        let result_truncated = truncate_text(&result_text, result_chars, result_share);
        drop(result_text);
        let stdout = (!stdout.is_empty())
            .then(|| truncate_text(&stdout, stdout_chars, stdout_share).unwrap_or(stdout));
        let stderr = (!stderr.is_empty())
            .then(|| truncate_text(&stderr, stderr_chars, stderr_share).unwrap_or(stderr));
        let restarted = mem::replace(&mut self.unreported_restart, lost_globals);
        match result {
            Ok(value) => Status::Finished {
                value: result_truncated.map_or(value, Value::String),
                stdout,
                stderr,
                restarted,
            },
            Err(error) => Status::Failed {
                error: result_truncated.unwrap_or(error),
                calls: answered_calls,
                stdout,
                stderr,
                restarted,
            },
        }
    }

    /// Sends `call`, made by the last run, to the host and traces its answer in a `call` span
    /// with no parent. The answer is `None` if the call timeout passes first.
    fn call_host(
        &self,
        call: &host::Call,
    ) -> BoxFuture<'static, Option<Result<Value, host::Error>>> {
        let run_id = self.last_run_id;
        let span = tracing::info_span!(parent: None, "call", run_id, tool_id = %call.tool_id, status = Empty, duration = Empty);
        let answer = self.builder.host.call(call);
        let timeout = self.builder.limits.call_timeout.map(Delay::new);
        let mut trace = CallTrace {
            span: span.clone(),
            started: Instant::now(),
            status: "dropped",
        };
        async move {
            let answer = match timeout {
                Some(timeout) => match future::select(answer, timeout).await {
                    Either::Left((answer, _)) => Some(answer),
                    Either::Right(_) => None,
                },
                None => Some(answer.await),
            };
            trace.status = match &answer {
                Some(Ok(_)) => "answered",
                Some(Err(host::Error::Refused(_))) => "refused",
                Some(Err(host::Error::Failed(_))) => "failed",
                None => "timed_out",
            };
            drop(trace);
            answer
        }
        .instrument(span)
        .boxed()
    }
}

impl<H: Host, B: Backend> Builder<H, B> {
    /// A builder for a sandbox whose scripts call `host`'s tools and run in `backend`. It reads
    /// the host's tool list here, once: a tool the host lists later is not callable in the
    /// sandbox, and the backend gets the tools on every call that runs a script.
    pub fn new(host: H, backend: B) -> Self {
        let tools = host.tools();
        Self {
            host,
            tools,
            backend,
            execute_wait: Duration::from_secs(30),
            poll_wait: Duration::from_secs(10),
            limits: Limits::default(),
            signing_key: None,
        }
    }

    /// How long `execute` waits on the host before it answers `running`. The default is 30 s.
    pub fn execute_wait(mut self, wait: Duration) -> Self {
        self.execute_wait = wait;
        self
    }

    /// How long `poll` waits on the host before it answers `running` again. The default is 10 s.
    pub fn poll_wait(mut self, wait: Duration) -> Self {
        self.poll_wait = wait;
        self
    }

    /// What a script may use before the sandbox stops it. The default is [`Limits::default`].
    pub fn limits(mut self, limits: Limits) -> Self {
        self.limits = limits;
        self
    }

    /// The secret key that signs dumps. With it, [`Sandbox::dump`] appends an HMAC-SHA256 tag,
    /// and [`Builder::restore`] checks the tag before the backend reads a byte. The default is
    /// unset, no signing.
    ///
    /// The tag proves the key signed the bytes. It doesn't hide them, and any dump the key
    /// signed restores, including an older one. Use the key only for dumps. The key isn't wiped
    /// from memory when the sandbox drops.
    pub fn signing_key(mut self, key: [u8; 32]) -> Self {
        self.signing_key = Some(key);
        self
    }

    /// The sandbox, with an empty Python session.
    pub async fn build(self) -> Sandbox<H, B> {
        let session = self.backend.new_session(&self.limits).await;
        self.build_with_session(session, 0, false)
    }

    /// The sandbox, with the session that `bytes` from [`Sandbox::dump`] hold, running under
    /// this builder's limits, though `backend::MontyPool` keeps the dumped session's limits
    /// where those are stricter. Its run ids go on from the dumped sandbox's last one, and its
    /// next finished or failed status says `restarted` if the dumped sandbox's would have. Bytes
    /// too short to be a dump, that start with another format version, or that the backend can't
    /// load, are [`Error::Restore`].
    ///
    /// With a [signing key](Builder::signing_key), bytes that don't end in the key's tag for
    /// them are [`Error::SignatureMismatch`], checked in constant time before any other byte is
    /// read. Without a key, restore trusts the bytes, so restore only bytes you made.
    ///
    /// A failed restore returns the error together with this builder, boxed, so the caller can
    /// [`build`](Builder::build) a fresh sandbox over the same host and backend.
    pub async fn restore(self, bytes: &[u8]) -> Result<Sandbox<H, B>, (Error, Box<Self>)> {
        match Self::read_dump(&self.backend, &self.limits, self.signing_key, bytes).await {
            Ok((session, last_run_id, unreported_restart)) => {
                Ok(self.build_with_session(session, last_run_id, unreported_restart))
            }
            Err(error) => Err((error, Box::new(self))),
        }
    }

    /// The session that `bytes` from [`Sandbox::dump`] hold, loaded by `backend` under
    /// `limits` and checked against `signing_key`, with the dumped sandbox's last run id and
    /// whether its next status says `restarted`.
    async fn read_dump(
        backend: &B,
        limits: &Limits,
        signing_key: Option<[u8; 32]>,
        bytes: &[u8],
    ) -> Result<(B::Session, u64, bool), Error> {
        let dump = match signing_key {
            Some(key) => {
                let (dump, tag) = bytes
                    .split_last_chunk::<TAG_BYTES>()
                    .ok_or(Error::SignatureMismatch)?;
                compute_hmac(&key, dump)
                    .verify_slice(tag)
                    .map_err(|_| Error::SignatureMismatch)?;
                dump
            }
            None => bytes,
        };
        let ([version, restart_byte, last_run_id_bytes @ ..], session_bytes) = dump
            .split_first_chunk::<HEADER_BYTES>()
            .ok_or_else(|| Error::Restore("the bytes are too short to be a dump".into()))?;
        if *version != DUMP_FORMAT_VERSION {
            return Err(Error::Restore(
                format!("the dump is format version {version}, not version {DUMP_FORMAT_VERSION}")
                    .into(),
            ));
        }
        let session = backend
            .restore(session_bytes, limits)
            .await
            .map_err(Error::Restore)?;
        Ok((
            session,
            u64::from_le_bytes(*last_run_id_bytes),
            *restart_byte != 0,
        ))
    }

    /// The sandbox, with `session` as its idle Python session, `last_run_id` as the id its next
    /// run counts on from, and `unreported_restart` as whether its next finished or failed status
    /// says `restarted`.
    fn build_with_session(
        self,
        session: B::Session,
        last_run_id: u64,
        unreported_restart: bool,
    ) -> Sandbox<H, B> {
        Sandbox {
            builder: self,
            state: Some(State::Idle(session)),
            last_run_id,
            unreported_restart,
        }
    }
}

impl<H, B: Backend> fmt::Debug for Sandbox<H, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Sandbox")
            .field("builder", &self.builder)
            .field("last_run_id", &self.last_run_id)
            .finish_non_exhaustive()
    }
}

impl<H, B> fmt::Debug for Builder<H, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Builder")
            .field("tools", &self.tools)
            .field("execute_wait", &self.execute_wait)
            .field("poll_wait", &self.poll_wait)
            .field("limits", &self.limits)
            .finish_non_exhaustive()
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::RunOpen { run_id } => write!(f, "run {run_id} is still running"),
            Self::Dump(_) => f.write_str("can't dump the session"),
            Self::Restore(_) => f.write_str("can't restore the session"),
            Self::SignatureMismatch => f.write_str("the signing key didn't sign these bytes"),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Self::RunOpen { .. } | Self::SignatureMismatch => None,
            Self::Dump(error) | Self::Restore(error) => Some(error.as_ref()),
        }
    }
}

impl Status {
    /// A `failed` status with `error`, no answered calls, no printed output and no restart.
    fn failed(error: String) -> Self {
        Self::Failed {
            error,
            calls: Vec::new(),
            stdout: None,
            stderr: None,
            restarted: false,
        }
    }

    /// The status's `status` tag in the model's JSON, such as `"running"`.
    fn as_str(&self) -> &'static str {
        match self {
            Self::Finished { .. } => "finished",
            Self::Running { .. } => "running",
            Self::Failed { .. } => "failed",
        }
    }
}

/// The HMAC-SHA256 of `bytes` under `key`, to finalize as a tag or to check a tag against.
fn compute_hmac(key: &[u8], bytes: &[u8]) -> Hmac<Sha256> {
    let mut hmac =
        Hmac::<Sha256>::new_from_slice(key).expect("HMAC-SHA256 takes a key of any length");
    hmac.update(bytes);
    hmac
}

/// A `call` span that records its status and duration when this value drops: the status the
/// host's answer set, or `dropped` if the call's future dropped before the answer came.
struct CallTrace {
    span: Span,
    started: Instant,
    status: &'static str,
}

impl Drop for CallTrace {
    fn drop(&mut self) {
        record_end(&self.span, self.status, self.started);
    }
}

/// Records on `span` the status a traced step ended with and how long it took since `started`.
fn record_end(span: &Span, status: &'static str, started: Instant) {
    span.record("status", status);
    span.record("duration", field::debug(started.elapsed()));
}