alpha2-gpu 0.2.2

GPU context and capability boundary for alpha2.
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
use std::collections::VecDeque;
use std::fmt;
use std::sync::{Arc, Mutex, PoisonError};

/// What class of error the driver reported.
///
/// WebGPU's three error filters. Only [`Self::OutOfMemory`] is evidence that
/// an allocation failed; the others are never read as memory pressure.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum GpuErrorKind {
    /// An allocation the driver was asked for could not be made.
    OutOfMemory,
    /// Invalid use of the API: a bug or bad data, not a resource shortage.
    Validation,
    /// Any other failure the implementation reports, including limits it hit.
    Internal,
}

impl GpuErrorKind {
    /// The kind `error` belongs to.
    #[must_use]
    #[cfg_attr(not(feature = "wgpu-interop"), doc(hidden))]
    pub const fn of(error: &wgpu::Error) -> Self {
        match error {
            wgpu::Error::OutOfMemory { .. } => Self::OutOfMemory,
            wgpu::Error::Validation { .. } => Self::Validation,
            wgpu::Error::Internal { .. } => Self::Internal,
        }
    }

    /// Stable lowercase name for logs and protocols.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::OutOfMemory => "out-of-memory",
            Self::Validation => "validation",
            Self::Internal => "internal",
        }
    }
}

/// A device-level GPU event that no call return value reports.
///
/// These arrive asynchronously from the driver, so an embedding application
/// observes them through [`GpuContext::set_diagnostic_handler`] rather than
/// from the call that triggered them.
///
/// [`GpuContext::set_diagnostic_handler`]: crate::GpuContext::set_diagnostic_handler
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum GpuDiagnostic {
    /// The driver reported an error that no pending call returned.
    UncapturedError {
        /// Which of WebGPU's error classes it was.
        kind: GpuErrorKind,
        /// Driver-supplied description.
        message: String,
    },
    /// The device was lost and can no longer serve work.
    DeviceLost {
        /// Loss reason reported by `wgpu`, or the browser event that lost it.
        reason: String,
        /// Driver-supplied description.
        message: String,
    },
}

impl GpuDiagnostic {
    /// The diagnostic for an uncaptured `wgpu` error, keeping its class.
    ///
    /// `wgpu` spells a validation or internal error's sources out in its
    /// description, but writes an out-of-memory error as the bare words "Out
    /// of Memory"; its sources name the call, the resource label, and the
    /// driver's cause, so they are appended, each after `": "`. A browser's
    /// report has only `wgpu`'s `<WebGPU Error>` placeholder there, which
    /// names nothing and is left out.
    #[must_use]
    #[cfg_attr(not(feature = "wgpu-interop"), doc(hidden))]
    pub fn uncaptured(error: &wgpu::Error) -> Self {
        let mut message = error.to_string();
        if matches!(error, wgpu::Error::OutOfMemory { .. }) {
            let mut source = std::error::Error::source(error);
            while let Some(cause) = source {
                let text = cause.to_string();
                if !text.is_empty() && text != "<WebGPU Error>" {
                    message.push_str(": ");
                    message.push_str(&text);
                }
                source = cause.source();
            }
        }
        Self::UncapturedError {
            kind: GpuErrorKind::of(error),
            message,
        }
    }

    /// The export failure class this event is evidence of:
    /// `OUT_OF_MEMORY`, `VALIDATION_FAILED`, `DEVICE_LOST`, or `UNKNOWN` for
    /// an internal error, which names no cause.
    #[must_use]
    pub const fn failure_code(&self) -> &'static str {
        match self {
            Self::UncapturedError {
                kind: GpuErrorKind::OutOfMemory,
                ..
            } => "OUT_OF_MEMORY",
            Self::UncapturedError {
                kind: GpuErrorKind::Validation,
                ..
            } => "VALIDATION_FAILED",
            Self::UncapturedError {
                kind: GpuErrorKind::Internal,
                ..
            } => "UNKNOWN",
            Self::DeviceLost { .. } => "DEVICE_LOST",
        }
    }

    /// Stable lowercase event name: `uncaptured-error` or `device-lost`.
    #[must_use]
    pub const fn event(&self) -> &'static str {
        match self {
            Self::UncapturedError { .. } => "uncaptured-error",
            Self::DeviceLost { .. } => "device-lost",
        }
    }

    fn truncated(self) -> Self {
        match self {
            Self::UncapturedError { kind, message } => Self::UncapturedError {
                kind,
                message: truncate(message),
            },
            Self::DeviceLost { reason, message } => Self::DeviceLost {
                reason: truncate(reason),
                message: truncate(message),
            },
        }
    }
}

impl fmt::Display for GpuDiagnostic {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UncapturedError { kind, message } => {
                write!(
                    formatter,
                    "uncaptured GPU {} error: {message}",
                    kind.as_str()
                )
            }
            Self::DeviceLost { reason, message } => {
                write!(formatter, "GPU device lost ({reason}): {message}")
            }
        }
    }
}

/// Longest message a [`GpuDiagnosticLog`] keeps, in bytes. A validation
/// error can quote a whole shader; the log only needs enough to identify it.
pub const MAX_DIAGNOSTIC_MESSAGE_BYTES: usize = 1_024;

/// What a cut message ends with.
const ELLIPSIS: &str = "...";

/// `message` cut to [`MAX_DIAGNOSTIC_MESSAGE_BYTES`] on a char boundary,
/// with an ellipsis where it was cut, in a buffer of its own length: the log
/// keeps what it returns, so it must not keep the driver's whole buffer too.
fn truncate(mut message: String) -> String {
    if message.len() <= MAX_DIAGNOSTIC_MESSAGE_BYTES {
        message.shrink_to_fit();
        return message;
    }
    let mut end = MAX_DIAGNOSTIC_MESSAGE_BYTES;
    while !message.is_char_boundary(end) {
        end -= 1;
    }
    let mut kept = String::with_capacity(end + ELLIPSIS.len());
    kept.push_str(&message[..end]);
    kept.push_str(ELLIPSIS);
    kept
}

/// The most recent [`GpuDiagnostic`]s, in arrival order, in fixed memory:
/// at most `capacity` events, each text at most
/// [`MAX_DIAGNOSTIC_MESSAGE_BYTES`] and an ellipsis.
///
/// A failure report reads it to connect an error a call returned (a readback
/// that failed to map carries no cause: `wgpu::BufferAsyncError` is a unit)
/// with what the device reported around it. Its size never grows
/// with the events, because it is consulted exactly when memory may be short.
#[derive(Debug)]
pub struct GpuDiagnosticLog {
    state: Mutex<LogState>,
    capacity: usize,
}

#[derive(Debug, Default)]
struct LogState {
    entries: VecDeque<GpuDiagnostic>,
    dropped: u64,
    device_lost: bool,
    out_of_memory: bool,
}

/// What a [`GpuDiagnosticLog`] held when it was read.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct GpuDiagnosticSnapshot {
    /// The kept events, oldest first.
    pub entries: Vec<GpuDiagnostic>,
    /// Events that arrived and were pushed out by later ones.
    pub dropped: u64,
    /// Whether any event, kept or dropped, was a device loss.
    pub device_lost: bool,
    /// Whether any event since the log was last cleared, kept or dropped,
    /// was an out-of-memory report.
    ///
    /// An allocation that ran out leaves an invalid resource behind, and
    /// every later use of it is reported as a validation error, so the
    /// report that names the cause is the first to be pushed out.
    pub out_of_memory: bool,
}

impl GpuDiagnosticSnapshot {
    /// The failure class the strongest evidence here names, if any: a device
    /// loss first, since everything after it fails too; then an out-of-memory
    /// report, kept or dropped; then a validation error. Internal errors
    /// name nothing.
    #[must_use]
    pub fn failure_code(&self) -> Option<&'static str> {
        if self.device_lost {
            return Some("DEVICE_LOST");
        }
        if self.out_of_memory {
            return Some("OUT_OF_MEMORY");
        }
        self.entries
            .iter()
            .any(|entry| entry.failure_code() == "VALIDATION_FAILED")
            .then_some("VALIDATION_FAILED")
    }
}

impl GpuDiagnosticLog {
    /// Default number of events a log keeps.
    pub const DEFAULT_CAPACITY: usize = 16;

    /// An empty log keeping at most `capacity` events (at least one).
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        Self {
            state: Mutex::new(LogState::default()),
            capacity: capacity.max(1),
        }
    }

    /// Records `diagnostic`, pushing out the oldest event when full.
    pub fn push(&self, diagnostic: GpuDiagnostic) {
        let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
        state.device_lost |= matches!(diagnostic, GpuDiagnostic::DeviceLost { .. });
        state.out_of_memory |= diagnostic.failure_code() == "OUT_OF_MEMORY";
        if state.entries.len() == self.capacity {
            state.entries.pop_front();
            state.dropped += 1;
        }
        state.entries.push_back(diagnostic.truncated());
    }

    /// A copy of what the log holds now.
    #[must_use]
    pub fn snapshot(&self) -> GpuDiagnosticSnapshot {
        let state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
        GpuDiagnosticSnapshot {
            entries: state.entries.iter().cloned().collect(),
            dropped: state.dropped,
            device_lost: state.device_lost,
            out_of_memory: state.out_of_memory,
        }
    }

    /// Forgets the events, so the next snapshot covers only what follows,
    /// out-of-memory reports included. A device loss is kept: a lost device
    /// stays lost.
    pub fn clear(&self) {
        let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
        state.entries.clear();
        state.dropped = 0;
        state.out_of_memory = false;
    }

    /// Whether a device loss was ever recorded.
    #[must_use]
    pub fn device_lost(&self) -> bool {
        self.state
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .device_lost
    }
}

impl Default for GpuDiagnosticLog {
    fn default() -> Self {
        Self::new(Self::DEFAULT_CAPACITY)
    }
}

/// Shared handler invoked for every [`GpuDiagnostic`].
///
/// The driver may call it from an arbitrary thread, so it must not block.
pub type GpuDiagnosticHandler = Arc<dyn Fn(GpuDiagnostic) + Send + Sync + 'static>;

/// Interior-mutable slot the device callbacks forward through.
///
/// Devices take their callbacks once at creation, so the slot is what lets a
/// caller install or replace a handler on an already-created context.
pub(crate) struct DiagnosticHook {
    handler: Mutex<Option<GpuDiagnosticHandler>>,
    strict: bool,
}

/// Whether an uncaptured error with no handler installed panics, as wgpu's own default does,
/// instead of being dropped: in debug builds, so every test that creates a context fails on the
/// call that made the error, and wherever `ALPHA2_GPU_STRICT=1`.
fn strict_by_default() -> bool {
    cfg!(debug_assertions)
        || std::env::var_os("ALPHA2_GPU_STRICT").is_some_and(|value| value == "1")
}

impl DiagnosticHook {
    pub(crate) fn new() -> Self {
        Self::with_strict(strict_by_default())
    }

    pub(crate) fn with_strict(strict: bool) -> Self {
        Self {
            handler: Mutex::new(None),
            strict,
        }
    }

    pub(crate) fn set(&self, handler: Option<GpuDiagnosticHandler>) {
        *self.handler.lock().unwrap_or_else(PoisonError::into_inner) = handler;
    }

    /// Emits without holding the lock across the call, so a handler that
    /// re-enters the context cannot deadlock.
    ///
    /// With no handler installed a strict hook panics on an uncaptured error,
    /// on the thread whose call made it; device loss is still dropped, since
    /// dropping a device reports it too.
    ///
    /// Only the native device callbacks emit; the browser owns its own
    /// callbacks, as [`GpuContext::set_diagnostic_handler`] documents.
    ///
    /// [`GpuContext::set_diagnostic_handler`]: crate::GpuContext::set_diagnostic_handler
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn emit(&self, diagnostic: GpuDiagnostic) {
        let handler = self
            .handler
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone();
        if let Some(handler) = handler {
            handler(diagnostic);
        } else if self.strict && matches!(diagnostic, GpuDiagnostic::UncapturedError { .. }) {
            panic!(
                "{diagnostic}\n(strict GPU diagnostics: no handler is installed; capture the error with an error scope or install one with GpuContext::set_diagnostic_handler)"
            );
        }
    }
}

impl fmt::Debug for DiagnosticHook {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let installed = self
            .handler
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .is_some();
        formatter
            .debug_struct("DiagnosticHook")
            .field("installed", &installed)
            .field("strict", &self.strict)
            .finish()
    }
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[test]
    fn an_uninstalled_lenient_hook_drops_diagnostics() {
        let hook = DiagnosticHook::with_strict(false);
        hook.emit(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Internal,
            message: "ignored".to_owned(),
        });
    }

    #[test]
    #[should_panic(expected = "uncaptured GPU validation error: unseen")]
    fn an_uninstalled_strict_hook_panics_on_an_uncaptured_error() {
        DiagnosticHook::with_strict(true).emit(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Validation,
            message: "unseen".to_owned(),
        });
    }

    #[test]
    fn a_strict_hook_drops_device_loss_and_defers_to_a_handler() {
        let hook = DiagnosticHook::with_strict(true);
        hook.emit(GpuDiagnostic::DeviceLost {
            reason: "Destroyed".to_owned(),
            message: "device dropped".to_owned(),
        });
        let count = Arc::new(AtomicUsize::new(0));
        let counted = Arc::clone(&count);
        hook.set(Some(Arc::new(move |_| {
            counted.fetch_add(1, Ordering::Release);
        })));
        hook.emit(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Validation,
            message: "handled".to_owned(),
        });
        assert_eq!(count.load(Ordering::Acquire), 1);
    }

    #[test]
    fn the_installed_handler_receives_every_diagnostic() {
        let hook = DiagnosticHook::new();
        let seen = Arc::new(Mutex::new(Vec::new()));
        let recorded = Arc::clone(&seen);
        hook.set(Some(Arc::new(move |diagnostic: GpuDiagnostic| {
            recorded
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .push(diagnostic.to_string());
        })));
        hook.emit(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Validation,
            message: "validation".to_owned(),
        });
        hook.emit(GpuDiagnostic::DeviceLost {
            reason: "Destroyed".to_owned(),
            message: "device dropped".to_owned(),
        });
        assert_eq!(
            *seen.lock().unwrap_or_else(PoisonError::into_inner),
            vec![
                "uncaptured GPU validation error: validation".to_owned(),
                "GPU device lost (Destroyed): device dropped".to_owned(),
            ]
        );
    }

    #[test]
    fn clearing_the_handler_stops_delivery() {
        let hook = DiagnosticHook::with_strict(false);
        let count = Arc::new(AtomicUsize::new(0));
        let counted = Arc::clone(&count);
        hook.set(Some(Arc::new(move |_| {
            counted.fetch_add(1, Ordering::Release);
        })));
        hook.emit(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Internal,
            message: "first".to_owned(),
        });
        hook.set(None);
        hook.emit(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Internal,
            message: "second".to_owned(),
        });
        assert_eq!(count.load(Ordering::Acquire), 1);
    }

    #[test]
    fn every_wgpu_error_keeps_its_class() {
        // The class is what a failure report and the retry policy read; text says nothing.
        let source = || -> wgpu::ErrorSource { Box::new(std::io::Error::other("validation")) };
        for (error, kind, code) in [
            (
                wgpu::Error::OutOfMemory { source: source() },
                GpuErrorKind::OutOfMemory,
                "OUT_OF_MEMORY",
            ),
            (
                wgpu::Error::Validation {
                    source: source(),
                    description: "out of memory, says the text".to_owned(),
                },
                GpuErrorKind::Validation,
                "VALIDATION_FAILED",
            ),
            (
                wgpu::Error::Internal {
                    source: source(),
                    description: "out of memory".to_owned(),
                },
                GpuErrorKind::Internal,
                "UNKNOWN",
            ),
        ] {
            assert_eq!(GpuErrorKind::of(&error), kind);
            let diagnostic = GpuDiagnostic::uncaptured(&error);
            assert_eq!(diagnostic.failure_code(), code, "{diagnostic}");
            assert_eq!(diagnostic.event(), "uncaptured-error");
        }
    }

    #[test]
    fn an_out_of_memory_error_names_the_call_and_resource_that_ran_out() {
        // wgpu writes only "Out of Memory" for this class; the call, label and cause are in its
        // sources, shaped as wgpu-core's ContextError.
        #[derive(Debug)]
        struct Context(std::io::Error);
        impl fmt::Display for Context {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("In Device::create_texture, label = 'alpha2 intermediate'")
            }
        }
        impl std::error::Error for Context {
            fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                Some(&self.0)
            }
        }
        let error = wgpu::Error::OutOfMemory {
            source: Box::new(Context(std::io::Error::other("Not enough memory left."))),
        };
        assert_eq!(
            GpuDiagnostic::uncaptured(&error),
            GpuDiagnostic::UncapturedError {
                kind: GpuErrorKind::OutOfMemory,
                message: "Out of Memory: In Device::create_texture, label = 'alpha2 intermediate': Not enough memory left.".to_owned(),
            }
        );
        // A browser's report carries only wgpu's placeholder, which names nothing.
        let browser = wgpu::Error::OutOfMemory {
            source: Box::<dyn std::error::Error + Send + Sync>::from("<WebGPU Error>"),
        };
        assert_eq!(
            GpuDiagnostic::uncaptured(&browser).to_string(),
            "uncaptured GPU out-of-memory error: Out of Memory"
        );
        // A validation description already spells its sources out.
        let validation = wgpu::Error::Validation {
            source: Box::new(std::io::Error::other("inner")),
            description: "Validation Error\n\nCaused by:\n  inner".to_owned(),
        };
        let GpuDiagnostic::UncapturedError { message, .. } = GpuDiagnostic::uncaptured(&validation)
        else {
            panic!("an uncaptured error");
        };
        assert_eq!(message, "Validation Error\n\nCaused by:\n  inner");
    }

    fn oom(message: &str) -> GpuDiagnostic {
        GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::OutOfMemory,
            message: message.to_owned(),
        }
    }

    #[test]
    fn the_log_keeps_the_newest_events_in_fixed_space() {
        let log = GpuDiagnosticLog::new(2);
        log.push(oom("a"));
        log.push(oom("b"));
        log.push(oom("c"));
        let snapshot = log.snapshot();
        assert_eq!(snapshot.entries, vec![oom("b"), oom("c")]);
        assert_eq!(snapshot.dropped, 1);
    }

    fn kept_message(log: &GpuDiagnosticLog) -> String {
        let GpuDiagnostic::UncapturedError { message, .. } = &log.snapshot().entries[0] else {
            panic!("an uncaptured error");
        };
        message.clone()
    }

    #[test]
    fn the_log_truncates_long_messages_on_a_char_boundary() {
        // The cut falls inside a 3-byte character (naga's box drawing), so the walk back runs.
        for (input, kept) in [
            (
                format!("a{}", "é".repeat(MAX_DIAGNOSTIC_MESSAGE_BYTES)),
                format!("a{}", "é".repeat(511)),
            ),
            ("─".repeat(MAX_DIAGNOSTIC_MESSAGE_BYTES), "─".repeat(341)),
        ] {
            assert!(!input.is_char_boundary(MAX_DIAGNOSTIC_MESSAGE_BYTES));
            let log = GpuDiagnosticLog::new(1);
            log.push(oom(&input));
            // The longest prefix ending on a boundary at or before the limit, then the ellipsis.
            assert_eq!(kept_message(&log), format!("{kept}{ELLIPSIS}"));
        }
        let log = GpuDiagnosticLog::new(1);
        let exact = "x".repeat(MAX_DIAGNOSTIC_MESSAGE_BYTES);
        log.push(oom(&exact));
        assert_eq!(kept_message(&log), exact, "a message at the limit is whole");
    }

    #[test]
    fn the_log_keeps_no_more_memory_than_the_messages_it_keeps() {
        // A cut message must not keep the buffer it was cut from; read through the kept log, not a
        // snapshot's compact clone.
        let log = GpuDiagnosticLog::new(2);
        let mut quoted = String::with_capacity(64 * 1024);
        quoted.push_str(&"v".repeat(64 * 1024));
        log.push(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Validation,
            message: quoted,
        });
        let mut short = String::with_capacity(16 * 1024);
        short.push_str("short");
        log.push(GpuDiagnostic::DeviceLost {
            reason: short.clone(),
            message: short,
        });
        let state = log.state.lock().unwrap_or_else(PoisonError::into_inner);
        let held: Vec<usize> = state
            .entries
            .iter()
            .flat_map(|entry| match entry {
                GpuDiagnostic::UncapturedError { message, .. } => vec![message.capacity()],
                GpuDiagnostic::DeviceLost { reason, message } => {
                    vec![reason.capacity(), message.capacity()]
                }
            })
            .collect();
        assert_eq!(
            held,
            [MAX_DIAGNOSTIC_MESSAGE_BYTES + ELLIPSIS.len(), 5, 5],
            "each kept text owns its length and no more"
        );
    }

    #[test]
    fn only_typed_evidence_names_a_failure_class() {
        let log = GpuDiagnosticLog::default();
        log.push(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Internal,
            message: "out of memory, says the text".to_owned(),
        });
        assert_eq!(log.snapshot().failure_code(), None);
        log.push(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Validation,
            message: "bad layout".to_owned(),
        });
        assert_eq!(log.snapshot().failure_code(), Some("VALIDATION_FAILED"));
        log.push(oom("buffer"));
        assert_eq!(log.snapshot().failure_code(), Some("OUT_OF_MEMORY"));
        log.push(GpuDiagnostic::DeviceLost {
            reason: "Unknown".to_owned(),
            message: "driver reset".to_owned(),
        });
        assert_eq!(log.snapshot().failure_code(), Some("DEVICE_LOST"));
    }

    #[test]
    fn an_out_of_memory_report_survives_eviction_until_cleared() {
        // Later uses of the invalid resource push the report out with validation errors, yet the
        // out-of-memory report is still the cause.
        let log = GpuDiagnosticLog::default();
        log.push(oom("Device::create_texture"));
        for _ in 0..GpuDiagnosticLog::DEFAULT_CAPACITY {
            log.push(GpuDiagnostic::UncapturedError {
                kind: GpuErrorKind::Validation,
                message: "texture is invalid".to_owned(),
            });
        }
        let snapshot = log.snapshot();
        assert_eq!(snapshot.dropped, 1);
        assert!(!snapshot.entries.contains(&oom("Device::create_texture")));
        assert!(snapshot.out_of_memory);
        assert_eq!(snapshot.failure_code(), Some("OUT_OF_MEMORY"));
        // What follows a clear is the next span's evidence; memory may be back.
        log.clear();
        log.push(GpuDiagnostic::UncapturedError {
            kind: GpuErrorKind::Validation,
            message: "bad layout".to_owned(),
        });
        let snapshot = log.snapshot();
        assert!(!snapshot.out_of_memory);
        assert_eq!(snapshot.failure_code(), Some("VALIDATION_FAILED"));
    }

    #[test]
    fn a_device_loss_survives_clearing_and_eviction() {
        let log = GpuDiagnosticLog::new(1);
        log.push(GpuDiagnostic::DeviceLost {
            reason: "Unknown".to_owned(),
            message: String::new(),
        });
        log.push(oom("later"));
        log.clear();
        let snapshot = log.snapshot();
        assert_eq!(snapshot.entries, [] as [GpuDiagnostic; 0]);
        assert!(snapshot.device_lost);
        assert_eq!(snapshot.failure_code(), Some("DEVICE_LOST"));
    }
}