# Code Review Report — Batch 31 / Cycle 1

**Batch**: 31
**Tasks**: AZ-298 (C7 TensorrtRuntime)
**Date**: 2026-05-12
**Verdict**: PASS_WITH_WARNINGS

## Findings

| # | Severity | Category | File:Line | Title |
|---|----------|----------|-----------|-------|
| 1 | Medium | Performance | `src/gps_denied_onboard/components/c7_inference/tensorrt_runtime.py::infer` | `_load_trt()` called inside per-output loop |
| 2 | Low | Maintainability | `tensorrt_runtime.py::_predicted_deserialize_bytes` | 256 MiB flat fallback when `extras["opt_buffer_bytes"]` is absent |
| 3 | Low | Test-quality | `tests/unit/c7_inference/test_tensorrt_runtime.py::test_infer_orders_h2d_enqueue_d2h_sync` | Ordering verified via fake-module call counts, not a real CUDA event trace |
| 4 | Low | Architecture | `tensorrt_runtime.py::infer` | Access to `TrtEngineHandle._input_buffers` / `_output_buffers` via slot names |
| 5 | Low | Scope | `tensorrt_runtime.py::_safe_del` | `del resource` only drops a local reference; helper is mostly defensive log-warn |

### Finding Details

**F1: `_load_trt()` called inside per-output loop** (Medium / Performance)
- Location: `src/gps_denied_onboard/components/c7_inference/tensorrt_runtime.py` — `TensorrtRuntime.infer`
- Description: The original implementation called `self._load_trt()` inside the per-output binding for-loop. The lazy import is module-cached so subsequent calls are cheap, but the attribute lookup + the try/except inside a hot path adds avoidable overhead.
- Suggestion: Hoist `trt = self._load_trt()` above the loops (alongside `cuda, _ = self._load_pycuda()`).
- Task: AZ-298
- Resolution: **AUTO-FIXED** in this batch.

**F2: 256 MiB flat fallback in `_predicted_deserialize_bytes`** (Low / Maintainability)
- Location: `tensorrt_runtime.py::_predicted_deserialize_bytes`
- Description: When `EngineCacheEntry.extras["opt_buffer_bytes"]` is missing (engine produced by an older compile path), the budget gate uses a flat 256 MiB upper-bound. This is conservative for typical engines but can underestimate for engines with very large profiles.
- Suggestion: `compile_engine` already stamps the field. Tighten the fallback only if an externally-produced engine appears in the cache; today the path is dormant.
- Task: AZ-298
- Resolution: Open (Low) — accepted as documented.

**F3: Fake-module call-count ordering** (Low / Test-quality)
- Location: `tests/unit/c7_inference/test_tensorrt_runtime.py::test_infer_orders_h2d_enqueue_d2h_sync`
- Description: Verifies H2D → enqueueV3 → D2H → sync via fake CUDA/TRT modules counting calls and asserting on a single linear flow. Does not capture a real CUDA event trace.
- Suggestion: The Tier-2 placeholder `test_ac7_real_infer_records_cuda_event_sequence` exists for the real event trace on Jetson; no change needed here.
- Task: AZ-298
- Resolution: Open (Low) — accepted as documented.

**F4: Slot-name access in `infer`** (Low / Architecture)
- Location: `tensorrt_runtime.py::TensorrtRuntime.infer`
- Description: `infer` reads `handle._input_buffers`, `handle._output_buffers`, `handle._exec_context`, etc. via the slot names declared on `TrtEngineHandle`. Per Invariant I-4 those fields are private to `TensorrtRuntime`, so the access is intra-class and the test code stays inside the c7_inference component boundary.
- Suggestion: None — the alternative (a getter method per field) would slow the hot path without contract gain.
- Task: AZ-298
- Resolution: Open (Low) — accepted as documented.

**F5: `_safe_del` is mostly defensive** (Low / Scope)
- Location: `tensorrt_runtime.py::_safe_del`
- Description: The helper calls `del resource` which only drops a local reference inside the helper scope; the real teardown happens when the caller drops its own reference. The helper exists as a single explicit place to swallow + WARN-log unusual teardown errors.
- Suggestion: Acceptable. The PyCUDA / TRT C++ shims hook destructors that fire when the last Python reference is released — `_safe_del` documents that contract in one place.
- Task: AZ-298
- Resolution: Open (Low) — accepted as documented.

## Verdict Logic

- 0 Critical
- 0 High
- 1 Medium (auto-fixed in this batch)
- 4 Low

→ **PASS_WITH_WARNINGS**: only Medium / Low findings; Medium was auto-fixed.
