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

arch: context pruning — pass domain-specific recon slices to hunters

Open santoshkumarradha opened this issue 8 months ago • 0 comments

Problem

Every hunter receives the entire ReconResult dict as context, regardless of domain relevance:

# reasoners/phases.py, hunt_phase()
async def _run_strategy(strategy: HuntStrategy) -> object:
    async with semaphore:
        return await _runtime_router.call(
            f"{NODE_ID}.run_{strategy.value}_hunter",
            repo_path=repo_path,
            recon_context=recon_context,   # FULL ReconResult — all fields
            depth=depth,
        )

ReconResult contains:

  • architecture (ArchitectureMap — modules, endpoints, entry points)
  • data_flows (DataFlowMap — source-to-sink mappings)
  • dependencies (DependencyReport — package lists, CVEs)
  • config (ConfigReport — secrets, misconfigs)
  • security_context (SecurityContext — auth model, crypto usage, framework security)
  • languages, frameworks, lines_of_code, file_count

The injection hunter doesn't need dependency CVE lists. The crypto hunter doesn't need API endpoint inventory. The supply chain hunter doesn't need data flow maps.

Architectural Principle Violated

Principle 4 (Contextual Fidelity):

"Every agent receives all necessary information to perform its task and nothing more. Passing irrelevant context confuses the agent and wastes precious context window space."

"The orchestrator's most critical job is to act as a 'context broker.' It must ensure that every agent receives all necessary information to perform its task and nothing more."

Impact of Current Approach

  1. Token waste: Full recon context × 12 hunters = 12× redundant serialization
  2. Lower quality: Irrelevant context dilutes the LLM's focus per the cognitive harness principle
  3. Higher cost: More tokens per call = higher API cost

Proposed Approach

Create a Context Broker

Define per-strategy context requirements:

# context.py or a new context_broker.py

STRATEGY_CONTEXT_MAP: dict[HuntStrategy, list[str]] = {
    HuntStrategy.INJECTION: ["architecture", "data_flows", "security_context", "languages"],
    HuntStrategy.XSS: ["architecture", "data_flows", "security_context", "frameworks"],
    HuntStrategy.SSRF: ["architecture", "data_flows", "security_context"],
    HuntStrategy.AUTH: ["architecture", "security_context"],
    HuntStrategy.CRYPTO: ["security_context"],
    HuntStrategy.DOS: ["architecture", "data_flows", "languages"],
    HuntStrategy.DATA_EXPOSURE: ["architecture", "data_flows", "config"],
    HuntStrategy.SUPPLY_CHAIN: ["dependencies"],
    HuntStrategy.CONFIG_SECRETS: ["config", "architecture"],
    HuntStrategy.API_SECURITY: ["architecture", "security_context", "data_flows"],
    HuntStrategy.BUSINESS_LOGIC: ["architecture", "data_flows", "security_context"],
}

def prune_recon_for_strategy(recon: ReconResult, strategy: HuntStrategy) -> dict[str, Any]:
    """Extract only the recon fields relevant to this strategy."""
    needed = STRATEGY_CONTEXT_MAP.get(strategy, [])
    pruned = {}
    for field in needed:
        pruned[field] = getattr(recon, field).model_dump() if hasattr(getattr(recon, field), 'model_dump') else getattr(recon, field)
    # Always include summary metadata
    pruned["languages"] = recon.languages
    pruned["frameworks"] = recon.frameworks
    return pruned

Apply in Hunt Phase

async def _run_strategy(strategy: HuntStrategy) -> object:
    async with semaphore:
        pruned_context = prune_recon_for_strategy(recon, strategy)
        return await _runtime_router.call(
            f"{NODE_ID}.run_{strategy.value}_hunter",
            repo_path=repo_path,
            recon_context=pruned_context,
            depth=depth,
        )

Backward Compatibility

  • Hunters that currently destructure ReconResult(**recon_context) will need graceful handling of missing fields
  • Add model_validate with defaults for missing sub-models
  • Alternatively, always pass the full ReconResult but only SERIALIZE the needed fields for the LLM prompt

Acceptance Criteria

  • [ ] Context broker maps each strategy to its required recon fields
  • [ ] Hunters receive pruned context instead of full ReconResult
  • [ ] Hunters handle partial context gracefully (missing fields → defaults)
  • [ ] Measurable reduction in per-hunter token usage (log sizes)
  • [ ] No regression in finding quality (DVGA benchmark)

Dependencies

  • Independent of other issues
  • Improves quality of: streaming pipeline (#TBD), dynamic adaptation (#TBD)

santoshkumarradha avatar Mar 05 '26 18:03 santoshkumarradha