56 lines
2.7 KiB
Python
56 lines
2.7 KiB
Python
"""Facts from one helper invocation; process exit is not a Save result."""
|
||
from collections.abc import Sequence
|
||
|
||
|
||
def classify_save_state(markers: Sequence[str], *, helper_exited: bool | None,
|
||
native_released: bool | None, forced: bool = False) -> dict:
|
||
names = {line.strip().split('|', 1)[0] for line in markers}
|
||
save_results = names & {'SAVE_OK', 'SAVE_REJECTED', 'SAVE_RESULT_UNKNOWN',
|
||
'CANCELLED_BEFORE_SAVE'}
|
||
conflicting_save = (len(save_results) > 1 or
|
||
('CANCELLED_BEFORE_SAVE' in names and 'SAVE_START' in names))
|
||
conflicting_close = {'CLOSE_OK', 'CLOSE_FAILED'} <= names
|
||
save_state = 'UNKNOWN'
|
||
if not conflicting_save:
|
||
for marker, state in (('SAVE_OK', 'COMMITTED'), ('SAVE_REJECTED', 'REJECTED'),
|
||
('CANCELLED_BEFORE_SAVE', 'NOT_STARTED')):
|
||
if marker in names:
|
||
save_state = state
|
||
close_state = 'UNKNOWN'
|
||
if not conflicting_close:
|
||
if 'CLOSE_OK' in names:
|
||
close_state = 'CLOSED'
|
||
elif 'CLOSE_FAILED' in names:
|
||
close_state = 'FAILED'
|
||
resource_state = ('RELEASED' if native_released is True and helper_exited is True else
|
||
'RETAINED' if native_released is False or helper_exited is False else
|
||
'UNKNOWN')
|
||
reasons = []
|
||
if conflicting_save or conflicting_close:
|
||
reasons.append('本轮返回标记矛盾')
|
||
if save_state == 'UNKNOWN':
|
||
reasons.append('保存返回结果无法确认')
|
||
if close_state == 'FAILED':
|
||
reasons.append('原生关闭失败')
|
||
if 'POST_SAVE_FAILED' in names:
|
||
reasons.append('保存后核对失败')
|
||
if forced:
|
||
reasons.append('会话被中断,退出不代表保存完成')
|
||
interrupted = (forced or conflicting_save or conflicting_close or
|
||
'SAVE_RESULT_UNKNOWN' in names or
|
||
(helper_exited is True and 'SAVE_START' in names and save_state == 'UNKNOWN'))
|
||
return dict(save_state=save_state, close_state=close_state,
|
||
helper_state='EXITED' if helper_exited is True else
|
||
'RUNNING' if helper_exited is False else 'UNKNOWN',
|
||
resource_state=resource_state, interrupted=interrupted,
|
||
detail=';'.join(reasons))
|
||
|
||
|
||
def stop_disposition(*, call_started: bool | None, call_returned: bool | None,
|
||
identity_confirmed: bool) -> str:
|
||
if not identity_confirmed or call_started is None:
|
||
return 'OBSERVE_UNCONFIRMED'
|
||
if call_started is False:
|
||
return 'CANCEL_BEFORE_CALL' if call_returned is False else 'OBSERVE_UNCONFIRMED'
|
||
return 'CLEANUP_RETURNED' if call_returned is True else 'WAIT_FOR_RETURN'
|