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

1 change: 1 addition & 0 deletions packages/cli/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,7 @@ export async function loadCliConfig(
showResponseTokensPerSecond:
settings.ui?.showResponseTokensPerSecond === true,
telemetry: telemetrySettings,
deferTelemetryInitialization: interactive && !isAcpMode,
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