Files
cbb-refresh-verify-795/src/cbb_refresh_verify/integration.py
T
阿白叔 11a2b2e46e Import CBB refresh verify r4 stability baseline
Preserve tested Verify source, authoritative READY checks, bounded bridge retries, diagnostics and failure-only reporting. Include 336-test suite and packaging assets; exclude live Mentor samples, field logs, backups and distributions.
2026-09-29 15:05:20 +08:00

122 lines
5.2 KiB
Python

"""State-driven Project Integration refresh; no UI or COM creation here."""
import time
from .mentor_adapter import ActivityDeadline
from .defaults import DEFAULT_TIMEOUT_SECONDS
ACTIONS = (
('ForwardAnnotation', 'ForwardAnnotate'),
('SynchCES', 'SynchCES'),
('LoadCES', 'LoadCES'),
('BackAnnotation', 'BackAnnotate'),
)
class IntegrationError(RuntimeError):
pass
def value(obj, name):
result = getattr(obj, name)
return result() if callable(result) else result
def statuses(pi):
return {name: int(value(pi, name + 'Status')) for name, _ in ACTIONS}
def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, timeout_seconds=DEFAULT_TIMEOUT_SECONDS,
error_reader=None, sleep=time.sleep, clock=None):
clock = clock or time.monotonic
def check_error():
if error_reader is None:
return
detail = error_reader()
if detail:
emit(stage='integration_error', detail=detail)
raise IntegrationError(f'Project Integration error: {detail}')
deadline = ActivityDeadline(timeout_seconds, clock=clock) if timeout_seconds is not None else None
in_flight = {}
executed = set()
action_history = []
green_entry = None
previous_state = None
while True:
check_error()
state = statuses(pi)
if state != previous_state:
emit(stage='integration_status', statuses=state)
if green_entry is None:
green_entry = all(status == 2 for status in state.values())
if previous_state is not None and state != previous_state and deadline is not None:
deadline.touch()
previous_state = state
for name, status in state.items():
# NoCES=3 is recorded as unsupported, never claimed green.
if status not in (1, 2):
raise IntegrationError(f'{name}: unsupported status {status}')
for name in tuple(in_flight):
if state[name] == 2:
started, call_elapsed = in_flight.pop(name)
emit(stage='integration_action_complete', action=dict(ACTIONS)[name], status=2,
elapsed_seconds=round(clock() - started, 6),
call_elapsed_seconds=round(call_elapsed, 6))
# When all four lamps start green, mirror the manual refresh: one
# ForwardAnnotate, then only repair statuses that actually become Required.
# Non-green entry keeps the previously validated four-action route.
refreshed = ('ForwardAnnotation' in executed if green_entry
else len(executed) == len(ACTIONS))
if refreshed and not in_flight and all(status == 2 for status in state.values()):
emit(stage='integration_complete', statuses=state, actions=action_history,
policy='green_first_only' if green_entry else 'required_full_sequence')
return state
if deadline is not None and deadline.expired():
raise IntegrationError('Project Integration timeout')
if in_flight:
sleep(.2)
continue
action_started = False
for name, method in ACTIONS:
if state[name] == 2 and (name in executed or
(green_entry and name != 'ForwardAnnotation')):
continue
allowed = bool(value(pi, 'Is' + name + 'Allowed'))
# A green item still has to receive one Verify refresh attempt.
# Mentor may report the capability as false for that item; this
# flag is a gate only for a genuinely Required (status=1) item.
if state[name] != 2 and not allowed:
emit(stage='integration_action_unavailable', action=method,
initial_status=state[name], reason='mentor_not_allowed')
raise IntegrationError(f'{name}: required but not allowed')
emit(stage='integration_action', action=method, initial_status=state[name],
reason='required_verification' if name not in executed else 'resynchronize')
started = clock()
try:
action_result = value(pi, method)
except Exception as error:
emit(stage='integration_error', action=method, detail=str(error),
elapsed_seconds=round(clock() - started, 6))
raise IntegrationError(f'{method}: {error}') from error
if not action_result:
raise IntegrationError(f'{method} returned false')
in_flight[name] = (started, clock() - started)
executed.add(name)
action_history.append(method)
action_started = True
if deadline is not None:
deadline.touch()
until = clock() + settle_seconds
previous = state
while statuses(pi) == state and clock() < until:
check_error()
sleep(.2)
if statuses(pi) != previous:
if deadline is not None:
deadline.touch()
break # Every operation may change other statuses; always reread.
if not action_started:
sleep(.2)