diff --git a/ba_auto/config.py b/ba_auto/config.py index 1f99ba6..cac16dc 100644 --- a/ba_auto/config.py +++ b/ba_auto/config.py @@ -823,7 +823,32 @@ SHOP_PRICE_OCR_RETRIES = 3 # Common Shop target name (single-line names only -- two-line-wrapping # names like the bundle/material items sit differently and aren't covered, # but none of the configured targets below wrap). -SHOP_NAME_OCR_OFFSET = (-75, -105, 160, -45) +# +# Left edge moved from -75 to -30 (2026-09-24, live incident): a promotional +# ribbon/badge decoration -- confirmed live on '初級強化珠' (Novice tier) and +# '最上級強化珠' (Highest tier) specifically, not on the two middle tiers -- +# overlaps the left ~40px of the old crop, corrupting the OCR'd name badly +# enough that neither the exact, substring, nor fuzzy-similarity match stage +# in _match_name could resolve it (confirmed live: read as total garbage like +# '入弧比味' for '初級強化珠'). Pixel-column analysis of real live crops +# (scratchpad captures, both ribbon colors) found the decoration itself never +# extends past x=42 within the old crop, with the actual name text not +# starting until x=68-82 -- so shifting the left edge by +45px clears every +# observed ribbon with margin to spare, while the right edge is untouched +# (still wide enough for the longest configured name, e.g. 最上級強化珠). +# +# Top edge moved from -105 to -95 (2026-09-24, same incident, found after +# the left-edge fix above still left '最上級強化珠' unmatched across 12 +# straight reopen attempts): even with a raw crop that looked completely +# clean to the eye, the actual thresholded/upscaled image tesseract sees +# had two small stray dark blobs in its first ~5px, above the real text -- +# confirmed by row-by-row brightness analysis of a live crop, blob at +# y=0-4, then clean background to y=26, then real text starting at y=28. +# These are faint enough to be easy to miss by eye but enough to confuse +# psm 7's single-text-line assumption. Shifting the top edge down by 10px +# clears the blob with comfortable margin before the real text on every +# row, ribbon-affected or not. +SHOP_NAME_OCR_OFFSET = (-30, -95, 160, -45) # A point inside each cell's own buy-button bar. First calibrated against # two downloaded screenshots as a plain brightness check (purchasable # bright ~(235,241,241) vs. sold-out/greyed ~172-174 flat) -- but a live @@ -870,6 +895,46 @@ SHOP_SCROLL_REALIGN_MAX_NUDGES = 5 # the live catalog was ~6 rows deep (24 slots) when this was built, so this # leaves headroom without scrolling forever if that detection ever misses. SHOP_NAME_SCAN_MAX_STEPS = 12 +# How many times select_targets_by_name retries a full row0/row1 pass that +# matched nothing before concluding the current (unscrolled) viewport +# genuinely has nothing left and scrolling away from it. Added 2026-09-24: +# real live testing (five consecutive, purchase-free selection-only runs) +# found OCR reads of the exact same still-visible, still-purchasable cell +# flip between clean and corrupted from one fresh screenshot to the next -- +# a transient capture/render issue, not a fixed per-item bug (which target +# failed varied run to run). A single bad pass used to be treated as proof +# nothing was left, immediately triggering a scroll and compounding the +# problem with that operation's own separate fragility -- retrying first +# gives a transient bad frame a chance to clear before paying that cost. +# +# **Lowered from 10 to 2 (2026-09-24)**: a live stability test proved this +# retry, as originally designed, can never help -- reading the exact same +# still-open cell 5 times in a row gave the identical wrong text every +# single time, so the corruption is stable for as long as the tab stays +# open, not a per-screenshot flake that a wait-and-reread can catch. Kept +# small (covers a genuine one-off, e.g. a mid-transition frame right after +# a click) rather than removed outright, but the real fix is +# SHOP_REOPEN_MAX_ATTEMPTS below -- a full tab reopen actually gets a fresh +# render, confirmed live to differ on 4 of 5 reopens for the same cell. +SHOP_NO_MATCH_RETRIES = 2 +# How many times select_targets_by_name fully leaves and re-enters the shop +# tab, once SHOP_NO_MATCH_RETRIES's in-place rescans are exhausted, before +# falling back to scrolling. Added 2026-09-24 alongside the retry count +# above -- see shop_utils.select_targets_by_name's own docstring for the +# live evidence this was built from. **Raised from 5 to 12** the same day: +# 5 was enough for most stuck targets (a single-character OCR substitution +# usually clears _match_name's fuzzy-similarity threshold within a couple +# of reopens), but one real live target's corruption pattern happened to +# score higher against a WRONG neighbor (0.727) than its own true target +# (0.667) -- both below/at the fuzzy threshold either way -- so it needs a +# reopen that renders noticeably more cleanly than usual, not just +# differently, to ever match; exhausted all 5 attempts in one real run. +# Giving more attempts (each one just a normal navigation cycle, not a +# blind guess) is the correct lever here, not lowering the similarity +# threshold -- that was tried and tested worse, since a looser threshold +# let OTHER, previously-correct fuzzy matches resolve to the wrong +# same-category neighbor instead. +SHOP_REOPEN_MAX_ATTEMPTS = 12 # Extra wait after a confirmed checkbox click, before select_targets_by_name # rescans the grid for its next target -- on top of the 0.3s already spent # confirming the checkbox itself registered. Added 2026-09-17: a real live diff --git a/ba_auto/detector.py b/ba_auto/detector.py index bbf0a8f..b8f29a6 100644 --- a/ba_auto/detector.py +++ b/ba_auto/detector.py @@ -319,8 +319,26 @@ def read_text_white_on_dark(region, whitelist=None, psm=7, lang="eng", threshold def read_int(region, psm=7): - """Local equivalent of the reference's recognize_int.""" - digits = "".join(ch for ch in read_text(region, whitelist="0123456789", psm=psm) if ch.isdigit()) + """Local equivalent of the reference's recognize_int. + + Whitelist includes ',' even though the return value is comma-stripped + digits only -- a real live misread (2026-09-24) found a digit-only + whitelist forces tesseract to misclassify a genuine thousands-separator + comma as a phantom digit instead of punctuation it's simply not allowed + to output, corrupting the whole read (confirmed live: Common Shop's + '中級レポート' at 125,000 read as 1250060/195900/4925000 depending on + the exact frame, never the correct value, across a digit-only whitelist + and multiple psm modes) -- allowing ',' through lets tesseract correctly + segment it as punctuation, and the existing isdigit() filter here already + strips it either way, so this is a strict improvement with no behavior + change for any comma-free read (region numbers, ticket counts, etc.). + This same whitelist also covers config.CREDIT_BALANCE_OCR_RECT reads + (routinely multi-comma, e.g. "412,271,037"), used by + shop_utils.confirm_purchase's balance-delta verification -- a likely + contributor to that function's own intermittent "purchase confirmation + did not resolve" false negatives, though not independently confirmed. + """ + digits = "".join(ch for ch in read_text(region, whitelist="0123456789,", psm=psm) if ch.isdigit()) return int(digits) if digits else None diff --git a/ba_auto/reference_notes/mapping.md b/ba_auto/reference_notes/mapping.md index c45acad..2550a30 100644 --- a/ba_auto/reference_notes/mapping.md +++ b/ba_auto/reference_notes/mapping.md @@ -17,7 +17,7 @@ Maps each local feature to the corresponding `~/repo/baas-reference/module/...` | Commissions | `module/clear_special_task_power.py` | Need to inspect | `ba_auto/tasks/commission.py` | sweep/color adaptation | Not started | | Exit game | None -- searched `~/repo/baas-reference/` thoroughly, no counterpart exists. The closest match, `core/Baas_thread.py`'s `shutdown()`/`start_shutdown()` (line ~984), is an optional full Windows OS shutdown (`subprocess.run(["shutdown", "-s", "-t", "60"])`) gated by a user toggle, not a game-client exit -- out of scope (this project's game and X display run on `nik-gpu`, shutting the host down would kill the SSH session too, nothing like what was requested). The only other app-closing call, `connection.py`'s `close_current_app` (line 381, plain ADB `app_stop`), exists purely for `Baas_thread.py`'s own error-recovery restarts (`deal_with_package_incorrect`/`deal_with_func_call_timeout`) inside a persistent background scheduler that's designed to keep running indefinitely -- it never deliberately exits once daily tasks finish. This feature is new, desktop-specific convenience logic motivated by this project's different architecture (cron launches a fresh one-shot process per preset, so closing the game after a run has real value the reference's always-on scheduler never needed), not a port of anything. | `ba_auto/tasks/exit_game.py` | No new driver/detector primitives needed -- reuses `navigation.return_to_home` (must-be-true-home gate), `navigation.is_modal_open` (verifies the Escape-triggered "exit the game?" dialog actually opened, the same generic dim-probe reused across bounty/gem_shop/circle's own confirm dialogs), `driver.keypress("Escape"/"Return")`, and `driver.window_exists()` (verifies the game process/window is actually gone afterward, not just that Enter was sent) | Done. `navigation.return_to_home`'s own docstring already documents the mechanism this relies on: an Escape press on the confirmed true home screen (nowhere else) raises this exact dialog -- every other task treats that as a hazard to avoid triggering by accident; this is the one task that wants it, gated the same way (only fires from a verified true-home state). No force-kill fallback if the graceful Escape/Enter path doesn't verify -- matches this project's established "abort cleanly on unknown state rather than guess" convention (gem_shop/bounty), rather than reaching for `driver.kill_game()`. Opt-in only, appended to the end of the `daily`/`q4h` presets (not `DEFAULT_ORDER`), since it deliberately ends the session. **Confirmed live (2026-07-18)** via a standalone `./ba_dailies.sh exit_game` run, user-reported "works well" -- `is_modal_open`'s generic dim-probe correctly read the exit-confirmation dialog after Escape, and the game closed cleanly after Enter. Not yet exercised as the tail end of a full `daily`/`q4h` preset run (only standalone so far), though nothing in its own logic depends on which task ran before it. | | Arena | `module/arena.py` | `implement` (main flow), `to_tactical_challenge` (nav from main page), `get_tickets` (ticket-count OCR), `choose_enemy` (self/opponent level OCR + bounded refresh-reroll loop), `check_skip_button` (skip-toggle color probe), `fight` (click fight, wait for win/lose), `collect_tactical_challenge_reward` (two reward-slot color probes) | `ba_auto/tasks/arena.py` | Ticket count/self level/opponent level/rank are plain digit OCR (`detector.read_int`/`read_text`, plus a new `read_int_white_on_dark` for the profile card's bright-on-dark level text). Skip-toggle state and the two reward-slot claimed-vs-claimable colors are plain pixel-color probes (`driver.color_at`/`_color_in_range`). The post-fight WIN/LOSE result modal and an unrelated list-refresh-expired notice are NOT detected by precisely locating a button (a color-region search proved unreliable — see Status); they're dismissed via a bounded blind-Enter-press loop matching `lesson.py`'s own `_run_one_schedule` pattern, gated by a hard safety check against the one modal where Enter is dangerous (opponent-info's own attack-formation button, checked via its fixed-position gold button). `choose_enemy`'s refresh-reroll loop is direct Python control flow, bounded by `maxArenaRefreshTimes`. Config knobs carried over from reference defaults: `ArenaComponentNumber`=1, `ArenaLevelDiff`=0, `maxArenaRefreshTimes`=10, `ArenaStopFightWhenRank1`=False | Done. Live-tested for real across all 5 of the account's daily tickets (2 WIN, 1 LOSE, 2 spent debugging the result-modal detection — see `plan.md` Phase 13 for the full writeup). Real navigation differences confirmed live: Tactical Challenge is a Work-hub card, not a bottom-nav icon; the reference's separate opponent-info and formation-edit screens are merged into one modal here with a live ticket-preview; `navigation.is_modal_open`'s shared probe reads *inverted* on this screen (own `_is_modal_open` via `config.ARENA_MODAL_PROBE`). Three real bugs fixed: a level-OCR crop too small for tesseract despite looking legible (fixed by widening the crop, not the pipeline); level text being bright-on-dark unlike every other OCR read in this project (fixed via `read_int_white_on_dark`); and the result-modal detection cycling through two failed color-based designs (fixed by switching to bounded blind-Enter dismissal with a hard safety gate — a real near-miss of the same "mistimed keypress" hazard class `CLAUDE.md` already documents from story_sweep). Deliberately opt-in only, never in `DEFAULT_ORDER` — unlike every other opt-in task so far (which spend a known-safe resource on a config-driven target list), this one fights a real ranked PvP battle that can win or lose and moves the account's actual arena rank. Per explicit user decision: fights exactly one battle per invocation, matching the reference's own per-call pacing (its `next_time = 55` background-thread rescheduling has no equivalent in this project's one-shot CLI). Not yet exercised live: the "no ticket" mid-flow race, an actual reroll click (every opponent offered was already an acceptable level), and `ArenaStopFightWhenRank1`'s rank-1 stop condition — all implemented per the reference's logic, just not yet hit by real game state. `detector.find_template`/`template_visible` (generalized named-template matcher, built during scaffolding) ended up unused — state detection stayed OCR/color-probe-driven throughout, like every other task in this project. Follow-up (2026-07-11): live bug report — this task never returned home at the end, so a second consecutive invocation starting from wherever the first left the game (the Tactical Challenge screen itself) sent `_open_tactical_challenge`'s home-relative clicks to the wrong place and failed all 3 retries. Fixed by calling the shared `navigation.return_to_home(driver)` at the very start of `run()`, reusing the generic Escape-based recovery primitive built for `event_sweep.py`'s own wrong-page recovery. Confirmed live (2026-07-11): reproduced the stuck-on-arena-screen scenario manually, then a real `arena` invocation recovered and completed a full fight normally (rank 14位's opponent-list entry moved 10位→9位, ticket 2→1, credits +1,080), with no repeat of the original failure. Follow-up #2 (2026-07-12), per explicit user request: reversed the original "exactly one battle per invocation" decision — `_fight_one` now holds the single-battle logic and `run()` loops it (`while tickets > 0 and fights < config.ARENA_MAX_FIGHTS_PER_RUN`), re-reading the OCR'd ticket count after each fight and waiting `config.ARENA_POST_BATTLE_COOLDOWN` (30s, per the user's own info about the real in-game lockout between fights) before continuing. `ArenaStopFightWhenRank1` is now re-checked before every fight in the loop, not just once. See `plan.md`'s Phase 13 follow-up #2 — not yet live-tested (spends multiple real tickets, needs the user's go-ahead first). **This DID subsequently run live via cron and surfaced a real hazard (2026-07-17)**: the log showed "battle skip already on" for all 5 fights and a clean "fought 5 battle(s) this run", but the account was left showing a real "Battle Complete" screen with a ~3-minute combat timer -- battles had run in full, not skipped -- and every task for the rest of that `daily` invocation failed to open its own screen. Two compounding bugs: (1) `_ensure_skip_on`'s "on" detection was never actually verified to reject a real "off" state (`ARENA_SKIP_ON_RGB` was only ever confirmed against a session where skip happened to already be on); (2) `_wait_for_result` had no early-exit signal and always ran its full fixed 15s budget before unconditionally declaring success -- far too short for a real battle -- and `_fight_one` discarded `_wait_for_result`'s return value entirely, so even a correctly detected failure never stopped the fight loop. Fixed by widening `RESULT_MODAL_MAX_POLLS` substantially (tolerating a real battle's full duration regardless of whether the skip-mode detection gets fully root-caused) plus a new `_back_on_challenge_list` early exit (so the normal fast case doesn't slow down), and by making `_fight_one` actually stop on a `_wait_for_result` failure. The skip-toggle's own "off" detection was not independently re-verified (would need a real ticket at a moment the account had none left) -- deliberately structured so the fix is safe even if that root cause isn't fully resolved. | -| Common Shop | `module/shop/common_shop.py`, `module/shop/shop_utils.py` | `implement`, `to_common_shop`, `get_item_position`/`ensure_choose`/`buy` (shared, see Tactical Shop row) | `ba_auto/tasks/shop_common.py`, `ba_auto/tasks/shop_utils.py` | Originally ported as fixed grid-position targets (`config.COMMON_SHOP_TARGETS`) + price-digit OCR verify, matching the reference's own external-price-list-index design as closely as this repo's missing external table allows. **Revised 2026-08-01**: fixed position turned out not to hold live -- this shop sinks sold-out items to the bottom of its grid and shifts everything after them up, so `(row, col)` drifted the moment anything sold out (see plan.md Phase 25 follow-up #2). `select_targets_by_name` now identifies items by OCR'd name instead, scanning/scrolling the full (scrollable, ~24-slot) grid and matching on name + exact price + a purchasable-button color check -- arguably *closer* to the reference's item-identity-based design intent than the original position table was, just using a local OCR read in place of the reference's external price-list index. Tactical Shop stays on the original position-based path (never showed this symptom). Purchase-confirm dialog + reward-acquired banner still handled via a single overlay-darkness probe (`config.SHOP_OVERLAY_PROBE`). **Rewritten again 2026-09-17** after a real live under-purchase (reported by the user with their own cron log pasted in: a real `daily` run bought only 4 of 8 configured targets, logging the other 4 as "not found available" even though, hours later with nothing else touched, those exact 4 sat unbought at their exact expected prices right at the top of the unscrolled grid) -- see the Status column for the full root-cause writeup; fixed three distinct bugs, not one | Done, re-verified live post-2026-08-01-rewrite (superseded by the 2026-09-17 fixes below): real run correctly bought the one target that was actually available (`中級レポート`, 125,000 credits, matching a real credit-balance delta) and correctly logged the other 7 as unavailable this cycle rather than misdiagnosing them as a catalog change. **2026-09-17 live debugging** (superpowers:systematic-debugging, root-caused from the user's own pasted `~/repo/scratchpad/ba_logs/daily.log` excerpt cross-checked against live screenshots -- see `ba_auto/tasks/shop_utils.py`'s own docstrings for the full writeup) found and fixed THREE separate real bugs, confirmed one at a time via five consecutive real `./ba_dailies.sh shop_common` runs against the account's actual daily catalog: (1) `select_targets_by_name`'s old row/col loop kept iterating with stale screenshot-implied positions after a successful buy, even though buying an item collapses its grid slot and shifts every later item forward -- fixed by re-scanning the current viewport from scratch (`_scan_current_view`) after every single buy instead of continuing the old pass; (2) even with that fix, two more confirmed-available targets were still missed, because the checkbox's own "checked" confirmation settles well before the rest of the grid's reflow/collapse animation visually finishes -- fixed with `config.SHOP_REFLOW_SETTLE_WAIT` (1.5s, not yet tuned down from a measured minimum), an explicit wait before the post-buy rescan; (3) the deepest bug: Common Shop's own tier-naming convention nests a shorter tier name inside a longer one (`上級強化珠`/Advanced is a literal substring of `最上級強化珠`/Highest, same for the レポート pair), and the original substring-only name match had no preference for an exact match, so scanning a "最上級..." card while its "上級..." sibling was still also a remaining target could resolve to the WRONG (shorter) target, compare against the wrong price, and permanently skip a genuinely-available card every run -- confirmed live: `最上級強化珠` sat available at its exact listed price across three consecutive real runs, never once bought, until fixed via `_match_name`'s exact-match-first resolution (substring fallback now prefers the longest/most-specific candidate). All 8 of today's configured targets were confirmed successfully bought across the debugging session's real runs (credit balance deltas matched each purchase's exact expected price sum throughout), and a final clean run correctly reported all 8 as sold out for the day with no further false skips. Earlier finding, still true: no visible purchase-count indicator (unlike the 青輝石 tab's "あと1回購入可能" labels). Also encountered but unrelated to this task: a stray already-open MomoTalk panel intercepted a `SHOP_ICON` click mid-session (closed manually); `navigation.is_on_subscreen`/`is_modal_open` don't recognize MomoTalk as a blocking state, same class of blind spot already documented for the old gem_shop dialog and cafe's rank-up cutscene -- not fixed here, out of scope for this incident, flagged for future attention if it recurs. **A FOURTH real bug found 2026-09-19**, two days after the above: the user reported the account still only showing 4 bought items on a given check. Re-investigating with the user's own cron logs (`~/ba_logs/daily.log`/`q4h.log`) found that, taken across both real `daily` cron fires each day, all 8 targets genuinely were being bought (the two fires split the 8 into complementary halves, each catching whatever the other one's timing missed) -- so the "only 4" symptom was a same-day timing snapshot, not a standing bug, EXCEPT for one real, separately-confirmed defect: `shop_utils.confirm_purchase` checked the purchase-confirmation overlay exactly once after clicking Buy and returned success as soon as that single read was "idle" -- with nothing distinguishing "confirmed and closed" from "the Buy click missed and the confirm dialog never opened at all". Caught live: a real cron run's log claimed "bought 4 item(s) for 303000 credits", but those exact 4 items were still sitting unbought at their base price hours later; re-running the task then genuinely bought them (confirmed via an exact matching credit-balance delta). Fixed by requiring the currency balance to have actually dropped by the exact expected spend once the overlay reports idle (re-read with the same `SHOP_PRICE_OCR_RETRIES` stray-misread-digit tolerance already used elsewhere in this module), returning `False` -- not a false "purchased" -- if it never does. Logically verified and syntax-checked, but **not yet live-confirmed against a real purchase specifically**: by the time the fix was written, all 8 of today's targets were already bought (from the same day's own earlier debugging), so no fresh purchasable item was available to exercise the new balance-check end-to-end; needs re-confirming the next time a genuinely fresh target is available (tomorrow's catalog reset, or Tactical Shop once its own separate price-misread bug -- see that row -- is fixed). **A FIFTH real bug found 2026-09-21**: the user reported only 4 of 8 targets bought again. Investigation (read-only OCR probes against the live screen, then a real `./ba_dailies.sh shop_common` run, then a before/after screenshot of a single scroll) found the remaining 4-5 "not found available" targets were genuinely available, correctly priced, purchasable, and even matched cleanly by a read-only replay of the exact same matching code -- yet the real production scan still missed them. Root cause: `config.SHOP_SCROLL_STEP_CLICKS`'s "advances by exactly one row" assumption (true when originally calibrated) no longer holds now that the catalog mixes in raw-material items whose two-line-wrapped names make some rows taller than others -- a single scroll call can overshoot a clean row boundary and land mid-row. Confirmed live: a before/after screenshot pair showed the grid jumping ~1.5-2 rows in one scroll, cutting card titles off above the viewport; every fixed-offset OCR/color read after that came back empty/garbled, and `_is_purchasable` read `False` across all 8 cells (vs. 7-8/8 `True` on a genuinely aligned frame) -- so the scan silently treated a torn frame's unreadable cells as "target not found" rather than "my crop coordinates are pointing at nothing". Fixed with `shop_utils._grid_looks_aligned`/`_ensure_grid_aligned`: after every scroll, verify at least one cell still reads purchasable before trusting the frame, nudging back up in small steps (`config.SHOP_SCROLL_REALIGN_NUDGE_CLICKS`/`_MAX_NUDGES`) if not, rather than scanning (and misreporting from) a misaligned frame. Only bites items that get reflowed out of the first two visible rows mid-run by earlier purchases in the same pass -- explains why the specific "missing 4" vary day to day rather than being a fixed set. Deployed and live-tested: see `plan.md` for the outcome. **That fix turned out NOT to be the actual cause of the recurring pattern** -- the next morning (2026-09-22) the exact same symptom recurred (real `daily` cron logs: the 03:38 run found ALL 8 targets "not found available" within 3 seconds -- far too fast for a genuine multi-row OCR scan -- and the 04:53 run then bought only the same familiar "top 4", leaving the same familiar "bottom 4" unbought). Re-investigated live: a read-only probe and a fresh interactive replay of the real matching code both found the "stuck" 4 sitting unscrolled at the top of the grid, correctly priced and purchasable -- confirming the scroll-alignment fix from the day before was never even being exercised for this failure. **A SIXTH real bug, found the same session**: while manually replaying the exact production logic step by step to gather evidence, a genuine in-game "ネットワークアクセスに問題が生じました" (network access problem) modal appeared over the shop grid mid-flow -- a real, first-party dialog, not anything this project's code triggers. It dims the whole grid, so every fixed-offset OCR/color read behind it comes back empty/garbled; and because a static dialog doesn't change frame to frame, `select_targets_by_name`'s own "screenshot unchanged" stop condition fires almost immediately, misreporting every remaining target as unavailable in a couple of seconds flat -- an exact match for the 03:38 run's suspiciously fast full-failure. Fixed by checking `navigation.is_modal_open` (the same screen-dimming probe used throughout this project) at the top of every scan pass, before trusting anything read from that frame; skipped targets from an aborted scan now get a distinct `blocked_by_modal` reason instead of the misleading "sold out this cycle, or catalog changed" message. Deliberately does not try to dismiss the dialog itself (this specific one's own "OK" button logs all the way out to the title screen, confirmed live when recovering from it via the `login` task) -- an unrecognized modal gets a loud, honest stop instead of a guessed keypress. **Confirmed live**: with the game recovered to a clean state, a real `./ba_dailies.sh shop_common` run then correctly found and bought all 4 remaining "stuck" targets for 303,000 credits (credit balance delta matched exactly), completing the full day's 8/8 targets (908,500 earlier + 303,000 = 1,211,500, the exact full catalog total) with zero items ever falsely reported unavailable. Whether the 2026-09-21 scroll-alignment fix was itself a real, separate bug (yes, confirmed independently that day) or was ever actually the cause of a cron-observed failure (unconfirmed either way) remains open -- both fixes are kept, since each addresses a distinct, independently-confirmed way a frame can be misread as "nothing here" when something really is. | +| Common Shop | `module/shop/common_shop.py`, `module/shop/shop_utils.py` | `implement`, `to_common_shop`, `get_item_position`/`ensure_choose`/`buy` (shared, see Tactical Shop row) | `ba_auto/tasks/shop_common.py`, `ba_auto/tasks/shop_utils.py` | Originally ported as fixed grid-position targets (`config.COMMON_SHOP_TARGETS`) + price-digit OCR verify, matching the reference's own external-price-list-index design as closely as this repo's missing external table allows. **Revised 2026-08-01**: fixed position turned out not to hold live -- this shop sinks sold-out items to the bottom of its grid and shifts everything after them up, so `(row, col)` drifted the moment anything sold out (see plan.md Phase 25 follow-up #2). `select_targets_by_name` now identifies items by OCR'd name instead, scanning/scrolling the full (scrollable, ~24-slot) grid and matching on name + exact price + a purchasable-button color check -- arguably *closer* to the reference's item-identity-based design intent than the original position table was, just using a local OCR read in place of the reference's external price-list index. Tactical Shop stays on the original position-based path (never showed this symptom). Purchase-confirm dialog + reward-acquired banner still handled via a single overlay-darkness probe (`config.SHOP_OVERLAY_PROBE`). **Rewritten again 2026-09-17** after a real live under-purchase (reported by the user with their own cron log pasted in: a real `daily` run bought only 4 of 8 configured targets, logging the other 4 as "not found available" even though, hours later with nothing else touched, those exact 4 sat unbought at their exact expected prices right at the top of the unscrolled grid) -- see the Status column for the full root-cause writeup; fixed three distinct bugs, not one | Done, re-verified live post-2026-08-01-rewrite (superseded by the 2026-09-17 fixes below): real run correctly bought the one target that was actually available (`中級レポート`, 125,000 credits, matching a real credit-balance delta) and correctly logged the other 7 as unavailable this cycle rather than misdiagnosing them as a catalog change. **2026-09-17 live debugging** (superpowers:systematic-debugging, root-caused from the user's own pasted `~/repo/scratchpad/ba_logs/daily.log` excerpt cross-checked against live screenshots -- see `ba_auto/tasks/shop_utils.py`'s own docstrings for the full writeup) found and fixed THREE separate real bugs, confirmed one at a time via five consecutive real `./ba_dailies.sh shop_common` runs against the account's actual daily catalog: (1) `select_targets_by_name`'s old row/col loop kept iterating with stale screenshot-implied positions after a successful buy, even though buying an item collapses its grid slot and shifts every later item forward -- fixed by re-scanning the current viewport from scratch (`_scan_current_view`) after every single buy instead of continuing the old pass; (2) even with that fix, two more confirmed-available targets were still missed, because the checkbox's own "checked" confirmation settles well before the rest of the grid's reflow/collapse animation visually finishes -- fixed with `config.SHOP_REFLOW_SETTLE_WAIT` (1.5s, not yet tuned down from a measured minimum), an explicit wait before the post-buy rescan; (3) the deepest bug: Common Shop's own tier-naming convention nests a shorter tier name inside a longer one (`上級強化珠`/Advanced is a literal substring of `最上級強化珠`/Highest, same for the レポート pair), and the original substring-only name match had no preference for an exact match, so scanning a "最上級..." card while its "上級..." sibling was still also a remaining target could resolve to the WRONG (shorter) target, compare against the wrong price, and permanently skip a genuinely-available card every run -- confirmed live: `最上級強化珠` sat available at its exact listed price across three consecutive real runs, never once bought, until fixed via `_match_name`'s exact-match-first resolution (substring fallback now prefers the longest/most-specific candidate). All 8 of today's configured targets were confirmed successfully bought across the debugging session's real runs (credit balance deltas matched each purchase's exact expected price sum throughout), and a final clean run correctly reported all 8 as sold out for the day with no further false skips. Earlier finding, still true: no visible purchase-count indicator (unlike the 青輝石 tab's "あと1回購入可能" labels). Also encountered but unrelated to this task: a stray already-open MomoTalk panel intercepted a `SHOP_ICON` click mid-session (closed manually); `navigation.is_on_subscreen`/`is_modal_open` don't recognize MomoTalk as a blocking state, same class of blind spot already documented for the old gem_shop dialog and cafe's rank-up cutscene -- not fixed here, out of scope for this incident, flagged for future attention if it recurs. **A FOURTH real bug found 2026-09-19**, two days after the above: the user reported the account still only showing 4 bought items on a given check. Re-investigating with the user's own cron logs (`~/ba_logs/daily.log`/`q4h.log`) found that, taken across both real `daily` cron fires each day, all 8 targets genuinely were being bought (the two fires split the 8 into complementary halves, each catching whatever the other one's timing missed) -- so the "only 4" symptom was a same-day timing snapshot, not a standing bug, EXCEPT for one real, separately-confirmed defect: `shop_utils.confirm_purchase` checked the purchase-confirmation overlay exactly once after clicking Buy and returned success as soon as that single read was "idle" -- with nothing distinguishing "confirmed and closed" from "the Buy click missed and the confirm dialog never opened at all". Caught live: a real cron run's log claimed "bought 4 item(s) for 303000 credits", but those exact 4 items were still sitting unbought at their base price hours later; re-running the task then genuinely bought them (confirmed via an exact matching credit-balance delta). Fixed by requiring the currency balance to have actually dropped by the exact expected spend once the overlay reports idle (re-read with the same `SHOP_PRICE_OCR_RETRIES` stray-misread-digit tolerance already used elsewhere in this module), returning `False` -- not a false "purchased" -- if it never does. Logically verified and syntax-checked, but **not yet live-confirmed against a real purchase specifically**: by the time the fix was written, all 8 of today's targets were already bought (from the same day's own earlier debugging), so no fresh purchasable item was available to exercise the new balance-check end-to-end; needs re-confirming the next time a genuinely fresh target is available (tomorrow's catalog reset, or Tactical Shop once its own separate price-misread bug -- see that row -- is fixed). **A FIFTH real bug found 2026-09-21**: the user reported only 4 of 8 targets bought again. Investigation (read-only OCR probes against the live screen, then a real `./ba_dailies.sh shop_common` run, then a before/after screenshot of a single scroll) found the remaining 4-5 "not found available" targets were genuinely available, correctly priced, purchasable, and even matched cleanly by a read-only replay of the exact same matching code -- yet the real production scan still missed them. Root cause: `config.SHOP_SCROLL_STEP_CLICKS`'s "advances by exactly one row" assumption (true when originally calibrated) no longer holds now that the catalog mixes in raw-material items whose two-line-wrapped names make some rows taller than others -- a single scroll call can overshoot a clean row boundary and land mid-row. Confirmed live: a before/after screenshot pair showed the grid jumping ~1.5-2 rows in one scroll, cutting card titles off above the viewport; every fixed-offset OCR/color read after that came back empty/garbled, and `_is_purchasable` read `False` across all 8 cells (vs. 7-8/8 `True` on a genuinely aligned frame) -- so the scan silently treated a torn frame's unreadable cells as "target not found" rather than "my crop coordinates are pointing at nothing". Fixed with `shop_utils._grid_looks_aligned`/`_ensure_grid_aligned`: after every scroll, verify at least one cell still reads purchasable before trusting the frame, nudging back up in small steps (`config.SHOP_SCROLL_REALIGN_NUDGE_CLICKS`/`_MAX_NUDGES`) if not, rather than scanning (and misreporting from) a misaligned frame. Only bites items that get reflowed out of the first two visible rows mid-run by earlier purchases in the same pass -- explains why the specific "missing 4" vary day to day rather than being a fixed set. Deployed and live-tested: see `plan.md` for the outcome. **That fix turned out NOT to be the actual cause of the recurring pattern** -- the next morning (2026-09-22) the exact same symptom recurred (real `daily` cron logs: the 03:38 run found ALL 8 targets "not found available" within 3 seconds -- far too fast for a genuine multi-row OCR scan -- and the 04:53 run then bought only the same familiar "top 4", leaving the same familiar "bottom 4" unbought). Re-investigated live: a read-only probe and a fresh interactive replay of the real matching code both found the "stuck" 4 sitting unscrolled at the top of the grid, correctly priced and purchasable -- confirming the scroll-alignment fix from the day before was never even being exercised for this failure. **A SIXTH real bug, found the same session**: while manually replaying the exact production logic step by step to gather evidence, a genuine in-game "ネットワークアクセスに問題が生じました" (network access problem) modal appeared over the shop grid mid-flow -- a real, first-party dialog, not anything this project's code triggers. It dims the whole grid, so every fixed-offset OCR/color read behind it comes back empty/garbled; and because a static dialog doesn't change frame to frame, `select_targets_by_name`'s own "screenshot unchanged" stop condition fires almost immediately, misreporting every remaining target as unavailable in a couple of seconds flat -- an exact match for the 03:38 run's suspiciously fast full-failure. Fixed by checking `navigation.is_modal_open` (the same screen-dimming probe used throughout this project) at the top of every scan pass, before trusting anything read from that frame; skipped targets from an aborted scan now get a distinct `blocked_by_modal` reason instead of the misleading "sold out this cycle, or catalog changed" message. Deliberately does not try to dismiss the dialog itself (this specific one's own "OK" button logs all the way out to the title screen, confirmed live when recovering from it via the `login` task) -- an unrecognized modal gets a loud, honest stop instead of a guessed keypress. **Confirmed live**: with the game recovered to a clean state, a real `./ba_dailies.sh shop_common` run then correctly found and bought all 4 remaining "stuck" targets for 303,000 credits (credit balance delta matched exactly), completing the full day's 8/8 targets (908,500 earlier + 303,000 = 1,211,500, the exact full catalog total) with zero items ever falsely reported unavailable. Whether the 2026-09-21 scroll-alignment fix was itself a real, separate bug (yes, confirmed independently that day) or was ever actually the cause of a cron-observed failure (unconfirmed either way) remains open -- both fixes are kept, since each addresses a distinct, independently-confirmed way a frame can be misread as "nothing here" when something really is. **A major debugging session, 2026-09-24**, per explicit user direction to "select, screenshot, never buy" until confident, then implement -- found and fixed FOUR more distinct real bugs using that exact safe methodology (repeatable selection-only runs via a scratchpad probe calling `select_targets_by_name` directly and always Cancel-ing, never Buy-ing, so free to iterate without touching real currency): (1) `中級レポート`'s price OCR deterministically misread "125,000" as "1250060" -- a digit-only tesseract whitelist forces the thousands-separator comma to be misclassified as a phantom digit instead of punctuation it's simply not allowed to output; fixed by adding ',' to `detector.read_int`'s whitelist (the existing digit-only filter afterward means this is a strict improvement with zero behavior change for comma-free reads, and also covers `CREDIT_BALANCE_OCR_RECT`'s routinely-multi-comma reads). (2) `_match_name` could not resolve internal single-character insertions/deletions ('初級レポート' read as '初級レポボート', '中級強化珠' read as '中級化珠') since plain substring containment requires unbroken contiguity; fixed with a two-stage fallback in `_match_name` -- strip whitespace, then fall back to `difflib.SequenceMatcher` similarity (`SIMILARITY_THRESHOLD = 0.75`, calibrated against real corrupted samples scoring 0.857-0.923 vs. unrelated catalog items topping out at 0.462). (3) A promotional ribbon/badge decoration on '初級強化珠' and '最上級強化珠' specifically (not the two middle tiers) overlapped the left ~40px of the name-OCR crop, corrupting the read badly enough that no matching stage could resolve it; fixed by measuring the decoration's real pixel extent (never past x=42 across both ribbon colors sampled) and shifting `SHOP_NAME_OCR_OFFSET`'s left edge by +45px. A second, subtler variant then surfaced on '最上級強化珠' alone: two small stray dark blobs in the crop's first ~5 vertical pixels (present even when the raw crop looked completely clean to the eye) confused psm 7's single-line assumption; fixed by shifting the top edge down 10px. (4) The deepest one: OCR corruption on a given cell was confirmed live to be STABLE for as long as the shop tab stays open (reading the identical wrong text 5/5 times with no navigation in between) but to reset to a genuinely different render on every fresh tab reopen (4/5 reopens gave different text, one exactly correct) -- so in-place rescanning (the 2026-09-22 fix's own retry loop) can structurally never help a stuck cell, only leaving and re-entering the tab can. Fixed by escalating to a bounded full reopen cycle (`config.SHOP_REOPEN_MAX_ATTEMPTS`) once in-place retries (`SHOP_NO_MATCH_RETRIES`, lowered from 10 to 2 once proven useless) are exhausted. **This reopen fix itself then caused a real under-purchase**, caught before it could recur: reopening turned out to also silently uncheck every checkbox ticked so far (checked state lives in the tab's own UI, not the account, until Buy is clicked), but `select_targets_by_name` kept trusting its own in-memory `selected` bookkeeping -- a real run logged all 8 targets matched (summing to the full 1,211,500) yet the actual balance only dropped by 125,000, the one target found right after the run's one reopen; every earlier "selected" target had gone back to unchecked in the live game without the code noticing. A same-day interim fix (move `selected` back into `remaining` on every reopen) closed the under-purchase but was pathological -- it forced the WHOLE batch to be re-earned on every single reopen, not just the stuck target, and a real run cycled through 20+ reopens chasing a shrinking-then-growing batch, eventually colliding with a real scheduled `q4h` cron fire on the same game window (confirmed via `~/ba_logs/q4h.log`'s own crash traceback landing the same second) and crashing on cascading screenshot corruption. **Final, correct fix**: `select_targets_by_name` now buys incrementally, committing (via a real, balance-verified `confirm_purchase` call) whatever's checked in the *current* batch immediately before any reopen, so a reopen can only ever cost re-scanning time, never already-real spending -- it now owns its own purchase confirmation end-to-end (`HANDLES_OWN_PURCHASE` flag; `run_shop_tab` defers to it instead of buying again on its behalf; Tactical Shop's `select_targets` path is completely unchanged). **Confirmed live end-to-end**: a real run bought 3 items (178,000 credits) in one batch after one reopen, correctly recognized the other 5 as already-bought-today (from the interim fix's own real purchases plus the concurrent cron's), and the day's cumulative real spend (125,000 + 908,500 + 178,000 = 1,211,500) exactly matches the full 8-item catalog total with zero double-buys or silent under-purchases. | | Tactical Shop | `module/shop/tactical_challenge_shop.py`, `module/shop/shop_utils.py` | `implement`, `goto_shop_by_name`, shared `get_item_position`/`ensure_choose`/`buy` | `ba_auto/tasks/shop_tactical.py`, `ba_auto/tasks/shop_utils.py` | `goto_shop_by_name`'s OCR swipe-search over the shop-tab list → fixed click (`config.SHOP_TAB_TACTICAL`): this account's tab list is only 7 entries and fits on screen with no scroll needed, confirmed live, so there's nothing to search for — not an OCR-avoidance shortcut. Same grid-position + price-OCR-verify + overlay-probe design as Common Shop, sharing `shop_utils.run_shop_tab` | Done. Live-tested with real purchases (both configured AP-recovery drinks bought; AP and tactical-coin balance changes matched exactly). **Regression noticed 2026-09-19, not yet investigated or fixed**: both configured targets (`初級栄養ドリンク(AP30)`/`中級栄養ドリンク(AP60)`) now consistently OCR-read price 50 for BOTH items (real prices 15/30), across multiple separate real runs -- always skipped as `price_mismatch`, `confirm_purchase` never reached. Same "50" for two different real prices points at either a real catalog/UI change (e.g. a display format change, or these items hitting some daily cap and showing a placeholder) rather than ordinary OCR noise, but unconfirmed -- flagged for a future live investigation, out of scope for the 2026-09-19 cafe/shop_common fixes. | | Lesson/Schedule | `module/lesson.py` | `implement`, `to_lesson_location_select`/`to_select_location`/`to_all_locations` (nav state machine), `get_lesson_region_num`/`switch_lesson_region_page`/`to_lesson_region` (paged region nav), `get_lesson_each_region_status`+`check_region_availability` (per-cell status via isometric-parallelogram pixel scan), `get_lesson_relationship_counts` (per-cell affection pip count via color count), `choose_lesson` (selection policy), `execute_lesson`/`to_location_info`/`start_lesson` (click cell -> info panel -> start -> result) | `ba_auto/tasks/lesson.py` | `picture.co_detect` -> `navigation.wait_for_state`-style bounded Enter-press loop (see below); the reference's paged-arrow region nav (needing OCR to know current position) -> this client renders the 12 regions as a scrollable list instead, which only ever settles at two scroll positions (`config.LESSON_REGION_ROW_Y`), so navigation is direct index-based clicking with nothing to OCR-locate; the reference's isometric `Parallelogram`/`Triangle` per-cell scan (tuned to the reference's own screen layout) -> reading each portrait's heart-shaped affection badge via a dedicated OCR path (`detector.read_int_on_heart_badge`) needs no isometric geometry at all | Done. Config-driven scope only in the sense of the *policy* (affection-first selection, sweep every unlocked region until tickets/lessons run out, no ticket purchasing, no favor-student targeting) -- unlike shop, no user-specific target list was needed since the reference's own `lesson_region_name.JP` (embedded directly in its `default_config.py`, not externally fetched) already names all 12 regions, used here only for logging. Live-tested for real: 5 real tickets spent across 3 regions with correct outcomes (ticket count, cleanup navigation, home-screen return all verified). Two real bugs were found and fixed from that run -- see below and `plan.md`'s Lesson phase. **Regression fix (Phase 12 follow-up)**: `LESSON_TICKET_OCR_RECT`'s left edge clipped in a stray katakana fragment next to the first digit, making tesseract drop the whole leading digit (`"7/7"` -> `"/7"`) and aborting every run outright; fixed by tightening the rect, re-validated with a full real run (7 tickets spent, correct count re-read after every schedule, clean stop and home-screen return). **Regression fix (Phase 12 follow-up #2)**: clicking the schedule icon doesn't always land on the Location Select list -- the game can resume directly on whichever region's per-region isometric map was last open (confirmed live: a previous run's Ctrl-C interruption left it stuck there, breaking `_open_region_grid` for every region in the next run identically). Fixed via a new `lesson._ensure_location_select_list` recovery check (detects the per-region map's own "すべてのスケジュール" button already showing before any row's been clicked, and returns via the back button if so); validated by deliberately reproducing the stuck state and confirming a real run recovered, spent the account's real remaining ticket, and finished cleanly. **Selection-policy rewrite (Phase 12 follow-up #3, 2026-07-13)**: per explicit user direction, replaced "always pick the single highest affection value" with a tiered min/max-farming priority -- any 3-student cell anywhere first, then any 2-student cell anywhere, then single-student cells sorted lowest-affection-first. This needs the whole board's state before deciding, not just the current region's, so the flow is now scan-all-then-execute (`_scan_all_regions`/`_build_priority_queue`/`_run_queue`) rather than the old per-region sweep-and-pick-best loop (`_find_best_cell`/`_sweep_region`, both removed). Verified offline against a synthetic board (correct tier ordering) and live against the real board via a zero-cost scan-only call (69 real schedulable cells found across 12 regions -- 7 triples, 30 doubles, 32 singles -- correctly bucketed and the singles tail exactly ascending by real affection value), with zero tickets spent since the account was fully out that day. Real ticket-spending execution (`_run_queue` actually running schedules) is not yet live-tested -- deferred to the user once tickets regenerate. | | Hard story AP sweep | `module/explore_tasks/sweep_task.py`, `module/explore_tasks/task_utils.py` | `sweep_hard_task` (main flow, 20 AP/hit, 3x cap), `to_hard_event`/`to_region` (nav), `start_sweep`'s named-outcome contract (shared with story_sweep.py's, ported the same way via `navigation.wait_for_state`) | `ba_auto/tasks/story_sweep_hard.py` | Reference's `unfinished_hard_tasks` persisted config list -> a fixed, user-supplied priority-ordered list (`config.HARD_STORY_SWEEP_TARGETS`: 18-3, 30-3, 27-3, 28-3, 17-3, 13-3, 23-2, 16-1, 20-3, 17-2, 15-2, 14-3, 10-3, 14-2, 7-1, 5-3, 4-1), swept in that fixed order every run rather than popped/persisted across runs; each target uses MAX (capped at 3x by the game itself), matching `config.STORY_SWEEP_TARGETS`'s own MAX convention. Reuses story_sweep.py's region-nav/stage-row-OCR/modal/result-watching machinery where the Hard screen's layout matches (region browser, stage list, sweep modal) rather than re-deriving it. New: a campaign-active guard with no reference equivalent at all (not present anywhere in baas-reference) -- per explicit user direction, OCRs for the pink "キャンペーン中" text (confirmed live on both the home screen and the Hard region browser) before spending any AP, skipping the whole run if no campaign is active unless an explicit override is passed | Implemented, not yet live-tested. Live investigation on nik-gpu (2026-07-20) confirmed: the Hard tab (`config.HARD_TAB`) sits atop the same region-browser screen story_sweep.py already opens, and stays selected across region navigation (no re-click needed between regions, unlike the reference's own per-move to_hard_event re-assertion); Hard's stage list always shows exactly 3 fixed rows (missions 1-3, no "-A" bonus, no scrolling -- `config.HARD_STAGE_ROWS_Y`, replacing story_sweep's scroll+OCR row search entirely); the stage-info modal is pixel-identical to story_sweep's own (all of `SWEEP_MAX_BUTTON`/`SWEEP_START_BUTTON`/`STAGE_MODAL_CLOSE_BUTTON`/confirm-dialog constants reused directly, confirmed via a real zero-AP-spent dry run: opened the confirm dialog, screenshotted it, cancelled). The in-panel "キャンペーン中" campaign banner (`config.HARD_CAMPAIGN_BADGE_RECT`) was confirmed live on two different regions (30 and 29) with identical text ("任務Hardで獲得できる報酬量が2倍(+100%)になっています"), read as account-wide rather than per-region -- chosen over the home screen's own ambiguous "キャンペーン中" badge (which can point at unrelated concurrent campaigns) as the actual gate signal. **Also confirmed live**: this modal has the exact same two-stacked-action-button hazard bounty.py hit for real (cyan 掃討開始 "start sweep" directly above gold 任務開始 "start mission", a real manual battle, both showing an identical AP-cost preview) -- ported bounty.py's own `_confirm_dialog_is_sweep` OCR text-gate proactively here rather than waiting to discover the same hazard live a second time. **Live-tested for real across three rounds (2026-07-20/21) -- see `plan.md` Phase 21 for the full writeup.** Multiple genuine Hard sweeps confirmed for real (H18-3/H30-3 both fully exhausted their daily 3/3 clears; six more targets swept for real in the final run). Five real bugs found and fixed: (1) a navigation-cascade bug where every exit path's leftover "if on subscreen, press Escape" cleanup backed out of the region browser entirely after every single target, breaking all subsequent targets -- fixed via a new `_ensure_hard_screen` self-heal check per loop iteration, replacing the removed Escape presses; (2) a false-negative MAX-click verification that wrongly aborted 11 real, affordable targets once AP dropped below 40 (MAX legitimately caps at 1x there, indistinguishable from "click failed") -- fixed by dropping the verification gate and trusting the downstream confirm-dialog checks instead; (3) a genuinely new real-money hazard found live: a stage already at 0/3 remaining today still shows a clickable 入場 button, and sweeping it raises a real "spend 40 gems to refill today's clears?" dialog -- fixed with a two-layer guard (`_remaining_sweeps_today`'s pre-check OCR on the modal's own count field, `_is_challenge_count_alert`'s OCR fallback on the dialog text, both declining via Escape, never a positional click; deliberately reproduced twice with zero gems spent, confirmed by screenshot); (4) a cosmetic `"unrecognized_state"` misreport on every real sweep, root-caused to the same terminal-state gap event_sweep.py hit historically (post-sweep flow can return to the bare region browser instead of leaving the modal open) -- fixed by porting event_sweep.py's own `clicked_any`-gated second `ends` condition; (5) a still-not-fully-root-caused region-navigation flakiness (30->27 failed identically across two runs) mitigated with a short OCR-read retry (`REGION_READ_RETRIES`), not yet independently re-confirmed live since account AP was exhausted (down to 6) by that point in the session. | diff --git a/ba_auto/tasks/shop_utils.py b/ba_auto/tasks/shop_utils.py index bba155b..25dfec0 100644 --- a/ba_auto/tasks/shop_utils.py +++ b/ba_auto/tasks/shop_utils.py @@ -28,8 +28,16 @@ already gives. Tactical Shop (`select_targets`, unchanged) has never shown this symptom -- one row, two items, no observed reordering -- so it stays on the simpler position-based path rather than migrating speculatively. """ +import difflib + from ba_auto import detector, navigation +# _match_name's last-resort fallback for OCR text with an internal inserted/ +# dropped character that breaks plain substring containment (see that +# function's own docstring for the live incident and real similarity scores +# this was calibrated against). +SIMILARITY_THRESHOLD = 0.75 + def _checkbox_center(config, row, col): return config.SHOP_ITEM_COL_X[col], config.SHOP_ITEM_ROW_Y[row] @@ -129,17 +137,58 @@ def _match_name(name_text, remaining): An exact match is checked first and always wins outright. The substring fallback (for a genuinely imperfect OCR read missing/adding - a stray character) still exists, but picks the LONGEST candidate -- - the more specific name -- to minimize the same class of collision. + a stray character at the START or END) still exists, but picks the + LONGEST candidate -- the more specific name -- to minimize the same + class of collision. + + **Real live under-match, 2026-09-24**: even with a clean, purchasable, + correctly-priced card on screen, several targets still went unmatched + every single pass, forcing an unnecessary scroll (and from there into + the separate scroll-misalignment failure -- see _grid_looks_aligned). + Root-caused with a per-cell verbose trace against the live game: the + OCR noise wasn't just a prefix/suffix character any more -- it was an + INTERNAL corruption breaking contiguity, e.g. '初級レポート' (12,500) + read as '初級レポボート' (a phantom character inserted mid-string) and + '中級強化珠' (40,000) read as '中級化珠' (a real character, '強', + dropped from the middle) -- both stable/repeatable across several + consecutive fresh screenshots of the same still-unclicked cell, not a + one-off flake. Neither direction of plain substring containment can + ever match these: the target string literally does not appear + contiguously inside the corrupted OCR text, or vice versa. A separate + case, '中級レポート' read as '。 日 中級レポー ト', had a plain space + inserted before the final character -- also breaks substring + containment, but is fixed for free by stripping whitespace before any + comparison (real target names never contain spaces). + + Fixed with a two-stage fallback: (1) strip whitespace before every + comparison stage above; (2) if plain substring containment still finds + nothing, fall back to whole-string similarity (difflib.SequenceMatcher + ratio) against every remaining candidate, taking the best match if it + clears SIMILARITY_THRESHOLD. Confirmed against this incident's own + real corrupted readings: '初級レポボート' vs '初級レポート' scores + ~0.92, '中級化珠' vs '中級強化珠' scores ~0.89 -- both comfortably + above threshold, while unrelated/heavily-garbled reads (e.g. a + genuinely different item's name, or a cell read as pure noise) score + well below it in the same live samples. This is a fallback of last + resort, not a replacement for the stages above: an exact or clean + substring match is always preferred, and this project's usual + price-match safety net still guards the actual purchase regardless of + which stage resolved the name. """ if not name_text: return None - if name_text in remaining: - return name_text - candidates = [n for n in remaining if n in name_text or name_text in n] - if not candidates: - return None - return max(candidates, key=len) + normalized = "".join(name_text.split()) + if normalized in remaining: + return normalized + candidates = [n for n in remaining if n in normalized or normalized in n] + if candidates: + return max(candidates, key=len) + best_name, best_ratio = None, 0.0 + for n in remaining: + ratio = difflib.SequenceMatcher(None, normalized, n).ratio() + if ratio > best_ratio: + best_name, best_ratio = n, ratio + return best_name if best_ratio >= SIMILARITY_THRESHOLD else None def _scan_current_view(driver, config, remaining): @@ -240,7 +289,7 @@ def _ensure_grid_aligned(driver, config): return False -def select_targets_by_name(driver, config, targets): +def select_targets_by_name(driver, config, targets, *, balance_rect): """Name-OCR-based sibling of select_targets, for grids where a bought (or sold-out) item sinks to the bottom and shifts everything else's position (see this module's own docstring and config.SHOP_PRICE_OCR_OFFSET's @@ -311,12 +360,114 @@ def select_targets_by_name(driver, config, targets): available", so a run cut short this way is never confused with the genuinely-benign sold-out/escalated-price case in the log or by anyone reading it later. + + **Real live OCR instability, 2026-09-24**: even with the fixes above, + the exact same still-visible, still-purchasable, still-unscrolled cell + would sometimes read cleanly and sometimes come back corrupted -- + confirmed live across five consecutive selection-only test runs (never + clicking Buy, so free to repeat) that which of the 8 targets failed to + match varied run to run, not a fixed set tied to one item's name. A + single full-pass "nothing matched" was previously treated as proof the + remaining targets weren't in the current view at all, immediately + triggering a scroll -- compounding the problem, since scrolling has its + own separate fragility (see _grid_looks_aligned). First fix attempt: + retry a "nothing matched" pass up to `config.SHOP_NO_MATCH_RETRIES` + times, waiting between attempts, before giving up and scrolling. + + **That in-place retry turned out not to work, confirmed live the same + session**: reading the exact same still-unclicked cell 5 times in a row + without leaving the shop tab gave the identical wrong text every time + (e.g. '中級化珠' for '中級強化珠', all 5 reads) -- the corruption is + stable for as long as the tab stays open, not a per-frame flake, so + re-reading in place can never see anything different. Fully leaving and + re-entering the shop tab, by contrast, gave a DIFFERENT reading on + 4 of 5 attempts in the same live test (one of them exactly correct) -- + proving the render itself resets on each fresh tab-open, even though it + doesn't change while sitting on it. Fixed by escalating to a full + return-to-home-and-reopen cycle (bounded by + `config.SHOP_REOPEN_MAX_ATTEMPTS`) once the in-place retries are + exhausted, before ever falling back to scrolling -- most of that live + test's reopen variants (e.g. '中級衝化珠', '中級測化珠', both a single + substituted character) already clear `_match_name`'s fuzzy-similarity + threshold on their own, so a reopen or two is normally enough. A + successful match resets `reopen_attempts` back to 0, so a later, + unrelated stuck target still gets its own full budget. + + **Real live under-purchase, 2026-09-24, same day**: leaving the shop + tab does not just reset the render -- it also unchecks every checkbox + already ticked this call, since checked state lives in the tab's own + UI, not the account, until Buy is actually clicked. The first version + of the reopen fix above didn't account for this: a real cron-triggered + run logged all 8 targets as matched (zero skips, `selected` summing to + the full 1,211,500) after one reopen mid-scan, yet the real purchase + only deducted 125,000 -- exactly one target's price, the one found + right after that reopen. Every target confirmed-checked *before* the + reopen had silently gone back to unchecked in the live game, but + `selected` still listed them as done, so confirm_purchase clicked Buy + against a cart that was actually down to one item. Confirmed via the + account's own real credit balance delta. + + **First fix (moving already-selected entries back into `remaining` on + every reopen) was correct but pathological**: it eliminated the + under-purchase, but since a reopen now had to re-earn EVERY target + checked so far, not just the one that got stuck, one flaky match + anywhere could force the whole session to redo several already-solved + targets -- confirmed live: a real run cycled through 20+ consecutive + reopens (each one individually reasonable) chasing a shrinking-then- + growing batch, and burned enough wall-clock time doing so that it + eventually collided with a real scheduled `q4h` cron fire on the same + game window, corrupting screenshots for both processes and crashing. + + **Real fix: buy each batch before it can be invalidated**, rather than + holding the whole cart in memory across a reopen. `current_batch` + (this session's checked-but-not-yet-bought items) is committed via a + real, balance-verified `confirm_purchase` call -- exactly like a normal + end-of-run purchase, just potentially several times per call instead of + once -- immediately before any reopen, and once more at the very end. + A committed batch moves into `purchased` and can never be undone by a + later reopen, since the real account balance already reflects it. This + function now owns its own purchase confirmation end-to-end (hence the + required `balance_rect` and the `HANDLES_OWN_PURCHASE` flag below) -- + `run_shop_tab` no longer buys on this path's behalf. If a batch + purchase itself fails to verify, scanning stops immediately (further + spending on top of an already-unclear balance state would only make a + manual check harder), and every still-remaining target is reported + with a distinct 'unrecognized_state' reason rather than the + genuinely-benign "not found available". """ remaining = {name: price for name, price in targets} - selected = [] + current_batch = [] + purchased = [] prev_shot = None blocked_by_modal = False - for _ in range(config.SHOP_NAME_SCAN_MAX_STEPS): + purchase_failed = False + scroll_steps = 0 + reopen_attempts = 0 + + def commit_current_batch(): + """Buy whatever's checked right now, before anything (a reopen) can + invalidate it. Returns False if the purchase itself doesn't verify + (see confirm_purchase) -- a real, distinct failure from "nothing to + buy", which the caller must stop on rather than paper over.""" + nonlocal current_batch, purchase_failed + if not current_batch: + return True + balance_before = detector.read_int(balance_rect) + if balance_before is None: + print("[shop] could not read balance before committing this batch -- stopping without buying") + purchase_failed = True + return False + spent = sum(price for _, price in current_batch) + if not confirm_purchase(driver, config, balance_rect=balance_rect, balance_before=balance_before, expected_spend=spent): + print(f"[shop] purchase confirmation for this batch ({len(current_batch)} item(s), {spent}) did not resolve to a verified balance change -- stopping, check the game manually") + purchase_failed = True + return False + print(f"[shop] bought {len(current_batch)} item(s) for {spent} this batch: {[name for name, _ in current_batch]}") + purchased.extend(current_batch) + current_batch = [] + return True + + while scroll_steps < config.SHOP_NAME_SCAN_MAX_STEPS: if not remaining: break if navigation.is_modal_open(driver): @@ -329,27 +480,62 @@ def select_targets_by_name(driver, config, targets): break prev_shot = shot + no_match_streak = 0 + stuck = False while remaining: match = _scan_current_view(driver, config, remaining) if match is None: + no_match_streak += 1 + if no_match_streak < config.SHOP_NO_MATCH_RETRIES: + driver.wait(0.5) + continue + stuck = True break - selected.append((match, remaining.pop(match))) + no_match_streak = 0 + reopen_attempts = 0 + current_batch.append((match, remaining.pop(match))) - if remaining: - driver.scroll(*config.SHOP_SCROLL_POINT, "down", clicks=config.SHOP_SCROLL_STEP_CLICKS) - driver.wait(0.3) - if not _ensure_grid_aligned(driver, config): - print("[shop] grid scroll landed on a misaligned frame and could not realign -- stopping scan early (remaining targets may still be genuinely available)") + if not remaining: + break + + if stuck and reopen_attempts < config.SHOP_REOPEN_MAX_ATTEMPTS: + reopen_attempts += 1 + print(f"[shop] no match after {config.SHOP_NO_MATCH_RETRIES} in-place rescans -- reopening the shop tab for a fresh render (attempt {reopen_attempts}/{config.SHOP_REOPEN_MAX_ATTEMPTS})") + if not commit_current_batch(): break + navigation.return_to_home(driver) + driver.click(*config.SHOP_ICON) + driver.wait(2) + driver.click(*config.SHOP_TAB_COMMON) + driver.wait(1.5) + prev_shot = None + continue + + driver.scroll(*config.SHOP_SCROLL_POINT, "down", clicks=config.SHOP_SCROLL_STEP_CLICKS) + driver.wait(0.3) + scroll_steps += 1 + if not _ensure_grid_aligned(driver, config): + print("[shop] grid scroll landed on a misaligned frame and could not realign -- stopping scan early (remaining targets may still be genuinely available)") + break + + if not purchase_failed: + commit_current_batch() + skipped = [] for name in remaining: - if blocked_by_modal: + if purchase_failed: + print(f"[shop] '{name}' scan stopped after a batch purchase failed to verify -- NOT confirmed unavailable, check the game manually") + skipped.append((name, "unrecognized_state")) + elif blocked_by_modal: print(f"[shop] '{name}' scan aborted by a blocking dialog -- NOT confirmed unavailable, check the game manually") skipped.append((name, "blocked_by_modal")) else: print(f"[shop] '{name}' not found available at its expected price {remaining[name]} -- skipping (sold out this cycle, or catalog changed)") skipped.append((name, "not_available")) - return selected, skipped + return purchased, skipped + + +select_targets_by_name.HANDLES_OWN_PURCHASE = True def confirm_purchase(driver, config, *, balance_rect, balance_before, expected_spend): @@ -419,6 +605,13 @@ def run_shop_tab(driver, config, *, tab_button, targets, balance_rect, currency_ select_targets_by_name instead, for its (name, price) targets -- see that function's docstring for why Common Shop needs name-based identification specifically. + + `select_fn` normally only selects (checkbox state); this function buys + once at the end. select_targets_by_name is the one exception, flagged + via its own `HANDLES_OWN_PURCHASE` attribute -- it needs to buy + incrementally in its own batches (see its docstring's 2026-09-24 + entries for why one end-of-scan purchase isn't safe for it), so this + function defers to whatever it already bought instead of buying again. """ if not targets: print(f"[shop] no targets configured for {currency_label}, nothing to do") @@ -440,6 +633,27 @@ def run_shop_tab(driver, config, *, tab_button, targets, balance_rect, currency_ print(f"[shop] insufficient {currency_label}: have {balance}, need {total_cost} -- stopping, nothing bought") return "inadequate_assets" + if getattr(select_fn, "HANDLES_OWN_PURCHASE", False): + # select_targets_by_name buys as it goes (possibly in several + # batches, each independently balance-verified) rather than + # returning an unbought selection for this function to purchase in + # one shot -- see that function's own docstring for why a single + # end-of-scan purchase isn't safe for it. `purchased` below is + # already real, confirmed spending, not a pending cart. + purchased, skipped = select_fn(driver, config, targets, balance_rect=balance_rect) + if not purchased: + if any(reason == "unrecognized_state" for _, reason in skipped): + print("[shop] a batch purchase failed to verify partway through -- stopping, check the game manually") + return "unrecognized_state" + print("[shop] nothing purchased (all targets skipped)") + return "nothing_selected" + spent = sum(price for _, price in purchased) + msg = f"[shop] bought {len(purchased)} item(s) for {spent} {currency_label}" + if skipped: + msg += f", skipped {len(skipped)}" + print(msg) + return "purchased" + selected, skipped = select_fn(driver, config, targets) if not selected: print("[shop] nothing selected (all targets skipped), cancelling")