fix: polling fallback когда SSE done event не доходит до клиента

Проблема: 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) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-26 03:34:35 +10:00
co-authored by Claude Opus 4.6
parent 3c9f4b8f4f
commit 6b98045184
2 changed files with 46 additions and 5 deletions
+16 -1
View File
@@ -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", {}),
}
+30 -4
View File
@@ -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() {