perf(cli): parallelize and background startup cleanup tasks (#23545)

This commit is contained in:
Sehoon Shon
2026-03-23 12:26:56 -04:00
committed by GitHub
parent ec0161ad37
commit 517961b2eb
+31 -15
View File
@@ -213,12 +213,36 @@ export async function main() {
loadSettingsHandle?.end(); loadSettingsHandle?.end();
// If a worktree is requested and enabled, set it up early. // If a worktree is requested and enabled, set it up early.
// This must be awaited before any other async tasks that depend on CWD (like loadCliConfig)
// because setupWorktree calls process.chdir().
const requestedWorktree = cliConfig.getRequestedWorktreeName(settings); const requestedWorktree = cliConfig.getRequestedWorktreeName(settings);
let worktreeInfo: WorktreeInfo | undefined; let worktreeInfo: WorktreeInfo | undefined;
if (requestedWorktree !== undefined) { if (requestedWorktree !== undefined) {
const worktreeHandle = startupProfiler.start('setup_worktree');
worktreeInfo = await setupWorktree(requestedWorktree || undefined); worktreeInfo = await setupWorktree(requestedWorktree || undefined);
worktreeHandle?.end();
} }
const cleanupOpsHandle = startupProfiler.start('cleanup_ops');
Promise.all([
cleanupCheckpoints(),
cleanupToolOutputFiles(settings.merged),
cleanupBackgroundLogs(),
])
.catch((e) => {
debugLogger.error('Early cleanup failed:', e);
})
.finally(() => {
cleanupOpsHandle?.end();
});
const parseArgsHandle = startupProfiler.start('parse_arguments');
const argvPromise = parseArguments(settings.merged).finally(() => {
parseArgsHandle?.end();
});
const rawStartupWarningsPromise = getStartupWarnings();
// Report settings errors once during startup // Report settings errors once during startup
settings.errors.forEach((error) => { settings.errors.forEach((error) => {
coreEvents.emitFeedback('warning', error.message); coreEvents.emitFeedback('warning', error.message);
@@ -232,15 +256,7 @@ export async function main() {
); );
}); });
await Promise.all([ const argv = await argvPromise;
cleanupCheckpoints(),
cleanupToolOutputFiles(settings.merged),
cleanupBackgroundLogs(),
]);
const parseArgsHandle = startupProfiler.start('parse_arguments');
const argv = await parseArguments(settings.merged);
parseArgsHandle?.end();
if ( if (
(argv.allowedTools && argv.allowedTools.length > 0) || (argv.allowedTools && argv.allowedTools.length > 0) ||
@@ -467,12 +483,10 @@ export async function main() {
await config.getHookSystem()?.fireSessionEndEvent(SessionEndReason.Exit); await config.getHookSystem()?.fireSessionEndEvent(SessionEndReason.Exit);
}); });
// Cleanup sessions after config initialization // Launch cleanup expired sessions as a background task
try { cleanupExpiredSessions(config, settings.merged).catch((e) => {
await cleanupExpiredSessions(config, settings.merged);
} catch (e) {
debugLogger.error('Failed to cleanup expired sessions:', e); debugLogger.error('Failed to cleanup expired sessions:', e);
} });
if (config.getListExtensions()) { if (config.getListExtensions()) {
debugLogger.log('Installed extensions:'); debugLogger.log('Installed extensions:');
@@ -524,7 +538,9 @@ export async function main() {
}); });
} }
const terminalHandle = startupProfiler.start('setup_terminal');
await setupTerminalAndTheme(config, settings); await setupTerminalAndTheme(config, settings);
terminalHandle?.end();
const initAppHandle = startupProfiler.start('initialize_app'); const initAppHandle = startupProfiler.start('initialize_app');
const initializationResult = await initializeApp(config, settings); const initializationResult = await initializeApp(config, settings);
@@ -548,7 +564,7 @@ export async function main() {
isAlternateBufferEnabled(config), isAlternateBufferEnabled(config),
config.getScreenReader(), config.getScreenReader(),
); );
const rawStartupWarnings = await getStartupWarnings(); const rawStartupWarnings = await rawStartupWarningsPromise;
const startupWarnings: StartupWarning[] = [ const startupWarnings: StartupWarning[] = [
...rawStartupWarnings.map((message) => ({ ...rawStartupWarnings.map((message) => ({
id: `startup-${createHash('sha256').update(message).digest('hex').substring(0, 16)}`, id: `startup-${createHash('sha256').update(message).digest('hex').substring(0, 16)}`,