agent-os-execution 0.2.0-rc.3

Native execution plane scaffold for Agent OS
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
use agent_os_execution::{
    CreatePythonContextRequest, PythonExecutionEngine, PythonExecutionEvent, PythonVfsRpcMethod,
    PythonVfsRpcResponsePayload, PythonVfsRpcStat, StartPythonExecutionRequest,
};
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::thread;
use std::time::Duration;
use tempfile::tempdir;

const PYTHON_WARMUP_METRICS_PREFIX: &str = "__AGENT_OS_PYTHON_WARMUP_METRICS__:";
const PYTHON_EXECUTION_TIMEOUT_MS_ENV: &str = "AGENT_OS_PYTHON_EXECUTION_TIMEOUT_MS";
const PYTHON_MAX_OLD_SPACE_MB_ENV: &str = "AGENT_OS_PYTHON_MAX_OLD_SPACE_MB";
const PYTHON_OUTPUT_BUFFER_MAX_BYTES_ENV: &str = "AGENT_OS_PYTHON_OUTPUT_BUFFER_MAX_BYTES";
const PYTHON_VFS_RPC_TIMEOUT_MS_ENV: &str = "AGENT_OS_PYTHON_VFS_RPC_TIMEOUT_MS";

#[derive(Debug, Clone, PartialEq)]
struct PythonPrewarmMetrics {
    executed: bool,
    reason: String,
    duration_ms: f64,
    compile_cache_dir: String,
    pyodide_dist_path: String,
}

#[derive(Debug, Clone, PartialEq)]
struct PythonStartupMetrics {
    prewarm_only: bool,
    startup_ms: f64,
    load_pyodide_ms: f64,
    package_load_ms: f64,
    package_count: usize,
    source: String,
}

fn assert_node_available() {
    let binary = std::env::var("AGENT_OS_NODE_BINARY").unwrap_or_else(|_| String::from("node"));
    let output = Command::new(binary)
        .arg("--version")
        .output()
        .expect("spawn node --version");
    assert!(output.status.success(), "node --version failed");
}

fn write_fixture(path: &Path, contents: &str) {
    fs::write(path, contents).expect("write fixture");
}

fn write_pyodide_lock_fixture(path: &Path) {
    write_fixture(path, "{\"packages\":[]}\n");
    let pyodide_dir = path.parent().expect("pyodide fixture parent");
    for asset in ["pyodide.asm.js", "pyodide.asm.wasm", "python_stdlib.zip"] {
        let asset_path = pyodide_dir.join(asset);
        if !asset_path.exists() {
            fs::write(&asset_path, []).expect("write pyodide runtime fixture");
        }
    }
}

fn parse_metrics_line<'a>(stderr: &'a str, phase: &str) -> &'a str {
    stderr
        .lines()
        .filter_map(|line| line.strip_prefix(PYTHON_WARMUP_METRICS_PREFIX))
        .find(|line| parse_string_metric(line, "phase") == phase)
        .unwrap_or_else(|| panic!("missing {phase} metrics line in stderr: {stderr}"))
}

fn parse_prewarm_metrics(stderr: &str) -> PythonPrewarmMetrics {
    let metrics_line = parse_metrics_line(stderr, "prewarm");
    PythonPrewarmMetrics {
        executed: parse_boolean_metric(metrics_line, "executed"),
        reason: parse_string_metric(metrics_line, "reason"),
        duration_ms: parse_float_metric(metrics_line, "durationMs"),
        compile_cache_dir: parse_string_metric(metrics_line, "compileCacheDir"),
        pyodide_dist_path: parse_string_metric(metrics_line, "pyodideDistPath"),
    }
}

fn parse_startup_metrics(stderr: &str) -> PythonStartupMetrics {
    let metrics_line = parse_metrics_line(stderr, "startup");
    PythonStartupMetrics {
        prewarm_only: parse_boolean_metric(metrics_line, "prewarmOnly"),
        startup_ms: parse_float_metric(metrics_line, "startupMs"),
        load_pyodide_ms: parse_float_metric(metrics_line, "loadPyodideMs"),
        package_load_ms: parse_float_metric(metrics_line, "packageLoadMs"),
        package_count: parse_metric_value(metrics_line, "packageCount"),
        source: parse_string_metric(metrics_line, "source"),
    }
}

fn parse_metric_value(metrics_line: &str, key: &str) -> usize {
    parse_float_metric(metrics_line, key) as usize
}

fn parse_float_metric(metrics_line: &str, key: &str) -> f64 {
    let marker = format!("\"{key}\":");
    let start = metrics_line.find(&marker).expect("metric key") + marker.len();
    let digits: String = metrics_line[start..]
        .chars()
        .skip_while(|ch| !ch.is_ascii_digit() && *ch != '-')
        .take_while(|ch| ch.is_ascii_digit() || matches!(ch, '.' | '-' | 'e' | 'E' | '+'))
        .collect();

    digits.parse().expect("float metric value")
}

fn parse_boolean_metric(metrics_line: &str, key: &str) -> bool {
    let marker = format!("\"{key}\":");
    let start = metrics_line.find(&marker).expect("metric key") + marker.len();
    let remaining = &metrics_line[start..];

    if remaining.starts_with("true") {
        true
    } else if remaining.starts_with("false") {
        false
    } else {
        panic!("invalid boolean metric for {key}: {metrics_line}");
    }
}

fn parse_string_metric(metrics_line: &str, key: &str) -> String {
    let marker = format!("\"{key}\":\"");
    let start = metrics_line.find(&marker).expect("metric key") + marker.len();
    let mut value = String::new();
    let mut escaped = false;

    for ch in metrics_line[start..].chars() {
        if escaped {
            value.push(match ch {
                'n' => '\n',
                'r' => '\r',
                't' => '\t',
                '"' => '"',
                '\\' => '\\',
                other => other,
            });
            escaped = false;
            continue;
        }

        match ch {
            '\\' => escaped = true,
            '"' => return value,
            other => value.push(other),
        }
    }

    panic!("unterminated string metric for {key}: {metrics_line}");
}

fn run_python_execution(
    engine: &mut PythonExecutionEngine,
    context_id: String,
    cwd: &Path,
    code: &str,
    env: BTreeMap<String, String>,
) -> (String, String, i32) {
    let execution = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id,
            code: String::from(code),
            file_path: None,
            env,
            cwd: cwd.to_path_buf(),
        })
        .expect("start Python execution");

    let result = execution.wait(None).expect("wait for Python execution");
    let stdout = String::from_utf8(result.stdout).expect("stdout utf8");
    let stderr = String::from_utf8(result.stderr).expect("stderr utf8");

    (stdout, stderr, result.exit_code)
}

fn assert_process_exits(pid: u32) {
    for _ in 0..20 {
        let status = Command::new("kill")
            .arg("-0")
            .arg(pid.to_string())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .expect("probe process with kill -0");
        if !status.success() {
            return;
        }
        thread::sleep(Duration::from_millis(25));
    }

    panic!("process {pid} was still alive after waiting for cleanup");
}

fn python_contexts_preserve_vm_and_pyodide_configuration() {
    let pyodide_dist_path = PathBuf::from("/tmp/pyodide");
    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dist_path.clone(),
    });

    assert_eq!(context.context_id, "python-ctx-1");
    assert_eq!(context.vm_id, "vm-python");
    assert_eq!(context.pyodide_dist_path, pyodide_dist_path);
}

fn python_execution_runs_code_and_streams_stdio() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide(options) {
  return {
    setStdin(_stdin) {},
    async runPythonAsync(code) {
      options.stdout(`stdout:${code}`);
      options.stderr(`stderr:${options.indexURL}`);
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir.clone(),
    });

    let (stdout, stderr, exit_code) = run_python_execution(
        &mut engine,
        context.context_id,
        temp.path(),
        "print('hello')",
        BTreeMap::new(),
    );
    assert_eq!(exit_code, 0);
    assert_eq!(stdout, "stdout:print('hello')\n");
    assert!(
        stderr.starts_with("stderr:/__agent_os_pyodide/"),
        "unexpected stderr: {stderr}"
    );
}

fn python_execution_wait_bounds_output_buffers() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide(options) {
  return {
    setStdin(_stdin) {},
    async runPythonAsync() {
      options.stdout('x'.repeat(80));
      options.stderr('y'.repeat(80));
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let result = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('ignored')"),
            file_path: None,
            env: BTreeMap::from([(
                String::from(PYTHON_OUTPUT_BUFFER_MAX_BYTES_ENV),
                String::from("32"),
            )]),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution")
        .wait(None)
        .expect("wait for Python execution");

    assert_eq!(result.exit_code, 0);
    assert_eq!(result.stdout.len(), 32, "stdout should be capped");
    assert_eq!(result.stderr.len(), 32, "stderr should be capped");
    assert!(result.stdout.iter().all(|byte| *byte == b'x'));
    assert!(result.stderr.iter().all(|byte| *byte == b'y'));
}

fn python_execution_emits_stdout_before_exit() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide(options) {
  return {
    setStdin(_stdin) {},
    async runPythonAsync(code) {
      options.stdout(`stdout:${code}`);
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let mut execution = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('streamed')"),
            file_path: None,
            env: BTreeMap::new(),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution");

    let mut saw_stdout = false;
    let mut saw_exit = false;

    while !saw_exit {
        match execution
            .poll_event_blocking(Duration::from_secs(5))
            .expect("poll Python event")
        {
            Some(PythonExecutionEvent::Stdout(chunk)) => {
                saw_stdout = String::from_utf8(chunk)
                    .expect("stdout utf8")
                    .contains("stdout:print('streamed')");
            }
            Some(PythonExecutionEvent::Exited(code)) => {
                assert_eq!(code, 0);
                saw_exit = true;
            }
            Some(PythonExecutionEvent::VfsRpcRequest(request)) => {
                panic!("unexpected VFS RPC request during stdout test: {request:?}");
            }
            Some(PythonExecutionEvent::JavascriptSyncRpcRequest(request)) => {
                panic!("unexpected JS sync RPC request during stdout test: {request:?}");
            }
            Some(PythonExecutionEvent::Stderr(chunk)) => {
                panic!("unexpected stderr: {}", String::from_utf8_lossy(&chunk));
            }
            None => panic!("timed out waiting for Python execution event"),
        }
    }

    assert!(saw_stdout, "expected stdout event before exit");
}

fn python_execution_reports_prewarm_and_startup_metrics_when_debug_enabled() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide() {
  await new Promise((resolve) => setTimeout(resolve, 20));
  return {
    setStdin(_stdin) {},
    async runPythonAsync(code) {
      console.log(`ran:${code}`);
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir.clone(),
    });
    let debug_env = BTreeMap::from([(
        String::from("AGENT_OS_PYTHON_WARMUP_DEBUG"),
        String::from("1"),
    )]);

    let (first_stdout, first_stderr, first_exit_code) = run_python_execution(
        &mut engine,
        context.context_id.clone(),
        temp.path(),
        "print('first')",
        debug_env.clone(),
    );
    let first_prewarm = parse_prewarm_metrics(&first_stderr);
    let first_startup = parse_startup_metrics(&first_stderr);

    assert_eq!(first_exit_code, 0);
    assert!(first_stdout.contains("ran:print('first')"));
    assert!(
        first_prewarm.executed,
        "first prewarm metrics: {first_prewarm:?}"
    );
    assert_eq!(first_prewarm.reason, "executed");
    assert!(first_prewarm.duration_ms >= 0.0);
    assert!(
        first_prewarm.compile_cache_dir.contains("compile-cache"),
        "unexpected prewarm metrics: {first_prewarm:?}"
    );
    assert_eq!(
        PathBuf::from(&first_prewarm.pyodide_dist_path),
        pyodide_dir,
        "unexpected prewarm metrics: {first_prewarm:?}"
    );
    assert!(!first_startup.prewarm_only);
    assert!(first_startup.startup_ms > 0.0);
    assert!(first_startup.load_pyodide_ms > 0.0);
    assert_eq!(first_startup.package_load_ms, 0.0);
    assert_eq!(first_startup.package_count, 0);
    assert_eq!(first_startup.source, "inline");

    let (_second_stdout, second_stderr, second_exit_code) = run_python_execution(
        &mut engine,
        context.context_id,
        temp.path(),
        "print('second')",
        debug_env,
    );
    let second_prewarm = parse_prewarm_metrics(&second_stderr);
    let second_startup = parse_startup_metrics(&second_stderr);

    assert_eq!(second_exit_code, 0);
    assert!(
        !second_prewarm.executed,
        "second prewarm metrics: {second_prewarm:?}"
    );
    assert_eq!(second_prewarm.reason, "cached");
    assert_eq!(second_prewarm.duration_ms, 0.0);
    assert!(!second_startup.prewarm_only);
    assert!(second_startup.startup_ms > 0.0);
    assert!(second_startup.load_pyodide_ms > 0.0);
    assert_eq!(second_startup.source, "inline");
}

fn python_execution_keeps_streaming_stdin_sessions_alive_until_closed() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
const decoder = new TextDecoder();

export async function loadPyodide(options) {
  let stdin = null;
  return {
    setStdin(config) {
      stdin = config;
    },
    async runPythonAsync(code) {
      const chunk = new Uint8Array(8192);
      const bytesRead = stdin.read(chunk);
      const text = decoder.decode(chunk.subarray(0, bytesRead));
      options.stdout(`stdin:${text}`);
      options.stdout(`code:${code}`);
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let mut execution = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('streaming')"),
            file_path: None,
            env: BTreeMap::new(),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution");

    assert!(
        execution
            .poll_event_blocking(Duration::from_millis(200))
            .expect("poll Python event before stdin write")
            .is_none(),
        "streaming-stdin execution should stay alive until stdin closes"
    );

    execution
        .write_stdin(b"still-open")
        .expect("write stdin after idle period");
    execution.close_stdin().expect("close stdin");

    let mut stdout = Vec::new();
    let mut exit_code = None;

    while exit_code.is_none() {
        match execution
            .poll_event_blocking(Duration::from_secs(5))
            .expect("poll Python event")
        {
            Some(PythonExecutionEvent::Stdout(chunk)) => stdout.extend(chunk),
            Some(PythonExecutionEvent::VfsRpcRequest(request)) => {
                panic!("unexpected VFS RPC request during stdin test: {request:?}");
            }
            Some(PythonExecutionEvent::JavascriptSyncRpcRequest(request)) => {
                panic!("unexpected JS sync RPC request during stdin test: {request:?}");
            }
            Some(PythonExecutionEvent::Stderr(chunk)) => {
                panic!("unexpected stderr: {}", String::from_utf8_lossy(&chunk));
            }
            Some(PythonExecutionEvent::Exited(code)) => exit_code = Some(code),
            None => panic!("timed out waiting for Python execution event"),
        }
    }

    assert_eq!(exit_code, Some(0));
    let stdout = String::from_utf8(stdout).expect("stdout utf8");
    assert!(
        stdout.contains("stdin:still-open"),
        "unexpected stdout: {stdout}"
    );
    assert!(
        stdout.contains("code:print('streaming')"),
        "unexpected stdout: {stdout}"
    );
}

fn python_execution_surfaces_vfs_rpc_requests_and_resumes_after_responses() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide(options) {
  return {
    setStdin(_stdin) {},
    async runPythonAsync(code) {
      const rpc = globalThis.__agentOsPythonVfsRpc;
      await rpc.fsMkdir('/workspace', { recursive: true });
      await rpc.fsWrite(
        '/workspace/note.txt',
        Buffer.from('hello from rpc', 'utf8').toString('base64'),
      );
      const content = Buffer.from(
        await rpc.fsRead('/workspace/note.txt'),
        'base64',
      ).toString('utf8');
      const stat = await rpc.fsStat('/workspace/note.txt');
      const entries = await rpc.fsReaddir('/workspace');
      options.stdout(JSON.stringify({ code, content, stat, entries }));
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let mut execution = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('rpc bridge')"),
            file_path: None,
            env: BTreeMap::new(),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution");

    let mut stdout = Vec::new();
    let mut exit_code = None;
    let mut saw_requests = Vec::new();

    while exit_code.is_none() {
        match execution
            .poll_event_blocking(Duration::from_secs(5))
            .expect("poll Python event")
        {
            Some(PythonExecutionEvent::Stdout(chunk)) => stdout.extend(chunk),
            Some(PythonExecutionEvent::Stderr(chunk)) => {
                panic!("unexpected stderr: {}", String::from_utf8_lossy(&chunk));
            }
            Some(PythonExecutionEvent::VfsRpcRequest(request)) => {
                saw_requests.push((request.method, request.path.clone()));
                match request.method {
                    PythonVfsRpcMethod::Mkdir => execution
                        .respond_vfs_rpc_success(request.id, PythonVfsRpcResponsePayload::Empty)
                        .expect("respond to mkdir"),
                    PythonVfsRpcMethod::Write => {
                        assert_eq!(request.path, "/workspace/note.txt");
                        assert_eq!(
                            request.content_base64.as_deref(),
                            Some("aGVsbG8gZnJvbSBycGM=")
                        );
                        execution
                            .respond_vfs_rpc_success(request.id, PythonVfsRpcResponsePayload::Empty)
                            .expect("respond to write");
                    }
                    PythonVfsRpcMethod::Read => execution
                        .respond_vfs_rpc_success(
                            request.id,
                            PythonVfsRpcResponsePayload::Read {
                                content_base64: String::from("aGVsbG8gZnJvbSBycGM="),
                            },
                        )
                        .expect("respond to read"),
                    PythonVfsRpcMethod::Stat => execution
                        .respond_vfs_rpc_success(
                            request.id,
                            PythonVfsRpcResponsePayload::Stat {
                                stat: PythonVfsRpcStat {
                                    mode: 0o100644,
                                    size: 14,
                                    is_directory: false,
                                    is_symbolic_link: false,
                                },
                            },
                        )
                        .expect("respond to stat"),
                    PythonVfsRpcMethod::ReadDir => execution
                        .respond_vfs_rpc_success(
                            request.id,
                            PythonVfsRpcResponsePayload::ReadDir {
                                entries: vec![String::from("note.txt")],
                            },
                        )
                        .expect("respond to read_dir"),
                    PythonVfsRpcMethod::HttpRequest
                    | PythonVfsRpcMethod::DnsLookup
                    | PythonVfsRpcMethod::SubprocessRun => {
                        panic!("unexpected non-filesystem Python RPC: {:?}", request.method)
                    }
                }
            }
            Some(PythonExecutionEvent::JavascriptSyncRpcRequest(request)) => {
                panic!("unexpected JS sync RPC request during VFS RPC test: {request:?}");
            }
            Some(PythonExecutionEvent::Exited(code)) => exit_code = Some(code),
            None => panic!("timed out waiting for Python execution event"),
        }
    }

    assert_eq!(exit_code, Some(0));
    assert_eq!(
        saw_requests,
        vec![
            (PythonVfsRpcMethod::Mkdir, String::from("/workspace")),
            (
                PythonVfsRpcMethod::Write,
                String::from("/workspace/note.txt")
            ),
            (
                PythonVfsRpcMethod::Read,
                String::from("/workspace/note.txt")
            ),
            (
                PythonVfsRpcMethod::Stat,
                String::from("/workspace/note.txt")
            ),
            (PythonVfsRpcMethod::ReadDir, String::from("/workspace")),
        ]
    );

    let stdout = String::from_utf8(stdout).expect("stdout utf8");
    assert!(
        stdout.contains("\"content\":\"hello from rpc\""),
        "unexpected stdout: {stdout}"
    );
    assert!(
        stdout.contains("\"entries\":[\"note.txt\"]"),
        "unexpected stdout: {stdout}"
    );
    assert!(
        stdout.contains("\"size\":14"),
        "unexpected stdout: {stdout}"
    );
}

fn python_execution_wait_timeout_cleans_up_hanging_child() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide() {
  return {
    setStdin(_stdin) {},
    async runPythonAsync() {
      await new Promise(() => setInterval(() => {}, 1000));
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let execution = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('hang')"),
            file_path: None,
            env: BTreeMap::new(),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution");
    let child_pid = execution.child_pid();
    let uses_shared_v8_runtime = execution.uses_shared_v8_runtime();

    let error = execution
        .wait(Some(Duration::from_millis(100)))
        .expect_err("timed out wait");
    match error {
        agent_os_execution::PythonExecutionError::TimedOut(timeout) => {
            assert_eq!(timeout, Duration::from_millis(100));
        }
        other => panic!("expected timeout error, got {other:?}"),
    }

    if !uses_shared_v8_runtime {
        assert_process_exits(child_pid);
    }
}

fn python_execution_uses_configured_default_timeout_when_wait_timeout_not_provided() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide() {
  return {
    setStdin(_stdin) {},
    async runPythonAsync() {
      await new Promise(() => setInterval(() => {}, 1000));
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let execution = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('hang')"),
            file_path: None,
            env: BTreeMap::from([(
                String::from(PYTHON_EXECUTION_TIMEOUT_MS_ENV),
                String::from("75"),
            )]),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution");
    let child_pid = execution.child_pid();
    let uses_shared_v8_runtime = execution.uses_shared_v8_runtime();

    let error = execution
        .wait(None)
        .expect_err("configured timeout should fire");
    match error {
        agent_os_execution::PythonExecutionError::TimedOut(timeout) => {
            assert_eq!(timeout, Duration::from_millis(75));
        }
        other => panic!("expected timeout error, got {other:?}"),
    }

    if !uses_shared_v8_runtime {
        assert_process_exits(child_pid);
    }
}

fn python_vfs_rpc_bridge_times_out_when_sidecar_never_responds() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide() {
  return {
    setStdin(_stdin) {},
    async runPythonAsync() {
      globalThis.__agentOsPythonVfsRpc.fsReadSync('/workspace/never.txt');
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let mut execution = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('rpc timeout')"),
            file_path: None,
            env: BTreeMap::from([(
                String::from(PYTHON_VFS_RPC_TIMEOUT_MS_ENV),
                String::from("50"),
            )]),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution");
    let child_pid = execution.child_pid();
    let uses_shared_v8_runtime = execution.uses_shared_v8_runtime();

    let mut saw_request = false;
    let mut stderr = Vec::new();
    let mut exit_code = None;

    for _ in 0..40 {
        match execution
            .poll_event_blocking(Duration::from_millis(250))
            .expect("poll Python event")
        {
            Some(PythonExecutionEvent::VfsRpcRequest(request)) => {
                saw_request = true;
                assert_eq!(request.method, PythonVfsRpcMethod::Read);
                assert_eq!(request.path, "/workspace/never.txt");
            }
            Some(PythonExecutionEvent::JavascriptSyncRpcRequest(request)) => {
                panic!("unexpected JS sync RPC request during timeout test: {request:?}");
            }
            Some(PythonExecutionEvent::Stderr(chunk)) => stderr.extend(chunk),
            Some(PythonExecutionEvent::Exited(code)) => {
                exit_code = Some(code);
                break;
            }
            Some(PythonExecutionEvent::Stdout(chunk)) => {
                panic!("unexpected stdout: {}", String::from_utf8_lossy(&chunk));
            }
            None => {}
        }
    }

    assert!(saw_request, "expected a VFS RPC request before timeout");
    assert_eq!(
        exit_code,
        Some(1),
        "stderr: {}",
        String::from_utf8_lossy(&stderr)
    );

    let stderr = String::from_utf8(stderr).expect("stderr utf8");
    assert!(
        stderr.contains("ERR_AGENT_OS_PYTHON_VFS_RPC_TIMEOUT")
            || stderr.contains("timed out waiting for a response")
            || stderr.contains("timed out after 50ms"),
        "unexpected stderr: {stderr}"
    );
    if !uses_shared_v8_runtime {
        assert_process_exits(child_pid);
    }
}

fn python_execution_surfaces_runtime_stderr() {
    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide() {
  console.error("runtime stderr before failure");
  return {
    setStdin(_stdin) {},
    async runPythonAsync() {
      throw new Error("simulated runtime failure");
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let result = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('oom')"),
            file_path: None,
            env: BTreeMap::from([(
                String::from(PYTHON_MAX_OLD_SPACE_MB_ENV),
                String::from("64"),
            )]),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution")
        .wait(None)
        .expect("wait for Python execution");

    let stderr = String::from_utf8(result.stderr).expect("stderr utf8");
    assert_eq!(result.exit_code, 1, "stderr: {stderr}");
    assert!(
        stderr.contains("runtime stderr before failure")
            && stderr.contains("simulated runtime failure"),
        "unexpected stderr: {stderr}"
    );
}

fn python_execution_kill_stops_inflight_process_and_emits_exit() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide(options) {
  options.stdout("ready\n");
  return {
    setStdin(_stdin) {},
    async runPythonAsync() {
      await new Promise(() => setInterval(() => {}, 1000));
    },
  };
}
"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let mut execution = engine
        .start_execution(StartPythonExecutionRequest {
            vm_id: String::from("vm-python"),
            context_id: context.context_id,
            code: String::from("print('hang')"),
            file_path: None,
            env: BTreeMap::new(),
            cwd: temp.path().to_path_buf(),
        })
        .expect("start Python execution");
    let child_pid = execution.child_pid();
    let uses_shared_v8_runtime = execution.uses_shared_v8_runtime();

    let mut saw_ready = false;
    while !saw_ready {
        match execution
            .poll_event_blocking(Duration::from_secs(5))
            .expect("poll Python event before kill")
        {
            Some(PythonExecutionEvent::Stdout(chunk)) => {
                saw_ready = String::from_utf8(chunk)
                    .expect("stdout utf8")
                    .contains("ready");
            }
            Some(PythonExecutionEvent::Stderr(chunk)) => {
                panic!("unexpected stderr: {}", String::from_utf8_lossy(&chunk));
            }
            Some(PythonExecutionEvent::VfsRpcRequest(request)) => {
                panic!("unexpected VFS RPC request during kill test: {request:?}");
            }
            Some(PythonExecutionEvent::JavascriptSyncRpcRequest(request)) => {
                panic!("unexpected JS sync RPC request during kill test: {request:?}");
            }
            Some(PythonExecutionEvent::Exited(code)) => {
                panic!("execution exited unexpectedly before kill with code {code}");
            }
            None => panic!("timed out waiting for Python execution readiness"),
        }
    }

    execution.kill().expect("kill hanging Python execution");

    let mut exit_code = None;
    while exit_code.is_none() {
        match execution
            .poll_event_blocking(Duration::from_millis(100))
            .expect("poll Python event after kill")
        {
            Some(PythonExecutionEvent::Exited(code)) => exit_code = Some(code),
            Some(PythonExecutionEvent::Stdout(_)) | Some(PythonExecutionEvent::Stderr(_)) => {}
            Some(PythonExecutionEvent::VfsRpcRequest(request)) => {
                panic!("unexpected VFS RPC request after kill: {request:?}");
            }
            Some(PythonExecutionEvent::JavascriptSyncRpcRequest(request)) => {
                panic!("unexpected JS sync RPC request after kill: {request:?}");
            }
            None => {}
        }
    }

    assert_eq!(exit_code, Some(1));
    if !uses_shared_v8_runtime {
        assert_process_exits(child_pid);
    }
}

fn python_execution_blocks_network_requests_during_pyodide_init() {
    assert_node_available();

    let temp = tempdir().expect("create temp dir");
    let pyodide_dir = temp.path().join("pyodide");
    fs::create_dir_all(&pyodide_dir).expect("create pyodide dir");
    write_fixture(
        &pyodide_dir.join("pyodide.mjs"),
        r#"
export async function loadPyodide() {
  let initResult;
  try {
    await fetch('https://example.com/pyodide-init-check');
    initResult = { ok: true };
  } catch (error) {
    initResult = {
      ok: false,
      code: error.code ?? null,
      message: error.message,
    };
  }

  return {
    setStdin(_stdin) {},
    async runPythonAsync() {
      console.log(JSON.stringify(initResult));
    },
  };
}

"#,
    );
    write_pyodide_lock_fixture(&pyodide_dir.join("pyodide-lock.json"));

    let mut engine = PythonExecutionEngine::default();
    let context = engine.create_context(CreatePythonContextRequest {
        vm_id: String::from("vm-python"),
        pyodide_dist_path: pyodide_dir,
    });

    let (stdout, stderr, exit_code) = run_python_execution(
        &mut engine,
        context.context_id,
        temp.path(),
        "print('ignored')",
        BTreeMap::new(),
    );

    assert_eq!(exit_code, 0, "stderr: {stderr}");
    assert!(stderr.is_empty(), "unexpected stderr: {stderr}");

    let parsed: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("parse init network JSON");
    assert_eq!(parsed["ok"], serde_json::Value::Bool(false));
    assert!(
        parsed["code"].is_null()
            || parsed["code"] == serde_json::Value::String(String::from("ERR_ACCESS_DENIED")),
        "unexpected network denial payload: {stdout}"
    );
    let message = parsed["message"].as_str().expect("network denial message");
    if parsed["code"].is_null() {
        assert!(
            message.contains("fetch failed"),
            "unexpected stdout: {stdout}"
        );
    } else {
        assert!(
            message.contains("network access"),
            "unexpected stdout: {stdout}"
        );
    }
}

// Separate libtest cases in this binary still trip a V8 teardown/init crash, so
// keep the Python runtime coverage in one top-level suite until that boundary is fixed.
#[test]
fn python_suite() {
    python_contexts_preserve_vm_and_pyodide_configuration();
    python_execution_runs_code_and_streams_stdio();
    python_execution_wait_bounds_output_buffers();
    python_execution_emits_stdout_before_exit();
    python_execution_reports_prewarm_and_startup_metrics_when_debug_enabled();
    python_execution_keeps_streaming_stdin_sessions_alive_until_closed();
    python_execution_surfaces_vfs_rpc_requests_and_resumes_after_responses();
    python_execution_wait_timeout_cleans_up_hanging_child();
    python_execution_uses_configured_default_timeout_when_wait_timeout_not_provided();
    python_vfs_rpc_bridge_times_out_when_sidecar_never_responds();
    python_execution_surfaces_runtime_stderr();
    python_execution_kill_stops_inflight_process_and_emits_exit();
    python_execution_blocks_network_requests_during_pyodide_init();
}