diff --git a/dzen-assistant/docs/superpowers/specs/2026-08-11-llm-json-retry-design.md b/dzen-assistant/docs/superpowers/specs/2026-08-11-llm-json-retry-design.md new file mode 100644 index 0000000..b74fd52 --- /dev/null +++ b/dzen-assistant/docs/superpowers/specs/2026-08-11-llm-json-retry-design.md @@ -0,0 +1,45 @@ +# LLM JSON Retry Fix Design + +## Context + +Production video `#213` failed while generating the first of nine chapter articles. The LLM response could not be parsed as JSON and produced `Expecting ',' delimiter` at line 11, column 17. + +The worker already has a three-attempt quality rewrite loop and a prompt that explicitly asks for corrected JSON after a parse failure. However, `generate_article_payload()` calls the LLM and parses its response before entering the loop's `try` block. The resulting `RuntimeError` therefore bypasses the existing retry handler and aborts processing after the first malformed response. + +## Scope + +- Move `generate_article_payload()` into the existing `try` block in `generate_articles_with_quality_retries()`. +- Preserve the current limit of three quality rewrite attempts. +- Preserve the existing JSON repair function and retry feedback prompt. +- Add regression coverage proving that a malformed first response is retried and a valid second response succeeds. +- Deploy through the existing Forgejo `main` workflow. +- Re-run production video `#213` and verify that draft articles are created or, if all three LLM responses remain invalid, that the progress log records retries `2/3` and `3/3` before the final error. + +## Non-goals + +- Do not store raw LLM responses. +- Do not add undocumented `response_format` parameters to the Timeweb request. +- Do not refactor the article-generation pipeline or JSON repair heuristics. +- Do not change the existing frame lifecycle during reprocessing; record the frame count before and after the production retry so this pre-existing behavior remains visible. +- Do not modify unrelated passkey/authentication or admin changes already present in the worktree. + +## Control Flow + +For each quality attempt, the worker will call and parse the LLM response inside the existing exception boundary. A JSON parse `RuntimeError` matching the existing quality markers will set `feedback` and continue to the next attempt. The next prompt will include the current instruction to return one valid JSON object. A valid payload continues through the existing article validation path. + +Network retries inside `call_llm()` remain separate from quality retries: transport and server failures follow the existing network retry policy, while syntactically invalid model content follows the three-attempt quality rewrite policy. + +## Testing + +A focused stdlib `unittest` test will replace `generate_article_payload()` with a deterministic fake and patch `append_progress()` to avoid accessing the real database: + +1. First call raises `RuntimeError` containing the existing invalid-JSON marker. +2. Second call returns a valid payload. +3. `articles_from_payload()` is patched to return a sentinel successful result so the test remains focused on retry control flow. +4. The test asserts that two payload-generation calls occurred, the second call received the first error as `quality_feedback`, and article generation returned the successful result. + +Existing syntax/tests will be run without relying on the real LLM service. + +## Production Verification + +After pushing only the task files, wait for the Forgejo deployment, verify `/health`, confirm the running worker contains the deployed fix, record the current frame count for video `#213`, and requeue it. Completion requires checking the video's production status, progress log, resulting article records, and final frame count; deployment health alone is insufficient.