xonecas.io
64875
Mc

mini-coder / commit c1266c2

perf(cli): cache git branch lookups in interactive loop

xonecas2026-03-14 c1266c2884452d44c254794f14d4ab2696c9e788 parent d235e08

3 files changed

src/cli/git-branch-cache.test.ts+84−0
@@ -0,0 +1,84 @@
1+import { describe, expect, test } from "bun:test";
2+import { createGitBranchCache } from "./git-branch-cache.ts";
3+
4+describe("createGitBranchCache", () => {
5+ test("stores fetched branch value", async () => {
6+ const cache = createGitBranchCache({
7+ cwd: "/tmp",
8+ fetchBranch: async () => "main",
9+ ttlMs: 10_000,
10+ now: () => 0,
11+ });
12+
13+ expect(cache.get()).toBeNull();
14+ await cache.refresh(true);
15+ expect(cache.get()).toBe("main");
16+ });
17+
18+ test("does not refetch within ttl window", async () => {
19+ let calls = 0;
20+ let nowValue = 1000;
21+ const cache = createGitBranchCache({
22+ cwd: "/tmp",
23+ fetchBranch: async () => {
24+ calls++;
25+ return "dev";
26+ },
27+ ttlMs: 5000,
28+ now: () => nowValue,
29+ });
30+
31+ await cache.refresh(true);
32+ await cache.refresh();
33+ nowValue += 1000;
34+ await cache.refresh();
35+
36+ expect(calls).toBe(1);
37+ });
38+
39+ test("refetches after ttl expires", async () => {
40+ let calls = 0;
41+ let nowValue = 1000;
42+ const cache = createGitBranchCache({
43+ cwd: "/tmp",
44+ fetchBranch: async () => {
45+ calls++;
46+ return calls === 1 ? "dev" : "feature";
47+ },
48+ ttlMs: 500,
49+ now: () => nowValue,
50+ });
51+
52+ await cache.refresh(true);
53+ nowValue += 1000;
54+ await cache.refresh();
55+
56+ expect(calls).toBe(2);
57+ expect(cache.get()).toBe("feature");
58+ });
59+
60+ test("coalesces concurrent refreshes", async () => {
61+ let calls = 0;
62+ let resolveFetch!: (value: string) => void;
63+ const fetchPromise = new Promise<string>((resolve) => {
64+ resolveFetch = resolve;
65+ });
66+ const cache = createGitBranchCache({
67+ cwd: "/tmp",
68+ fetchBranch: async () => {
69+ calls++;
70+ return fetchPromise;
71+ },
72+ ttlMs: 5000,
73+ now: () => 0,
74+ });
75+
76+ const first = cache.refresh(true);
77+ const second = cache.refresh(true);
78+ resolveFetch("main");
79+ await Promise.all([first, second]);
80+
81+ expect(calls).toBe(1);
82+ expect(cache.get()).toBe("main");
83+ });
84+});
src/cli/git-branch-cache.ts+49−0
@@ -0,0 +1,49 @@
1+import { getGitBranch } from "./cli-helpers.ts";
2+
3+type BranchFetcher = (cwd: string) => Promise<string | null>;
4+
5+type GitBranchCacheOptions = {
6+ cwd: string;
7+ ttlMs?: number;
8+ fetchBranch?: BranchFetcher;
9+ now?: () => number;
10+};
11+
12+export function createGitBranchCache(options: GitBranchCacheOptions): {
13+ get: () => string | null;
14+ refresh: (force?: boolean) => Promise<void>;
15+ refreshInBackground: (force?: boolean) => void;
16+} {
17+ const fetchBranch = options.fetchBranch ?? getGitBranch;
18+ const now = options.now ?? Date.now;
19+ const ttlMs = options.ttlMs ?? 5000;
20+
21+ let value: string | null = null;
22+ let lastRefreshAt = 0;
23+ let inFlight: Promise<void> | null = null;
24+
25+ const refresh = async (force = false): Promise<void> => {
26+ if (!force && now() - lastRefreshAt < ttlMs) return;
27+ if (inFlight) {
28+ await inFlight;
29+ return;
30+ }
31+
32+ inFlight = (async () => {
33+ value = await fetchBranch(options.cwd);
34+ lastRefreshAt = now();
35+ })().finally(() => {
36+ inFlight = null;
37+ });
38+
39+ await inFlight;
40+ };
41+
42+ return {
43+ get: () => value,
44+ refresh,
45+ refreshInBackground: (force = false) => {
46+ void refresh(force);
47+ },
48+ };
49+}
src/cli/input-loop.ts+11−1
@@ -6,6 +6,7 @@ import { getContextWindow } from "../llm-api/providers.ts";
66 import { getGitBranch, runShellPassthrough } from "./cli-helpers.ts";
77 import { handleCommand } from "./commands.ts";
88 import { resolveFileRefs } from "./file-refs.ts";
9+import { createGitBranchCache } from "./git-branch-cache.ts";
910 import { type InputResult, readline } from "./input.ts";
1011 import { tildePath } from "./output.ts";
1112 import { buildStatusBarSignature } from "./status-bar.ts";
@@ -23,9 +24,14 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
2324 const { cwd, reporter, cmdCtx, runner } = opts;
2425
2526 let lastStatusSignature: string | null = null;
27+ const gitBranchCache = createGitBranchCache({
28+ cwd,
29+ fetchBranch: getGitBranch,
30+ });
31+ await gitBranchCache.refresh(true);
2632
2733 while (true) {
28- const branch = await getGitBranch(cwd);
34+ const branch = gitBranchCache.get();
2935 const status = runner.getStatusInfo();
3036 const provider = status.model.split("/")[0] ?? "";
3137 const modelShort = status.model.split("/").slice(1).join("/");
@@ -64,6 +70,7 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
6470 return;
6571
6672 case "interrupt":
73+ gitBranchCache.refreshInBackground();
6774 continue;
6875
6976 case "command": {
@@ -81,6 +88,7 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
8188 // Error already rendered by stream-render; continue the loop.
8289 }
8390 }
91+ gitBranchCache.refreshInBackground();
8492 continue;
8593 }
8694
@@ -89,6 +97,7 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
8997 if (out) {
9098 runner.addShellContext(input.command, out);
9199 }
100+ gitBranchCache.refreshInBackground(true);
92101 continue;
93102 }
94103
@@ -103,6 +112,7 @@ export async function runInputLoop(opts: InputLoopOptions): Promise<void> {
103112 } catch {
104113 // Error already rendered by stream-render; continue the loop.
105114 }
115+ gitBranchCache.refreshInBackground();
106116 continue;
107117 }
108118 }