Skip to content

sb-dev/openquant / .agents/skills/rust-review

rust-review

Verified

Review Rust crates, services, or libraries for safe/unsafe boundary issues, memory safety, concurrency hazards, panic-induced denial of service, FFI safety, and async runtime mistakes. Use for a Rust security audit, especially unsafe, FFI, or concurrent code.

FileKindSize
SKILL.mdskill md5.4 KB
upstream/.claude-plugin/plugin.jsonconfig530 B
upstream/README.mdmarkdown8.2 KB
upstream/agents/rust-review-dedup-judge.mdmarkdown20.4 KB
upstream/agents/rust-review-fp-judge.mdmarkdown23.1 KB
upstream/agents/rust-review-worker.mdmarkdown31.8 KB
upstream/prompts/clusters/async-runtime.mdmarkdown855 B
upstream/prompts/clusters/concurrency-data-race.mdmarkdown2.6 KB
upstream/prompts/clusters/concurrency-locking.mdmarkdown6.9 KB
upstream/prompts/clusters/error-handling.mdmarkdown1.6 KB
upstream/prompts/clusters/ffi-cross-language.mdmarkdown3.3 KB
upstream/prompts/clusters/info-disclosure.mdmarkdown562 B
upstream/prompts/clusters/input-os-safety.mdmarkdown1.1 KB
upstream/prompts/clusters/layout-safety.mdmarkdown572 B
upstream/prompts/clusters/logic-correctness.mdmarkdown3.8 KB
upstream/prompts/clusters/manifest.jsonconfig10.9 KB
upstream/prompts/clusters/memory-safety.mdmarkdown4.6 KB
upstream/prompts/clusters/panic-dos.mdmarkdown6.0 KB
upstream/prompts/clusters/recursion-dos.mdmarkdown5.3 KB
upstream/prompts/clusters/resource-handling.mdmarkdown1.5 KB
upstream/prompts/clusters/static-hygiene.mdmarkdown746 B
upstream/prompts/clusters/unsafe-boundary.mdmarkdown15.6 KB
upstream/prompts/general/abi-mismatch-finder.mdmarkdown705 B
upstream/prompts/general/adversarial-trait-finder.mdmarkdown1.5 KB
upstream/prompts/general/arithmetic-overflow-finder.mdmarkdown2.1 KB
upstream/prompts/general/assertion-reachable-finder.mdmarkdown879 B
upstream/prompts/general/async-blocking-finder.mdmarkdown1.2 KB
upstream/prompts/general/atomic-race-finder.mdmarkdown1.3 KB
upstream/prompts/general/buffer-overflow-unsafe-finder.mdmarkdown2.6 KB
upstream/prompts/general/bufwriter-unflushed-finder.mdmarkdown995 B
upstream/prompts/general/cancel-safety-finder.mdmarkdown1.2 KB
upstream/prompts/general/cargo-lint-config-finder.mdmarkdown2.4 KB
upstream/prompts/general/closure-ffi-finder.mdmarkdown3.9 KB
upstream/prompts/general/closure-panic-finder.mdmarkdown1.1 KB
upstream/prompts/general/collection-key-mutation-finder.mdmarkdown1.9 KB
upstream/prompts/general/cstring-dangling-finder.mdmarkdown1.3 KB
upstream/prompts/general/deprecated-api-finder.mdmarkdown1.9 KB
upstream/prompts/general/destructor-skip-finder.mdmarkdown1.4 KB
upstream/prompts/general/double-free-finder.mdmarkdown1.6 KB
upstream/prompts/general/drop-panic-finder.mdmarkdown973 B
upstream/prompts/general/dyn-trait-ffi-finder.mdmarkdown2.8 KB
upstream/prompts/general/float-edge-finder.mdmarkdown798 B
upstream/prompts/general/foreign-drop-finder.mdmarkdown2.7 KB
upstream/prompts/general/invalid-free-finder.mdmarkdown1.2 KB
upstream/prompts/general/lossy-from-into-finder.mdmarkdown603 B
upstream/prompts/general/lossy-str-conversion-finder.mdmarkdown1.2 KB
upstream/prompts/general/msrv-mismatch-finder.mdmarkdown401 B
upstream/prompts/general/nondeterminism-finder.mdmarkdown1.6 KB
upstream/prompts/general/opaque-pointer-finder.mdmarkdown1.9 KB
upstream/prompts/general/ord-eq-hash-finder.mdmarkdown754 B
upstream/prompts/general/out-of-bounds-index-finder.mdmarkdown727 B
upstream/prompts/general/packed-field-ref-finder.mdmarkdown3.7 KB
upstream/prompts/general/panic-unwind-unsafe-finder.mdmarkdown3.0 KB
upstream/prompts/general/path-traversal-join-finder.mdmarkdown1.7 KB
upstream/prompts/general/pointer-exposure-finder.mdmarkdown1.8 KB
upstream/prompts/general/raw-fd-lifecycle-finder.mdmarkdown2.0 KB
upstream/prompts/general/recursive-deserialize-stack-overflow-finder.mdmarkdown4.8 KB
upstream/prompts/general/recursive-drop-stack-overflow-finder.mdmarkdown2.7 KB
upstream/prompts/general/recursive-format-stack-overflow-finder.mdmarkdown4.1 KB
upstream/prompts/general/refcell-borrow-panic-finder.mdmarkdown1.2 KB
upstream/prompts/general/repr-c-padding-finder.mdmarkdown2.2 KB
upstream/prompts/general/resource-exhaustion-finder.mdmarkdown2.4 KB
upstream/prompts/general/result-discarded-finder.mdmarkdown1.6 KB
upstream/prompts/general/select-bias-finder.mdmarkdown1.2 KB
upstream/prompts/general/send-sync-bounds-finder.mdmarkdown2.6 KB
upstream/prompts/general/serialize-struct-mismatch-finder.mdmarkdown2.3 KB
upstream/prompts/general/shared-memory-race-finder.mdmarkdown2.8 KB
upstream/prompts/general/static-mut-race-finder.mdmarkdown2.8 KB
upstream/prompts/general/str-slice-boundary-finder.mdmarkdown1.0 KB
upstream/prompts/general/string-comparison-finder.mdmarkdown1.4 KB
upstream/prompts/general/toctou-finder.mdmarkdown1.3 KB
upstream/prompts/general/uninitialized-read-finder.mdmarkdown1.8 KB
upstream/prompts/general/union-ub-finder.mdmarkdown3.5 KB
upstream/prompts/general/unsafe-sync-impl-finder.mdmarkdown3.7 KB
upstream/prompts/general/unwrap-on-untrusted-finder.mdmarkdown1.3 KB
upstream/prompts/general/use-after-free-finder.mdmarkdown2.9 KB
upstream/prompts/general/vec-set-len-uninit-finder.mdmarkdown2.5 KB
upstream/scripts/build_run_plan.pyscript24.0 KB
upstream/scripts/generate_sarif.pyexecutablescript22.9 KB
upstream/scripts/pyproject.tomlmanifest220 B
upstream/scripts/test_gating.pyscript12.6 KB
upstream/scripts/test_generate_sarif.pyscript19.5 KB
upstream/scripts/test_prompt_regexes.pyscript4.3 KB
upstream/scripts/test_split.pyscript10.2 KB
upstream/scripts/test_validate_artifacts.pyscript10.3 KB
upstream/scripts/validate_artifacts.pyscript11.4 KB
upstream/skills/rust-review/SKILL.upstream.mdmarkdown43.5 KB

upstream/scripts/test_gating.py

View on GitHub
"""Gating and per-pass filtering tests for build_run_plan.build_selection.

These run against the real cluster manifest and plugin root so the prompt-path
existence checks inside build_selection() resolve.
"""

from __future__ import annotations

import json
from pathlib import Path
from typing import Any

import pytest

import build_run_plan
from build_run_plan import build_selection

PLUGIN_ROOT = Path(__file__).resolve().parent.parent
MANIFEST_PATH = PLUGIN_ROOT / "prompts" / "clusters" / "manifest.json"


def load_manifest() -> dict[str, Any]:
    return json.loads(MANIFEST_PATH.read_text())


def make_flags(
    *,
    has_unsafe: bool = False,
    has_ffi: bool = False,
    has_concurrency: bool = False,
    has_async: bool = False,
    has_packed_repr: bool = False,
    has_fs_io: bool = False,
) -> dict[str, bool]:
    return {
        "has_unsafe": has_unsafe,
        "has_ffi": has_ffi,
        "has_concurrency": has_concurrency,
        "has_async": has_async,
        "has_packed_repr": has_packed_repr,
        "has_fs_io": has_fs_io,
    }


def select(flags: dict[str, bool], threat_model: str = "REMOTE") -> list[dict[str, Any]]:
    return build_selection(
        load_manifest(),
        plugin_root=PLUGIN_ROOT,
        flags=flags,
        threat_model=threat_model,
    )


def cluster_by_id(selected: list[dict[str, Any]], cid: str) -> dict[str, Any] | None:
    for c in selected:
        if c["cluster_id"] == cid:
            return c
    return None


def prefixes(cluster: dict[str, Any]) -> list[str]:
    return [p["prefix"] for p in cluster["passes"]]


def test_layout_safety_selected_only_with_packed_repr():
    off = select(make_flags())
    assert cluster_by_id(off, "layout-safety") is None

    on = select(make_flags(has_packed_repr=True))
    layout = cluster_by_id(on, "layout-safety")
    assert layout is not None
    assert prefixes(layout) == ["PACKEDREF"]


def test_packed_ref_not_in_ffi_cluster():
    selected = select(make_flags(has_ffi=True, has_packed_repr=False))
    ffi = cluster_by_id(selected, "ffi-cross-language")
    assert ffi is not None
    assert "PACKEDREF" not in prefixes(ffi)


def test_input_os_safety_gated_on_fs_io():
    off = select(make_flags())
    assert cluster_by_id(off, "input-os-safety") is None

    on = select(make_flags(has_fs_io=True))
    ios = cluster_by_id(on, "input-os-safety")
    assert ios is not None
    assert prefixes(ios) == ["PATHJOIN", "TOCTOU"]


def test_new_gates_are_isolated():
    fs_only = select(make_flags(has_fs_io=True))
    assert cluster_by_id(fs_only, "input-os-safety") is not None
    assert cluster_by_id(fs_only, "layout-safety") is None

    packed_only = select(make_flags(has_packed_repr=True))
    assert cluster_by_id(packed_only, "layout-safety") is not None
    assert cluster_by_id(packed_only, "input-os-safety") is None


def test_info_disclosure_always_on():
    selected = select(make_flags())
    info = cluster_by_id(selected, "info-disclosure")
    assert info is not None
    assert "PTREXPOSE" in prefixes(info)


def test_unsafe_scaffolding_passes_filtered_without_unsafe():
    off = select(make_flags(has_unsafe=False))
    logic_off = cluster_by_id(off, "logic-correctness")
    assert logic_off is not None
    off_prefixes = prefixes(logic_off)
    assert "TRAITADV" not in off_prefixes
    assert "CLOSUREPANIC" not in off_prefixes
    assert "ORDEQHASH" in off_prefixes

    on = select(make_flags(has_unsafe=True))
    logic_on = cluster_by_id(on, "logic-correctness")
    assert logic_on is not None
    on_prefixes = prefixes(logic_on)
    assert "TRAITADV" in on_prefixes
    assert "CLOSUREPANIC" in on_prefixes


def test_data_race_unsafe_passes_filtered():
    off = select(make_flags(has_concurrency=True, has_unsafe=False))
    race_off = cluster_by_id(off, "concurrency-data-race")
    assert race_off is not None
    off_prefixes = prefixes(race_off)
    assert "UNSAFESYNC" not in off_prefixes
    assert "STATICMUT" not in off_prefixes
    assert "ATOMICRACE" in off_prefixes
    assert "SENDSYNCBOUND" in off_prefixes
    assert "SHMRACE" in off_prefixes

    on = select(make_flags(has_concurrency=True, has_unsafe=True))
    race_on = cluster_by_id(on, "concurrency-data-race")
    assert race_on is not None
    on_prefixes = prefixes(race_on)
    assert "UNSAFESYNC" in on_prefixes
    assert "STATICMUT" in on_prefixes


def test_manifest_gates_and_requires_are_known():
    manifest = load_manifest()
    for cluster in manifest["clusters"]:
        assert cluster["gate"] in build_run_plan.GATE_VALUES, cluster["cluster_id"]
        for p in cluster.get("passes", []):
            for req in p.get("requires", []) or []:
                assert req in build_run_plan.KNOWN_REQUIRES, (cluster["cluster_id"], p)


def test_flag_vocabularies_derive_from_capability_flags():
    assert build_run_plan.GATE_VALUES == {"always", *build_run_plan.CAPABILITY_FLAGS}
    assert build_run_plan.KNOWN_REQUIRES == set(build_run_plan.CAPABILITY_FLAGS)
    assert set(make_flags()) == set(build_run_plan.CAPABILITY_FLAGS)


def test_rendered_prefix_lists_every_capability_flag():
    lines = build_run_plan._render_shared_prefix_lines(
        output_dir=Path("/tmp/out"),
        scope_root=".",
        context_roots=".",
        threat_model="REMOTE",
        severity_filter="all",
        flags=make_flags(has_unsafe=True, has_fs_io=True),
        context_md_body="ctx",
    )
    codebase = next(line for line in lines if line.startswith("Codebase: "))
    for flag in build_run_plan.CAPABILITY_FLAGS:
        assert f"{flag}=" in codebase


def test_full_run_all_flags_end_to_end_snapshot():
    """Golden snapshot of the production fan-out: real manifest, every capability
    flag on, default --max-passes-per-worker 4. Locks the selection->split seam
    (23 workers with these exact chunk ids/sizes), so a manifest edit that grows a
    cluster, or a regression in the selection->split pipeline, cannot silently
    change worker count or chunk ids without failing a test. The two consolidated
    clusters (unsafe-boundary, concurrency-locking) are never chunked — one worker
    each, full pass list."""
    flags = make_flags(
        has_unsafe=True,
        has_ffi=True,
        has_concurrency=True,
        has_async=True,
        has_packed_repr=True,
        has_fs_io=True,
    )
    selected = build_selection(
        load_manifest(), plugin_root=PLUGIN_ROOT, flags=flags, threat_model="REMOTE"
    )
    split = build_run_plan.split_oversized_clusters(selected, max_passes=4)

    expected_ids = [
        "unsafe-boundary",  # consolidated → not chunked (8 passes, one worker)
        "memory-safety-1",
        "memory-safety-2",
        "concurrency-locking",  # consolidated → not chunked (6 passes, one worker)
        "concurrency-data-race-1",
        "concurrency-data-race-2",
        "panic-dos-1",
        "panic-dos-2",
        "recursion-dos-1",
        "recursion-dos-2",
        "recursion-dos-3",
        "error-handling-1",
        "error-handling-2",
        "logic-correctness-1",
        "logic-correctness-2",
        "ffi-cross-language-1",
        "ffi-cross-language-2",
        "layout-safety",
        "async-runtime",
        "static-hygiene",
        "resource-handling",
        "input-os-safety",
        "info-disclosure",
    ]
    expected_sizes = [
        8,  # unsafe-boundary (consolidated, full pass list)
        4,
        4,
        6,  # concurrency-locking (consolidated, full pass list)
        4,
        1,
        4,
        3,
        1,
        1,
        1,
        4,
        1,
        4,
        4,
        4,
        3,
        1,
        3,
        3,
        2,
        2,
        1,
    ]

    assert [c["cluster_id"] for c in split] == expected_ids
    assert [len(c["passes"]) for c in split] == expected_sizes
    assert len(split) == 23
    assert sum(len(c["passes"]) for c in split) == 69  # total bug-class passes unchanged


def test_non_remote_threat_models_select():
    """The non-REMOTE threat models must also drive a valid selection. No pass in
    the manifest is threat-model-gated today, so the cluster set matches REMOTE —
    this exercises the LOCAL_UNPRIVILEGED / BOTH code path that no other test hits."""
    flags = make_flags(has_unsafe=True, has_ffi=True, has_concurrency=True, has_async=True)
    remote = [c["cluster_id"] for c in select(flags, threat_model="REMOTE")]
    assert [c["cluster_id"] for c in select(flags, threat_model="LOCAL_UNPRIVILEGED")] == remote
    assert [c["cluster_id"] for c in select(flags, threat_model="BOTH")] == remote


def test_skip_threat_models_filters_pass(tmp_path):
    """pass_filtered_out drops a pass whose skip_threat_models includes the active
    threat model, and keeps it under another model."""
    prompt = tmp_path / "prompts" / "clusters" / "c.md"
    prompt.parent.mkdir(parents=True)
    prompt.write_text("# c\n", encoding="utf-8")
    sub = tmp_path / "prompts" / "general" / "p.md"
    sub.parent.mkdir(parents=True)
    sub.write_text("# p\n", encoding="utf-8")
    manifest = {
        "version": 1,
        "clusters": [
            {
                "cluster_id": "c",
                "prompt": "prompts/clusters/c.md",
                "consolidated": False,
                "gate": "always",
                "passes": [
                    {
                        "bug_class": "remote-only",
                        "prefix": "RO",
                        "prompt": "prompts/general/p.md",
                        "skip_threat_models": ["LOCAL_UNPRIVILEGED"],
                    },
                    {"bug_class": "always", "prefix": "AL", "prompt": "prompts/general/p.md"},
                ],
            }
        ],
    }
    flags = make_flags()
    local = build_selection(
        manifest, plugin_root=tmp_path, flags=flags, threat_model="LOCAL_UNPRIVILEGED"
    )
    remote = build_selection(manifest, plugin_root=tmp_path, flags=flags, threat_model="REMOTE")
    local_c = cluster_by_id(local, "c")
    remote_c = cluster_by_id(remote, "c")
    assert local_c is not None and remote_c is not None
    assert prefixes(local_c) == ["AL"]
    assert prefixes(remote_c) == ["RO", "AL"]


def _render_cluster(cluster: dict[str, Any]) -> str:
    return build_run_plan.render_worker_prompt(
        worker_n=1,
        cluster=cluster,
        output_dir=Path("/tmp/out"),
        scope_root=".",
        context_roots=".",
        threat_model="REMOTE",
        severity_filter="all",
        flags=make_flags(has_unsafe=True, has_fs_io=True),
        context_md_body="ctx",
    )


def test_spawn_prompt_consolidated_omits_subprompt_section():
    """Contract lock (worker.md self-check): a consolidated cluster's spawn prompt
    must NOT render a 'Sub-prompt paths:' section at all — its absence is
    well-formed, not a missing required field."""
    prompt = _render_cluster(
        {
            "cluster_id": "unsafe-boundary",
            "consolidated": True,
            "cluster_prompt": "/abs/unsafe-boundary.md",
            "passes": [{"bug_class": "transmute-misuse", "prefix": "TRANS"}],
        }
    )
    assert "Sub-prompt paths:" not in prompt
    assert "Pass bug classes: transmute-misuse" in prompt
    assert "Pass prefixes: TRANS" in prompt
    assert "Skip subclasses: (none)" in prompt


def test_spawn_prompt_nonconsolidated_includes_subprompt_section():
    prompt = _render_cluster(
        {
            "cluster_id": "memory-safety",
            "consolidated": False,
            "cluster_prompt": "/abs/memory-safety.md",
            "passes": [
                {
                    "bug_class": "use-after-free",
                    "prefix": "UAF",
                    "prompt": "/abs/general/use-after-free-finder.md",
                }
            ],
        }
    )
    assert "Sub-prompt paths:" in prompt
    assert "  - /abs/general/use-after-free-finder.md" in prompt
    assert "Skip subclasses: (none)" in prompt


def test_spawn_prompt_codebase_line_is_comma_separated():
    """Contract lock (worker.md:27 illustration): the Codebase flags line is
    comma-separated."""
    prompt = _render_cluster(
        {
            "cluster_id": "info-disclosure",
            "consolidated": False,
            "cluster_prompt": "/abs/info-disclosure.md",
            "passes": [
                {
                    "bug_class": "pointer-exposure",
                    "prefix": "PTREXPOSE",
                    "prompt": "/abs/general/pointer-exposure-finder.md",
                }
            ],
        }
    )
    codebase = next(line for line in prompt.splitlines() if line.startswith("Codebase: "))
    assert ", " in codebase
    assert codebase.startswith("Codebase: has_unsafe=true, has_ffi=")


if __name__ == "__main__":
    import sys

    raise SystemExit(pytest.main([__file__, *sys.argv[1:]]))
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: 466kUpdated 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: 466kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 466kUpdated 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: 193kUpdated Claude CodeCodexVerifiedInstall