monty-pool 1.0.0

A secure, snapshotable Python sandbox written in Rust.
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
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
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
//! Aggregate metrics for pool health and turn latency — what the spans in
//! [`tracing`](crate::telemetry::tracing) cannot answer: is the fleet
//! saturated, how often do workers die, how much sandbox time does a typical
//! run cost.
//!
//! Recorded for *every* checkout, not only those a host gave a
//! [`TelemetryContext`](crate::telemetry::TelemetryContext) to: an aggregate
//! covering only traced sessions would mislead. Rust hosts record into Logfire
//! instruments directly. A foreign adapter can instead receive each raw
//! measurement so its host metrics SDK controls aggregation.
//!
//! **No value the sandbox controls may become an attribute.** Every attribute
//! here is a closed set fixed by this crate, because one time series per value
//! is a way to exhaust a metrics backend — and a script can mint values freely
//! (calling `f_1()`, `f_2()`, … or raising a class per iteration). So a called
//! function's name is never recorded, not even when the host resolved it: a
//! host whose lookup is a callable resolves *any* name. Only the fixed os-call
//! names in [`os_call`] look like an exception, and they come from the
//! protocol's oneof rather than from the sandbox.

use std::{
    collections::HashMap,
    fmt,
    sync::{Arc, PoisonError, RwLock},
    time::{Duration, Instant},
};

use logfire::{ExponentialHistogram, Logfire};
use monty_proto::{pb, pb::os_call::Call};
use opentelemetry::{
    KeyValue,
    metrics::{Counter, UpDownCounter},
};

use super::TelemetryAdapter;

/// Live workers, whatever they are doing.
static LIVE_WORKERS: Instrument = Instrument {
    kind: MetricKind::UpDownCounter,
    name: "monty.pool.workers.live",
    unit: "{worker}",
    description: "Workers the pool is keeping alive: pooled, checked out, or being spawned.",
};

/// Pooled workers immediately available for checkout.
static IDLE_WORKERS: Instrument = Instrument {
    kind: MetricKind::UpDownCounter,
    name: "monty.pool.workers.idle",
    unit: "{worker}",
    description: "Workers immediately available for checkout.",
};

/// Checked-out workers blocked waiting for the host.
static SUSPENDED_WORKERS: Instrument = Instrument {
    kind: MetricKind::UpDownCounter,
    name: "monty.pool.workers.suspended",
    unit: "{worker}",
    description: "Workers blocked waiting for the host to answer a suspension.",
};

/// Time spent waiting for a worker to check out.
static CHECKOUT_WAIT: Instrument = Instrument {
    kind: MetricKind::Histogram,
    name: "monty.pool.checkout.wait",
    unit: "s",
    description: "Time spent acquiring a worker for a checkout.",
};

/// Workers leaving the pool, by why.
static WORKER_TERMINATED: Instrument = Instrument {
    kind: MetricKind::Counter,
    name: "monty.pool.worker.terminated",
    unit: "{worker}",
    description: "Workers discarded by the pool, by reason.",
};

/// Sessions resumed after their relay shut down.
static SESSION_RESUMED: Instrument = Instrument {
    kind: MetricKind::Counter,
    name: "monty.pool.session.resumed",
    unit: "{session}",
    description: "Sessions the pool tried to resume after a relay shutdown, by outcome.",
};

/// Checkout lifetime.
static SESSION_DURATION: Instrument = Instrument {
    kind: MetricKind::Histogram,
    name: "monty.pool.session.duration",
    unit: "s",
    description: "Lifetime of a checked-out session.",
};

/// Wall time of one execution turn.
static RUN_DURATION: Instrument = Instrument {
    kind: MetricKind::Histogram,
    name: "monty.run.duration",
    unit: "s",
    description: "Wall time of one feed, including time spent waiting on the host.",
};

/// Sandbox time of one execution turn. Subtracting it from [`RUN_DURATION`]
/// leaves host and transport overhead, primarily suspension handling.
static RUN_EXECUTION: Instrument = Instrument {
    kind: MetricKind::Histogram,
    name: "monty.run.execution_time",
    unit: "s",
    description: "Sandbox execution time of one feed, excluding host round-trips.",
};

/// Wall time of one housekeeping turn.
static TURN_DURATION: Instrument = Instrument {
    kind: MetricKind::Histogram,
    name: "monty.turn.duration",
    unit: "s",
    description: "Wall time of one non-execution turn, by kind.",
};

/// Suspensions the sandbox raised.
static SUSPENSIONS: Instrument = Instrument {
    kind: MetricKind::Counter,
    name: "monty.run.suspensions",
    unit: "{suspension}",
    description: "Suspensions the sandbox raised for the host to answer, by kind.",
};

/// Round-trip time of one suspension, whoever answers it.
static EXT_CALL: Instrument = Instrument {
    kind: MetricKind::Histogram,
    name: "monty.ext.call.duration",
    unit: "s",
    description: "Time the host took to answer a suspension, by kind.",
};

/// Session dump size.
static SNAPSHOT_BYTES: Instrument = Instrument {
    kind: MetricKind::Histogram,
    name: "monty.snapshot.bytes",
    unit: "By",
    description: "Size of a session dump.",
};

/// Sandbox output volume.
static PRINT_BYTES: Instrument = Instrument {
    kind: MetricKind::Counter,
    name: "monty.print.bytes",
    unit: "By",
    description: "Bytes the sandbox printed, by stream.",
};

/// Protocol frame size.
static FRAME_BYTES: Instrument = Instrument {
    kind: MetricKind::Histogram,
    name: "monty.wire.frame.bytes",
    unit: "By",
    description: "Size of one protocol frame, by direction.",
};

/// The constant half of an instrument, shared by every measurement it records.
struct Instrument {
    kind: MetricKind,
    name: &'static str,
    unit: &'static str,
    description: &'static str,
}

/// Records pool measurements into a Rust or foreign-host metrics SDK.
///
/// Put on [`PoolConfig::metrics`](crate::PoolConfig::metrics); cheap to clone
/// (one `Arc`). Foreign-language bindings obtain it from
/// [`TelemetryAdapterHandle::metrics`](crate::telemetry::TelemetryAdapterHandle::metrics),
/// while Rust hosts construct one with [`Metrics::for_logfire`].
#[derive(Clone)]
pub struct Metrics(Arc<Sink>);

/// Where raw measurements are recorded.
enum Sink {
    /// Pushed one at a time to the foreign host that owns aggregation.
    Adapter(Arc<dyn TelemetryAdapter>),
    /// Recorded directly into a Rust host's configured instruments.
    Logfire(Instruments),
}

impl Metrics {
    /// Sends each measurement to a foreign host's telemetry adapter.
    pub(super) fn for_adapter(adapter: Arc<dyn TelemetryAdapter>) -> Self {
        Self(Arc::new(Sink::Adapter(adapter)))
    }

    /// Builds Monty's instruments on a configured `Logfire` meter.
    ///
    /// Create one and clone it per pool: the worker counters then sum over all
    /// pools and sessions that record through the handle.
    #[must_use]
    pub fn for_logfire(logfire: Logfire) -> Self {
        Self(Arc::new(Sink::Logfire(Instruments::new(logfire))))
    }

    /// Adjusts the number of live workers across all pools using this handle.
    pub(crate) fn live_workers(&self, delta: i64) {
        self.record(&LIVE_WORKERS, MetricValue::I64(delta), &[]);
    }

    /// Adjusts the number of workers immediately available for checkout.
    pub(crate) fn idle_workers(&self, delta: i64) {
        self.record(&IDLE_WORKERS, MetricValue::I64(delta), &[]);
    }

    /// Adjusts the number of workers waiting for the host to resume them.
    fn suspended_workers(&self, delta: i64) {
        self.record(&SUSPENDED_WORKERS, MetricValue::I64(delta), &[]);
    }

    /// Time [`Pool::checkout`](crate::Pool::checkout) spent obtaining a worker.
    ///
    /// The saturation signal: a non-zero `waited` tail means `max_processes`
    /// is below what the workload needs, and `exhausted` is already a
    /// user-visible failure.
    pub(crate) fn checkout_wait(&self, elapsed: Duration, outcome: &'static str) {
        self.record(
            &CHECKOUT_WAIT,
            MetricValue::seconds(elapsed),
            &[KeyValue::new("outcome", outcome)],
        );
    }

    /// One worker leaving the pool, by why it left. `crash` versus `oom`
    /// separates "our bug" from "the sandboxed code asked for too much".
    pub(crate) fn worker_terminated(&self, reason: &'static str) {
        self.record(
            &WORKER_TERMINATED,
            MetricValue::I64(1),
            &[KeyValue::new("reason", reason)],
        );
    }

    /// Lifetime of one checkout, from `Configure` to `finish` (or to the drop
    /// that abandoned it). With the checkout rate this sizes the pool.
    pub(crate) fn session_duration(&self, elapsed: Duration, outcome: &'static str) {
        self.record(
            &SESSION_DURATION,
            MetricValue::seconds(elapsed),
            &[KeyValue::new("outcome", outcome)],
        );
    }

    /// One attempt to resume a session after a relay shutdown (`ok`, or why it failed).
    pub(crate) fn session_resumed(&self, outcome: &'static str) {
        self.record(
            &SESSION_RESUMED,
            MetricValue::I64(1),
            &[KeyValue::new("outcome", outcome)],
        );
    }

    /// Records one measurement into its configured host.
    fn record(&self, instrument: &Instrument, value: MetricValue, attributes: &[KeyValue]) {
        match self.0.as_ref() {
            Sink::Adapter(adapter) => adapter.record_metric(&Measurement {
                kind: instrument.kind,
                name: instrument.name,
                unit: instrument.unit,
                description: instrument.description,
                value,
                attributes,
            }),
            Sink::Logfire(instruments) => instruments.record(instrument, value, attributes),
        }
    }
}

impl fmt::Debug for Metrics {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Metrics")
    }
}

/// One raw measurement and the metadata needed to create its instrument.
pub struct Measurement<'a> {
    /// Which kind of instrument records this measurement.
    pub kind: MetricKind,
    /// Dotted instrument name, e.g. `monty.pool.checkout.wait`.
    pub name: &'static str,
    /// UCUM unit: `s`, `By`, `1`, or a `{thing}` annotation for counts.
    pub unit: &'static str,
    /// One-line description of what the instrument measures.
    pub description: &'static str,
    /// The measured value.
    pub value: MetricValue,
    /// Low-cardinality dimensions to record it under.
    pub attributes: &'a [KeyValue],
}

/// The kind of host instrument a definition builds.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MetricKind {
    /// Monotonic sum: the value is an increment.
    Counter,
    /// Non-monotonic sum: the value adjusts a current count.
    UpDownCounter,
    /// Distribution: the value is one sample.
    Histogram,
}

/// A measured value, integral for counts and byte sizes, floating for
/// durations and ratios.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MetricValue {
    /// An integral count.
    I64(i64),
    /// A duration in seconds, or a ratio.
    F64(f64),
}

impl MetricValue {
    /// A duration as fractional seconds, the unit OTel durations use.
    fn seconds(duration: Duration) -> Self {
        Self::F64(duration.as_secs_f64())
    }

    /// A byte count, saturating rather than wrapping on absurd sizes.
    fn bytes(len: usize) -> Self {
        Self::count(len)
    }

    /// A count of things, saturating rather than wrapping.
    fn count(n: usize) -> Self {
        Self::I64(i64::try_from(n).unwrap_or(i64::MAX))
    }

    /// The value as a histogram sample.
    fn as_f64(self) -> f64 {
        match self {
            Self::I64(value) => value as f64,
            Self::F64(value) => value,
        }
    }

    /// The value as an up/down-counter adjustment. Only durations are `F64`,
    /// and no up/down counter records one, so the saturating cast never runs.
    #[expect(clippy::cast_possible_truncation, reason = "float→int casts saturate")]
    fn as_i64(self) -> i64 {
        match self {
            Self::I64(value) => value,
            Self::F64(value) => value as i64,
        }
    }

    /// The value as a counter increment; a negative one would be a bug in a
    /// caller, and saturates to zero rather than wrapping into a huge count.
    #[expect(
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss,
        reason = "float→int casts saturate, including negatives to zero"
    )]
    fn as_u64(self) -> u64 {
        match self {
            Self::I64(value) => u64::try_from(value).unwrap_or(0),
            Self::F64(value) => value as u64,
        }
    }
}

/// The `outcome` attribute for an operation that either worked or did not.
pub(crate) const fn outcome(ok: bool) -> &'static str {
    if ok { "ok" } else { "error" }
}

/// Instruments built from a Rust host's meter, on first use of each.
///
/// Lazy rather than a registered list built up front: an instrument then needs
/// no bookkeeping beyond its own [`Instrument`], so one that only a rare code
/// path records can never be forgotten and silently dropped. The read lock is
/// the steady state — building happens at most once per instrument per handle.
struct Instruments {
    logfire: Logfire,
    built: RwLock<HashMap<&'static str, Handle>>,
}

impl Instruments {
    fn new(logfire: Logfire) -> Self {
        Self {
            logfire,
            built: RwLock::new(HashMap::new()),
        }
    }

    /// Records into `instrument`'s handle, building it if this is its first use.
    fn record(&self, instrument: &Instrument, value: MetricValue, attributes: &[KeyValue]) {
        {
            let built = self.built.read().unwrap_or_else(PoisonError::into_inner);
            if let Some(handle) = built.get(instrument.name) {
                handle.record(value, attributes);
                return;
            }
        }
        // built under the write lock, not before it: two handles for one
        // exponential histogram would share a scale registration, and dropping
        // the loser would deregister the winner's scale
        let mut built = self.built.write().unwrap_or_else(PoisonError::into_inner);
        built
            .entry(instrument.name)
            .or_insert_with(|| self.build(instrument))
            .record(value, attributes);
    }

    /// Creates one instrument on the host's meter.
    fn build(&self, instrument: &Instrument) -> Handle {
        let metrics = self.logfire.metrics();
        match instrument.kind {
            MetricKind::Counter => Handle::Counter(
                metrics
                    .u64_counter(instrument.name)
                    .with_unit(instrument.unit)
                    .with_description(instrument.description)
                    .build(),
            ),
            MetricKind::UpDownCounter => Handle::UpDownCounter(
                metrics
                    .i64_up_down_counter(instrument.name)
                    .with_unit(instrument.unit)
                    .with_description(instrument.description)
                    .build(),
            ),
            // exponential, not the SDK's default buckets: those are scaled for
            // seconds-long requests and would put every monty turn in the
            // first bucket
            MetricKind::Histogram => Handle::Histogram(
                metrics
                    .f64_exponential_histogram(instrument.name, MAX_HISTOGRAM_SCALE)
                    .with_unit(instrument.unit)
                    .with_description(instrument.description)
                    .build(),
            ),
        }
    }
}

/// Upper bound on exponential histogram resolution; the SDK downscales from
/// here as a distribution widens, so this only says "as fine as OTel allows".
const MAX_HISTOGRAM_SCALE: i8 = 20;

/// One built instrument, typed by the kind that produced it.
enum Handle {
    Counter(Counter<u64>),
    UpDownCounter(UpDownCounter<i64>),
    Histogram(ExponentialHistogram<f64>),
}

impl Handle {
    fn record(&self, value: MetricValue, attributes: &[KeyValue]) {
        match self {
            Self::Counter(counter) => counter.add(value.as_u64(), attributes),
            Self::UpDownCounter(counter) => counter.add(value.as_i64(), attributes),
            Self::Histogram(histogram) => histogram.record(value.as_f64(), attributes),
        }
    }
}

/// Records one worker's turns, mirroring the protocol the way
/// [`crate::telemetry::tracing::Recorder`] mirrors it for spans.
///
/// A separate state machine rather than a branch inside that recorder, because
/// metrics must also run for untraced checkouts. Lives on the
/// [`Worker`](crate::worker::Worker), which sees every request and event.
pub(crate) struct TurnMetrics {
    metrics: Metrics,
    /// When the in-flight feed started; `None` between feeds, and for a feed
    /// restored mid-suspension (whose start this process never saw).
    feed: Option<Instant>,
    /// Whether a feed is running, including one restored mid-suspension.
    run_active: bool,
    /// The suspension the feed is blocked on, timing the host round-trip.
    /// While this is set, the worker counts towards [`SUSPENDED_WORKERS`].
    pending: Option<Suspension>,
    /// The in-flight housekeeping turn: what to label it, and when it started.
    turn: Option<(&'static str, Instant)>,
    /// Cumulative sandbox execution time as last reported, so each run
    /// contributes its own delta instead of the session total.
    reported_micros: u64,
}

impl TurnMetrics {
    /// Creates the per-worker recorder, which outlives the checkouts it serves.
    pub(crate) const fn new(metrics: Metrics) -> Self {
        Self {
            metrics,
            feed: None,
            run_active: false,
            pending: None,
            turn: None,
            reported_micros: 0,
        }
    }

    /// Records the size of one frame that reached the wire.
    pub(crate) fn frame(&self, direction: &'static str, len: usize) {
        self.metrics.record(
            &FRAME_BYTES,
            MetricValue::bytes(len),
            &[KeyValue::new("direction", direction)],
        );
    }

    /// Starts timing one turn; called once the request is on the wire.
    ///
    /// The resume family opens nothing — it *answers* the open suspension, so
    /// it closes that instead, which is what makes the recorded duration the
    /// host round-trip.
    pub(crate) fn begin_turn(&mut self, request: &pb::ParentRequest) {
        let now = Instant::now();
        match &request.kind {
            // a new session on this worker; its execution clock restarts, so
            // the delta ratchet has to as well
            Some(pb::parent_request::Kind::Configure(_)) => {
                self.feed = None;
                self.run_active = false;
                self.abandon_pending();
                self.turn = Some(("configure", now));
                self.reported_micros = 0;
            }
            Some(pb::parent_request::Kind::Feed(_)) => {
                self.abandon_pending();
                self.feed = Some(now);
                self.run_active = true;
            }
            Some(pb::parent_request::Kind::Load(_)) => {
                // a load adopts the dumped session's clock, which may be ahead
                // of or behind this worker's; the first reply re-bases it
                self.run_active = false;
                self.reported_micros = 0;
                self.turn = Some(("load", now));
            }
            Some(pb::parent_request::Kind::Dump(_)) => self.turn = Some(("dump", now)),
            Some(pb::parent_request::Kind::InstallDependencies(_)) => {
                self.turn = Some(("install_dependencies", now));
            }
            Some(pb::parent_request::Kind::Reset(_)) => {
                self.feed = None;
                self.run_active = false;
                self.abandon_pending();
                self.turn = Some(("reset", now));
            }
            // no reply is awaited, so nothing is left open to close
            Some(pb::parent_request::Kind::Shutdown(_)) => {
                self.feed = None;
                self.run_active = false;
                self.abandon_pending();
                self.turn = None;
            }
            Some(pb::parent_request::Kind::ResumeCall(r)) => self.close_suspension(ext_result(r.result.as_ref())),
            Some(pb::parent_request::Kind::ResumeNameLookup(r)) => {
                let outcome = match r.kind {
                    Some(pb::resume_name_lookup::Kind::Value(_)) => "value",
                    Some(pb::resume_name_lookup::Kind::Undefined(_)) => "undefined",
                    Some(pb::resume_name_lookup::Kind::Error(_)) => "error",
                    None => "missing",
                };
                self.close_suspension(outcome);
            }
            Some(pb::parent_request::Kind::ResumeFutures(r)) => {
                let outcome = match (&self.pending, r.results.as_slice()) {
                    (
                        Some(Suspension {
                            kind: SuspensionKind::FunctionCall(Some(call_id)),
                            ..
                        }),
                        [result],
                    ) if *call_id == result.call_id => ext_result(result.result.as_ref()),
                    _ => "resolved",
                };
                self.close_suspension(outcome);
            }
            Some(pb::parent_request::Kind::AbortFeed(_)) => self.close_suspension("aborted"),
            None => {}
        }
    }

    /// Records one event from the worker: a suspension opens the pending
    /// round-trip, a turn-ending event closes the run or housekeeping turn.
    pub(crate) fn event(&mut self, event: &pb::ChildEvent) {
        // a load's reply stamps the restored session's cumulative clock, spent
        // in another process: re-base the delta ratchet so the next run
        // records only its own cost
        if matches!(self.turn, Some(("load", _))) {
            self.reported_micros = self.reported_micros.max(event.total_execution_micros);
        }
        match &event.kind {
            Some(pb::child_event::Kind::Print(p)) => {
                // One measurement per stream rather than per run: the
                // instrument is a counter, so the totals sum identically, and a
                // sandbox alternating the streams starts a run per fragment.
                let totals = print_bytes_by_stream(&p.segments);
                for (stream, bytes) in PRINT_STREAM_NAMES.into_iter().zip(totals) {
                    if bytes > 0 {
                        self.metrics.record(
                            &PRINT_BYTES,
                            MetricValue::bytes(bytes),
                            &[KeyValue::new("stream", stream)],
                        );
                    }
                }
            }
            Some(pb::child_event::Kind::FunctionCall(c)) => {
                self.suspend(SuspensionKind::FunctionCall(c.allow_eager_await.then_some(c.call_id)));
            }
            Some(pb::child_event::Kind::OsCall(c)) => self.suspend(SuspensionKind::OsCall(os_call(c.call.as_ref()))),
            Some(pb::child_event::Kind::NameLookup(_)) => self.suspend(SuspensionKind::NameLookup),
            Some(pb::child_event::Kind::ResolveFutures(_)) => self.suspend(SuspensionKind::ResolveFutures),
            Some(pb::child_event::Kind::Complete(_)) => self.end_run("complete", event),
            Some(pb::child_event::Kind::Error(_)) => {
                // an error answering an open housekeeping turn (`Dump`,
                // `Load`, `InstallDependencies`, ...) is that turn's outcome,
                // not a run's — no run happened, so recording an execution
                // sample would fake one. A failed dump even leaves the feed
                // suspended and resumable.
                if self.turn.is_some() {
                    self.end_turn("error");
                } else {
                    self.end_run("error", event);
                }
            }
            Some(pb::child_event::Kind::TypingError(_)) => self.end_run("typing_error", event),
            Some(pb::child_event::Kind::DumpResult(d)) => {
                self.metrics.record(
                    &SNAPSHOT_BYTES,
                    MetricValue::bytes(d.state.len()),
                    &[KeyValue::new("op", "dump")],
                );
                self.end_turn("ok");
            }
            Some(pb::child_event::Kind::Ok(_)) => self.end_turn("ok"),
            // the worker is about to exit; the pool counts the termination
            Some(pb::child_event::Kind::FatalError(_)) => self.end_terminal("fatal_error", event),
            Some(pb::child_event::Kind::Shutdown(_)) => self.end_terminal("shutdown", event),
            None => {}
        }
    }

    /// Counts one suspension and starts timing the host's answer, during which
    /// this worker is suspended.
    fn suspend(&mut self, kind: SuspensionKind) {
        // a suspension answering `Load` is a restored feed re-raising it: the
        // load itself is done, and the host round-trip that follows must not
        // be billed to the load turn
        if matches!(self.turn, Some(("load", _))) {
            self.end_turn("ok");
        }
        self.run_active = true;
        self.metrics.record(
            &SUSPENSIONS,
            MetricValue::I64(1),
            &[KeyValue::new("kind", kind.label())],
        );
        // a suspension raised while one is open would be a protocol violation
        // the checkout rejects; releasing first keeps the count honest anyway
        self.abandon_pending();
        self.pending = Some(Suspension {
            start: Instant::now(),
            kind,
        });
        self.metrics.suspended_workers(1);
    }

    /// Records the round-trip the answering resume just closed, under its kind
    /// and outcome alone — never the name of what was called, which the
    /// sandboxed code chooses (see the module docs).
    fn close_suspension(&mut self, outcome: &'static str) {
        let Some(suspension) = self.take_pending() else {
            return;
        };
        let elapsed = MetricValue::seconds(suspension.start.elapsed());
        let mut attributes = vec![
            KeyValue::new("kind", suspension.kind.label()),
            KeyValue::new("outcome", outcome),
        ];
        // only the os call names what it did, and only from the protocol's own
        // fixed set — see the cardinality rule in the module docs
        if let SuspensionKind::OsCall(function) = suspension.kind {
            attributes.push(KeyValue::new("function", function));
        }
        self.metrics.record(&EXT_CALL, elapsed, &attributes);
    }

    /// Drops the open suspension without recording a round-trip, for the turns
    /// that end one without answering it.
    fn abandon_pending(&mut self) {
        let _ = self.take_pending();
    }

    /// Takes the open suspension, releasing this worker's claim on
    /// [`SUSPENDED_WORKERS`]. The single place `pending` is cleared, so the count
    /// cannot drift from the state it describes.
    fn take_pending(&mut self) -> Option<Suspension> {
        let suspension = self.pending.take();
        if suspension.is_some() {
            self.metrics.suspended_workers(-1);
        }
        suspension
    }

    /// Ends an execution turn: its wall time, and the sandbox time it consumed.
    ///
    /// A feed restored mid-suspension has no start instant in this process, so
    /// it contributes execution time but no wall time.
    fn end_run(&mut self, outcome: &'static str, event: &pb::ChildEvent) {
        self.run_active = false;
        self.abandon_pending();
        self.end_turn(outcome);
        if let Some(start) = self.feed.take() {
            self.metrics.record(
                &RUN_DURATION,
                MetricValue::seconds(start.elapsed()),
                &[KeyValue::new("outcome", outcome)],
            );
        }
        // the reported total is cumulative for the session (and never rewinds,
        // even from a worker misreporting it), so this run's cost is the delta
        let total = event.total_execution_micros;
        let delta = total.saturating_sub(self.reported_micros);
        self.reported_micros = self.reported_micros.max(total);
        self.metrics
            .record(&RUN_EXECUTION, MetricValue::seconds(Duration::from_micros(delta)), &[]);
    }

    /// Ends a terminal event as a run or housekeeping turn.
    fn end_terminal(&mut self, outcome: &'static str, event: &pb::ChildEvent) {
        if self.run_active {
            self.end_run(outcome, event);
        } else {
            self.end_turn(outcome);
        }
    }

    /// Ends the open housekeeping turn, if there is one.
    fn end_turn(&mut self, outcome: &'static str) {
        if let Some((turn, start)) = self.turn.take() {
            self.metrics.record(
                &TURN_DURATION,
                MetricValue::seconds(start.elapsed()),
                &[KeyValue::new("turn", turn), KeyValue::new("outcome", outcome)],
            );
        }
    }
}

impl Drop for TurnMetrics {
    /// A worker killed mid-suspension is no longer waiting on the host, and
    /// nothing else will close its round-trip.
    fn drop(&mut self) {
        self.abandon_pending();
    }
}

/// The suspension a feed is blocked on while the host answers it.
struct Suspension {
    start: Instant,
    kind: SuspensionKind,
}

/// One variant per suspension the protocol has: the four child events that
/// hand control to the host and wait for a `Resume*`.
///
/// Function call IDs correlate eager replies but are never metric attributes.
/// Only OS calls record their fixed function name; sandbox-chosen names are
/// omitted to bound cardinality (see the module docs).
enum SuspensionKind {
    /// The call ID when an eager reply is allowed.
    FunctionCall(Option<u32>),
    /// Carries the call's fixed name, which is the protocol's, not the
    /// sandbox's.
    OsCall(&'static str),
    NameLookup,
    ResolveFutures,
}

impl SuspensionKind {
    /// Value of the `kind` attribute this suspension is recorded under.
    const fn label(&self) -> &'static str {
        match self {
            Self::FunctionCall(_) => "function",
            Self::OsCall(_) => "os",
            Self::NameLookup => "name_lookup",
            Self::ResolveFutures => "futures",
        }
    }
}

/// Classifies the host's answer to a call suspension.
fn ext_result(result: Option<&pb::ExtFunctionResult>) -> &'static str {
    match result.and_then(|result| result.kind.as_ref()) {
        Some(pb::ext_function_result::Kind::ReturnValue(_)) => "value",
        Some(pb::ext_function_result::Kind::Error(_)) => "error",
        Some(pb::ext_function_result::Kind::Future(_)) => "future",
        Some(pb::ext_function_result::Kind::NotFound(_)) => "not_found",
        Some(pb::ext_function_result::Kind::NotHandled(_)) => "not_handled",
        None => "missing",
    }
}

/// The fixed name of an OS call — the protocol's own, so it is safe as an
/// attribute where anything the sandbox names is not.
///
/// The names match the `os call {function}` spans in
/// [`tracing`](crate::telemetry::tracing); keep the two lists in step.
fn os_call(call: Option<&Call>) -> &'static str {
    match call {
        Some(Call::Exists(_)) => "exists",
        Some(Call::IsFile(_)) => "is_file",
        Some(Call::IsDir(_)) => "is_dir",
        Some(Call::IsSymlink(_)) => "is_symlink",
        Some(Call::ReadText(_)) => "read_text",
        Some(Call::ReadBytes(_)) => "read_bytes",
        Some(Call::Stat(_)) => "stat",
        Some(Call::Iterdir(_)) => "iterdir",
        Some(Call::Resolve(_)) => "resolve",
        Some(Call::Absolute(_)) => "absolute",
        Some(Call::Unlink(_)) => "unlink",
        Some(Call::Rmdir(_)) => "rmdir",
        Some(Call::WriteText(_)) => "write_text",
        Some(Call::AppendText(_)) => "append_text",
        Some(Call::WriteBytes(_)) => "write_bytes",
        Some(Call::AppendBytes(_)) => "append_bytes",
        Some(Call::Open(_)) => "open",
        Some(Call::Mkdir(_)) => "mkdir",
        Some(Call::Rename(_)) => "rename",
        Some(Call::Getenv(_)) => "getenv",
        Some(Call::GetEnviron(_)) => "get_environ",
        Some(Call::DateToday(_)) => "date_today",
        Some(Call::DateTimeNow(_)) => "date_time_now",
        Some(Call::Urandom(_)) => "urandom",
        Some(Call::Time(_)) => "time",
        Some(Call::Sleep(_)) => "sleep",
        Some(Call::AsyncSleep(_)) => "async_sleep",
        Some(Call::SystemSleep(_)) => "system_sleep",
        Some(Call::AsyncSystemSleep(_)) => "async_system_sleep",
        None => "unknown",
    }
}

/// The stream names a print measurement can be labelled with, in the order
/// [`print_bytes_by_stream`] totals them.
///
/// An unrecognised value keeps its own total rather than folding into stdout,
/// so a stream added to the protocol later cannot quietly inflate one.
const PRINT_STREAM_NAMES: [&str; 3] = ["stdout", "stderr", "unspecified"];

/// Totals one `Print` event's text bytes per stream, indexed into
/// [`PRINT_STREAM_NAMES`].
fn print_bytes_by_stream(segments: &[pb::PrintSegment]) -> [usize; 3] {
    let mut totals = [0usize; 3];
    for segment in segments {
        let index = match pb::PrintStream::try_from(segment.stream) {
            Ok(pb::PrintStream::Stdout) => 0,
            Ok(pb::PrintStream::Stderr) => 1,
            _ => 2,
        };
        totals[index] = totals[index].saturating_add(segment.text.len());
    }
    totals
}
// tests live here rather than in `tests/` because `TurnMetrics` is
// crate-private: recording is a side effect of the worker, not part of the
// pool's public API. `tests/metrics.rs` covers the pool-level instruments,
// which a public `Pool` does emit.
#[cfg(test)]
mod tests {
    use std::{
        sync::{Arc, Barrier, Mutex, PoisonError},
        thread,
        time::Duration,
    };

    use logfire::{Logfire, config::MetricsOptions};
    use monty_proto::{WireFunctionCall, ext_result_to_proto, pb, pb::os_call::Call};
    use monty_types::{CallArgs, ExtFunctionResult, MontyObject, NameLookupResult, SourceRange};
    use opentelemetry::{
        KeyValue,
        trace::{SpanId, TraceId},
    };
    use opentelemetry_sdk::{
        logs::SdkLogRecord,
        metrics::{
            InMemoryMetricExporter, PeriodicReader,
            data::{AggregatedMetrics, MetricData},
        },
        trace::SpanData,
    };

    use super::{Measurement, MetricValue, Metrics, TelemetryAdapter, TurnMetrics, print_bytes_by_stream};

    /// The suspension position every hand-built event carries.
    fn position() -> SourceRange {
        SourceRange {
            filename: "main.py".to_owned(),
            start: 0,
            end: 7,
        }
    }
    /// A cumulative aggregate exported from the test's Logfire provider.
    struct Capture {
        logfire: Logfire,
        exporter: InMemoryMetricExporter,
    }

    /// One exported metric point, flattened for focused state-machine assertions.
    struct Recorded {
        name: String,
        value: Aggregate,
        attributes: Vec<(String, String)>,
    }

    /// The aggregate shapes produced by Monty's instruments.
    enum Aggregate {
        I64(i64),
        U64(u64),
        Histogram {
            count: usize,
            sum: f64,
            min: Option<f64>,
            max: Option<f64>,
        },
    }

    impl Capture {
        /// The attributes of every time series recorded under `name`.
        fn attributes(&self, name: &str) -> Vec<Vec<(String, String)>> {
            let mut series = self.select(name, |recorded| {
                let mut attributes = recorded.attributes.clone();
                attributes.sort();
                attributes
            });
            series.sort();
            series
        }

        /// The current value of an integral up/down counter.
        fn i64_sum(&self, name: &str) -> i64 {
            self.select(name, |recorded| match recorded.value {
                Aggregate::I64(value) => value,
                _ => panic!("{name} was not an i64 sum"),
            })
            .into_iter()
            .sum()
        }

        /// Total value across a monotonic counter's attribute series.
        fn u64_sum(&self, name: &str) -> u64 {
            self.select(name, |recorded| match recorded.value {
                Aggregate::U64(value) => value,
                _ => panic!("{name} was not a u64 sum"),
            })
            .into_iter()
            .sum()
        }

        /// Histogram aggregates under `name`.
        fn histograms(&self, name: &str) -> Vec<(usize, f64, Option<f64>, Option<f64>)> {
            self.select(name, |recorded| match recorded.value {
                Aggregate::Histogram { count, sum, min, max } => (count, sum, min, max),
                _ => panic!("{name} was not a histogram"),
            })
        }

        /// Whether the latest collection includes `name`.
        fn has(&self, name: &str) -> bool {
            self.records().iter().any(|recorded| recorded.name == name)
        }

        /// Selects every point from the latest cumulative collection.
        fn select<T>(&self, name: &str, map: impl Fn(&Recorded) -> T) -> Vec<T> {
            self.records()
                .iter()
                .filter(|recorded| recorded.name == name)
                .map(map)
                .collect()
        }

        /// Flushes and flattens the provider's latest cumulative collection.
        fn records(&self) -> Vec<Recorded> {
            self.exporter.reset();
            self.logfire.force_flush().unwrap();
            let exported = self.exporter.get_finished_metrics().unwrap();
            let mut records = Vec::new();
            for resource in &exported {
                for scope in resource.scope_metrics() {
                    for metric in scope.metrics() {
                        flatten(metric.name(), metric.data(), &mut records);
                    }
                }
            }
            records
        }
    }

    /// Adds each supported aggregate data point to the flattened capture.
    fn flatten(name: &str, data: &AggregatedMetrics, records: &mut Vec<Recorded>) {
        match data {
            AggregatedMetrics::I64(MetricData::Sum(sum)) => {
                for point in sum.data_points() {
                    records.push(Recorded {
                        name: name.to_owned(),
                        value: Aggregate::I64(point.value()),
                        attributes: attributes(point.attributes()),
                    });
                }
            }
            AggregatedMetrics::U64(MetricData::Sum(sum)) => {
                for point in sum.data_points() {
                    records.push(Recorded {
                        name: name.to_owned(),
                        value: Aggregate::U64(point.value()),
                        attributes: attributes(point.attributes()),
                    });
                }
            }
            AggregatedMetrics::F64(MetricData::ExponentialHistogram(histogram)) => {
                for point in histogram.data_points() {
                    records.push(Recorded {
                        name: name.to_owned(),
                        value: Aggregate::Histogram {
                            count: point.count(),
                            sum: point.sum(),
                            min: point.min(),
                            max: point.max(),
                        },
                        attributes: attributes(point.attributes()),
                    });
                }
            }
            other => panic!("unexpected Monty metric aggregate: {other:?}"),
        }
    }

    /// Flattens OTel attributes to their stable string representation.
    fn attributes<'a>(values: impl Iterator<Item = &'a KeyValue>) -> Vec<(String, String)> {
        values.map(|kv| (kv.key.to_string(), kv.value.to_string())).collect()
    }

    /// A recorder writing into a fresh local Logfire provider.
    /// A foreign-SDK adapter that keeps every raw measurement, so a test can
    /// count them — the aggregated exporter only ever shows their sum.
    #[derive(Default)]
    struct MeasurementCapture(Mutex<Vec<(String, String, i64)>>);

    impl TelemetryAdapter for MeasurementCapture {
        fn start_span(&self, _: &SpanData) -> bool {
            true
        }

        fn end_span(&self, _: &SpanData) -> bool {
            true
        }

        fn emit_log(&self, _: SpanId, _: &SdkLogRecord) -> bool {
            true
        }

        fn disable_root(&self, _: TraceId, _: SpanId) {}

        fn record_metric(&self, measurement: &Measurement<'_>) {
            let stream = measurement
                .attributes
                .iter()
                .find(|kv| kv.key.as_str() == "stream")
                .map_or_else(String::new, |kv| kv.value.to_string());
            let value = match measurement.value {
                MetricValue::I64(value) => value,
                other @ MetricValue::F64(_) => panic!("{} recorded {other:?}", measurement.name),
            };
            self.0
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .push((measurement.name.to_owned(), stream, value));
        }
    }

    fn recorder() -> (TurnMetrics, Arc<Capture>) {
        let exporter = InMemoryMetricExporter::default();
        let logfire = logfire::configure()
            .local()
            .send_to_logfire(false)
            .with_metrics(Some(
                MetricsOptions::default().with_additional_reader(PeriodicReader::builder(exporter.clone()).build()),
            ))
            .finish()
            .unwrap();
        let metrics = TurnMetrics::new(Metrics::for_logfire(logfire.clone()));
        (metrics, Arc::new(Capture { logfire, exporter }))
    }

    fn request(kind: pb::parent_request::Kind) -> pb::ParentRequest {
        pb::ParentRequest {
            kind: Some(kind),
            trace_parent: None,
        }
    }

    fn event(kind: pb::child_event::Kind) -> pb::ChildEvent {
        pb::ChildEvent {
            kind: Some(kind),
            total_execution_micros: 0,
            max_suspensions: None,
            restored_script_name: None,
            session_id: None,
            feed_execution_micros: 0,
            max_feed_duration_micros: None,
            max_turn_duration_micros: None,
            max_total_sleep_micros: None,
        }
    }

    fn feed() -> pb::ParentRequest {
        request(pb::parent_request::Kind::Feed(pb::Feed {
            code: "double(2)".to_owned(),
            inputs: vec![].into(),
            values: None,
            skip_type_check: false,
            cwd: "/".to_owned(),
        }))
    }

    fn call_event(function_name: &str) -> pb::ChildEvent {
        event(pb::child_event::Kind::FunctionCall(WireFunctionCall::new(
            function_name.to_owned(),
            CallArgs::new(),
            1,
            None,
            false,
            position(),
        )))
    }

    fn resume_call(kind: pb::ext_function_result::Kind) -> pb::ParentRequest {
        request(pb::parent_request::Kind::ResumeCall(pb::ResumeCall {
            call_id: 1,
            result: Some(pb::ExtFunctionResult { kind: Some(kind) }),
            values: None,
        }))
    }

    /// A `ResumeCall` returning `value`, with the arena it indexes.
    fn resume_return(value: MontyObject) -> pb::ParentRequest {
        let (result, values) = ext_result_to_proto(ExtFunctionResult::Return(value));
        request(pb::parent_request::Kind::ResumeCall(pb::ResumeCall {
            call_id: 1,
            result: Some(result),
            values,
        }))
    }

    fn attribute<'a>(attributes: &'a [(String, String)], key: &str) -> Option<&'a str> {
        attributes
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, value)| value.as_str())
    }

    /// Asserts two small metric values agree despite floating-point addition.
    fn assert_close(actual: f64, expected: f64) {
        assert!((actual - expected).abs() < 1e-12, "{actual} != {expected}");
    }

    /// Applies [`assert_close`] to two optional metric bounds.
    fn assert_optional_close(actual: Option<f64>, expected: Option<f64>) {
        match (actual, expected) {
            (Some(actual), Some(expected)) => assert_close(actual, expected),
            (None, None) => {}
            _ => panic!("{actual:?} != {expected:?}"),
        }
    }

    /// One feed with one host call: the suspension is counted, the round-trip
    /// timed on its own, and the run recorded when the completion arrives.
    #[test]
    fn a_feed_records_its_run_and_its_round_trip() {
        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&feed());
        metrics.event(&call_event("double"));
        metrics.begin_turn(&resume_return(MontyObject::int(4)));
        metrics.event(&event(pb::child_event::Kind::Complete(pb::Complete::from(
            MontyObject::int(4),
        ))));

        assert_eq!(
            capture.attributes("monty.run.suspensions"),
            [[("kind".to_owned(), "function".to_owned())]]
        );
        assert_eq!(
            capture.attributes("monty.ext.call.duration"),
            [[
                ("kind".to_owned(), "function".to_owned()),
                ("outcome".to_owned(), "value".to_owned())
            ]]
        );
        assert_eq!(
            capture.attributes("monty.run.duration"),
            [[("outcome".to_owned(), "complete".to_owned())]]
        );
        assert_eq!(capture.histograms("monty.run.execution_time")[0].0, 1);
        assert_eq!(capture.u64_sum("monty.run.suspensions"), 1);
    }

    /// The called name is chosen by the sandboxed code, so it must never reach
    /// an attribute — under *any* outcome, since a script can mint one name per
    /// call and each distinct value would cost the host a time series.
    ///
    /// `error` is the outcome that makes this more than theory: calling a
    /// method a host object does not have raises `AttributeError` there, which
    /// comes back as `error` rather than `not_found`. And `value` is no safer,
    /// because a host whose lookup is a callable resolves every name.
    #[test]
    fn sandbox_chosen_names_never_reach_attributes() {
        let (mut metrics, capture) = recorder();
        let outcomes = [
            pb::ext_function_result::Kind::NotFound("attacker_chosen".to_owned()),
            pb::ext_function_result::Kind::Error(pb::RaisedException {
                exc_type: "AttributeError".to_owned(),
                message: None,
                traceback: vec![].into(),
                data: None,
            }),
            pb::ext_function_result::Kind::ReturnValue(0),
        ];
        metrics.begin_turn(&feed());
        for (index, kind) in outcomes.into_iter().enumerate() {
            metrics.event(&call_event(&format!("attacker_chosen_{index}")));
            metrics.begin_turn(&resume_call(kind));
        }

        for attributes in capture.attributes("monty.ext.call.duration") {
            let keys: Vec<&str> = attributes.iter().map(|(key, _)| key.as_str()).collect();
            assert_eq!(keys, ["kind", "outcome"], "{attributes:?}");
        }
        let outcomes: Vec<_> = capture
            .attributes("monty.ext.call.duration")
            .iter()
            .map(|attributes| attribute(attributes, "outcome").unwrap().to_owned())
            .collect();
        assert_eq!(outcomes, ["error", "not_found", "value"]);
    }

    /// A sandbox that alternates between the streams starts a segment per
    /// fragment, so recording one measurement per segment would let it charge
    /// the host a measurement per byte. The instrument is a counter, so the
    /// per-stream totals carry the same numbers at one measurement per stream.
    #[test]
    fn a_print_event_records_one_measurement_per_stream() {
        let alternating: Vec<pb::PrintSegment> = (0..64)
            .map(|i| pb::PrintSegment {
                stream: if i % 2 == 0 {
                    pb::PrintStream::Stdout as i32
                } else {
                    pb::PrintStream::Stderr as i32
                },
                text: "a".to_owned(),
            })
            .collect();
        assert_eq!(print_bytes_by_stream(&alternating), [32, 32, 0]);

        let capture = Arc::new(MeasurementCapture::default());
        let mut metrics = TurnMetrics::new(Metrics::for_adapter(capture.clone()));
        metrics.begin_turn(&feed());
        metrics.event(&event(pb::child_event::Kind::Print(pb::Print {
            segments: alternating.into(),
        })));

        let recorded = capture.0.lock().unwrap_or_else(PoisonError::into_inner).clone();
        assert_eq!(
            recorded,
            [
                ("monty.print.bytes".to_owned(), "stdout".to_owned(), 32),
                ("monty.print.bytes".to_owned(), "stderr".to_owned(), 32),
            ]
        );
    }

    /// A stream that printed nothing gets no measurement, so an unrecognised
    /// value cannot mint a series for output that never happened.
    #[test]
    fn a_print_event_skips_streams_with_no_output() {
        let capture = Arc::new(MeasurementCapture::default());
        let mut metrics = TurnMetrics::new(Metrics::for_adapter(capture.clone()));
        metrics.begin_turn(&feed());
        metrics.event(&event(pb::child_event::Kind::Print(pb::Print {
            segments: vec![pb::PrintSegment {
                stream: pb::PrintStream::Stdout as i32,
                text: "hello\n".to_owned(),
            }]
            .into(),
        })));

        let recorded = capture.0.lock().unwrap_or_else(PoisonError::into_inner).clone();
        assert_eq!(recorded, [("monty.print.bytes".to_owned(), "stdout".to_owned(), 6)]);
    }

    /// An os call is the one suspension that names what it did — from the
    /// protocol's fixed set, so it costs a bounded number of series.
    #[test]
    fn os_calls_carry_their_protocol_name() {
        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&feed());
        metrics.event(&event(pb::child_event::Kind::OsCall(pb::OsCall {
            call_id: 1,
            values: None,
            allow_eager_await: false,
            position: Some((&position()).into()),
            call: Some(Call::ReadText("/mnt/f.txt".to_owned())),
        })));
        metrics.begin_turn(&resume_return(MontyObject::string("hello".to_owned())));

        assert_eq!(
            capture.attributes("monty.ext.call.duration"),
            [[
                ("function".to_owned(), "read_text".to_owned()),
                ("kind".to_owned(), "os".to_owned()),
                ("outcome".to_owned(), "value".to_owned())
            ]]
        );
    }

    /// The worker reports its execution clock cumulatively, so each run
    /// contributes the delta rather than the session total.
    #[test]
    fn execution_time_is_the_delta_of_a_cumulative_clock() {
        let (mut metrics, capture) = recorder();
        for total in [100, 250] {
            metrics.begin_turn(&feed());
            metrics.event(&pb::ChildEvent {
                kind: Some(pb::child_event::Kind::Complete(pb::Complete { value: 0, values: None })),
                total_execution_micros: total,
                max_suspensions: None,
                restored_script_name: None,
                session_id: None,
                feed_execution_micros: 0,
                max_feed_duration_micros: None,
                max_turn_duration_micros: None,
                max_total_sleep_micros: None,
            });
        }

        let execution = capture.histograms("monty.run.execution_time");
        let [(count, sum, min, max)] = execution.as_slice() else {
            panic!("expected one execution-time series")
        };
        assert_eq!(*count, 2);
        assert_close(*sum, Duration::from_micros(250).as_secs_f64());
        assert_optional_close(*min, Some(Duration::from_micros(100).as_secs_f64()));
        assert_optional_close(*max, Some(Duration::from_micros(150).as_secs_f64()));
    }

    /// A raised exception ends the run and shows up as its outcome. The class
    /// itself is not recorded anywhere: the sandbox names it.
    #[test]
    fn an_exception_ends_the_run_without_describing_itself() {
        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&feed());
        metrics.event(&event(pb::child_event::Kind::Error(pb::Error {
            exception: Some(pb::RaisedException {
                exc_type: "MyCustomError".to_owned(),
                message: None,
                traceback: vec![].into(),
                data: None,
            }),
        })));

        assert_eq!(
            capture.attributes("monty.run.duration"),
            [[("outcome".to_owned(), "error".to_owned())]]
        );
    }

    /// Terminal events close the operation that received them before checkout
    /// teardown drops the worker-side recorder.
    #[test]
    fn terminal_events_end_runs_and_housekeeping_turns() {
        let terminal_events = [
            (
                pb::child_event::Kind::FatalError(pb::FatalError {
                    message: "worker failed".to_owned(),
                }),
                "fatal_error",
            ),
            (
                pb::child_event::Kind::Shutdown(pb::ShutdownDump { dump: None }),
                "shutdown",
            ),
        ];
        for (kind, outcome) in terminal_events {
            let (mut metrics, capture) = recorder();
            metrics.begin_turn(&feed());
            metrics.event(&event(kind));
            assert_eq!(
                capture.attributes("monty.run.duration"),
                [[("outcome".to_owned(), outcome.to_owned())]]
            );
            assert_eq!(capture.histograms("monty.run.execution_time")[0].0, 1);
        }

        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&request(pb::parent_request::Kind::InstallDependencies(
            pb::InstallDependencies {
                requirements: vec![].into(),
            },
        )));
        metrics.event(&event(pb::child_event::Kind::Shutdown(pb::ShutdownDump { dump: None })));
        assert_eq!(
            capture.attributes("monty.turn.duration"),
            [[
                ("outcome".to_owned(), "shutdown".to_owned()),
                ("turn".to_owned(), "install_dependencies".to_owned())
            ]]
        );
        assert!(!capture.has("monty.run.execution_time"));
    }

    /// A restored session's cumulative clock was spent in another process, so
    /// the load's reply re-bases the ratchet and the next run records only
    /// its own delta — not the whole restored history.
    #[test]
    fn a_load_rebases_the_execution_clock() {
        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&request(pb::parent_request::Kind::Load(pb::Load {
            state: vec![].into(),
        })));
        metrics.event(&pb::ChildEvent {
            kind: Some(pb::child_event::Kind::Ok(pb::Ok {})),
            total_execution_micros: 10_000_000,
            max_suspensions: None,
            restored_script_name: Some("dumped.py".to_owned()),
            session_id: None,
            feed_execution_micros: 0,
            max_feed_duration_micros: None,
            max_turn_duration_micros: None,
            max_total_sleep_micros: None,
        });
        metrics.begin_turn(&feed());
        metrics.event(&pb::ChildEvent {
            kind: Some(pb::child_event::Kind::Complete(pb::Complete { value: 0, values: None })),
            total_execution_micros: 10_000_100,
            max_suspensions: None,
            restored_script_name: None,
            session_id: None,
            feed_execution_micros: 0,
            max_feed_duration_micros: None,
            max_turn_duration_micros: None,
            max_total_sleep_micros: None,
        });

        let execution = capture.histograms("monty.run.execution_time");
        assert_eq!(execution[0].0, 1);
        assert_close(execution[0].1, Duration::from_micros(100).as_secs_f64());
    }

    /// A feed restored mid-suspension re-raises the suspension as the load's
    /// reply: that ends the load turn — the host round-trip that follows is
    /// not the load's cost — and the eventual completion contributes only the
    /// post-resume execution delta, with no wall time (the feed's start was
    /// never seen in this process).
    #[test]
    fn a_restored_suspension_closes_the_load_turn() {
        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&request(pb::parent_request::Kind::Load(pb::Load {
            state: vec![].into(),
        })));
        metrics.event(&pb::ChildEvent {
            kind: Some(pb::child_event::Kind::NameLookup(pb::NameLookup {
                name: "value".to_owned(),
                object_id: None,
                position: Some((&position()).into()),
            })),
            total_execution_micros: 10_000_000,
            max_suspensions: None,
            restored_script_name: None,
            session_id: None,
            feed_execution_micros: 0,
            max_feed_duration_micros: None,
            max_turn_duration_micros: None,
            max_total_sleep_micros: None,
        });
        let turns = capture.attributes("monty.turn.duration");
        assert_eq!(
            turns,
            [[
                ("outcome".to_owned(), "ok".to_owned()),
                ("turn".to_owned(), "load".to_owned())
            ]]
        );

        metrics.begin_turn(&request(pb::parent_request::Kind::ResumeNameLookup(
            NameLookupResult::from(MontyObject::int(1)).into(),
        )));
        metrics.event(&pb::ChildEvent {
            kind: Some(pb::child_event::Kind::Complete(pb::Complete { value: 0, values: None })),
            total_execution_micros: 10_000_050,
            max_suspensions: None,
            restored_script_name: None,
            session_id: None,
            feed_execution_micros: 0,
            max_feed_duration_micros: None,
            max_turn_duration_micros: None,
            max_total_sleep_micros: None,
        });
        let execution = capture.histograms("monty.run.execution_time");
        assert_eq!(execution[0].0, 1);
        assert_close(execution[0].1, Duration::from_micros(50).as_secs_f64());
        assert!(!capture.has("monty.run.duration"));
    }

    /// An error answering a housekeeping turn is that turn's outcome; no run
    /// happened, so no run instruments may record.
    #[test]
    fn a_failed_housekeeping_turn_is_not_a_run() {
        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&request(pb::parent_request::Kind::InstallDependencies(
            pb::InstallDependencies {
                requirements: vec!["pydantic".to_owned()].into(),
            },
        )));
        metrics.event(&event(pb::child_event::Kind::Error(pb::Error {
            exception: Some(pb::RaisedException {
                exc_type: "ValueError".to_owned(),
                message: None,
                traceback: vec![].into(),
                data: None,
            }),
        })));

        assert_eq!(
            capture.attributes("monty.turn.duration"),
            [[
                ("outcome".to_owned(), "error".to_owned()),
                ("turn".to_owned(), "install_dependencies".to_owned())
            ]]
        );
        assert!(!capture.has("monty.run.duration"));
        assert!(!capture.has("monty.run.execution_time"));
    }

    /// Worker adjustments commute across concurrent users of one `Metrics`.
    #[test]
    fn worker_adjustments_are_commutative_under_concurrency() {
        const THREADS: usize = 8;
        const ITERATIONS: usize = 100;

        let (_, capture) = recorder();
        let metrics = Metrics::for_logfire(capture.logfire.clone());
        let barrier = Arc::new(Barrier::new(THREADS));
        let threads: Vec<_> = (0..THREADS)
            .map(|_| {
                let metrics = metrics.clone();
                let barrier = Arc::clone(&barrier);
                thread::spawn(move || {
                    barrier.wait();
                    for _ in 0..ITERATIONS {
                        metrics.live_workers(1);
                        metrics.idle_workers(1);
                        metrics.idle_workers(-1);
                        metrics.live_workers(-1);
                    }
                })
            })
            .collect();
        for thread in threads {
            thread.join().unwrap();
        }

        assert_eq!(capture.i64_sum("monty.pool.workers.live"), 0);
        assert_eq!(capture.i64_sum("monty.pool.workers.idle"), 0);
    }

    /// The suspended-worker count rises while the host owns a suspension and
    /// falls however it ends: answered, aborted, or abandoned with a dead worker.
    #[test]
    fn suspended_workers_tracks_host_round_trips() {
        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&feed());
        metrics.event(&call_event("double"));
        assert_eq!(capture.i64_sum("monty.pool.workers.suspended"), 1);
        metrics.begin_turn(&resume_return(MontyObject::int(4)));
        assert_eq!(capture.i64_sum("monty.pool.workers.suspended"), 0);

        metrics.event(&call_event("double"));
        metrics.begin_turn(&request(pb::parent_request::Kind::AbortFeed(pb::AbortFeed {
            exception: None,
        })));
        assert_eq!(capture.i64_sum("monty.pool.workers.suspended"), 0);
        assert!(capture.attributes("monty.ext.call.duration").contains(&vec![
            ("kind".to_owned(), "function".to_owned()),
            ("outcome".to_owned(), "aborted".to_owned()),
        ]));
        metrics.event(&event(pb::child_event::Kind::Error(pb::Error { exception: None })));

        // a worker dropped mid-suspension is no longer waiting on anyone
        metrics.begin_turn(&feed());
        metrics.event(&call_event("double"));
        assert_eq!(capture.i64_sum("monty.pool.workers.suspended"), 1);
        drop(metrics);
        assert_eq!(capture.i64_sum("monty.pool.workers.suspended"), 0);
    }

    /// A dump is a housekeeping turn: it reports its own size and duration and
    /// leaves the feed it interrupted open.
    #[test]
    fn a_dump_reports_its_size_without_ending_the_run() {
        let (mut metrics, capture) = recorder();
        metrics.begin_turn(&feed());
        metrics.begin_turn(&request(pb::parent_request::Kind::Dump(pb::Dump {})));
        metrics.event(&event(pb::child_event::Kind::DumpResult(pb::DumpResult {
            state: vec![0; 32].into(),
        })));

        let snapshots = capture.histograms("monty.snapshot.bytes");
        assert_eq!(snapshots[0].0, 1);
        assert_close(snapshots[0].1, 32.0);
        let turns = capture.attributes("monty.turn.duration");
        assert_eq!(attribute(&turns[0], "turn"), Some("dump"));
        assert!(!capture.has("monty.run.duration"));
    }

    /// A Rust host records into instruments of its own rather than through an
    /// adapter: the measurements have to reach its meter provider, and the
    /// duration histograms have to come out exponential (the SDK's default
    /// buckets would put every monty turn in the first one).
    #[test]
    fn a_logfire_host_records_into_its_own_meter() {
        let exporter = InMemoryMetricExporter::default();
        let logfire = logfire::configure()
            .local()
            .send_to_logfire(false)
            .with_metrics(Some(
                MetricsOptions::default().with_additional_reader(PeriodicReader::builder(exporter.clone()).build()),
            ))
            .finish()
            .unwrap();
        let mut metrics = TurnMetrics::new(Metrics::for_logfire(logfire.clone()));

        metrics.begin_turn(&feed());
        metrics.event(&call_event("double"));
        metrics.begin_turn(&resume_return(MontyObject::int(4)));
        metrics.event(&event(pb::child_event::Kind::Complete(pb::Complete {
            value: 0,
            values: None,
        })));
        logfire.force_flush().unwrap();

        let exported = exporter.get_finished_metrics().unwrap();
        let mut found = Vec::new();
        for resource in &exported {
            for scope in resource.scope_metrics() {
                for metric in scope.metrics() {
                    found.push((metric.name().to_owned(), is_exponential(metric.data())));
                }
            }
        }
        found.sort();
        assert_eq!(
            found,
            [
                ("monty.ext.call.duration".to_owned(), true),
                ("monty.pool.workers.suspended".to_owned(), false),
                ("monty.run.duration".to_owned(), true),
                ("monty.run.execution_time".to_owned(), true),
                ("monty.run.suspensions".to_owned(), false),
            ]
        );
    }

    /// Whether an exported metric used base-2 exponential bucketing.
    fn is_exponential(data: &AggregatedMetrics) -> bool {
        match data {
            AggregatedMetrics::F64(data) => matches!(data, MetricData::ExponentialHistogram(_)),
            AggregatedMetrics::U64(data) => matches!(data, MetricData::ExponentialHistogram(_)),
            AggregatedMetrics::I64(data) => matches!(data, MetricData::ExponentialHistogram(_)),
        }
    }
}