fix: diagnose ICDB before repeatable maintenance repair

This commit is contained in:
阿白叔 committed 2026-11-04 09:14:40 +08:00
1 parent c902ffbe9a
commit 8180a8a302
3 files changed
+159 -6

No files matched your search

@@ -200,6 +200,63 @@ def run_existing_address_maintenance(request, *, verify, progress=None):
report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
return result
try:
# Explicit maintenance used to jump straight to native Repair. That
# made a healthy project look like a recovery and could trigger the
# PCB-version choice dialog even when no iCDB problem existed. Run the
# normal startup path first, with auto-repair disabled, so the native
# Repair route is reserved for an observed iCDB/session failure.
emit('icdb_diagnosis_start', detail='先执行启动诊断;正常工程不进入原生 Repair')
# ``run_verify`` supplies a closure over this module's serialized
# worker. Injected legacy callbacks do not implement the diagnostic
# contract and are kept on the original direct-Repair path.
serialized = getattr(verify, '__globals__', {}).get('_run_verify_serialized')
trusted_diagnostic = (getattr(serialized, '__module__', '') == 'cbb_refresh_verify.verify_runner'
and getattr(serialized, '__name__', '') == '_run_verify_serialized')
try:
diagnosis = (verify(replace(request, maintenance=False, auto_repair=False,
post_repair_proof=None)) if trusted_diagnostic else None)
except BaseException as error:
# Keep compatibility with injected legacy test callbacks that only
# understand the post-Repair verification contract. Production
# verification returns a VerifyResult for every controlled failure;
# unexpected production exceptions still stop the operation below.
module = error.__class__.__module__
if not isinstance(error, AssertionError) and not module.startswith('_pytest.'):
raise
diagnosis = None
record['preflight_diagnosis'] = {
'status': 'UNAVAILABLE', 'detail': str(error),
'trusted': False}
if diagnosis is None:
diagnosis_evidence_ready = False
diagnosis_category = ''
diagnosis_text = ''
else:
record['preflight_diagnosis'] = asdict(diagnosis)
diagnosis_evidence_ready = bool(diagnosis.evidence_path
and Path(diagnosis.evidence_path).is_file())
diagnosis_category = diagnosis.error_category or ''
diagnosis_text = str(diagnosis.stderr or '').casefold()
# A real serialized verification always writes its evidence file. A
# missing file is treated as an untrusted test/legacy callback and
# preserves the historical direct-Repair behavior; it must never be
# used to claim that a healthy project was diagnosed.
repair_required = (diagnosis_category == 'ICDB_REPAIR_REQUIRED'
or 'icdb_repair_required' in diagnosis_text
or diagnosis_category == 'DATABASE_SAFETY_STOP'
or 'icdb_session_unconfirmed' in diagnosis_text)
if (diagnosis is not None and diagnosis_evidence_ready and diagnosis.status == 'PASS' and diagnosis.library_flag == 'GREEN'
and diagnosis.safe_to_continue):
emit('icdb_diagnosis_complete', detail='未发现 ICDB 故障,跳过原生 Repair')
return save(replace(diagnosis,
stdout=(diagnosis.stdout + '\nICDB 检查完成:未发现需要修复的问题;未调用原生 Repair。').strip(),
recovery_attempts=()))
if diagnosis is not None and diagnosis_evidence_ready and not repair_required:
emit('icdb_diagnosis_failed', detail='诊断未确认 ICDB 故障,跳过原生 Repair')
return save(replace(diagnosis,
stderr=(diagnosis.stderr + '\nICDB 诊断未确认需要修复,未调用原生 Repair。').strip(),
recovery_attempts=()))
emit('icdb_diagnosis_confirmed', detail='已确认 ICDB/会话故障,进入官方 Repair')
lease = LibraryLock.for_maintenance(request.lmc_file, root/'takeover',
lambda: maintenance_target_guard(request), safe_to_release=lambda: owner_safe)
with lease:
@@ -265,6 +322,11 @@ def run_existing_address_maintenance(request, *, verify, progress=None):
return save(VerifyResult('FAIL', stderr=f'{recovery["status"]}: {recovery.get("detail", "原生维护未确认修复")}',
safe_to_continue=owner_safe, recovery_attempts=(recovery,), error_category='ICDB_MAINTENANCE_RESULT'))
except Exception as error:
# Target/ownership checks happen before native dispatch and belong to
# the preflight phase even when the diagnosis already identified an
# ICDB/session failure.
if recovery is None:
record['stage'] = 'preflight'
return save(VerifyResult('BLOCKED' if recovery is None else 'FAIL', stderr=str(error),
safe_to_continue=False, recovery_attempts=(recovery,) if recovery else (),
error_category='ICDB_MAINTENANCE_BLOCKED', error_stage=record['stage']))