DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
refactor: shared pre-dispatch status helper + valid live-progress stage + regression test
Bingxi Zhao (Frank)c463ab838729
diff --git a/deeptutor/api/routers/knowledge.py b/deeptutor/api/routers/knowledge.pyindex f1e2f5cdf..ca9d7ea0a 100644--- a/deeptutor/api/routers/knowledge.py+++ b/deeptutor/api/routers/knowledge.py@@ -196,6 +196,31 @@ def _build_unique_task_id(task_type: str, task_key_prefix: str) -> str:return task_manager.generate_task_id(task_type, task_key)+def _mark_kb_queued_for_processing(+ manager: KnowledgeBaseManager, kb_name: str, task_id: str, message: str+) -> None:+ """Flip an existing KB to ``processing`` before its background task is dispatched.++ ``run_upload_processing_task`` only writes status once it starts running;+ without this pre-dispatch update the KB keeps reporting ``ready`` between+ the accepted upload/sync response and the task's first progress write.+ Mirrors the pre-dispatch update ``create_knowledge_base`` and reindex+ already do. ``stage`` must be a member of the frontend's+ ``LIVE_PROGRESS_STAGES`` set (web/lib/knowledge-helpers.ts).+ """+ manager.update_kb_status(+ name=kb_name,+ status="processing",+ progress={+ "stage": "starting",+ "message": message,+ "percent": 0,+ "task_id": task_id,+ "timestamp": datetime.now().isoformat(),+ },+ )++def _save_zip_archive(file: UploadFile,sanitized_filename: str,@@ -2075,16 +2100,11 @@ async def upload_files(logger.info(f"Uploading {len(uploaded_files)} files to KB '{kb_name}'")- manager.update_kb_status(- name=kb_name,- status="processing",- progress={- "stage": "processing",- "message": f"Processing {len(uploaded_files)} uploaded file(s)...",- "percent": 0,- "task_id": task_id,- "timestamp": datetime.now().isoformat(),- },+ _mark_kb_queued_for_processing(+ manager,+ kb_name,+ task_id,+ f"Processing {len(uploaded_files)} uploaded file(s)...",)background_tasks.add_task(@@ -2757,16 +2777,11 @@ async def sync_folder(kb_name: str, folder_id: str, background_tasks: Background# It is updated in run_upload_processing_task only after successful processing.# This prevents marking files as synced if processing fails (race condition fix).- manager.update_kb_status(- name=kb_name,- status="processing",- progress={- "stage": "processing",- "message": f"Syncing {len(files_to_process)} file(s) from linked folder...",- "percent": 0,- "task_id": task_id,- "timestamp": datetime.now().isoformat(),- },+ _mark_kb_queued_for_processing(+ manager,+ kb_name,+ task_id,+ f"Syncing {len(files_to_process)} file(s) from linked folder...",)# Add background task to process filesdiff --git a/tests/api/test_knowledge_router.py b/tests/api/test_knowledge_router.pyindex 0d26bf53f..196adce61 100644--- a/tests/api/test_knowledge_router.py+++ b/tests/api/test_knowledge_router.py@@ -379,6 +379,35 @@ def test_upload_ready_kb_returns_task_id(monkeypatch, tmp_path: Path) -> None:assert isinstance(body.get("task_id"), str) and body["task_id"]+def test_upload_flips_ready_kb_to_processing_before_dispatch(monkeypatch, tmp_path: Path) -> None:+ """An existing ready KB must not keep reporting ``ready`` between the+ accepted upload response and the background task's first progress write."""+ manager = _FakeKBManager(tmp_path / "knowledge_bases")+ manager.config["knowledge_bases"]["ready-kb"] = {+ "path": "ready-kb",+ "rag_provider": "llamaindex",+ "needs_reindex": False,+ "status": "ready",+ }+ monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)+ monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")++ async def _noop_upload_task(*_args, **_kwargs):+ return None++ monkeypatch.setattr(knowledge_router_module, "run_upload_processing_task", _noop_upload_task)++ with TestClient(_build_app()) as client:+ response = client.post("/api/v1/knowledge/ready-kb/upload", files=_upload_payload())++ assert response.status_code == 200+ entry = manager.config["knowledge_bases"]["ready-kb"]+ assert entry["status"] == "processing"+ # Stage must be a member of the frontend's LIVE_PROGRESS_STAGES set.+ assert entry["progress"]["stage"] == "starting"+ assert entry["progress"]["task_id"] == response.json()["task_id"]++def test_upload_task_marks_provider_failures_as_error(monkeypatch, tmp_path: Path) -> None:base_dir = tmp_path / "knowledge_bases"kb_dir = base_dir / "kb"