Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit 2f41c07

Browse files
t3dotggclaude
andauthored
Stop new threads inheriting checkout/branch from viewed thread (pingdotgg#4411)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 51672b6 commit 2f41c07

14 files changed

Lines changed: 425 additions & 385 deletions

‎apps/web/src/components/BranchToolbar.logic.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,100 @@ import {
1212
resolveBranchToolbarValue,
1313
resolveLockedWorkspaceLabel,
1414
resolveLocalCheckoutBranchMismatch,
15+
resolvePreviousWorktreeLabel,
16+
resolvePreviousWorktreeSeed,
1517
shouldIncludeBranchPickerItem,
1618
shouldShowEnvironmentIndicator,
1719
} from "./BranchToolbar.logic";
1820

1921
const localEnvironmentId = EnvironmentId.make("environment-local");
2022
const remoteEnvironmentId = EnvironmentId.make("environment-remote");
2123

24+
describe("resolvePreviousWorktreeSeed", () => {
25+
it("picks the most recently updated worktree thread", () => {
26+
expect(
27+
resolvePreviousWorktreeSeed({
28+
threads: [
29+
{
30+
branch: "t3/older",
31+
worktreePath: "/repo/.t3/worktrees/older",
32+
updatedAt: "2026-07-20T00:00:00.000Z",
33+
},
34+
{
35+
branch: "t3/newer",
36+
worktreePath: "/repo/.t3/worktrees/newer",
37+
updatedAt: "2026-07-22T00:00:00.000Z",
38+
},
39+
{ branch: "main", worktreePath: null, updatedAt: "2026-07-23T00:00:00.000Z" },
40+
],
41+
currentWorktreePath: null,
42+
}),
43+
).toEqual({ branch: "t3/newer", worktreePath: "/repo/.t3/worktrees/newer" });
44+
});
45+
46+
it("skips the worktree the composer already points at", () => {
47+
expect(
48+
resolvePreviousWorktreeSeed({
49+
threads: [
50+
{
51+
branch: "t3/current",
52+
worktreePath: "/repo/.t3/worktrees/current",
53+
updatedAt: "2026-07-22T00:00:00.000Z",
54+
},
55+
],
56+
currentWorktreePath: "/repo/.t3/worktrees/current",
57+
}),
58+
).toBeNull();
59+
});
60+
61+
it("returns null when no thread has a worktree", () => {
62+
expect(
63+
resolvePreviousWorktreeSeed({
64+
threads: [{ branch: "main", worktreePath: null, updatedAt: "2026-07-22T00:00:00.000Z" }],
65+
currentWorktreePath: null,
66+
}),
67+
).toBeNull();
68+
});
69+
70+
it("ignores archived threads and threads with unparseable timestamps", () => {
71+
expect(
72+
resolvePreviousWorktreeSeed({
73+
threads: [
74+
{
75+
branch: "t3/archived",
76+
worktreePath: "/repo/.t3/worktrees/archived",
77+
updatedAt: "2026-07-23T00:00:00.000Z",
78+
archivedAt: "2026-07-23T01:00:00.000Z",
79+
},
80+
{
81+
branch: "t3/garbage-timestamp",
82+
worktreePath: "/repo/.t3/worktrees/garbage",
83+
updatedAt: "not-a-date",
84+
},
85+
{
86+
branch: "t3/live",
87+
worktreePath: "/repo/.t3/worktrees/live",
88+
updatedAt: "2026-07-21T00:00:00.000Z",
89+
archivedAt: null,
90+
},
91+
],
92+
currentWorktreePath: null,
93+
}),
94+
).toEqual({ branch: "t3/live", worktreePath: "/repo/.t3/worktrees/live" });
95+
});
96+
});
97+
98+
describe("resolvePreviousWorktreeLabel", () => {
99+
it("includes the branch when known", () => {
100+
expect(resolvePreviousWorktreeLabel({ branch: "t3/fix-thing", worktreePath: "/wt" })).toBe(
101+
"Previous worktree (t3/fix-thing)",
102+
);
103+
expect(resolvePreviousWorktreeLabel({ branch: null, worktreePath: "/wt" })).toBe(
104+
"Previous worktree",
105+
);
106+
});
107+
});
108+
22109
describe("resolveDraftEnvModeAfterBranchChange", () => {
23110
it("switches to local mode when returning from an existing worktree to the main worktree", () => {
24111
expect(

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { EnvironmentId, VcsRef, ProjectId } from "@t3tools/contracts";
22
import * as Schema from "effect/Schema";
3+
import { toSortableTimestamp } from "../lib/threadSort";
34
export {
45
dedupeRemoteBranchesWithLocalMatches,
56
deriveLocalBranchNameFromRemoteRef,
@@ -65,6 +66,53 @@ export function resolveLockedWorkspaceLabel(activeWorktreePath: string | null):
6566
return activeWorktreePath ? "Worktree" : "Local checkout";
6667
}
6768

69+
export interface PreviousWorktreeSeed {
70+
branch: string | null;
71+
worktreePath: string;
72+
}
73+
74+
// The most recently touched worktree in the project that the composer isn't
75+
// already pointing at. Backs the "Previous worktree" entry in the workspace
76+
// selector so a follow-up thread can hop back into the worktree you just
77+
// worked in without hunting for its branch. Archived threads don't compete —
78+
// the rest of the UI hides them, so their worktrees shouldn't resurface here.
79+
export function resolvePreviousWorktreeSeed(input: {
80+
threads: ReadonlyArray<{
81+
branch: string | null;
82+
worktreePath: string | null;
83+
updatedAt: string;
84+
archivedAt?: string | null;
85+
}>;
86+
currentWorktreePath: string | null;
87+
}): PreviousWorktreeSeed | null {
88+
let latest: { branch: string | null; worktreePath: string; updatedAt: number } | null = null;
89+
for (const thread of input.threads) {
90+
if (
91+
!thread.worktreePath ||
92+
thread.worktreePath === input.currentWorktreePath ||
93+
(thread.archivedAt ?? null) !== null
94+
) {
95+
continue;
96+
}
97+
const updatedAt = toSortableTimestamp(thread.updatedAt);
98+
if (updatedAt === null) {
99+
continue;
100+
}
101+
if (latest === null || updatedAt > latest.updatedAt) {
102+
latest = {
103+
branch: thread.branch,
104+
worktreePath: thread.worktreePath,
105+
updatedAt,
106+
};
107+
}
108+
}
109+
return latest === null ? null : { branch: latest.branch, worktreePath: latest.worktreePath };
110+
}
111+
112+
export function resolvePreviousWorktreeLabel(seed: PreviousWorktreeSeed): string {
113+
return seed.branch ? `Previous worktree (${seed.branch})` : "Previous worktree";
114+
}
115+
68116
export function resolveEffectiveEnvMode(input: {
69117
activeWorktreePath: string | null;
70118
hasServerThread: boolean;

‎apps/web/src/components/BranchToolbar.tsx‎

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import {
66
FolderGit2Icon,
77
FolderGitIcon,
88
FolderIcon,
9+
HistoryIcon,
910
MonitorIcon,
1011
} from "lucide-react";
11-
import { memo, useMemo } from "react";
12+
import { memo, useCallback, useMemo } from "react";
1213

1314
import { useComposerDraftStore, type DraftId } from "../composerDraftStore";
14-
import { useProject, useThread } from "../state/entities";
15+
import { useProject, useThread, useThreadShellsForProjectRefs } from "../state/entities";
1516
import { useIsMobile } from "../hooks/useMediaQuery";
1617
import {
1718
type EnvMode,
@@ -20,6 +21,8 @@ import {
2021
resolveEnvModeLabel,
2122
resolveEffectiveEnvMode,
2223
resolveLockedWorkspaceLabel,
24+
resolvePreviousWorktreeLabel,
25+
resolvePreviousWorktreeSeed,
2326
shouldShowEnvironmentIndicator,
2427
} from "./BranchToolbar.logic";
2528
import { BranchToolbarBranchSelector } from "./BranchToolbarBranchSelector";
@@ -66,6 +69,8 @@ interface MobileRunContextSelectorProps {
6669
effectiveEnvMode: EnvMode;
6770
activeWorktreePath: string | null;
6871
onEnvModeChange: (mode: EnvMode) => void;
72+
previousWorktreeLabel: string | null;
73+
onUsePreviousWorktree: () => void;
6974
}
7075

7176
const MobileRunContextSelector = memo(function MobileRunContextSelector({
@@ -79,6 +84,8 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
7984
effectiveEnvMode,
8085
activeWorktreePath,
8186
onEnvModeChange,
87+
previousWorktreeLabel,
88+
onUsePreviousWorktree,
8289
}: MobileRunContextSelectorProps) {
8390
const activeEnvironment = useMemo(
8491
() => availableEnvironments?.find((env) => env.environmentId === environmentId) ?? null,
@@ -166,7 +173,13 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
166173
<MenuGroupLabel>Workspace</MenuGroupLabel>
167174
<MenuRadioGroup
168175
value={effectiveEnvMode}
169-
onValueChange={(value) => onEnvModeChange(value as EnvMode)}
176+
onValueChange={(value) => {
177+
if (value === "previous-worktree") {
178+
onUsePreviousWorktree();
179+
return;
180+
}
181+
onEnvModeChange(value as EnvMode);
182+
}}
170183
>
171184
<MenuRadioItem disabled={envModeLocked} value="local">
172185
<span className="flex min-w-0 items-center gap-1.5">
@@ -186,6 +199,14 @@ const MobileRunContextSelector = memo(function MobileRunContextSelector({
186199
<span className="min-w-0 truncate">{resolveEnvModeLabel("worktree")}</span>
187200
</span>
188201
</MenuRadioItem>
202+
{previousWorktreeLabel ? (
203+
<MenuRadioItem disabled={envModeLocked} value="previous-worktree">
204+
<span className="flex min-w-0 items-center gap-1.5">
205+
<HistoryIcon className="size-3" />
206+
<span className="min-w-0 truncate">{previousWorktreeLabel}</span>
207+
</span>
208+
</MenuRadioItem>
209+
) : null}
189210
</MenuRadioGroup>
190211
</MenuGroup>
191212
</MenuPopup>
@@ -217,6 +238,7 @@ export const BranchToolbar = memo(function BranchToolbar({
217238
const draftThread = useComposerDraftStore((store) =>
218239
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),
219240
);
241+
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
220242
const activeProjectRef = serverThread
221243
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
222244
: draftThread
@@ -234,6 +256,40 @@ export const BranchToolbar = memo(function BranchToolbar({
234256
});
235257
const envModeLocked = envLocked || (serverThread !== null && activeWorktreePath !== null);
236258

259+
// "Previous worktree" hops a draft into the most recently active worktree
260+
// of this project — the "keep going where I just was" follow-up flow. Only
261+
// drafts can hop; started server threads have their workspace pinned.
262+
const canUsePreviousWorktree = draftThread !== null && serverThread === null && !envModeLocked;
263+
const projectRefsForWorktreeLookup = useMemo(
264+
() => (canUsePreviousWorktree && activeProjectRef ? [activeProjectRef] : []),
265+
[canUsePreviousWorktree, activeProjectRef],
266+
);
267+
const projectThreads = useThreadShellsForProjectRefs(projectRefsForWorktreeLookup);
268+
const previousWorktreeSeed = useMemo(
269+
() =>
270+
canUsePreviousWorktree
271+
? resolvePreviousWorktreeSeed({
272+
threads: projectThreads,
273+
currentWorktreePath: activeWorktreePath,
274+
})
275+
: null,
276+
[activeWorktreePath, canUsePreviousWorktree, projectThreads],
277+
);
278+
const previousWorktreeLabel = previousWorktreeSeed
279+
? resolvePreviousWorktreeLabel(previousWorktreeSeed)
280+
: null;
281+
const onUsePreviousWorktree = useCallback(() => {
282+
if (!previousWorktreeSeed || !activeProjectRef) return;
283+
// Same shape the branch selector writes when picking a branch that
284+
// already lives in a worktree: point the draft at the existing tree.
285+
setDraftThreadContext(draftId ?? threadRef, {
286+
branch: previousWorktreeSeed.branch,
287+
worktreePath: previousWorktreeSeed.worktreePath,
288+
envMode: "worktree",
289+
projectRef: activeProjectRef,
290+
});
291+
}, [activeProjectRef, draftId, previousWorktreeSeed, setDraftThreadContext, threadRef]);
292+
237293
const showEnvironmentPicker = Boolean(
238294
availableEnvironments && availableEnvironments.length > 1 && onEnvironmentChange,
239295
);
@@ -261,6 +317,8 @@ export const BranchToolbar = memo(function BranchToolbar({
261317
effectiveEnvMode={effectiveEnvMode}
262318
activeWorktreePath={activeWorktreePath}
263319
onEnvModeChange={onEnvModeChange}
320+
previousWorktreeLabel={previousWorktreeLabel}
321+
onUsePreviousWorktree={onUsePreviousWorktree}
264322
/>
265323
) : (
266324
<div className="flex min-w-0 shrink-0 items-center gap-1">
@@ -280,6 +338,8 @@ export const BranchToolbar = memo(function BranchToolbar({
280338
effectiveEnvMode={effectiveEnvMode}
281339
activeWorktreePath={activeWorktreePath}
282340
onEnvModeChange={onEnvModeChange}
341+
previousWorktreeLabel={previousWorktreeLabel}
342+
onUsePreviousWorktree={onUsePreviousWorktree}
283343
/>
284344
</div>
285345
)}

‎apps/web/src/components/BranchToolbarEnvModeSelector.tsx‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FolderGit2Icon, FolderGitIcon, FolderIcon } from "lucide-react";
1+
import { FolderGit2Icon, FolderGitIcon, FolderIcon, HistoryIcon } from "lucide-react";
22
import { memo, useMemo } from "react";
33

44
import {
@@ -17,25 +17,35 @@ import {
1717
SelectValue,
1818
} from "./ui/select";
1919

20+
export const PREVIOUS_WORKTREE_SELECT_VALUE = "previous-worktree";
21+
2022
interface BranchToolbarEnvModeSelectorProps {
2123
envLocked: boolean;
2224
effectiveEnvMode: EnvMode;
2325
activeWorktreePath: string | null;
2426
onEnvModeChange: (mode: EnvMode) => void;
27+
previousWorktreeLabel?: string | null;
28+
onUsePreviousWorktree?: () => void;
2529
}
2630

2731
export const BranchToolbarEnvModeSelector = memo(function BranchToolbarEnvModeSelector({
2832
envLocked,
2933
effectiveEnvMode,
3034
activeWorktreePath,
3135
onEnvModeChange,
36+
previousWorktreeLabel,
37+
onUsePreviousWorktree,
3238
}: BranchToolbarEnvModeSelectorProps) {
39+
const showPreviousWorktree = Boolean(previousWorktreeLabel && onUsePreviousWorktree);
3340
const envModeItems = useMemo(
3441
() => [
3542
{ value: "local", label: resolveCurrentWorkspaceLabel(activeWorktreePath) },
3643
{ value: "worktree", label: resolveEnvModeLabel("worktree") },
44+
...(showPreviousWorktree && previousWorktreeLabel
45+
? [{ value: PREVIOUS_WORKTREE_SELECT_VALUE, label: previousWorktreeLabel }]
46+
: []),
3747
],
38-
[activeWorktreePath],
48+
[activeWorktreePath, previousWorktreeLabel, showPreviousWorktree],
3949
);
4050

4151
if (envLocked) {
@@ -60,7 +70,13 @@ export const BranchToolbarEnvModeSelector = memo(function BranchToolbarEnvModeSe
6070
<Select
6171
modal={false}
6272
value={effectiveEnvMode}
63-
onValueChange={(value) => onEnvModeChange(value as EnvMode)}
73+
onValueChange={(value: string | null) => {
74+
if (value === PREVIOUS_WORKTREE_SELECT_VALUE) {
75+
onUsePreviousWorktree?.();
76+
return;
77+
}
78+
onEnvModeChange(value as EnvMode);
79+
}}
6480
items={envModeItems}
6581
>
6682
<SelectTrigger variant="ghost" size="xs" className="font-medium" aria-label="Workspace">
@@ -92,6 +108,14 @@ export const BranchToolbarEnvModeSelector = memo(function BranchToolbarEnvModeSe
92108
{resolveEnvModeLabel("worktree")}
93109
</span>
94110
</SelectItem>
111+
{showPreviousWorktree && previousWorktreeLabel ? (
112+
<SelectItem value={PREVIOUS_WORKTREE_SELECT_VALUE}>
113+
<span className="inline-flex items-center gap-1.5">
114+
<HistoryIcon className="size-3" />
115+
{previousWorktreeLabel}
116+
</span>
117+
</SelectItem>
118+
) : null}
95119
</SelectGroup>
96120
</SelectPopup>
97121
</Select>

0 commit comments

Comments
 (0)