perf: optimize session loading and desktop startup (#2545)
* perf: optimize session loading and startup * fix(chat): stabilize history prepend virtualization * perf: unblock first session open from startup network contention Opening the first session after app start waited seconds for its message fetch. Three independent contributors, each measured via CDP network capture and Chromium net-log against the packaged desktop app: - The active-session watchdog fired an uncapped per-directory status poll and child-session discovery burst at startup, and other subsystems (git checks, global session pages, command/skill discovery) fanned out alongside it, saturating the browser's ~6 HTTP/1.1 sockets per origin. Add a shared background-network gate (concurrency 3) and route the watchdog, poll-shaped git reads (also priority: low), global session pages, command/skill loads, and the background update check through it. - The packaged renderer is cross-origin to the loopback backend, so every API call needs a CORS preflight; a few slow OpenCode-proxied requests held the whole pool while preflights and interactive traffic queued behind them. Lift Chromium's per-host connection cap for loopback via ignore-connections-limit in the Electron shell. - OpenCode initializes each directory lazily on its first request, so the first click paid that cost interactively. Warm the last-used directory and the three most recently opened projects right after OpenCode readiness, sequentially and best-effort, overlapping UI startup. Validation: new background-network tests, lifecycle warmup test, focused store/sync tests, UI type-check and lint, dead-code report, node --check plus electron type-check/lint, and CDP first-open measurements on the packaged app (message fetch socket queue 5.4s -> 0.03s). * fix(ui): keep interactive git reads out of background queue --------- Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
This commit is contained in:
committed by
GitHub
co-authored by
Bohdan Triapitsyn
parent
09f0c64839
commit
aae889b904
@@ -1,6 +1,7 @@
|
||||
import { spawn, spawnSync } from 'node:child_process';
|
||||
import net from 'node:net';
|
||||
import { registerManagedProcess, unregisterManagedProcess, reapOrphanedProcesses } from './managed-process-registry.js';
|
||||
import { recordStartupPerformance } from './startup-performance.js';
|
||||
|
||||
const parsePositiveInt = (value, fallback) => {
|
||||
const parsed = Number.parseInt(String(value ?? ''), 10);
|
||||
@@ -15,6 +16,10 @@ const HEALTH_CHECK_MAX_CONSECUTIVE_FAILURES = parsePositiveInt(
|
||||
const HEALTH_CHECK_INTERVAL_OVERRIDE_MS = parsePositiveInt(process.env.OPENCHAMBER_OPENCODE_HEALTH_INTERVAL_MS, 0);
|
||||
const HEALTH_CHECK_RESULT_CACHE_MS = parsePositiveInt(process.env.OPENCHAMBER_OPENCODE_HEALTH_CACHE_MS, 750);
|
||||
const OPENCODE_HEALTH_PATH = '/global/health';
|
||||
// Last-used directory plus the three most recently opened projects — deeper
|
||||
// tails are unlikely to be the user's first click and just add background work.
|
||||
const WARMUP_DIRECTORY_LIMIT = 4;
|
||||
const WARMUP_REQUEST_TIMEOUT_MS = 30000;
|
||||
|
||||
export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
const {
|
||||
@@ -40,6 +45,8 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
getManagedOpenCodeShellEnvSnapshot,
|
||||
getManagedOpenCodeEnv = async () => ({}),
|
||||
getActiveSessionCount = () => 0,
|
||||
reapManagedOrphanedProcesses = reapOrphanedProcesses,
|
||||
getWarmupDirectories = async () => [],
|
||||
now = Date.now,
|
||||
} = deps;
|
||||
|
||||
@@ -466,7 +473,10 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
|
||||
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
const startOpenCodeOnce = async () => {
|
||||
const startOpenCodeOnce = async (attempt) => {
|
||||
const attemptStartedAt = performance.now();
|
||||
let phaseStartedAt = attemptStartedAt;
|
||||
recordStartupPerformance('opencode.attempt.start', { attempt });
|
||||
const desiredPort = env.ENV_CONFIGURED_OPENCODE_PORT ?? 0;
|
||||
const spawnPort = await resolveManagedOpenCodePort(desiredPort, env.ENV_CONFIGURED_OPENCODE_HOSTNAME);
|
||||
console.log(
|
||||
@@ -477,6 +487,12 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
|
||||
await applyOpencodeBinaryFromSettings({ strict: true });
|
||||
ensureOpencodeCliEnv();
|
||||
recordStartupPerformance('opencode.binary.ready', {
|
||||
attempt,
|
||||
durationMs: performance.now() - phaseStartedAt,
|
||||
totalDurationMs: performance.now() - attemptStartedAt,
|
||||
});
|
||||
phaseStartedAt = performance.now();
|
||||
const openCodePassword = await ensureLocalOpenCodeServerPassword({ rotateManaged: true });
|
||||
let envPath = process.env.PATH;
|
||||
if (typeof buildManagedOpenCodePath === 'function') {
|
||||
@@ -488,6 +504,12 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
? getManagedOpenCodeShellEnvSnapshot() || {}
|
||||
: {};
|
||||
const managedOpenCodeEnv = await getManagedOpenCodeEnv();
|
||||
recordStartupPerformance('opencode.environment.ready', {
|
||||
attempt,
|
||||
durationMs: performance.now() - phaseStartedAt,
|
||||
totalDurationMs: performance.now() - attemptStartedAt,
|
||||
});
|
||||
phaseStartedAt = performance.now();
|
||||
|
||||
try {
|
||||
const serverInstance = await createManagedOpenCodeServerProcess({
|
||||
@@ -508,6 +530,12 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
if (!serverInstance || !serverInstance.url) {
|
||||
throw new Error('OpenCode server started but URL is missing');
|
||||
}
|
||||
recordStartupPerformance('opencode.process.ready', {
|
||||
attempt,
|
||||
durationMs: performance.now() - phaseStartedAt,
|
||||
totalDurationMs: performance.now() - attemptStartedAt,
|
||||
});
|
||||
phaseStartedAt = performance.now();
|
||||
|
||||
const url = new URL(serverInstance.url);
|
||||
const port = parseInt(url.port, 10);
|
||||
@@ -521,6 +549,13 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
state.lastOpenCodeError = null;
|
||||
state.openCodeNotReadySince = 0;
|
||||
|
||||
recordStartupPerformance('opencode.health.ready', {
|
||||
attempt,
|
||||
durationMs: performance.now() - phaseStartedAt,
|
||||
totalDurationMs: performance.now() - attemptStartedAt,
|
||||
outcome: 'ready',
|
||||
});
|
||||
|
||||
return serverInstance;
|
||||
}
|
||||
|
||||
@@ -534,6 +569,11 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
state.lastOpenCodeError = message;
|
||||
state.openCodePort = null;
|
||||
syncToHmrState();
|
||||
recordStartupPerformance('opencode.attempt.error', {
|
||||
attempt,
|
||||
totalDurationMs: performance.now() - attemptStartedAt,
|
||||
outcome: 'error',
|
||||
});
|
||||
console.error(`Failed to start OpenCode: ${message}`);
|
||||
throw error;
|
||||
}
|
||||
@@ -543,7 +583,7 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
let lastError = null;
|
||||
for (let attempt = 1; attempt <= START_OPEN_CODE_MAX_ATTEMPTS; attempt += 1) {
|
||||
try {
|
||||
return await startOpenCodeOnce();
|
||||
return await startOpenCodeOnce(attempt);
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (error?.code === 'OPENCODE_BINARY_INVALID') {
|
||||
@@ -792,12 +832,20 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
};
|
||||
|
||||
const bootstrapOpenCodeAtStartup = async () => {
|
||||
const bootstrapStartedAt = performance.now();
|
||||
let bootstrapError = null;
|
||||
recordStartupPerformance('opencode.bootstrap.start');
|
||||
try {
|
||||
// Before doing anything, reap any OpenCode process WE spawned in a prior
|
||||
// run that was orphaned by a crash/hard-exit. Verified + scoped to our own
|
||||
// pids, so it never touches a live instance's or the user's own server.
|
||||
try {
|
||||
const { reaped } = await reapOrphanedProcesses({ log: (msg) => console.log(msg) });
|
||||
const orphanReapStartedAt = performance.now();
|
||||
const { reaped } = await reapManagedOrphanedProcesses({ log: (msg) => console.log(msg) });
|
||||
recordStartupPerformance('opencode.orphan-reap.ready', {
|
||||
durationMs: performance.now() - orphanReapStartedAt,
|
||||
totalDurationMs: performance.now() - bootstrapStartedAt,
|
||||
});
|
||||
if (reaped > 0) console.log(`[lifecycle] startup reaped ${reaped} orphaned OpenCode process(es)`);
|
||||
} catch (error) {
|
||||
console.warn('[lifecycle] orphan reap failed:', error?.message ?? error);
|
||||
@@ -851,13 +899,64 @@ export const createOpenCodeLifecycleRuntime = (deps) => {
|
||||
try {
|
||||
await waitForOpenCodeReady();
|
||||
} catch (error) {
|
||||
bootstrapError = error;
|
||||
console.error(`OpenCode readiness check failed: ${error.message}`);
|
||||
}
|
||||
} catch (error) {
|
||||
bootstrapError = error;
|
||||
console.error(`Failed to start OpenCode: ${error.message}`);
|
||||
console.log('Continuing without OpenCode integration...');
|
||||
state.lastOpenCodeError = error.message;
|
||||
}
|
||||
recordStartupPerformance(
|
||||
bootstrapError ? 'opencode.bootstrap.error' : 'opencode.bootstrap.ready',
|
||||
{
|
||||
totalDurationMs: performance.now() - bootstrapStartedAt,
|
||||
outcome: bootstrapError ? 'error' : 'ready',
|
||||
},
|
||||
);
|
||||
if (!bootstrapError) {
|
||||
void warmOpenCodeDirectories();
|
||||
}
|
||||
};
|
||||
|
||||
// OpenCode initializes each project directory lazily on its first
|
||||
// directory-scoped request, and that initialization takes seconds on large
|
||||
// session stores. Without warming, the user's first session open pays it
|
||||
// interactively (the chat waits on the message fetch until the directory
|
||||
// finishes initializing). Warm the most recently used directories right
|
||||
// after readiness so the work overlaps UI startup instead. Sequential and
|
||||
// best-effort: a failed or slow directory never blocks the others for long,
|
||||
// and a restart invalidates the pass via the port/readiness guard.
|
||||
const warmOpenCodeDirectories = async () => {
|
||||
let directories = [];
|
||||
try {
|
||||
directories = await getWarmupDirectories();
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (!Array.isArray(directories) || directories.length === 0) return;
|
||||
|
||||
const warmedPort = state.openCodePort;
|
||||
for (const directory of directories.slice(0, WARMUP_DIRECTORY_LIMIT)) {
|
||||
if (typeof directory !== 'string' || !directory) continue;
|
||||
if (!state.isOpenCodeReady || state.openCodePort !== warmedPort) return;
|
||||
let timeout = null;
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
timeout = setTimeout(() => controller.abort(), WARMUP_REQUEST_TIMEOUT_MS);
|
||||
const url = `${buildOpenCodeUrl('/session/status', '')}?directory=${encodeURIComponent(directory)}`;
|
||||
await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: { Accept: 'application/json', ...getOpenCodeAuthHeaders() },
|
||||
signal: controller.signal,
|
||||
});
|
||||
} catch {
|
||||
// Best-effort — the directory stays lazy and the UI's own request warms it.
|
||||
} finally {
|
||||
if (timeout) clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user