interpretthis 0.2.0

Sandboxed Python AST interpreter for untrusted and LLM-generated code
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
// Copyright 2026 Thomas Santerre and Moderately AI Inc.
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#![expect(
    clippy::unwrap_used,
    clippy::items_after_statements,
    reason = "integration-test helper impls aren't detected as test context by clippy's \
              in-tests allowlist, and per-test local struct definitions are the idiomatic \
              scoping for mock ToolHandlers"
)]

//! Resource-limit boundary tests: op counter, while-iteration cap, recursion
//! depth, wall-clock execution timeout, and concurrent-tool semaphore. Also
//! covers proxy-related stress concerns that show up at limit boundaries —
//! ordering, error propagation, falsy-value resolution, nested data — since
//! those classes of bug are easiest to expose with a many-call fan-out.

use std::{
    collections::HashMap,
    fmt::Write as _,
    sync::{Arc, Mutex},
    time::Duration,
};

use async_trait::async_trait;
use interpretthis::{
    Interpreter, InterpreterConfig, InterpreterDeps, ToolDefinition, ToolError, ToolHandler, Tools,
    Value,
};

fn no_tools() -> Tools {
    Tools::new()
}

fn stress_interpreter() -> Interpreter {
    // Bigger budget than the default so the many-call stress tests run to
    // completion without tripping the op counter — the assertions here are
    // about concurrency/proxy correctness, not resource caps.
    let mut cfg = InterpreterConfig::default();
    cfg.max_operations = 500_000;
    cfg.max_while_iterations = 10_000;
    Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg)
}

// --- Tool fixtures used by the stress / proxy-behaviour cases ---

struct CountingTool {
    count: Arc<Mutex<u32>>,
}

#[async_trait]
impl ToolHandler for CountingTool {
    async fn call(&self, kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
        let i = {
            // MutexGuard must drop before the .await to avoid holding a sync
            // lock across an await point.
            let mut c = self.count.lock().unwrap();
            *c += 1;
            match kwargs.get("i") {
                Some(Value::Int(v)) => *v,
                _ => i64::from(*c) - 1,
            }
        };
        tokio::time::sleep(Duration::from_millis(20)).await;
        Ok(Value::Int(i))
    }
}

struct ConcurrencyCountingTool {
    active: Arc<Mutex<u32>>,
    peak: Arc<Mutex<u32>>,
    total: Arc<Mutex<u32>>,
}

#[async_trait]
impl ToolHandler for ConcurrencyCountingTool {
    async fn call(&self, kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
        let idx = {
            let mut t = self.total.lock().unwrap();
            let idx = *t;
            *t += 1;
            idx
        };
        {
            let mut a = self.active.lock().unwrap();
            *a += 1;
            let mut p = self.peak.lock().unwrap();
            if *a > *p {
                *p = *a;
            }
        }
        let delay = match kwargs.get("delay") {
            Some(Value::Float(d)) => Duration::from_secs_f64(*d),
            _ => Duration::from_millis(20),
        };
        tokio::time::sleep(delay).await;
        {
            let mut a = self.active.lock().unwrap();
            *a -= 1;
        }
        Ok(Value::Int(i64::from(idx)))
    }
}

struct MaybeFailTool;

#[async_trait]
impl ToolHandler for MaybeFailTool {
    async fn call(&self, kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
        tokio::time::sleep(Duration::from_millis(10)).await;
        let should_fail = matches!(kwargs.get("fail"), Some(Value::Bool(true)));
        if should_fail {
            Err(ToolError::new("deliberate_failure"))
        } else {
            Ok(Value::String("ok".into()))
        }
    }
}

struct FetchTool;

#[async_trait]
impl ToolHandler for FetchTool {
    async fn call(&self, kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
        tokio::time::sleep(Duration::from_millis(10)).await;
        let v = kwargs.get("v").cloned().unwrap_or(Value::Int(0));
        Ok(v)
    }
}

struct BoolTool {
    value: bool,
}

#[async_trait]
impl ToolHandler for BoolTool {
    async fn call(&self, _kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
        tokio::time::sleep(Duration::from_millis(10)).await;
        Ok(Value::Bool(self.value))
    }
}

struct IntTool {
    value: i64,
}

#[async_trait]
impl ToolHandler for IntTool {
    async fn call(&self, _kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
        tokio::time::sleep(Duration::from_millis(10)).await;
        Ok(Value::Int(self.value))
    }
}

struct EmptyStringTool;

#[async_trait]
impl ToolHandler for EmptyStringTool {
    async fn call(&self, _kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
        tokio::time::sleep(Duration::from_millis(10)).await;
        Ok(Value::String("".into()))
    }
}

// --- Operation counter ---

#[tokio::test]
async fn resource_limits_terminate_runaway_loop_via_op_counter() {
    let mut cfg = InterpreterConfig::default();
    cfg.max_operations = 100;
    let interp = Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg);
    let resp = interp
        .execute(
            r"
x = 0
for i in range(1000):
    x = x + 1
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_some());
    let err_msg = format!("{:?}", resp.error.unwrap());
    assert!(
        err_msg.contains("limit") || err_msg.contains("operation"),
        "error should mention limit: {err_msg}"
    );
}

#[tokio::test]
async fn resource_limits_normal_operations_within_default_budget() {
    let interp =
        Interpreter::new(InterpreterDeps { tools: Tools::new() }, InterpreterConfig::default());
    let resp = interp
        .execute(
            r"
total = 0
for i in range(100):
    total += i
print(total)
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
}

#[tokio::test]
async fn resource_limits_nested_loop_within_default_budget() {
    let interp =
        Interpreter::new(InterpreterDeps { tools: Tools::new() }, InterpreterConfig::default());
    let resp = interp
        .execute(
            r"
total = 0
for i in range(10):
    for j in range(10):
        total += i * j
print(total)
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
}

// --- While-iteration cap ---

#[tokio::test]
async fn resource_limits_terminate_while_loop_via_iteration_cap() {
    let mut cfg = InterpreterConfig::default();
    cfg.max_while_iterations = 50;
    let interp = Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg);
    let resp = interp
        .execute(
            r"
x = 0
while True:
    x += 1
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_some());
}

#[tokio::test]
async fn resource_limits_while_loop_with_break_succeeds() {
    let mut cfg = InterpreterConfig::default();
    cfg.max_while_iterations = 1000;
    let interp = Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg);
    let resp = interp
        .execute(
            r"
x = 0
while True:
    x += 1
    if x >= 10:
        break
print(x)
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
}

// --- Recursion-depth cap ---

/// Unbounded user-function recursion must surface as `RecursionLimitExceeded`
/// rather than bleeding the memory budget. Prior to #401 the interpreter had
/// no frame-depth cap; a `def f(): f()` exhausted memory and reported
/// `LimitExceeded(memory…)`. Uses a small cap (10) in tests: the harness runs
/// on a 2 MB-stack thread, and debug-mode async state machines inflate per
/// frame. The production default (1000) is configured via `InterpreterConfig`.
#[tokio::test]
async fn resource_limits_recursion_unbounded_self_call_errors() {
    let mut cfg = InterpreterConfig::default();
    cfg.max_recursion_depth = 5;
    let interp = Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg);
    let resp = interp
        .execute(
            r"
def f():
    return f()

f()
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    let err = resp.error.expect("infinite recursion must surface an error");
    let msg = format!("{err:?}");
    assert!(
        msg.contains("RecursionLimitExceeded") || msg.contains("maximum recursion depth"),
        "expected RecursionLimitExceeded, got: {msg}"
    );
}

/// Recursion that stays under the cap must succeed — confirms the counter is
/// decremented on exit and does not leak across calls.
#[tokio::test]
async fn resource_limits_recursion_under_cap_resets_counter() {
    let mut cfg = InterpreterConfig::default();
    cfg.max_recursion_depth = 8;
    let interp = Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg);
    let resp = interp
        .execute(
            r"
def count(n):
    if n == 0:
        return 0
    return 1 + count(n - 1)

# Shallow depth: native stack per frame is large; stay well under both
# the interpreter cap and the host stack ceiling.
a = count(2)
b = count(2)
print(a, b)
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
}

/// Recursion via lambda must also be bounded.
#[tokio::test]
async fn resource_limits_recursion_applies_to_lambdas() {
    let mut cfg = InterpreterConfig::default();
    // Set the cap just below the native-stack ceiling for the lambda
    // recursion path. Bumping requires further per-frame future-size
    // reduction in `call_lambda`; the def-time default-evaluation
    // landing enlarged `FunctionParams` slightly and trimmed the
    // headroom back from 8 to 6.
    cfg.max_recursion_depth = 3;
    let interp = Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg);
    let resp = interp
        .execute(
            r"
f = lambda g, n: 0 if n == 0 else g(g, n - 1)
f(f, 30)
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    let err = resp.error.expect("lambda recursion past limit must error");
    let msg = format!("{err:?}");
    assert!(
        msg.contains("RecursionLimitExceeded") || msg.contains("maximum recursion depth"),
        "expected RecursionLimitExceeded, got: {msg}"
    );
}

// --- Wall-clock execution timeout ---

#[tokio::test]
async fn resource_limits_terminate_via_wallclock_timeout() {
    let interp = {
        let mut cfg = InterpreterConfig::default();
        cfg.max_execution_time = Some(Duration::from_millis(50));
        Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg)
    };

    let resp = interp
        .execute(
            r"
x = 0
while True:
    x += 1
",
            &no_tools(),
            HashMap::new(),
        )
        .await;
    assert!(!resp.is_ok());
    let err = format!("{:?}", resp.error.unwrap());
    assert!(err.contains("time") || err.contains("execution"), "error should mention time: {err}");
}

// --- Concurrent-tool semaphore cap ---
//
// The `test_stress.rs` original ran two semaphore tests (cap=5 and cap=2);
// they exercise the same code path with different bounds. Keep the tighter
// case as the canonical assertion — a cap=2 violation is the more sensitive
// detector of a stale-counter bug.

#[tokio::test]
async fn resource_limits_semaphore_caps_concurrent_tool_calls() {
    let active = Arc::new(Mutex::new(0u32));
    let peak = Arc::new(Mutex::new(0u32));
    let total = Arc::new(Mutex::new(0u32));

    let mut tools = Tools::new();
    tools.insert(
        "track",
        ToolDefinition {
            handler: Arc::new(ConcurrencyCountingTool {
                active: active.clone(),
                peak: peak.clone(),
                total: total.clone(),
            }),
            parallelizable: true,
        },
    );

    let mut cfg = InterpreterConfig::default();
    cfg.max_operations = 500_000;
    cfg.max_while_iterations = 10_000;
    cfg.max_concurrent_tools = 2;
    let interp = Interpreter::new(InterpreterDeps { tools: Tools::new() }, cfg);

    let assignments: String = (0..15).fold(String::new(), |mut s, i| {
        let _ = writeln!(&mut s, "r{i} = track()");
        s
    });
    let uses: String = (0..15).map(|i| format!("r{i}")).collect::<Vec<_>>().join(" + ");
    let code = format!("{assignments}total = {uses}\nprint(total)");

    let resp = interp.execute(&code, &tools, HashMap::new()).await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
    let peak_val = *peak.lock().unwrap();
    assert!(peak_val <= 2, "peak {peak_val} exceeded limit 2");
    assert_eq!(*total.lock().unwrap(), 15);
}

// --- Proxy-resolution stress (large fan-out, error propagation, falsy values, nested) ---

#[tokio::test]
async fn resource_limits_fan_out_20_concurrent_calls_preserve_order() {
    let count = Arc::new(Mutex::new(0u32));
    let mut tools = Tools::new();
    tools.insert(
        "count",
        ToolDefinition {
            handler: Arc::new(CountingTool { count: count.clone() }),
            parallelizable: true,
        },
    );

    let interp = stress_interpreter();
    let assignments: String = (0..20).fold(String::new(), |mut s, i| {
        let _ = writeln!(&mut s, "r{i} = count(i={i})");
        s
    });
    let checks: String = (0..20).map(|i| format!("r{i} == {i}")).collect::<Vec<_>>().join(" and ");
    let code = format!("{assignments}all_correct = {checks}\nprint(all_correct)");

    let resp = interp.execute(&code, &tools, HashMap::new()).await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
    assert_eq!(resp.stdout.trim(), "True");
    assert_eq!(*count.lock().unwrap(), 20);
}

#[tokio::test]
async fn resource_limits_single_failure_among_many_propagates() {
    let mut tools = Tools::new();
    tools.insert(
        "maybe_fail",
        ToolDefinition { handler: Arc::new(MaybeFailTool), parallelizable: true },
    );

    let interp = stress_interpreter();
    let code = r#"
results = []
for i in range(10):
    results.append(maybe_fail(fail=(i == 7)))
output = []
for r in results:
    output.append(str(r))
print(",".join(output))
"#;
    let resp = interp.execute(code, &tools, HashMap::new()).await;
    assert!(resp.error.is_some(), "should have errored on index 7");
}

#[tokio::test]
async fn resource_limits_error_does_not_corrupt_subsequent_execution() {
    let mut tools = Tools::new();

    struct FailTool;
    #[async_trait]
    impl ToolHandler for FailTool {
        async fn call(&self, _kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
            Err(ToolError::new("boom"))
        }
    }
    struct WorkTool;
    #[async_trait]
    impl ToolHandler for WorkTool {
        async fn call(&self, _kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
            Ok(Value::String("works".into()))
        }
    }

    tools.insert("failing", ToolDefinition { handler: Arc::new(FailTool), parallelizable: true });
    tools.insert("working", ToolDefinition { handler: Arc::new(WorkTool), parallelizable: true });

    let interp = stress_interpreter();

    let resp1 = interp.execute("result = failing()\nprint(result)", &tools, HashMap::new()).await;
    assert!(resp1.error.is_some());

    let resp2 = interp.execute("result = working()\nprint(result)", &tools, HashMap::new()).await;
    assert!(resp2.error.is_none(), "error: {:?}", resp2.error);
    assert_eq!(resp2.stdout.trim(), "works");
}

#[tokio::test]
async fn resource_limits_proxy_resolves_false_as_falsy() {
    let mut tools = Tools::new();
    tools.insert(
        "get_false",
        ToolDefinition { handler: Arc::new(BoolTool { value: false }), parallelizable: true },
    );

    let interp = stress_interpreter();
    let resp = interp
        .execute(
            "val = get_false()\nif val:\n    print('truthy')\nelse:\n    print('falsy')",
            &tools,
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
    assert_eq!(resp.stdout.trim(), "falsy");
}

#[tokio::test]
async fn resource_limits_proxy_resolves_zero_as_falsy() {
    let mut tools = Tools::new();
    tools.insert(
        "get_zero",
        ToolDefinition { handler: Arc::new(IntTool { value: 0 }), parallelizable: true },
    );

    let interp = stress_interpreter();
    let resp = interp
        .execute(
            "val = get_zero()\nif val:\n    print('truthy')\nelse:\n    print('falsy')",
            &tools,
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
    assert_eq!(resp.stdout.trim(), "falsy");
}

#[tokio::test]
async fn resource_limits_proxy_resolves_empty_string_as_falsy() {
    let mut tools = Tools::new();
    tools.insert(
        "get_empty",
        ToolDefinition { handler: Arc::new(EmptyStringTool), parallelizable: true },
    );

    let interp = stress_interpreter();
    let resp = interp
        .execute(
            "val = get_empty()\nif val:\n    print('truthy')\nelse:\n    print('falsy')",
            &tools,
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
    assert_eq!(resp.stdout.trim(), "falsy");
}

#[tokio::test]
async fn resource_limits_proxy_inside_dict_values() {
    let mut tools = Tools::new();
    tools.insert("fetch", ToolDefinition { handler: Arc::new(FetchTool), parallelizable: true });

    let interp = stress_interpreter();
    let resp = interp
        .execute(
            r#"
data = {}
keys = ["alpha", "beta", "gamma"]
for k in keys:
    data[k] = fetch(v=k)
parts = []
for k in keys:
    parts.append(data[k])
print(",".join(parts))
"#,
            &tools,
            HashMap::new(),
        )
        .await;
    assert!(resp.error.is_none(), "error: {:?}", resp.error);
    assert_eq!(resp.stdout.trim(), "alpha,beta,gamma");
}

#[tokio::test]
async fn resource_limits_repeated_execution_does_not_leak_proxies() {
    let mut tools = Tools::new();

    struct EchoTool;
    #[async_trait]
    impl ToolHandler for EchoTool {
        async fn call(&self, kwargs: HashMap<String, Value>) -> Result<Value, ToolError> {
            tokio::time::sleep(Duration::from_millis(10)).await;
            Ok(kwargs.get("msg").cloned().unwrap_or(Value::String("".into())))
        }
    }

    tools.insert("echo", ToolDefinition { handler: Arc::new(EchoTool), parallelizable: true });

    let interp = stress_interpreter();
    for i in 0..5 {
        let code = format!("result = echo(msg='iteration_{i}')\nprint(result)");
        let resp = interp.execute(&code, &tools, HashMap::new()).await;
        assert!(resp.error.is_none(), "failed on iteration {i}: {:?}", resp.error);
        assert_eq!(resp.stdout.trim(), format!("iteration_{i}"));
    }
}