dzen.nstarikov.ru/docs/superpowers/specs/2026-08-11-timeweb-data-inspection-retry-design.md

33 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

2026-08-11 13:20:24 +03:00
# Timeweb Data Inspection Retry Design
## Context
After the malformed-JSON retry fix reached production, video `#213` passed JSON parsing and processed several chapters. Timeweb then intermittently returned HTTP 400 with `InternalError.Algo.DataInspectionFailed`. The same chapter was accepted in one run and rejected in another, so this provider response is transient in practice. The current HTTP client treats every 4xx response as final and aborts the whole multi-chapter transaction immediately.
## Design
- Recognize only HTTP 400 responses whose body contains the exact marker `DataInspectionFailed`.
- Retry those responses inside the existing `call_llm()` loop using `settings.llm_retry_attempts` and the existing `2 * attempt` backoff.
- Preserve immediate failure for every other 4xx response.
- Preserve the existing handling of transport errors and 5xx responses.
- After the configured attempts are exhausted, raise a specific runtime error that states Timeweb rejected input inspection after all attempts.
## Testing
Use isolated stdlib `unittest` mocks for `httpx.Client`, `SessionLocal`, and `time.sleep`:
1. A `DataInspectionFailed` response followed by a valid completion performs two requests, sleeps once, and returns the completion content.
2. A different HTTP 400 response performs one request, does not sleep, and raises the existing rejection error.
No real LLM, database, or network calls are used.
## Production Verification
Deploy through Forgejo, verify public health and the code inside the running worker, then reprocess video `#213`. Completion requires at least one draft article and a terminal `drafted` status; health alone is insufficient. Do not launch repeated full-video jobs if the bounded retry is exhausted.
## Non-goals
- Do not weaken or bypass Timeweb moderation.
- Do not retry arbitrary HTTP 400 responses.
- Do not change transcript text, prompts, article quality rules, or frame lifecycle.