From c035c5c0d23525aa93c3cea25a051399bdf68d7b Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 18 Apr 2026 16:18:20 +0100 Subject: [PATCH] refactor: cache lazy runtime imports --- src/commands/models/list.status-command.ts | 27 +++++++++++++++++++--- src/plugin-sdk/browser-maintenance.ts | 18 +++++++++++++-- src/process/supervisor/supervisor.ts | 11 ++++++++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/commands/models/list.status-command.ts b/src/commands/models/list.status-command.ts index d7bbed2d16f..a665ffed09f 100644 --- a/src/commands/models/list.status-command.ts +++ b/src/commands/models/list.status-command.ts @@ -46,14 +46,35 @@ import { } from "./shared.js"; type ProviderUsageRuntime = typeof import("../../infra/provider-usage.js"); +type ProgressRuntime = typeof import("../../cli/progress.js"); +type TerminalTableRuntime = typeof import("../../terminal/table.js"); +type ListProbeRuntime = typeof import("./list.probe.js"); let providerUsageRuntimePromise: Promise | undefined; +let progressRuntimePromise: Promise | undefined; +let terminalTableRuntimePromise: Promise | undefined; +let listProbeRuntimePromise: Promise | undefined; function loadProviderUsageRuntime(): Promise { providerUsageRuntimePromise ??= import("../../infra/provider-usage.js"); return providerUsageRuntimePromise; } +function loadProgressRuntime(): Promise { + progressRuntimePromise ??= import("../../cli/progress.js"); + return progressRuntimePromise; +} + +function loadTerminalTableRuntime(): Promise { + terminalTableRuntimePromise ??= import("../../terminal/table.js"); + return terminalTableRuntimePromise; +} + +function loadListProbeRuntime(): Promise { + listProbeRuntimePromise ??= import("./list.probe.js"); + return listProbeRuntimePromise; +} + export async function modelsStatusCommand( opts: { json?: boolean; @@ -220,8 +241,8 @@ export async function modelsStatusCommand( let probeSummary: AuthProbeSummary | undefined; if (opts.probe) { const [{ withProgressTotals }, { runAuthProbes }] = await Promise.all([ - import("../../cli/progress.js"), - import("./list.probe.js"), + loadProgressRuntime(), + loadListProbeRuntime(), ]); probeSummary = await withProgressTotals( { label: "Probing auth profiles…", total: 1 }, @@ -613,7 +634,7 @@ export async function modelsStatusCommand( const [ { getTerminalTableWidth, renderTable }, { describeProbeSummary, formatProbeLatency, sortProbeResults }, - ] = await Promise.all([import("../../terminal/table.js"), import("./list.probe.js")]); + ] = await Promise.all([loadTerminalTableRuntime(), loadListProbeRuntime()]); runtime.log(""); runtime.log(colorize(rich, theme.heading, "Auth probes")); if (probeSummary.results.length === 0) { diff --git a/src/plugin-sdk/browser-maintenance.ts b/src/plugin-sdk/browser-maintenance.ts index ba2958e0b67..06bdd490269 100644 --- a/src/plugin-sdk/browser-maintenance.ts +++ b/src/plugin-sdk/browser-maintenance.ts @@ -12,8 +12,12 @@ type CloseTrackedBrowserTabsParams = { type BrowserMaintenanceSurface = { closeTrackedBrowserTabsForSessions: (params: CloseTrackedBrowserTabsParams) => Promise; }; +type SecureRandomRuntime = typeof import("../infra/secure-random.js"); +type ExecRuntime = typeof import("../process/exec.js"); let cachedBrowserMaintenanceSurface: BrowserMaintenanceSurface | undefined; +let secureRandomRuntimePromise: Promise | undefined; +let execRuntimePromise: Promise | undefined; function hasRequestedSessionKeys(sessionKeys: Array): boolean { return sessionKeys.some((key) => Boolean(key?.trim())); @@ -28,6 +32,16 @@ function loadBrowserMaintenanceSurface(): BrowserMaintenanceSurface { return cachedBrowserMaintenanceSurface; } +function loadSecureRandomRuntime(): Promise { + secureRandomRuntimePromise ??= import("../infra/secure-random.js"); + return secureRandomRuntimePromise; +} + +function loadExecRuntime(): Promise { + execRuntimePromise ??= import("../process/exec.js"); + return execRuntimePromise; +} + export async function closeTrackedBrowserTabsForSessions( params: CloseTrackedBrowserTabsParams, ): Promise { @@ -47,8 +61,8 @@ export async function closeTrackedBrowserTabsForSessions( export async function movePathToTrash(targetPath: string): Promise { const [{ generateSecureToken }, { runExec }] = await Promise.all([ - import("../infra/secure-random.js"), - import("../process/exec.js"), + loadSecureRandomRuntime(), + loadExecRuntime(), ]); try { await runExec("trash", [targetPath], { timeoutMs: 10_000 }); diff --git a/src/process/supervisor/supervisor.ts b/src/process/supervisor/supervisor.ts index 214deaa7a5d..a9f81f0b7c0 100644 --- a/src/process/supervisor/supervisor.ts +++ b/src/process/supervisor/supervisor.ts @@ -13,11 +13,20 @@ import type { TerminationReason, } from "./types.js"; +type SupervisorLogRuntime = typeof import("./supervisor-log.runtime.js"); + type ActiveRun = { run: ManagedRun; scopeKey?: string; }; +let supervisorLogRuntimePromise: Promise | undefined; + +function loadSupervisorLogRuntime(): Promise { + supervisorLogRuntimePromise ??= import("./supervisor-log.runtime.js"); + return supervisorLogRuntimePromise; +} + function clampTimeout(value?: number): number | undefined { if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) { return undefined; @@ -263,7 +272,7 @@ export function createProcessSupervisor(): ProcessSupervisor { exitCode: null, exitSignal: null, }); - const { warnProcessSupervisorSpawnFailure } = await import("./supervisor-log.runtime.js"); + const { warnProcessSupervisorSpawnFailure } = await loadSupervisorLogRuntime(); warnProcessSupervisorSpawnFailure(`spawn failed: runId=${runId} reason=${String(err)}`); throw err; }