无人值守自动化: 配置预检、选队精确匹配、窗口守护、卡死重跑、计划任务, AALC 升级到 b0f4393

Browse Source
- lc_mcp/preflight.py: 启动前拦截 sinner_order 全 0、会花狂气、窗口放不进桌面 (退出码 3)
- lc_mcp/aalc_patches.py: 队伍名按编号精确匹配 (TEAMS #1 不再命中 #10); 后台守护客户区 16:9
- auto_cycle.py: AALC 卡死后停掉重跑; --mirror 打镜牢; 每轮写 cycle_*.log/.json 报告
- install_unattended.ps1: 注册会话看门狗 (每 5 分钟) 与每日一轮计划任务
- lc_ui.py: pythonw 启动时子进程改用 python 且不弹窗
- vendor/aalc: 7452fad -> b0f4393 (镜牢楼层识别、困牢奖励、排队启动修复)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yws
2026-10-13 03:12:19 +00:00
co-authored by Claude Opus 5.5
parent 6b129ddba3
commit 2190f57abe
10 changed files with 664 additions and 60 deletions
+84
View File
@@ -0,0 +1,84 @@
from pathlib import Path
import sys
import unittest
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import auto_cycle # noqa: E402
class FakeMcp:
"""按脚本返回 task_status, 记录 run_task / stop_task 调用。"""
def __init__(self, *runs):
self.runs = [list(r) for r in runs] # 每次 run_task 后依次返回的状态
self.calls = []
def probe(self, tool, args=None, quiet=True):
self.calls.append((tool, args))
if tool == 'run_task':
self.current = self.runs.pop(0)
return {'ok': True}
if tool == 'task_status':
return self.current.pop(0) if len(self.current) > 1 else self.current[0]
return {'ok': True}
RUNNING = {'state': 'running'}
STALL = {'state': 'running', 'stall': {'empty_team': 30}}
DONE = {'state': 'succeeded', 'exit_code': 0}
class RunAalcTest(unittest.TestCase):
def run_aalc(self, mcp, **kw):
with mock.patch.object(auto_cycle, 'probe', mcp.probe), \
mock.patch.object(auto_cycle, 'log', lambda m: None):
return auto_cycle.run_aalc('daily', {}, 3600, sleep=lambda s: None, **kw)
def tools(self, mcp):
return [t for t, _ in mcp.calls if t != 'task_status']
def test_stall_is_stopped_and_rerun_once(self):
mcp = FakeMcp([RUNNING, STALL, STALL], [RUNNING, DONE])
ok, info = self.run_aalc(mcp)
self.assertTrue(ok, info)
self.assertEqual(self.tools(mcp), ['run_task', 'stop_task', 'run_task'])
def test_single_stall_poll_is_not_enough(self):
mcp = FakeMcp([STALL, RUNNING, DONE])
ok, info = self.run_aalc(mcp)
self.assertTrue(ok, info)
self.assertEqual(self.tools(mcp), ['run_task'])
def test_gives_up_after_retries(self):
mcp = FakeMcp([STALL, STALL], [STALL, STALL])
ok, info = self.run_aalc(mcp, retries=1)
self.assertFalse(ok)
self.assertIn('卡死', info)
self.assertEqual(self.tools(mcp), ['run_task', 'stop_task', 'run_task', 'stop_task'])
def test_preflight_failure_is_not_retried(self):
failed = {'state': 'failed', 'exit_code': 3,
'errors': ['[ERROR] [lc_mcp] preflight: 队伍 1 的 sinner_order 没有罪人']}
mcp = FakeMcp([failed])
ok, info = self.run_aalc(mcp)
self.assertFalse(ok)
self.assertIn('preflight', info)
self.assertEqual(self.tools(mcp), ['run_task'])
def test_exit_zero_with_errors_is_failure(self):
mcp = FakeMcp([dict(DONE, errors=['[ERROR] 超出最大尝试次数,未能进入战斗'])])
ok, info = self.run_aalc(mcp)
self.assertFalse(ok)
def test_mirror_params_are_passed_with_task(self):
mcp = FakeMcp([DONE])
with mock.patch.object(auto_cycle, 'probe', mcp.probe), \
mock.patch.object(auto_cycle, 'log', lambda m: None):
auto_cycle.run_aalc('mirror', {'count': 2}, 3600, sleep=lambda s: None)
self.assertEqual(mcp.calls[0], ('run_task', {'count': 2, 'task': 'mirror'}))
if __name__ == '__main__':
unittest.main()
+163
View File
@@ -0,0 +1,163 @@
import os
import sys
import unittest
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from lc_mcp import aalc_patches, preflight # noqa: E402
def values(**over):
v = {'simulator': False, 'mirror': True, 'set_win_size': 720, 'set_lunacy_to_enkephalin': 0,
'teams_active_queue': [1],
'teams': {'1': {'sinner_order': [1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0]}}}
v.update(over)
return v
class PreflightTest(unittest.TestCase):
def test_healthy_config_passes(self):
self.assertEqual(preflight.check(values(), 'mirror', desktop=(1600, 900), env={}), [])
def test_default_all_zero_sinner_order_is_rejected_for_mirror(self):
v = values(teams={'1': {'sinner_order': [0] * 12}})
problems = preflight.check(v, 'mirror', env={})
self.assertEqual(len(problems), 1)
self.assertIn('sinner_order', problems[0])
# 日常本只按预设编号选队, 不用 sinner_order
self.assertEqual(preflight.check(v, 'daily', env={}), [])
# all 只在勾选了镜牢时检查
self.assertEqual(preflight.check(dict(v, mirror=False), 'all', env={}), [])
self.assertEqual(len(preflight.check(v, 'all', env={})), 1)
def test_missing_team_in_queue(self):
problems = preflight.check(values(teams_active_queue=[2]), 'mirror', env={})
self.assertIn('队伍 2', problems[0])
def test_lunacy_spending_is_rejected_unless_explicit(self):
v = values(set_lunacy_to_enkephalin=2)
self.assertIn('狂气', preflight.check(v, 'daily', env={})[0])
self.assertEqual(preflight.check(v, 'enkephalin', env={}), [])
self.assertEqual(preflight.check(v, 'daily', env={preflight.ALLOW_LUNACY_ENV: '1'}), [])
def test_window_that_cannot_fit_desktop_is_rejected(self):
# 10-04 坑 4: 900 高的客户区加标题栏放不进 1600x900 桌面
problems = preflight.check(values(set_win_size=900), 'daily', desktop=(1600, 900), env={})
self.assertIn('1616x940', problems[0])
self.assertEqual(preflight.check(values(set_win_size=900), 'daily', desktop=(1920, 1080), env={}), [])
self.assertEqual(preflight.check(values(set_win_size=900, simulator=True), 'daily',
desktop=(1600, 900), env={}), [])
def test_collect_reads_pydantic_teams(self):
class Team:
def model_dump(self):
return {'sinner_order': [1]}
class Cfg:
data = {'simulator': False, 'mirror': True, 'set_win_size': 720,
'set_lunacy_to_enkephalin': None, 'teams_active_queue': [1], 'teams': {1: Team()}}
def get_value(self, key):
return self.data[key]
v = preflight.collect(Cfg())
self.assertEqual(v['teams'], {'1': {'sinner_order': [1]}})
self.assertEqual(v['set_lunacy_to_enkephalin'], 0)
class ExactTeamMatchTest(unittest.TestCase):
class Auto:
def find_str_in_text(self, target, ocr_dict):
# AALC 原逻辑: 子串匹配
for text, pos in ocr_dict.items():
if target.replace(' ', '').lower() in text.replace(' ', '').lower():
return pos
return False
def setUp(self):
self.auto = self.Auto()
aalc_patches.install_exact_team_match(self.auto, log=lambda m: None)
def test_team_1_does_not_match_team_10(self):
ocr = {'TEAMS #10': [10, 10], 'TEAMS #11': [10, 20], 'TEAMS #1': [10, 30]}
self.assertEqual(self.auto.find_str_in_text('TEAMS #1', ocr), [10, 30])
self.assertFalse(self.auto.find_str_in_text('TEAMS #1', {'TEAMS #10': [1, 1]}))
def test_ocr_variants_and_chinese(self):
self.assertEqual(self.auto.find_str_in_text('TFAMS#1', {'TEAMS # 1': [5, 5]}), [5, 5])
self.assertEqual(self.auto.find_str_in_text('编队#1', {'编队#12': [1, 1], '编队#1': [2, 2]}), [2, 2])
self.assertEqual(self.auto.find_str_in_text('TEAMS #12', {'TEAMS #12': [3, 3]}), [3, 3])
def test_other_targets_keep_aalc_substring_logic(self):
self.assertEqual(self.auto.find_str_in_text('Hong Lu', {'W HongLu': [7, 7]}), [7, 7])
class FakeHandle:
def __init__(self, size):
self._hwnd = 1
self.size = size
self.isMinimized = False
def width(self, client=False):
return self.size[0]
def height(self, client=False):
return self.size[1]
class FakeScreen:
def __init__(self, size, resizable=True):
self.handle = FakeHandle(size)
self.resizable = resizable
self.calls = []
def adjust_win_size(self, want):
self.calls.append(want)
if self.resizable:
self.handle.size = want
class Cfg:
simulator = False
set_windows = True
set_win_size = 720
class WindowGuardTest(unittest.TestCase):
def guard(self, screen, cfg=Cfg):
self.logs = []
return aalc_patches.WindowGuard(screen, cfg, log=self.logs.append)
def test_correct_size_is_left_alone(self):
screen = FakeScreen((1280, 720))
self.assertEqual(self.guard(screen).check_once(), 'ok')
self.assertEqual(screen.calls, [])
def test_stretched_window_is_restored(self):
# 10-04 坑 5: RDP 重连后被拉伸成 1872x898
screen = FakeScreen((1872, 898))
with mock.patch.object(aalc_patches.time, 'sleep'):
self.assertEqual(self.guard(screen).check_once(), 'fixed')
self.assertEqual(screen.calls, [(1280, 720)])
def test_unfixable_size_is_reported_once(self):
screen = FakeScreen((1600, 877), resizable=False)
guard = self.guard(screen)
with mock.patch.object(aalc_patches.time, 'sleep'):
self.assertEqual(guard.check_once(), 'failed')
self.assertEqual(guard.check_once(), 'failed')
self.assertEqual(len([m for m in self.logs if '[ERROR]' in m]), 1)
def test_user_managed_window_is_not_touched(self):
class Manual(Cfg):
set_windows = False
screen = FakeScreen((1872, 898))
self.assertIsNone(self.guard(screen, Manual).check_once())
self.assertEqual(screen.calls, [])
if __name__ == '__main__':
unittest.main()