A KDE Plasma panel widget showing how much Claude Max quota is left, at a glance.
dipstick CLAUDE.md
11 kB · 167 lines
Markdown
at main

CLAUDE.md — dipstick #

What this is #

A KDE Plasma 6 plasmoid showing how much Claude Max quota is left, in the panel. Scope, non-goals and the data-source contract: README.md.

Constraints #

  • Target: Plasma 6 on Wayland. No X11, no other desktop environments. The plasmoid runs inside plasmashell: no heavy work (parsing, blocking network calls) in that process, or it takes the user's panel down with it.
  • Never touch ~/.claude/.credentials.json. The project runs its own OAuth flow with its own token. Touching that file risks invalidating the user's Claude Code session.
  • Never modify the desktop session. KDE configuration, panel, shortcuts, compositor: document how to install the plasmoid, do not do it. Installation is the user's to run.
  • No network beyond api.anthropic.com and the OAuth hosts used for login. No telemetry, no analytics, no crash reporter, no third-party endpoints.
  • Credentials never in the repository and never in the logs. The token lives only in the user's configuration directory.
  • ~/.claude/projects/ is READ-ONLY. Those are the user's real session logs, and Claude Code is writing to them while dipstick reads. Always open them read-only; any cache or state file belongs in dipstick's own configuration directory, never inside that tree.
  • The quota endpoint is undocumented: treat it as unstable. Every path that uses it must have a fallback onto local data.

Language #

All code is in English: identifiers, comments, error messages, logs, CLI output and interface source strings. Documentation and commit messages too.

Italian survives only where it is a translation: translations/it.po and the localised package descriptions in the spec (Summary(it), %description -l it).

Working on it #

Build, installation, code layout and diagnostic commands: README.md.

In short: make test for the tests (they never touch the network), make vet before committing, and plasmawindowed org.kde.plasma.dipstick to try the plasmoid without putting it in the panel — its QML errors go to the user journal, not to the terminal.

Traps already hit #

  • plasmashell does NOT re-read the QML after kpackagetool6 --upgrade. It keeps in memory the version loaded when the widget was added to the panel. It needs systemctl --user restart plasma-plasmashell.service, which has to be asked of the user because it makes their panel disappear for a few seconds. plasmawindowed is a fresh process every time and always sees the latest version: hence the risk of believing a change has landed while the user is still looking at the old one. Before calling a UI change done, make sure the user is looking at the same build. The restart only works if plasmashell runs under that unit. One started by hand (plasmashell --replace from a terminal) is outside systemd: the restart launches a second instance that finds the first, exits at once with status 0, and leaves the old build on screen — a silent no-op. Check systemctl --user is-active plasma-plasmashell.service and the start time in ps -o lstart= -C plasmashell; if the unit is inactive, the way back is kquitapp6 plasmashell; systemctl --user start plasma-plasmashell.service.
  • XMLHttpRequest on file:// does not work in QML. Qt 6 disables it. The state is read through Plasma5Support.DataSource (executable) instead.
  • Transcripts contain duplicate messages, in quantity: ~3900 lines with usage for ~2000 distinct message.id. Deduplicate by message.id or the token counts nearly double. The <synthetic> model is not a real call.
  • Duplicates are not identical copies: they are progressive snapshots of the same message, so keep the largest. Keeping the first one seen understates the total.
  • seven_day_opus and seven_day_sonnet come back null. The per-model breakdown lives in limits[], with the name the server chooses: it is not a closed set.
  • The token must be taken under a lock. The daemon and the CLI share auth.json: without flock, two concurrent refreshes can spend the refresh token twice and force a logout. It is the same risk for which this project refuses to touch ~/.claude/.credentials.json, so it must not inflict it on itself.
  • Window boundaries for the totals are CALENDAR boundaries, not rolling ones. The chart is made of days, so MonthTokens and the per-model breakdown must start from the same midnight the series starts from. With a rolling boundary a sliver of messages was counted in the totals but drawn in no column.
  • LDFLAGS in the Makefile must be called GO_LDFLAGS. The former is a standard environment variable that rpmbuild fills with C linker flags: with ?= the environment wins, Go gets flags it does not understand, and the build fails only inside rpmbuild, never locally.
  • xgettext marks strings containing %1d or %1h as javascript-format. They are not: %1 and %2 are KDE's placeholders and the letter after them is a unit of measure. With those flags msgfmt rejects a translation that changes the letter (%1d becoming %1g for "days"). translations/extract.sh strips them.
  • A refresh round is not instant, and its length varies. dipstick refresh only sends SIGUSR1 and returns at once; the wait the user actually feels is the daemon's round. The first round after the daemon starts reads every transcript (~9 s on 1.6 GB); later ones read only what was appended, and are left with the quota call (~0.5 s). Any "loading" state in the plasmoid must therefore be tied to state.json changing, never to the lifetime of the command that was launched.
  • The full rescan grew with the history. At 316 files and 1.5 GB every transcript had been touched within the month, so the mtime filter skipped nothing and each round re-parsed everything (~11 s, 93% in json.Unmarshal over tool output). transcript.Scanner keeps, per file, the offset past the last complete line and that file's deduplicated messages, and reopens only files that grew. The cache lives in the daemon's memory, never on disk: one more file to invalidate would buy nothing a running process does not already have, and it could never go under ~/.claude/projects/ anyway. The one-shot transcript.Scan (the CLI) still reads everything.
  • Icon in metadata.json is resolved against the icon THEME, not against the package. Putting the SVG in contents/icons/ and naming it there is not enough: KPackage does not look inside the package for it, and the widget turns up with a blank page in the "Add Widgets" list — worse than the generic theme icon it replaced. The same file has to be installed into share/icons/hicolor/scalable/apps/ as well, which the Makefile does. It only shows up in the widget explorer, so it survives every check made on the popup alone.
  • Screenshots for the docs are taken against a made-up state.json. The popup shows the e-mail of the logged-in account, and the real numbers are usually a quiet afternoon: mostly zeroes and half an empty chart. Stop the daemon, back up state.json, write a demo state, shoot, restore, restart. Never retouch the image instead — that leaves the original in the file's metadata. Three things bite:
    • A day in local.daily needs its own models map, not just tokens. The chart stacks per model, so days carrying only a total draw nothing and the chart comes out empty while every figure above it looks right.
    • severity is the server's word, not ours. "normal" is the only value ever observed, so a demo state must not invent "warning" to colour a bar.
    • Shoot under LANG=en_GB.UTF-8, or the chart's date labels come out in the session language while the README is in English. Say in the caption that the data is made up: a screenshot of invented figures presented as a real reading is a small lie that costs nothing to avoid.
  • plasmashell runs with a PATH that does NOT include ~/.local/bin. The session gives it /usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin and nothing else, so a plain dipstick refresh from the QML exits 127 after a make install into the home — while the very same command works from a terminal. A system/RPM install lands in /usr/bin and hides the bug entirely. Anything the plasmoid launches has to carry the path with it.
  • Kirigami does not resolve the theme for a hidden item. An Item with visible: false reads every Kirigami.Theme colour as black until it is shown, so Look.qml is visible with no size — and therefore must never sit directly inside a layout, where it would take a cell (DailyChart and ModelBreakdown wrap their layout in an Item for this).
  • Inside plasmashell the theme has fewer roles than in an app. Plasma's Kirigami theme gives alternateBackgroundColor equal to the background and ignores the Selection colour set, while an app under org.kde.desktop gets both: a preview outside the panel can look right on roles the panel does not have. Draw only with text, inactive text, background, highlight and the semantic colours; derive everything else from them.
  • Plasma remembers the popup's size per applet and reapplies it. It lives as popupWidth/popupHeight in the applet's configuration, clamped only by the representation's Layout minimum and maximum. The redesign shipped with a minimum and a preferred width but no maximum, and a width stretched under the old popup (1042px) came back and spread the cards across the screen. Pin all three, width and height alike. It never shows in plasmawindowed or in an offscreen render: only in the panel of someone who has used it.
  • No parent.* in a Repeater delegate. The Repeater creates the delegate before parenting it, and unparents it on teardown, so parent is null on those evaluations: the chart's width: parent.width logged a TypeError per segment on every change of state.json (227 lines in one morning of ~/.cache/plasmashell.log) while drawing correctly, so nothing on screen gave it away. Reach the container by id. An offscreen render with one state never shows it; replace the state a few times to catch it.
  • state.json carries no interface strings. Localisation belongs to the plasmoid; the daemon runs with no locale context.