s01
CLI 启动与初始化
基础设施层并行预取与启动性能优化
MDM + Keychain + GrowthBook 在 import 之前并行触发利用 ES 模块顶层副作用,在 ~135ms 的模块求值期间并行完成 I/O 操作
并行预取与启动性能优化
MDM + Keychain + GrowthBook 在 import 之前并行触发利用 ES 模块顶层副作用,在 ~135ms 的模块求值期间并行完成 I/O 操作
Claude Code 是一个 512K+ 行代码的重量级 CLI 工具,加载 ~135ms 的模块依赖。如果不做优化,启动时间会被 MDM 配置读取(plutil/reg query 子进程)、macOS Keychain 读取(OAuth + API Key)、GrowthBook 初始化等 I/O 操作拖慢。启动性能直接影响用户体验——每次敲 claude 都要等几秒是不可接受的。
顶层副作用并行预取 — 在其他 import 之前就触发 I/O 操作
// These side-effects must run before all other imports:
// 1. profileCheckpoint marks entry before heavy module evaluation begins
// 2. startMdmRawRead fires MDM subprocesses (plutil/reg query) so they run in
// parallel with the remaining ~135ms of imports below
// 3. startKeychainPrefetch fires both macOS keychain reads (OAuth + legacy API
// key) in parallel
import { profileCheckpoint } from './utils/startupProfiler.js';
profileCheckpoint('main_tsx_entry');
import { startMdmRawRead } from './utils/settings/mdm/rawRead.js';
startMdmRawRead();
import { startKeychainPrefetch } from './utils/secureStorage/keychainPrefetch.js';
startKeychainPrefetch();Feature Flag 死代码消除 — bun:bundle 在构建时移除未启用功能的代码
import { feature } from 'bun:bundle';
// Dead code elimination: conditional import for COORDINATOR_MODE
const coordinatorModeModule = feature('COORDINATOR_MODE')
? require('./coordinator/coordinatorMode.js')
: null;
// Dead code elimination: conditional import for KAIROS (assistant mode)
const assistantModule = feature('KAIROS')
? require('./assistant/index.js')
: null;工具的条件加载 — 未启用的工具在 bundle 中完全不存在
const SleepTool = feature('PROACTIVE') || feature('KAIROS')
? require('./tools/SleepTool/SleepTool.js').SleepTool
: null;
const cronTools = feature('AGENT_TRIGGERS')
? [
require('./tools/ScheduleCronTool/CronCreateTool.js').CronCreateTool,
require('./tools/ScheduleCronTool/CronDeleteTool.js').CronDeleteTool,
require('./tools/ScheduleCronTool/CronListTool.js').CronListTool,
]
: [];