Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

perf(scm): open the panel fast and keep it warm across close/reopen

enes849ffce6faef
diff --git a/src/core/git-service.ts b/src/core/git-service.ts
index 0509936ef..f307f4abd 100644
--- a/src/core/git-service.ts
+++ b/src/core/git-service.ts
@@ -54,24 +54,44 @@ type ActiveClone = {
}
let activeClone: ActiveClone | null = null
-// `gh auth status` is a network-touching CLI call but auth state changes rarely, so cache
-// it briefly — otherwise every status refresh pays a process spawn (and possible round-trip).
-const GH_AUTH_TTL_MS = 60_000
+// `gh auth status` is a network-touching CLI call (it validates the token against the GitHub
+// API — measured ~700ms even on a fast link) but auth state changes rarely, so cache it.
+const GH_AUTH_TTL_MS = 10 * 60_000
let ghAuthedCache: { value: boolean; at: number } | null = null
+let ghAuthedInFlight: Promise<boolean> | null = null
async function ghAuthed(): Promise<boolean> {
if (!GH_PATH) return false
const now = Date.now()
if (ghAuthedCache && now - ghAuthedCache.at < GH_AUTH_TTL_MS) return ghAuthedCache.value
- let value = false
- try {
- await run(GH_PATH, ['auth', 'status'], { env: GIT_ENV, maxBuffer: 1024 * 1024 })
- value = true
- } catch {
- value = false
- }
- ghAuthedCache = { value, at: now }
- return value
+ if (ghAuthedInFlight) return ghAuthedInFlight
+ ghAuthedInFlight = (async () => {
+ let value = false
+ try {
+ await run(GH_PATH, ['auth', 'status'], { env: GIT_ENV, maxBuffer: 1024 * 1024 })
+ value = true
+ } catch {
+ value = false
+ }
+ ghAuthedCache = { value, at: Date.now() }
+ ghAuthedInFlight = null
+ return value
+ })()
+ return ghAuthedInFlight
+}
+
+/**
+ * Stale-while-revalidate view of `ghAuthed` for the status() hot path: `status()` must never
+ * block the file list on a GitHub API round-trip. Returns the cached answer immediately (even
+ * expired — auth state changes rarely, a stale answer beats a blank panel) and refreshes the
+ * cache in the background so the panel's next refresh sees the real value. Only the very first
+ * call ever (no cache at all) reports `false` while the probe runs; that flips one refresh later.
+ */
+function ghAuthedSwr(): boolean {
+ if (!GH_PATH) return false
+ const fresh = !!ghAuthedCache && Date.now() - ghAuthedCache.at < GH_AUTH_TTL_MS
+ if (!fresh) void ghAuthed().catch(() => {})
+ return ghAuthedCache?.value ?? false
}
interface Exec {
@@ -345,7 +365,9 @@ export class GitService {
// These reads are independent of each other; run them concurrently instead of
// serially spawning ~10 git processes one after the next. (`remote get-url origin`
// simply fails to empty when there's no origin, so it needn't wait on `remote`.)
- const [branchR, branchesR, remotesR, originR, countsR, upstreamR, cachedR, workR, porcelainR, gh] =
+ // gh auth is deliberately NOT awaited here (ghAuthedSwr): it hits the GitHub API and
+ // used to hold the whole status — i.e. the panel's first paint — hostage for ~700ms.
+ const [branchR, branchesR, remotesR, originR, countsR, upstreamR, cachedR, workR, porcelainR] =
await Promise.all([
git(cwd, ['rev-parse', '--abbrev-ref', 'HEAD']),
git(cwd, ['branch', '--format=%(refname:short)']),
@@ -357,9 +379,9 @@ export class GitService {
git(cwd, ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}']),
git(cwd, ['diff', '--cached', '--numstat']),
git(cwd, ['diff', '--numstat']),
- git(cwd, ['status', '--porcelain']),
- ghAuthed()
+ git(cwd, ['status', '--porcelain'])
])
+ const gh = ghAuthedSwr()
const branch = branchR.out.trim() || 'HEAD'
const branches = branchesR.out.split('\n').map((b) => b.trim()).filter(Boolean)
diff --git a/src/renderer/components/SourceControlPanel.tsx b/src/renderer/components/SourceControlPanel.tsx
index 262c52c44..51f217059 100644
--- a/src/renderer/components/SourceControlPanel.tsx
+++ b/src/renderer/components/SourceControlPanel.tsx
@@ -7,6 +7,7 @@ import { useProjects } from '../state/projects'
import { useSettings } from '../state/settings'
import { useSshConn } from '../state/sshConn'
import { useScmDraft } from '../state/scmDraft'
+import { useScmCache } from '../state/scmCache'
import { useSession } from '../session/session'
import { GitHistoryPanel } from './git-history/GitHistoryPanel'
import { buildCommitMenuItems } from './git-history/git-history-menu'
@@ -84,7 +85,16 @@ export function SourceControlPanel({
// appears once `setConn` runs (after `setActiveRemote` arms remote routing). Observing it lets the
// refresh re-run when the master connects. Local projects have no entry → undefined → no effect.
const sshControlPath = useSshConn((s) => (project?.id ? s.byProject[project.id]?.controlPath : undefined))
- const [status, setStatus] = useState<GitStatus | null>(null)
+ // Status + history live in a per-cwd cache (scmCache), not component state: the panel is
+ // unmounted on close, and reopening used to show an empty drawer until git answered. The
+ // cached snapshot paints immediately; the refresh-on-mount below replaces it silently.
+ const status = useScmCache((s) => (cwd ? (s.status[cwd] ?? null) : null))
+ const setStatus = useCallback(
+ (st: GitStatus | null) => {
+ if (cwd && st) useScmCache.getState().setStatus(cwd, st)
+ },
+ [cwd]
+ )
// Commit message + AI-generate state live in a per-repo store (keyed by cwd), so closing the
// panel mid-generation neither discards the message nor abandons the run — reopening shows it.
const draftKey = cwd ?? ''
@@ -100,7 +110,13 @@ export function SourceControlPanel({
const [branchMenu, setBranchMenu] = useState<{ top: number; left: number } | null>(null)
const [newBranch, setNewBranch] = useState('')
const [fileMenu, setFileMenu] = useState<{ x: number; y: number; path: string } | null>(null)
- const [history, setHistory] = useState<GitHistoryResult | null>(null)
+ const history = useScmCache((s) => (cwd ? (s.history[cwd] ?? null) : null))
+ const setHistory = useCallback(
+ (h: GitHistoryResult | null) => {
+ if (cwd && h) useScmCache.getState().setHistory(cwd, h)
+ },
+ [cwd]
+ )
const [historyLoading, setHistoryLoading] = useState(false)
const [historyError, setHistoryError] = useState('')
const [commitMenu, setCommitMenu] = useState<{ x: number; y: number; item: GitHistoryItem } | null>(
@@ -128,11 +144,10 @@ export function SourceControlPanel({
const autoFetchOn = useSettings((s) => s.settings.gitAutoFetch)
const refreshHistory = useCallback(async () => {
- if (!cwd) {
- setHistory(null)
- return
- }
- setHistoryLoading(true)
+ if (!cwd) return
+ // Spinner only when there is nothing to show yet — cached history stays visible while
+ // the silent re-fetch runs.
+ if (!useScmCache.getState().history[cwd]) setHistoryLoading(true)
try {
setHistory(await git.history(cwd))
setHistoryError('')
diff --git a/src/renderer/state/scmCache.ts b/src/renderer/state/scmCache.ts
new file mode 100644
index 000000000..3a968093d
--- /dev/null
+++ b/src/renderer/state/scmCache.ts
@@ -0,0 +1,33 @@
+import { create } from 'zustand'
+import type { GitStatus } from '@shared/types'
+import type { GitHistoryResult } from '@shared/git-history'
+
+/**
+ * Last-known Source Control data per repo cwd, so reopening the panel paints instantly.
+ *
+ * The panel is mounted per open (`scOpen` in Canvas) and used to hold status/history in
+ * component-local useState — every close threw the data away and every reopen showed an empty
+ * drawer until a fresh `git.status` + `git.history` round-trip completed. This store is the
+ * same pattern as `scmDraft`: keyed by cwd (an SSH project's remoteCwd included), written
+ * through on every refresh, and read as the initial paint on mount while the panel's normal
+ * refresh-on-mount silently replaces it. Renderer-session-lifetime only — nothing is persisted.
+ */
+interface ScmCacheState {
+ status: Record<string, GitStatus>
+ history: Record<string, GitHistoryResult>
+ setStatus(cwd: string, status: GitStatus): void
+ setHistory(cwd: string, history: GitHistoryResult): void
+}
+
+export const useScmCache = create<ScmCacheState>((set) => ({
+ status: {},
+ history: {},
+ setStatus(cwd, status) {
+ if (!cwd) return
+ set((s) => ({ status: { ...s.status, [cwd]: status } }))
+ },
+ setHistory(cwd, history) {
+ if (!cwd) return
+ set((s) => ({ history: { ...s.history, [cwd]: history } }))
+ }
+}))