Merge branch 'codex/review-residual-release' into codex/runtime-lifecycle-safety

This commit is contained in:
阿白叔 committed 2026-11-09 14:10:38 +08:00
commit d0a5ce5d17
20 files changed
+454 -72

No files matched your search

+26 -2
View File
@@ -62,6 +62,16 @@ def resolve_data_root(cli_value: Path | None) -> Path:
local = os.environ.get("LOCALAPPDATA")
return (Path(local) / "CbbRefreshVerify" if local else Path.home() / "AppData" / "Local" / "CbbRefreshVerify").resolve()
def _positive_int(value):
try:
number = int(value)
except ValueError:
raise argparse.ArgumentTypeError('must be a positive integer') from None
if number <= 0:
raise argparse.ArgumentTypeError('must be a positive integer')
return number
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser(prog="CbbRefreshVerify")
parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS)
@@ -73,6 +83,7 @@ def main(argv: Sequence[str] | None = None) -> int:
parser.add_argument('--collect-failure-logs', type=Path, help=argparse.SUPPRESS)
parser.add_argument('--batch-lmc', type=Path, help=argparse.SUPPRESS)
parser.add_argument('--block', action='append', default=[], help=argparse.SUPPRESS)
parser.add_argument('--limit', type=_positive_int, help='Maximum requested CBB names for --batch-lmc (default: all)')
parser.add_argument('--libspec', default='', help=argparse.SUPPRESS)
parser.add_argument('--dms-login-config', default='', help=argparse.SUPPRESS)
parser.add_argument('--mentor-bin', default='', help=argparse.SUPPRESS)
@@ -126,6 +137,11 @@ def main(argv: Sequence[str] | None = None) -> int:
if library.error: raise RuntimeError(library.error)
names = [record.name for record in library.records if record.status != 'READY']
if not names: raise RuntimeError('中心库未读取到需要校验的 CBB')
total = len(names)
if args.limit is not None:
names = names[:args.limit]
logger.info('batch_selection selected=%s total=%s remaining=%s limit=%s',
len(names), total, total - len(names), args.limit or 'all')
requests = [VerifyRequest(mentor_path, name, context.root/'evidence',
libspec=args.libspec, dms_login_config=args.dms_login_config,
mentor_bin=args.mentor_bin, logging_context=context,
@@ -133,16 +149,24 @@ def main(argv: Sequence[str] | None = None) -> int:
mapped_remote_root=lease.remote_root,
native_lm_verify=native_lm_verify_enabled())
for name in names]
def retain_mapping(reason):
lease.retain_for_recovery()
logger.warning(
'batch_drive_retained drive=%s remote=%s display=%s mentor=%s reason=%s '
'guidance=请打开此中心库的“释放残留会话…”入口,确认原生会话安全释放后再释放映射;'
'原生写入仍可能运行时请保留映射。',
lease.drive or '-', lease.remote_root or '-', lease.display_path,
mentor_path, reason)
try:
from threading import Event
stop = Event()
with batch_stop_signals(stop):
results = run_batch(requests, stop=stop)
except Exception:
lease.retain_for_recovery()
retain_mapping('batch_exception')
raise
if any(not result.safe_to_continue for result in results):
lease.retain_for_recovery()
retain_mapping('native_release_unconfirmed')
report = write_batch_report(requests, results, context.root/'reports')
logger.info('batch_report path=%s', report)
logger.info('failure_report path=%s', failure_report_path(report))
+10 -3
View File
@@ -107,7 +107,7 @@ def fingerprint_library(lmc: Path) -> LmcFingerprint:
# 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.
# digest to invalidate changed LMC bytes; payload edits remain outside this key.
content_sha256 = _sha256_file(path)
combined = hashlib.sha256()
combined.update(str(path).casefold().encode('utf-8', errors='surrogatepass'))
@@ -186,7 +186,12 @@ def _now() -> str:
class BatchStatusSession:
"""Supervised serial bridge session with checkpoint recovery."""
"""Supervised serial bridge session; fresh status queries by default.
Checkpoints record scan evidence. Explicit ``resume=True`` is a low-level
opt-in for controlled read-only scans, not a production freshness proof:
the lightweight fingerprint does not cover native payload/database edits.
"""
def __init__(self, bridge: StatusBridgeClient, *, timeout_seconds: float = 10.0,
rotate_every: int = 500) -> None:
@@ -196,7 +201,7 @@ class BatchStatusSession:
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]:
*, block_type: str = "RULP", resume: bool = False) -> list[BatchStatusRecord]:
fingerprint = fingerprint_library(Path(lmc_file))
if resume:
checkpoint = BatchCheckpoint.load(checkpoint_path)
@@ -291,6 +296,8 @@ def authoritative_status_map(names: Iterable[str], lmc_file: Path, checkpoint_pa
timeout_seconds: float = 10.0) -> dict[str, str]:
"""Return only bridge-backed statuses for an optional UI integration.
Production always queries fresh statuses, including after native writes;
saved READY/PENDING rows are never reused here.
A missing bridge is represented as ``UNAVAILABLE``/``UNKNOWN`` by callers;
it is never promoted to ``READY``.
"""
+2 -1
View File
@@ -8,7 +8,7 @@ from uuid import uuid4
from .library_lock import LibraryLock, LibraryBusy
from .scoped_manifest import (ManifestScope, build_manifest, build_exclusive_library_manifest,
copy_verified, hash_file, scan_tree)
comparison_rules, copy_verified, hash_file, scan_tree)
@dataclass(frozen=True)
@@ -214,6 +214,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
nonlocal change_report
if mutated and original is not None and change_report is None:
change_report = {'status': 'UNCONFIRMED', 'scope': original['scope'],
**comparison_rules(scope),
'unrelated_cbb_content_checked': False,
'detail': '原生会话尚未释放,未读取写后内容'}
if safe:
-15
View File
@@ -160,18 +160,3 @@ def wait_for_target_document(job, app_provider, *, process_alive, emit=lambda **
last_observation = observation
sleep(.25)
raise PcbBridgeError('PCB_EXITED_BEFORE_COM', 'PCB 在 COM 文档就绪前退出')
def execution_outcome(*, save_complete: bool, pcb_exited: bool,
fatal_error: str = '') -> dict[str, object]:
if fatal_error:
code, _, detail = str(fatal_error).partition(':')
return {'status': 'FAIL', 'error_code': code.strip() or 'FATAL_DIALOG',
'stderr': detail.strip() or str(fatal_error), 'safe_to_continue': True}
if not save_complete:
return {'status': 'FAIL', 'error_code': 'SAVE_NOT_CONFIRMED',
'stderr': '保存/关闭未完成', 'safe_to_continue': True}
if not pcb_exited:
return {'status': 'FAIL', 'error_code': 'PCB_EXIT_NOT_CONFIRMED',
'stderr': '保存后未确认 PCB 退出', 'safe_to_continue': True}
return {'status': 'PASS', 'stdout': 'Project Integration 已同步;保存、关闭和 PCB 退出均已确认。'}
+59 -11
View File
@@ -15,6 +15,15 @@ import stat
MANIFEST_VERSION = 1
MANIFEST_RULE_VERSION = 1
_ROOT_RUNTIME_PREFIX = 'work.before-restore-'
_ROOT_RUNTIME_PATH = ('projectbackup', 'backups')
_NATIVE_RUNTIME_DIRECTORIES = ('log', 'dbcheck', 'dbrestore')
_NATIVE_SERVER_DIRECTORY = 'cdbsvr'
_PROJECT_PCB_DIRECTORY = 'pcb'
_PROJECT_DATABASE_PREFIXES = (('database',), (_PROJECT_PCB_DIRECTORY, 'database'))
_NONPRIMARY_FILENAMES = ('saddress.adr',)
_NONPRIMARY_FILENAME_PREFIXES = ('~$',)
_NONPRIMARY_DIRECTORIES = frozenset({
'log', 'logs', 'temp', 'tmp', '__pycache__', 'backup', 'backups',
'output', 'outputs', 'report', 'reports', 'gerber', 'gerbers', 'odb++',
@@ -29,22 +38,21 @@ def _root_runtime(directories):
"""Known runtime/history children of a library or project workspace root."""
return bool(directories and (
directories[0] in _NONPRIMARY_DIRECTORIES
or directories[0].startswith('work.before-restore-')
or directories[:2] == ('projectbackup', 'backups')))
or directories[0].startswith(_ROOT_RUNTIME_PREFIX)
or directories[:len(_ROOT_RUNTIME_PATH)] == _ROOT_RUNTIME_PATH))
def _native_runtime(directories, database_prefix):
runtime_prefix = database_prefix + ('cdbsvr',)
runtime_prefix = database_prefix + (_NATIVE_SERVER_DIRECTORY,)
return (directories[:len(runtime_prefix)] == runtime_prefix
and len(directories) > len(runtime_prefix)
and directories[len(runtime_prefix)] in ('log', 'dbcheck', 'dbrestore'))
and directories[len(runtime_prefix)] in _NATIVE_RUNTIME_DIRECTORIES)
def _project_runtime(directories):
return (_root_runtime(directories)
or (directories[:1] == ('pcb',) and _root_runtime(directories[1:]))
or _native_runtime(directories, ('database',))
or _native_runtime(directories, ('pcb', 'database')))
or (directories[:1] == (_PROJECT_PCB_DIRECTORY,) and _root_runtime(directories[1:]))
or any(_native_runtime(directories, prefix) for prefix in _PROJECT_DATABASE_PREFIXES))
@dataclass(frozen=True)
@@ -85,7 +93,8 @@ class ManifestScope:
"""
directories = parts if directory else parts[:-1]
if not directory and (Path(parts[-1]).suffix in _NONPRIMARY_SUFFIXES
or parts[-1] == 'saddress.adr' or parts[-1].startswith('~$')):
or parts[-1] in _NONPRIMARY_FILENAMES
or parts[-1].startswith(_NONPRIMARY_FILENAME_PREFIXES)):
return True
if self.kind == 'project':
return _project_runtime(directories)
@@ -123,6 +132,36 @@ class ManifestScope:
return 'shared_dependency'
def comparison_rules(scope):
"""Describe semantic exclusions using the classifier's rule constants."""
project_positions = ['project root or its PCB child']
project_native = ' or '.join('/'.join(prefix + (_NATIVE_SERVER_DIRECTORY,))
for prefix in _PROJECT_DATABASE_PREFIXES)
native_positions = [project_native + ' under project root']
if scope.kind == 'library':
project_positions = ['library root', 'selected CBB Work/Cert root or its PCB child']
native_positions = [
project_native + ' under selected CBB Work/Cert',
'SymbolLibs/<partition>/' + _NATIVE_SERVER_DIRECTORY,
'<shared dependency>/' + _NATIVE_SERVER_DIRECTORY,
]
return {
'rule_version': MANIFEST_RULE_VERSION,
'excluded_categories': sorted(_NONPRIMARY_DIRECTORIES),
'excluded_suffixes': sorted(_NONPRIMARY_SUFFIXES),
'runtime_exclusions': {
'directory_name_positions': project_positions,
'directory_prefixes': [_ROOT_RUNTIME_PREFIX],
'relative_directories': ['/'.join(_ROOT_RUNTIME_PATH)],
'native_directory_names': sorted(_NATIVE_RUNTIME_DIRECTORIES),
'native_directory_positions': native_positions,
'file_names': list(_NONPRIMARY_FILENAMES),
'file_name_prefixes': list(_NONPRIMARY_FILENAME_PREFIXES),
'coordination_checkpoints': 'recognized CBB coordination checkpoints',
},
}
def hash_file(path):
digest = hashlib.sha256()
with Path(path).open('rb') as stream:
@@ -271,7 +310,10 @@ def copy_verified(source, destination, *, scope=None, progress=None, hash_functi
Native icdb.dat/LMC files are held with exclusive read handles for the copy.
Their expected SHA256 comes from the source read stream, independently of
hashing destination bytes. Failures retain the partial destination evidence.
hashing destination bytes. Unlocked primary files are also hashed while
streaming the copy, then rehashed at source to detect even same-size/mtime
edits. Every primary destination is independently hashed. Failures retain
the partial destination evidence.
Progress receives one dict with stage, hashed_files/bytes and copied_files.
"""
import win32con
@@ -329,9 +371,15 @@ def copy_verified(source, destination, *, scope=None, progress=None, hash_functi
shutil.copystat(path, copied)
if stream_hash is not None:
digest = stream_hash.hexdigest()
elif label is not None:
stream_hash = hashlib.sha256()
with path.open('rb') as incoming, copied.open('wb') as outgoing:
for data in iter(lambda: incoming.read(1024 * 1024), b''):
stream_hash.update(data)
outgoing.write(data)
shutil.copystat(path, copied)
digest = stream_hash.hexdigest()
else:
if label is not None:
digest = hash_function(path)
shutil.copy2(path, copied)
before, after = entries[path], path.stat()
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
+41 -10
View File
@@ -99,7 +99,6 @@ class MainWindow(QWidget):
self._management_unsafe_libraries = set()
self._management_operation_library = None
self._maintenance_recovery_view = False
self._reload_after_maintenance = False
self._reimport_offers = ()
self._batch_requests = ()
self._batch_records = {}
@@ -135,6 +134,10 @@ class MainWindow(QWidget):
self._mapping_auto_idle = False
self._mapping_finish_waiters = []
self._closing = self._close_ready = False
self.close_wait_timer = QTimer(self)
self.close_wait_timer.setSingleShot(True)
self.close_wait_timer.setInterval(120_000)
self.close_wait_timer.timeout.connect(self._show_close_wait_notice)
from cbb_refresh_verify.operation_queue import NativeQueue
self.native_queue = NativeQueue()
self.icdb_task = None
@@ -498,7 +501,7 @@ class MainWindow(QWidget):
if self._defer_mapping_finish(self._release_dialog_finished): return
self._finished()
pending = getattr(self, '_resume_batch', None)
if pending and pending['released']:
if pending and pending['released'] and getattr(self, 'summary', None) is not None:
self.summary.setText('会话已释放,可以继续未执行项。')
self.summary.show()
@@ -620,7 +623,6 @@ class MainWindow(QWidget):
mapped_drive=getattr(self.library_lease, 'drive', ''),
mapped_remote_root=getattr(self.library_lease, 'remote_root', '')))
previous[record.name] = str(result.evidence_path)
self._reload_after_maintenance = False
if getattr(self, 'summary', None): self.summary.close()
self._open_management_dialog(requests, recovery_from=previous)
@@ -799,7 +801,7 @@ class MainWindow(QWidget):
if hasattr(task, 'summary'):
prefix = '已更新的工程:'+', '.join(sorted(task.invalidated))+'。下列为此前检查记录。\n'
task.summary.setDetailedText(prefix+task.summary.detailedText())
if names & getattr(task, 'recovery_names', set()):
if names & getattr(task, 'recovery_names', set()) and hasattr(task, 'recovery_action'):
task.recovery_action.setEnabled(False)
task.recovery_action.setToolTip('工程已有后续管理操作,请重新检查。')
self._reimport_offers = tuple(offer for offer in self._reimport_offers if offer[0].block_name not in names
@@ -899,7 +901,7 @@ class MainWindow(QWidget):
self.icdb_phase.setText(text)
if not self.busy: self.status_label.setText(text)
self.output.append(text+'\n'+str(task.report or task.report_error))
summary = QMessageBox(self); self.icdb_summary = summary
summary = self._new_result_summary('icdb_summary')
task.summary = summary
if not self.busy: self.summary = summary
summary.setWindowTitle('ICDB 批量检查结果'); summary.setText(text)
@@ -1446,6 +1448,30 @@ class MainWindow(QWidget):
if cell is not None:
cell.setBackground(QColor('#E8F5E9'))
def _new_result_summary(self, attribute):
previous = getattr(self, attribute, None)
if previous is not None:
previous.close()
summary = QMessageBox(self)
setattr(self, attribute, summary)
summary.setAttribute(Qt.WA_DeleteOnClose)
summary.finished.connect(lambda _: self._forget_result_summary(summary))
return summary
def _forget_result_summary(self, summary):
if getattr(self, '_batch_summary', None) is summary:
self._reimport_summary_action = None
self._residual_release_action = None
self._resume_batch_action = None
for attribute in ('summary', '_batch_summary', 'icdb_summary'):
if getattr(self, attribute, None) is summary:
setattr(self, attribute, None)
for task in self.icdb_tasks:
if getattr(task, 'summary', None) is summary:
del task.summary
if hasattr(task, 'recovery_action'):
del task.recovery_action
def _batch_done(self, outcome):
results, report = outcome[:2]
failure_report = failure_report_path(report) if report is not None else '未写入'
@@ -1507,7 +1533,7 @@ class MainWindow(QWidget):
text += guidance
status_text += guidance
self.status_label.setText(status_text.replace('\n', ' ')); self.output.append(text)
self.summary = QMessageBox(self)
self.summary = self._new_result_summary('_batch_summary')
self.summary.setWindowTitle('ICDB 批量检查结果' if self._global_icdb_run else 'CBB 校验结果')
self.summary.setText(status_text)
self.summary.setDetailedText(text)
@@ -1577,10 +1603,6 @@ class MainWindow(QWidget):
self.version_dialog.reject()
self.version_dialog = None
self.busy = False; self._update()
if self._reload_after_maintenance:
from PySide6.QtCore import QTimer
self._reload_after_maintenance = False
QTimer.singleShot(0, self.load_library)
def open_logs(self):
self.data_root.mkdir(parents=True, exist_ok=True); os.startfile(str(self.data_root))
@@ -1602,6 +1624,9 @@ class MainWindow(QWidget):
else:
self._closing = True
self.busy = True; self._update()
self.setWindowTitle('CBB 刷新校验 · 等待任务安全结束')
self.status_label.setText('正在等待当前操作安全结束,完成后关闭。')
self.close_wait_timer.start()
retained = None
if self._native_backup_session is not None:
self.native_backup_timer.stop()
@@ -1614,6 +1639,11 @@ class MainWindow(QWidget):
self._close_when_idle()
event.ignore()
def _show_close_wait_notice(self):
if self._closing and not self._close_ready:
self.setWindowTitle('CBB 刷新校验 · 仍在等待任务安全结束')
self.status_label.setText('当前操作尚未结束,仍在等待安全关闭。')
def _close_when_idle(self):
owners = [self, *self.icdb_tasks]
for name in ('management_dialog', 'backup_dialog', 'release_dialog'):
@@ -1628,5 +1658,6 @@ class MainWindow(QWidget):
if self._lease_pending or active:
QTimer.singleShot(20, self._close_when_idle)
return
self.close_wait_timer.stop()
self._close_ready = True
self.close()
@@ -211,8 +211,8 @@ class ManagementDialog(QDialog):
changes = getattr(result, 'change_report', None)
if changes:
if changes.get('status') == 'RECORDED':
text += (f'\n变更:新增 {len(changes["added"])}、删除 {len(changes["deleted"])}、'
f'修改 {len(changes["modified"])} 个主要文件。')
text += (f'\n已比对的工程文件:新增 {len(changes["added"])}、删除 {len(changes["deleted"])}、'
f'修改 {len(changes["modified"])};范围仅限报告列出的工程文件。')
else:
summary += ',变更清单待核对'
text += '\n'+changes.get('detail', '变更清单未确认')