vyre-driver-cuda 0.6.2

CUDA/PTX backend for vyre through the CUDA driver API.
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
use super::helpers::{
    borrow_resident_sequence_output_slots, prepare_resident_sequence_fills,
    stage_resident_fill_payload, validate_dense_resident_input_indices,
    validate_dense_resident_output_indices,
};

fn resident_dispatch_production_source() -> String {
    [
        include_str!("helpers.rs"),
        include_str!("borrowed.rs"),
        include_str!("async_dispatch.rs"),
        include_str!("batch.rs"),
        include_str!("sync.rs"),
        include_str!("sequence_api.rs"),
        include_str!("sequence_fused.rs"),
        include_str!("timed.rs"),
    ]
    .iter()
    .map(|s| s.split("#[cfg(test)]").next().unwrap_or(""))
    .collect::<Vec<_>>()
    .join("\n")
}

#[cfg(test)]
mod tests {
    use super::super::async_dispatch::resident_output_clear_for_readback;
    use super::super::borrowed::order_resident_fallback_inputs_by_logical_index;
    use super::{
        borrow_resident_sequence_output_slots, prepare_resident_sequence_fills,
        stage_resident_fill_payload, validate_dense_resident_input_indices,
        validate_dense_resident_output_indices,
    };
    use crate::backend::output_range::CudaOutputReadback;
    use crate::backend::resident::CudaResidentBuffer;

    #[test]
    fn resident_fallback_fill_payload_preserves_last_good_bytes_when_reservation_fails() {
        let mut payload = vec![0xC3, 0xC3, 0x7E, 0x11];

        let result = stage_resident_fill_payload(&mut payload, 0x5A, usize::MAX);

        assert!(
            result.is_err(),
            "oversized CUDA resident fill payload must fail preflight instead of mutating staging"
        );
        assert_eq!(
            payload,
            vec![0xC3, 0xC3, 0x7E, 0x11],
            "failed CUDA resident fill staging must preserve the last diagnostic payload"
        );
    }
    #[test]
    fn resident_fallback_fill_payload_reuses_capacity_and_overwrites_values() {
        let mut payload = Vec::new();
        {
            let bytes = stage_resident_fill_payload(&mut payload, 0xA5, 16)
                .expect("Fix: reusable resident fallback fill staging should reserve bytes");
            assert_eq!(bytes, &[0xA5; 16]);
        }
        let initial_capacity = payload.capacity();

        {
            let bytes = stage_resident_fill_payload(&mut payload, 0x5A, 8)
                .expect("Fix: smaller resident fallback fill staging should reuse capacity");
            assert_eq!(bytes, &[0x5A; 8]);
        }
        assert_eq!(
            payload.capacity(),
            initial_capacity,
            "CUDA resident fallback fill staging must reuse capacity across fills instead of allocating one Vec per fill"
        );

        {
            let bytes = stage_resident_fill_payload(&mut payload, 0x11, 0)
                .expect("Fix: zero-byte resident fallback fill staging should be valid");
            assert!(bytes.is_empty());
        }
        assert_eq!(
            payload.capacity(),
            initial_capacity,
            "zero-byte fallback fills must not release reusable staging capacity"
        );
    }

    #[test]
    fn resident_borrowed_fallback_does_not_allocate_vec_per_fill() {
        let source = super::resident_dispatch_production_source();
        assert!(
            source.contains("stage_resident_fill_payload(&mut fill_payload")
                && source.contains("let mut fill_payload = Vec::new();")
                && !source.contains(concat!("vec![value; ", "handle.byte_len]")),
            "Fix: CUDA resident borrowed fallback must stage fills through one reusable Vec, not allocate a fresh Vec per resident clear/fill."
        );
    }

    #[test]
    fn resident_h2d_enqueues_are_single_sourced_without_stealing_stream_order() {
        let source = super::resident_dispatch_production_source();
        let production = source
            .split("#[cfg(test)]")
            .next()
            .expect("Fix: resident_dispatch production source must precede tests.");
        assert!(
            production.contains("fn enqueue_resident_h2d_copy")
                && production.contains("fn enqueue_optional_resident_h2d_copy")
                && production.contains("fn enqueue_resident_upload_copies_on_stream")
                && production
                    .matches(concat!("crate::backend::copy::", "h2d_async_checked"))
                    .count()
                    == 1,
            "Fix: resident dispatch parameter uploads, sequence uploads, and per-step parameter uploads must share one local H2D enqueue helper while preserving the caller-owned CUDA stream."
        );
        assert!(
            production.contains("enqueue_resident_upload_copies_on_stream(\n                &upload_copies")
                && production.contains("enqueue_resident_h2d_copy(\n                        params_ptr")
                && production.contains("param_host_ptr,\n                        param_bytes")
                && production.contains("stream.raw(),"),
            "Fix: resident sequence uploads and per-step parameter uploads must use the shared stream-preserving enqueue helpers."
        );
    }

    #[test]
    fn resident_output_clear_uses_observable_readback_range() {
        let clear = resident_output_clear_for_readback(
            0x1000,
            CudaOutputReadback {
                device_offset: 128,
                byte_len: 4096,
            },
            "out",
        )
        .expect("Fix: ranged resident output clear planning must accept valid offsets.");

        assert_eq!(
            clear,
            Some((0x1080, 4096)),
            "Fix: resident dispatch must clear the declared output byte range, not the padded allocation."
        );

        let full = resident_output_clear_for_readback(
            0x2000,
            CudaOutputReadback {
                device_offset: 0,
                byte_len: 8192,
            },
            "full",
        )
        .expect("Fix: full resident output clear planning must preserve full-buffer clears.");
        assert_eq!(full, Some((0x2000, 8192)));
    }

    #[test]
    fn resident_output_clear_skips_zero_byte_ranges_and_rejects_pointer_overflow() {
        let skipped = resident_output_clear_for_readback(
            0x1000,
            CudaOutputReadback {
                device_offset: 256,
                byte_len: 0,
            },
            "empty",
        )
        .expect("Fix: zero-byte resident output ranges should not enqueue memsets.");
        assert_eq!(skipped, None);

        let error = resident_output_clear_for_readback(
            u64::MAX,
            CudaOutputReadback {
                device_offset: 1,
                byte_len: 4,
            },
            "overflow",
        )
        .expect_err("Fix: resident output clear planning must reject device-pointer overflow.");

        assert!(
            error.to_string().contains("overflowed"),
            "overflow error must explain the CUDA resident clear pointer failure: {error}"
        );
    }

    #[test]
    fn resident_output_index_validation_rejects_sparse_or_duplicate_sorted_indexes() {
        validate_dense_resident_output_indices([0, 1, 2], 3, "test output")
            .expect("Fix: dense resident output indexes must validate.");
        assert!(
            validate_dense_resident_output_indices([0, 0, 2], 3, "test output").is_err(),
            "Fix: duplicate resident output indexes must fail before readback ordering can alias an output slot."
        );
        assert!(
            validate_dense_resident_output_indices([0, 2, 3], 3, "test output").is_err(),
            "Fix: sparse resident output indexes must fail before readback ordering can skip an output slot."
        );
        assert!(
            validate_dense_resident_output_indices([0, 1], 3, "test output").is_err(),
            "Fix: truncated resident output indexes must fail before readback ordering can drop an output slot."
        );
    }

    #[test]
    fn resident_input_index_validation_rejects_sparse_duplicate_or_truncated_indexes() {
        validate_dense_resident_input_indices([0, 1, 2], 3, "test input")
            .expect("Fix: dense resident input indexes must validate.");
        assert!(
            validate_dense_resident_input_indices([0, 0, 2], 3, "test input").is_err(),
            "Fix: duplicate resident input indexes must fail before borrowed fallback can alias a logical input slot."
        );
        assert!(
            validate_dense_resident_input_indices([0, 2, 3], 3, "test input").is_err(),
            "Fix: sparse resident input indexes must fail before borrowed fallback can skip a logical input slot."
        );
        assert!(
            validate_dense_resident_input_indices([0, 1], 3, "test input").is_err(),
            "Fix: truncated resident input indexes must fail before borrowed fallback can drop a logical input slot."
        );
    }

    #[test]
    fn resident_borrowed_fallback_orders_downloaded_inputs_by_logical_slot() {
        let mut inputs = vec![(2, vec![0xCC]), (0, vec![0xAA]), (1, vec![0xBB])];

        order_resident_fallback_inputs_by_logical_index(&mut inputs, 3)
            .expect("Fix: reordered resident fallback inputs should sort by logical input slot.");

        assert_eq!(
            inputs,
            vec![
                (0, vec![0xAA]),
                (1, vec![0xBB]),
                (2, vec![0xCC]),
            ],
            "Fix: CUDA resident borrowed fallback must pass dispatch_borrowed inputs in Program::buffers logical order, not descriptor binding order."
        );

        let mut duplicate = vec![(0, vec![1]), (0, vec![2])];
        assert!(
            order_resident_fallback_inputs_by_logical_index(&mut duplicate, 2).is_err(),
            "Fix: resident fallback input ordering must reject duplicate logical input slots before launch."
        );
    }

    #[test]
    fn resident_sequence_fills_coalesce_duplicates_and_skip_full_upload_overwrites() {
        let first = CudaResidentBuffer {
            id: 1,
            byte_len: 16,
        };
        let second = CudaResidentBuffer {
            id: 2,
            byte_len: 16,
        };
        let upload = [0xFE_u8; 16];

        let effective = prepare_resident_sequence_fills(
            &[(first, 0x11), (second, 0x22), (first, 0x33)],
            &[(second, upload.as_slice())],
        )
        .expect("Fix: generated resident sequence fill coalescing must succeed.");

        assert_eq!(
            effective.as_slice(),
            &[(first, 0x33)],
            "Fix: resident sequence fills must keep the last duplicate fill and drop fills fully overwritten by same-sequence uploads."
        );
    }

    #[test]
    fn resident_sequence_fills_handle_dense_duplicate_streams_without_changing_order() {
        let handles: Vec<_> = (0..256)
            .map(|id| CudaResidentBuffer { id, byte_len: 1 })
            .collect();
        let mut fills = Vec::new();
        for round in 0..8_u8 {
            fills.extend(handles.iter().copied().map(|handle| (handle, round)));
        }

        let upload = [0xAA_u8];
        let uploads: Vec<_> = handles
            .iter()
            .copied()
            .filter(|handle| handle.id % 2 == 0)
            .map(|handle| (handle, upload.as_slice()))
            .collect();

        let effective = prepare_resident_sequence_fills(&fills, &uploads)
            .expect("Fix: dense CUDA resident fill coalescing must reserve bounded indices.");

        assert_eq!(
            effective.len(),
            128,
            "Fix: uploaded handles must suppress same-sequence fills even under dense duplicate traffic."
        );
        for (position, (handle, value)) in effective.iter().copied().enumerate() {
            assert_eq!(
                handle.id % 2,
                1,
                "Fix: uploaded resident handle {} must not retain a redundant fill.",
                handle.id
            );
            assert_eq!(
                handle.id as usize,
                position * 2 + 1,
                "Fix: first-seen fill order must be stable after duplicate coalescing."
            );
            assert_eq!(
                value, 7,
                "Fix: duplicate resident fills must keep the final value for each handle."
            );
        }
    }

    #[test]
    fn resident_sequence_fill_coalescing_uses_checked_effective_slot_updates() {
        let source = super::resident_dispatch_production_source();
        let helper = source
            .split("pub(crate) fn prepare_resident_sequence_fills")
            .nth(1)
            .and_then(|tail| tail.split("pub(crate) struct PreparedStep").next())
            .expect("Fix: resident dispatch helpers must expose prepare_resident_sequence_fills before PreparedStep.");

        assert!(
            helper.contains("effective.get_mut(index)")
                && helper.contains("pointed at stale effective fill slot {index}")
                && !helper.contains("effective[index]"),
            "Fix: duplicate resident sequence fill coalescing must convert stale effective-slot indexes into BackendError instead of panicking."
        );
    }

    #[test]
    fn resident_full_readback_preparation_is_single_sourced() {
        let source = super::resident_dispatch_production_source();
        let helper = source
            .split("fn prepare_full_resident_readbacks")
            .nth(1)
            .and_then(|tail| tail.split("pub(crate) fn upload_resident_many_sequence_read_ranges_into").next())
            .expect("Fix: resident sequence API must expose full readback preparation before ranged sequence APIs.");
        let readback_reserve = helper
            .find("reserve_smallvec(\n            readbacks")
            .expect(
                "Fix: full resident readback preparation must reserve caller scratch readbacks.",
            );
        let view_cache_reserve = helper
            .find("reserve_smallvec(\n            &mut resident_view_cache")
            .expect(
                "Fix: full resident readback preparation must reserve the resident view cache.",
            );
        let clear = helper.find("readbacks.clear();").expect(
            "Fix: full resident readback preparation must clear reusable scratch before refilling.",
        );

        assert!(
            source.contains("fn prepare_full_resident_readbacks")
                && source
                    .matches(concat!("self.", "prepare_full_resident_readbacks(read_handles"))
                    .count()
                    == 2,
            "Fix: CUDA resident full-handle readback preparation must be shared by read_many and fill_read_many paths."
        );
        assert!(
            readback_reserve < clear && view_cache_reserve < clear,
            "Fix: CUDA resident full-readback preparation must reserve all scratch before clearing reusable readback state."
        );
    }

    #[test]
    fn resident_sequence_output_slot_borrowing_is_single_sourced_and_reuses_slots() {
        let source = super::resident_dispatch_production_source();
        let production = source
            .split("#[cfg(test)]")
            .next()
            .expect("Fix: resident_dispatch production source must precede tests.");
        assert!(
            production.contains("fn borrow_resident_sequence_output_slots")
                && production
                    .matches("borrow_resident_sequence_output_slots(outputs,")
                    .count()
                    == 2,
            "Fix: CUDA resident sequence read_many and fill_read_many must share output-slot borrowing."
        );

        let mut outputs = vec![vec![1, 2, 3], Vec::new(), vec![4]];
        let initial_first_capacity = outputs[0].capacity();
        {
            let borrowed = borrow_resident_sequence_output_slots(&mut outputs, 2)
                .expect("Fix: output-slot borrowing should resize existing slots.");
            assert_eq!(borrowed.len(), 2);
        }
        assert_eq!(outputs.len(), 2);
        assert!(
            outputs[0].capacity() >= initial_first_capacity,
            "Fix: resizing borrowed resident output slots must preserve existing slot allocation."
        );
    }

    #[test]
    fn resident_sequence_resolves_views_once_per_sequence() {
        let source = super::resident_dispatch_production_source();
        let production = source
            .split("#[cfg(test)]")
            .next()
            .expect("Fix: resident_dispatch production source must precede tests.");

        assert!(
            production.contains("fn resolve_resident_sequence_launch_ptrs")
                && production
                    .matches("resolve_resident_sequence_launch_ptrs(step,")
                    .count()
                    == 1,
            "Fix: CUDA resident sequence launch-pointer validation must be single-sourced."
        );
        assert!(
            production.contains("let mut sequence_view_cache = ResidentViewCache::new();")
                && production.contains("resident sequence view cache")
                && !production.contains("resident sequence fill view cache")
                && !production.contains("resident sequence step view cache")
                && !production.contains("resident sequence readback view cache")
                && !production.contains("struct ClearCopy"),
            "Fix: CUDA resident sequence dispatch must use one sequence-wide resident view cache instead of rebuilding fill, step, and readback caches."
        );
    }

    #[test]
    fn resident_sequence_parameter_cache_growth_is_fallible() {
        let source = super::resident_dispatch_production_source();
        let production = source
            .split("#[cfg(test)]")
            .next()
            .expect("Fix: resident_dispatch production source must precede tests.");
        let cache_section = production
            .split("let mut sequence_param_cache")
            .nth(1)
            .expect("Fix: CUDA resident sequence dispatch must keep a per-sequence parameter cache.")
            .split("let mut upload_host_transfers")
            .next()
            .expect("Fix: CUDA resident sequence parameter cache must be reserved before upload staging.");

        assert!(
            production.contains(
                "let mut sequence_param_cache = FxHashMap::<SmallVec<[u32; 8]>, u64>::default();"
            )
                && cache_section.contains("reserve_hash_map(\n            &mut sequence_param_cache")
                && cache_section.contains("prepared_steps.len()")
                && cache_section.contains("\"resident sequence parameter cache\"")
                && production.contains(
                    "sequence_param_cache.get(step.prepared.launch.param_words.as_slice())"
                )
                && production.contains("sequence_param_cache.insert(cached_param_words, params_ptr)")
                && !production.contains("sequence_param_cache.iter().find"),
            "Fix: CUDA resident sequence parameter-cache growth must be fallibly reserved to the prepared-step bound and use exact hash lookup instead of rescanning cached launch words."
        );
    }

    #[test]
    fn resident_sequence_error_cleanup_leaks_resources_when_sync_is_unproven() {
        let source = super::resident_dispatch_production_source();
        let sequence = source
            .split("pub(crate) fn fill_upload_resident_many_repeated_sequence_read_ranges_borrowed_into")
            .nth(1)
            .expect("Fix: resident sequence fused dispatch function must exist.")
            .split("    }\n}")
            .next()
            .expect("Fix: resident sequence fused dispatch must end inside its module impl.");
        let cleanup = sequence
            .split("if result.is_err()")
            .nth(1)
            .expect("Fix: resident sequence dispatch must handle error cleanup explicitly.")
            .split("self.launch_resources.release_stream(stream);")
            .next()
            .expect("Fix: resident sequence error cleanup must precede stream release.");

        assert!(
            cleanup.contains("match stream.synchronize()")
                && cleanup.contains("Ok(()) => self.telemetry.record_sync_point()")
                && cleanup.contains("Err(error) =>")
                && cleanup.contains("In-flight resident sequence resources will not be recycled.")
                && !cleanup.contains("let _ = stream.synchronize();"),
            "Fix: CUDA resident sequence error cleanup must not ignore failed stream synchronization or record sync telemetry without proof."
        );
        for resource in [
            "stream",
            "resident_use",
            "allocations",
            "host_transfers",
            "upload_host_transfers",
            "readback_host_transfers",
            "timing_events",
        ] {
            assert!(
                cleanup.contains(&format!("std::mem::forget({resource});")),
                "Fix: CUDA resident sequence error cleanup must leak {resource} when stream completion is unproven."
            );
        }
        assert!(
            cleanup.contains("return result;"),
            "Fix: CUDA resident sequence error cleanup must not continue to pooled stream release after leaking in-flight resources."
        );

        let param_upload = sequence
            .split("let param_host_ptr =")
            .nth(1)
            .expect("Fix: resident sequence parameter upload staging must exist.")
            .split("self.telemetry.record_host_to_device_bytes")
            .next()
            .expect("Fix: resident sequence parameter upload must record telemetry after enqueue.");
        let retain_param_staging_pos = param_upload
            .find("host_transfers.push(step_host_transfers);")
            .expect("Fix: resident sequence parameter staging must be retained before async H2D enqueue.");
        let enqueue_param_pos = param_upload
            .find("enqueue_resident_h2d_copy(")
            .expect("Fix: resident sequence parameter upload must enqueue an async H2D copy.");
        assert!(
            retain_param_staging_pos < enqueue_param_pos,
            "Fix: resident sequence parameter host staging must enter outer cleanup ownership before async H2D enqueue."
        );

        let readback = sequence
            .split("readback_host_transfers = Some(HostTransferAllocations::with_capacity")
            .nth(1)
            .expect("Fix: resident sequence readback staging must be owned outside the fallible stream closure.")
            .split("self.telemetry.record_host_to_device_bytes")
            .next()
            .expect("Fix: resident sequence readback staging must precede final telemetry.");
        assert!(
            readback.contains("readback_host_transfers.as_mut()")
                && readback.contains("transfers.push_output(copy.byte_len)?")
                && readback.contains("stream.synchronize()?")
                && readback.contains("transfers.collect_output_range_into"),
            "Fix: resident sequence compact readback staging must remain owned by outer cleanup until stream completion is proven and outputs are collected."
        );
    }

    #[test]
    fn resident_sequence_captures_kernel_device_time_around_launches() {
        let source = super::resident_dispatch_production_source();
        let sequence = source
            .split("pub(crate) fn fill_upload_resident_many_repeated_sequence_read_ranges_borrowed_into")
            .nth(1)
            .expect("Fix: resident sequence fused dispatch function must exist.")
            .split("    }\n}")
            .next()
            .expect("Fix: resident sequence fused dispatch must end inside its module impl.");

        // Timing is acquired only when the sequence actually launches kernels, so
        // pure fill/upload/readback sequences never pay for a CUDA event pair, and
        // an exhausted event pool degrades to host-wall-only instead of failing.
        assert!(
            sequence.contains("let has_launch_work = !prefix_step_indices.is_empty()")
                && sequence
                    .contains("|| (repeat_count != 0 && !repeated_step_indices.is_empty());")
                && sequence.contains("if has_launch_work {")
                && sequence.contains("self.launch_resources.acquire_timing_event_pair()"),
            "Fix: CUDA resident sequence must acquire a CUDA timing-event pair, gated on real launch work, to measure kernel device time."
        );

        // The events must bracket exactly the kernel launches: start before the
        // first launch, end after the last, so the measured interval is kernel
        // device time and not host enqueue/readback overhead.
        let start_record = sequence
            .find("start_event.record(stream.raw())?;")
            .expect("Fix: CUDA resident sequence must record a start timing event before launching kernels.");
        let prefix_launch = sequence
            .find("for &step_index in &prefix_step_indices {")
            .expect("Fix: CUDA resident sequence must launch prefix steps by index.");
        let repeated_launch = sequence
            .find("for &step_index in &repeated_step_indices {")
            .expect("Fix: CUDA resident sequence must launch repeated steps by index.");
        let end_record = sequence
            .find("end_event.record(stream.raw())?;")
            .expect("Fix: CUDA resident sequence must record an end timing event after launching kernels.");
        assert!(
            start_record < prefix_launch && repeated_launch < end_record,
            "Fix: CUDA resident sequence timing events must bracket the kernel launches (start before the first launch, end after the last)."
        );

        // The kernel interval is recorded through telemetry only on the success
        // path (the closure already synchronized the stream), and a timing-read
        // failure must degrade to a debug log instead of failing a completed scan.
        let success_timing = sequence
            .split("if result.is_ok() {")
            .nth(1)
            .expect("Fix: CUDA resident sequence must record device time on the success path.")
            .split("if result.is_err() {")
            .next()
            .expect("Fix: CUDA resident sequence success-path timing must precede error handling.");
        assert!(
            success_timing
                .contains(".record_timed_dispatch(wall_ns, Some(device_ns), None, None)")
                && success_timing.contains(".elapsed_time_ns(end_event)")
                && success_timing.contains("tracing::debug!")
                && !success_timing.contains(".elapsed_time_ns(end_event)?"),
            "Fix: CUDA resident sequence must record measured kernel device time via telemetry on success and must not turn a timing-read failure into a dispatch error."
        );

        // Events are recycled to the pool on the normal path and forgotten with
        // the other in-flight resources when stream completion is unproven.
        assert!(
            sequence.contains("self.launch_resources.release_timing_event(start_event);")
                && sequence.contains("self.launch_resources.release_timing_event(end_event);"),
            "Fix: CUDA resident sequence must release timing events back to the pool after a successful dispatch."
        );
    }

    #[test]
    fn resident_async_error_cleanup_leaks_resources_when_sync_is_unproven() {
        let source = super::resident_dispatch_production_source();
        let dispatch = source
            .split("pub(crate) fn dispatch_resident_async_concrete_with_ptx_key")
            .nth(1)
            .expect("Fix: resident async dispatch function must exist.")
            .split("    }\n}")
            .next()
            .expect("Fix: resident async dispatch must end inside its module impl.");
        assert!(
            dispatch.contains("let mut launch_resources = Some(launch_resources);")
                && dispatch.contains("let mut allocations = Some(allocations);")
                && dispatch.contains("let mut resident_use = Some(resident_use);")
                && dispatch.contains("let mut host_transfers = Some(host_transfers);")
                && dispatch.contains("let enqueue_result = (||"),
            "Fix: CUDA resident async dispatch must retain launch resources, resident use, transient allocations, and pinned host staging in outer cleanup ownership until post-kernel completion is proven."
        );
        assert!(
            dispatch.contains("crate::stream::synchronize_raw_stream(\n                stream_raw,\n                \"cuStreamSynchronize (resident async error cleanup)\",")
                && dispatch.contains("In-flight resident dispatch resources will not be recycled.")
                && dispatch.contains("std::mem::forget(launch_resources);")
                && dispatch.contains("std::mem::forget(allocations);")
                && dispatch.contains("std::mem::forget(resident_use);")
                && dispatch.contains("std::mem::forget(host_transfers);"),
            "Fix: CUDA resident async dispatch must leak in-flight resources when completion is unproven after enqueue errors."
        );
        let cleanup_pos = dispatch
            .find("if let Err(error) = enqueue_result")
            .expect("Fix: resident async dispatch must classify enqueue cleanup errors.");
        let post_kernel_sync_pos = dispatch
            .find("\"cuStreamSynchronize (resident post-kernel)\"")
            .expect(
                "Fix: resident async dispatch must prove completion before synchronous readback.",
            );
        let output_readback_pos = dispatch
            .find("let mut staged_readback_bytes = 0_u64;")
            .expect("Fix: resident async dispatch must keep synchronous output readback after cleanup classification.");
        assert!(
            post_kernel_sync_pos < cleanup_pos && cleanup_pos < output_readback_pos,
            "Fix: resident async dispatch must wrap all fallible enqueue work through the post-kernel fence before releasing resources to synchronous readback."
        );
    }

    #[test]
    fn resident_batch_error_cleanup_leaks_resources_when_sync_is_unproven() {
        let source = super::resident_dispatch_production_source();
        let batch = source
            .split("pub(crate) fn dispatch_resident_batch_async_concrete_with_ptx_key")
            .nth(1)
            .expect("Fix: resident batch dispatch function must exist.")
            .split("    }\n}")
            .next()
            .expect("Fix: resident batch dispatch must end inside its module impl.");
        assert!(
            batch.contains("let mut launch_resources = Some(launch_resources);")
                && batch.contains("let mut allocations = Some(allocations);")
                && batch.contains("let mut resident_use = Some(resident_use);")
                && batch.contains("let mut host_transfers = Some(host_transfers);")
                && batch.contains("let pending = (||"),
            "Fix: CUDA resident batch dispatch must retain launch resources, resident use, transient allocations, and pinned host staging in outer cleanup ownership until pending dispatch takes over."
        );
        assert!(
            batch.contains("crate::stream::synchronize_raw_stream(\n                    stream_raw,\n                    \"cuStreamSynchronize (resident batch error cleanup)\",")
                && batch.contains("In-flight resident batch resources will not be recycled.")
                && batch.contains("std::mem::forget(launch_resources);")
                && batch.contains("std::mem::forget(allocations);")
                && batch.contains("std::mem::forget(resident_use);")
                && batch.contains("std::mem::forget(host_transfers);"),
            "Fix: CUDA resident batch dispatch must leak in-flight resources when completion is unproven after enqueue errors."
        );
        let cleanup_pos = batch
            .find("let pending = match pending")
            .expect("Fix: resident batch dispatch must classify pending construction errors.");
        let transfer_pos = batch
            .find("CudaPendingDispatch::new_resident_batch_pending")
            .expect("Fix: resident batch dispatch must eventually transfer ownership to CudaPendingDispatch.");
        assert!(
            transfer_pos < cleanup_pos,
            "Fix: resident batch dispatch must install fail-closed cleanup around all fallible enqueue work before returning pending ownership."
        );
    }
}