mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-04 09:06:04 +09:00
Part 1 of 3 of the **Incident hardening (post-NOMAD-1632)** initiative (plan: `docs/archive/2026-09-12-incident-hardening-plan.md`, shipped in #984). Follow-up hardening for the incident class — a silent partial publish caught ~24h late, then its fix over-firing and failing real jobs, caught only when a human saw the UI. This PR bundles the three **detection & guardrail** items (the other two PRs cover test-seam/contract/process and deploy/CI/runner robustness). ## D1 — near-real-time alert on ANY job failure The production error monitor is **spike-based** and mathematically blind to a novel low-volume error (`rolling_avg == 0` → never a spike) — exactly why the 2-job guard-over-fire outage went unnoticed. New `backend/services/ops_alerts.py` hooks the single status-write chokepoint (`FirestoreService.update_job_status`) on any transition to `FAILED` and sends an immediate Discord alert to the ops channel, de-duped by a normalized error signature (reuses the error-monitor normalizer). **Novel signatures always alert regardless of count.** - Best-effort: fully wrapped, **never raises** into the status write. - Gated to deployed environments (`is_production`/`FAILURE_ALERTS_ENABLED`); skips `test`/`development` jobs. - Throttle window collapses a burst of the same signature into one alert (configurable). ## G1 — publish-boundary completeness invariant, SHADOW-first New `backend/services/publish_completeness.py` computes the outputs a public GDrive release *should* have shipped (lossy 4K + 720p, CDG when enabled) and compares against what was actually distributed (`gdrive_files`). The orchestrator logs + alerts any shortfall at the end of `_run_distribution` **without failing the job** (shadow mode). Closes the residual silent-partial hole the original encode()-boundary fix leaves open (a `None` slot arising *after* `encode()` — e.g. a per-file download failure — still publishes a partial today), at the right altitude. ## D2 — same-day silent-partial detection The post-job trigger now carries the just-published `brand_code` through (orchestrator → Cloud Tasks → internal endpoint → Cloud Function → validator). The validator verifies that track is present in all expected folders **same-run**, independent of the global-max gap logic (which only notices a missing latest track once a *later* track raises the max). Surfaces as a `missing_for_track` issue ~5 min post-job instead of up to 24h later. `brand_code` is optional; absent → full-scan behaviour as before. ## Tests - `backend/tests/test_ops_alerts.py` — novelty, throttle/collapse, skip-env, fail-open, best-effort-never-raises. - `backend/tests/test_publish_completeness.py` — incl. the exact 720p-missing (NOMAD-1632) case. - `infrastructure/functions/gdrive_validator/test_main.py` — `check_track_completeness` incl. the 720p-missing case + prefix-not-substring. - `backend/tests/test_services.py::TestFirestoreService` — FAILED fires the alert, non-FAILED doesn't, alerter error never breaks the status write. All new + touched-area tests pass locally (`test_ops_alerts`, `test_publish_completeness`, `test_gdrive_validation_delay`, `test_video_worker_orchestrator`, validator suite, `TestFirestoreService`). 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added targeted validation confirming published tracks appear in required Google Drive folders. * Added conditional CDG checks based on whether a CDG package was produced. * Added near-real-time job-failure alerts with duplicate suppression and retry eligibility after delivery failures. * Added completeness checks for public releases, including missing video and CDG outputs. * Added highlighted partial-publish notifications when track files are missing. * **Bug Fixes** * Prevented alert content from triggering Discord mentions. * **Documentation** * Documented targeted validation and failure-alerting lessons. * **Chores** * Updated the project version to 0.223.2. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
94 lines
3.7 KiB
Python
94 lines
3.7 KiB
Python
"""Unit tests for the publish-boundary completeness invariant (incident-hardening G1).
|
|
|
|
These pin the exact behaviour that closes the NOMAD-1632 silent-partial class: a
|
|
public GDrive release missing its 720p variant (or any expected output) must be
|
|
reported as a shortfall, while a complete release — and any non-public job —
|
|
reports nothing.
|
|
"""
|
|
|
|
from types import SimpleNamespace
|
|
|
|
from backend.services.publish_completeness import (
|
|
compute_publish_shortfall,
|
|
expected_gdrive_outputs,
|
|
)
|
|
|
|
|
|
def _config(gdrive_folder_id="folder-1", enable_cdg=True):
|
|
return SimpleNamespace(
|
|
gdrive_folder_id=gdrive_folder_id,
|
|
enable_cdg=enable_cdg,
|
|
artist="Nat King Cole",
|
|
title="Portrait of Jennie",
|
|
)
|
|
|
|
|
|
def _result(gdrive_files, cdg_produced=True):
|
|
"""A result whose CDG was produced by default (final_karaoke_cdg_zip set)."""
|
|
return SimpleNamespace(
|
|
gdrive_files=gdrive_files,
|
|
brand_code="NOMAD-1632",
|
|
final_karaoke_cdg_zip=("/tmp/x.zip" if cdg_produced else None),
|
|
)
|
|
|
|
|
|
class TestExpectedOutputs:
|
|
def test_cdg_produced_expects_all_three(self):
|
|
res = _result({}, cdg_produced=True)
|
|
assert expected_gdrive_outputs(_config(enable_cdg=True), res) == ["mp4", "mp4_720p", "cdg"]
|
|
|
|
def test_cdg_not_produced_expects_two(self):
|
|
res = _result({}, cdg_produced=False)
|
|
assert expected_gdrive_outputs(_config(enable_cdg=True), res) == ["mp4", "mp4_720p"]
|
|
|
|
|
|
class TestComputePublishShortfall:
|
|
def test_complete_release_no_shortfall(self):
|
|
cfg = _config(enable_cdg=True)
|
|
res = _result({"mp4": "id1", "mp4_720p": "id2", "cdg": "id3"})
|
|
assert compute_publish_shortfall(cfg, res) == []
|
|
|
|
def test_missing_720p_is_the_incident(self):
|
|
"""The NOMAD-1632 case: 4K + CDG shipped, 720p silently skipped."""
|
|
cfg = _config(enable_cdg=True)
|
|
res = _result({"mp4": "id1", "cdg": "id3"})
|
|
assert compute_publish_shortfall(cfg, res) == ["720p MP4"]
|
|
|
|
def test_missing_cdg_when_produced(self):
|
|
"""CDG was produced but not distributed → real shortfall."""
|
|
cfg = _config(enable_cdg=True)
|
|
res = _result({"mp4": "id1", "mp4_720p": "id2"}, cdg_produced=True)
|
|
assert compute_publish_shortfall(cfg, res) == ["CDG zip"]
|
|
|
|
def test_lyricless_skip_does_not_require_cdg(self):
|
|
"""enable_cdg True but no CDG produced (lyric-less track) → no false alarm."""
|
|
cfg = _config(enable_cdg=True)
|
|
res = _result({"mp4": "id1", "mp4_720p": "id2"}, cdg_produced=False)
|
|
assert compute_publish_shortfall(cfg, res) == []
|
|
|
|
def test_cdg_not_expected_when_disabled(self):
|
|
cfg = _config(enable_cdg=False)
|
|
res = _result({"mp4": "id1", "mp4_720p": "id2"}, cdg_produced=False)
|
|
assert compute_publish_shortfall(cfg, res) == []
|
|
|
|
def test_empty_distribution_lists_all_expected(self):
|
|
cfg = _config(enable_cdg=True)
|
|
res = _result({}, cdg_produced=True)
|
|
assert compute_publish_shortfall(cfg, res) == ["lossy 4K MP4", "720p MP4", "CDG zip"]
|
|
|
|
def test_empty_string_id_counts_as_missing(self):
|
|
cfg = _config(enable_cdg=False)
|
|
res = _result({"mp4": "id1", "mp4_720p": ""}, cdg_produced=False)
|
|
assert compute_publish_shortfall(cfg, res) == ["720p MP4"]
|
|
|
|
def test_non_public_job_never_flags(self):
|
|
"""No gdrive_folder_id → not a public-share release → always clean."""
|
|
cfg = _config(gdrive_folder_id=None, enable_cdg=True)
|
|
res = _result({})
|
|
assert compute_publish_shortfall(cfg, res) == []
|
|
|
|
def test_none_gdrive_files_is_safe(self):
|
|
cfg = _config(enable_cdg=False)
|
|
res = _result(None, cdg_produced=False)
|
|
assert compute_publish_shortfall(cfg, res) == ["lossy 4K MP4", "720p MP4"]
|