+ ) : (
+
+
+ }
+ >
+ {rowContent}
+
+ {status.tooltip}
+
+ )}
+ {conflicting ? (
+ // The same words the detail panel says, one click away, so the two read as one thing.
+
+
+
+ Merge conflicts
+
+
+
+ }
+ >
+ {handoff === "conflicts" ? "Preparing..." : "Resolve"}
+
+
+
+ Check the branch out and resolve the conflicts in a new thread
+
+
+
+ ) : null}
+ {primaryAction === "merge" ? (
+ setConfirmingMerge(open)}>
+
+
+ Merge pull request?
+
+ This merges #{pr.number} using {selectedMergeMethod}.
+
+
+
+ }>
+ Cancel
+
+
+
+
+
+ ) : null}
+ >
+ );
+}
diff --git a/apps/web/src/components/chat/threadDetailsPanelStyles.ts b/apps/web/src/components/chat/threadDetailsPanelStyles.ts
index 57191feb3e..87290f9062 100644
--- a/apps/web/src/components/chat/threadDetailsPanelStyles.ts
+++ b/apps/web/src/components/chat/threadDetailsPanelStyles.ts
@@ -10,9 +10,6 @@ const THREAD_DETAILS_PANEL_RESTING_BUTTON_SURFACE_CLASS =
const THREAD_DETAILS_PANEL_HOVER_SURFACE_CLASS =
"hover:!bg-black/[0.055] data-pressed:!bg-black/[0.055] dark:hover:!bg-white/[0.075] dark:data-pressed:!bg-white/[0.075]";
-const THREAD_DETAILS_PANEL_SPLIT_TARGET_HOVER_SURFACE_CLASS =
- "hover:!bg-black/[0.07] data-pressed:!bg-black/[0.07] dark:hover:!bg-white/[0.11] dark:data-pressed:!bg-white/[0.11]";
-
const THREAD_DETAILS_PANEL_ROW_SURFACE_CLASS = `${THREAD_DETAILS_PANEL_RESTING_BUTTON_SURFACE_CLASS} ${THREAD_DETAILS_PANEL_HOVER_SURFACE_CLASS}`;
const THREAD_DETAILS_PANEL_SPLIT_BUTTON_SURFACE_CLASS = `${THREAD_DETAILS_PANEL_RESTING_BUTTON_SURFACE_CLASS} hover:bg-transparent data-pressed:bg-transparent`;
@@ -23,11 +20,17 @@ export const THREAD_DETAILS_PANEL_SELECT_ROW_CLASS = `${THREAD_DETAILS_PANEL_ROW
export const THREAD_DETAILS_PANEL_LINK_ROW_CLASS = `flex h-9 min-w-0 flex-1 cursor-pointer items-center justify-start gap-2.5 rounded-lg border border-transparent bg-transparent px-2.5 text-left text-[13px] font-medium text-foreground/80 disabled:cursor-not-allowed disabled:opacity-55 sm:h-9 sm:text-[13px] ${THREAD_DETAILS_PANEL_ROW_SURFACE_CLASS}`;
-export const THREAD_DETAILS_PANEL_LINK_SPLIT_GROUP_CLASS = `group/thread-details-link flex w-full items-center rounded-lg transition-colors ${THREAD_DETAILS_PANEL_HOVER_SURFACE_CLASS}`;
+// No transition on the group: the halves are Buttons whose background snaps (their base only
+// transitions shadow), so an eased group tint would land frames later and the hover would
+// visibly commit in two steps.
+export const THREAD_DETAILS_PANEL_LINK_SPLIT_GROUP_CLASS = `group/thread-details-link flex w-full items-center rounded-lg ${THREAD_DETAILS_PANEL_HOVER_SURFACE_CLASS}`;
-export const THREAD_DETAILS_PANEL_LINK_SPLIT_PRIMARY_CLASS = `flex h-9 min-w-0 flex-1 cursor-pointer items-center justify-start gap-2.5 rounded-e-none border border-transparent bg-transparent px-2.5 text-left text-[13px] font-medium text-foreground/80 shadow-none before:shadow-none disabled:cursor-not-allowed disabled:opacity-55 sm:h-9 sm:text-[13px] ${THREAD_DETAILS_PANEL_SPLIT_TARGET_HOVER_SURFACE_CLASS}`;
+export const THREAD_DETAILS_PANEL_LINK_SPLIT_PRIMARY_CLASS = `flex h-9 min-w-0 flex-1 cursor-pointer items-center justify-start gap-2.5 rounded-e-none border border-transparent px-2.5 text-left text-[13px] font-medium text-foreground/80 disabled:cursor-not-allowed disabled:opacity-55 sm:h-9 sm:text-[13px] ${THREAD_DETAILS_PANEL_SPLIT_BUTTON_SURFACE_CLASS}`;
-export const THREAD_DETAILS_PANEL_LINK_SPLIT_SECONDARY_CLASS = `h-9 w-8 shrink-0 rounded-s-none border-transparent bg-transparent px-0 text-[13px] font-medium text-foreground/80 shadow-none before:shadow-none sm:h-9 sm:w-8 sm:text-[13px] ${THREAD_DETAILS_PANEL_SPLIT_TARGET_HOVER_SURFACE_CLASS}`;
+export const THREAD_DETAILS_PANEL_LINK_SPLIT_SECONDARY_CLASS = `h-9 w-8 shrink-0 rounded-s-none border-transparent px-0 text-[13px] font-medium text-foreground/80 sm:h-9 sm:w-8 sm:text-[13px] ${THREAD_DETAILS_PANEL_SPLIT_BUTTON_SURFACE_CLASS}`;
+
+/** The trailing half of a link split row when it carries a word ("Merge") rather than an icon. */
+export const THREAD_DETAILS_PANEL_LINK_SPLIT_ACTION_CLASS = `h-9 shrink-0 rounded-s-none border-transparent px-2.5 text-[13px] font-medium text-primary sm:h-9 sm:text-[13px] ${THREAD_DETAILS_PANEL_SPLIT_BUTTON_SURFACE_CLASS}`;
export const THREAD_DETAILS_PANEL_LOCKED_ROW_CLASS =
"h-9 w-full justify-start gap-2.5 rounded-lg border border-transparent px-2.5 text-[13px] font-medium text-foreground/80 sm:h-9 sm:text-[13px]";
@@ -38,7 +41,7 @@ export const THREAD_DETAILS_PANEL_CHEVRON_CLASS = "size-4 shrink-0 text-muted-fo
export const THREAD_DETAILS_PANEL_ICON_ACTION_CLASS = `size-6 rounded-md border-transparent bg-transparent p-0 sm:size-6 ${THREAD_DETAILS_PANEL_ROW_SURFACE_CLASS}`;
-export const THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS = `group/thread-details-action flex w-full items-center rounded-lg transition-colors ${THREAD_DETAILS_PANEL_HOVER_SURFACE_CLASS}`;
+export const THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS = `group/thread-details-action flex w-full items-center rounded-lg ${THREAD_DETAILS_PANEL_HOVER_SURFACE_CLASS}`;
export const THREAD_DETAILS_PANEL_SPLIT_PRIMARY_CLASS = `h-9 min-w-0 flex-1 justify-start gap-2.5 rounded-e-none border-transparent bg-transparent px-2.5 pr-2 text-[13px] font-medium text-foreground/80 sm:h-9 sm:text-[13px] ${THREAD_DETAILS_PANEL_SPLIT_BUTTON_SURFACE_CLASS}`;
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
index 7237b43574..da78751d91 100644
--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
@@ -47,10 +47,7 @@ import {
useState,
} from "react";
-import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
-import { useNewThreadHandler } from "~/hooks/useHandleNewThread";
import { useCopyToClipboard, writeTextToClipboard } from "~/hooks/useCopyToClipboard";
-import { usePreparePullRequestThreadAction } from "~/lib/sourceControlActions";
import { cn } from "~/lib/utils";
import { readLocalApi } from "~/localApi";
import type { ReviewCommentContext } from "~/reviewCommentContext";
@@ -94,6 +91,7 @@ import { openOnHostLabel, showPullRequestLinkContextMenu } from "./pullRequestLi
import { PullRequestSummaryTab } from "./PullRequestSummaryTab";
import { PullRequestTimelineTab } from "./PullRequestTimelineTab";
import {
+ allowedPullRequestMergeMethods,
buildAskAboutLinesHandoff,
buildAskAboutPullRequestHandoff,
buildExplainPullRequestHandoff,
@@ -1013,26 +1011,8 @@ export function PullRequestDetailPanel({
useEffect(() => {
if (!visibleTabs.some((item) => item.value === tab)) setTab("summary");
}, [tab, visibleTabs]);
- // Two questions, both of which have to say yes: whether this host can do it at all, and
- // whether this account may. A reader with read access on someone else's project sees the pull
- // request and none of the buttons that would only ever be refused.
- const can = (action: PullRequestAction) =>
- detail?.capabilities.actions.includes(action) === true &&
- detail.viewerPermissions.actions.includes(action);
- // One live action holds the slot. A conflicting change cannot be merged now, so the slot goes
- // to the thing that would help instead of a Merge button that only ever says no.
- const primaryAction =
- detail === null || detail.state !== "open"
- ? null
- : detail.isDraft && can("ready")
- ? "ready"
- : !can("merge")
- ? null
- : conflicting
- ? "resolve"
- : allowedMergeMethods.length > 0
- ? "merge"
- : null;
+ const can = (action: PullRequestAction) => canPerformPullRequestAction(detail, action);
+ const primaryAction = resolvePullRequestPrimaryAction(detail);
// The pull request number carries this state in the overview and the right-panel tab mirrors
// it. Conflicts keep their own row below: an open pull request remains green there.
const statePresentation = detail
diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts
index ddb4e813bf..4c28f2764e 100644
--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts
+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts
@@ -4,9 +4,11 @@ import type {
PullRequestBaseComparison,
PullRequestCheck,
PullRequestComment,
+ PullRequestDetail,
PullRequestDetailView,
PullRequestMergeability,
PullRequestReaction,
+ PullRequestMergeMethod,
PullRequestReviewThread,
PullRequestState,
PullRequestUpdateMethod,
@@ -64,6 +66,68 @@ export function describePullRequestState(state: PullRequestState, isDraft: boole
return isDraft ? "Draft" : "Ready for review";
}
+/** The slice of a detail that decides which actions it offers. */
+export type PullRequestActionableDetail = Pick<
+ PullRequestDetail,
+ "state" | "isDraft" | "mergeability" | "capabilities" | "viewerPermissions" | "mergeCapabilities"
+>;
+
+/**
+ * The host says which strategies it offers at all; the repository narrows that to the ones it
+ * actually allows.
+ */
+export function allowedPullRequestMergeMethods(
+ detail: Pick | null,
+): ReadonlyArray {
+ return detail === null
+ ? []
+ : detail.capabilities.mergeMethods.filter((method) => detail.mergeCapabilities[method]);
+}
+
+/** The reader's preference where the repository allows it, and the first allowed method else. */
+export function resolveSelectedMergeMethod(
+ allowedMergeMethods: ReadonlyArray,
+ preferred: PullRequestMergeMethod,
+): PullRequestMergeMethod {
+ return allowedMergeMethods.includes(preferred) ? preferred : (allowedMergeMethods[0] ?? "merge");
+}
+
+/**
+ * Two questions, both of which have to say yes: whether this host can do it at all, and whether
+ * this account may. A reader with read access on someone else's project sees the pull request and
+ * none of the buttons that would only ever be refused.
+ */
+export function canPerformPullRequestAction(
+ detail: Pick | null,
+ action: PullRequestAction,
+): boolean {
+ return (
+ detail !== null &&
+ detail.capabilities.actions.includes(action) &&
+ detail.viewerPermissions.actions.includes(action)
+ );
+}
+
+export function isPullRequestConflicting(
+ detail: Pick | null,
+): boolean {
+ return detail?.state === "open" && detail.mergeability === "conflicting";
+}
+
+/**
+ * One live action holds the slot. A conflicting change cannot be merged now, so the slot goes to
+ * the thing that would help instead of a Merge button that only ever says no.
+ */
+export function resolvePullRequestPrimaryAction(
+ detail: PullRequestActionableDetail | null,
+): "ready" | "merge" | "resolve" | null {
+ if (detail === null || detail.state !== "open") return null;
+ if (detail.isDraft && canPerformPullRequestAction(detail, "ready")) return "ready";
+ if (!canPerformPullRequestAction(detail, "merge")) return null;
+ if (isPullRequestConflicting(detail)) return "resolve";
+ return allowedPullRequestMergeMethods(detail).length > 0 ? "merge" : null;
+}
+
/** Chronological ascending, oldest to newest — reversed for the "newest" reading order. */
export function orderPullRequestComments(
comments: ReadonlyArray,
diff --git a/apps/web/src/components/pullRequest/usePullRequestActions.ts b/apps/web/src/components/pullRequest/usePullRequestActions.ts
new file mode 100644
index 0000000000..43ab130a50
--- /dev/null
+++ b/apps/web/src/components/pullRequest/usePullRequestActions.ts
@@ -0,0 +1,333 @@
+/**
+ * The actions a pull request offers, extracted from the detail panel so smaller surfaces — the
+ * thread details panel's pull request row — perform them through the very same code. Two callers
+ * running two copies of "merge" or "resolve in a thread" would drift apart one fix at a time;
+ * these hooks are where that behavior lives, and the panels are only where it is rendered.
+ */
+import { scopeProjectRef } from "@t3tools/client-runtime/environment";
+import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
+import type {
+ EnvironmentId,
+ PullRequestAction,
+ PullRequestDetail,
+ PullRequestMergeMethod,
+ PullRequestRef,
+} from "@t3tools/contracts";
+import { useState } from "react";
+
+import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
+import { useNewThreadHandler } from "~/hooks/useHandleNewThread";
+import { usePreparePullRequestThreadAction } from "~/lib/sourceControlActions";
+import type { ReviewCommentContext } from "~/reviewCommentContext";
+import { pullRequestEnvironment } from "~/state/pullRequests";
+import { useAtomCommand } from "~/state/use-atom-command";
+
+import { toastManager } from "../ui/toast";
+import { handoffPrompt, handoffReviewComments, readableFailure } from "./pullRequestDetail.logic";
+
+const ACTION_SUCCESS_LABELS: Record = {
+ merge: "Pull request merged",
+ ready: "Marked ready for review",
+ draft: "Converted to draft",
+ close: "Pull request closed",
+ reopen: "Pull request reopened",
+};
+
+/** Said as the thing that did not happen, rather than as the operation that returned an error. */
+const ACTION_FAILURE_LABELS: Record = {
+ merge: "Could not merge this pull request",
+ ready: "Could not mark this ready for review",
+ draft: "Could not convert this to a draft",
+ close: "Could not close this pull request",
+ reopen: "Could not reopen this pull request",
+};
+
+/** What to try, for the times the host says only that it refused. */
+const ACTION_FAILURE_HINTS: Record = {
+ merge:
+ "The host refused the merge. Check that you have write access, that the checks it requires have passed, and that the branch is not conflicting.",
+ ready: "The host refused it. Check that you have write access to this repository.",
+ draft: "The host refused it. Check that you have write access to this repository.",
+ close: "The host refused it. Check that you have write access, or that you opened it.",
+ reopen:
+ "The host refused it. Check that you have write access, and that the branch still exists.",
+};
+
+/**
+ * Runs one host action against a pull request, with the toasts every surface should say the same
+ * way. `onSuccess` is where the caller re-reads whatever it is showing.
+ */
+export function usePullRequestActionRunner({
+ environmentId,
+ reference,
+ onSuccess,
+}: {
+ environmentId: EnvironmentId;
+ reference: PullRequestRef | null;
+ onSuccess?: (action: PullRequestAction) => void;
+}) {
+ const runAction = useAtomCommand(pullRequestEnvironment.runAction, { reportFailure: false });
+ const [actionPending, setActionPending] = useState(false);
+
+ const perform = async (action: PullRequestAction, method?: PullRequestMergeMethod) => {
+ if (actionPending || reference === null) return;
+ setActionPending(true);
+ const result = await runAction({
+ environmentId,
+ input: { ...reference, action, ...(method ? { mergeMethod: method } : {}) },
+ });
+ setActionPending(false);
+ if (result._tag === "Failure") {
+ // The host's own sentence, because it is the only thing that says why. A merge strategy a
+ // branch policy forbids is refused at completion and nowhere earlier — Azure DevOps
+ // publishes no per-strategy availability to hide the control with — so "action failed"
+ // would leave the reader pressing the same button again.
+ const failure = squashAtomCommandFailure(result);
+ toastManager.add({
+ type: "error",
+ title: ACTION_FAILURE_LABELS[action],
+ description: readableFailure(failure, ACTION_FAILURE_HINTS[action]),
+ });
+ return;
+ }
+ toastManager.add({ type: "success", title: ACTION_SUCCESS_LABELS[action] });
+ onSuccess?.(action);
+ };
+
+ return { actionPending, perform };
+}
+
+export interface PullRequestThreadTask {
+ prompt: string;
+ reviewComments?: ReadonlyArray;
+}
+
+/** What a hand-off needs to know about the pull request it is handing over. */
+export type PullRequestHandoffDetail = Pick<
+ PullRequestDetail,
+ "projectId" | "workspaceRoot" | "url"
+>;
+
+/**
+ * What the last hand-off wrote into each draft, kept outside React because the panel that wrote it
+ * is closed by the time the next one opens. It is how a prompt the reader has since edited is told
+ * apart from the one they were handed: only the sentence still exactly as written may be replaced.
+ */
+const lastHandoffPromptByDraft = new Map();
+
+/**
+ * The hand-offs from a pull request into a thread: a question that needs nothing checked out, and
+ * a task that needs the branch under the agent's feet first. One `handoff` key holds them all to
+ * one at a time, whatever surface pressed the button.
+ */
+export function usePullRequestHandoffs({
+ environmentId,
+ detail,
+}: {
+ environmentId: EnvironmentId;
+ detail: PullRequestHandoffDetail | null;
+}) {
+ const newThread = useNewThreadHandler();
+ const prepareThread = usePreparePullRequestThreadAction({
+ environmentId,
+ cwd: detail?.workspaceRoot ?? null,
+ });
+ // Which handoff is preparing, keyed so a per-finding button can say "Preparing..." on itself
+ // alone. One at a time whatever the key: they all check the same pull request out.
+ const [handoff, setHandoff] = useState(null);
+
+ /**
+ * Opens a thread on this project and leaves the task in its composer for the reader to send.
+ *
+ * Nothing is checked out: asking a question is not a reason to move somebody's working tree or
+ * to make a worktree they did not ask for. The two hand-offs that do need the code call this
+ * after preparing it, so there is one path from "a task" to "a thread holding it".
+ */
+ const openThreadWithTask = async (
+ projectRef: ReturnType,
+ task: PullRequestThreadTask | null,
+ opened?: { draftId: DraftId },
+ ): Promise<{ draftId: DraftId } | null> => {
+ const session =
+ opened ??
+ (await newThread(projectRef).then(
+ (result) => result,
+ () => null,
+ ));
+ if (session === null) return null;
+ const store = useComposerDraftStore.getState();
+ if (task === null) return session;
+ // The latest press is the ask: it takes over what an earlier hand-off left, prompt and chips
+ // both, rather than stacking a second one under the first. What the reader typed themselves
+ // survives — the composer they are handed is not always a fresh one, and a prompt they have
+ // since edited is theirs rather than the hand-off's.
+ const draft = store.getComposerDraft(session.draftId);
+ const existingComments = draft?.reviewComments ?? [];
+ const prompt = handoffPrompt(
+ {
+ prompt: draft?.prompt ?? "",
+ lastHandoffPrompt: lastHandoffPromptByDraft.get(session.draftId),
+ },
+ task.prompt,
+ );
+ // Remember the hand-off's own contribution, not the merged prompt: only that sentence is
+ // this session's to take back next time, and the reader's text around it is not.
+ lastHandoffPromptByDraft.set(session.draftId, task.prompt);
+ store.setPrompt(session.draftId, prompt);
+ store.setReviewComments(
+ session.draftId,
+ handoffReviewComments(existingComments, task.reviewComments ?? []),
+ );
+ return session;
+ };
+
+ /** A question about the change, which needs a thread and nothing else. */
+ const startAsk = async (kind: string, task: PullRequestThreadTask) => {
+ if (!detail || handoff !== null) return;
+ setHandoff(kind);
+ const projectRef = scopeProjectRef(environmentId, detail.projectId);
+ const opened = await openThreadWithTask(projectRef, task);
+ setHandoff(null);
+ if (opened === null) {
+ toastManager.add({
+ type: "error",
+ title: "Could not open a thread",
+ description: "Try again from the project, or open a thread first.",
+ });
+ return;
+ }
+ toastManager.add({
+ type: "success",
+ title: "Asked in a thread",
+ // "Ask" leaves the composer empty on purpose, so saying the question is in it would send
+ // the reader looking for something that is not there. The chips are what landed.
+ description:
+ task.prompt.length > 0
+ ? "The question is in the composer — read it over, then send."
+ : "The pull request is in the composer — type your question, then send.",
+ });
+ };
+
+ // Every handoff works the same way: check the pull request out into its own worktree, open a
+ // thread there, and — when it carries a task — put that in the composer for the user to read
+ // before sending. Checking out is the whole point of the ones that carry nothing.
+ const startHandoff = async (
+ kind: string,
+ task: PullRequestThreadTask | null,
+ // A worktree leaves whatever is open alone, which is why it is the default. Checking out in
+ // the repository itself is what you want when the point is to run the thing where you
+ // already work — and it moves the branch under everything else that is open there.
+ mode: "worktree" | "local" = "worktree",
+ ) => {
+ if (!detail || handoff !== null) return;
+ setHandoff(kind);
+ // The menu closes on the press and takes its "Preparing..." label with it, so this is the
+ // only thing answering for the checkout. It carries no timeout of its own: a loading toast
+ // never expires, and an explicit one would survive the update and pin the result on screen.
+ const toastId = toastManager.add({
+ type: "loading",
+ title: "Preparing the pull request checkout...",
+ });
+ const projectRef = scopeProjectRef(environmentId, detail.projectId);
+ // The thread is opened before the checkout rather than after it, because the project's setup
+ // script only runs for a checkout that knows which thread it is for — and a worktree with no
+ // dependencies installed is not something anyone can test.
+ const opened = await newThread(projectRef).then(
+ (session) => session,
+ () => null,
+ );
+ if (opened === null) {
+ setHandoff(null);
+ // Without a thread there is nowhere for the checkout to belong: its setup script would not
+ // run and its task would have no composer to land in. Better to stop before touching the
+ // working tree than to prepare a worktree nobody asked for.
+ toastManager.update(toastId, {
+ type: "error",
+ title: "Could not open a thread for the checkout",
+ description: "Try again from the project, or open a thread first.",
+ });
+ return;
+ }
+ const prepared = await prepareThread.run({
+ reference: detail.url,
+ mode,
+ threadId: opened.threadId,
+ });
+ if (prepared._tag === "Failure") {
+ setHandoff(null);
+ // The server says what to do about it — that the branch is already checked out in the main
+ // repository, say — and that sentence is the only way out of the failure.
+ const detailMessage =
+ prepareThread.error instanceof Error ? prepareThread.error.message : null;
+ toastManager.update(toastId, {
+ type: "error",
+ title: "Could not prepare the pull request checkout",
+ ...(detailMessage ? { description: detailMessage } : {}),
+ });
+ return;
+ }
+ // The same thread again, now that there is somewhere to point it at. A local checkout has
+ // no worktree of its own, so the thread runs where the repository already is.
+ const pointed = await newThread(projectRef, {
+ branch: prepared.value.branch,
+ worktreePath: prepared.value.worktreePath,
+ envMode: prepared.value.worktreePath === null ? "local" : "worktree",
+ }).then(
+ (session) => session !== null,
+ () => false,
+ );
+ if (!pointed) {
+ setHandoff(null);
+ // The checkout is on disk; only the thread failed to move onto it. Writing the task now
+ // would send the agent at whatever the thread was already open on — which is the one
+ // outcome worth stopping for, since it reads as success and is not.
+ toastManager.update(toastId, {
+ type: "error",
+ title: "Checked out, but the thread stayed where it was",
+ description: `The checkout is ready on \`${prepared.value.branch}\`. Point a thread at it from the branch picker, then ask again.`,
+ });
+ return;
+ }
+ // Released here whatever happened next: a loading toast never expires on its own, so leaving
+ // this set would spin forever and lock every handoff behind it until a reload.
+ setHandoff(null);
+ // A worktree that was already there and had been worked in keeps whatever it holds, so the
+ // thread opens on older code than the pull request carries. Said once, in place of the
+ // success, because everything else about the handoff did happen.
+ const staleCheckoutToast = {
+ type: "warning",
+ title: "Checked out, but not on the latest commits",
+ description:
+ "The checkout could not be moved onto the pull request's latest commits, so the code there is older than the pull request. Uncommitted work or local commits keep it where it is.",
+ } as const;
+ if (task === null) {
+ toastManager.update(
+ toastId,
+ prepared.value.isOnPullRequestHead
+ ? {
+ type: "success",
+ title: mode === "local" ? "Checked out here" : "Checked out",
+ description:
+ mode === "local"
+ ? "This repository is on the pull request's branch, with a thread open on it."
+ : "The pull request is in its own worktree, with a thread open on it.",
+ }
+ : staleCheckoutToast,
+ );
+ return;
+ }
+ await openThreadWithTask(projectRef, task, opened);
+ toastManager.update(
+ toastId,
+ prepared.value.isOnPullRequestHead
+ ? {
+ type: "success",
+ title: "Checkout ready",
+ description: "The task is in the composer — read it over, then send.",
+ }
+ : staleCheckoutToast,
+ );
+ };
+
+ return { handoff, startAsk, startHandoff };
+}