"""Persistent, authoritative batch status scanning for CBB libraries. This module only reads status through the Mentor bridge. It deliberately does not infer ``READY`` from files, icons, or previous UI observations. """ from __future__ import annotations from dataclasses import asdict, dataclass from datetime import datetime, timezone import hashlib import json import os from pathlib import Path from typing import Iterable from .status_bridge import (BridgeResponseError, BridgeResult, BridgeUnavailable, StatusBridgeClient, StatusBridgeServer) from .network_path import absolute_preserving_drive TERMINAL_ERRORS = frozenset({"ERROR", "TIMEOUT", "UNAVAILABLE", "MALFORMED", "CRASHED"}) VALID_STATUSES = frozenset({"READY", "PENDING"}) | TERMINAL_ERRORS @dataclass(frozen=True) class LmcFingerprint: path: str size: int mtime_ns: int sha256: str reusable_blocks_sha256: str @dataclass(frozen=True) class BatchStatusRequest: block_name: str lmc_file: Path block_type: str = "RULP" @dataclass(frozen=True) class BatchStatusRecord: block_name: str status: str source: str = "" raw_status: int | str | None = None detail: str = "" fingerprint: str = "" timestamp: str = "" block_type: str = "RULP" @property def completed(self) -> bool: return self.status in VALID_STATUSES def to_json(self) -> dict[str, object]: return asdict(self) def _sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def _fingerprint_tree(root: Path) -> str: """Fingerprint only the top-level CBB set; never descend into payloads.""" digest = hashlib.sha256() if not root.is_dir(): digest.update(b"") return digest.hexdigest() entries: list[tuple[str, int]] = [] try: children = os.scandir(root) except OSError: digest.update(b"") return digest.hexdigest() with children: for entry in children: try: if not entry.is_dir(follow_symlinks=False): continue metadata = entry.stat(follow_symlinks=False) entries.append((entry.name, metadata.st_mtime_ns)) except OSError: entries.append((entry.name, 0)) for name, mtime_ns in sorted(entries, key=lambda item: item[0].casefold()): digest.update(name.encode("utf-8", errors="surrogatepass")) digest.update(b"\0" + str(mtime_ns).encode("ascii") + b"\n") return digest.hexdigest() def fingerprint_library(lmc: Path) -> LmcFingerprint: """Return a checkpoint fingerprint without reading the reusable payload. The lightweight cache key includes LMC metadata and the top-level CBB set. Authoritative state is still queried from Mentor; no CBB payload tree is recursively traversed merely to decide whether a status scan can resume. """ path = absolute_preserving_drive(lmc) stat = path.stat() reusable = path.parent / "ReusableBlocksLP" reusable_sha256 = _fingerprint_tree(reusable) # Metadata alone is not a safe checkpoint key: on network shares and on # file systems with coarse timestamp resolution, two different LMC # contents can have the same size and mtime. Include the actual file # digest so a changed central library can never reuse an old status row. content_sha256 = _sha256_file(path) combined = hashlib.sha256() combined.update(str(path).casefold().encode('utf-8', errors='surrogatepass')) combined.update(b'\0') combined.update(content_sha256.encode("ascii")) combined.update(b"\0" + str(stat.st_size).encode("ascii")) combined.update(b"\0" + str(stat.st_mtime_ns).encode("ascii") + b"\0") combined.update(reusable_sha256.encode("ascii")) return LmcFingerprint( path=str(path), size=stat.st_size, mtime_ns=stat.st_mtime_ns, sha256=combined.hexdigest(), reusable_blocks_sha256=reusable_sha256, ) class BatchCheckpoint: """Read and append complete JSONL records without trusting torn lines.""" def __init__(self, records: dict[str, BatchStatusRecord] | None = None) -> None: self.records = dict(records or {}) @property def completed_names(self) -> set[str]: return set(self.records) def get(self, name: str) -> BatchStatusRecord | None: return self.records.get(name) @classmethod def load(cls, path: Path) -> "BatchCheckpoint": records: dict[str, BatchStatusRecord] = {} checkpoint = Path(path) if not checkpoint.is_file(): return cls(records) with checkpoint.open("r", encoding="utf-8") as handle: for line in handle: line = line.strip() if not line: continue try: payload = json.loads(line) except json.JSONDecodeError: # A process may have been interrupted during the final write. continue if not isinstance(payload, dict): continue name = str(payload.get("block_name") or "").strip() status = str(payload.get("status") or "").upper() if not name or status not in VALID_STATUSES: continue records[name] = BatchStatusRecord( block_name=name, status=status, source=str(payload.get("source") or ""), raw_status=payload.get("raw_status"), detail=str(payload.get("detail") or ""), fingerprint=str(payload.get("fingerprint") or ""), timestamp=str(payload.get("timestamp") or ""), block_type=str(payload.get("block_type") or "RULP"), ) return cls(records) def append(self, path: Path, record: BatchStatusRecord) -> None: checkpoint = Path(path) checkpoint.parent.mkdir(parents=True, exist_ok=True) with checkpoint.open("a", encoding="utf-8", newline="\n") as handle: handle.write(json.dumps(record.to_json(), ensure_ascii=False, separators=(",", ":")) + "\n") handle.flush() self.records[record.block_name] = record def _now() -> str: return datetime.now(timezone.utc).isoformat() class BatchStatusSession: """Supervised serial bridge session with checkpoint recovery.""" def __init__(self, bridge: StatusBridgeClient, *, timeout_seconds: float = 10.0, rotate_every: int = 500) -> None: self.bridge = bridge self.timeout_seconds = max(0.1, float(timeout_seconds)) self.rotate_every = max(1, int(rotate_every)) self.restart_count = 0 def scan(self, names: Iterable[str], lmc_file: Path, checkpoint_path: Path, *, block_type: str = "RULP", resume: bool = True) -> list[BatchStatusRecord]: fingerprint = fingerprint_library(Path(lmc_file)) if resume: checkpoint = BatchCheckpoint.load(checkpoint_path) else: checkpoint = BatchCheckpoint() checkpoint_file = Path(checkpoint_path) checkpoint_file.parent.mkdir(parents=True, exist_ok=True) checkpoint_file.write_text('', encoding='utf-8') results: list[BatchStatusRecord] = [] names = tuple(str(name) for name in names) server = self.bridge if all(callable(getattr(self.bridge, method, None)) for method in ("start", "restart", "close")) else None if server is not None: server.start() try: queried = 0 for index, name in enumerate(names): previous = checkpoint.get(name) if (previous and previous.status in {'READY', 'PENDING'} and previous.fingerprint == fingerprint.sha256 and previous.block_type == block_type): results.append(previous) continue try: result: BridgeResult = self.bridge.query( Path(lmc_file), name, block_type=block_type, timeout_seconds=self.timeout_seconds, ) record = BatchStatusRecord( block_name=name, status=result.status, source=result.source, raw_status=result.raw_status, detail=result.detail, fingerprint=fingerprint.sha256, timestamp=_now(), block_type=block_type, ) except BridgeUnavailable as error: record = BatchStatusRecord(block_name=name, status="UNAVAILABLE", detail=str(error), fingerprint=fingerprint.sha256, timestamp=_now(), block_type=block_type) except BridgeResponseError as error: message = str(error) lower_message = message.casefold() unavailable = any(marker in message for marker in ("LMCDLL70", "私有 MFC", "未找到可用")) or any( code in lower_message for code in ('0xc0000135', '-1073741515', '3221225781', '0xc000007b', '-1073741701', '3221225595')) malformed = (any(marker in message for marker in ( "路径不匹配", "请求标识不匹配", "CBB 标识不匹配", )) or "mismatch" in lower_message) status = ("TIMEOUT" if "超时" in message or "timeout" in lower_message else "UNAVAILABLE" if unavailable else "MALFORMED" if malformed else "ERROR") record = BatchStatusRecord(block_name=name, status=status, detail=message, fingerprint=fingerprint.sha256, timestamp=_now(), block_type=block_type) if server is not None and status != "UNAVAILABLE": try: server.restart() except BridgeUnavailable: pass except Exception as error: # bridge failures isolate one CBB record = BatchStatusRecord(block_name=name, status="ERROR", detail=str(error), fingerprint=fingerprint.sha256, timestamp=_now(), block_type=block_type) if server is not None: try: server.restart() except BridgeUnavailable: pass checkpoint.append(checkpoint_path, record) results.append(record) if record.status == "UNAVAILABLE" and index + 1 < len(names): # Startup/protocol failures cannot be repaired by retrying # the same request contract for every CBB. # Preserve a terminal record for each remaining name so a # later run can resume after the environment is fixed. for remaining in names[index + 1:]: unavailable_record = BatchStatusRecord( block_name=remaining, status=record.status, detail=record.detail, fingerprint=fingerprint.sha256, timestamp=_now(), block_type=block_type, ) checkpoint.append(checkpoint_path, unavailable_record) results.append(unavailable_record) break queried += 1 if server is not None and queried % self.rotate_every == 0 and name != names[-1]: server.restart() finally: if server is not None: server.close() return results def authoritative_status_map(names: Iterable[str], lmc_file: Path, checkpoint_path: Path, *, bridge_path: Path | None = None, timeout_seconds: float = 10.0) -> dict[str, str]: """Return only bridge-backed statuses for an optional UI integration. A missing bridge is represented as ``UNAVAILABLE``/``UNKNOWN`` by callers; it is never promoted to ``READY``. """ bridge = StatusBridgeServer(bridge_path, env={"CBB_STATUS_BRIDGE_ALLOW_PRIVATE": "1"}) try: names = tuple(str(name) for name in names) try: records = BatchStatusSession(bridge, timeout_seconds=timeout_seconds).scan( names, lmc_file, checkpoint_path, resume=False) return {record.block_name: record.status for record in records} except BridgeUnavailable: return {name: "UNAVAILABLE" for name in names} finally: bridge.close()