fix: preserve catalog state and verify PCB write access

This commit is contained in:
阿白叔 committed 2026-11-20 23:24:47 +08:00
1 parent 3efeaed656
commit 86837155ea
11 files changed
+651 -92

No files matched your search

+29 -22
View File
@@ -172,12 +172,13 @@ class BatchCheckpoint:
)
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()
def append(self, path: Path | None, record: BatchStatusRecord) -> None:
if path is not 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
@@ -200,16 +201,20 @@ class BatchStatusSession:
self.rotate_every = max(1, int(rotate_every))
self.restart_count = 0
def scan(self, names: Iterable[str], lmc_file: Path, checkpoint_path: Path,
def scan(self, names: Iterable[str], lmc_file: Path, checkpoint_path: Path | None,
*, block_type: str = "RULP", resume: bool = False) -> 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')
from .operation_queue import check_active
check_active()
fingerprint = fingerprint_library(Path(lmc_file)).sha256 if checkpoint_path is not None else ''
checkpoint = BatchCheckpoint()
if checkpoint_path is not None:
if resume:
checkpoint = BatchCheckpoint.load(checkpoint_path)
else:
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))
@@ -219,9 +224,10 @@ class BatchStatusSession:
try:
queried = 0
for index, name in enumerate(names):
check_active()
previous = checkpoint.get(name)
if (previous and previous.status in {'READY', 'PENDING'}
and previous.fingerprint == fingerprint.sha256 and previous.block_type == block_type):
and previous.fingerprint == fingerprint and previous.block_type == block_type):
results.append(previous)
continue
try:
@@ -232,12 +238,12 @@ class BatchStatusSession:
record = BatchStatusRecord(
block_name=name, status=result.status, source=result.source,
raw_status=result.raw_status, detail=result.detail,
fingerprint=fingerprint.sha256, timestamp=_now(),
fingerprint=fingerprint, timestamp=_now(),
block_type=block_type,
)
except BridgeUnavailable as error:
record = BatchStatusRecord(block_name=name, status="UNAVAILABLE",
detail=str(error), fingerprint=fingerprint.sha256,
detail=str(error), fingerprint=fingerprint,
timestamp=_now(), block_type=block_type)
except BridgeResponseError as error:
message = str(error)
@@ -252,7 +258,7 @@ class BatchStatusSession:
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)
fingerprint=fingerprint, timestamp=_now(), block_type=block_type)
if server is not None and status != "UNAVAILABLE":
try:
server.restart()
@@ -260,12 +266,13 @@ class BatchStatusSession:
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)
fingerprint=fingerprint, timestamp=_now(), block_type=block_type)
if server is not None:
try:
server.restart()
except BridgeUnavailable:
pass
check_active()
checkpoint.append(checkpoint_path, record)
results.append(record)
if record.status == "UNAVAILABLE" and index + 1 < len(names):
@@ -276,7 +283,7 @@ class BatchStatusSession:
for remaining in names[index + 1:]:
unavailable_record = BatchStatusRecord(
block_name=remaining, status=record.status, detail=record.detail,
fingerprint=fingerprint.sha256, timestamp=_now(),
fingerprint=fingerprint, timestamp=_now(),
block_type=block_type,
)
checkpoint.append(checkpoint_path, unavailable_record)
@@ -291,7 +298,7 @@ class BatchStatusSession:
return results
def authoritative_status_map(names: Iterable[str], lmc_file: Path, checkpoint_path: Path,
def authoritative_status_map(names: Iterable[str], lmc_file: Path, checkpoint_path: Path | None = None,
*, bridge_path: Path | None = None,
timeout_seconds: float = 10.0) -> dict[str, str]:
"""Return only bridge-backed statuses for an optional UI integration.