Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

139 KB
Raw Blame
"""
Tests for the check-sensitive-logging pre-commit hook.

Ensures the hook detects logging of passwords, API keys, tokens, and other
sensitive data that could leak user information to log files.
"""

import ast
import sys
from importlib import import_module
from pathlib import Path

import pytest


HOOKS_DIR = Path(__file__).parent.parent.parent / ".pre-commit-hooks"
sys.path.insert(0, str(HOOKS_DIR))

hook_module = import_module("check-sensitive-logging")
SensitiveLoggingChecker = hook_module.SensitiveLoggingChecker


def _check_code(code: str, filename: str = "src/module.py") -> list:
    """Parse code and run the sensitive logging checker."""
    tree = ast.parse(code)
    checker = SensitiveLoggingChecker(filename)
    checker.visit(tree)
    return checker.errors


class TestDetectsPasswordLogging:
    """Ensures passwords are never logged."""

    def test_detects_password_in_fstring(self):
        code = 'logger.info(f"User login with password={password}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1
        assert any("password" in e.lower() for e in errors)

    def test_detects_user_password_variable(self):
        code = 'logger.info(f"DB access: {user_password}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1

    def test_detects_pwd_variable(self):
        code = 'logger.warning(f"Connection failed: pwd={pwd}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1


class TestDetectsApiKeyLogging:
    """Ensures API keys and tokens are never logged."""

    def test_detects_api_key(self):
        code = 'logger.info(f"Using API key: {api_key}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1

    def test_detects_access_token(self):
        code = 'logger.info(f"Auth with token: {access_token}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1

    def test_detects_secret_key(self):
        code = 'logger.debug(f"Secret: {secret_key}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1


class TestDetectsSensitiveDictLogging:
    """Ensures sensitive dicts (kwargs, credentials) are not logged."""

    def test_detects_kwargs_logging(self):
        code = 'logger.info(f"Calling with: {kwargs}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1

    def test_detects_credentials_logging(self):
        code = 'logger.warning(f"Auth data: {credentials}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1

    def test_detects_settings_snapshot_logging(self):
        code = 'logger.info(f"Settings: {settings_snapshot}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1

    def test_detects_connection_string_logging(self):
        code = 'logger.info(f"DB: {connection_string}")\n'
        errors = _check_code(code)
        assert len(errors) >= 1


class TestDetectsExcInfoOnWarning:
    """Ensures exc_info=True is not used on warning/error (leaks tracebacks)."""

    def test_detects_exc_info_on_warning(self):
        code = 'logger.warning("Failed", exc_info=True)\n'
        errors = _check_code(code)
        assert len(errors) >= 1
        assert any("exc_info" in e for e in errors)

    def test_detects_exc_info_on_error(self):
        code = 'logger.error("Crash", exc_info=True)\n'
        errors = _check_code(code)
        assert len(errors) >= 1

    def test_allows_exc_info_on_debug(self):
        code = 'logger.debug("Debug trace", exc_info=True)\n'
        errors = _check_code(code)
        assert len(errors) == 0


class TestDetectsExceptionVarInLog:
    """Ensures exception variables are not interpolated in non-debug logs."""

    def test_detects_exception_in_warning(self):
        code = """
try:
    do_work()
except Exception as e:
    logger.warning(f"Failed: {e}")
"""
        errors = _check_code(code)
        assert len(errors) >= 1
        assert any("exception variable" in e.lower() for e in errors)

    def test_allows_exception_in_debug(self):
        code = """
try:
    do_work()
except Exception as e:
    logger.debug(f"Failed: {e}")
"""
        errors = _check_code(code)
        # debug level is allowed
        exc_var_errors = [
            e for e in errors if "exception variable" in e.lower()
        ]
        assert len(exc_var_errors) == 0

    def test_allows_logger_exception(self):
        code = """
try:
    do_work()
except Exception as e:
    logger.exception(f"Failed: {e}")
"""
        errors = _check_code(code)
        exc_var_errors = [
            e for e in errors if "exception variable" in e.lower()
        ]
        assert len(exc_var_errors) == 0


class TestAllowsSafePatterns:
    """Ensures safe logging patterns are not flagged."""

    def test_allows_normal_string_logging(self):
        code = 'logger.info("Processing 10 results")\n'
        errors = _check_code(code)
        assert len(errors) == 0

    def test_allows_non_sensitive_variables(self):
        code = 'logger.info(f"Found {count} journals with score {score}")\n'
        errors = _check_code(code)
        assert len(errors) == 0

    def test_allows_prompt_tokens(self):
        """prompt_tokens is about LLM tokens, not auth tokens."""
        code = 'logger.info(f"Used {prompt_tokens} tokens")\n'
        errors = _check_code(code)
        assert len(errors) == 0

    def test_allows_max_tokens(self):
        code = 'logger.info(f"Max tokens: {max_tokens}")\n'
        errors = _check_code(code)
        assert len(errors) == 0

    def test_allows_test_files(self):
        """Test files should be allowed to log sensitive data for debugging."""
        code = 'logger.info(f"Testing with password={password}")\n'
        # Tests may have relaxed rules for specific vars
        # (depends on ALLOWED_LOGGING config)
        _check_code(code, filename="tests/test_auth.py")


# ---------------------------------------------------------------------------
# #4183 step 2: wrapper-import enforcement + bypass-route detection in the
# secure-logging dirs (llm/providers/, embeddings/providers/,
# web_search_engines/).
# ---------------------------------------------------------------------------

SECURE_LOGGING_DIRS = hook_module.SECURE_LOGGING_DIRS

# Representative in-dir filenames (one per protected dir) and an unprotected
# control path.
IN_DIR = "src/local_deep_research/web_search_engines/engines/search_engine_x.py"
IN_DIR_LLM = "src/local_deep_research/llm/providers/implementations/x.py"
IN_DIR_EMB = "src/local_deep_research/embeddings/providers/implementations/x.py"
OUT_DIR = "src/local_deep_research/web/routes/x.py"

WRAPPER_IMPORT = "from ...security.secure_logging import logger\n"


class TestWrapperImportEnforcement:
    """Only the secure_logging wrapper may provide 'logger' in secure dirs."""

    def test_raw_loguru_flagged_in_each_secure_dir(self):
        for filename in (IN_DIR, IN_DIR_LLM, IN_DIR_EMB):
            errors = _check_code("from loguru import logger\n", filename)
            assert any("raw loguru" in e for e in errors), filename

    def test_plain_and_aliased_loguru_import_flagged(self):
        assert _check_code("import loguru\n", IN_DIR)
        assert _check_code("import loguru as lg\n", IN_DIR)
        assert _check_code("from loguru import logger as log\n", IN_DIR)

    def test_raw_loguru_not_flagged_outside_secure_dirs(self):
        assert _check_code("from loguru import logger\n", OUT_DIR) == []
        assert _check_code("import loguru\n", OUT_DIR) == []

    def test_wrapper_import_allowed(self):
        assert _check_code(WRAPPER_IMPORT, IN_DIR) == []
        assert (
            _check_code(
                "from local_deep_research.security.secure_logging import logger\n",
                IN_DIR,
            )
            == []
        )

    def test_wrapper_import_aliased_away_from_logger_flagged(self):
        errors = _check_code(
            "from ...security.secure_logging import logger as log\n", IN_DIR
        )
        assert any("aliasing" in e for e in errors)

    def test_non_wrapper_logger_imports_flagged(self):
        for code in (
            "from local_deep_research import logger\n",
            "from ...utilities.log_utils import logger\n",
            "from helper import logger\n",
        ):
            assert _check_code(code, IN_DIR), code

    def test_suffix_lookalike_module_is_not_the_wrapper(self):
        errors = _check_code(
            "from ...notsecurity.secure_logging import logger\n", IN_DIR
        )
        assert any("other than" in e for e in errors)

    def test_secure_logging_internals_flagged(self):
        for code in (
            "from ...security.secure_logging import _loguru_logger\n",
            "from ...security.secure_logging import SecureLogger\n",
        ):
            assert _check_code(code, IN_DIR), code

    def test_secure_logging_module_import_flagged(self):
        """v4 headline bypass: module import reaches _loguru_logger."""
        for code in (
            "from ...security import secure_logging\n",
            "from local_deep_research.security import secure_logging\n",
            "import local_deep_research.security.secure_logging\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any("secure_logging module" in e for e in errors), code

    def test_log_utils_imports_flagged_in_all_forms(self):
        """v5 headline bypass: log_utils re-exports the raw loguru logger."""
        for code in (
            "from ...utilities.log_utils import logger\n",
            "from ...utilities.log_utils import config_logger\n",
            "from ...utilities import log_utils\n",
            "from local_deep_research.utilities.log_utils import config_logger\n",
            "from local_deep_research.utilities import log_utils\n",
            "import local_deep_research.utilities.log_utils as lu\n",
        ):
            assert _check_code(code, IN_DIR), code
        assert (
            _check_code("from ...utilities import log_utils\n", OUT_DIR) == []
        )

    def test_absolute_self_package_import_flagged(self):
        for code in (
            "import local_deep_research\n",
            "import local_deep_research.web\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any("root package" in e for e in errors), code
        assert _check_code("import local_deep_research\n", OUT_DIR) == []

    def test_traceback_imports_flagged(self):
        for code in (
            "import traceback\n",
            "from traceback import format_exc\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any("traceback" in e for e in errors), code
        assert _check_code("import traceback\n", OUT_DIR) == []

    def test_function_local_import_flagged(self):
        code = "def f():\n    from loguru import logger\n"
        assert _check_code(code, IN_DIR)

    def test_dynamic_loguru_import_flagged(self):
        code = "import importlib\nx = importlib.import_module('loguru')\n"
        errors = _check_code(code, IN_DIR)
        assert any("dynamic import" in e for e in errors)

    def test_dynamic_import_of_other_modules_allowed(self):
        # The real auto_discovery.py pattern: f-string first arg, not "loguru"
        code = (
            "import importlib\n"
            "m = importlib.import_module(\n"
            "    f'.implementations.{module_name}', package='pkg'\n"
            ")\n"
        )
        assert _check_code(code, IN_DIR) == []


class TestLoggerRebindingBans:
    """The local name 'logger' is reserved for the wrapper import."""

    def test_assignment_rebinding_flagged(self):
        for code in (
            "logger = get_raw()\n",
            "logger, x = a(), None\n",
            "[logger, x] = pair\n",
            "logger: object = get_raw()\n",
            "(logger := get_raw())\n",
            "logger += 1\n",
        ):
            assert _check_code(code, IN_DIR), code

    def test_control_flow_shadowing_flagged(self):
        for code in (
            "for logger in xs:\n    pass\n",
            "with open('f') as logger:\n    pass\n",
            "try:\n    f()\nexcept Exception as logger:\n    pass\n",
        ):
            assert _check_code(code, IN_DIR), code

    def test_parameters_named_logger_flagged(self):
        for code in (
            "def f(logger):\n    pass\n",
            "async def f(logger):\n    pass\n",
            "g = lambda logger: 1\n",
        ):
            assert _check_code(code, IN_DIR), code

    def test_import_binding_logger_flagged(self):
        for code in (
            "import foo as logger\n",
            "from foo import bar as logger\n",
        ):
            assert _check_code(code, IN_DIR), code

    def test_deriving_new_name_from_logger_flagged(self):
        for code in (
            "log = logger\n",
            "log = logger.bind(x=1)\n",
            "log = pkg.logger.bind(x=1)\n",
            "log = pkg.logger.patch(f).bind(x=1)\n",
            "x = log_utils.logger\n",
            "x = ldr.logger\n",
        ):
            assert _check_code(code, IN_DIR), code

    def test_ordinary_assignments_allowed(self):
        for code in (
            "results = engine.run(q)\n",
            "log = get_audit_trail()\n",
            "safe_msg = scrub(e)\n",
        ):
            assert _check_code(code, IN_DIR) == [], code


class TestBypassRouteDetection:
    """.opt/.catch/.patch/.logger/private handles/sys.exc_info are banned."""

    def test_opt_flagged_in_all_forms(self):
        for code in (
            "logger.opt(exception=True).error('x')\n",
            "logger.opt(**kwargs).error('x')\n",
            "raw = logger.opt(lazy=True)\n",
            "logger.bind(x=1).opt(depth=1)\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any(".opt" in e for e in errors), code

    def test_opt_not_flagged_outside_secure_dirs(self):
        assert (
            _check_code("logger.opt(exception=True).error('x')\n", OUT_DIR)
            == []
        )

    def test_catch_flagged_in_all_forms(self):
        for code in (
            "@logger.catch\ndef f():\n    pass\n",
            "with logger.catch():\n    pass\n",
            "h = logger.catch\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any(".catch" in e for e in errors), code

    def test_catch_decorator_with_args_reports_exactly_once(self):
        errors = _check_code(
            "@logger.catch(reraise=True)\ndef f():\n    pass\n", IN_DIR
        )
        assert len(errors) == 1

    def test_patch_flagged_in_all_forms(self):
        for code in (
            "logger.patch(lambda record: None).error('x')\n",
            "logger.bind(x=1).patch(lambda record: None).error('x')\n",
            "raw = logger.patch(lambda record: None)\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any(".patch" in e for e in errors), code

    def test_getattr_dodges_flagged(self):
        # Fail closed: opt/catch/patch are banned on ANY receiver (a bare
        # `obj` name here) unless the receiver is provably not a logger —
        # see test_getattr_patch_dodge_allowed_on_safe_receiver_only for the
        # single narrow exception.
        for attr in (
            "opt",
            "catch",
            "patch",
            "logger",
            "_logger",
            "_loguru_logger",
            "__getattr__",
        ):
            code = f"getattr(obj, '{attr}')\n"
            errors = _check_code(code, IN_DIR)
            assert any("getattr" in e for e in errors), attr

    def test_getattr_patch_dodge_never_exempt(self):
        # getattr(requests, "patch") passes the module object as a value
        # to a callable this rule cannot prove is the builtin (``from x
        # import getattr``), which revokes the requests/httpx exemption
        # (see SAFE_PATCH_RECEIVER_MODULES, condition 6) — so no
        # receiver is exempted from the getattr(..., "patch") ban,
        # including `requests` when it is NOT actually imported.
        for code in (
            'import requests\ngetattr(requests, "patch")("u")\n',
            "import requests\n"
            "from .helpers import getattr\n"
            'getattr(requests, "patch")("u")\n',
            'getattr(obj, "patch")\n',
            # "requests" the bare name, but never imported: not provably
            # safe — could be a parameter holding a logger.
            'getattr(requests, "patch")\n',
        ):
            errors = _check_code(code, IN_DIR)
            assert any('getattr(..., "patch")' in e for e in errors), code

    def test_attribute_ban_flagged_on_generic_receiver(self):
        # Fail closed: opt/catch are receiver-blind (no concrete,
        # nameable false positive was ever found for them), and patch is
        # banned on any receiver that isn't the narrow requests/httpx case.
        for code in (
            "obj.catch()\n",
            "obj.opt()\n",
            "obj.patch()\n",
            "requests.patch('https://x')\n",  # no `import requests` present
        ):
            errors = _check_code(code, IN_DIR)
            assert errors, code

    def test_patch_allowed_on_safe_requests_httpx_receiver_only(self):
        # The one legitimate false positive #4976 named: bare, genuinely
        # imported `requests`/`httpx` making an HTTP PATCH call.
        for code in (
            "import requests\nrequests.patch('https://x')\n",
            "import httpx\nhttpx.patch('https://x')\n",
        ):
            assert _check_code(code, IN_DIR) == [], code

    def test_patch_safe_receiver_exception_does_not_cover_lookalikes(self):
        # Aliased imports, non-bare receivers, and other modules named
        # "patch" are NOT exempted — only the exact bare
        # requests/httpx-imported-unaliased shape is.
        for code in (
            "import requests as req\nreq.patch('u')\n",
            "from unittest import mock\nmock.patch('os.getenv')\n",
            "import requests\nsession.patch('u')\n",  # unrelated receiver
        ):
            errors = _check_code(code, IN_DIR)
            assert errors, code

    def test_patch_safe_receiver_closed_when_name_is_a_parameter(self):
        # The class of bug #4998 introduced: a logger reaching a banned
        # attribute through a differently-named receiver. If "requests" is
        # ever used as a parameter anywhere in the file, the name can no
        # longer be trusted to be the imported module — the exception must
        # not apply, even though `import requests` is also present.
        code = (
            "import requests\n"
            "def helper(requests):\n"
            "    requests.patch('boom')\n"
            "helper(logger)\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".patch" in e for e in errors), errors

    def test_patch_safe_receiver_closed_when_name_is_rebound(self):
        # The imported-client exemption used to
        # survive every rebinding that was not a function parameter.
        # ANY binding form that can hand the imported name to a logger
        # before the call site must close it — factory or container
        # assignment, annotated/augmented assignment, loop, comprehension,
        # with-as, except-as, walrus, match capture, nested/aliased/star
        # imports, del, and same-named def/class bindings.
        for code in (
            "import requests\n"
            "requests = get_logger()\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            "requests = holders[0]\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            "requests: Any = get_logger()\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            "requests += extras\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            "for requests in holders:\n"
            "    requests.patch(lambda r: r)\n",
            "import httpx\n"
            "async def f():\n"
            "    async for httpx in holders:\n"
            "        httpx.patch(lambda r: r)\n",
            "import requests\n"
            "out = [requests.patch(r) for requests in holders]\n",
            "import requests\n"
            "with get_ctx() as requests:\n"
            "    requests.patch(lambda r: r)\n",
            "import httpx\n"
            "async def f():\n"
            "    async with get_ctx() as httpx:\n"
            "        httpx.patch(lambda r: r)\n",
            "import requests\n"
            "if (requests := get_logger()):\n"
            "    requests.patch(lambda r: r)\n",
            "import requests\n"
            "try:\n"
            "    f()\n"
            "except Exception as requests:\n"
            "    requests.patch(lambda r: r)\n",
            "import requests\n"
            "match get_logger():\n"
            "    case requests:\n"
            "        requests.patch(lambda r: r)\n",
            "import requests\n"
            "def f():\n"
            "    import provider as requests\n"
            "    requests.patch(lambda r: r)\n",
            "import requests\n"
            "from helpers import requests\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            "from helpers import *\n"
            "requests.patch(lambda r: r)\n",
            "import requests\ndel requests\nrequests.patch(lambda r: r)\n",
            "import requests\n"
            "class requests:\n"
            "    pass\n"
            "requests.patch(lambda r: r)\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any(".patch" in e for e in errors), code

    def test_getattr_patch_dodge_closed_when_safe_receiver_is_rebound(self):
        # The same closure through the literal-getattr route: once the
        # name is rebound, getattr(httpx, "patch") is no longer provably
        # an HTTP-client call and must stay banned.
        code = (
            "import httpx\n"
            "httpx = get_logger()\n"
            'getattr(httpx, "patch")(lambda r: r)\n'
        )
        errors = _check_code(code, IN_DIR)
        assert any('getattr(..., "patch")' in e for e in errors), errors

    def test_patch_safe_receiver_closed_by_dynamic_namespace_rebinding(self):
        for code in (
            "import requests\n"
            "globals()[name] = get_logger()\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            'globals()["requests"] = get_logger()\n'
            "requests.patch(lambda r: r)\n",
            "import httpx\n"
            "globals().update(httpx=get_logger())\n"
            'getattr(httpx, "patch")(lambda r: r)\n',
            "import sys\n"
            "import requests\n"
            'setattr(sys.modules[__name__], "requests", get_logger())\n'
            "requests.patch(lambda r: r)\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), code

    def test_patch_safe_receiver_closed_by_ordinary_namespace_mutation(self):
        for code in (
            "import sys\n"
            "import requests\n"
            "sys.modules[__name__].requests = get_logger()\n"
            "requests.patch(lambda r: r)\n",
            "import sys as s\n"
            "import requests\n"
            "s.modules[__name__].requests = get_logger()\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            "g = globals\n"
            "g()['requests'] = get_logger()\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            "g = globals\n"
            "g().update(requests=get_logger())\n"
            "requests.patch(lambda r: r)\n",
            "import requests\n"
            "dict.update(globals(), requests=get_logger())\n"
            "requests.patch(lambda r: r)\n",
            "import sys\n"
            "import requests\n"
            "sa = setattr\n"
            "sa(sys.modules[__name__], 'requests', get_logger())\n"
            "requests.patch(lambda r: r)\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), code

    def test_getattr_aliases_and_alias_chains_are_checked_fail_closed(self):
        for code in (
            "ga = getattr\nga(logger, 'patch')(lambda r: r)\n",
            "ga = getattr\nlookup = ga\nlookup(logger, 'patch')(lambda r: r)\n",
            "import builtins as b\nga = b.getattr\nga(logger, 'patch')()\n",
            "from builtins import getattr as ga\nga(logger, 'patch')()\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any('getattr(..., "patch")' in error for error in errors), (
                code
            )

    def test_getattr_module_and_static_alias_forms_checked_fail_closed(self):
        for code in (
            "import builtins\nbuiltins.getattr(logger, 'patch')(lambda r: r)\n",
            "import builtins as b\nb.getattr(logger, 'patch')(lambda r: r)\n",
            "if (ga := getattr):\n    ga(logger, 'patch')(lambda r: r)\n",
            "ga, pr = getattr, print\nga(logger, 'patch')(lambda r: r)\n",
            "def f(ga=getattr):\n    ga(logger, 'patch')(lambda r: r)\n",
            "ga = getattr if flag else print\n"
            "ga(logger, 'patch')(lambda r: r)\n",
            "h = Holder()\n"
            "h.ga = getattr\n"
            "h.ga(logger, 'patch')(lambda r: r)\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any('getattr(..., "patch")' in error for error in errors), (
                code
            )

    def test_getattr_alias_rebind_and_parameter_shadow_are_allowed(self):
        for code in (
            "ga = getattr\nga = api_call\nga(obj, 'patch')\n",
            "ga = getattr\ndef f(ga):\n    ga(obj, 'patch')\n",
            "import builtins as b\n"
            "b = api_call\n"
            "ga = b.getattr\n"
            "ga(obj, 'patch')\n",
            "import builtins as b\nb = api_call\nb.getattr(obj, 'patch')\n",
        ):
            assert _check_code(code, IN_DIR) == [], code

    def test_patch_safe_receiver_survives_unrelated_rebinding(self):
        # Rebinding a DIFFERENT name must not close the exemption: the
        # whole-file invalidation is keyed on the safe name itself.
        code = (
            "import requests\n"
            "other = get_logger()\n"
            "for other in holders:\n"
            "    pass\n"
            "requests.patch('https://x')\n"
        )
        assert _check_code(code, IN_DIR) == [], code

    def test_patch_safe_receiver_rebinding_is_whole_file_fail_closed(self):
        # The invalidation is deliberately whole-file, matching the
        # parameter rule it extends: one rebinding anywhere closes the
        # exemption for every call site in the file, including genuine
        # HTTP calls in unrelated functions. An inline suppression is
        # the escape hatch, not a scope-aware allowlist.
        code = (
            "import requests\n"
            "def helper():\n"
            "    requests = get_logger()\n"
            "    requests.patch(lambda r: r)\n"
            "def genuine():\n"
            "    requests.patch('https://x')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".patch" in e for e in errors), errors

    def test_attribute_ban_flagged_on_indirect_logger_receiver(self):
        # Actual logger receivers must still trip the opt/catch/patch ban:
        # ``self.logger``, ``cls.logger``, ``pkg.logger`` and bind chains
        # rooted at them.
        for code in (
            "self.logger.opt(exception=True).error('x')\n",
            "cls.logger.catch()\n",
            "pkg.logger.patch(lambda r: None)\n",
            "self.logger.bind(x=1).opt(depth=1)\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any(
                ".opt" in e or ".catch" in e or ".patch" in e for e in errors
            ), code

    def test_attribute_ban_flagged_on_getattr_bind_chain(self):
        # Any getattr-obfuscated bind chain stays banned for opt/catch
        # (receiver-blind) and for patch (the receiver is a Call, never
        # the bare safe-module Name, so it fails closed too — even when
        # nominally rooted at ``requests``).
        for code in (
            'getattr(logger, "bind")(x=1).opt(exception=True).error("x")\n',
            'getattr(self.logger, "bind")(x=1).patch(lambda r: None)\n',
            'getattr(logger, "bind")(a=1).bind(b=2).catch()\n',
            'import requests\ngetattr(requests, "bind")(x=1).patch("u")\n',
        ):
            errors = _check_code(code, IN_DIR)
            assert any(
                ".opt" in e or ".catch" in e or ".patch" in e for e in errors
            ), code

    def test_logger_attribute_access_flagged(self):
        for code in (
            "something.logger.exception('x')\n",
            "x = pkg.logger\n",
            "f(log_utils.logger)\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any(".logger" in e for e in errors), code

    def test_logger_attribute_assignment_reports_once(self):
        errors = _check_code("x = pkg.logger\n", IN_DIR)
        assert len(errors) == 1
        assert ".logger" in errors[0]

    def test_private_handle_access_flagged(self):
        for code in (
            "logger._logger\n",
            "secure_logging._loguru_logger\n",
            "logger.__getattr__('exception')\n",
        ):
            assert _check_code(code, IN_DIR), code

    def test_sys_exc_info_flagged(self):
        for code in (
            "import sys\nx = sys.exc_info()\n",
            "import sys as s\nx = s.exc_info()\n",
            "import sys.monitoring\nx = sys.exc_info()\n",
            "import sys\nx = getattr(sys, 'exc_info')\n",
            "import sys as s\nx = getattr(s, 'exc_info')\n",
            "from sys import exc_info\nx = exc_info()\n",
            "from sys import exc_info as get_exc_info\nx = get_exc_info()\n",
            "from sys import *\nx = exc_info()\n",  # noqa: F403
            "x = __import__('sys').exc_info()\n",
            "x = getattr(__import__('sys'), 'exc_info')()\n",
            "import importlib\nx = importlib.import_module('sys').exc_info()\n",
            "from importlib import import_module\nx = import_module('sys').exc_info()\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any("exc_info" in e for e in errors), code

    def test_sys_alias_does_not_leak_between_function_scopes(self):
        code = (
            "def f():\n"
            "    import sys as s\n"
            "    return s.exc_info()\n"
            "\n"
            "def g(s):\n"
            "    return s.exc_info()\n"
        )
        errors = [e for e in _check_code(code, IN_DIR) if "exc_info" in e]
        assert len(errors) == 1

    def test_sys_alias_shadowing_shapes_not_flagged(self):
        cases = [
            (
                "import sys as s\n"
                "def g():\n"
                "    s = obj\n"
                "    return s.exc_info()\n"
            ),
            (
                "import sys as s\n"
                "def g():\n"
                "    import something as s\n"
                "    return s.exc_info()\n"
            ),
            (
                "import sys as s\n"
                "def g():\n"
                "    try:\n"
                "        raise RuntimeError()\n"
                "    except Exception as s:\n"
                "        return s.exc_info()\n"
            ),
            (
                "class C:\n"
                "    import sys as s\n"
                "    def m(self):\n"
                "        return s.exc_info()\n"
            ),
        ]
        for code in cases:
            errors = [e for e in _check_code(code, IN_DIR) if "exc_info" in e]
            assert errors == [], code

    def test_non_logger_exception_call_allowed(self):
        # tenacity pattern from rate_limiting/llm/wrapper.py:34
        code = "exc = retry_state.outcome.exception()\n"
        assert _check_code(code, IN_DIR) == []

    def test_dunder_getattr_method_definition_allowed(self):
        # shape of rate_limiting/llm/wrapper.py's attribute pass-through:
        # bans target Attribute access / literal getattr, not FunctionDef
        code = (
            "class W:\n"
            "    def __getattr__(self, name):\n"
            "        return object.__getattribute__(self, name)\n"
        )
        assert _check_code(code, IN_DIR) == []


class TestReceiverObfuscationClasses:
    """#4998 regression: a receiver-narrowing allowlist reopened the exact
    false-negative class the opt/catch/patch ban exists to close — a logger
    reaching a banned attribute through ANY receiver shape other than the
    handful of literal patterns an allowlist happened to name. These cover
    the CLASS (parameter passing, non-``logger``-named attributes, dict/list
    elements, ternaries, multi-hop aliasing), not just the two examples that
    slipped through review. Direction coverage is asymmetric by design:
    real loggers must be flagged, and genuine non-loggers must stay
    usable for method names the ban does not name at all (``.get``);
    a genuine non-logger calling an identically-named banned method
    through a non-provably-safe receiver stays blocked — that
    fail-closed tradeoff is pinned explicitly by
    test_requests_httpx_patch_still_flagged_through_ternary_or_param
    """

    # -- must be flagged: a logger reaches a banned attribute through an
    # obfuscated receiver ---------------------------------------------------

    def test_logger_passed_as_differently_named_parameter_flagged(self):
        code = (
            "def helper(log_obj):\n"
            "    log_obj.opt(exception=True).error('boom')\n"
            "helper(logger)\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".opt" in e for e in errors), errors

    def test_logger_stored_on_non_logger_named_attribute_flagged(self):
        code = (
            "class Foo:\n"
            "    def __init__(self, lg):\n"
            "        self.lg = lg\n"
            "    def go(self):\n"
            "        self.lg.catch()(func)\n"
            "Foo(logger).go()\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".catch" in e for e in errors), errors

    def test_logger_in_dict_element_flagged(self):
        code = (
            "handlers = {'main': logger}\n"
            "handlers['main'].opt(exception=True).error('boom')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".opt" in e for e in errors), errors

    def test_logger_in_list_element_flagged(self):
        code = "handlers = [logger]\nhandlers[0].catch()(func)\n"
        errors = _check_code(code, IN_DIR)
        assert any(".catch" in e for e in errors), errors

    def test_logger_selected_by_ternary_flagged(self):
        code = (
            "def pick(flag, other):\n"
            "    chosen = logger if flag else other\n"
            "    chosen.opt(exception=True).error('boom')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".opt" in e for e in errors), errors

    def test_logger_aliased_through_several_assignments_flagged(self):
        # Direct `x = logger` derivation is banned on the first hop, but the
        # attribute access on the further-aliased name must ALSO be caught
        # independently — belt and suspenders against a hop the derivation
        # check itself might miss.
        code = (
            "class Wrapper:\n"
            "    def __init__(self):\n"
            "        self.handle = logger\n"
            "    def emit(self):\n"
            "        h = self.handle\n"
            "        h2 = h\n"
            "        h2.patch(lambda r: r)('boom')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".patch" in e for e in errors), errors

    def test_logger_returned_from_call_then_chained_flagged(self):
        # Receiver is itself a Call (e.g. a factory/getter) rather than any
        # recognisable name shape at all.
        code = "get_logger().opt(exception=True).error('boom')\n"
        errors = _check_code(code, IN_DIR)
        assert any(".opt" in e for e in errors), errors

    # -- must NOT be flagged: no logger is anywhere in the picture ---------

    def test_non_logger_passed_as_parameter_not_flagged_for_bind(self):
        # Sanity check for the "both directions" requirement using a shape
        # that never touches a banned attribute name in the first place.
        code = (
            "def helper(session):\n"
            "    session.get('https://x')\n"
            "helper(requests.Session())\n"
        )
        assert _check_code(code, IN_DIR) == [], code

    def test_non_logger_dict_and_list_elements_not_flagged_absent_ban(self):
        code = (
            "sessions = {'main': make_session()}\n"
            "sessions['main'].get('https://x')\n"
            "queue = [make_session()]\n"
            "queue[0].get('https://x')\n"
        )
        assert _check_code(code, IN_DIR) == [], code

    def test_requests_httpx_patch_still_flagged_through_ternary_or_param(self):
        # The narrow, tracked-as-safe exception (SAFE_PATCH_RECEIVER_MODULES)
        # is a bare, genuinely-imported, never-shadowed name — it does not
        # extend through indirection. A ternary or parameter hop around
        # requests/httpx therefore stays banned even though no logger is
        # actually involved; that is the accepted, documented fail-closed
        # tradeoff. There is no inline suppression for ``.patch`` — the
        # remedy is to call through the bare imported module name
        # directly (``requests.patch(...)``/``httpx.patch(...)``) or to
        # move the call out of a secure logging directory.
        code = (
            "import requests\n"
            "def call(flag, other):\n"
            "    client = requests if flag else other\n"
            "    client.patch('https://x')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".patch" in e for e in errors), errors


class TestAttributeAndScopeAliasSoundness:
    """Attribute-held
    getattr aliases must be tracked per receiver, scope, and execution
    order with transitive propagation; any module-namespace spelling
    revokes the requests/httpx .patch exemption; and definite
    rebindings must clear getattr aliases in If tests, finally suites,
    and enclosing scopes without contributor-blocking false positives.
    """

    def test_attribute_alias_tracked_per_receiver(self):
        # Another receiver's definite rebind of the same attribute
        # text must not erase a live alias: the timeline is keyed by
        # receiver plus attribute, not attribute text alone.
        code = (
            "class H:\n"
            "    pass\n"
            "a = H()\n"
            "b = H()\n"
            "a.ga = getattr\n"
            "b.ga = print\n"
            'a.ga(logger, "patch")(lambda r: r)\n'
        )
        errors = _check_code(code, IN_DIR)
        assert any('getattr(..., "patch")' in e for e in errors), errors

    def test_attribute_alias_same_receiver_definite_rebind_allowed(self):
        # The definite clear on the SAME receiver retires the alias;
        # pins the false-positive direction of the receiver keying.
        code = (
            'h = Holder()\nh.ga = getattr\nh.ga = print\nh.ga(obj, "patch")\n'
        )
        assert _check_code(code, IN_DIR) == [], code

    def test_attribute_alias_propagates_through_name(self):
        # Transitive propagation: a name bound to an attribute-held
        # alias (``lookup = h.ga``) is itself a getattr alias.
        code = (
            "h = Holder()\n"
            "h.ga = getattr\n"
            "lookup = h.ga\n"
            'lookup(logger, "patch")(lambda r: r)\n'
        )
        errors = _check_code(code, IN_DIR)
        assert any('getattr(..., "patch")' in e for e in errors), errors

    def test_attribute_alias_respects_function_execution_order(self):
        # install() runs before use(), so the alias is live inside
        # use() even though the installing assignment textually
        # follows the call site it protects.
        code = (
            "h = Holder()\n"
            "def use():\n"
            '    h.ga(logger, "patch")(lambda r: r)\n'
            "def install():\n"
            "    h.ga = getattr\n"
            "install()\n"
            "use()\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any('getattr(..., "patch")' in e for e in errors), errors

    def test_class_body_alias_binding_covers_instances(self):
        # ``class H: ga = getattr`` puts the alias on every instance;
        # the instance's class cannot be recovered statically, so any
        # ``self.ga`` may reach it.
        code = (
            "class H:\n"
            "    ga = getattr\n"
            "    def use(self):\n"
            '        self.ga(logger, "patch")(lambda r: r)\n'
        )
        errors = _check_code(code, IN_DIR)
        assert any('getattr(..., "patch")' in e for e in errors), errors

    def test_class_body_alias_definite_rebind_retires_it(self):
        # A definite later rebind in the same class body retires the
        # instance-visible alias; pins the false-positive direction
        # of the class-body wildcard.
        code = (
            "class H:\n"
            "    ga = getattr\n"
            "    ga = print\n"
            "    def use(self):\n"
            '        self.ga(obj, "patch")\n'
        )
        assert _check_code(code, IN_DIR) == [], code

    def test_safe_receiver_closed_by_builtins_globals_alias(self):
        # ``g = builtins.globals`` is a realistic alias spelling the
        # old bare-Name propagation missed.
        code = (
            "import builtins\n"
            "import requests\n"
            "g = builtins.globals\n"
            'g()["requests"] = get_logger()\n'
            "requests.patch(lambda r: r)\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".patch" in e for e in errors), errors

    def test_safe_receiver_closed_by_globals_setitem(self):
        code = (
            "import requests\n"
            'globals().__setitem__("requests", get_logger())\n'
            "requests.patch(lambda r: r)\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".patch" in e for e in errors), errors

    def test_safe_receiver_closed_by_tuple_created_globals_alias(self):
        # Tuple destructuring created the alias; only the element-wise
        # value tracking recognizes it.
        code = (
            "import requests\n"
            "g, other = globals, None\n"
            'g()["requests"] = get_logger()\n'
            "requests.patch(lambda r: r)\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any(".patch" in e for e in errors), errors

    def test_stale_globals_alias_still_revokes_patch_exemption(self):
        # At runtime g no longer is the globals builtin when the
        # subscript runs, so this particular file is harmless. The
        # exemption deliberately does not model that: deciding it
        # needed execution-order reasoning that loops, starred
        # arguments and indirect call sites kept defeating. Any
        # ``globals`` spelling revokes it for the whole file, and the
        # diagnostic names the spelling so the author can see why.
        code = (
            "import requests\n"
            "g = globals\n"
            "g = lambda: {}\n"
            'g()["requests"] = get_logger()\n'
            'requests.patch("https://example.test")\n'
        )
        errors = _check_code(code, IN_DIR)
        assert any(
            ":5: .patch is banned" in e
            and "exemption is revoked for this file by globals on line 2" in e
            for e in errors
        ), errors

    def test_global_alias_install_flagged(self):
        # ``global ga`` inside install() writes the module binding, so
        # the module-level call after install() sees the alias.
        code = (
            "ga = print\n"
            "def install():\n"
            "    global ga\n"
            "    ga = getattr\n"
            "install()\n"
            'ga(logger, "patch")(lambda r: r)\n'
        )
        errors = _check_code(code, IN_DIR)
        assert any('getattr(..., "patch")' in e for e in errors), errors

    def test_nonlocal_alias_install_flagged(self):
        # ``nonlocal ga`` writes outer()'s binding, so the call inside
        # outer() after install() sees the alias.
        code = (
            "def outer():\n"
            "    ga = print\n"
            "    def install():\n"
            "        nonlocal ga\n"
            "        ga = getattr\n"
            "    install()\n"
            '    ga(logger, "patch")(lambda r: r)\n'
            "outer()\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any('getattr(..., "patch")' in e for e in errors), errors

    def test_walrus_in_if_test_definite_rebind_allowed(self):
        # The If test always executes before the body and any
        # following statement, so its walrus rebind is definite.
        code = 'ga = getattr\nif (ga := print):\n    pass\nga(obj, "patch")\n'
        assert _check_code(code, IN_DIR) == [], code

    def test_with_body_rebind_is_conditional(self):
        # A ``with`` body does not always run to completion — a context
        # manager such as ``contextlib.suppress`` can swallow the
        # exception raised partway through it, so the rebind here never
        # executes. The bare ``getattr`` is never retired by any rebind;
        # the ``ga`` alias pins the ``With: body`` conditional row.
        for name in ("getattr", "ga"):
            code = (
                "import contextlib\n"
                "ga = getattr\n"
                "with contextlib.suppress(AttributeError):\n"
                "    y = object().missing\n"
                f"    {name} = str\n"
                f"x = {name}(logger, 'opt')\n"
            )
            errors = _check_code(code, IN_DIR)
            assert any('getattr(..., "opt")' in e for e in errors), code

    def test_with_body_globals_rebind_does_not_arm_patch_exemption(self):
        # Same non-execution as above, but through the globals()
        # namespace-mutation channel: ``globals = lambda: {}`` inside
        # the suppressed body never runs, so ``globals()`` at module
        # level is still the real builtin and its subscript write
        # rebinds the module's ``requests`` name to a logger before
        # ``requests.patch(...)`` is reached — the full SAFE_PATCH_
        # RECEIVER_MODULES bypass chain from B1.
        # The ``g`` alias variant pins the same row for aliases, since
        # the bare ``globals`` is never retired by any rebind.
        for name in ("globals", "g"):
            code = (
                "import contextlib\n"
                "class FakeLogger:\n"
                "    def patch(self, *a, **k): return 'RAW-LOGURU-PATCH'\n"
                "def get_logger(): return FakeLogger()\n"
                "import requests\n"
                "g = globals\n"
                "with contextlib.suppress(AttributeError):\n"
                "    y = object().missing\n"
                f"    {name} = lambda: {{}}\n"
                f"{name}()['requests'] = get_logger()\n"
                "requests.patch('u')\n"
            )
            errors = _check_code(code, IN_DIR)
            assert any(".patch" in e for e in errors), code

    def test_assert_walrus_rebind_is_conditional(self):
        # ``assert`` statements (both test and msg) are stripped under
        # ``-O``, so a walrus rebind inside one is never definite — it
        # must not retire a globals alias, or the namespace-mutation
        # write below silently escapes the ban. (The bare ``globals``
        # is never retired by any rebind; ``g`` pins the Assert row.)
        for name in ("globals", "g"):
            code = (
                "import requests\n"
                "g = globals\n"
                f"assert ({name} := (lambda: {{}}))\n"
                f"{name}()['requests'] = get_logger()\n"
                "requests.patch('u')\n"
            )
            errors = _check_code(code, IN_DIR)
            assert any(".patch" in e for e in errors), code

    def test_finally_definite_rebind_allowed(self):
        # The finally suite always runs before any statement that
        # follows the try, so its rebind is definite.
        code = (
            "ga = getattr\n"
            "try:\n"
            "    pass\n"
            "finally:\n"
            "    ga = print\n"
            'ga(obj, "patch")\n'
        )
        assert _check_code(code, IN_DIR) == [], code

    def test_enclosing_scope_definite_rebind_allowed(self):
        # Both module-level rebinds precede use's definition, so the
        # free name inside use() resolves to print at every call
        # time — an enclosing-scope definite clear must be honored.
        code = (
            "ga = getattr\n"
            "ga = print\n"
            "def use():\n"
            '    ga(obj, "patch")\n'
            "use()\n"
        )
        assert _check_code(code, IN_DIR) == [], code


class TestBindingEvaluationAndNamespaceObjects:
    """Builtin lookups follow evaluation scope and namespace writes."""

    def test_assignment_reads_rhs_before_binding(self):
        cases = {
            "rhs_before_binding": 'getattr = getattr(obj, "patch")\ngetattr()\n',
            "annotated_rhs": 'getattr: object = getattr(obj, "patch")\n',
            "builtin_named_definition_default": 'def getattr(x=getattr(obj,"patch")):\n    pass\n',
            "builtin_named_class_base": 'class getattr(getattr(obj,"patch")):\n    pass\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), (name, errors)

    def test_deferred_outer_clears_do_not_hide_aliases(self):
        cases = {
            "uncalled_global_clear": 'def unused():\n    global getattr\n    getattr = lambda *a: None\ngetattr(obj, "patch")()\n',
            "nonlocal_clear": 'def outer():\n    ga=getattr\n    def unused():\n        nonlocal ga\n        ga=print\n    ga(obj,"patch")\n',
            "global_rhs_local_alias": 'def install():\n    global ga\n    source=getattr\n    ga=source\ninstall()\nga(obj,"patch")\n',
            "global_namespace_clear_deferred": 'import requests\ndef unused():\n    global globals\n    globals=lambda:{}\nglobals()["requests"]=candidate\nrequests.patch()\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), (name, errors)

    def test_definition_expressions_use_enclosing_scope(self):
        cases = {
            "default_builtin_parameter": 'def f(getattr, value=getattr(obj, "patch")):\n    pass\n',
            "keyword_default_builtin_parameter": 'def f(*, getattr=None, value=getattr(obj, "patch")):\n    pass\n',
            "annotation_builtin_parameter": 'def f(getattr: getattr(obj, "patch")):\n    pass\n',
            "decorator_outer_alias": 'ga = getattr\n@decorate(ga(obj, "patch"))\ndef f(ga):\n    pass\n',
            "default_outer_alias": 'ga = getattr\ndef f(ga, value=ga(obj, "patch")):\n    pass\n',
            "lambda_default_builtin_parameter": 'f = lambda getattr, value=getattr(obj, "patch"): None\n',
            "class_base_shadow": 'ga = getattr\nclass C(ga(obj, "patch")):\n    ga = print\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), (name, errors)

    def test_namespace_objects_and_dynamic_targets_invalidate_http_receiver(
        self,
    ):
        cases = {
            "namespace_object": 'import requests\nnamespace = globals()\nnamespace["requests"] = candidate\nrequests.patch()\n',
            "module_object": "import sys\nimport requests\nmodule = sys.modules[__name__]\nmodule.requests = candidate\nrequests.patch()\n",
            "module_locals": 'import requests\nlocals()["requests"] = candidate\nrequests.patch()\n',
            "loop_dynamic_target": 'import requests\nfor globals()["requests"] in [candidate]:\n    requests.patch()\n',
            "with_target": 'import requests\nwith resource as globals()["requests"]:\n    requests.patch()\n',
            "comprehension_target": 'import requests\n[None for globals()["requests"] in values]\nrequests.patch()\n',
            "namespace_update": "import requests\nns=globals()\nns.update(requests=candidate)\nrequests.patch()\n",
            "namespace_dunder": 'import requests\nns=globals()\nns.__setitem__("requests",candidate)\nrequests.patch()\n',
            "module_dict_alias": 'import sys\nimport requests\nm=sys.modules[__name__]\nns=m.__dict__\nns["requests"]=candidate\nrequests.patch()\n',
            "module_alias_setattr": 'import sys\nimport requests\nm=sys.modules[__name__]\nsetattr(m,"requests",candidate)\nrequests.patch()\n',
            "module_locals_alias": 'import requests\nf=locals\nns=f()\nns["requests"]=candidate\nrequests.patch()\n',
            "imported_locals": 'from builtins import locals as get_ns\nimport requests\nget_ns()["requests"]=candidate\nrequests.patch()\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), (name, errors)

    def test_namespace_vocabulary_revokes_http_receiver_unconditionally(
        self,
    ):
        # These writes miss the module's ``requests`` at runtime (another
        # key, a rebound alias, a function's locals), but the exemption
        # no longer reasons about which namespace a write reaches: any
        # namespace-reaching spelling revokes it for the whole file, so
        # each is flagged exactly as on main. Only the vocabulary-free
        # file keeps the exemption.
        cases = {
            "unrelated_namespace_key": 'import requests\nns=globals()\nns["unrelated"] = candidate\nrequests.patch()\n',
            "cleared_namespace_function": 'import requests\ng=globals\ng=lambda: {}\ng()["requests"] = candidate\nrequests.patch()\n',
            "namespace_rebound": 'import requests\nns=globals()\nns={}\nns["requests"]=candidate\nrequests.patch()\n',
            "module_alias_rebound": "import sys\nimport requests\nm=sys.modules[__name__]\nm=other\nm.requests=candidate\nrequests.patch()\n",
            "function_locals_control": 'import requests\ndef f():\n    ns=locals()\n    ns["requests"]=candidate\nrequests.patch()\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any(".patch is banned" in e for e in errors), (name, errors)
        genuine = 'import requests\nrequests.patch("synthetic")\n'
        assert _check_code(genuine, IN_DIR) == [], genuine


class TestDeletionFallbackAndNamespaceMutation:
    """Deletion fallback and identity-preserving namespace updates."""

    def test_deleted_names_resolve_correct_fallback(self):
        cases = {
            "delete_builtin_restore": 'getattr=print\ndel getattr\ngetattr(obj,"patch")\n',
            "delete_globals_restore": 'import requests\nglobals=lambda:{}\ndel globals\nglobals()["requests"]=candidate\nrequests.patch()\n',
            "class_delete_outer_alias": 'ga=getattr\nclass C:\n    ga=print\n    del ga\n    ga(obj,"patch")\n',
            "deferred_global_deletion": 'getattr=print\ndef restore():\n    global getattr\n    del getattr\nrestore()\ngetattr(obj,"patch")\n',
            # A bare builtin name is never retired by a rebind (as on
            # main), so these two formerly-allowed shapes are flagged.
            "class_delete_outer_safe": 'getattr=print\nclass C:\n    getattr=print\n    del getattr\n    getattr(obj,"patch")\n',
            "function_deleted_local_unbound": 'def f():\n    getattr=print\n    del getattr\n    getattr(obj,"patch")\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), (name, errors)

    def test_lexical_aliases_use_the_defining_binding(self):
        cases = {
            "default_alias_from_outer_shadowed_parameter": 'ga=getattr\ndef f(ga, lookup=ga):\n    lookup(obj,"patch")\nf(None)\n',
            "class_fallback_outer_alias": 'ga=getattr\nclass C:\n    ga(obj,"patch")\n    ga=print\n',
            "nonlocal_skips_unbound_middle": 'def outer():\n    ga=getattr\n    def middle():\n        def unused():\n            nonlocal ga\n            ga=print\n        ga(obj,"patch")\n    middle()\nouter()\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), (name, errors)

    def test_namespace_mutation_spellings_reject_replaced_clients(self):
        cases = {
            "namespace_property_storage": 'import requests\nh.ns=globals()\nh.ns["requests"]=candidate\nrequests.patch()\n',
            "class_locals": 'import requests\nclass C:\n    locals()["requests"]=candidate\n    requests.patch()\n',
            "namespace_ior_empty": 'import requests\nns=globals()\nns |= {}\nns["requests"]=candidate\nrequests.patch()\n',
            "namespace_ior_keys": 'import requests\nns=globals()\nns |= {"requests":candidate}\nrequests.patch()\n',
            "unbound_setitem": 'import requests\ndict.__setitem__(globals(),"requests",candidate)\nrequests.patch()\n',
            "vars_module": 'import requests\nimport sys\nvars(sys.modules[__name__])["requests"]=candidate\nrequests.patch()\n',
            "vars_zero": 'import requests\nvars()["requests"]=candidate\nrequests.patch()\n',
            "direct_ior": 'import requests\nglobals().__ior__({"requests":candidate})\nrequests.patch()\n',
            "class_setdefault": 'import requests\nclass C:\n    locals().setdefault("requests",candidate)\n    requests.patch()\n',
            "attribute_function_alias": 'import requests\nh.g=globals\nh.g()["requests"]=candidate\nrequests.patch()\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any("patch" in error for error in errors), (name, errors)

    def test_harmless_namespace_reads_and_updates_still_revoke(self):
        # Harmless at runtime (another key, a copy), yet flagged: the
        # exemption is vocabulary-gated, not a model of which mapping a
        # write lands in. This matches main, which bans every .patch.
        cases = {
            "class_unrelated": 'import requests\nclass C:\n    locals()["unrelated"]=candidate\n    requests.patch()\n',
            "globals_copy": 'import requests\nns=globals().copy()\nns["requests"]=candidate\nrequests.patch()\n',
            "unrelated_ior": 'import requests\nns=globals()\nns |= {"other":candidate}\nrequests.patch()\n',
        }
        for name, code in cases.items():
            errors = _check_code(code, IN_DIR)
            assert any(".patch is banned" in e for e in errors), (name, errors)


class TestBuiltinNamesNeverRetired:
    """A rebind never retires the bare ``getattr``.

    ``getattr`` always may be the builtin, whatever the file binds to
    it — as on main. Each class below is a construct an earlier
    revision modelled as a definite clear although at runtime the
    builtin was still (or again) reachable, silently dropping a
    diagnostic main emits. The ``ga`` alias variants pin the same
    constructs for aliases, which ARE cleared by definite rebinds.
    Each ``.patch`` case, run for real, rebinds ``requests`` to the
    logger before the call; they stay flagged because the
    ``globals``/``g`` spellings revoke the requests/httpx exemption.
    """

    PATCH_TAIL = "g()['requests'] = get_logger()\nrequests.patch('u')\n"

    @staticmethod
    def _assert_opt(code):
        errors = _check_code(code, IN_DIR)
        assert any('getattr(..., "opt")' in e for e in errors), (code, errors)

    @staticmethod
    def _assert_patch(code):
        errors = _check_code(code, IN_DIR)
        assert any(".patch" in e for e in errors), (code, errors)

    def test_with_item_behind_suppress_is_not_a_definite_rebind(self):
        # ``suppress`` swallows the AttributeError raised while the
        # second item is evaluated, so its ``as`` target never binds
        # and the with body is skipped.
        pre = "import contextlib\n"
        for code in (
            pre + "with contextlib.suppress(AttributeError),"
            " object().missing as getattr:\n"
            "    pass\n"
            "x = getattr(logger, 'opt')\n",
            # suppress(...).__enter__ returns None: unpacking it raises
            # TypeError inside the manager, which it swallows.
            pre + "with contextlib.suppress(TypeError) as (getattr, _):\n"
            "    pass\n"
            "x = getattr(logger, 'opt')\n",
        ):
            self._assert_opt(code)
        for code in (
            "import requests\n"
            + pre
            + "with contextlib.suppress(AttributeError),"
            " object().missing as globals:\n"
            "    pass\n"
            "globals()['requests'] = get_logger()\n"
            "requests.patch('u')\n",
            "import requests\n" + pre + "g = globals\n"
            "with contextlib.suppress(AttributeError),"
            " object().missing as g:\n"
            "    pass\n" + self.PATCH_TAIL,
            "import requests\n" + pre + "g = globals\n"
            "with contextlib.suppress(AttributeError),"
            " (object().missing, (g := dict))[0]:\n"
            "    pass\n" + self.PATCH_TAIL,
        ):
            self._assert_patch(code)

    def test_except_as_implicit_delete_restores_the_builtin(self):
        # Python deletes the handler name when the handler ends, so the
        # builtin is visible again afterwards.
        self._assert_opt(
            "getattr = str\n"
            "try:\n"
            "    raise ValueError\n"
            "except ValueError as getattr:\n"
            "    pass\n"
            "x = getattr(logger, 'opt')\n"
        )
        for code in (
            "import requests\n"
            "globals = (lambda: {})\n"
            "try:\n"
            "    raise ValueError\n"
            "except ValueError as globals:\n"
            "    pass\n"
            "globals()['requests'] = get_logger()\n"
            "requests.patch('u')\n",
            "import requests\n"
            "class K:\n"
            "    globals = lambda: {}\n"
            "    try:\n"
            "        raise ValueError\n"
            "    except ValueError as globals:\n"
            "        pass\n"
            "    globals()['requests'] = get_logger()\n"
            "requests.patch('u')\n",
            # In a class body the deleted alias falls back to the
            # module-level ``g = globals``.
            "import requests\n"
            "g = globals\n"
            "class K:\n"
            "    g = lambda: {}\n"
            "    try:\n"
            "        raise ValueError\n"
            "    except ValueError as g:\n"
            "        pass\n"
            "    g()['requests'] = get_logger()\n"
            "requests.patch('u')\n",
        ):
            self._assert_patch(code)

    def test_walrus_in_assignment_target_runs_after_the_rhs(self):
        # The RHS is evaluated before any target expression, and a For
        # target is never evaluated for an empty iterable.
        for code in (
            "d = {}\nd[(getattr := str)] = getattr(logger, 'opt')\n",
            "d = {}\n"
            "for d[(getattr := str)] in []:\n"
            "    pass\n"
            "x = getattr(logger, 'opt')\n",
        ):
            self._assert_opt(code)
        for code in (
            "import requests\n"
            "g = globals\n"
            "d = {}\n"
            "d[(g := dict)] = g().__setitem__('requests', get_logger())\n"
            "requests.patch('u')\n",
            "import requests\n"
            "g = globals\n"
            "d = {}\n"
            "for d[(g := dict)] in []:\n"
            "    pass\n" + self.PATCH_TAIL,
        ):
            self._assert_patch(code)

    def test_subscript_and_lambda_rhs_still_yield_the_builtin(self):
        for code in (
            "getattr = [getattr][0]\nx = getattr(logger, 'opt')\n",
            "getattr = (lambda: getattr)()\nx = getattr(logger, 'opt')\n",
            # Alias gains through the same value shapes.
            "ga = {'k': getattr}['k']\nx = ga(logger, 'opt')\n",
            "ga = __import__('builtins').getattr\nx = ga(logger, 'opt')\n",
        ):
            self._assert_opt(code)
        for code in (
            "import requests\n"
            "globals = (globals,)[0]\n"
            "globals()['requests'] = get_logger()\n"
            "requests.patch('u')\n",
            "import requests\ng = [globals][0]\n" + self.PATCH_TAIL,
            "import requests\ng = (lambda: globals)()\n" + self.PATCH_TAIL,
            "import requests\n"
            "import builtins\n"
            "g = vars(builtins)['globals']\n" + self.PATCH_TAIL,
            "import requests\n"
            "import builtins\n"
            "g = getattr(builtins, 'globals')\n" + self.PATCH_TAIL,
        ):
            self._assert_patch(code)

    def test_async_with_body_is_conditional(self):
        # The async context manager swallows the AttributeError, so the
        # alias rebind after it never runs.
        manager = (
            "class Suppress:\n"
            "    async def __aenter__(self): return self\n"
            "    async def __aexit__(self, *exc): return True\n"
        )
        self._assert_opt(
            manager + "async def f():\n"
            "    ga = getattr\n"
            "    async with Suppress():\n"
            "        y = object().missing\n"
            "        ga = str\n"
            "    return ga(logger, 'opt')\n"
        )
        self._assert_patch(
            "import requests\n" + manager + "async def f():\n"
            "    g = globals\n"
            "    async with Suppress():\n"
            "        y = object().missing\n"
            "        g = lambda: {}\n"
            "    g()['requests'] = get_logger()\n"
            "requests.patch('u')\n"
        )


class TestPatchExemptionNamespaceVocabulary:
    """The requests/httpx ``.patch`` exemption is vocabulary-gated.

    Each fixture below, run for real, hands ``requests`` a logger (or
    swaps ``requests.patch``) before the final call, through a channel
    an execution-order model of aliases got wrong. The exemption now
    ignores order entirely: any namespace-reaching spelling anywhere in
    the file revokes it, so each is flagged on the final line as on
    main. The clean files at the end are why the exemption exists.
    """

    @staticmethod
    def _assert_final_patch_flagged(code):
        last = code.rstrip("\n").count("\n") + 1
        errors = _check_code(code, IN_DIR)
        assert any(f":{last}: .patch is banned" in e for e in errors), (
            code,
            errors,
        )

    def test_loop_back_edge_rebinding(self):
        for code in (
            "import requests\n"
            "g = dict\n"
            "for i in range(2):\n"
            "    g()['requests'] = get_logger()\n"
            "    g = globals\n"
            "requests.patch('u')\n",
            "import requests\n"
            "ns = {}\n"
            "while cond():\n"
            "    ns['requests'] = get_logger()\n"
            "    ns = globals()\n"
            "requests.patch('u')\n",
        ):
            self._assert_final_patch_flagged(code)

    def test_starred_argument_evaluation_order(self):
        # Starred positionals run before keyword values.
        self._assert_final_patch_flagged(
            "import requests\n"
            "ns = globals()\n"
            "f(k=(ns := {}), *[ns.__setitem__('requests', get_logger())])\n"
            "requests.patch('u')\n"
        )

    def test_indirect_setattr_call_sites(self):
        for code in (
            "import requests, sys\n"
            "[setattr][0](sys.modules[__name__], 'requests', get_logger())\n"
            "requests.patch('u')\n",
            "import requests, sys\n"
            "(s := setattr)(sys.modules[__name__], 'requests', get_logger())\n"
            "requests.patch('u')\n",
            "import requests, sys\n"
            "(lambda: setattr)()(sys.modules[__name__], 'requests', L)\n"
            "requests.patch('u')\n",
        ):
            self._assert_final_patch_flagged(code)

    def test_function_and_frame_globals(self):
        for code in (
            "import requests\n"
            "def f():\n"
            "    pass\n"
            "f.__globals__['requests'] = get_logger()\n"
            "requests.patch('u')\n",
            "import requests, sys\n"
            "sys._getframe().f_globals['requests'] = get_logger()\n"
            "requests.patch('u')\n",
            "import requests\n"
            "def f():\n"
            "    pass\n"
            "getattr(f, '__glo' + 'bals__')['requests'] = get_logger()\n"
            "requests.patch('u')\n",
        ):
            self._assert_final_patch_flagged(code)

    def test_sys_modules_main_and_self_import(self):
        for code in (
            "import requests, sys\n"
            "sys.modules['__main__'].requests = get_logger()\n"
            "requests.patch('u')\n",
            "import requests\n"
            "from sys import modules\n"
            "modules[__name__].requests = get_logger()\n"
            "requests.patch('u')\n",
            "import requests\n"
            "import this_package.this_module as me\n"
            "me.requests = get_logger()\n"
            "requests.patch('u')\n",
        ):
            self._assert_final_patch_flagged(code)

    def test_import_poisoning_before_the_import(self):
        self._assert_final_patch_flagged(
            "import sys\n"
            "sys.modules['requests'] = get_logger()\n"
            "import requests\n"
            "requests.patch('u')\n"
        )

    def test_module_attribute_replacement(self):
        for code in (
            "import requests\n"
            "requests.patch = get_logger().info\n"
            "requests.patch('u')\n",
            "import httpx\n"
            "import httpx as client\n"
            "client.patch = get_logger().info\n"
            "httpx.patch('u')\n",
        ):
            self._assert_final_patch_flagged(code)

    def test_string_resolvers_and_code_runners_revoke(self):
        for code in (
            "import requests, gc\n"
            "gc.get_referrers(requests)[0]['requests'] = get_logger()\n"
            "requests.patch('u')\n",
            "import requests, cProfile\n"
            "cProfile.run('requests = get_logger()')\n"
            "requests.patch('u')\n",
            "import requests\n"
            "from unittest.mock import patch as p\n"
            "p(__name__ + '.requests', get_logger()).start()\n"
            "requests.patch('u')\n",
            "import requests\n"
            "import typing\n"
            'def f(x: "sys.modules[__name__].__setattr__(1, 2)"):\n'
            "    pass\n"
            "typing.get_type_hints(f)\n"
            "requests.patch('u')\n",
        ):
            self._assert_final_patch_flagged(code)

    def test_revocation_is_named_in_the_diagnostic(self):
        errors = _check_code(
            "import httpx\ndef f():\n    return 1\nvars(f)\nhttpx.patch('u')\n",
            IN_DIR,
        )
        assert any(
            "httpx.patch exemption is revoked for this file by vars on line 4"
            in e
            for e in errors
        ), errors

    def test_plain_http_client_patch_still_exempt(self):
        # The exemption's reason to exist (#4976): a realistic client
        # module with no namespace-reaching spelling stays clean.
        for code in (
            "import httpx\n"
            "httpx.patch('https://example.test/item', json={'a': 1})\n",
            "import httpx\n"
            "\n"
            "\n"
            "def update(url, payload, timeout=10):\n"
            "    response = httpx.patch(url, json=payload, timeout=timeout)\n"
            "    code = response.status_code\n"
            "    if code >= 400:\n"
            "        return None\n"
            "    return getattr(response, 'text', '')\n",
            "import requests\n"
            "\n"
            "\n"
            "class Client:\n"
            "    def __init__(self, base):\n"
            "        self.base = base\n"
            "\n"
            "    def update(self, path, data):\n"
            "        return requests.patch(self.base + path, data=data)\n",
        ):
            assert _check_code(code, IN_DIR) == [], code


class TestPatchExemptionStructuralConditions:
    """Structural conditions on the requests/httpx ``.patch`` exemption.

    The vocabulary denylist alone missed a class namespace supplied by a
    metaclass ``__prepare__`` (class keywords, or a base class's
    metaclass), a module class swap through an alias, and a function
    defined before the import that runs first. Each fixture here must be
    flagged on its ``.patch`` line, as on main.
    """

    @staticmethod
    def _assert_patch_flagged(code, needle=""):
        line = next(
            i for i, text in enumerate(code.split("\n"), 1) if ".patch(" in text
        )
        errors = _check_code(code, IN_DIR)
        assert any(
            f":{line}: .patch is banned" in e and needle in e for e in errors
        ), (code, errors)

    def test_class_keywords_revoke_the_exemption(self):
        for code in (
            "import requests\n"
            "\n"
            "\n"
            "def run(sink, secret):\n"
            "    class _C(metaclass=_Meta, requests=sink):\n"
            "        pass\n"
            "    requests.patch('leak {}', secret)\n",
            "import requests\n"
            "\n"
            "\n"
            "class _C(Base, lg=sink):\n"
            "    pass\n"
            "\n"
            "\n"
            "def run(secret):\n"
            "    requests.patch('leak {}', secret)\n",
        ):
            self._assert_patch_flagged(code, "keywords on class _C")

    def test_class_scope_reference_never_exempt(self):
        # The class namespace comes from the metaclass, possibly one a
        # base class inherits from another module.
        for code in (
            "import requests\n"
            "from .base import Base\n"
            "\n"
            "\n"
            "def run(secret):\n"
            "    class _C(Base):\n"
            "        requests.patch('leak {}', secret)\n",
            "import httpx\n"
            "from .base import Base\n"
            "\n"
            "\n"
            "class _C(Base):\n"
            "    @httpx.patch('u')\n"
            "    def f(self):\n"
            "        pass\n",
        ):
            self._assert_patch_flagged(code, "inside a class body")

    def test_reference_before_the_import_revokes(self):
        for code in (
            "import sys\n"
            "\n"
            "\n"
            "def _late(secret):\n"
            "    requests.patch('leak {}', secret)\n"
            "\n"
            "\n"
            "_late('SECRET')\n"
            "import requests  # noqa: E402\n",
            "import httpx\n"
            "run = lambda u: requests.patch(u)\n"
            "import requests  # noqa: E402\n",
        ):
            self._assert_patch_flagged(code, "before import requests")

    def test_dunder_attribute_write_revokes(self):
        for code in (
            "import requests\n"
            "\n"
            "\n"
            "def run(sink, secret):\n"
            "    m = requests\n"
            "    m.__doc__ = sink\n"
            "    requests.patch('leak {}', secret)\n",
            "import requests\n"
            "\n"
            "\n"
            "def run(sink, secret):\n"
            "    m = requests\n"
            "\n"
            "    class _Evil(type(m)):\n"
            "        patch = property(lambda self: sink)\n"
            "\n"
            "    m.__class__ = _Evil\n"
            "    requests.patch('leak {}', secret)\n",
        ):
            self._assert_patch_flagged(code)

    def test_frame_and_module_hook_spellings_revoke(self):
        for code in (
            "import sys\n"
            "import requests\n"
            "\n"
            "\n"
            "def _late(secret):\n"
            "    requests.patch('leak {}', secret)\n"
            "\n"
            "\n"
            "sys._getframe().f_builtins['requests'] = LOG\n",
            "import requests\n"
            "\n"
            "\n"
            "def run(tb, secret):\n"
            "    tb.tb_frame.f_back\n"
            "    requests.patch('leak {}', secret)\n",
            "import requests\n"
            "\n"
            "\n"
            "def __getattr__(name):\n"
            "    return LOG\n"
            "\n"
            "\n"
            "def run(secret):\n"
            "    requests.patch('leak {}', secret)\n",
            "import requests\n"
            "\n"
            "\n"
            "def run(gen, secret):\n"
            "    gen.gi_code\n"
            "    requests.patch('leak {}', secret)\n",
        ):
            self._assert_patch_flagged(code, "exemption is revoked")

    def test_import_first_function_and_method_still_exempt(self):
        for code in (
            "import httpx\n\n\ndef update(url):\n    return httpx.patch(url)\n",
            "import requests\n"
            "\n"
            "\n"
            "class Client:\n"
            "    def update(self, url):\n"
            "        return requests.patch(url, timeout=lambda: requests.codes)\n",
        ):
            assert _check_code(code, IN_DIR) == [], code

    def test_module_used_as_a_value_revokes(self):
        # The module object handed to code this rule cannot follow can
        # be mutated there: functools.update_wrapper copies the logger's
        # ``patch`` onto it with no denylisted spelling in the file.
        # Any load of the bare name other than as an attribute receiver
        # revokes the exemption.
        self._assert_patch_flagged(
            "import functools\n"
            "import requests\n"
            "from ...security.secure_logging import logger\n"
            "functools.update_wrapper("
            "requests, logger, assigned=('patch',), updated=())\n"
            "requests.patch(lambda record: record)\n",
            "requests used as a value on line 4",
        )
        for code in (
            "import requests\nhelper(requests)\nrequests.patch('u')\n",
            "import httpx\nhelper(client=httpx)\nhttpx.patch('u')\n",
            "import requests\nm = requests\nrequests.patch('u')\n",
            "import requests\n"
            "\n"
            "\n"
            "def get():\n"
            "    return requests\n"
            "\n"
            "\n"
            "requests.patch('u')\n",
            "import httpx\nmods = [httpx]\nhttpx.patch('u')\n",
        ):
            self._assert_patch_flagged(code, "used as a value")

    def test_aliased_module_import_revokes(self):
        # A second name bound to the same module object walks around the
        # bare-name value-use check: ``rq is requests`` at runtime, so
        # update_wrapper(rq, ...) replaces requests.patch. Any aliased
        # import of requests/httpx, at any scope, revokes the exemption.
        self._assert_patch_flagged(
            "import functools\n"
            "import requests\n"
            "import requests as rq\n"
            "from ...security.secure_logging import logger\n"
            "functools.update_wrapper("
            "rq, logger, assigned=('patch',), updated=())\n"
            "requests.patch(lambda r: r)\n",
            "requests imported as rq on line 3",
        )
        self._assert_patch_flagged(
            "import functools\n"
            "import requests\n"
            "from ...security.secure_logging import logger\n"
            "\n"
            "\n"
            "def f():\n"
            "    import requests as rq\n"
            "\n"
            "    functools.update_wrapper("
            "rq, logger, assigned=('patch',), updated=())\n"
            "\n"
            "\n"
            "f()\n"
            "requests.patch(lambda r: r)\n",
            "requests imported as rq on line 7",
        )
        for code in (
            "import httpx\nimport httpx as hx\nhttpx.patch('u')\n",
            "import requests\nimport requests.sessions as s\n"
            "requests.patch('u')\n",
            "import requests\nfrom pkg import requests as r2\n"
            "requests.patch('u')\n",
        ):
            self._assert_patch_flagged(code, "imported")


class TestWrapperChainMessageChecks:
    """bind() chains keep the wrapper but messages stay checked."""

    def test_bind_chain_with_safe_message_allowed(self):
        assert _check_code("logger.bind(x=1).exception('safe')\n", IN_DIR) == []

    def test_bind_chain_with_exception_var_flagged(self):
        for method, args in (
            ("exception", "f'failed: {e}'"),
            ("warning", "f'failed: {e}'"),
            ("error", "f'failed: {e}'"),
            ("critical", "f'failed: {e}'"),
            ("info", "f'failed: {e}'"),
            ("log", "'ERROR', f'failed: {e}'"),
        ):
            code = (
                "try:\n"
                "    f()\n"
                "except Exception as e:\n"
                f"    logger.bind(x=1).{method}({args})\n"
            )
            errors = _check_code(code, IN_DIR)
            assert any("Exception variable" in e for e in errors), method

    def test_bind_chain_debug_with_exception_var_allowed(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    logger.bind(x=1).debug(f'failed: {e}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_bind_chain_exc_info_true_not_flagged(self):
        # loguru stores exc_info=True in extra, not record["exception"] —
        # matches the pre-existing search_engine_elasticsearch.py site.
        code = "logger.bind(x=1).warning('failed', exc_info=True)\n"
        assert _check_code(code, IN_DIR) == []


class TestExceptionAttributeInterpolation:
    """Access *through* the exception variable leaks like str(e).

    Adversarial-review finding C1: e.args, e.response.text, e.strerror,
    e.__cause__, e.args[0] and receiver/keyword shapes evaded
    _expr_references_vars, which only recursed Name/JoinedStr/Call-args/
    BinOp/Tuple.
    """

    def test_attribute_access_flagged(self):
        for expr in (
            "f'failed: {e.args}'",
            "f'disk: {e.strerror}'",
            "f'API {e.response.status_code}: {e.response.text}'",
            "f'cause: {e.__cause__}'",
        ):
            code = (
                "try:\n"
                "    f()\n"
                "except Exception as e:\n"
                f"    logger.error({expr})\n"
            )
            errors = _check_code(code, IN_DIR)
            assert any("Exception variable" in e for e in errors), expr

    def test_subscript_access_flagged(self):
        for expr in ("f'{e.args[0]}'", "f'{e[\"detail\"]}'"):
            code = (
                "try:\n"
                "    f()\n"
                "except Exception as e:\n"
                f"    logger.error({expr})\n"
            )
            errors = _check_code(code, IN_DIR)
            assert any("Exception variable" in e for e in errors), expr

    def test_receiver_and_keyword_shapes_flagged(self):
        for expr in (
            "f'{str(e).upper()}'",  # exception hidden in call receiver
            "'{x}'.format(x=e)",  # exception hidden in call keyword
            "'%s' % e.args",  # %-format with attribute
            "str(e.args)",
        ):
            code = (
                "try:\n"
                "    f()\n"
                "except Exception as e:\n"
                f"    logger.error({expr})\n"
            )
            errors = _check_code(code, IN_DIR)
            assert any("Exception variable" in e for e in errors), expr

    def test_percent_style_attribute_arg_flagged(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    logger.error('failed %s', e.args)\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in e for e in errors)

    def test_exception_level_attribute_flagged_in_secure_dir(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    logger.exception(f'boom: {e.args}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("production-visible" in e for e in errors)

    def test_exception_class_name_allowed(self):
        # The sanctioned step-1 pattern: the class name carries no error
        # detail. Both spellings, incl. combined with a scrubbed message —
        # the exact shape used by search_engine_wayback.py et al.
        for expr in (
            "f'{type(e).__name__}'",
            "f'{e.__class__.__name__}'",
            "f'failed ({type(e).__name__}): {safe_msg}'",
        ):
            code = (
                "try:\n"
                "    f()\n"
                "except Exception as e:\n"
                "    safe_msg = sanitize_error_message(str(e))\n"
                f"    logger.error({expr})\n"
            )
            assert _check_code(code, IN_DIR) == [], expr

    def test_bare_type_call_still_flagged(self):
        # type(e) without .__name__ stringifies via repr — keep flagging it
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    logger.error(f'{type(e)}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in e for e in errors)

    def test_debug_level_attribute_allowed(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    logger.debug(f'{e.args}')\n"
        )
        assert _check_code(code, IN_DIR) == []


class TestTracebackDynamicRoutes:
    """Adversarial-review finding M1: dynamic imports of traceback and
    traceback-formatting attributes must be banned like the static forms."""

    def test_dynamic_import_of_traceback_flagged(self):
        for code in (
            "tb = importlib.import_module('traceback')\n",
            "tb = import_module('traceback')\n",
            "tb = __import__('traceback')\n",
            "raw = __import__('loguru')\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any("dynamic import" in e for e in errors), code

    def test_dynamic_import_of_other_modules_allowed(self):
        # auto_discovery.py's f-string import_module and constant imports of
        # ordinary modules must stay clean
        for code in (
            "m = importlib.import_module(f'.implementations.{name}', pkg)\n",
            "m = importlib.import_module('json')\n",
            "m = __import__('os')\n",
        ):
            assert _check_code(code, IN_DIR) == [], code

    def test_format_exc_attribute_flagged(self):
        for code in (
            "logger.error('boom: ' + tb.format_exc())\n",
            "x = tb.format_exception(e)\n",
            "x = tb.format_exception_only(e)\n",
            "x = tb.format_tb(e.__traceback__)\n",
            "tb.print_exc()\n",
        ):
            errors = _check_code(code, IN_DIR)
            assert any("traceback text" in e for e in errors), code

    def test_format_exc_getattr_flagged(self):
        errors = _check_code("f = getattr(tb, 'format_exc')\n", IN_DIR)
        assert any("traceback text" in e for e in errors)

    def test_traceback_routes_not_flagged_outside_secure_dirs(self):
        for code in (
            "tb = importlib.import_module('traceback')\n",
            "x = tb.format_exc()\n",
        ):
            assert _check_code(code, OUT_DIR) == [], code


class TestRecommendedPattern:
    def test_wrapper_import_plus_scrubbed_safe_msg_clean(self):
        code = (
            "from ...security.secure_logging import logger\n"
            "\n"
            "\n"
            "def search(self, q):\n"
            "    try:\n"
            "        return self._run(q)\n"
            "    except Exception as e:\n"
            "        safe_msg = self._scrub_error(e)\n"
            "        logger.exception(f'Search failed: {safe_msg}')\n"
            "        return []\n"
        )
        assert _check_code(code, IN_DIR) == []


class TestWrapperImportHintPerFile:
    """The wrapper-import hint must reflect the file's actual depth in the
    tree so the suggested import resolves to the right module."""

    @staticmethod
    def _hint_for(filename: str) -> str:
        checker = SensitiveLoggingChecker(filename)
        return checker.wrapper_import_hint

    def test_engines_dir_gets_three_dots(self):
        hint = self._hint_for(IN_DIR)
        # IN_DIR ends in web_search_engines/engines/x.py — three dots
        # to reach local_deep_research from .engines.
        assert "from ...security.secure_logging" in hint

    def test_llm_implementations_gets_four_dots(self):
        hint = self._hint_for(IN_DIR_LLM)
        assert "from ....security.secure_logging" in hint

    def test_embeddings_implementations_gets_four_dots(self):
        hint = self._hint_for(IN_DIR_EMB)
        assert "from ....security.secure_logging" in hint

    def test_root_package_file_gets_one_dot(self):
        # A hypothetical file directly under local_deep_research/ needs 1 dot.
        hint = self._hint_for("src/local_deep_research/something.py")
        assert "from .security.secure_logging" in hint

    def test_error_message_uses_per_file_hint(self):
        # Trigger an import ban and check the error carries the right dot count.
        code = "import loguru\n"
        errs = _check_code(code, IN_DIR_LLM)
        assert len(errs) == 1
        assert "from ....security.secure_logging" in errs[0]

    def test_hint_rejects_lookalike_package_prefix(self):
        """The marker ``/local_deep_research/`` is leading-slash-anchored
        so a lookalike directory like ``src/notlocal_deep_research/foo.py``
        must NOT trigger the per-file dot-count path — it falls back to
        the generic constant hint. Without this anchoring, a hypothetical
        sibling package could spoof the marker and produce a wrong dot
        count. Mutates the marker to ``local_deep_research/`` (no leading
        slash) and this test fails."""
        # Lookalike path that contains the marker substring but is not
        # actually under local_deep_research/. in_secure_dir is False so
        # no error is generated, but we can still assert the hint shape.
        checker = SensitiveLoggingChecker("src/notlocal_deep_research/foo.py")
        # Falls back to the generic 3-dot hint (not a per-file dot count).
        assert checker.wrapper_import_hint == hook_module.WRAPPER_IMPORT_HINT
        assert "from ...security.secure_logging" in checker.wrapper_import_hint


class TestMatchCaseExceptionCapture:
    """``match e: case X() as v:`` rebinds the exception variable. The
    rebind must be tracked so log calls inside the case body that
    interpolate the new name are flagged like the original."""

    def test_match_as_binds_exception_var_flagged(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError() as ve:\n"
            "            logger.error(f'err: {ve}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in e for e in errors), code

    def test_match_on_non_exception_subject_not_flagged(self):
        """If the match subject is not the current exception variable, we
        cannot prove the binding is an exception — do not flag."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    obj = get()\n"
            "    match obj:\n"
            "        case SomeClass() as x:\n"
            "            logger.error(f'val: {x}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_match_multiple_cases_each_flagged(self):
        """Each ``case ... as v`` binds a distinct name; all must be
        tracked separately (and the wildcard arm still sees the outer
        exception var)."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError() as ve:\n"
            "            logger.error(f'v: {ve}')\n"
            "        case TypeError() as te:\n"
            "            logger.warning(f't: {te}')\n"
            "        case _:\n"
            "            logger.error(f'other: {e}')\n"
        )
        errors = _check_code(code, IN_DIR)
        flagged_lines = {
            e.split(":")[1] for e in errors if "Exception variable" in e
        }
        assert len(flagged_lines) == 3, errors

    def test_match_nested_sequence_components_not_treated_as_exception(self):
        """Sequence captures are components, not aliases of the subject."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case [message, *rest]:\n"
            "            logger.error(f'message: {message}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_match_mapping_components_not_treated_as_exception(self):
        """Mapping captures are values from the subject, not the subject."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case {'message': message}:\n"
            "            logger.error(f'message: {message}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_match_or_pattern_binds(self):
        """``case (A() as x) | (B() as x):`` — both alternatives bind ``x``."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case (A() as x) | (B() as x):\n"
            "            logger.error(f'val: {x}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in e for e in errors), code

    def test_match_class_keyword_component_not_treated_as_exception(self):
        """A class keyword capture is an attribute, not the whole subject."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case HttpError(code=code):\n"
            "            logger.error(f'code: {code}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_match_class_positional_component_not_treated_as_exception(self):
        """A class positional capture is an attribute, not the subject."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case Wrapper(message, _):\n"
            "            logger.error(f'message: {message}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_match_expression_referencing_exception_does_not_alias_result(self):
        """Referencing ``e`` does not make a transformed subject identical."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match str(e):\n"
            "        case message:\n"
            "            logger.error(f'message: {message}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_match_compound_subject_does_not_alias_unrelated_capture(self):
        """A mixed subject must not taint every corresponding capture."""
        code = (
            "public_value = 'safe'\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match (e, public_value):\n"
            "        case (captured_e, captured_public):\n"
            "            logger.error(f'value: {captured_public}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_match_compound_subject_tracks_exception_position(self):
        """Structural mapping tracks the exception without tainting siblings."""
        code = (
            "public_value = 'safe'\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match (e, public_value):\n"
            "        case (captured_e, captured_public):\n"
            "            logger.error(f'err: {captured_e}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_match_star_preserves_fixed_exception_positions(self):
        cases = [
            ("(e, 'safe')", "(captured_e, *rest)"),
            ("('safe', e)", "(*rest, captured_e)"),
            ("(e, *values)", "(captured_e, *rest)"),
            ("(e, *values)", "(captured_e, other)"),
            ("(*values, e)", "(other, captured_e)"),
        ]
        for subject, pattern in cases:
            code = (
                "values = ['safe']\n"
                "try:\n"
                "    f()\n"
                "except Exception as e:\n"
                f"    match {subject}:\n"
                f"        case {pattern}:\n"
                "            logger.error(f'err: {captured_e}')\n"
            )
            errors = _check_code(code, IN_DIR)
            assert any("Exception variable" in error for error in errors), (
                code,
                errors,
            )

    def test_match_literal_mapping_tracks_exception_value_only(self):
        code_flagged = (
            "public_value = 'safe'\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match {'error': e, 'public': public_value}:\n"
            "        case {'error': captured_e, 'public': captured_public}:\n"
            "            logger.error(f'err: {captured_e}')\n"
        )
        errors = _check_code(code_flagged, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

        code_safe = code_flagged.replace(
            "logger.error(f'err: {captured_e}')",
            "logger.error(f'public: {captured_public}')",
        )
        assert _check_code(code_safe, IN_DIR) == []

    def test_match_subject_still_visited_for_existing_security_checks(self):
        """Custom match traversal must not hide checks in the subject."""
        code = (
            "import sys\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match (e, sys.exc_info()):\n"
            "        case _:\n"
            "            pass\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("exc_info is banned" in error for error in errors), errors

    def test_match_alias_remains_tracked_after_match(self):
        """Successful pattern bindings remain in Python's enclosing scope."""
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case _ as rebound:\n"
            "            pass\n"
            "    logger.error(f'err: {rebound}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_nested_match_alias_remains_tracked_after_outer_match(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case _ as outer:\n"
            "            match outer:\n"
            "                case _ as inner:\n"
            "                    pass\n"
            "    logger.error(f'err: {inner}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_conditional_capture_does_not_hide_possible_sys_alias(self):
        code = (
            "import sys as s\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError() as s:\n"
            "            pass\n"
            "        case _:\n"
            "            pass\n"
            "    s.exc_info()\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("exc_info is banned" in error for error in errors), errors

    def test_unguarded_case_alias_does_not_flow_to_later_case(self):
        code = (
            "x = 'safe'\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError() as x:\n"
            "            pass\n"
            "        case TypeError():\n"
            "            logger.error(f'value: {x}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_component_capture_shadows_exception_name_inside_case(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError(args=e):\n"
            "            logger.error(f'args: {e}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_component_capture_shadows_sys_alias_inside_case(self):
        code = (
            "import sys as s\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError(args=s):\n"
            "            s.exc_info()\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_pattern_capture_named_logger_is_banned(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match {'sink': object()}:\n"
            "        case {'sink': logger}:\n"
            "            pass\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("rebinding the name 'logger'" in error for error in errors)

    def test_walrus_subject_preserves_exception_identity(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match (subject := e):\n"
            "        case _ as rebound:\n"
            "            logger.error(f'err: {rebound}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_nested_walrus_in_compound_subject_persists_alias(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match ((alias := e), 'public'):\n"
            "        case _:\n"
            "            pass\n"
            "    logger.error(f'err: {alias}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_nested_walrus_in_compound_subject_kills_old_alias(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match ((e := 'safe'), 'public'):\n"
            "        case _:\n"
            "            logger.error(f'value: {e}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_ordered_subject_preserves_earlier_walrus_identity(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match ((alias := e), (e := 'safe')):\n"
            "        case (captured, _):\n"
            "            logger.error(f'err: {captured}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_ordered_subject_identity_survives_later_name_overwrites(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match ((alias := e), (alias := 'safe'), (e := 'safe')):\n"
            "        case (captured, _, _):\n"
            "            logger.error(f'err: {captured}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_match_guard_walrus_tracks_exception_alias(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case _ if (rebound := e):\n"
            "            logger.error(f'err: {rebound}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_failed_guard_walrus_alias_is_visible_to_later_case(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case _ if (rebound := e) and False:\n"
            "            pass\n"
            "        case _:\n"
            "            logger.error(f'err: {rebound}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_match_guard_walrus_safe_overwrite_remains_conservative(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case _ as rebound if (rebound := 'safe'):\n"
            "            logger.error(f'value: {rebound}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_short_circuit_guard_does_not_kill_possible_exception_alias(self):
        code = (
            "flag = True\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case _ as x if flag or (x := 'safe'):\n"
            "            logger.error(f'err: {x}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_short_circuit_guard_preserves_alias_for_later_case(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case _ as x if False and (x := 'safe'):\n"
            "            pass\n"
            "        case _:\n"
            "            logger.error(f'err: {x}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in error for error in errors), errors

    def test_match_subject_walrus_kills_overwritten_exception_name(self):
        code = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match (e := 'safe'):\n"
            "        case _:\n"
            "            logger.error(f'value: {e}')\n"
        )
        assert _check_code(code, IN_DIR) == []

    def test_match_case_rebind_does_not_inherit_sys_alias(self):
        """``import sys as v`` then ``case X() as v:`` rebinds ``v`` to
        the exception. The sys-alias interpretation must NOT persist into
        the case body — ``v.exc_info()`` should not be flagged as a
        sys.exc_info dodge (the case-bound ``v`` is the exception, not
        sys). The exception-var check still catches ``logger.error(f"{v}")``."""
        code = (
            "import sys as v\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError() as v:\n"
            "            logger.error(f'err: {v}')\n"
        )
        errors = _check_code(code, IN_DIR)
        # The exception-interpolation flag fires...
        assert any("Exception variable" in e for e in errors), code
        # ...and the sys.exc_info ban does NOT (v is the exception now,
        # not sys).
        assert not any("sys.exc_info" in e for e in errors), errors

    def test_except_star_match_as_flagged(self):
        """``except* Exception as e:`` (TryStar) followed by
        ``case X() as v:`` must also flag the renamed binding."""
        code = (
            "try:\n"
            "    f()\n"
            "except* Exception as e:\n"
            "    match e:\n"
            "        case ValueError() as ve:\n"
            "            logger.error(f'err: {ve}')\n"
        )
        errors = _check_code(code, IN_DIR)
        assert any("Exception variable" in e for e in errors), code

    def test_match_with_no_capture_not_flagged_for_other_vars(self):
        """``case ValueError():`` (no ``as``) does not rebind — only the
        outer ``e`` is on the stack. A log call inside this arm
        referencing ``e`` is flagged; referencing an unrelated var is not.
        """
        code_flag = (
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError():\n"
            "            logger.error(f'v: {e}')\n"
        )
        errors_flag = _check_code(code_flag, IN_DIR)
        assert any("Exception variable" in e for e in errors_flag), code_flag

        code_safe = (
            "x = 1\n"
            "try:\n"
            "    f()\n"
            "except Exception as e:\n"
            "    match e:\n"
            "        case ValueError():\n"
            "            logger.error(f'val: {x}')\n"
        )
        assert _check_code(code_safe, IN_DIR) == []


class TestSourceTreeInvariant:
    """The real secure dirs must satisfy every ban, even past --no-verify."""

    @staticmethod
    def _src_root() -> Path:
        return Path(__file__).parent.parent.parent / "src"

    @classmethod
    def _secure_dir_files(cls):
        repo_root = cls._src_root().parent
        for d in SECURE_LOGGING_DIRS:
            for path in sorted((repo_root / d).rglob("*.py")):
                yield path

    def test_checker_clean_on_real_secure_dirs(self):
        """Run the full checker over every file in the three dirs."""
        failures = []
        for path in self._secure_dir_files():
            content = path.read_text(encoding="utf-8")
            tree = ast.parse(content)
            # Mirrors check_file()'s construction so this sweep honours the
            # same allow-search-query-log suppressions pre-commit does,
            # rather than failing on a marker pre-commit would accept.
            checker = SensitiveLoggingChecker(
                str(path), source_lines=content.split("\n")
            )
            checker.visit(tree)
            failures.extend(checker.errors)
        assert not failures, "\n".join(failures)

    def test_no_raw_or_reexported_logger_imports_in_tree(self):
        """Independent import scan: headline v4/v5 bypasses asserted
        explicitly, without relying on the checker's own rules."""
        failures = []
        for path in self._secure_dir_files():
            tree = ast.parse(path.read_text(encoding="utf-8"))
            for node in ast.walk(tree):
                if isinstance(node, ast.Import):
                    for alias in node.names:
                        root = alias.name.split(".")[0]
                        leaf = alias.name.split(".")[-1]
                        if (
                            root in ("loguru", "traceback")
                            or leaf in ("secure_logging", "log_utils")
                            or root == "local_deep_research"
                        ):
                            failures.append(
                                f"{path}:{node.lineno}: import {alias.name}"
                            )
                elif isinstance(node, ast.ImportFrom):
                    module = node.module or ""
                    is_wrapper = (
                        node.level > 0 and module == "security.secure_logging"
                    ) or (
                        node.level == 0
                        and module
                        == "local_deep_research.security.secure_logging"
                    )
                    if module.split(".")[0] in ("loguru", "traceback"):
                        failures.append(
                            f"{path}:{node.lineno}: from {module} import ..."
                        )
                        continue
                    if module.split(".")[-1] == "log_utils":
                        failures.append(
                            f"{path}:{node.lineno}: from {module} import ..."
                        )
                        continue
                    for alias in node.names:
                        bound = alias.asname or alias.name
                        if bound == "logger" and not (
                            is_wrapper and alias.name == "logger"
                        ):
                            failures.append(
                                f"{path}:{node.lineno}: non-wrapper logger "
                                f"import from {module!r}"
                            )
                        if alias.name in ("secure_logging", "log_utils") and (
                            not is_wrapper
                        ):
                            failures.append(
                                f"{path}:{node.lineno}: module import of "
                                f"{alias.name} from {module!r}"
                            )
                        if is_wrapper and alias.name != "logger":
                            failures.append(
                                f"{path}:{node.lineno}: secure_logging "
                                f"internal {alias.name!r} imported"
                            )
        assert not failures, "\n".join(failures)

    def test_no_banned_attribute_or_getattr_access_in_tree(self):
        """Attribute/literal-getattr shapes only — a FunctionDef named
        __getattr__ (rate_limiting/llm/wrapper.py) must not trip this."""
        # Fail closed, mirroring the hook: opt/catch are receiver-blind
        # (always banned). patch has exactly one narrow, provably-safe
        # exception — a bare name genuinely bound by a top-level
        # `import requests`/`import httpx`, never rebound anywhere in the
        # file, in a file free of namespace-reaching spellings
        # (SAFE_PATCH_RECEIVER_MODULES in the hook).
        patch_only = {"patch"}
        banned_attrs = {
            "opt",
            "catch",
            "patch",
            "logger",
            "_logger",
            "_loguru_logger",
            "__getattr__",
        }

        def is_safe_patch_receiver(expr, safe_names):
            return isinstance(expr, ast.Name) and expr.id in safe_names

        def is_literal_dynamic_sys_import(expr):
            if not isinstance(expr, ast.Call) or not expr.args:
                return False
            func = expr.func
            is_dynamic_import = (
                (
                    isinstance(func, ast.Attribute)
                    and func.attr == "import_module"
                )
                or (isinstance(func, ast.Name) and func.id == "import_module")
                or (isinstance(func, ast.Name) and func.id == "__import__")
            )
            return (
                is_dynamic_import
                and isinstance(expr.args[0], ast.Constant)
                and expr.args[0].value == "sys"
            )

        def is_sys_module_expr(expr, sys_names):
            return (
                isinstance(expr, ast.Name) and expr.id in sys_names
            ) or is_literal_dynamic_sys_import(expr)

        failures = []
        saw_dunder_getattr_def = False
        for path in self._secure_dir_files():
            tree = ast.parse(path.read_text(encoding="utf-8"))
            sys_names = {"sys"}
            for node in ast.walk(tree):
                if isinstance(node, ast.Import):
                    for alias in node.names:
                        if alias.name == "sys" or (
                            alias.name.split(".")[0] == "sys"
                            and alias.asname is None
                        ):
                            sys_names.add(alias.asname or "sys")
            checker = SensitiveLoggingChecker(str(path))
            checker.visit(tree)
            safe_names = checker._safe_patch_receiver_names
            getattr_names = checker._getattr_names
            for node in ast.walk(tree):
                if isinstance(node, ast.FunctionDef) and (
                    node.name == "__getattr__"
                ):
                    saw_dunder_getattr_def = True
                if isinstance(node, ast.Attribute):
                    if node.attr in patch_only and is_safe_patch_receiver(
                        node.value, safe_names
                    ):
                        # requests.patch(...)/httpx.patch(...) — the one
                        # narrow, provably-safe exception. Allowed.
                        pass
                    elif node.attr in banned_attrs:
                        failures.append(
                            f"{path}:{node.lineno}: .{node.attr} access"
                        )
                    elif node.attr == "exc_info" and is_sys_module_expr(
                        node.value, sys_names
                    ):
                        receiver = (
                            node.value.id
                            if isinstance(node.value, ast.Name)
                            else "sys"
                        )
                        failures.append(
                            f"{path}:{node.lineno}: {receiver}.exc_info access"
                        )
                elif (
                    isinstance(node, ast.ImportFrom)
                    and node.level == 0
                    and node.module == "sys"
                    and any(
                        alias.name in ("*", "exc_info") for alias in node.names
                    )
                ):
                    failures.append(
                        f"{path}:{node.lineno}: from sys import exc_info"
                    )
                elif (
                    isinstance(node, ast.Call)
                    and isinstance(node.func, ast.Name)
                    and node.func.id in getattr_names
                    and len(node.args) >= 2
                    and isinstance(node.args[1], ast.Constant)
                    and isinstance(node.args[1].value, str)
                    and (
                        node.args[1].value in banned_attrs
                        or (
                            node.args[1].value == "exc_info"
                            and is_sys_module_expr(node.args[0], sys_names)
                        )
                    )
                    # Fail closed: banned unless it is the narrow, safe
                    # getattr(requests/httpx, "patch") exception.
                    and not (
                        node.args[1].value in patch_only
                        and is_safe_patch_receiver(node.args[0], safe_names)
                    )
                ):
                    failures.append(
                        f"{path}:{node.lineno}: literal getattr dodge"
                    )
        assert not failures, "\n".join(failures)
        # In-tree negative fixture: the attribute pass-through method exists
        # and did not trip the shape-based scan above.
        assert saw_dunder_getattr_def, (
            "expected def __getattr__ in rate_limiting/llm/wrapper.py "
            "as the negative fixture for the shape-based scan"
        )

    def test_no_logger_rebinding_or_derivation_in_tree(self):
        failures = []
        for path in self._secure_dir_files():
            tree = ast.parse(path.read_text(encoding="utf-8"))
            checker = SensitiveLoggingChecker(str(path))
            checker.visit(tree)
            failures.extend(
                e
                for e in checker.errors
                if "rebinding" in e
                or "shadows" in e
                or "assigning a logger" in e
            )
        assert not failures, "\n".join(failures)

    def test_every_secure_dir_file_using_logger_imports_wrapper(self):
        """Files calling logger.* must import it from the wrapper."""
        failures = []
        for path in self._secure_dir_files():
            tree = ast.parse(path.read_text(encoding="utf-8"))
            uses_logger = any(
                isinstance(node, ast.Attribute)
                and isinstance(node.value, ast.Name)
                and node.value.id == "logger"
                for node in ast.walk(tree)
            )
            if not uses_logger:
                continue
            has_wrapper_import = any(
                isinstance(node, ast.ImportFrom)
                and any(
                    a.name == "logger" and a.asname is None for a in node.names
                )
                and (
                    (
                        node.level > 0
                        and node.module == "security.secure_logging"
                    )
                    or (
                        node.level == 0
                        and node.module
                        == "local_deep_research.security.secure_logging"
                    )
                )
                for node in ast.walk(tree)
            )
            if not has_wrapper_import:
                failures.append(str(path))
        assert not failures, (
            "files using logger without the secure_logging wrapper import:\n"
            + "\n".join(failures)
        )

    def test_secure_logging_dirs_constant_in_sync_with_custom_checks(self):
        """Both hooks consume SECURE_LOGGING_DIRS via _hook_common.py, so
        they must stay identical; this catches either hook re-introducing
        a local literal with drifted content (the identity checks in
        tests/hooks/test_hook_common.py catch same-content copies)."""
        custom_checks = import_module("custom-checks")
        assert custom_checks.SECURE_LOGGING_DIRS == SECURE_LOGGING_DIRS, (
            "SECURE_LOGGING_DIRS drifted between hooks:\n"
            f"  custom-checks.py:        {custom_checks.SECURE_LOGGING_DIRS}\n"
            f"  check-sensitive-logging: {SECURE_LOGGING_DIRS}"
        )

    def test_secure_logging_dirs_nonempty_and_dirs_exist(self):
        """Vacuous-pass guard for the tree scans above.

        _secure_dir_files() iterates SECURE_LOGGING_DIRS; if the constant
        were ever emptied or a path typo'd, the scans would yield nothing
        (or skip the bad entry) and ``assert not failures`` would pass
        over an empty list. Pinning non-emptiness and on-disk existence
        here makes that case fail loudly. Structural + identity checks
        live in tests/hooks/test_hook_common.py.
        """
        assert SECURE_LOGGING_DIRS, "SECURE_LOGGING_DIRS must not be empty"
        repo_root = self._src_root().parent
        missing = [
            d for d in SECURE_LOGGING_DIRS if not (repo_root / d).is_dir()
        ]
        assert not missing, (
            "SECURE_LOGGING_DIRS lists dirs absent from the tree: "
            + ", ".join(missing)
        )
        # Existence is necessary but not sufficient: a dir that survived but
        # was emptied of *.py would still let the scans pass vacuously over
        # its (empty) contribution. Pin that every listed dir actually feeds
        # the generator the scans consume, so a dir going dark fails here.
        empty = [
            d
            for d in SECURE_LOGGING_DIRS
            if not any((repo_root / d).rglob("*.py"))
        ]
        assert not empty, (
            "SECURE_LOGGING_DIRS lists dirs with no .py files to scan: "
            + ", ".join(empty)
        )
        # And the aggregate generator the tree scans iterate must be non-empty.
        assert any(self._secure_dir_files()), (
            "_secure_dir_files() yielded nothing; the tree-scan invariants "
            "above would pass vacuously"
        )

    def test_secure_logging_dirs_membership_pinned(self):
        """Partial-loss guard: deleting ANY member must fail loudly.

        The checks above cannot catch a tuple that shrank but still
        lists valid, non-empty dirs — every remaining scan would pass
        while the removed security domain silently lost coverage.
        Pin the exact expected membership so any deletion (or other
        membership change) must consciously update this list too.
        """
        expected = (
            "src/local_deep_research/llm/providers/",
            "src/local_deep_research/embeddings/providers/",
            "src/local_deep_research/web_search_engines/",
        )
        assert SECURE_LOGGING_DIRS == expected, (
            "SECURE_LOGGING_DIRS membership changed; if intentional, update "
            "the pinned expected list with it:\n"
            f"  actual:   {SECURE_LOGGING_DIRS}\n"
            f"  expected: {expected}"
        )


# ---------------------------------------------------------------------------
# #5646: search queries on the empty-result path under web_search_engines/.
# ---------------------------------------------------------------------------


def _check_query_code(code: str, filename: str = IN_DIR) -> list:
    """Run the checker with source lines attached (suppression comments)."""
    tree = ast.parse(code)
    checker = SensitiveLoggingChecker(filename, source_lines=code.split("\n"))
    checker.visit(tree)
    return [e for e in checker.errors if "search query" in e]


class TestDetectsSearchQueryLogging:
    """BaseSearchEngine.run() omits the query; engines must not re-add it."""

    def test_detects_query_in_get_previews(self):
        code = (
            "def _get_previews(self, query):\n"
            '    logger.info(f"Getting X previews for query: {query}")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_in_get_search_results(self):
        code = (
            "def _get_search_results(self, query):\n"
            '    logger.info(f"X running search for query: {query}")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_rewritten_query_variant(self):
        code = (
            "def _get_previews(self, query):\n"
            '    logger.info(f"trying {simplified_query}")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_in_warning_outside_preview_path(self):
        code = (
            "def _direct_search(self, query):\n"
            '    logger.warning(f"No data for query: {query}")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_in_percent_format(self):
        code = (
            "def _get_previews(self, query):\n"
            '    logger.info("running search for %s" % query)\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_in_bind_chain(self):
        code = (
            "def _get_previews(self, query):\n"
            '    logger.bind(engine="x").info("previews for {}", query)\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_allows_query_length_and_derived_values(self):
        """Documented gap: only len(query) is genuinely non-reproducing.

        ``query.split()`` reproduces the query verbatim modulo whitespace, but
        the rule cannot ban calls on the query without also banning
        ``len(query)``. No call site in the package uses it.
        """
        code = (
            "def _get_previews(self, query):\n"
            '    logger.info(f"query length {len(query)}")\n'
            '    logger.info("terms %s", query.split())\n'
        )
        assert _check_query_code(code) == []

    def test_detects_query_in_helper_called_from_the_preview_path(self):
        """Scope is the package, not the lexically enclosing function name.

        ``_optimize_query`` is reached from ``_get_previews`` one frame down;
        scoping the rule by enclosing function name exempted every such
        helper by construction, which is the leak this rule exists to stop.
        """
        code = (
            "def _optimize_query(self, query):\n"
            '    logger.info(f"Original query: {query}")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_in_str_format_call(self):
        """``.format(query)`` is a Call, not a JoinedStr or a BinOp."""
        code = (
            "def _optimize_query(self, query):\n"
            '    logger.warning("no results for {}".format(query))\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_in_string_concatenation(self):
        """``"..." + query`` is a BinOp whose op is Add, not Mod."""
        code = (
            "def _optimize_query(self, query):\n"
            '    logger.warning("no results for " + query)\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_bound_as_a_bind_kwarg(self):
        """loguru copies bind kwargs into record["extra"] — a live sink.

        The message itself is clean here; only ``bind()`` carries the query,
        so a check that looks at the outer ``.warning()`` alone misses it.
        """
        code = (
            "def _get_previews(self, query):\n"
            '    logger.bind(query=query).warning("empty")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_truncated_query_slices(self):
        """``query[:100]`` still reproduces the first 100 chars the user typed."""
        code = (
            "def _optimize_query(self, query):\n"
            '    logger.debug(f"head {query[:100]}")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_allows_query_length_and_counts_keyed_by_query(self):
        """The shapes a Subscript rule must not catch.

        ``len(query)`` is a Call, and ``query_counts[engine]`` subscripts a
        name the exact-name matcher does not treat as a query.
        """
        code = (
            "def _optimize_query(self, query):\n"
            '    logger.debug(f"{len(query)} chars, {query_counts[engine]} hits")\n'
        )
        assert _check_query_code(code) == []

    def test_ignores_files_outside_web_search_engines(self):
        code = (
            "def _get_previews(self, query):\n"
            '    logger.warning(f"no results for {query}")\n'
        )
        assert _check_query_code(code, filename=OUT_DIR) == []

    def test_ignores_non_query_names(self):
        code = (
            "def _get_previews(self, query):\n"
            '    logger.info(f"url {query_url} params {query_params}")\n'
        )
        assert _check_query_code(code) == []

    def test_suppression_comment_with_reason_silences_the_rule(self):
        code = (
            "def _get_previews(self, query):\n"
            "    logger.info(  # allow-search-query-log: needed to match "
            "the upstream rejection log\n"
            '        f"rejected: {query}"\n'
            "    )\n"
        )
        assert _check_query_code(code) == []

    def test_suppression_comment_without_reason_does_not_silence(self):
        code = (
            "def _get_previews(self, query):\n"
            "    logger.info(  # allow-search-query-log:\n"
            '        f"rejected: {query}"\n'
            "    )\n"
        )
        assert len(_check_query_code(code)) == 1

    @pytest.mark.parametrize(
        "code",
        [
            'logger.info(f"allow-search-query-log: {query}")',
            'logger.info("allow-search-query-log:" + query)',
            'logger.info(f"{query}", note="allow-search-query-log: reason")',
            'logger.info(f"{query} # allow-search-query-log: reason")',
            'logger.info(\n f"{query}",\n note="allow-search-query-log: reason"\n)',
            'logger.info(f"""{query}\nallow-search-query-log: reason""")',
            # PEP 701 (3.12+): a comment may sit inside a replacement field of
            # a multi-line f-string. tokenize reports it as a COMMENT token
            # even though it is part of the string literal.
            'logger.info(\n    f"""no results for {\n'
            "        query  # allow-search-query-log: this text lives inside "
            'a string literal\n    }"""\n)',
            # Same shape, but with the in-string comment on the method-name
            # line, so the f-string tracking is the only thing rejecting it.
            'logger.info(f"""{query  # allow-search-query-log: this text '
            'lives inside a string literal\n}""")',
        ],
    )
    def test_marker_inside_string_does_not_suppress_query(self, code):
        assert len(_check_query_code(code)) == 1

    def test_suppression_comment_on_previous_line_does_not_silence_call(self):
        code = (
            "# allow-search-query-log: unrelated previous line\n"
            'logger.info(f"{query}")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_suppression_comment_on_closing_line_does_not_silence_call(self):
        """The marker counts only on the call's method-name line.

        A marker anywhere in the call's span let a reason written about one
        part of a multi-line call exempt the whole thing.
        """
        code = (
            'logger.info(\n f"{query}"\n'
            ") # allow-search-query-log: required upstream correlation\n"
        )
        assert len(_check_query_code(code)) == 1

    def test_suppression_comment_on_bind_kwarg_does_not_silence_the_message(
        self,
    ):
        """A reason given for a bind() kwarg is not a reason for the message.

        The package has eleven ``logger.bind(policy_audit=True).warning(``
        sites of exactly this shape, where the comment justifying the bound
        field sits lines above the method name.
        """
        code = (
            "def _get_previews(self, query, engine):\n"
            "    logger.bind(\n"
            "        engine=engine,  # allow-search-query-log: policy audit "
            "needs the engine\n"
            '    ).warning(f"No results for {query}")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_suppression_comment_on_the_method_name_line_silences_call(self):
        """A bind chain broken across lines is exempt from its own last line."""
        code = (
            "def _get_previews(self, query, engine):\n"
            "    logger.bind(\n"
            "        engine=engine,\n"
            "    ).warning(  # allow-search-query-log: required upstream "
            "correlation\n"
            '        f"No results for {query}"\n'
            "    )\n"
        )
        assert _check_query_code(code) == []

    @pytest.mark.parametrize(
        "comment",
        [
            # The accidental one: the comment written while cleaning these up.
            "# TODO: drop the allow-search-query-log: marker below",
            # The marker must open the comment, not merely occur in it.
            "# xxallow-search-query-log: y",
            # A written reason, not a single punctuation character.
            "# allow-search-query-log: .",
            "# allow-search-query-log: short",
        ],
    )
    def test_unqualified_marker_does_not_suppress_query(self, comment):
        code = (
            "def _get_previews(self, query):\n"
            f'    logger.info(f"empty {{query}}")  {comment}\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_in_bind_chain_at_debug_level(self):
        """``debug`` is in the shared level set; the bind branch omitted it.

        ``logger.bind(query=query).debug(...)`` was the one shape neither the
        direct-call rule (its receiver is a Call, not the ``logger`` Name) nor
        the bind-chain rule (its level set had no ``debug``) inspected.
        """
        code = (
            "def _get_previews(self, query):\n"
            '    logger.bind(query=query).debug("No preview results")\n'
        )
        assert len(_check_query_code(code)) == 1

    def test_detects_query_passed_as_a_brace_style_keyword_argument(self):
        """``logger.info("...{q}", q=query)`` is the style loguru wants.

        ``check-loguru-formatting.py`` pushes writers toward brace
        placeholders, which put the query in a keyword argument rather than
        in the message expression, so the keyword scan is the only branch
        that sees it.
        """
        code = (
            "def _get_previews(self, query):\n"
            '    logger.info("No results for {q}", q=query)\n'
            '    logger.warning("Empty: {q}", q=query[:100])\n'
        )
        assert len(_check_query_code(code)) == 2

    def test_reason_length_counts_non_space_characters_not_stripped_length(
        self,
    ):
        """``QUERY_LOG_REASON_MIN_CHARS`` counts non-space characters.

        The check is ``len("".join(reason.split()))``, not
        ``len(reason.strip())`` — a reason padded with internal whitespace
        can be long after ``.strip()`` while carrying few actual words.
        """
        reason_ok = "a b c d e f g h"  # 8 non-space chars: accepted
        assert len("".join(reason_ok.split())) == 8
        code_ok = (
            "def _get_previews(self, query):\n"
            "    logger.info(  # allow-search-query-log: " + reason_ok + "\n"
            '        f"rejected: {query}"\n'
            "    )\n"
        )
        assert _check_query_code(code_ok) == []

        # Same reason, padded with extra internal spaces: .strip() alone
        # leaves 21 characters (well over the 8-char minimum), but only 3 are
        # non-space, so this must still be rejected.
        reason_padded = "  a         b         c  "
        assert len(reason_padded.strip()) == 21
        assert len("".join(reason_padded.split())) == 3
        code_padded = (
            "def _get_previews(self, query):\n"
            "    logger.info(  # allow-search-query-log:" + reason_padded + "\n"
            '        f"rejected: {query}"\n'
            "    )\n"
        )
        assert len(_check_query_code(code_padded)) == 1

    def test_tokenize_error_fails_closed_and_does_not_suppress(self):
        """A tokenize failure must never leave a stale suppression standing.

        The constructor tokenizes ``source_lines`` up front and, on
        ``TokenError``/``IndentationError``, clears
        ``query_log_suppression_lines`` (fail-closed) rather than keeping
        whatever markers it had already collected. Feed the checker a
        well-formed AST (so ``visit()`` can run) alongside source lines that
        break ``tokenize`` (an unterminated string) to exercise that branch
        directly — a marker that would otherwise clearly suppress the call
        does not.
        """
        good_source = (
            "def _get_previews(self, query):\n"
            "    logger.info(  # allow-search-query-log: enough reason given here\n"
            '        f"empty {query}"\n'
            "    )\n"
        )
        # Sanity: this exact source, tokenized cleanly, does suppress.
        assert _check_query_code(good_source) == []

        broken_source_lines = (good_source + '    x = "unterminated\n').split(
            "\n"
        )
        tree = ast.parse(good_source)
        checker = SensitiveLoggingChecker(
            IN_DIR, source_lines=broken_source_lines
        )
        assert checker.query_log_suppression_lines == set()
        checker.visit(tree)
        assert any("search query" in e for e in checker.errors)


@pytest.mark.parametrize(
    "separator", ["\u2028", "\u2029", "\x85", "\v", "\f", "\x1c"]
)
@pytest.mark.parametrize("comment_on_query", [False, True])
def test_query_exemption_uses_physical_source_lines(
    tmp_path, separator, comment_on_query
):
    source = 'note = """' + separator + '"""\n'
    comment = " # allow-search-query-log: required upstream correlation"
    source += (
        'logger.info("safe")' + ("" if comment_on_query else comment) + "\n"
    )
    source += 'logger.info(f"{query}")' + (comment if comment_on_query else "")
    path = tmp_path / "src/local_deep_research/web_search_engines/example.py"
    path.parent.mkdir(parents=True)
    path.write_text(source, encoding="utf-8")

    errors = [
        error
        for error in hook_module.check_file(path)
        if "search query" in error
    ]
    assert bool(errors) is not comment_on_query