From cd8761c2f4879590aef32ba4de5913839d1f45e5 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 12 Jul 2026 01:11:22 -0500 Subject: [PATCH] refac --- cptr/utils/timers.py | 16 +++++++++++----- cptr/utils/tools.py | 8 ++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cptr/utils/timers.py b/cptr/utils/timers.py index 6fc3183..fcd29ba 100644 --- a/cptr/utils/timers.py +++ b/cptr/utils/timers.py @@ -12,7 +12,9 @@ from cptr.env import TIMER_POLL_INTERVAL logger = logging.getLogger(__name__) -_RELATIVE_TIME = re.compile(r"^\+(\d+)([smhd])$") +_RELATIVE_TIME = re.compile( + r"^(?:\+|in\s+)?(\d+)\s*(s|sec(?:onds?)?|m|min(?:utes?)?|h|hours?|d|days?)$" +) _RFC3339_TIME = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$") _TIME_UNITS_NS = { "s": 1_000_000_000, @@ -27,20 +29,24 @@ def parse_timer_at(value: str) -> int: """Normalize a relative offset or timezone-aware RFC 3339 timestamp.""" raw = value.strip() now = time.time_ns() - relative = _RELATIVE_TIME.fullmatch(raw) + relative = _RELATIVE_TIME.fullmatch(raw.lower()) if relative: count = int(relative.group(1)) if count <= 0: raise ValueError("at must be in the future.") - return now + count * _TIME_UNITS_NS[relative.group(2)] + return now + count * _TIME_UNITS_NS[relative.group(2)[0]] if not _RFC3339_TIME.fullmatch(raw): - raise ValueError("at must be +5s, +5m, +1h, +2d, or an RFC 3339 timestamp with a timezone.") + raise ValueError( + "at must be a relative time such as 10s or in 10 seconds, " + "or an RFC 3339 timestamp with a timezone." + ) try: parsed = datetime.fromisoformat(raw.replace("Z", "+00:00")) except ValueError as exc: raise ValueError( - "at must be +5s, +5m, +1h, +2d, or an RFC 3339 timestamp with a timezone." + "at must be a relative time such as 10s or in 10 seconds, " + "or an RFC 3339 timestamp with a timezone." ) from exc if parsed.tzinfo is None or parsed.utcoffset() is None: raise ValueError("absolute at values must include an explicit timezone.") diff --git a/cptr/utils/tools.py b/cptr/utils/tools.py index cb65f09..104f347 100644 --- a/cptr/utils/tools.py +++ b/cptr/utils/tools.py @@ -2379,10 +2379,14 @@ async def timer( """Set a one-shot timer for this chat. Use it when time is the missing input: wait for a deployment, retry after - backoff, revisit work after a deadline, or follow up after silence. At - `at`, the agent receives `prompt` and the latest thread context; it may + backoff, revisit work after a deadline, or follow up after silence. When + it fires, the agent receives `prompt` and the latest thread context; it may act, set another timer, or finish silently. + `at` is normally relative to now. Prefer `10s`, `5m`, `1h`, or `2d`; plain + language such as `in 10 seconds` also works. Use an RFC 3339 timestamp + with a timezone only for a real calendar deadline. + `cancel_on` prevents launch when `chat.read` or `chat.user_message` happens first in this chat. Omit it when timed work must run regardless. """