Repository navigation
fix(auth): cache 'gh auth status' OK state 300s — kills /user throttle root cause - #419
Merged
Merged
Conversation
…user throttle b69f's diagnostic 2026-05-02: core gh rate limit was 4766/5000 (fine). The secondary throttle was on /user specifically — the endpoint 'gh auth status' probes. Every airc-connect startup calls auth-state detection → hits /user. Daemon respawn cascade = many /user calls/min = secondary throttle locks for ~15min. #416 fixed the symptom (don't cascade on rate-limit). This PR fixes the cause (don't call /user on every startup). Cache the "ok" state for AIRC_AUTH_CACHE_SEC (default 300s = 5min) at /tmp/airc-gh-auth-ok-<uid>. Only "ok" is cached — failure paths (rate_limited, invalid, env_token_invalid) run fresh classification so we never hide a real failure behind a stale cache. Cache file is deleted on every non-ok detection. Effect: - Daemon respawn (within 5min of last successful auth check) = cache hit, zero gh API calls - First call after 5min OR after auth failure = fresh check, hits /user once - Worst-case rate of /user calls drops from "once per startup" to "once per 5min per process tree" This compounds with #415 (don't delete gist on teardown = fewer respawns) + #416 (sleep on rate-limit instead of cascade) for the "gh rate limits killing airc" structural fix. ## Tests - bash syntax clean - TTL math uses standard stat fall-through (Mac %m, Linux %Y, default 0) - Cache invalidation runs on every non-ok path so stale "ok" can't persist Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR reduces GitHub secondary rate-limit pressure by caching the “gh auth status is OK” result for a short TTL, avoiding repeated /user probes during frequent airc connect startups (notably in daemon respawn scenarios).
Changes:
- Add an OK-state cache for
airc_detect_gh_auth_statewith configurable TTL (AIRC_AUTH_CACHE_SEC, default 300s). - Refresh the cache on successful auth and invalidate it immediately on any non-OK detection.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| local _now; _now=$(date +%s 2>/dev/null || echo 0) | ||
| local _cache_mtime; _cache_mtime=$(stat -f %m "$_cache_file" 2>/dev/null || stat -c %Y "$_cache_file" 2>/dev/null || echo 0) | ||
| _cache_age=$(( _now - _cache_mtime )) | ||
| if [ "$_cache_age" -lt "$_cache_ttl" ] 2>/dev/null; then |
Comment on lines
+72
to
+73
| local _cache_file="${TMPDIR:-/tmp}/airc-gh-auth-ok-$(id -u 2>/dev/null || echo nobody)" | ||
| local _cache_ttl="${AIRC_AUTH_CACHE_SEC:-300}" |
Comment on lines
+72
to
+93
| local _cache_file="${TMPDIR:-/tmp}/airc-gh-auth-ok-$(id -u 2>/dev/null || echo nobody)" | ||
| local _cache_ttl="${AIRC_AUTH_CACHE_SEC:-300}" | ||
| if [ -f "$_cache_file" ]; then | ||
| local _cache_age=0 | ||
| local _now; _now=$(date +%s 2>/dev/null || echo 0) | ||
| local _cache_mtime; _cache_mtime=$(stat -f %m "$_cache_file" 2>/dev/null || stat -c %Y "$_cache_file" 2>/dev/null || echo 0) | ||
| _cache_age=$(( _now - _cache_mtime )) | ||
| if [ "$_cache_age" -lt "$_cache_ttl" ] 2>/dev/null; then | ||
| echo "ok" | ||
| return 0 | ||
| fi | ||
| fi | ||
|
|
||
| if gh auth status >/dev/null 2>&1; then | ||
| echo "ok" | ||
| # Refresh cache on success. | ||
| touch "$_cache_file" 2>/dev/null | ||
| return 0 | ||
| fi | ||
| # Auth not OK — invalidate cache so we re-check freshly next time | ||
| # (don't want a stale "ok" cache hiding a now-bad state). | ||
| rm -f "$_cache_file" 2>/dev/null |
joelteply
added a commit
that referenced
this pull request
May 2, 2026
…File: unbound variable' bug from #419 (#421) fix(portable): wrap stat in numeric-validating helpers — kills MSYS 'File: unbound variable' bug introduced by #419 ## What Two violations of the BSD-vs-GNU-stat trap fixed: - `lib/airc_bash/lib_auth.sh:77` (the immediate breaker — airc connect fails on Windows after #419 cached-mtime cache check) - `lib/airc_bash/platform_adapters.sh:118` (file_size: same pattern, same MSYS trap, just hadn't bitten yet) Both now route through new `file_mtime` / `file_size` helpers in `platform_adapters.sh` that VALIDATE the captured value is numeric before returning. Internal `_emit_if_numeric` does the check. ## Root cause `stat -f` is BSD's "format specifier" but GNU's "filesystem info." On MSYS Git Bash (GNU stat), `stat -f %m FILE` exits 0 and prints multi-line filesystem metadata to STDOUT — silently succeeding with non-numeric junk. The standard `bsd_cmd || gnu_cmd || fallback` chain doesn't fall through because the BSD attempt exits 0. Captured value started with ` File: "<path>" / ID: ...`. Then arithmetic at lib_auth.sh:78 (`$(( _now - _cache_mtime ))`) word- splits the string and bash strict-mode flags `File: unbound variable` when it tries to dereference `$File`. ## Fix shape (per Joel "wrap in function possibly") Each helper: 1. Tries GNU stat first (Linux + MSYS — the broader audience) 2. Validates output with `_emit_if_numeric` (case statement, not regex, so it works on every bash) 3. Falls through to BSD stat (macOS) on numeric-validation failure 4. file_size has additional `wc -c` POSIX fallback 5. Final fallback: echo 0 (safe for arithmetic, signals "couldn't tell") Adapters live in platform_adapters.sh which is already the portability home (detect_platform, _to_win_path, _to_bash_path). ## Verified on Windows Git Bash ``` $ source lib/airc_bash/platform_adapters.sh $ touch /tmp/foo $ file_mtime /tmp/foo # 1777746712 (clean integer) $ file_size /tmp/foo # 12 $ now=$(date +%s); mt=$(file_mtime /tmp/foo); echo $(( now - mt )) # 0 (NO 'File: unbound variable' anymore) ``` End-to-end: `airc connect` post-fix succeeds, takes over as host of #general, joins both subscribed channels, monitor running. Pre-fix bombed at gh-auth-cache-check on every invocation. ## Joel's broader directive Joel 2026-05-02: "yeah must adapt to systems" + "wrap in function possibly" + "search for more violations". This PR addresses all three: adapts via numeric validation (not just exit code), wraps both stat sites in shared helpers, sweeps for more (only 2 sites; both fixed). Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
joelteply
added a commit
that referenced
this pull request
May 2, 2026
…once-per-session contract notice (#423) fix(monitor,vuln-a): wrap peer chat in <peer-message> sandbox tags Vuln-A: peer broadcasts contain raw text that reaches the AI session via Claude Code Monitor. An attacker peer can put prompt-injection payloads in `msg` ("ignore previous instructions and ...") and the text arrives indistinguishable from operator commands. Fix: wrap peer-supplied content in <peer-message>...</peer-message> tags before printing to stdout. System events (fr in airc/sys) are trusted and pass through unwrapped. Anthropic's prompt-injection guidance recommends exactly this pattern for any third-party text fed to a model — Claude has been trained on XML-tag input boundaries forever, so the tagged content is naturally treated as data not directives. Plus b69f's suggestion 2026-05-02: emit a one-shot stdout contract notice on the FIRST peer message of the session, so the receiving Claude knows the contract surface. Idempotent — only fires once, subsequent peer messages just get the wrap. NOT a complete defense — sufficiently-crafted payload may still attempt escape — but raises the bar dramatically and aligns with the substrate's "evidence is for the debugger" rule (peer content surfaces, but as DATA, not COMMAND). Pairs with future transport- level peer auth (#418 design) for defense-in-depth. ## Tests - python ast.parse: clean - Manual smoke: send a broadcast → check formatter output has <peer-message> wrap + first-message contract notice - Existing system-events still unwrapped (alice joined, watchdog beacons, etc.) ## Pairs with - #418 (fusion-transport): vuln-A + transport-auth shrinks attack surface from "anyone with gh write" to "anyone on tailnet" - #401, #402, #403, #404, #406, #413, #419, #421: receive-path silent-failure sweep. Vuln-A is the next class — silent injection. Now loud + sandboxed. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
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.
b69f's diagnostic 2026-05-02: secondary throttle was on
/userspecifically (gh auth statusprobe target), NOT gist API (which was 4766/5000 fine). Daemon respawn cascade = many /user calls/min = throttle. #416 fixed symptom (don't cascade); this PR fixes cause (don't call /user every startup).Cache 'ok' state for AIRC_AUTH_CACHE_SEC (default 300s) at
/tmp/airc-gh-auth-ok-<uid>. Only 'ok' is cached — failure paths run fresh classification. Cache invalidates on any non-ok detection.Compounds with #415 (fewer teardown-respawns) + #416 (sleep on rate-limit) for the structural 'gh rate limits killing airc' fix.
🤖 Generated with Claude Code