Skip to content

perf: move token refresh to a detached subprocess#134

Merged
angeloashmore merged 7 commits intomainfrom
aa/refresh-token-perf
Apr 15, 2026
Merged

perf: move token refresh to a detached subprocess#134
angeloashmore merged 7 commits intomainfrom
aa/refresh-token-perf

Conversation

@angeloashmore
Copy link
Copy Markdown
Member

@angeloashmore angeloashmore commented Apr 15, 2026

Resolves: #132

Description

Move token refresh out of the main process to prevent CLI commands from hanging after completion.

  • Only refresh when the JWT token expires within 1 hour (most commands skip refresh entirely)
  • When refresh is needed, run it in a detached subprocess matching the existing pattern used by Segment and the update notifier
  • Subprocess files are thin entry points that parse argv and call domain functions (e.g. sendSegmentEvents(), updateVersionState(), refreshToken())
  • Inline subprocess scripts extracted into dedicated TypeScript files with camelCase naming

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

  • A comprehensive Linear ticket, providing sufficient context and details to facilitate the review of the PR, is linked to the PR.
  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

Preview

How to QA 1

  1. Build with node --run build
  2. Run ./dist/index.mjs whoami — should exit immediately after printing
  3. Run ./dist/index.mjs repo list — should exit immediately after listing

Note

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 new decodePayload in lib/jwt) and, if the token is missing/near expiry (≤1h), schedules a detached refreshToken subprocess 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 -e scripts into compiled src/subprocesses/* entrypoints (plus exported sendSegmentEvents/updateVersionState helpers), and npm registry version lookup is routed through the shared request helper 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

  1. Please use these labels when submitting a review:
    ❓ #ask: Ask a question.
    💡 #idea: Suggest an idea.
    ⚠️ #issue: Strongly suggest a change.
    🎉 #nice: Share a compliment.

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>
Comment thread src/auth.ts Outdated
Comment thread src/auth.ts
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>
@angeloashmore angeloashmore marked this pull request as draft April 15, 2026 20:37
… 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>
Comment thread src/lib/jwt.ts Outdated
Comment thread src/lib/segment.ts Outdated
Comment thread src/lib/segment.ts
Comment thread src/subprocesses/flush-telemetry.ts Outdated
Comment thread src/subprocesses/update-check.ts Outdated
- 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>
@angeloashmore angeloashmore marked this pull request as ready for review April 15, 2026 20:54
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>
Comment thread src/lib/update-notifier.ts
Comment thread src/lib/update-notifier.ts Outdated
…ocess

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@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.

Fix All in Cursor

❌ 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.

Comment thread src/lib/update-notifier.ts
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>
@angeloashmore angeloashmore changed the title perf: move token refresh to a detached subprocess fix: move token refresh to a detached subprocess Apr 15, 2026
@angeloashmore angeloashmore changed the title fix: move token refresh to a detached subprocess perf: move token refresh to a detached subprocess Apr 15, 2026
@angeloashmore angeloashmore merged commit e189ed5 into main Apr 15, 2026
12 of 13 checks passed
@angeloashmore angeloashmore deleted the aa/refresh-token-perf branch April 15, 2026 21:42
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.

perf: move token refresh and user identification to a detached subprocess

1 participant