@in-the-loop-labs/pair-review 4.1.1 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +81 -13
  2. package/package.json +6 -2
  3. package/plugin/.claude-plugin/plugin.json +1 -1
  4. package/plugin-code-critic/.claude-plugin/plugin.json +1 -1
  5. package/plugin-pair-loop/.claude-plugin/plugin.json +1 -1
  6. package/plugin-pair-loop/skills/loop/SKILL.md +17 -8
  7. package/public/css/ai-summary-modal.css +1 -1
  8. package/public/css/analysis-config.css +33 -49
  9. package/public/css/council-card.css +118 -0
  10. package/public/css/council-dropdown.css +164 -0
  11. package/public/css/pr.css +668 -393
  12. package/public/css/repo-settings.css +15 -305
  13. package/public/css/settings.css +1141 -0
  14. package/public/css/snippet-manager.css +233 -0
  15. package/public/index.html +40 -59
  16. package/public/js/components/AIPanel.js +82 -16
  17. package/public/js/components/ChatPanel.js +401 -5
  18. package/public/js/components/CouncilCard.js +202 -0
  19. package/public/js/components/CouncilDropdown.js +292 -0
  20. package/public/js/components/DiffOptionsDropdown.js +177 -4
  21. package/public/js/components/SnippetManager.js +463 -0
  22. package/public/js/index.js +7 -30
  23. package/public/js/local.js +20 -2
  24. package/public/js/modules/comment-manager.js +8 -1
  25. package/public/js/modules/comment-minimizer.js +249 -190
  26. package/public/js/modules/diff-renderer.js +16 -4
  27. package/public/js/modules/external-comment-manager.js +414 -48
  28. package/public/js/modules/file-comment-manager.js +28 -21
  29. package/public/js/modules/hunk-summary-renderer.js +155 -29
  30. package/public/js/modules/pierre-bridge.js +2513 -0
  31. package/public/js/modules/pierre-context.js +345 -0
  32. package/public/js/modules/suggestion-manager.js +176 -20
  33. package/public/js/modules/tour-renderer.js +400 -184
  34. package/public/js/pr.js +1614 -158
  35. package/public/js/repo-settings.js +60 -284
  36. package/public/js/settings.js +1086 -0
  37. package/public/js/theme-bootstrap.js +40 -0
  38. package/public/js/theme.js +247 -0
  39. package/public/js/utils/provider-model.js +3 -3
  40. package/public/js/utils/suggestion-ui.js +157 -8
  41. package/public/local.html +20 -31
  42. package/public/pr.html +14 -23
  43. package/public/repo-settings.html +24 -14
  44. package/public/settings.html +138 -0
  45. package/public/setup.html +8 -13
  46. package/src/chat/api-reference.js +9 -5
  47. package/src/chat/prompt-builder.js +8 -1
  48. package/src/config.js +15 -6
  49. package/src/database.js +307 -14
  50. package/src/external/github-adapter.js +39 -5
  51. package/src/headless/delegate.js +682 -0
  52. package/src/local-review.js +10 -10
  53. package/src/local-scope.js +9 -0
  54. package/src/main.js +276 -157
  55. package/src/mcp-stdio.js +24 -1
  56. package/src/review-config.js +93 -25
  57. package/src/routes/chat.js +2 -2
  58. package/src/routes/config.js +12 -10
  59. package/src/routes/external-comments.js +5 -1
  60. package/src/routes/local.js +2 -4
  61. package/src/routes/mcp.js +18 -13
  62. package/src/routes/reviews.js +223 -4
  63. package/src/routes/settings.js +252 -0
  64. package/src/routes/setup.js +80 -1
  65. package/src/routes/shared.js +31 -29
  66. package/src/routes/snippets.js +154 -0
  67. package/src/routes/stack-analysis.js +17 -14
  68. package/src/server.js +86 -18
  69. package/src/settings/global-settings-service.js +472 -0
  70. package/src/settings/registry.js +624 -0
  71. package/src/utils/db-identity.js +44 -0
@@ -0,0 +1,345 @@
1
+ // Copyright 2026 Tim Perkins (tjwp) | SPDX-License-Identifier: Apache-2.0
2
+ /**
3
+ * PierreContext — merge context-only hunks into @pierre/diffs FileDiffMetadata.
4
+ *
5
+ * Used by PierreBridge to programmatically reveal arbitrary non-contiguous
6
+ * line ranges in diff files (e.g., for AI suggestions targeting lines deep
7
+ * inside collapsed gaps).
8
+ *
9
+ * All line coordinates are 1-indexed and refer to the NEW (addition) file side.
10
+ */
11
+
12
+ // ─── Range Utilities ──────────────────────────────────────────────────
13
+
14
+ /**
15
+ * Sort ranges by startLine and merge overlapping/adjacent ranges.
16
+ * Adjacent means range1.endLine + 1 >= range2.startLine.
17
+ * @param {Array<{startLine: number, endLine: number}>} ranges
18
+ * @returns {Array<{startLine: number, endLine: number}>}
19
+ */
20
+ function mergeOverlapping(ranges) {
21
+ if (!ranges || ranges.length === 0) return [];
22
+
23
+ const sorted = ranges
24
+ .filter(r => r && r.startLine <= r.endLine)
25
+ .sort((a, b) => a.startLine - b.startLine || a.endLine - b.endLine);
26
+
27
+ if (sorted.length === 0) return [];
28
+
29
+ const merged = [{ startLine: sorted[0].startLine, endLine: sorted[0].endLine }];
30
+
31
+ for (let i = 1; i < sorted.length; i++) {
32
+ const prev = merged[merged.length - 1];
33
+ const cur = sorted[i];
34
+
35
+ // Adjacent or overlapping: prev.endLine + 1 >= cur.startLine
36
+ if (prev.endLine + 1 >= cur.startLine) {
37
+ prev.endLine = Math.max(prev.endLine, cur.endLine);
38
+ } else {
39
+ merged.push({ startLine: cur.startLine, endLine: cur.endLine });
40
+ }
41
+ }
42
+
43
+ return merged;
44
+ }
45
+
46
+ /**
47
+ * Remove toRemove ranges from existing ranges.
48
+ * Both inputs are arrays of {startLine, endLine}.
49
+ * Returns the remaining ranges after subtraction.
50
+ * @param {Array<{startLine: number, endLine: number}>} existing
51
+ * @param {Array<{startLine: number, endLine: number}>} toRemove
52
+ * @returns {Array<{startLine: number, endLine: number}>}
53
+ */
54
+ function subtractRanges(existing, toRemove) {
55
+ if (!existing || existing.length === 0) return [];
56
+ if (!toRemove || toRemove.length === 0) {
57
+ return existing.map(r => ({ startLine: r.startLine, endLine: r.endLine }));
58
+ }
59
+
60
+ // Normalize both sides
61
+ const normalizedExisting = mergeOverlapping(existing);
62
+ const normalizedRemove = mergeOverlapping(toRemove);
63
+
64
+ const result = [];
65
+
66
+ for (const range of normalizedExisting) {
67
+ let remaining = [{ startLine: range.startLine, endLine: range.endLine }];
68
+
69
+ for (const cut of normalizedRemove) {
70
+ const next = [];
71
+ for (const r of remaining) {
72
+ // No overlap — cut is entirely before or after this range
73
+ if (cut.endLine < r.startLine || cut.startLine > r.endLine) {
74
+ next.push(r);
75
+ continue;
76
+ }
77
+ // Portion before the cut
78
+ if (cut.startLine > r.startLine) {
79
+ next.push({ startLine: r.startLine, endLine: cut.startLine - 1 });
80
+ }
81
+ // Portion after the cut
82
+ if (cut.endLine < r.endLine) {
83
+ next.push({ startLine: cut.endLine + 1, endLine: r.endLine });
84
+ }
85
+ // If cut completely covers r, nothing is pushed
86
+ }
87
+ remaining = next;
88
+ }
89
+
90
+ result.push(...remaining);
91
+ }
92
+
93
+ return result;
94
+ }
95
+
96
+ // ─── Core Algorithm ───────────────────────────────────────────────────
97
+
98
+ /**
99
+ * Merge context-only hunks into existing FileDiffMetadata.
100
+ *
101
+ * @param {Object} baseMetadata - Original FileDiffMetadata (with real diff hunks).
102
+ * Has properties: hunks[], additionLines[], deletionLines[], isPartial, name,
103
+ * type, cacheKey, splitLineCount, unifiedLineCount.
104
+ * @param {Array<{startLine: number, endLine: number}>} ranges
105
+ * Line ranges in NEW file coordinates (1-indexed).
106
+ * @returns {Object} New FileDiffMetadata with original + context hunks merged.
107
+ */
108
+ function mergeContextRanges(baseMetadata, ranges) {
109
+ if (!ranges || ranges.length === 0) return baseMetadata;
110
+ if (!baseMetadata || !baseMetadata.hunks) return baseMetadata;
111
+
112
+ const totalNewLines = baseMetadata.additionLines
113
+ ? baseMetadata.additionLines.length
114
+ : 0;
115
+
116
+ // ── Step 1: Normalize ranges ──────────────────────────────────────
117
+ const normalized = mergeOverlapping(ranges);
118
+ if (normalized.length === 0) return baseMetadata;
119
+
120
+ // Clamp to file length
121
+ const clamped = normalized
122
+ .map(r => ({
123
+ startLine: Math.max(1, r.startLine),
124
+ endLine: totalNewLines > 0 ? Math.min(r.endLine, totalNewLines) : r.endLine,
125
+ }))
126
+ .filter(r => r.startLine <= r.endLine);
127
+
128
+ if (clamped.length === 0) return baseMetadata;
129
+
130
+ const existingHunks = baseMetadata.hunks;
131
+
132
+ // ── Step 2: Compute old↔new offset map ────────────────────────────
133
+ // The cumulative offset at each hunk boundary. For a new-file line N
134
+ // in a gap between hunks: oldLine = N - cumulativeOffset.
135
+ // Offset changes by (hunk.additionLines - hunk.deletionLines) for each
136
+ // hunk — the count of actual +/- lines, not additionCount/deletionCount.
137
+ //
138
+ // offsets[i] = cumulative offset AFTER processing hunk i.
139
+ // Before the first hunk, offset is 0.
140
+ const offsets = [];
141
+ let cumOffset = 0;
142
+ for (let i = 0; i < existingHunks.length; i++) {
143
+ cumOffset += existingHunks[i].additionLines - existingHunks[i].deletionLines;
144
+ offsets.push(cumOffset);
145
+ }
146
+
147
+ /**
148
+ * Get the cumulative offset for a new-file line in a gap.
149
+ * gapIndex 0 = before first hunk, gapIndex i = after hunk i-1.
150
+ */
151
+ function offsetForGap(gapIndex) {
152
+ if (gapIndex === 0) return 0;
153
+ return offsets[gapIndex - 1];
154
+ }
155
+
156
+ // ── Step 3: Clip ranges against existing hunks ────────────────────
157
+ // Build list of existing hunk spans in new-file coordinates.
158
+ const existingSpans = existingHunks.map(h => ({
159
+ startLine: h.additionStart,
160
+ endLine: h.additionStart + h.additionCount - 1,
161
+ }));
162
+
163
+ // Subtract existing hunk spans from requested ranges
164
+ const clipped = subtractRanges(clamped, existingSpans);
165
+ if (clipped.length === 0) return baseMetadata;
166
+
167
+ // Split clipped ranges that span across multiple gaps.
168
+ // A range cannot cross an existing hunk — we must split it at hunk boundaries.
169
+ const gapRanges = [];
170
+ for (const range of clipped) {
171
+ let current = { startLine: range.startLine, endLine: range.endLine };
172
+
173
+ for (const span of existingSpans) {
174
+ // If current range doesn't reach this span, done splitting
175
+ if (current.startLine > current.endLine) break;
176
+ if (current.endLine < span.startLine) break;
177
+
178
+ // If range starts after this span, skip
179
+ if (current.startLine > span.endLine) continue;
180
+
181
+ // Range crosses this span — split into before-span portion
182
+ // (The span itself was already subtracted, so if startLine < span.startLine
183
+ // there's a portion before the span. After the span, continue with remainder.)
184
+ if (current.startLine < span.startLine) {
185
+ gapRanges.push({
186
+ startLine: current.startLine,
187
+ endLine: span.startLine - 1,
188
+ });
189
+ current = { startLine: span.endLine + 1, endLine: current.endLine };
190
+ } else {
191
+ // startLine is within or after span (shouldn't happen after subtraction)
192
+ current = { startLine: span.endLine + 1, endLine: current.endLine };
193
+ }
194
+ }
195
+
196
+ if (current.startLine <= current.endLine) {
197
+ gapRanges.push(current);
198
+ }
199
+ }
200
+
201
+ if (gapRanges.length === 0) return baseMetadata;
202
+
203
+ // ── Step 4: Build context-only hunks ──────────────────────────────
204
+
205
+ /**
206
+ * Determine which gap a new-file line falls in.
207
+ * Returns the gap index: 0 = before first hunk, i = between hunk i-1 and hunk i.
208
+ */
209
+ function findGapIndex(newLine) {
210
+ for (let i = 0; i < existingHunks.length; i++) {
211
+ if (newLine < existingHunks[i].additionStart) return i;
212
+ }
213
+ return existingHunks.length; // after last hunk
214
+ }
215
+
216
+ const contextHunks = [];
217
+ for (const range of gapRanges) {
218
+ const newStart = range.startLine;
219
+ const newEnd = range.endLine;
220
+ const rangeLen = newEnd - newStart + 1;
221
+ const gapIdx = findGapIndex(newStart);
222
+ const gapOffset = offsetForGap(gapIdx);
223
+ const oldStart = newStart - gapOffset;
224
+
225
+ contextHunks.push({
226
+ collapsedBefore: 0, // recomputed in step 6
227
+ deletionStart: oldStart,
228
+ deletionCount: rangeLen,
229
+ deletionLines: 0, // context-only: no actual deletions
230
+ deletionLineIndex: oldStart - 1,
231
+ additionStart: newStart,
232
+ additionCount: rangeLen,
233
+ additionLines: 0, // context-only: no actual additions
234
+ additionLineIndex: newStart - 1,
235
+ hunkContent: [{
236
+ type: 'context',
237
+ lines: rangeLen,
238
+ deletionLineIndex: oldStart - 1,
239
+ additionLineIndex: newStart - 1,
240
+ }],
241
+ hunkSpecs: `@@ -${oldStart},${rangeLen} +${newStart},${rangeLen} @@`,
242
+ hunkContext: '',
243
+ splitLineCount: rangeLen,
244
+ splitLineStart: 0, // recomputed in step 6
245
+ unifiedLineCount: rangeLen,
246
+ unifiedLineStart: 0, // recomputed in step 6
247
+ noEOFCRAdditions: false,
248
+ noEOFCRDeletions: false,
249
+ });
250
+ }
251
+
252
+ // ── Step 5: Merge and sort all hunks by additionStart ─────────────
253
+ // Shallow-clone each existing hunk so step 6 doesn't mutate baseMetadata
254
+ // (baseMetadata is stored and reused for re-merging when ranges change).
255
+ const allHunks = [
256
+ ...existingHunks.map(h => ({ ...h })),
257
+ ...contextHunks,
258
+ ].sort((a, b) => a.additionStart - b.additionStart);
259
+
260
+ // ── Step 6: Recompute derived fields ──────────────────────────────
261
+
262
+ // collapsedBefore: gap between previous hunk's end and this hunk's start
263
+ for (let i = 0; i < allHunks.length; i++) {
264
+ if (i === 0) {
265
+ allHunks[i].collapsedBefore = allHunks[i].additionStart - 1;
266
+ } else {
267
+ const prevEnd = allHunks[i - 1].additionStart + allHunks[i - 1].additionCount;
268
+ allHunks[i].collapsedBefore = allHunks[i].additionStart - prevEnd;
269
+ }
270
+ }
271
+
272
+ // Walk merged hunks accumulating splitLineStart / unifiedLineStart
273
+ let cumulativeSplit = 0;
274
+ let cumulativeUnified = 0;
275
+ for (const hunk of allHunks) {
276
+ hunk.splitLineStart = cumulativeSplit;
277
+ hunk.unifiedLineStart = cumulativeUnified;
278
+ cumulativeSplit += hunk.collapsedBefore + hunk.splitLineCount;
279
+ cumulativeUnified += hunk.collapsedBefore + hunk.unifiedLineCount;
280
+ }
281
+
282
+ // Trailing collapsed lines after the last hunk
283
+ let trailingCollapsed = 0;
284
+ if (allHunks.length > 0) {
285
+ const lastHunk = allHunks[allHunks.length - 1];
286
+ const lastHunkEnd = lastHunk.additionStart + lastHunk.additionCount - 1;
287
+ trailingCollapsed = Math.max(0, totalNewLines - lastHunkEnd);
288
+ }
289
+
290
+ const fileSplitLineCount = cumulativeSplit + trailingCollapsed;
291
+ const fileUnifiedLineCount = cumulativeUnified + trailingCollapsed;
292
+
293
+ // ── Step 7: Return new metadata object ────────────────────────────
294
+ // Shallow copy preserving all original fields, replacing hunks and totals.
295
+ return {
296
+ ...baseMetadata,
297
+ hunks: allHunks,
298
+ splitLineCount: fileSplitLineCount,
299
+ unifiedLineCount: fileUnifiedLineCount,
300
+ };
301
+ }
302
+
303
+ /**
304
+ * Convert OLD-file (deletion-side) line numbers to NEW-file coordinates.
305
+ * Uses the same cumulative offset that mergeContextRanges builds internally.
306
+ * @param {Object} baseMetadata - FileDiffMetadata with hunks
307
+ * @param {number} oldStart - OLD-file line (1-indexed)
308
+ * @param {number} oldEnd - OLD-file line (1-indexed)
309
+ * @returns {{startLine: number, endLine: number}}
310
+ */
311
+ function convertOldToNew(baseMetadata, oldStart, oldEnd) {
312
+ if (!baseMetadata?.hunks?.length) {
313
+ return { startLine: oldStart, endLine: oldEnd };
314
+ }
315
+ // Build same offset map as mergeContextRanges Step 2
316
+ // offset = cumulative (additionLines - deletionLines)
317
+ // For old-file line in a gap: newLine = oldLine + offset
318
+ const hunks = baseMetadata.hunks;
319
+ const offsets = [];
320
+ let cum = 0;
321
+ for (const h of hunks) {
322
+ cum += h.additionLines - h.deletionLines;
323
+ offsets.push(cum);
324
+ }
325
+ function offsetForOldLine(oldLine) {
326
+ // Find which gap the old line falls in (using deletion-side starts)
327
+ for (let i = 0; i < hunks.length; i++) {
328
+ if (oldLine < hunks[i].deletionStart) return i === 0 ? 0 : offsets[i - 1];
329
+ }
330
+ return offsets[offsets.length - 1];
331
+ }
332
+ return {
333
+ startLine: oldStart + offsetForOldLine(oldStart),
334
+ endLine: oldEnd + offsetForOldLine(oldEnd),
335
+ };
336
+ }
337
+
338
+ // ─── Exports ──────────────────────────────────────────────────────────
339
+
340
+ window.PierreContext = { mergeContextRanges, mergeOverlapping, subtractRanges, convertOldToNew };
341
+
342
+ // CommonJS export for tests
343
+ if (typeof module !== 'undefined') {
344
+ module.exports = { mergeContextRanges, mergeOverlapping, subtractRanges, convertOldToNew };
345
+ }
@@ -71,22 +71,34 @@ class SuggestionManager {
71
71
  * @param {HTMLElement} toggleBtn - The brain icon button that was clicked
72
72
  */
73
73
  _openReasoningPopover(toggleBtn) {
74
+ // Reasoning steps and an optional dismissal reason are stored separately on
75
+ // the button. Either may be present; the popover opens when at least one is.
76
+ let reasoning = null;
74
77
  const reasoningData = toggleBtn.dataset.reasoning;
75
- if (!reasoningData) return;
78
+ if (reasoningData) {
79
+ try {
80
+ reasoning = JSON.parse(decodeURIComponent(reasoningData));
81
+ } catch {
82
+ reasoning = null;
83
+ }
84
+ }
76
85
 
77
- let reasoning;
78
- try {
79
- reasoning = JSON.parse(decodeURIComponent(reasoningData));
80
- } catch {
81
- return;
86
+ let dismissalReason = '';
87
+ const dismissalData = toggleBtn.dataset.dismissalReason;
88
+ if (dismissalData) {
89
+ try {
90
+ dismissalReason = decodeURIComponent(dismissalData);
91
+ } catch {
92
+ dismissalReason = '';
93
+ }
82
94
  }
83
- if (!Array.isArray(reasoning)) return;
84
95
 
85
- const escapeHtml = this.prManager?.escapeHtml?.bind(this.prManager) || ((s) => s);
86
- const bulletMd = reasoning.map(step => `- ${step}`).join('\n');
87
- const rendered = window.renderMarkdown
88
- ? window.renderMarkdown(bulletMd)
89
- : `<ul>${reasoning.map(step => `<li>${escapeHtml(step)}</li>`).join('')}</ul>`;
96
+ const hasReasoning = Array.isArray(reasoning) && reasoning.length > 0;
97
+ if (!hasReasoning && !dismissalReason) return;
98
+
99
+ const rendered = window.SuggestionUI?.buildReasoningPopoverContentHtml
100
+ ? window.SuggestionUI.buildReasoningPopoverContentHtml(hasReasoning ? reasoning : null, dismissalReason)
101
+ : '';
90
102
 
91
103
  const popover = document.createElement('div');
92
104
  popover.className = 'reasoning-popover';
@@ -195,6 +207,14 @@ class SuggestionManager {
195
207
  // Get side from suggestion, default to 'RIGHT' for backwards compatibility
196
208
  const side = suggestion.side || 'RIGHT';
197
209
 
210
+ // For PierreBridge files, check if the line is actually visible in the rendered DOM
211
+ if (this.prManager.pierreBridge?.files.has(file)) {
212
+ if (!this.prManager.pierreBridge.isLineVisible(file, line, side)) {
213
+ hiddenItems.push({ file, line, lineEnd, side });
214
+ }
215
+ continue;
216
+ }
217
+
198
218
  // Find the file wrapper
199
219
  const fileElement = this.findFileElement(file);
200
220
 
@@ -235,6 +255,19 @@ class SuggestionManager {
235
255
  return hiddenItems;
236
256
  }
237
257
 
258
+ /**
259
+ * Normalize the `is_file_level` flag to a boolean.
260
+ * This field arrives in two shapes across the codebase: boolean `true` from
261
+ * the analyses merge path (src/routes/analyses.js) and integer `1` from the
262
+ * SQLite-backed paths (src/routes/pr.js, src/routes/chat.js). Matches the
263
+ * normalization used in AIPanel.js.
264
+ * @param {Object} suggestion
265
+ * @returns {boolean} true if the suggestion is file-level
266
+ */
267
+ _isFileLevel(suggestion) {
268
+ return suggestion?.is_file_level === 1 || suggestion?.is_file_level === true;
269
+ }
270
+
238
271
  /**
239
272
  * Display AI suggestions inline with diff
240
273
  * Uses a concurrency guard to prevent multiple simultaneous executions
@@ -274,7 +307,8 @@ class SuggestionManager {
274
307
  // without this findHiddenSuggestions() would flag every line as "hidden"
275
308
  // and we'd gap-expand against bodies that aren't even built yet.
276
309
  // ensureFileBodyRendered is a cheap no-op for files already rendered or
277
- // not in the diff.
310
+ // not in the diff. Pierre-rendered files are not in _lazyFileBodies, so
311
+ // this only affects legacy-rendered files.
278
312
  if (this.prManager?.ensureFileBodyRendered) {
279
313
  const targetFiles = new Set(inlineSuggestions.map(s => s.file));
280
314
  for (const file of targetFiles) {
@@ -282,6 +316,75 @@ class SuggestionManager {
282
316
  }
283
317
  }
284
318
 
319
+ // Clear stale Pierre annotations/ranges before expanding for the new set.
320
+ if (this.prManager.pierreBridge) {
321
+ for (const [fileName] of this.prManager.pierreBridge.files) {
322
+ this.prManager.pierreBridge.removeAnnotationsByType(fileName, 'suggestion');
323
+ this.prManager.pierreBridge.clearContextRanges(fileName);
324
+ }
325
+ }
326
+
327
+ const lineTargets = suggestions
328
+ .filter(suggestion =>
329
+ suggestion.file
330
+ && !this._isFileLevel(suggestion)
331
+ && (suggestion.line_start != null || suggestion.line_end != null)
332
+ )
333
+ .map(suggestion => ({
334
+ file: suggestion.file,
335
+ line_start: suggestion.line_start || suggestion.line_end,
336
+ line_end: suggestion.line_end || suggestion.line_start,
337
+ side: suggestion.side || 'RIGHT',
338
+ }));
339
+ if (lineTargets.length > 0 && this.prManager?.ensureLinesVisible) {
340
+ await this.prManager.ensureLinesVisible(lineTargets);
341
+ }
342
+
343
+ // For files rendered by @pierre/diffs, use annotation-based display.
344
+ // Deferred files may have been materialized by ensureLinesVisible() above,
345
+ // so group after that step.
346
+ if (this.prManager.pierreBridge) {
347
+ // Group suggestions by file
348
+ const suggestionsByFile = new Map();
349
+ for (const suggestion of suggestions) {
350
+ const file = suggestion.file;
351
+ if (!file) continue;
352
+ if (!suggestionsByFile.has(file)) {
353
+ suggestionsByFile.set(file, []);
354
+ }
355
+ suggestionsByFile.get(file).push(suggestion);
356
+ }
357
+
358
+ // Add suggestion annotations for pierre-bridge managed files
359
+ // File-level suggestions without line numbers are skipped here;
360
+ // they are routed to FileCommentManager via the fileLevelSuggestions path below
361
+ for (const [file, fileSuggestions] of suggestionsByFile) {
362
+ if (!this.prManager.pierreBridge.files.has(file)) continue;
363
+
364
+ // Collect this file's line-level annotations and apply them in one
365
+ // batch so the file rerenders ONCE, not once per suggestion.
366
+ const annotations = [];
367
+ for (const suggestion of fileSuggestions) {
368
+ // File-level suggestions go to file comment manager (handled below)
369
+ if (!suggestion.line_start && !suggestion.line_end) {
370
+ continue;
371
+ }
372
+
373
+ const side = suggestion.side || 'RIGHT';
374
+ const lineNumber = suggestion.line_end || suggestion.line_start;
375
+
376
+ annotations.push({
377
+ lineNumber: lineNumber,
378
+ side: side,
379
+ type: 'suggestion',
380
+ id: `suggestion-${suggestion.id}`,
381
+ data: suggestion,
382
+ });
383
+ }
384
+ this.prManager.pierreBridge.addAnnotations(file, annotations);
385
+ }
386
+ }
387
+
285
388
  // Auto-expand hidden lines for suggestions that target non-visible lines
286
389
  // Pass the side parameter so expandForSuggestion knows which coordinate system to use:
287
390
  // - RIGHT side = NEW coordinates (modified file, most common for AI suggestions)
@@ -289,7 +392,40 @@ class SuggestionManager {
289
392
  const hiddenSuggestions = this.findHiddenSuggestions(inlineSuggestions);
290
393
  if (hiddenSuggestions.length > 0) {
291
394
  console.log(`[UI] Found ${hiddenSuggestions.length} suggestions targeting hidden lines, expanding...`);
395
+
396
+ // Batch Pierre-bridge files: collect all ranges per file, one addContextRanges call each
397
+ const pierreRanges = new Map();
398
+ const legacyItems = [];
399
+
292
400
  for (const hidden of hiddenSuggestions) {
401
+ if (this.prManager?.pierreBridge?.files.has(hidden.file)) {
402
+ if (!pierreRanges.has(hidden.file)) pierreRanges.set(hidden.file, []);
403
+ const padding = 3;
404
+ let rangeStart = hidden.line;
405
+ let rangeEnd = hidden.lineEnd || hidden.line;
406
+ // addContextRanges expects NEW-file coordinates; LEFT-side items carry OLD numbers.
407
+ if (hidden.side === 'LEFT') {
408
+ const converted = this.prManager.pierreBridge.convertOldToNew(hidden.file, rangeStart, rangeEnd);
409
+ if (!converted) continue;
410
+ rangeStart = converted.startLine;
411
+ rangeEnd = converted.endLine;
412
+ }
413
+ pierreRanges.get(hidden.file).push({
414
+ startLine: Math.max(1, rangeStart - padding),
415
+ endLine: rangeEnd + padding,
416
+ });
417
+ } else {
418
+ legacyItems.push(hidden);
419
+ }
420
+ }
421
+
422
+ // One render per Pierre file instead of N
423
+ for (const [file, ranges] of pierreRanges) {
424
+ this.prManager.pierreBridge.addContextRanges(file, ranges);
425
+ }
426
+
427
+ // Legacy files: expand individually
428
+ for (const hidden of legacyItems) {
293
429
  if (this.prManager?.expandForSuggestion) {
294
430
  await this.prManager.expandForSuggestion(hidden.file, hidden.line, hidden.lineEnd, hidden.side);
295
431
  }
@@ -327,7 +463,7 @@ class SuggestionManager {
327
463
  const lineLevelSuggestions = [];
328
464
 
329
465
  suggestions.forEach(suggestion => {
330
- if (suggestion.is_file_level === 1 || suggestion.line_start === null) {
466
+ if (this._isFileLevel(suggestion) || suggestion.line_start === null) {
331
467
  fileLevelSuggestions.push(suggestion);
332
468
  } else {
333
469
  lineLevelSuggestions.push(suggestion);
@@ -370,6 +506,11 @@ class SuggestionManager {
370
506
  const file = parts.join(':'); // Remaining parts are file path (may contain colons)
371
507
  const line = parseInt(lineStr);
372
508
 
509
+ // Skip files managed by PierreBridge - they use annotation-based display
510
+ if (this.prManager.pierreBridge?.files.has(file)) {
511
+ return; // Already handled via PierreBridge annotations above
512
+ }
513
+
373
514
  // Use helper method for file lookup
374
515
  const fileElement = this.findFileElement(file);
375
516
 
@@ -506,14 +647,28 @@ class SuggestionManager {
506
647
  // Apply collapsed class if the suggestion is dismissed or was adopted
507
648
  // Check both: wasAdopted (from user comments with parent_id) OR status='adopted' (from DB)
508
649
  const isAdopted = wasAdopted || suggestion.status === 'adopted';
650
+ const isDismissed = !isAdopted && suggestion.status === 'dismissed';
509
651
  if (isAdopted) {
510
652
  suggestionDiv.classList.add('collapsed');
511
653
  // Mark the suggestion div as adopted after it's created
512
654
  suggestionDiv.dataset.hiddenForAdoption = 'true';
513
- } else if (suggestion.status === 'dismissed') {
655
+ } else if (isDismissed) {
514
656
  suggestionDiv.classList.add('collapsed');
515
657
  }
516
658
 
659
+ // Collapsed-bar state tooltip (replaces the old inline state text).
660
+ const collapsedStateLabel = isAdopted ? 'Adopted' : (isDismissed ? 'Dismissed' : '');
661
+
662
+ // The reasoning brain button surfaces both the reasoning steps and, when
663
+ // the suggestion was dismissed with a recorded reason, that reason. Show
664
+ // the button when either is present. data-reasoning holds the steps;
665
+ // data-dismissal-reason holds the (agent-controlled) reason, encoded so
666
+ // quotes/angle brackets cannot break out of the attribute.
667
+ const hasReasoning = Array.isArray(suggestion.reasoning) && suggestion.reasoning.length > 0;
668
+ const dismissalReasonAttr = suggestion.status_reason ? encodeURIComponent(suggestion.status_reason) : '';
669
+ const reasoningDataAttr = hasReasoning ? encodeURIComponent(JSON.stringify(suggestion.reasoning)) : '';
670
+ const showReasoningBtn = hasReasoning || !!suggestion.status_reason;
671
+
517
672
  // Get category label for display
518
673
  const categoryLabel = suggestion.type || suggestion.category || '';
519
674
 
@@ -528,22 +683,22 @@ class SuggestionManager {
528
683
  <span class="ai-title">${escapeHtml(suggestion.title || '')}</span>
529
684
  </div>
530
685
  <div class="ai-suggestion-header-right">
531
- ${suggestion.reasoning && suggestion.reasoning.length > 0 ? `
532
- <button class="btn-reasoning-toggle" title="View reasoning" data-suggestion-id="${suggestion.id}" data-reasoning="${encodeURIComponent(JSON.stringify(suggestion.reasoning))}">
686
+ ${showReasoningBtn ? `
687
+ <button class="btn-reasoning-toggle" title="View reasoning" data-suggestion-id="${suggestion.id}" data-reasoning="${reasoningDataAttr}" data-dismissal-reason="${dismissalReasonAttr}">
533
688
  <svg viewBox="0 0 24 24" width="14" height="14" fill="currentColor"><path d="M21.33 12.91c.09 1.55-.62 3.04-1.89 3.95l.77 1.49c.23.45.26.98.06 1.45c-.19.47-.58.84-1.06 1l-.79.25a1.69 1.69 0 0 1-1.86-.55L14.44 18c-.89-.15-1.73-.53-2.44-1.1c-.5.15-1 .23-1.5.23c-.88 0-1.76-.27-2.5-.79c-.53.16-1.07.23-1.62.22c-.79.01-1.57-.15-2.3-.45a4.1 4.1 0 0 1-2.43-3.61c-.08-.72.04-1.45.35-2.11c-.29-.75-.32-1.57-.07-2.33C2.3 7.11 3 6.32 3.87 5.82c.58-1.69 2.21-2.82 4-2.7c1.6-1.5 4.05-1.66 5.83-.37c.42-.11.86-.17 1.3-.17c1.36-.03 2.65.57 3.5 1.64c2.04.53 3.5 2.35 3.58 4.47c.05 1.11-.25 2.2-.86 3.13c.07.36.11.72.11 1.09m-5-1.41c.57.07 1.02.5 1.02 1.07a1 1 0 0 1-1 1h-.63c-.32.9-.88 1.69-1.62 2.29c.25.09.51.14.77.21c5.13-.07 4.53-3.2 4.53-3.25a2.59 2.59 0 0 0-2.69-2.49a1 1 0 0 1-1-1a1 1 0 0 1 1-1c1.23.03 2.41.49 3.33 1.3c.05-.29.08-.59.08-.89c-.06-1.24-.62-2.32-2.87-2.53c-1.25-2.96-4.4-1.32-4.4-.4c-.03.23.21.72.25.75a1 1 0 0 1 1 1c0 .55-.45 1-1 1c-.53-.02-1.03-.22-1.43-.56c-.48.31-1.03.5-1.6.56c-.57.05-1.04-.35-1.07-.9a.97.97 0 0 1 .88-1.1c.16-.02.94-.14.94-.77c0-.66.25-1.29.68-1.79c-.92-.25-1.91.08-2.91 1.29C6.75 5 6 5.25 5.45 7.2C4.5 7.67 4 8 3.78 9c1.08-.22 2.19-.13 3.22.25c.5.19.78.75.59 1.29c-.19.52-.77.78-1.29.59c-.73-.32-1.55-.34-2.3-.06c-.32.27-.32.83-.32 1.27c0 .74.37 1.43 1 1.83c.53.27 1.12.41 1.71.4q-.225-.39-.39-.81a1.038 1.038 0 0 1 1.96-.68c.4 1.14 1.42 1.92 2.62 2.05c1.37-.07 2.59-.88 3.19-2.13c.23-1.38 1.34-1.5 2.56-1.5m2 7.47l-.62-1.3l-.71.16l1 1.25zm-4.65-8.61a1 1 0 0 0-.91-1.03c-.71-.04-1.4.2-1.93.67c-.57.58-.87 1.38-.84 2.19a1 1 0 0 0 1 1c.57 0 1-.45 1-1c0-.27.07-.54.23-.76c.12-.1.27-.15.43-.15c.55.03 1.02-.38 1.02-.92"/></svg>
534
689
  </button>
535
690
  ` : ''}
536
691
  </div>
537
692
  </div>
538
- <div class="ai-suggestion-collapsed-content">
693
+ <div class="ai-suggestion-collapsed-content"${collapsedStateLabel ? ` title="${collapsedStateLabel}"` : ''}>
539
694
  ${suggestion.type === 'praise'
540
695
  ? `<span class="praise-badge" title="Nice Work"><svg viewBox="0 0 16 16"><path d="M8 .25a.75.75 0 01.673.418l1.882 3.815 4.21.612a.75.75 0 01.416 1.279l-3.046 2.97.719 4.192a.75.75 0 01-1.088.791L8 12.347l-3.766 1.98a.75.75 0 01-1.088-.79l.72-4.194L.818 6.374a.75.75 0 01.416-1.28l4.21-.611L7.327.668A.75.75 0 018 .25z"/></svg>Nice Work</span>`
541
696
  : `<span class="ai-suggestion-badge collapsed" data-type="${suggestion.type}" title="${this.getTypeDescription(suggestion.type)}"><svg viewBox="0 0 16 16" fill="currentColor" width="10" height="10"><path d="M9.6 2.279a.426.426 0 0 1 .8 0l.407 1.112a6.386 6.386 0 0 0 3.802 3.802l1.112.407a.426.426 0 0 1 0 .8l-1.112.407a6.386 6.386 0 0 0-3.802 3.802l-.407 1.112a.426.426 0 0 1-.8 0l-.407-1.112a6.386 6.386 0 0 0-3.802-3.802L4.279 8.4a.426.426 0 0 1 0-.8l1.112-.407a6.386 6.386 0 0 0 3.802-3.802L9.6 2.279Zm-4.267 8.837a.178.178 0 0 1 .334 0l.169.464a2.662 2.662 0 0 0 1.584 1.584l.464.169a.178.178 0 0 1 0 .334l-.464.169a2.662 2.662 0 0 0-1.584 1.584l-.169.464a.178.178 0 0 1-.334 0l-.169-.464a2.662 2.662 0 0 0-1.584-1.584l-.464-.169a.178.178 0 0 1 0-.334l.464-.169a2.662 2.662 0 0 0 1.584-1.584l.169-.464ZM2.8.14a.213.213 0 0 1 .4 0l.203.556a3.2 3.2 0 0 0 1.901 1.901l.556.203a.213.213 0 0 1 0 .4l-.556.203a3.2 3.2 0 0 0-1.901 1.901L3.2 5.86a.213.213 0 0 1-.4 0l-.203-.556A3.2 3.2 0 0 0 .696 3.403L.14 3.2a.213.213 0 0 1 0-.4l.556-.203A3.2 3.2 0 0 0 2.597.696L2.8.14Z"/></svg>AI Suggestion</span>`}
542
697
  ${suggestion.severity ? `<span class="severity-badge severity-${suggestion.severity}">${escapeHtml(suggestion.severity.toUpperCase())}</span>` : ''}
543
698
  <span class="collapsed-title">${escapeHtml(suggestion.title || '')}</span>
544
699
  <div class="ai-suggestion-header-right">
545
- ${suggestion.reasoning && suggestion.reasoning.length > 0 ? `
546
- <button class="btn-reasoning-toggle collapsed-reasoning" title="View reasoning" data-suggestion-id="${suggestion.id}" data-reasoning="${encodeURIComponent(JSON.stringify(suggestion.reasoning))}">
700
+ ${showReasoningBtn ? `
701
+ <button class="btn-reasoning-toggle collapsed-reasoning" title="View reasoning" data-suggestion-id="${suggestion.id}" data-reasoning="${reasoningDataAttr}" data-dismissal-reason="${dismissalReasonAttr}">
547
702
  <svg viewBox="0 0 24 24" width="12" height="12" fill="currentColor"><path d="M21.33 12.91c.09 1.55-.62 3.04-1.89 3.95l.77 1.49c.23.45.26.98.06 1.45c-.19.47-.58.84-1.06 1l-.79.25a1.69 1.69 0 0 1-1.86-.55L14.44 18c-.89-.15-1.73-.53-2.44-1.1c-.5.15-1 .23-1.5.23c-.88 0-1.76-.27-2.5-.79c-.53.16-1.07.23-1.62.22c-.79.01-1.57-.15-2.3-.45a4.1 4.1 0 0 1-2.43-3.61c-.08-.72.04-1.45.35-2.11c-.29-.75-.32-1.57-.07-2.33C2.3 7.11 3 6.32 3.87 5.82c.58-1.69 2.21-2.82 4-2.7c1.6-1.5 4.05-1.66 5.83-.37c.42-.11.86-.17 1.3-.17c1.36-.03 2.65.57 3.5 1.64c2.04.53 3.5 2.35 3.58 4.47c.05 1.11-.25 2.2-.86 3.13c.07.36.11.72.11 1.09m-5-1.41c.57.07 1.02.5 1.02 1.07a1 1 0 0 1-1 1h-.63c-.32.9-.88 1.69-1.62 2.29c.25.09.51.14.77.21c5.13-.07 4.53-3.2 4.53-3.25a2.59 2.59 0 0 0-2.69-2.49a1 1 0 0 1-1-1a1 1 0 0 1 1-1c1.23.03 2.41.49 3.33 1.3c.05-.29.08-.59.08-.89c-.06-1.24-.62-2.32-2.87-2.53c-1.25-2.96-4.4-1.32-4.4-.4c-.03.23.21.72.25.75a1 1 0 0 1 1 1c0 .55-.45 1-1 1c-.53-.02-1.03-.22-1.43-.56c-.48.31-1.03.5-1.6.56c-.57.05-1.04-.35-1.07-.9a.97.97 0 0 1 .88-1.1c.16-.02.94-.14.94-.77c0-.66.25-1.29.68-1.79c-.92-.25-1.91.08-2.91 1.29C6.75 5 6 5.25 5.45 7.2C4.5 7.67 4 8 3.78 9c1.08-.22 2.19-.13 3.22.25c.5.19.78.75.59 1.29c-.19.52-.77.78-1.29.59c-.73-.32-1.55-.34-2.3-.06c-.32.27-.32.83-.32 1.27c0 .74.37 1.43 1 1.83c.53.27 1.12.41 1.71.4q-.225-.39-.39-.81a1.038 1.038 0 0 1 1.96-.68c.4 1.14 1.42 1.92 2.62 2.05c1.37-.07 2.59-.88 3.19-2.13c.23-1.38 1.34-1.5 2.56-1.5m2 7.47l-.62-1.3-.71.16l1 1.25zm-4.65-8.61a1 1 0 0 0-.91-1.03c-.71-.04-1.4.2-1.93.67c-.57.58-.87 1.38-.84 2.19a1 1 0 0 0 1 1c.57 0 1-.45 1-1c0-.27.07-.54.23-.76c.12-.1.27-.15.43-.15c.55.03 1.02-.38 1.02-.92"/></svg>
548
703
  </button>
549
704
  ` : ''}
@@ -567,6 +722,7 @@ class SuggestionManager {
567
722
  return window.renderMarkdown ? window.renderMarkdown(displayBody) : escapeHtml(displayBody);
568
723
  })()}
569
724
  </div>
725
+ ${window.SuggestionUI?.buildDismissalNoteHtml?.(suggestion.status_reason) || ''}
570
726
  <div class="ai-suggestion-actions">
571
727
  <button class="ai-action ai-action-adopt" onclick="prManager.adoptSuggestion(${suggestion.id})">
572
728
  <svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path></svg>