Compare commits

..

3 Commits

Author SHA1 Message Date
80c1a645b8 feat: implement grid alignment checks for shop item selection to prevent misreads after scrolling 2026-09-21 19:21:22 +09:00
19e39ec8b2 feat: detect Steam sign-out during login, stop cascading crashes after a missing game window
Root-caused 3 consecutive real cron failures (one q4h, two daily, all
2026-09-20) to Steam itself sitting signed out on its own account
sign-in dialog -- not a Blue Archive client issue, and not something any
amount of retrying/relaunching the game can fix.

login._steam_needs_sign_in (new detector.read_text_white_on_dark, since
the dialog's own light-on-dark text is the opposite OCR polarity this
project usually assumes) recognizes that exact state and turns the
generic "game window never appeared" warning into a specific, actionable
one instead of requiring a fresh screenshot investigation every time it
recurs.

ba_daily.py's _run_sequence now checks driver.window_exists() after every
task (except the last, and except exit_game) 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 two tasks deep in a stack
trace. Still propagates as a real FAILED exit code -- this project
deliberately never swallows a genuine crash.

Confirmed live: reproduced the original failure via a real
./ba_cron_run.sh q4h run while Steam was still genuinely signed out, then
verified the new diagnostic fired correctly and the sequence aborted
immediately. After the user signed back into Steam, ran the actual daily
recovery preset end-to-end (all 17 tasks, ~24 minutes, exit 0) to catch
up the account's daily claims.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-20 08:51:06 +09:00
6a800f731b feat: batch cafe sparkle patting, add diagnostic capture, verify shop purchases via balance delta
Cafe: find_cafe_sparkles (replacing find_cafe_sparkle) matches every
sparkle in one screenshot instead of the single best one; _pat_current_view
now pats a whole batch per frame and stops after two consecutive empty
rounds instead of a fixed 15-click budget. Cuts an empty room's farming
time from ~100s to ~14s in live testing. _dismiss_rank_up_if_shown also
saves a diagnostic screenshot on its own final failure, since the real
"screen not confirmed" cron failures (100% reproducible in logs, 0%
reproducible live across several debugging sessions) need hard evidence
from the next real occurrence.

shop_utils.confirm_purchase now requires the currency balance to have
actually dropped by the expected spend before reporting success, instead
of trusting a single post-click overlay read -- closes a real false
positive caught live where a cron run logged a successful purchase that
never actually happened.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-19 18:18:51 +09:00
8 changed files with 386 additions and 80 deletions

View File

@ -77,10 +77,17 @@ MISSION_DAILY_GEM_CLAIM_BUTTON = (1400, 1095)
CAFE_ICON = (165, 1100) CAFE_ICON = (165, 1100)
CAFE_ROOM_SWITCH = (190, 160) CAFE_ROOM_SWITCH = (190, 160)
CAFE_INCOME = (1780, 1105) CAFE_INCOME = (1780, 1105)
# Max sparkle-detection attempts per room (hits and misses both count -- # _pat_current_view (2026-09-19 redesign, per explicit user direction):
# sparkles are on a per-student cooldown, so most checks legitimately find # screenshot once, pat every sparkle found in that one frame, screenshot
# nothing and the loop keeps polling rather than giving up after one miss). # again, repeat -- stopping once CAFE_EMPTY_ROUNDS_TO_STOP consecutive
CAFE_MAX_CLICKS_PER_ROOM = 15 # rounds each find zero sparkles, rather than the old fixed budget of
# individual click attempts (which re-screenshotted and re-matched once per
# pat even when several sparkles were already visible in the same frame).
# CAFE_MAX_SPARKLE_ROUNDS is a hard safety cap on top of that -- the real
# stopping condition is the consecutive-empty-rounds count, this just
# bounds worst case if that somehow never triggers.
CAFE_MAX_SPARKLE_ROUNDS = 10
CAFE_EMPTY_ROUNDS_TO_STOP = 2
CAFE_SPARKLE_TEMPLATE = os.path.join(ASSET_DIR, "cafe_sparkle.png") CAFE_SPARKLE_TEMPLATE = os.path.join(ASSET_DIR, "cafe_sparkle.png")
# A pat that crosses an affection-rank threshold shows a full-screen "絆ラン # A pat that crosses an affection-rank threshold shows a full-screen "絆ラン
# クアップ!" (Bond Rank Up!) cutscene with no cafe header visible at all. # クアップ!" (Bond Rank Up!) cutscene with no cafe header visible at all.
@ -833,14 +840,31 @@ SHOP_NAME_OCR_OFFSET = (-75, -105, 160, -45)
# threshold, set well clear of both clusters. # threshold, set well clear of both clusters.
SHOP_BUTTON_PROBE_OFFSET = (90, 210) SHOP_BUTTON_PROBE_OFFSET = (90, 210)
SHOP_BUTTON_PURCHASABLE_MIN_BLUE_TINT = 40 SHOP_BUTTON_PURCHASABLE_MIN_BLUE_TINT = 40
# One driver.scroll(..., clicks=N) call at this many clicks reliably # One driver.scroll(..., clicks=N) call at this many clicks USED TO reliably
# advances the grid by exactly one row (confirmed live: the item at # advance the grid by exactly one row (confirmed live at the time: the item
# row_y[1] before the scroll reappears at row_y[0] after it, same name and # at row_y[1] before the scroll reappeared at row_y[0] after it, same name
# price) -- so select_targets_by_name can scan two full rows per # and price). **No longer trustworthy alone** (found live 2026-09-21, see
# screenshot and scroll by exactly one row's worth between reads, without # script_error.md and shop_utils._grid_looks_aligned's own docstring): once
# skipping or double-reading a row. # the catalog grew to include raw-material items whose names wrap to two
# lines, row heights are no longer uniform, so this fixed click count can
# overshoot past a clean row boundary and land mid-row. Kept as the primary
# scroll step (still correct most of the time -- the catalog's stone/report
# rows are still single-line and uniform), but select_targets_by_name now
# verifies the landing frame is actually row-aligned before trusting any
# read from it, rather than assuming this value alone guarantees that.
SHOP_SCROLL_STEP_CLICKS = 3 SHOP_SCROLL_STEP_CLICKS = 3
SHOP_SCROLL_POINT = (1310, 485) SHOP_SCROLL_POINT = (1310, 485)
# Realignment nudge used when a scroll lands mid-row (see
# shop_utils._grid_looks_aligned/_ensure_grid_aligned): a single small
# scroll-up click, retried up to this many times, searching for a frame
# where at least one cell's buy button reads purchasable again. 1 click is
# deliberately much smaller than SHOP_SCROLL_STEP_CLICKS's 3, so repeated
# nudges can land somewhere near a row boundary that a single big step
# jumped straight over; 5 nudges (5px-scroll-equivalent clicks) gives
# comfortable headroom to reverse a single 3-click overshoot. Not yet tuned
# against a measured per-click pixel distance -- first live value to try.
SHOP_SCROLL_REALIGN_NUDGE_CLICKS = 1
SHOP_SCROLL_REALIGN_MAX_NUDGES = 5
# Hard cap on scroll steps for select_targets_by_name, independent of its # Hard cap on scroll steps for select_targets_by_name, independent of its
# own "screenshot unchanged after scrolling" bottom-of-list detection -- # own "screenshot unchanged after scrolling" bottom-of-list detection --
# the live catalog was ~6 rows deep (24 slots) when this was built, so this # the live catalog was ~6 rows deep (24 slots) when this was built, so this
@ -1409,7 +1433,7 @@ BOUNTY_SWEEP_CONFIRM_TEXT_RECT = (605, 505, 1320, 615)
# visual differences (see cafe.py's CLAIM_DISABLED_RGB, stamina.py's # visual differences (see cafe.py's CLAIM_DISABLED_RGB, stamina.py's
# MISSION_CLAIM_PROBE) rather than building new template assets, since every # MISSION_CLAIM_PROBE) rather than building new template assets, since every
# state below reduces to a clean, high-contrast flat color rather than a # state below reduces to a clean, high-contrast flat color rather than a
# complex shape needing find_cafe_sparkle-style matching. # complex shape needing find_cafe_sparkles-style matching.
# #
# RECALIBRATED 2026-07-29 after a real Blue Archive client UI update changed # RECALIBRATED 2026-07-29 after a real Blue Archive client UI update changed
# this screen's whole layout. Re-verified live on nik-gpu the same day (see # this screen's whole layout. Re-verified live on nik-gpu the same day (see
@ -1685,6 +1709,27 @@ LOGIN_UPDATE_DIALOG_TEXT_RECT = (600, 420, 1320, 465)
LOGIN_UPDATE_DIALOG_KEYWORD = "ダウンロード" LOGIN_UPDATE_DIALOG_KEYWORD = "ダウンロード"
LOGIN_UPDATE_OK_BUTTON = (1040, 851) LOGIN_UPDATE_OK_BUTTON = (1040, 851)
# Steam's OWN account sign-in dialog -- not a Blue Archive client screen at
# all. Real incident, 2026-09-20: three consecutive real cron runs (one
# q4h, two daily) all failed identically with "game window never appeared
# after launch" -- root-caused by a live screenshot showing Steam itself
# sitting on its own username/password + QR-code sign-in dialog the entire
# time, meaning it had been signed out and every launch attempt opened
# Steam but could never reach the point of starting BlueArchive.exe at all.
# No amount of retrying/relaunching the GAME can get past this -- it
# needs a real person to sign in, so `login._steam_needs_sign_in` uses this
# purely as a diagnostic aid, to turn a generic "never appeared" warning
# into an actionable one instead of requiring a fresh screenshot
# investigation every time it recurs. Rect crops the "SIGN IN WITH ACCOUNT
# NAME" label specifically (confirmed live via a saved screenshot from the
# actual incident, scratchpad/stuck_launch_check.png at the time) --
# light-blue-on-dark-navy, the opposite polarity of this project's usual
# OCR assumption, read via detector.read_text_white_on_dark. Confirmed live
# the label's own grayscale luminance tops out at 126, well under that
# function's default 150 threshold, so this passes a lower one explicitly.
LOGIN_STEAM_SIGNIN_TEXT_RECT = (640, 490, 960, 520)
LOGIN_STEAM_SIGNIN_OCR_THRESHOLD = 90
# Battle Pass (バトルパス) -- ported from baas-reference's # Battle Pass (バトルパス) -- ported from baas-reference's
# module/collect_pass_reward.py. See ba_auto/tasks/battle_pass.py's module # module/collect_pass_reward.py. See ba_auto/tasks/battle_pass.py's module
# docstring for the full reference-mapping writeup; constants below were # docstring for the full reference-mapping writeup; constants below were

View File

@ -18,7 +18,7 @@ SPARKLE_SHOT_PATH = os.path.join(config.SCRATCHPAD_DIR, "cafe_live.png")
# Real-usage bug (2026-07-16, reported live with a screenshot): when two # Real-usage bug (2026-07-16, reported live with a screenshot): when two
# students stand close together, (75, 47) overshoots past the intended # students stand close together, (75, 47) overshoots past the intended
# target's own head and lands on a DIFFERENT, closer student's body instead # target's own head and lands on a DIFFERENT, closer student's body instead
# -- the click doesn't register as a pat there, so find_cafe_sparkle() just # -- the click doesn't register as a pat there, so find_cafe_sparkles() just
# matches the same still-showing sparkle again next iteration, repeatedly # matches the same still-showing sparkle again next iteration, repeatedly
# clicking nearly the same point for the rest of the room's budget (confirmed # clicking nearly the same point for the rest of the room's budget (confirmed
# live: 13 of 16 "pats" in one run clustered within a ~10px box, all # live: 13 of 16 "pats" in one run clustered within a ~10px box, all
@ -60,32 +60,75 @@ def _masked_template(template):
return cv2.merge([mask_plane, mask_plane, mask_plane]) return cv2.merge([mask_plane, mask_plane, mask_plane])
def find_cafe_sparkle(image=None): # How close two sparkle detections' click points need to be (in px, both
"""`image` optionally supplies an already-decoded frame instead of # axes) to be treated as the same real sparkle rather than two distinct
taking a fresh screenshot -- for cafe.py's per-iteration capture, shared # ones -- the multi-scale sweep in find_cafe_sparkles commonly matches one
with that same iteration's navigation.is_header_bar_visible rank-up # real sparkle at several neighboring scales with slightly different
check (see cafe.py's _pat_current_view).""" # computed centers, and those need collapsing into a single click.
# Conservative starting guess, not yet live-calibrated against two genuinely
# distinct sparkles sitting close together: comfortably smaller than the
# template's own on-screen size across the sampled scales (70x93 at 1.0x,
# so ~42x56 to ~84x112 across SPARKLE_SCALES), on the assumption that two
# different students' sparkles won't normally render closer together than
# that. Tighten if a live run is ever seen merging two real sparkles into
# one click, or loosen if the same sparkle still produces duplicate clicks.
SPARKLE_DEDUPE_DISTANCE = 50
def find_cafe_sparkles(image=None):
"""Every distinct sparkle currently visible in one frame, as
`(x, y, score)` tuples sorted by descending score -- not just the
single best match `find_cafe_sparkle` used to return. `image`
optionally supplies an already-decoded frame instead of taking a fresh
screenshot -- for cafe.py's per-round capture, shared with that same
round's navigation.is_header_bar_visible rank-up check (see cafe.py's
_pat_current_view).
Finding every sparkle in one screenshot, instead of just the best one,
lets the pat loop click through all of them from a single capture
before re-screenshotting, rather than paying for a fresh scrot +
multi-scale template match per individual pat when several sparkles are
already visible at once. Replaces the original single-match
find_cafe_sparkle (2026-09-19, per explicit user direction) as part of
the same change that made _pat_current_view stop after two consecutive
empty screenshots instead of a fixed click-attempt budget -- see that
function's own docstring.
"""
template_full = cv2.imread(config.CAFE_SPARKLE_TEMPLATE) template_full = cv2.imread(config.CAFE_SPARKLE_TEMPLATE)
img = image if image is not None else driver.read_screenshot(SPARKLE_SHOT_PATH) img = image if image is not None else driver.read_screenshot(SPARKLE_SHOT_PATH)
th0, tw0 = template_full.shape[:2] th0, tw0 = template_full.shape[:2]
ox, oy = SPARKLE_CLICK_OFFSET
best = None candidates = []
for scale in SPARKLE_SCALES: for scale in SPARKLE_SCALES:
tw, th = max(1, round(tw0 * scale)), max(1, round(th0 * scale)) tw, th = max(1, round(tw0 * scale)), max(1, round(th0 * scale))
template = cv2.resize(template_full, (tw, th)) template = cv2.resize(template_full, (tw, th))
mask = _masked_template(template) mask = _masked_template(template)
result = cv2.matchTemplate(img, template, cv2.TM_CCORR_NORMED, mask=mask) result = cv2.matchTemplate(img, template, cv2.TM_CCORR_NORMED, mask=mask)
_, max_val, _, max_loc = cv2.minMaxLoc(result) ys, xs = np.where(result >= SPARKLE_THRESHOLD)
if max_val >= SPARKLE_THRESHOLD and (best is None or max_val > best[0]): for x, y in zip(xs, ys):
best = (max_val, max_loc[0], max_loc[1], tw, th, scale) cx = int(x) + tw // 2 + round(ox * scale)
cy = int(y) + th // 2 + round(oy * scale)
candidates.append((float(result[y, x]), cx, cy))
if best is None: return _dedupe_sparkles(candidates)
return None
score, x, y, tw, th, scale = best
ox, oy = SPARKLE_CLICK_OFFSET def _dedupe_sparkles(candidates):
return (x + tw // 2 + round(ox * scale), y + th // 2 + round(oy * scale), score) """Collapse candidates within SPARKLE_DEDUPE_DISTANCE of each other
(the same real sparkle matched at several nearby scales/positions) into
a single detection, keeping the highest-scoring one each time."""
candidates = sorted(candidates, key=lambda c: c[0], reverse=True)
kept = []
for score, x, y in candidates:
if any(
abs(x - kx) < SPARKLE_DEDUPE_DISTANCE and abs(y - ky) < SPARKLE_DEDUPE_DISTANCE
for _, kx, ky in kept
):
continue
kept.append((score, x, y))
return [(x, y, score) for score, x, y in kept]
def find_template(template_path, region=None, threshold=0.85): def find_template(template_path, region=None, threshold=0.85):
@ -96,7 +139,7 @@ def find_template(template_path, region=None, threshold=0.85):
-- used wherever the reference identifies a screen/state by comparing a -- used wherever the reference identifies a screen/state by comparing a
fixed on-screen crop to a known template image, rather than by OCR (OCR fixed on-screen crop to a known template image, rather than by OCR (OCR
reads stay on read_text/read_int; see CLAUDE.md's OCR policy for why reads stay on read_text/read_int; see CLAUDE.md's OCR policy for why
these two stay separate detection paths). Unlike find_cafe_sparkle, this these two stay separate detection paths). Unlike find_cafe_sparkles, this
project's UI chrome (nav icons, modal titles, result banners) renders at project's UI chrome (nav icons, modal titles, result banners) renders at
a fixed resolution with no camera zoom involved, so no multi-scale search a fixed resolution with no camera zoom involved, so no multi-scale search
or color masking is needed here -- a single plain match is enough. Add a or color masking is needed here -- a single plain match is enough. Add a
@ -246,6 +289,35 @@ def read_int_white_on_dark(region, psm=7):
return int(digits) if digits else None return int(digits) if digits else None
def read_text_white_on_dark(region, whitelist=None, psm=7, lang="eng", threshold=150):
"""General-text equivalent of read_int_white_on_dark, for light-on-dark
UI outside this project's own game client -- e.g. a genuine Steam
client dialog (confirmed live 2026-09-20: three consecutive real cron
failures traced back to Steam itself being signed out, sitting on its
own account sign-in screen instead of ever launching the game --
login.py's `_steam_needs_sign_in` uses this to recognize that specific
state and give a diagnostic message that says so, instead of a generic
"game window never appeared").
`threshold` defaults to the same 150 read_int_white_on_dark already
uses, but is adjustable here since not every light-on-dark UI is
equally bright -- Steam's own "SIGN IN WITH ACCOUNT NAME" label topped
out at grayscale 126 in a live capture, well under 150, and needed a
lower threshold (confirmed live: 70-100 all read it cleanly) to
binarize into anything at all rather than a blank crop.
"""
x1, y1, x2, y2 = region
img = driver.read_screenshot(OCR_SHOT_PATH)
crop = img[y1:y2, x1:x2]
gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY_INV)
upscaled = cv2.resize(thresh, None, fx=OCR_UPSCALE, fy=OCR_UPSCALE, interpolation=cv2.INTER_CUBIC)
tess_config = f"--psm {psm}"
if whitelist:
tess_config += f" -c tessedit_char_whitelist={whitelist}"
return pytesseract.image_to_string(upscaled, lang=lang, config=tess_config).strip()
def read_int(region, psm=7): def read_int(region, psm=7):
"""Local equivalent of the reference's recognize_int.""" """Local equivalent of the reference's recognize_int."""
digits = "".join(ch for ch in read_text(region, whitelist="0123456789", psm=psm) if ch.isdigit()) digits = "".join(ch for ch in read_text(region, whitelist="0123456789", psm=psm) if ch.isdigit())

View File

@ -90,8 +90,8 @@ def is_header_bar_visible(driver, image=None):
# from the same frame instead of drifting across ~8 sequential captures). # from the same frame instead of drifting across ~8 sequential captures).
# #
# `image` optionally supplies an already-decoded frame (e.g. cafe.py's # `image` optionally supplies an already-decoded frame (e.g. cafe.py's
# per-iteration capture, shared with that same iteration's # per-round capture, shared with that same round's
# find_cafe_sparkle call) instead of taking a fresh screenshot here. # find_cafe_sparkles call) instead of taking a fresh screenshot here.
points = [(x, HEADER_ROW_Y) for x in HEADER_ROW_X_OFFSETS] points = [(x, HEADER_ROW_Y) for x in HEADER_ROW_X_OFFSETS]
for r, g, b in driver.colors_at(points, image=image): for r, g, b in driver.colors_at(points, image=image):
if not (r > SUBSCREEN_HEADER_MIN_CHANNEL and g > SUBSCREEN_HEADER_MIN_CHANNEL and b > SUBSCREEN_HEADER_MIN_CHANNEL): if not (r > SUBSCREEN_HEADER_MIN_CHANNEL and g > SUBSCREEN_HEADER_MIN_CHANNEL and b > SUBSCREEN_HEADER_MIN_CHANNEL):

File diff suppressed because one or more lines are too long

View File

@ -93,6 +93,9 @@ _pat_current_view poll iteration instead of only right after a pat -- see
that function's own comment. that function's own comment.
""" """
import os
import time
from ba_auto import detector, navigation from ba_auto import detector, navigation
ROOM_OPEN_RETRIES = 3 ROOM_OPEN_RETRIES = 3
@ -113,6 +116,28 @@ def _claim_disabled(driver):
) )
def _save_diagnostic_screenshot(driver, config, label):
# Added 2026-09-19, per explicit user request, after this task's own
# "screen not confirmed (rank-up cutscene stuck?)" warning turned out to
# fire on ~90% of real scheduled runs in the logs but could not be
# reproduced on demand in several separate live debugging sessions
# (fresh game relaunch, full login->cafe sequence, the cooldown-dialog-
# dismiss path all included) -- every attempt to force it live
# succeeded cleanly instead. Since the exact interrupting state
# couldn't be captured live, the next REAL occurrence needs to leave
# hard evidence of what the screen actually looked like on its own,
# rather than relying on log text alone to diagnose after the fact.
# Written to scratchpad/ per this project's own temporary-file
# convention -- not auto-cleaned, review/delete via clean_scratchpad.sh
# once a real occurrence has been captured and inspected.
path = os.path.join(config.SCRATCHPAD_DIR, f"cafe_diag_{label}_{int(time.time())}.png")
try:
driver.read_screenshot(path)
print(f"[cafe] saved diagnostic screenshot: {path}")
except RuntimeError as exc:
print(f"[cafe] could not save diagnostic screenshot: {exc}")
def _enter_room(driver, coords): def _enter_room(driver, coords):
for attempt in range(1, ROOM_OPEN_RETRIES + 1): for attempt in range(1, ROOM_OPEN_RETRIES + 1):
driver.click(*coords) driver.click(*coords)
@ -157,14 +182,32 @@ def _dismiss_rank_up_if_shown(driver, config, image=None):
first_check_image = None first_check_image = None
driver.keypress("Return") driver.keypress("Return")
driver.wait(1.5) driver.wait(1.5)
return navigation.is_header_bar_visible(driver) confirmed = navigation.is_header_bar_visible(driver)
if not confirmed:
_save_diagnostic_screenshot(driver, config, "not_confirmed")
return confirmed
def _pat_current_view(driver, config): def _pat_current_view(driver, config):
# Sparkles appear on a per-student cooldown, so most single checks find # Sparkles appear on a per-student cooldown. Original design: one
# nothing -- the old Bash loop (and an earlier version of this one) gave # screenshot + one sparkle-match attempt per click, giving up after a
# up on the very first miss, which meant it essentially never farmed. # fixed budget of individual click attempts -- correct, but wasteful
# Keep polling for the full budget instead of bailing early. # whenever several sparkles were visible in the same frame, since each
# still paid for its own fresh scrot + multi-scale template match.
# Redesigned 2026-09-19, per explicit user direction: one screenshot,
# find and pat EVERY sparkle visible in it (detector.find_cafe_sparkles,
# not just the single best match), then take a new screenshot and
# repeat -- stopping once config.CAFE_EMPTY_ROUNDS_TO_STOP consecutive
# rounds each find nothing, rather than polling a fixed click-attempt
# budget regardless of how many were found per frame.
#
# Patting through a whole batch from one screenshot without
# re-capturing between individual pats is safe here specifically
# because cafe sparkles sit on static room furniture/students, not a
# reflowing list -- unlike Common Shop's item grid (see shop_utils.py's
# own 2026-09-17 incident, where reusing stale positions after an
# action that could reflow the grid was NOT safe). Patting one sparkle
# doesn't move where any other already-detected sparkle is sitting.
# #
# Real-usage bug (2026-07-16): the rank-up check used to run only once, # Real-usage bug (2026-07-16): the rank-up check used to run only once,
# immediately after a pat, with no wait beforehand -- but the rank-up # immediately after a pat, with no wait beforehand -- but the rank-up
@ -172,7 +215,7 @@ def _pat_current_view(driver, config):
# animation delay, so that single check could catch the tail end of the # animation delay, so that single check could catch the tail end of the
# still-normal room view (header genuinely still visible in that instant) # still-normal room view (header genuinely still visible in that instant)
# and conclude "clear" a beat before the actual cutscene appeared. Once # and conclude "clear" a beat before the actual cutscene appeared. Once
# that happened, nothing ever checked again: find_cafe_sparkle() can't # that happened, nothing ever checked again: find_cafe_sparkles() can't
# tell "stuck on an undetected cutscene" apart from the ordinary # tell "stuck on an undetected cutscene" apart from the ordinary
# "nothing to pat right now" case, so the loop just polled dead air for # "nothing to pat right now" case, so the loop just polled dead air for
# the rest of its budget, the following camera pan dragged over a static # the rest of its budget, the following camera pan dragged over a static
@ -180,41 +223,40 @@ def _pat_current_view(driver, config):
# return_to_home's generic cleanup failed to recover afterward -- this # return_to_home's generic cleanup failed to recover afterward -- this
# exact cutscene's background happens to read under is_on_subscreen/ # exact cutscene's background happens to read under is_on_subscreen/
# is_modal_open's thresholds too, so every "are we home" check downstream # is_modal_open's thresholds too, so every "are we home" check downstream
# falsely agreed nothing was wrong. Fixed by checking on EVERY poll # falsely agreed nothing was wrong. Fixed by checking before every
# iteration, not just right after a pat, so a delayed appearance is # individual pat, not just once per round, so a delayed appearance is
# caught (and dismissed, via _dismiss_rank_up_if_shown's own Enter-press # caught mid-batch instead of only between rounds.
# loop) on the next iteration, roughly a second later, instead of never.
#
# One capture per iteration shared between the rank-up check and the
# sparkle match (previously two separate scrot calls back-to-back with
# nothing in between): the header-bar check only samples fixed chrome
# pixels unrelated to the room scene, and the sparkle match already
# reports "wherever it is in this one frame" regardless of whether that
# frame is shared -- the room animating (a student walking around)
# doesn't change that, since neither read depends on the other lagging
# or leading it. See plan.md's "Performance improvement plan".
patted = 0 patted = 0
for _ in range(config.CAFE_MAX_CLICKS_PER_ROOM): empty_rounds = 0
for _ in range(config.CAFE_MAX_SPARKLE_ROUNDS):
image = detector.capture_screen() image = detector.capture_screen()
if not _dismiss_rank_up_if_shown(driver, config, image=image): if not _dismiss_rank_up_if_shown(driver, config, image=image):
print("[cafe] warning: cafe screen not confirmed (rank-up cutscene stuck?) -- stopping this view's pat loop rather than clicking blindly") print("[cafe] warning: cafe screen not confirmed (rank-up cutscene stuck?) -- stopping this view's pat loop rather than clicking blindly")
return patted
matches = detector.find_cafe_sparkles(image=image)
if not matches:
empty_rounds += 1
if empty_rounds >= config.CAFE_EMPTY_ROUNDS_TO_STOP:
break break
match = detector.find_cafe_sparkle(image=image)
if match is None:
driver.wait(1) driver.wait(1)
continue continue
x, y, score = match empty_rounds = 0
for x, y, score in matches:
driver.click(x, y) driver.click(x, y)
driver.wait(1) driver.wait(1)
driver.keypress("Return") driver.keypress("Return")
# park the cursor away from the sparkle area so it can't occlude the # park the cursor away from the sparkle area so it can't occlude
# next detection screenshot (see screenshots/cafe/sparkle/02_*_cursor_on_head.png) # the next detection screenshot (see
# screenshots/cafe/sparkle/02_*_cursor_on_head.png)
driver.move_mouse(10, 1190) driver.move_mouse(10, 1190)
# No image= here -- the click/Return/mouse-move above are real state # No image= here -- the click/Return/mouse-move above are real
# changes since this iteration's capture, so this check must be fresh. # state changes since this batch's own capture, so this check
# must be fresh.
if not _dismiss_rank_up_if_shown(driver, config): if not _dismiss_rank_up_if_shown(driver, config):
print("[cafe] warning: cafe screen not confirmed after a pat (rank-up cutscene stuck?) -- stopping this view's pat loop rather than clicking blindly") print("[cafe] warning: cafe screen not confirmed after a pat (rank-up cutscene stuck?) -- stopping this view's pat loop rather than clicking blindly")
break return patted
patted += 1 patted += 1
print(f"[cafe] patted sparkle at ({x}, {y}), score={score:.3f}") print(f"[cafe] patted sparkle at ({x}, {y}), score={score:.3f}")
return patted return patted

View File

@ -337,6 +337,22 @@ def _loading_buffer_visible(driver, config):
return all(_color_matches(c, config.LOGIN_LOADING_BUFFER_RGB, config.LOGIN_LOADING_BUFFER_TOLERANCE) for c in colors) return all(_color_matches(c, config.LOGIN_LOADING_BUFFER_RGB, config.LOGIN_LOADING_BUFFER_TOLERANCE) for c in colors)
def _steam_needs_sign_in(driver, config):
"""Positive OCR check for Steam's OWN account sign-in dialog -- not a
Blue Archive client screen at all (see config.py's
LOGIN_STEAM_SIGNIN_* comment for the full real-incident writeup). Only
meaningful to call after the game window has already failed to appear
at all -- this is a diagnostic aid to explain WHY, not a state this
flow can do anything about (it needs a real person to sign in), so
`run()` only checks it once, right before giving up entirely.
"""
text = detector.read_text_white_on_dark(
config.LOGIN_STEAM_SIGNIN_TEXT_RECT,
threshold=config.LOGIN_STEAM_SIGNIN_OCR_THRESHOLD,
)
return "SIGN IN" in text.upper()
def _update_dialog_open(driver, config): def _update_dialog_open(driver, config):
"""Positive OCR check for the pending-update/additional-data download """Positive OCR check for the pending-update/additional-data download
notice (see config.py's LOGIN_UPDATE_DIALOG_* comment for the full notice (see config.py's LOGIN_UPDATE_DIALOG_* comment for the full
@ -499,6 +515,9 @@ def _recover(driver, config):
driver.wait(2) driver.wait(2)
driver.launch_game() driver.launch_game()
if not _wait_for_window(driver, config): if not _wait_for_window(driver, config):
if _steam_needs_sign_in(driver, config):
print("[login] warning: Steam itself is showing its own account sign-in screen after relaunch -- signed out, needs a real person to log in; cannot proceed automatically")
else:
print("[login] warning: game window did not reappear after relaunch") print("[login] warning: game window did not reappear after relaunch")
return False return False
if not driver.window_exists(): if not driver.window_exists():
@ -517,6 +536,9 @@ def run(driver, config):
print("[login] game window not found -- launching") print("[login] game window not found -- launching")
driver.launch_game() driver.launch_game()
if not _wait_for_window(driver, config): if not _wait_for_window(driver, config):
if _steam_needs_sign_in(driver, config):
print("[login] warning: Steam itself is showing its own account sign-in screen -- signed out, needs a real person to log in (password or the Steam Mobile app QR code); cannot proceed automatically")
else:
print("[login] warning: game window never appeared after launch -- giving up") print("[login] warning: game window never appeared after launch -- giving up")
return return
if not driver.window_exists(): if not driver.window_exists():

View File

@ -185,6 +185,61 @@ def _scan_current_view(driver, config, remaining):
return None return None
def _grid_looks_aligned(driver, config):
"""True if at least one of the 8 fixed grid cells currently reads
purchasable -- a cheap proxy for "the fixed SHOP_ITEM_ROW_Y/
SHOP_ITEM_COL_X crops are actually landing on real cards right now",
not a torn mid-scroll frame.
Real live bug (2026-09-21): a real cron run of Common Shop logged 4-5
of 8 configured targets as "not found available at its expected price"
-- '中級強化珠' among them -- even though a read-only diagnostic probe
against the very same live screen, moments apart, found it sitting
unscrolled at the top of the grid with its correct name, correct price,
and a purchasable buy button. Root-caused with a before/after screenshot
of a single select_targets_by_name scroll call: the grid visibly moved
by something closer to 1.5-2 rows, not the clean single row
SHOP_SCROLL_STEP_CLICKS's own docstring assumed -- landing on a frame
with card titles cut off above the viewport and buy buttons sitting
nowhere near their calibrated offsets. Every fixed-offset OCR/color read
against that frame came back empty or garbled, and _is_purchasable read
False for all 8 cells (vs. 7-8/8 True on a genuinely aligned frame, e.g.
the very first unscrolled screenshot of the same run) -- so the scan
silently treated a real, available item as gone rather than as "my own
crop coordinates are pointing at nothing right now".
Not foolproof: a row that is genuinely, simultaneously sold out across
all 8 visible cells would also read this way. Accepted as an unlikely
edge case, consistent with this module's other scan heuristics (e.g.
select_targets_by_name's own "screenshot unchanged" bottom-of-list
check has a similar blind spot).
"""
n_rows = len(config.SHOP_ITEM_ROW_Y)
n_cols = len(config.SHOP_ITEM_COL_X)
return any(
_is_purchasable(driver, config, row, col)
for row in range(n_rows)
for col in range(n_cols)
)
def _ensure_grid_aligned(driver, config):
"""After a scroll, confirm the grid landed row-aligned (see
_grid_looks_aligned); if not, nudge back up in small steps until it is,
or give up after config.SHOP_SCROLL_REALIGN_MAX_NUDGES attempts.
Returns True once aligned (including if it already was), False if every
nudge was exhausted without recovering.
"""
if _grid_looks_aligned(driver, config):
return True
for _ in range(config.SHOP_SCROLL_REALIGN_MAX_NUDGES):
driver.scroll(*config.SHOP_SCROLL_POINT, "up", clicks=config.SHOP_SCROLL_REALIGN_NUDGE_CLICKS)
driver.wait(0.3)
if _grid_looks_aligned(driver, config):
return True
return False
def select_targets_by_name(driver, config, targets): def select_targets_by_name(driver, config, targets):
"""Name-OCR-based sibling of select_targets, for grids where a bought """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 (or sold-out) item sinks to the bottom and shifts everything else's
@ -250,6 +305,9 @@ def select_targets_by_name(driver, config, targets):
if remaining: if remaining:
driver.scroll(*config.SHOP_SCROLL_POINT, "down", clicks=config.SHOP_SCROLL_STEP_CLICKS) driver.scroll(*config.SHOP_SCROLL_POINT, "down", clicks=config.SHOP_SCROLL_STEP_CLICKS)
driver.wait(0.3) 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)")
break
skipped = [] skipped = []
for name in remaining: for name in remaining:
print(f"[shop] '{name}' not found available at its expected price {remaining[name]} -- skipping (sold out this cycle, or catalog changed)") print(f"[shop] '{name}' not found available at its expected price {remaining[name]} -- skipping (sold out this cycle, or catalog changed)")
@ -257,24 +315,58 @@ def select_targets_by_name(driver, config, targets):
return selected, skipped return selected, skipped
def confirm_purchase(driver, config): def confirm_purchase(driver, config, *, balance_rect, balance_before, expected_spend):
"""Click the bulk Buy button, then click through the purchase-confirm """Click the bulk Buy button, then click through the purchase-confirm
dialog and the reward-acquired banner. Both dim dialog and the reward-acquired banner. Both dim
config.SHOP_OVERLAY_PROBE away from pure white; press Enter until it config.SHOP_OVERLAY_PROBE away from pure white; press Enter until it
reads idle again, bounded by config.SHOP_PURCHASE_MAX_ENTER_PRESSES -- reads idle again, bounded by config.SHOP_PURCHASE_MAX_ENTER_PRESSES --
this project doesn't press keys past a bound against an unrecognized this project doesn't press keys past a bound against an unrecognized
state (see navigation.wait_for_state). Returns True once idle, False if state (see navigation.wait_for_state).
it never clears within the bound.
Real live false positive (2026-09-19): the overlay can already read
"idle" on the very first check -- there's nothing in a single
overlay-darkness read that distinguishes "confirmed and closed" from
"the Buy click missed and the confirm dialog never opened at all".
Confirmed live: a real cron run logged "bought 4 item(s)" for a real
credit amount, but those exact 4 items were still sitting unbought at
their base price hours later -- the overlay had simply never dimmed at
any point this function actually checked it. Fixed by also requiring
the currency balance to have actually dropped by `expected_spend`
once the overlay reports idle -- re-read with the same
stray-misread-digit retry tolerance SHOP_PRICE_OCR_RETRIES already uses
elsewhere in this module, so one bad OCR frame on the balance itself
can't sink an otherwise-real purchase.
Returns True only once the overlay is idle AND the balance dropped by
exactly `expected_spend`; False otherwise -- whether the overlay never
cleared within the retry bound, or it cleared but the balance never
moved (or moved by some other amount), meaning nothing was really
bought despite looking "idle".
""" """
driver.click(*config.SHOP_BUY_BUTTON) driver.click(*config.SHOP_BUY_BUTTON)
driver.wait(1.5) driver.wait(1.5)
min_ch = config.SHOP_OVERLAY_IDLE_MIN_CHANNEL min_ch = config.SHOP_OVERLAY_IDLE_MIN_CHANNEL
idle = False
for _ in range(config.SHOP_PURCHASE_MAX_ENTER_PRESSES): for _ in range(config.SHOP_PURCHASE_MAX_ENTER_PRESSES):
r, g, b = driver.color_at(*config.SHOP_OVERLAY_PROBE) r, g, b = driver.color_at(*config.SHOP_OVERLAY_PROBE)
if r > min_ch and g > min_ch and b > min_ch: if r > min_ch and g > min_ch and b > min_ch:
return True idle = True
break
driver.keypress("Return") driver.keypress("Return")
driver.wait(1.5) driver.wait(1.5)
if not idle:
return False
balance_after = None
for attempt in range(1, config.SHOP_PRICE_OCR_RETRIES + 1):
balance_after = detector.read_int(balance_rect)
if balance_after is not None and balance_before - balance_after == expected_spend:
return True
if attempt < config.SHOP_PRICE_OCR_RETRIES:
driver.wait(0.3)
actual_spend = None if balance_after is None else balance_before - balance_after
print(f"[shop] purchase overlay cleared but balance changed by {actual_spend}, expected {expected_spend} -- treating as not confirmed")
return False return False
@ -316,11 +408,11 @@ def run_shop_tab(driver, config, *, tab_button, targets, balance_rect, currency_
print("[shop] nothing selected (all targets skipped), cancelling") print("[shop] nothing selected (all targets skipped), cancelling")
return "nothing_selected" return "nothing_selected"
if not confirm_purchase(driver, config): spent = sum(price for _, price in selected)
print("[shop] purchase confirmation did not resolve to an idle screen within the retry bound -- stopping, check the game manually") if not confirm_purchase(driver, config, balance_rect=balance_rect, balance_before=balance, expected_spend=spent):
print("[shop] purchase confirmation did not resolve to a verified balance change within the retry bound -- stopping, check the game manually")
return "unrecognized_state" return "unrecognized_state"
spent = sum(price for _, price in selected)
msg = f"[shop] bought {len(selected)} item(s) for {spent} {currency_label}" msg = f"[shop] bought {len(selected)} item(s) for {spent} {currency_label}"
if skipped: if skipped:
msg += f", skipped {len(skipped)}" msg += f", skipped {len(skipped)}"

View File

@ -246,8 +246,41 @@ def _run_task(name):
def _run_sequence(names): def _run_sequence(names):
for name in names: # Real incident, 2026-09-20: three consecutive cron runs (one q4h, two
# daily) all failed the same way -- login.py's own launch attempt gave
# up cleanly (game window never appeared -- traced to Steam itself
# being signed out, see login.py's _steam_needs_sign_in), but that
# warning alone doesn't stop the sequence, so the very NEXT task still
# ran, paid for its own full PRE_TASK_HOME_RETRIES retry cycle (~15s
# of doomed waiting), and only then crashed with an uncaught
# RuntimeError from driver.focus_game() -- the first task genuinely
# able to raise, since login.py's own window_exists()/launch_game()
# calls are the only ones in this codebase that tolerate a missing
# window. That crash DID correctly propagate as a real cron FAILED
# exit code (this project deliberately never swallows a real crash --
# see _run_task's own docstring), so nothing was silently hidden, but
# every task after the one that happened to crash first never got a
# chance to even try, and the log buried the real cause two tasks deep
# behind a stack trace instead of surfacing it immediately.
#
# Checking window_exists() after every task closes this gap for ANY
# cause the window can disappear (not just this one incident) --
# login's own launch failing, or the game crashing mid-sequence later
# on -- and turns the same eventual crash into one immediate, clear
# line instead of a slow doomed retry-then-traceback. Skipped for the
# last task and for exit_game specifically, since exit_game closing the
# window is its own intended success, not a failure to report.
last_index = len(names) - 1
for i, name in enumerate(names):
_run_task(name) _run_task(name)
if i == last_index or name == "exit_game":
continue
if not driver.window_exists():
raise RuntimeError(
f"[{name}] game window does not exist after this task -- aborting the rest of this run "
"rather than letting every remaining task independently fail the same way "
"(check nik-gpu manually -- Steam may need signing in, or the game may have crashed)"
)
print("All done.") print("All done.")