fix: continue hidden sweep battles safely in Web 2.0.12

This commit is contained in:
AttilaNet
2026-11-14 12:41:11 +08:00
parent 7ca2ee13e3
commit 69b59e18dd
21 changed files with 1691 additions and 102 deletions
+99 -2
View File
@@ -134,6 +134,8 @@ class Engine:
self._battle_roster = None
self._battle_runtimes = {"original": battle_runtime, "experimental": experimental_runtime}
self._selected_battle_mode = "original"
self._sweep_recovery_cache = {}
self._sweep_recovery_message = ""
def _map_catalog(self):
if self.resource_catalog is None:
@@ -597,7 +599,7 @@ class Engine:
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
origin = ("自动扫荡" if record.get("kind") in ("map_battle", "map_sweep") else
"自动委托" if self._is_commission(record) else "自动主线")
return {"state": "blocked_policy", "resumable": False,
"message": "等待启用" + origin + "来源策略;已保留待核对战斗"}
@@ -622,6 +624,9 @@ class Engine:
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", "source_task_id", "source_task_type_id", "source_step_number")}
policy = self._intent_policy(intent)
if record.get("sweep_sample_id"):
self._select_battle_runtime(record["battle_mode"])
return self.battles.continue_sweep(self.client, self.snapshot, policy, cancel=cancel)
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)
@@ -868,6 +873,8 @@ class Engine:
return value(intent, "kind") in ("sweep", "progression_sweep", "progression_farm_sweep", "progression_map_sweep") or value(intent, "battle_kind") == "sweep"
def _intent_policy(self, intent):
if value(intent, "kind") == "map_sweep":
return SimpleNamespace(**(vars(self._map_policy()) | {"enabled": self.sweep_policy.enabled}))
if value(intent, "kind") == "map_battle":
return self._map_policy()
if not self._is_commission(intent):
@@ -1227,11 +1234,84 @@ class Engine:
finally:
self._sync_commission_history()
def _recover_sweep_entry(self):
"""Worker-only local migration, immediately after a successful read."""
pending = self.sweeper.state.get("pending")
if not isinstance(pending, dict):
return
message = self.battles.archive_cancelled_sweep(self.snapshot)
if message:
self._sweep_recovery_message = message
return
if self.battles.handoff_sweep(self.snapshot):
return
phases = ("遭遇隐藏关卡,请在游戏内手动战斗", "遇到事件或响应缺少事件状态,请在游戏内处理", "开始请求待确认")
if pending.get("phase") not in phases or pending.get("start_response"):
return
from .sweep_recovery import scan_trace, role_path_matches
owner = str(self.snapshot.raw["master"]["id"])
path = getattr(getattr(self.client, "trace", None), "path", None)
key = owner + ":" + digest(pending)
if key not in self._sweep_recovery_cache:
evidence = scan_trace(path, owner)
recovered = (evidence["valid"] and (pending.get("role_id") is not None
or role_path_matches(self.sweeper.path, getattr(self.client, "session", None), owner, path))
and self.sweeper.recover_hidden_start(self.snapshot, path, events=evidence["events"]))
# Keep only bounded evidence after the one scan. Presentation and
# subsequent snapshots never rescan the 16 MB rotated logs.
evidence.pop("events", None)
self._sweep_recovery_cache[key] = evidence
if recovered:
self.battles.handoff_sweep(self.snapshot)
return
else:
# An unsuccessful legacy proof is not retried on every refresh.
# A completed archive still repairs a crash before sweep.save.
if not any(row.get("sweep_sample_id") == pending.get("sampleId")
and (row.get("outcome") == "abandoned_missing_hidden_id" or row.get("phase") == "legacy_hidden_archived")
and row.get("sweep_entry") == pending
for row in self.battle_journal.role(self.snapshot)["records"]):
return
evidence = self._sweep_recovery_cache[key]
if (pending.get("phase") != phases[0] or set(pending) != {"stage", "level", "phase", "sampleId"}
or not all(isinstance(pending.get(field), str) and pending[field] for field in ("stage", "level", "sampleId"))
or not evidence["valid"] or not evidence["owner_read"] or not evidence["no_consumption"]
or self.snapshot.raw.get("masterCurrentEvent")
or not role_path_matches(self.sweeper.path, getattr(self.client, "session", None), owner, path)
or any(state.get("pending") and not any(row.get("attempt_id") == state["pending"]
and row.get("phase") == "legacy_hidden_archived" and row.get("sweep_entry") == pending
and row.get("sweep_sample_id") == pending["sampleId"]
and str(row.get("role_id")) == owner
for row in state.get("records", [])) for state in self.battle_journal.state["roles"].values())):
return
reservations = [str(key).split(":") for key, count in self.sweeper.state.get("counts", {}).items()
if type(count) is int and count > 0 and str(key).split(":")[-2:] == [pending["stage"], pending["level"]]]
if not reservations or any(len(parts) != 4 or parts[0] != owner for parts in reservations):
return
# Archive before releasing the guard. A crash between these two atomic
# saves is repaired by the sample ID on the next trusted read.
existing = next((row for row in self.battle_journal.role(self.snapshot)["records"]
if row.get("sweep_sample_id") == pending["sampleId"]
and row.get("outcome") == "abandoned_missing_hidden_id" and row.get("sweep_entry") == pending), None)
message = "旧版未保存隐藏关卡编号,本次未完成已放弃;未结算奖励"
if existing is None:
record = self.battle_journal.pending(self.snapshot) or self.battle_journal.begin(self.snapshot,
{"kind": "map_sweep", "stage_id": pending["stage"], "difficulty": pending["level"], "battle_kind": "sweep"},
sweep_entry=pending, sweep_phase="legacy_hidden_archived")
self.battle_journal.update(record, "legacy_hidden_archived", sweep_sample_id=pending["sampleId"],
sweep_entry=deepcopy(pending), archival_evidence={"role_id": owner, "snapshot_read_at": time.time(),
"role_path_bound": True, "trace_files": evidence["files"], "no_later_consumption": True})
self.battle_journal.finish(self.snapshot, record, "abandoned_missing_hidden_id", message)
self.sweeper.state["pending"] = None
self.sweeper.save()
self._sweep_recovery_message = message
@account_serial
def read_snapshot(self, *, sync_master=True):
if self._progression_pending():
self.last_pending_check = time.monotonic()
self.snapshot = self.client.snapshot()
self._recover_sweep_entry()
self._reconcile_procurement()
pending = self.battle_journal.pending(self.snapshot)
pending_id = pending.get("attempt_id") if pending else None
@@ -1384,6 +1464,9 @@ class Engine:
if not hatch_only:
settled = self.settle_pending_growth()
message = "状态已刷新" + (";已请求孵化 / 成长结算" if settled else "")
if self._sweep_recovery_message:
message += ";" + self._sweep_recovery_message
self._sweep_recovery_message = ""
if self.sweeper.recovered_event:
message += ";已解除旧版对已结算奖励事件的误阻塞"
self.sweeper.recovered_event = False
@@ -1451,7 +1534,21 @@ class Engine:
stage = self.sweeper.next(self.snapshot, self.sweep_policy)
if self.demo:
return self.snapshot, plans, "扫荡需要登录真实账号;演示不会提交战斗"
result = self.sweeper.run(self.client, self.snapshot, stage, self.sweep_policy)
origin = {"kind": "map_sweep", "stage_id": stage.stage_id, "difficulty": stage.level,
"battle_kind": "sweep", "battle_mode": "experimental"}
try:
result = self.sweeper.run(self.client, self.snapshot, stage, self.sweep_policy,
cancel=self._progression_cancel, origin=origin)
except CancelledBeforeDispatch:
return self.snapshot, [], "扫荡已停止,未确认的入场已保留"
if self.sweeper.last_outcome == "hidden":
self.battles.handoff_sweep(self.snapshot)
self._select_battle_runtime("experimental")
message = self.battles.continue_sweep(self.client, self.snapshot, self._intent_policy(origin),
cancel=self._progression_cancel)
if not self._progression_cancel.is_set():
self.read_snapshot(sync_master=False)
return self.snapshot, self.plans(), "扫荡遭遇隐藏关卡;" + (message or "隐藏入场已保存,等待来源策略开启后继续")
self.read_snapshot()
label = "探索奖励事件已结算" if self.sweeper.last_outcome == "event" else "扫荡完成"
message = f"{label}:{stage.number} {stage.name} / 难度{stage.level};" + reward_text(self.sweeper.last_reward)