Explorar o código

Merge upstream dev through 8b4dc669 with desktop recovery and theme updates

Integrate 65311c50..8b4dc669 (12 commits, including five merges).
Adopt the FindJade scheduler priority, invite-again debounce, updated
collaboration invitation PNGs/coordinates, and all 19 dream-flower
Shikigami theme assets plus config/mapping/translation.

Adopt existing-window login detection, the opt-in desktop force-restart
setting, and allow Restart past a missing-window pre-screenshot. Resolve
the Restart conflict by preserving the local checked desktop_stop_client
path: failed cleanup must stop recovery instead of claiming success.
Retain bounded retries, PID ownership/cleanup/rebinding protections,
anti-ban idle cleanup, cropped confirmations, mail/raid/exploration,
activity/AreaBoss navigation, purchase limits and costume cache behavior.
No incoming commit was skipped wholesale.

Fix an incoming recovery gap demonstrated by a failing replay: ownership
PIDs persist for the whole session, so they cannot alone mean newly
launched. Track the first-login exemption explicitly, clear it on login
completion/adoption, and do not restore it on later logout. A later stuck
owned instance can now be forcibly restarted; a fresh one is reused.

Validation: 496 pytest tests passed, including both FrogBoss suites and
12 new desktop recovery/theme cases. 15 Python syntax checks, 12 production
imports, 4 JSON files, 23 PNGs and 23 rule metadata pairs passed, with
92 synthetic image matching cases. Nine sensitive files and 115 methods
outside targeted changes retained; config defaults and priority checked.
git diff --check passed after trimming new upstream comment whitespace.
Six existing deprecation warnings (Pydantic, imp, docstring escapes).
No emulator or desktop game run.
unknown hai 2 semanas
pai
achega
990deeb326
Modificáronse 42 ficheiros con 425 adicións e 27 borrados
  1. 3 0
      assets/i18n/zh-CN.json
  2. 2 1
      config/template.json
  3. 1 1
      module/config/config_manual.py
  4. 5 1
      module/device/desktop_device.py
  5. 3 1
      module/device/handle.py
  6. 25 3
      module/device/method/desktop_impl.py
  7. 8 1
      script.py
  8. 1 0
      tasks/Component/Costume/config.py
  9. 1 1
      tasks/Component/Costume/costume_base.py
  10. 41 0
      tasks/Component/CostumeShikigami/assets.py
  11. 173 0
      tasks/Component/CostumeShikigami/sk13/image.json
  12. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_check_records_13.png
  13. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_record_soul_back_13.png
  14. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_1_13.png
  15. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_2_13.png
  16. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_3_13.png
  17. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_4_13.png
  18. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_5_13.png
  19. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_6_13.png
  20. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_7_13.png
  21. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_check_in_13.png
  22. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_click_present_13.png
  23. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_1_13.png
  24. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_2_13.png
  25. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_3_13.png
  26. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_4_13.png
  27. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_sou_team_present_13.png
  28. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_soul_preset_13.png
  29. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_st_replace_13.png
  30. BIN=BIN
      tasks/Component/CostumeShikigami/sk13/sk13_st_souls_13.png
  31. 1 1
      tasks/Component/GeneralInvite/general_invite.py
  32. 4 4
      tasks/GlobalGame/assets.py
  33. BIN=BIN
      tasks/GlobalGame/gg/gg_g_accept.png
  34. BIN=BIN
      tasks/GlobalGame/gg/gg_g_ignore.png
  35. BIN=BIN
      tasks/GlobalGame/gg/gg_g_jade.png
  36. BIN=BIN
      tasks/GlobalGame/gg/gg_g_reject.png
  37. 5 5
      tasks/GlobalGame/gg/image.json
  38. 26 6
      tasks/Restart/script_task.py
  39. 3 0
      tasks/Script/config_desktop.py
  40. 22 0
      tests/tasks/Component/Costume/test_costume.py
  41. 97 0
      tests/tasks/Restart/test_desktop_recovery.py
  42. 4 2
      tests/tasks/Restart/test_restart.py

+ 3 - 0
assets/i18n/zh-CN.json

@@ -5,6 +5,8 @@
   "game_path_help": "游戏安装目录(如 D:\\yys),留空自动查找。",
   "game_path_help": "游戏安装目录(如 D:\\yys),留空自动查找。",
   "launch_instance": "启动客户端实例",
   "launch_instance": "启动客户端实例",
   "launch_instance_help": "一次性按钮:点击后按上面的设置启动本配置对应的客户端,已在运行则跳过。\n它是按钮不是开关,点击后自动弹回,不会保存为开启。",
   "launch_instance_help": "一次性按钮:点击后按上面的设置启动本配置对应的客户端,已在运行则跳过。\n它是按钮不是开关,点击后自动弹回,不会保存为开启。",
+  "restart_when_window_exists": "窗口存在时强制重启",
+  "restart_when_window_exists_help": "Restart 任务遇到桌面客户端时,若窗口还在,是否先杀掉再重新拉起(真正的重启),而不是只重走一次登录流程。\n开启后能救回卡在坏状态的客户端,但会中断正在手玩的客户端;默认关闭。",
   "profile_dir": "档案目录",
   "profile_dir": "档案目录",
   "profile_dir_help": "多开账号隔离:每个配置填一个不同目录(如 D:/yys-profiles/p1),无需手动创建,OAS 会自动准备好。\n填了之后每个实例拥有独立的游戏数据和账号。\n留空 = 和直接双击打开的游戏共享账号。",
   "profile_dir_help": "多开账号隔离:每个配置填一个不同目录(如 D:/yys-profiles/p1),无需手动创建,OAS 会自动准备好。\n填了之后每个实例拥有独立的游戏数据和账号。\n留空 = 和直接双击打开的游戏共享账号。",
   "window_pos": "窗口位置",
   "window_pos": "窗口位置",
@@ -157,6 +159,7 @@
   "costume_battle_scene_type": "战斗场景",
   "costume_battle_scene_type": "战斗场景",
   "costume_battle_scene_type_help": "",
   "costume_battle_scene_type_help": "",
   "costume_shikigami_12": "拾光之窗",
   "costume_shikigami_12": "拾光之窗",
+  "costume_shikigami_13": "梦隙闲花",
   "save_touch_fish": "惠比寿的摸鱼行动",
   "save_touch_fish": "惠比寿的摸鱼行动",
   "save_touch_fish_help": "默认使用全部存储按钮,主要目的是为了配合真蛇每周打一次即可获得俩次奖励(并不会节省体力)",
   "save_touch_fish_help": "默认使用全部存储按钮,主要目的是为了配合真蛇每周打一次即可获得俩次奖励(并不会节省体力)",
   "green_mark_type": "绿标匹配方式",
   "green_mark_type": "绿标匹配方式",

+ 2 - 1
config/template.json

@@ -21,7 +21,8 @@
       "profile_dir": "",
       "profile_dir": "",
       "window_pos": "",
       "window_pos": "",
       "window_title": "",
       "window_title": "",
-      "launch_instance": false
+      "launch_instance": false,
+      "restart_when_window_exists": false
     },
     },
     "error": {
     "error": {
       "handle_error": true,
       "handle_error": true,

+ 1 - 1
module/config/config_manual.py

@@ -17,7 +17,7 @@ class ConfigManual:
         > BondlingFairyland > EvoZone > GoryouRealm > HeroTest
         > BondlingFairyland > EvoZone > GoryouRealm > HeroTest
         > CollectiveMissions
         > CollectiveMissions
         > Pets > TalismanPass > Delegation > Hyakkiyakou
         > Pets > TalismanPass > Delegation > Hyakkiyakou
-        > Secret > WeeklyTrifles > MysteryShop > Duel 
+        > Secret > WeeklyTrifles > MysteryShop > Duel > FindJade
         > TrueOrochi > RichMan
         > TrueOrochi > RichMan
         > MetaDemon > FrogBoss > FloatParade > Quiz > KittyShop > DyeTrials > MemoryScrolls
         > MetaDemon > FrogBoss > FloatParade > Quiz > KittyShop > DyeTrials > MemoryScrolls
         """
         """

+ 5 - 1
module/device/desktop_device.py

@@ -37,7 +37,9 @@ class DesktopDevice:
             if not hwnd:
             if not hwnd:
                 hwnd = find_desktop_window_by_pid(
                 hwnd = find_desktop_window_by_pid(
                     located_pid, desktop_window_titles(self.config))
                     located_pid, desktop_window_titles(self.config))
-            self.desktop_bind_pid(located_pid, hwnd)
+            # 认领的是已存在的窗口,可能是用户手动开的且已登录,
+            # 登录态由 find_desktop_login_popup 实测,不能假设未登录
+            self.desktop_bind_pid(located_pid, hwnd, just_launched=False)
             return True
             return True
         return self.launch_desktop_client()
         return self.launch_desktop_client()
 
 
@@ -56,6 +58,8 @@ class DesktopDevice:
     def desktop_mark_logged_in(self) -> None:
     def desktop_mark_logged_in(self) -> None:
         """标记客户端已完成登录(Restart 的登录流程成功后调用)。"""
         """标记客户端已完成登录(Restart 的登录流程成功后调用)。"""
         self._desktop_login_done = True
         self._desktop_login_done = True
+        # 首次登录完成后,后续 Restart 必须能重建本会话启动的客户端。
+        self._desktop_just_launched = False
 
 
     def desktop_mark_logged_out(self) -> None:
     def desktop_mark_logged_out(self) -> None:
         """标记客户端处于未登录态。"""
         """标记客户端处于未登录态。"""

+ 3 - 1
module/device/handle.py

@@ -182,7 +182,9 @@ class Handle(DesktopWindow):
             self.root_handle = ''
             self.root_handle = ''
             pid, hwnd = self._desktop_locate_window()
             pid, hwnd = self._desktop_locate_window()
             if pid and hwnd:
             if pid and hwnd:
-                self.desktop_bind_pid(pid, hwnd)
+                # 认领的是已存在的窗口,可能早已登录,
+                # 登录态由 find_desktop_login_popup 实测,不能假设未登录
+                self.desktop_bind_pid(pid, hwnd, just_launched=False)
                 logger.info(f'Desktop client window bound: PID={pid}, hwnd={hwnd}')
                 logger.info(f'Desktop client window bound: PID={pid}, hwnd={hwnd}')
                 return
                 return
             if pid:
             if pid:

+ 25 - 3
module/device/method/desktop_impl.py

@@ -905,6 +905,19 @@ class DesktopWindow:
         logger.warning(f'Desktop MPay login popup still present after {wait}s, Enter had no effect')
         logger.warning(f'Desktop MPay login popup still present after {wait}s, Enter had no effect')
         return False
         return False
 
 
+    def desktop_window_is_own_launch(self) -> bool:
+        """仅首次登录前豁免本轮新实例,避免刚启动就强杀重拉。"""
+        if not getattr(self, '_desktop_just_launched', False):
+            return False
+        pid = self.desktop_pid()
+        spawned = getattr(self, '_desktop_spawned_pids', None)
+        if not spawned or pid is None:
+            return False
+        try:
+            return int(str(pid)) in spawned
+        except (TypeError, ValueError):
+            return False
+
     def desktop_window_exists(self) -> bool:
     def desktop_window_exists(self) -> bool:
         """桌面模式:目标 PID 对应窗口仍存在即视为客户端运行中。
         """桌面模式:目标 PID 对应窗口仍存在即视为客户端运行中。
 
 
@@ -1284,12 +1297,15 @@ class DesktopWindow:
                            f'无法区分归属,将重新拉起一个实例')
                            f'无法区分归属,将重新拉起一个实例')
         return None, 0
         return None, 0
 
 
-    def desktop_bind_pid(self, pid, hwnd=0) -> None:
+    def desktop_bind_pid(self, pid, hwnd=0, just_launched=True) -> None:
         """把运行时 PID/HWND 绑定到实例;PID 不写配置,只进档案状态文件。
         """把运行时 PID/HWND 绑定到实例;PID 不写配置,只进档案状态文件。
 
 
         PID 每次重启都会变,落配置只会认领一个死进程;配了档案目录时写
         PID 每次重启都会变,落配置只会认领一个死进程;配了档案目录时写
         oas_instance.json 供下次认领。同时强制显示器常亮,防止电源超时关屏导致
         oas_instance.json 供下次认领。同时强制显示器常亮,防止电源超时关屏导致
         BitBlt 截图返回黑帧卡死登录循环。
         BitBlt 截图返回黑帧卡死登录循环。
+
+        just_launched=True 表示窗口是本轮新拉起的,必然未登录;
+        False 表示认领已存在的窗口,登录态由 find_desktop_login_popup() 实测判断。
         """
         """
         self.root_handle = str(pid)
         self.root_handle = str(pid)
         if hwnd:
         if hwnd:
@@ -1311,8 +1327,14 @@ class DesktopWindow:
         self.is_desktop_window = True
         self.is_desktop_window = True
         # 换了新 hwnd,截图句柄缓存必须同步失效,否则截图仍走上一个客户端的废句柄
         # 换了新 hwnd,截图句柄缓存必须同步失效,否则截图仍走上一个客户端的废句柄
         self._desktop_clear_handle_cache()
         self._desktop_clear_handle_cache()
-        # 新绑定的客户端刚启动,必然未登录,需先走 restart 登录流程
-        self._desktop_login_done = False
+        self._desktop_just_launched = just_launched
+        if just_launched:
+            # 新拉起的客户端必然停在登录页
+            self._desktop_login_done = False
+        else:
+            # 认领已存在的窗口:可能早已登录。已登录的客户端全系统枚举不到
+            # MPAY_LOGIN 顶层窗口(见 find_desktop_login_popup),以此区分
+            self._desktop_login_done = not self.find_desktop_login_popup()
         # 客户端已拉起,强制保持显示器常亮(线程级状态,随客户端关闭解除)
         # 客户端已拉起,强制保持显示器常亮(线程级状态,随客户端关闭解除)
         desktop_keep_screen_on(True)
         desktop_keep_screen_on(True)
         # 档案状态文件:下次启动凭它认领「这个档案的实例是哪个 PID」
         # 档案状态文件:下次启动凭它认领「这个档案的实例是哪个 PID」

+ 8 - 1
script.py

@@ -568,7 +568,14 @@ class Script:
             return False
             return False
 
 
         try:
         try:
-            self.device.screenshot()
+            try:
+                self.device.screenshot()
+            except GameNotRunningError:
+                # 桌面版窗口不在时预截图必抛;Restart 的职责就是拉起客户端,
+                # 不能让它被这层拦住(实测恢复循环里 Restart 永远跑不到)
+                if command != 'Restart':
+                    raise
+                logger.info('Client window gone, skip pre-screenshot and let Restart recover it')
             module_name = 'script_task'
             module_name = 'script_task'
             module_path = str(Path.cwd() / 'tasks' / command / (module_name+'.py'))
             module_path = str(Path.cwd() / 'tasks' / command / (module_name+'.py'))
             logger.info(f'module_path: {module_path}, module_name: {module_name}')
             logger.info(f'module_path: {module_path}, module_name: {module_name}')

+ 1 - 0
tasks/Component/Costume/config.py

@@ -55,6 +55,7 @@ class ShikigamiType(str, Enum):
     COSTUME_SHIKIGAMI_10 = 'costume_shikigami_10'  # 今宵胧明
     COSTUME_SHIKIGAMI_10 = 'costume_shikigami_10'  # 今宵胧明
     COSTUME_SHIKIGAMI_11 = 'costume_shikigami_11'  # 花札幕台
     COSTUME_SHIKIGAMI_11 = 'costume_shikigami_11'  # 花札幕台
     COSTUME_SHIKIGAMI_12 = 'costume_shikigami_12'  # 拾光之窗
     COSTUME_SHIKIGAMI_12 = 'costume_shikigami_12'  # 拾光之窗
+    COSTUME_SHIKIGAMI_13 = 'costume_shikigami_13'  # 梦隙闲花
 
 
 # 签到主题
 # 签到主题
 class SignType(str, Enum):
 class SignType(str, Enum):

+ 1 - 1
tasks/Component/Costume/costume_base.py

@@ -99,7 +99,7 @@ shikigami_costume_model = {
         'I_ST_SOULS': f'I_ST_SOULS_{i}',
         'I_ST_SOULS': f'I_ST_SOULS_{i}',
         'I_ST_REPLACE': f'I_ST_REPLACE_{i}',
         'I_ST_REPLACE': f'I_ST_REPLACE_{i}',
     }
     }
-    for i in range(1, 13)  # 目前支持 COSTUME_SHIKIGAMI_1 到 COSTUME_SHIKIGAMI_12
+    for i in range(1, 14)  # 目前支持 COSTUME_SHIKIGAMI_1 到 COSTUME_SHIKIGAMI_13
 }
 }
 
 
 class CostumeBase:
 class CostumeBase:

+ 41 - 0
tasks/Component/CostumeShikigami/assets.py

@@ -174,6 +174,47 @@ class CostumeShikigamiAssets:
 	I_SOU_CLICK_PRESENT_12 = RuleImage(roi_front=(975,292,34,34), roi_back=(971,134,42,530), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk12/sk12_sou_click_present_12.png")
 	I_SOU_CLICK_PRESENT_12 = RuleImage(roi_front=(975,292,34,34), roi_back=(971,134,42,530), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk12/sk12_sou_click_present_12.png")
 
 
 
 
+	# Image Rule Assets
+	# 用于判断在式神录
+	I_CHECK_RECORDS_13 = RuleImage(roi_front=(279,87,30,35), roi_back=(274,82,40,45), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_check_records_13.png")
+	# 退出式神录
+	I_RECORD_SOUL_BACK_13 = RuleImage(roi_front=(21,10,38,36), roi_back=(14,4,52,54), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_record_soul_back_13.png")
+	# 用于判断是否在式神录里面
+	I_SOU_CHECK_IN_13 = RuleImage(roi_front=(277,84,34,36), roi_back=(277,84,34,36), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_check_in_13.png")
+	# 预设
+	I_SOUL_PRESET_13 = RuleImage(roi_front=(350,87,56,25), roi_back=(344,82,70,36), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_soul_preset_13.png")
+	# 队伍预设标题
+	I_SOU_TEAM_PRESENT_13 = RuleImage(roi_front=(730,82,103,30), roi_back=(724,76,115,42), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_team_present_13.png")
+	# 第一队切换
+	I_SOU_SWITCH_1_13 = RuleImage(roi_front=(975,137,35,34), roi_back=(970,132,45,44), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_1_13.png")
+	# 第二队切换
+	I_SOU_SWITCH_2_13 = RuleImage(roi_front=(975,291,35,34), roi_back=(970,286,45,44), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_2_13.png")
+	# 第三队切换
+	I_SOU_SWITCH_3_13 = RuleImage(roi_front=(975,445,35,34), roi_back=(970,440,45,44), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_3_13.png")
+	# 第四队切换
+	I_SOU_SWITCH_4_13 = RuleImage(roi_front=(975,599,35,34), roi_back=(970,594,45,44), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_4_13.png")
+	# 点击预设队伍
+	I_SOU_CLICK_PRESENT_13 = RuleImage(roi_front=(975,291,35,34), roi_back=(971,134,42,530), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_click_present_13.png")
+	# 第一组选中态
+	I_SOU_CHECK_GROUP_1_13 = RuleImage(roi_front=(1088,79,18,55), roi_back=(1088,79,18,55), threshold=0.9, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_1_13.png")
+	# 第二组选中态
+	I_SOU_CHECK_GROUP_2_13 = RuleImage(roi_front=(1088,149,18,55), roi_back=(1088,149,18,55), threshold=0.9, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_2_13.png")
+	# 第三组选中态
+	I_SOU_CHECK_GROUP_3_13 = RuleImage(roi_front=(1088,219,18,55), roi_back=(1088,219,18,55), threshold=0.9, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_3_13.png")
+	# 第四组选中态
+	I_SOU_CHECK_GROUP_4_13 = RuleImage(roi_front=(1088,289,18,55), roi_back=(1088,289,18,55), threshold=0.9, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_4_13.png")
+	# 第五组选中态
+	I_SOU_CHECK_GROUP_5_13 = RuleImage(roi_front=(1088,359,18,55), roi_back=(1088,359,18,55), threshold=0.9, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_5_13.png")
+	# 第六组选中态
+	I_SOU_CHECK_GROUP_6_13 = RuleImage(roi_front=(1088,429,18,55), roi_back=(1088,429,18,55), threshold=0.9, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_6_13.png")
+	# 第七组选中态
+	I_SOU_CHECK_GROUP_7_13 = RuleImage(roi_front=(1088,499,18,55), roi_back=(1088,499,18,55), threshold=0.9, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_7_13.png")
+	# 御魂按钮
+	I_ST_SOULS_13 = RuleImage(roi_front=(1195,228,37,85), roi_back=(1187,220,53,100), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_st_souls_13.png")
+	# 更换按钮
+	I_ST_REPLACE_13 = RuleImage(roi_front=(863,141,94,125), roi_back=(853,131,114,145), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk13/sk13_st_replace_13.png")
+
+
 	# Image Rule Assets
 	# Image Rule Assets
 	# 用于判断在式神录 
 	# 用于判断在式神录 
 	I_CHECK_RECORDS_2 = RuleImage(roi_front=(267,76,51,46), roi_back=(265,74,55,50), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk2/sk2_check_records_2.png")
 	I_CHECK_RECORDS_2 = RuleImage(roi_front=(267,76,51,46), roi_back=(265,74,55,50), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeShikigami/sk2/sk2_check_records_2.png")

+ 173 - 0
tasks/Component/CostumeShikigami/sk13/image.json

@@ -0,0 +1,173 @@
+[
+  {
+    "itemName": "check_records_13",
+    "imageName": "sk13_check_records_13.png",
+    "roiFront": "279,87,30,35",
+    "roiBack": "274,82,40,45",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "用于判断在式神录"
+  },
+  {
+    "itemName": "record_soul_back_13",
+    "imageName": "sk13_record_soul_back_13.png",
+    "roiFront": "21,10,38,36",
+    "roiBack": "14,4,52,54",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "退出式神录"
+  },
+  {
+    "itemName": "sou_check_in_13",
+    "imageName": "sk13_sou_check_in_13.png",
+    "roiFront": "277,84,34,36",
+    "roiBack": "277,84,34,36",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "用于判断是否在式神录里面"
+  },
+  {
+    "itemName": "soul_preset_13",
+    "imageName": "sk13_soul_preset_13.png",
+    "roiFront": "350,87,56,25",
+    "roiBack": "344,82,70,36",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "预设"
+  },
+  {
+    "itemName": "sou_team_present_13",
+    "imageName": "sk13_sou_team_present_13.png",
+    "roiFront": "730,82,103,30",
+    "roiBack": "724,76,115,42",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "队伍预设标题"
+  },
+  {
+    "itemName": "sou_switch_1_13",
+    "imageName": "sk13_sou_switch_1_13.png",
+    "roiFront": "975,137,35,34",
+    "roiBack": "970,132,45,44",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "第一队切换"
+  },
+  {
+    "itemName": "sou_switch_2_13",
+    "imageName": "sk13_sou_switch_2_13.png",
+    "roiFront": "975,291,35,34",
+    "roiBack": "970,286,45,44",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "第二队切换"
+  },
+  {
+    "itemName": "sou_switch_3_13",
+    "imageName": "sk13_sou_switch_3_13.png",
+    "roiFront": "975,445,35,34",
+    "roiBack": "970,440,45,44",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "第三队切换"
+  },
+  {
+    "itemName": "sou_switch_4_13",
+    "imageName": "sk13_sou_switch_4_13.png",
+    "roiFront": "975,599,35,34",
+    "roiBack": "970,594,45,44",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "第四队切换"
+  },
+  {
+    "itemName": "sou_click_present_13",
+    "imageName": "sk13_sou_click_present_13.png",
+    "roiFront": "975,291,35,34",
+    "roiBack": "971,134,42,530",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "点击预设队伍"
+  },
+  {
+    "itemName": "sou_check_group_1_13",
+    "imageName": "sk13_sou_check_group_1_13.png",
+    "roiFront": "1088,79,18,55",
+    "roiBack": "1088,79,18,55",
+    "method": "Template matching",
+    "threshold": 0.9,
+    "description": "第一组选中态"
+  },
+  {
+    "itemName": "sou_check_group_2_13",
+    "imageName": "sk13_sou_check_group_2_13.png",
+    "roiFront": "1088,149,18,55",
+    "roiBack": "1088,149,18,55",
+    "method": "Template matching",
+    "threshold": 0.9,
+    "description": "第二组选中态"
+  },
+  {
+    "itemName": "sou_check_group_3_13",
+    "imageName": "sk13_sou_check_group_3_13.png",
+    "roiFront": "1088,219,18,55",
+    "roiBack": "1088,219,18,55",
+    "method": "Template matching",
+    "threshold": 0.9,
+    "description": "第三组选中态"
+  },
+  {
+    "itemName": "sou_check_group_4_13",
+    "imageName": "sk13_sou_check_group_4_13.png",
+    "roiFront": "1088,289,18,55",
+    "roiBack": "1088,289,18,55",
+    "method": "Template matching",
+    "threshold": 0.9,
+    "description": "第四组选中态"
+  },
+  {
+    "itemName": "sou_check_group_5_13",
+    "imageName": "sk13_sou_check_group_5_13.png",
+    "roiFront": "1088,359,18,55",
+    "roiBack": "1088,359,18,55",
+    "method": "Template matching",
+    "threshold": 0.9,
+    "description": "第五组选中态"
+  },
+  {
+    "itemName": "sou_check_group_6_13",
+    "imageName": "sk13_sou_check_group_6_13.png",
+    "roiFront": "1088,429,18,55",
+    "roiBack": "1088,429,18,55",
+    "method": "Template matching",
+    "threshold": 0.9,
+    "description": "第六组选中态"
+  },
+  {
+    "itemName": "sou_check_group_7_13",
+    "imageName": "sk13_sou_check_group_7_13.png",
+    "roiFront": "1088,499,18,55",
+    "roiBack": "1088,499,18,55",
+    "method": "Template matching",
+    "threshold": 0.9,
+    "description": "第七组选中态"
+  },
+  {
+    "itemName": "st_souls_13",
+    "imageName": "sk13_st_souls_13.png",
+    "roiFront": "1195,228,37,85",
+    "roiBack": "1187,220,53,100",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "御魂按钮"
+  },
+  {
+    "itemName": "st_replace_13",
+    "imageName": "sk13_st_replace_13.png",
+    "roiFront": "863,141,94,125",
+    "roiBack": "853,131,114,145",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "更换按钮"
+  }
+]

BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_check_records_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_record_soul_back_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_1_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_2_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_3_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_4_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_5_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_6_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_check_group_7_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_check_in_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_click_present_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_1_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_2_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_3_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_switch_4_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_sou_team_present_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_soul_preset_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_st_replace_13.png


BIN=BIN
tasks/Component/CostumeShikigami/sk13/sk13_st_souls_13.png


+ 1 - 1
tasks/Component/GeneralInvite/general_invite.py

@@ -507,7 +507,7 @@ class GeneralInvite(BaseTask, GeneralInviteAssets):
                 self.screenshot()
                 self.screenshot()
                 if self.appear(self.I_UI_CHECK):
                 if self.appear(self.I_UI_CHECK):
                     break
                     break
-                if self.appear_then_click(self.I_UI_UNCHECK, interval=1):
+                if self.appear_then_click(self.I_UI_UNCHECK, interval=3):
                     continue
                     continue
         else:
         else:
             logger.info('Click no default invite')
             logger.info('Click no default invite')

+ 4 - 4
tasks/GlobalGame/assets.py

@@ -12,17 +12,17 @@ class GlobalGameAssets:
 
 
 	# Image Rule Assets
 	# Image Rule Assets
 	# 接受 
 	# 接受 
-	I_G_ACCEPT = RuleImage(roi_front=(846,471,56,46), roi_back=(810,448,120,192), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_accept.png")
+	I_G_ACCEPT = RuleImage(roi_front=(852,472,42,50), roi_back=(810,448,120,192), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_accept.png")
 	# 拒绝 
 	# 拒绝 
-	I_G_REJECT = RuleImage(roi_front=(846,561,53,51), roi_back=(826,458,99,172), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_reject.png")
+	I_G_REJECT = RuleImage(roi_front=(848,560,48,50), roi_back=(826,458,99,172), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_reject.png")
 	# 勾玉 
 	# 勾玉 
-	I_G_JADE = RuleImage(roi_front=(677,510,43,46), roi_back=(549,483,202,111), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_jade.png")
+	I_G_JADE = RuleImage(roi_front=(682,518,32,38), roi_back=(549,483,202,111), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_jade.png")
 	# 猫粮 
 	# 猫粮 
 	I_G_CAT_FOOD = RuleImage(roi_front=(635,501,59,65), roi_back=(558,472,191,132), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_cat_food.png")
 	I_G_CAT_FOOD = RuleImage(roi_front=(635,501,59,65), roi_back=(558,472,191,132), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_cat_food.png")
 	# 狗粮 
 	# 狗粮 
 	I_G_DOG_FOOD = RuleImage(roi_front=(643,505,60,63), roi_back=(541,485,234,115), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_dog_food.png")
 	I_G_DOG_FOOD = RuleImage(roi_front=(643,505,60,63), roi_back=(541,485,234,115), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_dog_food.png")
 	# 忽略 
 	# 忽略 
-	I_G_IGNORE = RuleImage(roi_front=(859,94,21,21), roi_back=(725,40,197,142), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_ignore.png")
+	I_G_IGNORE = RuleImage(roi_front=(794,88,14,16), roi_back=(725,40,197,142), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_g_ignore.png")
 	# 网络异常 
 	# 网络异常 
 	I_NETWORK_ABNORMAL = RuleImage(roi_front=(583,330,221,67), roi_back=(583,330,221,67), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_network_abnormal.png")
 	I_NETWORK_ABNORMAL = RuleImage(roi_front=(583,330,221,67), roi_back=(583,330,221,67), threshold=0.8, method="Template matching", file="./tasks/GlobalGame/gg/gg_network_abnormal.png")
 	# 已端口游戏服务器,需要重新连接,这种情况一般是断开很久了 
 	# 已端口游戏服务器,需要重新连接,这种情况一般是断开很久了 

BIN=BIN
tasks/GlobalGame/gg/gg_g_accept.png


BIN=BIN
tasks/GlobalGame/gg/gg_g_ignore.png


BIN=BIN
tasks/GlobalGame/gg/gg_g_jade.png


BIN=BIN
tasks/GlobalGame/gg/gg_g_reject.png


+ 5 - 5
tasks/GlobalGame/gg/image.json

@@ -2,7 +2,7 @@
   {
   {
     "itemName": "g_accept",
     "itemName": "g_accept",
     "imageName": "gg_g_accept.png",
     "imageName": "gg_g_accept.png",
-    "roiFront": "846,471,56,46",
+    "roiFront": "852,472,42,50",
     "roiBack": "810,448,120,192",
     "roiBack": "810,448,120,192",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
@@ -11,7 +11,7 @@
   {
   {
     "itemName": "g_reject",
     "itemName": "g_reject",
     "imageName": "gg_g_reject.png",
     "imageName": "gg_g_reject.png",
-    "roiFront": "846,561,53,51",
+    "roiFront": "848,560,48,50",
     "roiBack": "826,458,99,172",
     "roiBack": "826,458,99,172",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
@@ -20,7 +20,7 @@
   {
   {
     "itemName": "g_jade",
     "itemName": "g_jade",
     "imageName": "gg_g_jade.png",
     "imageName": "gg_g_jade.png",
-    "roiFront": "677,510,43,46",
+    "roiFront": "682,518,32,38",
     "roiBack": "549,483,202,111",
     "roiBack": "549,483,202,111",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
@@ -47,7 +47,7 @@
   {
   {
     "itemName": "g_ignore",
     "itemName": "g_ignore",
     "imageName": "gg_g_ignore.png",
     "imageName": "gg_g_ignore.png",
-    "roiFront": "859,94,21,21",
+    "roiFront": "794,88,14,16",
     "roiBack": "725,40,197,142",
     "roiBack": "725,40,197,142",
     "method": "Template matching",
     "method": "Template matching",
     "threshold": 0.8,
     "threshold": 0.8,
@@ -89,4 +89,4 @@
     "threshold": 0.8,
     "threshold": 0.8,
     "description": "description"
     "description": "description"
   }
   }
-]
+]

+ 26 - 6
tasks/Restart/script_task.py

@@ -48,12 +48,15 @@ class ScriptTask(LoginHandler):
     def app_restart(self):
     def app_restart(self):
         logger.hr('App restart')
         logger.hr('App restart')
         if self.device.is_desktop:
         if self.device.is_desktop:
-            # 客户端可能刚被自动拉起并停在登录页,此时直接复用;已登录时才真正重启
-            if self.device.app_is_running():
-                if not self.device.desktop_stop_client():
-                    logger.critical('桌面客户端无法关闭,停止重启以免复用残留实例')
-                    raise RequestHumanTakeover
-            self._desktop_start_and_login()
+            if self.config.script.desktop.restart_when_window_exists:
+                self._desktop_restart_and_login()
+            else:
+                # 客户端可能刚被自动拉起并停在登录页,此时直接复用;已登录时才真正重启
+                if self.device.app_is_running():
+                    if not self.device.desktop_stop_client():
+                        logger.critical('桌面客户端无法关闭,停止重启以免复用残留实例')
+                        raise RequestHumanTakeover
+                self._desktop_start_and_login()
         else:
         else:
             self.device.app_stop()
             self.device.app_stop()
             self.device.app_start()
             self.device.app_start()
@@ -98,6 +101,23 @@ class ScriptTask(LoginHandler):
         logger.critical(f'桌面客户端连续 {DESKTOP_RESTART_ATTEMPTS} 轮启动失败,请检查客户端与机器状态')
         logger.critical(f'桌面客户端连续 {DESKTOP_RESTART_ATTEMPTS} 轮启动失败,请检查客户端与机器状态')
         raise RequestHumanTakeover
         raise RequestHumanTakeover
 
 
+    def _desktop_restart_and_login(self) -> None:
+        """桌面模式:既有窗口就强杀重拉(真正的重启),不存在则只确保启动。
+
+        刚拉起的实例(网易 logo 先出、窗口标题后出,ensure_launched 在标题一出现就绑定)
+        不该再杀一次,否则会唤起两次客户端 —— 由 desktop_window_is_own_launch 区分。
+        """
+        if self.device.desktop_window_exists():
+            if self.device.desktop_window_is_own_launch():
+                logger.info('桌面客户端窗口存在,但这是本轮刚拉起的实例,跳过强制重启')
+            else:
+                logger.info('桌面客户端窗口存在,执行强制重启')
+                if not self.device.desktop_stop_client():
+                    logger.error('桌面客户端窗口存在但无法关闭,拒绝重建以免多开')
+                    raise RequestHumanTakeover(
+                        '桌面客户端窗口存在但无法关闭,请手动结束该进程后重试')
+        self._desktop_start_and_login()
+
     def delay_pending_tasks(self) -> bool:
     def delay_pending_tasks(self) -> bool:
         """
         """
         周三更新游戏的时候延迟
         周三更新游戏的时候延迟

+ 3 - 0
tasks/Script/config_desktop.py

@@ -24,3 +24,6 @@ class Desktop(BaseModel):
     window_title: str = Field(default='', description='window_title_help')
     window_title: str = Field(default='', description='window_title_help')
     # 一次性按钮:点击后启动本配置对应的客户端实例;执行后自动弹回 false
     # 一次性按钮:点击后启动本配置对应的客户端实例;执行后自动弹回 false
     launch_instance: bool = Field(default=False, description='launch_instance_help')
     launch_instance: bool = Field(default=False, description='launch_instance_help')
+    # Restart 遇到桌面客户端时,窗口存在也强制重启(杀进程重拉)
+    restart_when_window_exists: bool = Field(
+        default=False, description='restart_when_window_exists_help')

+ 22 - 0
tests/tasks/Component/Costume/test_costume.py

@@ -9,6 +9,8 @@ from module.atom.image import RuleImage
 from tasks.Component.Costume import costume_base
 from tasks.Component.Costume import costume_base
 from tasks.Component.Costume.assets import CostumeAssets
 from tasks.Component.Costume.assets import CostumeAssets
 from tasks.Component.Costume.config import MainType, BattleSceneType
 from tasks.Component.Costume.config import MainType, BattleSceneType
+from tasks.Component.Costume.config import ShikigamiType
+from tasks.Component.CostumeShikigami.assets import CostumeShikigamiAssets
 from tasks.Component.GeneralBattle.assets import GeneralBattleAssets
 from tasks.Component.GeneralBattle.assets import GeneralBattleAssets
 
 
 
 
@@ -21,6 +23,26 @@ def screenshot_for(asset):
     return screenshot
     return screenshot
 
 
 
 
+def test_shikigami_13_switch_updates_all_cached_rules():
+    host = costume_base.CostumeBase()
+    before = costume_base.shikigami_costume_model[ShikigamiType.COSTUME_SHIKIGAMI_12]
+    after = costume_base.shikigami_costume_model[ShikigamiType.COSTUME_SHIKIGAMI_13]
+    references = {}
+    for key, name in before.items():
+        rule = deepcopy(getattr(CostumeShikigamiAssets, name))
+        setattr(host, key, rule)
+        references[key] = rule
+        assert rule.match(screenshot_for(rule))
+    host.check_costume_shikigami(ShikigamiType.COSTUME_SHIKIGAMI_13)
+    assert len(after) == 19
+    for key, name in after.items():
+        rule = getattr(host, key)
+        target = getattr(CostumeShikigamiAssets, name)
+        assert rule is references[key]
+        assert rule.file == target.file
+        assert rule.match(screenshot_for(target))
+
+
 @pytest.mark.parametrize('animated_type', [MainType.COSTUME_MAIN_13, MainType.COSTUME_MAIN_17])
 @pytest.mark.parametrize('animated_type', [MainType.COSTUME_MAIN_13, MainType.COSTUME_MAIN_17])
 def test_switch_animated_to_static_and_back_preserves_shared_reference(monkeypatch, animated_type):
 def test_switch_animated_to_static_and_back_preserves_shared_reference(monkeypatch, animated_type):
     # Match real costume templates without mutating the process-wide asset objects.
     # Match real costume templates without mutating the process-wide asset objects.

+ 97 - 0
tests/tasks/Restart/test_desktop_recovery.py

@@ -0,0 +1,97 @@
+from types import SimpleNamespace
+from unittest.mock import Mock
+
+import pytest
+
+import script as script_module
+from module.device.method import desktop_impl as desktop
+from module.device.desktop_device import DesktopDevice
+from module.exception import GameNotRunningError, RequestHumanTakeover, TaskEnd
+from tasks.Restart.script_task import ScriptTask
+
+
+@pytest.mark.parametrize('just_launched,popup,expected', [
+    (True, 0, False), (False, 0, True), (False, 99, False),
+])
+def test_binding_distinguishes_new_client_from_existing_login(monkeypatch, just_launched, popup, expected):
+    window = desktop.DesktopWindow()
+    window.desktop_profile_dir = lambda: None
+    window.find_desktop_login_popup = Mock(return_value=popup)
+    monkeypatch.setattr(desktop, 'desktop_keep_screen_on', Mock())
+    monkeypatch.setattr(desktop, 'desktop_write_instance_state', Mock())
+    window.desktop_bind_pid(123, just_launched=just_launched)
+    assert window._desktop_login_done is expected
+    assert window._desktop_just_launched is just_launched
+    assert window.find_desktop_login_popup.call_count == (0 if just_launched else 1)
+
+
+@pytest.mark.parametrize('logged_in', [False, True])
+def test_force_restart_reuses_owned_window_only_until_first_login(logged_in):
+    class Client(desktop.DesktopWindow, DesktopDevice):
+        pass
+
+    client = Client()
+    client.root_handle = '123'
+    client._desktop_spawned_pids = {123}
+    client._desktop_just_launched = True
+    client._desktop_login_done = False
+    client.desktop_window_exists = lambda: True
+    client.desktop_stop_client = Mock(return_value=True)
+    if logged_in:
+        client.desktop_mark_logged_in()
+        # Later loss of login must not restore the fresh-launch exemption.
+        client.desktop_mark_logged_out()
+    task = object.__new__(ScriptTask)
+    task.device = client
+    task._desktop_start_and_login = Mock()
+    task._desktop_restart_and_login()
+    assert client.desktop_stop_client.call_count == int(logged_in)
+    task._desktop_start_and_login.assert_called_once_with()
+
+
+@pytest.mark.parametrize('exists,stop_ok', [(False, True), (True, True), (True, False)])
+def test_force_restart_existing_or_absent_window(exists, stop_ok):
+    task = object.__new__(ScriptTask)
+    task.device = SimpleNamespace(
+        desktop_window_exists=lambda: exists, desktop_window_is_own_launch=lambda: False,
+        desktop_stop_client=Mock(return_value=stop_ok),
+    )
+    task._desktop_start_and_login = Mock()
+    if not stop_ok:
+        with pytest.raises(RequestHumanTakeover):
+            task._desktop_restart_and_login()
+        task._desktop_start_and_login.assert_not_called()
+    else:
+        task._desktop_restart_and_login()
+        task._desktop_start_and_login.assert_called_once_with()
+    assert task.device.desktop_stop_client.call_count == int(exists)
+
+
+@pytest.mark.parametrize('command', ['Restart', 'RealmRaid'])
+def test_missing_prescreenshot_allows_only_restart_to_execute(monkeypatch, command):
+    script = object.__new__(script_module.Script)
+    script.instance_guard = None
+    script._try_acquire_queue_token = Mock(return_value=True)
+    script.device = SimpleNamespace(screenshot=Mock(side_effect=GameNotRunningError('closed')))
+    script.config = SimpleNamespace(task_call=Mock())
+    script.exception_handler = Mock()
+    run = Mock(side_effect=TaskEnd)
+    loader = Mock(return_value=SimpleNamespace(ScriptTask=lambda **kw: SimpleNamespace(run=run)))
+    monkeypatch.setattr(script_module, 'load_module', loader)
+    assert script.run(command) is True
+    assert run.call_count == int(command == 'Restart')
+    assert script.config.task_call.call_count == int(command != 'Restart')
+
+
+def test_enabled_restart_option_uses_window_recovery():
+    task = object.__new__(ScriptTask)
+    task.device = SimpleNamespace(is_desktop=True)
+    task.config = SimpleNamespace(
+        script=SimpleNamespace(desktop=SimpleNamespace(restart_when_window_exists=True)),
+        restart=SimpleNamespace(harvest_config=SimpleNamespace(enable_ap=False)),
+    )
+    task._desktop_restart_and_login = Mock()
+    task.set_next_run = Mock()
+    task.app_restart()
+    task._desktop_restart_and_login.assert_called_once_with()
+    task.set_next_run.assert_called_once_with(task='Restart', success=True, finish=True, server=True)

+ 4 - 2
tests/tasks/Restart/test_restart.py

@@ -11,7 +11,8 @@ from tasks.Restart.script_task import ScriptTask
 def test_restart_reuses_only_a_desktop_client_that_is_not_logged_in(desktop, running):
 def test_restart_reuses_only_a_desktop_client_that_is_not_logged_in(desktop, running):
     calls = []
     calls = []
     task = object.__new__(ScriptTask)
     task = object.__new__(ScriptTask)
-    task.config = SimpleNamespace(restart=SimpleNamespace(harvest_config=SimpleNamespace(enable_ap=False)))
+    task.config = SimpleNamespace(restart=SimpleNamespace(harvest_config=SimpleNamespace(enable_ap=False)),
+                                  script=SimpleNamespace(desktop=SimpleNamespace(restart_when_window_exists=False)))
     task.device = SimpleNamespace(
     task.device = SimpleNamespace(
         is_desktop=desktop, app_is_running=lambda: running,
         is_desktop=desktop, app_is_running=lambda: running,
         app_stop=lambda: calls.append('stop'),
         app_stop=lambda: calls.append('stop'),
@@ -28,7 +29,8 @@ def test_restart_reuses_only_a_desktop_client_that_is_not_logged_in(desktop, run
 
 
 def test_failed_desktop_stop_cannot_report_a_successful_restart():
 def test_failed_desktop_stop_cannot_report_a_successful_restart():
     task = object.__new__(ScriptTask)
     task = object.__new__(ScriptTask)
-    task.config = SimpleNamespace(restart=SimpleNamespace(harvest_config=SimpleNamespace(enable_ap=False)))
+    task.config = SimpleNamespace(restart=SimpleNamespace(harvest_config=SimpleNamespace(enable_ap=False)),
+                                  script=SimpleNamespace(desktop=SimpleNamespace(restart_when_window_exists=False)))
     task.device = SimpleNamespace(is_desktop=True, app_is_running=lambda: True,
     task.device = SimpleNamespace(is_desktop=True, app_is_running=lambda: True,
                                   app_stop=lambda: None, desktop_stop_client=lambda: False)
                                   app_stop=lambda: None, desktop_stop_client=lambda: False)
     task._desktop_start_and_login = Mock()
     task._desktop_start_and_login = Mock()