This is a bit confusing and conflates a few conditions. Maybe split out the invalid trace condition separately and have the for comprehension only check for ID matching?
```
if (!l.isValid || !r.isValid) {
  // throw an exception
}
```