Repository navigation
feat(telemetry): robust on-demand JWT refresh + non-destructive 401 handling (v0.16.0) - #73
Conversation
…andling; v0.16.0
The license JWT (15-min TTL) was refreshed only at SessionStart, so mid-session
and retry-daemon uploads ran with an expired token. Two failure modes followed:
the drain skipped uploads entirely (expired token -> no resolvable endpoint),
and a 401 wiped the stored license (`setLicenseToken("")`).
P1:
- lib/license-activation.js: add `ensureFreshLicense(deviceId,{force})` —
best-effort, cross-process single-flight (mtime lock, 60s cooldown / 15s
staleness) wrapper around the refresh orchestration (`refreshLicense`, lifted
from logger.tryRefreshLicense, which now delegates). Pure `shouldRefresh`
decision split out for testing. Placed in this leaf module to avoid a
logger<->transfer require cycle.
- transfer.js: refresh once per drain batch (covers SessionStart, daemon,
session-end); on 401/403 refresh-and-retry instead of wiping the token;
read the token uncached so the long-lived daemon sees cross-process refreshes.
- jwt.js: `getEndpointFromTokenAllowExpired` — endpoint is routing info, valid
even from an expired token (collector accepts unauth), so a drain still
delivers while refresh is pending.
- credstore.js: `getLicenseTokenUncached` (disk-bypass read for the daemon).
P2:
- retry_daemon.js: in-memory adaptive backoff (exp up to 30m, reset on queue
progress) via new transfer.queuedFileCount(); empty-queue sweeps no longer
fire a refresh.
Verified: node -c (6 files), no require cycle, shouldRefresh unit table 9/9,
hermetic fake-server E2E (single /refresh per batch, token preserved on 401,
zero refresh on empty queue, all files delivered). Works before/after the
server-side /refresh route (skillbench-infra #45) via the gh fallback.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Caution Review failedPull request was closed or merged during review No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
WalkthroughAdds a cross-process single-flight license token refresh system using a lock file with cooldown and stale thresholds. Introduces uncached token reads, expired-JWT-tolerant endpoint resolution, and ChangesLicense Refresh Reliability & Retry Hardening
Sequence Diagram(s)sequenceDiagram
rect rgba(70, 130, 180, 0.5)
Note over transfer.js,TelemetryEndpoint: Upload with auth-retry
transfer.js->>credstore.js: getLicenseTokenUncached()
credstore.js-->>transfer.js: token (dropped if expired)
transfer.js->>jwt.js: getEndpointFromTokenAllowExpired(token)
jwt.js-->>transfer.js: endpoint URL (or null → keep file for retry)
transfer.js->>TelemetryEndpoint: POST event log / transcript with JWT
end
rect rgba(220, 100, 60, 0.5)
Note over transfer.js,ensureFreshLicense: 401/403 refresh-and-retry path
TelemetryEndpoint-->>transfer.js: 401 or 403
transfer.js->>ensureFreshLicense: ensureFreshLicense(deviceId, {force:true})
ensureFreshLicense->>license-activation.js: shouldRefresh() → acquire lock file
license-activation.js->>credstore.js: getLicenseTokenUncached()
license-activation.js->>ActivationEndpoint: POST /refresh (or gh auth token fallback)
ActivationEndpoint-->>license-activation.js: new JWT
ensureFreshLicense-->>transfer.js: fresh token
transfer.js->>TelemetryEndpoint: retry POST with fresh JWT
alt retry still fails
transfer.js->>TelemetryEndpoint: unauthenticated upload fallback
end
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 OpenGrep (1.22.0)OpenGrep fatal error (exit code 2): �[32m✔�[39m �[1mOpengrep OSS�[0m �[1m Loading rules from local config...�[0m Comment |
Problem
The license JWT has a 15-min TTL but was refreshed only at SessionStart (
session_start.js→tryRefreshLicense). Mid-session and the retry daemon ran with an expired token, causing two failures (verified against ~6.5k stuck events, May–Jun):getEndpointFromTokenreturns null for an expired token, so it returned before any attempt (even though the collector accepts unauthenticated uploads).transfer.jscalledsetLicenseToken(""), destroying the license on a transient rejection.P1 — robust, non-destructive refresh
lib/license-activation.js:ensureFreshLicense(deviceId, {force})— best-effort, cross-process single-flight (mtime lock, 60s cooldown / 15s staleness) aroundrefreshLicense(orchestration lifted fromlogger.tryRefreshLicense, which now delegates). PureshouldRefresh(...)split out for unit testing. Lives in this leaf module to avoid alogger ↔ transferrequire cycle.lib/transfer.js: refresh once per drain batch (covers SessionStart, daemon, session-end — daemon needs no change); on 401/403 refresh-and-retry, never wipe the token; read the token uncached so the long-lived daemon observes cross-process refreshes; resolve endpoint viagetEndpointFromTokenAllowExpiredfallback so a drain still delivers while refresh is pending.lib/jwt.js:getEndpointFromTokenAllowExpired— endpoint is routing info, valid even from an expired token (never used for auth).credstore.js:getLicenseTokenUncached(disk-bypass read).P2
monitors/retry_daemon.js: in-memory adaptive backoff (exp ×2 up to 30 min, reset on queue progress) via newtransfer.queuedFileCount(). Empty-queue sweeps no longer trigger a refresh.Compatibility
Works before and after the server-side
POST /refreshroute (skillbench-infra #45): until it's deployed, refresh falls back to the silentghpath (unchanged). The collector still accepts unauthenticated uploads, so delivery never depends on a valid token.Verification
node -con all 6 changed files; no require cycle.shouldRefreshunit decision table — 9/9 (cooldown vs staleness, force bypasses cooldown but respects in-flight lock)./refreshper batch despite 3 files + a 401, token preserved (not wiped), zero refresh on empty queue, all files delivered →.sent.Version
0.15.0 → 0.16.0(feat).🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Chores