Skip to content

fix(mcp-query): recent_alerts applies the severity floor before the LIMIT - #98

Merged
fastrevmd-lab merged 1 commit into
mainfrom
fix/recent-alerts-floor-before-limit
Nov 12, 2026
Merged

fastrevmd-lab merged 1 commit into
mainfrom
fix/recent-alerts-floor-before-limit

Conversation

@fastrevmd-lab

Copy link
Copy Markdown
Member

Summary

recent_alerts could return zero rows with truncated: true while medium and high alerts sat in the window, and it named every Junos screen alert N/A.

  • Floor after LIMIT. The query took the newest LIMIT alert-class rows and dropped those below min_severity afterwards, in Python. Every PAN-OS system log is alert-class (it carries panw.panos.severity) and most are informational, so on a busy fabric they fill the LIMIT and nothing survives the filter.
  • Screen signature. The signature came from rule_name, which is the literal N/A for RT_SCREEN rows. The screen name is in event_action, which was never selected.

Changes

  • alerts.py: SEVERITY_NUM_SQL, a ClickHouse expression mirroring normalize_severity, is added to the WHERE as >= %(floor)s. normalize_severity still labels each row and the Python floor stays as a second check.
  • alerts.py: select event_action; signature falls back unifi.ips.signature → rule_name (unless empty or N/A) → event_action → event_kind.
  • test_alerts.py: the floor is in the WHERE and in the params; an unknown min_severity floors at high; a screen row is named by its event action.

Touches an MCP tool that reads ssdf.events. No schema change and nothing changes for stored data. Visible behaviour changes: calls that returned nothing now return rows, and rows with no rule name (Junos screens, PAN-OS system logs) get their event action as the signature instead of N/A or the event kind.

Verification

cd services/mcp-query && uv run pytest -m "not integration" -q   # 734 passed, 48 skipped
ruff check services/mcp-query && ruff format --check services/mcp-query   # clean
# new tests against main's alerts.py: 3 failed, 12 passed
gitleaks git --staged --no-banner --redact .   # no leaks found

The built SQL was also run read-only against a live event store: the default call (min_severity=high, 24h, limit 500) returned rows where the old query returned none, and a GROUP BY on the SQL expression matched the per-severity counts from the Python mapping.

pip-audit did not run (it cannot install the editable services/common requirement); this change touches no dependency.

Checklist

  • ruff check services and ruff format --check services pass
  • Unit tests added or updated for this change, and they fail against the old code
  • Touches an MCP tool that reads ssdf.events: called out above
  • Test data is synthetic
  • No secrets, credentials, real hostnames, serials, or real configs
  • No new telemetry, analytics, or outbound network call
  • Deterministic code decides; no model output is wired to act

Anything you're unsure about

  • The severity mapping now exists twice (Python and SQL). They differ in one corner: a severity key that is present but empty is medium in Python for PAN-OS, and falls through to the next rule in SQL. Both end at medium unless the row also carries syslog.severity.
  • truncated is still fetched rows >= limit, which is now a count of matching rows, so it means what its name says.

🤖 Generated with Claude Code

…IMIT

recent_alerts took the newest LIMIT alert-class rows and only then dropped
the ones below min_severity in Python. Every PAN-OS system log is
alert-class (it carries panw.panos.severity) and most are informational,
so on a busy fabric they fill the LIMIT and the tool returns zero rows
with truncated=true while medium and high alerts sit in the window. The
floor is now a ClickHouse expression in the WHERE, mirroring
normalize_severity, which still labels each row.

Junos screen (RT_SCREEN) rows were also all named "N/A": the signature
came from rule_name, which is that literal for screens. The screen name is
in event_action, which is now selected and used when there is no rule name.

Co-Authored-By: Claude <noreply@anthropic.com>
@fastrevmd-lab
fastrevmd-lab merged commit 225b3ce into main Nov 12, 2026
15 checks passed
@fastrevmd-lab
fastrevmd-lab deleted the fix/recent-alerts-floor-before-limit branch November 12, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant