@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
@@ -3,9 +3,34 @@
3
3
  * ExternalCommentManager - Read-only renderer for external review comments.
4
4
  *
5
5
  * Consumes `GET /api/reviews/:reviewId/external-comments?source=<source>` and
6
- * renders thread-grouped comments as `.external-comment-row` rows inserted
6
+ * renders thread-grouped comments as `.external-comment-row` rows anchored
7
7
  * after the appropriate diff line.
8
8
  *
9
+ * FILE-LEVEL PATH — a thread with `is_file_level` (GitHub subject_type='file')
10
+ * has no line anchor. It renders into the file's `.file-comments-zone` above
11
+ * the diff (the same light-DOM zone FileCommentManager uses for native
12
+ * file-level comments), carrying `external-comment-row--file-level`. Threads
13
+ * whose line anchor can't be found (outdated / force-pushed) fall back into the
14
+ * SAME zone with `external-comment-row--file-fallback`. Both keep the base
15
+ * `.external-comment-row` class so `clear()`'s DOM sweep, the comment-minimizer,
16
+ * and AIPanel.scrollToExternalThread stay agnostic to placement. A bare
17
+ * file-wrapper append is the last resort only when the zone is missing.
18
+ *
19
+ * DUAL RENDERING PATH — a thread's file is rendered by ONE of two engines:
20
+ * - PIERRE (@pierre/diffs): the file lives in a shadow-DOM
21
+ * `<diffs-container>` with NO light-DOM `<tr>` rows. The thread mounts as
22
+ * a custom `'external-comment'` annotation (via PierreBridge.addAnnotation),
23
+ * which the bridge slots BELOW its anchor line into the file's light DOM
24
+ * as a `<div class="external-comment-row">`. A file is pierre-rendered iff
25
+ * `window.prManager.pierreBridge.files.has(filePath)`.
26
+ * - LEGACY (table renderer): context files / bridge-less fallbacks still
27
+ * render as an HTML table. The thread mounts as a
28
+ * `<tr class="external-comment-row">` inserted BELOW the anchor row.
29
+ * Both variants carry the SAME inner `.external-comment-thread` card and the
30
+ * same `data-thread-id` / `data-source` attributes, so page CSS, the
31
+ * comment-minimizer, and AIPanel.scrollToExternalThread are agnostic to which
32
+ * path mounted them.
33
+ *
9
34
  * Read-only: no draft / submit / edit / dismiss flows. Only chat-about
10
35
  * actions, which delegate to the global chat panel.
11
36
  *
@@ -13,7 +38,10 @@
13
38
  * "Hazards"): three independent renderers append rows after the same diff
14
39
  * line: `.ai-suggestion-row`, `.user-comment-row`, `.external-comment-row`.
15
40
  * External rows sit BELOW AI suggestion + user comment rows for the same
16
- * diff line. This module ONLY touches `.external-comment-row` elements when
41
+ * diff line. In the legacy path this is enforced by `_insertAtOrderedPosition`;
42
+ * in the pierre path by PierreBridge's `typeOrder` map (external-comment sorts
43
+ * after suggestion + comment). This module ONLY touches its own
44
+ * `.external-comment-row` elements / `'external-comment'` annotations when
17
45
  * clearing — it never strips rows owned by other renderers.
18
46
  */
19
47
 
@@ -36,6 +64,10 @@ class ExternalCommentManager {
36
64
  // Per-manager in-flight promise. Coalesces concurrent loadAndRender calls
37
65
  // (page-load + manual refresh + post-AI re-render) into one round-trip.
38
66
  this._inflight = null;
67
+ // One-time guard: the 'external-comment' custom annotation renderer is
68
+ // registered on the shared PierreBridge exactly once (it persists on the
69
+ // bridge across re-renders). See `_ensurePierreRendererRegistered`.
70
+ this._pierreRendererRegistered = false;
39
71
  }
40
72
 
41
73
  // ------------------------------------------------------------------
@@ -238,11 +270,40 @@ class ExternalCommentManager {
238
270
  }
239
271
 
240
272
  /**
241
- * Remove all `.external-comment-row` rows we own from the DOM.
242
- * Leaves `.user-comment-row` and `.ai-suggestion-row` rows untouched.
273
+ * Remove all external-comment rows we own from BOTH rendering engines.
274
+ *
275
+ * Pierre-rendered files: drop the `'external-comment'` annotations via the
276
+ * bridge (which rerenders the file, un-slotting their `<div>` rows). We must
277
+ * NOT `.remove()` a slotted pierre `<div>` directly — the bridge still holds
278
+ * the annotation in `fileState.annotations` and would re-slot it on the next
279
+ * rerender, causing duplicates. Drop the annotation and let the bridge own
280
+ * the DOM lifecycle.
281
+ *
282
+ * Legacy files: strip the injected `<tr class="external-comment-row">`
283
+ * (and any file-level fallback `<div>`) from the DOM. Leaves
284
+ * `.user-comment-row` and `.ai-suggestion-row` rows untouched.
285
+ *
286
+ * Order matters: clear the pierre annotations FIRST so their rerender
287
+ * removes the slotted divs, then the DOM sweep only finds legacy rows.
243
288
  */
244
289
  clear() {
245
290
  if (typeof document === 'undefined') return;
291
+
292
+ const bridge = this._pierreBridge();
293
+ if (bridge && bridge.files && typeof bridge.removeAnnotationsByType === 'function') {
294
+ for (const [file] of bridge.files) {
295
+ // Guard the rerender: only touch files that actually carry an
296
+ // external-comment annotation so we don't force a needless rerender
297
+ // on every file in a large diff.
298
+ const anns = typeof bridge.getAnnotations === 'function'
299
+ ? bridge.getAnnotations(file, 'external-comment')
300
+ : null;
301
+ if (!anns || anns.length > 0) {
302
+ bridge.removeAnnotationsByType(file, 'external-comment');
303
+ }
304
+ }
305
+ }
306
+
246
307
  const rows = document.querySelectorAll('.external-comment-row');
247
308
  rows.forEach((row) => row.remove());
248
309
  }
@@ -316,6 +377,15 @@ class ExternalCommentManager {
316
377
  */
317
378
  _resolveAnchor(comment) {
318
379
  if (!comment || !comment.file) return null;
380
+
381
+ // File-level comments (GitHub subject_type='file') have no line anchor —
382
+ // they belong in the per-file comments zone above the diff, not on any
383
+ // line. Signal that with `fileLevel: true` so `_renderThread` routes them
384
+ // to the zone regardless of which diff engine rendered the file.
385
+ if (comment.is_file_level === 1 || comment.is_file_level === true) {
386
+ return { file: comment.file, fileLevel: true };
387
+ }
388
+
319
389
  const side = comment.side || 'RIGHT';
320
390
  const outdated = comment.is_outdated === 1 || comment.is_outdated === true;
321
391
 
@@ -361,6 +431,20 @@ class ExternalCommentManager {
361
431
  return;
362
432
  }
363
433
 
434
+ // File-level comments render in the per-file comments zone above the diff
435
+ // (light DOM, shared by both diff engines) — no line anchor, no bridge.
436
+ if (anchor.fileLevel) {
437
+ this._renderThreadFileLevel(thread, anchor.file);
438
+ return;
439
+ }
440
+
441
+ // Route by rendering engine. Pierre-rendered files have no light-DOM
442
+ // <tr> rows to anchor against, so they mount via the annotation bridge.
443
+ if (this._isPierreFile(anchor.file)) {
444
+ await this._renderThreadPierre(thread, anchor);
445
+ return;
446
+ }
447
+
364
448
  let targetRow = this._findDiffLineRow(anchor.file, anchor.line, anchor.side);
365
449
 
366
450
  // Outdated discussions often land on lines that the diff renderer
@@ -387,18 +471,11 @@ class ExternalCommentManager {
387
471
  }
388
472
 
389
473
  if (!targetRow) {
390
- // File-level fallback: outdated discussions that we can't anchor
391
- // precisely still need to be discoverable. Render them at the top of
392
- // the file wrapper so the reviewer can find them via the file panel.
393
- const fallbackTarget = this._resolveFileFallbackTarget(anchor.file);
394
- if (fallbackTarget) {
395
- const externalRow = this._buildThreadRow(thread, fallbackTarget);
396
- if (externalRow) {
397
- externalRow.classList.add('external-comment-row--file-fallback');
398
- this._insertAtOrderedPosition(fallbackTarget, externalRow);
399
- return;
400
- }
401
- }
474
+ // Unanchorable discussion (outdated / force-pushed line no longer in the
475
+ // diff). Render into the per-file comments zone above the diff rather
476
+ // than faking a line-1 annotation at the top of the table. Falls back to
477
+ // a bare wrapper append only when the zone itself is missing.
478
+ if (this._renderFileFallback(thread, anchor.file)) return;
402
479
 
403
480
  if (typeof console !== 'undefined') {
404
481
  console.warn(`[ExternalCommentManager] Could not find diff row for ${anchor.file}:${anchor.line} (${anchor.side})`);
@@ -423,51 +500,293 @@ class ExternalCommentManager {
423
500
  );
424
501
  }
425
502
 
503
+ // ------------------------------------------------------------------
504
+ // Pierre (@pierre/diffs) rendering path
505
+ // ------------------------------------------------------------------
506
+
426
507
  /**
427
- * Find a fallback insertion target for a file when no diff row matches
428
- * the comment's anchor (e.g. outdated comment whose original line is no
429
- * longer in the diff at all). Returns the first <tr> in the file
430
- * wrapper's table so the thread renders at file-level. Returns null when
431
- * the file wrapper itself isn't in the DOM.
508
+ * The shared PierreBridge instance, or null when @pierre/diffs isn't in
509
+ * play (unit tests, legacy-only fallback).
510
+ * @returns {Object|null}
432
511
  * @private
433
512
  */
434
- _resolveFileFallbackTarget(file) {
435
- if (typeof document === 'undefined' || !file) return null;
436
- let fileElement = null;
437
- if (typeof window !== 'undefined' && window.DiffRenderer && typeof window.DiffRenderer.findFileElement === 'function') {
438
- fileElement = window.DiffRenderer.findFileElement(file);
513
+ _pierreBridge() {
514
+ if (typeof window === 'undefined') return null;
515
+ return (window.prManager && window.prManager.pierreBridge) || null;
516
+ }
517
+
518
+ /**
519
+ * Whether `file` is (or will be) rendered by @pierre/diffs vs the legacy
520
+ * table. Pierre files have no light-DOM `<tr>` rows, so their threads mount
521
+ * as annotations. Lazily-registered Pierre files are absent from
522
+ * `bridge.files` until their body renders, so also consult the PR manager's
523
+ * lazy registry — `_renderThreadPierre` materializes the body itself.
524
+ * @param {string} file
525
+ * @returns {boolean}
526
+ * @private
527
+ */
528
+ _isPierreFile(file) {
529
+ const bridge = this._pierreBridge();
530
+ if (!bridge || !bridge.files || typeof bridge.files.has !== 'function') return false;
531
+ if (bridge.files.has(file)) return true;
532
+ const lazyBodies = window.prManager && window.prManager._lazyFileBodies;
533
+ const lazyEntry = lazyBodies && typeof lazyBodies.get === 'function'
534
+ ? lazyBodies.get(file)
535
+ : null;
536
+ return !!(lazyEntry && lazyEntry.pierre);
537
+ }
538
+
539
+ /**
540
+ * Stable, per-thread annotation id. Deterministic so a re-render targets
541
+ * (replaces / removes) the existing annotation by id instead of stacking.
542
+ * @param {Object} thread
543
+ * @returns {string}
544
+ * @private
545
+ */
546
+ _pierreAnnotationId(thread) {
547
+ const source = thread && thread.source ? thread.source : 'unknown';
548
+ const id = thread && thread.id != null ? thread.id : 'noid';
549
+ return `external-thread-${source}-${id}`;
550
+ }
551
+
552
+ /**
553
+ * The light-DOM container a pierre file's annotations are slotted into,
554
+ * or null.
555
+ * @param {string} file
556
+ * @returns {HTMLElement|null}
557
+ * @private
558
+ */
559
+ _pierreContainer(file) {
560
+ const fileState = this._pierreBridge()?.files?.get?.(file);
561
+ return (fileState && fileState.container) || null;
562
+ }
563
+
564
+ /**
565
+ * Register the `'external-comment'` custom annotation renderer on the shared
566
+ * PierreBridge once. The bridge re-invokes this callback whenever it (re)slots
567
+ * an external-comment annotation — including after worker rebuilds / content
568
+ * upgrades that re-apply `fileState.annotations` — so it rebuilds the card
569
+ * from the annotation's stored thread data each time.
570
+ * @param {Object} bridge
571
+ * @private
572
+ */
573
+ _ensurePierreRendererRegistered(bridge) {
574
+ if (this._pierreRendererRegistered) return;
575
+ if (!bridge || typeof bridge.registerAnnotationRenderer !== 'function') return;
576
+ bridge.registerAnnotationRenderer('external-comment', (data) => this._buildThreadDiv(data));
577
+ this._pierreRendererRegistered = true;
578
+ }
579
+
580
+ /**
581
+ * Re-query the live slotted `<div class="external-comment-row">` for a
582
+ * thread in a pierre file. The element is rebuilt on every FileDiff
583
+ * rerender, so cached references go stale — resolve fresh before use.
584
+ * @param {string} file
585
+ * @param {Object} thread
586
+ * @returns {HTMLElement|null}
587
+ * @private
588
+ */
589
+ _queryPierreRow(file, thread) {
590
+ const container = this._pierreContainer(file);
591
+ if (!container) return null;
592
+ const idAttr = thread && thread.id != null ? String(thread.id) : '';
593
+ let selector;
594
+ try {
595
+ const esc = (typeof globalThis !== 'undefined' && globalThis.CSS?.escape)
596
+ ? globalThis.CSS.escape(idAttr)
597
+ : idAttr;
598
+ selector = `.external-comment-row[data-thread-id="${esc}"]`;
599
+ } catch {
600
+ selector = `.external-comment-row[data-thread-id="${idAttr}"]`;
439
601
  }
440
- if (!fileElement) {
602
+ return container.querySelector(selector) || null;
603
+ }
604
+
605
+ /**
606
+ * Mount a thread into a PIERRE-rendered file as an `'external-comment'`
607
+ * annotation. The bridge slots the card BELOW its anchor line into the
608
+ * file's light DOM (so page CSS + inline handlers work) and orders it after
609
+ * suggestion + comment annotations on the same line via its `typeOrder` map.
610
+ *
611
+ * Outdated / gap-folded anchors: we first ask the PR manager to expand any
612
+ * collapsed gap covering the anchor line (mirrors `loadUserComments`) so the
613
+ * line is rendered before we add the annotation. If the annotation still
614
+ * doesn't slot (line genuinely absent from the diff), we roll it back and
615
+ * fall to a file-level fallback so the discussion stays discoverable.
616
+ * @private
617
+ */
618
+ async _renderThreadPierre(thread, anchor) {
619
+ const bridge = this._pierreBridge();
620
+ if (!bridge || typeof bridge.addAnnotation !== 'function') return;
621
+
622
+ if (this._canEnsureLinesVisible()) {
441
623
  try {
442
- const escaped = (typeof globalThis !== 'undefined' && globalThis.CSS?.escape)
443
- ? globalThis.CSS.escape(file)
444
- : file;
445
- fileElement = document.querySelector(`[data-file-name="${escaped}"]`);
446
- } catch {
447
- fileElement = null;
624
+ await window.prManager.ensureLinesVisible([
625
+ { file: anchor.file, line_start: anchor.line, line_end: anchor.line, side: anchor.side }
626
+ ]);
627
+ } catch (err) {
628
+ if (typeof console !== 'undefined') {
629
+ console.warn('[ExternalCommentManager] ensureLinesVisible threw (pierre), continuing', err);
630
+ }
448
631
  }
449
632
  }
450
- if (!fileElement) return null;
451
- return fileElement.querySelector('tr');
633
+
634
+ this._ensurePierreRendererRegistered(bridge);
635
+
636
+ const id = this._pierreAnnotationId(thread);
637
+ bridge.addAnnotation(anchor.file, {
638
+ lineNumber: anchor.line,
639
+ side: anchor.side,
640
+ type: 'external-comment',
641
+ id,
642
+ data: thread,
643
+ });
644
+
645
+ // addAnnotation rerenders synchronously; verify the row actually slotted.
646
+ // If the anchor line still isn't rendered, roll back and fall back to a
647
+ // file-level card so an unanchorable (e.g. destroyed-upstream) discussion
648
+ // stays discoverable rather than silently vanishing.
649
+ if (!this._queryPierreRow(anchor.file, thread)) {
650
+ if (typeof bridge.removeAnnotation === 'function') {
651
+ try { bridge.removeAnnotation(anchor.file, id); } catch { /* best effort */ }
652
+ }
653
+ this._renderPierreFileFallback(thread, anchor.file);
654
+ }
452
655
  }
453
656
 
454
657
  /**
455
- * Build the `<tr class="external-comment-row">` wrapper containing the
456
- * root comment, replies, and per-thread actions.
658
+ * File-level fallback for a pierre file: same zone-first strategy as the
659
+ * legacy path. Shares `_renderFileFallback`.
457
660
  * @private
458
661
  */
459
- _buildThreadRow(thread, targetRow) {
460
- if (typeof document === 'undefined') return null;
461
- const tr = document.createElement('tr');
462
- tr.className = 'external-comment-row';
463
- tr.dataset.threadId = thread.id != null ? String(thread.id) : '';
464
- tr.dataset.source = thread.source || '';
662
+ _renderPierreFileFallback(thread, file) {
663
+ if (!this._renderFileFallback(thread, file) && typeof console !== 'undefined') {
664
+ console.warn(`[ExternalCommentManager] Could not anchor pierre thread ${thread?.id} in ${file}`);
665
+ }
666
+ }
465
667
 
466
- const td = document.createElement('td');
467
- // Match user-comment-row colspan of 4 used elsewhere in the diff table
468
- td.colSpan = this._resolveColSpan(targetRow);
469
- td.className = 'external-comment-cell';
668
+ /**
669
+ * Render a genuine file-level thread (root + replies) into the file's
670
+ * comments zone above the diff. Carries `external-comment-row--file-level`.
671
+ * @private
672
+ */
673
+ _renderThreadFileLevel(thread, file) {
674
+ if (this._appendThreadToZone(thread, file, 'external-comment-row--file-level')) return;
675
+ // Zone missing (rare — created per file in renderFileDiff). Fall back to a
676
+ // bare wrapper append so the discussion stays discoverable.
677
+ if (!this._appendThreadToWrapper(thread, file, 'external-comment-row--file-level')
678
+ && typeof console !== 'undefined') {
679
+ console.warn(`[ExternalCommentManager] Could not anchor file-level thread ${thread?.id} in ${file}`);
680
+ }
681
+ }
682
+
683
+ /**
684
+ * Render an unanchorable line thread (outdated / line dropped from the diff)
685
+ * into the file's comments zone, carrying `external-comment-row--file-fallback`
686
+ * so the outdated badge + fallback styling are preserved. Returns true when
687
+ * the card was placed (zone OR wrapper). Bare wrapper append is the last
688
+ * resort when the zone is absent.
689
+ * @private
690
+ */
691
+ _renderFileFallback(thread, file) {
692
+ if (this._appendThreadToZone(thread, file, 'external-comment-row--file-fallback')) return true;
693
+ return this._appendThreadToWrapper(thread, file, 'external-comment-row--file-fallback');
694
+ }
695
+
696
+ /**
697
+ * Build the thread card `<div class="external-comment-row">`, tag it with
698
+ * `extraClass`, and append it to the file's `.file-comments-container`.
699
+ * Returns true on success, false when the zone is missing. The zone is light
700
+ * DOM created per file by FileCommentManager.createFileCommentsZone, so it
701
+ * works for BOTH the legacy table and pierre engines.
702
+ * @private
703
+ */
704
+ _appendThreadToZone(thread, file, extraClass) {
705
+ const zone = this._resolveFileCommentsZone(file);
706
+ if (!zone) return false;
707
+ const container = zone.querySelector('.file-comments-container') || zone;
708
+ const div = this._buildThreadDiv(thread);
709
+ if (!div) return false;
710
+ if (extraClass) div.classList.add(extraClass);
711
+ container.appendChild(div);
712
+ return true;
713
+ }
714
+
715
+ /**
716
+ * Last-resort placement when the comments zone is missing: append the card
717
+ * directly to the file wrapper (light DOM). `clear()`'s DOM sweep removes it
718
+ * on the next render cycle. Returns true on success.
719
+ * @private
720
+ */
721
+ _appendThreadToWrapper(thread, file, extraClass) {
722
+ const wrapper = this._findFileWrapper(file);
723
+ if (!wrapper) return false;
724
+ const div = this._buildThreadDiv(thread);
725
+ if (!div) return false;
726
+ if (extraClass) div.classList.add(extraClass);
727
+ wrapper.appendChild(div);
728
+ return true;
729
+ }
730
+
731
+ /**
732
+ * Resolve a file's `.file-comments-zone` element. Prefers
733
+ * FileCommentManager's own resolver (DiffRenderer-aware) when present, then
734
+ * falls back to the file wrapper lookup and a direct attribute selector.
735
+ * @param {string} file
736
+ * @returns {HTMLElement|null}
737
+ * @private
738
+ */
739
+ _resolveFileCommentsZone(file) {
740
+ if (typeof document === 'undefined' || !file) return null;
741
+ const fcm = (typeof window !== 'undefined' && window.prManager && window.prManager.fileCommentManager) || null;
742
+ if (fcm && typeof fcm.findZoneForFile === 'function') {
743
+ const zone = fcm.findZoneForFile(file);
744
+ if (zone) return zone;
745
+ }
746
+ const wrapper = this._findFileWrapper(file);
747
+ if (wrapper) {
748
+ const zone = wrapper.querySelector('.file-comments-zone');
749
+ if (zone) return zone;
750
+ }
751
+ try {
752
+ const esc = (typeof globalThis !== 'undefined' && globalThis.CSS?.escape)
753
+ ? globalThis.CSS.escape(file)
754
+ : file;
755
+ return document.querySelector(`.file-comments-zone[data-file-name="${esc}"]`);
756
+ } catch {
757
+ return null;
758
+ }
759
+ }
470
760
 
761
+ /**
762
+ * Resolve a file's `.d2h-file-wrapper` element (shared by both engines).
763
+ * @param {string} file
764
+ * @returns {HTMLElement|null}
765
+ * @private
766
+ */
767
+ _findFileWrapper(file) {
768
+ if (typeof document === 'undefined' || !file) return null;
769
+ if (typeof window !== 'undefined' && window.DiffRenderer && typeof window.DiffRenderer.findFileElement === 'function') {
770
+ const el = window.DiffRenderer.findFileElement(file);
771
+ if (el) return el;
772
+ }
773
+ try {
774
+ const esc = (typeof globalThis !== 'undefined' && globalThis.CSS?.escape)
775
+ ? globalThis.CSS.escape(file)
776
+ : file;
777
+ return document.querySelector(`.d2h-file-wrapper[data-file-name="${esc}"]`);
778
+ } catch {
779
+ return null;
780
+ }
781
+ }
782
+
783
+ /**
784
+ * Build the `.external-comment-thread` element containing the root comment,
785
+ * its replies, and per-comment actions. Shared by the legacy `<tr>` wrapper
786
+ * (`_buildThreadRow`) and the pierre `<div>` wrapper (`_buildThreadDiv`).
787
+ * @private
788
+ */
789
+ _buildThreadElement(thread) {
471
790
  const threadEl = document.createElement('div');
472
791
  threadEl.className = 'external-comment-thread';
473
792
 
@@ -482,11 +801,58 @@ class ExternalCommentManager {
482
801
  threadEl.appendChild(replyEl);
483
802
  }
484
803
 
485
- td.appendChild(threadEl);
804
+ return threadEl;
805
+ }
806
+
807
+ /**
808
+ * Apply the `data-thread-id` / `data-source` attributes both engines' row
809
+ * wrappers rely on (AIPanel.scrollToExternalThread queries them, and the
810
+ * comment-minimizer keys off the class). Kept in one place so the legacy
811
+ * `<tr>` and pierre `<div>` stay identical.
812
+ * @private
813
+ */
814
+ _applyThreadDataset(el, thread) {
815
+ el.dataset.threadId = thread.id != null ? String(thread.id) : '';
816
+ el.dataset.source = thread.source || '';
817
+ }
818
+
819
+ /**
820
+ * Build the LEGACY `<tr class="external-comment-row">` wrapper (table-based
821
+ * files) containing the shared thread card. Inserted below the anchor row.
822
+ * @private
823
+ */
824
+ _buildThreadRow(thread, targetRow) {
825
+ if (typeof document === 'undefined') return null;
826
+ const tr = document.createElement('tr');
827
+ tr.className = 'external-comment-row';
828
+ this._applyThreadDataset(tr, thread);
829
+
830
+ const td = document.createElement('td');
831
+ // Match user-comment-row colspan of 4 used elsewhere in the diff table
832
+ td.colSpan = this._resolveColSpan(targetRow);
833
+ td.className = 'external-comment-cell';
834
+
835
+ td.appendChild(this._buildThreadElement(thread));
486
836
  tr.appendChild(td);
487
837
  return tr;
488
838
  }
489
839
 
840
+ /**
841
+ * Build the PIERRE `<div class="external-comment-row">` wrapper. The bridge
842
+ * slots this into the file's light DOM below the anchor line. Carries the
843
+ * same class + `data-thread-id` / `data-source` as the legacy `<tr>` so page
844
+ * CSS, the minimizer, and the AIPanel scroll-to lookup treat both uniformly.
845
+ * @private
846
+ */
847
+ _buildThreadDiv(thread) {
848
+ if (typeof document === 'undefined') return null;
849
+ const div = document.createElement('div');
850
+ div.className = 'external-comment-row';
851
+ this._applyThreadDataset(div, thread);
852
+ div.appendChild(this._buildThreadElement(thread));
853
+ return div;
854
+ }
855
+
490
856
  /**
491
857
  * Best-effort colSpan resolution. The diff renderer uses a 4-column
492
858
  * layout (matches `.user-comment-cell` colSpan=4). Fall back to that
@@ -474,6 +474,19 @@ class FileCommentManager {
474
474
  card.classList.add('collapsed');
475
475
  }
476
476
 
477
+ // Collapsed-bar state tooltip (replaces the old inline state text).
478
+ const collapsedStateLabel = isAdopted ? 'Adopted' : (isDismissed ? 'Dismissed' : '');
479
+
480
+ // The reasoning brain button surfaces both the reasoning steps and, when the
481
+ // suggestion was dismissed with a recorded reason, that reason. Show the
482
+ // button when either is present. data-reasoning holds the steps;
483
+ // data-dismissal-reason holds the (agent-controlled) reason, encoded so
484
+ // quotes/angle brackets cannot break out of the attribute.
485
+ const hasReasoning = Array.isArray(suggestion.reasoning) && suggestion.reasoning.length > 0;
486
+ const dismissalReasonAttr = suggestion.status_reason ? encodeURIComponent(suggestion.status_reason) : '';
487
+ const reasoningDataAttr = hasReasoning ? encodeURIComponent(JSON.stringify(suggestion.reasoning)) : '';
488
+ const showReasoningBtn = hasReasoning || !!suggestion.status_reason;
489
+
477
490
  // Get category label for display (same as line-level)
478
491
  const categoryLabel = suggestion.type || suggestion.category || '';
479
492
 
@@ -494,22 +507,21 @@ class FileCommentManager {
494
507
  <span class="ai-title">${this.escapeHtml(suggestion.title || '')}</span>
495
508
  </div>
496
509
  <div class="ai-suggestion-header-right">
497
- ${suggestion.reasoning && suggestion.reasoning.length > 0 ? `
498
- <button class="btn-reasoning-toggle" title="View reasoning" data-suggestion-id="${suggestion.id}" data-reasoning="${encodeURIComponent(JSON.stringify(suggestion.reasoning))}">
510
+ ${showReasoningBtn ? `
511
+ <button class="btn-reasoning-toggle" title="View reasoning" data-suggestion-id="${suggestion.id}" data-reasoning="${reasoningDataAttr}" data-dismissal-reason="${dismissalReasonAttr}">
499
512
  <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>
500
513
  </button>
501
514
  ` : ''}
502
515
  </div>
503
516
  </div>
504
- <div class="ai-suggestion-collapsed-content">
517
+ <div class="ai-suggestion-collapsed-content"${collapsedStateLabel ? ` title="${collapsedStateLabel}"` : ''}>
505
518
  ${suggestion.type === 'praise'
506
519
  ? `<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>`
507
520
  : `<span class="ai-suggestion-badge collapsed" data-type="${suggestion.type}" title="AI Suggestion"><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>`}
508
- <span class="collapsed-text">${isAdopted ? 'Suggestion adopted' : 'Hidden AI suggestion'}</span>
509
521
  <span class="collapsed-title">${this.escapeHtml(suggestion.title || '')}</span>
510
522
  <div class="ai-suggestion-header-right">
511
- ${suggestion.reasoning && suggestion.reasoning.length > 0 ? `
512
- <button class="btn-reasoning-toggle collapsed-reasoning" title="View reasoning" data-suggestion-id="${suggestion.id}" data-reasoning="${encodeURIComponent(JSON.stringify(suggestion.reasoning))}">
523
+ ${showReasoningBtn ? `
524
+ <button class="btn-reasoning-toggle collapsed-reasoning" title="View reasoning" data-suggestion-id="${suggestion.id}" data-reasoning="${reasoningDataAttr}" data-dismissal-reason="${dismissalReasonAttr}">
513
525
  <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>
514
526
  </button>
515
527
  ` : ''}
@@ -524,6 +536,7 @@ class FileCommentManager {
524
536
  <div class="ai-suggestion-body">
525
537
  ${renderedBody}
526
538
  </div>
539
+ ${window.SuggestionUI?.buildDismissalNoteHtml?.(suggestion.status_reason) || ''}
527
540
  <div class="ai-suggestion-actions">
528
541
  <button class="ai-action ai-action-adopt">
529
542
  <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>
@@ -594,11 +607,7 @@ class FileCommentManager {
594
607
  const suggestionCard = zone.querySelector(`[data-suggestion-id="${suggestion.id}"]`);
595
608
  if (suggestionCard) {
596
609
  suggestionCard.classList.add('collapsed');
597
- // Update collapsed text to show "Suggestion adopted"
598
- const collapsedText = suggestionCard.querySelector('.collapsed-text');
599
- if (collapsedText) {
600
- collapsedText.textContent = 'Suggestion adopted';
601
- }
610
+ window.SuggestionUI?.setCollapsedStateTooltip?.(suggestionCard, 'Adopted');
602
611
  }
603
612
 
604
613
  // Use the server-formatted body — server is the single source of truth
@@ -676,11 +685,7 @@ class FileCommentManager {
676
685
  const card = zone.querySelector(`[data-suggestion-id="${suggestionId}"]`);
677
686
  if (card) {
678
687
  card.classList.add('collapsed');
679
- // Update collapsed text to show "Hidden AI suggestion"
680
- const collapsedText = card.querySelector('.collapsed-text');
681
- if (collapsedText) {
682
- collapsedText.textContent = 'Hidden AI suggestion';
683
- }
688
+ window.SuggestionUI?.setCollapsedStateTooltip?.(card, 'Dismissed');
684
689
  }
685
690
 
686
691
  this.updateCommentCount(zone);
@@ -728,6 +733,12 @@ class FileCommentManager {
728
733
  const card = zone.querySelector(`[data-suggestion-id="${suggestionId}"]`);
729
734
  if (card) {
730
735
  card.classList.remove('collapsed');
736
+ // Restored to active — clear the collapsed-bar state tooltip.
737
+ window.SuggestionUI?.setCollapsedStateTooltip?.(card, '');
738
+ // Strip stale dismissal-reason markup (note + popover attr/brain button)
739
+ // baked in while dismissed; same-tab broadcasts are suppressed so no
740
+ // refetch rescues this card.
741
+ window.SuggestionUI?.clearDismissalReasonUI?.(card);
731
742
  }
732
743
 
733
744
  // Update finding status in AI Panel (mark suggestion as active)
@@ -864,11 +875,7 @@ class FileCommentManager {
864
875
  const suggestionCard = zone.querySelector(`[data-suggestion-id="${suggestion.id}"]`);
865
876
  if (suggestionCard) {
866
877
  suggestionCard.classList.add('collapsed');
867
- // Update collapsed text to show "Suggestion adopted"
868
- const collapsedText = suggestionCard.querySelector('.collapsed-text');
869
- if (collapsedText) {
870
- collapsedText.textContent = 'Suggestion adopted';
871
- }
878
+ window.SuggestionUI?.setCollapsedStateTooltip?.(suggestionCard, 'Adopted');
872
879
  }
873
880
 
874
881
  // Use the server-formatted body — server is the single source of truth