-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(cli): reduce startup time by deferring Kilo module loading and telemetry work #12682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@kilocode/cli": patch | ||
| "@kilocode/kilo-telemetry": patch | ||
| --- | ||
|
|
||
| Reduce CLI startup time by deferring Kilo-specific module loading until commands actually run, caching the telemetry profile lookup across invocations, and uploading telemetry in the background so process exit is not delayed by a network round trip |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { mkdtempSync, readFileSync, existsSync, statSync } from "node:fs" | ||
| import { tmpdir } from "node:os" | ||
| import path from "node:path" | ||
| import { describe, test, expect, beforeEach, mock, afterEach } from "bun:test" | ||
| import { createHash } from "node:crypto" | ||
|
|
||
| let profileCalls = 0 | ||
| mock.module("@kilocode/kilo-gateway", () => ({ | ||
| fetchProfile: async (token: string) => { | ||
| profileCalls++ | ||
| if (token === "bad-token") return null | ||
| return { email: `user-${token}@example.com` } | ||
| }, | ||
| })) | ||
|
|
||
| const { Identity } = await import("../identity.js") | ||
|
|
||
| function digest(token: string) { | ||
| return createHash("sha256").update(token).digest("hex") | ||
| } | ||
|
|
||
| let dir: string | ||
|
|
||
| beforeEach(() => { | ||
| profileCalls = 0 | ||
| dir = mkdtempSync(path.join(tmpdir(), "kilo-telemetry-identity-")) | ||
| Identity.reset() | ||
| Identity.setDataPath(dir) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| Identity.setDataPath("") | ||
| }) | ||
|
|
||
| describe("Identity.updateFromKiloAuth profile cache", () => { | ||
| test("fetches profile and writes cache keyed by token hash", async () => { | ||
| await Identity.updateFromKiloAuth("token-a") | ||
| expect(Identity.getUserId()).toBe("user-token-a@example.com") | ||
| expect(profileCalls).toBe(1) | ||
|
|
||
| const file = path.join(dir, "telemetry-profile.json") | ||
| expect(existsSync(file)).toBe(true) | ||
| const cache = JSON.parse(readFileSync(file, "utf8")) | ||
| expect(cache.token).toBe(digest("token-a")) | ||
| expect(cache.email).toBe("user-token-a@example.com") | ||
| expect(cache.token).not.toBe("token-a") | ||
| // The cache stores an email and a token verifier, so it must be owner-only. | ||
| // POSIX only: Windows reports default mode bits and enforces access via ACLs. | ||
| if (process.platform !== "win32") expect(statSync(file).mode & 0o777).toBe(0o600) | ||
| }) | ||
|
|
||
| test("uses cached email without a network request on later invocations", async () => { | ||
| await Identity.updateFromKiloAuth("token-a") | ||
| expect(profileCalls).toBe(1) | ||
|
|
||
| // Simulate a fresh process: identity state resets, cache file persists. | ||
| Identity.reset() | ||
| await Identity.updateFromKiloAuth("token-a") | ||
| expect(Identity.getUserId()).toBe("user-token-a@example.com") | ||
| expect(profileCalls).toBe(1) | ||
| }) | ||
|
|
||
| test("refetches when the token changes", async () => { | ||
| await Identity.updateFromKiloAuth("token-a") | ||
| Identity.reset() | ||
| await Identity.updateFromKiloAuth("token-b") | ||
| expect(Identity.getUserId()).toBe("user-token-b@example.com") | ||
| expect(profileCalls).toBe(2) | ||
| }) | ||
|
|
||
| test("clears identity when token is null", async () => { | ||
| await Identity.updateFromKiloAuth("token-a") | ||
| Identity.reset() | ||
| await Identity.updateFromKiloAuth(null) | ||
| expect(Identity.getUserId()).toBeNull() | ||
| expect(profileCalls).toBe(1) | ||
| }) | ||
|
|
||
| test("ignores a cache file for a different token", async () => { | ||
| await Identity.updateFromKiloAuth("token-a") | ||
| const file = path.join(dir, "telemetry-profile.json") | ||
| const cache = JSON.parse(readFileSync(file, "utf8")) | ||
| expect(cache.token).toBe(digest("token-a")) | ||
|
|
||
| Identity.reset() | ||
| await Identity.updateFromKiloAuth("token-b") | ||
| expect(Identity.getUserId()).toBe("user-token-b@example.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
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
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.