fix(terra): surface Claude usage errors and retry sooner

The CLD panel went blank with no reason when Claude Code's saved OAuth
token had expired and the widget's own renewal was refused.

- Collapsed panel shows EXPIRED/ERR instead of dashes; failures are
  logged to `qs log`.
- Retry every minute after a failure instead of every 5.
- claude-usage identifies as claude-cli, the suspected cause of the
  refresh 429s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-30 21:21:20 +02:00
co-authored by Claude Opus 5.5
parent c33d184228
commit bee9b49ce1
4 changed files with 47 additions and 6 deletions
@@ -15,6 +15,7 @@ BarPanel {
property var sevenDay: usage.sevenDay
property var extra: usage.extra
property string error: usage.error
property string shortError: usage.shortError
property bool ready: usage.ready
readonly property var usage: ClaudeState {}
@@ -74,6 +75,8 @@ BarPanel {
required property var modelData
spacing: 4
anchors.verticalCenter: parent.verticalCenter
// Dashes say nothing; with no numbers yet the error label stands alone.
visible: claude.ready || claude.shortError === ""
Text {
anchors.verticalCenter: parent.verticalCenter
@@ -93,6 +96,16 @@ BarPanel {
}
}
}
Text {
anchors.verticalCenter: parent.verticalCenter
visible: claude.shortError !== ""
text: claude.shortError
color: Theme.hot
font.family: Theme.displayFont
font.pixelSize: 13
font.bold: true
}
}
Column {
@@ -16,8 +16,16 @@ QtObject {
property string error: ""
property bool ready: false
// The endpoint is rate-limited and the numbers move slowly.
property int interval: 300000
// The endpoint is rate-limited and the numbers move slowly; a failure
// retries sooner so the panel recovers once Claude Code renews the token.
property int interval: root.error !== "" ? 60000 : 300000
// Collapsed-rail label for the current error.
readonly property string shortError: {
if (root.error === "")
return "";
return /refresh|401|not logged in|no refresh token/.test(root.error) ? "EXPIRED" : "ERR";
}
readonly property var proc: Process {
id: proc
@@ -39,17 +47,22 @@ QtObject {
}
}
function fail(message: string): void {
console.warn(`claude-usage: ${message}`);
root.error = message;
}
function ingest(text: string): void {
let msg;
try {
msg = JSON.parse(text);
} catch (e) {
root.error = "no output";
root.fail("no output");
return;
}
if (msg.error) {
// Keep the last good numbers; a failed poll is not zero usage.
root.error = msg.error;
root.fail(msg.error);
return;
}
root.error = "";
@@ -29,10 +29,20 @@ Rectangle {
sevenDay: ({ utilization: 91, resets_at: later })
}
ClaudePanel {
expanded: false
ready: true
error: "usage HTTP 429"
shortError: "ERR"
fiveHour: ({ utilization: 42, resets_at: soon })
sevenDay: ({ utilization: 91, resets_at: later })
}
ClaudePanel {
expanded: false
ready: false
error: "token refresh HTTP 400"
shortError: "EXPIRED"
}
}
@@ -51,6 +61,7 @@ Rectangle {
expanded: true
ready: true
error: "usage HTTP 429"
shortError: "ERR"
fiveHour: ({ utilization: 8, resets_at: soon })
sevenDay: ({ utilization: 30, resets_at: later })
}
+6 -2
View File
@@ -7,6 +7,10 @@ fail() {
[ -r "$creds" ] || fail "not logged in"
# Identify as Claude Code: the suspected cause of refreshes failing with 429.
version="$(claude --version 2>/dev/null | cut -d" " -f1)" || true
ua="claude-cli/${version:-2.1.0} (external, cli)"
# The refresh token rotates, so the new one must be written back or the CLI's
# next refresh fails and forces a re-login.
refresh() {
@@ -20,7 +24,7 @@ refresh() {
client_id: "9d1c250a-e61b-44d9-88ed-5944d1962f5e", scope: $s}' \
| curl -s -m 20 -o "$body" -w '%{http_code}' \
https://platform.claude.com/v1/oauth/token \
-H 'Content-Type: application/json' --data-binary @-)" || code=000
-H 'Content-Type: application/json' -A "$ua" --data-binary @-)" || code=000
if [ "$code" != 200 ]; then
rm -f "$body"
fail "token refresh HTTP $code"
@@ -51,7 +55,7 @@ token="$(jq -r '.claudeAiOauth.accessToken // empty' "$creds")"
body="$(curl -s -m 20 -w '\n%{http_code}' https://api.anthropic.com/api/oauth/usage \
-H "Authorization: Bearer $token" \
-H 'anthropic-beta: oauth-2025-04-20')" || fail "usage request failed"
-H 'anthropic-beta: oauth-2025-04-20' -A "$ua")" || fail "usage request failed"
code="${body##*$'\n'}"
[ "$code" = 200 ] || fail "usage HTTP $code"
printf '%s' "${body%$'\n'*}" | jq -c . || fail "bad usage response"