diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx index 1e14ab19d9012..349bf0f09d861 100644 --- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx +++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx @@ -27,6 +27,7 @@ import { type PullRequestSummary, resolveEnvironmentMachineKind, type ScopedThreadRef, + pullRequestConversationThreads, } from "@t3tools/contracts"; import { ArrowDownUpIcon, @@ -1278,14 +1279,14 @@ export function PullRequestDetailPanel({ /** * Brings a remark into the composer beside this panel, with the pull request it belongs to, - * and nothing else: what to ask about it is the reader's to write. A remark on a line travels - * as its whole conversation, the way fixing one does. + * and nothing else: what to ask about it is the reader's to write. A remark in a thread travels + * as its whole conversation, so a reply arrives with what it answers. */ const addCommentToChat = (subject: PullRequestChatSubject) => { if (!detail || attachTarget === null) return; const thread = subject.kind === "comment" - ? detail.reviewThreads.find((candidate) => + ? pullRequestConversationThreads(detail).find((candidate) => candidate.comments.some((comment) => comment.id === subject.comment.id), ) : undefined; diff --git a/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx b/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx index 1f409d282ecc3..e5441ec3fd90b 100644 --- a/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx +++ b/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx @@ -248,15 +248,14 @@ function CommentBody({ } /** - * What Add to chat hands over for a remark: a remark on a line goes as its whole conversation, - * the way fixing one does. Null when there are no words in it to talk about. + * What Add to chat hands over for a remark: a remark in a thread goes as its whole conversation. + * Null when there are no words in it to talk about. */ function chatSubject( comment: PullRequestComment, thread: PullRequestConversationThread | undefined, ): PullRequestChatSubject | null { - // An overview thread has no line to attach, so it goes as its opening remark, as fixing it does. - if (thread !== undefined && thread.path !== null) { + if (thread !== undefined) { return thread.comments.some((entry) => visibleBody(entry.body) !== null) ? { kind: "thread", thread } : null; @@ -768,8 +767,7 @@ export function PullRequestSummaryTab({ ? null : { kind: "comment", comment } : { kind: "thread", thread }; - // A reply goes with its conversation, from the card that heads it. - const chat = reply ? null : chatSubject(comment, thread); + const chat = chatSubject(comment, thread); return (
void; reactions: ReactionSurface; showPath?: boolean; - /** A reply goes with its conversation, from the card that heads it. */ - reply?: boolean; resolveButton?: ReactNode; }) { const gt = useGT(); @@ -335,7 +332,7 @@ function ConversationCard({ /> ) : null} {resolveButton} - {event.body && event.comment && !reply ? ( + {event.body && event.comment ? ( ) : null} {canReact || event.reactions.length > 0 ? ( @@ -600,7 +597,6 @@ function ReviewThreadEvent({ onOpen={onOpen} reactions={reactions} showPath={false} - reply /> ))} diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts index 3d0ae22b15017..d6d3c74d04bd8 100644 --- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts @@ -1,5 +1,6 @@ import { SourceControlProviderKind } from "@t3tools/contracts"; import { + COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS, GitCommandError, ProjectId, type PullRequestCheck, @@ -1241,6 +1242,124 @@ describe("adding a remark to the chat", () => { expect(sent).toContain("more characters of the review summary are omitted"); }); + it("brings an overview thread's replies along with its opening remark", () => { + const context = buildPullRequestCommentContext(pullRequest, { + kind: "thread", + thread: { + id: "t1", + path: null, + line: null, + side: "right", + isResolved: false, + isOutdated: false, + comments: [ + remark("c1", "Does this skip the prompt?"), + { + ...remark("c2", "One concern remains."), + author: { login: "coderabbit", name: null, avatarUrl: null }, + }, + ], + }, + }); + + expect(context && reviewCommentContextLabel(context)).toBe("#42 @octocat thread"); + expect(context?.text).toBe( + "octocat: Does this skip the prompt?\ncoderabbit: One concern remains.", + ); + }); + + it("keeps a reply when the remark it answers runs past the length bound", () => { + const context = buildPullRequestCommentContext(pullRequest, { + kind: "thread", + thread: { + id: "t1", + path: "src/app.ts", + line: 3, + side: "right", + isResolved: false, + isOutdated: false, + comments: [remark("c1", "x".repeat(5_000)), remark("c2", "One concern remains.")], + }, + }); + + expect(context?.text).toMatch(/\.\.\.\noctocat: One concern remains\.$/u); + }); + + it("puts a remark's visible text ahead of its collapsed details", () => { + const context = buildPullRequestCommentContext(pullRequest, { + kind: "comment", + comment: remark( + "c1", + `
\nAnalysis chain\n\n${"script ".repeat(400)}\n
\n\nOne concern remains.`, + ), + }); + + expect(context?.text).toMatch(/^octocat: One concern remains\.\n\n
{ + const context = buildPullRequestCommentContext(pullRequest, { + kind: "comment", + comment: remark("c1", `
\n${"script ".repeat(400)}
Tail.\nVisible.`), + }); + const nested = buildPullRequestCommentContext(pullRequest, { + kind: "comment", + comment: remark( + "c2", + `
\n
\n${"script ".repeat(400)}
\n${"more ".repeat(400)}\n
\nFinding.`, + ), + }); + + expect(context?.text).toMatch(/^octocat: Tail\.\nVisible\./u); + expect(nested?.text).toMatch(/^octocat: Finding\./u); + }); + + it("gives way to the newest replies when a thread outgrows what the composer sends", () => { + const comments = Array.from({ length: 20 }, (_, index) => + remark(`c${index}`, `${index} ${"x".repeat(990)}`), + ); + const context = buildPullRequestCommentContext(pullRequest, { + kind: "thread", + thread: { + id: "t1", + path: null, + line: null, + side: "right", + isResolved: false, + isOutdated: false, + comments, + nextCommentsCursor: "more", + }, + }); + + expect(context?.text.length).toBeLessThanOrEqual(COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS); + expect(context?.text).toMatch(/^octocat: 0 /u); + expect(context?.text).toMatch(/\n\(5 earlier replies omitted\.\)\noctocat: 6 /u); + expect(context?.text).toMatch(/octocat: 19 x+\.\.\.\n\(More replies are on the host/u); + }); + + it("keeps a details section that starts open in place", () => { + const context = buildPullRequestCommentContext(pullRequest, { + kind: "comment", + comment: remark( + "c1", + `
\nFinding\n\nRename x.\n
\n\n${"Context. ".repeat(200)}`, + ), + }); + + expect(context?.text).toMatch(/^octocat:
/u); + }); + + it("leaves a details tag shown inside code where it is", () => { + const body = `\`\`\`html\n
\nExample\n
\n\`\`\`\n${"Explanation. ".repeat(100)}`; + const context = buildPullRequestCommentContext(pullRequest, { + kind: "comment", + comment: remark("c1", body), + }); + + expect(context?.text).toMatch(/^octocat: ```html\n
/u); + }); + it("keeps every added remark when a later hand-off lands", () => { const added = ["c1", "c2"].flatMap( (id) => diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts index d98ec3ff114ec..b9501ba2b27e2 100644 --- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts @@ -713,6 +713,62 @@ function readableBody(raw: string): string | null { return readable === "" ? null : readable; } +/** + * A body too long for the bound, with collapsed `
` sections moved after the text a + * reader sees so the bound keeps that text. Details shown inside code are source and stay put. + */ +function visibleTextFirst(body: string): string { + if (body.length <= FINDING_BODY_MAX_LENGTH) return body; + const lines = body.split("\n"); + const lineStarts: number[] = []; + let offset = 0; + for (const line of lines) { + lineStarts.push(offset); + offset += line.length + 1; + } + const codeLines = new Set(); + const visit = (node: Nodes) => { + // Words' source positions carry offsets only; their line numbers are not source lines. + const start = node.position?.start.offset; + const end = node.position?.end.offset; + if (node.type === "element" && node.tagName === "pre" && start != null && end != null) { + lineStarts.forEach((lineStart, index) => { + if (lineStart < end && lineStart + lines[index]!.length >= start) codeLines.add(index); + }); + } + if ("children" in node) for (const child of node.children) visit(child); + }; + visit(markdownTree(body, { skipHtml: true })); + const shown: string[] = []; + const folded: string[] = []; + // Whether each open section starts collapsed; `
` reads as visible text. + const sections: boolean[] = []; + for (const [index, line] of lines.entries()) { + const inCode = codeLines.has(index); + // Only a line that starts as a details block opens sections; a tag named in prose does not. + const opens = !inCode && /^\s*])/iu.test(line); + let from = 0; + for (const tag of inCode ? [] : line.matchAll(/])([^>]*)>?|<\/details>/giu)) { + if (tag[0].startsWith("; +/** + * A conversation's lines within what the composer sends. Earlier replies give way first, since a + * reader reaches for the latest one, and the agent is told what it is not seeing. + */ +function threadText(lines: ReadonlyArray, moreOnHost: boolean): string { + const tail = moreOnHost ? ["(More replies are on the host and not included here.)"] : []; + let kept = lines; + let omitted = 0; + const join = () => + [ + ...kept.slice(0, 1), + ...(omitted > 0 + ? [`(${omitted} earlier ${omitted === 1 ? "reply" : "replies"} omitted.)`] + : []), + ...kept.slice(1), + ...tail, + ].join("\n"); + while (kept.length > 2 && join().length > COMPOSER_CONTEXT_REVIEW_TEXT_MAX_CHARS) { + kept = [kept[0]!, ...kept.slice(2)]; + omitted += 1; + } + return join(); +} + +/** A remark the reader can bring into the chat: a whole conversation, or one comment. */ +export type PullRequestChatSubject = + | { readonly kind: "thread"; readonly thread: PullRequestConversationThread } + | { readonly kind: "comment"; readonly comment: PullRequestComment }; /** * A remark or conversation brought into the chat to talk about, not to fix. Its id carries the @@ -1307,36 +1389,65 @@ export function buildPullRequestCommentContext( subject: PullRequestChatSubject, ): ReviewCommentContext | null { const pullRequestNumber = pullRequest.number; - // Unlike a batch fix, one remark brought in to talk about travels whole. - if (subject.kind === "thread") { - const text = reviewThreadText(subject.thread); - return text === "" - ? null - : { - ...reviewThreadContext(subject.thread, pullRequestNumber), - id: `pr-comment:${pullRequest.url}:thread:${subject.thread.id}`, - text, - }; + if (subject.kind === "comment") { + const comment = subject.comment; + const body = readableBody(comment.body); + if (body === null) return null; + const author = comment.author?.login ?? "ghost"; + return { + id: `pr-comment:${pullRequest.url}:comment:${comment.id}`, + sectionId: `pull-request:${pullRequestNumber}`, + // oxlint-disable-next-line t3code/no-untranslated-copy -- context title sent to the agent + sectionTitle: `PR #${pullRequestNumber} conversation`, + // The chip wears `filePath rangeLabel`: the file a remark names, or who wrote one that names none. + filePath: comment.path ?? `@${author}`, + startIndex: 0, + endIndex: 0, + rangeLabel: "comment", + text: remarkText( + `${author}: ${visibleTextFirst(body)}`.trim(), + reviewLineComments(comment, pullRequest), + ), + diff: "", + ...(comment.path === null + ? {} + : { fenceLanguage: inferReviewCommentFenceLanguage(comment.path) }), + }; + } + const comments = subject.thread.comments; + // Bounded per comment so a long bot report cannot crowd the replies out of the thread. + const lines = comments.flatMap((comment) => { + const body = readableBody(comment.body); + return body === null + ? [] + : [bounded(`${comment.author?.login ?? "ghost"}: ${visibleTextFirst(body)}`)]; + }); + const first = comments[0]; + if (lines.length === 0 || first === undefined) return null; + const text = threadText( + lines, + (subject.thread.commentCount ?? 0) > comments.length || + subject.thread.nextCommentsCursor !== undefined, + ); + if (subject.thread.path !== null) { + return { + ...reviewThreadContext(subject.thread, pullRequestNumber), + id: `pr-comment:${pullRequest.url}:thread:${subject.thread.id}`, + text, + }; } - const comment = subject.comment; - const body = readableBody(comment.body); - if (body === null) return null; - const author = comment.author?.login ?? "ghost"; return { - id: `pr-comment:${pullRequest.url}:comment:${comment.id}`, + id: `pr-comment:${pullRequest.url}:thread:${subject.thread.id}`, sectionId: `pull-request:${pullRequestNumber}`, // oxlint-disable-next-line t3code/no-untranslated-copy -- context title sent to the agent sectionTitle: `PR #${pullRequestNumber} conversation`, - // The chip wears `filePath rangeLabel`: the file a remark names, or who wrote one that names none. - filePath: comment.path ?? `@${author}`, + // The chip wears `filePath rangeLabel`: who opened a thread that names no file. + filePath: `@${first.author?.login ?? "ghost"}`, startIndex: 0, endIndex: 0, - rangeLabel: "comment", - text: remarkText(`${author}: ${body}`.trim(), reviewLineComments(comment, pullRequest)), + rangeLabel: "thread", + text, diff: "", - ...(comment.path === null - ? {} - : { fenceLanguage: inferReviewCommentFenceLanguage(comment.path) }), }; }