mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-18 21:54:34 +01:00
Standalone utils subproject (pyproject, pre-commit, CI, install.sh, README with the library API). It no longer imports leetcode_guard: guard_bridge resolves the three read-only calls on demand. Q5: SQL problems skip req: and ask for an OUTPUT SHAPE (columns, rows, grouping) before code; algorithm problems keep req:. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWYGW5NZL5uVpnqmNKo
75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
"""Step 4: has LeetCode seen an Accepted for this problem since it started?
|
|
|
|
The harvester's own read-only probe (leetcode-guard's ``fetch_recent_ac``,
|
|
reached through `guard_bridge`), at most once a minute: LeetCode answers
|
|
a rate limit exactly like a dead session, so polling harder manufactures
|
|
the failure. Three answers, never two: ``accepted``, ``not_yet``, and
|
|
``unknown`` when the probe is unusable -- "could not check" is never "not
|
|
solved" (CLAUDE.md). The probe mints nothing; crediting stays the
|
|
harvester's job. ``timestamp`` is unix SECONDS, ``started`` is ms.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import time
|
|
from typing import TYPE_CHECKING, Final
|
|
|
|
from leetcode_coach import guard_bridge
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Callable
|
|
|
|
from leetcode_coach.guard_bridge import SolveProbe
|
|
|
|
_logger: Final = logging.getLogger(__name__)
|
|
|
|
EVERY_S: Final = 60.0
|
|
ACCEPTED: Final = "accepted"
|
|
NOT_YET: Final = "not_yet"
|
|
UNKNOWN: Final = "unknown"
|
|
|
|
|
|
def live_probe() -> SolveProbe:
|
|
"""One read of kuhy's recent accepted submissions."""
|
|
return guard_bridge.recent_accepted()
|
|
|
|
|
|
class AcceptedProbe:
|
|
"""Rate-limited, three-valued Accepted check."""
|
|
|
|
def __init__(
|
|
self,
|
|
probe: Callable[[], SolveProbe] = live_probe,
|
|
monotonic: Callable[[], float] = time.monotonic,
|
|
every_s: float = EVERY_S,
|
|
) -> None:
|
|
"""Bind the probe and the clock that spaces it."""
|
|
self._probe = probe
|
|
self._monotonic = monotonic
|
|
self._every = every_s
|
|
self._last: float | None = None
|
|
|
|
def due(self) -> bool:
|
|
"""Whether a minute has passed since the last probe."""
|
|
last = self._last
|
|
return last is None or self._monotonic() - last >= self._every
|
|
|
|
def check(self, slug: str, started_ms: int) -> str:
|
|
"""``accepted`` / ``not_yet`` / ``unknown`` for `slug` since `started_ms`."""
|
|
self._last = self._monotonic()
|
|
try:
|
|
answer = self._probe()
|
|
except Exception: # pylint: disable=broad-exception-caught
|
|
# Anything the probe raises is a probe we cannot believe.
|
|
_logger.exception("the Accepted probe failed")
|
|
return UNKNOWN
|
|
if not answer.usable:
|
|
_logger.warning("Accepted probe unusable: %s", answer.reason)
|
|
return UNKNOWN
|
|
seen = any(
|
|
s.title_slug == slug and s.timestamp * 1000 >= started_ms
|
|
for s in answer.submissions
|
|
)
|
|
return ACCEPTED if seen else NOT_YET
|