Files
ai-agent/backend/test_limit_reset.py
gabrielvidalandClaude Opus 5.5 6c4900779f feat(runs): resume after a usage limit + stop & send now (backend)
- parse.limit_resets_at reads "resets 2:20pm (Europe/Paris)" off a
  usage-limit turn: ThreadItem.resetsAt and lastApiError.resetsAt
- cards carry stoppedOn {kind, resetsAt} for a run stopped on a
  rate_limit / overloaded / network error (older summaries re-read the
  reset off their stored message — no parser bump)
- /api/resume `now: true` skips the live run's inbox and force-resumes
  (_send_message restart=True)
- sidecar _stop_run drops the run's Stop-guard hold mailbox so a
  message left for a hold is never relayed twice

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 22:25:09 +02:00

88 lines
3.6 KiB
Python

"""conversations.limit_resets_at: a usage-limit turn's "resets 2:20pm
(Europe/Paris)" becomes the instant the window reopens — the card's
*Resume at …* — and the parser stamps it on the turn and on lastApiError."""
import datetime
import json
from zoneinfo import ZoneInfo
import conversations as c
PARIS = ZoneInfo("Europe/Paris")
def _local(ts):
return datetime.datetime.fromtimestamp(ts, PARIS)
def test_same_day_time_in_the_named_zone():
# 13:47 Paris (11:47Z, CEST) → today 14:20 Paris
t = c.limit_resets_at("You've hit your session limit · resets 2:20pm (Europe/Paris)",
"2026-10-21T11:47:00.000Z")
assert _local(t).strftime("%Y-%m-%d %H:%M") == "2026-10-21 14:20"
def test_a_time_already_past_is_tomorrow():
# 22:30 Paris, "resets 9pm" → tomorrow 21:00
t = c.limit_resets_at("You've hit your session limit · resets 9pm (Europe/Paris)",
"2026-10-21T20:30:00Z")
assert _local(t).strftime("%Y-%m-%d %H:%M") == "2026-10-22 21:00"
def test_a_dated_weekly_reset():
t = c.limit_resets_at("You've hit your weekly limit · resets Oct 23, 8pm (Europe/Paris)",
"2026-10-21T11:47:00Z")
assert _local(t).strftime("%Y-%m-%d %H:%M") == "2026-10-23 20:00"
def test_twelve_am_and_pm():
assert _local(c.limit_resets_at("resets 12am (Europe/Paris)", "2026-10-21T10:00:00Z")
).strftime("%d %H:%M") == "22 00:00"
assert _local(c.limit_resets_at("resets 12pm (Europe/Paris)", "2026-10-21T08:00:00Z")
).strftime("%d %H:%M") == "21 12:00"
def test_the_old_epoch_form():
assert c.limit_resets_at("Claude AI usage limit reached|1792580400", None) == 1792580400.0
def test_no_reset_named():
assert c.limit_resets_at("API Error: 529 overloaded", "2026-10-21T10:00:00Z") is None
assert c.limit_resets_at(None, None) is None
def test_the_parser_stamps_the_turn_and_last_api_error(tmp_path):
text = "You've hit your session limit · resets 2:20pm (Europe/Paris)"
recs = [
{"type": "user", "timestamp": "2026-10-21T11:40:00Z", "sessionId": "s1",
"message": {"role": "user", "content": "go"}},
{"type": "assistant", "timestamp": "2026-10-21T11:47:00Z", "sessionId": "s1",
"isApiErrorMessage": True, "error": "rate_limit",
"message": {"id": "m1", "role": "assistant", "model": "<synthetic>",
"content": [{"type": "text", "text": text}]}},
]
p = tmp_path / "s1.jsonl"
p.write_text("".join(json.dumps(r) + "\n" for r in recs))
st = c.ParserState(p, full=True)
for line in p.read_text().splitlines():
st.feed(json.loads(line))
card = st.thread[-1]
assert card["apiError"] == "rate_limit"
assert _local(card["resetsAt"]).strftime("%H:%M") == "14:20"
assert st.api_error["resetsAt"] == card["resetsAt"]
def test_the_card_chip_reads_old_summaries_too():
from conversations.cards import _stopped_on
err = {"kind": "rate_limit", "at": "2026-10-21T11:47:00Z",
"message": "You've hit your session limit · resets 2:20pm (Europe/Paris)"}
out = _stopped_on({"lastApiError": err}, "finished")
assert out["kind"] == "rate_limit"
assert _local(out["resetsAt"]).strftime("%H:%M") == "14:20"
# running, a non-resumable error, or none: no chip
assert _stopped_on({"lastApiError": err}, "running") is None
assert _stopped_on({"lastApiError": {**err, "kind": "auth"}}, "finished") is None
assert _stopped_on({}, "finished") is None
assert _stopped_on({"lastApiError": {"kind": "overloaded", "at": "x"}},
"interrupted") == {"kind": "overloaded", "resetsAt": None}