Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ jobs:
- name: Run E2E test suite
run: bash tests/e2e.sh

- name: Escript packaging soundness
# PR #278 documented that the rule definitions can be correct
# but the *packaged escript* can silently drop entire rule
# families. The in-process `mix test` catches rule-definition
# regressions; this step catches packaging regressions by
# building the escript fresh and running it against every
# known-bad sample in test/soundness/manifest.json.
run: bash test/soundness/run-escript-soundness.sh

e2e-rust:
name: E2E — Rust CLI Scan
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ defmodule Hypatia.Application do
Hypatia.Dispatch.Pipeline,
# Layer 0.7: Diagnostics -- system health monitoring and auto-recovery
Hypatia.Diagnostics.Monitor,
# Layer 0.8: Watcher -- live monitoring aggregator (subscribes to
# telemetry events, maintains rolling windows in ETS, backs the
# /api/status endpoint and `mix hypatia.watch` TUI).
Hypatia.Watcher,
# Layer 1: Safety -- rate limiting and bot quarantine
Hypatia.Safety.RateLimiter,
Hypatia.Safety.Quarantine,
Expand Down
53 changes: 43 additions & 10 deletions lib/fleet_dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,49 @@ defmodule Hypatia.FleetDispatcher do
# --- Eliminate dispatch helpers (called after Gate approval) ---

defp do_eliminate_dispatch(:auto_execute, recipe, pattern, confidence) do
dispatch_to_robot_repo_automaton(%{
type: :auto_fix_request,
repo: get_pattern_repo(pattern),
file: Map.get(pattern, "file", ""),
issue: Map.get(pattern, "description", ""),
fix_type: "eliminate",
confidence: confidence,
recipe_id: Map.get(recipe, "id"),
suggestion: Map.get(recipe, "description", "")
})
recipe_id = Map.get(recipe, "id")

if recipe_id && Hypatia.OutcomeTracker.quarantined?(recipe_id) do
# Verification-rate gate: this recipe's post-fix re-scans have been
# failing too often to trust for auto_execute. Downgrade to :review
# so rhodibot opens a PR for human inspection. This is the
# closed-loop safety net: a recipe drifting toward false fixes
# can no longer ship to repos automatically.
Hypatia.Telemetry.quarantine_triggered(
kind: :recipe,
id: recipe_id,
reason: "verification_rate",
level: :auto_downgrade
)

Hypatia.Telemetry.dispatch_decision(confidence,
strategy: :review,
tier: :eliminate,
recipe_id: recipe_id,
repo: get_pattern_repo(pattern),
quarantine_downgraded: true
)

do_eliminate_dispatch(:review, recipe, pattern, confidence)
else
Hypatia.Telemetry.dispatch_decision(confidence,
strategy: :auto_execute,
tier: :eliminate,
recipe_id: recipe_id,
repo: get_pattern_repo(pattern)
)

dispatch_to_robot_repo_automaton(%{
type: :auto_fix_request,
repo: get_pattern_repo(pattern),
file: Map.get(pattern, "file", ""),
issue: Map.get(pattern, "description", ""),
fix_type: "eliminate",
confidence: confidence,
recipe_id: recipe_id,
suggestion: Map.get(recipe, "description", "")
})
end
end

defp do_eliminate_dispatch(:review, recipe, pattern, _confidence) do
Expand Down
13 changes: 13 additions & 0 deletions lib/hypatia/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ defmodule Hypatia.CLI do
System.halt(2)
end

started = System.monotonic_time(:millisecond)
findings = collect_findings(abs_path, config.rules)
duration_ms = System.monotonic_time(:millisecond) - started

Hypatia.Telemetry.scan_complete(duration_ms, length(findings),
path: abs_path,
severity_floor: config.severity
)

# Filter by severity threshold
filtered =
Expand Down Expand Up @@ -731,6 +738,12 @@ defmodule Hypatia.CLI do
"ocaml" => [".ml", ".mli"],
"coq" => [".v"],
"lean" => [".lean"],
"agda" => [".agda"],
"isabelle" => [".thy"],
"hol4" => [".sml"],
"zig" => [".zig"],
"fstar" => [".fst", ".fsti"],
"ada" => [".adb", ".ads"],
"nickel" => [".ncl"],
"elixir" => [".ex", ".exs"],
"erlang" => [".erl", ".hrl"],
Expand Down
125 changes: 125 additions & 0 deletions lib/hypatia/telemetry.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# SPDX-License-Identifier: MPL-2.0
# Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>

defmodule Hypatia.Telemetry do
@moduledoc """
Centralised event-name registry for Hypatia's telemetry surface.

Every observable decision in the pipeline emits a `:telemetry` event
through one of the helpers in this module. Centralising event names
here (rather than spreading magic atom lists across call sites) means
the watcher / Prometheus exporter / future alerting layer can
enumerate the full surface from a single source.

All events follow the convention `[:hypatia, <area>, <verb>]`. The
measurements map carries numeric values (counts, durations);
metadata carries categorical context (recipe_id, repo, severity).

Calling `:telemetry.execute/3` is safe with no handlers attached —
it's a no-op, so instrumenting a code path costs nothing when the
watcher isn't running (e.g. inside the escript scanner).

## Event catalogue

| event | measurements | metadata |
|--------------------------------------|---------------------------|-------------------------------------------------|
| `[:hypatia, :scan, :complete]` | `duration_ms, findings` | `path, severity_floor` |
| `[:hypatia, :dispatch, :decision]` | `confidence` | `strategy, tier, recipe_id, repo` |
| `[:hypatia, :outcome, :recorded]` | `count` | `recipe_id, repo, outcome, verification` |
| `[:hypatia, :verification, :result]` | `count` | `recipe_id, repo, verdict` |
| `[:hypatia, :quarantine, :triggered]`| `count` | `kind, id, reason, level` |
| `[:hypatia, :rate_limit, :exceeded]` | `count` | `bot, scope` |
| `[:hypatia, :neural, :cycle]` | `duration_ms` | `networks_updated` |
| `[:hypatia, :soundness, :violation]` | `count` | `rule_module, rule_id, fixture` |

## Subscribers

Subscribe by attaching a handler with `:telemetry.attach_many/4`:

:telemetry.attach_many(
"my-handler",
Hypatia.Telemetry.all_events(),
fn event, measurements, metadata, _config ->
# handle event
end,
nil
)

The watcher (`Hypatia.Watcher`) does this on startup and aggregates
into rolling-window ETS tables.
"""

@scan_complete [:hypatia, :scan, :complete]
@dispatch_decision [:hypatia, :dispatch, :decision]
@outcome_recorded [:hypatia, :outcome, :recorded]
@verification_result [:hypatia, :verification, :result]
@quarantine_triggered [:hypatia, :quarantine, :triggered]
@rate_limit_exceeded [:hypatia, :rate_limit, :exceeded]
@neural_cycle [:hypatia, :neural, :cycle]
@soundness_violation [:hypatia, :soundness, :violation]

@all_events [
@scan_complete,
@dispatch_decision,
@outcome_recorded,
@verification_result,
@quarantine_triggered,
@rate_limit_exceeded,
@neural_cycle,
@soundness_violation
]

@doc "Every event the watcher should subscribe to."
def all_events, do: @all_events

# ─── Emit helpers ──────────────────────────────────────────────────────
#
# Hand-written rather than meta-programmed so each emit site shows
# what it's saying. Each helper takes the metadata fields as a
# keyword list to keep call sites self-documenting.

def scan_complete(duration_ms, findings, metadata) when is_integer(duration_ms) do
safe_execute(@scan_complete, %{duration_ms: duration_ms, findings: findings}, Map.new(metadata))
end

def dispatch_decision(confidence, metadata) when is_number(confidence) do
safe_execute(@dispatch_decision, %{confidence: confidence}, Map.new(metadata))
end

def outcome_recorded(metadata) do
safe_execute(@outcome_recorded, %{count: 1}, Map.new(metadata))
end

def verification_result(metadata) do
safe_execute(@verification_result, %{count: 1}, Map.new(metadata))
end

def quarantine_triggered(metadata) do
safe_execute(@quarantine_triggered, %{count: 1}, Map.new(metadata))
end

def rate_limit_exceeded(metadata) do
safe_execute(@rate_limit_exceeded, %{count: 1}, Map.new(metadata))
end

def neural_cycle(duration_ms, metadata) when is_integer(duration_ms) do
safe_execute(@neural_cycle, %{duration_ms: duration_ms}, Map.new(metadata))
end

def soundness_violation(metadata) do
safe_execute(@soundness_violation, %{count: 1}, Map.new(metadata))
end

# `:telemetry` is a transitive dep of phoenix/bandit, but if Hypatia
# is consumed in an unusual build (escript-only, stripped releases)
# the module may not be loaded. Wrap the call so a missing
# `:telemetry` is a no-op rather than a crash. Instrumentation must
# never break the host.
defp safe_execute(event, measurements, metadata) do
if Code.ensure_loaded?(:telemetry) do
:telemetry.execute(event, measurements, metadata)
end

:ok
end
end
Loading
Loading