build: add static type checking for runfiles with rules_pyrefly (#4009)

Enable static type verification for the runfiles library using the
rules_pyrefly ruleset.

To ensure hermetic type safety across Bazel runfiles utilities, this
configures rules_pyrefly's Bzlmod dev toolchain, tags the runfiles
target with `tags = ["pyrefly"]`, and enables global aspect evaluation
in .bazelrc via
`--aspects=//tests/support/pyrefly:pyrefly.bzl%pyrefly_aspect`.

To support WORKSPACE mode builds without breaking dependency resolution,
a no-op stub repository for rules_pyrefly is defined under
tests/modules/rules_pyrefly_stub and registered in
internal_dev_deps.bzl.
Under Bzlmod, the aspect performs static type checking on tagged
targets,
while under WORKSPACE mode, the stub aspect evaluates cleanly as a
no-op.
diff --git a/.agents/rules/no_gh_auth_token_in_cmdline.md b/.agents/rules/no_gh_auth_token_in_cmdline.md
new file mode 100644
index 0000000..e8cb779
--- /dev/null
+++ b/.agents/rules/no_gh_auth_token_in_cmdline.md
@@ -0,0 +1,11 @@
+---
+trigger: always_on
+---
+
+# No Auth Token in Cmdline Rule
+
+* NEVER pass `gh auth token` or embed authentication tokens in command line
+  arguments or URLs. Doing so can leak credentials in process tables (`ps`),
+  shell history, or logs.
+* Use configured SSH keys, standard `git push`, or `gh` commands natively
+  instead.
diff --git a/.bazelrc b/.bazelrc
index 044990f..90f3bc8 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -19,6 +19,7 @@
 # Python targets as required.
 build --incompatible_default_to_explicit_init_py
 build --//python/config_settings:incompatible_default_to_explicit_init_py=True
+build --aspects=//tests/support/pyrefly:pyrefly.bzl%pyrefly_aspect
 
 # Ensure ongoing compatibility with this flag.
 common --incompatible_disallow_struct_provider_syntax
diff --git a/.bazelrc.deleted_packages b/.bazelrc.deleted_packages
index 9c4745c..da79f11 100644
--- a/.bazelrc.deleted_packages
+++ b/.bazelrc.deleted_packages
@@ -53,3 +53,4 @@
 common --deleted_packages=tests/modules/other/simple_v1
 common --deleted_packages=tests/modules/other/simple_v2
 common --deleted_packages=tests/modules/other/with_external_data
+common --deleted_packages=tests/modules/rules_pyrefly_stub/pyrefly
diff --git a/MODULE.bazel b/MODULE.bazel
index 1c20421..027277a 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -98,6 +98,20 @@
 bazel_dep(name = "rules_go", version = "0.60.0", dev_dependency = True, repo_name = "io_bazel_rules_go")
 bazel_dep(name = "gazelle", version = "0.40.0", dev_dependency = True, repo_name = "bazel_gazelle")
 bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.8.2", dev_dependency = True)
+bazel_dep(name = "rules_pyrefly", version = "0.1.0", dev_dependency = True)
+
+pyrefly = use_extension(
+    "@rules_pyrefly//pyrefly:extensions.bzl",
+    "pyrefly",
+    dev_dependency = True,
+)
+pyrefly.toolchain(version = "1.2.0")
+use_repo(pyrefly, "pyrefly_toolchains")
+
+register_toolchains(
+    "@pyrefly_toolchains//:all",
+    dev_dependency = True,
+)
 
 internal_dev_deps = use_extension(
     "//python/private:internal_dev_deps.bzl",
diff --git a/internal_dev_deps.bzl b/internal_dev_deps.bzl
index 7ab4717..6d4d765 100644
--- a/internal_dev_deps.bzl
+++ b/internal_dev_deps.bzl
@@ -107,6 +107,13 @@
         ],
     )
 
+    # Stub repository for rules_pyrefly in WORKSPACE mode so that load()
+    # statements for @rules_pyrefly resolve without requiring full Pyrefly.
+    local_repository(
+        name = "rules_pyrefly",
+        path = "tests/modules/rules_pyrefly_stub",
+    )
+
     # The below two deps are required for the integration test with bazel
     # gazelle. Maybe the test should be moved to the `gazelle` workspace?
     http_archive(
diff --git a/python/runfiles/BUILD.bazel b/python/runfiles/BUILD.bazel
index 7366347..4e119ed 100644
--- a/python/runfiles/BUILD.bazel
+++ b/python/runfiles/BUILD.bazel
@@ -40,6 +40,7 @@
         # to the --experimental_python_import_all_repositories setting.
         "../..",
     ],
+    tags = ["pyrefly"],
     visibility = ["//visibility:public"],
 )
 
diff --git a/python/runfiles/runfiles.py b/python/runfiles/runfiles.py
index 16afeea..6fe2e8f 100644
--- a/python/runfiles/runfiles.py
+++ b/python/runfiles/runfiles.py
@@ -323,7 +323,7 @@
         return self._as_path().is_socket()
 
     # override
-    def open(
+    def open(  # pyrefly: ignore[bad-override]
         self,
         mode: str = "r",
         buffering: int = -1,
diff --git a/tests/modules/rules_pyrefly_stub/WORKSPACE b/tests/modules/rules_pyrefly_stub/WORKSPACE
new file mode 100644
index 0000000..48a0b30
--- /dev/null
+++ b/tests/modules/rules_pyrefly_stub/WORKSPACE
@@ -0,0 +1 @@
+workspace(name = "rules_pyrefly")
diff --git a/tests/modules/rules_pyrefly_stub/pyrefly/BUILD.bazel b/tests/modules/rules_pyrefly_stub/pyrefly/BUILD.bazel
new file mode 100644
index 0000000..2f14f71
--- /dev/null
+++ b/tests/modules/rules_pyrefly_stub/pyrefly/BUILD.bazel
@@ -0,0 +1,3 @@
+package(default_visibility = ["//visibility:public"])
+
+exports_files(["pyrefly.bzl"])
diff --git a/tests/modules/rules_pyrefly_stub/pyrefly/pyrefly.bzl b/tests/modules/rules_pyrefly_stub/pyrefly/pyrefly.bzl
new file mode 100644
index 0000000..fc1a44e
--- /dev/null
+++ b/tests/modules/rules_pyrefly_stub/pyrefly/pyrefly.bzl
@@ -0,0 +1,21 @@
+"""Stub implementation of rules_pyrefly for WORKSPACE mode."""
+
+# buildifier: disable=unused-variable
+def _noop_aspect_impl(_target, _ctx):
+    return []
+
+_noop_aspect = aspect(
+    implementation = _noop_aspect_impl,
+    doc = "No-op Pyrefly aspect stub for WORKSPACE mode.",
+)
+
+def pyrefly(**_kwargs):
+    """Stub pyrefly aspect constructor.
+
+    Args:
+        **_kwargs: Ignored keyword arguments.
+
+    Returns:
+        A no-op aspect.
+    """
+    return _noop_aspect
diff --git a/tests/support/pyrefly/BUILD.bazel b/tests/support/pyrefly/BUILD.bazel
new file mode 100644
index 0000000..447af06
--- /dev/null
+++ b/tests/support/pyrefly/BUILD.bazel
@@ -0,0 +1,8 @@
+load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+
+package(default_visibility = ["//:__subpackages__"])
+
+bzl_library(
+    name = "pyrefly",
+    srcs = ["pyrefly.bzl"],
+)
diff --git a/tests/support/pyrefly/pyrefly.bzl b/tests/support/pyrefly/pyrefly.bzl
new file mode 100644
index 0000000..bcb7914
--- /dev/null
+++ b/tests/support/pyrefly/pyrefly.bzl
@@ -0,0 +1,7 @@
+"""Aspect definition for Pyrefly static type checking."""
+
+load("@rules_pyrefly//pyrefly:pyrefly.bzl", "pyrefly")
+
+pyrefly_aspect = pyrefly(
+    opt_in_tags = ["pyrefly"],
+)