Skip to content

perf: lazy-load inquirer and defer telemetry init to halve startup latency#286

Merged
rhuanbarreto merged 1 commit into
mainfrom
perf/lazy-load-startup-optimizations
May 9, 2026
Merged

perf: lazy-load inquirer and defer telemetry init to halve startup latency#286
rhuanbarreto merged 1 commit into
mainfrom
perf/lazy-load-startup-optimizations

Conversation

@rhuanbarreto

Copy link
Copy Markdown
Contributor

Summary

  • Lazy-load inquirer via dynamic import() at the 4 call sites that actually prompt (adr/create, init, editor-detect, login-flow), saving ~200ms on every invocation that doesn't need interactive prompts
  • Defer telemetry/sentry init — start eagerly but only await in the preAction hook, saving ~150ms for --help/--version which never trigger preAction
  • Add ExitPromptError re-throw to all 18 command catch blocks and a new automated rule (ARCH-012/exit-prompt-error-rethrow) to enforce it going forward
  • Add startup latency budget tests to catch import-time regressions (--help <1s, --version <1s, adr list <1.5s, check <2.5s)

Result: --help drops from ~640ms to ~310ms (52% faster).

Startup profile (before → after)

Component Before After
inquirer static import ~200ms 0ms (lazy)
initSentry() + initTelemetry() ~150ms (blocking) 0ms for --help (deferred)
Total --help ~640ms ~310ms

Test plan

  • bun run validate passes (lint, typecheck, format, 738 tests, 24/24 ADR rules, knip, build)
  • --help, --version, adr list, check all produce correct output
  • Startup budget tests pass with headroom (median well under budgets)
  • Interactive commands (adr create, init) still load inquirer correctly when prompts are needed
  • Verify CI passes on all platforms (Linux, macOS, Windows)

…tency

The CLI had a ~640ms startup overhead even for `--help` / `--version`
due to two bottlenecks:

1. `inquirer` (~200ms) was statically imported via `adr/create.ts` →
   `adr/index.ts` → `cli.ts`, forcing every invocation to pay the
   parse cost even when no interactive prompt was needed.

2. `initSentry()` + `initTelemetry()` (~150ms) were awaited before
   command registration, blocking `--help` / `--version` which never
   trigger the `preAction` hook where telemetry events are emitted.

Fixes:

- Lazy-load `inquirer` via dynamic `import()` at the 4 call sites that
  actually prompt: `adr/create.ts`, `init.ts`, `editor-detect.ts`,
  `login-flow.ts`.

- Start telemetry/sentry init eagerly but only `await` it in the
  `preAction` hook (which `--help`/`--version` never reach). By the
  time a real command fires `preAction`, the init has usually already
  resolved concurrently, so the await is effectively free.

- Add `ExitPromptError` re-throw to all 18 command catch blocks — the
  canonical ARCH-012 pattern was documented but not enforced. Without
  it, Ctrl+C during a prompt exits with code 1 (failure) instead of
  130 (user cancellation).

- Add automated rule `ARCH-012/exit-prompt-error-rethrow` to prevent
  future regressions — catches any async command action whose catch
  block is missing the re-throw guard.

- Add startup latency budget tests (--help <1s, --version <1s,
  adr list <1.5s, check <2.5s) to catch import-time regressions.

Result: `--help` drops from ~640ms to ~310ms (52% faster).
Signed-off-by: Rhuan Barreto <rhuan@barreto.work>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying archgate-cli with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1f2ca11
Status: ✅  Deploy successful!
Preview URL: https://87c790e7.archgate-cli.pages.dev
Branch Preview URL: https://perf-lazy-load-startup-optim.archgate-cli.pages.dev

View logs

@rhuanbarreto rhuanbarreto merged commit 15c6b3c into main May 9, 2026
10 checks passed
@rhuanbarreto rhuanbarreto deleted the perf/lazy-load-startup-optimizations branch May 9, 2026 19:06
@archgatebot archgatebot Bot mentioned this pull request May 9, 2026
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.

1 participant