"""Watchdog grace follows business progress, never mere worker liveness.""" import pytest from dx_productivity.trial_task_center.worker_monitor import ProgressWatch def frame(sequence, *, stage='save_and_close', done=0, safe=0): return f'DX_PROGRESS\ttoken\tjob\t{sequence}\t{stage}\t{done}\t100\t{safe}\n'.encode() def native_frame(sequence, *, done=0, parent=1, safe=0): return f'DX_NATIVE_PROGRESS\ttoken\tjob\t{parent}\tjob\t{sequence}\t{done}\t100\t{safe}\n'.encode() @pytest.mark.parametrize('heartbeat', [False, True]) def test_stalled_unsafe_work_reaches_failure_cleanup_despite_liveness(heartbeat): watch = ProgressWatch('token', 'job', 10, 0) assert watch.accept(frame(1), 0) assert watch.action(10) == 'request_stop' assert watch.action(20) == 'await_safe_completion' for sequence, now in enumerate((30, 49, 51, 60, 120, 1000), 2): if heartbeat: assert watch.accept(frame(sequence), now) assert watch.action(now) == (None if now < 50 else 'failure_cleanup') assert watch.snapshot(now=now)['waiting_for_safe_completion'] assert watch.last_business_progress == 0 assert watch.last_progress == (1000 if heartbeat else 0) @pytest.mark.parametrize('source', ['stdout', 'native']) def test_business_progress_restarts_grace_without_revoking_requested_pause(source): watch = ProgressWatch('token', 'job', 10, 0) stage = 'native_snapshot_read' if source == 'native' else 'save_and_close' assert watch.accept(frame(1, stage=stage), 0) assert watch.action(10) == 'request_stop' assert watch.action(20) == 'await_safe_completion' if source == 'native': assert watch.accept_native(native_frame(1, done=1), 49) else: assert watch.accept(frame(2, stage=stage, done=1), 49) assert not watch.snapshot(now=49)['waiting_for_safe_completion'] assert watch.stop_requested assert watch.action(50) is None assert watch.action(68.999) is None assert watch.action(69) == 'await_safe_completion' assert watch.action(98.999) is None assert watch.action(99) == 'failure_cleanup' def test_native_heartbeats_cannot_restart_stalled_business_grace(): watch = ProgressWatch('token', 'job', 10, 0) assert watch.accept(frame(1, stage='native_snapshot_read'), 0) assert watch.action(10) == 'request_stop' assert watch.action(20) == 'await_safe_completion' for sequence, now in enumerate((30, 49, 51), 1): assert watch.accept_native(native_frame(sequence), now) assert watch.action(now) == (None if now < 50 else 'failure_cleanup') @pytest.mark.parametrize('line', [ native_frame(1, done=1, parent=2), native_frame(1, done=1, safe=1), native_frame(1, done=1).replace(b'token', b'other'), native_frame(1, done=1).replace(b'\tjob\t1\tjob\t', b'\tjob\t1\tother\t'), native_frame(1, done=1)[:-1], ]) def test_unbound_native_progress_cannot_clear_wait_or_grant_safety(line): watch = ProgressWatch('token', 'job', 10, 0) assert watch.accept(frame(1, stage='native_snapshot_read'), 0) assert watch.action(10) == 'request_stop' assert watch.action(20) == 'await_safe_completion' assert not watch.accept_native(line, 49) assert not watch.safe_stop assert watch.action(51) == 'failure_cleanup'