Skip to content

refactor(canary): split _render_forensic — complexity 123, the highest in the backend — into one renderer per invariant #4459

Open

Description

@vybe

Summary

_render_forensic in src/backend/services/canary_alerts.py is 407 lines with cyclomatic complexity 123, the highest of any function in the backend. Split it into one renderer per forensic section.

Context

Found by the /refactor-audit run of 2026-11-10. In the June 2026 audit the same function was 183 lines at complexity 66, so it has roughly doubled in five months. _render_message in the same file is 199 lines at complexity 53.

The function turns a canary violation into alert text. Every invariant's branch lives in one body, so a missing or wrong branch is hard to notice by reading. #1880 was that class: five invariants alerted with no name, runbook or detail.

Acceptance Criteria

  • Each invariant (or invariant family) has its own renderer function, selected through a registry keyed by invariant id
  • _render_forensic and _render_message are each under complexity 15
  • Before the split, a golden-output test captures the rendered text for every invariant in the catalog; it passes unchanged after the split
  • A test fails when an invariant in the catalog has no registered renderer and no explicit generic fallback
  • No change to alert text, severity or routing

Technical Notes

  • Read docs/memory/architecture/reliability.md first; it owns canary_alerts.py.
  • src/backend/canary/ is the invariant library; the registry should key on its ids and not on a second hand-kept list.

Activity

  1. added
    priority-p3
    type-refactor
    theme-reliability
    complexity-low
    status-ready
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p3status-readytheme-reliabilitytype-refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions