审查修复: 任务配置恢复、执行器容错与续局状态、识别性能

Browse Source
正确性
- stop_task 强杀后 run_task 的临时次数会残留在 config.yaml (AALC 中途整份存盘);
  任务启动时先落盘原值, 强停/异常退出后由 TaskRunner 用 --restore 写回,
  写回失败时拒绝启动新任务并在 task_status 显示 pending_restore
- Jev 执行器: 工具失败不再抛异常导致整局退出 (Session raise_on_error=False),
  原有的"截图暂不可用"等降级分支终于生效; 刷新计数/开战标记只在动作成功后更新
- 一局完成后归档 jev_shop_context.json, 否则 claim_requested 让下一局一启动就判定完成
- 通用按钮直接点已核对的坐标, 不再 click_text 重新搜索 (可能点到背景同名按钮)
- 认证行按字节比较, 非 ASCII 输入不再让 compare_digest 抛 TypeError
- 单实例互斥改用 use_last_error 读取 ERROR_ALREADY_EXISTS

性能
- find_image 只在窗口尺寸变化时清模板缓存 (原来每次两次全量 gc); 模板列表只扫描一次
- 截图 PNG 压缩级别 1: 1080p 存图 0.66s -> 0.19s
- 执行器每帧截图只解码一次 (button_enabled 75ms -> 0.3ms); 地图模板与 ORB 描述子按比例缓存
- 箭头模板用 imdecode 读取, 支持中文路径 (输出与 imread 逐像素一致)

测试 73 -> 84, 修改前的识别输出与修改后逐项比对一致。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yws
2026-10-03 00:31:21 +00:00
co-authored by Claude Opus 5.5
parent 5ab5fd9508
commit 030527f5de
9 changed files with 444 additions and 105 deletions
+87 -41
View File
@@ -1,11 +1,9 @@
"""Validated scene actions for Jev decisions and fixed speedrun macros.""" """Validated scene actions for Jev decisions and fixed speedrun macros."""
import argparse import argparse
import functools
import json import json
import math
import os import os
import re import re
import subprocess
import sys
import time import time
import urllib.error import urllib.error
import urllib.request import urllib.request
@@ -14,6 +12,7 @@ from pathlib import Path
from mcp_session import Session as Bridge from mcp_session import Session as Bridge
REPO_ROOT = Path(__file__).resolve().parents[1] REPO_ROOT = Path(__file__).resolve().parents[1]
MIRROR_ASSETS = REPO_ROOT / 'vendor' / 'aalc' / 'assets' / 'images' / 'default' / 'share' / 'mirror'
RUNTIME_HOME = Path(os.environ.get('LC_JEV_HOME', str(Path.home() / 'lc_mcp' / 'jev'))) RUNTIME_HOME = Path(os.environ.get('LC_JEV_HOME', str(Path.home() / 'lc_mcp' / 'jev')))
STOP_FILE = RUNTIME_HOME / 'JEV_STOP' STOP_FILE = RUNTIME_HOME / 'JEV_STOP'
@@ -46,31 +45,65 @@ def death_retry_screen(texts):
or '当前剩余人员' in combined and '重试关卡' in combined) or '当前剩余人员' in combined and '重试关卡' in combined)
_SCREEN_CACHE = {}
def screen_image(path, mode):
"""按模式解码当前截图并缓存: 一帧里多次判色/裁剪只解码一次整张 PNG (1080p 约 75ms/次)。
键含修改时间和大小, 同路径写入新内容时不会命中旧图。"""
from PIL import Image
st = os.stat(path)
key = (str(path), st.st_mtime_ns, st.st_size, mode)
image = _SCREEN_CACHE.get(key)
if image is None:
if len(_SCREEN_CACHE) >= 4:
_SCREEN_CACHE.clear()
with Image.open(path) as source:
image = source.convert(mode)
_SCREEN_CACHE[key] = image
return image
def button_enabled(state, item): def button_enabled(state, item):
try: try:
from PIL import Image
import numpy as np import numpy as np
x, y, w, h = (int(item[k]) for k in ('x', 'y', 'w', 'h')) x, y, w, h = (int(item[k]) for k in ('x', 'y', 'w', 'h'))
with Image.open(state['screen']['path']) as image: crop = np.array(screen_image(state['screen']['path'], 'L').crop((x, y, x + w, y + h)))
crop = np.array(image.convert('L').crop((x, y, x + w, y + h)))
return float(np.quantile(crop, 0.9)) > 85 return float(np.quantile(crop, 0.9)) > 85
except (OSError, KeyError, ImportError): except (OSError, KeyError, ImportError):
return False return False
@functools.lru_cache(maxsize=64)
def _map_template(kind, name, scale):
"""地图识别模板, 按截图比例缩放; 窗口尺寸不变时每帧复用, 不再重复读盘和计算特征点。
返回的数组是共享缓存, 调用方不能原地修改。"""
import cv2
import numpy as np
from PIL import Image
path = MIRROR_ASSETS / name
if kind == 'bus':
with Image.open(path) as image:
gray = cv2.cvtColor(np.array(image.convert('RGB')), cv2.COLOR_RGB2GRAY)
return cv2.resize(gray, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
if kind == 'arrow':
# 等价于 cv2.imread(path, 0); imread 在 Windows 上打不开含中文的路径
gray = cv2.imdecode(np.fromfile(str(path), dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
return cv2.resize(gray, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
# 节点图标: ORB 描述子只取决于模板和参数
with Image.open(path) as image:
gray = cv2.resize(np.array(image.convert('L')), None, fx=2*scale, fy=2*scale)
return cv2.ORB_create(nfeatures=1000, scaleFactor=1.2, edgeThreshold=10).detectAndCompute(gray, None)[1]
def map_candidates(state): def map_candidates(state):
"""Only observe reachable nodes; Jev chooses their route.""" """Only observe reachable nodes; Jev chooses their route."""
import cv2 import cv2
import numpy as np import numpy as np
from PIL import Image rgb = np.array(screen_image(state['screen']['path'], 'RGB'))
assets = REPO_ROOT / 'vendor' / 'aalc' / 'assets' / 'images' / 'default' / 'share' / 'mirror'
with Image.open(state['screen']['path']) as image:
rgb = np.array(image.convert('RGB'))
gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY) gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
scale = min(rgb.shape[0] / 1440, rgb.shape[1] / 2560) scale = min(rgb.shape[0] / 1440, rgb.shape[1] / 2560)
with Image.open(assets / 'mybus_default_distance.png') as image: bus = _map_template('bus', 'mybus_default_distance.png', scale)
bus = cv2.cvtColor(np.array(image.convert('RGB')), cv2.COLOR_RGB2GRAY)
bus = cv2.resize(bus, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
_, confidence, _, location = cv2.minMaxLoc(cv2.matchTemplate(gray, bus, cv2.TM_CCOEFF_NORMED)) _, confidence, _, location = cv2.minMaxLoc(cv2.matchTemplate(gray, bus, cv2.TM_CCOEFF_NORMED))
if confidence < 0.88: if confidence < 0.88:
return {} return {}
@@ -88,8 +121,7 @@ def map_candidates(state):
arrow_roi = gray[max(0,ay-radius):min(gray.shape[0],ay+radius), arrow_roi = gray[max(0,ay-radius):min(gray.shape[0],ay+radius),
max(0,ax-radius):min(gray.shape[1],ax+radius)] max(0,ax-radius):min(gray.shape[1],ax+radius)]
arrow_name = {'w':'up','d':'mid','s':'down'}[key] arrow_name = {'w':'up','d':'mid','s':'down'}[key]
template = cv2.imread(str(assets / 'road_in_mir' / (arrow_name+'.png')), 0) template = _map_template('arrow', 'road_in_mir/%s.png' % arrow_name, scale)
template = cv2.resize(template, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
arrow_reachable = False arrow_reachable = False
if arrow_roi.shape[0] >= template.shape[0] and arrow_roi.shape[1] >= template.shape[1]: if arrow_roi.shape[0] >= template.shape[0] and arrow_roi.shape[1] >= template.shape[1]:
_, arrow_score, _, arrow_pos = cv2.minMaxLoc(cv2.matchTemplate(arrow_roi, template, cv2.TM_CCOEFF_NORMED)) _, arrow_score, _, arrow_pos = cv2.minMaxLoc(cv2.matchTemplate(arrow_roi, template, cv2.TM_CCOEFF_NORMED))
@@ -111,10 +143,7 @@ def map_candidates(state):
if target_desc is not None: if target_desc is not None:
for name, label in (('shop','商店'),('event','事件'),('battle','普通战斗'), for name, label in (('shop','商店'),('event','事件'),('battle','普通战斗'),
('risky_encounter','精英战斗'),('focused_encounter','集中战斗')): ('risky_encounter','精英战斗'),('focused_encounter','集中战斗')):
with Image.open(assets / 'road_in_mir' / (name + '.png')) as image: desc = _map_template('icon', 'road_in_mir/%s.png' % name, scale)
template = np.array(image.convert('L'))
template = cv2.resize(template, None, fx=2*scale, fy=2*scale)
_, desc = orb.detectAndCompute(template, None)
if desc is not None: if desc is not None:
matches = cv2.BFMatcher(cv2.NORM_HAMMING).knnMatch(desc, target_desc, k=2) matches = cv2.BFMatcher(cv2.NORM_HAMMING).knnMatch(desc, target_desc, k=2)
score = sum(len(pair) == 2 and pair[0].distance < 0.7*pair[1].distance for pair in matches) score = sum(len(pair) == 2 and pair[0].distance < 0.7*pair[1].distance for pair in matches)
@@ -270,10 +299,8 @@ def candidates(state, previous_battle=False, refresh_used=0):
try: try:
import cv2 import cv2
import numpy as np import numpy as np
from PIL import Image border = np.array(screen_image(state['screen']['path'], 'RGB').crop(
with Image.open(state['screen']['path']) as image: (int(cx-width*.12), int(height*.31), int(cx-width*.107), int(height*.69))))
border = np.array(image.convert('RGB').crop((int(cx-width*.12), int(height*.31),
int(cx-width*.107), int(height*.69))))
hsv = cv2.cvtColor(border, cv2.COLOR_RGB2HSV) hsv = cv2.cvtColor(border, cv2.COLOR_RGB2HSV)
target_selected = float(((hsv[:,:,0] > 8) & (hsv[:,:,0] < 35) & target_selected = float(((hsv[:,:,0] > 8) & (hsv[:,:,0] < 35) &
(hsv[:,:,1] > 200) & (hsv[:,:,2] > 220)).mean()) > .18 (hsv[:,:,1] > 200) & (hsv[:,:,2] > 220)).mean()) > .18
@@ -493,8 +520,10 @@ def candidates(state, previous_battle=False, refresh_used=0):
if label in known_buttons and button_enabled(state, item): if label in known_buttons and button_enabled(state, item):
options['button_%d' % index] = { options['button_%d' % index] = {
'description': '当前可见且非灰色按钮:%s;点击 OCR 中心。' % label, 'description': '当前可见且非灰色按钮:%s;点击 OCR 中心。' % label,
# 直接点已核对过的这一项: 重新按文字搜索要多一次截图+OCR, 还可能命中背景里的同名按钮
'action': ({'tool': 'keypress', 'args': {'key': 'enter'}} if label in ('进入', '开始战斗') 'action': ({'tool': 'keypress', 'args': {'key': 'enter'}} if label in ('进入', '开始战斗')
else {'tool': 'click_text', 'args': {'text': item['text']}}), else {'tool': 'click', 'args': {'x': item['x'] + item['w']//2,
'y': item['y'] + item['h']//2}}),
} }
if (not battle and not in_shop and state.get('screen', {}).get('path') if (not battle and not in_shop and state.get('screen', {}).get('path')
and Path(state['screen']['path']).is_file() and Path(state['screen']['path']).is_file()
@@ -512,9 +541,7 @@ def candidates(state, previous_battle=False, refresh_used=0):
try: try:
import cv2 import cv2
import numpy as np import numpy as np
from PIL import Image rgb = np.array(screen_image(state['screen']['path'], 'RGB'))
with Image.open(state['screen']['path']) as image:
rgb = np.array(image.convert('RGB'))
for number in range(12): for number in range(12):
cx = round(width*(.061 + number*.0542)) cx = round(width*(.061 + number*.0542))
crop = rgb[round(height*.841):round(height*.878), max(0,cx-round(width*.026)):cx+round(width*.026)] crop = rgb[round(height*.841):round(height*.878), max(0,cx-round(width*.026)):cx+round(width*.026)]
@@ -685,32 +712,50 @@ def execute(bridge, action):
return call(action['tool'], action.get('args')) return call(action['tool'], action.get('args'))
def acquire_single_instance():
"""Windows 命名互斥量, 防止另一个终端重复启动同一个游戏的执行器。
返回释放函数; 已有实例在跑(或创建失败)时返回 None。"""
import ctypes
# use_last_error: 直接调 GetLastError 可能读到 ctypes 自身调用留下的值
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
kernel.CreateMutexW.restype = ctypes.c_void_p
kernel.CreateMutexW.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_wchar_p]
kernel.CloseHandle.argtypes = [ctypes.c_void_p]
lease = kernel.CreateMutexW(None, False, 'Local\\LimbusMirrorJevController')
if not lease:
return None
if ctypes.get_last_error() == 183: # ERROR_ALREADY_EXISTS
kernel.CloseHandle(lease)
return None
return lambda: kernel.CloseHandle(lease)
def archive_context(context_path):
"""一局结束后归档本局状态。claim_requested 等标记若留给下一局, 新局一启动就会被判定为已完成。"""
if context_path.exists():
context_path.replace(context_path.with_name('jev_shop_context.%s.json' % time.strftime('%Y%m%d_%H%M%S')))
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('config') parser.add_argument('config')
parser.add_argument('--steps', type=int, default=250) parser.add_argument('--steps', type=int, default=250)
parser.add_argument('--seconds', type=int, default=1200) parser.add_argument('--seconds', type=int, default=1200)
args = parser.parse_args() args = parser.parse_args()
# 防止另一个终端重复启动同一个游戏的执行器。 release = acquire_single_instance()
import ctypes if release is None:
kernel = ctypes.windll.kernel32
kernel.CreateMutexW.restype = ctypes.c_void_p
kernel.CreateMutexW.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_wchar_p]
lease = kernel.CreateMutexW(None, False, 'Local\\LimbusMirrorJevController')
if not lease or kernel.GetLastError() == 183:
print(json.dumps({'state':'already_running'}), flush=True) print(json.dumps({'state':'already_running'}), flush=True)
if lease:
kernel.CloseHandle(ctypes.c_void_p(lease))
return return
cfg = json.loads(Path(args.config).read_text(encoding='utf-8')) cfg = json.loads(Path(args.config).read_text(encoding='utf-8'))
cfg['apiKey'] = os.environ.get('OPENCODE_API_KEY') or cfg.get('apiKey') cfg['apiKey'] = os.environ.get('OPENCODE_API_KEY') or cfg.get('apiKey')
if not cfg['apiKey']: if not cfg['apiKey']:
kernel.CloseHandle(ctypes.c_void_p(lease)) release()
raise RuntimeError('Set OPENCODE_API_KEY or use an untracked local config with apiKey') raise RuntimeError('Set OPENCODE_API_KEY or use an untracked local config with apiKey')
RUNTIME_HOME.mkdir(parents=True, exist_ok=True) RUNTIME_HOME.mkdir(parents=True, exist_ok=True)
log = RUNTIME_HOME / 'jev_mirror_log.jsonl' log = RUNTIME_HOME / 'jev_mirror_log.jsonl'
status = RUNTIME_HOME / 'jev_mirror_status.json' status = RUNTIME_HOME / 'jev_mirror_status.json'
bridge = Bridge() # 工具失败按 {'ok': False} 返回: 截图暂不可用、按钮已消失等情况观察后继续, 而不是整局退出
bridge = Bridge(raise_on_error=False)
deadline = time.monotonic() + args.seconds deadline = time.monotonic() + args.seconds
recent = [] recent = []
repeats = {} repeats = {}
@@ -821,6 +866,7 @@ def main():
end_utc=now.isoformat(), elapsed_seconds=(now-datetime.fromisoformat(run['start_utc'])).total_seconds(), end_utc=now.isoformat(), elapsed_seconds=(now-datetime.fromisoformat(run['start_utc'])).total_seconds(),
completion_evidence=state.get('screen')) completion_evidence=state.get('screen'))
run_path.write_text(json.dumps(run, ensure_ascii=False, indent=2), encoding='utf-8') run_path.write_text(json.dumps(run, ensure_ascii=False, indent=2), encoding='utf-8')
archive_context(context_path)
print(json.dumps(record, ensure_ascii=False), flush=True) print(json.dumps(record, ensure_ascii=False), flush=True)
return return
if choice == 'requires_analysis': if choice == 'requires_analysis':
@@ -849,10 +895,10 @@ def main():
if choice=='modal_confirm' and result.get('ok') and any(i['text'].replace(' ','')=='快感' for i in text_items(state)) and any('获得E.G.O' in i['text'].replace(' ','') for i in text_items(state)): if choice=='modal_confirm' and result.get('ok') and any(i['text'].replace(' ','')=='快感' for i in text_items(state)) and any('获得E.G.O' in i['text'].replace(' ','') for i in text_items(state)):
context['thrill_obtained']=True context['thrill_obtained']=True
context_path.write_text(json.dumps(context),encoding='utf-8') context_path.write_text(json.dumps(context),encoding='utf-8')
if choice == 'shop_refresh': if choice == 'shop_refresh' and result.get('ok'):
context['refresh_used'] += 1 context['refresh_used'] += 1
context_path.write_text(json.dumps(context), encoding='utf-8') context_path.write_text(json.dumps(context), encoding='utf-8')
started_battle = choice.startswith('button_') and any( started_battle = choice.startswith('button_') and bool(result.get('ok')) and any(
i['text'].replace(' ', '') == '开始战斗' for i in text_items(state)) i['text'].replace(' ', '') == '开始战斗' for i in text_items(state))
previous_battle = ((action['tool'] == 'battle' and not result.get('terminal')) or started_battle previous_battle = ((action['tool'] == 'battle' and not result.get('terminal')) or started_battle
or (previous_battle and action['tool'] == 'wait')) or (previous_battle and action['tool'] == 'wait'))
@@ -869,7 +915,7 @@ def main():
print(json.dumps(record, ensure_ascii=False), flush=True) print(json.dumps(record, ensure_ascii=False), flush=True)
finally: finally:
bridge.close() bridge.close()
kernel.CloseHandle(ctypes.c_void_p(lease)) release()
if __name__ == '__main__': if __name__ == '__main__':