fix(web): preserve citations when adding review comments #19955
main fromrefs/pull/19955/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
- apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx line 1140OutdatedResolvedby jakeleventhal(GitHub profile, opens in a new tab)@@ -1132,3 +1133,8 @@11321133 const writeTaskToComposer = (target: ScopedThreadRef | DraftId, task: ThreadTask) => {11331134 const store = useComposerDraftStore.getState();11341135 const draft = store.getComposerDraft(target);1136+ // Line comments accumulate in the draft, including their citations.1137+ if (task.append) {1138+ store.setPrompt(1139+ target,1140+ handoffPrompt({ prompt: draft?.prompt ?? "", lastHandoffPrompt: undefined }, task.prompt),apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx line 1143OutdatedResolvedby jakeleventhal(GitHub profile, opens in a new tab)@@ -1132,3 +1133,11 @@11321133 const writeTaskToComposer = (target: ScopedThreadRef | DraftId, task: ThreadTask) => {11331134 const store = useComposerDraftStore.getState();11341135 const draft = store.getComposerDraft(target);1136+ // Line comments accumulate in the draft, including their citations.1137+ if (task.append) {1138+ store.setPrompt(1139+ target,1140+ handoffPrompt({ prompt: draft?.prompt ?? "", lastHandoffPrompt: undefined }, task.prompt),1141+ );1142+ for (const comment of task.reviewComments ?? []) {1143+ store.addReviewComment(target, comment, {
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/web/src/components/pullRequest/pullRequestDetail.logic.ts lines 837 to 841OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -833,13 +832,10 @@833832 ): string {834833 if (existing.prompt.trim().length === 0) return incoming;835834 const last = existing.lastHandoffPrompt ?? "";836- // Only the sentence the last hand-off wrote is taken back: alone, or off the end of the837- // reader's own text it was appended under.838- const kept =839- last.length === 0840- ? existing.prompt841- : existing.prompt === last842- ? ""843- : existing.prompt.endsWith(`\n\n${last}`)844- ? existing.prompt.slice(0, -(last.length + 2))845- : existing.prompt;835+ // Line comments may have been appended after the last general request. Remove only its836+ // unchanged paragraph, wherever it now sits, leaving the reader's edits and comments alone.837+ const start = last.length === 0 ? -1 : existing.prompt.lastIndexOf(last);838+ const before = existing.prompt.slice(0, start);839+ const after = existing.prompt.slice(start + last.length);840+ const ownsParagraph =841+ start >= 0 && (start === 0 || before.endsWith("\n\n")) && /^[ \t]*(?:\n\n|$)/u.test(after);
- apps/web/src/components/pullRequest/pullRequestDetail.logic.ts lines 843 to 843OutdatedResolvedby jakeleventhal(GitHub profile, opens in a new tab)@@ -834,12 +833,11 @@834833 if (existing.prompt.trim().length === 0) return incoming;835834 const last = existing.lastHandoffPrompt ?? "";836- // Only the sentence the last hand-off wrote is taken back: alone, or off the end of the837- // reader's own text it was appended under.838- const kept =839- last.length === 0840- ? existing.prompt841- : existing.prompt === last842- ? ""843- : existing.prompt.endsWith(`\n\n${last}`)844- ? existing.prompt.slice(0, -(last.length + 2))845- : existing.prompt;835+ // Line comments may have been appended after the last general request. Remove only its836+ // unchanged paragraph, wherever it now sits, leaving the reader's edits and comments alone.837+ let kept = existing.prompt;838+ let start = last.length === 0 ? -1 : existing.prompt.lastIndexOf(last);839+ while (start >= 0) {840+ const before = existing.prompt.slice(0, start);841+ const after = existing.prompt.slice(start + last.length);842+ if ((start === 0 || before.endsWith("\n\n")) && /^[ \t]*(?:\n\n|$)/u.test(after)) {843+ kept = [before.trimEnd(), after.trimStart()].filter(Boolean).join("\n\n");
- apps/web/src/components/pullRequest/pullRequestDetail.logic.ts lines 838 to 844OutdatedResolvedby jakeleventhal(GitHub profile, opens in a new tab)@@ -835,11 +834,11 @@835834 const last = existing.lastHandoffPrompt ?? "";836- // Only the sentence the last hand-off wrote is taken back: alone, or off the end of the837- // reader's own text it was appended under.838- const kept =839- last.length === 0840- ? existing.prompt841- : existing.prompt === last842- ? ""843- : existing.prompt.endsWith(`\n\n${last}`)844- ? existing.prompt.slice(0, -(last.length + 2))845- : existing.prompt;835+ // Line comments may have been appended after the last general request. Remove only its836+ // unchanged paragraph, wherever it now sits, leaving the reader's edits and comments alone.837+ let kept = existing.prompt;838+ let start = last.length === 0 ? -1 : existing.prompt.lastIndexOf(last);839+ while (start >= 0) {840+ const before = existing.prompt.slice(0, start);841+ const after = existing.prompt.slice(start + last.length);842+ if ((start === 0 || before.endsWith("\n\n")) && /^[ \t]*(?:\n\n|$)/u.test(after)) {843+ kept = [before.trimEnd(), after.trimStart()].filter(Boolean).join("\n\n");844+ break;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/pullRequest/usePullRequestActions.ts lines 316 to 326OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -307,11 +313,14 @@307313 const existingComments = draft?.reviewComments ?? [];308314 const prompt = handoffPrompt(309315 {310- prompt: draft?.prompt ?? "",316+ prompt: stripPullRequestHandoffReferences(draft?.prompt ?? "", existingComments),311317 lastHandoffPrompt: lastHandoffPromptByDraft.get(session.draftId),312318 },313319 task.prompt,314320 );315321 // Remember the hand-off's own contribution, not the merged prompt: only that sentence is316322 // this session's to take back next time, and the reader's text around it is not.317- lastHandoffPromptByDraft.set(session.draftId, task.prompt);323+ lastHandoffPromptByDraft.set(session.draftId, {324+ text: task.prompt,325+ start: prompt.length - task.prompt.length,326+ });
- apps/web/src/components/pullRequest/pullRequestDetail.logic.ts line 854OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -832,14 +845,10 @@832845 incoming: string,833846 ): string {834847 if (existing.prompt.trim().length === 0) return incoming;835- const last = existing.lastHandoffPrompt ?? "";836- // Only the sentence the last hand-off wrote is taken back: alone, or off the end of the837- // reader's own text it was appended under.838- const kept =839- last.length === 0840- ? existing.prompt841- : existing.prompt === last842- ? ""843- : existing.prompt.endsWith(`\n\n${last}`)844- ? existing.prompt.slice(0, -(last.length + 2))845- : existing.prompt;848+ const last = existing.lastHandoffPrompt;849+ let kept = existing.prompt;850+ // Anchor to the preceding prose rather than a byte offset: caret citations can move the851+ // unchanged request. A later reader-authored copy has different preceding prose.852+ const expectedPrefix = last ? handoffPrefixProse(last.prefix) : "";853+ const previousText = last?.text ?? "";854+ let start = previousText.length > 0 ? existing.prompt.indexOf(previousText) : -1;
juliusmarminge assignedjuliusmarminge
- apps/web/src/components/pullRequest/pullRequestComposerHandoff.ts lines 46 to 46OutdatedResolvedby jakeleventhal(GitHub profile, opens in a new tab)@@ -0,0 +26,21 @@26+// Drafts outlive the panel. Only unchanged text at the recorded insertion belongs to us.27+const generalHandoffs = new Map<string, OwnedHandoff>();28+29+function unchangedRange(previous: OwnedHandoff, prompt: string) {30+ if (previous.start < 0) return null;31+ const before = previous.snapshot;32+ let prefix = 0;33+ while (prefix < before.length && prefix < prompt.length && before[prefix] === prompt[prefix]) {34+ prefix += 1;35+ }36+ let suffix = 0;37+ while (38+ suffix < before.length &&39+ suffix < prompt.length &&40+ before[before.length - suffix - 1] === prompt[prompt.length - suffix - 1]41+ ) {42+ suffix += 1;43+ }44+ // Identical insertions can make either copy look unchanged, even when other prose changed.45+ if (46+ stripInlineContextReferences(prompt).split(previous.text).length >apps/web/src/components/pullRequest/pullRequestComposerHandoff.ts lines 95 to 95OutdatedResolvedby jakeleventhal(GitHub profile, opens in a new tab)@@ -0,0 +75,21 @@75+function handoffPrompt(prompt: string, incoming: string): string {76+ if (prompt.trim().length === 0) return incoming;77+ return incoming.length === 0 ? prompt : `${prompt}\n\n${incoming}`;78+}79+80+function removeHandoffPrompt(prompt: string, start: number, end: number): string {81+ if (prompt[end] === " ") end += 1;82+ if (prompt.slice(end, end + 2) === "\n\n") end += 2;83+ else if (prompt.slice(start - 2, start) === "\n\n") start -= 2;84+ return prompt.slice(0, start) + prompt.slice(end);85+}86+87+useComposerDraftStore.subscribe((state, previousState) => {88+ if (state.draftsByThreadKey === previousState.draftsByThreadKey) return;89+ for (const [key, owned] of generalHandoffs) {90+ const draft = state.draftsByThreadKey[key];91+ if (!draft) {92+ generalHandoffs.delete(key);93+ continue;94+ }95+ if (draft.prompt === owned.snapshot) continue;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
tanstack-source added labelsbug
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
This looks like a good fix for a frustrating workflow bug — adding a second line comment shouldn't make the first citation disappear.
I'd make sure the regression test covers comments added from both Code and Guide, including a mix of those sources in the same draft. It'd also be worth checking that the existing replacement behavior for Ask/Explain handoffs is unchanged, since the fix is intentionally scoped to line-comment handoffs.
That should help verify both sides of the change: citations accumulate where they should, without changing the behavior of the other handoff types.