Files
cbb-refresh-verify-795/tests/test_icdb_recovery.py

312 lines
15 KiB
Python

import json
from pathlib import Path
import pytest
from cbb_refresh_verify import mentor_adapter as adapter
from cbb_refresh_verify import verify_runner as runner
ERROR = (
"The last server session on different server machine lost the connection\n"
"to the database. Please use iCDB Project Backup 'Repair project' functionality "
"to fix this issue. (Machine:[200091-CBB] Path:"
"[S:\\SymbolLibs\\__ReusableBlocksSymb] session:[2026-10-24 08.22.29-10308])"
)
def test_native_session_loss_has_priority_over_generic_load_and_cleanup_errors():
detail = 'DATABASE_LOAD_FAILED: Could not load Job Preferences DB\n' + ERROR
detail += '\nPCB_RELEASE_UNCONFIRMED'
assert runner.classify_failure_category(detail) == 'ICDB_REPAIR_REQUIRED'
def test_live_repair_dialog_blocks_without_clicking():
from test_dialog_rules import prompt, ASSETS
from cbb_refresh_verify.dialog_rules import decide, load_rules
from cbb_refresh_verify.dialog_supervisor import classify_dialog, DialogSnapshot, DialogControl
decision = decide(prompt(ERROR), load_rules(ASSETS/'dialog_rules.v1.json'))
legacy = classify_dialog(DialogSnapshot(1, 2, 'Expedition PCB', (
DialogControl(3, 1, 'Static', ERROR, True),)))
for item in (decision, legacy):
assert item.error_code == 'ICDB_REPAIR_REQUIRED'
assert not item.action
assert '200091-CBB' in item.detail
def test_startup_probe_reports_session_loss_instead_of_port_reset(tmp_path):
log = tmp_path/'LogFiles/PCBLogFile.txt'
log.parent.mkdir()
log.write_text(ERROR + '\nCould not run server', encoding='utf-8')
detail = adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb')
assert 'ICDB_REPAIR_REQUIRED' in detail
assert '__ReusableBlocksSymb' in detail
assert 'Repair project' in detail
assert 'net stop winnat' not in detail
def test_generic_server_log_does_not_claim_port_reservation(tmp_path):
log = tmp_path/'LogFiles/PCBLogFile.txt'
log.parent.mkdir()
log.write_text('Could not run server', encoding='utf-8')
detail = adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb')
assert 'DESIGN_SERVER_FAILED' in detail
assert 'WinError 10013' not in detail
assert 'net stop winnat' not in detail
@pytest.mark.parametrize('mode', ['append', 'shorter', 'same_size', 'longer'])
def test_current_run_probe_handles_appended_and_rewritten_logs(tmp_path, mode):
from cbb_refresh_verify.icdb_recovery import snapshot_log
log = tmp_path/'LogFiles/PCBLogFile.txt'
log.parent.mkdir()
old = 'old harmless text\n' if mode == 'longer' else 'x'*(len(ERROR.encode())+40)
if mode == 'same_size':
old = 'x'*len(ERROR.encode())
log.write_text(old, encoding='utf-8')
baseline = snapshot_log(log)
log.write_text(old+ERROR if mode == 'append' else ERROR, encoding='utf-8')
assert 'ICDB_REPAIR_REQUIRED' in adapter.pcb_log_reports_missing_server(
tmp_path/'CBB.pcb', offset=baseline)
def test_old_loss_record_does_not_fail_a_new_run(tmp_path):
from cbb_refresh_verify.icdb_recovery import snapshot_log
log = tmp_path/'LogFiles/PCBLogFile.txt'
log.parent.mkdir()
log.write_text(ERROR, encoding='utf-8')
baseline = snapshot_log(log)
assert adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline) == ''
with log.open('a', encoding='utf-8') as stream:
stream.write('\nNormal startup completed\n')
assert adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline) == ''
def test_rewritten_header_does_not_promote_retained_old_session_loss(tmp_path):
from cbb_refresh_verify.icdb_recovery import snapshot_log
log = tmp_path/'LogFiles/PCBLogFile.txt'
log.parent.mkdir()
log.write_text('old header\n'+ERROR, encoding='utf-8')
baseline = snapshot_log(log)
log.write_text('new header\n'+ERROR+'\nNormal startup\n', encoding='utf-8')
assert adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline) == ''
def test_repeated_old_session_loss_is_current_when_verifiably_appended(tmp_path):
from cbb_refresh_verify.icdb_recovery import snapshot_log
log = tmp_path/'LogFiles/PCBLogFile.txt'
log.parent.mkdir()
log.write_text(ERROR+'\n', encoding='utf-8')
baseline = snapshot_log(log)
with log.open('a', encoding='utf-8') as stream:
stream.write(ERROR)
assert 'ICDB_REPAIR_REQUIRED' in adapter.pcb_log_reports_missing_server(
tmp_path/'CBB.pcb', offset=baseline)
@pytest.mark.parametrize('name', ['MsgBoxResponse.log', 'ForwardAnnotation.txt', 'PartPkg.log'])
def test_integration_log_retains_multiline_loss_identity(tmp_path, name):
from cbb_refresh_verify.icdb_recovery import snapshot_log
log = tmp_path/'LogFiles'/name
log.parent.mkdir()
baseline = snapshot_log(log)
log.write_text(ERROR.replace(' (Machine:', '\nMachine:').replace(' Path:', '\nPath:')
.replace(' session:', '\nSession:'), encoding='utf-8')
detail = adapter.pcb_log_reports_integration_error(tmp_path/'CBB.pcb', offsets={log: baseline})
assert 'ICDB_REPAIR_REQUIRED' in detail
assert '200091-CBB' in detail
assert '__ReusableBlocksSymb' in detail
assert '2026-10-24 08.22.29-10308' in detail
@pytest.mark.parametrize('production_checkpoint', [False, True])
def test_integration_generic_filter_does_not_repromote_historical_repair(tmp_path, production_checkpoint):
import hashlib
from cbb_refresh_verify.icdb_recovery import snapshot_log
log = tmp_path/'LogFiles/MsgBoxResponse.log'
log.parent.mkdir()
old = 'old header\nERROR: '+ERROR.replace('\n', ' ')
log.write_text(old, encoding='utf-8')
baseline = snapshot_log(log)
if production_checkpoint:
raw = log.read_bytes()
baseline = {'size': len(raw), 'digest': hashlib.sha256(raw).hexdigest(),
'icdb_baseline': baseline}
log.write_text(old.replace('old header', 'new header')+'\nnormal startup', encoding='utf-8')
detail = adapter.pcb_log_reports_integration_error(tmp_path/'CBB.pcb', offsets={log: baseline})
assert 'ICDB_REPAIR_REQUIRED' not in detail
assert runner.classify_failure_category(detail) != 'ICDB_REPAIR_REQUIRED'
def test_unreadable_integration_baseline_never_falls_back_to_historical_log(tmp_path):
log = tmp_path/'LogFiles/MsgBoxResponse.log'
log.parent.mkdir()
log.write_text('ERROR: '+ERROR.replace('\n', ' '), encoding='utf-8')
baseline = {'size': 0, 'mtime_ns': None, 'digest': '',
'icdb_baseline': {'error': 'access denied'}}
detail = adapter.pcb_log_reports_integration_error(tmp_path/'CBB.pcb', offsets={log: baseline})
assert runner.classify_failure_category(detail) != 'ICDB_REPAIR_REQUIRED'
def test_rewrite_of_large_log_does_not_promote_error_outside_baseline_window(tmp_path):
from cbb_refresh_verify.icdb_recovery import LOG_LIMIT, snapshot_log
log = tmp_path/'LogFiles/PCBLogFile.txt'
log.parent.mkdir()
log.write_text(ERROR+'\n'+'normal\n'*LOG_LIMIT, encoding='utf-8')
baseline = snapshot_log(log)
log.write_text('changed header\n'+ERROR, encoding='utf-8')
assert adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline) == ''
def test_integration_session_loss_has_priority_over_generic_startup_error(tmp_path, monkeypatch):
from test_verify_worker_stability import exercise_worker
from cbb_refresh_verify.pcb_bridge import PcbBridgeError
job, events, execute = exercise_worker(tmp_path, monkeypatch)
def attach(job, provider, **kwargs):
logs = Path(job['pcb']).parent/'LogFiles'
logs.mkdir()
(logs/'PCBLogFile.txt').write_text('Could not run server', encoding='utf-8')
(logs/'MsgBoxResponse.log').write_text(ERROR, encoding='utf-8')
detail = kwargs['error_reader']()
assert 'ICDB_REPAIR_REQUIRED' in detail
assert 'net stop winnat' not in detail
raise PcbBridgeError('PCB_STARTUP_FAILED', detail)
monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', attach)
with pytest.raises(PcbBridgeError, match='ICDB_REPAIR_REQUIRED'):
execute()
assert not any(event['stage'] == 'save_start' for event in events)
def test_optional_baseline_evidence_write_failure_does_not_block_verify(tmp_path, monkeypatch):
from test_verify_worker_stability import exercise_worker
job, events, execute = exercise_worker(tmp_path, monkeypatch)
original = Path.write_text
def deny_baseline(path, *args, **kwargs):
if path.name == 'startup-log-baseline.json':
raise PermissionError('diagnostic evidence unavailable')
return original(path, *args, **kwargs)
monkeypatch.setattr(Path, 'write_text', deny_baseline)
assert execute()['status'] == 'PASS'
def test_recovery_record_preserves_identity_and_distinguishes_shared_library():
from cbb_refresh_verify.icdb_recovery import parse_session_loss
record = parse_session_loss(ERROR)
assert record['machine'] == '200091-CBB'
assert record['database_path'] == r'S:\SymbolLibs\__ReusableBlocksSymb'
assert record['session'] == '2026-10-24 08.22.29-10308'
assert record['scope'] == 'central_library'
assert parse_session_loss(ERROR.replace(r'S:\SymbolLibs\__ReusableBlocksSymb',
r'S:\ReusableBlocksLP\CBB\Work\database'))['scope'] == 'project'
assert parse_session_loss('Could not run server') is None
@pytest.mark.parametrize('encoding', ['utf-8', 'utf-16', 'utf-16-le'])
def test_new_utf16_or_utf8_log_errors_are_detected_after_append(tmp_path, encoding):
from cbb_refresh_verify.icdb_recovery import snapshot_log
log = tmp_path/'LogFiles/PCBLogFile.txt'
log.parent.mkdir()
log.write_bytes('old text\n'.encode(encoding))
baseline = snapshot_log(log)
with log.open('ab') as stream:
stream.write(ERROR.encode('utf-16-le' if encoding == 'utf-16' else encoding))
assert 'ICDB_REPAIR_REQUIRED' in adapter.pcb_log_reports_missing_server(
tmp_path/'CBB.pcb', offset=baseline)
@pytest.mark.parametrize('baseline_state', ['stale', 'absent', 'unreadable'])
def test_historical_log_evidence_never_creates_a_repair_recommendation(tmp_path, baseline_state):
from cbb_refresh_verify.icdb_recovery import snapshot_log
from cbb_refresh_verify.verify_diagnostics import collect_native_failure_logs
pcb = tmp_path/'Work/PCB/CBB.pcb'
log = pcb.parent/'LogFiles/PCBLogFile.txt'
log.parent.mkdir(parents=True)
log.write_text(ERROR, encoding='utf-8')
evidence = tmp_path/'evidence'
(evidence/'diagnostics').mkdir(parents=True)
if baseline_state != 'absent':
baseline = snapshot_log(log) if baseline_state == 'stale' else {'error': 'access denied'}
(evidence/'diagnostics/startup-log-baseline.json').write_text(json.dumps({
'source': str(log), 'baseline': baseline}), encoding='utf-8')
collect_native_failure_logs(pcb, evidence)
assert not (evidence/'diagnostics/icdb-recovery.json').exists()
def test_content_success_text_cannot_override_native_session_loss():
assert runner.classify_output(0, 'content checking completed successfully\n'+ERROR) == 'FAIL'
@pytest.mark.parametrize('name', ['PCBLogFile.txt', 'MsgBoxResponse.log', 'ForwardAnnotation.txt', 'PartPkg.log'])
def test_native_log_capture_generates_only_fresh_repair_evidence(tmp_path, name):
from cbb_refresh_verify.icdb_recovery import snapshot_log
from cbb_refresh_verify.verify_diagnostics import collect_native_failure_logs
pcb = tmp_path/'Work/PCB/CBB.pcb'
log = pcb.parent/'LogFiles'/name
log.parent.mkdir(parents=True)
evidence = tmp_path/'evidence'
(evidence/'diagnostics').mkdir(parents=True)
log.write_text('old text', encoding='utf-8')
(evidence/'diagnostics/startup-log-baseline.json').write_text(json.dumps({
'source': str(pcb.parent/'LogFiles/PCBLogFile.txt'),
'baseline': snapshot_log(pcb.parent/'LogFiles/PCBLogFile.txt'),
'integration_logs': {str(log): snapshot_log(log)}}), encoding='utf-8')
log.write_text(ERROR, encoding='utf-8')
collect_native_failure_logs(pcb, evidence)
report = json.loads((evidence/'diagnostics/icdb-recovery.json').read_text('utf-8'))
assert report['scope'] == 'central_library'
assert report['source'] == str(log)
assert log.read_text('utf-8') == ERROR
def test_contents_session_loss_records_each_item_without_launching_pcb_or_repair(tmp_path, monkeypatch):
from test_verify_runner import fixture, request
fixture(tmp_path)
monkeypatch.setattr(runner, 'resolve_importer', lambda *_: Path('ReuseBlockImporter.exe'))
calls = []
def content_check(*args):
calls.append(args)
return 1, ERROR, '', False
results = runner.run_batch([request(tmp_path, libspec='EE', dms_login_config='login')]*2,
verifier=lambda req: runner.run_verify(req, process_runner=content_check,
port_check=lambda: pytest.fail('PCB must not start')))
assert len(calls) == 2
assert results[0].error_category == 'ICDB_REPAIR_REQUIRED'
assert results[0].safe_to_continue is True
assert 'Repair project' in results[0].stderr
assert results[1].status == 'FAIL'
assert results[1].error_category == 'ICDB_REPAIR_REQUIRED'
def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, monkeypatch):
from test_verify_runner import fixture, request
from cbb_refresh_verify.icdb_recovery import snapshot_log
from cbb_refresh_verify.verify_diagnostics import failure_logs_main
work = fixture(tmp_path)
log = work/'PCB/LogFiles/PCBLogFile.txt'
log.parent.mkdir()
class Worker:
desktop = None
returncode = 1
def __init__(self, command, *_a, **_kw):
root = Path(command[-1]).parent
(root/'diagnostics').mkdir()
(root/'diagnostics/startup-log-baseline.json').write_text(json.dumps({
'source': str(log), 'baseline': snapshot_log(log)}), encoding='utf-8')
log.write_text(ERROR, encoding='utf-8')
(root/'outcome.json').write_text(json.dumps({
'status': 'FAIL', 'stderr': 'MENTOR_DATABASE_LOAD_FAILED: opening failed',
'safe_to_continue': False}), encoding='utf-8')
def poll(self): return 1
def close(self): pass
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True})
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', failure_logs_main)
result = runner.run_verify(request(tmp_path), port_check=lambda: (True, ''))
assert result.error_category == 'ICDB_REPAIR_REQUIRED'
assert result.safe_to_continue is True
assert 'opening failed' in result.stderr
assert '200091-CBB' in result.stderr
assert '__ReusableBlocksSymb' in result.stderr
assert '2026-10-24 08.22.29-10308' in result.stderr
assert json.loads(result.evidence_path.read_text('utf-8'))['error_category'] == 'ICDB_REPAIR_REQUIRED'