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
+70 -14
View File
@@ -335,12 +335,54 @@ class AccountWorker:
self._legacy_pending.pop(identity)
self._save_run_intents()
def _pending_battle_recovery(self):
classify = getattr(self.engine, "pending_battle_recovery", None)
if callable(classify):
status = classify()
return status if isinstance(status, dict) else {}
# Compatibility for older Engine test doubles without the public API.
if self.engine.snapshot is not None:
record = self.engine.battle_journal.pending(self.engine.snapshot)
if record:
if self.engine.battles.resumable_task_clock(record) is True:
return {"resumable": True, "message": "战果已保存;等待结算时间及对应策略启用后续交"}
return {}
def _pending_execution_reason(self):
suffix = ";禁止自动重发,请在游戏内核对结果"
if self.active_identity in self._legacy_pending:
return "赠礼、装填或喂养请求结果待核对,禁止自动重发;后台继续读取游戏状态,确认后自动恢复已勾选策略"
if self.engine.snapshot is not None and (self.engine._progression_pending()
or self.engine.explore_planner.pending(self.engine.snapshot) or self.engine._breakthrough_pending):
return "存在未确认的任务、战斗、探险或突破请求,禁止自动重发;后台继续核对,核对完成后恢复已勾选策略"
kind = self._legacy_pending[self.active_identity]["kind"]
return {"gift": "赠礼", "hatch": "装填", "feed": "喂养"}[kind] + "请求结果待核对" + suffix
if self.engine.snapshot is None:
return ""
if self.engine._progression_pending():
sweep = self.engine.sweeper.state.get("pending")
if sweep:
return f"扫荡(关卡 {sweep.get('stage', '')}):{sweep.get('phase') or '请求结果待核对'}" + suffix
recovery = self._pending_battle_recovery()
if recovery.get("state") == "blocked_policy":
return recovery.get("message") or "等待启用本次战斗的来源策略后恢复"
if recovery.get("resumable") is not True:
record = self.engine.battle_journal.pending(self.engine.snapshot) or {}
label = record.get("description") or ("战斗" if record.get("stage_id") else "任务")
if record.get("stage_id"):
label += f"(关卡 {record['stage_id']},难度 {record.get('difficulty') or 1})"
detail = record.get("message") or "请求结果待核对"
if record.get("execution_status") == "timed_out" or record.get("terminal_reason") == "worker_wall_clock_limit":
detail = "本地战斗计算超时"
if (not record.get("battle_data") and not record.get("settlement_dispatched")
and not record.get("settlement_confirmed")
and record.get("path") in ("", "battleService/battleStart", "stageService/stageStart")):
detail += ",尚未提交结算"
return label + ":" + detail + suffix
explore = self.engine.explore_planner.pending(self.engine.snapshot)
if explore:
label = "探险出发" if explore.get("kind") == "explore_start" else "探险领奖"
return f"{label}(队伍 {explore.get('team_id', '')}):{explore.get('message') or '请求结果待核对'}" + suffix
if self.engine._breakthrough_pending:
record = self.engine._breakthrough_pending
label = "潜能提升" if record.get("kind") == "potential" else "突破"
return label + ":" + (record.get("message") or "请求结果待核对") + suffix
return ""
def _refresh_execution(self):
@@ -372,6 +414,9 @@ class AccountWorker:
state, message = "recovering", self.connection_message
elif not self._running:
state, message = "offline", self.connection_message
elif (recovery := self._pending_battle_recovery()).get("resumable") is True:
state = "recovering"
message = recovery.get("message") or "等待对应策略允许恢复"
else:
state, message = "running", "已勾选的策略将自动执行"
return {"strategy_requested": requested, "manual_stop": not requested,
@@ -1117,15 +1162,24 @@ class AccountWorker:
self.schedule.next_fast[kind] = math.inf
auto = True
value = self.engine.poll_automation(kind)
elif kind == "poll":
auto = self._set_auto(self._running)
value = self.engine.poll(defer_hatch=True)
self.schedule.poll_finished(self.config)
elif kind == "reconcile":
self._set_auto(False)
# Engine's durable pending barriers reconcile before planning.
# A check never silently replays an uncertain write.
value = self.engine.poll(defer_hatch=True)
elif kind in ("poll", "reconcile"):
recovering = self._pending_battle_recovery().get("resumable") is True
with self.lock:
# Recover only the original entry, with general automation
# disabled. Stop/config changes still revoke this job.
auto = self._set_auto(self._running and kind == "poll" and not recovering)
resume_pending = bool(recovering and self._running and self.strategy_requested
and not self._closing and not self.manager._closing and job.generation == self._generation)
if resume_pending:
job.consuming = True
self.check_intent()
if callable(getattr(self.engine, "pending_battle_recovery", None)):
value = self.engine.poll(defer_hatch=True, resume_pending=resume_pending)
else:
# Older Engine doubles use auto to authorize saved results.
if resume_pending:
auto = self._set_auto(True)
value = self.engine.poll(defer_hatch=True)
self.schedule.poll_finished(self.config)
else:
self._set_auto(False)
@@ -1140,7 +1194,9 @@ class AccountWorker:
# by the current Engine generation turns that switch off.
if auto and job.generation == self._generation and not self.engine.auto:
if self._pending_execution_reason():
self.stop()
# A pending barrier pauses execution, not user intent
# or the generation of other queued local edits.
self._refresh_execution()
else:
self._execution_failed(ValueError(value[2] or "策略暂不可用"))
elif auto and job.generation == self._generation: