fix(execution): track native read progress and retry transient library redraws

This commit is contained in:
阿白叔 committed 2026-10-19 13:45:39 +08:00
1 parent e74ec5bcbd
commit 911afdf1cc
7 files changed
+198 -8

No files matched your search

@@ -132,7 +132,17 @@ def wait_for_library_rows(callback, target, timeout=30, stable_for=0.45, poll_in
stable_since = None
latest = []
while time.monotonic() < deadline:
latest = callback()
try:
latest = callback()
except KeyError as error:
# During DxDataBook redraw, pywinauto can see a destroyed row
# whose UIA control_type is None. Reacquire the read-only list;
# never carry its earlier stability interval across that gap.
if error.args != (None,):
raise
previous = stable_since = None
time.sleep(poll_interval)
continue
signature = tuple(tuple(str(value) for value in columns) for _, columns in latest)
now = time.monotonic()
if signature != previous:
@@ -8,6 +8,7 @@ STAGES = {
'find_components': '定位目标位号', 'index_pins': '建立引脚索引',
'snapshot_attributes': '记录原始属性', 'capture': '采集替换前快照',
'capture_untouched': '记录未选中器件状态', 'verify_untouched': '核验未选中器件',
'native_snapshot_read': '采集并核验器件完整状态',
'before_native': '准备原生替换', 'native_replace': '执行原生替换',
'restore_preflight': '检查属性恢复条件',
'before_restore_transaction': '准备恢复属性',
@@ -28,6 +28,8 @@ class ProgressWatch:
self.idle_timeout = idle_timeout
self.last_progress = started
self.sequence = 0
self.native_sequence = 0
self.native_progress_frames = 0
self.stop_requested = False
self.stage = 'starting'
self.done = 0
@@ -55,16 +57,45 @@ class ProgressWatch:
if current_job != self.current_job_id:
self.current_job_id = current_job
self.stage_seconds = {}
self.native_progress_frames = 0
self.started = self.stage_started = now
self.stage = fields[4]
if fields[4] != self.stage:
self.stage_seconds[self.stage] = self.stage_seconds.get(self.stage, 0.0) + max(0.0, now - self.stage_started)
self.stage_started = now
self.sequence = sequence
self.native_sequence = 0
self.last_progress = now
self.stage, self.done, self.total, self.safe_stop = fields[4], done, total, bool(safe)
return True
def accept_native(self, line: bytes, now: float) -> bool:
"""Accept only the sidecar of the currently authorized blocking read.
The parent stdout sequence identifies one native call. A sidecar can
update liveness and counts; it cannot advance stdout or grant safety.
"""
if self.stage != 'native_snapshot_read' or not line.endswith(b'\n'):
return False
try:
fields = line.decode('ascii').strip().split('\t')
if len(fields) != 9 or fields[:3] != ['DX_NATIVE_PROGRESS', self.token, self.job_id]:
return False
if int(fields[3]) != self.sequence or fields[4] != self.current_job_id:
return False
sequence, done, total, safe = map(int, fields[5:])
if not self.native_sequence < sequence <= 2**31 - 1:
return False
if done < 0 or total < 0 or (total and done > total) or safe != int(self.safe_stop):
return False
except (ValueError, UnicodeError):
return False
self.native_sequence = sequence
self.native_progress_frames += 1
self.last_progress = now
self.done, self.total = done, total
return True
def action(self, now: float) -> str | None:
idle = now - self.last_progress
if not self.stop_requested and idle >= self.idle_timeout:
@@ -80,6 +111,7 @@ class ProgressWatch:
stages[self.stage] = stages.get(self.stage, 0.0) + max(0.0, now - self.stage_started)
return {name: getattr(self, name) for name in ('token', 'sequence', 'stage', 'done', 'total', 'safe_stop', 'stop_requested')} | {
'job_id': self.current_job_id,
'native_progress_frames': self.native_progress_frames,
'elapsed_seconds': max(0.0, now - self.started), 'stage_seconds': stages}
@@ -165,6 +197,7 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
ready = root / f'monitor-{token}.ready'
stop = root / f'monitor-{token}.stop'
progress_path = root / 'worker-progress.json'
native_progress_path = root / f'monitor-{token}.native-progress'
root.mkdir(parents=True, exist_ok=True)
watch = ProgressWatch(token, job_id, idle_timeout, time.monotonic())
owned = _OwnedWorkers(token, {command[0], command[4]})
@@ -227,6 +260,15 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
receive(*messages.get_nowait())
except queue.Empty:
pass
if watch.stage == 'native_snapshot_read':
line = b''
try:
with native_progress_path.open('rb') as native:
line = native.read(2048)
except OSError:
pass # Missing/temporarily locked telemetry cannot prove progress.
if watch.accept_native(line, time.monotonic()):
_publish_progress(progress_path, watch.snapshot())
if process.poll() is not None:
break
action = watch.action(time.monotonic())