Files
orca/tests/tools/benchmarks/startup-bench-state-fixture.mjs
Neil b00ec20731 perf(startup): stop an unreachable SSH host from gating local terminal restore (#18164)
* perf(startup): stop an unreachable SSH host from gating local terminal restore

An asleep or unreachable SSH target held the terminal-restoration gate for the
full 15s reconnect timeout, so no terminal restored — local ones included.
Startup now awaits only the target that owns the active workspace's tabs and
lets the rest connect in the background, folded into the existing deferred path
that reattaches their PTYs on tab focus.

Also splits the renderer's git-environment fence out of the first-window PTY
services barrier: worktree hydration needs shell-PATH generation and the managed
WSL CLI registration, not a daemon PTY spawn or a hook-server bind. Terminal
restoration still fences on the first-window services via
app:prepareTerminalStartupRestoration.

Measured with tests/tools/benchmarks/startup-time-bench.mjs (382 restored tabs,
28k-file profile, medians of 3):
  unreachable SSH host: 17.27s -> 1.34s to renderer-startup-hydration-done
  all-local:             1.98s -> 1.33s

* fix(startup): restore the startup-ordering oracle and keep a connected background SSH target undeferred

app-startup-routing.test.ts pinned the old step names, so the two ordering cases
went vacuous-then-red when the barrier split. Repoint them at the steps that now
carry the same fences: 'git-environment-barrier-await' (shell PATH + managed WSL,
the fence host Git needs) before hydration worktrees, and
'prepare-terminal-startup-restoration' (which awaits firstWindowStartupServicesReady
in main) before terminal reconnect. Both still fail against main's hydration source.

Also: the timed-out-eager rewrite of the deferred list re-added background targets
that had already connected, undoing removeDeferredSshReconnectTarget and sending
fresh panes on a reachable host down the cold-restore path.
2026-09-02 13:10:02 -07:00

194 lines
5.6 KiB
JavaScript

/**
* Persisted-state fixtures for the startup benchmark: the git repos, GitHub
* remotes, restored terminal tabs, and unreachable SSH targets that `orca-data.json`
* must contain for a run to exercise the corresponding startup path.
*/
import { spawnSync } from 'node:child_process'
import { existsSync, mkdirSync, realpathSync, unlinkSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
function initFixtureGitRepo(repoDir) {
mkdirSync(repoDir, { recursive: true })
if (!existsSync(join(repoDir, '.git'))) {
const init = spawnSync('git', ['init', repoDir], { stdio: 'ignore' })
if (init.status !== 0) {
throw new Error(`Failed to create git repo fixture at ${repoDir}`)
}
}
return realpathSync(repoDir)
}
/**
* Seed repos whose hydration reaches the `gh` login probe: a GitHub `origin`
* remote and no github.user/user.username config (the bench also points
* GIT_CONFIG_GLOBAL away from the developer's real config at launch).
*/
function buildGithubRepoFixtures(fixtureDir, githubRepos) {
const repos = []
for (let i = 0; i < githubRepos; i++) {
const repoPath = initFixtureGitRepo(join(fixtureDir, `bench-gh-repo-${i}`))
const remote = spawnSync(
'git',
[
'-C',
repoPath,
'remote',
'add',
'origin',
`https://github.com/orca-bench/bench-gh-repo-${i}.git`
],
{ stdio: 'ignore' }
)
// Exit 3 (remote exists) is fine on fixture reuse; anything else is not.
if (remote.status !== 0 && remote.status !== 3) {
throw new Error(`Failed to add GitHub remote to ${repoPath}`)
}
repos.push({
id: `bench-gh-repo-${i}`,
path: repoPath,
displayName: `Bench GH Repo ${i}`,
badgeColor: '#000000',
addedAt: 1,
externalWorktreeVisibility: 'show'
})
}
return repos
}
/**
* SSH targets on TEST-NET-3 (RFC 5737). The address is guaranteed unroutable,
* so the TCP handshake never completes and never gets a reset — the wire
* behaviour of a host that is asleep or behind a dropped VPN.
*/
function buildUnreachableSshTargets(count) {
const targets = []
for (let i = 0; i < count; i++) {
targets.push({
id: `bench-ssh-unreachable-${i}`,
label: `Unreachable Host ${i}`,
host: `203.0.113.${i + 1}`,
port: 22,
username: 'orca',
source: 'manual',
lastRequiredPassphrase: false
})
}
return targets
}
export function writePersistedStateFixture(
fixtureDir,
{ stateProfile, sessionTabs, githubRepos, sshUnreachableTargets = 0 }
) {
const dataPath = join(fixtureDir, 'orca-data.json')
if (stateProfile === 'none' && githubRepos === 0 && sshUnreachableTargets === 0) {
try {
unlinkSync(dataPath)
} catch {
// no persisted state fixture
}
return 0
}
if (!['none', 'restored-local-tabs'].includes(stateProfile)) {
throw new Error(`Unknown state profile: ${stateProfile}`)
}
const githubRepoEntries = buildGithubRepoFixtures(fixtureDir, githubRepos)
const sshTargets = buildUnreachableSshTargets(sshUnreachableTargets)
if (stateProfile === 'none') {
const state = {
schemaVersion: 1,
...(sshTargets.length > 0 ? { sshTargets } : {}),
repos: githubRepoEntries,
settings: {
telemetry: {
installId: 'startup-bench',
optedIn: false,
existedBeforeTelemetryRelease: true
}
}
}
const json = JSON.stringify(state, null, 2)
writeFileSync(dataPath, json, 'utf-8')
return Buffer.byteLength(json)
}
const repoPath = initFixtureGitRepo(join(fixtureDir, 'bench-repo'))
const repoId = 'bench-repo'
const worktreeId = `${repoId}::${repoPath}`
const tabCount = Math.max(1, sessionTabs)
const tabs = []
const terminalLayoutsByTabId = {}
const activeTabIdByWorktree = {}
for (let i = 0; i < tabCount; i++) {
const tabId = `bench-tab-${String(i).padStart(5, '0')}`
const ptyId = `bench-pty-${String(i).padStart(5, '0')}`
tabs.push({
id: tabId,
ptyId,
worktreeId,
title: `Terminal ${i + 1}`,
customTitle: null,
color: null,
sortOrder: i,
createdAt: 1
})
terminalLayoutsByTabId[tabId] = {
root: null,
activeLeafId: null,
expandedLeafId: null
}
}
activeTabIdByWorktree[worktreeId] = tabs[0]?.id ?? null
const state = {
schemaVersion: 1,
repos: [
{
id: repoId,
path: repoPath,
displayName: 'Bench Repo',
badgeColor: '#000000',
addedAt: 1,
externalWorktreeVisibility: 'show'
},
...githubRepoEntries
],
settings: {
telemetry: {
installId: 'startup-bench',
optedIn: false,
existedBeforeTelemetryRelease: true
}
},
ui: {
lastActiveRepoId: repoId,
lastActiveWorktreeId: worktreeId
},
workspaceSession: {
activeRepoId: repoId,
activeWorktreeId: worktreeId,
activeTabId: tabs[0]?.id ?? null,
tabsByWorktree: {
[worktreeId]: tabs
},
terminalLayoutsByTabId,
activeTabIdByWorktree,
activeWorktreeIdsOnShutdown: [worktreeId],
defaultTerminalTabsAppliedByWorktreeId: {
[worktreeId]: true
},
// Why on the session and not just the target list: startup reconnect only
// dials targets that were connected at shutdown.
...(sshTargets.length > 0
? { activeConnectionIdsAtShutdown: sshTargets.map((target) => target.id) }
: {})
}
}
if (sshTargets.length > 0) {
state.sshTargets = sshTargets
}
const json = JSON.stringify(state, null, 2)
writeFileSync(dataPath, json, 'utf-8')
return Buffer.byteLength(json)
}