fix(launch): back off build retries on GitHub limits, dedupe fetcher misses, no ad hoc render before first build

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shay
2026-11-30 09:11:03 -08:00
co-authored by Claude Opus 5.5
parent d907815f19
commit d4b4a8bc52
8 changed files with 147 additions and 4 deletions
+14
View File
@@ -719,6 +719,20 @@ def test_query_override_without_params_uses_cached_composite(client):
assert b"<svg>cached</svg>" in r.data
def test_query_override_skips_adhoc_render_before_first_build(client):
"""With no build landed yet the fetcher likely has no data, so an ad hoc
render would start a GitHub fetch per request. Serve the building
placeholder instead."""
dbmod.enroll("alice", {"theme": "midnight"})
with patch("src.worker.render_composite_adhoc") as adhoc:
r = client.get("/api/alice?theme=onyx&widgets=name,grade")
assert adhoc.call_count == 0
assert r.status_code == 200
assert r.headers["X-Widget-Status"] == "building"
def test_query_override_ignores_unknown_params(client):
"""Keys not in the allow-list are dropped by sanitize_settings_query so
bogus query strings don't trigger the expensive ad-hoc path."""
+50
View File
@@ -83,6 +83,56 @@ def test_worker_marks_failed_after_max_attempts(tmp_dbs):
assert row["attempts"] >= 3
def _http_503(retry_after=None):
import requests
resp = requests.Response()
resp.status_code = 503
if retry_after is not None:
resp.headers["Retry-After"] = str(retry_after)
return requests.HTTPError("503 Server Error", response=resp)
def test_worker_backs_off_on_rate_limit(tmp_dbs):
"""A fetcher 503 (GitHub quota spent) delays the retry instead of
burning all attempts in seconds; the job is not claimable until then."""
dbmod.enroll("alice", {"theme": "dark"})
with patch("src.worker.fetcher_client.get_data", side_effect=_http_503()):
worker.process_one()
assert worker.process_one() is False # backing off, nothing claimable
with dbmod._settings_conn() as c:
row = c.execute("SELECT status, attempts, updated_at FROM jobs WHERE username='alice'").fetchone()
assert row["status"] == "pending"
assert row["attempts"] == 1
assert row["updated_at"] > dbmod._now()
# Once the delay has passed the job is claimed again.
with dbmod._settings_conn() as c:
c.execute("UPDATE jobs SET updated_at='2020-01-01T00:00:00Z'")
c.commit()
assert dbmod.claim_next_job()["username"] == "alice"
def test_rate_limit_delay_grows_honors_retry_after_and_caps():
with patch("src.worker.random.uniform", return_value=1.0):
assert worker._rate_limit_delay(_http_503(), 1) == 60
assert worker._rate_limit_delay(_http_503(), 3) == 240
assert worker._rate_limit_delay(_http_503(retry_after=600), 1) == 600
assert worker._rate_limit_delay(_http_503(), 10) == worker.RATE_LIMIT_BACKOFF_CAP_S
def test_worker_rate_limit_allows_more_attempts(tmp_dbs):
dbmod.enroll("alice", {"theme": "dark"})
with patch("src.worker.fetcher_client.get_data", side_effect=_http_503()):
for _ in range(worker.RATE_LIMIT_MAX_ATTEMPTS):
with dbmod._settings_conn() as c:
c.execute("UPDATE jobs SET updated_at='2020-01-01T00:00:00Z' WHERE status='pending'")
c.commit()
worker.process_one()
with dbmod._settings_conn() as c:
row = c.execute("SELECT status, attempts FROM jobs WHERE username='alice'").fetchone()
assert row["status"] == "failed"
assert row["attempts"] == worker.RATE_LIMIT_MAX_ATTEMPTS
def test_process_one_persists_streak(tmp_dbs):
"""Phase 1 worker must compute and persist the user's streak so max survives
future settings changes."""