Skip to content

feat: Twitter UsageCapExceeded flag-file early-exit#2

Open
ClawdiaETH wants to merge 2 commits intomainfrom
feat/twitter-cap-exceeded-guard
Open

feat: Twitter UsageCapExceeded flag-file early-exit#2
ClawdiaETH wants to merge 2 commits intomainfrom
feat/twitter-cap-exceeded-guard

Conversation

@ClawdiaETH
Copy link
Owner

What

Adds a flag-file guard so spellblock-mentions.mjs exits immediately when the Twitter monthly usage cap is hit, instead of burning DB connections and exec cycles on every cron tick.

Why

Twitter monthly cap was hit 2026-02-24 ~19:40 CT. Every 15-min cron since has been a guaranteed API failure until cap resets ~2026-03-01. This fix makes those runs near-instantaneous no-ops.

How

  • ~/clawd/data/twitter-cap-exceeded.flag — shared flag file (ISO expiry timestamp, 1st of next month UTC)
  • Startup pre-flight: isCapFlagActive() runs before DB connection or any API call. If active → single log line + return.
  • Error detection: apiGet() detects UsageCapExceeded in response body (or HTTP 429) → calls writeCapFlag()
  • Auto-clear: clearCapFlag() runs on every successful API response
  • Auto-expire: flag deletes itself when expiry timestamp passes on next check

Flag file spec

~/clawd/data/twitter-cap-exceeded.flag
Content: 2026-03-01T00:00:00+00:00

Companion change

Same guard added to mention-screener.py in starl3xx/clawd (local branch feat/twitter-cap-exceeded-guard — awaiting push auth from Jake).

Verified

Flag logic smoke-tested:

Flag active: True | Expiry: 2026-03-01 | Time until reset: 3d 18h

When Twitter monthly cap is hit, apiGet() now:
- Detects UsageCapExceeded (or 429) in the API error response
- Writes ~/clawd/data/twitter-cap-exceeded.flag with expiry = 1st of next month
- Clears the flag on any successful API response

main() now checks the flag at startup (before DB connection or any API call)
and exits immediately with a single warning log.

This saves DB connections and wasted exec cycles on every 15-min cron tick
when the cap is guaranteed to be exceeded until month rollover.

Fixes: backlog [NEXT] 'Add UsageCapExceeded early-exit to spellblock-mentions.mjs'
Context: Cap hit 2026-02-24 ~19:40 CT; resets ~2026-03-01
@vercel
Copy link

vercel bot commented Feb 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
spellblock Ready Ready Preview, Comment Feb 25, 2026 5:13am

Request Review

@cursor
Copy link

cursor bot commented Feb 25, 2026

PR Summary

Low Risk
Small, localized control-flow change around Twitter API error handling and early-exit behavior; primary risk is skipping mention processing until the flag expires if the file is written incorrectly.

Overview
Adds a Twitter monthly usage-cap guard to spellblock-mentions.mjs using a shared flag file (~/clawd/data/twitter-cap-exceeded.flag) so cron runs exit immediately when the cap has been hit.

apiGet() now detects UsageCapExceeded in error responses (including after the 401 refresh retry), writes an expiry timestamp for the next month reset, and clears the flag after any successful Twitter API call; expired flags self-delete on next check.

Written by Cursor Bugbot for commit be59928. This will update automatically on new commits. Configure here.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is ON. A Cloud Agent has been kicked off to fix the reported issue.

Remove the '|| res.status === 429' fallback from the cap detection logic
at both lines 122 and 135. Twitter returns HTTP 429 for both per-window
rate limits (15-min cooldown) and monthly usage caps. Only the
UsageCapExceeded string in the response body reliably identifies the
monthly cap. The previous code would incorrectly disable the bot for
up to 30 days when hitting a routine rate limit.
@cursor
Copy link

cursor bot commented Feb 25, 2026

Bugbot Autofix prepared fixes for 1 of the 1 bugs found in the latest run.

  • ✅ Fixed: All HTTP 429 responses treated as monthly cap
    • Removed the '|| res.status === 429' fallback from both cap detection conditions at lines 122 and 135, so only the UsageCapExceeded body check triggers the monthly cap flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants