← Back to Pull requests

fix: bound evidence batches by size and release flushed evidence #34

Closed
unknown closed this pull request without merging on Oct 30, 2026 13:40 UTC
View on GitHub
unknown unknown commented on Oct 30, 2026 12:24 UTC
Author

Fixes #33.

Problem

Every evidence carries a JSON copy of the Rego input it was evaluated against (PolicyEvaluation.Input), and that input includes every collected resource in the fleet. Eval held up to 100 evidences before sending them, so a batch could hold 100 × (full fleet JSON). After a flush, clusterEvidences[:0] also kept the sent evidence reachable through the slice's backing array.

Changes

  • Size-based batching: a batch is sent at 100 evidences or when its policy-evaluation data (input + policy data) reaches a byte limit, whichever comes first. Evidences from one evaluation share a PolicyEvaluation, so it's counted once.

  • CCF_K8S_EVIDENCE_BATCH_MAX_BYTES: overrides the 64 MiB default. It's read in Configure and logged at startup. Invalid or non-positive values log a warning and fall back to the default. The agent passes its environment through to plugins, so it's set on the agent.

  • Release flushed evidence: clear(clusterEvidences) before reslicing.

  • README: documents the batching behaviour and the environment variable.

  • Repro harness (oom_repro_test.go, build tag oomrepro, excluded from the normal test run): runs the real Eval loop and agent policy evaluator against a synthetic fleet and writes a heap profile at each flush. Usage is in the file header.
  • Verification

    Unit tests cover byte-limit flushing, shared evaluations counted once, the count limit still applying, flushed evidence no longer being reachable, and env var parsing. With the clear() removed, the reachability test fails with 95 stale evidences, matching what pprof showed.

    The repro harness, run with plugin-k8s-opres-policies v0.2.0 and the reported config (2 clusters; 750 pods, 30 nodes and 110 deployments per cluster):

    | | Before | After |
    |---|---|---|
    | Peak RSS | 4.66 GB | 1.51 GB |
    | Heap obtained from OS | 5.9 GiB | 1.37 GiB |
    | Live heap after GC at flushes | 1,949–2,555 MiB | 194–536 MiB |
    | Input JSON held per batch | 1,790 MiB (100 evidences) | 71.6 MiB (4 evidences) |
    | Run time | 4m05s | 3m55s |

    Not addressed

    Peak memory now grows with the size of one input rather than 100. Each evaluation still allocates about 800 MB of short-lived memory converting the input for OPA, and each evidence's full input is still uploaded by the agent. Reducing the input itself is tracked in #33.

    🤖 Generated with Claude Code

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    Evidence batches now flush when they reach either 100 items or the configured data-size limit, whichever comes first. This can help prevent large batches from using excessive memory.
    The batch-size limit can be set with CCF_K8S_EVIDENCE_BATCH_MAX_BYTES. It defaults to 64 MiB; invalid or non-positive values use the default, with a warning for invalid entries.
    Documentation
    * Clarified that each evidence item includes a copy of its policy input, which contains collected fleet resources.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 396 lines (+297 / -99) across 4 files Reviewer: gusfcarvalho Review duration: 75m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 396 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.358
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #34"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 43.2% (MEDIUM) · Higher = larger blast radius
    43.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.1% · Safe to merge
    17.1%