Skip to content

Commit 300ccc3

Browse files
authored
preserve task.lock (#764)
2 parents 04bfdfc + 36c38a2 commit 300ccc3

File tree

5 files changed

+317
-75
lines changed

5 files changed

+317
-75
lines changed

backend/app/controller/chat_controller.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,19 @@ def supplement(id: str, data: SupplementChat):
204204
@traceroot.trace()
205205
def stop(id: str):
206206
"""stop the task"""
207-
chat_logger.warning("Stopping chat session", extra={"task_id": id})
207+
chat_logger.info("=" * 80)
208+
chat_logger.info("🛑 [STOP-BUTTON] DELETE /chat/{id} request received from frontend")
209+
chat_logger.info(f"[STOP-BUTTON] project_id/task_id: {id}")
210+
chat_logger.info("=" * 80)
208211
try:
209212
task_lock = get_task_lock(id)
213+
chat_logger.info(f"[STOP-BUTTON] Task lock retrieved, task_lock.id: {task_lock.id}, task_lock.status: {task_lock.status}")
214+
chat_logger.info(f"[STOP-BUTTON] Queueing ActionStopData(Action.stop) to task_lock queue")
210215
asyncio.run(task_lock.put_queue(ActionStopData(action=Action.stop)))
211-
chat_logger.info("Chat stop signal sent", extra={"task_id": id})
216+
chat_logger.info(f"[STOP-BUTTON] ✅ ActionStopData queued successfully, this will trigger workforce.stop_gracefully()")
212217
except Exception as e:
213218
# Task lock may not exist if task is already finished or never started
214-
chat_logger.info("Task lock not found or already stopped", extra={"task_id": id, "error": str(e)})
219+
chat_logger.warning(f"[STOP-BUTTON] ⚠️ Task lock not found or already stopped, task_id: {id}, error: {str(e)}")
215220
return Response(status_code=204)
216221

217222

@@ -282,18 +287,33 @@ def remove_task(project_id: str, task_id: str):
282287
@router.post("/chat/{project_id}/skip-task", name="skip task in workforce")
283288
@traceroot.trace()
284289
def skip_task(project_id: str):
285-
"""Skip a task in the workforce"""
286-
chat_logger.info(f"Skipping task in workforce for project_id: {project_id}")
290+
"""
291+
Skip/Stop current task execution while preserving context.
292+
This endpoint is called when user clicks the Stop button.
293+
294+
Behavior:
295+
- Stops workforce gracefully
296+
- Marks task as done
297+
- Preserves conversation_history and last_task_result in task_lock
298+
- Sends 'end' event to frontend
299+
- Keeps SSE connection alive for multi-turn conversation
300+
"""
301+
chat_logger.info("=" * 80)
302+
chat_logger.info(f"🛑 [STOP-BUTTON] SKIP-TASK request received from frontend (User clicked Stop)")
303+
chat_logger.info(f"[STOP-BUTTON] project_id: {project_id}")
304+
chat_logger.info("=" * 80)
287305
task_lock = get_task_lock(project_id)
306+
chat_logger.info(f"[STOP-BUTTON] Task lock retrieved, task_lock.id: {task_lock.id}, task_lock.status: {task_lock.status}")
288307

289308
try:
290-
# Queue the skip task action
309+
# Queue the skip task action - this will preserve context for multi-turn
291310
skip_task_action = ActionSkipTaskData(project_id=project_id)
311+
chat_logger.info(f"[STOP-BUTTON] Queueing ActionSkipTaskData (preserves context, marks as done)")
292312
asyncio.run(task_lock.put_queue(skip_task_action))
293313

294-
chat_logger.info(f"Task skip request queued for project_id: {project_id}")
314+
chat_logger.info(f"[STOP-BUTTON] ✅ Skip request queued - task will stop gracefully and preserve context")
295315
return Response(status_code=201)
296316

297317
except Exception as e:
298-
chat_logger.error(f"Error skipping task for project_id: {project_id}: {e}")
318+
chat_logger.error(f"[STOP-BUTTON] ❌ Error skipping task for project_id: {project_id}: {e}")
299319
raise UserException(code.error, f"Failed to skip task: {str(e)}")

0 commit comments

Comments
 (0)