From 21dd30969f80847f2c4e2ae4bf3a38ac2bc69ca2 Mon Sep 17 00:00:00 2001 From: claude_dev Date: Sat, 11 Jul 2026 14:48:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=5Fget=5Fmetrics=5Ffile=5Fpath=20?= =?UTF-8?q?=E4=B8=A4=E6=AD=A5=E6=9F=A5=E6=89=BE(=E7=9B=B4=E6=9F=A5task=5Fi?= =?UTF-8?q?d=E2=86=92=E5=9B=9E=E9=80=80result.task=5Fid)=E2=80=94=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E5=8D=95=E6=B5=8B=E7=9B=B4=E6=9F=A5=E4=B8=8EAPI?= =?UTF-8?q?=E7=9A=84uuid=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sanguo_api/routes.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/sanguo_api/routes.py b/sanguo_api/routes.py index 0da44c9..3ac3d91 100644 --- a/sanguo_api/routes.py +++ b/sanguo_api/routes.py @@ -329,18 +329,24 @@ def optimization_results(task_id: str): # ===== Task 4: Backtest result API extensions ===== def _get_metrics_file_path(task_id: str) -> str | None: - """Get the metrics file path. Resolves the request task_id to the result's - internal task_id first — run_cta_backtest writes {task_id}_metrics.json under - ITS OWN uuid task_id (result.task_id), which differs from the runner's task_id.""" + """Get the metrics file path. Tries the request task_id directly first, then + falls back to the result's internal task_id — run_cta_backtest writes the file + under ITS OWN uuid task_id (result.task_id), which differs from the runner's + task_id when submitted via the API.""" orch = get_orchestrator() - r = orch.get_result(task_id) - if r is None or not getattr(r, "task_id", None): + if not (hasattr(orch, 'db_path') and orch.db_path): return None - if hasattr(orch, 'db_path') and orch.db_path: - file_dir = os.path.dirname(os.path.abspath(orch.db_path)) - metrics_file = os.path.join(file_dir, f"{r.task_id}_metrics.json") - if os.path.exists(metrics_file): - return metrics_file + file_dir = os.path.dirname(os.path.abspath(orch.db_path)) + # 1. direct lookup by request task_id + direct = os.path.join(file_dir, f"{task_id}_metrics.json") + if os.path.exists(direct): + return direct + # 2. resolve via result.task_id (run_cta_backtest's own uuid) + r = orch.get_result(task_id) + if r is not None and getattr(r, "task_id", None): + resolved = os.path.join(file_dir, f"{r.task_id}_metrics.json") + if os.path.exists(resolved): + return resolved return None