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

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
+5 -1
View File
@@ -81,7 +81,9 @@ codex mcp add limbus --env PYTHONUTF8=1 -- D:\lc-mcp\vendor\aalc\.venv\Scripts\p
| `task_status(lines)` / `stop_task` | 任务状态、日志尾部(`~/lc_mcp/runs`)、强制停止 | | `task_status(lines)` / `stop_task` | 任务状态、日志尾部(`~/lc_mcp/runs`)、强制停止 |
任务运行期间,点击/按键类工具会被拒绝(防止打乱 AALC 的流程),只读工具仍可用。 任务运行期间,点击/按键类工具会被拒绝(防止打乱 AALC 的流程),只读工具仍可用。
`run_task` 传入的次数只对本次运行生效,结束后恢复 `config.yaml` 原值。 `run_task` 传入的次数只对本次运行生效,结束后恢复 `config.yaml` 原值。AALC 运行中会整份保存配置,
所以任务启动时先把原值写到 `~/lc_mcp/runs/*.restore.json`;`stop_task` 强停或任务进程异常退出后,
服务会据此写回。写回失败时 `task_status` 显示 `pending_restore`,并在写回成功前拒绝启动新任务。
## 环境变量 ## 环境变量
@@ -119,6 +121,8 @@ vendor\aalc\.venv\Scripts\python.exe -X utf8 scripts\jev_mirror_agent.py scripts
``` ```
运行数据默认写到 `~/lc_mcp/jev`,可用 `LC_JEV_HOME` 指定其他目录。创建该目录下的 `JEV_STOP` 文件即可停止;续跑前移除它。不要与 AALC 完整任务或另一个游戏执行器同时操作。 运行数据默认写到 `~/lc_mcp/jev`,可用 `LC_JEV_HOME` 指定其他目录。创建该目录下的 `JEV_STOP` 文件即可停止;续跑前移除它。不要与 AALC 完整任务或另一个游戏执行器同时操作。
中途续跑沿用 `jev_shop_context.json`;一局领取完成后它会归档为 `jev_shop_context.<时间>.json`,下一局从干净状态开始。
工具调用失败(截图暂不可用、按钮已消失等)会记入日志并重新观察画面,同一画面上同一动作最多尝试两次,不会因单次失败整局退出。
本次修复、实际失败反馈、操作日志、截图清单、修改源码快照和速通 skill 见 [2026-10-02 实测记录](docs/feedback/2026-10-02/README.md);安装与测试说明见 [INSTALL_TEST_REPORT.md](INSTALL_TEST_REPORT.md)。这些是开发中的真实记录,包含失败和仍未验证的步骤。 本次修复、实际失败反馈、操作日志、截图清单、修改源码快照和速通 skill 见 [2026-10-02 实测记录](docs/feedback/2026-10-02/README.md);安装与测试说明见 [INSTALL_TEST_REPORT.md](INSTALL_TEST_REPORT.md)。这些是开发中的真实记录,包含失败和仍未验证的步骤。
+20 -10
View File
@@ -57,6 +57,9 @@ def prune(directory, pattern, keep):
class AalcEngine(Engine): class AalcEngine(Engine):
_templates = None # 模板相对路径集合; assets 运行中不变, 只扫描一次磁盘
_template_size = None # 当前 AALC 模板缓存对应的缩放尺寸
def __init__(self, shot_dir, keep_shots=30): def __init__(self, shot_dir, keep_shots=30):
self.shot_dir = os.path.abspath(shot_dir) self.shot_dir = os.path.abspath(shot_dir)
os.makedirs(self.shot_dir, exist_ok=True) os.makedirs(self.shot_dir, exist_ok=True)
@@ -90,7 +93,8 @@ class AalcEngine(Engine):
def _save(self, img): def _save(self, img):
stamp = time.strftime('%Y%m%d_%H%M%S') + '_%03d' % (int(time.time() * 1000) % 1000) stamp = time.strftime('%Y%m%d_%H%M%S') + '_%03d' % (int(time.time() * 1000) % 1000)
path = os.path.join(self.shot_dir, 'shot_%s.png' % stamp) path = os.path.join(self.shot_dir, 'shot_%s.png' % stamp)
img.save(path) # 每次 get_state 都存图: 1080p 下默认压缩级别 6 约 0.66s, 级别 1 约 0.19s, 文件只大 ~17%
img.save(path, compress_level=1)
prune(self.shot_dir, 'shot_*.png', self.keep_shots) prune(self.shot_dir, 'shot_*.png', self.keep_shots)
return {'path': path.replace('\\', '/'), 'w': img.width, 'h': img.height} return {'path': path.replace('\\', '/'), 'w': img.width, 'h': img.height}
@@ -114,13 +118,15 @@ class AalcEngine(Engine):
return shot, items return shot, items
def list_images(self, prefix=''): def list_images(self, prefix=''):
names = set() if self._templates is None:
for theme in ('dark', 'default'): names = set()
for lang in ('zh_cn', 'en', 'share'): for theme in ('dark', 'default'):
base = os.path.join(self.images_root, theme, lang) for lang in ('zh_cn', 'en', 'share'):
for p in glob.glob(os.path.join(base, '**', '*.png'), recursive=True): base = os.path.join(self.images_root, theme, lang)
names.add(os.path.relpath(p, base).replace('\\', '/')) for p in glob.glob(os.path.join(base, '**', '*.png'), recursive=True):
return sorted(n for n in names if n.startswith(prefix)) names.add(os.path.relpath(p, base).replace('\\', '/'))
self._templates = frozenset(names)
return sorted(n for n in self._templates if n.startswith(prefix))
def find_image(self, name, threshold=0.8, model='normal'): def find_image(self, name, threshold=0.8, model='normal'):
if name not in self.list_images(name): if name not in self.list_images(name):
@@ -132,16 +138,20 @@ class AalcEngine(Engine):
# 保留亚像素比例:例如 1278 宽对应 718.875 高,取整会改变 # 保留亚像素比例:例如 1278 宽对应 718.875 高,取整会改变
# 缩放采样,使小图标的匹配置信度明显下降。 # 缩放采样,使小图标的匹配置信度明显下降。
match_size = max(1, min(img.height, img.width * 9 / 16)) match_size = max(1, min(img.height, img.width * 9 / 16))
# AALC 的模板缓存按加载时的 set_win_size 缩放, 键里不含尺寸; 尺寸变了才清空。
# 本进程只有这里用模板, 缓存可以一直保持当前客户区的比例
# (clear_img_cache 每次都会全量 gc, 不能每次调用都清)。
if match_size != self._template_size:
self.auto.clear_img_cache()
self._template_size = match_size
changed = match_size != old_size changed = match_size != old_size
try: try:
if changed: if changed:
self.auto.clear_img_cache()
self.cfg.unsaved_set_value('set_win_size', match_size) self.cfg.unsaved_set_value('set_win_size', match_size)
pos = self.auto.find_image_element(name, threshold, model=model) pos = self.auto.find_image_element(name, threshold, model=model)
finally: finally:
if changed: if changed:
self.cfg.unsaved_set_value('set_win_size', old_size) self.cfg.unsaved_set_value('set_win_size', old_size)
self.auto.clear_img_cache()
return (int(pos[0]), int(pos[1])) if pos else None return (int(pos[0]), int(pos[1])) if pos else None
# ---- 动手 (AALC 输入后端,由 config.yaml 决定后台消息/前台输入) ---- # ---- 动手 (AALC 输入后端,由 config.yaml 决定后台消息/前台输入) ----
+45 -10
View File
@@ -2,9 +2,11 @@
"""子进程入口: 跑 AALC 现成的任务流程。 """子进程入口: 跑 AALC 现成的任务流程。
用法: python -m lc_mcp.aalc_task <task> [json_params] 用法: python -m lc_mcp.aalc_task <task> [json_params]
python -m lc_mcp.aalc_task --restore <file> 写回被强杀任务临时覆盖的配置
放在子进程里跑, 停止 = 杀进程树; 不会像 QThread.terminate 那样留下被占用的锁。 放在子进程里跑, 停止 = 杀进程树; 不会像 QThread.terminate 那样留下被占用的锁。
""" """
import json import json
import os
import sys import sys
from .aalc_engine import bootstrap from .aalc_engine import bootstrap
@@ -17,6 +19,42 @@ TASKS = {
'enkephalin': {'times': 'set_lunacy_to_enkephalin'}, 'enkephalin': {'times': 'set_lunacy_to_enkephalin'},
'mirror': {'count': 'set_mirror_count'}, 'mirror': {'count': 'set_mirror_count'},
} }
RESTORE_ENV = 'LCP_RESTORE_FILE'
def apply_overrides(cfg, task, params, restore_path=None):
"""临时覆盖本次运行的次数, 返回原值。
AALC 运行中会整份保存配置(如镜牢后 rotate_team_queue), 覆盖值会被写进 config.yaml。
所以先把原值落盘: 进程被 stop_task 强杀、finally 来不及执行时, TaskRunner 据此写回。
"""
originals = {TASKS[task][name]: cfg.get_value(TASKS[task][name]) for name in params}
if originals and restore_path:
tmp = restore_path + '.tmp'
with open(tmp, 'w', encoding='utf-8') as f:
json.dump(originals, f, ensure_ascii=False)
os.replace(tmp, restore_path)
for name, value in params.items():
cfg.unsaved_set_value(TASKS[task][name], int(value))
return originals
def restore(cfg, originals, restore_path=None):
for key, value in originals.items():
cfg.set_value(key, value)
if originals:
cfg.flush()
if restore_path and os.path.exists(restore_path):
os.remove(restore_path)
def restore_file(path, cfg=None):
with open(path, encoding='utf-8') as f:
originals = json.load(f)
if cfg is None:
bootstrap()
from module.config import cfg
restore(cfg, originals, path)
def run(task, params): def run(task, params):
@@ -28,11 +66,8 @@ def run(task, params):
from tasks.battle import battle from tasks.battle import battle
from utils.path_manager import path_manager from utils.path_manager import path_manager
originals = {} restore_path = os.environ.get(RESTORE_ENV)
for name, value in params.items(): originals = apply_overrides(cfg, task, params, restore_path)
key = TASKS[task][name]
originals[key] = cfg.get_value(key)
cfg.unsaved_set_value(key, int(value))
try: try:
if task == 'all': if task == 'all':
scheme.script_task() scheme.script_task()
@@ -54,14 +89,14 @@ def run(task, params):
}[task]() }[task]()
finally: finally:
retry_monitor.stop() retry_monitor.stop()
# AALC 运行中会自行保存配置(换队伍队列等), 把临时覆盖的次数写回原值 restore(cfg, originals, restore_path)
for key, value in originals.items():
cfg.set_value(key, value)
if originals:
cfg.flush()
def main(argv): def main(argv):
if len(argv) == 3 and argv[1] == '--restore':
restore_file(argv[2])
print('[lc_mcp] restored config from %s' % argv[2], flush=True)
return 0
if len(argv) < 2 or argv[1] not in TASKS: if len(argv) < 2 or argv[1] not in TASKS:
print('usage: python -m lc_mcp.aalc_task <%s> [json]' % '|'.join(TASKS), file=sys.stderr) print('usage: python -m lc_mcp.aalc_task <%s> [json]' % '|'.join(TASKS), file=sys.stderr)
return 2 return 2
+3 -2
View File
@@ -95,8 +95,9 @@ class _Handler(socketserver.StreamRequestHandler):
def _handle_session(self): def _handle_session(self):
rfile, wfile = self.rfile, self.wfile rfile, wfile = self.rfile, self.wfile
first = rfile.readline(256).decode('utf-8', 'replace').strip() # 按字节比较: compare_digest 对含非 ASCII 字符的 str 会抛 TypeError
if not (first.startswith('AUTH ') and hmac.compare_digest(first[5:], self.server.token)): first = rfile.readline(256).strip()
if not (first.startswith(b'AUTH ') and hmac.compare_digest(first[5:], self.server.token.encode('ascii'))):
wfile.write(b'DENIED\n') wfile.write(b'DENIED\n')
log('rejected connection from %s' % (self.client_address,)) log('rejected connection from %s' % (self.client_address,))
return return
+81 -36
View File
@@ -9,14 +9,21 @@ import sys
import threading import threading
import time import time
from .aalc_task import RESTORE_ENV
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
IS_WIN = sys.platform.startswith('win') IS_WIN = sys.platform.startswith('win')
RESTORE_SUFFIX = '.restore.json'
def default_argv(task, params): def default_argv(task, params):
return [sys.executable, '-m', 'lc_mcp.aalc_task', task, json.dumps(params)] return [sys.executable, '-m', 'lc_mcp.aalc_task', task, json.dumps(params)]
def default_restore_argv(path):
return [sys.executable, '-m', 'lc_mcp.aalc_task', '--restore', path]
def tail(path, n): def tail(path, n):
try: try:
with open(path, 'rb') as f: with open(path, 'rb') as f:
@@ -30,10 +37,11 @@ def tail(path, n):
class TaskRunner: class TaskRunner:
def __init__(self, run_dir, argv_factory=default_argv, keep_logs=20): def __init__(self, run_dir, argv_factory=default_argv, keep_logs=20, restore_argv=default_restore_argv):
self.run_dir = os.path.abspath(run_dir) self.run_dir = os.path.abspath(run_dir)
os.makedirs(self.run_dir, exist_ok=True) os.makedirs(self.run_dir, exist_ok=True)
self.argv_factory = argv_factory self.argv_factory = argv_factory
self.restore_argv = restore_argv
self.keep_logs = keep_logs self.keep_logs = keep_logs
self._lock = threading.Lock() self._lock = threading.Lock()
self.proc = None self.proc = None
@@ -42,61 +50,98 @@ class TaskRunner:
def running(self): def running(self):
return self.proc is not None and self.proc.poll() is None return self.proc is not None and self.proc.poll() is None
def _env(self):
return dict(os.environ, PYTHONPATH=REPO + os.pathsep + os.environ.get('PYTHONPATH', ''),
PYTHONIOENCODING='utf-8', PYTHONUNBUFFERED='1')
def _pending_restores(self):
"""被强杀/崩溃的任务留下的原值文件(不含正在运行的任务自己的)。新的在前:
有多份时最后写回的是最早一份, 也就是真正的原值。"""
current = self.info.get('restore') if self.info and self.running() else None
return [p for p in sorted(glob.glob(os.path.join(self.run_dir, '*' + RESTORE_SUFFIX)), reverse=True)
if p != current]
def _restore_leftovers(self):
"""把遗留的原值写回 config.yaml。成功返回 None, 否则返回原因。"""
for path in self._pending_restores():
name = os.path.basename(path)
try:
r = subprocess.run(self.restore_argv(path), cwd=REPO, env=self._env(),
stdin=subprocess.DEVNULL, capture_output=True, timeout=120)
except (OSError, subprocess.TimeoutExpired) as e:
return 'config restore from %s failed: %s' % (name, e)
if r.returncode != 0 or os.path.exists(path):
out = (r.stdout + r.stderr).decode('utf-8', 'replace').strip().splitlines()[-3:]
return 'config restore from %s failed (exit %s): %s' % (name, r.returncode, ' | '.join(out))
return None
def start(self, task, params): def start(self, task, params):
with self._lock: with self._lock:
if self.running(): if self.running():
return {'ok': False, 'error': '已有任务在跑: %s (先 stop_task)' % self.info['task']} return {'ok': False, 'error': '已有任务在跑: %s (先 stop_task)' % self.info['task']}
err = self._restore_leftovers()
if err:
# 否则新任务会把残留的覆盖值当成原值记下, 真正的原值就丢了
return {'ok': False, 'error': err + '; 写回 config.yaml 之前不启动新任务'}
stamp = time.strftime('%Y%m%d_%H%M%S') stamp = time.strftime('%Y%m%d_%H%M%S')
log_path = os.path.join(self.run_dir, 'task_%s_%s.log' % (stamp, task)) base = os.path.join(self.run_dir, 'task_%s_%s' % (stamp, task))
env = dict(os.environ, PYTHONPATH=REPO + os.pathsep + os.environ.get('PYTHONPATH', ''), env = self._env()
PYTHONIOENCODING='utf-8', PYTHONUNBUFFERED='1') env[RESTORE_ENV] = base + RESTORE_SUFFIX
kw = {'creationflags': subprocess.CREATE_NEW_PROCESS_GROUP} if IS_WIN else {'start_new_session': True} kw = {'creationflags': subprocess.CREATE_NEW_PROCESS_GROUP} if IS_WIN else {'start_new_session': True}
with open(log_path, 'wb') as logf: with open(base + '.log', 'wb') as logf:
self.proc = subprocess.Popen(self.argv_factory(task, params), cwd=REPO, env=env, self.proc = subprocess.Popen(self.argv_factory(task, params), cwd=REPO, env=env,
stdin=subprocess.DEVNULL, stdout=logf, stderr=subprocess.STDOUT, **kw) stdin=subprocess.DEVNULL, stdout=logf, stderr=subprocess.STDOUT, **kw)
self.info = {'task': task, 'params': params, 'log': log_path.replace('\\', '/'), self.info = {'task': task, 'params': params, 'log': (base + '.log').replace('\\', '/'),
'started': time.time(), 'stopped_by_user': False} 'restore': env[RESTORE_ENV], 'started': time.time(), 'stopped_by_user': False}
self._prune() self._prune()
return {'ok': True, 'task': task, 'pid': self.proc.pid, 'log': self.info['log']} return {'ok': True, 'task': task, 'pid': self.proc.pid, 'log': self.info['log']}
def status(self, lines=30): def status(self, lines=30):
with self._lock: with self._lock:
if self.info is None: if self.info is None:
return {'ok': True, 'state': 'idle'} r = {'ok': True, 'state': 'idle'}
code = self.proc.poll()
if code is None:
state = 'running'
elif self.info['stopped_by_user']:
state = 'stopped'
else: else:
state = 'succeeded' if code == 0 else 'failed' code = self.proc.poll()
r = {'ok': True, 'state': state, 'task': self.info['task'], 'params': self.info['params'], if code is None:
'elapsed_s': int((self.info.get('ended') or time.time()) - self.info['started']), state = 'running'
'log': self.info['log'], 'log_tail': tail(self.info['log'], max(1, min(int(lines), 200)))} else:
if code is not None: self.info.setdefault('ended', time.time())
r['exit_code'] = code state = 'stopped' if self.info['stopped_by_user'] else 'succeeded' if code == 0 else 'failed'
self.info.setdefault('ended', time.time()) r = {'ok': True, 'state': state, 'task': self.info['task'], 'params': self.info['params'],
'elapsed_s': int((self.info.get('ended') or time.time()) - self.info['started']),
'log': self.info['log'], 'log_tail': tail(self.info['log'], max(1, min(int(lines), 200)))}
if code is not None:
r['exit_code'] = code
pending = self._pending_restores()
if pending:
r['pending_restore'] = [os.path.basename(p) for p in pending]
return r return r
def stop(self): def stop(self):
with self._lock: with self._lock:
if not self.running(): if self.running():
return {'ok': True, 'state': 'idle', 'note': '没有在跑的任务'} self.info['stopped_by_user'] = True
self.info['stopped_by_user'] = True pid = self.proc.pid
pid = self.proc.pid if IS_WIN:
if IS_WIN: subprocess.run(['taskkill', '/PID', str(pid), '/T', '/F'], capture_output=True)
subprocess.run(['taskkill', '/PID', str(pid), '/T', '/F'], capture_output=True) else:
else: try:
os.killpg(pid, signal.SIGKILL)
except ProcessLookupError:
pass
try: try:
os.killpg(pid, signal.SIGKILL) self.proc.wait(timeout=10)
except ProcessLookupError: except subprocess.TimeoutExpired:
pass return {'ok': False, 'error': '进程 %d 10 秒内没退出' % pid}
try: self.info['ended'] = time.time()
self.proc.wait(timeout=10) r = {'ok': True, 'state': 'stopped', 'task': self.info['task']}
except subprocess.TimeoutExpired: else:
return {'ok': False, 'error': '进程 %d 10 秒内没退出' % pid} r = {'ok': True, 'state': 'idle', 'note': '没有在跑的任务'}
self.info['ended'] = time.time() # 强杀时任务的 finally 不会执行, 由这里把临时覆盖的次数写回
return {'ok': True, 'state': 'stopped', 'task': self.info['task']} err = self._restore_leftovers()
if err:
r.update(ok=False, error=err)
return r
def _prune(self): def _prune(self):
logs = sorted(glob.glob(os.path.join(self.run_dir, 'task_*.log')), key=os.path.getmtime) logs = sorted(glob.glob(os.path.join(self.run_dir, 'task_*.log')), key=os.path.getmtime)
+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__':
+4 -2
View File
@@ -7,7 +7,9 @@ from pathlib import Path
class Session: class Session:
def __init__(self): def __init__(self, raise_on_error=True):
# False: 工具失败按 {'ok': False, ...} 返回, 由调用方观察后继续; 协议层错误仍抛异常
self.raise_on_error = raise_on_error
root = Path(__file__).resolve().parents[1] root = Path(__file__).resolve().parents[1]
self.process = subprocess.Popen( self.process = subprocess.Popen(
[sys.executable, '-X', 'utf8', str(root / 'lc_mcp' / 'bridge.py')], [sys.executable, '-X', 'utf8', str(root / 'lc_mcp' / 'bridge.py')],
@@ -42,7 +44,7 @@ class Session:
def call(self, name, arguments=None): def call(self, name, arguments=None):
result = self.request('tools/call', {'name': name, 'arguments': arguments or {}}) result = self.request('tools/call', {'name': name, 'arguments': arguments or {}})
data = json.loads(result['content'][0]['text']) data = json.loads(result['content'][0]['text'])
if result.get('isError'): if result.get('isError') and self.raise_on_error:
raise RuntimeError(data) raise RuntimeError(data)
return data return data
+89 -1
View File
@@ -1,3 +1,5 @@
import json
import os
import tempfile import tempfile
import sys import sys
import unittest import unittest
@@ -435,7 +437,38 @@ class SceneRegressionTest(unittest.TestCase):
def test_disabled_selection_is_not_clickable_candidate(self): def test_disabled_selection_is_not_clickable_candidate(self):
state = {'items': [{'text': '选择', 'x': 100, 'y': 10, 'w': 80, 'h': 40}]} state = {'items': [{'text': '选择', 'x': 100, 'y': 10, 'w': 80, 'h': 40}]}
self.assertFalse(any(o['action']['tool'] == 'click_text' for o in agent.candidates(state).values())) self.assertFalse(any(k.startswith('button_') for k in agent.candidates(state)))
@mock.patch.object(agent, 'button_enabled', return_value=True)
def test_generic_button_clicks_the_checked_item(self, _enabled):
# 重新按文字搜索会多一次截图+OCR, 且可能点到背景里同名的另一个按钮
state = {'items': [{'text': '继续', 'x': 100, 'y': 200, 'w': 80, 'h': 40}]}
self.assertEqual(agent.candidates(state)['button_0']['action'],
{'tool': 'click', 'args': {'x': 140, 'y': 220}})
def test_screenshot_decoded_once_for_repeated_checks(self):
from PIL import Image
with tempfile.TemporaryDirectory() as folder:
shot = Path(folder) / 'shot.png'
Image.new('L', (200, 100), 200).save(shot)
state = {'screen': {'path': str(shot)}}
item = {'x': 10, 'y': 10, 'w': 50, 'h': 20}
with mock.patch.object(Image, 'open', wraps=Image.open) as opened:
self.assertTrue(agent.button_enabled(state, item))
self.assertTrue(agent.button_enabled(state, dict(item, x=100)))
self.assertEqual(opened.call_count, 1)
Image.new('L', (200, 101), 0).save(shot) # 同路径的新内容不能命中旧缓存
self.assertFalse(agent.button_enabled(state, item))
def test_map_templates_are_loaded_once(self):
screenshot = Path(__file__).resolve().parent / 'fixtures' / 'floor4_white_events.png'
if not (agent.REPO_ROOT / 'vendor' / 'aalc' / 'assets').is_dir():
self.skipTest('AALC submodule assets unavailable')
state = {'screen': {'path': str(screenshot)}}
first = agent.map_candidates(state)
loads = agent._map_template.cache_info().misses
self.assertEqual(agent.map_candidates(state), first)
self.assertEqual(agent._map_template.cache_info().misses, loads)
def test_battle_cinematic_keeps_macro_available(self): def test_battle_cinematic_keeps_macro_available(self):
state = {'items': [{'text': '破裂+5', 'x': 100, 'y': 10, 'w': 80, 'h': 40}]} state = {'items': [{'text': '破裂+5', 'x': 100, 'y': 10, 'w': 80, 'h': 40}]}
@@ -464,5 +497,60 @@ class SceneRegressionTest(unittest.TestCase):
self.assertNotIn('battle', agent.candidates(state, previous_battle=True)) self.assertNotIn('battle', agent.candidates(state, previous_battle=True))
class MainLoopTest(unittest.TestCase):
def run_main(self, home, bridge, steps=3):
config = home / 'cfg.json'
config.write_text('{}', encoding='utf-8')
with mock.patch.object(agent, 'RUNTIME_HOME', home), \
mock.patch.object(agent, 'STOP_FILE', home / 'JEV_STOP'), \
mock.patch.object(agent, 'Bridge', return_value=bridge) as make_bridge, \
mock.patch.object(agent, 'acquire_single_instance', return_value=lambda: None), \
mock.patch.object(agent.time, 'sleep'), \
mock.patch.dict(os.environ, {'OPENCODE_API_KEY': 'test-key', 'LC_FIXED_FALLBACK': '1'}), \
mock.patch.object(sys, 'argv', ['agent', str(config), '--steps', str(steps)]):
agent.main()
return make_bridge
def test_completed_run_archives_context_for_next_run(self):
with tempfile.TemporaryDirectory() as folder:
home = Path(folder)
(home / 'jev_shop_context.json').write_text(json.dumps(
{'refresh_used': 0, 'floor': 5, 'claim_requested': True, 'battery_obtained': True}))
bridge = mock.Mock()
bridge.call.return_value = {'ok': True, 'items': [
{'text': t, 'x': 0, 'y': 0, 'w': 1, 'h': 1, 'score': 1.0} for t in ('探索状态', '进入')]}
self.run_main(home, bridge)
self.assertEqual(json.loads((home / 'jev_mirror_status.json').read_text())['state'], 'completed')
# 否则下一局一启动就因 claim_requested 被判定为“已完成”
self.assertFalse((home / 'jev_shop_context.json').exists())
self.assertEqual(len(list(home.glob('jev_shop_context.*.json'))), 1)
def test_tool_errors_are_observed_instead_of_aborting(self):
with tempfile.TemporaryDirectory() as folder:
home = Path(folder)
bridge = mock.Mock()
bridge.call.return_value = {'ok': False, 'error': '截图失败'}
make_bridge = self.run_main(home, bridge, steps=2)
make_bridge.assert_called_once_with(raise_on_error=False)
status = json.loads((home / 'jev_mirror_status.json').read_text())
self.assertNotIn('error', status) # 没有因异常退出, 而是按“截图暂不可用”继续等待
self.assertEqual(status['choice'], 'wait')
class SessionTest(unittest.TestCase):
def test_tool_errors_can_be_returned_instead_of_raised(self):
import mcp_session
session = mcp_session.Session.__new__(mcp_session.Session)
session.request = mock.Mock(return_value={'isError': True, 'content': [
{'type': 'text', 'text': json.dumps({'ok': False, 'error': 'text not found'})}]})
session.raise_on_error = False
self.assertEqual(session.call('click_text', {'text': 'x'}), {'ok': False, 'error': 'text not found'})
session.raise_on_error = True
with self.assertRaises(RuntimeError):
session.call('click_text', {'text': 'x'})
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
+110 -2
View File
@@ -153,6 +153,92 @@ class AalcTaskArgsTest(unittest.TestCase):
self.assertEqual(aalc_task.main(['x', 'mirror', '{"exp": 1}']), 2) self.assertEqual(aalc_task.main(['x', 'mirror', '{"exp": 1}']), 2)
self.assertEqual(aalc_task.main(['x', 'nope']), 2) self.assertEqual(aalc_task.main(['x', 'nope']), 2)
def test_originals_saved_before_override_and_restored(self):
from lc_mcp import aalc_task
class Cfg:
def __init__(self):
self.values, self.saved, self.flushes = {'set_mirror_count': 1}, {}, 0
def get_value(self, key):
return self.values[key]
def unsaved_set_value(self, key, value):
self.values[key] = value
def set_value(self, key, value):
self.values[key] = self.saved[key] = value
def flush(self):
self.flushes += 1
cfg = Cfg()
with tempfile.TemporaryDirectory() as d:
path = os.path.join(d, 'x.restore.json')
originals = aalc_task.apply_overrides(cfg, 'mirror', {'count': 5}, path)
self.assertEqual(cfg.values['set_mirror_count'], 5)
with open(path, encoding='utf-8') as f:
self.assertEqual(json.load(f), {'set_mirror_count': 1})
self.assertEqual(originals, {'set_mirror_count': 1})
# 任务进程被强杀后, TaskRunner 用 --restore 读文件写回
aalc_task.restore_file(path, cfg)
self.assertEqual((cfg.saved, cfg.flushes), ({'set_mirror_count': 1}, 1))
self.assertFalse(os.path.exists(path))
self.assertEqual(aalc_task.apply_overrides(cfg, 'reward', {}, path), {})
self.assertFalse(os.path.exists(path)) # 没有覆盖就不留恢复文件
class TaskRestoreTest(unittest.TestCase):
"""run_task 的临时次数会随 AALC 中途存盘写进 config.yaml; 强杀后必须写回原值。"""
CHILD = ('import json, os, time; p = os.environ["LCP_RESTORE_FILE"]; '
'open(p, "w").write(json.dumps({"set_mirror_count": 1})); time.sleep(30)')
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.marker = os.path.join(self.tmp.name, 'restored.txt')
self.runs = os.path.join(self.tmp.name, 'runs')
self.restore_code = 'import os, sys; open(sys.argv[2], "a").write(open(sys.argv[1]).read()); os.remove(sys.argv[1])'
self.runner = TaskRunner(self.runs, argv_factory=lambda t, p: [sys.executable, '-c', self.CHILD],
restore_argv=lambda path: [sys.executable, '-c', self.restore_code, path, self.marker])
def tearDown(self):
self.runner.stop()
self.tmp.cleanup()
def _wait_for_restore_file(self):
deadline = time.time() + 10
while time.time() < deadline:
if any(n.endswith('.restore.json') for n in os.listdir(self.runs)):
return
time.sleep(0.05)
self.fail('child never wrote its restore file')
def test_stop_restores_overridden_config(self):
self.assertTrue(self.runner.start('mirror', {'count': 5})['ok'])
self._wait_for_restore_file()
r = self.runner.stop()
self.assertEqual((r['ok'], r['state']), (True, 'stopped'), r)
with open(self.marker) as f:
self.assertEqual(json.load(f), {'set_mirror_count': 1})
self.assertFalse([n for n in os.listdir(self.runs) if n.endswith('.restore.json')])
def test_leftover_restored_before_next_start_or_start_refused(self):
leftover = os.path.join(self.runs, 'task_20260101_000000_mirror.restore.json')
with open(leftover, 'w') as f:
json.dump({'set_mirror_count': 2}, f)
self.runner.restore_argv = lambda path: [sys.executable, '-c', 'import sys; sys.exit(1)', path]
r = self.runner.start('mirror', {'count': 5})
self.assertFalse(r['ok'])
self.assertIn('restore', r['error'])
self.assertFalse(self.runner.running())
self.assertEqual(self.runner.status()['pending_restore'], [os.path.basename(leftover)])
self.runner.restore_argv = lambda path: [sys.executable, '-c', self.restore_code, path, self.marker]
self.assertTrue(self.runner.start('mirror', {'count': 5})['ok'])
with open(self.marker) as f:
self.assertEqual(json.load(f), {'set_mirror_count': 2})
self.assertFalse(os.path.exists(leftover))
class RuntimeTest(unittest.TestCase): class RuntimeTest(unittest.TestCase):
@@ -261,13 +347,30 @@ server.main(['--stdio'])
engine.auto.find_image_element.side_effect = match engine.auto.find_image_element.side_effect = match
self.assertEqual(engine.find_image('battle/setting_assets.png'), (1233, 42)) self.assertEqual(engine.find_image('battle/setting_assets.png'), (1233, 42))
self.assertEqual(engine.cfg.set_win_size, 1080) self.assertEqual(engine.cfg.set_win_size, 1080)
self.assertEqual(engine.auto.clear_img_cache.call_count, 2) self.assertEqual(engine.auto.clear_img_cache.call_count, 1)
engine.auto.find_image_element.side_effect = RuntimeError('match failed') engine.auto.find_image_element.side_effect = RuntimeError('match failed')
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
engine.find_image('battle/setting_assets.png') engine.find_image('battle/setting_assets.png')
self.assertEqual(engine.cfg.set_win_size, 1080) self.assertEqual(engine.cfg.set_win_size, 1080)
self.assertEqual(engine.auto.clear_img_cache.call_count, 4) # 同一窗口尺寸复用已缩放的模板缓存 (clear_img_cache 每次都会全量 gc)
self.assertEqual(engine.auto.clear_img_cache.call_count, 1)
engine.cfg.set_value.assert_not_called() engine.cfg.set_value.assert_not_called()
engine.auto.find_image_element.side_effect = lambda *a, **k: (10, 10)
engine._grab.return_value = mock.Mock(width=1920, height=1080)
engine.find_image('battle/setting_assets.png')
self.assertEqual(engine.auto.clear_img_cache.call_count, 2)
def test_template_list_is_scanned_once(self):
from lc_mcp import aalc_engine
engine = aalc_engine.AalcEngine.__new__(aalc_engine.AalcEngine)
with tempfile.TemporaryDirectory() as root:
os.makedirs(os.path.join(root, 'default', 'share', 'battle'))
open(os.path.join(root, 'default', 'share', 'battle', 'turn_assets.png'), 'wb').close()
engine.images_root = root
with mock.patch.object(aalc_engine.glob, 'glob', wraps=aalc_engine.glob.glob) as scan:
self.assertEqual(engine.list_images('battle/'), ['battle/turn_assets.png'])
self.assertEqual(engine.list_images(), ['battle/turn_assets.png'])
self.assertEqual(scan.call_count, 6) # 2 主题 x 3 语言, 只扫一轮
def free_port(): def free_port():
@@ -308,6 +411,11 @@ class TransportTest(unittest.TestCase):
s.sendall(b'AUTH wrong\n') s.sendall(b'AUTH wrong\n')
self.assertEqual(s.makefile('rb').readline().strip(), b'DENIED') self.assertEqual(s.makefile('rb').readline().strip(), b'DENIED')
def test_non_ascii_token_is_denied_not_crashed(self):
with socket.create_connection(('127.0.0.1', self.port)) as s:
s.sendall('AUTH 密码\n'.encode('utf-8'))
self.assertEqual(s.makefile('rb').readline().strip(), b'DENIED')
def test_bridge_end_to_end(self): def test_bridge_end_to_end(self):
env = dict(os.environ, LCP_PORT=str(self.port), HOME=self.tmp.name, USERPROFILE=self.tmp.name) env = dict(os.environ, LCP_PORT=str(self.port), HOME=self.tmp.name, USERPROFILE=self.tmp.name)
os.makedirs(os.path.join(self.tmp.name, 'lc_mcp')) os.makedirs(os.path.join(self.tmp.name, 'lc_mcp'))