apr-cli 0.32.0

CLI tool for APR model inspection, debugging, and operations
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
//! CUDA OOM postmortem classifier (CRUX-F-13).
//!
//! Pure, deterministic classifiers that discharge FALSIFY-CRUX-F-13-{001,002,003,005}
//! at the PARTIAL_ALGORITHM_LEVEL — algorithm-level necessary conditions on
//! an already-captured `/tmp/apr-oom-<ts>.json` postmortem file:
//!
//!   * `classify_oom_schema` — report object has all 7 required top-level keys
//!     with correct types (numbers, arrays, object, RFC3339 string).
//!   * `classify_oom_invariants` — peak_reserved >= peak_allocated >= 0,
//!     last_100_ops length <= 100, tensor_histogram non-empty,
//!     largest_alloc_stack non-empty, exit_code != 0.
//!   * `classify_oom_size` — serialized report size < 10 MiB (no raw tensor
//!     data leaked into the postmortem).
//!
//! FALSIFY-CRUX-F-13-004 (stderr breadcrumb) is discharged by the CLI layer
//! in `oom_lint.rs` via `classify_oom_breadcrumb` over a captured stderr body.
//!
//! Full discharge blocks on a live CUDA OOM trigger in `aprender-serve`
//! (BLOCKER-UPSTREAM-MISSING — no OOM hook plumbed yet as of 2026-04-21).

use serde_json::Value;

/// The 7 required top-level keys on a CRUX-F-13 OOM postmortem report
/// (see `contracts/crux-F-13-v1.yaml` equation `oom_report_schema`).
pub const OOM_REPORT_REQUIRED_KEYS: &[&str] = &[
    "peak_allocated_bytes",
    "peak_reserved_bytes",
    "largest_alloc_stack",
    "tensor_histogram",
    "last_100_ops",
    "exit_code",
    "timestamp",
];

/// Maximum allowed size of a serialized OOM report file, in bytes.
/// Contract: `sizeof(report.json) < 10 * 1024 * 1024`.
pub const OOM_REPORT_MAX_SIZE_BYTES: u64 = 10 * 1024 * 1024;

/// Maximum number of entries allowed in `last_100_ops`.
pub const OOM_REPORT_MAX_OPS: usize = 100;

/// Outcome of `classify_oom_schema`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OomSchemaOutcome {
    /// Report is a JSON object with all 7 required keys present and of correct type.
    Ok,
    /// Report root is not a JSON object.
    NotAnObject,
    /// One of the 7 required top-level keys is absent.
    MissingRequiredKey { key: &'static str },
    /// `peak_allocated_bytes` or `peak_reserved_bytes` is not a JSON number.
    PeakBytesNotNumber { key: &'static str },
    /// `largest_alloc_stack` is not a JSON array of strings.
    StackNotStringArray,
    /// `tensor_histogram` is not a JSON object.
    HistogramNotObject,
    /// `last_100_ops` is not a JSON array.
    OpsNotArray,
    /// `exit_code` is not a JSON number.
    ExitCodeNotNumber,
    /// `timestamp` is not a JSON string.
    TimestampNotString,
}

/// Outcome of `classify_oom_invariants`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OomInvariantsOutcome {
    /// All invariants hold.
    Ok,
    /// `peak_reserved_bytes < peak_allocated_bytes` — CUDA accounting bug.
    ReservedLessThanAllocated { reserved: u64, allocated: u64 },
    /// `last_100_ops` array length exceeds 100.
    TooManyOps { got: usize },
    /// `tensor_histogram` has zero buckets.
    EmptyHistogram,
    /// `largest_alloc_stack` is empty.
    EmptyStack,
    /// `exit_code == 0` — OOM was silent-swallowed.
    SilentExitCode,
}

/// Outcome of `classify_oom_size`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OomSizeOutcome {
    /// Serialized file size < 10 MiB.
    Ok { bytes: u64 },
    /// Serialized file size >= 10 MiB — raw tensor data leaked into the postmortem.
    TooLarge { bytes: u64, cap_bytes: u64 },
}

/// Outcome of `classify_oom_breadcrumb`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OomBreadcrumbOutcome {
    /// Stderr has a single `OOM_REPORT path=/tmp/apr-oom-<digits>.json` line.
    Ok { path: String },
    /// Zero breadcrumb lines found.
    NotFound,
    /// Breadcrumb line present but path does not match `/tmp/apr-oom-<digits>.json`.
    MalformedPath { got: String },
}

/// Non-streaming schema gate.
/// Pure function of `report`; deterministic.
#[allow(clippy::too_many_lines)]
pub fn classify_oom_schema(report: &Value) -> OomSchemaOutcome {
    let obj = match report.as_object() {
        Some(o) => o,
        None => return OomSchemaOutcome::NotAnObject,
    };
    for &key in OOM_REPORT_REQUIRED_KEYS {
        if !obj.contains_key(key) {
            return OomSchemaOutcome::MissingRequiredKey { key };
        }
    }
    for key in ["peak_allocated_bytes", "peak_reserved_bytes"] {
        if !obj[key].is_number() {
            let static_key: &'static str = match key {
                "peak_allocated_bytes" => "peak_allocated_bytes",
                "peak_reserved_bytes" => "peak_reserved_bytes",
                _ => unreachable!(),
            };
            return OomSchemaOutcome::PeakBytesNotNumber { key: static_key };
        }
    }
    match obj["largest_alloc_stack"].as_array() {
        Some(arr) => {
            if !arr.iter().all(Value::is_string) {
                return OomSchemaOutcome::StackNotStringArray;
            }
        }
        None => return OomSchemaOutcome::StackNotStringArray,
    }
    if !obj["tensor_histogram"].is_object() {
        return OomSchemaOutcome::HistogramNotObject;
    }
    if !obj["last_100_ops"].is_array() {
        return OomSchemaOutcome::OpsNotArray;
    }
    if !obj["exit_code"].is_number() {
        return OomSchemaOutcome::ExitCodeNotNumber;
    }
    if !obj["timestamp"].is_string() {
        return OomSchemaOutcome::TimestampNotString;
    }
    OomSchemaOutcome::Ok
}

/// Report invariants gate (contract `oom_report_schema.invariants`).
/// Assumes schema gate passed; callers MUST check `classify_oom_schema` first.
pub fn classify_oom_invariants(report: &Value) -> OomInvariantsOutcome {
    let allocated = report["peak_allocated_bytes"].as_u64().unwrap_or(0);
    let reserved = report["peak_reserved_bytes"].as_u64().unwrap_or(0);
    if reserved < allocated {
        return OomInvariantsOutcome::ReservedLessThanAllocated {
            reserved,
            allocated,
        };
    }
    if let Some(ops) = report["last_100_ops"].as_array() {
        if ops.len() > OOM_REPORT_MAX_OPS {
            return OomInvariantsOutcome::TooManyOps { got: ops.len() };
        }
    }
    if let Some(hist) = report["tensor_histogram"].as_object() {
        if hist.is_empty() {
            return OomInvariantsOutcome::EmptyHistogram;
        }
    }
    if let Some(stack) = report["largest_alloc_stack"].as_array() {
        if stack.is_empty() {
            return OomInvariantsOutcome::EmptyStack;
        }
    }
    if report["exit_code"].as_i64() == Some(0) {
        return OomInvariantsOutcome::SilentExitCode;
    }
    OomInvariantsOutcome::Ok
}

/// File size gate (contract `sizeof(report.json) < 10 MiB`).
#[must_use]
pub fn classify_oom_size(bytes: u64) -> OomSizeOutcome {
    if bytes < OOM_REPORT_MAX_SIZE_BYTES {
        OomSizeOutcome::Ok { bytes }
    } else {
        OomSizeOutcome::TooLarge {
            bytes,
            cap_bytes: OOM_REPORT_MAX_SIZE_BYTES,
        }
    }
}

/// Stderr breadcrumb gate. Parses `stderr` and returns the captured
/// `OOM_REPORT path=...` line if present.
///
/// Contract: stderr MUST contain exactly one line matching the pattern
/// `^OOM_REPORT path=/tmp/apr-oom-\d+\.json$`.
#[must_use]
pub fn classify_oom_breadcrumb(stderr: &str) -> OomBreadcrumbOutcome {
    let mut found: Option<&str> = None;
    for line in stderr.lines() {
        if let Some(rest) = line.strip_prefix("OOM_REPORT path=") {
            found = Some(rest);
        }
    }
    match found {
        None => OomBreadcrumbOutcome::NotFound,
        Some(path) => {
            if !is_valid_oom_path(path) {
                return OomBreadcrumbOutcome::MalformedPath {
                    got: path.to_string(),
                };
            }
            OomBreadcrumbOutcome::Ok {
                path: path.to_string(),
            }
        }
    }
}

fn is_valid_oom_path(path: &str) -> bool {
    let Some(rest) = path.strip_prefix("/tmp/apr-oom-") else {
        return false;
    };
    let Some(stem) = rest.strip_suffix(".json") else {
        return false;
    };
    !stem.is_empty() && stem.chars().all(|c| c.is_ascii_digit())
}

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

    fn well_formed_report() -> Value {
        json!({
            "peak_allocated_bytes": 2_000_000_u64,
            "peak_reserved_bytes": 3_000_000_u64,
            "largest_alloc_stack": ["apr::run", "realizar::load", "cuda::malloc"],
            "tensor_histogram": { "1-64KB": 12, "64KB-1MB": 4, "1MB+": 1 },
            "last_100_ops": [ { "op": "matmul", "bytes": 4096_u64, "ts_ns": 1_u64 } ],
            "exit_code": 137_i64,
            "timestamp": "2026-04-21T18:30:00Z",
        })
    }

    #[test]
    fn schema_accepts_well_formed_report() {
        assert_eq!(
            classify_oom_schema(&well_formed_report()),
            OomSchemaOutcome::Ok
        );
    }

    #[test]
    fn schema_rejects_non_object() {
        assert_eq!(
            classify_oom_schema(&json!(["not", "object"])),
            OomSchemaOutcome::NotAnObject
        );
    }

    #[test]
    fn schema_rejects_each_missing_key() {
        for &drop in OOM_REPORT_REQUIRED_KEYS {
            let mut r = well_formed_report();
            r.as_object_mut().unwrap().remove(drop);
            assert_eq!(
                classify_oom_schema(&r),
                OomSchemaOutcome::MissingRequiredKey { key: drop },
                "drop={drop}"
            );
        }
    }

    #[test]
    fn schema_rejects_non_number_peak_allocated() {
        let mut r = well_formed_report();
        r["peak_allocated_bytes"] = json!("not a number");
        assert_eq!(
            classify_oom_schema(&r),
            OomSchemaOutcome::PeakBytesNotNumber {
                key: "peak_allocated_bytes"
            }
        );
    }

    #[test]
    fn schema_rejects_non_number_peak_reserved() {
        let mut r = well_formed_report();
        r["peak_reserved_bytes"] = json!(null);
        assert_eq!(
            classify_oom_schema(&r),
            OomSchemaOutcome::PeakBytesNotNumber {
                key: "peak_reserved_bytes"
            }
        );
    }

    #[test]
    fn schema_rejects_stack_with_non_strings() {
        let mut r = well_formed_report();
        r["largest_alloc_stack"] = json!(["frame", 7_u64]);
        assert_eq!(
            classify_oom_schema(&r),
            OomSchemaOutcome::StackNotStringArray
        );
    }

    #[test]
    fn schema_rejects_stack_not_array() {
        let mut r = well_formed_report();
        r["largest_alloc_stack"] = json!("one frame");
        assert_eq!(
            classify_oom_schema(&r),
            OomSchemaOutcome::StackNotStringArray
        );
    }

    #[test]
    fn schema_rejects_histogram_not_object() {
        let mut r = well_formed_report();
        r["tensor_histogram"] = json!([1, 2, 3]);
        assert_eq!(
            classify_oom_schema(&r),
            OomSchemaOutcome::HistogramNotObject
        );
    }

    #[test]
    fn schema_rejects_ops_not_array() {
        let mut r = well_formed_report();
        r["last_100_ops"] = json!({ "op": "matmul" });
        assert_eq!(classify_oom_schema(&r), OomSchemaOutcome::OpsNotArray);
    }

    #[test]
    fn schema_rejects_exit_code_not_number() {
        let mut r = well_formed_report();
        r["exit_code"] = json!("137");
        assert_eq!(classify_oom_schema(&r), OomSchemaOutcome::ExitCodeNotNumber);
    }

    #[test]
    fn schema_rejects_timestamp_not_string() {
        let mut r = well_formed_report();
        r["timestamp"] = json!(1_745_000_000_i64);
        assert_eq!(
            classify_oom_schema(&r),
            OomSchemaOutcome::TimestampNotString
        );
    }

    #[test]
    fn invariants_accepts_well_formed_report() {
        assert_eq!(
            classify_oom_invariants(&well_formed_report()),
            OomInvariantsOutcome::Ok
        );
    }

    #[test]
    fn invariants_rejects_reserved_less_than_allocated() {
        let mut r = well_formed_report();
        r["peak_reserved_bytes"] = json!(1_000_000_u64);
        r["peak_allocated_bytes"] = json!(2_000_000_u64);
        assert_eq!(
            classify_oom_invariants(&r),
            OomInvariantsOutcome::ReservedLessThanAllocated {
                reserved: 1_000_000,
                allocated: 2_000_000,
            }
        );
    }

    #[test]
    fn invariants_accepts_reserved_equals_allocated() {
        let mut r = well_formed_report();
        r["peak_reserved_bytes"] = json!(2_000_000_u64);
        r["peak_allocated_bytes"] = json!(2_000_000_u64);
        assert_eq!(classify_oom_invariants(&r), OomInvariantsOutcome::Ok);
    }

    #[test]
    fn invariants_rejects_too_many_ops() {
        let mut r = well_formed_report();
        let ops: Vec<Value> = (0..101_u64)
            .map(|i| json!({ "op": "x", "bytes": 1_u64, "ts_ns": i }))
            .collect();
        r["last_100_ops"] = json!(ops);
        assert_eq!(
            classify_oom_invariants(&r),
            OomInvariantsOutcome::TooManyOps { got: 101 }
        );
    }

    #[test]
    fn invariants_accepts_exactly_100_ops() {
        let mut r = well_formed_report();
        let ops: Vec<Value> = (0..100_u64)
            .map(|i| json!({ "op": "x", "bytes": 1_u64, "ts_ns": i }))
            .collect();
        r["last_100_ops"] = json!(ops);
        assert_eq!(classify_oom_invariants(&r), OomInvariantsOutcome::Ok);
    }

    #[test]
    fn invariants_rejects_empty_histogram() {
        let mut r = well_formed_report();
        r["tensor_histogram"] = json!({});
        assert_eq!(
            classify_oom_invariants(&r),
            OomInvariantsOutcome::EmptyHistogram
        );
    }

    #[test]
    fn invariants_rejects_empty_stack() {
        let mut r = well_formed_report();
        r["largest_alloc_stack"] = json!([]);
        assert_eq!(
            classify_oom_invariants(&r),
            OomInvariantsOutcome::EmptyStack
        );
    }

    #[test]
    fn invariants_rejects_silent_exit_code_zero() {
        let mut r = well_formed_report();
        r["exit_code"] = json!(0_i64);
        assert_eq!(
            classify_oom_invariants(&r),
            OomInvariantsOutcome::SilentExitCode
        );
    }

    #[test]
    fn invariants_accepts_non_zero_exit_code() {
        for ec in [1_i64, 137, 139, -1] {
            let mut r = well_formed_report();
            r["exit_code"] = json!(ec);
            assert_eq!(
                classify_oom_invariants(&r),
                OomInvariantsOutcome::Ok,
                "exit_code={ec}"
            );
        }
    }

    #[test]
    fn size_accepts_under_cap() {
        assert_eq!(classify_oom_size(100), OomSizeOutcome::Ok { bytes: 100 });
        assert_eq!(
            classify_oom_size(OOM_REPORT_MAX_SIZE_BYTES - 1),
            OomSizeOutcome::Ok {
                bytes: OOM_REPORT_MAX_SIZE_BYTES - 1
            }
        );
    }

    #[test]
    fn size_rejects_at_and_over_cap() {
        assert_eq!(
            classify_oom_size(OOM_REPORT_MAX_SIZE_BYTES),
            OomSizeOutcome::TooLarge {
                bytes: OOM_REPORT_MAX_SIZE_BYTES,
                cap_bytes: OOM_REPORT_MAX_SIZE_BYTES,
            }
        );
        assert_eq!(
            classify_oom_size(OOM_REPORT_MAX_SIZE_BYTES + 1),
            OomSizeOutcome::TooLarge {
                bytes: OOM_REPORT_MAX_SIZE_BYTES + 1,
                cap_bytes: OOM_REPORT_MAX_SIZE_BYTES,
            }
        );
    }

    #[test]
    fn breadcrumb_accepts_well_formed() {
        let stderr = "Some noise\nOOM_REPORT path=/tmp/apr-oom-1745000000.json\nmore noise\n";
        assert_eq!(
            classify_oom_breadcrumb(stderr),
            OomBreadcrumbOutcome::Ok {
                path: "/tmp/apr-oom-1745000000.json".to_string()
            }
        );
    }

    #[test]
    fn breadcrumb_rejects_when_missing() {
        assert_eq!(
            classify_oom_breadcrumb("no breadcrumb here"),
            OomBreadcrumbOutcome::NotFound
        );
    }

    #[test]
    fn breadcrumb_rejects_wrong_path_prefix() {
        let stderr = "OOM_REPORT path=/var/log/apr-oom-1.json\n";
        assert_eq!(
            classify_oom_breadcrumb(stderr),
            OomBreadcrumbOutcome::MalformedPath {
                got: "/var/log/apr-oom-1.json".to_string()
            }
        );
    }

    #[test]
    fn breadcrumb_rejects_non_digit_ts() {
        let stderr = "OOM_REPORT path=/tmp/apr-oom-abc.json\n";
        assert_eq!(
            classify_oom_breadcrumb(stderr),
            OomBreadcrumbOutcome::MalformedPath {
                got: "/tmp/apr-oom-abc.json".to_string()
            }
        );
    }

    #[test]
    fn breadcrumb_rejects_empty_ts() {
        let stderr = "OOM_REPORT path=/tmp/apr-oom-.json\n";
        assert_eq!(
            classify_oom_breadcrumb(stderr),
            OomBreadcrumbOutcome::MalformedPath {
                got: "/tmp/apr-oom-.json".to_string()
            }
        );
    }

    #[test]
    fn breadcrumb_rejects_wrong_suffix() {
        let stderr = "OOM_REPORT path=/tmp/apr-oom-1.txt\n";
        assert_eq!(
            classify_oom_breadcrumb(stderr),
            OomBreadcrumbOutcome::MalformedPath {
                got: "/tmp/apr-oom-1.txt".to_string()
            }
        );
    }
}