The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 23:30 UTC
fix(web): a thread started from a pull request links it (#15848)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Lars Nieuwenhuis committed
Nov 11, 2026 at 09:03 UTC
479ee74135a9dce9e59ac0c01a9306604450fe28
9 files changed
+273
-10
apps/web/src/components/ChatView.tsx
+9
@@ -13,6 +13,7 @@ import {
13
presentThreadShellFromProjection,
14
} from "./ChatView.logic";
15
import { usePullRequestDraftThread } from "./chat/usePullRequestDraftThread";
16
+import { usePullRequestLinking } from "../hooks/usePullRequestLinking";
17
import { useComposerSelection } from "./chat/useComposerSelection";
18
import { useProjectCloneBanner } from "./chat/useProjectCloneBanner";
19
import { useAttachmentPreviewHandoff } from "./chat/useAttachmentPreviewHandoff";
@@ -1193,6 +1194,13 @@ export default function ChatView(props: ChatViewProps) {
1194
canPickEnvironment: hasMultipleEnvironments,
1195
});
1196
1197
+ const pullRequestLinking = usePullRequestLinking(environmentId);
1198
+ // Best effort: the thread already exists, so a failed link is only logged.
1199
+ const linkCheckoutPullRequest = useStableCallback((threadRef: ScopedThreadRef, url: string) => {
1200
+ pullRequestLinking.changeLink(threadRef, url, true).catch((error: unknown) => {
1201
+ console.warn("Failed to link the checked-out pull request to its thread.", error);
1202
+ });
1203
+ });
1204
const { handlePreparedPullRequestThreadStable, openPullRequestDialogStable } =
1205
usePullRequestDraftThread({
1206
routeKind,
@@ -2939,6 +2947,7 @@ export default function ChatView(props: ChatViewProps) {
2947
sideChatDisabledReason,
2948
handleNewThreadInActiveProject,
2949
persistThreadSettingsForNextTurn,
2950
+ linkCheckoutPullRequest,
2951
});
2952
2953
const onWorktreeSetupWorkLocally = () => {
apps/web/src/components/PullRequestThreadDialog.tsx
+6
-1
@@ -43,7 +43,11 @@ interface PullRequestThreadDialogProps {
43
cwd: string | null;
44
initialReference: string | null;
45
onOpenChange: (open: boolean) => void;
46
- onPrepared: (input: { branch: string; worktreePath: string | null }) => Promise<void> | void;
46
+ onPrepared: (input: {
47
+ branch: string;
48
+ worktreePath: string | null;
49
+ pullRequestUrl: string;
50
+ }) => Promise<void> | void;
51
}
52
53
function pullRequestStatusTone(state: string | undefined): string {
@@ -160,6 +164,7 @@ export function PullRequestThreadDialog({
164
await onPrepared({
165
branch: result.value.branch,
166
worktreePath: result.value.worktreePath,
167
+ pullRequestUrl: result.value.pullRequest.url,
168
});
169
if (!result.value.isOnPullRequestHead) {
170
toastManager.add(offHeadPullRequestCheckoutToast(terminology));
apps/web/src/components/chat/ChatOverlayHosts.tsx
+1
@@ -75,6 +75,7 @@ export function PullRequestThreadDialogHost(props: {
75
readonly onPrepared: (input: {
76
branch: string;
77
worktreePath: string | null;
78
+ pullRequestUrl: string;
79
}) => Promise<void> | void;
80
}) {
81
const state = useThreadOverlay(pullRequestThreadDialogAtom, props.threadKey);
apps/web/src/components/chat/useComposerSend.ts
+18
@@ -22,6 +22,7 @@ import {
22
type EnvironmentId,
23
type ModelSelection,
24
type PreviewAnnotationPayload,
25
+ type ScopedThreadRef,
26
type ThreadId,
27
ProviderInteractionMode,
28
RuntimeMode,
@@ -68,6 +69,7 @@ import {
69
finalizePromotedDraftThreadByRef,
70
markPromotedDraftThreadByRef,
71
restoreFailedDraftThread,
72
+ checkoutPullRequestUrlToLink,
73
composerDraftHasUserContent,
74
type ComposerFileAttachment,
75
type ComposerImageAttachment,
@@ -245,6 +247,8 @@ interface ComposerSendInput {
247
runtimeMode: RuntimeMode;
248
interactionMode: ProviderInteractionMode;
249
}) => Promise<AtomCommandResult<void, unknown>>;
250
+ /** Links a checked-out pull request to its thread; best effort, failures only logged. */
251
+ readonly linkCheckoutPullRequest: (threadRef: ScopedThreadRef, url: string) => void;
252
}
253
254
/**
@@ -324,6 +328,7 @@ export function useComposerSend(input: ComposerSendInput) {
328
sideChatDisabledReason,
329
handleNewThreadInActiveProject,
330
persistThreadSettingsForNextTurn,
331
+ linkCheckoutPullRequest,
332
} = input;
333
const linkedEnvironments = useLinkedEnvironments(environmentId);
334
const handleNewThread = useNewThreadHandler();
@@ -1239,6 +1244,13 @@ export function useComposerSend(input: ComposerSendInput) {
1244
submissionIntent === "background" && isLocalDraftThread
1245
? scopeThreadRef(environmentId, threadIdForSend)
1246
: null;
1247
+ // Read the live session, not the render snapshot: the draft is finalized away once the
1248
+ // thread exists.
1249
+ const checkoutPullRequestUrl = isLocalDraftThread
1250
+ ? checkoutPullRequestUrlToLink(
1251
+ draftId === null ? null : useComposerDraftStore.getState().getDraftSession(draftId),
1252
+ )
1253
+ : null;
1254
if (backgroundThreadRef) beginBackgroundDraftSubmissionByRef(backgroundThreadRef);
1255
const startPromise = startThreadTurn({
1256
environmentId,
@@ -1293,6 +1305,12 @@ export function useComposerSend(input: ComposerSendInput) {
1305
failure = startResult;
1306
} else {
1307
turnStartSucceeded = true;
1308
+ if (checkoutPullRequestUrl) {
1309
+ linkCheckoutPullRequest(
1310
+ scopeThreadRef(environmentId, threadIdForSend),
1311
+ checkoutPullRequestUrl,
1312
+ );
1313
+ }
1314
setKeepFullHistory(routeThreadKey, false);
1315
if (freshArm !== undefined) disarmFreshSession(routeThreadKey, freshArm);
1316
clearUsageLimitsFor(routeThreadKey);
apps/web/src/components/chat/usePullRequestDraftThread.ts
+10
-1
@@ -11,7 +11,12 @@ import {
11
selectProjectGroupingSettings,
12
} from "@t3tools/client-runtime/state/project-grouping";
13
import { buildDraftThreadRouteParams } from "../../threadRoutes";
14
-import { type DraftThreadEnvMode, useComposerDraftStore, DraftId } from "../../composerDraftStore";
14
+import {
15
+ type DraftCheckoutPullRequest,
16
+ type DraftThreadEnvMode,
17
+ useComposerDraftStore,
18
+ DraftId,
19
+} from "../../composerDraftStore";
20
import { openPullRequestThreadDialog } from "./ChatOverlayHosts";
21
interface PullRequestDraftThreadInput {
22
readonly routeKind: "server" | "draft";
@@ -62,6 +67,7 @@ export function usePullRequestDraftThread(input: PullRequestDraftThreadInput) {
67
branch: string;
68
worktreePath: string | null;
69
envMode: DraftThreadEnvMode;
70
+ checkoutPullRequest: DraftCheckoutPullRequest;
71
}) => {
72
if (!activeProject) {
73
throw new Error("No active project is available for this pull request.");
@@ -125,11 +131,14 @@ export function usePullRequestDraftThread(input: PullRequestDraftThreadInput) {
131
const handlePreparedPullRequestThread = async (input: {
132
branch: string;
133
worktreePath: string | null;
134
+ pullRequestUrl: string;
135
}) => {
136
+ // The draft carries the pull request until its first send makes it a thread, which links it.
137
await openOrReuseProjectDraftThread({
138
branch: input.branch,
139
worktreePath: input.worktreePath,
140
envMode: input.worktreePath ? "worktree" : "local",
141
+ checkoutPullRequest: { url: input.pullRequestUrl, branch: input.branch },
142
});
143
};
144
const handlePreparedPullRequestThreadStable = useStableCallback(handlePreparedPullRequestThread);
apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+13
-4
@@ -1014,10 +1014,10 @@ export function PullRequestDetailPanel({
1014
worktreePath: prepared.value.worktreePath,
1015
envMode: prepared.value.worktreePath === null ? "local" : "worktree",
1016
}).then(
1017
- (session) => session !== null,
1018
- () => false,
1017
+ (session) => session,
1018
+ () => null,
1019
);
1020
- if (!pointed) {
1020
+ if (pointed === null) {
1021
setHandoff(null);
1022
// Sending after failed rebinding would use the previous checkout.
1023
toastManager.update(toastId, {
@@ -1027,6 +1027,13 @@ export function PullRequestDetailPanel({
1027
});
1028
return;
1029
}
1030
+ // The draft carries the pull request until its first send makes it a thread, which links it.
1031
+ useComposerDraftStore.getState().setDraftThreadContext(pointed.draftId, {
1032
+ checkoutPullRequest: {
1033
+ url: prepared.value.pullRequest.url,
1034
+ branch: prepared.value.branch,
1035
+ },
1036
+ });
1037
setHandoff(null);
1038
if (task === null) {
1039
toastManager.update(
@@ -1044,7 +1051,9 @@ export function PullRequestDetailPanel({
1051
);
1052
return;
1053
}
1047
- await openThreadWithTask(projectRef, task, opened);
1054
+ // The task goes on the checkout draft: if the user typed into `opened` while the
1055
+ // checkout ran, that is no longer the draft the user is looking at.
1056
+ await openThreadWithTask(projectRef, task, pointed);
1057
toastManager.update(
1058
toastId,
1059
prepared.value.isOnPullRequestHead
apps/web/src/components/pullRequest/usePullRequestActions.ts
+13
-4
@@ -373,10 +373,10 @@ export function usePullRequestHandoffs({
373
worktreePath: prepared.value.worktreePath,
374
envMode: prepared.value.worktreePath === null ? "local" : "worktree",
375
}).then(
376
- (session) => session !== null,
377
- () => false,
376
+ (session) => session,
377
+ () => null,
378
);
379
- if (!pointed) {
379
+ if (pointed === null) {
380
setHandoff(null);
381
toastManager.update(toastId, {
382
type: "error",
@@ -385,6 +385,13 @@ export function usePullRequestHandoffs({
385
});
386
return;
387
}
388
+ // The draft carries the pull request until its first send makes it a thread, which links it.
389
+ useComposerDraftStore.getState().setDraftThreadContext(pointed.draftId, {
390
+ checkoutPullRequest: {
391
+ url: prepared.value.pullRequest.url,
392
+ branch: prepared.value.branch,
393
+ },
394
+ });
395
setHandoff(null);
396
if (task === null) {
397
toastManager.update(
@@ -402,7 +409,9 @@ export function usePullRequestHandoffs({
409
);
410
return;
411
}
405
- await openThreadWithTask(projectRef, task, opened);
412
+ // The task goes on the checkout draft: if the user typed into `opened` while the
413
+ // checkout ran, that is no longer the draft the user is looking at.
414
+ await openThreadWithTask(projectRef, task, pointed);
415
toastManager.update(
416
toastId,
417
prepared.value.isOnPullRequestHead
apps/web/src/composerDraftStore.test.ts
+124
@@ -78,6 +78,7 @@ import {
78
COMPOSER_DRAFT_STORAGE_KEY,
79
subscribeComposerDraftModelReconciliation,
80
deriveEffectiveComposerModelState,
81
+ checkoutPullRequestUrlToLink,
82
clearComposerDraftsEnvironment,
83
beginBackgroundDraftSubmissionByRef,
84
clearBackgroundDraftSubmissionByRef,
@@ -2655,3 +2656,126 @@ describe("composerDraftStore attachment references", () => {
2656
);
2657
});
2658
});
2659
+
2660
+describe("composerDraftStore checkout pull request", () => {
2661
+ const projectId = ProjectId.make("project-checkout");
2662
+ const projectRef = scopeProjectRef(TEST_ENVIRONMENT_ID, projectId);
2663
+ const otherProjectRef = scopeProjectRef(TEST_ENVIRONMENT_ID, ProjectId.make("project-other"));
2664
+ const threadId = ThreadId.make("thread-checkout");
2665
+ const draftId = DraftId.make("draft-checkout");
2666
+ const url = "https://github.com/acme/widgets/pull/42";
2667
+ const checkout = { url, branch: "feature/pr-42" };
2668
+
2669
+ beforeEach(() => {
2670
+ resetComposerDraftStore();
2671
+ });
2672
+
2673
+ const store = () => useComposerDraftStore.getState();
2674
+ const toLink = () => checkoutPullRequestUrlToLink(store().getDraftSession(draftId));
2675
+
2676
+ function draftOnMain() {
2677
+ store().setProjectDraftThreadId(projectRef, draftId, { threadId, branch: "main" });
2678
+ }
2679
+
2680
+ // GitActionsControl's live branch sync writes the git status branch onto the draft. The status
2681
+ // refresh after a checkout is forked server-side, so the prepare response lands first and the
2682
+ // sync replays the stale pre-checkout branch before the fresh status corrects it.
2683
+ function liveBranchSync(branch: string) {
2684
+ store().setDraftThreadContext(draftId, {
2685
+ branch,
2686
+ worktreePath: null,
2687
+ environmentSelection: "manual",
2688
+ });
2689
+ }
2690
+
2691
+ it("still links after a local checkout the live branch sync briefly reverts", () => {
2692
+ draftOnMain();
2693
+ store().setDraftThreadContext(draftId, {
2694
+ branch: checkout.branch,
2695
+ worktreePath: null,
2696
+ envMode: "local",
2697
+ checkoutPullRequest: checkout,
2698
+ });
2699
+ liveBranchSync("main");
2700
+ liveBranchSync(checkout.branch);
2701
+
2702
+ expect(toLink()).toBe(url);
2703
+ });
2704
+
2705
+ it("does not link once the draft has moved to another branch", () => {
2706
+ draftOnMain();
2707
+ store().setDraftThreadContext(draftId, {
2708
+ branch: checkout.branch,
2709
+ checkoutPullRequest: checkout,
2710
+ });
2711
+ liveBranchSync("main");
2712
+
2713
+ expect(toLink()).toBeNull();
2714
+ });
2715
+
2716
+ it("is dropped when cleared or when the draft moves to another project", () => {
2717
+ store().setProjectDraftThreadId(projectRef, draftId, {
2718
+ threadId,
2719
+ branch: checkout.branch,
2720
+ checkoutPullRequest: checkout,
2721
+ });
2722
+ store().setDraftThreadContext(draftId, { checkoutPullRequest: null });
2723
+ expect(store().getDraftSession(draftId)?.checkoutPullRequest).toBeUndefined();
2724
+
2725
+ store().setDraftThreadContext(draftId, { checkoutPullRequest: checkout });
2726
+ store().setDraftThreadContext(draftId, { projectRef: otherProjectRef });
2727
+ expect(store().getDraftSession(draftId)?.checkoutPullRequest).toBeUndefined();
2728
+ });
2729
+
2730
+ it("persists the pull request and still restores drafts saved without one", async () => {
2731
+ vi.useFakeTimers();
2732
+ try {
2733
+ await useComposerDraftStore.persist.clearStorage();
2734
+ const legacyDraftId = DraftId.make("draft-before-checkout-links");
2735
+ const storage = useComposerDraftStore.persist.getOptions().storage;
2736
+ expect(storage).toBeDefined();
2737
+ const persistedDraftThread = (id: ThreadId) => ({
2738
+ threadId: id,
2739
+ environmentId: TEST_ENVIRONMENT_ID,
2740
+ projectId,
2741
+ logicalProjectKey: scopedProjectKey(projectRef),
2742
+ createdAt: "2026-08-01T00:00:00.000Z",
2743
+ runtimeMode: "full-access",
2744
+ interactionMode: "default",
2745
+ branch: checkout.branch,
2746
+ worktreePath: null,
2747
+ envMode: "local",
2748
+ startFromOrigin: false,
2749
+ promotedTo: null,
2750
+ });
2751
+ storage?.setItem(COMPOSER_DRAFT_STORAGE_KEY, {
2752
+ version: 9,
2753
+ state: {
2754
+ draftsByThreadKey: {},
2755
+ draftThreadsByThreadKey: {
2756
+ [draftId]: { ...persistedDraftThread(threadId), checkoutPullRequest: checkout },
2757
+ [legacyDraftId]: persistedDraftThread(ThreadId.make("thread-legacy")),
2758
+ },
2759
+ logicalProjectDraftThreadKeyByLogicalProjectKey: {
2760
+ [scopedProjectKey(projectRef)]: draftId,
2761
+ },
2762
+ },
2763
+ } as never);
2764
+ await vi.advanceTimersByTimeAsync(300);
2765
+
2766
+ await useComposerDraftStore.persist.rehydrate();
2767
+
2768
+ expect(toLink()).toBe(url);
2769
+ const legacy = store().getDraftSession(legacyDraftId);
2770
+ expect(legacy).toMatchObject({ branch: checkout.branch });
2771
+ expect(checkoutPullRequestUrlToLink(legacy)).toBeNull();
2772
+ expect(
2773
+ partializeComposerDraftStoreState(store()).draftThreadsByThreadKey[draftId],
2774
+ ).toMatchObject({ checkoutPullRequest: checkout });
2775
+ } finally {
2776
+ await useComposerDraftStore.persist.clearStorage();
2777
+ vi.useRealTimers();
2778
+ resetComposerDraftStore();
2779
+ }
2780
+ });
2781
+});
apps/web/src/composerDraftStore.ts
+79
@@ -319,6 +319,9 @@ const PersistedDraftThreadState = Schema.Struct({
319
worktreePath: Schema.NullOr(Schema.String),
320
envMode: DraftThreadEnvModeSchema,
321
startFromOrigin: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
322
+ checkoutPullRequest: Schema.optionalKey(
323
+ Schema.Struct({ url: Schema.String, branch: Schema.String }),
324
+ ),
325
promotedTo: Schema.optionalKey(
326
Schema.NullOr(
327
Schema.Struct({
@@ -412,11 +415,31 @@ export interface DraftSessionState {
415
worktreePath: string | null;
416
envMode: DraftThreadEnvMode;
417
startFromOrigin: boolean;
418
+ /** Linked to the thread when the draft is sent on this branch. */
419
+ checkoutPullRequest?: DraftCheckoutPullRequest;
420
promotedTo?: ScopedThreadRef | null;
421
}
422
423
export type DraftThreadState = DraftSessionState;
424
425
+export interface DraftCheckoutPullRequest {
426
+ readonly url: string;
427
+ readonly branch: string;
428
+}
429
+
430
+/**
431
+ * The pull request to link when this draft becomes a thread. Matching the branch at send time,
432
+ * rather than clearing on every branch write, matters: right after a local checkout the live
433
+ * branch sync briefly writes the stale pre-checkout branch back onto the draft before the fresh
434
+ * git status lands.
435
+ */
436
+export function checkoutPullRequestUrlToLink(
437
+ draft: Pick<DraftSessionState, "branch" | "checkoutPullRequest"> | null | undefined,
438
+): string | null {
439
+ const checkout = draft?.checkoutPullRequest;
440
+ return checkout && draft.branch === checkout.branch ? checkout.url : null;
441
+}
442
+
443
interface ProjectDraftSession extends DraftSessionState {
444
draftId: DraftId;
445
}
@@ -466,6 +489,7 @@ interface ComposerDraftStoreState {
489
interactionMode?: ProviderInteractionMode;
490
environmentSelection?: "auto" | "manual";
491
loadBalancedEnvironmentId?: EnvironmentId | null;
492
+ checkoutPullRequest?: DraftCheckoutPullRequest | null;
493
},
494
) => void;
495
setProjectDraftThreadId: (
@@ -482,6 +506,7 @@ interface ComposerDraftStoreState {
506
interactionMode?: ProviderInteractionMode;
507
environmentSelection?: "auto" | "manual";
508
loadBalancedEnvironmentId?: EnvironmentId | null;
509
+ checkoutPullRequest?: DraftCheckoutPullRequest | null;
510
},
511
) => void;
512
setDraftThreadContext: (
@@ -497,6 +522,7 @@ interface ComposerDraftStoreState {
522
interactionMode?: ProviderInteractionMode;
523
environmentSelection?: "auto" | "manual";
524
loadBalancedEnvironmentId?: EnvironmentId | null;
525
+ checkoutPullRequest?: DraftCheckoutPullRequest | null;
526
},
527
) => void;
528
clearProjectDraftThreadId: (projectRef: ScopedProjectRef) => void;
@@ -1482,6 +1508,7 @@ function createDraftThreadState(
1508
interactionMode?: ProviderInteractionMode;
1509
environmentSelection?: "auto" | "manual";
1510
loadBalancedEnvironmentId?: EnvironmentId | null;
1511
+ checkoutPullRequest?: DraftCheckoutPullRequest | null;
1512
},
1513
): DraftThreadState {
1514
const projectChanged =
@@ -1506,6 +1533,11 @@ function createDraftThreadState(
1533
: options.startFromOrigin;
1534
const environmentSelection =
1535
options?.environmentSelection ?? existingThread?.environmentSelection;
1536
+ const checkoutPullRequest = resolveCheckoutPullRequest(
1537
+ existingThread,
1538
+ options?.checkoutPullRequest,
1539
+ projectChanged,
1540
+ );
1541
return {
1542
threadId,
1543
environmentId: projectRef.environmentId,
@@ -1530,10 +1562,28 @@ function createDraftThreadState(
1562
envMode:
1563
options?.envMode ?? (nextWorktreePath ? "worktree" : (existingThread?.envMode ?? "local")),
1564
startFromOrigin: nextStartFromOrigin,
1565
+ ...(checkoutPullRequest ? { checkoutPullRequest } : {}),
1566
promotedTo: null,
1567
};
1568
}
1569
1570
+/** An explicit value wins; otherwise a checkout's pull request stays with its project. */
1571
+function resolveCheckoutPullRequest(
1572
+ existing: DraftThreadState | undefined,
1573
+ explicit: DraftCheckoutPullRequest | null | undefined,
1574
+ projectChanged: boolean,
1575
+): DraftCheckoutPullRequest | undefined {
1576
+ if (explicit !== undefined) return explicit ?? undefined;
1577
+ return projectChanged ? undefined : existing?.checkoutPullRequest;
1578
+}
1579
+
1580
+function checkoutPullRequestsEqual(
1581
+ left: DraftCheckoutPullRequest | undefined,
1582
+ right: DraftCheckoutPullRequest | undefined,
1583
+): boolean {
1584
+ return left === right || (left?.url === right?.url && left?.branch === right?.branch);
1585
+}
1586
+
1587
function scopedThreadRefsEqual(
1588
left: ScopedThreadRef | null | undefined,
1589
right: ScopedThreadRef | null | undefined,
@@ -1564,6 +1614,7 @@ function draftThreadsEqual(left: DraftThreadState | undefined, right: DraftThrea
1614
left.worktreePath === right.worktreePath &&
1615
left.envMode === right.envMode &&
1616
left.startFromOrigin === right.startFromOrigin &&
1617
+ checkoutPullRequestsEqual(left.checkoutPullRequest, right.checkoutPullRequest) &&
1618
scopedThreadRefsEqual(left.promotedTo, right.promotedTo)
1619
);
1620
}
@@ -1603,6 +1654,17 @@ function removeDraftThreadReferences(
1654
};
1655
}
1656
1657
+function normalizePersistedCheckoutPullRequest(value: unknown): DraftCheckoutPullRequest | null {
1658
+ if (!value || typeof value !== "object") return null;
1659
+ const { url, branch } = value as Record<string, unknown>;
1660
+ return typeof url === "string" &&
1661
+ url.length > 0 &&
1662
+ typeof branch === "string" &&
1663
+ branch.length > 0
1664
+ ? { url, branch }
1665
+ : null;
1666
+}
1667
+
1668
function normalizePersistedDraftThreads(
1669
rawDraftThreadsByThreadId: unknown,
1670
rawProjectDraftThreadIdByProjectKey: unknown,
@@ -1665,6 +1727,9 @@ function normalizePersistedDraftThreads(
1727
const worktreePath = candidateDraftThread.worktreePath;
1728
const startFromOrigin = candidateDraftThread.startFromOrigin === true;
1729
const normalizedWorktreePath = typeof worktreePath === "string" ? worktreePath : null;
1730
+ const checkoutPullRequest = normalizePersistedCheckoutPullRequest(
1731
+ candidateDraftThread.checkoutPullRequest,
1732
+ );
1733
const promotedToCandidate = candidateDraftThread.promotedTo;
1734
const promotedToRecord =
1735
promotedToCandidate && typeof promotedToCandidate === "object"
@@ -1722,6 +1787,7 @@ function normalizePersistedDraftThreads(
1787
: candidateDraftThread.loadBalancedEnvironmentId === null
1788
? { loadBalancedEnvironmentId: null }
1789
: {}),
1790
+ ...(checkoutPullRequest ? { checkoutPullRequest } : {}),
1791
promotedTo,
1792
};
1793
}
@@ -2485,6 +2551,9 @@ function toHydratedDraftThreadState(
2551
persistedDraftThread.loadBalancedEnvironmentId as EnvironmentId | null,
2552
}
2553
: {}),
2554
+ ...(persistedDraftThread.checkoutPullRequest
2555
+ ? { checkoutPullRequest: persistedDraftThread.checkoutPullRequest }
2556
+ : {}),
2557
promotedTo: persistedDraftThread.promotedTo
2558
? scopeThreadRef(
2559
persistedDraftThread.promotedTo.environmentId as EnvironmentId,
@@ -2744,6 +2813,11 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
2813
(options.branch != null || options.worktreePath != null
2814
? "manual"
2815
: existing.environmentSelection);
2816
+ const checkoutPullRequest = resolveCheckoutPullRequest(
2817
+ existing,
2818
+ options.checkoutPullRequest,
2819
+ projectChanged,
2820
+ );
2821
const nextDraftThread: DraftThreadState = {
2822
threadId: existing.threadId,
2823
environmentId: nextProjectRef.environmentId,
@@ -2767,6 +2841,7 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
2841
envMode:
2842
options.envMode ?? (nextWorktreePath ? "worktree" : (existing.envMode ?? "local")),
2843
startFromOrigin: nextStartFromOrigin,
2844
+ ...(checkoutPullRequest ? { checkoutPullRequest } : {}),
2845
promotedTo: existing.promotedTo ?? null,
2846
};
2847
const isUnchanged =
@@ -2782,6 +2857,10 @@ const composerDraftStore = create<ComposerDraftStoreState>()(
2857
nextDraftThread.worktreePath === existing.worktreePath &&
2858
nextDraftThread.envMode === existing.envMode &&
2859
nextDraftThread.startFromOrigin === existing.startFromOrigin &&
2860
+ checkoutPullRequestsEqual(
2861
+ nextDraftThread.checkoutPullRequest,
2862
+ existing.checkoutPullRequest,
2863
+ ) &&
2864
scopedThreadRefsEqual(nextDraftThread.promotedTo, existing.promotedTo);
2865
if (isUnchanged) {
2866
return state;