From bda02606f5d76341f10df89131a7ca05d1d2729f Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Wed, 25 Nov 2026 12:59:18 -0700 Subject: [PATCH 1/2] refactor(pathfinder): give the found_via labels one home The `found_via` strings ("site-packages", "conda", "CUDA_PATH", ...) are public: they appear on every result object. They were typed by hand in five modules, and `load_dl_linux.py` branches on two of them, so a typo in one module would silently change loader behavior. They are now constants in `_utils/found_via.py`, and every producer and the one consumer use the constants. A new test pins the eight public values. No behavior change: the strings are identical. 1647 passed, 6 skipped, and the step-by-step comparisons against `main` are still byte-identical. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin docs(pathfinder): tell agents where the found_via labels live `AGENTS.md` now says to use the constants in `_utils/found_via.py` and not to type the label strings anywhere else. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin docs(pathfinder): say the label rule is for package code AGENTS.md told agents not to type `found_via` label strings anywhere but `found_via.py`. Tests type them in many places on purpose: that is how they pin the public values. A review bot already quoted the rule against a test. The rule now says what was meant: package code uses the constants, tests may type the strings. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- cuda_pathfinder/AGENTS.md | 4 +++ .../pathfinder/_dynamic_libs/load_dl_linux.py | 16 ++++++++---- .../_dynamic_libs/load_dl_windows.py | 7 +++-- .../_dynamic_libs/search_platform.py | 3 ++- .../pathfinder/_dynamic_libs/search_steps.py | 14 +++++++--- .../_headers/find_nvidia_headers.py | 17 ++++++++---- .../pathfinder/_static_libs/locate_file.py | 7 ++--- .../cuda/pathfinder/_utils/found_via.py | 26 +++++++++++++++++++ cuda_pathfinder/tests/test_utils_found_via.py | 19 ++++++++++++++ 9 files changed, 93 insertions(+), 20 deletions(-) create mode 100644 cuda_pathfinder/cuda/pathfinder/_utils/found_via.py create mode 100644 cuda_pathfinder/tests/test_utils_found_via.py diff --git a/cuda_pathfinder/AGENTS.md b/cuda_pathfinder/AGENTS.md index f3fdb7784c1a..f6fd69ac6b02 100644 --- a/cuda_pathfinder/AGENTS.md +++ b/cuda_pathfinder/AGENTS.md @@ -46,6 +46,10 @@ Windows. - unknown name -> `DynamicLibUnknownError` - known but unsupported on this OS -> `DynamicLibNotAvailableError` - known/supported but not found/loadable -> `DynamicLibNotFoundError` +- **Spell `found_via` labels once**: package code uses the constants in + `cuda/pathfinder/_utils/found_via.py` and never types the label strings. + The labels are public and the Linux loader branches on them. Tests type + the strings on purpose: that pins the public values. - **Do not hardcode host assumptions**: avoid baking in machine-local paths, shell-specific quoting, or environment assumptions. - **Prefer focused abstractions**: if a change is platform-specific, route it diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py index d631267bbe83..484e5724593a 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py @@ -11,6 +11,12 @@ from typing import TYPE_CHECKING, cast from cuda.pathfinder._dynamic_libs.load_dl_common import LoadedDL +from cuda.pathfinder._utils.found_via import ( + FOUND_VIA_CONDA, + FOUND_VIA_SITE_PACKAGES, + FOUND_VIA_SYSTEM_SEARCH, + FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE, +) if TYPE_CHECKING: from cuda.pathfinder._dynamic_libs.lib_descriptor import LibDescriptor @@ -142,7 +148,7 @@ def check_if_already_loaded_from_elsewhere(desc: LibDescriptor) -> LoadedDL | No abs_path_for_dynamic_library(desc.name, handle), True, handle._handle, - "was-already-loaded-from-elsewhere", + FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE, ) return None @@ -178,17 +184,17 @@ def load_with_system_search(desc: LibDescriptor) -> LoadedDL | None: else: abs_path = abs_path_for_dynamic_library(desc.name, handle) assert abs_path - return LoadedDL(abs_path, False, handle._handle, "system-search") + return LoadedDL(abs_path, False, handle._handle, FOUND_VIA_SYSTEM_SEARCH) return None def _preload_nvrtc_builtins(found_path: str, found_via: str | None) -> None: # Let Conda packaging provide sibling lookup through RPATH. - if found_via == "conda": + if found_via == FOUND_VIA_CONDA: return real_path = os.path.realpath(found_path) version = os.path.basename(real_path).removeprefix("libnvrtc.so.").split(".") - if found_via == "site-packages": + if found_via == FOUND_VIA_SITE_PACKAGES: # Retain only the historical 13.0 wheel workaround. if version[0] != "13": return @@ -204,7 +210,7 @@ def _preload_nvrtc_builtins(found_path: str, found_via: str | None) -> None: major, minor = version[0], version[1] sibling = os.path.join(os.path.dirname(real_path), f"libnvrtc-builtins.so.{major}.{minor}") if not os.path.isfile(sibling): - if found_via != "site-packages": + if found_via != FOUND_VIA_SITE_PACKAGES: warnings.warn(f"NVRTC builtins sibling not found: {sibling}", RuntimeWarning, stacklevel=2) return # If preloading fails, NVRTC's own builtins lookup may still succeed. diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py index e70aa1c42fbe..6e7220a19e74 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py @@ -12,6 +12,7 @@ from typing import TYPE_CHECKING from cuda.pathfinder._dynamic_libs.load_dl_common import LoadedDL +from cuda.pathfinder._utils.found_via import FOUND_VIA_SYSTEM_SEARCH, FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE if TYPE_CHECKING: from cuda.pathfinder._dynamic_libs.lib_descriptor import LibDescriptor @@ -129,7 +130,9 @@ def check_if_already_loaded_from_elsewhere(desc: LibDescriptor) -> LoadedDL | No # Match load_with_abs_path(): lazy component DLLs need the directory # of the module that is actually loaded, regardless of how it arrived. add_dll_directory(abs_path) - return LoadedDL(abs_path, True, ctypes_handle_to_unsigned_int(handle), "was-already-loaded-from-elsewhere") + return LoadedDL( + abs_path, True, ctypes_handle_to_unsigned_int(handle), FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE + ) return None @@ -153,7 +156,7 @@ def load_with_system_search(desc: LibDescriptor) -> LoadedDL | None: handle = kernel32.LoadLibraryExW(dll_name, None, 0) if handle: abs_path = abs_path_for_dynamic_library(desc.name, handle) - return LoadedDL(abs_path, False, ctypes_handle_to_unsigned_int(handle), "system-search") + return LoadedDL(abs_path, False, ctypes_handle_to_unsigned_int(handle), FOUND_VIA_SYSTEM_SEARCH) return None diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py index a91ccabbb264..916ff315b14e 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py @@ -20,6 +20,7 @@ from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import is_suppressed_dll_file from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages +from cuda.pathfinder._utils.found_via import FOUND_VIA_SITE_PACKAGES from cuda.pathfinder._utils.platform_aware import IS_WINDOWS from cuda.pathfinder._utils.windows_arch import windows_pe_matches_arch, windows_python_arch @@ -177,7 +178,7 @@ def find_lib_in_site_packages(ctx: SearchContext) -> FindResult | NotFound: for abs_dir in find_sub_dirs_all_sitepackages(sub_dir): lib_path = _find_lib_file(ctx, abs_dir) if lib_path is not None: - return FindResult(lib_path, "site-packages") + return FindResult(lib_path, FOUND_VIA_SITE_PACKAGES) sub_dirs_searched.append(sub_dir) missed = NotFound() for sub_dir in sub_dirs_searched: diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py index 63425e8e15b0..73eef6fc2265 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py @@ -35,6 +35,12 @@ find_lib_in_site_packages, ) from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home +from cuda.pathfinder._utils.found_via import ( + FOUND_VIA_CONDA, + FOUND_VIA_CUDA_PATH, + FOUND_VIA_PROGRAM_FILES, + FOUND_VIA_SYSTEM_CTK_ROOT, +) from cuda.pathfinder._utils.path_sort import numeric_aware_path_sort_key from .._utils.resolve_path import resolve_path @@ -123,7 +129,7 @@ def derive_ctk_root(resolved_lib_path: str) -> str | None: def find_via_ctk_root(ctx: SearchContext, ctk_root: str) -> FindResult | NotFound: """Find a library under a previously derived CTK root.""" - return _find_under_anchor_root(ctx, ctk_root, "system-ctk-root") + return _find_under_anchor_root(ctx, ctk_root, FOUND_VIA_SYSTEM_CTK_ROOT) # --------------------------------------------------------------------------- @@ -144,7 +150,7 @@ def find_in_conda(ctx: SearchContext) -> FindResult | NotFound: if not conda_prefix: return NotFound() anchor = ctx.conda_anchor_point(conda_prefix) - return _find_under_anchor_root(ctx, anchor, "conda") + return _find_under_anchor_root(ctx, anchor, FOUND_VIA_CONDA) def find_in_install_root_env_vars(ctx: SearchContext) -> FindResult | NotFound: @@ -176,7 +182,7 @@ def find_in_cuda_path(ctx: SearchContext) -> FindResult | NotFound: cuda_home = get_cuda_path_or_home() if cuda_home is None: return NotFound() - return _find_under_anchor_root(ctx, cuda_home, "CUDA_PATH") + return _find_under_anchor_root(ctx, cuda_home, FOUND_VIA_CUDA_PATH) def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound: @@ -186,7 +192,7 @@ def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound: for root in sorted(glob.glob(root_glob), key=numeric_aware_path_sort_key, reverse=True): if not os.path.isdir(root): continue - result = _find_under_anchor_root(ctx, str(resolve_path(root)), "ProgramFiles") + result = _find_under_anchor_root(ctx, str(resolve_path(root)), FOUND_VIA_PROGRAM_FILES) if result.found: return result missed += result diff --git a/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py b/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py index a4afada8555b..5ed4043ba35f 100644 --- a/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py +++ b/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py @@ -23,6 +23,13 @@ from cuda.pathfinder._utils.ctk_root_canary import CTK_ROOT_CANARY_ANCHOR_LIBNAMES from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages +from cuda.pathfinder._utils.found_via import ( + FOUND_VIA_CONDA, + FOUND_VIA_CUDA_PATH, + FOUND_VIA_SITE_PACKAGES, + FOUND_VIA_SUPPORTED_INSTALL_DIR, + FOUND_VIA_SYSTEM_CTK_ROOT, +) from cuda.pathfinder._utils.path_sort import numeric_aware_path_sort_key from .._utils.resolve_path import resolve_path @@ -87,7 +94,7 @@ def find_in_site_packages(desc: HeaderDescriptor) -> LocatedHeaderDir | None: hdr_dir: str # help mypy for hdr_dir in find_sub_dirs_all_sitepackages(tuple(sub_dir.split("/"))): if _joined_isfile(hdr_dir, desc.header_basename): - return LocatedHeaderDir(abs_path=hdr_dir, found_via="site-packages") + return LocatedHeaderDir(abs_path=hdr_dir, found_via=FOUND_VIA_SITE_PACKAGES) return None @@ -101,7 +108,7 @@ def find_in_conda(desc: HeaderDescriptor) -> LocatedHeaderDir | None: return None found_header_path = _locate_in_anchor_layout(desc, anchor_point) if found_header_path: - return LocatedHeaderDir(abs_path=found_header_path, found_via="conda") + return LocatedHeaderDir(abs_path=found_header_path, found_via=FOUND_VIA_CONDA) return None @@ -124,7 +131,7 @@ def find_in_cuda_path(desc: HeaderDescriptor) -> LocatedHeaderDir | None: return None result = _locate_in_anchor_layout(desc, cuda_home) if result is not None: - return LocatedHeaderDir(abs_path=result, found_via="CUDA_PATH") + return LocatedHeaderDir(abs_path=result, found_via=FOUND_VIA_CUDA_PATH) return None @@ -146,7 +153,7 @@ def find_via_ctk_root_canary(desc: HeaderDescriptor) -> LocatedHeaderDir | None: return None result = _locate_in_anchor_layout(desc, ctk_root) if result is not None: - return LocatedHeaderDir(abs_path=result, found_via="system-ctk-root") + return LocatedHeaderDir(abs_path=result, found_via=FOUND_VIA_SYSTEM_CTK_ROOT) return None @@ -155,7 +162,7 @@ def find_in_system_install_dirs(desc: HeaderDescriptor) -> LocatedHeaderDir | No for pattern in system_install_dir_patterns(desc): for hdr_dir in sorted(glob.glob(pattern), key=numeric_aware_path_sort_key, reverse=True): if _joined_isfile(hdr_dir, desc.header_basename): - return LocatedHeaderDir(abs_path=hdr_dir, found_via="supported_install_dir") + return LocatedHeaderDir(abs_path=hdr_dir, found_via=FOUND_VIA_SUPPORTED_INSTALL_DIR) return None diff --git a/cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py b/cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py index 941d1f7bdad2..36cf85a1a9e3 100644 --- a/cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py +++ b/cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py @@ -8,6 +8,7 @@ from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages +from cuda.pathfinder._utils.found_via import FOUND_VIA_CONDA, FOUND_VIA_CUDA_PATH, FOUND_VIA_SITE_PACKAGES from cuda.pathfinder._utils.platform_aware import IS_WINDOWS from .._utils.resolve_path import resolve_path @@ -41,7 +42,7 @@ def locate_file( for abs_dir in find_sub_dirs_all_sitepackages(tuple(rel_dir.split("/"))): abs_path = _resolved_if_file(abs_dir, filename) if abs_path is not None: - return abs_path, "site-packages" + return abs_path, FOUND_VIA_SITE_PACKAGES conda_prefix = os.environ.get("CONDA_PREFIX") if conda_prefix: @@ -49,14 +50,14 @@ def locate_file( for rel_path in conda_rel_paths: abs_path = _resolved_if_file(anchor / rel_path, filename) if abs_path is not None: - return abs_path, "conda" + return abs_path, FOUND_VIA_CONDA cuda_home = get_cuda_path_or_home() if cuda_home is not None: for rel_path in ctk_rel_paths: abs_path = _resolved_if_file(Path(cuda_home, rel_path), filename) if abs_path is not None: - return abs_path, "CUDA_PATH" + return abs_path, FOUND_VIA_CUDA_PATH raise not_found_error(_not_found_message(filename, cuda_home, ctk_rel_paths)) diff --git a/cuda_pathfinder/cuda/pathfinder/_utils/found_via.py b/cuda_pathfinder/cuda/pathfinder/_utils/found_via.py new file mode 100644 index 000000000000..90a2e5865655 --- /dev/null +++ b/cuda_pathfinder/cuda/pathfinder/_utils/found_via.py @@ -0,0 +1,26 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""The ``found_via`` labels: where a search found what it was looking for. + +These strings are public. They appear in ``LoadedDL.found_via``, +``LocatedHeaderDir.found_via``, ``LocatedStaticLib.found_via`` and +``LocatedBitcodeLib.found_via``, and the Linux loader branches on two of them. +Spell them here and nowhere else. + +A product root found through a product-specific environment variable is +labeled with that variable's name (for example ``"CUDNN_PATH"``), so it has no +constant here. +""" + +from typing import Final + +FOUND_VIA_SITE_PACKAGES: Final = "site-packages" +FOUND_VIA_CONDA: Final = "conda" +FOUND_VIA_SYSTEM_SEARCH: Final = "system-search" +#: Covers both the ``CUDA_PATH`` and the ``CUDA_HOME`` environment variable. +FOUND_VIA_CUDA_PATH: Final = "CUDA_PATH" +FOUND_VIA_PROGRAM_FILES: Final = "ProgramFiles" +FOUND_VIA_SYSTEM_CTK_ROOT: Final = "system-ctk-root" +FOUND_VIA_SUPPORTED_INSTALL_DIR: Final = "supported_install_dir" +FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE: Final = "was-already-loaded-from-elsewhere" diff --git a/cuda_pathfinder/tests/test_utils_found_via.py b/cuda_pathfinder/tests/test_utils_found_via.py new file mode 100644 index 000000000000..42c552b68a7c --- /dev/null +++ b/cuda_pathfinder/tests/test_utils_found_via.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +import pytest + +from cuda.pathfinder._utils import found_via + + +@pytest.mark.agent_authored(model="claude-fable-5-2") +def test_found_via_labels_keep_their_public_values(): + # Callers compare ``found_via`` against these exact strings. + assert found_via.FOUND_VIA_SITE_PACKAGES == "site-packages" + assert found_via.FOUND_VIA_CONDA == "conda" + assert found_via.FOUND_VIA_SYSTEM_SEARCH == "system-search" + assert found_via.FOUND_VIA_CUDA_PATH == "CUDA_PATH" + assert found_via.FOUND_VIA_PROGRAM_FILES == "ProgramFiles" + assert found_via.FOUND_VIA_SYSTEM_CTK_ROOT == "system-ctk-root" + assert found_via.FOUND_VIA_SUPPORTED_INSTALL_DIR == "supported_install_dir" + assert found_via.FOUND_VIA_WAS_ALREADY_LOADED_FROM_ELSEWHERE == "was-already-loaded-from-elsewhere" From e2c6e59f425115d95fdb7e90f993cf5b09659ff4 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 26 Nov 2026 11:17:26 -0800 Subject: [PATCH 2/2] ci: require manual copy-pr-bot approval Signed-off-by: Ralf W. Grosse-Kunstleve