Skip to content

refactor(pathfinder): write the "first hit, else every miss" loop once - #3632

Open
rparolin wants to merge 2 commits into
pathfinder-readability/22-plain-sub-dir-lookupfrom
pathfinder-readability/23-one-first-hit-loop
Open

rparolin wants to merge 2 commits into
pathfinder-readability/22-plain-sub-dir-lookupfrom
pathfinder-readability/23-one-first-hit-loop

Conversation

@rparolin

@rparolin rparolin commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Description

Part 23 of 23 of a readability stack for cuda.pathfinder (stack: #3623 → #3602 → #3603 → #3604 → #3605 → #3606 → #3607 → #3608 → #3609 → #3610 → #3611 → #3613 → #3618 → #3619 → #3620 → #3621 → #3622 → #3627 → #3628 → #3629 → #3630 → #3631 → #3632). No behavior change.

Five functions repeated the same loop: take results in order, return the first hit, otherwise add up the misses. first_hit() in search_steps.py now holds it, and the five pass it their results:

  • _find_under_root
  • find_in_install_root_env_vars
  • find_in_program_files_roots
  • run_find_steps
  • _try_ctk_root_canary in load_nvidia_dynamic_lib.py

Results are produced lazily, so nothing after the first hit runs. That matters for the canary step, which starts a subprocess per anchor: it still stops at the first root that has the library.

No behavior change. The existing run_find_steps tests cover the helper (first hit wins, later steps are not called, misses combine in order). Three planted bugs in the helper each fail the suite.

Testing

  • Full cuda_pathfinder test suite at this commit: 1298 passed, 6 skipped (Linux, Python 3.14).
  • pre-commit clean, including mypy-pathfinder. mypy also passes with --platform win32.
  • The step-by-step comparisons against main are still byte-identical (Linux, simulated Windows x64 and arm64), and all 526 public-API scenarios give the same result as fix(pathfinder): check for an already-loaded library before searching the disk #3623.
  • Not run on Windows. Windows rules are exercised by tests that simulate Windows on Linux.

PLC Local Security Evidence (Advisory)

Local advisory checks only, run over the whole stack. Not release-gate results.

Check Status Tool Details
Secrets SKIP Pulse Secret Scanner scanner image not available locally (registry login); the repo trufflehog pre-commit hook passed
SAST WARN Semgrep 1 finding, not from this stack: the existing subprocess.run call in the canary probe, now in canary_probe.py
Dependencies WARN pip-audit / osv-scanner no manifest or lockfile in scope, so the local scan could not run; this stack changes no dependencies
License N/A - no dependencies changed
Container N/A - no container changed

Checklist

  • I have read the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

🤖 Generated with Claude Code

@rparolin
rparolin added this pull request to stack #3616 November 26, 2026 18:40
@copy-pr-bot

copy-pr-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.pathfinder Everything related to the cuda.pathfinder module label Nov 26, 2026
@rparolin rparolin added the enhancement Any code-related improvements label Nov 26, 2026
@rparolin rparolin added this to the cuda.pathfinder next milestone Nov 26, 2026
@rparolin rparolin self-assigned this Nov 26, 2026
@rparolin
rparolin requested review from juenglin and rwgk November 26, 2026 18:42
@rparolin
rparolin marked this pull request as ready for review November 26, 2026 18:46
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.

Next included review available in 27 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 12 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 73d5042 and a0fac69.

Files selected for processing: 2

  • cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py
  • cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Refactor
    • Streamlined how the application searches for NVIDIA CUDA libraries. The existing search order and results are unchanged, so this update does not alter the user-facing behavior.

The changes add first_hit to handle ordered search results and misses, then use it across dynamic library searches, including the CTK root canary search. Existing search ordering, filters, and miss aggregation remain unchanged.

Changes: Dynamic library search

Layer / File(s) Summary
Centralize ordered search handling
cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py, cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py
Adds first_hit to lazily return the first match or combine misses in order. Directory, environment-root, Program Files, step-sequence, and CTK canary searches use the helper. Their ordering and filters remain unchanged.

Possibly related PRs

Suggested reviewers: cpcloud, dekhtiarjonathan

Change: Refactor

Merge Risk: Minimal · previous review 73d50

This refactor consolidates repeated first-hit search loops into one helper without a behavior change. No concrete defect was found, and the existing tests plus the author's comparisons support merging.

Architecture risk: High · up to 73d50

The change affects 1 system.

Changed systems: cuda_pathfinder

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — cuda_pathfinder (service) was modified; 2 changed files map to changed impact.

Before / after behavior

  • observed — Modified behavior in cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py: The first_hit helper is imported for use by the canary search.
  • observed — Modified behavior in cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py: _try_ctk_root_canary replaces its explicit loop—which skipped unresolved anchors, returned on the first found result, and accumulated misses—with a generator of resolved roots and a first_hit call over searches for non-None roots.
  • observed — Modified behavior in cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py: The Iterable type is added to the collection imports.
  • observed — Modified behavior in cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py: Adds first_hit, which evaluates results lazily, returns the first hit, and otherwise combines misses in iteration order.

Reliability and maintainability

  • inferred — Risk-relevant change factors for cuda_pathfinder: blast_radius_1; blast_radius_4; direct_dependents_1; direct_dependents_4
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 71f7017 and 73d5042.

Files selected for processing: 2

  • cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py
  • cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 8 remain after this review.

rparolin and others added 2 commits November 26, 2026 11:17
Five functions repeated the same loop: take results in order, return
the first hit, otherwise add up the misses. `first_hit()` in
`search_steps.py` now holds it, and the five pass it their results:

- `_find_under_root`
- `find_in_install_root_env_vars`
- `find_in_program_files_roots`
- `run_find_steps`
- `_try_ctk_root_canary` in `load_nvidia_dynamic_lib.py`

Results are produced lazily, so nothing after the first hit runs. That
matters for the canary step, which starts a subprocess per anchor: it
still stops at the first root that has the library.

No behavior change. The existing `run_find_steps` tests cover the helper
(first hit wins, later steps are not called, misses combine in order),
and three planted bugs in it each fail the suite.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Rob Parolin <rparolin@nvidia.com>
Signed-off-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
@rwgk
rwgk removed this pull request from stack #3616 November 26, 2026 19:19
@rwgk
rwgk force-pushed the pathfinder-readability/23-one-first-hit-loop branch from 73d5042 to a0fac69 Compare November 26, 2026 19:19
@copy-pr-bot

copy-pr-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@rwgk
rwgk added this pull request to stack #3633 November 26, 2026 19:19
@github-actions

Copy link
Copy Markdown
Contributor

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.pathfinder Everything related to the cuda.pathfinder module enhancement Any code-related improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants