Files
dxdesigner-part-replacement/tools/accept_random_cad.py

979 lines
56 KiB
Python

"""Seeded, disposable real-project acceptance; actual Qt mouse/keyboard UI only.
Prepare does not start CAD. Run requires the exclusive CAD slot. This deliberately
records a single engineering family; twenty tasks do not imply twenty designs.
"""
from __future__ import annotations
import argparse
from collections import Counter
from datetime import datetime
import hashlib
import json
import os
from pathlib import Path
import random
import re
import secrets
import shutil
import sqlite3
import sys
import time
import traceback
def write(path, value):
path.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding='utf-8')
def fingerprint(directory):
return {p.relative_to(directory).as_posix(): hashlib.sha256(p.read_bytes()).hexdigest()
for p in sorted(directory.rglob('*')) if p.is_file()}
def has_nc_marker(value):
return '/' in value and re.search(r'(?:^|/)\s*NC\s*(?:$|/)', value, re.I) is not None
def code_fingerprint(directory):
"""Fingerprint source/assets without installed dependencies or bytecode caches."""
result = {}
for current, directories, files in os.walk(directory):
directories[:] = sorted(name for name in directories if name not in {'node_modules', '__pycache__', '.git'})
for name in sorted(files):
path = Path(current) / name
if path.suffix != '.pyc':
result[path.relative_to(directory).as_posix()] = hashlib.sha256(path.read_bytes()).hexdigest()
return result
def prepare(root, source, graph, target_parts, *, seed=None, count=20, cells=(), supplemental='', part_prefixes=('4100',)):
root, source = Path(root).resolve(), Path(source).resolve(strict=True)
if root.exists() or root.is_relative_to(source.parent) or source.is_relative_to(root):
raise ValueError('Output must be a new directory outside the original project ancestry')
if (count < 20 and not supplemental) or not target_parts:
raise ValueError('At least20 cases and compatible target parts required')
if supplemental == 'rule_mode' and (count != 1 or list(target_parts) != ['4100-CA0000-T0']):
raise ValueError('rule_mode requires count=1 and target 4100-CA0000-T0')
root.mkdir(parents=True)
system_seed = seed is None
seed = secrets.randbits(128) if system_seed else seed
write(root / 'seed.json', {'seed': seed, 'created_ns': time.time_ns(), 'system_seed': system_seed})
rng = random.Random(seed)
candidates = sorted((c for c in graph['components'].values()
if c['refdes'] and c['attributes'].get('Part Number', '').startswith(tuple(part_prefixes))),
key=lambda c: c['refdes'])
if supplemental in {'rule_mode', 'pon_root'}:
if count != 1:
raise ValueError('Root supplemental requires count=1')
part_counts = Counter(c['attributes'].get('Part Number', '').casefold() for c in graph['components'].values())
ref_counts = Counter(c['refdes'].casefold() for c in graph['components'].values() if c['refdes'])
source_uids = Counter(c.get('source_uid', uid) for uid,c in graph['components'].items())
unique_source = {id(c): source_uids[c.get('source_uid',uid)] == 1 for uid,c in graph['components'].items()}
candidates = [c for c in candidates if not c.get('instance_path') and not c.get('source_refdes') and not c.get('ambiguous_attributes')
and c['attributes']['Part Number'].startswith('4100-')
and c['attributes']['Part Number'].casefold() not in {p.casefold() for p in target_parts}
and part_counts[c['attributes']['Part Number'].casefold()] == 1
and ref_counts[c['refdes'].casefold()] == 1 and len(c['pins']) == 2 and unique_source[id(c)]]
if not candidates:
raise ValueError('Root supplemental needs a globally unique root resistor Part Number')
elif len(candidates) < 2:
raise ValueError('Need two authentic compatible source resistor references')
kinds = ([supplemental] * count if supplemental else
['normal'] * (count - 9) + ['already_target'] * 2 + ['unknown_ref'] * 2 + ['stale_old'] * 2 + ['invalid_part', 'corrupt_database', 'pause_resume'])
rng.shuffle(kinds)
cases = []
for index, kind in enumerate(kinds):
nested = rng.choice(('项目', 'nested/工程', 'ASCII/boards'))
directory = root / 'projects' / f'{index+1:02d}-{kind}-{rng.randrange(10000,99999)}' / nested
directory.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(source.parent, directory)
project = directory / source.name
selected = rng.sample(candidates, 1 if kind in {'rule_mode','pon_root'} else rng.randint(1, 2))
requests = []
for comp in selected:
old = comp['attributes']['Part Number']
new = rng.choice(target_parts)
mode = rng.choice(('preserve', 'part_label', 'part_number', 'custom'))
request = {'refdes': comp['refdes'], 'old_part_number': old, 'new_part_number': new,
'value_mode': mode, 'custom_value': f'{rng.randrange(10000,99999)}Ω-验收',
'sync_nc': rng.choice((True, False)), 'enabled': True}
if kind == 'rule_mode':
request.update(value_mode='preserve', custom_value='', sync_nc=True)
elif kind == 'already_target':
request.update(old_part_number=new, new_part_number=old, value_mode='preserve')
elif kind == 'unknown_ref':
request['refdes'] = f'R_UNKNOWN_{rng.randrange(1000000)}'
elif kind == 'stale_old':
request['old_part_number'] = f'STALE-{rng.randrange(1000000)}'
elif kind == 'invalid_part':
request['new_part_number'] = f'NOT-IN-LIBRARY-{rng.randrange(1000000)}'
requests.append(request)
if kind == 'corrupt_database':
databases = [p for p in directory.rglob('*') if p.name == 'icdb.dat' and p.is_file()]
if len(databases) != 1:
raise ValueError('Expected one self-copied database to corrupt')
assert databases[0].resolve().is_relative_to((root / 'projects').resolve())
databases[0].write_bytes(b'RANDOM-ACCEPTANCE-CORRUPT-' + str(seed).encode())
cases.append({'id': index+1, 'kind': kind, 'project': str(project), 'requests': requests,
'target_cell': rng.choice(cells) if kind == 'valid_cell' and cells else '',
'fault': {'stage': 'native_replace', 'delay_seconds': rng.randint(3, 12)} if kind == 'force_stop' else None,
'input_hashes': fingerprint(directory)})
manifest = {'seed': seed, 'source': str(source), 'source_hashes': fingerprint(source.parent),
'family_limit': 'One ordinary authentic resistor engineering design; task semantics vary, project family does not.',
'target_parts': list(target_parts), 'part_prefixes': list(part_prefixes), 'available_cells': list(cells), 'cases': cases,
'source_code_hashes': code_fingerprint(Path(__file__).resolve().parents[1] / 'src'),
'integration_hashes': code_fingerprint(Path(__file__).resolve().parents[1] / 'integration'),
'code_fingerprint_exclusions': ['node_modules', '__pycache__', '.git', '*.pyc']}
write(root / 'manifest.json', manifest)
return manifest
def prepare_baseline(root):
from dx_productivity.trial_task_center.native_netlist import capture_project, export_netlist
manifest = json.loads((root / 'manifest.json').read_text(encoding='utf-8'))
result = export_netlist(capture_project(Path(manifest['source']), root / 'baseline-capture'), root / 'baseline-export')
if not result['complete']:
raise AssertionError(result)
match = re.search(r'^KEY CentralLibrary "([^"]+)"', Path(manifest['source']).read_text(encoding='utf-8'), re.M)
if match:
library = Path(match[1])
manifest['library'] = {'path': str(library), 'sha256': hashlib.sha256(library.read_bytes()).hexdigest(), 'bytes': library.stat().st_size}
manifest['baseline'] = str(root / 'baseline-export/graph.json')
write(root / 'manifest.json', manifest)
print(json.dumps({'baseline': result['status'], 'components': len(result['graph']['components'])}), flush=True)
def workbook(root, manifest):
from openpyxl import Workbook
headers = ['project_name', 'project_path', 'old_part_number', 'new_part_number', 'refdes',
'schematic_name', 'sheet_number', 'uid', 'enabled', 'custom_value', 'sync_nc', 'value_mode']
book = Workbook()
sheet = book.active
sheet.append(headers)
for case in manifest['cases']:
for request in case['requests']:
row = dict(request, project_name=Path(case['project']).stem, project_path=case['project'])
sheet.append([row.get(h, '') for h in headers])
book.save(root / 'random-bom.xlsx')
book.close()
def database(root, sql):
with sqlite3.connect((root / 'data/task-center.db').as_uri() + '?mode=ro', uri=True) as conn:
conn.row_factory = sqlite3.Row
return [dict(row) for row in conn.execute(sql)]
def import_timing(root):
lines = (root / 'data/logs/application.log').read_text(encoding='utf-8').splitlines()
starts, spans, import_started, import_seconds = {}, [], None, None
for line in lines:
match = re.search(r'operation_(start|complete)=(\w+) task_id=(\S*) at=(\S+)', line)
if not match:
continue
phase, operation, task_id, timestamp = match.groups()
stamp = datetime.fromisoformat(timestamp)
if operation == 'import_bom_drafts' and phase == 'start':
starts, spans, import_started = {}, [], stamp
if import_started is None:
continue
if operation == 'refresh_bound_project':
if phase == 'start':
starts[task_id] = stamp
elif task_id in starts:
spans.append((stamp-starts.pop(task_id)).total_seconds())
if operation == 'import_bom_drafts' and phase == 'complete':
import_seconds = (stamp-import_started).total_seconds()
import_started = None
result = {'import_total_seconds': import_seconds, 'metadata_refresh_count': len(spans),
'metadata_refresh_seconds_sum': sum(spans),
'note': 'Import total includes routing/metadata refresh and workbook I/O; it is not Excel parse time.'}
write(root / 'import-timing.json', result)
return result
def run_ui(root, *, pause=True, prepare_only=False):
"""Source-run visible windows; no service import/create/execute shortcut."""
from PySide6.QtCore import Qt, QTimer
from PySide6.QtTest import QTest
from PySide6.QtWidgets import QApplication, QDialogButtonBox, QFileDialog, QLineEdit, QMessageBox, QPushButton
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.ui import TrialTaskCenterWindow
manifest = json.loads((root / 'manifest.json').read_text(encoding='utf-8'))
pause = pause and len(manifest['cases']) > 1 and any(case['kind'] == 'pause_resume' for case in manifest['cases'])
assert fingerprint(Path(manifest['source']).parent) == manifest['source_hashes']
assert all(Path(c['project']).resolve().is_relative_to(root / 'projects') for c in manifest['cases'])
QApplication.setAttribute(Qt.ApplicationAttribute.AA_DontUseNativeDialogs)
app = QApplication.instance() or QApplication([])
logging = configure_logging(root / 'data')
store = TaskStore(root / 'data/task-center.db')
service = TaskCenterService(store, logging, supervised=True)
window = TrialTaskCenterWindow(service)
window.resize(1450, 1000)
window.show()
stamp = str(time.time_ns())
history = root / 'ui-history'
history.mkdir(exist_ok=True)
prior = root / 'ui-report.json'
if prior.exists():
shutil.copyfile(prior, history / (stamp + '-prior.json'))
results = {'mode': 'source Qt QTest visible mouse/keyboard UI, real supervised Mentor', 'started_ns': time.time_ns(), 'steps': [], 'failures': []}
shots = root / 'screenshots' / stamp
shots.mkdir(parents=True, exist_ok=True)
def record(step, **details):
results['steps'].append(dict(step=step, ns=time.time_ns(), **details))
write(root / 'ui-report.json', results)
window.grab().save(str(shots / f'{len(results["steps"]):02d}-{step}.png'))
print(json.dumps(results['steps'][-1], ensure_ascii=False), flush=True)
def wait(check, timeout=1200):
deadline = time.monotonic()+timeout
while time.monotonic() < deadline:
app.processEvents()
if check():
return
QTest.qWait(100)
raise TimeoutError('UI condition exceeded deadline')
def click(widget):
assert widget.isEnabled(), widget.objectName()
QTest.mouseClick(widget, Qt.MouseButton.LeftButton)
def text(widget, value):
widget.setFocus()
QTest.keyClick(widget, Qt.Key.Key_A, Qt.KeyboardModifier.ControlModifier)
app.clipboard().setText(str(value))
QTest.keyClick(widget, Qt.Key.Key_V, Qt.KeyboardModifier.ControlModifier)
def directory_dialog():
dialog = app.activeModalWidget()
if dialog is None or dialog.windowTitle() != '从目录创建工程任务':
QTimer.singleShot(100, directory_dialog)
return
text(dialog.findChild(QLineEdit, 'taskDirectory'), root / 'projects')
click(dialog.findChild(QPushButton, 'readTaskProjects'))
def create():
button = dialog.findChild(QDialogButtonBox).button(QDialogButtonBox.StandardButton.Ok)
if not button.isEnabled():
QTimer.singleShot(100, create)
return
record('directory-scanned')
click(button)
QTimer.singleShot(100, create)
def choose_bom():
dialog = app.activeModalWidget()
if not isinstance(dialog, QFileDialog):
QTimer.singleShot(100, choose_bom)
return
edit = dialog.findChild(QLineEdit, 'fileNameEdit')
text(edit, root / 'random-bom.xlsx')
QTest.keyClick(edit, Qt.Key.Key_Return)
def add_cell_rule(request, cell):
# Real rule dialog: load actual library choices, navigate the dropdown.
bar = window.mapping_tabs.tabBar()
QTest.mouseClick(bar, Qt.MouseButton.LeftButton, pos=bar.tabRect(0).center())
def fill():
dialog = app.activeModalWidget()
if dialog is None or dialog.windowTitle() != '替换规则':
QTimer.singleShot(100, fill)
return
text(dialog.old_edit, request['old_part_number'])
text(dialog.new_edit, request['new_part_number'])
click(dialog.cell_load_button)
def select():
if not dialog.cell_load_button.isEnabled():
QTimer.singleShot(100, select)
return
combo = dialog.cell_combo
index = combo.findData(cell)
if index < 0:
record('cell-choice-unavailable', expected=cell, status=dialog.cell_status.text())
click(dialog.buttons.button(QDialogButtonBox.StandardButton.Cancel))
return
combo.setFocus()
QTest.keyClick(combo, Qt.Key.Key_Home)
for _ in range(index):
QTest.keyClick(combo, Qt.Key.Key_Down)
record('cell-rule-selected', cell=combo.currentData(), refdes=request['refdes'])
click(dialog.buttons.button(QDialogButtonBox.StandardButton.Ok))
QTimer.singleShot(100, select)
QTimer.singleShot(100, fill)
click(window.add_mapping_button)
wait(lambda: not window._workers)
assert any(row['target_cell'] == cell for row in database(root, 'select * from rules'))
QTest.mouseClick(bar, Qt.MouseButton.LeftButton, pos=bar.tabRect(1).center())
try:
record('opened')
if window.auto_intervention_check.isChecked():
click(window.auto_intervention_check)
record('automatic-ai-disabled-for-independent-ui-acceptance')
if not database(root, 'select * from tasks'):
QTimer.singleShot(100, directory_dialog)
click(window.task_list.new_button)
wait(lambda: len(database(root, 'select * from tasks')) == len(manifest['cases']))
record('tasks-created', count=len(manifest['cases']))
wait(lambda: window.task_list.list.count() == len(manifest['cases']))
first = window.task_list.list.item(0)
QTest.mouseClick(window.task_list.list.viewport(), Qt.MouseButton.LeftButton,
pos=window.task_list.list.visualItemRect(first).center())
wait(window.plan_panel.import_bom_button.isEnabled)
cell_cases = [case for case in manifest['cases'] if case['target_cell']]
if cell_cases:
assert len(manifest['cases']) == 1, 'Cell supplemental UI uses one standalone task'
unique_rules = {(r['old_part_number'], r['new_part_number']): r for r in cell_cases[0]['requests']}
for request in unique_rules.values():
add_cell_rule(request, cell_cases[0]['target_cell'])
QTimer.singleShot(100, choose_bom)
click(window.plan_panel.import_bom_button)
wait(lambda: window.plan_panel._active_bom_import is None and window.plan_panel.last_bom_report is not None)
record('bom-imported', report=window.plan_panel.report_text, timings=import_timing(root))
if prepare_only:
record('prepare-only-finished')
return results
results['execution_source_hashes'] = code_fingerprint(Path(__file__).resolve().parents[1] / 'src')
results['execution_integration_hashes'] = code_fingerprint(Path(__file__).resolve().parents[1] / 'integration')
# Execute performs the single actual Save -> range confirmation UI flow.
def confirm():
dialog = app.activeModalWidget()
if not isinstance(dialog, QMessageBox):
QTimer.singleShot(100, confirm)
return
record('execution-confirmation', title=dialog.windowTitle(), text=dialog.text())
yes = dialog.button(QMessageBox.StandardButton.Yes)
if yes is None:
raise AssertionError('Unexpected modal instead of execution confirmation')
click(yes)
QTimer.singleShot(100, confirm)
button = window.findChild(QPushButton, 'executeProjectTasks')
wait(button.isEnabled)
click(button)
wait(lambda: bool(list((root / 'data/task-queues').glob('*.json'))))
record('execution-started')
last = None
paused_once = False
resumed = False
saved_before_resume = None
fault = next((case['fault'] for case in manifest['cases'] if case.get('fault')), None)
fault_stage_seen = None
forced = recovered = False
deadline = time.monotonic()+14400
while time.monotonic() < deadline:
app.processEvents()
tasks = database(root, 'select task_id,name,status,error_id from tasks order by group_position')
status = [(t['task_id'], t['status']) for t in tasks]
if status != last:
record('task-state', tasks=tasks)
last = status
queues_live = [json.loads(p.read_text(encoding='utf-8')) for p in (root / 'data/task-queues').glob('*.json')]
has_pause_window = any(
any(e['state'] == 'completed' for e in q['entries'].values())
and any(e['state'] == 'running' for e in q['entries'].values())
for q in queues_live)
if fault and not forced:
frames = [json.loads(p.read_text(encoding='utf-8')) for p in (root / 'data/tasks').glob('*/quick-run/worker-progress.json')]
in_stage = any(frame.get('stage') == fault['stage'] for frame in frames)
if in_stage and fault_stage_seen is None:
fault_stage_seen = time.monotonic()
if (in_stage and fault_stage_seen is not None
and time.monotonic()-fault_stage_seen >= fault['delay_seconds']
and window.run_panel.force_stop_button.isEnabled()):
QTimer.singleShot(100, confirm)
click(window.run_panel.force_stop_button)
forced = True
results['fault_injected'] = {'trigger': fault, 'frames': frames}
record('real-ui-force-stop-requested', fault=fault)
if pause and not paused_once and has_pause_window:
stop = window.run_panel.stop_queue_button
if stop.isEnabled():
click(stop)
paused_once = True
record('pause-requested')
if not window._workers:
queues = [json.loads(p.read_text(encoding='utf-8')) for p in (root / 'data/task-queues').glob('*.json')]
record('queue-idle', queues=queues)
if forced and not recovered:
for dialog in app.topLevelWidgets():
if dialog is not window and dialog.isVisible() and '替换结果' in dialog.windowTitle():
QTest.keyClick(dialog, Qt.Key.Key_Escape)
if window.run_panel.recover_button.isEnabled():
QTimer.singleShot(100, confirm)
click(window.run_panel.recover_button)
recovered = True
record('real-ui-recovery-requested')
QTest.qWait(500)
continue
record('force-stop-requires-manual-verification', message=window.run_panel.operator_band.text())
if paused_once and not resumed and window._project_queues:
saved_before_resume = saved_uids(root)
# Close actual result dialog to expose the same batch resume button.
for dialog in app.topLevelWidgets():
if dialog is not window and dialog.isVisible() and '替换结果' in dialog.windowTitle():
QTest.keyClick(dialog, Qt.Key.Key_Escape)
wait(button.isEnabled, timeout=30)
QTimer.singleShot(100, confirm)
click(button)
resumed = True
record('resume-confirmed', saved_uids=saved_before_resume)
QTest.qWait(500)
continue
break
QTest.qWait(500)
else:
raise TimeoutError('Actual execution still active; preserve process for observation')
results['tasks'] = database(root, 'select * from tasks')
if fault:
results['force_stop_actual'] = forced
results['recovery_actual'] = recovered
if saved_before_resume:
after = saved_uids(root)
assert all(after.get(key) == count for key, count in saved_before_resume.items()), 'Saved UID replayed on resume'
results['resume_saved_uid_no_replay'] = True
results['finished_ns'] = time.time_ns()
record('run-finished')
except BaseException:
results['failures'].append(traceback.format_exc())
write(root / 'ui-report.json', results)
raise
finally:
write(root / 'ui-report.json', results)
write(history / (stamp + '.json'), results)
# Keep Qt alive until owned workers release resources; never kill on timeout.
return results
def saved_uids(root):
events = []
for path in (root / 'data/tasks').glob('*/events.jsonl'):
for line in path.read_text(encoding='utf-8').splitlines():
event = json.loads(line)
if event.get('operation') == 'quick_run_executed' and event.get('details', {}).get('state') == 'saved_ok':
events.append((path.parent.name, event))
if not events:
return {}
try:
operations = database(root, '''SELECT o.task_id,o.command_json,o.receipt_json
FROM execution_operations o JOIN execution_rounds r
ON r.task_id=o.task_id AND r.round_id=o.round_id''')
bindings = {}
for row in operations:
receipt = json.loads(row['receipt_json'] or '{}')
if receipt.get('native_state') == 'saved_ok' and receipt.get('job_id'):
key = (row['task_id'], receipt['job_id'])
bindings.setdefault(key, []).append((json.loads(row['command_json']), receipt))
counts = Counter()
for task_id, event in events:
job = event['details']['timing']['job_id']
if not job or event.get('task_id', task_id) != task_id:
raise ValueError('Saved event task/job identity is missing or inconsistent')
command, receipt = bindings[(task_id, job)][0]
if len(bindings[(task_id, job)]) != 1:
raise ValueError('Saved event job is bound to multiple operations')
for uid in _saved_operation_uids(root, task_id, command, receipt):
counts[task_id + ':' + uid] += 1
return dict(counts)
except (OSError, sqlite3.Error, KeyError, TypeError, ValueError, IndexError) as error:
raise ValueError('Acceptance evidence insufficient: saved event job/UID binding: ' + str(error)) from error
def _saved_operation_uids(root, task_id, command, receipt):
if command.get('target_scope') == 'targets':
selectors = [selector.split('\x1f') for selector in command['target_selectors'].split('\x1e')]
if any(len(fields) != 3 or not re.fullmatch(r'\$\d+I\d+', fields[0]) for fields in selectors):
raise ValueError('Frozen target selectors lack exact native UIDs')
uids = [fields[0] for fields in selectors]
if len(set(uids)) != len(uids):
raise ValueError('Duplicate frozen selector UID')
return uids
if command.get('target_scope', 'all') != 'all' or command.get('target_selectors'):
raise ValueError('Unsupported saved operation scope')
from dx_productivity.trial_task_center.native_helper import read_snapshot, require_resumable_transaction
files = {Path(item['path']).resolve(strict=True): item['sha256'] for item in receipt['proof']['files']}
uids = set()
for path in files:
if not path.name.endswith('.native.json'):
continue
snapshot = Path(str(path).removesuffix('.native.json'))
owner = (root / 'data/tasks' / task_id).resolve()
if (not path.is_relative_to(owner) or snapshot not in files
or any(hashlib.sha256(p.read_bytes()).hexdigest() != files[p] for p in (path, snapshot))):
raise ValueError('Native receipt snapshot proof is missing, foreign or changed')
report = json.loads(path.read_text(encoding='utf-8'))
attempt = report['batch_attempt']
metadata, records = read_snapshot(snapshot)
if (report.get('ok') is not True or report.get('stage') != 'native_replace_verified'
or attempt['task_id'] != task_id or attempt['job_id'] != receipt['job_id']
or Path(metadata[1]).resolve() != Path(command['project_path']).resolve()
or [part.casefold() for part in metadata[2:4]] != [command['old_part'].casefold(), command['new_part'].casefold()]
or metadata[4] != command['schematic_name'] + ':' + command['sheet_number']):
raise ValueError('Native receipt does not bind this saved rule operation')
binding = require_resumable_transaction(snapshot, report, metadata, records)
uids.update(binding['source_uids'])
if not uids:
raise ValueError('Saved rule operation lacks proven native UIDs')
return sorted(uids)
def raw_attributes(path):
"""Independent tiny ASCII attribute reader, deliberately outside graph compare."""
result, ref = {}, None
raw = path.read_bytes()
try:
text = raw.decode('utf-8')
except UnicodeDecodeError:
text = raw.decode('mbcs')
for line in text.splitlines():
match = re.match(r' Symbol .+\tref des ([^\t]*)\tpage ', line)
if match:
ref = match[1]
if ref:
if ref in result:
raise ValueError('Independent acceptance requires unique root references')
result[ref] = {}
elif ref and line.startswith('\tProp '):
for name in ('Part Number', 'Part Label', 'Cell Name', 'Value'):
prefix = '\tProp ' + name + ' '
if line.startswith(prefix):
if name in result[ref]:
raise ValueError('Ambiguous raw attribute')
result[ref][name] = line[len(prefix):]
return result
def raw_identity_attributes(path):
"""Independent ASCII property multisets, retaining every gate and instance."""
data=path.read_bytes()
try:
text=data.decode('utf-8')
except UnicodeDecodeError:
text=data.decode('mbcs')
records=[]
current=None
for line in text.splitlines():
match=re.match(r' Symbol (.+?)\tref des ([^\t]*)\tpage ',line)
if match:
current={'source_uid':match[1],'refdes':match[2],'instance_path':'','properties':[], 'symbol':''}
records.append(current)
elif line.startswith((' Net ','Block ')):
current=None
elif current is not None:
if line.startswith('\tPath '):
value=line[6:].strip()
current['instance_path']='' if value in {'','root'} else value
elif line.startswith('\tProp '):
current['properties'].append(line[6:])
elif line.startswith('\tSymbol name '):
current['symbol']=line[13:]
if not records:
raise ValueError('Independent raw identity report contains no symbols')
return records
def unselected_raw_identities(records, selected_root_uids):
return Counter((row['source_uid'],row['instance_path'],row['refdes'],row['symbol'],tuple(sorted(row['properties'])))
for row in records if row['instance_path'] or row['source_uid'] not in selected_root_uids)
def selected_root_attributes(records, selected_root_uids):
result={}
found=set()
for row in records:
if row['instance_path'] or row['source_uid'] not in selected_root_uids:
continue
if row['source_uid'] in found or not row['refdes'] or row['refdes'] in result:
raise ValueError('Selected root UID/reference is not unique in independent ASCII')
found.add(row['source_uid'])
attrs={}
for prop in row['properties']:
for name in ('Part Number','Part Label','Cell Name','Value'):
if prop.startswith(name+' '):
if name in attrs:
raise ValueError('Selected root has ambiguous raw attribute: '+name)
attrs[name]=prop[len(name)+1:]
result[row['refdes']]=attrs
if found != selected_root_uids:
raise ValueError('Selected root UID is absent from independent ASCII')
return result
def force_recovery_evidence(root, *, expected_stage='native_replace', ui_report=None):
path=root/(ui_report or 'packaged-ui-report.json')
if ui_report is not None:
assert path.resolve().is_relative_to(root.resolve()) and path.is_file()
if path.exists():
report=json.loads(path.read_text(encoding='utf-8'))
assert report['stage']=='execute' and report.get('passed') is True
assert report['mode'].startswith('frozen EXE external Windows UIA')
assert Path(report['root']).resolve()==root.resolve()
session=json.loads((root/'ui-session.json').read_text(encoding='utf-8'))
assert report['pid']==session['pid'] and report['exe_sha256']==session['exe_sha256']
assert Path(report['exe']).resolve()==Path(session['executable']).resolve()
assert hashlib.sha256(Path(report['exe']).read_bytes()).hexdigest()==report['exe_sha256']
steps=[step['step'] for step in report['steps']]
assert 'real-ui-force-confirmed' in steps and 'real-ui-recovery-finished' in steps
assert steps.index('real-ui-force-confirmed') < steps.index('real-ui-recovery-finished')
forced=next(step for step in report['steps'] if step['step']=='real-ui-force-confirmed')
active=[frame for frame in forced['frames'] if frame.get('stage')==expected_stage]
assert active and all(re.fullmatch('[0-9a-f]{32}',frame.get('token',''))
and frame.get('job_id') and type(frame.get('sequence')) is int and frame['sequence']>0 for frame in active)
else:
path=root/'ui-report.json'
report=json.loads(path.read_text(encoding='utf-8'))
assert report.get('force_stop_actual') and report.get('recovery_actual')
return {'force_stop_actual':True,'recovery_observed':True,'ui_evidence':str(path),
'ui_evidence_sha256':hashlib.sha256(path.read_bytes()).hexdigest()}
def native_value_observation(text, *, job, uid, old, new):
"""Read count-complete raw COM groups; never derive baseline from reopened Value."""
decode = lambda value: re.sub(r'%([0-9a-fA-F]{4})', lambda m: chr(int(m[1], 16)), value)
groups, current, previous_sequence = [], None, 0
for line in text.lstrip('\ufeff').splitlines():
fields = [decode(field) for field in line.split('\t')]
if fields[0] == 'O':
if current is not None or len(fields) != 7 or fields[1] != job:
raise ValueError('Legacy, incomplete or foreign observation group')
sequence = int(fields[5])
if (sequence <= previous_sequence or (previous_sequence and sequence != previous_sequence+1)
or not 0 <= float(fields[4].replace(',', '.')) < 86400
or fields[6] not in {'before_native', 'pre_restore', 'restoring', 'save_pending', 'saved_readback'}):
raise ValueError('Invalid observation sequence or time')
current = {'job': fields[1], 'uid': fields[2], 'stage': fields[3], 'sequence': sequence,
'window': fields[6], 'attributes': {}}
elif fields[0] == 'E':
if (current is None or len(fields) != 7 or fields[1] != current['uid']
or int(fields[2]) != len(current['attributes']) or fields[6] != 'unknown'
or fields[3].casefold() in current['attributes']):
raise ValueError('Invalid attribute identity, ordinal or duplicate name')
current['attributes'][fields[3].casefold()] = fields[5] or fields[4]
elif fields[0] == 'Z_OBS':
if (current is None or len(fields) != 7
or fields[1:4] != [current['job'], current['uid'], current['stage']]
or int(fields[4]) != current['sequence'] or int(fields[5]) != len(current['attributes'])
or fields[6] != current['window']):
raise ValueError('Observation completion identity/count mismatch')
current['count'] = int(fields[5])
groups.append(current)
previous_sequence = current['sequence']
current = None
else:
raise ValueError('Unknown observation record')
if current is not None:
raise ValueError('Observation group has no completion marker')
selected = [group for group in groups if group['uid'] == uid]
seen_old, candidate = False, None
for index, group in enumerate(selected):
part = group['attributes'].get('part number')
if part == old and candidate is None and group['window'] == 'before_native':
seen_old = True
elif part == new and candidate is None:
if not seen_old or group['window'] != 'pre_restore':
raise ValueError('First target observation is outside the native pre-restoration window')
if not any(g['window'] in {'restoring', 'save_pending', 'saved_readback'} for g in selected[index+1:]):
raise ValueError('Native observation has no later restoration/save boundary')
candidate = group
elif part not in {old, new} or (candidate is not None and part != new):
raise ValueError('Observation Part Number differs from bound transaction')
if candidate is None:
raise ValueError('Bound native intermediate observation is missing')
reopened = next((g for g in reversed(selected) if g['sequence'] > candidate['sequence']
and g['window'] == 'saved_readback'), None)
result = {'sequence': candidate['sequence'], 'count': candidate['count'], 'stage': candidate['stage'],
'window': candidate['window'],
'value': candidate['attributes'].get('value', ''), 'present': 'value' in candidate['attributes'],
'part_label': candidate['attributes'].get('part label'), 'uid': uid, 'job': job}
if reopened is not None:
result.update(reopened_present='value' in reopened['attributes'], reopened_value=reopened['attributes'].get('value', ''),
reopened_sequence=reopened['sequence'], reopened_count=reopened['count'])
return result
def bound_native_observation(root, task_id, uid, request):
work = root / 'data/tasks' / task_id / 'quick-run'
matches = []
for path in work.glob('snapshot-*.tsv.native.json'):
record = json.loads(path.read_text(encoding='utf-8'))
transaction, attempt = record.get('transaction', {}), record.get('batch_attempt', {})
if uid not in transaction.get('source_uids', ()):
continue
snapshot = Path(record['snapshot']).resolve(strict=True)
if (not snapshot.is_relative_to(work.resolve()) or not record.get('ok')
or record.get('stage') != 'native_replace_verified' or attempt.get('task_id') != task_id
or transaction.get('old_part_number') != request['old_part_number']
or transaction.get('new_part_number') != request['new_part_number']
or hashlib.sha256(snapshot.read_bytes()).hexdigest() != transaction.get('snapshot_sha256')
or not record.get('native_options', {}).get('1700', {}).get('checked')):
raise ValueError('Native intermediate transaction binding is invalid')
library_row = record.get('library_row', ())
if len(library_row) != 7 or library_row[1] != request['new_part_number']:
raise ValueError('Raw selected library row does not match target Part Number')
job = attempt['job_id']
observations = work / ('attribute-observations-' + job + '.tsv')
result = native_value_observation(observations.read_text(encoding='utf-8-sig'), job=job, uid=uid,
old=request['old_part_number'], new=request['new_part_number'])
result.update(raw_observation=str(observations), native_transaction=str(path),
observation_sha256=hashlib.sha256(observations.read_bytes()).hexdigest(),
transaction_sha256=hashlib.sha256(path.read_bytes()).hexdigest(), library_row=list(library_row))
matches.append(result)
if len(matches) != 1:
raise ValueError('Native intermediate transaction is absent or ambiguous')
return matches[0]
def rule_netlist_evidence(root, task, entry, case, before):
"""Require the application's frozen rule -> native UID -> whole-graph result."""
assert not task['target_plan_mode'] and not task['copy_part_number_to_value']
request, = case['requests']
uid, = [uid for uid, comp in before['components'].items() if comp['refdes'] == request['refdes']]
assert not before['components'][uid].get('instance_path')
assert before['components'][uid]['attributes']['Part Number'] == request['old_part_number']
path = Path(entry['netlist_verification']['report_path']).resolve(strict=True)
assert path.is_relative_to((root / 'data/tasks' / task['task_id'] / 'netlist-checks').resolve())
report = json.loads(path.read_text(encoding='utf-8'))
plan_path = path.with_name('plan.json')
plan = json.loads(plan_path.read_text(encoding='utf-8'))
assert plan['rows'] == [] and plan['copy_value'] is False
assert Path(plan['project_path']).resolve() == Path(case['project']).resolve()
assert plan['rules'] == [{'old_part': request['old_part_number'].casefold(),
'new_part': request['new_part_number'].casefold(), 'value_mode': 'preserve',
'custom_value': '', 'sync_nc': '1', 'target_cell': ''}]
assert report['status'] == 'verified' and report['verified'] is True
assert all(report[side]['status'] == 'success' and report[side]['complete'] is True for side in ('before', 'after'))
scope, = report['rule_scopes']
assert scope['source_uids'] == [uid] and scope['schematic_name'] and scope['sheet_number']
assert Path(scope['project_path']).resolve() == Path(case['project']).resolve()
assert scope['old_part'].casefold() == request['old_part_number'].casefold()
assert scope['new_part'].casefold() == request['new_part_number'].casefold()
assert set(report['expected_changes']) == {uid}
return {'bound_uid': uid, 'rule_scope': scope, 'verification_report': str(path),
'verification_sha256': hashlib.sha256(path.read_bytes()).hexdigest(),
'frozen_plan': str(plan_path), 'plan_sha256': hashlib.sha256(plan_path.read_bytes()).hexdigest()}
def queue_entries(root, *, queue_id=None):
result = {}
directory = root / 'data/task-queues'
if queue_id is not None:
if not isinstance(queue_id, str) or not re.fullmatch('[0-9a-f]{32}', queue_id):
raise ValueError('Invalid explicit queue identity')
paths = [directory / (queue_id + '.json')]
else:
paths = directory.glob('*.json')
for path in paths:
queue = json.loads(path.read_text(encoding='utf-8'))
reports = queue.get('netlist_verification', {})
for task_id, entry in queue.get('entries', {}).items():
result[task_id] = dict(entry)
if task_id in reports:
result[task_id]['netlist_verification'] = reports[task_id]
return result
def verify(root, *, queue_id=None, force_ui_report=None):
"""Fresh native CLI exports, independent comparisons, no Mentor fallback."""
from dx_productivity.trial_task_center.native_netlist import capture_project, export_netlist
manifest = json.loads((root / 'manifest.json').read_text(encoding='utf-8'))
before = json.loads(Path(manifest['baseline']).read_text(encoding='utf-8'))
references=Counter(c['refdes'] for c in before['components'].values() if c['refdes'])
complex_graph=(any(count>1 for count in references.values())
or any(c.get('instance_path') or c.get('ambiguous_attributes') for c in before['components'].values()))
original_identities=raw_identity_attributes(Path(manifest['baseline']).with_name('effective.txt')) if complex_graph else None
original_raw = {} if complex_graph else raw_attributes(Path(manifest['baseline']).with_name('effective.txt'))
tasks = database(root, 'select * from tasks')
projects = database(root, 'select * from projects')
entries = queue_entries(root, queue_id=queue_id)
stamp = str(time.time_ns())
if (root / 'independent-report.json').exists():
archive = root / 'independent-report-history'
archive.mkdir(exist_ok=True)
shutil.copyfile(root / 'independent-report.json', archive / (stamp + '.json'))
report = {'reader': 'Fresh native ASCII+Keyin CLI export; independent raw attributes and pin/net comparison',
'started_ns': int(stamp), 'preserve_contract': 'Preserve native Replace Part result, not old component Value.',
'cases': [], 'passed': False}
for case in manifest['cases']:
project = Path(case['project'])
row = next(p for p in projects if Path(p['path']).resolve() == project.resolve())
task = next(t for t in tasks if t['task_id'] == row['task_id'])
entry = entries.get(task['task_id'], {})
item = {'id': case['id'], 'kind': case['kind'], 'task_id': task['task_id'], 'task_status': task['status'], 'queue_entry': entry, 'passed': False}
try:
if case['kind'] == 'rule_mode':
item['rule_mode_evidence'] = rule_netlist_evidence(root, task, entry, case, before)
if entry.get('state') in {None, 'pending', 'running'}:
raise AssertionError('Whole-flow task has not reached a terminal acceptance state; do not count it')
expected_negative = case['kind'] in {'unknown_ref', 'stale_old', 'invalid_part', 'corrupt_database'}
if case['kind'] == 'corrupt_database':
assert entry.get('state') == 'failed'
assert hashlib.sha256((project.parent / 'database/icdb.dat').read_bytes()).hexdigest() == case['input_hashes']['database/icdb.dat']
item.update(passed=True, unchanged_corrupt_database=True)
else:
start = time.monotonic()
destination = root / 'independent' / stamp / f'{case["id"]:02d}'
output = export_netlist(capture_project(project, destination / 'capture'), destination / 'export')
assert output['complete'], output.get('reason')
after = output['graph']
selected = {request['refdes']: request for request in case['requests']}
if complex_graph:
selected_uids={uid for uid,c in before['components'].items() if c['refdes'] in selected}
if not expected_negative and case['kind'] != 'already_target':
assert len(selected_uids)==len(selected) and all(not before['components'][uid].get('instance_path') for uid in selected_uids)
preserved=set() if expected_negative or case['kind']=='already_target' else selected_uids
raw_identities=raw_identity_attributes(destination/'export/effective.txt')
assert unselected_raw_identities(original_identities,preserved)==unselected_raw_identities(raw_identities,preserved), 'Unselected raw instance/gate/property multiset changed'
original_raw=selected_root_attributes(original_identities,selected_uids)
raw=selected_root_attributes(raw_identities,selected_uids)
else:
raw = raw_attributes(destination / 'export/effective.txt')
# Physical endpoints and every unselected component must stay identical.
assert before['nets'] == after['nets'], 'Physical net endpoints changed'
assert before['components'].keys() == after['components'].keys(), 'Symbol identity coverage changed'
checks = []
for uid, old in before['components'].items():
new = after['components'][uid]
assert old['pins'] == new['pins'], f'Pins changed: {uid}'
ref = old['refdes']
if expected_negative or case['kind'] == 'already_target' or ref not in selected:
assert old == new, f'Unintended symbol modification: {ref or uid}'
if ref and not complex_graph:
assert original_raw[ref] == raw[ref], f'Unintended raw attribute modification: {ref}'
continue
request = selected[ref]
attrs = raw[ref]
if case['kind'] == 'force_stop' and task['status'] != 'completed':
assert attrs.get('Part Number') in {request['old_part_number'], request['new_part_number']}
checks.append({'refdes': ref, 'observed': attrs, 'requires_verification': True})
continue
assert attrs['Part Number'] == request['new_part_number']
if case['target_cell']:
assert attrs.get('Cell Name') == case['target_cell'], (ref, attrs)
mode = request['value_mode']
native = None
if mode in {'preserve', 'part_label'}:
try:
native = bound_native_observation(root, task['task_id'], uid, request)
except (OSError, KeyError, ValueError) as error:
checks.append({'refdes': ref, 'part_number': attrs['Part Number'], 'value': attrs.get('Value'),
'mode': mode, 'value_check': 'native_' + mode + '_not_independently_proven',
'reason': str(error)})
continue
expected_value = {'custom': request['custom_value'], 'part_number': request['new_part_number'],
'part_label': native['part_label'] if native else None,
'preserve': native['value'] if native else None}[mode]
if mode == 'part_label':
assert attrs.get('Part Label') == native['part_label'] == native['library_row'][5]
expected_present = native['present'] if mode == 'preserve' else True
if request['sync_nc'] and has_nc_marker(original_raw[ref].get('Value', '')) and not has_nc_marker(expected_value or ''):
expected_value = expected_value.rstrip('/') + '/NC'
expected_present = True
assert attrs.get('Value', '') == expected_value, (ref, attrs, expected_value)
check = {'refdes': ref, 'part_number': attrs['Part Number'], 'value': attrs.get('Value'), 'mode': mode,
'value_check': 'fresh_ascii_text_matches_declared_policy'}
if native:
check['native_intermediate'] = native
if 'reopened_present' in native:
assert native['reopened_present'] == expected_present
assert native['reopened_value'] == expected_value
check['presence_check'] = 'count_complete_raw_reopened_COM_observation'
else:
check['presence_check'] = 'not_proven_by_ASCII_absent_empty_serialization'
checks.append(check)
if case['kind'] == 'force_stop':
item.update(force_recovery_evidence(root,expected_stage=case['fault']['stage'],ui_report=force_ui_report))
assert entry.get('state') in {'paused', 'blocked', 'failed','completed'}, entry
assert all(count == 1 for count in saved_uids(root).values()), 'Saved UID replayed after interruption'
item['unknown_outcome_not_claimed_success'] = task['status'] != 'completed'
else:
expected_state = 'failed' if expected_negative else 'skipped' if case['kind'] == 'already_target' else 'completed'
assert entry.get('state') == expected_state, (task, entry, expected_state)
item.update(passed=True, physical_nets_unchanged=True, unselected_unchanged=True,
raw_attribute_checks=checks, seconds=time.monotonic()-start)
if any(check.get('value_check', '').endswith('_not_independently_proven') for check in checks):
item.update(passed=False, structural_and_part_number_passed=True,
unverified_reason='Native preserve intermediate was not independently observed; old Value is not its contract.')
except Exception:
item['failure'] = traceback.format_exc()
report['cases'].append(item)
write(root / 'independent-report.json', report)
print(json.dumps(item, ensure_ascii=False), flush=True)
assert fingerprint(Path(manifest['source']).parent) == manifest['source_hashes']
if manifest.get('library'):
assert hashlib.sha256(Path(manifest['library']['path']).read_bytes()).hexdigest() == manifest['library']['sha256']
if manifest.get('origin') == 'user':
for name in ('source_workbook', 'working_workbook'):
assert hashlib.sha256(Path(manifest[name]['path']).read_bytes()).hexdigest() == manifest[name]['sha256']
if manifest.get('expected_no_cad'):
from accept_random_packaged_ui import verify_no_cad_fixture
report['no_op_validation'] = verify_no_cad_fixture(root, manifest)
resource_errors = []
for path in (root / 'data/batch-resources').glob('*/session.json'):
record = json.loads(path.read_text(encoding='utf-8'))
cleanup = {} if record.get('cleanup') is None else record['cleanup']
if record.get('state') != 'released' or not isinstance(cleanup, dict) or cleanup.get('errors'):
resource_errors.append({'path': str(path), 'record': record})
for path in (root / 'data/tasks').glob('*/quick-run-history/native-inventory-*/native-inventory.json'):
record = json.loads(path.read_text(encoding='utf-8'))
if record.get('backend') != 'native_cli' or record.get('state') == 'fallback':
resource_errors.append({'path': str(path), 'reason': 'Inventory fallback or wrong backend'})
if record.get('cleanup', {}).get('errors') or Path(record['workspace']).exists():
resource_errors.append({'path': str(path), 'reason': 'Inventory resources not released'})
observation_path = root / 'desktop-observation.json'
if observation_path.exists():
observation = json.loads(observation_path.read_text(encoding='utf-8'))
if (observation['observer_errors'] or observation['owned_cad_became_foreground']
or observation['owned_cad_on_input_desktop']
or not all(observation['cad_exit_confirmed'].values())
or not all(observation['desktop_absent'].values())):
resource_errors.append({'path': str(observation_path), 'reason': 'Independent desktop/process observation failed'})
report.update(passed=all(c['passed'] for c in report['cases']) and not resource_errors,
source_unchanged=True, library_unchanged=True, resource_errors=resource_errors)
if queue_id is not None:
report['verified_queue_id'] = queue_id
write(root / 'independent-report.json', report)
return report
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--root', type=Path, required=True)
parser.add_argument('--source', type=Path)
parser.add_argument('--graph', type=Path)
parser.add_argument('--target-part', action='append')
parser.add_argument('--part-prefix', action='append', default=[])
parser.add_argument('--cell', action='append', default=[])
parser.add_argument('--seed', type=int)
parser.add_argument('--count', type=int, default=20)
parser.add_argument('--supplemental', choices=('valid_cell', 'force_stop', 'rule_mode','pon_root'), default='')
parser.add_argument('--run-ui', action='store_true')
parser.add_argument('--ui-prepare-only', action='store_true')
parser.add_argument('--verify', action='store_true')
args = parser.parse_args()
if args.verify:
verify(args.root.resolve(strict=True))
elif args.run_ui:
root = args.root.resolve(strict=True)
if args.ui_prepare_only:
run_ui(root, prepare_only=True)
else:
from private_desktop_observer import PrivateDesktopObserver
with PrivateDesktopObserver(root):
run_ui(root)
else:
if not args.source or not args.graph or not args.target_part:
parser.error('Prepare needs --source, --graph and --target-part')
manifest = prepare(args.root, args.source, json.loads(args.graph.read_text(encoding='utf-8')),
args.target_part, seed=args.seed, count=args.count, cells=args.cell, supplemental=args.supplemental,
part_prefixes=args.part_prefix or ('4100',))
workbook(args.root.resolve(), manifest)
print(json.dumps({'root': str(args.root.resolve()), 'seed': manifest['seed'], 'count': len(manifest['cases'])}))
if __name__ == '__main__':
main()