@devries-cameron20 / t3code-1 / commits / a1532d5b2d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Refresh git upstream before status and tune Git query polling

- Fetch tracked upstream (throttled + timeout) before `statusDetails` so ahead/behind reflects remote updates - Add integration coverage for behind-count updates after remote commits - Configure React Query stale/refetch intervals for git status and branch lists

Julius Marminge committed Feb 17, 2026 at 16:59 UTC a1532d5b2de71e4a2c3d90d9653da02fa931d8d6
3 files changed +104 -6
apps/server/src/git.test.ts
+29
@@ -760,6 +760,35 @@ describe("git integration", () => {
760 expect(dirty.hasWorkingTreeChanges).toBe(true);
761 });
762
763 + it("refreshes upstream before statusDetails so behind count reflects remote updates", async () => {
764 + await using remote = await makeTmpDir();
765 + await using source = await makeTmpDir();
766 + await using clone = await makeTmpDir();
767 + await git(remote.path, "init --bare");
768 +
769 + await initRepoWithCommit(source.path);
770 + const initialBranch = (await listGitBranches({ cwd: source.path })).branches.find(
771 + (branch) => branch.current,
772 + )!.name;
773 + await git(source.path, `remote add origin ${JSON.stringify(remote.path)}`);
774 + await git(source.path, `push -u origin ${initialBranch}`);
775 +
776 + await git(clone.path, `clone ${JSON.stringify(remote.path)} .`);
777 + await git(clone.path, "config user.email 'test@test.com'");
778 + await git(clone.path, "config user.name 'Test'");
779 + await git(clone.path, `checkout -B ${initialBranch} --track origin/${initialBranch}`);
780 + await writeFile(path.join(clone.path, "CHANGELOG.md"), "remote change\n");
781 + await git(clone.path, "add CHANGELOG.md");
782 + await git(clone.path, "commit -m 'remote update'");
783 + await git(clone.path, `push origin ${initialBranch}`);
784 +
785 + const core = new GitCoreService();
786 + const details = await core.statusDetails(source.path);
787 + expect(details.branch).toBe(initialBranch);
788 + expect(details.aheadCount).toBe(0);
789 + expect(details.behindCount).toBe(1);
790 + });
791 +
792 it("prepares commit context by auto-staging and creates commit", async () => {
793 await using tmp = await makeTmpDir();
794 await initRepoWithCommit(tmp.path);
apps/server/src/git.ts
+62 -6
@@ -65,6 +65,8 @@ interface ExecuteGitOptions {
65 }
66
67 const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
68 +const STATUS_UPSTREAM_REFRESH_INTERVAL_MS = 15_000;
69 +const STATUS_UPSTREAM_REFRESH_TIMEOUT_MS = 5_000;
70
71 /** Spawn git directly with an argv array — no shell, no quoting needed. */
72 function runGit(
@@ -216,6 +218,8 @@ async function executeGit(
218 }
219
220 export class GitCoreService {
221 + private readonly lastUpstreamRefreshAt = new Map<string, number>();
222 +
223 async status(raw: GitStatusInput): Promise<GitStatusResult> {
224 const input = gitStatusInputSchema.parse(raw);
225 const details = await this.statusDetails(input.cwd);
@@ -231,6 +235,8 @@ export class GitCoreService {
235 }
236
237 async statusDetails(cwd: string): Promise<GitStatusDetails> {
238 + await this.refreshStatusUpstreamIfStale(cwd).catch(() => undefined);
239 +
240 const [statusStdout, unstagedNumstatStdout, stagedNumstatStdout] = await Promise.all([
241 this.gitStdout(cwd, ["status", "--porcelain=2", "--branch"]),
242 this.gitStdout(cwd, ["diff", "--numstat"]),
@@ -553,7 +559,9 @@ export class GitCoreService {
559 });
560 }
561
556 - private async refreshCheckedOutBranchUpstream(cwd: string): Promise<void> {
562 + private async resolveCurrentUpstream(
563 + cwd: string,
564 + ): Promise<{ upstreamRef: string; remoteName: string; upstreamBranch: string } | null> {
565 const upstreamRef = trimStdout(
566 await this.gitStdout(
567 cwd,
@@ -562,24 +570,72 @@ export class GitCoreService {
570 ),
571 );
572 if (upstreamRef.length === 0 || upstreamRef === "@{upstream}") {
565 - return;
573 + return null;
574 }
575
576 const separatorIndex = upstreamRef.indexOf("/");
577 if (separatorIndex <= 0) {
570 - return;
578 + return null;
579 }
580 const remoteName = upstreamRef.slice(0, separatorIndex);
581 const upstreamBranch = upstreamRef.slice(separatorIndex + 1);
582 if (remoteName.length === 0) {
575 - return;
583 + return null;
584 }
585 if (upstreamBranch.length === 0) {
586 + return null;
587 + }
588 +
589 + return {
590 + upstreamRef,
591 + remoteName,
592 + upstreamBranch,
593 + };
594 + }
595 +
596 + private async fetchUpstreamRef(
597 + cwd: string,
598 + upstream: { upstreamRef: string; remoteName: string; upstreamBranch: string },
599 + ): Promise<void> {
600 + const refspec = `+refs/heads/${upstream.upstreamBranch}:refs/remotes/${upstream.upstreamRef}`;
601 + await this.git(cwd, ["fetch", "--quiet", "--no-tags", upstream.remoteName, refspec], true);
602 + }
603 +
604 + private async fetchUpstreamRefForStatus(
605 + cwd: string,
606 + upstream: { upstreamRef: string; remoteName: string; upstreamBranch: string },
607 + ): Promise<void> {
608 + const refspec = `+refs/heads/${upstream.upstreamBranch}:refs/remotes/${upstream.upstreamRef}`;
609 + await executeGit(cwd, ["fetch", "--quiet", "--no-tags", upstream.remoteName, refspec], {
610 + allowNonZeroExit: true,
611 + timeoutMs: STATUS_UPSTREAM_REFRESH_TIMEOUT_MS,
612 + });
613 + }
614 +
615 + private async refreshStatusUpstreamIfStale(cwd: string): Promise<void> {
616 + const upstream = await this.resolveCurrentUpstream(cwd);
617 + if (!upstream) {
618 return;
619 }
620
581 - const refspec = `+refs/heads/${upstreamBranch}:refs/remotes/${upstreamRef}`;
582 - await this.git(cwd, ["fetch", "--quiet", "--no-tags", remoteName, refspec], true);
621 + const cacheKey = `${cwd}\u0000${upstream.upstreamRef}`;
622 + const now = Date.now();
623 + const lastRefreshAt = this.lastUpstreamRefreshAt.get(cacheKey) ?? 0;
624 + if (now - lastRefreshAt < STATUS_UPSTREAM_REFRESH_INTERVAL_MS) {
625 + return;
626 + }
627 +
628 + // Record attempt time first to avoid repeated fetch spikes when status is polled often.
629 + this.lastUpstreamRefreshAt.set(cacheKey, now);
630 + await this.fetchUpstreamRefForStatus(cwd, upstream);
631 + }
632 +
633 + private async refreshCheckedOutBranchUpstream(cwd: string): Promise<void> {
634 + const upstream = await this.resolveCurrentUpstream(cwd);
635 + if (!upstream) {
636 + return;
637 + }
638 + await this.fetchUpstreamRef(cwd, upstream);
639 }
640
641 async checkoutBranch(input: GitCheckoutInput): Promise<void> {
apps/web/src/lib/gitReactQuery.ts
+13
@@ -1,6 +1,11 @@
1 import type { GitStackedAction, NativeApi } from "@t3tools/contracts";
2 import { mutationOptions, queryOptions, type QueryClient } from "@tanstack/react-query";
3
4 +const GIT_STATUS_STALE_TIME_MS = 5_000;
5 +const GIT_STATUS_REFETCH_INTERVAL_MS = 15_000;
6 +const GIT_BRANCHES_STALE_TIME_MS = 15_000;
7 +const GIT_BRANCHES_REFETCH_INTERVAL_MS = 60_000;
8 +
9 export const gitQueryKeys = {
10 all: ["git"] as const,
11 status: (cwd: string | null) => ["git", "status", cwd] as const,
@@ -21,6 +26,10 @@ export function gitStatusQueryOptions(api: NativeApi | undefined, cwd: string |
26 return api.git.status({ cwd });
27 },
28 enabled: !!api && !!cwd,
29 + staleTime: GIT_STATUS_STALE_TIME_MS,
30 + refetchOnWindowFocus: "always",
31 + refetchOnReconnect: "always",
32 + refetchInterval: GIT_STATUS_REFETCH_INTERVAL_MS,
33 });
34 }
35
@@ -34,6 +43,10 @@ export function gitBranchesQueryOptions(api: NativeApi | undefined, cwd: string
43 return api.git.listBranches({ cwd });
44 },
45 enabled: !!api && !!cwd,
46 + staleTime: GIT_BRANCHES_STALE_TIME_MS,
47 + refetchOnWindowFocus: true,
48 + refetchOnReconnect: true,
49 + refetchInterval: GIT_BRANCHES_REFETCH_INTERVAL_MS,
50 });
51 }
52