Back off after a failed usage fetch instead of retrying every redraw

A failed fetch left the cache mtime untouched, so with no network every
redraw — and a redraw happens on every keystroke — tried curl again and
could wait --max-time 10 for it. Cutting the TTL from an hour to five
minutes made that start 55 minutes sooner, so it is worth fixing now.

The marker is a separate file rather than a touch of the cached
response: a cold /tmp has no response to touch, and that is exactly the
case with no stale data to fall back on, where the timeout is paid in
full with nothing to show for it.

It goes down before the request and is cleared when that lands, not
written afterwards. A fetch is in flight for as long as curl takes to
give up, and this cache dir is shared by every session, so redraws
starting inside that window are real rather than hypothetical: they now
serve stale data instead of each launching their own doomed request.
Every attempt writes it, so a repeated failure restarts the backoff, and
only an attempt does, so the redraws it suppresses cannot keep
re-stamping it and it always lapses.

Missing credentials stays outside the backoff — it costs no timeout, and
the redraw after a login should show the bars rather than wait out a
retry window it had no part in earning.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jonny Barnes 2026-09-02 13:12:45 +01:00
commit 35e170b40c
No known key found for this signature in database
2 changed files with 163 additions and 16 deletions

View file

@ -19,6 +19,7 @@ OVERHEAD=$(( 2 * PAD + 1 ))
# written there is shown in the running TUI as real usage until it expires.
export STATUSLINE_CACHE_DIR="$(mktemp -d)"
CACHE="$STATUSLINE_CACHE_DIR/statusline-usage-cache.json"
FAIL_MARKER="$STATUSLINE_CACHE_DIR/statusline-usage-fail"
REPO="$(mktemp -d)"
STUB="$(mktemp -d)"
@ -33,9 +34,17 @@ git -C "$REPO" -c user.email=t@t -c user.name=t commit -q --allow-empty -m init
# usage API. The token is faked too, so the keychain is never read.
export CLAUDE_CODE_OAUTH_TOKEN=test-token
export CURL_CALLS="$STUB/calls"
export CURL_MARKER_SEEN="$STUB/marker-seen"
export FAIL_MARKER
cat > "$STUB/curl" <<'SH'
#!/bin/bash
echo call >> "$CURL_CALLS"
# Record whether the backoff marker was already written when the fetch
# started, which is what a concurrent redraw would see.
[ -f "$FAIL_MARKER" ] && echo seen >> "$CURL_MARKER_SEEN"
# With $CURL_FAIL set, behave as curl does with no network: nothing on
# stdout and a non-zero exit.
[ -n "$CURL_FAIL" ] && exit 6
printf '%s' "$CURL_REPLY"
SH
chmod +x "$STUB/curl"
@ -71,15 +80,25 @@ cached_response_aged() {
"seven_day":{"utilization":7.0,"resets_at":"2026-08-28T02:00:00+00:00"},
"extra_usage":{"is_enabled":false},"limits":[]}
JSON
AGE="$1" FILE="$CACHE" python3 -c '
age_file "$CACHE" "$1"
rm -f "$FAIL_MARKER"
: > "$CURL_CALLS"
: > "$CURL_MARKER_SEEN"
}
# Backdate $1 by $2 seconds.
age_file() {
FILE="$1" AGE="$2" python3 -c '
import os, time
f = os.environ["FILE"]; t = time.time() - int(os.environ["AGE"])
os.utime(f, (t, t))'
: > "$CURL_CALLS"
}
calls() { wc -l < "$CURL_CALLS" | tr -d ' '; }
# No cached response and no backoff marker: a cold /tmp.
cold_cache() { rm -f "$CACHE" "$FAIL_MARKER"; : > "$CURL_CALLS"; : > "$CURL_MARKER_SEEN"; }
# ------------------------------------------------------------------- 5min TTL
echo "Usage cache TTL (5 minutes):"
@ -131,6 +150,102 @@ case "$out" in *"5h"*) bad "usable 67: no bars at the narrow tier" "$out" ;;
[ "$(calls)" = "0" ] && ok "usable 67: no API call when no bars are shown" \
|| bad "usable 67: no API call when no bars are shown" "$out"
# --------------------------------------------------------- offline retry backoff
# With no network, curl can burn --max-time 10 before giving up. A failed fetch
# is remembered so redraws in the next minute do not each pay for that.
echo
echo "A failed fetch backs off instead of retrying every redraw:"
export CURL_FAIL=1
cached_response_aged 360
out=$(render 245)
[ "$(calls)" = "1" ] && ok "offline: first redraw attempts the fetch" \
|| bad "offline: first redraw attempts the fetch" "$out"
case "$out" in *"5%"*) ok "offline: falls back to the stale response" ;;
*) bad "offline: falls back to the stale response" "$out" ;; esac
out=$(render 245)
[ "$(calls)" = "1" ] && ok "offline: the next redraw does not retry" \
|| bad "offline: the next redraw does not retry" "$out"
case "$out" in *"5%"*) ok "offline: still shows the stale response while backed off" ;;
*) bad "offline: still shows the stale response while backed off" "$out" ;; esac
# The marker must be the thing that ages, not be re-stamped by the redraws it
# suppresses — otherwise the backoff never lapses and the bars never return.
age_file "$FAIL_MARKER" 90
out=$(render 245)
[ "$(calls)" = "2" ] && ok "offline: retries once the backoff lapses" \
|| bad "offline: retries once the backoff lapses" "$out"
out=$(render 245)
[ "$(calls)" = "2" ] && ok "offline: the second failure restarts the backoff" \
|| bad "offline: the second failure restarts the backoff" "$out"
# The marker goes down before the request, not after it: a fetch is in flight
# for as long as curl takes to time out, and every redraw starting inside that
# window would otherwise launch its own. The cache dir is shared between
# sessions, so those redraws are not hypothetical. Starting from no marker at
# all, so only this attempt's own write can satisfy it.
cold_cache
out=$(render 245)
case "$(cat "$CURL_MARKER_SEEN")" in *seen*) ok "offline: the in-flight fetch marked itself before calling" ;;
*) bad "offline: the in-flight fetch marked itself before calling" "$out" ;; esac
# A cold /tmp is the case a touch of the response file cannot cover: there is
# no response to touch, so every redraw would pay the timeout.
cold_cache
out=$(render 245)
[ "$(calls)" = "1" ] && ok "offline, no cache: first redraw attempts the fetch" \
|| bad "offline, no cache: first redraw attempts the fetch" "$out"
out=$(render 245)
[ "$(calls)" = "1" ] && ok "offline, no cache: the next redraw does not retry" \
|| bad "offline, no cache: the next redraw does not retry" "$out"
case "$out" in *"5h"*) bad "offline, no cache: no bars, having no data" "$out" ;;
*) ok "offline, no cache: no bars, having no data" ;; esac
unset CURL_FAIL
# Back online, a marker that has lapsed must not keep suppressing fetches.
cold_cache
: > "$FAIL_MARKER"
age_file "$FAIL_MARKER" 90
out=$(render 245)
case "$out" in *"42%"*) ok "back online: fetches and renders the fresh percentage" ;;
*) bad "back online: fetches and renders the fresh percentage" "$out" ;; esac
[ ! -f "$FAIL_MARKER" ] && ok "back online: a success clears the backoff marker" \
|| bad "back online: a success clears the backoff marker"
cold_cache
out=$(render 245)
case "$(cat "$CURL_MARKER_SEEN")" in *seen*) ok "back online: a succeeding fetch is marked while it runs" ;;
*) bad "back online: a succeeding fetch is marked while it runs" "$out" ;; esac
[ ! -f "$FAIL_MARKER" ] && ok "back online: and unmarked once it lands" \
|| bad "back online: and unmarked once it lands" "$out"
# ------------------------------------------------------------ nothing to fetch
# No credentials is not a network failure: it costs no timeout, and the next
# redraw could succeed the instant a login lands. Backing off would hide the
# bars for a minute for nothing.
echo
echo "No credentials is not a failure to back off from:"
cat > "$STUB/security" <<'SH'
#!/bin/bash
exit 44
SH
chmod +x "$STUB/security"
NOHOME="$(mktemp -d)"
cold_cache
out=$(stdin_json "$REPO" \
| env -u CLAUDE_CODE_OAUTH_TOKEN HOME="$NOHOME" TERM_WIDTH=$(( 245 + OVERHEAD )) \
bash "$SCRIPT" 2>&1 | sed $'s/\033\[[0-9;]*m//g')
[ "$(calls)" = "0" ] && ok "no token: no fetch attempted" \
|| bad "no token: no fetch attempted" "$out"
[ ! -f "$FAIL_MARKER" ] && ok "no token: no backoff marker written" \
|| bad "no token: no backoff marker written" "$out"
rm -f "$STUB/security"
rm -rf "$NOHOME"
echo
echo " $pass passed, $fail failed"
[ "$fail" -eq 0 ]