diff --git a/src/studyflow/platforms/ucampus/readonly_monitor.py b/src/studyflow/platforms/ucampus/readonly_monitor.py index b2864e35..f91ac4e7 100644 --- a/src/studyflow/platforms/ucampus/readonly_monitor.py +++ b/src/studyflow/platforms/ucampus/readonly_monitor.py @@ -168,7 +168,11 @@ def tick(*, clock=None, reader_factory=UCampusProgressReader) -> dict[str, Any]: if (type(previous_attempt) not in (int, float) or previous_attempt < 0 or not float(previous_attempt) < float("inf")): previous_attempt = 0 - if now - float(previous_attempt) < _MIN_INTERVAL_SECONDS: + elapsed = now - float(previous_attempt) + # The persisted timestamp uses wall time across restarts. If Windows + # corrects the clock backwards, do not suppress progress checks until + # the old wall-clock value eventually comes around again. + if 0 <= elapsed < _MIN_INTERVAL_SECONDS: return public saved["last_attempt"] = now _write(saved) # claim before GET: concurrent ticks do not duplicate it diff --git a/tests/test_ucampus_readonly_monitor.py b/tests/test_ucampus_readonly_monitor.py index 185e6d7a..790f2d10 100644 --- a/tests/test_ucampus_readonly_monitor.py +++ b/tests/test_ucampus_readonly_monitor.py @@ -202,3 +202,27 @@ def list_tasks(self, course_id, unit_id): assert not worker.is_alive() assert monitor.status()["state"] == "verified" assert len(calls) == 1 + + +def test_clock_rollback_resumes_readonly_polling_without_request_storm(store): + monitor.configure("course-v2:eng+course+1", "U1", True) + calls = [] + + class Reader: + def list_tasks(self, course_id, unit_id): + calls.append((course_id, unit_id)) + return [{"id": "v", "completed": True}] + + first = monitor.tick(clock=lambda: 2_000_000, reader_factory=Reader) + assert first["state"] == "verified" + assert len(calls) == 1 + + # A Windows clock adjustment can make the persisted last_attempt appear + # to be in the future. One fresh GET must be allowed, rather than + # suppressing all reads until the old wall-clock timestamp is reached. + rolled_back = monitor.tick(clock=lambda: 1_999_000, reader_factory=Reader) + assert rolled_back["state"] == "verified" + assert len(calls) == 2 + + monitor.tick(clock=lambda: 1_999_010, reader_factory=Reader) + assert len(calls) == 2, "ordinary 60-second pacing must still apply"