Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Cache worktree behind counts by ref pair (PRD-9497) (#6560)
Andrew Mikofalvya9b385cd8821
diff --git a/.changeset/worktree-behind-counts-cache.md b/.changeset/worktree-behind-counts-cache.mdnew file mode 100644index 000000000..114c806e8--- /dev/null+++ b/.changeset/worktree-behind-counts-cache.md@@ -0,0 +1,6 @@+---+"@inkeep/open-knowledge": patch+"@inkeep/open-knowledge-desktop": patch+---++The desktop worktree switcher no longer starts a burst of git processes, one per branch, each time a window comes to the front.diff --git a/packages/app/src/lib/worktree-store.test.ts b/packages/app/src/lib/worktree-store.test.tsindex f5cd66e81..fefc0d6db 100644--- a/packages/app/src/lib/worktree-store.test.ts+++ b/packages/app/src/lib/worktree-store.test.ts@@ -194,4 +194,45 @@ describe('createWorktreeStore', () => {expect(fetchModel).toHaveBeenCalledTimes(2);expect(store.getSnapshot()?.mainRoot).toBe('/repo-revalidated');});++ test('revalidations inside the minimum interval are dropped, and refresh() still reloads at once', async () => {+ let clock = 1_000;+ const fetchModel = vi.fn(() => Promise.resolve(model('/repo')));+ let fireRevalidate: (() => void) | null = null;+ const subscribeRevalidate = vi.fn((onRevalidate: () => void) => {+ fireRevalidate = onRevalidate;+ return () => {};+ });+ const store = createWorktreeStore({+ fetchModel,+ subscribeRevalidate,+ minRevalidateIntervalMs: 10_000,+ now: () => clock,+ });+ store.subscribe(() => {});+ await flush();+ expect(fetchModel).toHaveBeenCalledTimes(1);++ for (let i = 0; i < 5; i += 1) {+ clock += 1_000;+ fireRevalidate?.();+ fireRevalidate?.();+ await flush();+ }+ expect(fetchModel).toHaveBeenCalledTimes(1);++ store.refresh();+ await flush();+ expect(fetchModel).toHaveBeenCalledTimes(2);++ clock += 9_999;+ fireRevalidate?.();+ await flush();+ expect(fetchModel).toHaveBeenCalledTimes(2);++ clock += 1;+ fireRevalidate?.();+ await flush();+ expect(fetchModel).toHaveBeenCalledTimes(3);+ });});diff --git a/packages/app/src/lib/worktree-store.ts b/packages/app/src/lib/worktree-store.tsindex e2f51d869..b8775eb36 100644--- a/packages/app/src/lib/worktree-store.ts+++ b/packages/app/src/lib/worktree-store.ts@@ -9,13 +9,20 @@ export interface WorktreeStore {interface WorktreeStoreDeps {fetchModel: () => Promise<WorktreeSelectorModel | null>;subscribeRevalidate?: (onRevalidate: () => void) => () => void;+ minRevalidateIntervalMs?: number;+ now?: () => number;}+const REVALIDATE_MIN_INTERVAL_MS = 2_000;+export function createWorktreeStore(deps: WorktreeStoreDeps): WorktreeStore {let model: WorktreeSelectorModel | null = null;let bootstrapped = false;let inFlight = false;let reloadQueued = false;+ let lastLoadStartedAt = Number.NEGATIVE_INFINITY;+ const now = deps.now ?? (() => performance.now());+ const minRevalidateIntervalMs = deps.minRevalidateIntervalMs ?? 0;let revalidateUnsub: (() => void) | null = null;const listeners = new Set<() => void>();@@ -29,6 +36,7 @@ export function createWorktreeStore(deps: WorktreeStoreDeps): WorktreeStore {return;}inFlight = true;+ lastLoadStartedAt = now();try {const next = await deps.fetchModel();if (next !== null && next !== model) {@@ -45,6 +53,11 @@ export function createWorktreeStore(deps: WorktreeStoreDeps): WorktreeStore {}}+ function revalidate(): void {+ if (now() - lastLoadStartedAt < minRevalidateIntervalMs) return;+ void load();+ }+return {getSnapshot: () => model,subscribe(listener) {@@ -54,9 +67,7 @@ export function createWorktreeStore(deps: WorktreeStoreDeps): WorktreeStore {void load();}if (deps.subscribeRevalidate && revalidateUnsub === null) {- revalidateUnsub = deps.subscribeRevalidate(() => {- void load();- });+ revalidateUnsub = deps.subscribeRevalidate(revalidate);}return () => {listeners.delete(listener);@@ -97,6 +108,7 @@ const productionStore: WorktreeStore =: createWorktreeStore({fetchModel: fetchWorktreeModel,subscribeRevalidate: subscribeBrowserRevalidate,+ minRevalidateIntervalMs: REVALIDATE_MIN_INTERVAL_MS,});export const subscribeToWorktrees = productionStore.subscribe;diff --git a/packages/core/src/git/branch-list-parser.test.ts b/packages/core/src/git/branch-list-parser.test.tsdeleted file mode 100644index 8d697336f..000000000--- a/packages/core/src/git/branch-list-parser.test.ts+++ /dev/null@@ -1,29 +0,0 @@-import { describe, expect, test } from 'vitest';-import { parseBranchList } from './branch-list-parser.ts';--describe('parseBranchList', () => {- test('empty stdout yields empty array', () => {- expect(parseBranchList('')).toEqual([]);- expect(parseBranchList('\n\n')).toEqual([]);- });-- test('parses one branch per line, preserving order', () => {- expect(parseBranchList('main\nfeat/foo\nrelease-1\n')).toEqual([- 'main',- 'feat/foo',- 'release-1',- ]);- });-- test('tolerates missing trailing newline, CRLF, and surrounding whitespace', () => {- expect(parseBranchList('main\r\n feat/bar ')).toEqual(['main', 'feat/bar']);- });-- test('drops blank lines and de-duplicates', () => {- expect(parseBranchList('main\n\nmain\ndev\n')).toEqual(['main', 'dev']);- });-- test('keeps slash-namespaced branch names intact', () => {- expect(parseBranchList('feat/a/b\nfix/c')).toEqual(['feat/a/b', 'fix/c']);- });-});diff --git a/packages/core/src/git/branch-list-parser.ts b/packages/core/src/git/branch-list-parser.tsdeleted file mode 100644index 6d2e449f5..000000000--- a/packages/core/src/git/branch-list-parser.ts+++ /dev/null@@ -1,13 +0,0 @@-export function parseBranchList(stdout: string): string[] {- if (stdout.length === 0) return [];-- const seen = new Set<string>();- const branches: string[] = [];- for (const rawLine of stdout.split('\n')) {- const branch = rawLine.replace(/\r$/, '').trim();- if (branch.length === 0 || seen.has(branch)) continue;- seen.add(branch);- branches.push(branch);- }- return branches;-}diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 26e37fbe3..4e02e4b32 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -756,7 +756,6 @@ export {FrontmatterDeltaSchema,type PropertyChange,} from './frontmatter-diff.ts';-export { parseBranchList } from './git/branch-list-parser.ts';export {agentToolDirs,augmentAgentSpawnPath,diff --git a/packages/desktop/src/main/worktree-service.git-spawns.test.ts b/packages/desktop/src/main/worktree-service.git-spawns.test.tsnew file mode 100644index 000000000..9260ebf3d--- /dev/null+++ b/packages/desktop/src/main/worktree-service.git-spawns.test.ts@@ -0,0 +1,231 @@+import { execFile, execFileSync } from 'node:child_process';+import {+ chmodSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ realpathSync,+ rmSync,+ writeFileSync,+} from 'node:fs';+import { tmpdir } from 'node:os';+import { delimiter, join } from 'node:path';+import { promisify } from 'node:util';+import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';+import { configureTestGitRepository } from '../../../../test-support/configure-git-fixture.test-helper.ts';+import { listWorktreeSelector } from './worktree-service.ts';++const execFileAsync = promisify(execFile);+const SETUP_ENV = { ...process.env, LANG: 'C', LC_ALL: 'C', GIT_CONFIG_GLOBAL: '/dev/null' };+const BRANCH_COUNT = 40;++const shimRoot = realpathSync(mkdtempSync(join(tmpdir(), 'wt-git-shim-')));+const spawnLog = join(shimRoot, 'spawns.log');+const failRevListFlag = join(shimRoot, 'fail-rev-list');+const originalPath = process.env.PATH;++async function git(cwd: string, ...args: string[]): Promise<string> {+ const { stdout } = await execFileAsync('git', args, { cwd, env: SETUP_ENV });+ return String(stdout);+}++function configureRepo(repo: string): void {+ configureTestGitRepository(repo, (args) =>+ execFileSync('git', [...args], { env: SETUP_ENV, stdio: 'pipe' }),+ );+}++interface SpawnStats {+ readonly total: number;+ readonly revList: number;+ readonly maxConcurrentRevList: number;+}++function readSpawnStats(): SpawnStats {+ let total = 0;+ let revList = 0;+ let running = 0;+ let maxConcurrentRevList = 0;+ const revListPids = new Set<string>();+ for (const line of readFileSync(spawnLog, 'utf8').split('\n')) {+ const [event, pid, subcommand] = line.split(' ');+ if (event === 'start') {+ total += 1;+ if (subcommand !== 'rev-list') continue;+ revList += 1;+ revListPids.add(pid ?? '');+ running += 1;+ maxConcurrentRevList = Math.max(maxConcurrentRevList, running);+ } else if (event === 'end' && revListPids.delete(pid ?? '')) {+ running -= 1;+ }+ }+ return { total, revList, maxConcurrentRevList };+}++async function behindCountsFromGit(repo: string, branches: readonly string[]) {+ const out: Record<string, number> = {};+ for (const branch of branches) {+ const n = await git(repo, 'rev-list', '--count', `${branch}..origin/${branch}`);+ out[branch] = Number.parseInt(n.trim(), 10);+ }+ return out;+}++async function listBehindCounts(repo: string): Promise<Record<string, number | undefined>> {+ writeFileSync(spawnLog, '');+ const res = await listWorktreeSelector(repo, repo);+ if (!res.ok) throw new Error(`listWorktreeSelector failed: ${res.reason}`);+ return Object.fromEntries(res.model.entries.map((e) => [e.branch, e.behind]));+}++async function advanceOrigin(repo: string, branch: string, commits: number): Promise<void> {+ let tip = (await git(repo, 'rev-parse', `origin/${branch}`)).trim();+ const tree = (await git(repo, 'rev-parse', `${tip}^{tree}`)).trim();+ for (let i = 0; i < commits; i += 1) {+ tip = (await git(repo, 'commit-tree', tree, '-p', tip, '-m', `${branch} ahead ${i}`)).trim();+ }+ await git(repo, 'push', 'origin', `${tip}:refs/heads/${branch}`);+}++describe.skipIf(process.platform === 'win32')('worktree behind counts: git spawns', () => {+ let root: string | null = null;+ let repo = '';+ let branches: string[] = [];++ beforeAll(() => {+ const realGit = execFileSync('sh', ['-c', 'command -v git'], { env: SETUP_ENV })+ .toString()+ .trim();+ const shimDir = join(shimRoot, 'bin');+ mkdirSync(shimDir);+ writeFileSync(+ join(shimDir, 'git'),+ [+ '#!/bin/sh',+ `printf 'start %s %s\\n' "$$" "$1" >> '${spawnLog}'`,+ `if [ "$1" = rev-list ] && [ -f '${failRevListFlag}' ]; then printf 'end %s\\n' "$$" >> '${spawnLog}'; exit 1; fi`,+ `'${realGit}' "$@"`,+ 'status=$?',+ `printf 'end %s\\n' "$$" >> '${spawnLog}'`,+ 'exit $status',+ '',+ ].join('\n'),+ );+ chmodSync(join(shimDir, 'git'), 0o755);+ process.env.PATH = `${shimDir}${delimiter}${originalPath ?? ''}`;+ });++ afterAll(() => {+ process.env.PATH = originalPath;+ rmSync(shimRoot, { recursive: true, force: true });+ });++ afterEach(() => {+ rmSync(failRevListFlag, { force: true });+ if (root) rmSync(root, { recursive: true, force: true });+ root = null;+ });++ async function makeRepoWithBranches(): Promise<void> {+ root = realpathSync(mkdtempSync(join(tmpdir(), 'wt-behind-')));+ repo = join(root, 'main');+ mkdirSync(repo);+ await git(repo, 'init', '--initial-branch=main', '.');+ configureRepo(repo);+ await git(repo, 'config', 'user.email', '[email protected]');+ await git(repo, 'config', 'user.name', 'Test');+ writeFileSync(join(repo, 'README.md'), '# main\n');+ await git(repo, 'add', '-A');+ await git(repo, 'commit', '-m', 'initial');+ branches = ['main'];+ for (let i = 0; i < BRANCH_COUNT; i += 1) {+ await git(repo, 'commit', '--allow-empty', '-m', `commit ${i}`);+ const n = String(i).padStart(2, '0');+ const branch = i % 5 === 1 ? `team/fix/item-${n}` : `feature-${n}`;+ await git(repo, 'branch', branch);+ branches.push(branch);+ }+ await git(repo, 'commit', '--allow-empty', '-m', 'main tip');+ await git(repo, 'branch', 'local-only');+ const bare = join(root, 'origin.git');+ await git(repo, 'init', '--bare', '--initial-branch=main', bare);+ configureRepo(bare);+ await git(repo, 'remote', 'add', 'origin', bare);+ await git(repo, 'push', 'origin', ...branches);+ await git(repo, 'fetch', 'origin');+ }++ test('a cold load counts each ref pair once with at most 8 rev-list processes running', async () => {+ await makeRepoWithBranches();+ const behind = await listBehindCounts(repo);+ const stats = readSpawnStats();+ expect(stats.revList).toBe(branches.length);+ expect(stats.maxConcurrentRevList).toBeGreaterThan(0);+ expect(stats.maxConcurrentRevList).toBeLessThanOrEqual(8);+ expect(behind).toMatchObject(await behindCountsFromGit(repo, branches));+ expect(behind['local-only']).toBeUndefined();+ });++ test('a refresh where no ref moved starts a constant number of git processes', async () => {+ await makeRepoWithBranches();+ const first = await listBehindCounts(repo);+ const firstStats = readSpawnStats();++ const second = await listBehindCounts(repo);+ const secondStats = readSpawnStats();+ expect(secondStats.revList).toBe(0);+ expect(secondStats.total).toBe(firstStats.total - firstStats.revList);+ expect(secondStats.total).toBeLessThanOrEqual(3);+ expect(second).toEqual(first);+ });++ test('after refs move only the changed pairs are recounted and every count matches git', async () => {+ await makeRepoWithBranches();+ await listBehindCounts(repo);++ await advanceOrigin(repo, 'feature-03', 2);+ await advanceOrigin(repo, 'feature-17', 5);+ await advanceOrigin(repo, 'main', 1);+ await advanceOrigin(repo, 'team/fix/item-06', 3);+ await git(repo, 'fetch', 'origin');+ await git(repo, 'branch', '-f', 'feature-30', 'feature-28');++ const behind = await listBehindCounts(repo);+ const stats = readSpawnStats();+ expect(stats.revList).toBe(5);+ const expected = await behindCountsFromGit(repo, branches);+ expect(expected).toMatchObject({+ 'feature-03': 2,+ 'feature-17': 5,+ main: 1,+ 'feature-30': 2,+ 'team/fix/item-06': 3,+ });+ expect(behind).toMatchObject(expected);+ });++ test('a count that failed is recounted on the next refresh instead of served from the cache', async () => {+ await makeRepoWithBranches();+ writeFileSync(failRevListFlag, '');+ const failed = await listBehindCounts(repo);+ expect(readSpawnStats().revList).toBe(branches.length);+ expect(failed.main).toBeUndefined();++ rmSync(failRevListFlag);+ const retried = await listBehindCounts(repo);+ expect(readSpawnStats().revList).toBe(branches.length);+ expect(retried).toMatchObject(await behindCountsFromGit(repo, branches));+ });++ test('a window on a linked worktree reuses the counts its repository already has', async () => {+ await makeRepoWithBranches();+ await listBehindCounts(repo);+ const linked = join(root ?? '', 'linked');+ await git(repo, 'worktree', 'add', linked, 'feature-02');++ const behind = await listBehindCounts(linked);+ expect(readSpawnStats().revList).toBe(0);+ expect(behind).toMatchObject(await behindCountsFromGit(repo, branches));+ });+});diff --git a/packages/desktop/src/main/worktree-service.ts b/packages/desktop/src/main/worktree-service.tsindex 946c439ce..19cdbae47 100644--- a/packages/desktop/src/main/worktree-service.ts+++ b/packages/desktop/src/main/worktree-service.ts@@ -9,7 +9,6 @@ import {isBranchNotFoundGitError,isLoginFixableGitAuthError,isValidBranchName,- parseBranchList,stripRemotePrefix,WORKTREES_PARENT_DIR,type WorktreeCreateRequest,@@ -60,14 +59,13 @@ export async function listWorktreeSelector(): Promise<WorktreeListResult> {const worktrees = await listGitWorktrees(anchorPath);if (worktrees.length === 0) return { ok: false, reason: 'no-git' };- const branches = await listLocalBranches(anchorPath);- const remoteBranches = await listRemoteBranches(anchorPath);- const behind = await computeBehindCounts(anchorPath, branches, remoteBranches);+ const refs = await readRefSnapshot(anchorPath);+ const behind = await computeBehindCounts(anchorPath, worktrees[0]?.path ?? anchorPath, refs);const resolvedCurrent = resolveAnchorToplevel(currentProjectPath, worktrees);const model = buildWorktreeSelectorModel({worktrees,- branches,- remoteBranches,+ branches: refs.branches,+ remoteBranches: refs.remoteBranches,behind,currentProjectPath: resolvedCurrent,});@@ -415,59 +413,139 @@ function buildAddArgs(args: CreateWorktreeArgs, worktreePath: string): string[]return ['worktree', 'add', worktreePath, '--', branch];}-async function listLocalBranches(anchorPath: string): Promise<string[]> {- if (!isAbsolute(anchorPath)) return [];+interface RefSnapshot {+ readonly branches: readonly string[];+ readonly remoteBranches: readonly string[];+ readonly localShas: ReadonlyMap<string, string>;+ readonly originShas: ReadonlyMap<string, string>;+}++const EMPTY_REF_SNAPSHOT: RefSnapshot = {+ branches: [],+ remoteBranches: [],+ localShas: new Map(),+ originShas: new Map(),+};++const REF_LIST_MAX_BUFFER_BYTES = 10 * 1024 * 1024;+const REF_LIST_TIMEOUT_MS = 15_000;+const BEHIND_COUNT_TIMEOUT_MS = 30_000;+const LOCAL_REF_PREFIX = 'refs/heads/';+const REMOTE_REF_PREFIX = 'refs/remotes/';+const ORIGIN_PREFIX = 'origin/';++async function readRefSnapshot(anchorPath: string): Promise<RefSnapshot> {+ if (!isAbsolute(anchorPath)) return EMPTY_REF_SNAPSHOT;+ let stdout: string;try {- const { stdout } = await execFileAsync(+ const result = await execFileAsync('git',- ['for-each-ref', '--format=%(refname:short)', 'refs/heads/'],- { cwd: anchorPath, env: gitSpawnEnv(), windowsHide: true },+ ['for-each-ref', '--format=%(objectname) %(refname)', LOCAL_REF_PREFIX, REMOTE_REF_PREFIX],+ {+ cwd: anchorPath,+ env: gitSpawnEnv(),+ maxBuffer: REF_LIST_MAX_BUFFER_BYTES,+ timeout: REF_LIST_TIMEOUT_MS,+ windowsHide: true,+ },);- return parseBranchList(String(stdout));+ stdout = String(result.stdout);} catch {- return [];+ return EMPTY_REF_SNAPSHOT;}+ const branches: string[] = [];+ const remoteBranches: string[] = [];+ const localShas = new Map<string, string>();+ const originShas = new Map<string, string>();+ for (const rawLine of stdout.split('\n')) {+ const line = rawLine.replace(/\r$/, '');+ const space = line.indexOf(' ');+ if (space === -1) continue;+ const sha = line.slice(0, space);+ const refname = line.slice(space + 1);+ if (refname.startsWith(LOCAL_REF_PREFIX)) {+ const branch = refname.slice(LOCAL_REF_PREFIX.length);+ if (branch.length === 0) continue;+ branches.push(branch);+ localShas.set(branch, sha);+ } else if (refname.startsWith(REMOTE_REF_PREFIX)) {+ const ref = refname.slice(REMOTE_REF_PREFIX.length);+ if (!ref.includes('/') || stripRemotePrefix(ref) === 'HEAD') continue;+ remoteBranches.push(ref);+ if (ref.startsWith(ORIGIN_PREFIX)) originShas.set(ref.slice(ORIGIN_PREFIX.length), sha);+ }+ }+ return { branches, remoteBranches, localShas, originShas };}-async function listRemoteBranches(anchorPath: string): Promise<string[]> {- if (!isAbsolute(anchorPath)) return [];+const BEHIND_COUNT_CONCURRENCY = 8;+let activeBehindCounts = 0;+const behindCountWaiters: (() => void)[] = [];+const behindCountCache = new Map<string, Map<string, Promise<number | null>>>();++async function withBehindCountSlot<T>(run: () => Promise<T>): Promise<T> {+ if (activeBehindCounts < BEHIND_COUNT_CONCURRENCY) {+ activeBehindCounts += 1;+ } else {+ await new Promise<void>((resolve) => behindCountWaiters.push(resolve));+ }try {- const { stdout } = await execFileAsync(- 'git',- ['for-each-ref', '--format=%(refname:short)', 'refs/remotes/'],- { cwd: anchorPath, env: gitSpawnEnv(), windowsHide: true },- );- return parseBranchList(String(stdout)).filter(- (ref) => ref.includes('/') && stripRemotePrefix(ref) !== 'HEAD',- );+ return await run();+ } finally {+ const next = behindCountWaiters.shift();+ if (next) next();+ else activeBehindCounts -= 1;+ }+}++async function countCommitsInRange(anchorPath: string, range: string): Promise<number | null> {+ try {+ const { stdout } = await execFileAsync('git', ['rev-list', '--count', range], {+ cwd: anchorPath,+ env: gitSpawnEnv(),+ timeout: BEHIND_COUNT_TIMEOUT_MS,+ windowsHide: true,+ });+ const n = Number.parseInt(String(stdout).trim(), 10);+ return Number.isFinite(n) && n >= 0 ? n : null;} catch {- return [];+ return null;}}async function computeBehindCounts(anchorPath: string,- branches: readonly string[],- remoteBranches: readonly string[],+ repositoryRoot: string,+ refs: RefSnapshot,): Promise<Record<string, number>> {if (!isAbsolute(anchorPath)) return {};- const remoteRefSet = new Set(remoteBranches);+ const previous = behindCountCache.get(repositoryRoot);+ const current = new Map<string, Promise<number | null>>();+ behindCountCache.set(repositoryRoot, current);const out: Record<string, number> = {};- await Promise.all(- branches.map(async (branch) => {- const upstream = `origin/${branch}`;- if (!remoteRefSet.has(upstream)) return;- try {- const { stdout } = await execFileAsync(- 'git',- ['rev-list', '--count', `${branch}..${upstream}`],- { cwd: anchorPath, env: gitSpawnEnv(), windowsHide: true },- );- const n = Number.parseInt(String(stdout).trim(), 10);- if (Number.isFinite(n) && n >= 0) out[branch] = n;- } catch {}- }),- );+ const settled: Promise<void>[] = [];+ for (const branch of refs.branches) {+ const localSha = refs.localShas.get(branch);+ const originSha = refs.originShas.get(branch);+ if (localSha === undefined || originSha === undefined) continue;+ const range = `${localSha}..${originSha}`;+ const count =+ current.get(range) ??+ previous?.get(range) ??+ withBehindCountSlot(() => countCommitsInRange(anchorPath, range));+ current.set(range, count);+ settled.push(+ count.then((n) => {+ if (n !== null) {+ out[branch] = n;+ return;+ }+ const cache = behindCountCache.get(repositoryRoot);+ if (cache?.get(range) === count) cache.delete(range);+ }),+ );+ }+ await Promise.all(settled);return out;}