Skip to content

Commit 9efc9f9

Browse files
Allow empty server threads to bootstrap new worktrees
- Let the first send on an empty server thread override env mode and base branch - Use shorter locked workspace labels in the branch toolbar - Add coverage for worktree bootstrap and env-mode resolution
1 parent 934037c commit 9efc9f9

9 files changed

Lines changed: 305 additions & 20 deletions

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
resolveEffectiveEnvMode,
1111
resolveEnvModeLabel,
1212
resolveBranchToolbarValue,
13+
resolveLockedWorkspaceLabel,
1314
shouldIncludeBranchPickerItem,
1415
} from "./BranchToolbar.logic";
1516

@@ -157,6 +158,16 @@ describe("resolveCurrentWorkspaceLabel", () => {
157158
});
158159
});
159160

161+
describe("resolveLockedWorkspaceLabel", () => {
162+
it("uses a shorter label for the main repo checkout", () => {
163+
expect(resolveLockedWorkspaceLabel(null)).toBe("Local checkout");
164+
});
165+
166+
it("uses a shorter label for an attached worktree", () => {
167+
expect(resolveLockedWorkspaceLabel("/repo/.t3/worktrees/feature-a")).toBe("Worktree");
168+
});
169+
});
170+
160171
describe("deriveLocalBranchNameFromRemoteRef", () => {
161172
it("strips the remote prefix from a remote ref", () => {
162173
expect(deriveLocalBranchNameFromRemoteRef("origin/feature/demo")).toBe("feature/demo");

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export function resolveCurrentWorkspaceLabel(activeWorktreePath: string | null):
5050
return activeWorktreePath ? "Current worktree" : resolveEnvModeLabel("local");
5151
}
5252

53+
export function resolveLockedWorkspaceLabel(activeWorktreePath: string | null): string {
54+
return activeWorktreePath ? "Worktree" : "Local checkout";
55+
}
56+
5357
export function resolveEffectiveEnvMode(input: {
5458
activeWorktreePath: string | null;
5559
hasServerThread: boolean;

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ interface BranchToolbarProps {
2020
threadId: ThreadId;
2121
draftId?: DraftId;
2222
onEnvModeChange: (mode: EnvMode) => void;
23+
effectiveEnvModeOverride?: EnvMode;
24+
activeThreadBranchOverride?: string | null;
25+
onActiveThreadBranchOverrideChange?: (branch: string | null) => void;
2326
envLocked: boolean;
2427
onCheckoutPullRequestRequest?: (reference: string) => void;
2528
onComposerFocusRequest?: () => void;
@@ -32,6 +35,9 @@ export const BranchToolbar = memo(function BranchToolbar({
3235
threadId,
3336
draftId,
3437
onEnvModeChange,
38+
effectiveEnvModeOverride,
39+
activeThreadBranchOverride,
40+
onActiveThreadBranchOverrideChange,
3541
envLocked,
3642
onCheckoutPullRequestRequest,
3743
onComposerFocusRequest,
@@ -59,11 +65,13 @@ export const BranchToolbar = memo(function BranchToolbar({
5965
const activeProject = useStore(activeProjectSelector);
6066
const hasActiveThread = serverThread !== undefined || draftThread !== null;
6167
const activeWorktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
62-
const effectiveEnvMode = resolveEffectiveEnvMode({
63-
activeWorktreePath,
64-
hasServerThread: serverThread !== undefined,
65-
draftThreadEnvMode: draftThread?.envMode,
66-
});
68+
const effectiveEnvMode =
69+
effectiveEnvModeOverride ??
70+
resolveEffectiveEnvMode({
71+
activeWorktreePath,
72+
hasServerThread: serverThread !== undefined,
73+
draftThreadEnvMode: draftThread?.envMode,
74+
});
6775
const envModeLocked = envLocked || (serverThread !== undefined && activeWorktreePath !== null);
6876

6977
const showEnvironmentPicker =
@@ -98,6 +106,9 @@ export const BranchToolbar = memo(function BranchToolbar({
98106
threadId={threadId}
99107
{...(draftId ? { draftId } : {})}
100108
envLocked={envLocked}
109+
{...(effectiveEnvModeOverride ? { effectiveEnvModeOverride } : {})}
110+
{...(activeThreadBranchOverride !== undefined ? { activeThreadBranchOverride } : {})}
111+
{...(onActiveThreadBranchOverrideChange ? { onActiveThreadBranchOverrideChange } : {})}
101112
{...(onCheckoutPullRequestRequest ? { onCheckoutPullRequestRequest } : {})}
102113
{...(onComposerFocusRequest ? { onComposerFocusRequest } : {})}
103114
/>

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ interface BranchToolbarBranchSelectorProps {
4949
threadId: ThreadId;
5050
draftId?: DraftId;
5151
envLocked: boolean;
52+
effectiveEnvModeOverride?: "local" | "worktree";
53+
activeThreadBranchOverride?: string | null;
54+
onActiveThreadBranchOverrideChange?: (branch: string | null) => void;
5255
onCheckoutPullRequestRequest?: (reference: string) => void;
5356
onComposerFocusRequest?: () => void;
5457
}
@@ -77,6 +80,9 @@ export function BranchToolbarBranchSelector({
7780
threadId,
7881
draftId,
7982
envLocked,
83+
effectiveEnvModeOverride,
84+
activeThreadBranchOverride,
85+
onActiveThreadBranchOverrideChange,
8086
onCheckoutPullRequestRequest,
8187
onComposerFocusRequest,
8288
}: BranchToolbarBranchSelectorProps) {
@@ -108,16 +114,21 @@ export function BranchToolbarBranchSelector({
108114
const activeProject = useStore(activeProjectSelector);
109115

110116
const activeThreadId = serverThread?.id ?? (draftThread ? threadId : undefined);
111-
const activeThreadBranch = serverThread?.branch ?? draftThread?.branch ?? null;
117+
const activeThreadBranch =
118+
activeThreadBranchOverride !== undefined
119+
? activeThreadBranchOverride
120+
: (serverThread?.branch ?? draftThread?.branch ?? null);
112121
const activeWorktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
113122
const activeProjectCwd = activeProject?.cwd ?? null;
114123
const branchCwd = activeWorktreePath ?? activeProjectCwd;
115124
const hasServerThread = serverThread !== undefined;
116-
const effectiveEnvMode = resolveEffectiveEnvMode({
117-
activeWorktreePath,
118-
hasServerThread,
119-
draftThreadEnvMode: draftThread?.envMode,
120-
});
125+
const effectiveEnvMode =
126+
effectiveEnvModeOverride ??
127+
resolveEffectiveEnvMode({
128+
activeWorktreePath,
129+
hasServerThread,
130+
draftThreadEnvMode: draftThread?.envMode,
131+
});
121132

122133
// ---------------------------------------------------------------------------
123134
// Thread branch mutation (colocated — only this component calls it)
@@ -146,6 +157,7 @@ export function BranchToolbarBranchSelector({
146157
});
147158
}
148159
if (hasServerThread) {
160+
onActiveThreadBranchOverrideChange?.(branch);
149161
setThreadBranchAction(threadRef, branch, worktreePath);
150162
return;
151163
}
@@ -167,6 +179,7 @@ export function BranchToolbarBranchSelector({
167179
serverSession,
168180
activeWorktreePath,
169181
hasServerThread,
182+
onActiveThreadBranchOverrideChange,
170183
setThreadBranchAction,
171184
setDraftThreadContext,
172185
draftId,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { memo, useMemo } from "react";
44
import {
55
resolveCurrentWorkspaceLabel,
66
resolveEnvModeLabel,
7+
resolveLockedWorkspaceLabel,
78
type EnvMode,
89
} from "./BranchToolbar.logic";
910
import {
@@ -43,12 +44,12 @@ export const BranchToolbarEnvModeSelector = memo(function BranchToolbarEnvModeSe
4344
{activeWorktreePath ? (
4445
<>
4546
<FolderGitIcon className="size-3" />
46-
{resolveCurrentWorkspaceLabel(activeWorktreePath)}
47+
{resolveLockedWorkspaceLabel(activeWorktreePath)}
4748
</>
4849
) : (
4950
<>
5051
<FolderIcon className="size-3" />
51-
{resolveCurrentWorkspaceLabel(activeWorktreePath)}
52+
{resolveLockedWorkspaceLabel(activeWorktreePath)}
5253
</>
5354
)}
5455
</span>

‎apps/web/src/components/ChatView.browser.tsx‎

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,6 +2521,186 @@ describe("ChatView timeline estimator parity (full app)", () => {
25212521
}
25222522
});
25232523

2524+
it("keeps new-worktree mode on empty server threads and bootstraps the first send", async () => {
2525+
const snapshot = addThreadToSnapshot(createDraftOnlySnapshot(), THREAD_ID);
2526+
const mounted = await mountChatView({
2527+
viewport: DEFAULT_VIEWPORT,
2528+
snapshot: {
2529+
...snapshot,
2530+
threads: snapshot.threads.map((thread) =>
2531+
thread.id === THREAD_ID ? Object.assign({}, thread, { session: null }) : thread,
2532+
),
2533+
},
2534+
resolveRpc: (body) => {
2535+
if (body._tag === WS_METHODS.gitListBranches) {
2536+
return {
2537+
isRepo: true,
2538+
hasOriginRemote: true,
2539+
nextCursor: null,
2540+
totalCount: 1,
2541+
branches: [
2542+
{
2543+
name: "main",
2544+
current: true,
2545+
isDefault: true,
2546+
worktreePath: null,
2547+
},
2548+
],
2549+
};
2550+
}
2551+
if (body._tag === ORCHESTRATION_WS_METHODS.dispatchCommand) {
2552+
return {
2553+
sequence: fixture.snapshot.snapshotSequence + 1,
2554+
};
2555+
}
2556+
return undefined;
2557+
},
2558+
});
2559+
2560+
try {
2561+
await page.getByText("Current checkout", { exact: true }).click();
2562+
await page.getByText("New worktree", { exact: true }).click();
2563+
2564+
await vi.waitFor(
2565+
() => {
2566+
expect(findButtonByText("New worktree")).toBeTruthy();
2567+
},
2568+
{ timeout: 8_000, interval: 16 },
2569+
);
2570+
2571+
useComposerDraftStore.getState().setPrompt(THREAD_REF, "Ship it");
2572+
await waitForLayout();
2573+
2574+
const sendButton = await waitForSendButton();
2575+
expect(sendButton.disabled).toBe(false);
2576+
sendButton.click();
2577+
2578+
await vi.waitFor(
2579+
() => {
2580+
const turnStartRequest = wsRequests.find(
2581+
(request) =>
2582+
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
2583+
request.type === "thread.turn.start",
2584+
) as
2585+
| {
2586+
_tag: string;
2587+
type?: string;
2588+
bootstrap?: {
2589+
createThread?: { projectId?: string };
2590+
prepareWorktree?: { projectCwd?: string; baseBranch?: string; branch?: string };
2591+
runSetupScript?: boolean;
2592+
};
2593+
}
2594+
| undefined;
2595+
2596+
expect(turnStartRequest).toMatchObject({
2597+
_tag: ORCHESTRATION_WS_METHODS.dispatchCommand,
2598+
type: "thread.turn.start",
2599+
bootstrap: {
2600+
prepareWorktree: {
2601+
projectCwd: "/repo/project",
2602+
baseBranch: "main",
2603+
branch: expect.stringMatching(/^t3code\/[0-9a-f]{8}$/),
2604+
},
2605+
runSetupScript: true,
2606+
},
2607+
});
2608+
expect(turnStartRequest?.bootstrap?.createThread).toBeUndefined();
2609+
},
2610+
{ timeout: 8_000, interval: 16 },
2611+
);
2612+
} finally {
2613+
await mounted.cleanup();
2614+
}
2615+
});
2616+
2617+
it("updates the selected worktree base branch on empty server threads", async () => {
2618+
const snapshot = addThreadToSnapshot(createDraftOnlySnapshot(), THREAD_ID);
2619+
const mounted = await mountChatView({
2620+
viewport: DEFAULT_VIEWPORT,
2621+
snapshot: {
2622+
...snapshot,
2623+
threads: snapshot.threads.map((thread) =>
2624+
thread.id === THREAD_ID ? Object.assign({}, thread, { session: null }) : thread,
2625+
),
2626+
},
2627+
resolveRpc: (body) => {
2628+
if (body._tag === WS_METHODS.gitListBranches) {
2629+
return {
2630+
isRepo: true,
2631+
hasOriginRemote: true,
2632+
nextCursor: null,
2633+
totalCount: 2,
2634+
branches: [
2635+
{
2636+
name: "main",
2637+
current: true,
2638+
isDefault: true,
2639+
worktreePath: null,
2640+
},
2641+
{
2642+
name: "release/next",
2643+
current: false,
2644+
isDefault: false,
2645+
worktreePath: null,
2646+
},
2647+
],
2648+
};
2649+
}
2650+
if (body._tag === ORCHESTRATION_WS_METHODS.dispatchCommand) {
2651+
return {
2652+
sequence: fixture.snapshot.snapshotSequence + 1,
2653+
};
2654+
}
2655+
return undefined;
2656+
},
2657+
});
2658+
2659+
try {
2660+
await page.getByText("Current checkout", { exact: true }).click();
2661+
await page.getByText("New worktree", { exact: true }).click();
2662+
await page.getByText("From main", { exact: true }).click();
2663+
await page.getByText("release/next", { exact: true }).click();
2664+
2665+
await vi.waitFor(
2666+
() => {
2667+
expect(findButtonByText("From release/next")).toBeTruthy();
2668+
},
2669+
{ timeout: 8_000, interval: 16 },
2670+
);
2671+
2672+
useComposerDraftStore.getState().setPrompt(THREAD_REF, "Ship it");
2673+
await waitForLayout();
2674+
2675+
const sendButton = await waitForSendButton();
2676+
expect(sendButton.disabled).toBe(false);
2677+
sendButton.click();
2678+
2679+
await vi.waitFor(
2680+
() => {
2681+
const turnStartRequest = wsRequests.find(
2682+
(request) =>
2683+
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
2684+
request.type === "thread.turn.start",
2685+
) as
2686+
| {
2687+
_tag: string;
2688+
type?: string;
2689+
bootstrap?: {
2690+
prepareWorktree?: { baseBranch?: string };
2691+
};
2692+
}
2693+
| undefined;
2694+
2695+
expect(turnStartRequest?.bootstrap?.prepareWorktree?.baseBranch).toBe("release/next");
2696+
},
2697+
{ timeout: 8_000, interval: 16 },
2698+
);
2699+
} finally {
2700+
await mounted.cleanup();
2701+
}
2702+
});
2703+
25242704
it("shows the send state once bootstrap dispatch is in flight", async () => {
25252705
useTerminalStateStore.setState({
25262706
terminalStateByThreadKey: {},

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
deriveComposerSendState,
1212
hasServerAcknowledgedLocalDispatch,
1313
reconcileMountedTerminalThreadIds,
14+
resolveSendEnvMode,
1415
shouldWriteThreadErrorToCurrentServerThread,
1516
waitForStartedServerThread,
1617
} from "./ChatView.logic";
@@ -82,6 +83,17 @@ describe("buildExpiredTerminalContextToastCopy", () => {
8283
});
8384
});
8485

86+
describe("resolveSendEnvMode", () => {
87+
it("keeps worktree mode for git repositories", () => {
88+
expect(resolveSendEnvMode({ requestedEnvMode: "worktree", isGitRepo: true })).toBe("worktree");
89+
});
90+
91+
it("forces local mode for non-git repositories", () => {
92+
expect(resolveSendEnvMode({ requestedEnvMode: "worktree", isGitRepo: false })).toBe("local");
93+
expect(resolveSendEnvMode({ requestedEnvMode: "local", isGitRepo: false })).toBe("local");
94+
});
95+
});
96+
8597
describe("reconcileMountedTerminalThreadIds", () => {
8698
it("keeps previously mounted open threads and adds the active open thread", () => {
8799
expect(

0 commit comments

Comments
 (0)