Quality Gates

DOCTRINE

Quality Gates

Overview

Quality Gates are automated checkpoints that block any code change that fails to meet Prismatic Platform standards. Every commit, every pull request, and every merge must pass through a rigorous multi-stage gate pipeline before reaching the codebase. These gates are not advisory warnings or optional recommendations -- they are hard enforcement mechanisms that reject non-compliant code with zero override capability.

The Quality Gates system operates at three distinct enforcement levels: local pre-commit hooks that catch violations before they leave the developer's machine, CI pipeline gates that perform comprehensive analysis on the remote server, and merge gates that provide final validation before code enters the main branch. This defense-in-depth approach ensures that quality violations are caught at the earliest and cheapest point of correction.

Built on Elixir Mix tasks and integrated with Credo, Dialyzer, and ExUnit, the Quality Gates system enforces the NO MERCY doctrine's zero-tolerance standards across all 99 umbrella applications in the platform.

Gate Pipeline Architecture

The Quality Gates pipeline processes every code change through three sequential enforcement stages. Each stage must pass completely before the next stage begins, and failure at any stage blocks progression entirely.

Code Change
    |
    +---> Pre-Commit Hooks (Local, ~10-30 seconds)
    |       +-- Format check (mix format --check-formatted)
    |       +-- Compile (--warnings-as-errors)
    |       +-- Credo --strict
    |       +-- Quick tests (changed files only)
    |       +-- TODO/FIXME/HACK scan
    |       +-- Template validation (promo site)
    |       +-- Design consistency check
    |
    +---> CI Pipeline (Remote, ~3-10 minutes)
    |       +-- Full compilation (--force --warnings-as-errors)
    |       +-- Dialyzer PLT analysis
    |       +-- Full test suite with coverage
    |       +-- Coverage threshold check (>= 80%)
    |       +-- Security audit (mix_audit)
    |       +-- Performance benchmarks
    |       +-- Pattern analysis (CASCADE detection)
    |
    +---> Merge Gate (Final, ~1-2 minutes)
            +-- All CI stages green
            +-- Code review approved
            +-- Quality score >= threshold
            +-- No regression in quality domains
            +-- Conflict resolution verified

Pre-Commit Gates

Pre-commit gates execute locally on the developer's machine before the commit is created. They provide the fastest feedback loop, catching common violations in seconds rather than minutes.

Gate Configuration

GateCommandThresholdFailure Action
Formatmix format --check-formatted100% formattedCommit blocked
Compilemix compile --warnings-as-errors0 warningsCommit blocked
Credomix credo --strict0 issuesCommit blocked
Quick Testmix test --only quick100% passCommit blocked
TODO ScanPattern regex scanner0 matchesCommit blocked
Templatescripts/validate-promo-templates.sh0 violationsCommit blocked
Designscripts/validate-design-consistency.sh0 violationsCommit blocked

Pre-Commit Hook Implementation

The pre-commit hook executes a phased validation pipeline, organized for fast failure to minimize developer wait time:

defmodule PrismaticSafety.PreCommitGate do
  @moduledoc """
  Pre-commit quality gate implementation.
  Executes validation phases in order, failing fast on first violation.
  Enforces NO MERCY standards before any commit reaches the repository.
  """

  @phases [
    {:format_check, "mix format --check-formatted", "Code formatting"},
    {:compile_check, "mix compile --warnings-as-errors", "Zero-warning compilation"},
    {:credo_check, "mix credo --strict", "Code quality analysis"},
    {:quick_test, "mix test --only quick", "Quick test suite"},
    {:todo_scan, :custom, "TODO/FIXME/HACK pattern scan"},
    {:template_check, "scripts/validate-promo-templates.sh", "Template validation"},
    {:design_check, "scripts/validate-design-consistency.sh", "Design consistency"}
  ]

  @spec run_all_gates() :: :ok | {:blocked, atom(), String.t()}
  def run_all_gates do
    Enum.reduce_while(@phases, :ok, fn phase, :ok ->
      case execute_phase(phase) do
        :ok -> {:cont, :ok}
        {:error, reason} -> {:halt, {:blocked, elem(phase, 0), reason}}
      end
    end)
  end

  defp execute_phase({name, :custom, _description}) do
    execute_custom_phase(name)
  end

  defp execute_phase({_name, command, _description}) do
    case System.cmd("mix", String.split(command, " ") |> tl(), stderr_to_stdout: true) do
      {_output, 0} -> :ok
      {output, _code} -> {:error, output}
    end
  end

  defp execute_custom_phase(:todo_scan) do
    staged_files = get_staged_elixir_files()

    violations =
      staged_files
      |> Enum.flat_map(&scan_for_forbidden_patterns/1)

    case violations do
      [] -> :ok
      found -> {:error, Enum.join(found, "\n")}
    end
  end

  defp scan_for_forbidden_patterns(file) do
    patterns = [~r/# TODO:/i, ~r/# FIXME:/i, ~r/# HACK:/i, ~r/:not_implemented/]

    file
    |> File.read!()
    |> String.split("\n")
    |> Enum.with_index(1)
    |> Enum.flat_map(fn {line, num} ->
      Enum.flat_map(patterns, fn pattern ->
        if Regex.match?(pattern, line), do: ["#{file}:#{num}: #{String.trim(line)}"], else: []
      end)
    end)
  end

  defp get_staged_elixir_files do
    {output, 0} = System.cmd("git", ["diff", "--cached", "--name-only", "--diff-filter=ACMR"])
    output |> String.split("\n") |> Enum.filter(&String.ends_with?(&1, [".ex", ".exs"]))
  end
end

CI Pipeline Gates

CI pipeline gates execute on the remote server after a push, performing comprehensive analysis that requires the full codebase and build infrastructure.

Gate Requirements

GateCommandThresholdDuration
Full Compilemix compile --force --warnings-as-errors0 warnings~60s
Dialyzermix dialyzer0 type errors~120s
Full Testsmix test --cover100% pass~180s
CoverageExCoveralls report>= 80% line coverageIncluded in tests
Securitymix_audit0 known vulnerabilities~10s
Pattern AnalysisCASCADE detection0 anti-patterns~30s
PerformanceBenchee critical pathsWithin thresholds~60s

Dialyzer Integration

Dialyzer performs static type analysis across the entire codebase, catching type errors that unit tests cannot detect. The PLT (Persistent Lookup Table) is cached between runs for performance:

# mix.exs configuration for Dialyzer
defp dialyzer do
  [
    plt_file: {:no_warn, "priv/plts/dialyzer.plt"},
    plt_add_apps: [:mix, :ex_unit],
    flags: [
      :unmatched_returns,
      :error_handling,
      :underspecs,
      :no_opaque
    ]
  ]
end

Performance Gate

Critical paths must meet strict latency requirements as defined in the page load performance standard:

MetricHard LimitGate Action
Total page load< 250msMerge rejected if exceeded
Server-side render< 100msMerge rejected if exceeded
LiveView mount< 150msMerge rejected if exceeded
LiveView handle_event< 50msMerge rejected if exceeded
Health check endpoint< 10msMerge rejected if exceeded

Quality Score Calculation

The Quality Gates system computes a composite quality score that aggregates all enforcement dimensions. This score provides a single metric for platform health assessment.

Score Formula

Quality Score = Compilation (25%) + Tests (25%) + Coverage (25%) + Static Analysis (25%)

Each component is binary: 100% if passing, 0% if failing.

100/100 = PERFECT     -- All domains passing, no violations
95-99   = OPTIMAL     -- Minor non-blocking issues detected
90-94   = ACCEPTABLE  -- Warning-level issues, investigation needed
< 90    = BLOCKED     -- Hard block on all commits until resolved

Domain Breakdown

Quality DomainScore WeightMeasurementCurrent
Compiler warnings8%Count of warnings0
Dialyzer violations8%Count of type errors0
Credo issues8%Count of strict-mode issues0
Test pass rate10%Percentage of tests passing100%
Line coverage8%ExCoveralls line %>= 80%
Branch coverage8%ExCoveralls branch %>= 80%
@impl annotations5%Coverage of callback implementations709/709
@spec annotations5%Coverage of public functions100%
TODO/FIXME absence5%Count of prohibited patterns0
Memory safety5%Unsafe pattern count0
Performance compliance10%Benchmarks within thresholds100%
DateTime precision5%Microsecond precision usage100%
Guard function usage5%Proper guard clause patterns100%
Unsafe map access5%Direct map access without guard0
TOTAL100%100/100

Quality Floor Guardian

The Quality Floor Guardian is an autonomous monitoring system that continuously tracks the platform's quality score and triggers corrective action when it drops below defined thresholds.

Guardian Enforcement Levels

Score RangeStatusResponseAutomation
100-99%OPTIMALMonitor onlyPassive observation
98-99%WARNINGAlert + investigationBlue Team notification
95-98%CRITICALAuto-evolution triggeredSelf-healing cycle
< 95%EMERGENCYBlock all commits + escalateFull platform halt
defmodule PrismaticSafety.QualityFloorGuardian do
  @moduledoc """
  Autonomous quality monitoring system.
  Continuously tracks quality score and triggers corrective action.
  """

  use GenServer

  @check_interval :timer.seconds(30)
  @warning_threshold 99
  @critical_threshold 98
  @emergency_threshold 95

  def init(state) do
    schedule_check()
    {:ok, state}
  end

  def handle_info(:check_quality, state) do
    score = compute_quality_score()

    state =
      cond do
        score >= @warning_threshold ->
          %{state | status: :optimal}

        score >= @critical_threshold ->
          Logger.warning("Quality score WARNING: #{score}/100")
          trigger_investigation(score)
          %{state | status: :warning}

        score >= @emergency_threshold ->
          Logger.error("Quality score CRITICAL: #{score}/100")
          trigger_auto_evolution(score)
          %{state | status: :critical}

        true ->
          Logger.error("Quality score EMERGENCY: #{score}/100")
          block_all_commits()
          trigger_full_escalation(score)
          %{state | status: :emergency}
      end

    schedule_check()
    {:noreply, state}
  end

  defp schedule_check do
    Process.send_after(self(), :check_quality, @check_interval)
  end

  defp compute_quality_score do
    # Aggregate all 13+ quality domains into composite score
    domains = PrismaticSafety.QualityDomains.all()
    Enum.sum(Enum.map(domains, & &1.score)) / length(domains)
  end

  defp trigger_investigation(score), do: :ok
  defp trigger_auto_evolution(score), do: :ok
  defp block_all_commits, do: :ok
  defp trigger_full_escalation(score), do: :ok
end

Risk Pattern Detection

Quality Gates include proactive detection of known risk patterns that historically lead to quality degradation:

Risk PatternDetection MethodExampleGate Response
length(list) > 0AST analysisAnti-pattern: use list != []WARNING
Process.sleep/1AST analysisNon-deterministic timingBLOCK
Missing @specModule introspectionUntyped public functionBLOCK
Unsafe map.fieldAST analysisRuntime crash on missing keyBLOCK
Dead code pathsCoverage + ASTUnreachable branchesWARNING
High cyclomatic complexityCredo analysis> 10 branchesWARNING

Integration with Platform Subsystems

Quality Gates serve as the central enforcement mechanism connecting multiple platform capabilities:

  • NO MERCY: Quality Gates are the technical implementation of NO MERCY's zero-tolerance standards
  • NO DOUBTS: Gates verify that evidence requirements are met before execution proceeds
  • Trinity Gate: Formal verification layer for critical decisions beyond code quality
  • Regression Tests: Mandatory regression test protocol enforced as a gate requirement
  • Session Discipline: Gates enforce that commits meet session tracking requirements
  • Telemetry Integration: Gate execution emits telemetry events for monitoring
  • Real-Time Monitoring: Quality Floor Guardian provides continuous score tracking
  • Autonomous Self-Healing: Auto-healing triggered when quality score drops
  • AIAD Standard: Agent operations subject to same gate requirements
  • Color Teams: Purple Team validates gate effectiveness through adversarial testing

Configuration

Running Quality Gates

# Full quality gate pipeline (recommended)
mix quality.gates

# Individual gate checks
mix format --check-formatted          # Format compliance
mix compile --warnings-as-errors      # Zero-warning compilation
mix credo --strict                    # Code quality analysis
mix dialyzer                          # Static type analysis
mix test --cover                      # Test suite with coverage

# Quick check (pre-command validation)
mix quality.gates.check --fast

# CI-friendly JSON output
mix quality.enforce_standard --json

# Auto-fix mode (applies safe corrections)
mix quality.enforce_standard --fix

Nuclear Cache Resolution

When stale compilation artifacts cause false Dialyzer errors, the nuclear cache fix resolves the issue:

rm -rf _build/dev/lib/prismatic_claude/ebin && rm -rf priv/plts/dialyzer.plt
mix compile --warnings-as-errors --force
mix dialyzer

Commands

CommandPurposeAuthority
/quality-gatesRun complete quality gate pipelineUniversal
/quality-gates.checkQuick validation (pre-command)Universal
/quality-gates.fullFull pipeline including benchmarksCI/CD
mix quality.gatesMix task for quality gate executionDevelopment
mix quality.enforce_standardCompliance check across all appsSystem

Connect & Contribute

Created by Tomáš Korcak (korczis) | Open Source under GHL