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

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 (70) 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 +666 -394
  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/external-comment-manager.js +414 -48
  27. package/public/js/modules/file-comment-manager.js +28 -21
  28. package/public/js/modules/hunk-summary-renderer.js +155 -29
  29. package/public/js/modules/pierre-bridge.js +2513 -0
  30. package/public/js/modules/pierre-context.js +345 -0
  31. package/public/js/modules/suggestion-manager.js +176 -20
  32. package/public/js/modules/tour-renderer.js +400 -184
  33. package/public/js/pr.js +1434 -150
  34. package/public/js/repo-settings.js +60 -284
  35. package/public/js/settings.js +1086 -0
  36. package/public/js/theme-bootstrap.js +40 -0
  37. package/public/js/theme.js +247 -0
  38. package/public/js/utils/provider-model.js +3 -3
  39. package/public/js/utils/suggestion-ui.js +157 -8
  40. package/public/local.html +20 -31
  41. package/public/pr.html +14 -23
  42. package/public/repo-settings.html +24 -14
  43. package/public/settings.html +138 -0
  44. package/public/setup.html +8 -13
  45. package/src/chat/api-reference.js +9 -5
  46. package/src/chat/prompt-builder.js +8 -1
  47. package/src/config.js +15 -6
  48. package/src/database.js +307 -14
  49. package/src/external/github-adapter.js +39 -5
  50. package/src/headless/delegate.js +682 -0
  51. package/src/local-review.js +10 -10
  52. package/src/local-scope.js +9 -0
  53. package/src/main.js +276 -157
  54. package/src/mcp-stdio.js +24 -1
  55. package/src/review-config.js +93 -25
  56. package/src/routes/chat.js +2 -2
  57. package/src/routes/config.js +12 -10
  58. package/src/routes/external-comments.js +5 -1
  59. package/src/routes/local.js +2 -4
  60. package/src/routes/mcp.js +18 -13
  61. package/src/routes/reviews.js +223 -4
  62. package/src/routes/settings.js +252 -0
  63. package/src/routes/setup.js +80 -1
  64. package/src/routes/shared.js +31 -29
  65. package/src/routes/snippets.js +154 -0
  66. package/src/routes/stack-analysis.js +17 -14
  67. package/src/server.js +86 -18
  68. package/src/settings/global-settings-service.js +472 -0
  69. package/src/settings/registry.js +624 -0
  70. package/src/utils/db-identity.js +44 -0
@@ -2,16 +2,29 @@
2
2
  /**
3
3
  * CommentMinimizer - Manages "minimize comments" mode for the diff view.
4
4
  *
5
- * When active, all inline comment rows (.user-comment-row, .ai-suggestion-row,
6
- * and .external-comment-row) are hidden via CSS class. Small indicator
7
- * buttons are injected on the right edge of each diff line that has
8
- * comments, showing a person icon (user comments), sparkles icon (AI
9
- * suggestions), or chat-bubble icon (external review comments).
5
+ * When active, all inline comment cards (.user-comment-row), AI suggestion
6
+ * cards (.ai-suggestion) and external review threads (.external-comment-row)
7
+ * anchored to diff lines are hidden via CSS. A single small clickable
8
+ * indicator is injected for each diff line that has hidden cards, showing a
9
+ * person icon (user comments), AI-comment icon (adopted suggestions),
10
+ * sparkles icon (AI suggestions) or chat-bubble icon (external comments),
11
+ * with a count badge when a line holds more than one card.
10
12
  *
11
13
  * File-level comments (.file-comment-card inside .file-comments-zone) are also
12
14
  * hidden, with an indicator button injected into the file header bar.
13
15
  *
14
- * Clicking an indicator toggles visibility of that line's or file's comments.
16
+ * Clicking an indicator toggles visibility of that line's or file's cards.
17
+ *
18
+ * ── @pierre/diffs rendering ──────────────────────────────────────────────
19
+ * Diff lines are shadow-DOM elements owned by the vendor renderer; annotation
20
+ * cards live in the LIGHT DOM, each wrapped by the vendor in a
21
+ * `<div data-annotation-slot slot="annotation-{side}-{lineNumber}">` that is a
22
+ * child of the file's `<diffs-container>` host and projected into the matching
23
+ * shadow annotation cell. All cards anchored to the same line+side share the
24
+ * same `slot` value, so we group by `{fileName}\0{slot}` — a STABLE string key
25
+ * that survives the vendor's card-recreating rerenders (element identity does
26
+ * NOT survive them). The line-level indicator is injected into the first
27
+ * wrapper of each group (light DOM, so page CSS reaches it).
15
28
  */
16
29
 
17
30
  class CommentMinimizer {
@@ -29,10 +42,18 @@ class CommentMinimizer {
29
42
 
30
43
  constructor() {
31
44
  this._active = false;
32
- // Track which diff lines have been expanded by the user (Set of diff row elements)
45
+ // Diff lines the user has expanded, keyed by the STABLE string
46
+ // `${fileName}\0${slot}` (see class doc). NOT element references — the
47
+ // vendor recreates annotation wrappers on every rerender.
33
48
  this._expandedLines = new Set();
34
- // Track which file-comments-zones have been expanded (Set of zone elements)
49
+ // Files whose file-level zone has been expanded (Set of zone elements —
50
+ // zones are pr.js light DOM, not vendor-managed, so they persist).
35
51
  this._expandedFiles = new Set();
52
+ // MutationObserver that re-injects indicators after vendor rerenders
53
+ // (diffStyle switch, hunk expansion, worker-highlight streaming, lazy file
54
+ // render) that are NOT followed by an explicit refreshIndicators() call.
55
+ this._observer = null;
56
+ this._refreshScheduled = false;
36
57
  }
37
58
 
38
59
  /** @returns {boolean} Whether minimize mode is active */
@@ -55,7 +76,9 @@ class CommentMinimizer {
55
76
  if (minimized) {
56
77
  diffContainer.classList.add('comments-minimized');
57
78
  this.refreshIndicators();
79
+ this._startObserving();
58
80
  } else {
81
+ this._stopObserving();
59
82
  diffContainer.classList.remove('comments-minimized');
60
83
  this._removeAllIndicators();
61
84
  // Remove any per-line expansion overrides
@@ -66,118 +89,163 @@ class CommentMinimizer {
66
89
  }
67
90
 
68
91
  /**
69
- * Rebuild all indicator buttons on diff lines.
92
+ * Rebuild all indicator buttons on diff lines and file headers.
70
93
  * Call this after comments or suggestions are added/removed/re-rendered.
94
+ * Idempotent: starts by removing every existing indicator.
71
95
  */
72
96
  refreshIndicators() {
73
97
  if (!this._active) return;
74
98
 
75
- this._removeAllIndicators();
99
+ // The vendor recreates annotation wrappers as we inject indicators; pause
100
+ // the observer so our own DOM writes don't re-trigger a refresh loop.
101
+ this._stopObserving();
102
+ try {
103
+ this._removeAllIndicators();
76
104
 
77
- // Find all comment, suggestion, and external-comment rows currently in the DOM
78
- const commentRows = document.querySelectorAll('.user-comment-row');
79
- const suggestionRows = document.querySelectorAll('.ai-suggestion-row');
80
- const externalRows = document.querySelectorAll('.external-comment-row');
105
+ // Line-level: group the vendor annotation wrappers by file + slot.
106
+ for (const group of this._buildLineGroups().values()) {
107
+ this._injectLineIndicator(group);
108
+ }
81
109
 
82
- // Build a map: diff row element → indicator info entry
83
- const lineMap = new Map();
110
+ // File-level indicators (pr.js light DOM — unchanged path).
111
+ this._refreshFileIndicators();
112
+ } finally {
113
+ if (this._active) this._startObserving();
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Build a map of line-level groups keyed by `${fileName}\0${slot}`.
119
+ * Each group aggregates the hidden cards anchored to one line+side and keeps
120
+ * references to their vendor wrappers (`[data-annotation-slot]`).
121
+ * @returns {Map<string, {key: string, wrappers: HTMLElement[], info: Object}>}
122
+ * @private
123
+ */
124
+ _buildLineGroups() {
125
+ const groups = new Map();
84
126
  const newEntry = () => ({
85
127
  hasUser: false, hasAI: false, hasAdopted: false, hasExternal: false,
86
128
  userCount: 0, aiCount: 0, adoptedCount: 0, externalCount: 0
87
129
  });
88
130
 
89
- for (const row of commentRows) {
90
- const diffRow = this._findDiffRowFor(row);
91
- if (!diffRow) continue;
92
- const entry = lineMap.get(diffRow) || newEntry();
93
- if (row.querySelector('.adopted-comment')) {
94
- entry.hasAdopted = true;
95
- entry.adoptedCount++;
96
- } else {
97
- entry.hasUser = true;
98
- entry.userCount++;
99
- }
100
- lineMap.set(diffRow, entry);
101
- }
131
+ const wrappers = document.querySelectorAll('#diff-container [data-annotation-slot]');
132
+ for (const wrapper of wrappers) {
133
+ const card = this._cardOf(wrapper);
134
+ if (!card) continue;
102
135
 
103
- for (const row of suggestionRows) {
104
- const diffRow = this._findDiffRowFor(row);
105
- if (!diffRow) continue;
106
- const entry = lineMap.get(diffRow) || newEntry();
107
- // Count non-adopted suggestion divs only — adopted ones are already
108
- // represented by the adopted comment row (avoid double-counting)
109
- const allSuggestions = row.querySelectorAll('.ai-suggestion');
110
- let activeCount = 0;
111
- for (const s of allSuggestions) {
112
- if (!s.dataset?.hiddenForAdoption) {
113
- activeCount++;
114
- }
136
+ const isComment = card.classList.contains('user-comment-row');
137
+ const isSuggestion = card.classList.contains('ai-suggestion');
138
+ const isExternal = card.classList.contains('external-comment-row');
139
+ // Skip comment forms, tour stops, hunk summaries — those stay visible.
140
+ if (!isComment && !isSuggestion && !isExternal) continue;
141
+
142
+ const key = this._lineKeyFor(wrapper);
143
+ if (!key) continue;
144
+
145
+ let group = groups.get(key);
146
+ if (!group) {
147
+ group = { key, wrappers: [], info: newEntry() };
148
+ groups.set(key, group);
115
149
  }
116
- if (activeCount > 0) {
117
- entry.hasAI = true;
118
- entry.aiCount += activeCount;
150
+ group.wrappers.push(wrapper);
151
+ const info = group.info;
152
+
153
+ if (isComment) {
154
+ // Adopted AI suggestions render with an inner `.adopted-comment` card.
155
+ if (card.querySelector('.adopted-comment')) {
156
+ info.hasAdopted = true;
157
+ info.adoptedCount++;
158
+ } else {
159
+ info.hasUser = true;
160
+ info.userCount++;
161
+ }
162
+ } else if (isSuggestion) {
163
+ // A suggestion hidden for adoption is already represented by its
164
+ // adopted comment card — don't double-count it. The flag is a string:
165
+ // 'true' when adopted, and suggestion-ui.js writes the literal 'false'
166
+ // on runtime restore, so compare against 'true' (not truthiness) or a
167
+ // restored suggestion would be wrongly dropped from the count.
168
+ if (card.dataset?.hiddenForAdoption !== 'true') {
169
+ info.hasAI = true;
170
+ info.aiCount++;
171
+ }
172
+ } else {
173
+ // External thread: count root + replies so the badge matches the
174
+ // total the thread card itself shows.
175
+ const bubbles = card.querySelectorAll('.external-comment');
176
+ info.hasExternal = true;
177
+ info.externalCount += (bubbles.length || 1);
119
178
  }
120
- lineMap.set(diffRow, entry);
121
179
  }
122
180
 
123
- for (const row of externalRows) {
124
- const diffRow = this._findDiffRowFor(row);
125
- if (!diffRow) continue;
126
- const entry = lineMap.get(diffRow) || newEntry();
127
- // Count root + replies — each .external-comment is one bubble in the
128
- // thread card and the reviewer should see the same total here that
129
- // the thread itself shows.
130
- const bubbles = row.querySelectorAll('.external-comment');
131
- const bubbleCount = bubbles.length || 1;
132
- entry.hasExternal = true;
133
- entry.externalCount += bubbleCount;
134
- lineMap.set(diffRow, entry);
135
- }
181
+ return groups;
182
+ }
136
183
 
137
- // Inject line-level indicators
138
- for (const [diffRow, info] of lineMap) {
139
- this._injectIndicator(diffRow, info);
140
- }
184
+ /**
185
+ * The annotation card element inside a vendor `[data-annotation-slot]`
186
+ * wrapper (the element returned by PierreBridge's renderAnnotation).
187
+ * @param {HTMLElement} wrapper
188
+ * @returns {HTMLElement|null}
189
+ * @private
190
+ */
191
+ _cardOf(wrapper) {
192
+ return wrapper.firstElementChild || null;
193
+ }
141
194
 
142
- // Scan file-comments-zones and inject file-header indicators
143
- this._refreshFileIndicators();
195
+ /**
196
+ * Stable grouping key for a vendor annotation wrapper: the owning file plus
197
+ * the vendor slot name (`annotation-{side}-{lineNumber}`). Wrappers on the
198
+ * same line+side share a slot, so they share a key.
199
+ * @param {HTMLElement} wrapper
200
+ * @returns {string|null}
201
+ * @private
202
+ */
203
+ _lineKeyFor(wrapper) {
204
+ const slot = wrapper.getAttribute('slot');
205
+ if (!slot) return null;
206
+ const fileWrapper = wrapper.closest('.d2h-file-wrapper');
207
+ const file = fileWrapper?.dataset?.fileName || '';
208
+ return `${file}\0${slot}`;
144
209
  }
145
210
 
146
211
  /**
147
- * Walk backward from a comment/suggestion/external row to find its parent
148
- * diff line. Skips other comment/suggestion/external rows and
149
- * context-expand rows.
150
- * @param {HTMLElement} row
151
- * @returns {HTMLElement|null}
212
+ * All vendor annotation wrappers that share a group key (a line+side).
213
+ * @param {string} key
214
+ * @returns {HTMLElement[]}
215
+ * @private
152
216
  */
153
- _findDiffRowFor(row) {
154
- let prev = row.previousElementSibling;
155
- while (prev) {
156
- if (
157
- !prev.classList.contains('user-comment-row') &&
158
- !prev.classList.contains('ai-suggestion-row') &&
159
- !prev.classList.contains('external-comment-row') &&
160
- !prev.classList.contains('comment-form-row') &&
161
- !prev.classList.contains('context-expand-row')
162
- ) {
163
- return prev;
164
- }
165
- prev = prev.previousElementSibling;
217
+ _wrappersForKey(key) {
218
+ const out = [];
219
+ for (const wrapper of document.querySelectorAll('#diff-container [data-annotation-slot]')) {
220
+ if (this._lineKeyFor(wrapper) === key) out.push(wrapper);
166
221
  }
167
- return null;
222
+ return out;
168
223
  }
169
224
 
170
225
  /**
171
- * Inject an indicator button into a diff line's code cell.
172
- * @param {HTMLElement} diffRow - The diff table row
173
- * @param {Object} info - { hasUser, hasAI, hasAdopted, userCount, aiCount, adoptedCount }
226
+ * Inject a single indicator button for a line group into the first wrapper,
227
+ * and sync the group's expanded/collapsed card visibility.
228
+ * @param {{key: string, wrappers: HTMLElement[], info: Object}} group
229
+ * @private
174
230
  */
175
- _injectIndicator(diffRow, info) {
176
- const codeCell = diffRow.querySelector('.d2h-code-line-ctn');
177
- if (!codeCell) return;
231
+ _injectLineIndicator(group) {
232
+ const first = group.wrappers[0];
233
+ if (!first) return;
234
+
235
+ // Nothing to represent — e.g. the line's only card is a suggestion hidden
236
+ // for adoption (represented by its adopted comment elsewhere). Injecting
237
+ // here would produce an empty button with no icon, count, or title.
238
+ const info = group.info;
239
+ const total = info.userCount + info.adoptedCount + info.aiCount + info.externalCount;
240
+ if (total === 0) return;
178
241
 
179
- // Don't double-inject
180
- if (codeCell.querySelector('.comment-indicator')) return;
242
+ // Sync card visibility to the persisted expanded state (survives rerenders
243
+ // because the state is keyed by the stable group key).
244
+ const expanded = this._expandedLines.has(group.key);
245
+ this._applyExpanded(group.wrappers, expanded);
246
+
247
+ // Don't double-inject.
248
+ if (first.querySelector(':scope > .comment-indicator')) return;
181
249
 
182
250
  const btn = document.createElement('button');
183
251
  btn.className = 'comment-indicator';
@@ -202,7 +270,6 @@ class CommentMinimizer {
202
270
  icons.push(`<span class="indicator-icon indicator-external" title="${info.externalCount} external comment${info.externalCount !== 1 ? 's' : ''}">${CommentMinimizer.EXTERNAL_ICON}</span>`);
203
271
  }
204
272
 
205
- const total = info.userCount + info.adoptedCount + info.aiCount + info.externalCount;
206
273
  const countBadge = total > 1 ? `<span class="indicator-count">${total}</span>` : '';
207
274
 
208
275
  btn.innerHTML = icons.join('') + countBadge;
@@ -214,118 +281,81 @@ class CommentMinimizer {
214
281
  if (info.externalCount) totalLabel.push(`${info.externalCount} external comment${info.externalCount !== 1 ? 's' : ''}`);
215
282
  btn.title = totalLabel.join(', ');
216
283
 
217
- // Check if this line was previously expanded. Re-applying
218
- // `.comment-expanded` to the (possibly rebuilt) comment rows is
219
- // important: when an external-comment refresh tears down and rebuilds
220
- // `.external-comment-row` elements, the new rows have no expanded
221
- // class even though the indicator button is still marked expanded.
222
- // Without this step the indicator says "open" but the rows stay
223
- // hidden via the `.comments-minimized` display: none rule.
224
- if (this._expandedLines.has(diffRow)) {
284
+ if (expanded) {
225
285
  btn.classList.add('expanded');
226
- this._getCommentRowsFor(diffRow).forEach(row => row.classList.add('comment-expanded'));
227
286
  }
228
287
 
229
- // Click handler: toggle this line's comments
288
+ // Click handler toggles this line's cards. Closes over the current
289
+ // wrappers; a rerender replaces both wrappers and handler together.
230
290
  btn.addEventListener('click', (e) => {
231
291
  e.stopPropagation();
232
292
  e.preventDefault();
233
- this._toggleLineComments(diffRow, btn);
293
+ this._toggleLineComments(group.key, btn);
234
294
  });
235
295
 
236
- // Make the code cell position:relative for absolute positioning of the indicator
237
- codeCell.style.position = 'relative';
238
- codeCell.appendChild(btn);
296
+ first.appendChild(btn);
239
297
  }
240
298
 
241
299
  /**
242
- * Toggle visibility of comment/suggestion rows for a specific diff line.
243
- * @param {HTMLElement} diffRow
244
- * @param {HTMLElement} btn - The indicator button
300
+ * Show or hide the cards of a line group by toggling `.comment-expanded` on
301
+ * each wrapper's card element.
302
+ * @param {HTMLElement[]} wrappers
303
+ * @param {boolean} expanded
304
+ * @private
245
305
  */
246
- _toggleLineComments(diffRow, btn) {
247
- const isExpanded = this._expandedLines.has(diffRow);
248
-
249
- if (isExpanded) {
250
- // Collapse: remove .comment-expanded from this line's rows
251
- this._expandedLines.delete(diffRow);
252
- btn.classList.remove('expanded');
253
- this._getCommentRowsFor(diffRow).forEach(row => row.classList.remove('comment-expanded'));
254
- } else {
255
- // Expand: add .comment-expanded to this line's rows
256
- this._expandedLines.add(diffRow);
257
- btn.classList.add('expanded');
258
- this._getCommentRowsFor(diffRow).forEach(row => row.classList.add('comment-expanded'));
306
+ _applyExpanded(wrappers, expanded) {
307
+ for (const wrapper of wrappers) {
308
+ const card = this._cardOf(wrapper);
309
+ if (card) card.classList.toggle('comment-expanded', expanded);
259
310
  }
260
311
  }
261
312
 
262
313
  /**
263
- * Get all comment/suggestion rows that belong to a diff line.
264
- * Walks forward from the diff row, collecting adjacent comment/suggestion rows.
265
- * @param {HTMLElement} diffRow
266
- * @returns {HTMLElement[]}
314
+ * Toggle visibility of the cards for one line group.
315
+ * @param {string} key - The stable group key
316
+ * @param {HTMLElement} btn - The indicator button
317
+ * @private
267
318
  */
268
- _getCommentRowsFor(diffRow) {
269
- const rows = [];
270
- let next = diffRow.nextElementSibling;
271
- while (next) {
272
- if (
273
- next.classList.contains('user-comment-row') ||
274
- next.classList.contains('ai-suggestion-row') ||
275
- next.classList.contains('external-comment-row')
276
- ) {
277
- rows.push(next);
278
- } else if (
279
- next.classList.contains('comment-form-row') ||
280
- next.classList.contains('context-expand-row')
281
- ) {
282
- // Skip these but keep looking
283
- next = next.nextElementSibling;
284
- continue;
285
- } else {
286
- // Hit another diff line — stop
287
- break;
288
- }
289
- next = next.nextElementSibling;
319
+ _toggleLineComments(key, btn) {
320
+ const wrappers = this._wrappersForKey(key);
321
+ if (this._expandedLines.has(key)) {
322
+ this._expandedLines.delete(key);
323
+ btn.classList.remove('expanded');
324
+ this._applyExpanded(wrappers, false);
325
+ } else {
326
+ this._expandedLines.add(key);
327
+ btn.classList.add('expanded');
328
+ this._applyExpanded(wrappers, true);
290
329
  }
291
- return rows;
292
330
  }
293
331
 
294
332
  /**
295
- * Find the parent diff row for a given comment/suggestion element.
296
- * Public wrapper around _findDiffRowFor that first locates the containing
297
- * comment/suggestion row from any child element.
298
- * @param {HTMLElement} element - Any element inside (or equal to) a comment/suggestion row
299
- * @returns {HTMLElement|null} The parent diff row, or null
333
+ * Find the vendor annotation wrapper (`[data-annotation-slot]`) that holds a
334
+ * given card element. Used by navigation callers as a stable scroll anchor;
335
+ * returns null for elements not slotted onto a diff line (callers fall back
336
+ * to the element itself).
337
+ * @param {HTMLElement} element - A card element (or a child of one)
338
+ * @returns {HTMLElement|null}
300
339
  */
301
340
  findDiffRowFor(element) {
302
- const commentRow = element.closest('.user-comment-row, .ai-suggestion-row, .external-comment-row') || element;
303
- if (
304
- !commentRow.classList.contains('user-comment-row') &&
305
- !commentRow.classList.contains('ai-suggestion-row') &&
306
- !commentRow.classList.contains('external-comment-row')
307
- ) {
308
- return null;
309
- }
310
- return this._findDiffRowFor(commentRow);
341
+ return element?.closest?.('[data-annotation-slot]') || null;
311
342
  }
312
343
 
313
344
  // TODO: expose via API route so chat can programmatically expand findings when discussing them
314
345
  /**
315
- * Expand comments for a given element so it becomes visible when minimized.
316
- * Call this before scrolling to a comment/suggestion row that may be hidden.
317
- * @param {HTMLElement} element - The target comment/suggestion element (or row)
346
+ * Expand the comments for a given element so it becomes visible when
347
+ * minimized. Call this before scrolling to a card that may be hidden.
348
+ * @param {HTMLElement} element - The target card element (or a child of one)
318
349
  */
319
350
  expandForElement(element) {
320
- if (!this._active) return;
351
+ if (!this._active || !element) return;
321
352
 
322
- // Check if this element is inside a file-comments-zone (file-level comment)
353
+ // File-level comment inside a file-comments-zone.
323
354
  const zone = element.closest('.file-comments-zone');
324
355
  if (zone) {
325
356
  if (this._expandedFiles.has(zone)) return; // already expanded
326
357
  this._expandedFiles.add(zone);
327
358
  zone.classList.add('file-comments-expanded');
328
- // Update the file-header indicator button
329
359
  const wrapper = zone.closest('.d2h-file-wrapper');
330
360
  const btn = wrapper?.querySelector('.d2h-file-header .file-comment-indicator');
331
361
  if (btn) {
@@ -334,29 +364,18 @@ class CommentMinimizer {
334
364
  return;
335
365
  }
336
366
 
337
- // Line-level: find the containing comment/suggestion row
338
- const commentRow = element.closest('.user-comment-row, .ai-suggestion-row, .external-comment-row') || element;
339
- if (
340
- !commentRow.classList.contains('user-comment-row') &&
341
- !commentRow.classList.contains('ai-suggestion-row') &&
342
- !commentRow.classList.contains('external-comment-row')
343
- ) {
344
- return;
345
- }
346
-
347
- // Find the parent diff row for this comment row
348
- const diffRow = this._findDiffRowFor(commentRow);
349
- if (!diffRow) return;
367
+ // Line-level: resolve the containing vendor annotation wrapper.
368
+ const annotationWrapper = element.closest('[data-annotation-slot]');
369
+ if (!annotationWrapper) return;
370
+ const key = this._lineKeyFor(annotationWrapper);
371
+ if (!key) return;
350
372
 
351
- // Already expanded — nothing to do
352
- if (this._expandedLines.has(diffRow)) return;
373
+ this._expandedLines.add(key);
374
+ const wrappers = this._wrappersForKey(key);
375
+ this._applyExpanded(wrappers, true);
353
376
 
354
- // Expand all comment rows for this diff line
355
- this._expandedLines.add(diffRow);
356
- this._getCommentRowsFor(diffRow).forEach(row => row.classList.add('comment-expanded'));
357
-
358
- // Update the indicator button's expanded state
359
- const btn = diffRow.querySelector('.d2h-code-line-ctn .comment-indicator');
377
+ // Mark the indicator (injected into the group's first wrapper) as expanded.
378
+ const btn = wrappers[0]?.querySelector(':scope > .comment-indicator');
360
379
  if (btn) {
361
380
  btn.classList.add('expanded');
362
381
  }
@@ -487,6 +506,46 @@ class CommentMinimizer {
487
506
  document.querySelectorAll('.comment-indicator').forEach(btn => btn.remove());
488
507
  document.querySelectorAll('.file-comment-indicator').forEach(btn => btn.remove());
489
508
  }
509
+
510
+ // ---------------------------------------------------------------------------
511
+ // Rerender resilience
512
+ // ---------------------------------------------------------------------------
513
+
514
+ /**
515
+ * Observe the diff container for the vendor's card-recreating rerenders and
516
+ * re-inject indicators. Debounced via rAF and idempotent (refreshIndicators
517
+ * clears first). Paused during our own refresh so injecting indicators does
518
+ * not loop. No-op without a DOM (tests may run headless without MutationObserver).
519
+ * @private
520
+ */
521
+ _startObserving() {
522
+ if (this._observer || typeof MutationObserver === 'undefined') return;
523
+ const container = document.getElementById('diff-container');
524
+ if (!container) return;
525
+ this._observer = new MutationObserver(() => this._onDomMutation());
526
+ this._observer.observe(container, { childList: true, subtree: true });
527
+ }
528
+
529
+ /** Stop observing the diff container. @private */
530
+ _stopObserving() {
531
+ if (this._observer) {
532
+ this._observer.disconnect();
533
+ this._observer = null;
534
+ }
535
+ }
536
+
537
+ /** Debounced reaction to diff-container mutations. @private */
538
+ _onDomMutation() {
539
+ if (!this._active || this._refreshScheduled) return;
540
+ this._refreshScheduled = true;
541
+ const raf = (typeof requestAnimationFrame === 'function')
542
+ ? requestAnimationFrame
543
+ : (fn) => setTimeout(fn, 16);
544
+ raf(() => {
545
+ this._refreshScheduled = false;
546
+ if (this._active) this.refreshIndicators();
547
+ });
548
+ }
490
549
  }
491
550
 
492
551
  window.CommentMinimizer = CommentMinimizer;