Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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.

28 changes: 28 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 @@ -1570,6 +1578,26 @@ 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 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,
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 () => {
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