diff --git a/ba_auto/config.py b/ba_auto/config.py index 0b0e4b6..564f6d5 100644 --- a/ba_auto/config.py +++ b/ba_auto/config.py @@ -645,59 +645,79 @@ EVENT_FINISHED_TEXT_RECT = (1080, 585, 1760, 635) # nothing guarantees that for a future event). EVENT_QUEST_TAB = (1416, 165) -# Stage list, scrolled to its bottom extreme. Confirmed live this always -# reveals the event's LAST 5 stages at these exact row positions regardless -# of starting scroll state -- unlike story_sweep's region stage list, row -# height here stayed constant across both 1-line and 2-line wrapped titles -# during calibration, so no separate top/bottom row-position sets were -# needed for this task's stages-9-12 target range. -EVENT_STAGE_LIST_SCROLL_POINT = (1400, 700) -EVENT_STAGE_LIST_SCROLL_CLICKS = 25 -# **Recalibrated 2026-09-24**: these were calibrated against the event -# running at the time ("鉄道爆走事件"). A brand-new, unrelated event -# ("It's our Engine!", 2026.09.23 11:00-2026.10.07 10:59) replaced it and -# shifted this same bottom-scrolled row layout down by a consistent ~12-13px -# -- confirmed live via pixel-column scanning of the actual digit glyphs -# (not guessed): row centers measured at 379/550/722/893 (previously -# 366/538/710/883), a steady +12 to +13px offset across all four rows. The -# 5th slot (previously 1055) no longer lands on a stage row at all for this -# event -- only 4 stages (9-12) fit in the bottom-scrolled view now, with -# the event-points progress bar's own "4799/15000" digits sitting in -# roughly that vertical area instead; keeping a 5th slot there risked -# misreading those digits as a stage number, so it's dropped rather than -# guessed at. `_scan_stage_rows_once` loops over however many entries this -# tuple has, so 4 instead of 5 needs no other code change. Root-caused with -# `detector.read_int_bordered` itself returning None/garbage at the OLD -# positions on the live page (matching the user's own reported log -# verbatim) before re-measuring -- not assumed from the symptom alone. If a -# future event shifts this again, re-run the same pixel-column-scan -# approach (scratchpad probe, sample the actual glyph pixel extents) rather -# than guessing a new offset. -EVENT_STAGE_ROW_Y = (379, 550, 722, 893) -EVENT_STAGE_ENTER_X = 1698 - -# Stage-number OCR crop (e.g. "09", "12"), offset from a row's known center -# y -- rect = (x1, row_y - top_pad, x2, row_y + bottom_pad). Pixel-scanned -# live across all bottom-extreme row positions; tight enough to exclude the -# 3-star rating row rendered just below each number. +# Stage list: located DYNAMICALLY every scan, not from fixed row positions. # -# **Widened from (30, 5) to (20, 20) alongside the 2026-09-24 row-position -# recalibration above**: the old asymmetric pad assumed the digit glyph sat -# mostly ABOVE row_y (true for the old event, digit center measured ~8px -# above row_y); the new event's digit center sits ~16px BELOW row_y instead -# (measured band e.g. 363-395 around center 379, i.e. +-16px), so a -# symmetric pad was chosen instead of just re-flipping the asymmetry -- -# comfortably covers the ~32px-tall digit glyph with a few px of margin on -# both sides without reaching into the star-rating row (~13px further below -# the digit's own bottom edge in every measured row). -EVENT_STAGE_NUMBER_OCR_X = (1055, 1145) -EVENT_STAGE_NUMBER_OCR_Y_PAD = (20, 20) +# History: this used to be a fixed tuple of row-center y's for the list's +# bottom-scrolled extreme (EVENT_STAGE_ROW_Y), plus a fixed number-OCR rect +# offset from each (EVENT_STAGE_NUMBER_OCR_X/_Y_PAD) and a fixed click x +# (EVENT_STAGE_ENTER_X). Every new event shifted the bottom-scrolled rows by +# a different amount (the list's total content height differs per event): +# calibrated 366/538/710/883/1055 for "鉄道爆走事件" (2026-07), recalibrated +# to 379/550/722/893 for "It's our Engine!" (2026-09-24) -- and the very next +# event, a rerun of an older one ("我らオカルト研究会!", 2026-10-07), went +# right back to the ~2026-07 positions (digits measured at y 344-376 / +# 515-548 / 686-719 / 857-890 / 1029-1061), so every fixed crop missed its +# digits and all 6 outer attempts of the 2026-10-08 01:00 q4h run gave up +# with "wrong_page" while sitting on the correct, fully-rendered list +# (scratchpad/event_sweep_no_rows_debug.png). Recalibrating the tuple a +# third time would only fix the current event. +# +# Now a port of the reference's own approach (core/image.py +# swipe_search_target_str, called from activity_utils.to_mission_task_info): +# find every visible 入場 (enter) button, OCR the stage number on that +# button's own row, and scroll toward the target by comparing it against the +# smallest/largest stage number currently visible. Row positions, row count +# and scroll extent can all differ per event without any recalibration. +# +# Search area for the 入場 buttons: the whole right-hand list panel below the +# Story/Quest/Challenge tab bar (bottom edge y~208). Deliberately much wider +# than the buttons' measured x-span (1605-1780) so a horizontally-shifted +# future layout still lands inside it. +EVENT_STAGE_LIST_REGION = (1000, 212, 1910, 1195) +# 入場 button fill, sampled live from the 2026-10-07 event's list: +# (141, 228, 255) near the top edge, (122, 219, 253) mid-button. +EVENT_STAGE_ENTER_BUTTON_RGB = ((90, 185, 225), (185, 245, 255)) +# Every fully-visible button measured 175x117 (all 5 bottom-scrolled rows); +# the row half-hidden under the tab bar measured 165x45. Requiring >= 140x90 +# keeps only fully-visible rows, whose stage number is also fully visible. +EVENT_STAGE_ENTER_BUTTON_MIN_SIZE = (140, 90) +# Horizontal band, relative to a button's LEFT edge, searched for that row's +# stage-number digits (vertically: the button's own top-to-bottom span, +# which covers the number but not the cards above/below). Measured digits +# at x 1075-1120 with the button's left edge at 1605 (i.e. -530..-485); +# widened to -570..-440 so the digits don't need to sit at one exact offset. +# The number crop itself is then fitted to the dark digit glyphs found in +# that band (detector.find_glyph_bbox) -- the yellow star-rating row below +# the number never reaches the glyph darkness threshold (measured: no dark +# components other than the digits inside any button's span), so it's +# excluded without a hand-tuned pad. +EVENT_STAGE_NUMBER_BAND_DX = (-570, -440) -# 任務情報 (stage info) modal. Corner probe is pixel-identical to -# story_sweep's STAGE_MODAL_PROBE (same dark-backdrop dialog component) but -# kept as its own constant to avoid coupling the two tasks' config together. -EVENT_STAGE_MODAL_PROBE = (1870, 600) -EVENT_STAGE_MODAL_DIM_MAX_CHANNEL = 150 +# Scroll gesture for stepping through the list toward the target stage. +# Measured live (2026-10-08): one wheel click moves this list by exactly one +# row (~171px), with 5 full rows visible at once. The step must move the +# list by LESS than the visible rows so no row can be skipped between two +# scans -- 4 keeps a one-row overlap. +EVENT_STAGE_LIST_SCROLL_POINT = (1400, 700) +EVENT_STAGE_LIST_SCROLL_STEP_CLICKS = 4 + +# 任務情報 (stage info) modal -- no constant here any more. It used to be +# detected by EVENT_STAGE_MODAL_PROBE = (1870, 600) reading dark (<150 every +# channel) under the modal's dim backdrop, but that point is the EVENT'S OWN +# background art, which changes with every event: the 2026-10-07 rerun +# ("我らオカルト研究会!") has a dark night-castle background that reads +# (71, 65, 84) there with NO modal open (scratchpad/ +# event_sweep_no_rows_debug.png). The 2026-10-08 02:09 run's +# _close_stage_modal therefore believed the modal was still open on the +# plain Quest list after a successful sweep and pressed Escape/the X +# position three times, knocking the game back toward home (recovered by +# return_to_home; bounty.py hit the same art-dependence on its own screen, +# see BOUNTY_STAGE_MODAL_PROBE). event_sweep._is_stage_modal_open now reads +# the white top header bar instead -- fixed UI chrome, identical on every +# event: ~(248, 250, 252) on the plain list, dimmed to ~(80, 81, 82) by the +# modal's backdrop (measured on a real stuck event stage modal, see +# navigation._not_home) -- with navigation.is_home_nav_bar_visible ruling +# out the home screen, whose own header art is dark too. # Sweep sub-panel. Unlike story_sweep, this modal has ONE fixed layout -- # confirmed identical across both stages tested live (09 and 12), no diff --git a/ba_auto/detector.py b/ba_auto/detector.py index b8f29a6..338bc24 100644 --- a/ba_auto/detector.py +++ b/ba_auto/detector.py @@ -233,6 +233,67 @@ def find_color_centroid(region, rgb_min, rgb_max, min_pixels=1): return (x1 + int(xs.mean()), y1 + int(ys.mean())) +def find_color_blobs(region, rgb_min, rgb_max, min_size=(1, 1), image=None): + """Bounding box `(x1, y1, x2, y2)` of every connected blob of pixels + within `region` falling in the given RGB range, at least `min_size` + `(width, height)` large, sorted top-to-bottom. + + Unlike find_color_centroid (one centroid over ALL matching pixels), this + keeps several same-colored buttons apart -- built for event_sweep.py's + stage list, where every visible row has its own identical 入場 button. + The local equivalent of the reference's core/image.py + get_image_all_appear_position + merge_nearby_coordinates (every + on-screen instance of one button, deduplicated). `min_size` doubles as + the "only fully-visible instances count" filter: a button half-clipped + by a scroll viewport's edge comes out as a short blob and is dropped, + the same way a partial template fails the reference's match threshold. + """ + x1, y1, _, _ = region + mask = _color_mask(region, rgb_min, rgb_max, image=image).astype(np.uint8) + count, _, stats, _ = cv2.connectedComponentsWithStats(mask) + min_w, min_h = min_size + boxes = [] + for i in range(1, count): + x, y, w, h, _ = stats[i] + if w >= min_w and h >= min_h: + boxes.append((x1 + int(x), y1 + int(y), x1 + int(x + w), y1 + int(y + h))) + return sorted(boxes, key=lambda box: (box[1], box[0])) + + +def find_glyph_bbox(region, max_gray=110, glyph_height=(20, 60), max_glyph_width=45, image=None): + """Union bounding box `(x1, y1, x2, y2)` of the dark text glyphs inside + `region`, or None if there are none. + + A "glyph" is a connected component of near-black pixels (grayscale <= + `max_gray`) whose height falls within `glyph_height` and whose width is + at most `max_glyph_width` -- tall enough to drop small specks/outline + fragments, narrow enough to drop horizontal rules or a dark background + strip. For locating a number whose exact position inside its card isn't + reliable (event_sweep.py's stage numbers: the same card's digits sat at + different offsets in different events), so the OCR crop can be fitted + to wherever the digits actually rendered instead of a hardcoded rect. + """ + x1, y1, x2, y2 = region + img = image if image is not None else driver.read_screenshot(OCR_SHOT_PATH) + gray = cv2.cvtColor(img[y1:y2, x1:x2], cv2.COLOR_BGR2GRAY) + mask = (gray <= max_gray).astype(np.uint8) + count, _, stats, _ = cv2.connectedComponentsWithStats(mask) + min_h, max_h = glyph_height + found = None + for i in range(1, count): + x, y, w, h, _ = stats[i] + if not (min_h <= h <= max_h and w <= max_glyph_width): + continue + box = (int(x), int(y), int(x + w), int(y + h)) + if found is None: + found = box + else: + found = (min(found[0], box[0]), min(found[1], box[1]), max(found[2], box[2]), max(found[3], box[3])) + if found is None: + return None + return (x1 + found[0], y1 + found[1], x1 + found[2], y1 + found[3]) + + def _ocr_crop(region): x1, y1, x2, y2 = region img = driver.read_screenshot(OCR_SHOT_PATH) @@ -342,7 +403,7 @@ def read_int(region, psm=7): return int(digits) if digits else None -def read_int_bordered(region, psm=7, border=20): +def read_int_bordered(region, psm=7, border=20, image=None): """read_int, but with a white margin added around the upscaled crop before OCR. @@ -362,9 +423,13 @@ def read_int_bordered(region, psm=7, border=20): this one tight edge-to-edge crop shape has ever been confirmed to need it -- every other OCR read in this project already has enough natural margin. + + `image` optionally supplies an already-decoded frame (from + capture_screen()) instead of taking a fresh screenshot -- see + region_contains_color's own `image` param for why. """ x1, y1, x2, y2 = region - img = driver.read_screenshot(OCR_SHOT_PATH) + img = image if image is not None else driver.read_screenshot(OCR_SHOT_PATH) crop = img[y1:y2, x1:x2] gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) diff --git a/ba_auto/navigation.py b/ba_auto/navigation.py index 22b5263..c543856 100644 --- a/ba_auto/navigation.py +++ b/ba_auto/navigation.py @@ -117,8 +117,9 @@ HOME_NAV_BAR_MIN_CHANNEL = 220 HOME_NAV_BAR_MAX_SPREAD = 25 -def is_home_nav_bar_visible(driver): - for r, g, b in driver.colors_at(HOME_NAV_BAR_PROBES): +def is_home_nav_bar_visible(driver, image=None): + # `image`: optional already-decoded frame, same as is_header_bar_visible's. + for r, g, b in driver.colors_at(HOME_NAV_BAR_PROBES, image=image): if min(r, g, b) < HOME_NAV_BAR_MIN_CHANNEL: return False if max(r, g, b) - min(r, g, b) > HOME_NAV_BAR_MAX_SPREAD: diff --git a/ba_auto/reference_notes/mapping.md b/ba_auto/reference_notes/mapping.md index 23ad4c6..7547a3d 100644 --- a/ba_auto/reference_notes/mapping.md +++ b/ba_auto/reference_notes/mapping.md @@ -10,7 +10,7 @@ Maps each local feature to the corresponding `~/repo/baas-reference/module/...` | Stamina/AP | `module/collect_daily_task_power.py` | `to_tasks`/`implement` | `ba_auto/tasks/stamina.py` | `color.rgb_in_range` → `driver.color_at`; reference's per-tab claim loop → live UI's single "一括受取" bulk-claim button + Enter | Migrated: Mission-panel claim done (see `plan.md` Phase 8). The gem shop's own Daily Free Power flow (`module/collect_daily_free_power.py`) is a separate task now — see the "Gem shop daily free package" row below. **Daily gem reward bugfix (2026-07-16)**: reported live by the user against a real account state — the reference's own `implement` checks TWO separate button regions (the main 一括受取 area, then a second, separate "claim daily pyroxenes" button checked independently after), but only the first was ported originally. Fixed by porting the second region too: a new `MISSION_DAILY_GEM_CLAIM_BUTTON` (config.py), reusing the same bright-yellow-vs-grey color-probe style as the existing `MISSION_CLAIM_PROBE`, clicked via `driver.click` (not Enter — unlike 一括受取, this button has no keybind shown on screen) with a click-then-verify retry loop matching this project's established discipline. **Confirmed live**: gem balance went from 12,384 → 12,404 (exactly the reward's stated +20), clean return to the true home screen, no warnings. | | Gem shop daily free package | `module/collect_daily_free_power.py` | `implement`, `to_purchase_pyroxenes_menu`, `to_purchase_type`, `detect_free_power_availability`, `collect_daily_free_power`, `return_to_main_page` | `ba_auto/tasks/gem_shop.py` | Reference detects every step via `core.picture.co_detect` + fixed-region OpenCV template matching (no OCR anywhere in this reference flow). Originally ported here as plain fixed-coordinate clicks + color probes, matching this project's own established equivalent for a simple flat-color state-A/state-B difference (see `cafe.py`'s `CLAIM_DISABLED_RGB`, `stamina.py`'s `MISSION_CLAIM_PROBE`). **Rewritten 2026-09-17**: the left sidebar and the free card's status/buy-button are now located dynamically via OCR (`detector.read_lines`, new primitive) instead of fixed coordinates/colors — see the Status column for why. | Done, rewritten. Original: live-calibrated against nik-gpu 2026-07-15, genuinely claimed the account's real free package for the day (0 yen, confirmed +10 AP / +10,000 credits). Recalibrated 2026-07-29 after a UI update replaced the old 3-top-tab overlay dialog with a real full subscreen (left sidebar + デイリー/ウィークリー/マンスリー sub-tabs under 一般商品) — every coordinate/color re-verified live at that point. **Real live failure, reported by the user 2026-09-17**: `./ba_dailies.sh gem_shop` logged "free card status not recognized after click" 3/3 attempts then aborted. Root-caused (superpowers:systematic-debugging — `scratchpad/probe_gem_shop_careful_nav.py`/`probe_gem_shop_general_tab_fixed.py`, screenshots pulled back for direct visual inspection) to the sidebar gaining a new 限定商品 (NEW) entry above 一般商品 sometime after 2026-07-29, silently shifting 一般商品 from the 3rd to the 4th slot and swapping order with 定額商品 — the old fixed `GEM_SHOP_GENERAL_PRODUCTS_TAB=(196,376)` coordinate landed on 定額商品 every single attempt, reading an unrelated card's content and never matching either status color, exactly matching the reported symptom (a genuine "pressing the wrong button" bug, not a flaky click). Per explicit user request ("make the button press dynamic. Read word then click the correct position of the word"), fixed by adding `detector.read_lines()` (OCR a region, return `(x, y, text)` per detected line) and rewriting the sidebar click, the status read, and the buy-button click to all locate their target text dynamically each run instead of trusting a fixed position — calibrated live against real screenshots (`scratchpad/probe_gem_shop_ocr_regions.py`): the sidebar and the card's own text (`一日にN回まで購入可能`, `毎日無料パッケージ`, `無料`) all OCR cleanly as vertically-stacked lines in their normal (unselected) styling; a selected/highlighted sidebar item's inverted white-on-blue-pill styling was confirmed live to OCR unreliably (garbage text, not just a clean failure) even after trying an inverted threshold, so the design avoids ever needing to re-read a label the task itself just selected (checking for a still-unselected marker instead — see `_on_general_products_tab`, which checks for ウィークリー rather than whichever subtab is selected). The デイリー/ウィークリー/マンスリー subtab row was confirmed NOT the root cause (that exact fixed coordinate landed correctly every time in live testing) and was confirmed live NOT to OCR reliably as one horizontal region (tesseract fragments side-by-side columns far worse than stacked lines), so it deliberately stays a fixed click. **Confirmed live end-to-end** via a real `./ba_dailies.sh gem_shop` run post-fix: OCR-found the sidebar tab, correctly read "available", OCR-clicked the real 無料 button, and genuinely claimed the day's package — credits went from 373,773,507 to 373,783,507 (exactly +10,000) and AP rose accordingly, confirmed via before/after screenshots. Still added to `DEFAULT_ORDER`, unchanged from before — a pure free reclaim with no choice to make. | | Normal/Hard story AP sweep | `module/explore_tasks/sweep_task.py`, `module/explore_tasks/task_utils.py` | `to_region` (ported: OCR region-number readout + delta-click), a scoped-down `swipe_search_target_str` (ported: OCR stage-row label matching), `start_sweep`'s named-outcome contract (ported via `navigation.wait_for_state`, this project's scoped `co_detect` port) | `ba_auto/tasks/story_sweep.py` | OCR region/stage-name matching, ported for real (Phase 10) — replaces Phase 9's "next-region arrow stops advancing, then random stage" heuristic; modal closed via its own X button (Escape doesn't close it; X-button position re-calibrated per stage-layout variant, see Phase 10) | Done (see `plan.md` Phase 10, supersedes Phase 9; rewritten Phase 22 to match story_sweep_hard.py's Phase 21 fixes and add its own campaign guard). Config-driven exact `(region, stage, count)` targets (`config.STORY_SWEEP_TARGETS`), not latest-region/random-stage. Opt-in only, not in default flow. **2026-07-21 rewrite (Phase 22)**: per explicit user direction ("match the hard implementation on best practices"), ported story_sweep_hard.py's fixes back into this module — an explicit Normal-tab guard (`config.NORMAL_TAB`/`_open_normal_tab`, needed now that story_sweep_hard can run earlier in the same `q4h` preset and leave Hard selected), a loop-level self-heal (`_ensure_task_screen`) replacing the old per-path "Escape if on subscreen" cleanup that would have backed out of the region browser after every target (never triggered in production since this module only ever swept one rotation target per run, but a real latent bug), MAX/+ clicks no longer requiring a visible stepper raise as proof of success (same low-AP false-negative class Hard hit), `_click_plus` now resets to a known baseline via a new `SWEEP_MIN_BUTTON` before raising (closing a latent version of bounty.py's real overspend bug that this module's original `_click_plus_and_verify` never guarded against), a newly-added `_confirm_dialog_is_sweep` OCR gate against the two-stacked-button hazard (confirmed live 2026-07-21 that Normal's own tabbed modal has the identical gold 任務開始 button beneath 掃討開始 that config.py's own `SWEEP_MAX_BUTTON` comment already documented but this module never actually guarded against), the same `clicked_any`-gated `_watch_sweep_result` fix, and a `REGION_READ_RETRIES` OCR-read retry. Also added a genuinely new campaign-active guard (`config.TASK_CAMPAIGN_BADGE_RECT`, renamed from `HARD_CAMPAIGN_BADGE_RECT` once shared) — confirmed live to be pixel-identical rect/color to Hard's own banner, just reading "任務Normalで獲得できる報酬量が2倍(+100%)になっています" instead of "Hard". Confirmed live that Normal mode has no per-stage daily clear cap (no "残り回数" indicator anywhere), so Hard's gem-refill hazard does not apply here. `story_sweep_force` added as the override command. **Live-tested for real the same day**: a real MAX sweep of the day's rotation target (30-2) succeeded cleanly end-to-end on the first attempt — campaign guard passed, OCR stage-row match succeeded despite the known leading-digit misread ("20-2" read but the "2" suffix still matched), sweep confirmed and correctly detected as `"swept"` (not the old cosmetic misreport), clean return home. AP dropped from ~139 to 1 and credits rose by 13,966, confirming a real substantial MAX sweep. | -| Event sweep | `module/sweep_activity.py`, `module/activities/activity_utils.py` | `activity_sweep` (main flow), `to_activity` (nav to the event's Story/Mission/Challenge tabs), `check_sweep_availability`/`color.check_sweep_availability` (SSS gate), `start_sweep`'s named-outcome contract (shared with story_sweep's, ported the same way via `navigation.wait_for_state`) | `ba_auto/tasks/event_sweep.py`, `ba_auto/navigation.py` (`return_to_home`) | `to_activity`'s bottom-nav-icon entry -> this client's home-screen event badge (`config.EVENT_BADGE_ICON`, confirmed live to be a rotating carousel -- see Status); the reference's config-string sweep-list parsing (arbitrary stage lists, per-stage float/fraction counts via `preprocess_activity_region`/`preprocess_activity_sweep_times`) -> a single date-ordinal-modulo rotation target over a fixed 9-12 sub-range, mirroring `story_sweep.py`'s own rotation, per explicit user direction; stage-number OCR (`detector.read_int`) replaces the reference's `swipe_search_target_str` template-button search, since this client's stage list only ever needs its bottom scroll extreme for the 9-12 target range | Done, with a live-reported bug fixed (see `plan.md`'s Phase 14 follow-up). Live-calibrated against nik-gpu 2026-07-10 against the currently-running "鉄道爆走事件" event (12 stages) -- zero real AP spent during calibration (every confirm dialog reached was cancelled via Escape, verified by the AP counter). The stage-info modal is structurally identical to story_sweep's (MIN/-/+/MAX stepper, same AP-confirm dialog reusing `SWEEP_CONFIRM_*`/`SWEEP_RESULT_BUTTON_REGION`), but simpler: no region navigation, one fixed modal layout confirmed across two different stages (09 and 12), and the modal closes on Escape (story_sweep's doesn't). The reference's SSS-availability gate for a never-cleared stage was never exercised (every stage 9-12 on this account was already 3-starred) -- `event_sweep.py` handles that gate the same defensive way story_sweep handles an unavailable target: if the MAX-button count-raise can't be verified, it aborts without spending AP rather than guessing. **Bug found on a real run**: the home-screen event badge turned out to be a rotating carousel (cycles between the current event's countdown and other notices, e.g. a finished event's leftover reward-claim reminder), so a click could land on a stale event's page instead. Fixed via a new shared `navigation.return_to_home` primitive (bounded press-back-until-home loop, built generically so other tasks can reuse it, per explicit user request) plus wrong-page detection in `_find_stage_row` (zero stage-row numbers OCR'd at all -> retry via `return_to_home`, up to 3 attempts). **Second bug found on the next real run** (after the above fix correctly recovered and correctly OCR'd the target row): the row's own 入場 (enter) button click had no retry, unlike every other click-then-confirm step in this module -- missed once, aborted the whole run. Fixed via `_open_stage_modal`, the same click-then-verify-then-retry pattern already used everywhere else in the file. **Third bug found on a third real run**: `_find_stage_row`'s wrong-page detection false-positived twice (a fresh navigation's list hadn't finished rendering on the first OCR pass) before self-correcting on the 3rd attempt, wasting expensive return-home retries on what was really just a timing race -- fixed with a cheap in-place rescan before concluding "wrong page." Also, once past that, the 掃討開始 (start sweep) click turned out to be the last bare, unretried click in the file -- fixed via `_click_sweep_start_and_verify`. **Fourth round, self-driven live iteration per explicit user direction (fix/deploy/run/screenshot/diagnose in a loop, no stopping to report)**: found two more root causes and reached the first confirmed real live sweep. (1) The badge carousel's auto-rotate timer is far slower than the retry window -- a run hit "wrong page" on all 3 attempts genuinely, confirmed by screenshot; fixed by discovering and clicking the carousel's own pagination dots directly (`config.EVENT_BADGE_DOT_X`) instead of hoping the ambiguous badge shows the right item. (2) A genuine cold-start settle delay (up to ~20s for the Quest tab's stage list to populate after a fresh navigation, most likely a one-time server round-trip), not a flaky race -- confirmed via standalone probe scripts polling the real OCR pipeline once/second for a minute; fixed by widening `STAGE_ROW_SCAN_ATTEMPTS`/`_RETRY_WAIT` to match. With both fixes, a real run completed end-to-end: 200 AP spent (10x MAX sweep), credits gained, clean return home, confirmed by screenshot -- see `plan.md`'s Phase 14 follow-up #4 for the full writeup. **Fifth round, reported by the user a day later**: the exact same wrong-page-looping symptom recurred. Per the user's explicit request, added a DIRECT check for the finished event's own "イベント期間が終了しました" text (Japanese OCR, newly installed on nik-gpu since this project previously only needed digit/English reads) instead of the indirect "zero valid rows" heuristic -- `config.EVENT_FINISHED_TEXT_RECT`/`_is_finished_event_page`, live-calibrated and confirmed both ways (exact phrase match on a real finished page, no false positive on the correct page). This immediately proved the badge carousel's dot-clicking (follow-up #4's fix) is NOT reliably controllable after all -- sometimes worked, sometimes didn't, on the identical badge state -- while simply waiting longer let the carousel's own auto-rotate timer land on the correct item independently. Fixed by widening `WRONG_PAGE_RETRIES`/`_WAIT` (3x3s -> 6x12s) to give the timer real room to cycle, keeping dot-clicking as a harmless supplementary nudge. Two more real sweeps confirmed this fully working (11x MAX sweep, 219 AP spent, credits gained, clean home return, confirmed by screenshot -- three real confirmed sweeps total across this whole investigation). Also found and fixed the real root cause of the lingering `"unrecognized_state"` cosmetic bug (not a budget issue after all): `_watch_sweep_result`'s "swept" condition required the stage modal to still be open, but this event's flow can auto-return all the way to the Quest list instead, a terminal state it never anticipated -- fixed with a second `ends` condition gated on having clicked at least one result button first. Not yet re-verified live (AP exhausted by the successful sweep). See `plan.md`'s Phase 14 follow-up #5 for the full writeup. **Sixth round (2026-07-11)**: the daily rotation picked stage 9 for the first time, hitting a previously-flagged-but-never-exercised gap -- rows 366/538 ("08"/"09") consistently misread by OCR ("2" and empty/None) on every psm mode, even though the crop looked completely clean by eye; confirmed NOT a navigation/timing bug since rows 710/883/1055 ("10"/"11"/"12") read fine in the same run. Root cause (`scratchpad/probe_ocr_fix_08_09.py`): tesseract's segmentation fails on this tight edge-to-edge crop (no whitespace margin) for a leading-zero digit pair specifically -- adding a plain white border around the upscaled crop before OCR fixed both digits exactly, at every psm mode, without affecting "10". Fixed via new `detector.read_int_bordered`, now used for all `EVENT_STAGE_ROW_Y` reads. **Confirmed live**: stage 9 found, sweep executed for real (AP 233->14, credits +5,892, screenshot-confirmed safe return home) -- the originally reported bug is fixed. The same run again logged `unrecognized_state`, proving follow-up #5's `clicked_any`-gate fix addressed a real but secondary issue, not the full story. Diagnosed at zero AP cost (`scratchpad/probe_result_button_fp.py`, checks `_find_result_button` against the plain Quest list with no sweep running): the shared `SWEEP_RESULT_BUTTON_REGION` (borrowed from `story_sweep.py`) reaches into this event's own character-art panel and false-positive-matches `SWEEP_CONFIRM_CYAN` there with no dialog showing at all, confirmed on both a wrong event page and the correct one's own plain list -- so `_watch_sweep_result` kept "finding" a result button after the real one was already dismissed, and its "modal closed, no result button" end condition could never match. Fixed with a new event_sweep-only `config.EVENT_SWEEP_RESULT_BUTTON_REGION` (x narrowed to exclude the character-art panel, still comfortably covering the real buttons), confirmed live against the actual false-positive condition (now returns `None` where it previously didn't) -- not yet re-confirmed via a fresh full sweep since AP was too low that day. See `plan.md`'s Phase 14 follow-up #6. **Seventh round (2026-07-29), direct user bug report with exact log**: "when it enter wrong page it will just wait then exit on false positive instead of return home and retry" -- the log showed a single stray/partial OCR digit ('7', matching nothing real) appearing at one row position while the other 4 were still `None`, which the old `_find_stage_row` trusted as proof of a genuinely-rendered page (`stage_not_found`), a terminal outcome `run()` never retried (only `wrong_page` was retryable). Also found live while investigating (user supplied a real screenshot, `scratchpad/event_event.png`, of the actual finished-event page): `EVENT_FINISHED_TEXT_RECT`'s "authoritative" check had been silently broken since whatever event succeeded the one it was calibrated against -- re-tested live against a real capture, it read pure background-art garbage with no "終了" match at all, not just clipped text. Fixed three ways: (1) `_find_stage_row` now requires a "some rows read, target absent" conclusion to read back identically on two consecutive scans before trusting it (the all-`None` case already had this stabilize-before-trusting protection; the partial-read case didn't); (2) `EVENT_FINISHED_TEXT_RECT` re-calibrated from (1030,600,1810,750) to (1080,585,1760,635) against a fresh live capture; (3) `run()` now retries on `stage_not_found` the same as `wrong_page`. **Confirmed live immediately after deploying**: a real run landed on the finished-event page first (now correctly and immediately detected via the fixed text rect, instead of silently falling through to the fragile row-count heuristic), returned home, retried, landed on the actual current event ("出航!万魔紐", which succeeded the event this module's stage-list layout was originally calibrated against but happens to share the identical 12-stage/last-5-rows-visible template), read all 5 rows cleanly, found the target stage, and safely declined without spending AP once the MAX-count-raise couldn't be confirmed (that stage isn't sweep-eligible yet) -- the full wrong-page-to-correct-page recovery loop worked exactly as intended end to end. **Eighth round (2026-09-24), reported by the user with an exact log**: `EVENT_STAGE_ROW_Y` (366/538/710/883/1055) had gone stale again -- a brand-new, unrelated event ("It's our Engine!", 2026.09.23 11:00-2026.10.07 10:59) had replaced whatever event preceded it, and this same bottom-scrolled row layout shifted down by a consistent ~12-13px. Root-caused precisely, not guessed: `detector.read_int_bordered` at the old positions reproduced the user's exact reported failure live (`None`/`None`/`None`/garbled `4`), then a pixel-column scan of the actual digit glyphs on the live page measured real row centers at 379/550/722/893 -- a steady +12 to +13px offset across all four rows, confirmed by re-running the same OCR call at the corrected positions and getting back exactly `9`/`10`/`11`/`12`. The 5th slot (previously 1055) no longer lands on a stage row at all for this event -- only 4 stages (9-12) fit in the bottom-scrolled view now, with the event-points progress bar's own digits sitting in roughly that vertical area instead -- so it was dropped from `EVENT_STAGE_ROW_Y` rather than guessed at (the scan loop iterates over however many entries the tuple has, no other code change needed). `EVENT_STAGE_NUMBER_OCR_Y_PAD` also widened from the old asymmetric (30, 5) to a symmetric (20, 20), since the digit glyph's position relative to `row_y` flipped from mostly-above to mostly-below between the two events' layouts -- a symmetric pad tolerates either. **Confirmed live end-to-end**: a real `./ba_dailies.sh event_sweep` run's first attempt hit the same pre-existing, already-documented cold-start "wrong page" retry (unrelated to this fix -- the Quest list hadn't finished rendering yet, exactly the class of timing issue the Fourth/Fifth rounds above already built retry budget for), and the very next attempt read all 4 rows correctly, found stage 12, executed a real MAX sweep (AP 122->3, credits +3,058), and returned cleanly to home. | +| Event sweep | `module/sweep_activity.py`, `module/activities/activity_utils.py` | `activity_sweep` (main flow), `to_activity` (nav to the event's Story/Mission/Challenge tabs), `check_sweep_availability`/`color.check_sweep_availability` (SSS gate), `start_sweep`'s named-outcome contract (shared with story_sweep's, ported the same way via `navigation.wait_for_state`) | `ba_auto/tasks/event_sweep.py`, `ba_auto/navigation.py` (`return_to_home`) | `to_activity`'s bottom-nav-icon entry -> this client's home-screen event badge (`config.EVENT_BADGE_ICON`, confirmed live to be a rotating carousel -- see Status); the reference's config-string sweep-list parsing (arbitrary stage lists, per-stage float/fraction counts via `preprocess_activity_region`/`preprocess_activity_sweep_times`) -> a single date-ordinal-modulo rotation target over a fixed 9-12 sub-range, mirroring `story_sweep.py`'s own rotation, per explicit user direction; `swipe_search_target_str` (via `to_mission_task_info`) ported as `_locate_stage_rows`/`_find_stage_row` (since 2026-10-08; previously fixed bottom-extreme row positions): every 入場 button found by color blob (`detector.find_color_blobs`, local stand-in for `get_image_all_appear_position` + `merge_nearby_coordinates`), the stage number OCR'd beside each one with a crop fitted to the digit glyphs actually there (`detector.find_glyph_bbox` + `read_int_bordered`, in place of the reference's fixed `ocr_region_offsets`), then a scroll toward the target by comparing it against the min/max visible number; swipe -> mouse-wheel `driver.scroll` | Done, with a live-reported bug fixed (see `plan.md`'s Phase 14 follow-up). Live-calibrated against nik-gpu 2026-07-10 against the currently-running "鉄道爆走事件" event (12 stages) -- zero real AP spent during calibration (every confirm dialog reached was cancelled via Escape, verified by the AP counter). The stage-info modal is structurally identical to story_sweep's (MIN/-/+/MAX stepper, same AP-confirm dialog reusing `SWEEP_CONFIRM_*`/`SWEEP_RESULT_BUTTON_REGION`), but simpler: no region navigation, one fixed modal layout confirmed across two different stages (09 and 12), and the modal closes on Escape (story_sweep's doesn't). The reference's SSS-availability gate for a never-cleared stage was never exercised (every stage 9-12 on this account was already 3-starred) -- `event_sweep.py` handles that gate the same defensive way story_sweep handles an unavailable target: if the MAX-button count-raise can't be verified, it aborts without spending AP rather than guessing. **Bug found on a real run**: the home-screen event badge turned out to be a rotating carousel (cycles between the current event's countdown and other notices, e.g. a finished event's leftover reward-claim reminder), so a click could land on a stale event's page instead. Fixed via a new shared `navigation.return_to_home` primitive (bounded press-back-until-home loop, built generically so other tasks can reuse it, per explicit user request) plus wrong-page detection in `_find_stage_row` (zero stage-row numbers OCR'd at all -> retry via `return_to_home`, up to 3 attempts). **Second bug found on the next real run** (after the above fix correctly recovered and correctly OCR'd the target row): the row's own 入場 (enter) button click had no retry, unlike every other click-then-confirm step in this module -- missed once, aborted the whole run. Fixed via `_open_stage_modal`, the same click-then-verify-then-retry pattern already used everywhere else in the file. **Third bug found on a third real run**: `_find_stage_row`'s wrong-page detection false-positived twice (a fresh navigation's list hadn't finished rendering on the first OCR pass) before self-correcting on the 3rd attempt, wasting expensive return-home retries on what was really just a timing race -- fixed with a cheap in-place rescan before concluding "wrong page." Also, once past that, the 掃討開始 (start sweep) click turned out to be the last bare, unretried click in the file -- fixed via `_click_sweep_start_and_verify`. **Fourth round, self-driven live iteration per explicit user direction (fix/deploy/run/screenshot/diagnose in a loop, no stopping to report)**: found two more root causes and reached the first confirmed real live sweep. (1) The badge carousel's auto-rotate timer is far slower than the retry window -- a run hit "wrong page" on all 3 attempts genuinely, confirmed by screenshot; fixed by discovering and clicking the carousel's own pagination dots directly (`config.EVENT_BADGE_DOT_X`) instead of hoping the ambiguous badge shows the right item. (2) A genuine cold-start settle delay (up to ~20s for the Quest tab's stage list to populate after a fresh navigation, most likely a one-time server round-trip), not a flaky race -- confirmed via standalone probe scripts polling the real OCR pipeline once/second for a minute; fixed by widening `STAGE_ROW_SCAN_ATTEMPTS`/`_RETRY_WAIT` to match. With both fixes, a real run completed end-to-end: 200 AP spent (10x MAX sweep), credits gained, clean return home, confirmed by screenshot -- see `plan.md`'s Phase 14 follow-up #4 for the full writeup. **Fifth round, reported by the user a day later**: the exact same wrong-page-looping symptom recurred. Per the user's explicit request, added a DIRECT check for the finished event's own "イベント期間が終了しました" text (Japanese OCR, newly installed on nik-gpu since this project previously only needed digit/English reads) instead of the indirect "zero valid rows" heuristic -- `config.EVENT_FINISHED_TEXT_RECT`/`_is_finished_event_page`, live-calibrated and confirmed both ways (exact phrase match on a real finished page, no false positive on the correct page). This immediately proved the badge carousel's dot-clicking (follow-up #4's fix) is NOT reliably controllable after all -- sometimes worked, sometimes didn't, on the identical badge state -- while simply waiting longer let the carousel's own auto-rotate timer land on the correct item independently. Fixed by widening `WRONG_PAGE_RETRIES`/`_WAIT` (3x3s -> 6x12s) to give the timer real room to cycle, keeping dot-clicking as a harmless supplementary nudge. Two more real sweeps confirmed this fully working (11x MAX sweep, 219 AP spent, credits gained, clean home return, confirmed by screenshot -- three real confirmed sweeps total across this whole investigation). Also found and fixed the real root cause of the lingering `"unrecognized_state"` cosmetic bug (not a budget issue after all): `_watch_sweep_result`'s "swept" condition required the stage modal to still be open, but this event's flow can auto-return all the way to the Quest list instead, a terminal state it never anticipated -- fixed with a second `ends` condition gated on having clicked at least one result button first. Not yet re-verified live (AP exhausted by the successful sweep). See `plan.md`'s Phase 14 follow-up #5 for the full writeup. **Sixth round (2026-07-11)**: the daily rotation picked stage 9 for the first time, hitting a previously-flagged-but-never-exercised gap -- rows 366/538 ("08"/"09") consistently misread by OCR ("2" and empty/None) on every psm mode, even though the crop looked completely clean by eye; confirmed NOT a navigation/timing bug since rows 710/883/1055 ("10"/"11"/"12") read fine in the same run. Root cause (`scratchpad/probe_ocr_fix_08_09.py`): tesseract's segmentation fails on this tight edge-to-edge crop (no whitespace margin) for a leading-zero digit pair specifically -- adding a plain white border around the upscaled crop before OCR fixed both digits exactly, at every psm mode, without affecting "10". Fixed via new `detector.read_int_bordered`, now used for all `EVENT_STAGE_ROW_Y` reads. **Confirmed live**: stage 9 found, sweep executed for real (AP 233->14, credits +5,892, screenshot-confirmed safe return home) -- the originally reported bug is fixed. The same run again logged `unrecognized_state`, proving follow-up #5's `clicked_any`-gate fix addressed a real but secondary issue, not the full story. Diagnosed at zero AP cost (`scratchpad/probe_result_button_fp.py`, checks `_find_result_button` against the plain Quest list with no sweep running): the shared `SWEEP_RESULT_BUTTON_REGION` (borrowed from `story_sweep.py`) reaches into this event's own character-art panel and false-positive-matches `SWEEP_CONFIRM_CYAN` there with no dialog showing at all, confirmed on both a wrong event page and the correct one's own plain list -- so `_watch_sweep_result` kept "finding" a result button after the real one was already dismissed, and its "modal closed, no result button" end condition could never match. Fixed with a new event_sweep-only `config.EVENT_SWEEP_RESULT_BUTTON_REGION` (x narrowed to exclude the character-art panel, still comfortably covering the real buttons), confirmed live against the actual false-positive condition (now returns `None` where it previously didn't) -- not yet re-confirmed via a fresh full sweep since AP was too low that day. See `plan.md`'s Phase 14 follow-up #6. **Seventh round (2026-07-29), direct user bug report with exact log**: "when it enter wrong page it will just wait then exit on false positive instead of return home and retry" -- the log showed a single stray/partial OCR digit ('7', matching nothing real) appearing at one row position while the other 4 were still `None`, which the old `_find_stage_row` trusted as proof of a genuinely-rendered page (`stage_not_found`), a terminal outcome `run()` never retried (only `wrong_page` was retryable). Also found live while investigating (user supplied a real screenshot, `scratchpad/event_event.png`, of the actual finished-event page): `EVENT_FINISHED_TEXT_RECT`'s "authoritative" check had been silently broken since whatever event succeeded the one it was calibrated against -- re-tested live against a real capture, it read pure background-art garbage with no "終了" match at all, not just clipped text. Fixed three ways: (1) `_find_stage_row` now requires a "some rows read, target absent" conclusion to read back identically on two consecutive scans before trusting it (the all-`None` case already had this stabilize-before-trusting protection; the partial-read case didn't); (2) `EVENT_FINISHED_TEXT_RECT` re-calibrated from (1030,600,1810,750) to (1080,585,1760,635) against a fresh live capture; (3) `run()` now retries on `stage_not_found` the same as `wrong_page`. **Confirmed live immediately after deploying**: a real run landed on the finished-event page first (now correctly and immediately detected via the fixed text rect, instead of silently falling through to the fragile row-count heuristic), returned home, retried, landed on the actual current event ("出航!万魔紐", which succeeded the event this module's stage-list layout was originally calibrated against but happens to share the identical 12-stage/last-5-rows-visible template), read all 5 rows cleanly, found the target stage, and safely declined without spending AP once the MAX-count-raise couldn't be confirmed (that stage isn't sweep-eligible yet) -- the full wrong-page-to-correct-page recovery loop worked exactly as intended end to end. **Eighth round (2026-09-24), reported by the user with an exact log**: `EVENT_STAGE_ROW_Y` (366/538/710/883/1055) had gone stale again -- a brand-new, unrelated event ("It's our Engine!", 2026.09.23 11:00-2026.10.07 10:59) had replaced whatever event preceded it, and this same bottom-scrolled row layout shifted down by a consistent ~12-13px. Root-caused precisely, not guessed: `detector.read_int_bordered` at the old positions reproduced the user's exact reported failure live (`None`/`None`/`None`/garbled `4`), then a pixel-column scan of the actual digit glyphs on the live page measured real row centers at 379/550/722/893 -- a steady +12 to +13px offset across all four rows, confirmed by re-running the same OCR call at the corrected positions and getting back exactly `9`/`10`/`11`/`12`. The 5th slot (previously 1055) no longer lands on a stage row at all for this event -- only 4 stages (9-12) fit in the bottom-scrolled view now, with the event-points progress bar's own digits sitting in roughly that vertical area instead -- so it was dropped from `EVENT_STAGE_ROW_Y` rather than guessed at (the scan loop iterates over however many entries the tuple has, no other code change needed). `EVENT_STAGE_NUMBER_OCR_Y_PAD` also widened from the old asymmetric (30, 5) to a symmetric (20, 20), since the digit glyph's position relative to `row_y` flipped from mostly-above to mostly-below between the two events' layouts -- a symmetric pad tolerates either. **Confirmed live end-to-end**: a real `./ba_dailies.sh event_sweep` run's first attempt hit the same pre-existing, already-documented cold-start "wrong page" retry (unrelated to this fix -- the Quest list hadn't finished rendering yet, exactly the class of timing issue the Fourth/Fifth rounds above already built retry budget for), and the very next attempt read all 4 rows correctly, found stage 12, executed a real MAX sweep (AP 122->3, credits +3,058), and returned cleanly to home. **Ninth round (2026-10-08), user report**: the eighth round's recalibration broke the very next event, a rerun of an older one ("我らオカルト研究会!", 2026.10.07-10.21) that renders its bottom-scrolled rows at the ORIGINAL 2026-07 positions again. The 01:00 q4h run spent all 6 outer attempts on `wrong_page` while sitting on the correct, fully-rendered list (its own saved `event_sweep_no_rows_debug.png` shows rows 08-12). Replaced the fixed `EVENT_STAGE_ROW_Y`/`EVENT_STAGE_NUMBER_OCR_*`/`EVENT_STAGE_ENTER_X` with the dynamic `swipe_search_target_str` port described under Backend replacements: no row position, row count or digit offset is hardcoded any more. Also: a scan only counts if it reads 2+ strictly-ascending numbers (a saved home-screen capture showed its お仕事 button is a big cyan blob with a stray glyph beside it that OCRs as "4"), and a target past the list's end ends as `stage_not_in_event`, not retried. **Confirmed live at zero AP**: the failing screenshot now reads 8-12 correctly; from the top, middle and bottom of the list, targets 1/5/9/12 were all found by scrolling; target 13 came back as past the end after one scroll that didn't move the list. One wheel click = exactly one row (~171px), so the scroll step is 4 clicks (one-row overlap). First real q4h run on it: stage 10 found on attempt 2 (attempt 1 hit the finished event's page, correctly detected) and swept for real (AP 240 -> 10, `result: swept`). That run also exposed `EVENT_STAGE_MODAL_PROBE` (1870,600) as event-art-dependent too (this event's dark background read as "modal open" on the plain list, so `_close_stage_modal` pressed Escape/X three times after the sweep) -- replaced by a header-bar dimming check plus a home-screen exclusion, live-confirmed at zero AP across home / plain list / real stage modal / after close. | | Circle (Group/Club) daily check-in | `module/group.py` | `implement`, `to_group` | `ba_auto/tasks/circle.py` | Reference's `picture.co_detect` polling against fixed screen positions + `rgb_possible`/`img_possible` template states → this client's own 2-click nav (home → bottom-nav ソーシャル icon → サークル card) verified with the existing shared `navigation.is_modal_open`/`is_on_subscreen` (no new probe needed — pixel-confirmed both real "reached the circle screen" states, with or without the reward modal, satisfy that combined check) | Done. Live-calibrated 2026-07-15 on nik-gpu by driving the real flow end-to-end via raw xdotool/scrot — this **genuinely claimed the account's real circle check-in reward for the day** (+10 AP, confirmed via the real reward dialog "今日のサークルへの参加報酬... AP x10... 報酬はメールボックスから受け取ることができます"), which calibrated both real states from real data: the first-entry reward modal, and (by re-entering immediately after) the already-checked-in straight-to-chat state with no modal. Also live-confirmed a real `XIGNCODE` anti-cheat overlay stole a `BACK_BUTTON`-coordinate click mid-calibration (same recurring gotcha documented elsewhere in this project) — recovered via the standard `windowactivate`+`windowraise` escalation. Per explicit user direction, the task presses Escape directly to return home (confirmed live: a single Escape from the サークル screen returns straight to true home, skipping back through the intermediate ソーシャル hub page) rather than clicking `navigation.BACK_BUTTON`. The reference's `group_join-club` ("not in a circle") outcome is deliberately not ported — this account is already a member, and the task's scope (per explicit user direction) is entry only, no mailbox claim. The actual `circle.py` module was then live-tested for real via `./ba_dailies.sh circle`, correctly reading the "already checked in today" state left over from calibration and returning cleanly home. **Not yet live-tested**: the "first entry → claim reward" code path itself, since the account was already checked in for today by the time the module existed — same disclosed gap shape as gem_shop's own "available → claim" path. Added to `DEFAULT_ORDER` alongside mailbox/cafe/stamina/gem_shop, since it's a pure free reclaim with no decision to make. **A `reference-parity-reviewer` pass the same day caught and fixed three real gaps**: a too-generic terminal-state check that could false-positive "already checked in" from a stuck non-home starting state (fixed via `navigation.return_to_home(driver)` at the top of `run()`, matching arena.py/bounty.py precedent); an unverified reward-dismiss Enter press and closing Escape (fixed via `_dismiss_reward`/`_leave_circle`, bounded retry-until-verified loops directly porting `gem_shop.py`'s `_claim_free_package`/`_close_gem_shop`); and a missing `driver.focus_game()` escalation in the entry retry loop for the `XIGNCODE` overlay (which had already struck live during this task's own calibration) — fixed to match `navigation.click_back`'s own escalation convention. Re-confirmed live via `./ba_dailies.sh circle` after all three fixes. See `plan.md`'s Phase 17 for the full writeup. | | Bounty | `module/rewarded_task.py` | `implement` (main flow, `get_task_count`/`purchase_bounty_ticket`/per-area `rewarded_task_status` loop, not ported -- see Status), `to_bounty`/`to_choose_bounty` (nav), `get_los`/`one_detect`/`bounty_common_operation` (per-row SSS-color scan + sweep) | `ba_auto/tasks/bounty.py` | `to_bounty`'s bottom-nav "bus" icon -> this client's Work-hub `指名手配` card (`config.BOUNTY_CARD`), landing directly on Location Select with no separate bus-icon sub-navigation step; `get_los`/`one_detect`'s per-row `color.check_sweep_availability` SSS-color scan across however many rows are visible -> a fixed bottom-most row click (`config.BOUNTY_LATEST_STAGE_ROW_Y`), since this client's 3 areas each have exactly 10 stages (confirmed live, scrolling past the 10th is a no-op) all already SSS-cleared, making "scroll to the bottom extreme, click the last row" equivalent to "the latest stage available" by construction; the reference's config-string per-area sweep-count list (`rewarded_task_times`, `get_task_count`) and its loop across all 3 areas -> a single date-ordinal-modulo rotation choosing ONE area per run, mirroring story_sweep.py/event_sweep.py's own rotation, per explicit user direction (2026-07-11); `purchase_bounty_ticket` (buying more tickets with real currency) -> not ported, matching plan.md's "OCR optional for coin balance/refresh logic, can skip for first version" | Done, live-tested for real -- see `plan.md` Phase 15. Live-calibrated against nik-gpu 2026-07-11 with **zero real tickets spent** during calibration (every ticket-usage confirm dialog reached was cancelled via Escape, verified by the ticket counter (6/6) unchanged before/after, across all 3 areas). All 3 areas (ハイウェイ/砂漠の線路/校舎, matching the reference's OVERPASS/DESSERT RAILWAY/CLASSROOM groupings) confirmed to share an identical layout: same area-row/stage-row/modal-button coordinates, same 10-stage-per-area structure. The 任務情報 modal's MAX stepper, minus-button raised-color check, and ticket-usage confirm dialog are pixel-identical to event_sweep.py's own stage modal / shared `SWEEP_CONFIRM_*` dialog -- confirmed live -- and reused directly. The modal-open probe could NOT be reused from `EVENT_STAGE_MODAL_PROBE`: that corner point reads dark on this screen regardless of modal state (different background art), so a fresh `BOUNTY_STAGE_MODAL_PROBE` was calibrated that does discriminate both states. **2 real sweeps confirmed live** (credits +180,000 then +36,000, clean automatic return home both times), which surfaced and fixed two real bugs: (1) `_set_sweep_count`'s count==1 path wrongly assumed the modal defaults to count=1 on open -- it actually remembers the last-used count -- causing an unintended 5-ticket spend instead of the intended 1; fixed via a new `_click_min_and_verify` that always forces a known baseline first. (2) `BOUNTY_SWEEP_RESULT_BUTTON_REGION`'s first-guess copy of event_sweep's own region overlapped `BOUNTY_SWEEP_START_BUTTON`'s real cyan pixels, causing `_find_result_button` to re-click it once tickets hit 0 -- which surfaced a real Pyroxene ticket-purchase prompt (no gem actually spent, confirmed by an unchanged balance, but a real near-miss). Fixed three ways: the region was corrected to the real pixel-scanned OK-button bbox; a new `min_pixels` parameter on `detector.find_color_centroid` (plus `config.BOUNTY_RESULT_BUTTON_MIN_PIXELS`) filters out a second, smaller contamination source (stray cyan-range pixels in the modal's own reward-icon artwork); and `_watch_sweep_result` now has an explicit `_is_ticket_purchase_prompt` guard as a named `ends` condition. **Not yet re-confirmed live**: a fresh sweep with the fixes deployed (account at 0/6 tickets as of session end) and any bulk/MAX-count sweep's result-screen flow (only count=1 was ever tested; a bulk sweep may show a SKIP-then-OK sequence like story_sweep/event_sweep instead of the single-OK dialog confirmed here). **Real hazard found and fixed via a real unattended `daily` cron run (2026-07-17)**: reported live by the user -- a real "Battle Complete" screen (a live ~3-minute combat timer) was found on the account instead of an instant sweep, with `_watch_sweep_result` having reported "swept" cleanly and no warnings anywhere in the log; every task that ran for the rest of that `daily` invocation failed to open its own screen. Manually reproducing the flow live (reaching the real confirm dialog and cancelling before confirming, the same safe-calibration pattern the original live-testing used) found the actual hazard: the 任務情報 modal has TWO separate action buttons stacked vertically -- the intended cyan 掃討開始 (start sweep, instant) and a separate gold 任務開始 (start mission, a REAL manual battle) directly below it, both showing an identical ticket-cost preview. Every check along the commit path (`_count_raised_above_one`/`_is_sweep_usage_confirm`/`_watch_sweep_result`'s own "swept" conditions) is a generic color/position probe with no verification of what's actually showing. Fixed with `_confirm_dialog_is_sweep`, an OCR text check (`config.BOUNTY_SWEEP_CONFIRM_TEXT_RECT`, captured live from the real dialog: "指名手配チケットをN使用して、掃討をN回行いますか?") gating the one irreversible click in this flow -- cancels rather than confirms if the dialog doesn't read as a sweep. The exact mechanism that let a real run reach 任務開始 instead of 掃討開始 was not fully reproduced live (would mean deliberately repeating a real battle); the fix closes the hazard regardless of the exact upstream cause. Not yet re-verified live against a fresh real sweep. **Expanded from 3 areas to 5 (2026-09-24)**, per explicit user request once the game added two new areas, 道路 (Road) and 屋敷 (Mansion). Config-only change (`config.BOUNTY_AREA_NAMES`/`BOUNTY_AREA_ROW_Y` extended to 5 entries) -- `bounty.py`'s own logic was already generic over the area count, no code changes needed. Verified live via a read-only recon (zero tickets touched): both new areas fit on the same non-scrolling Location Select screen continuing the existing 175px row spacing (825/1000), and share the identical 10-stage/already-SSS-cleared structure as the original 3. Rotation is now modulo 5. **Live-tested for real the same day**: `./ba_dailies.sh bounty` ran end-to-end against 道路 (that day's rotation pick) and completed cleanly, `result: swept`. One cosmetic-only gap found: the bottom-row stage-number OCR diagnostic log (not on the decision path -- selection is always the bottom-most row regardless) read `None` instead of a real digit string on this new area; left as-is per explicit user direction since it doesn't affect the sweep. | | Login | `core/Baas_thread.py`, `module/restart.py` | `to_main_page` (generic post-launch arrival routine, reused by every other reference feature's own navigation -- no separate "login" module exists in the reference), `restart.py`'s `implement`/`start` (check app running, launch if not) | `ba_auto/tasks/login.py` | Reference detects every one-off popup (~20 named `img_reactions`/`rgb_possibles`) via `picture.co_detect` image-template matching; this port scopes down to what was actually confirmed live (title screen via a fixed-chrome logo-color probe, a real network-error notice, the daily attendance card, S.C.H.A.L.E NEWS) plus a bounded generic Enter-press fallback for anything else recognized, mirroring co_detect's own "blind action once nothing matches" fallback shape but using this project's own established Enter-dismiss idiom rather than a blind coordinate click. `module/restart.py`'s kill-then-relaunch pattern ported directly via new `driver.kill_game`/`launch_game`/`is_game_running`/`window_exists` primitives (`pkill -f`/the account's own `/usr/local/bin/launch-blue-archive.sh`/`pgrep -f`/`xdotool search`) | Done. Live-calibrated 2026-07-16 on nik-gpu against the account's real overnight login-screen state (native 1920x1200 captures throughout, not the non-native `screenshots/daily_login/*.png` reference photos originally supplied -- same not-1:1 gap already documented for `screenshots/gem_shop/`/`screenshots/cafe/student/`). Confirmed live: the title screen's own logo reads a fixed brand-chrome color independent of rotating seasonal background art (confirmed across two different pieces), bright when clean and uniformly dimmed when a real notice is open on top of it (a genuine "ネットワークへの接続に失敗しました" network error surfaced unprompted during calibration); the daily attendance card only appears once per day (confirmed absent on an immediate same-day re-run after being claimed) and, like the network notice, responds to a plain Enter with no dedicated detection needed; the S.C.H.A.L.E NEWS popup needed a dedicated header-color probe since `navigation.is_on_subscreen`/`is_modal_open` both proved unreliable on it (same class of mismatch as `gem_shop.py`'s own dialog -- confirmed live by direct pixel comparison). **A real stuck-loading incident hit live during calibration itself**: the loading transition (a full-bleed variant with no chrome, distinct from a brief chrome-visible variant also seen) stalled past 6 minutes with zero progress, confirmed not a network/process-health issue; the user's own live guidance ("kill the game and rerun it") identified the relaunch script, and a second attempt after manually killing+relaunching completed the entire remaining flow (title tap -> attendance card -> home) in well under 15 seconds, confirming the stall was a genuine stuck state now handled automatically by `_recover`. Also observed live (twice, non-deterministically) but deliberately NOT worked around: a known pre-existing client rendering bug (per the user) where the news popup's own promo image can get stuck as a blank white rectangle after closing -- confirmed harmless to this module specifically (sits clear of every probe point used here, and the shared home-check probes still read correctly through it), and the user's own fix for it (reload the app) is already this module's existing stuck-recovery path. Added as the very first step in `DEFAULT_ORDER`, ahead of mailbox, since no other task can reach home from the title/loading/attendance-card states on its own. **Not yet live-confirmed**: the infrequent 業務復帰ログインボーナス welcome-back login bonus card from the user's own reference screenshots (account wasn't in that state during calibration) -- expected to fall through to the same generic-Enter path already confirmed for the attendance card and network notice, but not yet exercised for real; and the kill+relaunch recovery path has only been exercised once, manually, not yet through a fresh invocation of the actual `login.py` module hitting a real stuck state on its own. **Connectivity-probe fix, `_connectivity_confirmed` (2026-07-17)**: reported live by the user via real pulled cron logs -- a 4:30 AM `daily` fire logged a clean `[login] reached home`, but every task that ran afterward (cafe, event_sweep, circle, lesson, arena, both shops, gem_shop, mailbox, stamina) failed to open its own screen, persisting across multiple consecutive `q4h` fires spanning hours. Per the user's own direct knowledge: the server's daily reset or a concurrent login from another device can silently kill the session while the client keeps showing a cached-looking, visually normal home screen -- the error only surfaces as the game's own "connection lost" popup once an actual navigation/API call is attempted, which `_true_home`'s purely-visual checks never triggered. Fixed per the user's own suggested design ("enter the stamina claim/reward area then go back home"): `_connectivity_confirmed` now opens the Mission panel and closes it again before `_wait_for_home` will return success. Reproducing this live (the user logged into the account on their phone to trigger a real session kill) surfaced two FURTHER false positives in the same family, both from an unusually long, multi-frame animated loading sequence that never resolved on its own: first with `navigation.is_on_subscreen` (single-pixel) as the panel-opened check, then again after upgrading to `navigation.is_header_bar_visible` (8-point) -- each independently got fooled by a different coincidental splash frame within the same run. Fixed by requiring `_true_home` to re-verify a second time, after a short wait, before finally trusting a passed connectivity check. **Confirmed live**: the stuck session was cleared with a manual kill+relaunch, then a genuinely fresh cold-start `login` run (game process not running at all) correctly launched the game, passed through the title screen, and reached independently-verified true home (full HUD -- Lv/AP/credits/gems/bottom nav all visible, confirmed via direct screenshot and a separate `_true_home`/`is_header_bar_visible` check, not just the module's own printed message). Not yet re-confirmed against a fresh instance of the original silent-session-death failure mode specifically (the live reproduction available this session was the full-kick-to-login-screen variant, not the "still looks like home but isn't" variant from the cron logs), though the same `_connectivity_confirmed` mechanism covers both by design. **Faster stuck-loading detection, `_loading_buffer_visible` (2026-07-19)**: a manual `ba_cron_run.sh daily` run left the game stuck at login; `_recover`'s existing kill+relaunch DID fire correctly once the generic 240s timeout elapsed (confirmed live -- the user reported "the game did relaunched"), but waiting the full generic timeout for this specific, recognizable state is slower than necessary. Live-captured the real stuck screen and pixel-probed it (`screenshots/daily_login/stuck_loading_buffer.png`, calibrated via `scratchpad/probe_login_buffer.py`): the full-bleed loading variant's center spinner badge reads a flat, exact neutral gray `(118,118,118)` at a fixed screen-center position, independent of whichever splash-art frame is rotating behind it -- unlike the earlier `_true_home`/`_connectivity_confirmed` false positives, this doesn't share the "different frame fools a single-snapshot check" failure class, since the badge is a fixed UI overlay, not art. `_wait_for_home` now tracks continuous badge visibility and bails out to `_recover` after `config.LOGIN_LOADING_BUFFER_STUCK_SECONDS` (60s) instead of the full 240s once this exact state is confidently recognized. Verified offline against the real capture (all 6 probe points matched). Not yet live-confirmed against a fresh stuck instance recovering via this specific faster path (the triggering instance had already recovered via the pre-existing generic timeout by the time the fix was written). **Real Steam-signed-out incident + crash-cascade fix (2026-09-20)**: the user reported 3 consecutive real cron failures (`q4h` 01:00, `daily` 03:30 and 04:30, all same day). Every one logged `[login] game window not found -- launching` then `[login] warning: game window never appeared after launch -- giving up`, followed immediately by the NEXT task in the sequence crashing with an uncaught `RuntimeError` from `driver.focus_game()`. Root-caused with a live screenshot (superpowers:systematic-debugging) to Steam itself sitting on its own account sign-in dialog (username/password + QR code) the entire time -- not a Blue Archive client issue at all, and not something any amount of retrying/relaunching the GAME can fix, since it needs a real person to sign in. Two fixes: (1) `login._steam_needs_sign_in` (new `detector.read_text_white_on_dark`, since the dialog's own "SIGN IN WITH ACCOUNT NAME" label is light-blue-on-dark-navy, the opposite polarity of this project's usual OCR assumption -- confirmed live the label's grayscale luminance tops out at 126, needing a threshold well under the existing `read_int_white_on_dark`'s default 150) recognizes this exact state and turns the generic "never appeared" warning into a specific, actionable one; (2) `ba_daily.py`'s `_run_sequence` now checks `driver.window_exists()` after every task (except the last, and except `exit_game`, which is expected to close it) and aborts the whole run immediately with one clear message if the window is gone, instead of letting every remaining task independently rediscover this via its own doomed pre-task retry cycle before crashing -- still propagates as a real `FAILED` exit code (this project deliberately never swallows a genuine crash), just immediately and legibly instead of two tasks deep in a stack trace. **Confirmed live**: reproduced the exact original failure via a real `./ba_cron_run.sh q4h` run while Steam was still genuinely signed out, confirmed the new diagnostic message fired correctly and the sequence aborted right after `login` instead of wasting ~15s on `cafe`'s own doomed retry-then-crash. After the user signed back into Steam manually (confirmed via a live screenshot showing the real Blue Archive title screen), a real `./ba_cron_run.sh daily` recovery run was executed to catch up the account's daily claims -- see this row's own task entries for that run's outcome. | diff --git a/ba_auto/tasks/event_sweep.py b/ba_auto/tasks/event_sweep.py index 3a62b04..c3a6332 100644 --- a/ba_auto/tasks/event_sweep.py +++ b/ba_auto/tasks/event_sweep.py @@ -355,6 +355,58 @@ kind (the live investigation above spent its effort reproducing/fixing the finished-text rect instead, since that page happened to be reachable at the time) -- next real `stage_not_found`-triggering run should confirm the stability fix directly. + +**Dynamic stage-row search (2026-10-08).** Everything above still located +rows by a fixed tuple of y positions at the list's bottom-scrolled extreme +(config.EVENT_STAGE_ROW_Y), recalibrated per event: 366/538/710/883/1055 +for the 2026-07 event, shifted to 379/550/722/893 on 2026-09-24 for "It's +our Engine!". The very next event, a rerun of an older one ("我らオカルト研 +究会!", from 2026-10-07), rendered at the ORIGINAL 2026-07 positions again, +so every fixed crop missed its digits. The 2026-10-08 01:00 q4h run spent +all 6 outer attempts on "wrong_page" while sitting on the correct, fully +rendered list (its saved event_sweep_no_rows_debug.png shows rows 08-12 +plainly). The bottom-scrolled position just depends on each event's list +content height, so no fixed tuple can cover every event. + +Replaced with a port of the reference's own approach, core/image.py +swipe_search_target_str (called from activity_utils.to_mission_task_info), +instead of a fourth recalibration: +- _locate_stage_rows finds every fully-visible 入場 button by color blob + (detector.find_color_blobs; size filter rejects buttons clipped by the + viewport edge), then fits an OCR crop to the dark digit glyphs beside each + one (detector.find_glyph_bbox) and reads it with read_int_bordered. No y + position, row count or digit offset is hardcoded. +- _find_stage_row scrolls toward the target by comparing it with the + smallest/largest visible stage number, from wherever the list starts, + instead of jumping to the bottom extreme -- so any stage of any event + length is reachable, not just the last 5. +- A scan only counts if it reads 2+ strictly-ascending stage numbers. A + saved home-screen capture showed why: its お仕事 button is a big cyan blob + with a stray glyph that OCRs as "4". +- A target past the list's end (scrolled toward it, the list didn't move) + ends as "stage_not_in_event" with no further retries, since re-navigating + can't add a stage. +- The 入場 click now targets the detected button's own center. + +Live-confirmed at zero AP on the rerun event: the old failing screenshot +reads 8/9/10/11/12 correctly; starting from the top, middle and bottom of +the list, targets 1, 5, 9 and 12 were all found by scrolling; target 13 +came back "beyond_list" after one scroll that didn't move the list. One +wheel click measured as exactly one row (see +config.EVENT_STAGE_LIST_SCROLL_STEP_CLICKS). The first real q4h run on it +(2026-10-08 02:09) found stage 10 on its second attempt (the first landed +on the finished event's page, caught by _is_finished_event_page) and +swept it for real (AP 240 -> 10, "result: swept"). + +That same run exposed one more event-art-dependent probe: +_is_stage_modal_open read the event's own background at (1870, 600), +which this event paints dark, so the plain list read as "modal open" and +_close_stage_modal pressed Escape/the X position three times after the +sweep. Now reads the white header bar (fixed UI chrome) instead, with a +home-screen exclusion -- see config.py's "任務情報 (stage info) modal" +note. Live-confirmed at zero AP: home and plain list read closed, the real +stage-10 modal read open, and after _close_stage_modal it read closed with +the list intact. """ import datetime import os @@ -370,12 +422,26 @@ WRONG_PAGE_RETRIES = 6 STAGE_ENTER_RETRIES = 3 STAGE_ROW_SCAN_ATTEMPTS = 3 STAGE_ROW_SCAN_RETRY_WAIT = 2.5 +# Scroll-and-rescan budget for walking the list toward the target -- the +# reference's own swipe_search_target_str call uses max_swipe_times=10. +STAGE_SEARCH_STEPS = 10 +STAGE_MISREAD_RESCANS = 2 +STAGE_MISREAD_RESCAN_WAIT = 1 +STAGE_LIST_SCROLL_SETTLE = 1 SWEEP_START_RETRIES = 3 def _is_stage_modal_open(driver, config): - r, g, b = driver.color_at(*config.EVENT_STAGE_MODAL_PROBE) - return r < config.EVENT_STAGE_MODAL_DIM_MAX_CHANNEL and g < config.EVENT_STAGE_MODAL_DIM_MAX_CHANNEL and b < config.EVENT_STAGE_MODAL_DIM_MAX_CHANNEL + # The modal's dim backdrop darkens the event screen's white header bar; + # that bar is fixed UI chrome, unlike the event background art the old + # single-point probe read -- see config.py's "任務情報 (stage info) + # modal" note. The home check keeps a dark home header from ever + # reading as "modal open", so _close_stage_modal can't press Escape on + # the home screen (Blue Archive's exit-game prompt). + image = detector.capture_screen() + if navigation.is_header_bar_visible(driver, image=image): + return False + return not navigation.is_home_nav_bar_visible(driver, image=image) def _color_in_range(rgb, rgb_range): @@ -435,22 +501,44 @@ def _open_event_screen(driver, config): return True -def _row_number_rect(config, row_y): - x1, x2 = config.EVENT_STAGE_NUMBER_OCR_X - top_pad, bottom_pad = config.EVENT_STAGE_NUMBER_OCR_Y_PAD - return (x1, row_y - top_pad, x2, row_y + bottom_pad) +def _locate_stage_rows(config, image): + """Every fully-visible stage row in one frame, top to bottom, as + `(stage_number_or_None, enter_button_center, number_glyph_box_or_None)`. + + The local port of swipe_search_target_str's inner loop: find every 入場 + button (get_image_all_appear_position), then OCR the stage number on + that button's own row (its ocr_region_offsets crop). Unlike the + reference's fixed offset crop, the number crop here is fitted to the + digit glyphs actually found beside the button -- see + config.EVENT_STAGE_NUMBER_BAND_DX. + """ + dx1, dx2 = config.EVENT_STAGE_NUMBER_BAND_DX + rows = [] + for bx1, by1, bx2, by2 in detector.find_color_blobs( + config.EVENT_STAGE_LIST_REGION, *config.EVENT_STAGE_ENTER_BUTTON_RGB, + min_size=config.EVENT_STAGE_ENTER_BUTTON_MIN_SIZE, image=image, + ): + center = ((bx1 + bx2) // 2, (by1 + by2) // 2) + number_box = detector.find_glyph_bbox((max(0, bx1 + dx1), by1, max(0, bx1 + dx2), by2), image=image) + label = None + if number_box is not None: + nx1, ny1, nx2, ny2 = number_box + label = detector.read_int_bordered((nx1 - 4, ny1 - 4, nx2 + 4, ny2 + 4), image=image) + # Stage numbers render as two digits ("01".."12"); anything + # longer is an OCR artifact, not a real stage. + if label is not None and not 1 <= label <= 99: + label = None + rows.append((label, center, number_box)) + return rows -def _scan_stage_rows_once(driver, config, stage): - labels = [] - row_y_match = None - for row_y in config.EVENT_STAGE_ROW_Y: - label = detector.read_int_bordered(_row_number_rect(config, row_y)) - print(f"[event_sweep] row @ {row_y}: read '{label}'") - labels.append(label) - if label == stage: - row_y_match = row_y - return row_y_match, labels +def _scan_stage_rows_once(driver, config): + rows = _locate_stage_rows(config, detector.capture_screen()) + if not rows: + print("[event_sweep] no fully-visible 入場 buttons found") + for label, (_, y), _ in rows: + print(f"[event_sweep] row @ {y}: read '{label}'") + return rows def _is_finished_event_page(driver, config): @@ -469,71 +557,110 @@ def _is_finished_event_page(driver, config): def _find_stage_row(driver, config, stage): - # This event's target range (9-12) always sits within the last 5 rows, - # confirmed live regardless of starting scroll position -- so unlike - # story_sweep._find_stage_row, only the bottom extreme is ever checked. - # - # Also tracks whether ANY row OCR'd a real number at all. Originally, - # all-empty reads alone were treated as "wrong page" -- live testing - # found that produces false positives (a fresh navigation's list can - # still be settling/rendering for far longer than expected, up to - # ~20s+), and false negatives are cheap to avoid: an explicit check for - # the finished-event page's own "period ended" text (_is_finished_event_ - # page) is the AUTHORITATIVE signal now. All-empty reads alone just - # mean "keep waiting patiently", not "wrong page" -- see module - # docstring for the full history of this getting fixed twice. - # - # A real run's log (2026-07-29, user report) surfaced a related false - # positive this all-empty handling didn't cover: scan 1 read all 5 rows - # None (correctly treated as "still settling"), but scan 2 read a single - # stray/partial digit ('7', not matching any real row on this event) at - # just ONE row position while the other 4 were still None -- one non- - # None read was enough to satisfy the old "saw ANY valid row -> trust - # it" check, so the loop immediately concluded "genuinely on the right - # page, target stage just isn't in it" (stage_not_found) off a single - # transient misread, instead of recognizing the list was still mid- - # render. Fixed the same way the all-empty case already was: don't - # trust a "some rows read, target not among them" conclusion the first - # time it appears either -- require the exact same set of labels to - # read back identically on two consecutive scans first. A genuinely - # rendered (even wrong) page's OCR reads are stable frame to frame; a - # mid-render one isn't, which is exactly the signal that already fixed - # the all-empty version of this same problem class. + """Scroll the Quest list until `stage`'s row is visible. Returns + `(enter_button_center_or_None, status)`, status one of: + + - "found" + - "finished_page": the finished-event "period ended" text is showing + - "no_rows": no stage number ever read -- still rendering, or a page + this task doesn't recognize (debug screenshot saved) + - "beyond_list": scrolled toward the target and the list didn't move + -- `stage` lies past this event's last/first row + - "not_found": rows read, but the target's own row never did + + Port of core/image.py swipe_search_target_str's outer loop: compare the + target against the smallest/largest stage number visible and scroll + toward it (down if bigger than everything visible, up if smaller), + rather than jumping to one pre-calibrated scroll extreme and reading + fixed row positions -- see config.EVENT_STAGE_LIST_REGION for why the + fixed-position version kept breaking on every new event. + + Carried over from the previous fixed-row version (see module docstring): + - a freshly-opened list can take many seconds to render, so a scan with + no readable numbers waits and rescans in place rather than concluding + "wrong page" at once; _is_finished_event_page is the only immediate, + authoritative wrong-page signal; + - a single transient read is never trusted to conclude the target is + absent: "beyond_list" requires a real scroll toward the target that + left the exact same labels on screen, and a target that should be + visible (between the smallest and largest number read) but wasn't is + rescanned in place, not given up on. + """ x, y = config.EVENT_STAGE_LIST_SCROLL_POINT - driver.scroll(x, y, "down", config.EVENT_STAGE_LIST_SCROLL_CLICKS) - driver.wait(0.5) - + empty_scans = 0 + misread_rescans = 0 + saw_any_valid_row = False + last_direction = None previous_labels = None - for attempt in range(1, STAGE_ROW_SCAN_ATTEMPTS + 1): - row_y, labels = _scan_stage_rows_once(driver, config, stage) - if row_y is not None: - return row_y, True, False - saw_any_valid_row = any(label is not None for label in labels) - if saw_any_valid_row and labels == previous_labels: - return None, True, False + for _ in range(STAGE_SEARCH_STEPS): + rows = _scan_stage_rows_once(driver, config) + labels = [label for label, _, _ in rows] + visible = [label for label in labels if label is not None] - if _is_finished_event_page(driver, config): - print("[event_sweep] finished-event page text detected -- confirmed wrong page") - return None, False, True - - if saw_any_valid_row: - print(f"[event_sweep] some rows read but not yet stable across scans (scan {attempt}/{STAGE_ROW_SCAN_ATTEMPTS}): {labels}") + # Only trust a scan that actually looks like this list: 2+ stage + # numbers, strictly ascending top to bottom. Confirmed against a + # saved home-screen capture: the お仕事 button there is a big cyan + # blob too, with a stray glyph beside it that OCRs as "4" -- one + # bogus row like that must never be clicked as the target or steer + # the scroll. A mid-render or misread list fails this the same way + # and is just rescanned. + if len(visible) >= 2 and all(a < b for a, b in zip(visible, visible[1:])): + for label, center, _ in rows: + if label == stage: + return center, "found" else: - print(f"[event_sweep] no stage-row numbers recognized on scan {attempt}/{STAGE_ROW_SCAN_ATTEMPTS} -- screen may still be settling") - previous_labels = labels - if attempt < STAGE_ROW_SCAN_ATTEMPTS: + if visible: + print(f"[event_sweep] rows read {labels} don't look like a stage list yet (need 2+ ascending numbers)") + visible = [] + + if not visible: + if _is_finished_event_page(driver, config): + print("[event_sweep] finished-event page text detected -- confirmed wrong page") + return None, "finished_page" + empty_scans += 1 + if empty_scans >= STAGE_ROW_SCAN_ATTEMPTS: + break + print(f"[event_sweep] no stage-row numbers recognized (empty scan {empty_scans}/{STAGE_ROW_SCAN_ATTEMPTS}) -- screen may still be settling") driver.wait(STAGE_ROW_SCAN_RETRY_WAIT) + continue + saw_any_valid_row = True + + if stage > max(visible): + direction = "down" + elif stage < min(visible): + direction = "up" + else: + misread_rescans += 1 + if misread_rescans > STAGE_MISREAD_RESCANS: + print(f"[event_sweep] stage {stage} should be among the visible rows {labels} but its row never read as {stage}") + return None, "not_found" + print(f"[event_sweep] stage {stage} should be among the visible rows {labels} -- rescanning in place ({misread_rescans}/{STAGE_MISREAD_RESCANS})") + driver.wait(STAGE_MISREAD_RESCAN_WAIT) + continue + + if direction == last_direction and labels == previous_labels: + print(f"[event_sweep] list didn't move scrolling {direction} (still {labels}) -- stage {stage} is past the end of this event's stage list") + return None, "beyond_list" + + print(f"[event_sweep] stage {stage} is outside the visible rows {visible} -- scrolling {direction}") + driver.scroll(x, y, direction, config.EVENT_STAGE_LIST_SCROLL_STEP_CLICKS) + driver.wait(STAGE_LIST_SCROLL_SETTLE) + last_direction = direction + previous_labels = labels + + if saw_any_valid_row: + print(f"[event_sweep] stage {stage} not reached within {STAGE_SEARCH_STEPS} scans") + return None, "not_found" # Exhausted the patience budget with no valid rows AND no confirmed # finished-event text -- a genuinely inconclusive state. Save a debug - # screenshot so a future recurrence can actually be diagnosed/used to - # fix EVENT_FINISHED_TEXT_RECT's calibration, per CLAUDE.md's "write - # debug images to scratchpad/" convention. + # screenshot so a future recurrence can actually be diagnosed, per + # CLAUDE.md's "write debug images to scratchpad/" convention. debug_path = os.path.join(config.SCRATCHPAD_DIR, "event_sweep_no_rows_debug.png") driver.screenshot(debug_path) print(f"[event_sweep] gave up waiting for stage rows without confirming finished-event text either -- saved {debug_path}") - return None, False, False + return None, "no_rows" def _click_max_and_verify(driver, config): @@ -636,14 +763,14 @@ def _watch_sweep_result(driver, config): return outcome or "unrecognized_state" -def _open_stage_modal(driver, config, row_y): +def _open_stage_modal(driver, config, button_center): # A single click-then-check here was found live to intermittently miss # (CLAUDE.md already documents this exact failure mode for the # mailbox/cafe icons: "missed the first click and worked on retry") -- # retry-with-verify like every other click-then-confirm step in this # module, rather than aborting on the first miss. for attempt in range(1, STAGE_ENTER_RETRIES + 1): - driver.click(config.EVENT_STAGE_ENTER_X, row_y) + driver.click(*button_center) driver.wait(2) if _is_stage_modal_open(driver, config): return True @@ -669,17 +796,19 @@ def _click_sweep_start_and_verify(driver, config): def _sweep_target(driver, config, stage, count): print(f"[event_sweep] --- target stage {stage} x {count} ---") - row_y, saw_any_valid_row, confirmed_wrong_page = _find_stage_row(driver, config, stage) - if row_y is None: - if confirmed_wrong_page: - return "wrong_page" - if not saw_any_valid_row: - print("[event_sweep] gave up waiting for stage rows without ever confirming finished-event text -- treating as wrong page anyway (see scratchpad debug screenshot)") - return "wrong_page" - print(f"[event_sweep] stage {stage} not found in the visible stage list") + button_center, status = _find_stage_row(driver, config, stage) + if status == "finished_page": + return "wrong_page" + if status == "no_rows": + print("[event_sweep] gave up waiting for stage rows without ever confirming finished-event text -- treating as wrong page anyway (see scratchpad debug screenshot)") + return "wrong_page" + if status == "beyond_list": + return "stage_not_in_event" + if status != "found": + print(f"[event_sweep] stage {stage} not found in the stage list") return "stage_not_found" - if not _open_stage_modal(driver, config, row_y): + if not _open_stage_modal(driver, config, button_center): print("[event_sweep] stage info panel not detected, aborting") return "unrecognized_state"