sec-af icon indicating copy to clipboard operation
sec-af copied to clipboard

perf: parallelize reachability assessment in prove phase

Open santoshkumarradha opened this issue 8 months ago • 0 comments

Problem

In orchestrator.py, the reachability assessment for verified findings runs as a sequential loop:

# orchestrator.py, _run_prove()
for finding in verified:
    if not any(tag in reachability_tags for tag in finding.tags):
        try:
            summary = (
                f"Finding: {finding.title}\n"
                f"Description: {finding.description}\n"
                f"CWE: {finding.cwe_id}\n"
                f"File: {finding.location.file_path}:{finding.location.start_line}\n"
                f"Verdict: {finding.verdict.value}"
            )
            gate_result = await self.ai_gate.assess_reachability(summary)
            finding.tags.append(gate_result.reachability)
        except Exception:
            finding.tags.append("requires_auth")

With ~2s per AI gate call and potentially 30 findings, this is 60 seconds of pure serial waste. The same codebase already uses asyncio.gather + semaphore patterns everywhere else.

Fix

Replace the sequential loop with the established parallel pattern:

async def _assess_reachability_parallel(self, verified: list[VerifiedFinding]) -> None:
    reachability_tags = {"externally_reachable", "requires_auth", "internal_only", "unreachable"}
    needs_assessment = [f for f in verified if not any(tag in reachability_tags for tag in f.tags)]
    if not needs_assessment:
        return

    semaphore = asyncio.Semaphore(min(5, len(needs_assessment)))

    async def _assess_one(finding: VerifiedFinding) -> None:
        async with semaphore:
            try:
                summary = (
                    f"Finding: {finding.title}\n"
                    f"Description: {finding.description}\n"
                    f"CWE: {finding.cwe_id}\n"
                    f"File: {finding.location.file_path}:{finding.location.start_line}\n"
                    f"Verdict: {finding.verdict.value}"
                )
                gate_result = await self.ai_gate.assess_reachability(summary)
                finding.tags.append(gate_result.reachability)
            except Exception:
                finding.tags.append("requires_auth")

    await asyncio.gather(*[_assess_one(f) for f in needs_assessment])

Estimated Impact

  • 30-60 seconds saved per scan (depending on finding count)
  • Semaphore prevents overwhelming the AI provider
  • Zero risk — same pattern already proven throughout the codebase

Acceptance Criteria

  • [ ] Reachability assessment uses asyncio.gather with semaphore (cap at 5 concurrent)
  • [ ] Same error handling (fallback to requires_auth)
  • [ ] No change to reachability tagging behavior
  • [ ] Measurable latency reduction in prove phase

Dependencies

  • Independent — no dependencies on other issues
  • Smallest, safest change. Should be merged first.

santoshkumarradha avatar Mar 05 '26 18:03 santoshkumarradha