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:
@@ -15,6 +15,7 @@ BarPanel {
|
|||||||
property var sevenDay: usage.sevenDay
|
property var sevenDay: usage.sevenDay
|
||||||
property var extra: usage.extra
|
property var extra: usage.extra
|
||||||
property string error: usage.error
|
property string error: usage.error
|
||||||
|
property string shortError: usage.shortError
|
||||||
property bool ready: usage.ready
|
property bool ready: usage.ready
|
||||||
|
|
||||||
readonly property var usage: ClaudeState {}
|
readonly property var usage: ClaudeState {}
|
||||||
@@ -74,6 +75,8 @@ BarPanel {
|
|||||||
required property var modelData
|
required property var modelData
|
||||||
spacing: 4
|
spacing: 4
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
// Dashes say nothing; with no numbers yet the error label stands alone.
|
||||||
|
visible: claude.ready || claude.shortError === ""
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
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 {
|
Column {
|
||||||
|
|||||||
@@ -16,8 +16,16 @@ QtObject {
|
|||||||
property string error: ""
|
property string error: ""
|
||||||
property bool ready: false
|
property bool ready: false
|
||||||
|
|
||||||
// The endpoint is rate-limited and the numbers move slowly.
|
// The endpoint is rate-limited and the numbers move slowly; a failure
|
||||||
property int interval: 300000
|
// 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 {
|
readonly property var proc: Process {
|
||||||
id: proc
|
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 {
|
function ingest(text: string): void {
|
||||||
let msg;
|
let msg;
|
||||||
try {
|
try {
|
||||||
msg = JSON.parse(text);
|
msg = JSON.parse(text);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
root.error = "no output";
|
root.fail("no output");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (msg.error) {
|
if (msg.error) {
|
||||||
// Keep the last good numbers; a failed poll is not zero usage.
|
// Keep the last good numbers; a failed poll is not zero usage.
|
||||||
root.error = msg.error;
|
root.fail(msg.error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
root.error = "";
|
root.error = "";
|
||||||
|
|||||||
@@ -29,10 +29,20 @@ Rectangle {
|
|||||||
sevenDay: ({ utilization: 91, resets_at: later })
|
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 {
|
ClaudePanel {
|
||||||
expanded: false
|
expanded: false
|
||||||
ready: false
|
ready: false
|
||||||
error: "token refresh HTTP 400"
|
error: "token refresh HTTP 400"
|
||||||
|
shortError: "EXPIRED"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,6 +61,7 @@ Rectangle {
|
|||||||
expanded: true
|
expanded: true
|
||||||
ready: true
|
ready: true
|
||||||
error: "usage HTTP 429"
|
error: "usage HTTP 429"
|
||||||
|
shortError: "ERR"
|
||||||
fiveHour: ({ utilization: 8, resets_at: soon })
|
fiveHour: ({ utilization: 8, resets_at: soon })
|
||||||
sevenDay: ({ utilization: 30, resets_at: later })
|
sevenDay: ({ utilization: 30, resets_at: later })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ fail() {
|
|||||||
|
|
||||||
[ -r "$creds" ] || fail "not logged in"
|
[ -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
|
# The refresh token rotates, so the new one must be written back or the CLI's
|
||||||
# next refresh fails and forces a re-login.
|
# next refresh fails and forces a re-login.
|
||||||
refresh() {
|
refresh() {
|
||||||
@@ -20,7 +24,7 @@ refresh() {
|
|||||||
client_id: "9d1c250a-e61b-44d9-88ed-5944d1962f5e", scope: $s}' \
|
client_id: "9d1c250a-e61b-44d9-88ed-5944d1962f5e", scope: $s}' \
|
||||||
| curl -s -m 20 -o "$body" -w '%{http_code}' \
|
| curl -s -m 20 -o "$body" -w '%{http_code}' \
|
||||||
https://platform.claude.com/v1/oauth/token \
|
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
|
if [ "$code" != 200 ]; then
|
||||||
rm -f "$body"
|
rm -f "$body"
|
||||||
fail "token refresh HTTP $code"
|
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 \
|
body="$(curl -s -m 20 -w '\n%{http_code}' https://api.anthropic.com/api/oauth/usage \
|
||||||
-H "Authorization: Bearer $token" \
|
-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="${body##*$'\n'}"
|
||||||
[ "$code" = 200 ] || fail "usage HTTP $code"
|
[ "$code" = 200 ] || fail "usage HTTP $code"
|
||||||
printf '%s' "${body%$'\n'*}" | jq -c . || fail "bad usage response"
|
printf '%s' "${body%$'\n'*}" | jq -c . || fail "bad usage response"
|
||||||
|
|||||||
Reference in New Issue
Block a user