Files
dxdesigner-part-replacement/tools/accept_dsh_operations.py
T

257 lines
17 KiB
Python

"""Real Qt/CDP operations acceptance across two clean application processes."""
from __future__ import annotations
import argparse
from datetime import datetime, timedelta, timezone
import hashlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import threading
import time
import traceback
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO / 'src'))
def write(path, value):
path.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding='utf-8')
def seed(service, output, data):
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow
paths, hashes, ids = {}, {}, {}
for name in ('Board-A', 'Board-B', 'Board-C'):
project = output / 'projects' / name / 'board.prj'
project.parent.mkdir(parents=True, exist_ok=True)
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n', encoding='utf8')
paths[name] = project
hashes[str(project)] = hashlib.sha256(project.read_bytes()).hexdigest()
task = service.create_project_tasks(project.parent, selected_paths=(project,))[0]
service.update_task(task.task_id, name=name, note='Isolated operations acceptance fixture')
ids[name] = task.task_id
service.add_rule(ids['Board-A'], 'OLD123', 'NEW456')
service.add_rule(ids['Board-A'], 'OLD222', 'NEW222')
service.add_rule(ids['Board-C'], 'SCHED_OLD', 'SCHED_NEW')
targets = tuple(ComponentTarget(paths['Board-B'], 'MAIN', '1', f'accept-uid-{i}', f'R{i}', 'EXACT_OLD', f'{i}k') for i in (1, 2))
service._store.record_target_inventory(ids['Board-B'], targets)
service._store.replace_target_plan(ids['Board-B'], (
TargetPlanRow(targets[0], True, 'EXACT_NEW', target_cell='CELL-X'),
TargetPlanRow(targets[1], True, 'EXACT_NEW', True, 'CUSTOM-2', False)))
for identity, verified in (('a' * 24, True), ('b' * 24, False)):
directory = data / 'dsh' / 'incidents' / identity
directory.mkdir(parents=True)
write(directory / 'incident.json', {'incident_id': identity, 'task_id': ids['Board-A'],
'issue_code': 'acceptance_verified' if verified else 'acceptance_suggestion',
'message': 'Isolated persisted verification fixture' if verified else 'Evidence-less suggestion fixture',
'status': 'resolved' if verified else 'analyzed',
'analyses': [{'phase': 'diagnosis', 'reply': 'Controlled fixture analysis, no execution authority.'}]})
if verified:
write(directory / 'snapshot.json', {'fixture': True, 'task_id': ids['Board-A']})
write(directory / 'verification.json', {'queue_complete': True, 'failed_items': 0,
'interrupted': False, 'paused': False, 'saved_items': 0, 'no_match_items': 1})
manifest = {'tasks': ids, 'sourceHashes': hashes, 'data': str(data)}
write(output / 'manifest.json', manifest)
return manifest
def phase(args):
output, data = args.output.resolve(), args.data.resolve()
os.environ.pop('QT_QPA_PLATFORM', None)
os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = f'127.0.0.1:{args.port}'
from PySide6.QtCore import QTimer, Qt, QPoint
from PySide6.QtTest import QTest
from PySide6.QtWidgets import QApplication, QDialogButtonBox
from dx_productivity.trial_task_center import application
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
from dx_productivity.trial_task_center.ui.dsh_task_lifecycle import DshLifecycleDialog
from dx_productivity.trial_task_center.ui.dsh_operations import DshScheduleDialog
from dx_productivity.dsh.run_journal import RunJournal
report = {'phase': args.phase, 'executor': 'controlled_no_match', 'realCadStarted': False, 'checks': [], 'dialogs': []}
state = {'child': None, 'stage': 'opening', 'deadline': time.monotonic() + 180, 'selection': None, 'scheduleDialogs': 0}
selection_path = output / 'native-selection.json'
if selection_path.is_file():
prior_nonce = json.loads(selection_path.read_text(encoding='utf8'))['nonce']
state.update(selection=prior_nonce, selection_ready=prior_nonce)
release = threading.Event()
calls_path = output / 'controlled-calls.jsonl'
def build(service):
def worker(command, timeout):
queue, job = Path(command[3]), command[-1]
with calls_path.open('a', encoding='utf8') as stream:
stream.write(json.dumps({'phase': args.phase, 'job': job, 'queue': str(queue)}) + '\n')
if args.phase == 'initial' and not release.wait(90):
raise TimeoutError('The real running-tab safe pause was not received')
with (queue / 'status.tsv').open('a', encoding='utf8') as stream:
stream.write(f'{job}\tno_match\tbatch_closed\tNo matching components\n')
return subprocess.CompletedProcess(command, 0, '', '')
service._supervised = False
service._quick_run_preparer = QuickRunPreparer(data, process_runner=worker,
cscript_resolver=lambda: Path('cscript.exe'), sheet_discoverer=lambda _path: (('MAIN', '1'),))
manifest = seed(service, output, data) if args.phase == 'initial' else json.loads((output / 'manifest.json').read_text(encoding='utf8'))
class AcceptanceWindow(TrialTaskCenterWindow):
def closeEvent(self, event):
report.setdefault('closeEvents', []).append({'spontaneous': event.spontaneous(),
'stage': state['stage'], 'report_status': report.get('status'),
'pending': self._close_pending, 'worker_count': len(self._workers)})
super().closeEvent(event)
window = AcceptanceWindow(service); window.resize(1500, 950)
window.setWindowTitle('DSH 自动验收(进行中) · ' + args.phase)
window.archive_on_close_check.setChecked(False)
def fail(error):
release.set()
report.update(status='failed', error=str(error), traceback=traceback.format_exc())
child = state['child']
if child and child.poll() is None:
child.terminate()
timer.stop(); window.close()
def select(task_id):
QTest.mouseClick(window.task_center_navigation, Qt.MouseButton.LeftButton)
for index in range(window.task_list.list.count()):
item = window.task_list.list.item(index)
if item.data(Qt.ItemDataRole.UserRole) == task_id:
window.task_list.list.scrollToItem(item)
QTest.mouseClick(window.task_list.list.viewport(), Qt.MouseButton.LeftButton,
pos=window.task_list.list.visualItemRect(item).center())
return
raise AssertionError('Requested fixture task is absent from native list')
def tick():
try:
if time.monotonic() > state['deadline']:
view = window._dsh_view
raise TimeoutError(f"Operations acceptance {state['stage']} timed out; runtime_error={getattr(view, 'last_error', None)}; status={view.status.text() if view else 'not-created'}")
if window._batch_stop and window._batch_stop.is_set(): release.set()
modal = QApplication.activeModalWidget()
if isinstance(modal, DshLifecycleDialog) and not modal.property('acceptanceClicked'):
modal.setProperty('acceptanceClicked', True)
text = modal.preview.toPlainText()
cancel = False
if args.phase == 'initial' and modal.windowTitle().endswith('执行所选任务'):
state['executionConfirmations'] = state.get('executionConfirmations', 0) + 1
# A safe pause is a user decision. Exercise cancellation
# if a model proposes continuation before a new request.
cancel = state['executionConfirmations'] > 1
if isinstance(modal, DshScheduleDialog):
state['scheduleDialogs'] += 1
cancel = state['scheduleDialogs'] == 1
assert 'SCHED_OLD' in text and 'SCHED_NEW' in text, 'Future-run confirmation omitted engineering impact'
if not cancel:
modal.time_edit.selectAll(); QTest.keyClicks(modal.time_edit, (datetime.now(timezone.utc)-timedelta(seconds=2)).isoformat(timespec='seconds'))
if not modal.auto_start.isChecked():
QTest.mouseClick(modal.auto_start, Qt.MouseButton.LeftButton,
pos=QPoint(8, modal.auto_start.height() // 2))
assert modal.auto_start.isChecked(), 'Actual checkbox click must grant future-run authorization'
report['dialogs'].append({'title': modal.windowTitle(), 'preview': text, 'action': 'cancel' if cancel else 'confirm'})
modal.grab().save(str(output / f"{args.phase}-native-{len(report['dialogs'])}.png"))
QTest.mouseClick(modal.buttons.button(QDialogButtonBox.StandardButton.Cancel if cancel else QDialogButtonBox.StandardButton.Ok), Qt.MouseButton.LeftButton)
command_path = output / 'native-selection.json'
if state['stage'] == 'web' and command_path.exists():
command = json.loads(command_path.read_text(encoding='utf8'))
if command['nonce'] != state['selection'] and not window._workers:
select(command['task_id']); state['selection'] = command['nonce']
if state['selection'] == command['nonce'] and state.get('selection_ready') != command['nonce'] and window._context and window._context.task.task_id == command['task_id'] and not window._loading:
QTest.mouseClick(window.dsh_navigation, Qt.MouseButton.LeftButton)
write(output / 'native-selection-done.json', command)
state['selection_ready'] = command['nonce']
if state['stage'] == 'opening' and not window._task_query.busy and window.task_list.list.count() == 3:
select(manifest['tasks']['Board-A']); state['stage'] = 'selected'
elif state['stage'] == 'selected' and window._context and not window._loading:
QTest.mouseClick(window.dsh_navigation, Qt.MouseButton.LeftButton); state['stage'] = 'runtime'
elif state['stage'] == 'runtime' and window._dsh_view._started:
state['child'] = subprocess.Popen(['node', str(REPO / 'tools/verify_dsh_operations_ui.mjs'), str(output), str(args.port), args.phase],
cwd=REPO, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0)
state.update(stage='web', deadline=time.monotonic()+360)
elif state['stage'] == 'web' and state['child'].poll() is not None:
stdout, stderr = state['child'].communicate()
if state['child'].returncode:
raise RuntimeError(stderr.decode('utf8', 'replace'))
report['web'] = json.loads((output / f'{args.phase}-web.json').read_text(encoding='utf8'))
rows = RunJournal(data).list_runs()
original = next(row for row in rows if row['task_ids'] == [manifest['tasks']['Board-A']])
assert original['status'] == ('paused' if args.phase == 'initial' else 'completed'), original
if args.phase == 'initial': write(output / 'paused-run.json', original)
else:
prior = json.loads((output / 'paused-run.json').read_text(encoding='utf8'))
assert original['run_id'] == prior['run_id'] and original['queue_id'] == prior['queue_id']
assert original['result']['tasks'][0]['no_match_items'] == 2
assert all(hashlib.sha256(Path(path).read_bytes()).hexdigest() == digest for path, digest in manifest['sourceHashes'].items())
report.update(status='passed', runs=rows)
window.grab().save(str(output / f'{args.phase}-desktop.png'))
timer.stop(); window.close()
except Exception as error: fail(error)
timer = QTimer(window); timer.setInterval(150); timer.timeout.connect(tick); timer.start()
return window
application._load_window = lambda: build
code = application.main(['--data-root', str(data)])
if 'status' not in report:
report.update(status='failed', error='application_closed_before_acceptance_completed', stage=state['stage'])
text = (data / 'logs/application.log').read_text(encoding='utf8')
report['cleanShutdown'] = 'application_clean_shutdown exit_code=0' in text
report['unexpectedUiErrors'] = 'uncaught_main_thread' in text or 'operation=ui message=' in text
audit = data / 'dsh/harness-home/task-tool-audit.jsonl'
report['modelToolCalls'] = [json.loads(line) for line in audit.read_text(encoding='utf8').splitlines()] if audit.is_file() else []
write(output / f'{args.phase}-desktop.json', report)
print(json.dumps({'phase': args.phase, 'status': report.get('status'), 'error': report.get('error'), 'cleanShutdown': report['cleanShutdown']}, ensure_ascii=True))
return int(bool(code or report.get('status') != 'passed' or not report['cleanShutdown'] or report['unexpectedUiErrors']))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--output', type=Path, required=True)
parser.add_argument('--provider-config', type=Path)
parser.add_argument('--port', type=int, default=9237)
parser.add_argument('--phase', choices=('initial', 'resume'))
parser.add_argument('--data', type=Path)
parser.add_argument('--ticket-incident', type=Path, help='Existing incident directory to copy for read-only ticket synchronization')
args = parser.parse_args()
if args.phase: return phase(args)
output = args.output.resolve() / time.strftime('%Y%m%d-%H%M%S')
output.mkdir(parents=True)
data = output / 'data'; (data / 'dsh').mkdir(parents=True)
if not args.provider_config: parser.error('--provider-config is required for the real provider acceptance')
for name in ('provider.json', 'provider-key.dpapi'):
shutil.copy2(args.provider_config / name, data / 'dsh' / name)
for name in ('issue-config.json', 'issue-key.dpapi'):
if (args.provider_config / name).is_file(): shutil.copy2(args.provider_config / name, data / 'dsh' / name)
# Copy one existing binding only; no remote issue or comment is created.
for source in sorted((args.provider_config / 'incidents').glob('*/incident.json')):
value = json.loads(source.read_text(encoding='utf8'))
if value.get('ticket', {}).get('number') and len(source.parent.name) == 24:
destination = data / 'dsh/incidents' / source.parent.name
destination.mkdir(parents=True); shutil.copy2(source, destination / 'incident.json'); break
if args.ticket_incident:
source = args.ticket_incident / 'incident.json'
value = json.loads(source.read_text(encoding='utf8'))
if not value.get('ticket', {}).get('number') or value.get('incident_id') != args.ticket_incident.name:
parser.error('--ticket-incident must contain an existing bound ticket')
destination = data / 'dsh/incidents' / args.ticket_incident.name
destination.mkdir(parents=True, exist_ok=True); shutil.copy2(source, destination / 'incident.json')
for item in ('initial', 'resume'):
result = subprocess.run([sys.executable, '-X', 'utf8', str(Path(__file__)), '--output', str(output), '--data', str(data), '--port', str(args.port), '--phase', item], cwd=REPO)
if result.returncode:
print(json.dumps({'status': 'failed', 'phase': item, 'output': str(output)})); return result.returncode
calls = [json.loads(line) for line in (output / 'controlled-calls.jsonl').read_text().splitlines()]
assert len(calls) == 3 and len({(row['queue'], row['job']) for row in calls}) == 3, calls
audit = data / 'dsh/harness-home/task-tool-audit.jsonl'
phases = [json.loads((output / f'{name}-desktop.json').read_text(encoding='utf8')) for name in ('initial', 'resume')]
write(output / 'report.json', {'status': 'passed', 'processes': 2, 'calls': calls,
'realCadStarted': False, 'remoteIssuesCreated': False, 'sourceFixtureHashesUnchanged': True,
'cleanShutdowns': [value['cleanShutdown'] for value in phases],
'modelToolCalls': [json.loads(line) for line in audit.read_text(encoding='utf8').splitlines()] if audit.is_file() else [],
'data': str(data)})
print(json.dumps({'status': 'passed', 'output': str(output), 'controlledCalls': len(calls)}))
return 0
if __name__ == '__main__': raise SystemExit(main())