Skip to content

Commit 1ba6a87

Browse files
fix(web): sidebar rows drop a branch and worktree that were deleted
The sidebar row always showed the branch and worktree saved on the thread. Its live branch subscription carried only the ref name, so a checkout removed outside T3 Code looked like a detached HEAD and the row kept the stale label while the thread details card showed "Initialize Git". The branch status stream now reports whether the cwd is still a repository, and sidebar and search rows hide the saved branch and worktree once live status says it is not. Older servers omit the field, which keeps today's behavior. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1a1f9ac commit 1ba6a87

7 files changed

Lines changed: 78 additions & 40 deletions

File tree

‎apps/server/src/vcs/VcsStatusBroadcaster.test.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as TestClock from "effect/testing/TestClock";
1919
import * as Tracer from "effect/Tracer";
2020
import type {
2121
BackgroundScope,
22+
VcsBranchStatus,
2223
VcsStatusLocalResult,
2324
VcsStatusRemoteResult,
2425
VcsStatusResult,
@@ -125,6 +126,7 @@ function layerTestFor(state: {
125126
Effect.as(state.currentLocalStatus),
126127
);
127128
}),
129+
repositoryRoot: () => Effect.sync(() => (state.currentLocalStatus.isRepo ? "/repo" : null)),
128130
currentBranch: () =>
129131
state.branchRead
130132
? state.branchRead()
@@ -257,14 +259,17 @@ describe("VcsStatusBroadcaster", () => {
257259
return Effect.gen(function* () {
258260
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
259261
const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" }));
260-
assert.deepEqual(yield* branches, [{ refName: baseLocalStatus.refName }]);
262+
assert.deepEqual(yield* branches, [{ refName: baseLocalStatus.refName, isRepo: true }]);
261263
assert.equal(state.localStatusCalls, 0);
262264
state.currentLocalStatus = { ...baseLocalStatus, refName: "external" };
263265
yield* broadcaster.refreshBranchStatus({ cwd: "/repo" });
264-
assert.deepEqual(yield* branches, [{ refName: "external" }]);
266+
assert.deepEqual(yield* branches, [{ refName: "external", isRepo: true }]);
265267
state.currentLocalStatus = { ...baseLocalStatus, refName: null };
266268
yield* broadcaster.refreshBranchStatus({ cwd: "/repo" });
267-
assert.deepEqual(yield* branches, [{ refName: null }]);
269+
assert.deepEqual(yield* branches, [{ refName: null, isRepo: true }]);
270+
state.currentLocalStatus = { ...baseLocalStatus, isRepo: false, refName: null };
271+
yield* broadcaster.refreshBranchStatus({ cwd: "/repo" });
272+
assert.deepEqual(yield* branches, [{ refName: null, isRepo: false }]);
268273
assert.equal(state.localStatusCalls, 0);
269274
const detailScope = yield* Scope.make();
270275
const details = yield* Stream.toPull(
@@ -289,7 +294,7 @@ describe("VcsStatusBroadcaster", () => {
289294
assert.deepEqual(yield* details, [
290295
{ _tag: "localUpdated", local: state.currentLocalStatus },
291296
]);
292-
assert.deepEqual(yield* branches, [{ refName: "changed" }]);
297+
assert.deepEqual(yield* branches, [{ refName: "changed", isRepo: true }]);
293298
const secondScope = yield* Scope.make();
294299
const secondDetails = yield* Stream.toPull(
295300
broadcaster.streamStatus({ cwd: "/alias", includeRemote: false }),
@@ -327,7 +332,7 @@ describe("VcsStatusBroadcaster", () => {
327332
assert.deepEqual(yield* reopened, [
328333
{ _tag: "localUpdated", local: state.currentLocalStatus },
329334
]);
330-
assert.deepEqual(yield* branches, [{ refName: null }]);
335+
assert.deepEqual(yield* branches, [{ refName: null, isRepo: false }]);
331336
assert.equal(state.remoteStatusCalls, 0);
332337
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
333338
},
@@ -1345,14 +1350,14 @@ describe("VcsStatusBroadcaster", () => {
13451350
yield* Effect.gen(function* () {
13461351
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
13471352
const branches = yield* Stream.toPull(broadcaster.streamBranchStatus({ cwd: "/repo" }));
1348-
assert.deepEqual(yield* branches, [{ refName: "main" }]);
1353+
assert.deepEqual(yield* branches, [{ refName: "main", isRepo: true }]);
13491354

13501355
state.currentLocalStatus = { ...baseLocalStatus, refName: "repro" };
13511356
const lockPath = path.join(gitDir, "HEAD.lock");
13521357
yield* fs.writeFileString(lockPath, "ref: refs/heads/repro\n");
13531358
yield* fs.rename(lockPath, headPath);
13541359

1355-
assert.deepEqual(yield* branches, [{ refName: "repro" }]);
1360+
assert.deepEqual(yield* branches, [{ refName: "repro", isRepo: true }]);
13561361
assert.equal(state.localStatusCalls, 0);
13571362
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
13581363
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
@@ -1712,7 +1717,10 @@ it.effect("publishes overlapping branch reads in checkout order", () => {
17121717
yield* Deferred.succeed(resume, undefined);
17131718
yield* Fiber.join(older);
17141719
yield* Fiber.join(newer);
1715-
assert.deepEqual(yield* branches, [{ refName: "old" }, { refName: "new" }]);
1720+
assert.deepEqual(yield* branches, [
1721+
{ refName: "old", isRepo: true },
1722+
{ refName: "new", isRepo: true },
1723+
]);
17161724
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
17171725
});
17181726

@@ -1722,7 +1730,7 @@ it.effect("does not replay older queued branches after a new subscriber snapshot
17221730
const b = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
17231731
const entered = yield* Deferred.make<void>();
17241732
const resume = yield* Deferred.make<void>();
1725-
const values = yield* Queue.unbounded<{ readonly refName: string | null }>();
1733+
const values = yield* Queue.unbounded<VcsBranchStatus>();
17261734
let reads = 0;
17271735
state.branchRead = () =>
17281736
++reads === 1
@@ -1741,10 +1749,10 @@ it.effect("does not replay older queued branches after a new subscriber snapshot
17411749
);
17421750
yield* Deferred.succeed(resume, undefined);
17431751
yield* Fiber.join(older);
1744-
assert.deepEqual(yield* Queue.take(values), { refName: "new" });
1752+
assert.deepEqual(yield* Queue.take(values), { refName: "new", isRepo: true });
17451753
state.branchRead = () => Effect.succeed("latest");
17461754
yield* b.refreshBranchStatus({ cwd: "/repo" });
1747-
assert.deepEqual(yield* Queue.take(values), { refName: "latest" });
1755+
assert.deepEqual(yield* Queue.take(values), { refName: "latest", isRepo: true });
17481756
yield* Fiber.join(subscriber);
17491757
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
17501758
});

‎apps/server/src/vcs/VcsStatusBroadcaster.ts‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ export const make = Effect.gen(function* () {
275275
PubSub.unbounded<{
276276
readonly cwd: string;
277277
readonly refName: string | null;
278+
readonly isRepo: boolean;
278279
readonly revision: number;
279280
}>(),
280281
(pubsub) => PubSub.shutdown(pubsub),
@@ -285,17 +286,18 @@ export const make = Effect.gen(function* () {
285286
const detailedSubscribers = new Map<string, number>();
286287
const branches = new Map<
287288
string,
288-
{ readonly refName: string | null; readonly revision: number }
289+
{ readonly refName: string | null; readonly isRepo: boolean; readonly revision: number }
289290
>();
290291
let branchRevision = 0;
291292
const publishBranch = Effect.fn("VcsStatusBroadcaster.publishBranch")(function* (
292293
cwd: string,
293-
refName: string | null,
294+
{ refName, isRepo }: { readonly refName: string | null; readonly isRepo: boolean },
294295
) {
295-
if (branches.get(cwd)?.refName === refName) return;
296+
const previous = branches.get(cwd);
297+
if (previous?.refName === refName && previous.isRepo === isRepo) return;
296298
const revision = ++branchRevision;
297-
branches.set(cwd, { refName, revision });
298-
yield* PubSub.publish(branchChangesPubSub, { cwd, refName, revision });
299+
branches.set(cwd, { refName, isRepo, revision });
300+
yield* PubSub.publish(branchChangesPubSub, { cwd, refName, isRepo, revision });
299301
});
300302
const localRefreshLocks = yield* KeyedLock.make<string>();
301303
const cacheRef = yield* Ref.make(new Map<string, CachedVcsStatus>());
@@ -325,7 +327,7 @@ export const make = Effect.gen(function* () {
325327
new Map(cache).set(cwd, { local: nextLocal, remote }),
326328
] as const;
327329
});
328-
yield* publishBranch(cwd, local.refName);
330+
yield* publishBranch(cwd, local);
329331
if (options?.publish && remoteCleared) {
330332
yield* PubSub.publish(changesPubSub, {
331333
cwd,
@@ -771,9 +773,15 @@ export const make = Effect.gen(function* () {
771773
Effect.gen(function* () {
772774
if (detailedSubscribers.has(cwd)) {
773775
const local = yield* getOrLoadLocalStatusUnlocked(cwd);
774-
return { refName: local.refName, revision: branches.get(cwd)?.revision ?? 0 };
776+
return {
777+
refName: local.refName,
778+
isRepo: local.isRepo,
779+
revision: branches.get(cwd)?.revision ?? 0,
780+
};
775781
}
776782
const refName = yield* workflow.currentBranch(cwd);
783+
// A null branch is a detached HEAD or a checkout that is gone, such as a removed worktree.
784+
const isRepo = refName !== null || (yield* workflow.repositoryRoot(cwd)) !== null;
777785
// HEAD cannot certify working-tree totals, even when it has not moved.
778786
localRefreshResults.delete(cwd);
779787
yield* workflow.invalidateLocalStatus(cwd);
@@ -784,10 +792,14 @@ export const make = Effect.gen(function* () {
784792
if (detailedSubscribers.has(cwd)) {
785793
const local = yield* workflow.localStatus({ cwd });
786794
yield* updateCachedLocalStatus(cwd, local, { publish: true });
787-
return { refName: local.refName, revision: branches.get(cwd)?.revision ?? 0 };
795+
return {
796+
refName: local.refName,
797+
isRepo: local.isRepo,
798+
revision: branches.get(cwd)?.revision ?? 0,
799+
};
788800
}
789-
yield* publishBranch(cwd, refName);
790-
return { refName, revision: branches.get(cwd)?.revision ?? 0 };
801+
yield* publishBranch(cwd, { refName, isRepo });
802+
return { refName, isRepo, revision: branches.get(cwd)?.revision ?? 0 };
791803
}),
792804
),
793805
);
@@ -940,10 +952,10 @@ export const make = Effect.gen(function* () {
940952
const refreshBranchStatus: VcsStatusBroadcaster["Service"]["refreshBranchStatus"] = Effect.fn(
941953
"VcsStatusBroadcaster.refreshBranchStatus",
942954
)(function* (input) {
943-
const { refName } = yield* refreshBranchStatusCore(
955+
const { refName, isRepo } = yield* refreshBranchStatusCore(
944956
yield* withFileSystem(normalizeCwd(input.cwd)),
945957
);
946-
return { refName };
958+
return { refName, isRepo };
947959
});
948960

949961
const streamBranchStatus: VcsStatusBroadcaster["Service"]["streamBranchStatus"] = (input) =>
@@ -954,12 +966,16 @@ export const make = Effect.gen(function* () {
954966
yield* Effect.acquireRelease(retainHeadWatcher(cwd), () => releaseHeadWatcher(cwd));
955967
const branch = yield* refreshBranchStatusCore(cwd);
956968
return Stream.concat(
957-
Stream.make({ refName: branch.refName }),
969+
Stream.make({ refName: branch.refName, isRepo: branch.isRepo }),
958970
Stream.fromSubscription(subscription).pipe(
959971
Stream.filter((event) => event.cwd === cwd && event.revision > branch.revision),
960-
Stream.map(({ refName }) => ({ refName })),
972+
Stream.map(({ refName, isRepo }) => ({ refName, isRepo })),
961973
),
962-
).pipe(Stream.changesWith((left, right) => left.refName === right.refName));
974+
).pipe(
975+
Stream.changesWith(
976+
(left, right) => left.refName === right.refName && left.isRepo === right.isRepo,
977+
),
978+
);
963979
}),
964980
);
965981

‎apps/web/src/components/Sidebar.logic.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ export function useSnoozeWakeClock(
163163
return clockMs;
164164
}
165165

166+
// Live status that says the cwd is not a repository means the checkout was removed.
167+
export function withoutRemovedCheckout<
168+
T extends { readonly branch: string | null; readonly worktreePath: string | null },
169+
>(thread: T, gitStatus: { readonly isRepo?: boolean | undefined } | null): T {
170+
return gitStatus?.isRepo === false ? { ...thread, branch: null, worktreePath: null } : thread;
171+
}
172+
166173
export function useRetainedValue<T>(key: string | null, value: T | null): T | null {
167174
const [retained, setRetained] = React.useState<{
168175
readonly key: string;

‎apps/web/src/components/sidebar/SidebarThreadRow.tsx‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import {
9090
shouldRecedeSidebarThread,
9191
resolveWorkingStartedAt,
9292
useRetainedValue,
93+
withoutRemovedCheckout,
9394
useSidebarRowSubscriptionLease,
9495
setFileDragActive,
9596
} from "../Sidebar.logic";
@@ -719,6 +720,7 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
719720
JSON.stringify([thread.environmentId, gitCwd]),
720721
gitStatus.data,
721722
);
723+
const checkoutThread = withoutRemovedCheckout(thread, visibleGitStatus);
722724
const pr = linkedPullRequestStatus?.pr ?? null;
723725
const supportsMultiplePullRequests = useSupportsMultiplePullRequests(thread.environmentId);
724726
const currentLinkedPr = supportsMultiplePullRequests
@@ -824,11 +826,11 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
824826
: thread.modelSelection.model;
825827

826828
const isRemote = thread.environmentId !== props.currentEnvironmentId;
827-
const showsScratchMachine = !thread.branch && props.scratchMachineLabel !== null;
829+
const showsScratchMachine = !checkoutThread.branch && props.scratchMachineLabel !== null;
828830

829831
const detailsTooltip = (
830832
<SidebarThreadTooltip
831-
thread={thread}
833+
thread={checkoutThread}
832834
project={props.project}
833835
projectDisplayName={props.projectDisplayName}
834836
environmentLabel={props.environmentLabel}
@@ -1157,18 +1159,18 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP
11571159
const diff = latestRunDiff(thread);
11581160
// Only the trailing icons would remain, so the row moves them to the title line instead.
11591161
const metaLineEmpty =
1160-
!thread.branch &&
1162+
!checkoutThread.branch &&
11611163
!showsScratchMachine &&
11621164
prBadge === null &&
11631165
diff === null &&
11641166
runningTerminalIds.length === 0;
11651167
const metaLine = metaLineEmpty ? null : (
11661168
<div className="mt-0.5 flex min-w-0 items-center gap-1.5 text-secondary-label text-xs">
1167-
{thread.branch ? (
1169+
{checkoutThread.branch ? (
11681170
<>
1169-
<ThreadWorktreeIndicator thread={thread} />
1171+
<ThreadWorktreeIndicator thread={checkoutThread} />
11701172
<span className="flex min-w-0 flex-1">
1171-
<MiddleTruncate value={thread.branch} showTitle={false} />
1173+
<MiddleTruncate value={checkoutThread.branch} showTitle={false} />
11721174
</span>
11731175
</>
11741176
) : showsScratchMachine ? (
@@ -1685,6 +1687,7 @@ export const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props
16851687
JSON.stringify([thread.environmentId, gitCwd]),
16861688
gitStatus.data,
16871689
);
1690+
const checkoutThread = withoutRemovedCheckout(thread, visibleGitStatus);
16881691
const branchMismatch = resolveLocalCheckoutBranchMismatch({
16891692
effectiveEnvMode: thread.worktreePath === null ? "local" : "worktree",
16901693
activeWorktreePath: thread.worktreePath,
@@ -1800,7 +1803,7 @@ export const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props
18001803
</span>
18011804
</TooltipTrigger>
18021805
<SidebarThreadTooltip
1803-
thread={thread}
1806+
thread={checkoutThread}
18041807
project={props.project}
18051808
projectDisplayName={props.projectDisplayName}
18061809
environmentLabel={props.environmentLabel}

‎packages/client-runtime/src/rpc/client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ export const subscribeVcsBranch = (
485485
includeRemote: false,
486486
}).pipe(
487487
Stream.filter((event) => event._tag !== "remoteUpdated"),
488-
Stream.map((event) => ({ refName: event.local.refName })),
488+
Stream.map(({ local }) => ({ refName: local.refName, isRepo: local.isRepo })),
489489
),
490490
),
491491
),

‎packages/client-runtime/src/state/vcs.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe("branch subscriptions", () => {
140140
return Stream.concat(
141141
Stream.make(
142142
branchSupported
143-
? { refName }
143+
? { refName, isRepo: true }
144144
: { _tag: "snapshot", local: status({ refName }), remote: null },
145145
),
146146
Stream.never,
@@ -183,19 +183,19 @@ describe("branch subscriptions", () => {
183183
const first = yield* Stream.toPull(
184184
AtomRegistry.toStreamResult(registry, atoms.branch(target)),
185185
);
186-
expect(yield* first).toEqual([{ refName: "main" }]);
186+
expect(yield* first).toEqual([{ refName: "main", isRepo: true }]);
187187
const second = yield* Stream.toPull(
188188
AtomRegistry.toStreamResult(registry, atoms.branch(target)),
189189
);
190-
expect(yield* second).toEqual([{ refName: "main" }]);
190+
expect(yield* second).toEqual([{ refName: "main", isRepo: true }]);
191191
expect(subscriptions).toBe(1);
192192
yield* SubscriptionRef.set(
193193
activeSession,
194194
Option.some(branchSession("external", !supported)),
195195
);
196196
expect(yield* Queue.take(released)).toBe("main");
197-
expect(yield* first).toEqual([{ refName: "external" }]);
198-
expect(yield* second).toEqual([{ refName: "external" }]);
197+
expect(yield* first).toEqual([{ refName: "external", isRepo: true }]);
198+
expect(yield* second).toEqual([{ refName: "external", isRepo: true }]);
199199
registry.dispose();
200200
expect(yield* Queue.take(released)).toBe("external");
201201
}).pipe(Effect.scoped),

‎packages/contracts/src/git.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ export const VcsStatusInput = Schema.Struct({
111111
});
112112
export type VcsStatusInput = typeof VcsStatusInput.Type;
113113

114-
export const VcsBranchStatus = Schema.Struct({ refName: Schema.NullOr(Schema.String) });
114+
export const VcsBranchStatus = Schema.Struct({
115+
refName: Schema.NullOr(Schema.String),
116+
/** Absent from older servers, where a removed checkout looks like a detached HEAD. */
117+
isRepo: Schema.optional(Schema.Boolean),
118+
});
115119
export type VcsBranchStatus = typeof VcsBranchStatus.Type;
116120

117121
export const VcsStatusSubscriptionInput = Schema.Struct({

0 commit comments

Comments
 (0)