Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
369 changes: 369 additions & 0 deletions docs/design/performance/fire-and-forget-startup-prefetch.md

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions packages/cli/src/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const mockSessionServiceInstance = vi.hoisted(() => ({
const mockSessionServiceCtor = vi.hoisted(() =>
vi.fn(() => mockSessionServiceInstance),
);
const mockConfigConstructorParams = vi.hoisted(() => vi.fn());

vi.mock('../utils/stdioHelpers.js', () => ({
writeStderrLine: mockWriteStderrLine,
Expand Down Expand Up @@ -160,8 +161,15 @@ vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => {
SkillManagerMock.prototype.listSkills = vi.fn().mockResolvedValue([]);
SkillManagerMock.prototype.addChangeListener = vi.fn();
SkillManagerMock.prototype.removeChangeListener = vi.fn();
class ConfigWithParamCapture extends actualServer.Config {
constructor(...args: ConstructorParameters<typeof actualServer.Config>) {
mockConfigConstructorParams(args[0]);
super(...args);
}
}
return {
...actualServer,
Config: ConfigWithParamCapture,
NativeLspService: vi
.fn()
.mockImplementation(() => createNativeLspServiceInstance()),
Expand Down Expand Up @@ -1541,6 +1549,7 @@ describe('loadCliConfig', () => {

describe('loadCliConfig telemetry', () => {
const originalArgv = process.argv;
const originalIsTTY = process.stdin.isTTY;

beforeEach(() => {
vi.resetAllMocks();
Expand All @@ -1550,6 +1559,7 @@ describe('loadCliConfig telemetry', () => {

afterEach(() => {
process.argv = originalArgv;
process.stdin.isTTY = originalIsTTY;
vi.unstubAllEnvs();
vi.restoreAllMocks();
});
Expand All @@ -1570,6 +1580,41 @@ describe('loadCliConfig telemetry', () => {
expect(config.getTelemetryEnabled()).toBe(true);
});

it('should initialize telemetry before prompt-interactive startup', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This test verifies deferTelemetryInitialization: false for prompt-interactive, but there is no corresponding positive test verifying deferTelemetryInitialization: true for the ordinary interactive case (no -p, no -i "prompt", not ACP). The positive path is the primary motivation for the feature — deferring telemetry for ordinary TUI startup — yet it has no test. A regression that accidentally always sets false would go undetected.

Consider adding a test with process.argv = ['node', 'script.js', '-i'] (interactive, no question) that asserts deferTelemetryInitialization: true.

— qwen3.7-max via Qwen Code /review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 6cba2c578.

Added a positive loadCliConfig test for ordinary interactive startup that sets process.stdin.isTTY = true, uses no initial prompt, and asserts deferTelemetryInitialization: true is passed to Config.

process.argv = [
'node',
'script.js',
'-i',
'hello from prompt-interactive',
'--telemetry',
];
const argv = await parseArguments();

await loadCliConfig({}, argv);

expect(mockConfigConstructorParams).toHaveBeenCalledWith(
expect.objectContaining({
question: 'hello from prompt-interactive',
deferTelemetryInitialization: false,
}),
);
});

it('should defer telemetry for ordinary interactive startup', async () => {
process.argv = ['node', 'script.js', '--telemetry'];
process.stdin.isTTY = true;
const argv = await parseArguments();

await loadCliConfig({}, argv);

expect(mockConfigConstructorParams).toHaveBeenCalledWith(
expect.objectContaining({
question: '',
deferTelemetryInitialization: true,
}),
);
});

it('should set telemetry to false when --no-telemetry flag is present', async () => {
process.argv = ['node', 'script.js', '--no-telemetry'];
const argv = await parseArguments();
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,9 @@ export async function loadCliConfig(
showResponseTokensPerSecond:
settings.ui?.showResponseTokensPerSecond === true,
telemetry: telemetrySettings,
// Prompt-interactive (`qwen -i "prompt"`) auto-submits its first prompt,
// so telemetry must be ready before that request is sent.
deferTelemetryInitialization: interactive && !isAcpMode && !question,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Two accepted tradeoffs converge at this line, and it would help future readers to document both:

  1. Auth telemetry gap: auth-related events emitted between performInitialAuth() (which runs eagerly in initializeApp) and the deferred initializeTelemetry() call are no-ops. This is acknowledged in the PR risk section.
  2. Asymmetric deferral conditions: deferIdeConnection (in gemini.tsx) considers question (prompt-interactive doesn't defer IDE), but deferTelemetryInitialization here does not check question. The design rationale is sound — prompt-interactive needs IDE context for the auto-submitted prompt but doesn't need telemetry before the first request — but a cross-reference comment would prevent future maintainers from "fixing" the asymmetry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 95c5f6c03.

I documented the accepted telemetry deferral tradeoffs at the deferTelemetryInitialization decision point: auth events before deferred telemetry init may be no-ops, and the telemetry condition is intentionally not identical to IDE deferral because prompt-interactive needs IDE context before auto-submit while telemetry can remain post-render unless an initial prompt is present.

outboundCorrelation: settings.outboundCorrelation,
usageStatisticsEnabled: settings.privacy?.usageStatisticsEnabled ?? true,
clearContextOnIdle: settings.context?.clearContextOnIdle,
Expand Down
26 changes: 24 additions & 2 deletions packages/cli/src/core/initializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { initializeApp } from './initializer.js';
import { connectIdeForStartup, initializeApp } from './initializer.js';

const mockPerformInitialAuth = vi.fn();
const mockValidateTheme = vi.fn();
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('initializeApp', () => {
expect(result.shouldOpenAuthDialog).toBe(false);
});

it('should connect to IDE when in IDE mode', async () => {
it('should connect to IDE by default when in IDE mode', async () => {
mockConfig.getIdeMode.mockReturnValue(true);

await initializeApp(mockConfig as never, mockSettings as never);
Expand All @@ -174,6 +174,28 @@ describe('initializeApp', () => {
expect(mockLogIdeConnection).toHaveBeenCalled();
});

it('should not connect to IDE when deferred', async () => {
mockConfig.getIdeMode.mockReturnValue(true);

await initializeApp(mockConfig as never, mockSettings as never, {
deferIdeConnection: true,
});

expect(mockGetInstance).not.toHaveBeenCalled();
expect(mockConnect).not.toHaveBeenCalled();
expect(mockLogIdeConnection).not.toHaveBeenCalled();
});

it('should connect to IDE through startup helper', async () => {
mockConfig.getIdeMode.mockReturnValue(true);

await connectIdeForStartup(mockConfig as never);

expect(mockGetInstance).toHaveBeenCalled();
expect(mockConnect).toHaveBeenCalled();
expect(mockLogIdeConnection).toHaveBeenCalled();
});

it('should not connect to IDE when not in IDE mode', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] connectIdeForStartup is tested at line 189 with getIdeMode()=true, but the early-return path (getIdeMode()=false → skip connection) is only tested indirectly through initializeApp at line 200. A dedicated unit test calling connectIdeForStartup(mockConfig) with getIdeMode()=false and asserting no IDE client calls would better document the contract and catch regressions if the guard condition changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 95c5f6c03.

I added a direct connectIdeForStartup() early-return test for getIdeMode() === false, asserting that IdeClient.getInstance(), connect(), and logIdeConnection() are not called. This locks the shared helper contract directly instead of only covering it through initializeApp().

mockConfig.getIdeMode.mockReturnValue(false);

Expand Down
36 changes: 28 additions & 8 deletions packages/cli/src/core/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import {
import { type LoadedSettings } from '../config/settings.js';
import { performInitialAuth } from './auth.js';
import { validateTheme } from './theme.js';
import {
initializeI18n,
resolveLanguageSetting,
} from '../i18n/index.js';
import { initializeI18n, resolveLanguageSetting } from '../i18n/index.js';

export interface InitializationResult {
authError: string | null;
Expand All @@ -26,6 +23,30 @@ export interface InitializationResult {
geminiMdFileCount: number;
}

export interface InitializeAppOptions {
/**
* When true, skip the awaited IDE connection inside initializeApp().
* Ordinary interactive TUI startup uses this so IDE IPC can run after first
* paint; non-TUI paths leave it false so the first request keeps IDE context.
*/
deferIdeConnection?: boolean;
}

/**
* Establishes the startup IDE connection and records the connection telemetry.
*
* Callers choose whether to await this on the startup critical path. Headless,
* stream-json, and ACP/Zed await it before their first request; ordinary TUI
* startup schedules it post-render through startup prefetch.
*/
export async function connectIdeForStartup(config: Config): Promise<void> {
if (!config.getIdeMode()) return;

const ideClient = await IdeClient.getInstance();
await ideClient.connect();
logIdeConnection(config, new IdeConnectionEvent(IdeConnectionType.START));
}

/**
* Orchestrates the application's startup initialization.
* This runs BEFORE the React UI is rendered.
Expand All @@ -36,6 +57,7 @@ export interface InitializationResult {
export async function initializeApp(
config: Config,
settings: LoadedSettings,
options: InitializeAppOptions = {},
): Promise<InitializationResult> {
// Initialize i18n system
await initializeI18n(
Expand All @@ -52,10 +74,8 @@ export async function initializeApp(
const shouldOpenAuthDialog =
!config.getModelsConfig().wasAuthTypeExplicitlyProvided() || !!authError;

if (config.getIdeMode()) {
const ideClient = await IdeClient.getInstance();
await ideClient.connect();
logIdeConnection(config, new IdeConnectionEvent(IdeConnectionType.START));
if (!options.deferIdeConnection) {
await connectIdeForStartup(config);
}

return {
Expand Down
Loading
Loading