Fix fudai return guards and sample selection
This commit is contained in:
1 parent
0c8166d616
commit
c5b7198145
8 files changed
+285
-47
No files matched your search
@@ -318,6 +318,113 @@ class SwipeHandler:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
@track_action("escape_same_room_after_reset")
|
||||
def escape_same_room_after_reset(
|
||||
analyser: Any,
|
||||
reason: str = "same_room_after_reset",
|
||||
) -> bool:
|
||||
"""回顶后仍停在同一直播间时,返回并切到下一个直播间。"""
|
||||
try:
|
||||
if bool(getattr(analyser, "_escaping_same_room_after_reset", False)):
|
||||
return False
|
||||
setattr(analyser, "_escaping_same_room_after_reset", True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
now_ts = time.time()
|
||||
try:
|
||||
last_ts = float(
|
||||
getattr(analyser, "_same_room_escape_last_ts", 0.0) or 0.0
|
||||
)
|
||||
except Exception:
|
||||
last_ts = 0.0
|
||||
try:
|
||||
window_s = float(
|
||||
getattr(analyser, "same_room_escape_window_seconds", 12.0)
|
||||
or 12.0
|
||||
)
|
||||
except Exception:
|
||||
window_s = 12.0
|
||||
if last_ts > 0 and now_ts - last_ts < max(3.0, window_s):
|
||||
try:
|
||||
analyser._log_kv( # type: ignore[attr-defined]
|
||||
"自救",
|
||||
"回顶后仍同房兜底被速率限制,跳过本次返回+下一个直播间",
|
||||
reason=reason,
|
||||
since_last_seconds=round(now_ts - last_ts, 2),
|
||||
window_seconds=window_s,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
try:
|
||||
cur_pos = int(getattr(analyser, "room_pos", 0) or 0)
|
||||
except Exception:
|
||||
cur_pos = 0
|
||||
try:
|
||||
analyser._log_kv( # type: ignore[attr-defined]
|
||||
"自救",
|
||||
"检测到回顶后仍停留同一直播间,点击返回键并切到下一个直播间",
|
||||
reason=reason,
|
||||
current_room_pos=cur_pos,
|
||||
current_room_title=str(
|
||||
getattr(analyser, "current_room_title", "") or ""
|
||||
)[:50],
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
analyser.operation.click_back(getattr(analyser, "device_id", ""))
|
||||
except Exception:
|
||||
try:
|
||||
analyser.operation.keyevent(getattr(analyser, "device_id", ""), 4)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
delay_s = float(getattr(analyser, "BACK_KEY_CLICK_DELAY", 0.8) or 0.8)
|
||||
analyser.operation.delay(max(0.2, min(delay_s, 2.0)))
|
||||
except Exception:
|
||||
time.sleep(0.8)
|
||||
|
||||
douyin_swipe_ops.swipe_up_switch_live(analyser)
|
||||
try:
|
||||
analyser.room_pos = cur_pos + 1
|
||||
except Exception:
|
||||
analyser.room_pos = 1
|
||||
try:
|
||||
analyser._swipe_times = int(getattr(analyser, "_swipe_times", 0) or 0) + 1
|
||||
analyser._same_room_escape_last_ts = now_ts
|
||||
analyser._just_switched_room = True
|
||||
analyser._fudai_detail_first_read = False
|
||||
analyser._last_fudai_detail_tuple = None
|
||||
analyser._current_fudai_content = ""
|
||||
analyser.current_room_title = ""
|
||||
analyser.current_room_title_raw = ""
|
||||
analyser._room_title_identified_positions = set()
|
||||
analyser._screenshot_ready_for_ocr = False
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
analyser._log_kv( # type: ignore[attr-defined]
|
||||
"自救",
|
||||
"回顶后仍同房兜底完成:已返回并切到下一个直播间",
|
||||
reason=reason,
|
||||
new_room_pos=int(getattr(analyser, "room_pos", 0) or 0),
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
finally:
|
||||
try:
|
||||
setattr(analyser, "_escaping_same_room_after_reset", False)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@staticmethod # type: ignore[misc]
|
||||
def maybe_reset_after_swipes(analyser: Any) -> int:
|
||||
"""达到切换阈值后,在当前直播间页面回退到第一个直播间。
|
||||
|
||||
@@ -506,6 +506,48 @@ def record_room_title_and_pos(analyser: Any) -> bool:
|
||||
getattr(analyser, "_record_title_before_swipe_up", False)
|
||||
)
|
||||
if detect_before_swipe and current_title_norm:
|
||||
pending_after_reset = str(
|
||||
getattr(analyser, "_same_room_reset_pending_title_norm", "")
|
||||
or ""
|
||||
)
|
||||
if pending_after_reset and pending_after_reset == current_title_norm:
|
||||
did_escape = False
|
||||
try:
|
||||
analyser._log_kv( # type: ignore[attr-defined]
|
||||
"警告",
|
||||
"[警告] 回顶后仍识别到同一直播间,触发返回键+下一个直播间兜底",
|
||||
room_pos=room_pos,
|
||||
room_title=current_title_for_norm[:50],
|
||||
level="WARNING",
|
||||
reason="same_room_after_reset",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
did_escape = bool(
|
||||
douyin_swipe_handler.SwipeHandler.escape_same_room_after_reset(
|
||||
analyser,
|
||||
reason="same_room_after_reset",
|
||||
)
|
||||
)
|
||||
except Exception:
|
||||
did_escape = False
|
||||
try:
|
||||
analyser._same_room_reset_pending_title_norm = ""
|
||||
analyser._same_room_title_streak = 0
|
||||
except Exception:
|
||||
pass
|
||||
if did_escape:
|
||||
reset_consumed_swipe = True
|
||||
_emit_flow_flag(
|
||||
analyser,
|
||||
"same_room_after_reset",
|
||||
"escape_back_then_next",
|
||||
room_pos=room_pos,
|
||||
room_title=current_title_for_norm[:50],
|
||||
)
|
||||
raise StopIteration
|
||||
|
||||
last_norm = str(getattr(analyser, "_last_room_title_norm_seen", "") or "")
|
||||
streak = int(getattr(analyser, "_same_room_title_streak", 0) or 0)
|
||||
if current_title_norm == last_norm:
|
||||
@@ -537,6 +579,12 @@ def record_room_title_and_pos(analyser: Any) -> bool:
|
||||
did_reset = False
|
||||
if did_reset:
|
||||
reset_consumed_swipe = True
|
||||
try:
|
||||
analyser._same_room_reset_pending_title_norm = (
|
||||
current_title_norm
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
_emit_flow_flag(
|
||||
analyser,
|
||||
"same_room_streak",
|
||||
@@ -546,6 +594,13 @@ def record_room_title_and_pos(analyser: Any) -> bool:
|
||||
)
|
||||
# 触发后归位计数,避免同一轮重复触发
|
||||
analyser._same_room_title_streak = 0
|
||||
elif pending_after_reset and pending_after_reset != current_title_norm:
|
||||
try:
|
||||
analyser._same_room_reset_pending_title_norm = ""
|
||||
except Exception:
|
||||
pass
|
||||
except StopIteration:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -242,7 +242,19 @@ def seek_reservation_room(
|
||||
analyser._skip_record_title_during_seek = False
|
||||
except Exception:
|
||||
pass
|
||||
return True
|
||||
try:
|
||||
analyser._log_kv(
|
||||
operation_type,
|
||||
f"[{operation_type}] _seek_reservation_room:纠偏切换已执行但标题尚未复核,本次不视为回退成功",
|
||||
direction=direction_hint,
|
||||
target_pos=target_pos,
|
||||
expected_title=target_room_title,
|
||||
reason="mismatch_realign_requires_reverify",
|
||||
level="WARNING",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
return False
|
||||
except Exception:
|
||||
# 若验证流程异常,返回 False 以避免误判
|
||||
|
||||
@@ -161,7 +161,6 @@ def _read_recent_log_text(root: Path, *, max_chars: int = 120_000) -> str:
|
||||
|
||||
def _classify_long_term_sample(path: Path, log_text: str) -> str | None:
|
||||
name = path.name.lower()
|
||||
lower_log = log_text.lower()
|
||||
if any(
|
||||
token in name
|
||||
for token in (
|
||||
@@ -200,47 +199,6 @@ def _classify_long_term_sample(path: Path, log_text: str) -> str | None:
|
||||
)
|
||||
):
|
||||
return "attend_or_result_unknown"
|
||||
if any(
|
||||
token in lower_log
|
||||
for token in (
|
||||
"优惠券",
|
||||
"购物红包",
|
||||
"位置交换",
|
||||
"互换",
|
||||
"遮挡",
|
||||
"误触",
|
||||
)
|
||||
):
|
||||
return "ui_position_swap_or_overlay"
|
||||
if any(
|
||||
token in lower_log
|
||||
for token in (
|
||||
"未找到福袋",
|
||||
"漏检",
|
||||
"疑似有福袋",
|
||||
)
|
||||
):
|
||||
return "miss_or_not_found"
|
||||
if any(
|
||||
token in lower_log
|
||||
for token in (
|
||||
"detail_open_verdict",
|
||||
"detail_not_recognized",
|
||||
"detail_data_verdict",
|
||||
"详情",
|
||||
"阶段",
|
||||
)
|
||||
):
|
||||
return "detail_stage_anomaly"
|
||||
if any(
|
||||
token in lower_log
|
||||
for token in (
|
||||
"参与",
|
||||
"开奖结果",
|
||||
"未知",
|
||||
)
|
||||
):
|
||||
return "attend_or_result_unknown"
|
||||
return None
|
||||
|
||||
|
||||
@@ -253,12 +211,11 @@ def _promote_long_term_samples(
|
||||
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]] = {}
|
||||
for src in source_images:
|
||||
if not src.exists():
|
||||
continue
|
||||
category = _classify_long_term_sample(src, log_text)
|
||||
category = _classify_long_term_sample(src, "")
|
||||
if not category:
|
||||
continue
|
||||
bucket = selected.setdefault(category, [])
|
||||
|
||||
@@ -174,6 +174,44 @@ class TestSwipeHandler:
|
||||
assert len(a._room_pos_to_title_map) == 3
|
||||
assert 0 in a._room_pos_to_title_map and 2 in a._room_pos_to_title_map
|
||||
|
||||
def test_escape_same_room_after_reset_clicks_back_and_switches_next(self):
|
||||
"""回顶后仍停在同一直播间时,应返回并切到下一个直播间。"""
|
||||
from douyin.handlers.douyin_swipe_handler import SwipeHandler
|
||||
|
||||
class _A:
|
||||
pass
|
||||
|
||||
a = _A()
|
||||
a._escaping_same_room_after_reset = False
|
||||
a.room_pos = 7
|
||||
a._swipe_times = 3
|
||||
a._just_switched_room = False
|
||||
a.device_id = "MOCK"
|
||||
a._log_kv = MagicMock(return_value=None)
|
||||
a.operation = MagicMock()
|
||||
a.operation.click_back = MagicMock(return_value=None)
|
||||
a.operation.delay = MagicMock(return_value=None)
|
||||
a._current_fudai_content = "上一房福袋"
|
||||
a._last_fudai_detail_tuple = ("old",)
|
||||
a._fudai_detail_first_read = True
|
||||
a.current_room_title = "旧标题"
|
||||
a.current_room_title_raw = "旧标题"
|
||||
|
||||
with patch("douyin.handlers.douyin_swipe_ops.swipe_up_switch_live") as mock_next:
|
||||
ok = SwipeHandler.escape_same_room_after_reset(a, reason="ut")
|
||||
|
||||
assert ok is True
|
||||
a.operation.click_back.assert_called_once_with("MOCK")
|
||||
mock_next.assert_called_once_with(a)
|
||||
assert int(getattr(a, "room_pos", -1)) == 8
|
||||
assert int(getattr(a, "_swipe_times", -1)) == 4
|
||||
assert bool(getattr(a, "_just_switched_room", False)) is True
|
||||
assert a._current_fudai_content == ""
|
||||
assert a._last_fudai_detail_tuple is None
|
||||
assert a._fudai_detail_first_read is False
|
||||
assert a.current_room_title == ""
|
||||
assert a.current_room_title_raw == ""
|
||||
|
||||
|
||||
class TestSwipeOps:
|
||||
"""douyin_swipe_ops 模块测试。"""
|
||||
|
||||
@@ -112,6 +112,42 @@ def test_same_room_streak_triggers_reset_and_consumes_swipe():
|
||||
assert len(a._room_pos_to_title_map) >= 1
|
||||
|
||||
|
||||
def test_same_room_after_recent_reset_triggers_back_then_next_escape():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod
|
||||
|
||||
a = _mk_analyser()
|
||||
a.room_pos = 4
|
||||
a.current_room_title = "欧莱雅官"
|
||||
a.current_room_title_raw = "欧莱雅官"
|
||||
a._room_title_identified_positions = {4}
|
||||
a._room_pos_to_title_map = {
|
||||
4: {
|
||||
"title": "欧莱雅官",
|
||||
"title_raw": "欧莱雅官",
|
||||
"has_fudai": False,
|
||||
"fudai_content": "",
|
||||
"fudai_detail": None,
|
||||
"timestamp": 1.0,
|
||||
}
|
||||
}
|
||||
a._record_title_before_swipe_up = True
|
||||
a._last_room_title_norm_seen = "欧莱雅官"
|
||||
a._same_room_title_streak = 1
|
||||
a._same_room_reset_pending_title_norm = "欧莱雅官"
|
||||
|
||||
with patch.object(mod, "_dump_room_pos_map_to_state", return_value=None):
|
||||
with patch(
|
||||
"douyin.reservation.douyin_reservation_handler_pkg.record_title.douyin_swipe_handler.SwipeHandler.escape_same_room_after_reset",
|
||||
return_value=True,
|
||||
) as mock_escape:
|
||||
consumed = mod.record_room_title_and_pos(a)
|
||||
|
||||
assert consumed is True
|
||||
mock_escape.assert_called_once()
|
||||
assert int(getattr(a, "_same_room_title_streak", -1)) == 0
|
||||
assert str(getattr(a, "_same_room_reset_pending_title_norm", "")) == ""
|
||||
|
||||
|
||||
def test_pending_reservation_target_not_overwritten_after_failed_seek():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg import record_title as mod
|
||||
|
||||
|
||||
@@ -200,3 +200,36 @@ def test_collect_run_regression_artifacts_sample_only_skips_full_run_package(tmp
|
||||
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()
|
||||
|
||||
|
||||
def test_collect_run_regression_artifacts_does_not_promote_normal_stage_images_from_global_log(
|
||||
tmp_path, monkeypatch
|
||||
):
|
||||
from douyin.testing.regression_collector import collect_run_regression_artifacts
|
||||
|
||||
root = tmp_path
|
||||
logs = root / "logs"
|
||||
debug = root / "pic" / "debug_captures" / "2026-06-19"
|
||||
logs.mkdir(parents=True)
|
||||
debug.mkdir(parents=True)
|
||||
|
||||
(logs / "unified_2026-06-19.log").write_text(
|
||||
"DETAIL_DATA_VERDICT ok\n未找到福袋图标,退出预约直接参与\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
(debug / "20260619_px_pxstage_ov_ok_1.png").write_bytes(b"normal-stage")
|
||||
|
||||
monkeypatch.setattr(
|
||||
"douyin.testing.regression_collector.path_config.get_project_root",
|
||||
lambda: str(root),
|
||||
)
|
||||
|
||||
result = collect_run_regression_artifacts(
|
||||
run_id="normal_stage",
|
||||
started_at=0.0,
|
||||
full_run_package=False,
|
||||
max_debug_images=10,
|
||||
)
|
||||
|
||||
assert result["counts"]["long_term_samples"] == 0
|
||||
assert not (root / "artifacts" / "fudai_regression_samples" / "index.jsonl").exists()
|
||||
@@ -3,7 +3,7 @@ from __future__ import annotations
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
|
||||
def test_same_pos_title_mismatch_direction_hint_performs_realign_and_returns_true():
|
||||
def test_same_pos_title_mismatch_direction_hint_performs_realign_but_requires_reverify():
|
||||
from douyin.reservation.douyin_reservation_handler_pkg import seek_room as mod
|
||||
|
||||
class _Op:
|
||||
@@ -34,7 +34,7 @@ def test_same_pos_title_mismatch_direction_hint_performs_realign_and_returns_tru
|
||||
) as mock_down:
|
||||
ok = mod.seek_reservation_room(analyser, item)
|
||||
|
||||
assert ok is True
|
||||
assert ok is False
|
||||
mock_down.assert_called_once_with(analyser)
|
||||
assert item["_seek_start_room_pos"] == 7
|
||||
assert item["_seek_mismatch_realign_done"] is True
|
||||
Reference in new issue
Block a user