Files
cbb-refresh-verify-795/src/cbb_refresh_verify/verify_diagnostics.py
T

220 lines
11 KiB
Python

"""Bounded, diagnostic-only snapshots for a native Mentor Verify failure.
Only known paths are inspected. No database/lock repair, recursive traversal,
or sustained file handles are allowed here. The parent repeats log collection
once after cleanup if the worker was killed while a native file was open.
"""
from pathlib import Path
import json
import os
import re
import stat
import tempfile
import subprocess
import sys
import time
NATIVE_LOG_MAX_BYTES = 128 * 1024
def _write_record(path, record):
try:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
except OSError as error:
record['evidence_error'] = str(error)
return record
def _path_metadata(path, *, directory=False):
item = {'path': str(path), 'exists': None}
try:
info = path.stat()
item.update(exists=True, size_bytes=info.st_size, mtime_ns=info.st_mtime_ns)
if directory:
item['is_directory'] = stat.S_ISDIR(info.st_mode)
else:
item['readable'] = False
if not stat.S_ISREG(info.st_mode):
item['error'] = 'expected a regular file'
else:
with path.open('rb') as stream:
stream.read(1)
item['readable'] = True
except FileNotFoundError:
item['exists'] = False
except OSError as error:
item['error'] = str(error)
return item
def collect_preflight_diagnostics(pcb_path, evidence_root):
"""One-shot metadata/readability and unique scratch-file writability probe.
Layout_Temp is generated by Mentor. Its absence is never a preflight gate.
This runs in the supervised worker BEFORE Mentor starts, never in polling.
"""
directory = Path(pcb_path).parent
paths = [_path_metadata(directory, directory=True)]
paths.extend(_path_metadata(directory/relative) for relative in (
'Layout/JobPrefsDB.jpf', 'Layout_Temp/JobPrefsDB.jpf',
'Work/Layout_Temp/JobPrefsDB.jpf'))
warnings = [f"{item['path']}: {item.get('error') or 'absent (may be generated by Mentor)'}"
for item in paths if item['exists'] is not True or item.get('error')]
record = {'non_blocking': True, 'paths': paths, 'warnings': warnings,
'directory_writable': False,
'note': 'Missing temporary JobPrefs paths are diagnostic only; no files repaired.'}
# tempfile uses exclusive creation and delete-on-close for this new file;
# never construct a deletion path or touch an existing library-side file.
try:
with tempfile.NamedTemporaryFile(mode='wb', prefix='.cbb-verify-write-probe-',
suffix='.tmp', dir=directory):
pass
record['directory_writable'] = True
except OSError as error:
record['directory_write_error'] = str(error)
warnings.append(f'{directory}: create/close write probe failed: {error}')
return _write_record(Path(evidence_root)/'diagnostics/preflight.json', record)
def collect_native_failure_logs(pcb_path, evidence_root, *, max_bytes=NATIVE_LOG_MAX_BYTES):
"""Copy the last bounded bytes of known logs, preserving native multi-line text.
Evidence is binary so native ANSI/UTF-16 encodings are not damaged. Each
original path/size/truncation/error is recorded in a UTF-8 JSON manifest.
Native sources are opened read-only and promptly closed, never modified.
"""
pcb_directory = Path(pcb_path).parent
destination = Path(evidence_root)/'diagnostics/native-logs'
record = {'non_blocking': True, 'collected_at': time.strftime('%Y-%m-%d %H:%M:%S'),
'max_bytes_per_file': max(1, min(int(max_bytes), 256 * 1024)), 'files': []}
try:
destination.mkdir(parents=True, exist_ok=True)
except OSError as error:
record['evidence_error'] = str(error)
return record
sources = [('pcb-logfiles', pcb_directory/'LogFiles'/name)
for name in ('PCBLogFile.txt', 'ForwardAnnotation.txt', 'PartPkg.log',
'MsgBoxResponse.log', 'ReusableBlock.txt')]
sources.extend((('pcb-root', pcb_directory/'PCBLogFile.txt'),
('pcb-root', pcb_directory/'ReusableBlock.txt'),
('work-root', pcb_directory.parent/'ReusableBlock.txt')))
for label, source in sources:
item = {'source': str(source), 'exists': None}
record['files'].append(item)
try:
with source.open('rb') as stream:
stream.seek(0, os.SEEK_END)
size = stream.tell()
item['mtime_ns'] = os.fstat(stream.fileno()).st_mtime_ns
stream.seek(max(0, size-record['max_bytes_per_file']))
data = stream.read(record['max_bytes_per_file'])
item.update(exists=True, original_size_bytes=size,
truncated=size > record['max_bytes_per_file'])
target = destination/(label+'-'+source.name)
target.write_bytes(data)
item.update(evidence=str(target), copied_bytes=len(data))
if label == 'pcb-logfiles' and source.name != 'ReusableBlock.txt':
_collect_icdb_recovery(source, evidence_root)
except FileNotFoundError:
item['exists'] = False
except OSError as error:
item['error'] = str(error)
return _write_record(destination.parent/'native-logs.json', record)
def _collect_icdb_recovery(source, evidence_root):
"""Only promote a native error whose log changed after this launch."""
from .icdb_recovery import read_log_failure, read_icdb_error
root = Path(evidence_root)/'diagnostics'
try:
baseline = json.loads((root/'startup-log-baseline.json').read_text('utf-8'))
checkpoint = (baseline['baseline'] if baseline['source'] == str(source) else
baseline.get('integration_logs', {}).get(str(source)))
if checkpoint is None:
return
_, recovery = read_log_failure(source, checkpoint)
if recovery:
_write_record(root/'icdb-recovery.json', {**recovery, 'source': str(source)})
message = read_icdb_error(source, checkpoint)
if message:
_write_record(root/'icdb-error-content.json', {'native_message': message, 'source': str(source)})
except (OSError, ValueError, KeyError, TypeError):
pass # Unknown freshness must not replace the original failure.
def failure_logs_main(request_path):
job = json.loads(Path(request_path).read_text(encoding='utf-8'))
collect_native_failure_logs(job['pcb'], job['root'])
collect_native_client_error(job['root'])
return 0
def collect_native_client_error(evidence_root):
"""Read only this PCB PID's new iCDB manager log in the bounded log helper."""
root = Path(evidence_root)/'diagnostics'
try:
context = json.loads((root/'native-client-context.json').read_text('utf-8'))
pid, started = context['native_pid'], context['started_ns']
if type(pid) is not int or pid <= 0 or type(started) is not int:
return
target = str(context['database']).replace('/', '\\').casefold()
if not target:
return
from .icdb_recovery import decode_log
for directory in Path(context['client_log_root']).glob(f'*-{pid}'):
for source in directory.glob('*.iCDBManager.txt'):
with source.open('rb') as stream:
metadata = os.fstat(stream.fileno())
if (metadata.st_mtime_ns < started
or getattr(metadata, 'st_birthtime_ns', started) < started):
continue
header = stream.read(16*1024)
stream.seek(0, os.SEEK_END)
size = stream.tell()
stream.seek(max(0, size-NATIVE_LOG_MAX_BYTES))
tail = stream.read(NATIVE_LOG_MAX_BYTES)
if not re.search(rf'\bPID\s*\[{pid}\]', decode_log(header)):
continue
# The native iCDB logger supplies the domain; a pathname in a
# generic PCB error alone never establishes this identity.
lines = decode_log(tail).splitlines()
if size > NATIVE_LOG_MAX_BYTES:
lines = lines[1:] # The first bounded-tail line may start mid-message.
message = next((line for line in lines
if re.search(r'\bERROR\s*:', line, re.I)
and target in line.replace('/', '\\').casefold()), '')
if message:
target_file = root/'native-logs/current-icdb-manager.txt'
target_file.parent.mkdir(parents=True, exist_ok=True)
target_file.write_bytes(tail)
_write_record(root/'icdb-error-content.json', {'native_message': 'iCDB: '+message,
'source': str(source), 'native_pid': pid, 'started_ns': started,
'database': context['database'], 'evidence': str(target_file)})
return
except (OSError, ValueError, KeyError, TypeError):
pass # A missing/ambiguous native log must not replace the open failure.
def capture_failure_logs_with_timeout(request_path, *, timeout_seconds=8):
"""Post-cleanup retry in a killable helper, not a potentially stuck SMB thread."""
request_path = Path(request_path)
command = ([sys.executable] if getattr(sys, 'frozen', False) else
[sys.executable, '-m', 'cbb_refresh_verify'])
command += ['--collect-failure-logs', str(request_path)]
record = {'status': 'ERROR', 'timeout_seconds': timeout_seconds}
try:
outcome = subprocess.run(command, capture_output=True, timeout=timeout_seconds,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
record.update(status='COMPLETE' if outcome.returncode == 0 else 'ERROR',
exit_code=outcome.returncode)
if outcome.stderr:
record['error'] = outcome.stderr[-4096:].decode('utf-8', errors='replace')
except subprocess.TimeoutExpired:
# subprocess.run kills and reaps this child; it never launches children.
record.update(status='TIMEOUT', error='Native log collection budget exceeded; original Verify result unchanged.')
except OSError as error:
record['error'] = str(error)
return _write_record(request_path.parent/'diagnostics/native-log-capture.json', record)