From 9ed73bf64b3a510dfe36b9ceadf5cb1a28fd325f Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 19 Jun 2026 02:13:42 +0800 Subject: [PATCH] Default fudai regression collection to samples --- bin/run_cli.py | 8 +- douyin/testing/regression_collector.py | 116 +++++++++++++++---------- tests/test_bin_modules.py | 2 + tests/test_regression_collector.py | 27 ++++++ 4 files changed, 106 insertions(+), 47 deletions(-) diff --git a/bin/run_cli.py b/bin/run_cli.py index df67fe5..753f36a 100644 --- a/bin/run_cli.py +++ b/bin/run_cli.py @@ -351,7 +351,12 @@ def parse_args(argv: list[str]) -> argparse.Namespace: "--regression-collection-max-images", type=int, default=500, - help="运行结束最多保留本轮 debug capture 图片数(默认500,OCR图片按比例保留)", + help="运行结束最多扫描本轮 debug capture 图片数(默认500,仅挑高价值小样本长期保留)", + ) + p.add_argument( + "--regression-full-run-package", + action="store_true", + help="额外保留完整本轮回归包(日志、状态、debug/ocr 图片);默认只保留长期小样本池", ) return p.parse_args(argv) @@ -382,6 +387,7 @@ def _collect_regression_artifacts_if_enabled( device_id=device_id, max_debug_images=max_images, max_ocr_images=max_ocr_images, + full_run_package=bool(getattr(args, "regression_full_run_package", False)), ) print( "[REGRESSION] collected output_dir={} counts={}".format( diff --git a/douyin/testing/regression_collector.py b/douyin/testing/regression_collector.py index a429b67..6669525 100644 --- a/douyin/testing/regression_collector.py +++ b/douyin/testing/regression_collector.py @@ -248,16 +248,14 @@ def _promote_long_term_samples( *, root: Path, run_id: str, - copied_debug_images: list[str], - out_dir: Path, + source_images: list[Path], + source_base: Path, max_per_category: int, ) -> dict[str, Any]: sample_root = root / "artifacts" / "fudai_regression_samples" log_text = _read_recent_log_text(root) selected: dict[str, list[Path]] = {} - debug_root = out_dir / "debug_captures" - for rel in copied_debug_images: - src = debug_root / rel + for src in source_images: if not src.exists(): continue category = _classify_long_term_sample(src, log_text) @@ -277,10 +275,14 @@ def _promote_long_term_samples( if not _copy_file(src, dst): continue rel_dst = dst.relative_to(sample_root) + try: + source_rel = src.relative_to(source_base) + except Exception: + source_rel = Path(src.name) item = { "run_id": run_id, "category": category, - "source": str(src.relative_to(out_dir)).replace("\\", "/"), + "source": str(source_rel).replace("\\", "/"), "sample": str(rel_dst).replace("\\", "/"), "note": LONG_TERM_SAMPLE_NOTES.get(category, ""), } @@ -308,53 +310,71 @@ def collect_run_regression_artifacts( max_ocr_images: int = DEFAULT_MAX_OCR_IMAGES, screenshot_tail_lines: int = DEFAULT_SCREENSHOT_CALLS_TAIL_LINES, max_long_term_samples_per_category: int = DEFAULT_MAX_LONG_TERM_SAMPLES_PER_CATEGORY, + full_run_package: bool = True, ) -> dict[str, Any]: """Collect a lightweight-but-rich local regression package after a run exits.""" root = Path(path_config.get_project_root()) safe_run_id = _safe_name(str(run_id or "run")) out_dir = root / "artifacts" / "fudai_regression_runs" / safe_run_id - out_dir.mkdir(parents=True, exist_ok=True) - - copied_logs = _copy_run_logs( - root, - out_dir, + debug_source_root = root / "pic" / "debug_captures" + debug_source_images = _iter_recent_files( + debug_source_root, since_ts=float(started_at or 0.0), - screenshot_tail_lines=screenshot_tail_lines, + suffixes=(".png", ".jpg", ".jpeg"), ) + if max_debug_images > 0 and len(debug_source_images) > max_debug_images: + debug_source_images = debug_source_images[-int(max_debug_images):] + copied_logs: list[str] = [] copied_state: list[str] = [] - for rel in ( - "douyin/core/state/room_pos_map.json", - "douyin/core/state/reservations.json", - "douyin/core/state/room_pos_map.csv", - "douyin/core/state/room_pos_map.md", - ): - src = root / rel - dst = out_dir / "state" / Path(rel).name - if _copy_file(src, dst): - copied_state.append(f"state/{dst.name}") - copied_config: list[str] = [] - if _copy_file(root / "config" / "config.json", out_dir / "config" / "config.json"): - copied_config.append("config/config.json") + debug_images: list[str] = [] + ocr_images: list[str] = [] + if full_run_package: + out_dir.mkdir(parents=True, exist_ok=True) + + copied_logs = _copy_run_logs( + root, + out_dir, + since_ts=float(started_at or 0.0), + screenshot_tail_lines=screenshot_tail_lines, + ) + + for rel in ( + "douyin/core/state/room_pos_map.json", + "douyin/core/state/reservations.json", + "douyin/core/state/room_pos_map.csv", + "douyin/core/state/room_pos_map.md", + ): + src = root / rel + dst = out_dir / "state" / Path(rel).name + if _copy_file(src, dst): + copied_state.append(f"state/{dst.name}") + + if _copy_file(root / "config" / "config.json", out_dir / "config" / "config.json"): + copied_config.append("config/config.json") + + for src in debug_source_images: + try: + rel = src.relative_to(debug_source_root) + except Exception: + rel = Path(src.name) + dst = out_dir / "debug_captures" / rel + if _copy_file(src, dst): + debug_images.append(str(rel).replace("\\", "/")) + + ocr_images = _copy_recent_images( + src_root=root / "pic" / "ocr", + dst_root=out_dir / "ocr", + since_ts=float(started_at or 0.0), + max_images=int(max_ocr_images or 0), + ) - debug_images = _copy_recent_images( - src_root=root / "pic" / "debug_captures", - dst_root=out_dir / "debug_captures", - since_ts=float(started_at or 0.0), - max_images=int(max_debug_images or 0), - ) - ocr_images = _copy_recent_images( - src_root=root / "pic" / "ocr", - dst_root=out_dir / "ocr", - since_ts=float(started_at or 0.0), - max_images=int(max_ocr_images or 0), - ) long_term_samples = _promote_long_term_samples( root=root, run_id=safe_run_id, - copied_debug_images=debug_images, - out_dir=out_dir, + source_images=debug_source_images, + source_base=debug_source_root, max_per_category=max_long_term_samples_per_category, ) @@ -364,7 +384,8 @@ def collect_run_regression_artifacts( "started_at": float(started_at or 0.0), "exit_code": exit_code, "device_id": str(device_id or ""), - "output_dir": str(out_dir), + "full_run_package": bool(full_run_package), + "output_dir": str(out_dir) if full_run_package else "", "copied": { "logs": copied_logs, "state": copied_state, @@ -381,14 +402,17 @@ def collect_run_regression_artifacts( "long_term_samples": len(long_term_samples.get("samples", [])), }, } - (out_dir / "manifest.json").write_text( - json.dumps(manifest, ensure_ascii=False, indent=2), - encoding="utf-8", - ) + manifest_path = "" + if full_run_package: + manifest_path = str(out_dir / "manifest.json") + (out_dir / "manifest.json").write_text( + json.dumps(manifest, ensure_ascii=False, indent=2), + encoding="utf-8", + ) return { "run_id": safe_run_id, - "output_dir": str(out_dir), - "manifest": str(out_dir / "manifest.json"), + "output_dir": str(out_dir) if full_run_package else "", + "manifest": manifest_path, "counts": manifest["counts"], } diff --git a/tests/test_bin_modules.py b/tests/test_bin_modules.py index 026b2b1..ad7d9fc 100644 --- a/tests/test_bin_modules.py +++ b/tests/test_bin_modules.py @@ -117,9 +117,11 @@ class TestRunCliParseArgs: '--disable-regression-collection', '--regression-collection-max-images', '120', + '--regression-full-run-package', ]) assert result.disable_regression_collection is True assert result.regression_collection_max_images == 120 + assert result.regression_full_run_package is True class TestRunCliModule: diff --git a/tests/test_regression_collector.py b/tests/test_regression_collector.py index 3b91dd1..d5582f3 100644 --- a/tests/test_regression_collector.py +++ b/tests/test_regression_collector.py @@ -173,3 +173,30 @@ def test_collect_run_regression_artifacts_promotes_long_term_error_samples(tmp_p sample_root = root / "artifacts" / "fudai_regression_samples" assert (sample_root / "index.jsonl").exists() assert len(list(sample_root.rglob("*.png"))) >= 3 + + +def test_collect_run_regression_artifacts_sample_only_skips_full_run_package(tmp_path, monkeypatch): + from douyin.testing.regression_collector import collect_run_regression_artifacts + + root = tmp_path + debug = root / "pic" / "debug_captures" / "2026-06-19" + debug.mkdir(parents=True) + (debug / "20260619_px_fdicon_false_left_coupon_x162.png").write_bytes(b"coupon") + + monkeypatch.setattr( + "douyin.testing.regression_collector.path_config.get_project_root", + lambda: str(root), + ) + + result = collect_run_regression_artifacts( + run_id="sample_only", + started_at=0.0, + full_run_package=False, + max_debug_images=10, + ) + + assert result["output_dir"] == "" + assert result["manifest"] == "" + assert result["counts"]["long_term_samples"] == 1 + assert not (root / "artifacts" / "fudai_regression_runs" / "sample_only").exists() + assert (root / "artifacts" / "fudai_regression_samples" / "index.jsonl").exists()