Skip to content

fix(staged): schedule a usage-limit pause on the reset time its message prints - #1129

Open
matt2e wants to merge 3 commits into
mainfrom
api-limit-detection
Open

matt2e wants to merge 3 commits into
mainfrom
api-limit-detection

Conversation

@matt2e

@matt2e matt2e commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary

A Claude usage-limit prompt error sometimes arrives with no structured reset time (data.rateLimit is absent). Staged then had nothing to schedule the auto-resume on and fell back to the 40–50 min usage_limit_backoff, so the session resumed well after the window reopened, or spent retry attempts before it did. This PR reads the reset time from the error message itself (resets 4:10pm (Australia/Melbourne)) and schedules the pause on it, the same way as a structured reset.

The structured reset is missing whenever a freshly started agent process refuses the turn: the CLI turns the prompt away from its own saved limit state without calling the API, so the SDK sends no rate_limit_event and the bridge has nothing to attach. Every message sent to an idle session while over the limit, and every refused auto-resume, starts such a process. In one user's DB that was 75 of 140 rate-limit errors.

What changes

Parser (new session_runner/usage_limit_reset.rs). It only runs on a message the existing usage-limit wording check has already picked out, and only on the prompt error's own message, so a resets … in agent output can't plant a reset.

  • resets 4:10pm (Zone) / resets 4pm (Zone): the next occurrence of that wall-clock time in the zone, possibly the next day.
  • resets Dec 3 at 2am (Zone) / resets Jun 30, 7pm (Zone): the year of the nearest occurrence, across New Year.
  • DST gaps move forward by the gap; a repeated time takes the first occurrence.
  • Unparseable text, an unknown zone, or an overloaded failure keep the old behaviour.

Guards on the parsed time

  • End of the printed minute (+60 s). The message has no seconds, so a real reset of 16:10:45 prints as 4:10pm. Without the rounding, a retry could land before the window reopened and spend a counted attempt. The pause copy may show 4:11pm where the CLI printed 4:10pm.
  • Just-passed grace (1 h). A printed reset up to an hour past is kept, not rolled to the next day or year, so a window that just reopened takes the minute-scale backoff instead of waiting a day.
  • 6 h cap on a time-only reset, failing closed. A time of day more than 6 h ahead is dropped unless the message names a long window (week, 7-day / seven-day, Opus, Sonnet). Otherwise clock skew or stale saved state could roll a past reset to tomorrow and park a 5-hour-limit pause for about 22 h. Dropped resets fall back to the 40–50 min schedule.
  • The parsed instant goes through the same epoch normalisation and reset_exceeds_allowance check as a structured reset.

Precedence in rate_limit_recovery_details

  • A structured reset that is still ahead wins.
  • A structured reset that is already past gives way to a printed one that is still ahead. The limit_type from that stale info is dropped with it, so the kind falls back to usage_limit and the pause copy doesn't name the wrong limit.
  • With no usable printed reset, the structured one stays, with its limit type.

rate_limit_scheduler::parse_reset_timestamp_millis becomes pub(crate) for the staleness check. The comment in rate_limit_recovery_details that said data.rateLimit is the only copy refused turns get is corrected: they get none.

Dependency: jiff = "0.2" for the IANA tz database. It was already in Cargo.lock through other crates, so the lock only gains the direct edge.

Known limitations

  • A long-window limit printed with a time alone, in wording the long-window check doesn't recognise, and 6–24 h ahead, takes the slow schedule instead of its exact reset.
  • The tests need a system tz database that includes Australia/Melbourne.
  • The upstream gap (no rate_limit_event on a locally refused prompt) is not addressed here; it belongs with claude-agent-acp / the Claude Agent SDK.

Testing

  • 15 parser tests in usage_limit_reset.rs: both forms, 12am/12pm, a DST gap, New Year, the grace at exactly 59 and 61 minutes, the 6 h boundary, and the wordings the cap applies to and leaves alone.
  • tests/usage_limit_details.rs covers the path through rate_limit_recovery_details: a bare error parks on its printed reset (including a second refused auto-resume), the stale-structured fallback, and the resulting kind.
  • cargo fmt, cargo clippy -D warnings and the src-tauri test suite passed locally before the last commit. The branch was pushed with --no-verify, so the pre-push hook did not run on it.

🤖 Generated with Claude Code

matt2e and others added 3 commits December 3, 2026 17:37
…ge prints

A Claude usage-limit prompt error has no `data.rateLimit` when a freshly
started agent process refuses the turn: the CLI turns the prompt away from
its own saved limit state without calling the API, so the SDK sends no
`rate_limit_event` and the bridge has nothing to attach. Every message sent
to an idle session while over the limit and every refused auto-resume starts
such a process (75 of 140 rate-limit errors in one user's DB). Those pauses
fell back to the 40–50 min `usage_limit_backoff`, retrying up to hours after
the window reopened or before it did.

When neither `data.rateLimit.resetsAt` nor `_meta["_claude/rateLimit"]
.resetsAt` (nor a reset on the error's own data) is present, the reset is now
read from the usage-limit message the wording check already picks out:

- `resets 4:10pm (Australia/Melbourne)` / `resets 4pm (...)`: the next
  occurrence of that wall-clock time in the zone, possibly the next day;
- `resets Dec 3 at 2am (Australia/Melbourne)` (or `Jun 30, 7pm`): the year
  of the nearest occurrence, across New Year.

A reset up to an hour past is kept rather than rolled to the next day or
year, so a just-passed window goes to the minute-scale backoff instead of
waiting a day. DST gaps move forward by the gap and repeated times take the
first occurrence (jiff's `compatible`). The parsed instant goes through the
same epoch normalisation and `reset_exceeds_allowance` check as a structured
one, so `retry_schedule` treats it exactly like `reset_at`. Unparseable text,
unknown zones and non-usage-limit kinds (an `overloaded` failure) keep
today's behaviour. The kind stays `usage_limit` (or the bridge's limit type),
as before.

Uses `jiff` (already in the lock via other crates) for the IANA tz database.

Also corrects the comment in `rate_limit_recovery_details`, which said
`data.rateLimit` is the only copy such refused turns get; they get none.

Signed-off-by: Matt Toohey <contact@matttoohey.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ay-late retries

Follow-ups from review of fe562a4, which reads the reset a bare Claude
usage-limit message prints (`resets 4:10pm (Australia/Melbourne)`):

- Minute precision: the printed time is truncated to the minute, so a real
  reset of 16:10:45 prints as `4:10pm`. A retry at reset + 30 s grace with
  low jitter could land before the window reopened, spend a counted
  attempt, and drop to the minute backoff because the same `4:10pm` now
  reads as just passed. A parsed reset is now taken as the end of its
  printed minute (+60 s). A CLI that rounds instead costs at most a minute.
  Structured resets are unchanged.

- Time-only reset rolled to tomorrow: a printed reset more than an hour
  past (CLI clock skew, e.g. a workstation, or stale saved state) rolls to
  its next occurrence. A date form rolls a year and is dropped by the
  allowance, so it falls back to the 40–50 min slow schedule, but a time of
  day rolled a day was within the allowance and parked the pause for about
  22 h. When the message names a 5-hour or session limit, a time-only reset
  more than 6 h ahead is now dropped the same way. The trade-off (including
  a weekly limit printed with a time alone, which keeps the day's wait) is
  documented next to `JUST_PASSED_GRACE_MS`.

- Stale structured reset: a structured reset still wins when it is ahead,
  but one already past no longer shadows a printed reset that is still
  ahead; a stale `rejected` info would otherwise send the pause to the
  minute backoff. With no usable printed reset, the structured one stays.
  `rate_limit_scheduler::parse_reset_timestamp_millis` is now crate-visible
  for that check.

Tests pin the just-passed grace at exactly 59 and 61 minutes for both
forms, both in the parser and through `rate_limit_recovery_details` (61 min
gives no reset and keeps `usage_limit`, the safe slow-schedule fallback),
plus the 6 h boundary, the weekly and dated cases it leaves alone, the
end-of-minute rounding, and the stale-structured fallback. Existing
assertions on message-read resets move one minute later.

Signed-off-by: Matt Toohey <contact@matttoohey.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tale limit type

Follow-ups from review of 90176e2:

- Time-only cap now fails closed. A printed time-of-day reset more than
  6 h ahead used to be dropped only when the message named a 5-hour or
  session limit, so any other wording (a bare `You've hit your limit ·
  resets 4pm (…)`, a future rewording) skipped the cap and could park the
  pause for ~22 h on a past reset rolled to the next day. The cap now
  applies unless the message names a long window: `week`, `7-day` /
  `seven-day`, `Opus` or `Sonnet`. An unrecognised wording falls back to
  the 40–50 min slow schedule instead of a day's wait; the cost is that a
  long window in an unrecognised wording, 6–24 h ahead, also takes the
  slow schedule. `SESSION_LIMIT_MAX_LEAD_MS` becomes
  `TIME_OF_DAY_MAX_LEAD_MS` and the trade-off is documented next to
  `JUST_PASSED_GRACE_MS`.

- No stale limit type beside a printed reset. When a printed reset
  replaces a structured reset that is already past, the `limit_type` from
  that stale info is dropped too, so the kind falls back to
  `USAGE_LIMIT_KIND` from the wording. Before, a stale `five_hour` info
  beside `weekly limit · resets Jun 30, 7pm` stored a weekly reset under
  `five_hour`, and the pause copy and auto-resume prompt named the wrong
  limit. A structured reset that is kept (still ahead, or with no usable
  printed one) keeps its limit type, and so does info that gave a type but
  no reset.

Tests: the bare `You've hit your limit` wording (with and without
minutes) and `out of extra usage` are capped at 6 h; a bare wording within
6 h is kept; weekly, Opus and Sonnet time-only resets keep the next day's;
a printed reset that replaces a stale one gives kind `usage_limit` (from
both the prompt error and the usage update), and a kept stale reset keeps
`five_hour`.

Signed-off-by: Matt Toohey <contact@matttoohey.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T09:19:04.498985Z 150ed89 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant