From bee9b49ce135d85de520351819cbd0565b05b90c Mon Sep 17 00:00:00 2001 From: Erik Simon Date: Wed, 30 Sep 2026 21:21:20 +0200 Subject: [PATCH] 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 --- .../Widgets/Bar/Claude/ClaudePanel.qml | 13 ++++++++++++ .../Widgets/Bar/Claude/ClaudeState.qml | 21 +++++++++++++++---- .../quickshell/tests/ClaudePanelHeadless.qml | 11 ++++++++++ pkgs/claude-usage.sh | 8 +++++-- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/dotfiles/quickshell/HyprChrome/Widgets/Bar/Claude/ClaudePanel.qml b/dotfiles/quickshell/HyprChrome/Widgets/Bar/Claude/ClaudePanel.qml index 51c7106..0d4a1d2 100644 --- a/dotfiles/quickshell/HyprChrome/Widgets/Bar/Claude/ClaudePanel.qml +++ b/dotfiles/quickshell/HyprChrome/Widgets/Bar/Claude/ClaudePanel.qml @@ -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 { diff --git a/dotfiles/quickshell/HyprChrome/Widgets/Bar/Claude/ClaudeState.qml b/dotfiles/quickshell/HyprChrome/Widgets/Bar/Claude/ClaudeState.qml index a72aa4f..63fb6cd 100644 --- a/dotfiles/quickshell/HyprChrome/Widgets/Bar/Claude/ClaudeState.qml +++ b/dotfiles/quickshell/HyprChrome/Widgets/Bar/Claude/ClaudeState.qml @@ -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 = ""; diff --git a/dotfiles/quickshell/tests/ClaudePanelHeadless.qml b/dotfiles/quickshell/tests/ClaudePanelHeadless.qml index 0479aec..c58e0be 100644 --- a/dotfiles/quickshell/tests/ClaudePanelHeadless.qml +++ b/dotfiles/quickshell/tests/ClaudePanelHeadless.qml @@ -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 }) } diff --git a/pkgs/claude-usage.sh b/pkgs/claude-usage.sh index b5fc1d2..e838d11 100644 --- a/pkgs/claude-usage.sh +++ b/pkgs/claude-usage.sh @@ -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"