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

arch: reduce schema bloat at phase boundaries — context-specific view models

Open santoshkumarradha opened this issue 8 months ago • 0 comments

Problem

The LLM-facing sub-schemas are well-designed (2-4 attributes). But schemas that cross phase boundaries are bloated:

Schema Field Count Used By
RawFinding 18 fields Assembled in code, passed as full dict to every prover
VerifiedFinding 30+ fields Final output + serialized at every checkpoint boundary
Proof 15 optional fields Nested sub-models, rarely all populated
HuntResult 7 fields Container with lists of above

Architectural Principle Violated

Principle 1 (Granular Decomposition):

"Every AI call must use a simple, flat Pydantic schema (2-4 attributes)... compels the architect to deconstruct a complex thought process."

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."

Current Pain Points

1. RawFinding (18 fields) passed to provers

When run_verifier is called, it receives the entire finding.model_dump():

# reasoners/prove.py
return await _runtime_router.call(
    f"{NODE_ID}.run_verifier",
    repo_path=repo_path,
    finding=finding.model_dump(),  # ALL 18 fields serialized
)

The verifier pipeline (tracer, sanitization, exploit, verdict) only needs ~6 of those 18 fields. The rest is dead weight in the serialization and LLM context.

2. VerifiedFinding (30+ fields) over-assembled

VerifiedFinding carries compliance mappings, CVSS scores, EPSS data, reproduction steps, SARIF metadata, and proof artifacts — all assembled into one mega-model. Most downstream consumers only need subsets.

3. Proof (15 optional fields) is a kitchen sink

Most findings only populate 3-5 of the 15 Proof fields. But the full schema definition is still part of the model, and model_dump() serializes all None fields.

Proposed Approach

Create Phase-Specific View Models

Instead of passing full schemas, create view models that extract exactly what each consumer needs:

# schemas/views.py — phase boundary contracts

class FindingForVerifier(BaseModel):
    """What the verifier pipeline actually needs from a RawFinding."""
    id: str
    title: str
    file_path: str
    start_line: int
    end_line: int
    code_snippet: str
    cwe_id: str
    function_name: str | None = None
    data_flow_summary: str | None = None  # Flattened from data_flow list

class FindingForDedup(BaseModel):
    """What the deduplicator needs."""
    id: str
    fingerprint: str
    title: str
    file_path: str
    start_line: int
    cwe_id: str
    finding_type: str
    estimated_severity: str

class FindingForReachability(BaseModel):
    """What the reachability gate needs."""
    title: str
    description: str
    cwe_id: str
    file_path: str
    start_line: int

Add .for_verifier(), .for_dedup() projection methods

class RawFinding(BaseModel):
    # ... existing 18 fields ...
    
    def for_verifier(self) -> FindingForVerifier:
        return FindingForVerifier(
            id=self.id,
            title=self.title,
            file_path=self.file_path,
            start_line=self.start_line,
            end_line=self.end_line,
            code_snippet=self.code_snippet,
            cwe_id=self.cwe_id,
            function_name=self.function_name,
            data_flow_summary=self._flatten_data_flow(),
        )

Use model_dump(exclude_none=True) at serialization boundaries

For Proof and VerifiedFinding, use exclude_none=True to avoid serializing empty fields.

Acceptance Criteria

  • [ ] View models created for verifier, dedup, and reachability consumers
  • [ ] RawFinding has projection methods for each consumer
  • [ ] Serialization at phase boundaries uses view models (not full model_dump)
  • [ ] No regression in functionality — full models still used for checkpoints/output
  • [ ] Token savings measurable (log context sizes before/after)

Dependencies

  • Independent of other issues
  • Benefits: streaming pipeline (#TBD), context pruning (#TBD)

santoshkumarradha avatar Mar 05 '26 18:03 santoshkumarradha