Guard VCS status updates against stale targets (#3084)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-06-14 11:41:12 -07:00
1 parent 56e57de947
commit 1db585878e
9 files changed
+125 -18

No files matched your search

@@ -100,6 +100,7 @@ vi.mock("../lib/vcsStatusState", () => {
};
return {
getVcsStatusDataForTarget: (state: typeof status) => state.data,
getVcsStatusSnapshot: () => status,
useVcsStatus: () => status,
useVcsStatuses: () => new Map(),
@@ -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(() => ({
@@ -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", () => {
@@ -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],
@@ -67,6 +67,7 @@ vi.mock("../lib/vcsStatusState", () => {
};
return {
getVcsStatusDataForTarget: (state: typeof status) => state.data,
getVcsStatusSnapshot: () => status,
useVcsStatus: () => status,
useVcsStatuses: () => new Map(),
+2
View File
@@ -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,
@@ -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" });
});
});
@@ -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 response
expect(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,
+23 -9
View File
@@ -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();
}