diff --git a/src/daemon/dispatcher.py b/src/daemon/dispatcher.py index b0d59a4..484be52 100644 --- a/src/daemon/dispatcher.py +++ b/src/daemon/dispatcher.py @@ -191,14 +191,51 @@ class Dispatcher: if self.counter: await self.counter.acquire(agent_id) + # [v2.7.1] Mail: spawn 前系统标 working + is_mail = project_config.get("project_id") == "_mail" if project_config else False + if is_mail: + db_path = Path(project_config["db_path"]) if project_config and "db_path" in project_config else None + if not db_path or not self._mail_auto_working(task.id, db_path): + if self.counter: + self.counter.release(agent_id) + self._record_routing(task, decision, "error", "mail working failed", _routing_db) + return {"level": level.value, "agent_id": agent_id, + "session_id": None, "status": "error", + "reason": "mail_auto_working_failed"} + # 构建 spawn message message = self._build_spawn_message(task, agent_id, project_config, mode=decision.get("mode", "")) - # Agent 完成后自动 release counter - on_complete = ( - lambda aid, _outcome: self.counter.release(aid) - ) if self.counter else None + # [v2.7.1] Mail: on_complete 增强自动标 done/failed + on_complete = None + if is_mail: + _task_id = task.id + _agent_id = agent_id + _mail_db = db_path + _must_haves = task.must_haves or "" + _counter = self.counter + _dispatcher = self + + def _mail_on_complete(aid, outcome): + # 先 release counter + if _counter: + _counter.release(aid) + # 再自动标 done/failed(幻觉门控) + if outcome in ("completed", "agent_failed"): + try: + _dispatcher._mail_auto_complete(_task_id, aid, _mail_db, _must_haves) + except Exception as e: + logger.error("Mail %s: on_complete error: %s", _task_id, e) + else: + logger.info("Mail %s: on_complete outcome=%s, skip auto-done", _task_id, outcome) + if _counter: + _counter.release(aid) + on_complete = _mail_on_complete + else: + on_complete = ( + lambda aid, _outcome: self.counter.release(aid) + ) if self.counter else None session_id = await self.spawner.spawn_full_agent( agent_id=agent_id, @@ -206,7 +243,7 @@ class Dispatcher: new_session=(level == DispatchLevel.ESCALATE), task_id=task.id, on_complete=on_complete, - use_main_session=project_config.get("project_id") == "_mail" if project_config else False, + use_main_session=is_mail, task_db_path=Path(project_config["db_path"]) if project_config and "db_path" in project_config else None, )