Skip to content

fix(auth): cache 'gh auth status' OK state 300s — kills /user throttle root cause - #419

Merged
joelteply merged 1 commit into
canaryfrom
fix/airc-cache-gh-auth-status
May 2, 2026
Merged

joelteply merged 1 commit into
canaryfrom
fix/airc-cache-gh-auth-status

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

b69f's diagnostic 2026-05-02: secondary throttle was on /user specifically (gh auth status probe 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

…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]>
Copilot AI review requested due to automatic review settings May 2, 2026 17:23
@joelteply
joelteply merged commit 92675f6 into canary May 2, 2026
8 checks passed
@joelteply
joelteply deleted the fix/airc-cache-gh-auth-status branch May 2, 2026 17:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_state with 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.

Comment thread lib/airc_bash/lib_auth.sh
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 thread lib/airc_bash/lib_auth.sh
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 thread lib/airc_bash/lib_auth.sh
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]>
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