perf: move token refresh to a detached subprocess#134
Merged
angeloashmore merged 7 commits intomainfrom Apr 15, 2026
Merged
Conversation
Resolves process hanging after commands complete by moving the token refresh to a detached subprocess and only refreshing when the JWT expires within 1 hour. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Address PR review: check process.env.PRISMIC_HOST in the refresh subprocess and remove the now-unused refreshToken function. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
… files Move the three inline JavaScript strings (token refresh, telemetry flush, update check) into real files under src/subprocesses/ so they get proper type checking, linting, and formatting. Each subprocess is a separate tsdown entry point that can import shared code directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
angeloashmore
commented
Apr 15, 2026
- Trim JWT schema to only `exp` (the only claim we use) - Compute Segment authorization in the subprocess instead of passing it - Use `getNpmPackageVersion` lib helper in update-check subprocess Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Subprocess files are now thin entry points that parse argv and call domain functions, matching the refreshToken pattern. Renamed files to camelCase matching their function names. - sendSegmentEvents() in src/lib/segment.ts - updateVersionState() in src/lib/update-notifier.ts Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
angeloashmore
commented
Apr 15, 2026
…ocess Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit eb53960. Configure here.
Use `request` with a Zod schema to ensure the response is OK and contains a valid version string. Previously, a failed fetch could write a bad state file that blocked retries for 24 hours. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

Resolves: #132
Description
Move token refresh out of the main process to prevent CLI commands from hanging after completion.
sendSegmentEvents(),updateVersionState(),refreshToken())User identification (profile fetch) stays in-process as fire-and-forget using the current token. If the token is expired, identification is skipped — the next command will have a fresh token from the background refresh.
Checklist
Preview
How to QA 1
node --run build./dist/index.mjs whoami— should exit immediately after printing./dist/index.mjs repo list— should exit immediately after listingNote
Medium Risk
Touches auth token lifecycle and background process spawning, so regressions could impact login state, telemetry delivery, or CLI exit behavior across commands.
Overview
Prevents CLI hang by moving token refresh off the main process. The CLI now inspects the JWT
exp(via newdecodePayloadinlib/jwt) and, if the token is missing/near expiry (≤1h), schedules a detachedrefreshTokensubprocess on process exit; user identification/profile fetch remains in-process and is skipped when the token is expired.Standardizes background tasks into dedicated subprocess modules. Segment telemetry flushing and the update-notifier background version check are refactored from inline
node -escripts into compiledsrc/subprocesses/*entrypoints (plus exportedsendSegmentEvents/updateVersionStatehelpers), and npm registry version lookup is routed through the sharedrequesthelper with schema validation. Build config (tsdown.config.ts) is updated to emit these extra subprocess entry files.Reviewed by Cursor Bugbot for commit e660351. Bugbot is set up for automated code reviews on this repo. Configure here.
Footnotes
Please use these labels when submitting a review:
⚠️ #issue: Strongly suggest a change.
❓ #ask: Ask a question.
💡 #idea: Suggest an idea.
🎉 #nice: Share a compliment. ↩