Skip to content
TypeScript

fix(web): Add to agent works on pull requests you cannot comment on (#19701)

Shubhanshu Shuklaee3a3e8eca5d
diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
index 60cfe7b049d..41b8db7114e 100644
--- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
@@ -463,6 +463,9 @@ function PullRequestCodeTab({
resolve: hostReview.resolve && detail.viewerPermissions.resolve,
};
const canCommentOnLines = review.inlineComment && commit === null;
+ // Agent context is local, so it does not require host comment permission.
+ const canSelectLines =
+ commit === null && (review.inlineComment || onAddToAgentSelection !== undefined);
// The cache key carries the theme, which is what the tokenizer caches against, so a theme
// change is still a fresh parse. The patch's own hash is part of it too: a refreshed page
// reuses its cursor, and a key of position alone would hand back the parse it replaced.
@@ -708,7 +711,7 @@ function PullRequestCodeTab({
range: SelectedLineRange | null,
context: { item: CodeViewItem<ReviewAnnotationGroup> },
) => {
- if (!range || !canCommentOnLines) return;
+ if (!range || !canSelectLines) return;
const item = context.item;
if (item.type !== "diff") return;
const file = files.find((candidate) => buildFileDiffRenderKey(candidate) === item.id);
@@ -832,8 +835,8 @@ function PullRequestCodeTab({
themeType: resolvedTheme,
stickyHeaders: true,
loadDiffFiles,
- enableGutterUtility: canCommentOnLines && draft === null,
- enableLineSelection: canCommentOnLines && draft === null,
+ enableGutterUtility: canSelectLines && draft === null,
+ enableLineSelection: canSelectLines && draft === null,
onGutterUtilityClick: beginComment,
onLineSelectionEnd: beginComment,
};
@@ -930,13 +933,13 @@ function PullRequestCodeTab({
onRemove={() => removeComment(reviewKey, comment.id)}
/>
))}
- {annotation.metadata.draft && draft ? (
+ {annotation.metadata.draft && draft && canSelectLines ? (
<DiffCommentAnnotation
kind="draft"
rangeLabel={`${draft.path}:${getReviewPositionAnchor(draft.position).line}`}
text=""
- submitLabel="Add to review"
- {...(onAddToAgentSelection
+ submitLabel={canCommentOnLines ? "Add to review" : "Add to agent"}
+ {...(canCommentOnLines && onAddToAgentSelection
? {
secondaryAction: {
label: "Add to agent",
@@ -952,6 +955,14 @@ function PullRequestCodeTab({
setSelectedLines(null);
}}
onComment={(body) => {
+ if (!canCommentOnLines) {
+ if (onAddToAgentSelection) {
+ finishSelection(draft, body, (comment) =>
+ onAddToAgentSelection({ comment, request: body }),
+ );
+ }
+ return;
+ }
addComment(reviewKey, {
id: nextPendingReviewCommentId(),
path: draft.path,
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.browser.test.tsx
index 8df76af4a24..4e847e9cc7c 100644
--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.browser.test.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.browser.test.tsx
@@ -13,21 +13,32 @@ import * as Cause from "effect/Cause";
import { act, type ComponentProps } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
-import { page } from "vite-plus/test/browser";
+import { page, userEvent } from "vite-plus/test/browser";
import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
import "~/index.css";
-const { newThread, prepareThread, refresh, update, addToast, updateToast, checkoutState } =
- vi.hoisted(() => ({
- newThread: vi.fn(),
- prepareThread: vi.fn(),
- refresh: vi.fn(),
- update: vi.fn(),
- addToast: vi.fn(),
- updateToast: vi.fn(),
- checkoutState: { error: null as Error | null },
- }));
+const {
+ newThread,
+ prepareThread,
+ refresh,
+ update,
+ addToast,
+ updateToast,
+ checkoutState,
+ hostCommand,
+ queries,
+} = vi.hoisted(() => ({
+ hostCommand: vi.fn(),
+ queries: { diff: undefined as unknown },
+ newThread: vi.fn(),
+ prepareThread: vi.fn(),
+ refresh: vi.fn(),
+ update: vi.fn(),
+ addToast: vi.fn(),
+ updateToast: vi.fn(),
+ checkoutState: { error: null as Error | null },
+}));
vi.mock(import("~/state/session"), async (original) => ({
...(await original()),
@@ -52,17 +63,20 @@ vi.mock(import("~/lib/sourceControlActions"), async (original) => ({
}));
vi.mock(import("~/state/use-atom-command"), async (original) => ({
...(await original()),
- useAtomCommand: ((command: unknown) => (command === "update" ? update : vi.fn())) as never,
+ useAtomCommand: ((command: unknown) => (command === "update" ? update : hostCommand)) as never,
}));
vi.mock(import("~/state/pullRequests"), async (original) => {
const actual = await original();
const { Atom } = await import("effect/reactivity");
const cannotWriteGuides = Atom.make(false);
+ const diff = Atom.make(null);
+ queries.diff = diff;
return {
...actual,
pullRequestEnvironment: {
...actual.pullRequestEnvironment,
detail: (() => "detail") as never,
+ diff: (() => diff) as never,
activity: (() => "activity") as never,
update: "update" as never,
reviewGuide: (() => "review-guide") as never,
@@ -76,7 +90,7 @@ vi.mock(import("~/state/pullRequests"), async (original) => {
vi.mock(import("~/state/query"), async (original) => ({
...(await original()),
useEnvironmentQuery: ((query: unknown) => ({
- data: query === "detail" ? detail : null,
+ data: query === "detail" ? currentDetail : query === queries.diff ? diff : null,
dataUpdatedAt: 0,
failure: null,
isPending: false,
@@ -95,8 +109,7 @@ vi.mock(import("~/state/usePullRequestStack"), async (original) => ({
refresh,
})) as never,
}));
-// The real tabs load diffs, markdown and checks from the environment; these stand-ins
-// expose the actions they hand back to the panel and a tall scroller.
+// The summary stand-in exposes its panel actions and nested scrolling.
vi.mock(import("./PullRequestSummaryTab"), async (original) => ({
...(await original()),
PullRequestSummaryTab: (({
@@ -122,38 +135,6 @@ vi.mock(import("./PullRequestSummaryTab"), async (original) => ({
</div>
)) as never,
}));
-vi.mock(import("./PullRequestCodeTab"), async (original) => ({
- ...(await original()),
- default: (({
- onAddToAgentSelection,
- scrollerRef,
- }: ComponentProps<typeof import("./PullRequestCodeTab").default>) => (
- <div ref={scrollerRef} data-testid="code-scroller" className="h-full overflow-y-auto">
- <button
- type="button"
- onClick={() =>
- onAddToAgentSelection?.({
- request: "Fix this line",
- comment: {
- id: "note-1",
- sectionId: "file:a.ts",
- sectionTitle: "a.ts",
- filePath: "a.ts",
- startIndex: 0,
- endIndex: 0,
- rangeLabel: "L1",
- text: "Please fix",
- diff: "+broken()",
- },
- })
- }
- >
- Add to agent
- </button>
- <div className="h-[3000px]" />
- </div>
- )) as never,
-}));
vi.mock(import("../ui/toast"), async (original) => {
const actual = await original();
@@ -165,6 +146,7 @@ vi.mock(import("../ui/toast"), async (original) => {
import { PullRequestDetailPanel } from "./PullRequestDetailPanel";
import { pullRequestPanelContext } from "./pullRequestDetail.logic";
+import { usePullRequestReviewStore } from "./pullRequestReviewStore";
const detail: PullRequestDetailView = {
provider: SourceControlProviderKind.make("github"),
@@ -218,6 +200,24 @@ const detail: PullRequestDetailView = {
},
};
+let currentDetail = detail;
+
+// Enough lines to exercise the panel's title folding with the real virtualized Code tab.
+const diff = {
+ patch: [
+ "diff --git a/a.ts b/a.ts",
+ "new file mode 100644",
+ "--- /dev/null",
+ "+++ b/a.ts",
+ "@@ -0,0 +1,100 @@",
+ "+broken()",
+ ...Array.from({ length: 99 }, (_, index) => `+// line ${index + 2}`),
+ "",
+ ].join("\n"),
+ truncated: false,
+ nextCursor: null,
+};
+
const threadRef: ScopedThreadRef = {
environmentId: EnvironmentId.make("env-1"),
threadId: ThreadId.make("thread-1"),
@@ -257,7 +257,10 @@ function render(props: Partial<ComponentProps<typeof PullRequestDetailPanel>> =
beforeEach(() => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ currentDetail = detail;
useComposerDraftStore.setState({ draftsByThreadKey: {} });
+ usePullRequestReviewStore.setState({ drafts: {}, summaries: {} });
+ hostCommand.mockReset();
newThread
.mockReset()
.mockResolvedValue({ draftId: newDraftId, threadId: ThreadId.make("new-thread") });
@@ -300,6 +303,16 @@ const unrelatedThread = {
const tab = (name: string) => page.getByRole("button", { name, exact: true });
+const selectionDraft = () => page.getByRole("textbox", { name: "Comment on lines a.ts:1" });
+
+async function selectFirstLine() {
+ const line = page.getByText("broken()", { exact: true });
+ await expect.element(line).toBeVisible();
+ const host = line.element().getRootNode() as ShadowRoot;
+ const gutter = host.querySelector<HTMLElement>('[data-column-number="1"]')!;
+ await userEvent.click(gutter);
+}
+
it.each([
{ isComposing: true, keyCode: 13 },
{ isComposing: false, keyCode: 229 },
@@ -333,6 +346,14 @@ it.each([
async function runAction(action: string) {
if (action === "Add to agent") {
await tab("Code").click();
+ await page.getByRole("button", { name: "Expand all files", exact: true }).click();
+ await selectFirstLine();
+ const draft = page.getByRole("textbox", { name: "Comment on lines a.ts:1" });
+ await expect.element(draft).toBeVisible();
+ expect(
+ page.getByRole("button", { name: "Add to review", exact: true }).elements(),
+ ).toHaveLength(0);
+ await draft.fill("Fix this line");
}
if (["Ask a question", "Explain this PR"].includes(action) || action.startsWith("Fix findings")) {
await page.getByRole("button", { name: "More pull request actions" }).click();
@@ -369,6 +390,17 @@ describe.each([
"Fix check",
"Add to agent",
])("%s writes to the correct composer", async (action) => {
+ // Cover both a host without inline comments and an account that cannot write them.
+ currentDetail =
+ thread === stackThread
+ ? {
+ ...detail,
+ capabilities: {
+ ...detail.capabilities,
+ review: { ...detail.capabilities.review, inlineComment: true },
+ },
+ }
+ : { ...detail, viewerPermissions: { ...detail.viewerPermissions, comment: true } };
if (target) useComposerDraftStore.getState().setPrompt(target, "Keep my draft");
await renderPanel();
await runAction(target ? action : action.replace("in this thread", "in a thread"));
@@ -384,8 +416,20 @@ describe.each([
if (action === "Add to agent") {
expect(draft()?.prompt).toContain("Fix this line");
expect(draft()?.reviewComments).toEqual(
- expect.arrayContaining([expect.objectContaining({ id: "note-1", diff: "+broken()" })]),
+ expect.arrayContaining([
+ expect.objectContaining({
+ filePath: "a.ts",
+ diff: expect.stringContaining("+broken()"),
+ startIndex: 0,
+ endIndex: 0,
+ }),
+ ]),
);
+ expect(usePullRequestReviewStore.getState().drafts).toEqual({});
+ expect(hostCommand).not.toHaveBeenCalled();
+ await expect
+ .element(page.getByRole("textbox", { name: "Comment on lines a.ts:1" }))
+ .not.toBeInTheDocument();
}
if (target) {
expect(draft()?.prompt).toContain("Keep my draft");
@@ -397,13 +441,72 @@ describe.each([
});
});
+it("offers both selection actions and queues a permitted host review locally", async () => {
+ currentDetail = {
+ ...detail,
+ capabilities: {
+ ...detail.capabilities,
+ review: { ...detail.capabilities.review, inlineComment: true },
+ },
+ viewerPermissions: { ...detail.viewerPermissions, comment: true },
+ };
+ await render({ composerDraftTarget: draftId, threadRef });
+ await tab("Code").click();
+ await page.getByRole("button", { name: "Expand all files", exact: true }).click();
+ await selectFirstLine();
+ const addToReview = page.getByRole("button", { name: "Add to review", exact: true });
+ await expect.element(addToReview).toBeVisible();
+ await expect
+ .element(page.getByRole("button", { name: "Add to agent", exact: true }))
+ .toBeVisible();
+ await selectionDraft().fill("Fix this line before merging");
+ await addToReview.click();
+ await expect.element(selectionDraft()).not.toBeInTheDocument();
+ expect(Object.values(usePullRequestReviewStore.getState().drafts).flat()).toEqual([
+ expect.objectContaining({ path: "a.ts", body: "Fix this line before merging" }),
+ ]);
+ expect(useComposerDraftStore.getState().getComposerDraft(draftId)?.reviewComments ?? []).toEqual(
+ [],
+ );
+ expect(hostCommand).not.toHaveBeenCalled();
+});
+
+it("cancels an agent-only selection and submits a fresh one with the keyboard without a host review", async () => {
+ await render({ composerDraftTarget: draftId, threadRef });
+ await tab("Code").click();
+ await page.getByRole("button", { name: "Expand all files", exact: true }).click();
+ await selectFirstLine();
+ await selectionDraft().fill("Discard this request");
+ await page.getByRole("button", { name: "Cancel", exact: true }).click();
+ await expect.element(selectionDraft()).not.toBeInTheDocument();
+ expect(useComposerDraftStore.getState().getComposerDraft(draftId)?.reviewComments ?? []).toEqual(
+ [],
+ );
+ expect(usePullRequestReviewStore.getState().drafts).toEqual({});
+ expect(hostCommand).not.toHaveBeenCalled();
+
+ await selectFirstLine();
+ await expect.element(selectionDraft()).toHaveValue("");
+ await selectionDraft().fill("Fix this line");
+ await userEvent.keyboard("{Control>}{Enter}{/Control}");
+ await expect.element(selectionDraft()).not.toBeInTheDocument();
+ expect(useComposerDraftStore.getState().getComposerDraft(draftId)?.prompt).toContain(
+ "Fix this line",
+ );
+ expect(usePullRequestReviewStore.getState().drafts).toEqual({});
+ expect(hostCommand).not.toHaveBeenCalled();
+});
+
it("folds the title only for the active tab's own scroller", async () => {
await render();
const title = page.getByRole("heading", { level: 1, name: "Test pull request" });
await expect.element(title).toBeVisible();
const titleFold = () => title.element().closest("[inert]") !== null;
const scrollTo = async (testId: string, top: number) => {
- const scroller = page.getByTestId(testId).element() as HTMLElement;
+ const scroller =
+ testId === "code-scroller"
+ ? document.querySelector<HTMLElement>(".\\@container.overflow-auto")!
+ : (page.getByTestId(testId).element() as HTMLElement);
await act(async () => {
scroller.scrollTop = top;
scroller.dispatchEvent(new Event("scroll"));
@@ -425,7 +528,8 @@ it("folds the title only for the active tab's own scroller", async () => {
await vi.waitFor(() => expect(titleFold()).toBe(true));
await tab("Code").click();
- await expect.element(page.getByTestId("code-scroller")).toBeInTheDocument();
+ await page.getByRole("button", { name: "Expand all files", exact: true }).click();
+ await expect.element(page.getByText("broken()", { exact: true })).toBeVisible();
await vi.waitFor(() => expect(titleFold()).toBe(false));
await scrollTo("code-scroller", 600);
await vi.waitFor(() => expect(titleFold()).toBe(true));