Repository navigation
feat(bin): add opt-in local Claude usage threshold warner - #3
Merged
Merged
Conversation
A threshold warner, not a viewer: fm-usage-warner.sh runs quota-axi's existing read-only --json snapshot (never a credential refresh) and posts a macOS Notification Center banner, edge-triggered with a de-dupe record, only when a configured window crosses a configured percentage. Silent and inert when unconfigured; arm refuses without a threshold or off macOS. Reuses the watcher's existing check-shim cadence rather than a new schedule or daemon.
…this PR, and that is now fixed. Shard 7 (fm-contributions.test.sh) failed for a reason outside this PR, so I left it alone. - **Shard 5:** `action_arm` refuses to run on anything but macOS before it checks for thresholds. The arm tests ran on a Linux runner without a Darwin stub. So `test_arm_refuses_without_a_configured_threshold` got "only available on macOS" when it expected "no threshold configured", and the three arm tests after it would have failed the same way. Fix: the test now makes one `uname` stub that prints Darwin (`DARWIN_BIN`) and puts it first on PATH for every `arm` call. The Linux-refusal test keeps its own stub that prints Linux. `bin/fm-usage-warner.sh` is unchanged. Checked: all 19 cases pass on macOS, all 19 pass with a fake `uname` printing Linux on PATH to stand in for the CI runner, and `shellcheck -x` is clean. - **Shard 7:** the failing assertion is `test_reservation_defers_later_url_when_fifteen_seconds_do_not_remain` ("a later PR began without the fifteen-second observation reservation"). It depends on the wall clock and a 20-second budget. This PR changes only usage-warner files (`bin/fm-usage-warner.sh`, its test, and docs) and does not touch `fm-contributions`. This looks like a timing flake on the runner, not something this change caused. The only file changed is `tests/fm-usage-warner.test.sh`, and it is not committed
… `tests/fm-usage-warner.test.sh`. `bin/fm-usage-warner.sh` is unchanged. The failing case was `test_arm_writes_and_binds_the_check_and_disarm_removes_it`, which reported "arm must succeed: could not write .../state/usage-warner.check.sh". The last fix round put a `uname` stub that prints Darwin in front of every `arm` call. But the stat helpers in `bin/fm-pr-lib.sh` (`fm_pr_file_device`, `fm_pr_file_mode`) also check `uname`, so the stub sent them to BSD `/usr/bin/stat -f` on the Linux runner. That call fails, so `shim_write` could never succeed. The symlink-refusal case only passed on Linux by accident, because this stat failure produces the same "could not write" message. Fix: - The Darwin stub is now used only by `test_arm_refuses_without_a_configured_threshold`. That check returns before any stat call, so the case still means something on Linux. - The three cases that go through the real shim write (arm/disarm, symlink refusal, armed shim runs a real check) no longer use the stub. They run only when the host is really Darwin. On other platforms the test prints one `ok - ... macOS-only; skipping on <os>` line, the same way `fm-stat-shadowing.test.sh` handles its Darwin-only cases. `arm` itself refuses to run anywhere but macOS, so those cases have nothing real to test on Linux. Checked: - On macOS, all cases pass. - With a `uname` stub that prints Linux first on PATH (standing in for the CI runner), every case passes or skips as expected and the exit code is 0. - `shellcheck -x` is clean. The change is not committed
max-metaplanet
added a commit
that referenced
this pull request
Oct 6, 2026
Carried from fork commit 80595ae (#3). Conflict resolution in docs/configuration.md: upstream has since restructured the watched-tool-updates section into labelled sub-headings with bullet lists. As authored, this commit inserted the usage-warner heading partway through that section's prose, so the warner section absorbed four lines and a settings paragraph that describe watched tool updates rather than the warner - the `FM_TOOL_UPDATE_*` bounds, the watched-tool edit note, and the secondmate inheritance note about watched tools. Upstream's restructure now owns all of that correctly under its own headings, so the warner is placed as a self-contained section after it and carries only the warner's own schema and arming contract, with its own one-line statement that the file is not inherited by secondmate homes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
yes start the warner
Build the local Claude usage-limit warner for macOS exactly as its approved design specifies: a threshold warner only, NOT a usage viewer. quota-axi already reads Claude quota locally with --json, --full, --tui and --no-credential-refresh, and a second reader that drifts from it is the defect class being removed. The one prerequisite, a one-time Keychain approval for quota-axi, landed on 2026-09-22 and a live read now returns real windows.
The approved design, in full, is at ~/.treehouse/firstmate-7bab20/2/firstmate/data/usage-warner-design.md: one read with two callers, on demand and periodic, reusing existing scheduling with no daemon; warnings through Notification Center via the same path the wedge alarm resolves to, edge-triggered with a de-dupe record; lives in firstmate bin/ with a doc; opt-in and silent when unconfigured, so an unconfigured user sees no behaviour change, no new failure mode, and no new hard dependency at dispatch. The upstream claude-swap licence is MIT and allows vendoring with notice; this change does not vendor, and the PR records that the licence was checked and what it allows.
What Changed
bin/fm-usage-warner.sh, a macOS-only warner for Claude usage limits. It does not display usage.checkruns the existingquota-axiread with--no-credential-refreshand compares each window against the<window-id>:<percent>thresholds in a localconfig/usage-warnerfile. When a window crosses its threshold it sends one Notification Center alert throughosascript, the same OS path the wedge alarm uses. Several crossings from one read go out as a single alert. A de-dupe record instate/.usage-warnerkeeps it quiet until the window drops back below its threshold, and a read failure that persists is reported once until it changes. The read is capped at 10s, or atFM_CHECK_TIMEOUTminus 3s when that is lower.checkprints nothing andarmrefuses.armalso refuses on anything other than macOS.armwritesstate/usage-warner.check.shand registers it withbin/fm-check-register.sh, so the existing watcher polls it on its normal schedule. There is no new daemon and no separate schedule.disarmremoves the check script, its registration and the de-dupe record.docs/usage-warner.md, a config-schema section indocs/configuration.md, an example file atdocs/examples/usage-warner, a README link and a documentation-audience entry. Also addstests/fm-usage-warner.test.sh.No code is copied in from claude-swap. Its licence was checked: it is MIT, which would allow copying code in as long as the licence notice is kept.
🤖 Generated with Claude Code
Risk Assessment
✅ Low: The warner does nothing unless configured and is isolated behind its own config gate. The last fix round now takes FM_CHECK_TIMEOUT the same way fm-mail-check.sh and fm-tool-update-check.sh do: same default and same fallback for bad values, with 3 seconds reserved for startup and kill grace. The read is capped at 10s, and when no time is left it is skipped and recorded as not measured. It never exits nonzero, so a user who has not configured it cannot hit a failure. I traced it with FM_CHECK_TIMEOUT set to 30, 6, 4, 3 and 2: the read always ends inside the watcher's limit.
Testing
The baseline suite (tests/fm-usage-warner.test.sh) passed. I then drove the warner live against the real quota-axi read in isolated temp homes, first on demand and then through the real fm-watch.sh periodic sweep. An unconfigured home stayed silent with exit 0 and wrote nothing, even with quota-axi missing from PATH and a bogus FM_CHECK_TIMEOUT, and arm refused. A configured five_hour:20 threshold, against a live reading of 22%, produced exactly one line and one notification call. Repeated checks stayed quiet. The warning fired again only after the record was cleared by a read below threshold, which I simulated by raising the threshold, since the live window could not be made to drop. Every quota-axi call carried --no-credential-refresh, and none carried --allow-keychain-prompt. Under the real watcher, the armed shim was dispatched and its warning surfaced as the wake reason. With FM_CHECK_TIMEOUT=5 and a hanging quota-axi, the warner reported its own 2s read timeout before the watcher could kill it, and recorded it. With FM_CHECK_TIMEOUT=2, it returned at once and recorded the read as not measured, exiting 0. Disarm removed the shim, trust file and record. The real osascript notification call succeeded, but I could not screenshot the banner: screencapture failed with 'could not create image from display' because the process lacks Screen Recording permission. The second watcher sweep woke on the watcher's own 'rearm-resurface' reason, so it only confirms that no second notification was posted; the de-dupe itself is shown by the on-demand repeats. All temp homes were removed, and the worktree is clean.
Evidence: On-demand live transcript (real quota-axi read, opt-in gate, edge-trigger, read-only argv)
Source: On-demand live transcript (real quota-axi read, opt-in gate, edge-trigger, read-only argv)
Evidence: Real watcher transcript (arm, periodic dispatch, timeout guard, disarm)
Source: Real watcher transcript (arm, periodic dispatch, timeout guard, disarm)
Evidence: Real Notification Center post transcript
Source: Real Notification Center post transcript
Evidence: On-demand driver script
Source: On-demand driver script
Evidence: Watcher driver script
Source: Watcher driver script
Evidence: Watcher dispatch of the armed check
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 5 issues found → auto-fixed (2) ✅
docs/configuration.md:669- The new 'Local Claude usage warner' section was inserted in the middle of the watched-tools section. The watched-tools lines that followed it ('A changed or returning condition is reported again.', 'Adding, removing, or changing a watched tool is an edit to this file and needs no code change or re-arming.', 'This file is not inherited by secondmate homes...', and the FM_TOOL_UPDATE_* paragraph) now sit under the usage-warner heading. Readers will take them as statements about config/usage-warner, and the watched-tools section loses its closing rules. Fix: move the new section so it starts after the FM_TOOL_UPDATE budget paragraph (just before '## Mail plane (.env)').bin/fm-usage-warner.sh:310- The de-dupe record is keyed only by window id, not by the directive. If one window has more than one threshold (for examplefive_hour:80andfive_hour:95, which the schema allows), the higher one never fires. At 85% the id is recorded as notified. When the window later reads 96%,id_in_list "$RECORD_NOTIFIED" five_houris true, so the 95% crossing is dropped silently until the window falls below 80%. Fix: keynotified_newand the record entries byid<TAB>threshold, or reject duplicate ids in config_thresholds.bin/fm-usage-warner.sh:132- FM_USAGE_WARNER_TIMEOUT_SECS accepts up to 60s, but the watcher kills a check after FM_CHECK_TIMEOUT (default 30). A killed run prints nothing and records nothing (docs/configuration.md:675). With a value such as 45 and a slow quota-axi, every poll is killed silently: no timeout report and no de-dupe update, repeated forever. The sibling checks (fm-mail-check.sh, fm-tool-update-check.sh) cut their budget to fit FM_CHECK_TIMEOUT. Fix: clamp TIMEOUT_SECS below ${FM_CHECK_TIMEOUT:-30}, leaving room for the record write and notification, the same way the siblings do.bin/fm-usage-warner.sh:323- usage_warner_notify runs osascript with no time limit, and record_write only runs after it. The wedge alarm it claims parity with wraps the same call in wedge_alarm_run_bounded. If osascript stalls (for example on a first-run notification-permission prompt) until the watcher kills the check, the crossing is never recorded, so every later poll re-attempts the notification, and the watcher discards the printed line each time. Fix: write the record before notifying, and wrap osascript in fm_run_timed.bin/fm-usage-warner.sh:125- Simplification: the user-tunable FM_USAGE_WARNER_TIMEOUT_SECS environment variable, with its own validation, its exit-2 refusal, and a documented configuration entry, is not required by the intent, which only needs the read to be bounded. It also causes the watcher-timeout mismatch reported above. The validation also runs before action dispatch, so a bad value makes even an unconfiguredcheck,disarm, or--helpexit 2. Recommend removing it in favour of a fixed internal bound that fits FM_CHECK_TIMEOUT.🔧 Fix applied.
1 warning still open:
bin/fm-usage-warner.sh:124- The fix round's fixed READ_TIMEOUT_SECS=10 only stays under the watcher's default FM_CHECK_TIMEOUT of 30. FM_CHECK_TIMEOUT is a documented user setting (docs/configuration.md:1150, bin/fm-watch.sh:233), and the sibling checks (fm-mail-check.sh:96, fm-tool-update-check.sh:163) cut their budgets to fit it. Failure case: a user sets FM_CHECK_TIMEOUT=8 and quota-axi hangs. The watcher kills the check at 8s, before the 10s bound fires. Nothing is printed and nothing is recorded, and that silent kill repeats on every poll. Your instruction for the fix said the bound should sit 'safely below FM_CHECK_TIMEOUT so the watcher never kills the check first'. Today that only holds for the default. The smallest remedy is to clamp the fixed bound to min(10, FM_CHECK_TIMEOUT minus a margin), reading FM_CHECK_TIMEOUT only on the configured read path so unconfigured runs still ignore the environment. Asking first because that brings back an environment read, which your 'one fixed internal bound' wording may have meant to exclude. Leaving it as is and documenting that FM_CHECK_TIMEOUT must stay above about 12 is the other option.🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
bash tests/fm-usage-warner.test.sh(baseline targeted suite, 19 cases)~/.no-mistakes/evidence/01M37WQ78FK5ZNWFRM18FZ8GBR/drive-on-demand.sh: on-demandfm-usage-warner.sh check|armagainst the real quota-axi (five_hour read 22%) in a throwaway FM_HOME, with a quota-axi wrapper that logs argv, then passes the call to the real binary, plus an osascript recorder~/.no-mistakes/evidence/01M37WQ78FK5ZNWFRM18FZ8GBR/drive-watcher.sh:fm-usage-warner.sh arm, then the realbin/fm-watch.sh(FM_CHECK_INTERVAL=0, fake tmux) dispatching the armed shim; a hanging-quota-axi run with FM_CHECK_TIMEOUT=5 under the watcher; FM_CHECK_TIMEOUT=2 on demand;disarm, then another watcher sweepFM_HOME=<tmp> bin/fm-usage-warner.sh checkwith the real osascript (a real Notification Center post), followed by ascreencapture -xattempt✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.