fix(cli): reduce startup time by deferring Kilo module loading and telemetry work - #12682
Conversation
Code Review SummaryStatus: No Issues Found | Recommendation: Merge All four previously flagged suggestions have been addressed in the latest commit:
Files Reviewed (3 files)
Previous Review Summary (commit 9ab316e)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 9ab316e)Status: 4 Issues Found | Recommendation: Merge (nice-to-haves only) Overview
Issue Details (click to expand)SUGGESTION
Files Reviewed (21 files)
All Reviewed by claude-sonnet-5 · Input: 20 · Output: 3.7K · Cached: 395.9K Review guidance: REVIEW.md from base branch |
…tup-performance fix(cli): reduce startup time by deferring Kilo module loading and telemetry work
Problem
Kilo CLI startup has grown slower than upstream OpenCode on the same machine (see #10242), and the gap is widest for short invocations. Profiling the startup path showed the extra time is Kilo-specific work that runs (or loads) before any command executes:
kilocode/cli/setup.tswas imported eagerly fromsrc/index.tsand statically pulled in telemetry, the gateway, AppRuntime, config, auth, session-export, and the JSON migration (~800ms of module loading from source).run,tui,serve,web,mcp,attach,providers) and the Kilo command modules (console,cloud,roll-call,profile,daemon,remote,config) re-eagerized heavy dependency chains that upstream opencode#30453 had already deferred behind lazy handlers.Telemetry.updateIdentityblocked every bootstrap on a profile HTTP request (~150-200ms) even though the resolved email rarely changes.Telemetry.shutdownblocked every process exit on a PostHog flush (~500ms from EU: TCP+TLS setup plus one round trip).What changed
Lazy module loading, same pattern as upstream opencode#30453. Command modules keep light top levels (yargs definitions only) and dynamically import implementations inside their handlers.
KiloCli.bootstrap/shutdowninkilocode/cli/setup.tsdynamically import telemetry, the gateway, AppRuntime, config, auth, session-export, and the migration instead of loading them at module evaluation. All shared-file changes stay behindkilocode_changemarkers, and upstream's own import structure is untouched so future merges are unaffected.profile.tsalso no longer constructs the auth runtime at module load, and the daemon/console commands share one lazy resolve-and-warn network helper.Telemetry identity cache. The email resolved from the auth token is cached in
telemetry-profile.jsonunder the telemetry data path, keyed by a SHA-256 hash of the token (never the raw token), written with 0600 permissions. Repeat invocations skip the profile request entirely; a token change refetches, and entries older than a week are used for the current run but refreshed in the background.Background telemetry flush. After
trackCliStart, a flush is scheduled on a 300ms unref'd timer. Commands that outlive it upload while they run, so the shutdown flush usually finds an empty queue or a warm connection (~550ms down to ~150ms). Commands that exit faster keep today's single boundedshutdown(2000)flush, so no events are lost and the unreachable-endpoint cap from #9788 still applies.Measured impact
All numbers from the same machine (macOS arm64, EU network), A/B against
mainin the same session viabun devsource runs (n=25 unless noted).End to end:
kilo --versionkilo --helpkilo daemon statusComponent level:
--versionCPU profile)kilocode/cli/setup.tsimport costTelemetry.updateIdentityper bootstrapTelemetry.shutdown(commands >= ~1s)Telemetry.shutdown(instant commands)The remaining gap to upstream for informational commands is the bootstrap/telemetry lifecycle itself, which #12659 skips independently; the two changes compose, and whichever lands second only needs a small rebase on the
bootstrap()signature.Closes #10242. Scope note: this PR addresses the Kilo-specific contributors the profiling identified (the issue's prioritization ask). The flag-gated startup profiler from the issue's acceptance criteria is intentionally not part of this change and should be tracked as follow-up tooling work.