Skip to content

alexgreensh/repo-forensics / skills/repo-forensics

repo-forensics

Flagged

Security forensics for git repos, AI skills, and MCP servers. Audits dependencies, detects prompt injection, credential theft, runtime dynamism, manifest drift, known CVEs, CISA KEV (actively exploited) vulns, and 2026 attack patterns. Not for fixing vulnerabilities or pentesting.

FileKindSize
.gitignoreother193 B
SKILL.mdskill md43.4 KB
checksums.jsonconfig10.8 KB
checksums.json.sigother64 B
data/README.mdmarkdown1.3 KB
data/compromised_versions.jsonconfig188.3 KB
data/rule_ids.csvother35.3 KB
data/rulepacks/.gitkeepother221 B
data/rulepacks/dead_anchors.jsonconfig9.0 KB
data/rulepacks/mcp_security.jsonconfig31.2 KB
data/rulepacks/runtime_dynamism.jsonconfig46.7 KB
data/rulepacks/sast.jsonconfig107.6 KB
data/rulepacks/secrets.jsonconfig30.4 KB
data/rulepacks/shared.jsonconfig4.4 KB
data/rulepacks/skill_threats.jsonconfig95.4 KB
data/runtime_floors.jsonconfig2.3 KB
data/yara/cryptominers.yarother1.8 KB
data/yara/hacktools.yarother2.2 KB
data/yara/malware.yarother2.2 KB
data/yara/manifest.jsonconfig8.0 KB
data/yara/webshells.yarother2.5 KB
references/cursor-hook-contract.mdmarkdown5.6 KB
references/kernel_privesc_sandbox_pack.mdmarkdown5.0 KB
references/mcp-attack-patterns.mdmarkdown8.7 KB
references/research_sources.mdmarkdown8.0 KB
references/threat_patterns.mdmarkdown20.3 KB
scripts/_context_gate.pyscript21.7 KB
scripts/_ed25519.pyscript5.1 KB
scripts/_pyc_unmarshal.pyscript17.1 KB
scripts/_shared_patterns.pyscript4.8 KB
scripts/adjudication.pyscript16.4 KB
scripts/adjudication_bridge.pyscript1.0 KB
scripts/aggregate_json.pyscript69.2 KB
scripts/auto_scan.pyexecutablescript152.1 KB
scripts/corpus_sync.pyscript10.0 KB
scripts/cursor_pre_scan.pyscript22.5 KB
scripts/dast_sandbox.sbother1.6 KB
scripts/dead_anchors_extract.pyscript14.8 KB
scripts/dead_anchors_probe.pyscript27.1 KB
scripts/forensics_core.pyscript176.5 KB
scripts/gen_rule_ids.pyscript8.6 KB
scripts/hook_adapter.pyscript19.4 KB
scripts/ioc_manager.pyscript49.6 KB
scripts/parse_pnpm_lock.pyscript7.9 KB
scripts/pre_scan.pyscript143.5 KB
scripts/pypi_pins.pyscript8.1 KB
scripts/refresh_controller.pyexecutablescript48.4 KB
scripts/refresh_threat_dbs.pyexecutablescript19.4 KB
scripts/rule_loader.pyscript44.8 KB
scripts/rulepack_feed.pyscript26.4 KB
scripts/run_forensics.shexecutablescript20.3 KB
scripts/sarif_export.pyscript18.6 KB
scripts/scan_agent_skills.pyscript33.2 KB
scripts/scan_archive.pyscript29.1 KB
scripts/scan_ast.pyscript44.9 KB
scripts/scan_binary.pyscript10.9 KB
scripts/scan_bytecode.pyscript28.2 KB
scripts/scan_dast.pyscript28.8 KB
scripts/scan_dataflow.pyscript15.4 KB
scripts/scan_dead_anchors.pyscript15.9 KB
scripts/scan_decode.pyscript37.5 KB
scripts/scan_dependencies.pyscript124.5 KB
scripts/scan_devcontainer.pyscript8.5 KB
scripts/scan_entropy.pyscript15.6 KB
scripts/scan_entrypoint.pyscript34.3 KB
scripts/scan_git_config.pyscript96.3 KB
scripts/scan_git_forensics.pyscript24.1 KB
scripts/scan_history.pyscript14.5 KB
scripts/scan_infra.pyscript54.3 KB
scripts/scan_integrity.pyscript41.0 KB
scripts/scan_lifecycle.pyscript65.1 KB
scripts/scan_manifest_drift.pyscript47.0 KB
scripts/scan_mcp_security.pyscript42.3 KB
scripts/scan_oversize.pyscript11.7 KB
scripts/scan_post_incident.pyscript36.9 KB
scripts/scan_provenance.pyscript19.4 KB
scripts/scan_runtime_dynamism.pyscript23.5 KB
scripts/scan_runtime_floors.pyscript33.5 KB
scripts/scan_sast.pyscript59.1 KB
scripts/scan_secrets.pyscript11.9 KB
scripts/scan_skill_threats.pyscript102.4 KB
scripts/scan_splitstream.pyscript29.5 KB
scripts/scan_yara.pyscript19.7 KB
scripts/session_scan.pyscript54.1 KB
scripts/suppressions.pyscript15.4 KB
scripts/validate_manifests.pyscript13.7 KB
scripts/verify_install.pyscript26.2 KB
scripts/vuln_feed.pyscript31.1 KB
tests/conftest.pyscript13.4 KB
tests/corpus/DEAD_ANCHORS_CORPUS.mdmarkdown2.3 KB
tests/corpus/budgets.jsonconfig5.6 KB
tests/cursor_helpers.pyscript12.6 KB
tests/fixtures/legacy_main_blocks.jsonconfig10.3 KB
tests/fixtures/powershell_verdict_corpus.jsonconfig114.9 KB
tests/golden/parity_mcp_security.jsonconfig17.5 KB
tests/golden/parity_runtime_dynamism.jsonconfig31.7 KB
tests/golden/parity_sast.jsonconfig59.8 KB
tests/golden/parity_secrets.jsonconfig18.8 KB
tests/golden/parity_skill_threats.jsonconfig48.8 KB
tests/parity_corpus.pyscript6.3 KB
tests/powershell_hook.pyscript8.2 KB
tests/report_builders.pyscript5.0 KB
tests/schemas/sarif-schema-2.1.0.jsonconfig109.1 KB
tests/shell_compat.pyscript2.2 KB
tests/test_adjudication_bridge.pyscript832 B
tests/test_adjudication_containment.pyscript1.4 KB
tests/test_adjudication_output.pyscript20.0 KB
tests/test_adjudication_suppressions.pyscript18.7 KB
tests/test_advisory_adjudication.pyscript1.3 KB
tests/test_agent_relay_rules.pyscript7.8 KB
tests/test_aggregate_json.pyscript10.0 KB
tests/test_auto_scan_patterns.pyscript10.5 KB
tests/test_benign_corpus.pyscript16.4 KB
tests/test_benign_new_scanners.pyscript6.1 KB
tests/test_build_output_skip.pyscript10.6 KB
tests/test_capability_ledger.pyscript18.8 KB
tests/test_codex_install.pyscript10.0 KB
tests/test_codex_marketplace.pyscript3.3 KB
tests/test_comment_false_positives.pyscript2.2 KB
tests/test_composer_dependencies.pyscript4.0 KB
tests/test_compromised_versions.pyscript51.2 KB
tests/test_context_gate.pyscript32.4 KB
tests/test_coverage_status.pyscript7.6 KB
tests/test_css_compounding_shrink.pyscript20.8 KB
tests/test_cursor_adapter.pyscript7.8 KB
tests/test_cursor_benign.pyscript3.4 KB
tests/test_cursor_degrade.pyscript12.7 KB
tests/test_cursor_install.pyscript17.1 KB
tests/test_cursor_latency.pyscript7.2 KB
tests/test_cursor_parity.pyscript5.8 KB
tests/test_cursor_redteam_fn.pyscript4.9 KB
tests/test_cursor_tamper_modify.pyscript14.2 KB
tests/test_cursor_wiring.pyscript32.3 KB
tests/test_d1_yara_correlation.pyscript9.2 KB
tests/test_dead_anchors_extract.pyscript7.6 KB
tests/test_dead_anchors_probe.pyscript19.7 KB
tests/test_evidence_engine_parity.pyscript8.1 KB
tests/test_evidence_model.pyscript11.4 KB
tests/test_forensics_core.pyscript79.2 KB
tests/test_fp_hardening_r3.pyscript9.7 KB
tests/test_fp_hardening_r4.pyscript18.8 KB
tests/test_fp_regression_pairs.pyscript62.5 KB
tests/test_fp_structural.pyscript21.2 KB
tests/test_freshness.pyscript6.2 KB
tests/test_frontmatter_validity.pyscript7.0 KB
tests/test_gpg_rce_hotfix.pyscript4.8 KB
tests/test_hook_inert_data.pyscript6.1 KB
tests/test_hook_scripts.pyscript14.0 KB
tests/test_inspection_cpu_budget.pyscript11.6 KB
tests/test_ioc_manager.pyscript55.4 KB
tests/test_kernel_privesc_rules.pyscript27.8 KB
tests/test_legit_precision_regressions.pyscript6.8 KB
tests/test_lethal_trifecta.pyscript20.7 KB
tests/test_manifest_signature.pyscript1.5 KB
tests/test_manifest_validity.pyscript2.9 KB
tests/test_monotonic_trust_e2e.pyscript4.4 KB
tests/test_non_breaking_contract.pyscript22.7 KB
tests/test_openclaw_install.pyscript6.6 KB
tests/test_pack_parity.pyscript24.8 KB
tests/test_parse_pnpm_lock.pyscript10.7 KB
tests/test_plugin_checkout_provenance.pyscript11.1 KB
tests/test_powershell_hook_wire.pyscript10.6 KB
tests/test_powershell_verdict_corpus.pyscript6.2 KB
tests/test_pr44_hardening.pyscript6.5 KB
tests/test_pre_scan.pyscript19.6 KB
tests/test_pre_scan_composer.pyscript7.4 KB
tests/test_precondition_fields.pyscript4.6 KB
tests/test_provenance_offline.pyscript2.5 KB
tests/test_published_rulepack_feed_current.pyscript1.4 KB
tests/test_py38_compat.pyscript3.3 KB
tests/test_pypi_pinned_versions.pyscript19.3 KB
tests/test_pyproject_string_forms.pyscript2.2 KB
tests/test_redteam_fn_regression.pyscript32.5 KB
tests/test_reflective_fetch_execute.pyscript23.5 KB
tests/test_reflective_review_fixes.pyscript17.8 KB
tests/test_refresh_controller.pyscript19.9 KB
tests/test_refresh_rulepacks.pyscript12.4 KB
tests/test_review_gate_regressions.pyscript9.3 KB
tests/test_rule_loader.pyscript23.2 KB
tests/test_rulepack_feed.pyscript38.1 KB
tests/test_run_forensics_json.pyscript1012 B
tests/test_run_scanner_fail_loud.pyscript6.5 KB
tests/test_runtime_floors.pyscript19.8 KB
tests/test_sandbox_probing_rules.pyscript9.6 KB
tests/test_sarif_export.pyscript19.9 KB
tests/test_scan_agent_skills.pyscript31.3 KB
tests/test_scan_archive.pyscript11.6 KB
tests/test_scan_ast.pyscript12.8 KB
tests/test_scan_binary.pyscript15.3 KB
tests/test_scan_bytecode.pyscript21.5 KB
tests/test_scan_dast.pyscript32.1 KB
tests/test_scan_dataflow.pyscript7.1 KB
tests/test_scan_dead_anchors.pyscript11.5 KB
tests/test_scan_decode.pyscript28.0 KB
tests/test_scan_dependencies.pyscript37.8 KB
tests/test_scan_devcontainer.pyscript8.5 KB
tests/test_scan_entropy.pyscript11.2 KB
tests/test_scan_entrypoint.pyscript19.5 KB
tests/test_scan_git_config.pyscript58.5 KB
tests/test_scan_git_config_review_fixes.pyscript33.3 KB

scripts/scan_runtime_dynamism.py

View on GitHub
#!/usr/bin/env python3
"""
scan_runtime_dynamism.py - Runtime Behavior Prediction Scanner (v1)
Detects static indicators that code will behave differently at runtime:
dynamic imports, fetch-then-execute, self-modification, time bombs,
and dynamic tool descriptions.

Pure static analysis. Zero new dependencies.

Research basis:
- CVE-2026-2297: Python SourcelessFileLoader audit bypass
- PylangGhost RAT (March 2026): benign v1.0.0 -> weaponized v1.0.1
- Socket.dev NuGet time bombs (Nov 2025): hardcoded activation dates
- Check Point MCP rug pull (Feb 2026): dynamic tool descriptions
- OWASP MCP03 (Tool Poisoning), MCP07 (Rug Pull)
- Lukas Kania: MCP contract diffs (March 2026)

Created by Alex Greenshpun
"""

import os
import ast
import re
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import forensics_core as core
import rule_loader
import _context_gate

SCANNER_NAME = "runtime_dynamism"

# ============================================================
# Detection rules load from the shipped pack at import time (rule_loader
# memoizes, so this parses once per process). The 8 categories below preserve
# the pre-extraction severity-per-category mapping (severity drives the parity
# key). The AST visitor, two-stage scan, and dedup are scanning *algorithm* and
# stay in code (KTD-3). load_pack returns None only on a missing/tampered
# install -> PACK_LOAD_ERROR, one loud diagnostic, no hardcoded fallback.
# ============================================================
_PACK = rule_loader.load_pack(SCANNER_NAME)
PACK_LOAD_ERROR = _PACK is None

# Output category -> emitted severity (the pre-extraction call-site default).
# Rules carry their category in the pack; we group by it so each category emits
# at its historical severity, preserving the (title, severity, ...) parity key.
_CATEGORY_SEVERITY = {
    "dynamic-import": "high",
    "fetch-execute": "critical",
    "self-modification": "critical",
    "time-bomb": "medium",
    "worm-propagation": "critical",
    "probabilistic-activation": "high",
    "environment-detection": "medium",
    "cloud-ide-detection": "low",
    "locale-gating": "medium",
}


def _rules_by_category():
    """Group the loaded pack's rules by output category (stable order)."""
    grouped = {}
    if _PACK is not None:
        for rule in _PACK.all_rules:
            grouped.setdefault(rule.category, []).append(rule)
    return grouped


_RULES_BY_CATEGORY = _rules_by_category()


def _pack_load_finding(rel_path):
    """One loud diagnostic emitted when the rule pack failed to load. We do NOT
    fall back to a hardcoded copy (a corrupted install is caught independently
    by the integrity scanner)."""
    return core.Finding(
        scanner=SCANNER_NAME, severity="critical",
        title="Runtime-dynamism rule pack failed to load",
        description=("data/rulepacks/runtime_dynamism.json is missing or "
                     "schema-incompatible; regex-based runtime-dynamism "
                     "detection is disabled. Reinstall repo-forensics to "
                     "restore detection."),
        file=rel_path, line=0,
        snippet="rule pack failed to load",
        category="scanner-integrity",
    )


class RuntimeDynamismASTVisitor(ast.NodeVisitor):
    """AST visitor for patterns that regex alone can't reliably catch."""

    def __init__(self, rel_path, source_lines):
        self.rel_path = rel_path
        self.source_lines = source_lines
        self.findings = []

    def _snippet(self, lineno):
        if lineno and 1 <= lineno <= len(self.source_lines):
            return self.source_lines[lineno - 1].strip()[:120]
        return ""

    def _add(self, severity, title, description, lineno, category):
        self.findings.append(core.Finding(
            scanner=SCANNER_NAME, severity=severity,
            title=title, description=description,
            file=self.rel_path, line=lineno or 0,
            snippet=self._snippet(lineno),
            category=category
        ))

    def _is_variable(self, node):
        """Check if a node is a variable (not a string literal)."""
        return isinstance(node, (ast.Name, ast.Attribute, ast.Subscript, ast.Call, ast.IfExp))

    def visit_Call(self, node):
        lineno = getattr(node, 'lineno', None)

        # importlib.import_module(variable) - AST-level check
        if (isinstance(node.func, ast.Attribute) and
                node.func.attr == 'import_module' and
                isinstance(node.func.value, ast.Name) and
                node.func.value.id == 'importlib'):
            if node.args and self._is_variable(node.args[0]):
                self._add(
                    "high",
                    "Dynamic Import: importlib.import_module(variable)",
                    "Module loaded at runtime from variable. Actual module unknown at install time.",
                    lineno,
                    "dynamic-import"
                )

        # importlib.reload() - runtime code replacement
        if (isinstance(node.func, ast.Attribute) and
                node.func.attr == 'reload' and
                isinstance(node.func.value, ast.Name) and
                node.func.value.id == 'importlib'):
            self._add(
                "high",
                "Runtime Module Reload: importlib.reload()",
                "Module reloaded at runtime. Code can change between calls.",
                lineno,
                "dynamic-import"
            )

        # NOTE: types.FunctionType, types.CodeType, marshal.loads/load,
        # sys.addaudithook are detected by scan_ast.py (patterns 8-10).
        # This AST visitor only covers patterns unique to runtime dynamism.

        self.generic_visit(node)

    def visit_Compare(self, node):
        """Detect time bomb patterns in AST: datetime comparisons."""
        lineno = getattr(node, 'lineno', None)

        # Check for datetime.now() > datetime(...) or date.today() > date(...)
        left = node.left
        for comparator in node.comparators:
            if self._is_datetime_call(left) and self._is_datetime_constructor(comparator):
                self._add(
                    "high",
                    "Time Bomb Pattern: datetime comparison with hardcoded date",
                    "Code path activated based on current date/time reaching a hardcoded value.",
                    lineno,
                    "time-bomb"
                )
            elif self._is_datetime_call(comparator) and self._is_datetime_constructor(left):
                self._add(
                    "high",
                    "Time Bomb Pattern: hardcoded date compared to current datetime",
                    "Code path activated based on current date/time reaching a hardcoded value.",
                    lineno,
                    "time-bomb"
                )

        self.generic_visit(node)

    def _is_datetime_call(self, node):
        """Check if node is datetime.now(), date.today(), time.time()."""
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
            attr = node.func.attr
            if attr in ('now', 'today', 'utcnow'):
                return True
        return False

    def _is_datetime_constructor(self, node):
        """Check if node is datetime(YYYY, ...) or date(YYYY, ...) with a literal year."""
        if isinstance(node, ast.Call):
            if node.args and isinstance(node.args[0], ast.Constant):
                val = node.args[0].value
                if isinstance(val, int) and 2020 <= val <= 2099:
                    return True
        return False


def scan_file_regex(file_path, rel_path):
    """Run regex-based detection on a single file."""
    ext = os.path.splitext(file_path)[1].lower()
    target_exts = {'.py', '.js', '.ts', '.mjs', '.cjs', '.jsx', '.tsx', '.mts', '.sh', '.bash'}
    if ext not in target_exts:
        return []

    try:
        with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
            content = f.read()
    except (OSError, UnicodeDecodeError):
        return []

    if not content.strip():
        return []

    if PACK_LOAD_ERROR:
        return [_pack_load_finding(rel_path)]

    findings = []

    # Each pack rule carries its output category; emit per category at the
    # historical severity (preserves the (title, severity, file, line, category)
    # parity key). scan_rule_patterns stamps rule_id + confidence onto every
    # finding. The two probabilistic-activation tiers and the environment vs
    # locale split that used to be slice-based are now expressed by the rule's
    # own category, so the call-site no longer needs slice arithmetic.
    for category, severity in _CATEGORY_SEVERITY.items():
        rules = _RULES_BY_CATEGORY.get(category)
        if not rules:
            continue
        findings.extend(core.scan_rule_patterns(
            content, rel_path, rules, category, severity, SCANNER_NAME
        ))

    # The shipped or signed-overlay regex can match import(node) at the end
    # of a helper name such as _render_import(node). Require the JS keyword.
    content_lines = content.splitlines()
    js_import_call = re.compile(r"(?<![\w$.])import\s*\(")
    findings = [
        f for f in findings
        if f.rule_id not in {"RD-DYN-006", "RD-DYN-008"}
        or not (1 <= f.line <= len(content_lines))
        or js_import_call.search(content_lines[f.line - 1])
    ]

    # A time bomb needs a comparison against the counter. `calls=40` in a call or
    # a default (a lone `=`) assigns a value; it only counts as a threshold when
    # the line also shows a branch or test (if/while/where/&&/?/[ ...).
    findings = [
        f for f in findings
        if f.rule_id != "RD-TB-007"
        or not (1 <= f.line <= len(content_lines))
        or not _counter_assignment_only(content_lines[f.line - 1])
    ]

    # __import__("json") or a lazy __import__("pkg.tools") reaches no
    # capability; only a computed name or a capability module stays high.
    findings = core.downgrade_inert_literal_imports(findings, content, {"RD-DYN-004"},
                                                    file_path, rel_path)

    # module_from_spec constructs a module but does not execute it. Treat a
    # standalone call as HIGH; retain CRITICAL when its function writes a file
    # before loading and executes the resulting module. Test fixtures keep the
    # existing inferred-evidence cap, and the finding always stays visible.
    loader_tree = None
    if any(f.rule_id in {"RD-SMOD-003", "RD-SMOD-004"} for f in findings) and ext == '.py':
        try:
            loader_tree = ast.parse(content, filename=file_path)
        except (SyntaxError, ValueError, RecursionError):
            pass
    is_test = (any(f.rule_id in {"RD-SMOD-003", "RD-SMOD-004"} for f in findings)
               and _context_gate.classify_file_context(rel_path, content).is_test_fixture)
    for f in findings:
        if f.rule_id == "RD-SMOD-003":
            try:
                if is_test:
                    f.evidence_class = "inferred"
                elif loader_tree is not None and not _write_then_load(loader_tree, f.line):
                    if _fixed_sibling_loader(loader_tree, f.line, _repo_depth(rel_path)):
                        f.evidence_class = "inferred"
                    else:
                        f.severity = "high"
            except Exception:
                pass
        elif (f.rule_id == "RD-SMOD-004" and is_test and loader_tree is not None
              and _test_generated_compile(loader_tree, f.line)):
            f.evidence_class = "inferred"

    return findings


def _write_then_load(tree, load_line):
    """Find a file write and exec_module around one loader in its Python scope."""
    scope = tree
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            if node.lineno <= load_line <= getattr(node, 'end_lineno', node.lineno):
                if scope is tree or (node.end_lineno - node.lineno
                                     < scope.end_lineno - scope.lineno):
                    scope = node

    wrote = False
    executed = False
    for node in ast.walk(scope):
        if not isinstance(node, ast.Call):
            continue
        if load_line - 30 <= node.lineno < load_line:
            if isinstance(node.func, ast.Attribute) and node.func.attr in ('write_text', 'write_bytes'):
                wrote = True
            elif isinstance(node.func, ast.Name) and node.func.id == 'open':
                mode = node.args[1] if len(node.args) > 1 else next(
                    (kw.value for kw in node.keywords if kw.arg == 'mode'), None)
                if isinstance(mode, ast.Constant) and isinstance(mode.value, str):
                    wrote = wrote or any(flag in mode.value for flag in 'wax+')
        elif (load_line <= node.lineno <= load_line + 30
              and isinstance(node.func, ast.Attribute)
              and node.func.attr == 'exec_module'):
            executed = True
    return wrote and executed


def _test_generated_compile(tree, compile_line):
    """Recognize a test compiling source returned by a local generator."""
    scope = tree
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            if node.lineno <= compile_line <= getattr(node, 'end_lineno', node.lineno):
                if scope is tree or (node.end_lineno - node.lineno
                                     < scope.end_lineno - scope.lineno):
                    scope = node
    if scope is tree:
        return False

    generated = set()
    for node in sorted(ast.walk(scope), key=lambda item: getattr(item, 'lineno', 0)):
        if isinstance(node, ast.Assign) and node.lineno < compile_line:
            value = node.value
            from_generator = (isinstance(value, ast.Call)
                              and isinstance(value.func, ast.Attribute)
                              and value.func.attr.startswith('_generate_'))
            from_slice = (isinstance(value, ast.Subscript)
                          and isinstance(value.value, ast.Name)
                          and value.value.id in generated)
            for target in node.targets:
                if isinstance(target, ast.Name):
                    if from_generator or from_slice:
                        generated.add(target.id)
                    else:
                        generated.discard(target.id)
        if (isinstance(node, ast.Call) and node.lineno == compile_line
                and isinstance(node.func, ast.Name) and node.func.id == 'compile'
                and node.args and isinstance(node.args[0], ast.Name)
                and node.args[0].id in generated):
            return True
    return False


_AST_INDEX = ast.Index if sys.version_info < (3, 9) else None


def _repo_depth(rel_path):
    parts = [p for p in (rel_path or '').replace('\\', '/').split('/') if p not in ('', '.')]
    if '..' in parts:
        return 0
    return max(len(parts) - 1, 0)


def _fixed_sibling_loader(tree, load_line, repo_depth=0):
    """Prove the spec path is a literal .py file rooted at __file__ that stays
    inside the scanned tree. `repo_depth` is how many folders sit between the
    file and the scan root."""
    scope = tree
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            if node.lineno <= load_line <= getattr(node, 'end_lineno', node.lineno):
                if scope is tree or (node.end_lineno - node.lineno
                                     < scope.end_lineno - scope.lineno):
                    scope = node

    assignments = {}
    ambiguous = set()
    def collect(body):
        for node in body:
            if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
                continue
            if isinstance(node, ast.Assign) and node.lineno < load_line:
                for target in node.targets:
                    if isinstance(target, ast.Name):
                        if target.id in assignments:
                            ambiguous.add(target.id)
                        assignments[target.id] = node.value
            for field in ('body', 'orelse', 'finalbody'):
                nested = getattr(node, field, None)
                if isinstance(nested, list):
                    collect(nested)
            for handler in getattr(node, 'handlers', ()):
                collect(handler.body)

    collect(tree.body)
    if scope is not tree:
        collect(scope.body)

    # path_proof returns (rooted, fixed_py, ups): rooted at __file__ through
    # fixed steps only, ends in a literal .py name, and how many directory
    # levels it climbs from the file (the file's own folder counts as one).
    _NO = (False, False, 0)

    def safe_part(value):
        return (isinstance(value, str) and value not in ('', '.', '..')
                and '/' not in value and '\\' not in value and ':' not in value)

    def path_proof(expr, seen=frozenset()):
        if isinstance(expr, ast.Name):
            if expr.id == '__file__':
                return True, True, 0
            if expr.id in seen or expr.id not in assignments or expr.id in ambiguous:
                return _NO
            return path_proof(assignments[expr.id], seen | {expr.id})
        if isinstance(expr, ast.Attribute) and expr.attr in ('parent', 'resolve'):
            rooted, fixed_py, ups = path_proof(expr.value, seen)
            if expr.attr == 'resolve':
                return rooted, fixed_py, ups
            return rooted, False, ups + 1
        if isinstance(expr, ast.BinOp) and isinstance(expr.op, ast.Div):
            rooted, _, ups = path_proof(expr.left, seen)
            if (rooted and isinstance(expr.right, ast.Constant)
                    and safe_part(expr.right.value)):
                return True, expr.right.value.endswith('.py'), ups
            return _NO
        index = getattr(expr, 'slice', None)
        if _AST_INDEX is not None and isinstance(index, _AST_INDEX):
            index = index.value                      # Python 3.8 wraps the index
        if (isinstance(expr, ast.Subscript) and isinstance(expr.value, ast.Attribute)
                and expr.value.attr == 'parents'
                and isinstance(index, ast.Constant)
                and type(index.value) is int and 0 <= index.value < 64):
            # Path(__file__).resolve().parents[1]: a fixed ancestor directory.
            rooted, _, ups = path_proof(expr.value.value, seen)
            return rooted, False, ups + index.value + 1
        if (isinstance(expr, ast.Call) and isinstance(expr.func, ast.Attribute)
                and isinstance(expr.func.value, ast.Attribute)
                and expr.func.value.attr == 'path'
                and isinstance(expr.func.value.value, ast.Name)
                and expr.func.value.value.id == 'os'
                and expr.args and not expr.keywords):
            fn = expr.func.attr
            if fn in ('abspath', 'realpath', 'normpath') and len(expr.args) == 1:
                return path_proof(expr.args[0], seen)
            if fn == 'dirname' and len(expr.args) == 1:
                rooted, _, ups = path_proof(expr.args[0], seen)
                return rooted, False, ups + 1
            if fn == 'join' and len(expr.args) >= 2:
                rooted, _, ups = path_proof(expr.args[0], seen)
                parts = expr.args[1:]
                if rooted and all(isinstance(p, ast.Constant) and safe_part(p.value)
                                  for p in parts):
                    return True, parts[-1].value.endswith('.py'), ups
            return _NO
        if isinstance(expr, ast.Call):
            name = (expr.func.id if isinstance(expr.func, ast.Name)
                    else expr.func.attr if isinstance(expr.func, ast.Attribute) else '')
            if name in ('Path', 'str') and expr.args:
                return path_proof(expr.args[0], seen)
            if name in ('resolve', 'expanduser') and isinstance(expr.func, ast.Attribute):
                return path_proof(expr.func.value, seen)
            if name == 'next' and expr.args and isinstance(expr.args[0], ast.GeneratorExp):
                gen = expr.args[0]
                if len(gen.generators) == 1:
                    return path_proof(gen.generators[0].iter, seen)
        if isinstance(expr, (ast.Tuple, ast.List)) and expr.elts:
            proofs = [path_proof(item, seen) for item in expr.elts]
            return (all(p[0] for p in proofs), all(p[1] for p in proofs),
                    max(p[2] for p in proofs))
        return _NO

    for node in ast.walk(scope):
        if not (isinstance(node, ast.Call) and node.lineno == load_line
                and isinstance(node.func, ast.Attribute)
                and node.func.attr == 'module_from_spec'
                and node.args and isinstance(node.args[0], ast.Name)):
            continue
        if node.args[0].id in ambiguous:
            return False
        spec = assignments.get(node.args[0].id)
        if not (isinstance(spec, ast.Call) and isinstance(spec.func, ast.Attribute)
                and spec.func.attr == 'spec_from_file_location' and len(spec.args) >= 2):
            return False
        rooted, fixed_py, ups = path_proof(spec.args[1])
        # Climbing above the scanned tree (parents[4] / ".cache" / x.py) is not
        # a sibling: only the file's folder or an ancestor inside the repo is.
        return rooted and fixed_py and max(ups - 1, 0) <= repo_depth
    return False


def scan_file_ast(file_path, rel_path):
    """Run AST-based detection on Python files."""
    if not file_path.endswith('.py'):
        return []

    try:
        with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
            source = f.read()
    except (OSError, UnicodeDecodeError):
        return []

    if not source.strip():
        return []

    try:
        tree = ast.parse(source, filename=file_path)
    except (SyntaxError, ValueError, RecursionError):
        return []

    source_lines = source.split('\n')
    visitor = RuntimeDynamismASTVisitor(rel_path, source_lines)
    visitor.visit(tree)
    return visitor.findings


_COUNTER_TERM = re.compile(r"(?:count|counter|attempts?|calls?|invocations?)\s*([><=!]+)\s*\d{2,}", re.IGNORECASE)
_BRANCH_CONTEXT = re.compile(
    r"\b(?:if|elif|elsif|else|while|until|unless|when|where|having|case|switch|assert|and|or|not|then|return|yield)\b"
    r"|&&|\|\||\?|\[|\bselect\b|=>|:=|\blambda\b|\bfilter\b", re.IGNORECASE)


def _counter_assignment_only(line):
    """True when every counter match on the line is a plain `=` (keyword argument
    or assignment) and nothing on the line is a branch or test."""
    ops = [m.group(1) for m in _COUNTER_TERM.finditer(line)]
    return bool(ops) and all(op == "=" for op in ops) and not _BRANCH_CONTEXT.search(line)


def scan_file(file_path, rel_path):
    """Scan a single file for runtime dynamism indicators."""
    findings = []
    findings.extend(scan_file_regex(file_path, rel_path))
    findings.extend(scan_file_ast(file_path, rel_path))

    # Deduplicate findings on same line with same category
    seen = set()
    deduped = []
    for f in findings:
        key = (f.file, f.line, f.category)
        if key not in seen:
            seen.add(key)
            deduped.append(f)

    return deduped


def main():
    args = core.parse_common_args(sys.argv, "Runtime Behavior Prediction Scanner")
    repo_path = args.repo_path

    core.emit_status(args.format, f"[*] Scanning {repo_path} for runtime dynamism indicators...")

    ignore_patterns = core.load_ignore_patterns(repo_path)
    all_findings = []

    for file_path, rel_path in core.walk_repo(repo_path, ignore_patterns, skip_binary=True):
        findings = scan_file(file_path, rel_path)
        all_findings.extend(findings)

    core.output_findings(all_findings, args.format, SCANNER_NAME)


if __name__ == "__main__":
    main()
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall