"""Exercise real Harness/API diagnosis with an explicit controlled repair executor.""" from __future__ import annotations import json import os from pathlib import Path from queue import Queue import runpy import subprocess import sys from threading import Thread from uuid import uuid4 REPO = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO / 'src')) def main(): from dx_productivity.dsh.incidents import IncidentRunner from dx_productivity.dsh.native_runtime import launch_environment from dx_productivity.dsh.provider_config import load_provider from dx_productivity.trial_task_center.application import resolve_data_root fixture = runpy.run_path(str(REPO / 'tests/dsh/test_incidents.py')) root = REPO / 'build' / ('dsh-auto-repair-' + uuid4().hex[:8]) (root / 'dsh').mkdir(parents=True) context = root / 'dsh/task-context.json' context.write_text(json.dumps({'task_id': 'different-task', 'revision': 'different'}), encoding='utf8') env = launch_environment(root / 'dsh') | load_provider(resolve_data_root(None) / 'dsh') process = subprocess.Popen(['node', str(REPO / 'integration/dsh/start.mjs')], cwd=REPO, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, encoding='utf8', creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0) events = Queue() def read(): for line in process.stdout: if line.startswith('DX_DSH_READY '): events.put({'status': 'ready'}) elif line.startswith('DX_DSH_DIAGNOSIS '): events.put(json.loads(line[len('DX_DSH_DIAGNOSIS '):])) Thread(target=read, daemon=True).start() try: assert events.get(timeout=65)['status'] == 'ready' def analyze(incident_id, snapshot, evidence, phase): request_id = uuid4().hex request = {'type': 'diagnose', 'request_id': request_id, 'incident_id': incident_id, 'snapshot': snapshot, 'evidence': evidence, 'phase': phase} process.stdin.write(json.dumps(request, ensure_ascii=False) + '\n') process.stdin.flush() while True: value = events.get(timeout=140) if value.get('request_id') != request_id or value.get('status') == 'started': continue assert value['status'] == 'completed', value.get('error') return value def no_ticket(*args): raise AssertionError('A verified repair must not submit an unresolved issue') service = fixture['Service']() runner = IncidentRunner(root, service, analyze, no_ticket) row = runner.run('controlled-retry-task', 'worker_error', '受控执行器验收:worker 在第一项写入前发生瞬时失败。当前阶段 retry,没有未核验标记;已授权执行,仅需重试一次。', execution_authorized=True, run_id='controlled-run') assert row['status'] == 'resolved', row['status'] assert service.calls == ['prepare', 'execute'] duplicate = runner.run('controlled-retry-task', 'worker_error', 'duplicate', execution_authorized=True, run_id='controlled-run') assert duplicate['incident_id'] == row['incident_id'] and service.calls == ['prepare', 'execute'] report = {'status': 'passed', 'model': 'real configured API through native Harness', 'executor': 'controlled test fixture; no CAD writes', 'incident': row, 'actualCalls': service.calls, 'duplicateDidNotReplay': True} (root / 'report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf8') print(json.dumps({'status': 'passed', 'report': str(root / 'report.json'), 'calls': service.calls, 'verification': row['verification'], 'duplicateDidNotReplay': True}, ensure_ascii=False)) finally: process.stdin.close() try: process.wait(timeout=8) except subprocess.TimeoutExpired: process.kill(); process.wait(timeout=8) if __name__ == '__main__': main()