Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
Guard VCS status updates against stale targets (#3084)
Julius Marminge1db585878ebc
diff --git a/apps/web/src/components/ChatView.browser.tsx b/apps/web/src/components/ChatView.browser.tsxindex 81b9c7423..583f6096b 100644--- a/apps/web/src/components/ChatView.browser.tsx+++ b/apps/web/src/components/ChatView.browser.tsx@@ -100,6 +100,7 @@ vi.mock("../lib/vcsStatusState", () => {};return {+ getVcsStatusDataForTarget: (state: typeof status) => state.data,getVcsStatusSnapshot: () => status,useVcsStatus: () => status,useVcsStatuses: () => new Map(),diff --git a/apps/web/src/components/GitActionsControl.browser.tsx b/apps/web/src/components/GitActionsControl.browser.tsxindex 0a79f2f2c..996bf5ff8 100644--- a/apps/web/src/components/GitActionsControl.browser.tsx+++ b/apps/web/src/components/GitActionsControl.browser.tsx@@ -94,6 +94,7 @@ vi.mock("~/lib/sourceControlActions", () => ({}));vi.mock("~/lib/vcsStatusState", () => ({+ getVcsStatusDataForTarget: (state: { data: unknown }) => state.data,refreshVcsStatus: refreshVcsStatusSpy,resetVcsStatusStateForTests: () => undefined,useVcsStatus: vi.fn(() => ({diff --git a/apps/web/src/components/GitActionsControl.logic.test.ts b/apps/web/src/components/GitActionsControl.logic.test.tsindex 95c6ae7aa..3781fad83 100644--- a/apps/web/src/components/GitActionsControl.logic.test.ts+++ b/apps/web/src/components/GitActionsControl.logic.test.ts@@ -1100,6 +1100,15 @@ describe("resolveLiveThreadBranchUpdate", () => {assert.equal(update, null);});++ it("allows a temporary worktree ref to reconcile to a semantic branch", () => {+ const update = resolveLiveThreadBranchUpdate({+ threadBranch: "t3code/a9628676",+ gitStatus: status({ refName: "feature/diff-panel-toggle" }),+ });++ assert.deepEqual(update, { branch: "feature/diff-panel-toggle" });+ });});describe("resolveAutoFeatureBranchName", () => {diff --git a/apps/web/src/components/GitActionsControl.tsx b/apps/web/src/components/GitActionsControl.tsxindex 6a8dc53fb..8c7356e28 100644--- a/apps/web/src/components/GitActionsControl.tsx+++ b/apps/web/src/components/GitActionsControl.tsx@@ -71,7 +71,7 @@ import {useVcsInitAction,useVcsPullAction,} from "~/lib/sourceControlActions";-import { refreshVcsStatus, useVcsStatus } from "~/lib/vcsStatusState";+import { getVcsStatusDataForTarget, refreshVcsStatus, useVcsStatus } from "~/lib/vcsStatusState";import { useSourceControlDiscovery } from "~/lib/sourceControlDiscoveryState";import { newCommandId, randomUUID } from "~/lib/utils";import { resolvePathLinkTarget } from "~/terminal-links";@@ -1058,10 +1058,13 @@ export default function GitActionsControl({[persistThreadBranchSync],);- const { data: gitStatus, error: gitStatusError } = useVcsStatus({- environmentId: activeEnvironmentId,- cwd: gitCwd,- });+ const vcsStatusTarget = useMemo(+ () => ({ environmentId: activeEnvironmentId, cwd: gitCwd }),+ [activeEnvironmentId, gitCwd],+ );+ const gitStatusQuery = useVcsStatus(vcsStatusTarget);+ const { error: gitStatusError } = gitStatusQuery;+ const gitStatus = getVcsStatusDataForTarget(gitStatusQuery, vcsStatusTarget);const sourceControlPresentation = useMemo(() => getSourceControlPresentation(gitStatus?.sourceControlProvider),[gitStatus?.sourceControlProvider],diff --git a/apps/web/src/components/KeybindingsToast.browser.tsx b/apps/web/src/components/KeybindingsToast.browser.tsxindex 01f501b0a..127810053 100644--- a/apps/web/src/components/KeybindingsToast.browser.tsx+++ b/apps/web/src/components/KeybindingsToast.browser.tsx@@ -67,6 +67,7 @@ vi.mock("../lib/vcsStatusState", () => {};return {+ getVcsStatusDataForTarget: (state: typeof status) => state.data,getVcsStatusSnapshot: () => status,useVcsStatus: () => status,useVcsStatuses: () => new Map(),diff --git a/apps/web/src/lib/vcsStatusState.ts b/apps/web/src/lib/vcsStatusState.tsindex 986f1c589..6d0bba3bd 100644--- a/apps/web/src/lib/vcsStatusState.ts+++ b/apps/web/src/lib/vcsStatusState.ts@@ -6,6 +6,7 @@ import {EMPTY_VCS_STATUS_ATOM,EMPTY_VCS_STATUS_STATE,createVcsStatusManager,+ getVcsStatusDataForTarget,getVcsStatusTargetKey,vcsStatusStateAtom,} from "@t3tools/client-runtime";@@ -19,6 +20,7 @@ import {import { appAtomRegistry } from "../rpc/atomRegistry";export type { VcsStatusState, VcsStatusTarget };+export { getVcsStatusDataForTarget };const manager = createVcsStatusManager({getRegistry: () => appAtomRegistry,diff --git a/packages/client-runtime/src/gitActions.test.ts b/packages/client-runtime/src/gitActions.test.tsnew file mode 100644index 000000000..39c671834--- /dev/null+++ b/packages/client-runtime/src/gitActions.test.ts@@ -0,0 +1,43 @@+import type { VcsStatusResult } from "@t3tools/contracts";+import { assert, describe, it } from "vite-plus/test";++import { resolveLiveThreadBranchUpdate } from "./gitActions.js";++function status(refName: string): VcsStatusResult {+ return {+ isRepo: true,+ hasPrimaryRemote: true,+ isDefaultRef: refName === "main",+ refName,+ hasWorkingTreeChanges: false,+ workingTree: {+ files: [],+ insertions: 0,+ deletions: 0,+ },+ hasUpstream: true,+ aheadCount: 0,+ behindCount: 0,+ pr: null,+ };+}++describe("resolveLiveThreadBranchUpdate", () => {+ it("allows a temporary worktree ref to reconcile to a semantic branch", () => {+ const update = resolveLiveThreadBranchUpdate({+ threadBranch: "t3code/a9628676",+ gitStatus: status("feature/diff-panel-toggle"),+ });++ assert.deepEqual(update, { branch: "feature/diff-panel-toggle" });+ });++ it("still reconciles ordinary semantic branch changes", () => {+ const update = resolveLiveThreadBranchUpdate({+ threadBranch: "feature/old",+ gitStatus: status("feature/new"),+ });++ assert.deepEqual(update, { branch: "feature/new" });+ });+});diff --git a/packages/client-runtime/src/vcsStatusState.test.ts b/packages/client-runtime/src/vcsStatusState.test.tsindex bb49abf34..c671cb497 100644--- a/packages/client-runtime/src/vcsStatusState.test.ts+++ b/packages/client-runtime/src/vcsStatusState.test.ts@@ -2,7 +2,11 @@ import { EnvironmentId, type VcsStatusResult } from "@t3tools/contracts";import { AtomRegistry } from "effect/unstable/reactivity";import { afterEach, describe, expect, it, vi } from "vite-plus/test";-import { type VcsStatusClient, createVcsStatusManager } from "./vcsStatusState.ts";+import {+ type VcsStatusClient,+ createVcsStatusManager,+ getVcsStatusDataForTarget,+} from "./vcsStatusState.ts";/* ─── Test helpers ──────────────────────────────────────────────────── */@@ -57,12 +61,24 @@ function createMockClient(): {};}-const PENDING = { data: null, error: null, cause: null, isPending: true };-const EMPTY = { data: null, error: null, cause: null, isPending: false };-const TARGET = { environmentId: EnvironmentId.make("env-local"), cwd: "/repo" } as const;const FRESH_TARGET = { environmentId: EnvironmentId.make("env-local"), cwd: "/fresh" } as const;const OTHER_ENV_TARGET = { environmentId: EnvironmentId.make("env-remote"), cwd: "/repo" } as const;+const TARGET_KEY = "env-local:/repo";+const PENDING = {+ targetKey: TARGET_KEY,+ data: null,+ error: null,+ cause: null,+ isPending: true,+};+const EMPTY = {+ targetKey: null,+ data: null,+ error: null,+ cause: null,+ isPending: false,+};/* ─── Tests ─────────────────────────────────────────────────────────── */@@ -99,6 +115,7 @@ describe("createVcsStatusManager", () => {emit(BASE_STATUS);expect(manager.getSnapshot(TARGET)).toEqual({+ targetKey: TARGET_KEY,data: BASE_STATUS,error: null,cause: null,@@ -130,6 +147,7 @@ describe("createVcsStatusManager", () => {// Snapshot still reflects stream data, not the refresh responseexpect(manager.getSnapshot(TARGET)).toEqual({+ targetKey: TARGET_KEY,data: BASE_STATUS,error: null,cause: null,@@ -160,6 +178,19 @@ describe("createVcsStatusManager", () => {releaseRemote();});+ it("rejects status data from a previous cwd during target transitions", () => {+ const staleState = {+ targetKey: TARGET_KEY,+ data: BASE_STATUS,+ error: null,+ cause: null,+ isPending: false,+ };++ expect(getVcsStatusDataForTarget(staleState, FRESH_TARGET)).toBeNull();+ expect(getVcsStatusDataForTarget(staleState, TARGET)).toBe(BASE_STATUS);+ });+it("returns null from refresh when no client is available", async () => {const manager = createVcsStatusManager({getRegistry: () => atomRegistry,@@ -204,6 +235,7 @@ describe("createVcsStatusManager", () => {mock.emit(BASE_STATUS);expect(manager.getSnapshot(TARGET)).toEqual({+ targetKey: TARGET_KEY,data: BASE_STATUS,error: null,cause: null,@@ -241,6 +273,7 @@ describe("createVcsStatusManager", () => {for (const listener of connectionListeners) listener();expect(manager.getSnapshot(TARGET)).toEqual({+ targetKey: TARGET_KEY,data: BASE_STATUS,error: null,cause: null,diff --git a/packages/client-runtime/src/vcsStatusState.ts b/packages/client-runtime/src/vcsStatusState.tsindex 38379a7fe..08c8f6227 100644--- a/packages/client-runtime/src/vcsStatusState.ts+++ b/packages/client-runtime/src/vcsStatusState.ts@@ -7,6 +7,7 @@ import type { WsRpcClient } from "./wsRpcClient.ts";/* ─── Types ─────────────────────────────────────────────────────────── */export interface VcsStatusState {+ readonly targetKey: string | null;readonly data: VcsStatusResult | null;readonly error: GitManagerServiceError | null;readonly cause: Cause.Cause<GitManagerServiceError> | null;@@ -30,18 +31,22 @@ interface WatchedEntry {const NOOP: () => void = () => undefined;export const EMPTY_VCS_STATUS_STATE = Object.freeze<VcsStatusState>({+ targetKey: null,data: null,error: null,cause: null,isPending: false,});-const INITIAL_VCS_STATUS_STATE = Object.freeze<VcsStatusState>({- data: null,- error: null,- cause: null,- isPending: true,-});+function initialVcsStatusState(targetKey: string): VcsStatusState {+ return {+ targetKey,+ data: null,+ error: null,+ cause: null,+ isPending: true,+ };+}/* ─── Atoms ─────────────────────────────────────────────────────────── */@@ -49,7 +54,7 @@ const knownVcsStatusKeys = new Set<string>();export const vcsStatusStateAtom = Atom.family((key: string) => {knownVcsStatusKeys.add(key);- return Atom.make(INITIAL_VCS_STATUS_STATE).pipe(+ return Atom.make(initialVcsStatusState(key)).pipe(Atom.keepAlive,Atom.withLabel(`vcs-status:${key}`),);@@ -69,6 +74,14 @@ export function getVcsStatusTargetKey(target: VcsStatusTarget): string | null {return `${target.environmentId}:${target.cwd}`;}+export function getVcsStatusDataForTarget(+ state: VcsStatusState,+ target: VcsStatusTarget,+): VcsStatusResult | null {+ const targetKey = getVcsStatusTargetKey(target);+ return targetKey !== null && state.targetKey === targetKey ? state.data : null;+}+/* ─── Subscription manager ──────────────────────────────────────────── */export interface VcsStatusManagerConfig {@@ -107,7 +120,7 @@ export function createVcsStatusManager(config: VcsStatusManagerConfig) {const current = config.getRegistry().get(atom);const next: VcsStatusState =current.data === null- ? INITIAL_VCS_STATUS_STATE+ ? initialVcsStatusState(targetKey): { ...current, error: null, cause: null, isPending: true };if (current.data === next.data &&@@ -122,6 +135,7 @@ export function createVcsStatusManager(config: VcsStatusManagerConfig) {function setData(targetKey: string, status: VcsStatusResult): void {config.getRegistry().set(vcsStatusStateAtom(targetKey), {+ targetKey,data: status,error: null,cause: null,@@ -283,7 +297,7 @@ export function createVcsStatusManager(config: VcsStatusManagerConfig) {refreshInFlight.clear();lastRefreshAt.clear();for (const key of knownVcsStatusKeys) {- config.getRegistry().set(vcsStatusStateAtom(key), INITIAL_VCS_STATUS_STATE);+ config.getRegistry().set(vcsStatusStateAtom(key), initialVcsStatusState(key));}knownVcsStatusKeys.clear();}