"""Step 4 and 6: short typed message cards, and the freeze policy that goes with them.""" TYPES = ("request", "answer", "result", "close") PROGRESS = {"answer", "result", "close"} # these reset the thread's freeze counter LIMITS = {"task": 120, "deadline": 40, "expected_format": 80, "summary": 280, "body": 2000} MAX_TAGS, MAX_TAG_LEN = 8, 24 FROZEN_LINE = "Send a one-line summary of the outcome so far." class CardError(ValueError): pass def card(type, summary, task="", deadline="", expected_format="", tags=(), body=""): """Build the card fields for send_message. Reject, never silently cut, anything over a limit.""" if type not in TYPES: raise CardError(f"type must be one of {', '.join(TYPES)}") if not summary.strip(): raise CardError("summary is required when a type is set") fields = {"type": type, "task": task, "deadline": deadline, "expected_format": expected_format, "summary": summary, "body": body} for name, limit in LIMITS.items(): if len(fields[name]) > limit: raise CardError(f"{name} is {len(fields[name])} characters; the limit is {limit}") tags = list(tags) if len(tags) > MAX_TAGS or any(len(t) > MAX_TAG_LEN for t in tags): raise CardError(f"use at most {MAX_TAGS} tags of up to {MAX_TAG_LEN} characters") out = {k: v for k, v in fields.items() if v} return {**out, "tags": tags} if tags else out def progress_due(types, max_turns=6): """True when one more open-ended message would reach the freeze limit (default 6 turns).""" types = list(types) return next((i for i, t in enumerate(reversed(types)) if t in PROGRESS), len(types)) >= max_turns - 1 def is_frozen(text): return "dialog-frozen" in text or FROZEN_LINE in text def is_rate_limited(text): return "429" in text or "maxPerMinute" in text or "rate limit" in text.lower() def backoff(attempt, base=15, cap=120): # seconds before retry 0, 1, 2...: wait, never spin return min(cap, base * 2 ** attempt)