From 6b980451849f980626759529cc5fc9f71edea6b7 Mon Sep 17 00:00:00 2001 From: anor Date: Thu, 26 Mar 2026 03:34:35 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20polling=20fallback=20=D0=BA=D0=BE=D0=B3?= =?UTF-8?q?=D0=B4=D0=B0=20SSE=20done=20event=20=D0=BD=D0=B5=20=D0=B4=D0=BE?= =?UTF-8?q?=D1=85=D0=BE=D0=B4=D0=B8=D1=82=20=D0=B4=D0=BE=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B8=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Проблема: Claude Code Docker завершается, файлы на диске есть, но SSE event 'done' не доходит до браузера через nginx. UI зависает навечно. Решение: двойная страховка 1. JS: после завершения SSE stream поллит /api/batch-v2/status каждые 2с 2. API status: проверяет файлы на диске, если job.status ещё "uploaded" но batch_script.py уже существует → переводит в "analyzed" Co-Authored-By: Claude Opus 4.6 (1M context) --- main.py | 17 ++++++++++++++++- static/batch2.html | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 4688576..84ecbad 100644 --- a/main.py +++ b/main.py @@ -508,12 +508,27 @@ async def batch_v2_status(job_id: str, request: Request): job = batch_v2.get_job(job_id) if not job: raise HTTPException(404, "Job not found") + + # If status is still "uploaded" but files exist on disk — Claude finished + # but SSE didn't deliver the done event + if job["status"] == "uploaded" and job.get("job_dir"): + job_dir = Path(job["job_dir"]) + result_data = batch_v2._read_result_files(job_dir) + if result_data.get("script"): + job["analysis"] = { + "python_script": result_data["script"], + "summary": result_data.get("summary", {}), + } + job["status"] = "analyzed" + + analysis = job.get("analysis", {}) return { "status": job["status"], "total": job.get("total_files", 0), "processed": job.get("processed", 0), "errors": job.get("errors", [])[:20], - "script": job.get("analysis", {}).get("python_script", ""), + "script": analysis.get("python_script", ""), + "summary": analysis.get("summary", {}), } diff --git a/static/batch2.html b/static/batch2.html index 44d6a6d..a6c50dc 100644 --- a/static/batch2.html +++ b/static/batch2.html @@ -192,11 +192,37 @@ async function startAnalysis() { } } } catch(e) { - document.getElementById('analyze-spinner').style.display = 'none'; - stopTimer(); - document.getElementById('analyze-status').textContent = 'Ошибка: ' + e.message; - document.getElementById('analyze-status').style.color = 'var(--accent)'; + // Network error or stream closed — ignore, fall through to polling } + + // Fallback: if SSE ended without 'done' event, poll status + if (!document.getElementById('step-confirm').classList.contains('hidden')) return; // already shown + stopTimer(); + document.getElementById('analyze-status').textContent = 'Проверяю результат...'; + for (let i = 0; i < 10; i++) { + await new Promise(r => setTimeout(r, 2000)); + try { + const sr = await fetch('/api/batch-v2/status/' + jobId, {headers: H}); + const sd = await sr.json(); + if (sd.status === 'analyzed' && sd.script) { + document.getElementById('analyze-spinner').style.display = 'none'; + document.getElementById('analyze-status').textContent = 'Анализ завершён'; + document.getElementById('script-code').textContent = sd.script; + // Fetch summary from status if available + renderSummary(sd.summary || {}); + show('step-confirm'); + return; + } else if (sd.status === 'analysis_failed') { + document.getElementById('analyze-spinner').style.display = 'none'; + document.getElementById('analyze-status').textContent = 'Ошибка анализа'; + document.getElementById('analyze-status').style.color = 'var(--accent)'; + return; + } + } catch(e2) {} + } + document.getElementById('analyze-spinner').style.display = 'none'; + document.getElementById('analyze-status').textContent = 'Таймаут — попробуйте ещё раз'; + document.getElementById('analyze-status').style.color = 'var(--accent)'; } async function reanalyze() {