DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
Fix P1: Auto-advance learning stages on stage_end with retry logic, error handling, stale closure fixes
whf6195c22e867a
diff --git a/web/app/(workspace)/learning/[bookId]/page.tsx b/web/app/(workspace)/learning/[bookId]/page.tsxindex 95ffe8568..b241b23c8 100644--- a/web/app/(workspace)/learning/[bookId]/page.tsx+++ b/web/app/(workspace)/learning/[bookId]/page.tsx@@ -40,10 +40,13 @@ export default function LearningBookPage() {const params = useParams<{ bookId: string }>();const { t } = useTranslation();const [stages, setStages] = useState<StageProgress[]>([]);+ const stagesRef = useRef<StageProgress[]>([]);const [currentStage, setCurrentStage] = useState<string>("");const currentStageRef = useRef<string>("");const [connecting, setConnecting] = useState(true);const [error, setError] = useState<string | null>(null);+ const errorRef = useRef<boolean>(false);+ const activeTurnRetryRef = useRef<boolean>(false);const [toast, setToast] = useState("");const wsRef = useRef<WebSocket | null>(null);@@ -104,6 +107,11 @@ export default function LearningBookPage() {if (evt.type === "stage_start") {currentStageRef.current = evt.stage;setCurrentStage(evt.stage);+ // Clear recoverable errors on new turn+ if (activeTurnRetryRef.current) {+ setError(null);+ activeTurnRetryRef.current = false;+ }setStages(prev => {const updated = [...prev];const idx = updated.findIndex(s => s.stage === evt.stage);@@ -112,6 +120,7 @@ export default function LearningBookPage() {} else {updated.push({ stage: evt.stage, status: "active", content: "" });}+ stagesRef.current = updated;return updated;});} else if (evt.type === "content") {@@ -119,21 +128,62 @@ export default function LearningBookPage() {s.stage === currentStageRef.current ? { ...s, content: s.content + evt.content } : s));} else if (evt.type === "result" || evt.type === "stage_end") {- setStages(prev => prev.map(s =>- s.stage === currentStageRef.current ? { ...s, status: "completed" } : s- ));+ setStages(prev => {+ const updated = prev.map(s =>+ s.stage === currentStageRef.current ? { ...s, status: "completed" as const } : s+ );+ stagesRef.current = updated;+ return updated;+ });const endedStage = currentStageRef.current;currentStageRef.current = "";setCurrentStage("");if (["plan", "diagnostic_phase1", "diagnostic_phase2", "review", "module_test"].includes(endedStage)) {fetchProgressRef.current();}+ } else if (evt.type === "done") {+ // Auto-advance after turn completes, skip if terminal completed stage exists or any error occurred+ const hasCompletedTerminal = stagesRef.current.some(s => s.stage === "completed" && s.status === "completed");+ const hasError = errorRef.current || stagesRef.current.some(s => s.status === "error" || s.stage === "error");+ if (!hasCompletedTerminal && !hasError && wsRef.current?.readyState === WebSocket.OPEN) {+ const sendContinue = (retriesLeft: number) => {+ if (wsRef.current?.readyState !== WebSocket.OPEN) return;+ errorRef.current = false;+ activeTurnRetryRef.current = false;+ wsRef.current.send(JSON.stringify({+ type: "start_turn",+ session_id: params.bookId,+ capability: "guided_learning",+ content: "Continue",+ book_references: [{ book_id: params.bookId, page_ids: [] }],+ config: {},+ }));+ // If still active turn after 2s, retry (only for active turn errors)+ if (retriesLeft > 0) {+ setTimeout(() => {+ if (activeTurnRetryRef.current && retriesLeft > 0) {+ sendContinue(retriesLeft - 1);+ }+ }, 2000);+ }+ };+ setTimeout(() => sendContinue(3), 500);+ }} else if (evt.type === "error") {setError(evt.content);+ errorRef.current = true;+ // Check if this is a recoverable "active turn" error+ if (evt.content?.includes("active turn")) {+ activeTurnRetryRef.current = true;+ }setToast(t("guidedLearning.stageFailed"));- setStages(prev => prev.map(s =>- s.stage === currentStageRef.current ? { ...s, status: "error" } : s- ));+ setStages(prev => {+ const updated = prev.map(s =>+ s.stage === currentStageRef.current ? { ...s, status: "error" as const } : s+ );+ stagesRef.current = updated;+ return updated;+ });}};