From 81838ebe41e5ef3f2a872780d249247581ed129c Mon Sep 17 00:00:00 2001 From: wSedlacek Date: Mon, 9 Nov 2026 10:10:46 -0800 Subject: [PATCH] feat(web): add failing checks to composer context chips Fixing a failed check no longer replaces the composer draft. Each check becomes an additive context chip, and Fix beside the failing count attaches every failing check in one chip. Thread details uses the same path. Co-authored-by: Cursor --- .../BranchToolbarBranchSelector.tsx | 1 + .../chat/ThreadDetailsPrRow.browser.test.tsx | 57 +++++- .../components/chat/ThreadDetailsPrRow.tsx | 73 +++++--- .../components/chat/ThreadDetailsPrRows.tsx | 12 +- .../chat/UserMessageContextChips.tsx | 29 +++- .../composerContextPresentation.tsx | 21 ++- .../pullRequest/PullRequestChecksPopover.tsx | 162 +++++++++++++----- .../pullRequest/PullRequestDetailPanel.tsx | 97 ++++++++--- .../pullRequest/PullRequestSummaryTab.tsx | 66 ++----- .../pullRequestDetail.logic.test.ts | 96 ++++++++++- .../pullRequest/pullRequestDetail.logic.ts | 130 +++++++++++++- .../pullRequest/usePullRequestActions.ts | 8 + apps/web/src/lib/composerContextRecords.ts | 8 + 13 files changed, 593 insertions(+), 167 deletions(-) diff --git a/apps/web/src/components/BranchToolbarBranchSelector.tsx b/apps/web/src/components/BranchToolbarBranchSelector.tsx index 9782f258afe19..681314396447c 100644 --- a/apps/web/src/components/BranchToolbarBranchSelector.tsx +++ b/apps/web/src/components/BranchToolbarBranchSelector.tsx @@ -738,6 +738,7 @@ export function BranchToolbarBranchSelector({ {displayMode === "panel" && prNumber !== undefined && prUrl !== undefined ? ( { label="Test PR" openAriaLabel="Open PR" onOpen={vi.fn()} + composerTarget={DraftId.make("draft")} />, ); }); @@ -136,6 +137,59 @@ it("merges with the last selected merge method", async () => { expect(state.perform).toHaveBeenCalledWith("merge", "squash"); }); +it("adds fixes from the row and checks popover to this thread's composer without a new thread", async () => { + await using screen = await setup(); + state.status = "failure"; + const composerTarget = { + environmentId: EnvironmentId.make("environment"), + threadId: ThreadId.make("thread"), + }; + await act( + async () => + await screen.rerender( + , + ), + ); + const insertAtCaret = vi.fn(() => true); + const removeHandler = useComposerDraftStore + .getState() + .setContextInsertionHandler(composerTarget, insertAtCaret); + using _handler = { [Symbol.dispose]: () => removeHandler?.() }; + await act(async () => + [...screen.container.querySelectorAll("button")] + .find((button) => button.textContent?.trim() === "Fix")! + .click(), + ); + await act(async () => + screen.container.querySelector('[aria-label^="Open checks"]')!.click(), + ); + await act(async () => + document.querySelector('[aria-label="Fix CI-failure"]')!.click(), + ); + + const draft = useComposerDraftStore.getState().getComposerDraft(composerTarget); + const comments = draft?.reviewComments; + expect(comments?.map((comment) => comment.filePath)).toEqual([ + "Failing checks on #1", + "CI-failure", + "PR #1", + ]); + expect(insertAtCaret).not.toHaveBeenCalled(); + expect(draft?.prompt).toContain("Failing checks on #1"); + expect(state.newThread).not.toHaveBeenCalled(); +}); + it("requires a new merge click after passing checks become pending and pass again", async () => { await using screen = await setup(); const { container } = screen; @@ -149,6 +203,7 @@ it("requires a new merge click after passing checks become pending and pass agai label="Test PR" openAriaLabel="Open PR" onOpen={vi.fn()} + composerTarget={DraftId.make("draft")} /> ); const clickMerge = async () => diff --git a/apps/web/src/components/chat/ThreadDetailsPrRow.tsx b/apps/web/src/components/chat/ThreadDetailsPrRow.tsx index 23d705a8dae7c..a99d5f897125c 100644 --- a/apps/web/src/components/chat/ThreadDetailsPrRow.tsx +++ b/apps/web/src/components/chat/ThreadDetailsPrRow.tsx @@ -8,18 +8,26 @@ import { ThreadDetailsControl } from "./ThreadDetailsControl"; import { PullRequestGlyph } from "../pullRequest/pullRequestIcons"; import { selectEnvironmentQueryData } from "@t3tools/client-runtime/state/runtime"; import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; -import type { EnvironmentId, ProjectId, PullRequestRef } from "@t3tools/contracts"; +import type { + EnvironmentId, + ProjectId, + PullRequestCheck, + PullRequestRef, + ScopedThreadRef, +} from "@t3tools/contracts"; import { ArrowUpRightIcon, EyeIcon, EyeOffIcon, FileDiffIcon, GitBranchIcon, + HammerIcon, TriangleAlertIcon, } from "lucide-react"; import { AsyncResult } from "effect/reactivity"; import { useState, type MouseEvent as ReactMouseEvent } from "react"; +import type { DraftId } from "~/composerDraftStore"; import { useLiveRefresh } from "~/hooks/useLiveRefresh"; import { usePullRequestChecksRefresh } from "~/hooks/usePullRequestChecksRefresh"; import { cn } from "~/lib/utils"; @@ -33,7 +41,8 @@ import { } from "~/state/query"; import { - buildFixFindingsHandoff, + buildFixChecksHandoff, + buildFixFindingHandoff, buildResolveConflictsPrompt, classifyPullRequestChecks, describePullRequestChecks, @@ -41,6 +50,7 @@ import { resolvePullRequestMergeMethod, allowedPullRequestMergeMethods, resolveThreadPanelPullRequestAction, + type PullRequestFinding, } from "../pullRequest/pullRequestDetail.logic"; import { PullRequestChecksPopover } from "../pullRequest/PullRequestChecksPopover"; import { @@ -53,6 +63,8 @@ import { usePullRequestActionRunner, usePullRequestDefaultMergeMethodResolver, usePullRequestHandoffs, + writeHandoffToComposer, + type PullRequestThreadTask, } from "../pullRequest/usePullRequestActions"; import { ChangeRequestStatusIcon, @@ -69,6 +81,7 @@ import { AlertDialogTitle, } from "../ui/alert-dialog"; import { Button } from "../ui/button"; +import { toastManager } from "../ui/toast"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { THREAD_DETAILS_PANEL_ICON_CLASS, @@ -93,6 +106,8 @@ export function ThreadDetailsPrRow({ onOpen, onActed, onStopWatching, + composerTarget, + threadRef = null, }: { environmentId: EnvironmentId; pr: ThreadPr; @@ -105,6 +120,8 @@ export function ThreadDetailsPrRow({ onOpen: (event: ReactMouseEvent) => void; onActed?: () => void; onStopWatching?: (() => void) | undefined; + composerTarget: ScopedThreadRef | DraftId; + threadRef?: ScopedThreadRef | null; }) { const serverConfigs = useServerConfigs(); const supportsPullRequests = serverConfigs.get(environmentId) !== undefined; @@ -201,25 +218,29 @@ export function ThreadDetailsPrRow({ }); }; - const startFixChecks = () => { + const addToComposer = (task: PullRequestThreadTask) => { + writeHandoffToComposer(composerTarget, task); + toastManager.add({ + type: "success", + title: "Added to the composer", + description: "The task is in the composer. Read it over, then send.", + }); + }; + const fixAllChecks = () => { if (detail === null) return; - void startHandoff( - "findings", - buildFixFindingsHandoff({ - number: detail.number, - title: detail.title, - url: detail.url, - headBranch: detail.headBranch, - baseBranch: detail.baseBranch, - state: detail.state, - isDraft: detail.isDraft, - reviewThreads: [], - comments: [], - checks: detail.checks, - commentsTruncated: false, - }), - ); + addToComposer(buildFixChecksHandoff(detail)); }; + const checksFix = + detail !== null && open + ? { + onFixCheck: (check: PullRequestCheck) => { + const finding: PullRequestFinding = { kind: "check", check }; + addToComposer(buildFixFindingHandoff({ ...detail, finding })); + }, + onFixAll: fixAllChecks, + pending: null, + } + : undefined; const statePresentation = detail === null @@ -342,12 +363,13 @@ export function ThreadDetailsPrRow({ : rowAction === "fix" ? { label: "Fix", - pendingLabel: "Preparing...", - pending: handoff === "findings", + pendingLabel: "Fix", + pending: false, destructive: true, - suffix: , - tooltip: "Fix the failing checks in a new thread", - onClick: startFixChecks, + icon: , + suffix: null, + tooltip: "Add the failing checks to the composer", + onClick: fixAllChecks, } : rowAction === "merge" ? { @@ -445,6 +467,8 @@ export function ThreadDetailsPrRow({ } /> @@ -466,6 +490,7 @@ export function ThreadDetailsPrRow({ /> } > + {trailingAction.icon} {trailingAction.pending ? trailingAction.pendingLabel : trailingAction.label} {trailingAction.suffix} diff --git a/apps/web/src/components/chat/ThreadDetailsPrRows.tsx b/apps/web/src/components/chat/ThreadDetailsPrRows.tsx index 2da6019787f5a..0823ca35aadea 100644 --- a/apps/web/src/components/chat/ThreadDetailsPrRows.tsx +++ b/apps/web/src/components/chat/ThreadDetailsPrRows.tsx @@ -28,12 +28,16 @@ function ThreadDetailsPrLinkRow({ onOpen, onActed, onStopWatching, + composerTarget, + threadRef, }: { environmentId: EnvironmentId; link: ThreadPullRequestLink; onOpen: (event: ReactMouseEvent) => void; onActed?: (() => void) | undefined; onStopWatching?: (() => void) | undefined; + composerTarget: ComponentProps["composerTarget"]; + threadRef: ScopedThreadRef; }) { const projects = useProjects(); const parsed = sourceControlClients.parseChangeRequestUrl(link.url)?.link; @@ -58,6 +62,8 @@ function ThreadDetailsPrLinkRow({ openAriaLabel={link.url} onOpen={onOpen} onStopWatching={onStopWatching} + composerTarget={composerTarget} + threadRef={threadRef} {...(onActed ? { onActed } : {})} /> ); @@ -91,7 +97,9 @@ export function ThreadDetailsPrRows({ watching: false, }, }); - const currentRow = ; + const currentRow = ( + + ); const rest = currentLink === null ? [] @@ -113,6 +121,8 @@ export function ThreadDetailsPrRows({ onOpen={(event) => onOpenLink(event, link.url)} onActed={row.onActed} onStopWatching={stopWatching(link)} + composerTarget={row.composerTarget} + threadRef={threadRef} /> ))} diff --git a/apps/web/src/components/chat/UserMessageContextChips.tsx b/apps/web/src/components/chat/UserMessageContextChips.tsx index 2638692caab26..f2212d1d89e22 100644 --- a/apps/web/src/components/chat/UserMessageContextChips.tsx +++ b/apps/web/src/components/chat/UserMessageContextChips.tsx @@ -24,7 +24,12 @@ import { PREFERRED_HIGHLIGHTER } from "../../lib/syntaxHighlighting"; import ChatMarkdown from "../ChatMarkdown"; import { EnvironmentContextChip } from "../EnvironmentContextChip"; import { ThreadContextChip } from "../ThreadContextChip"; -import { MessageCircleIcon, MousePointerClickIcon, PaintbrushIcon } from "lucide-react"; +import { + ListChecksIcon, + MessageCircleIcon, + MousePointerClickIcon, + PaintbrushIcon, +} from "lucide-react"; import type { ComposerContextId, KnownComposerContextRecord } from "@t3tools/contracts"; import { buildExpandedImagePreview } from "./ExpandedImagePreview"; import { WorkspaceEntryIcon, WorkspaceEntryTooltip } from "./WorkspaceEntryIcon"; @@ -39,6 +44,7 @@ import { UnresolvedChip, } from "../contextChipParts"; import { + isPullRequestCheckContext, isPullRequestSummaryContext, pullRequestContextDisplayState, pullRequestContextKindLabel, @@ -446,7 +452,12 @@ const userMessageContextPresentationRegistry = createContextPresentationRegistry } const isPullRequest = isPullRequestSummaryContext(record); const label = reviewCommentContextLabel(record); - const kindLabel = isPullRequest ? pullRequestContextKindLabel(record) : "Review comment"; + const isCheck = isPullRequestCheckContext(record); + const kindLabel = isPullRequest + ? pullRequestContextKindLabel(record) + : isCheck + ? "Check" + : "Review comment"; const pullRequestState = pullRequestContextDisplayState(record) ?? "unknown"; if (isPullRequest && record.pullRequest !== undefined) { return ( @@ -462,7 +473,15 @@ const userMessageContextPresentationRegistry = createContextPresentationRegistry copyMarkdown={context.copyMarkdown} accessibleLabel={`${kindLabel}, ${label}${record.pullRequest ? `, ${record.pullRequest.title}` : ""}`} kind={isPullRequest ? PULL_REQUEST_CHIP_KINDS[pullRequestState] : "review-comment"} - icon={isPullRequest ? : } + icon={ + isPullRequest ? ( + + ) : isCheck ? ( + + ) : ( + + ) + } label={label} >
- {formatWorkspaceRelativePath(comment.filePath, ctx.workspaceRoot)} + {isPullRequestCheckContext(comment) + ? comment.filePath + : formatWorkspaceRelativePath(comment.filePath, ctx.workspaceRoot)}
{comment.sectionTitle} ยท {comment.rangeLabel} diff --git a/apps/web/src/components/composerContextPresentation.tsx b/apps/web/src/components/composerContextPresentation.tsx index 845e921725b55..9cb4ec30496ab 100644 --- a/apps/web/src/components/composerContextPresentation.tsx +++ b/apps/web/src/components/composerContextPresentation.tsx @@ -8,7 +8,7 @@ import type { } from "@t3tools/contracts"; import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments"; import { videoMimeType } from "@t3tools/shared/video"; -import { MessageCircleIcon, MousePointerClickIcon } from "lucide-react"; +import { ListChecksIcon, MessageCircleIcon, MousePointerClickIcon } from "lucide-react"; import { createContext, type ReactElement, type ReactNode, use } from "react"; import { ComposerContextActionsContext } from "./composerContextActions"; @@ -23,6 +23,7 @@ import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons"; import { fileContextReference, imageContextReference, + isPullRequestCheckContext, isPullRequestSummaryContext, pullRequestContextDisplayState, pullRequestContextKindLabel, @@ -425,9 +426,23 @@ const composerContextPresentationRegistry = createContextPresentationRegistry< } return ( : } + icon={ + isPullRequest ? ( + + ) : isPullRequestCheckContext(entry.record) ? ( + + ) : ( + + ) + } label={reviewCommentContextLabel(entry.record)} - kindLabel={isPullRequest ? pullRequestContextKindLabel(entry.record) : "Review comment"} + kindLabel={ + isPullRequest + ? pullRequestContextKindLabel(entry.record) + : isPullRequestCheckContext(entry.record) + ? "Check" + : "Review comment" + } details={} detailsMode={definition.capabilities.details} kind={isPullRequest ? PULL_REQUEST_CHIP_KINDS[pullRequestState] : "review-comment"} diff --git a/apps/web/src/components/pullRequest/PullRequestChecksPopover.tsx b/apps/web/src/components/pullRequest/PullRequestChecksPopover.tsx index cfcea204a56ee..cf13030deb852 100644 --- a/apps/web/src/components/pullRequest/PullRequestChecksPopover.tsx +++ b/apps/web/src/components/pullRequest/PullRequestChecksPopover.tsx @@ -6,6 +6,7 @@ import type { PullRequestRef, ScopedThreadRef, } from "@t3tools/contracts"; +import { HammerIcon } from "lucide-react"; import { useState } from "react"; import { useOpenLink } from "~/browser/useOpenLink"; @@ -18,7 +19,12 @@ import { Button } from "../ui/button"; import { ScrollArea } from "../ui/scroll-area"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { toastManager } from "../ui/toast"; -import { groupPullRequestChecks } from "./pullRequestDetail.logic"; +import { + groupPullRequestChecks, + isFailingCheck, + PULL_REQUEST_FIX_CHECKS_KEY, + pullRequestFindingKey, +} from "./pullRequestDetail.logic"; import { PullRequestCheckStatusIcon, pullRequestCheckStatusLabel, @@ -52,62 +58,126 @@ function LazyChecksBody({ return ; } +export function useOpenPullRequestCheck(threadRef: ScopedThreadRef | null) { + const openLink = useOpenLink(threadRef); + return (url: string) => { + void openLink(url).catch((error: unknown) => { + console.error(error); + toastManager.add({ type: "error", title: "Unable to open check details" }); + }); + }; +} + +export function PullRequestFixButton({ + pending, + disabled, + onClick, + "aria-label": ariaLabel, +}: { + pending: boolean; + disabled: boolean; + onClick: () => void; + "aria-label": string; +}) { + return ( + + ); +} + +export function PullRequestCheckRow({ + check, + onOpen, + onFix, + pendingFix, +}: { + check: PullRequestCheck; + onOpen: (url: string) => void; + onFix?: ((check: PullRequestCheck) => void) | undefined; + pendingFix?: string | null | undefined; +}) { + const name = {check.name}; + return ( +
+ + {onFix && isFailingCheck(check) ? ( + onFix(check)} + /> + ) : null} +
+ ); +} + +export interface PullRequestChecksFix { + readonly onFixCheck: (check: PullRequestCheck) => void; + readonly onFixAll: () => void; + readonly pending: string | null; +} + function ChecksBody({ checks, threadRef, + fix, }: { checks: ReadonlyArray; threadRef: ScopedThreadRef | null; + fix?: PullRequestChecksFix | undefined; }) { - const openLink = useOpenLink(threadRef); + const openCheck = useOpenPullRequestCheck(threadRef); const [showAll, setShowAll] = useState(false); const { attention, running, completed } = groupPullRequestChecks(checks); const canCollapse = attention.length + running.length > 0 && completed.length > 0; const visibleChecks = [...attention, ...running, ...(showAll || !canCollapse ? completed : [])]; - const hasDetails = visibleChecks.some((check) => check.url !== null); if (checks.length === 0) { return

No checks reported

; } return ( <> - -
    + +
      {visibleChecks.map( (check, index) => ( /* oxlint-disable react/no-array-index-key -- Check names can repeat; these status rows have no local state. */
    • - - - {check.name}} /> - {check.description ?? check.name} - - - {pullRequestCheckStatusLabel(check)} - - {check.url === null ? null : ( - - )} +
    • ) /* oxlint-enable react/no-array-index-key */, )} @@ -134,6 +204,7 @@ export function PullRequestChecksPopover({ environmentId, reference, threadRef = null, + fix, variant = "icon", className, render, @@ -144,6 +215,7 @@ export function PullRequestChecksPopover({ environmentId?: EnvironmentId; reference?: PullRequestRef; threadRef?: ScopedThreadRef | null; + fix?: PullRequestChecksFix | undefined; variant?: "icon" | "count"; className?: string; render?: React.ReactElement; @@ -192,14 +264,26 @@ export function PullRequestChecksPopover({ ) : null} -

      {presentation.label}

      - {summary === null ? null :

      {summary}

      } +
      +
      +

      {presentation.label}

      + {summary === null ? null :

      {summary}

      } +
      + {fix && !stale && failedCount > 0 ? ( + + ) : null} +
      {stale ? (

      Check details are out of date. Refresh the pull request to update them.

      ) : checks !== undefined ? ( - + ) : environmentId !== undefined && reference !== undefined ? ( ; + additive?: boolean; }; const attachTarget = composerDraftTarget ?? null; @@ -826,7 +830,7 @@ export function PullRequestDetailPanel({ const startHandoff = async ( kind: string, - task: { prompt: string; reviewComments?: ReadonlyArray } | null, + task: ThreadTask | null, mode: "worktree" | "local" = "worktree", ) => { if (!handoffSummary || handoff !== null) return; @@ -1075,6 +1079,23 @@ export function PullRequestDetailPanel({ ); }; + const startFixChecks = () => { + if (!detail) return; + void startHandoff( + PULL_REQUEST_FIX_CHECKS_KEY, + buildFixChecksHandoff({ + number: detail.number, + title: detail.title, + url: detail.url, + headBranch: detail.headBranch, + baseBranch: detail.baseBranch, + state: detail.state, + isDraft: detail.isDraft, + checks: detail.checks, + }), + ); + }; + const startFixFindings = () => { if (!detail) return; void startHandoff( @@ -1590,31 +1611,52 @@ export function PullRequestDetailPanel({ }} > {tab === "summary" ? ( - showsApproveWorkflows ? ( - - ) : ( - - ) : ( - - ) - } - label={checksSummary} - /> - ) + <> + {showsApproveWorkflows ? ( + + ) : ( + startFixFinding({ kind: "check", check }), + onFixAll: startFixChecks, + pending: handoff, + } + : undefined + } + /> + ) : ( + + ) + } + label={checksSummary} + /> + )} + {canFixFindings && + !checksStale && + classifyPullRequestChecks(detail.checks) === "failing" ? ( + + ) : null} + ) : tab === "timeline" ? (
      void; /** Absent where there is no composer beside the panel to add a remark to. */ onAddToChat?: (subject: PullRequestChatSubject) => void; @@ -525,13 +521,7 @@ export function PullRequestSummaryTab({ })), ]; - const openLink = useOpenLink(threadRef); - const openCheck = (url: string) => { - void openLink(url).catch((error: unknown) => { - console.error(error); - toastManager.add({ type: "error", title: "Unable to open check details" }); - }); - }; + const openCheck = useOpenPullRequestCheck(threadRef); const update = useAtomCommand(pullRequestEnvironment.update, { reportFailure: false }); const updateComment = useAtomCommand(pullRequestEnvironment.updateComment, { @@ -887,49 +877,15 @@ export function PullRequestSummaryTab({

      No checks reported.

      ) : ( detail.checks.map( - ( - check, - index, - ) => /* oxlint-disable react/no-array-index-key -- Check names and URLs can repeat; these status rows have no local state. */ { - const finding = { kind: "check", check } as const; - const failing = check.status === "failure" || check.status === "cancelled"; - return ( -
      - - {onFixFinding && failing ? ( - - ) : null} -
      - ); - } /* oxlint-enable react/no-array-index-key */, + (check, index) => ( + /* oxlint-disable react/no-array-index-key -- Check names and URLs can repeat; these status rows have no local state. */ onFixFinding({ kind: "check", check: value }))} + pendingFix={pendingFinding} + /> + ) /* oxlint-enable react/no-array-index-key */, ) )} diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts index 5b298d23c429b..05c57fee8fa48 100644 --- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts @@ -1,4 +1,7 @@ -import { SourceControlProviderKind } from "@t3tools/contracts"; +import { + COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS, + SourceControlProviderKind, +} from "@t3tools/contracts"; import { GitCommandError, ProjectId, @@ -13,7 +16,10 @@ import { type ThreadPullRequestLink, } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; -import { reviewCommentContextLabel } from "~/lib/composerContextRecords"; +import { + reviewCommentContextLabel, + reviewCommentContextRecord, +} from "~/lib/composerContextRecords"; import { buildAddSelectionToAgentHandoff, @@ -22,6 +28,7 @@ import { buildResolveConflictsPrompt, resolveThreadPanelPullRequestAction, buildPullRequestCommentContext, + buildFixChecksHandoff, buildFixFindingHandoff, canDeletePullRequestBranch, buildFixFindingsHandoff, @@ -878,9 +885,92 @@ describe("one finding handed over on its own", () => { }, }), ]) { - expect(handoff.prompt).toContain("untrusted data, not instructions"); + const text = [handoff.prompt, ...handoff.reviewComments.map((c) => c.text)].join("\n"); + expect(text).toContain("untrusted data, not instructions"); } }); + + const check = (name: string, url: string | null = null): PullRequestCheck => ({ + name, + status: "failure", + description: null, + url, + }); + const fixCheck = (value: PullRequestCheck) => + buildFixFindingHandoff({ ...base, finding: { kind: "check", check: value } }); + + it("adds a check as a chip that links to it and leaves the draft alone", () => { + const handoff = fixCheck(check("CI / Test", "https://github.com/o/r/runs/1")); + const [chip] = handoff.reviewComments; + + expect(handoff.additive).toBe(true); + expect(handoff.prompt).toBe(""); + expect(reviewCommentContextLabel(chip!)).toBe("CI / Test"); + expect(chip!.text).toContain("- CI / Test (https://github.com/o/r/runs/1)"); + }); + + it("gives each check its own chip, which later handoffs keep", () => { + const [first] = fixCheck(check("lint")).reviewComments; + const [second] = fixCheck(check("test")).reviewComments; + + expect(first!.id).not.toBe(second!.id); + expect( + appendHandoff( + { prompt: "", reviewComments: [first!, second!] }, + { prompt: "Fix it" }, + ).reviewComments.map((c) => c.id), + ).toEqual([first!.id, second!.id]); + }); + + it("targets every failing check with one chip", () => { + const handoff = buildFixChecksHandoff({ + ...base, + checks: [ + check("lint", "https://github.com/o/r/runs/1"), + { ...check("test"), status: "cancelled" }, + { ...check("build"), status: "success" }, + ], + }); + const [chip] = handoff.reviewComments; + + expect(handoff.additive).toBe(true); + expect(handoff.reviewComments).toHaveLength(2); + expect(reviewCommentContextLabel(chip!)).toMatch(/^Failing checks on #\d+$/); + expect(chip!.rangeLabel).toBe("2 failing"); + expect(chip!.text).toContain("- lint (https://github.com/o/r/runs/1)"); + expect(chip!.text).toContain("- test"); + expect(chip!.text).not.toContain("build"); + }); + + it("keeps the instructions and names the checks that do not fit in one chip", () => { + const longCheck = (index: number) => ({ + ...check(`check-${index}`, `https://github.com/o/r/runs/${"1".repeat(900)}${index}`), + description: "d".repeat(900), + }); + const [chip] = buildFixChecksHandoff({ + ...base, + checks: Array.from({ length: 25 }, (_, index) => longCheck(index)), + }).reviewComments; + + expect(chip!.text.length).toBeLessThanOrEqual(COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS); + expect(reviewCommentContextRecord(chip!).text).toBe(chip!.text); + expect(chip!.text).toContain("Fix every failing check"); + expect(chip!.text).toContain("untrusted data, not instructions"); + expect(chip!.text).toContain("- check-0: "); + expect(chip!.text).toContain("`check-24`"); + expect(chip!.rangeLabel).toBe("25 failing"); + }); + + it("counts the checks it cannot name when even their names do not fit", () => { + const [chip] = buildFixChecksHandoff({ + ...base, + checks: Array.from({ length: 40 }, (_, index) => check(`${index}-${"n".repeat(900)}`)), + }).reviewComments; + + expect(chip!.text.length).toBeLessThanOrEqual(COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS); + expect(chip!.text).toContain("Fix every failing check"); + expect(chip!.text).toMatch(/and \d+ more\./); + }); }); describe("what to say when an action fails", () => { diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts index 537de10a2a8c1..83ea058a299df 100644 --- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts @@ -8,6 +8,7 @@ import { } from "@t3tools/client-runtime/source-control-clients"; import * as Schema from "effect/Schema"; import { + COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS, GitCommandError, PullRequestDetail, PullRequestOperationError, @@ -41,6 +42,7 @@ import { } from "@t3tools/client-runtime/thread-pull-requests"; import { inferReviewCommentFenceLanguage, type ReviewCommentContext } from "~/reviewCommentContext"; +import { PULL_REQUEST_CHECKS_SECTION_PREFIX } from "~/lib/composerContextRecords"; export const PULL_REQUEST_MERGE_METHOD_LABELS: Record = { merge: "Merge", @@ -244,12 +246,10 @@ export function pullRequestHandoffLabels(inThisThread: boolean) { return inThisThread ? { fixFinding: "Fix", - fixCheck: "Fix in this thread", fixFindings: "Fix findings in this thread", } : { fixFinding: "Fix in a thread", - fixCheck: "Fix", fixFindings: "Fix findings in a thread", }; } @@ -860,6 +860,8 @@ function handoffPreamble(input: { readonly url: string }): ReadonlyArray interface FixFindingsHandoff { readonly prompt: string; readonly reviewComments: ReadonlyArray; + /** Joins what the draft holds instead of replacing the last handoff. */ + readonly additive?: boolean; } export function appendHandoff( @@ -938,7 +940,7 @@ export function buildFixFindingsHandoff(input: { return [`${boundedField(comment.author?.login ?? "ghost")}${where}: ${boundedField(body)}`]; }); const failingChecks = input.checks - .filter((check) => check.status === "failure" || check.status === "cancelled") + .filter(isFailingCheck) .map((check) => boundedField(check.description ? `${check.name}: ${check.description}` : check.name), ); @@ -998,11 +1000,61 @@ export function buildFixFindingsHandoff(input: { }; } +export function isFailingCheck(check: PullRequestCheck): boolean { + return check.status === "failure" || check.status === "cancelled"; +} + +function checkListItem(check: PullRequestCheck): string { + const name = boundedField(check.description ? `${check.name}: ${check.description}` : check.name); + return check.url ? `- ${name} (${boundedField(check.url)})` : `- ${name}`; +} + +const OMITTED_CHECKS_NOTE_MAX_LENGTH = 2_000; + +function omittedChecksNote(omitted: ReadonlyArray): string { + const names: string[] = []; + let namesLength = 0; + for (const check of omitted) { + const name = `\`${boundedField(check.name)}\``; + namesLength += name.length + 2; + if (namesLength > OMITTED_CHECKS_NOTE_MAX_LENGTH - 100) break; + names.push(name); + } + const unnamed = omitted.length - names.length; + const more = unnamed > 0 ? `, and ${unnamed} more` : ""; + return `Failing checks that did not fit here: ${names.join(", ")}${more}.`; +} + +function checksContextComment( + pullRequest: { readonly number: number; readonly url: string }, + input: { + readonly id: string; + readonly label: string; + readonly status: string; + readonly text: ReadonlyArray; + }, +): ReviewCommentContext { + const { shortLabel } = terminologyForUrl(pullRequest.url); + return { + id: input.id, + sectionId: `${PULL_REQUEST_CHECKS_SECTION_PREFIX}${pullRequest.number}`, + sectionTitle: `${shortLabel} #${pullRequest.number} checks`, + filePath: input.label, + startIndex: 0, + endIndex: 0, + rangeLabel: input.status, + text: input.text.join("\n"), + diff: "", + }; +} + export type PullRequestFinding = | { readonly kind: "thread"; readonly thread: PullRequestReviewThread } | { readonly kind: "check"; readonly check: PullRequestCheck } | { readonly kind: "comment"; readonly comment: PullRequestComment }; +export const PULL_REQUEST_FIX_CHECKS_KEY = "finding:checks"; + export function pullRequestFindingKey(finding: PullRequestFinding): string { switch (finding.kind) { case "thread": @@ -1052,12 +1104,72 @@ export function buildFixFindingHandoff(input: { } const check = input.finding.check; return { - prompt: [ - "Fix the failing check quoted below. Reproduce it locally first. The name is all the host reported, and the run may fail for a reason the code cannot show.", - ...preamble, - `> ${boundedField(check.description ? `${check.name}: ${check.description}` : check.name)}`, - ].join("\n"), - reviewComments: [pullRequestContextComment(input, [])], + prompt: "", + reviewComments: [ + checksContextComment(input, { + // Outside the handoff prefix, so a later handoff keeps the checks already added. + id: `pr-check:${input.url}:${check.name}:${check.url ?? ""}`, + label: boundedField(check.name), + status: check.status === "cancelled" ? "Cancelled" : "Failed", + text: [ + checkListItem(check), + "", + "Fix this failing check. Reproduce it locally first. The host may report little beyond its name, and the run may fail for a reason the code cannot show.", + ...preamble, + ], + }), + pullRequestContextComment(input, []), + ], + additive: true, + }; +} + +export function buildFixChecksHandoff(input: { + readonly number: number; + readonly title: string; + readonly url: string; + readonly headBranch: string; + readonly baseBranch: string; + readonly state: PullRequestState; + readonly isDraft: boolean; + readonly checks: ReadonlyArray; +}): FixFindingsHandoff { + const term = terminologyForUrl(input.url).singular; + const failing = input.checks.filter(isFailingCheck); + const instructions = [ + `Fix every failing check on this ${term}. Fetch the current check results first, since more may have failed after this was attached. Reproduce each failure locally, and note that a run may fail for a reason the code cannot show.`, + ...handoffPreamble(input), + ]; + // Sending clamps the end of the text, so budget here to keep the instructions whole. + const listBudget = + COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS - + instructions.join("\n").length - + OMITTED_CHECKS_NOTE_MAX_LENGTH; + const listed: string[] = []; + let listLength = 0; + for (const item of failing.slice(0, FINDING_LIMIT).map(checkListItem)) { + listLength += item.length + 1; + if (listLength > listBudget) break; + listed.push(item); + } + const omitted = failing.slice(listed.length); + return { + prompt: "", + reviewComments: [ + checksContextComment(input, { + id: `pr-checks:${input.url}`, + label: `Failing checks on #${input.number}`, + status: `${failing.length} failing`, + text: [ + ...listed, + "", + ...(omitted.length > 0 ? [omittedChecksNote(omitted)] : []), + ...instructions, + ], + }), + pullRequestContextComment(input, []), + ], + additive: true, }; } diff --git a/apps/web/src/components/pullRequest/usePullRequestActions.ts b/apps/web/src/components/pullRequest/usePullRequestActions.ts index b4814dfbb5a4d..db75fb3ba8737 100644 --- a/apps/web/src/components/pullRequest/usePullRequestActions.ts +++ b/apps/web/src/components/pullRequest/usePullRequestActions.ts @@ -242,6 +242,7 @@ export function usePullRequestCloseBatch( export interface PullRequestThreadTask { prompt: string; reviewComments?: ReadonlyArray; + additive?: boolean; } export function writeHandoffToComposer( @@ -249,6 +250,13 @@ export function writeHandoffToComposer( task: PullRequestThreadTask, ) { const store = useComposerDraftStore.getState(); + if (task.additive === true) { + // The caret may be in a provider question's answer, which is not this draft. + for (const comment of task.reviewComments ?? []) { + store.addReviewComment(target, comment, { insertAtCaret: false }); + } + return; + } const draft = store.getComposerDraft(target); const next = appendHandoff( { prompt: draft?.prompt ?? "", reviewComments: draft?.reviewComments ?? [] }, diff --git a/apps/web/src/lib/composerContextRecords.ts b/apps/web/src/lib/composerContextRecords.ts index 5f8eda0008cd9..a32441c56c6a3 100644 --- a/apps/web/src/lib/composerContextRecords.ts +++ b/apps/web/src/lib/composerContextRecords.ts @@ -66,7 +66,15 @@ function basename(filePath: string): string { return filePath.split(/[\\/]/).at(-1) ?? filePath; } +export const PULL_REQUEST_CHECKS_SECTION_PREFIX = "pull-request-checks:"; + +export function isPullRequestCheckContext(comment: ReviewCommentPresentation): boolean { + return comment.sectionId.startsWith(PULL_REQUEST_CHECKS_SECTION_PREFIX); +} + export function reviewCommentContextLabel(comment: ReviewCommentPresentation): string { + // Check names often contain slashes, so they are not paths to shorten. + if (isPullRequestCheckContext(comment)) return comment.filePath; const pullRequestNumber = pullRequestContextNumber(comment); if (isPullRequestSummaryContext(comment) && pullRequestNumber !== null) { return `#${pullRequestNumber}`;