|
| 1 | +"""Unified completion gate evaluator — port of auditGateReport.ts.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import Any, Optional |
| 6 | + |
| 7 | +from plugins.dietcode.lib.agent.audit.config import CompletionGateConfig, get_completion_gate_config |
| 8 | +from plugins.dietcode.lib.agent.audit.gate_policy import resolve_effective_gate_threshold |
| 9 | +from plugins.dietcode.lib.agent.audit.hardening import compute_hardening_assessment |
| 10 | +from plugins.dietcode.lib.agent.audit.severity import has_critical_violations, partition_violations |
| 11 | + |
| 12 | + |
| 13 | +@dataclass(frozen=True) |
| 14 | +class GateReason: |
| 15 | + code: str |
| 16 | + message: str |
| 17 | + |
| 18 | + |
| 19 | +@dataclass(frozen=True) |
| 20 | +class CompletionGateDecision: |
| 21 | + blocked: bool |
| 22 | + score: int |
| 23 | + effective_threshold: int |
| 24 | + grade: str |
| 25 | + reasons: tuple[GateReason, ...] |
| 26 | + |
| 27 | + |
| 28 | +def evaluate_completion_gate( |
| 29 | + metadata: dict[str, Any], |
| 30 | + *, |
| 31 | + config: Optional[CompletionGateConfig] = None, |
| 32 | + baseline_metadata: Optional[dict[str, Any]] = None, |
| 33 | + advisory_metadata: Optional[dict[str, Any]] = None, |
| 34 | +) -> CompletionGateDecision: |
| 35 | + cfg = config or get_completion_gate_config() |
| 36 | + assessment = compute_hardening_assessment(metadata) |
| 37 | + score = int(metadata.get("hardening_score") or assessment["score"]) |
| 38 | + grade = str(metadata.get("hardening_grade") or assessment["grade"]) |
| 39 | + intent = str(metadata.get("intent_classification") or "GENERAL") |
| 40 | + effective_threshold = resolve_effective_gate_threshold( |
| 41 | + cfg.score_threshold, |
| 42 | + intent, |
| 43 | + intent_adjustments_enabled=cfg.intent_adjusted_threshold, |
| 44 | + ) |
| 45 | + |
| 46 | + violations = list(metadata.get("violations") or []) |
| 47 | + if cfg.new_violations_only and baseline_metadata: |
| 48 | + baseline_set = set(baseline_metadata.get("violations") or []) |
| 49 | + violations = [v for v in violations if v not in baseline_set] |
| 50 | + |
| 51 | + reasons: list[GateReason] = [] |
| 52 | + |
| 53 | + if not cfg.enabled: |
| 54 | + return CompletionGateDecision( |
| 55 | + blocked=False, |
| 56 | + score=score, |
| 57 | + effective_threshold=effective_threshold, |
| 58 | + grade=grade, |
| 59 | + reasons=(GateReason("gate_disabled", "Completion gate disabled"),), |
| 60 | + ) |
| 61 | + |
| 62 | + if cfg.advisory_escalation_enabled and advisory_metadata: |
| 63 | + adv_critical = has_critical_violations(advisory_metadata.get("violations")) |
| 64 | + if adv_critical and has_critical_violations(metadata.get("violations")): |
| 65 | + reasons.append( |
| 66 | + GateReason( |
| 67 | + "advisory_escalation", |
| 68 | + "Critical act-mode advisory findings remain unresolved", |
| 69 | + ) |
| 70 | + ) |
| 71 | + |
| 72 | + if cfg.plan_regression_gate_enabled and baseline_metadata: |
| 73 | + base_score = int( |
| 74 | + baseline_metadata.get("hardening_score") |
| 75 | + or compute_hardening_assessment(baseline_metadata)["score"] |
| 76 | + ) |
| 77 | + if score < base_score: |
| 78 | + reasons.append( |
| 79 | + GateReason( |
| 80 | + "plan_regression", |
| 81 | + "Hardening score regressed from plan audit baseline", |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + spider = metadata.get("spider_gate") or {} |
| 86 | + if cfg.spider_gate_required: |
| 87 | + if spider.get("blocked"): |
| 88 | + reasons.append( |
| 89 | + GateReason( |
| 90 | + "spider_gate_blocked", |
| 91 | + f"Spider structural gate blocked (exit {spider.get('exitCode', 1)})", |
| 92 | + ) |
| 93 | + ) |
| 94 | + elif cfg.fail_on_spider_warning and str(spider.get("qualityGate", "")).upper() == "WARNING": |
| 95 | + reasons.append( |
| 96 | + GateReason("spider_warning", "Spider gate reported WARNING-level structural findings") |
| 97 | + ) |
| 98 | + |
| 99 | + if cfg.require_recent_verify and not metadata.get("recent_verify_passed"): |
| 100 | + reasons.append( |
| 101 | + GateReason( |
| 102 | + "missing_validation_evidence", |
| 103 | + "No passing joyzoning verify recorded for active mutation scope", |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + if cfg.new_violations_only: |
| 108 | + if cfg.critical_only: |
| 109 | + if has_critical_violations(violations): |
| 110 | + reasons.append( |
| 111 | + GateReason( |
| 112 | + "critical_violations", |
| 113 | + f"{len(violations)} new critical violation(s) since baseline", |
| 114 | + ) |
| 115 | + ) |
| 116 | + elif violations: |
| 117 | + reasons.append( |
| 118 | + GateReason( |
| 119 | + "policy_violations", |
| 120 | + f"{len(violations)} new violation(s) since baseline", |
| 121 | + ) |
| 122 | + ) |
| 123 | + elif cfg.critical_only and has_critical_violations(metadata.get("violations")): |
| 124 | + reasons.append( |
| 125 | + GateReason( |
| 126 | + "critical_violations", |
| 127 | + f"{len(metadata.get('violations') or [])} critical violation(s) present", |
| 128 | + ) |
| 129 | + ) |
| 130 | + elif score < effective_threshold: |
| 131 | + if assessment["critical_count"] > 0 or violations: |
| 132 | + reasons.append( |
| 133 | + GateReason( |
| 134 | + "score_below_threshold", |
| 135 | + f"Score {score} below threshold {effective_threshold}", |
| 136 | + ) |
| 137 | + ) |
| 138 | + |
| 139 | + blocked = any(r.code != "gate_disabled" for r in reasons) |
| 140 | + return CompletionGateDecision( |
| 141 | + blocked=blocked, |
| 142 | + score=score, |
| 143 | + effective_threshold=effective_threshold, |
| 144 | + grade=grade, |
| 145 | + reasons=tuple(reasons), |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +def build_gate_block_message(decision: CompletionGateDecision, metadata: dict[str, Any]) -> str: |
| 150 | + if not decision.blocked: |
| 151 | + return ( |
| 152 | + f"Gate ready: Grade {decision.grade} " |
| 153 | + f"({decision.score}/100, threshold {decision.effective_threshold})" |
| 154 | + ) |
| 155 | + parts = partition_violations(metadata.get("violations")) |
| 156 | + lines = [ |
| 157 | + "COMPLETION BLOCKED — quality audit gate failed.", |
| 158 | + f"Grade: {decision.grade} ({decision.score}/100, threshold {decision.effective_threshold}).", |
| 159 | + "", |
| 160 | + "Resolve before kanban_complete:", |
| 161 | + ] |
| 162 | + for reason in decision.reasons: |
| 163 | + if reason.code != "gate_disabled": |
| 164 | + lines.append(f"- {reason.message}") |
| 165 | + critical = parts["critical"][:5] |
| 166 | + warning = parts["warning"][:3] |
| 167 | + if critical: |
| 168 | + lines.append(f"Critical: {', '.join(critical)}") |
| 169 | + if warning: |
| 170 | + lines.append(f"Warnings: {', '.join(warning)}") |
| 171 | + lines.append("") |
| 172 | + lines.append("Run broccolidb_violations / joyzoning(action='verify') then retry.") |
| 173 | + return "\n".join(lines) |
0 commit comments