Compare commits

...
2 Commits
Author SHA1 Message Date
Andrew Beveridge ☄️andClaude Opus 5.5 cd11088be5 feat(match-judge): ground AI corrections in live Spotify (v0.294.0) (#1256)
## Summary
At a live kjbox night (2026-11-05, job `f01c9997`) a singer typed "Cole
Phillips – Roll of Dice", a 2025 release missing from our BigQuery
catalogue. Gemini "corrected" it to "Roll of the Dice" twice:
1. in the free-text resolver;
2. in the job form's full judge, after the singer had tapped "keep what
I typed".

Both times the catalogue check found nothing, and the guess still went
out as `confident` ("No catalog match → AI verdict unchanged").
Meanwhile the audio search had found the real track on Spotify at score
1.0.

This PR asks **live Spotify** before trusting Gemini, and **grounds**
every AI correction in real data.

Depends on karaoke-decide's new `GET /api/partner/gen/spotify-search`.
Until that's deployed, the call returns `None` ("no evidence"). That is
safe, but it makes AI corrections non-confident in the meantime, so
**merge and deploy decide first**.

## Changes
- `catalog_proxy_service.spotify_search`: calls decide's partner route
with `X-Partner-Secret` (the existing `decide_partner_secret`). Cached
10 min. Returns `None` when there's no evidence. Never raises.
- `match_judge/service.py`, fast and full passes:
- When the catalogue can't confirm the typed song, an exact live-Spotify
hit is a confident catalog verdict. No Gemini call: "Roll of dice" →
"Roll of Dice".
  - Spotify results go to Gemini as candidates.
- **Grounding rule:** a confident AI cosmetic/content verdict must name
a song in the candidates, or found by one Spotify lookup of it.
Otherwise `confident=False`, which both clients already show as "Did you
mean…?" and never apply.
- `match_judge/free_text.py` (`resolve_and_tidy`):
- Live Spotify first, capped at 3 s so the worst case stays inside
decide's 25 s wait.
  - A result that *is* the query (either order) answers it directly.
  - Otherwise the results go into the Gemini prompt.
- An AI answer the tidy can't confirm is not confident. A timed-out tidy
keeps today's behaviour (`unavailable`, not cached).
- Docs: API.md (judge + resolver), LESSONS-LEARNED entry. Version
0.294.0.

## Testing
- New and updated unit tests:
`tests/services/match_judge/test_service.py` (Spotify confirm in
fast/full, catalogue hit skips Spotify, unconfirmed downgrade with the
Cole Phillips case, second-lookup confirm, any credited artist, Spotify
failure, ambiguous untouched, merge), `test_match_judge_free_text.py`
(exact Spotify answer without Gemini, both word orders, downgrade, AI
pick from live results kept, results in prompt, Spotify timeout/failure
fallback), `tests/services/test_catalog_proxy_spotify.py` (secret
header, cache, no-evidence cases, network error, no secret).
- `make test-backend-unit`: 7212 passed. The 8 failures in
`test_emulator_integration.py` also fail on `main` without the emulator
stack.
- Local CodeRabbit review: no findings.

@coderabbitai ignore

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_012RYLVUQ68KFYtZF7vEfhAq

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 20:00:50 -05:00
Andrew Beveridge ☄️andClaude Opus 5.5 5c41be1d3b fix(error-monitor): ignore runner-manager's other Cloud Run abort wording (#1252)
## Summary
On 2026-11-04 at 10:00Z a PR opened on python-audio-separator queued a
burst of `workflow_job` webhooks. 22 of 54 deliveries got a 500 from
Cloud Run while `github-runner-manager` scaled up. The error monitor
alerted because Cloud Run used a second wording, "aborted after waiting
too long for an available instance", which the existing ignore pattern
didn't match.

There was no impact: all self-hosted jobs in the affected runs got
runners within about 3.5 minutes and passed.

## Changes
- **Error monitor:** match both Cloud Run abort wordings for
`github-runner-manager`, but only on **webhook** requests (the bare
function URL). Cloud Scheduler ticks (`?action=check_idle`) and entries
with no URL still alert. `_SERVICE_URL_IGNORE_PATTERNS` now accepts a
predicate on the split URL as well as an exact path.
- **Runner manager (Cloud Function):** the ignore relies on the 5-minute
stalled-job tick catching dropped webhooks, so that tick's own failures
now log as structured `severity: ERROR` and page. That covers a
redispatch crash, a repo skipped in the scan, a job given up at 60 min,
and a failed re-dispatch. Before this, they were severity-less prints
that nothing alerted on. Both changes came from CodeRabbit feedback.
- Corrected the pattern's reason: GitHub did not redeliver these
(`redelivery: false`).
- Version 0.292.1; merged latest main.

## ⚠️ Deploy
`infrastructure/functions/runner_manager/` only goes live with a local
`pulumi up` (CI never runs Pulumi). Apply it **before** merging, so the
safety net's alerting is live before the webhook-abort ignore ships with
the backend deploy.

## Testing
- `tests/unit/services/error_monitor/`: 452 passed (webhook,
scheduler-tick, no-URL and other-service cases for both wordings).
- `infrastructure/functions/runner_manager`: 71 passed. The 4 updated
tests fail against main's `ephemeral.py`.
- Full `make test` not run locally; relying on CI.

Alert:
https://discord.com/channels/1198691101943541770/1265517528608669707/1567479055887114363

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_012RYLVVTKcWCcDArTQrjS82

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Runner-manager webhook errors are filtered more precisely. Recognized
abort messages are ignored only for matching webhook requests;
scheduler-tick errors, requests with other URLs, and requests without a
URL continue to trigger alerts.
* Runner-manager job-processing failures are recorded as structured
error logs, including stalled-job scans and dispatch failures. Give-up
logs now report the configured limit rather than the job’s current time
in the queue.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 19:58:46 -05:00
13 changed files with 712 additions and 56 deletions

No files matched your search

+40
View File
@@ -103,3 +103,43 @@ async def search_tracks(query: str, artist: str | None = None, limit: int = 10)
except Exception as e:
logger.warning(f"Failed to search tracks from karaoke-decide: {e}")
return []
SPOTIFY_SEARCH_TIMEOUT = 6 # seconds; decide's own Spotify call is capped at 4 s
SPOTIFY_CACHE_TTL_SECONDS = 600
async def spotify_search(query: str, limit: int = 10) -> list[dict] | None:
"""Live Spotify track search via karaoke-decide (partner route, X-Partner-Secret).
Lets the match judge confirm a song our BigQuery catalogue doesn't know (it's a July 2025
Spotify snapshot + MusicBrainz, so 2025+ releases and small artists are missing). Returns
track dicts with ``artist_name`` / ``track_name`` (the catalogue candidate shape), or
**None when there's no evidence either way**: secret unset, decide down, or decide's
Spotify breaker open. Never raises.
"""
secret = getattr(settings, "decide_partner_secret", "") or ""
q = " ".join((query or "").split())
if not secret or len(q) < 2:
return None
cache_key = f"spotify:{q.casefold()}:{limit}"
cached = _cache_get(cache_key)
if cached is not None:
return cached
url = f"{_get_base_url()}/api/partner/gen/spotify-search"
try:
async with httpx.AsyncClient(timeout=SPOTIFY_SEARCH_TIMEOUT) as client:
resp = await client.get(
url, params={"q": q[:300], "limit": min(limit, 10)},
headers={"X-Partner-Secret": secret},
)
resp.raise_for_status()
data = resp.json()
except Exception as e:
logger.warning(f"Spotify search via karaoke-decide failed: {e}")
return None
if not isinstance(data, dict) or not data.get("available"):
return None
tracks = [t for t in data.get("tracks") or [] if isinstance(t, dict)]
_cache_set(cache_key, tracks, SPOTIFY_CACHE_TTL_SECONDS)
return tracks
+33 -18
View File
@@ -14,8 +14,9 @@ Usage:
"""
import re
from collections.abc import Callable
from dataclasses import dataclass
from urllib.parse import urlsplit
from urllib.parse import SplitResult, urlsplit
@dataclass
@@ -119,17 +120,6 @@ _IGNORE_PATTERNS: list[tuple[str, re.Pattern, str]] = [
# Patterns that are noise only for specific services: the same message from a
# customer-facing service is a real problem. (pattern_name, services, regex, reason)
_SERVICE_IGNORE_PATTERNS: list[tuple[str, frozenset[str], re.Pattern, str]] = [
(
"runner_manager_no_available_instance",
frozenset({"github-runner-manager"}),
re.compile(
r"request was aborted because there was no available instance",
re.IGNORECASE,
),
"github-runner-manager scale-up burst: Cloud Run aborts webhook requests "
"while instances start, GitHub redelivers and the runners still dispatch "
"(seen 2026-09-20, 09-23, 09-27, 10-08)",
),
(
"audio_separation_experiment_traceback",
frozenset({"audio-separation-job"}),
@@ -151,9 +141,33 @@ _SERVICE_IGNORE_PATTERNS: list[tuple[str, frozenset[str], re.Pattern, str]] = [
]
# Noise only for one service AND request URL path (Cloud Run request logs carry the
# URL outside the message). (pattern_name, services, message regex, exact URL path, reason)
_SERVICE_URL_IGNORE_PATTERNS: list[tuple[str, frozenset[str], re.Pattern, str, str]] = [
def _is_runner_manager_webhook(url: SplitResult) -> bool:
"""GitHub webhooks hit the bare function URL; Cloud Scheduler ticks add ``?action=``."""
return url.path == "/github-runner-manager" and not url.query
# Noise only for one service AND request URL (Cloud Run request logs carry the URL
# outside the message). The URL matcher is an exact path, or a predicate on the split
# URL when the path alone can't tell requests apart.
# (pattern_name, services, message regex, URL path or predicate, reason)
_SERVICE_URL_IGNORE_PATTERNS: list[
tuple[str, frozenset[str], re.Pattern, str | Callable[[SplitResult], bool], str]
] = [
(
"runner_manager_no_available_instance",
frozenset({"github-runner-manager"}),
re.compile(
r"request was aborted (because there was no available instance"
r"|after waiting too long for an available instance)",
re.IGNORECASE,
),
_is_runner_manager_webhook,
"github-runner-manager scale-up burst: Cloud Run aborts webhook requests "
"while instances start. GitHub does not redeliver them; the 5-minute "
"redispatch_stalled_jobs tick launches a runner for any job left queued "
"(seen 2026-09-20, 09-23, 09-27, 10-08, 11-04). Scheduler ticks are not "
"ignored: a failed tick means that safety net didn't run.",
),
(
"decide_search_health_abort",
frozenset({"karaoke-decide-search"}),
@@ -196,9 +210,10 @@ def should_ignore(service: str, message: str, request_url: str | None = None) ->
return IgnoreReason(pattern_name=pattern_name, reason=reason)
if request_url:
url_path = urlsplit(request_url).path
for pattern_name, services, pattern, path, reason in _SERVICE_URL_IGNORE_PATTERNS:
if service in services and url_path == path and pattern.search(message):
url = urlsplit(request_url)
for pattern_name, services, pattern, url_match, reason in _SERVICE_URL_IGNORE_PATTERNS:
url_ok = url_match(url) if callable(url_match) else url.path == url_match
if service in services and url_ok and pattern.search(message):
return IgnoreReason(pattern_name=pattern_name, reason=reason)
return None
+92 -6
View File
@@ -9,6 +9,12 @@ verdict shape (kind cosmetic/content/ambiguous/none) plus the typed split.
:func:`judge_match` catalog pass, so the singer gets exactly the canonical
artist/title gen's AudioSourceStep would "tidy" to (the catalog can't parse a
one-line query itself — the AI only does the split + typo fix).
Live Spotify (2026-11-06, see ``service.py``'s grounding rule): before Gemini, the
raw query goes to a live Spotify search. A result whose artist + title IS the query
(either order, after normalising) answers it without Gemini. Otherwise Gemini gets
those results as candidates, and a confident answer that neither the catalogue nor
Spotify can confirm is returned with ``confident: false``.
"""
from __future__ import annotations
@@ -26,6 +32,7 @@ logger = logging.getLogger(__name__)
Generate = Callable[[str, str, str], Awaitable[dict]]
Judge = Callable[[str, str], Awaitable[MatchVerdict]]
SpotifySearch = Callable[[str], Awaitable[Optional[list[dict]]]]
_VALID_KINDS = {"cosmetic", "content", "ambiguous"}
@@ -45,6 +52,10 @@ GEMINI_ATTEMPT_TIMEOUT_S = 8.0
GEMINI_MIN_DEADLINE_MS = 10000 # the API's minimum request deadline
GEMINI_ATTEMPTS = 2
CATALOG_TIDY_TIMEOUT_S = 4.0
# Live Spotify lookup before Gemini. Kept short so the worst case still fits decide's
# 25 s wait: 3 + 2 x 8 (Gemini) + 4 (tidy) = 23 s. A miss just means no candidates.
SPOTIFY_TIMEOUT_S = 3.0
SPOTIFY_CANDIDATES_FOR_AI = 8
# In-flight catalog searches. One the tidy gave up on keeps running (warming decide and
# the catalog proxy's cache for the next ask); strong refs so it isn't GC'd mid-flight.
@@ -75,7 +86,10 @@ _SYSTEM_PROMPT = (
" 'ambiguous' – several real songs are plausible; list them in alternatives.\n"
" 'none' – not a recognisable song, or you are not sure.\n"
"confident: true only when you are sure which released song is meant. Never "
"invent songs; prefer kind='none' when unsure. canonical_artist/canonical_title "
"invent songs; prefer kind='none' when unsure. When live Spotify results are "
"listed, they are real released songs: if one is the song meant, use its exact "
"artist and title. Never 'correct' a query to a different, more familiar title "
"when a listed result matches what was typed. canonical_artist/canonical_title "
"must use official formatting. alternatives is a list of {artist,title} (max 4)."
)
@@ -167,8 +181,18 @@ def _retryable(exc: BaseException) -> bool:
return not (isinstance(code, int) and 400 <= code < 500 and code != 429)
def _user_prompt(query: str, candidates: Optional[list[dict]]) -> str:
lines = [f'Singer typed: "{query}"']
if candidates:
lines.append("Live Spotify search results for this query (real released songs):")
lines += [f'- {c.get("artist_name", "")} — {c.get("track_name", "")}'
for c in candidates[:SPOTIFY_CANDIDATES_FOR_AI]]
return "\n".join(lines)
async def resolve_free_text(query: str, *, generate: Optional[Generate] = None,
model: Optional[str] = None) -> dict:
model: Optional[str] = None,
candidates: Optional[list[dict]] = None) -> dict:
"""Split + correct a free-text song query. Never raises: failures → kind 'none'.
Each Gemini attempt is bounded by GEMINI_ATTEMPT_TIMEOUT_S; a transient failure
@@ -180,7 +204,7 @@ async def resolve_free_text(query: str, *, generate: Optional[Generate] = None,
for attempt in range(1, GEMINI_ATTEMPTS + 1):
try:
data = await asyncio.wait_for(
gen(model or _model(), _SYSTEM_PROMPT, f'Singer typed: "{query}"'),
gen(model or _model(), _SYSTEM_PROMPT, _user_prompt(query, candidates)),
GEMINI_ATTEMPT_TIMEOUT_S)
except Exception as e:
reason = f"timed out after {GEMINI_ATTEMPT_TIMEOUT_S:g}s" if isinstance(e, asyncio.TimeoutError) else str(e)
@@ -192,8 +216,49 @@ async def resolve_free_text(query: str, *, generate: Optional[Generate] = None,
return _none(query, "unavailable") # pragma: no cover - loop always returns
def _split_like_query(query: str, artist: str, title: str) -> tuple[str, str]:
"""The query's own words as (typed_artist, typed_title), given the song it names."""
words = query.split()
n_artist = len(artist.split())
if normalize_for_match(" ".join(words[:n_artist])) == normalize_for_match(artist):
return " ".join(words[:n_artist]), " ".join(words[n_artist:])
n_title = len(title.split())
if normalize_for_match(" ".join(words[:n_title])) == normalize_for_match(title):
return " ".join(words[n_title:]), " ".join(words[:n_title])
return artist, title
def _spotify_exact(query: str, live: list[dict]) -> Optional[dict]:
"""A live result whose artist + title is exactly the query (either order) → its verdict."""
typed = normalize_for_match(query)
if not typed:
return None
for c in live:
ca, ct = (c.get("artist_name") or "").strip(), (c.get("track_name") or "").strip()
if not (ca and ct):
continue
if typed in (normalize_for_match(f"{ca} {ct}"), normalize_for_match(f"{ct} {ca}")):
typed_artist, typed_title = _split_like_query(query, ca, ct)
return {
"kind": KIND_COSMETIC, "confident": True,
"typed_artist": typed_artist, "typed_title": typed_title,
"canonical_artist": ca, "canonical_title": ct, "alternatives": [],
"engine": "catalog", "reason": "live Spotify match",
}
return None
async def _live_spotify(spotify: SpotifySearch, query: str) -> list[dict]:
try:
return list(await asyncio.wait_for(spotify(query), SPOTIFY_TIMEOUT_S) or [])
except Exception as e: # timeout / decide down: no evidence, carry on as before
logger.warning(f"resolve_and_tidy live Spotify search failed: {e!r}")
return []
async def resolve_and_tidy(query: str, *, generate: Optional[Generate] = None,
judge: Optional[Judge] = None, model: Optional[str] = None) -> dict:
judge: Optional[Judge] = None, model: Optional[str] = None,
spotify: Optional[SpotifySearch] = None) -> dict:
"""Free-text split/correct, then gen's catalog tidy (the job flow's fast pass).
The AI verdict's song — its canonical pick when confident, else the typed
@@ -201,9 +266,17 @@ async def resolve_and_tidy(query: str, *, generate: Optional[Generate] = None,
"fast": catalog only, no second AI call). A confident catalog match wins:
its formatting becomes the canonical artist/title, kind "cosmetic" when the
query already named that song (only casing/punctuation/word order differ),
else "content". No catalog match → the AI verdict unchanged. Never raises.
else "content". No catalog match → the AI verdict, but a confident answer
that live Spotify didn't list either comes back ``confident: False``
(grounding rule). A timed-out tidy keeps the AI verdict as "unavailable"
(transient, not cached). A live Spotify result that IS the query answers
before Gemini runs. Never raises.
"""
verdict = await resolve_free_text(query, generate=generate, model=model)
live = await _live_spotify(spotify or _default_spotify, query)
exact = _spotify_exact(query, live)
if exact is not None:
return exact
verdict = await resolve_free_text(query, generate=generate, model=model, candidates=live)
kind = verdict.get("kind")
if kind in ("cosmetic", "content") and verdict.get("confident"):
artist, title = verdict["canonical_artist"], verdict["canonical_title"]
@@ -231,6 +304,14 @@ async def resolve_and_tidy(query: str, *, generate: Optional[Generate] = None,
return {**verdict, "reason": "unavailable"}
if not (cat.confident and cat.engine == "catalog" and cat.kind in (KIND_COSMETIC, KIND_NONE)
and cat.canonical_artist and cat.canonical_title):
if verdict.get("confident") and kind in ("cosmetic", "content"):
from backend.services.match_judge.service import names_song
if not names_song(live, artist, title):
# Grounding rule: neither the catalogue nor live Spotify has the song the
# AI named, so it's a guess. Offer it at most ("Did you mean…?").
logger.info(f"resolve_and_tidy: AI answer {artist!r} / {title!r} unconfirmed; not confident")
return {**verdict, "confident": False,
"reason": f"{verdict.get('reason') or ''} [unconfirmed: not in catalog or Spotify]".strip()}
return verdict
ca, ct = cat.canonical_artist, cat.canonical_title
typed = normalize_for_match(query)
@@ -252,6 +333,11 @@ async def _default_judge(artist: str, title: str) -> MatchVerdict:
return await judge_match(artist, title, stage="fast")
async def _default_spotify(query: str) -> Optional[list[dict]]:
from backend.services.catalog_proxy_service import spotify_search
return await spotify_search(query)
async def _default_generate(model: str, system_prompt: str, user_prompt: str) -> dict:
return await asyncio.to_thread(_blocking_generate, model, system_prompt, user_prompt)
+95 -3
View File
@@ -3,14 +3,26 @@
Composes the deterministic+catalog classifier with the AI judge. The AI judge
runs ONLY when the classifier can't confidently decide, and never blocks or
errors the caller — any failure degrades to "no suggestion".
Evidence comes from two places: decide's BigQuery catalogue (a July 2025 Spotify
snapshot + MusicBrainz) and, when that can't confirm the typed song, a **live
Spotify search** (via decide's partner route). The live search is what knows 2025+
releases and small artists.
**Grounding rule** (2026-11-06, "Cole Phillips – Roll of Dice" became "Roll of the
Dice"): a confident AI correction must name a song the catalogue or live Spotify
actually has. Otherwise it is downgraded to ``confident=False``, which both clients
show as "Did you mean…?" and never apply.
"""
from __future__ import annotations
import logging
from typing import Awaitable, Callable, Literal, Optional
from backend.services.match_judge.classifier import classify_catalog_match
from backend.services.match_judge.verdict import KIND_NONE, MatchVerdict
from dataclasses import replace
from backend.services.match_judge.classifier import classify_catalog_match, normalize_for_match
from backend.services.match_judge.verdict import KIND_CONTENT, KIND_COSMETIC, KIND_NONE, MatchVerdict
logger = logging.getLogger(__name__)
@@ -25,6 +37,8 @@ WEAK_TIER = 3
SearchTracks = Callable[[str, Optional[str], int], Awaitable[list[dict]]]
AiJudge = Callable[[str, str, list[dict], Optional[int]], Awaitable[MatchVerdict]]
# Live Spotify track search: candidate dicts, or None when there's no evidence either way.
SpotifySearch = Callable[[str], Awaitable[Optional[list[dict]]]]
async def _default_search_tracks(query: str, artist: Optional[str], limit: int) -> list[dict]:
@@ -32,6 +46,72 @@ async def _default_search_tracks(query: str, artist: Optional[str], limit: int)
return await search_tracks(query, artist, limit)
async def _default_spotify_search(query: str) -> Optional[list[dict]]:
from backend.services.catalog_proxy_service import spotify_search
return await spotify_search(query)
async def _safe_spotify(spotify: SpotifySearch, query: str) -> list[dict]:
try:
return list(await spotify(query) or [])
except Exception:
logger.warning("match-judge Spotify search failed; continuing without it", exc_info=True)
return []
def _merge(*groups: list[dict]) -> list[dict]:
"""Concatenate candidate lists, dropping repeats of the same artist/title."""
seen: set[tuple[str, str]] = set()
out: list[dict] = []
for group in groups:
for c in group:
key = (normalize_for_match(c.get("artist_name") or ""), normalize_for_match(c.get("track_name") or ""))
if key in seen or not all(key):
continue
seen.add(key)
out.append(c)
return out
def _names(candidate: dict) -> list[str]:
names = [candidate.get("artist_name") or ""] + list(candidate.get("artist_names") or [])
return [n for n in names if n]
def names_song(candidates: list[dict], artist: str, title: str) -> bool:
"""True when a candidate is this artist/title, after normalising. Any credited artist counts."""
key_artist, key_title = normalize_for_match(artist), normalize_for_match(title)
if not key_artist or not key_title:
return False
return any(
normalize_for_match(c.get("track_name") or "") == key_title
and any(normalize_for_match(n) == key_artist for n in _names(c))
for c in candidates
)
async def ground_verdict(
verdict: MatchVerdict, candidates: list[dict], spotify: SpotifySearch
) -> MatchVerdict:
"""Apply the grounding rule to an AI verdict (see module docstring).
A confident cosmetic/content verdict is kept only when its canonical song is
among ``candidates``, or turns up in one live Spotify search for it.
"""
if not (verdict.confident and verdict.kind in (KIND_COSMETIC, KIND_CONTENT)):
return verdict
artist, title = verdict.canonical_artist, verdict.canonical_title
if names_song(candidates, artist, title):
return verdict
if names_song(await _safe_spotify(spotify, f"{artist} {title}"), artist, title):
return verdict
logger.info(
f"match-judge: AI suggestion {artist!r} / {title!r} not found in catalog or Spotify; "
"downgraded to not confident"
)
return replace(verdict, confident=False, reason=f"{verdict.reason} [unconfirmed: not in catalog or Spotify]".strip())
async def _default_ai_judge(
artist: str, title: str, candidates: list[dict], audio_tier: Optional[int]
) -> MatchVerdict:
@@ -55,6 +135,7 @@ async def judge_match(
stage: Stage = "full",
search_tracks: Optional[SearchTracks] = None,
ai_judge: Optional[AiJudge] = None,
spotify_search: Optional[SpotifySearch] = None,
enabled: Optional[bool] = None,
) -> MatchVerdict:
"""Return a :class:`MatchVerdict` for the typed artist/title.
@@ -73,6 +154,7 @@ async def judge_match(
"""
search = search_tracks or _default_search_tracks
ai = ai_judge or _default_ai_judge
spotify = spotify_search or _default_spotify_search
if enabled is None:
enabled = _default_enabled()
@@ -86,6 +168,14 @@ async def judge_match(
candidates = []
catalog_verdict = classify_catalog_match(artist, title, candidates)
if catalog_verdict is None:
# The snapshot doesn't have it: ask live Spotify. An exact (normalised) hit
# confirms the typed song, e.g. "Roll of dice" -> "Roll of Dice".
live = await _safe_spotify(spotify, f"{artist} {title}")
spotify_verdict = classify_catalog_match(artist, title, live)
if spotify_verdict is not None:
catalog_verdict = replace(spotify_verdict, reason=f"{spotify_verdict.reason} (live Spotify)")
candidates = _merge(candidates, live)
# Fast pass: catalog only. Never touches the AI layer.
if stage == "fast":
@@ -115,6 +205,7 @@ async def judge_match(
exc_info=True,
)
return catalog_verdict
ai_verdict = await ground_verdict(ai_verdict, candidates, spotify)
if ai_verdict.confident and ai_verdict.kind != KIND_NONE:
return ai_verdict
return catalog_verdict
@@ -125,7 +216,7 @@ async def judge_match(
)
try:
return await ai(artist, title, candidates, audio_tier)
ai_verdict = await ai(artist, title, candidates, audio_tier)
except Exception as exc:
from backend.services.gemini_client import note_gemini_failure
@@ -139,3 +230,4 @@ async def judge_match(
return MatchVerdict(
KIND_NONE, True, artist, title, engine="ai", reason="ai failed"
)
return await ground_verdict(ai_verdict, candidates, spotify)
@@ -26,6 +26,22 @@ def _ai_never(*_a, **_k):
raise AssertionError("AI judge must not be called")
def _spotify(*pairs, calls=None):
async def _search(query):
if calls is not None:
calls.append(query)
return [{"artist_name": a, "track_name": t} for a, t in pairs]
return _search
@pytest.fixture(autouse=True)
def _no_live_spotify(monkeypatch):
"""Tests that don't inject a Spotify search get "no evidence" (never the network)."""
async def none(query):
return None
monkeypatch.setattr("backend.services.match_judge.service._default_spotify_search", none)
@pytest.mark.asyncio
async def test_catalog_cosmetic_match_skips_ai():
calls = {"ai": 0}
@@ -68,12 +84,17 @@ async def test_no_catalog_match_calls_ai():
audio_tier=3,
search_tracks=_candidates(("Queen", "Radio Ga Ga")),
ai_judge=ai,
spotify_search=_spotify(("Queen", "Bohemian Rhapsody")),
enabled=True,
)
assert verdict.kind == KIND_CONTENT
assert verdict.engine == "ai"
assert verdict.confident is True # grounded: live Spotify lists the AI's song
assert seen["tier"] == 3
assert seen["candidates"] == [{"artist_name": "Queen", "track_name": "Radio Ga Ga"}]
assert seen["candidates"] == [
{"artist_name": "Queen", "track_name": "Radio Ga Ga"},
{"artist_name": "Queen", "track_name": "Bohemian Rhapsody"},
]
@pytest.mark.asyncio
@@ -165,7 +186,7 @@ async def test_weak_tier_catalog_match_is_verified_and_overridden_by_ai():
verdict = await judge_match(
"Queen", "Bohemian Rapsody",
audio_tier=3,
search_tracks=_candidates(("Queen", "Bohemian Rapsody")),
search_tracks=_candidates(("Queen", "Bohemian Rapsody"), ("Queen", "Bohemian Rhapsody")),
ai_judge=ai,
enabled=True,
)
@@ -174,6 +195,23 @@ async def test_weak_tier_catalog_match_is_verified_and_overridden_by_ai():
assert verdict.engine == "ai"
@pytest.mark.asyncio
async def test_weak_tier_unconfirmed_ai_override_keeps_catalog_verdict():
async def ai(artist, title, candidates, audio_tier):
return MatchVerdict(KIND_CONTENT, True, "Queen", "Bohemian Rhapsody", engine="ai")
verdict = await judge_match(
"Queen", "Bohemian Rapsody",
audio_tier=3,
search_tracks=_candidates(("Queen", "Bohemian Rapsody")),
ai_judge=ai,
spotify_search=_spotify(),
enabled=True,
)
# The AI's song is in neither source, so it can't override the catalog match.
assert verdict.engine == "catalog"
@pytest.mark.asyncio
async def test_strong_tier_catalog_match_still_skips_ai():
verdict = await judge_match(
@@ -270,3 +308,128 @@ async def test_catalog_search_failure_still_lets_ai_run():
enabled=True,
)
assert verdict.engine == "ai"
# --- Live Spotify + grounding (2026-11-06: "Cole Phillips – Roll of Dice" became "Roll of the Dice") ---
@pytest.mark.asyncio
@pytest.mark.parametrize("stage", ["fast", "full"])
async def test_live_spotify_confirms_typed_song_missing_from_catalog(stage):
calls = []
verdict = await judge_match(
"Cole Phillips", "Roll of dice",
audio_tier=2, stage=stage,
search_tracks=_candidates(), # the July 2025 snapshot doesn't have it
spotify_search=_spotify(("Cole Phillips", "Roll of Dice"), ("Bruce Springsteen", "Roll of the Dice"),
calls=calls),
ai_judge=_ai_never,
enabled=True,
)
assert verdict.kind == KIND_COSMETIC and verdict.confident is True
assert (verdict.canonical_artist, verdict.canonical_title) == ("Cole Phillips", "Roll of Dice")
assert verdict.engine == "catalog" and "live Spotify" in verdict.reason
assert verdict.needs_ai is False
assert calls == ["Cole Phillips Roll of dice"]
@pytest.mark.asyncio
async def test_catalog_hit_never_calls_spotify():
calls = []
await judge_match(
"paramore", "big man, little dignity",
search_tracks=_candidates(("Paramore", "Big Man, Little Dignity")),
spotify_search=_spotify(calls=calls), ai_judge=_ai_never, enabled=True,
)
assert calls == []
@pytest.mark.asyncio
async def test_unconfirmed_ai_correction_is_downgraded():
"""The incident: no source has the song the AI named, so it must not be applied."""
calls = []
async def ai(artist, title, candidates, audio_tier):
return MatchVerdict(KIND_CONTENT, True, "Cole Phillips", "Roll of the Dice", engine="ai", reason="typo")
verdict = await judge_match(
"Cole Phillips", "Roll of dice",
audio_tier=2,
search_tracks=_candidates(),
spotify_search=_spotify(("Bruce Springsteen", "Roll of the Dice"), calls=calls),
ai_judge=ai, enabled=True,
)
assert verdict.kind == KIND_CONTENT and verdict.confident is False
assert verdict.canonical_title == "Roll of the Dice" # still offered as "Did you mean…?"
assert "unconfirmed" in verdict.reason
# One search for what was typed, one for the AI's song.
assert calls == ["Cole Phillips Roll of dice", "Cole Phillips Roll of the Dice"]
@pytest.mark.asyncio
async def test_ai_correction_confirmed_by_second_spotify_lookup():
async def ai(artist, title, candidates, audio_tier):
return MatchVerdict(KIND_CONTENT, True, "The Strokes", "Machu Picchu", engine="ai")
async def spotify(query):
return [{"artist_name": "The Strokes", "track_name": "Machu Picchu"}] if "Machu" in query else []
verdict = await judge_match(
"the stokes", "max picu", audio_tier=3,
search_tracks=_candidates(), spotify_search=spotify, ai_judge=ai, enabled=True,
)
assert verdict.confident is True and verdict.canonical_title == "Machu Picchu"
@pytest.mark.asyncio
async def test_grounding_accepts_any_credited_artist():
async def ai(artist, title, candidates, audio_tier):
return MatchVerdict(KIND_CONTENT, True, "Sean Paul", "Breathe", engine="ai")
async def spotify(query):
return [{"artist_name": "Blu Cantrell", "artist_names": ["Blu Cantrell", "Sean Paul"],
"track_name": "Breathe"}]
verdict = await judge_match(
"sean pual", "breath", audio_tier=3,
search_tracks=_candidates(), spotify_search=spotify, ai_judge=ai, enabled=True,
)
assert verdict.confident is True
@pytest.mark.asyncio
async def test_spotify_unavailable_or_failing_means_no_evidence():
async def ai(artist, title, candidates, audio_tier):
return MatchVerdict(KIND_CONTENT, True, "Queen", "Bohemian Rhapsody", engine="ai")
async def broken(query):
raise RuntimeError("decide down")
for spotify in (broken, None):
verdict = await judge_match(
"queen", "bohemian rapsody", audio_tier=3,
search_tracks=_candidates(), spotify_search=spotify, ai_judge=ai, enabled=True,
)
assert verdict.kind == KIND_CONTENT and verdict.confident is False
@pytest.mark.asyncio
async def test_ambiguous_and_none_ai_verdicts_are_not_grounded():
calls = []
async def ai(artist, title, candidates, audio_tier):
return MatchVerdict(KIND_AMBIGUOUS, True, artist, title,
alternatives=[{"artist": "A", "title": "T"}], engine="ai")
verdict = await judge_match(
"x", "y", search_tracks=_candidates(), spotify_search=_spotify(calls=calls), ai_judge=ai, enabled=True,
)
assert verdict.kind == KIND_AMBIGUOUS and verdict.confident is True
assert calls == ["x y"] # only the typed-song lookup
def test_merge_drops_repeats_and_blanks():
from backend.services.match_judge.service import _merge
a = [{"artist_name": "Queen", "track_name": "Radio Ga Ga"}, {"artist_name": "", "track_name": "x"}]
b = [{"artist_name": "queen", "track_name": "radio ga ga!"}, {"artist_name": "Queen", "track_name": "Innuendo"}]
assert [c["track_name"] for c in _merge(a, b)] == ["Radio Ga Ga", "Innuendo"]
@@ -0,0 +1,81 @@
"""catalog_proxy_service.spotify_search: gen -> decide's partner Spotify search."""
import httpx
import pytest
from backend.services import catalog_proxy_service as proxy
@pytest.fixture(autouse=True)
def _clean(monkeypatch):
proxy._cache.clear()
monkeypatch.setattr(proxy.settings, "decide_partner_secret", "s3cret", raising=False)
monkeypatch.setattr(proxy.settings, "karaoke_decide_api_url", "https://decide.test", raising=False)
yield
proxy._cache.clear()
def _mock(monkeypatch, handler):
real = httpx.AsyncClient
seen = []
def wrapped(request):
seen.append(request)
return handler(request)
monkeypatch.setattr(proxy.httpx, "AsyncClient",
lambda **kw: real(transport=httpx.MockTransport(wrapped), **kw))
return seen
_BODY = {"available": True, "tracks": [{"artist_name": "Cole Phillips", "track_name": "Roll of Dice"}]}
@pytest.mark.asyncio
async def test_sends_secret_and_returns_tracks(monkeypatch):
seen = _mock(monkeypatch, lambda r: httpx.Response(200, json=_BODY))
tracks = await proxy.spotify_search(" Cole Phillips Roll of dice ")
assert tracks == _BODY["tracks"]
req = seen[0]
assert req.url.path == "/api/partner/gen/spotify-search"
assert req.url.params["q"] == "Cole Phillips Roll of dice"
assert req.url.params["limit"] == "10"
assert req.headers["X-Partner-Secret"] == "s3cret"
@pytest.mark.asyncio
async def test_results_are_cached(monkeypatch):
seen = _mock(monkeypatch, lambda r: httpx.Response(200, json=_BODY))
await proxy.spotify_search("Cole Phillips Roll of dice")
await proxy.spotify_search("cole phillips roll of dice")
assert len(seen) == 1
@pytest.mark.asyncio
@pytest.mark.parametrize("response", [
httpx.Response(200, json={"available": False, "tracks": []}), # decide's breaker open
httpx.Response(401, json={"detail": "partner_auth"}),
httpx.Response(503, json={"detail": "partner_disabled"}),
httpx.Response(200, json=["not", "a", "dict"]),
])
async def test_no_evidence_is_none_and_not_cached(monkeypatch, response):
seen = _mock(monkeypatch, lambda r: response)
assert await proxy.spotify_search("some song") is None
assert await proxy.spotify_search("some song") is None
assert len(seen) == 2
@pytest.mark.asyncio
async def test_network_error_is_none(monkeypatch):
def boom(request):
raise httpx.ConnectError("down", request=request)
_mock(monkeypatch, boom)
assert await proxy.spotify_search("some song") is None
@pytest.mark.asyncio
async def test_no_secret_or_short_query_skips_the_call(monkeypatch):
seen = _mock(monkeypatch, lambda r: httpx.Response(200, json=_BODY))
assert await proxy.spotify_search("x") is None
monkeypatch.setattr(proxy.settings, "decide_partner_secret", "", raising=False)
assert await proxy.spotify_search("some song") is None
assert seen == []
+92 -3
View File
@@ -53,11 +53,18 @@ def test_ambiguous_keeps_up_to_four_alternatives():
assert v["kind"] == "ambiguous" and len(v["alternatives"]) == 4
async def _no_spotify(q):
return None
# ---- resolve_and_tidy: AI split → the job flow's judge_match catalog pass
def _tidy(query, ai, judge_result=None, judge_exc=None):
def _tidy(query, ai, judge_result=None, judge_exc=None, live=None):
seen = []
async def spotify(q):
return live
async def gen(model, system_prompt, user_prompt):
return ai
@@ -66,7 +73,7 @@ def _tidy(query, ai, judge_result=None, judge_exc=None):
if judge_exc:
raise judge_exc
return judge_result
v = asyncio.run(resolve_and_tidy(query, generate=gen, judge=judge, model="m"))
v = asyncio.run(resolve_and_tidy(query, generate=gen, judge=judge, model="m", spotify=spotify))
return v, seen
@@ -125,6 +132,87 @@ def test_no_catalog_match_keeps_ai_verdict(judge_result, judge_exc):
assert v["engine"] == "ai" and v["canonical_title"] == "Push Up on Me"
# A failed catalog pass is transient — the route must not cache it.
assert (v["reason"] == "unavailable") is (judge_exc is not None)
# Grounding: a check that ran and found nothing makes the AI answer unconfirmed;
# a check that never ran (decide down) leaves it as it was.
assert v["confident"] is (judge_exc is not None)
# ---- live Spotify (2026-11-06: "Cole Phillips – Roll of Dice" became "Roll of the Dice")
_COLE = [{"artist_name": "Cole Phillips", "track_name": "Roll of Dice"},
{"artist_name": "Bruce Springsteen", "track_name": "Roll of the Dice"}]
def test_live_spotify_exact_answers_without_gemini():
v, seen = _tidy("Roll of dice Cole Phillips", None, live=_COLE) # ai=None: Gemini must not matter
assert v["kind"] == "cosmetic" and v["confident"] is True and v["engine"] == "catalog"
assert (v["canonical_artist"], v["canonical_title"]) == ("Cole Phillips", "Roll of Dice")
assert (v["typed_artist"], v["typed_title"]) == ("Cole Phillips", "Roll of dice")
assert seen == []
def test_live_spotify_exact_artist_first():
v, _ = _tidy("cole phillips roll of dice", None, live=_COLE)
assert (v["typed_artist"], v["typed_title"]) == ("cole phillips", "roll of dice")
assert v["canonical_title"] == "Roll of Dice"
def test_unconfirmed_ai_correction_is_not_confident():
"""The incident: Gemini 'corrects' to a more familiar title no source has for this artist."""
ai = {"kind": "content", "confident": True, "typed_artist": "Cole Philips",
"typed_title": "Roll of dice", "canonical_artist": "Cole Phillips",
"canonical_title": "Roll of the Dice"}
inconclusive = MatchVerdict("none", False, "Cole Phillips", "Roll of the Dice",
engine="catalog", needs_ai=True)
v, _ = _tidy("Roll of dice Cole Philips", ai, inconclusive, live=[])
assert v["kind"] == "content" and v["confident"] is False
assert "unconfirmed" in v["reason"]
def test_ai_pick_from_live_results_is_kept():
ai = {"kind": "content", "confident": True, "typed_artist": "Cole Philips",
"typed_title": "Roll of dice", "canonical_artist": "Cole Phillips",
"canonical_title": "Roll of Dice"}
inconclusive = MatchVerdict("none", False, "Cole Phillips", "Roll of Dice",
engine="catalog", needs_ai=True)
v, _ = _tidy("Roll of dice Cole Philips", ai, inconclusive, live=_COLE)
assert v["confident"] is True and v["canonical_title"] == "Roll of Dice"
def test_live_results_are_handed_to_gemini():
prompts = []
async def gen(model, system_prompt, user_prompt):
prompts.append(user_prompt)
return {"kind": "none", "confident": False}
async def spotify(q):
return _COLE
asyncio.run(resolve_and_tidy("Roll of dice Cole Philips", generate=gen, model="m", spotify=spotify))
assert "Live Spotify search results" in prompts[0]
assert "- Cole Phillips — Roll of Dice" in prompts[0]
def test_live_spotify_failure_falls_back_to_gemini(monkeypatch):
import backend.services.match_judge.free_text as ft
monkeypatch.setattr(ft, "SPOTIFY_TIMEOUT_S", 0.05)
prompts = []
async def gen(model, system_prompt, user_prompt):
prompts.append(user_prompt)
return {"kind": "none", "confident": False}
async def slow(q):
await asyncio.sleep(1)
async def broken(q):
raise RuntimeError("decide down")
for spotify in (slow, broken):
v = asyncio.run(resolve_and_tidy("some query", generate=gen, model="m", spotify=spotify))
assert v["kind"] == "none"
assert prompts == ['Singer typed: "some query"'] * 2
def test_ambiguous_and_failures_skip_the_catalog():
@@ -208,7 +296,8 @@ def test_slow_catalog_tidy_answers_with_ai_verdict_and_keeps_searching(monkeypat
return MatchVerdict("cosmetic", True, "Céline Dion", "My Heart Will Go On", engine="catalog")
async def main():
v = await resolve_and_tidy("song from titanic", generate=gen, judge=judge, model="m")
v = await resolve_and_tidy("song from titanic", generate=gen, judge=judge, model="m",
spotify=_no_spotify)
assert finished == [] # answered before the catalog search finished
await asyncio.sleep(0.3) # ...which kept running in the background
return v
+24 -1
View File
@@ -1093,6 +1093,21 @@ the AI layer with `MATCH_JUDGE_ENABLED=false` (deterministic+catalog still run).
> is designing an on-device typo-tolerant matcher that uses Gemini only as a fallback. Read
> `kjbox/docs/SONG-IDENTIFICATION.md` before reusing this judge for free-text search.
**Live Spotify + grounding (v0.294.0).** When the catalogue can't confirm the typed song, the judge
(both stages) runs a live Spotify search through decide's partner route
(`GET /api/partner/gen/spotify-search`, `X-Partner-Secret`, via
`catalog_proxy_service.spotify_search`). The catalogue is a July 2025 snapshot, so new releases are
missing from it.
- An exact match after normalisation is a confident `catalog` verdict with reason
"… (live Spotify)" and **no Gemini call**. Example: "Cole Phillips" / "Roll of dice" → "Roll of Dice".
- Gemini also gets the Spotify results as candidates.
- **Grounding rule:** a confident AI `cosmetic` / `content` verdict must name a song that is in the
candidates or in one more Spotify lookup of the AI's song. Otherwise it comes back
`confident: false` with reason "… [unconfirmed: not in catalog or Spotify]". Clients show that as
"Did you mean…?" and never apply it.
- Spotify unavailable means no evidence, so AI corrections aren't confident. That is conservative on
purpose. Incident: kjbox 2026-11-05, job `f01c9997`, "Roll of Dice" became "Roll of the Dice".
`stage` (optional, `"fast"` | `"full"`, default `"full"`) supports a two-call
pattern the frontend uses to keep the tidy off the critical path:
@@ -3461,7 +3476,15 @@ AudioSourceStep ("Tidied to …" / "Corrected to …"). Two steps
`judge_match(stage="fast")`. A confident catalog match wins: its formatting
becomes `canonical_*`, `engine: "catalog"`, kind `cosmetic` when the query
already named that song (only casing/punctuation/word order differ), else
`content`. No catalog match → the AI verdict unchanged.
`content`. No catalog match → the AI verdict, but a confident answer that neither the
catalogue nor live Spotify confirms comes back `confident: false` (grounding rule, above).
A tidy that timed out keeps the AI verdict with reason `unavailable` (not cached).
**Step 0 (v0.294.0): live Spotify.** Before Gemini, the raw query goes to live Spotify (3 s cap).
A result whose artist + title *is* the query (either order, after normalising) is answered
directly: `cosmetic`, `engine: "catalog"`, reason `live Spotify match`. Otherwise the results go to
Gemini as candidates ("use the listed song's exact artist and title; never 'correct' to a more
familiar title").
kind `none` keeps the AI's `typed_artist`/`typed_title` split when it has one
(kjbox pre-fills its make-it form with it). kjbox calls this only when its own
+17
View File
@@ -6,6 +6,23 @@ Key insights for future AI agents working on this codebase.
---
## An AI "correction" nothing can confirm is a guess: ground it in real data (Nov 2026, v0.294.0)
At a live kjbox night a singer typed "Cole Phillips – Roll of Dice", a 2025 release missing from the
July 2025 Spotify snapshot, from MusicBrainz and from KaraokeNerds. Gemini "corrected" it to the
more familiar phrase "Roll of the Dice", and `resolve_and_tidy` shipped that as confident:
"No catalog match → AI verdict unchanged". After the singer tapped "keep what I typed", the make
wizard's full judge did it again. The job came out titled wrong (`f01c9997`) even though the audio
search had found the real track on Spotify at score 1.0.
The LLM is weakest exactly where our data is thinnest: new releases and small artists. So silence
from the catalogue is not evidence for the AI's answer. Fix: a live Spotify search (decide partner
route, client-credentials, cached, 429 circuit breaker) confirms the typed song before Gemini. Any
AI correction must name a song the catalogue or Spotify actually has, otherwise it is
`confident: false` (offered, never applied). When a real data source is cheap and fresh, ask it
before asking a model, and check what the model says against it.
## A lazy import on the loop can stall it for 15s: the image had no precompiled bytecode (Oct 2026, v0.282.1)
The hourly `process-stale-reviews` cron stalled the API loop 10–15s, 4 times in 2 days. The culprit was `internal.py:670`: a lazy `from backend.services.conversion_emails import ...` inside an `async def`. The stall stack ended in `importlib … _write_atomic`. The backend Dockerfile never ran `compileall`, so the first import on each instance compiled the module and wrote a `.pyc` on the event loop, on a CPU-throttled instance.
@@ -13,7 +13,6 @@ This module is invoked from main.py when RUNNER_MODE=ephemeral.
from __future__ import annotations
import base64
import concurrent.futures
import json
import os
@@ -174,6 +173,17 @@ try {
_compute_client: compute_v1.InstancesClient | None = None
def _log_error(message: str) -> None:
"""Log at ERROR so the error monitor (severity>=ERROR) alerts on it.
Plain ``print`` lines have no severity in Cloud Logging. Use this for
failures of the stalled-job safety net: the error monitor ignores
webhook aborts on the assumption that this net catches dropped jobs,
so the net failing must page.
"""
print(json.dumps({"severity": "ERROR", "message": message}), flush=True)
def get_compute_client() -> compute_v1.InstancesClient:
global _compute_client
if _compute_client is None:
@@ -716,7 +726,7 @@ def cleanup_orphans(pat: str) -> dict:
try:
result["redispatch"] = redispatch_stalled_jobs(pat, live_vms, runner_by_name)
except Exception as exc: # noqa: BLE001
print(f"redispatch_stalled_jobs failed: {exc!r}")
_log_error(f"redispatch_stalled_jobs failed: {exc!r}")
result["redispatch"] = {"error": str(exc)}
return result
@@ -781,7 +791,7 @@ def list_queued_self_hosted_jobs(pat: str) -> list[dict]:
if job.get("status") == "queued" and "self-hosted" in labels and can_serve(labels):
jobs.append({**job, "repo": repo})
except Exception as exc: # noqa: BLE001
print(f"Skipping {repo} in stalled-job scan: {exc!r}")
_log_error(f"Skipping {repo} in stalled-job scan: {exc!r}")
return jobs
@@ -849,7 +859,11 @@ def redispatch_stalled_jobs(
if age >= REDISPATCH_GIVE_UP_MINUTES:
# Excluded from demand too, so a job we've abandoned doesn't make
# every later stalled job in its family look under-supplied.
print(f"Not re-dispatching {job['repo']} job {job.get('id')}: queued {age:.0f}min (> give-up)")
# Fixed text (no live age) so every tick groups into one error pattern.
_log_error(
f"Not re-dispatching {job['repo']} job {job.get('id')}: "
f"queued longer than the {REDISPATCH_GIVE_UP_MINUTES}min give-up limit"
)
result["given_up"].append(job.get("id"))
continue
demand[family] = demand.get(family, 0) + 1
@@ -886,6 +900,6 @@ def redispatch_stalled_jobs(
try:
result["dispatched"].append(fut.result()["runner_name"])
except Exception as exc: # noqa: BLE001
print(f"Re-dispatch for {job['repo']} job {job.get('id')} failed: {exc!r}")
_log_error(f"Re-dispatch for {job['repo']} job {job.get('id')} failed: {exc!r}")
result["dispatch_failed"].append(job.get("id"))
return result
@@ -67,6 +67,18 @@ def _fresh_module(**env):
return ephemeral
def _error_messages(out: str) -> list[str]:
"""Messages printed as structured ERROR logs (what the error monitor sees)."""
msgs = []
for line in out.splitlines():
try:
entry = json.loads(line)
except ValueError:
continue
if isinstance(entry, dict) and entry.get("severity") == "ERROR":
msgs.append(entry["message"])
return msgs
class TestResolveFamily:
def test_gpu_label_wins(self):
ep = _fresh_module()
@@ -583,7 +595,7 @@ class TestRedispatchStalledJobs:
create.assert_not_called()
def test_given_up_job_does_not_consume_spare_capacity(self):
def test_given_up_job_does_not_consume_spare_capacity(self, capsys):
ep = _fresh_module()
vms = [("us-central1-a", _make_instance("gha-build-idle", age_minutes=10))]
runners = {"gha-build-idle": {"name": "gha-build-idle", "status": "online", "busy": False}}
@@ -592,6 +604,11 @@ class TestRedispatchStalledJobs:
create.assert_not_called()
assert result["given_up"] == [1]
# Giving up means the safety net failed: it must reach the error monitor.
errors = [m for m in _error_messages(capsys.readouterr().out) if "Not re-dispatching" in m]
assert errors
# No live queue age in the text, so repeat ticks group into one alert pattern.
assert all("give-up limit" in m and "90min" not in m for m in errors)
def test_idle_registered_runner_covers_the_stalled_job(self):
ep = _fresh_module()
@@ -656,7 +673,7 @@ class TestRedispatchStalledJobs:
assert create.call_count == 2
def test_dispatch_failure_is_recorded_and_others_continue(self):
def test_dispatch_failure_is_recorded_and_others_continue(self, capsys):
ep = _fresh_module()
jobs = [_queued_job(1, BUILD_LABELS, age_minutes=10), _queued_job(2, BUILD_LABELS, age_minutes=10)]
outcomes = iter([RuntimeError("stockout"), {"runner_name": "gha-build-ok"}])
@@ -674,6 +691,7 @@ class TestRedispatchStalledJobs:
assert result["dispatched"] == ["gha-build-ok"]
assert len(result["dispatch_failed"]) == 1
assert any("Re-dispatch for" in m and "stockout" in m for m in _error_messages(capsys.readouterr().out))
class TestFamilyOfRunnerName:
@@ -685,7 +703,7 @@ class TestFamilyOfRunnerName:
class TestListQueuedSelfHostedJobs:
def test_collects_queued_self_hosted_jobs_across_repos(self):
def test_collects_queued_self_hosted_jobs_across_repos(self, capsys):
ep = _fresh_module()
def fake_request(method, path, pat, body=None):
@@ -713,6 +731,7 @@ class TestListQueuedSelfHostedJobs:
jobs = ep.list_queued_self_hosted_jobs("ghp_test")
assert [(j["repo"], j["id"]) for j in jobs] == [("karaoke-gen", 1)]
assert any("Skipping flacfetch in stalled-job scan" in m for m in _error_messages(capsys.readouterr().out))
# Run 11 appeared under both statuses but its jobs are fetched once.
job_calls = [c for c in req.call_args_list if "/runs/11/jobs" in c.args[1]]
assert len(job_calls) == 1
@@ -736,7 +755,7 @@ class TestCleanupTriggersRedispatch:
assert live == ["gha-build-booting"]
assert result["redispatch"] == {"dispatched": []}
def test_redispatch_error_does_not_break_cleanup(self):
def test_redispatch_error_does_not_break_cleanup(self, capsys):
ep = _fresh_module()
with patch.object(ep, "_list_all_ephemeral_vms", return_value=[]), patch.object(
ep, "list_org_runners", return_value=[]
@@ -744,6 +763,7 @@ class TestCleanupTriggersRedispatch:
result = ep.cleanup_orphans("ghp_test")
assert result["redispatch"] == {"error": "github down"}
assert any("redispatch_stalled_jobs failed" in m for m in _error_messages(capsys.readouterr().out))
assert result["deleted_vms"] == []
def test_kill_switch_disables_redispatch(self):
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-gen"
version = "0.293.0"
version = "0.294.0"
description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens."
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"
@@ -558,34 +558,50 @@ class TestCaseInsensitiveMatching:
class TestRunnerManagerNoAvailableInstance:
"""github-runner-manager's Cloud Run 'no available instance' abort is a burst
during runner scale-up; the runners still dispatch (seen 09-20, 09-23, 09-27,
10-08), so it must not page."""
"""github-runner-manager's Cloud Run abort is a burst during runner scale-up; the
stalled-job tick still dispatches the runners (seen 09-20, 09-23, 09-27, 10-08,
11-04), so a webhook abort must not page. A scheduler-tick abort still alerts:
that tick is the safety net."""
MSG = (
"The request was aborted because there was no available instance. "
"Additional troubleshooting documentation can be found at: "
"https://cloud.google.com/run/docs/troubleshooting#abort-request"
)
WAITED_MSG = (
"The request was aborted after waiting too long for an available instance. "
"Additional troubleshooting documentation can be found at: "
"https://cloud.google.com/run/docs/troubleshooting#abort-request"
)
WEBHOOK = "https://us-central1-nomadkaraoke.cloudfunctions.net/github-runner-manager"
SCHEDULER = WEBHOOK + "?action=check_idle"
def test_ignored_for_runner_manager(self):
@pytest.mark.parametrize("msg", [MSG, WAITED_MSG])
def test_webhook_abort_ignored(self, msg):
from backend.services.error_monitor.known_issues import should_ignore
result = should_ignore("github-runner-manager", self.MSG)
result = should_ignore("github-runner-manager", msg, self.WEBHOOK)
assert result is not None
assert result.pattern_name == "runner_manager_no_available_instance"
def test_still_alerts_for_other_services(self):
@pytest.mark.parametrize("msg", [MSG, WAITED_MSG])
def test_scheduler_tick_abort_still_alerts(self, msg):
from backend.services.error_monitor.known_issues import should_ignore
assert should_ignore("github-runner-manager", msg, self.SCHEDULER) is None
@pytest.mark.parametrize("msg", [MSG, WAITED_MSG])
def test_no_url_still_alerts(self, msg):
from backend.services.error_monitor.known_issues import should_ignore
assert should_ignore("github-runner-manager", msg) is None
@pytest.mark.parametrize("msg", [MSG, WAITED_MSG])
def test_still_alerts_for_other_services(self, msg):
# On the customer-facing backend the same abort means dropped requests.
from backend.services.error_monitor.known_issues import should_ignore
assert should_ignore("karaoke-backend", self.MSG) is None
def test_other_runner_manager_errors_still_alert(self):
from backend.services.error_monitor.known_issues import should_ignore
assert should_ignore("github-runner-manager", "Failed to create runner VM: quota exceeded") is None
assert should_ignore("karaoke-backend", msg, "https://api.nomadkaraoke.com/github-runner-manager") is None
class TestAudioSeparationExperimentTracebacks:
"""Ad-hoc `python -c` experiments on prod job images (2026-10-31, 11-01) are not