fix: recover interrupted Web battles in 2.0.10

This commit is contained in:
AttilaNet
2026-11-14 10:56:43 +08:00
parent bbdf578c87
commit 9f3945a717
24 changed files with 1912 additions and 127 deletions
+55 -14
View File
@@ -588,14 +588,43 @@ class Engine:
or self._breakthrough_pending and now - self._breakthrough_pending_last_check >= 120))
return not self.demo and bool(automation or self._progression_pending() and now - self.last_pending_check >= 120)
def _resume_pending_task_clock(self):
def pending_battle_recovery(self):
"""Read-only recovery contract for desktop/Web pending guards."""
record = self.battle_journal.pending(self.snapshot) if self.snapshot is not None else None
status = self.battles.recovery_status(record, snapshot=self.snapshot)
if status["resumable"]:
if self.game_data is None and (record.get("source_task_type_id") or record.get("task_type_id")):
from .game_data import load_progression_data
self.game_data = load_progression_data()
if not self._intent_policy(record).enabled:
origin = ("自动扫荡" if record.get("kind") == "map_battle" else
"自动委托" if self._is_commission(record) else "自动主线")
return {"state": "blocked_policy", "resumable": False,
"message": "等待启用" + origin + "来源策略;已保留待核对战斗"}
return status
def _resume_pending_task_clock(self, *, running=None, generation=None):
record = self.battle_journal.pending(self.snapshot)
if not self.auto or self._progression_cancel.is_set() or not self.battles.resumable_task_clock(record):
if not self.pending_battle_recovery()["resumable"]:
return ""
if record.get("kind") == "map_battle":
with self._progression_cancel_lock:
if (not (self.auto if running is None else running is True)
or generation is not None and generation != self._progression_cancel_generation):
return ""
# Web deliberately keeps auto=False. Only its current running intent
# may rearm an older cancellation; a newer Stop always wins.
if running is True and self._progression_cancel.is_set():
self._progression_cancel = threading.Event()
cancel = self._progression_cancel
if cancel.is_set():
return ""
if not record.get("task_id"):
intent = {key: record.get(key) for key in ("kind", "task_id", "task_type_id", "step_number", "stage_category",
"stage_id", "difficulty", "battle_kind", "team", "path")}
return self.battles.resume_task_clock(self.client, self.snapshot, intent, self._map_policy(), cancel=self._progression_cancel)
"stage_id", "difficulty", "battle_kind", "team", "path", "source_task_id", "source_task_type_id", "source_step_number")}
policy = self._intent_policy(intent)
if self.pending_battle_recovery()["state"] == "retry_local_runtime":
self._select_battle_runtime(record["battle_mode"])
return self.battles.resume_task_clock(self.client, self.snapshot, intent, policy, cancel=cancel)
pending = self.progression_planner.pending(self.snapshot) if self.progression_planner else None
if not pending or not isinstance(pending.get("intent"), dict):
return ""
@@ -607,8 +636,10 @@ class Engine:
if intent.token in issued and issued[intent.token] != intent.to_dict():
return ""
issued[intent.token] = intent.to_dict()
if self.pending_battle_recovery()["state"] == "retry_local_runtime":
self._select_battle_runtime(record["battle_mode"])
return self.battles.resume_task_clock(self.client, self.snapshot, intent, self._intent_policy(intent),
cancel=self._progression_cancel)
cancel=cancel)
def _battle_capability(self, intent):
try:
@@ -648,7 +679,11 @@ class Engine:
supported, reason = self._commission_offer_checks[key]
if not supported:
intent = replace(intent, block_reason=reason)
if (value(intent, "kind") in ("progression_battle", "battle", "task_battle", "stage_battle", "map_battle")
# Local battle preludes share the planner's cached input preflight.
prelude = value(intent, "task_id") and value(intent, "battle_kind") in battle_kinds and (
value(intent, "kind") == "progression_navigate" or
value(intent, "kind") == "progression_dialogue" and value(intent, "phase") == "before")
if (prelude or value(intent, "kind") in ("progression_battle", "battle", "task_battle", "stage_battle", "map_battle")
or self._is_farm(intent) and not self._is_sweep_intent(intent)) and not value(intent, "block_reason"):
supported, reason = self._battle_capability(intent)
reasons[str(value(intent, "source_task_type_id") or value(intent, "task_type_id"))] = (supported, reason)
@@ -673,14 +708,15 @@ class Engine:
stage_id=value(farm, "stage_id"), difficulty=value(farm, "difficulty"))
reason = row.get("block_reason") or row.get("blocked_reason") or ""
if row.get("battle_kind") in battle_kinds:
supported, runtime_reason = reasons.get(str(row.get("task_type_id")),
(False, "战斗运行时能力尚未核对;配置存在不代表已支持实战"))
row["supported"] = bool(source_supported and supported and not reason)
supported, runtime_reason = reasons.get(str(row.get("task_type_id")), (None, ""))
reason = reason or runtime_reason
row["supported"] = False if not source_supported or reason else supported
else:
row["supported"] = bool(source_supported and not reason)
row["block_reason"] = row["blocked_reason"] = reason
row["reason_kind"] = commission_reason_kind(reason)
row["reason_kind"] = "unchecked" if row["supported"] is None else commission_reason_kind(reason)
if row["supported"] is None and row.get("category") == "mainline" and not self.progression_policy.enabled:
row.setdefault("status", "自动主线策略未启用")
return rows, adjusted
def _battle_request(self, snapshot, intent, policy, request):
@@ -1331,7 +1367,10 @@ class Engine:
@single_flight
@account_serial
def poll(self, *, hatch_only=False, defer_hatch=False):
def poll(self, *, hatch_only=False, defer_hatch=False, resume_pending=None):
if resume_pending is not None and type(resume_pending) is not bool:
raise ValueError("resume_pending must be a boolean or None")
recovery_generation = self._progression_cancel_generation
self.last_executed = False
settled = False
if self.demo:
@@ -1355,7 +1394,7 @@ class Engine:
if self._progression_pending():
self.pending_hatches = []
pending_map = (self.battle_journal.pending(self.snapshot) or {}).get("kind") == "map_battle"
recovered = self._resume_pending_task_clock() if not self.demo else ""
recovered = self._resume_pending_task_clock(running=resume_pending, generation=recovery_generation) if not self.demo else ""
if recovered:
self.last_executed = True
if not self.battle_journal.pending(self.snapshot):
@@ -1364,7 +1403,9 @@ class Engine:
self.stage_cache.get(self.snapshot, refresh=True)
return self.snapshot, self.plans(), recovered
pending = self.battle_journal.pending(self.snapshot) or {}
return self.snapshot, [], message + ";" + (pending.get("message") or "有待核对的任务 / 战斗,已暂停冲突操作") + ";后台每2分钟自动核对"
recovery = self.pending_battle_recovery()
detail = recovery["message"] or pending.get("message") or "有待核对的任务 / 战斗,已暂停冲突操作"
return self.snapshot, [], message + ";" + detail
explore_pending = self.explore_planner.pending(self.snapshot)
if explore_pending or self._breakthrough_pending:
self.pending_hatches = []