@in-the-loop-labs/pair-review 5.3.6 → 5.4.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 (41) hide show
  1. package/README.md +8 -0
  2. package/package.json +3 -3
  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/public/css/pr.css +407 -13
  7. package/public/js/components/AIPanel.js +323 -104
  8. package/public/js/components/ReviewHistoryModal.js +294 -0
  9. package/public/js/components/ReviewModal.js +131 -14
  10. package/public/js/components/SplitButton.js +14 -0
  11. package/public/js/local.js +4 -8
  12. package/public/js/modules/comment-manager.js +6 -12
  13. package/public/js/modules/comment-minimizer.js +4 -1
  14. package/public/js/modules/file-comment-manager.js +38 -74
  15. package/public/js/modules/pierre-bridge.js +36 -0
  16. package/public/js/modules/suggestion-manager.js +4 -1
  17. package/public/js/pr.js +337 -191
  18. package/public/js/utils/posted-comment.js +326 -0
  19. package/public/js/vendor/pierre-diffs-worker.js +41 -13485
  20. package/public/js/vendor/pierre-diffs.js +131 -1364
  21. package/public/local.html +1 -0
  22. package/public/pr.html +2 -0
  23. package/src/chat/api-reference.js +22 -2
  24. package/src/config.js +28 -4
  25. package/src/database.js +177 -35
  26. package/src/github/client.js +95 -16
  27. package/src/github/impl/graphql/pending-review-comments.js +118 -14
  28. package/src/github/impl/graphql/pending-review.js +8 -2
  29. package/src/github/impl/graphql/review-comment-edit.js +89 -0
  30. package/src/github/impl/host/pending-review-comments.js +41 -10
  31. package/src/github/impl/host/review-comment-edit.js +36 -0
  32. package/src/github/impl/rest/pending-review.js +6 -2
  33. package/src/github/impl/rest/review-comment-edit.js +44 -0
  34. package/src/github/impl/review-comment-edit-utils.js +45 -0
  35. package/src/github/operations/pending-review.js +4 -3
  36. package/src/github/operations/review-comment-edit.js +35 -0
  37. package/src/github/record-submission.js +78 -0
  38. package/src/github/update-posted-comment.js +115 -0
  39. package/src/main.js +24 -38
  40. package/src/routes/mcp.js +1 -0
  41. package/src/routes/pr.js +152 -100
package/public/local.html CHANGED
@@ -574,6 +574,7 @@
574
574
 
575
575
  <!-- Markdown renderer utility -->
576
576
  <script src="/js/utils/markdown.js"></script>
577
+ <script src="/js/utils/posted-comment.js"></script>
577
578
 
578
579
  <!-- Tier icons utility -->
579
580
  <script src="/js/utils/tier-icons.js"></script>
package/public/pr.html CHANGED
@@ -379,6 +379,7 @@
379
379
 
380
380
  <!-- Markdown renderer utility -->
381
381
  <script src="/js/utils/markdown.js"></script>
382
+ <script src="/js/utils/posted-comment.js"></script>
382
383
 
383
384
  <!-- Tier icons utility -->
384
385
  <script src="/js/utils/tier-icons.js"></script>
@@ -428,6 +429,7 @@
428
429
  <script src="/js/components/StatusIndicator.js"></script>
429
430
  <script src="/js/components/SuggestionNavigator.js"></script>
430
431
  <script src="/js/components/ReviewModal.js"></script>
432
+ <script src="/js/components/ReviewHistoryModal.js"></script>
431
433
  <script src="/js/components/PreviewModal.js"></script>
432
434
  <script src="/js/components/SplitButton.js"></script>
433
435
  <script src="/js/components/AISummaryModal.js"></script>
@@ -69,6 +69,8 @@ curl -s http://localhost:{{PORT}}/api/reviews/{{REVIEW_ID}}/comments
69
69
 
70
70
  Optional query param: \`includeDismissed=true\` to include soft-deleted (inactive) comments.
71
71
 
72
+ User comment \`status\` is lifecycle only: \`active\` or \`inactive\`. Each comment has \`publication: null\` (never posted) or \`{ state: "draft"|"posted", posted_at, review_url, comment_url, github_review_row_id, posting_count, changed }\`. Restoring a posted comment does not post it again. Local-mode comments have no publication.
73
+
72
74
  **Response:** \`{ "success": true, "comments": [...] }\`
73
75
 
74
76
  ### Create a comment
@@ -123,6 +125,24 @@ curl -s -X PUT http://localhost:{{PORT}}/api/reviews/{{REVIEW_ID}}/comments/COMM
123
125
 
124
126
  **Response:** \`{ "success": true, "message": "Comment updated successfully" }\`
125
127
 
128
+ ### Update a posted comment on GitHub (PR mode only)
129
+
130
+ Editing a comment locally does not create another GitHub comment. If \`publication.changed\` is true, an explicit update sends the edited text to the existing host comment, including a comment in a pending draft review:
131
+
132
+ \`\`\`bash
133
+ curl -s -X POST http://localhost:{{PORT}}/api/reviews/{{REVIEW_ID}}/comments/COMMENT_ID/update-on-github
134
+ \`\`\`
135
+
136
+ **Response:** \`{ "success": true, "comment": {...} }\`. Failed updates return \`{ error, code, review_url? }\` (\`not_found_on_github\`, \`forbidden\`, or \`unresolved_identity\`) and never create a replacement. Missing identities are recovered only from exactly one strict match of the original sent text, author, and anchor. Dismissal remains local-only.
137
+
138
+ ### Review history (PR mode only)
139
+
140
+ \`\`\`bash
141
+ curl -s http://localhost:{{PORT}}/api/reviews/{{REVIEW_ID}}/submissions
142
+ \`\`\`
143
+
144
+ **Response:** \`{ "submissions": [{ ...review, "postings": [...] }] }\`, newest review first. Each posting retains the sent body, anchor, host link, current \`comment_status\`, and \`kind\` (\`post\` or \`update\`). Legacy duplicate review identities are grouped; backfilled snapshots are approximate.
145
+
126
146
  ### Delete a comment
127
147
 
128
148
  > **Terminology note:** The UI refers to this operation as "dismiss." When a user asks to "dismiss a comment," use this DELETE endpoint.
@@ -143,9 +163,9 @@ curl -s -X PUT http://localhost:{{PORT}}/api/reviews/{{REVIEW_ID}}/comments/COMM
143
163
 
144
164
  **Response:** \`{ "success": true, "message": "Comment restored successfully", "comment": {...} }\`
145
165
 
146
- ### Bulk delete all comments
166
+ ### Bulk delete pending comments
147
167
 
148
- Deletes all user comments for a review. Also dismisses any parent AI suggestions.
168
+ Dismisses all user comments not yet posted for a review. Posted comments are kept. Also dismisses any parent AI suggestions of the affected comments.
149
169
 
150
170
  \`\`\`bash
151
171
  curl -s -X DELETE http://localhost:{{PORT}}/api/reviews/{{REVIEW_ID}}/comments
package/src/config.js CHANGED
@@ -12,11 +12,13 @@ const _stackWalkerOps = require('./github/operations/stack-walker');
12
12
  const _pendingReviewOps = require('./github/operations/pending-review');
13
13
  const _reviewLifecycleOps = require('./github/operations/review-lifecycle');
14
14
  const _pendingReviewCommentsOps = require('./github/operations/pending-review-comments');
15
+ const _reviewCommentEditOps = require('./github/operations/review-comment-edit');
15
16
  const IMPLEMENTATION_MATRIX = {
16
17
  stack_walker: _stackWalkerOps.IMPLEMENTED_MODES,
17
18
  pending_review_check: _pendingReviewOps.IMPLEMENTED_MODES,
18
19
  review_lifecycle: _reviewLifecycleOps.IMPLEMENTED_MODES,
19
- pending_review_comments: _pendingReviewCommentsOps.IMPLEMENTED_MODES
20
+ pending_review_comments: _pendingReviewCommentsOps.IMPLEMENTED_MODES,
21
+ review_comment_edit: _reviewCommentEditOps.IMPLEMENTED_MODES
20
22
  };
21
23
 
22
24
  // Recognised sub-keys that configure an implementation without selecting a
@@ -24,7 +26,9 @@ const IMPLEMENTATION_MATRIX = {
24
26
  // explicitly keeps typos from being silently ignored.
25
27
  const KNOWN_FEATURE_SUBKEYS = new Set([
26
28
  'pending_review_comments_endpoint',
27
- 'pending_review_comments_batch'
29
+ 'pending_review_comments_batch',
30
+ 'review_comment_edit_endpoint',
31
+ 'review_comment_edit_list_endpoint'
28
32
  ]);
29
33
 
30
34
  let _cachedCommandToken = null;
@@ -41,13 +45,15 @@ const FEATURE_AREAS = [
41
45
  'pending_review_check',
42
46
  'stack_walker',
43
47
  'review_lifecycle',
44
- 'pending_review_comments'
48
+ 'pending_review_comments',
49
+ 'review_comment_edit'
45
50
  ];
46
51
  const GRAPHQL_DEFAULT_AREAS = new Set([
47
52
  'pending_review_check',
48
53
  'stack_walker',
49
54
  'review_lifecycle',
50
- 'pending_review_comments'
55
+ 'pending_review_comments',
56
+ 'review_comment_edit'
51
57
  ]);
52
58
  const ALLOWED_FEATURE_VALUES = new Set(['graphql', 'rest', 'host']);
53
59
 
@@ -1062,6 +1068,24 @@ function validateRepoConfig(config) {
1062
1068
  }
1063
1069
  }
1064
1070
 
1071
+ for (const [key, required] of [
1072
+ ['review_comment_edit_endpoint', ['{owner}', '{repo}', '{comment_id}']],
1073
+ ['review_comment_edit_list_endpoint', ['{owner}', '{repo}', '{pull_number}', '{review_id}']]
1074
+ ]) {
1075
+ const value = features[key];
1076
+ if (value === undefined || value === null) continue;
1077
+ const prefix = `Invalid pair-review config: repos["${repoKey}"].features.${key}`;
1078
+ if (typeof value !== 'string' || !value) throw new Error(`${prefix} must be a non-empty string.`);
1079
+ if (resolvedFeatures.review_comment_edit !== 'host') throw new Error(`${prefix} is only valid when review_comment_edit = "host".`);
1080
+ if (!value.startsWith('/') || value.startsWith('//') || /[\\\\\r\n]/.test(value)) {
1081
+ throw new Error(`${prefix} must be a relative path starting with a single slash.`);
1082
+ }
1083
+ const missing = required.filter(placeholder => !value.includes(placeholder));
1084
+ if (missing.length) throw new Error(`${prefix} is missing required placeholder(s): ${missing.join(', ')}.`);
1085
+ const unknown = value.match(/\{[^}]+\}/g)?.filter(placeholder => !required.includes(placeholder)) || [];
1086
+ if (unknown.length) throw new Error(`${prefix} has unknown placeholder(s): ${unknown.join(', ')}.`);
1087
+ }
1088
+
1065
1089
  // Some hosts expose the pending-review endpoint but accept one comment
1066
1090
  // per POST instead of the generic batch envelope. Batch mode remains the
1067
1091
  // default to preserve the documented host-extension contract.
package/src/database.js CHANGED
@@ -21,7 +21,7 @@ function getDbPath() {
21
21
  /**
22
22
  * Current schema version - increment this when adding new migrations
23
23
  */
24
- const CURRENT_SCHEMA_VERSION = 55;
24
+ const CURRENT_SCHEMA_VERSION = 58;
25
25
 
26
26
  /**
27
27
  * Database schema SQL statements
@@ -52,6 +52,27 @@ const SCHEMA_SQL = {
52
52
  )
53
53
  `,
54
54
 
55
+ comment_postings: `
56
+ CREATE TABLE IF NOT EXISTS comment_postings (
57
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
58
+ comment_id INTEGER NOT NULL REFERENCES comments(id) ON DELETE CASCADE,
59
+ github_review_row_id INTEGER REFERENCES github_reviews(id) ON DELETE SET NULL,
60
+ comment_body TEXT,
61
+ body TEXT NOT NULL,
62
+ file TEXT,
63
+ line_start INTEGER,
64
+ line_end INTEGER,
65
+ side TEXT,
66
+ is_file_level INTEGER NOT NULL DEFAULT 0,
67
+ github_comment_id TEXT,
68
+ github_comment_node_id TEXT,
69
+ github_comment_url TEXT,
70
+ backfilled INTEGER NOT NULL DEFAULT 0,
71
+ kind TEXT NOT NULL DEFAULT 'post' CHECK (kind IN ('post', 'update')),
72
+ posted_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
73
+ )
74
+ `,
75
+
55
76
  comments_old: `
56
77
  CREATE TABLE IF NOT EXISTS comments_old (
57
78
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -395,6 +416,8 @@ const SCHEMA_SQL = {
395
416
  * Index SQL statements for performance
396
417
  */
397
418
  const INDEX_SQL = [
419
+ 'CREATE INDEX IF NOT EXISTS idx_comment_postings_comment ON comment_postings(comment_id, id)',
420
+ 'CREATE INDEX IF NOT EXISTS idx_comment_postings_github_review ON comment_postings(github_review_row_id)',
398
421
  'CREATE INDEX IF NOT EXISTS idx_reviews_pr ON reviews(pr_number, repository)',
399
422
  'CREATE INDEX IF NOT EXISTS idx_comments_review_file ON comments(review_id, file, line_start)',
400
423
  'CREATE INDEX IF NOT EXISTS idx_comments_ai_run ON comments(ai_run_id)',
@@ -490,6 +513,27 @@ function tableExists(db, tableName) {
490
513
  return !!row;
491
514
  }
492
515
 
516
+ // Bind each legacy comment's values rather than referencing outer columns in
517
+ // ORDER BY (unsupported by SQLite). Guarding by posting makes crash retry safe.
518
+ function backfillLegacyPostings(db) {
519
+ const comments = db.prepare(`SELECT * FROM comments c
520
+ WHERE status IN ('submitted', 'draft')
521
+ AND NOT EXISTS (SELECT 1 FROM comment_postings p WHERE p.comment_id = c.id)`).all();
522
+ const findReview = db.prepare(`SELECT id FROM github_reviews WHERE review_id = ?
523
+ ORDER BY (state = 'pending') = ? DESC,
524
+ julianday(created_at) <= julianday(?) + 5.0/86400 DESC,
525
+ created_at DESC, id DESC LIMIT 1`);
526
+ const insert = db.prepare(`INSERT INTO comment_postings
527
+ (comment_id, github_review_row_id, comment_body, body, file, line_start, line_end,
528
+ side, is_file_level, backfilled, posted_at)
529
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1, COALESCE(?, CURRENT_TIMESTAMP))`);
530
+ for (const c of comments) {
531
+ const review = findReview.get(c.review_id, c.status === 'draft' ? 1 : 0, c.updated_at);
532
+ insert.run(c.id, review?.id ?? null, c.body, c.body ?? '', c.file,
533
+ c.line_start, c.line_end, c.side, c.is_file_level || 0, c.updated_at);
534
+ }
535
+ }
536
+
493
537
  const MIGRATIONS = {
494
538
  // Migration to version 1: handles all legacy column additions
495
539
  1: (db) => {
@@ -2363,6 +2407,42 @@ const MIGRATIONS = {
2363
2407
  console.log(' pool_fetch_owner column already present; nothing to do');
2364
2408
  }
2365
2409
  console.log('Migration to schema version 55 complete');
2410
+ },
2411
+
2412
+ 56: (db) => {
2413
+ db.transaction(() => {
2414
+ db.exec(SCHEMA_SQL.comment_postings);
2415
+ db.exec('CREATE INDEX IF NOT EXISTS idx_comment_postings_comment ON comment_postings(comment_id, id)');
2416
+ db.exec('CREATE INDEX IF NOT EXISTS idx_comment_postings_github_review ON comment_postings(github_review_row_id)');
2417
+ backfillLegacyPostings(db);
2418
+ })();
2419
+ },
2420
+
2421
+ 57: (db) => {
2422
+ db.transaction(() => {
2423
+ backfillLegacyPostings(db);
2424
+ // Fix obsolete pending duplicates without deleting history. Subsequent
2425
+ // upserts propagate state to all rows of the same GitHub identity.
2426
+ const pending = db.prepare("SELECT * FROM github_reviews WHERE state = 'pending'").all();
2427
+ const newer = db.prepare(`SELECT * FROM github_reviews WHERE review_id = ? AND id > ?
2428
+ AND state != 'pending' AND
2429
+ ((? IS NOT NULL AND github_node_id = ?) OR (? IS NOT NULL AND github_review_id = ?))
2430
+ ORDER BY id DESC LIMIT 1`);
2431
+ for (const row of pending) {
2432
+ const match = newer.get(row.review_id, row.id, row.github_node_id, row.github_node_id,
2433
+ row.github_review_id, row.github_review_id);
2434
+ if (match) db.prepare('UPDATE github_reviews SET state = ?, submitted_at = ?, event = ? WHERE id = ?')
2435
+ .run(match.state, match.submitted_at, match.event, row.id);
2436
+ }
2437
+ db.exec("UPDATE comments SET status = 'active' WHERE source = 'user' AND status IN ('submitted', 'draft')");
2438
+ db.exec("UPDATE comments SET status = 'submitted' WHERE source = 'ai' AND status = 'draft'");
2439
+ })();
2440
+ },
2441
+
2442
+ 58: (db) => {
2443
+ if (!columnExists(db, 'comment_postings', 'kind')) {
2444
+ db.exec("ALTER TABLE comment_postings ADD COLUMN kind TEXT NOT NULL DEFAULT 'post' CHECK (kind IN ('post', 'update'))");
2445
+ }
2366
2446
  }
2367
2447
  };
2368
2448
 
@@ -3623,7 +3703,32 @@ class RepoSettingsRepository {
3623
3703
  /**
3624
3704
  * CommentRepository class for managing comment database records
3625
3705
  */
3706
+ // Shared joins for list/single/restore responses. Publication is independent
3707
+ // of lifecycle: dismissed comments retain their posting history.
3708
+ const PUBLICATION_JOIN = `LEFT JOIN comment_postings lp ON lp.id =
3709
+ (SELECT MAX(id) FROM comment_postings WHERE comment_id = c.id)
3710
+ LEFT JOIN github_reviews lgr ON lgr.id = lp.github_review_row_id`;
3711
+ const PUBLICATION_COLUMNS = `lp.id AS posting_id, lp.posted_at AS publication_posted_at,
3712
+ lp.github_review_row_id, lp.github_comment_url, lgr.github_url AS review_url,
3713
+ lgr.state AS publication_review_state, lp.comment_body IS NOT c.body AS publication_changed,
3714
+ (SELECT COUNT(*) FROM comment_postings WHERE comment_id = c.id) AS posting_count`;
3715
+
3716
+ function shapePublication(row) {
3717
+ if (!row) return row;
3718
+ const { posting_id, publication_posted_at, github_review_row_id,
3719
+ github_comment_url, review_url, publication_review_state, publication_changed, posting_count, ...comment } = row;
3720
+ return { ...comment, publication: posting_id == null ? null : {
3721
+ state: publication_review_state === 'pending' ? 'draft' : 'posted',
3722
+ posted_at: publication_posted_at, review_url, comment_url: github_comment_url,
3723
+ github_review_row_id, posting_count, changed: Boolean(publication_changed)
3724
+ } };
3725
+ }
3726
+
3626
3727
  class CommentRepository {
3728
+ // Mirrored by isPendingComment in posted-comment.js; pinned by parity tests.
3729
+ static pendingCommentSql(alias = 'c') {
3730
+ return `${alias}.status = 'active' AND NOT EXISTS (SELECT 1 FROM comment_postings p WHERE p.comment_id = ${alias}.id)`;
3731
+ }
3627
3732
  /**
3628
3733
  * Create a new CommentRepository instance
3629
3734
  * @param {Database} db - Database instance
@@ -3855,7 +3960,7 @@ class CommentRepository {
3855
3960
  * @returns {Promise<Object|null>} Comment record or null if not found
3856
3961
  */
3857
3962
  async getComment(id, source = null) {
3858
- let sql = 'SELECT * FROM comments WHERE id = ?';
3963
+ let sql = `SELECT c.*, ${PUBLICATION_COLUMNS} FROM comments c ${PUBLICATION_JOIN} WHERE c.id = ?`;
3859
3964
  const params = [id];
3860
3965
 
3861
3966
  if (source) {
@@ -3863,7 +3968,7 @@ class CommentRepository {
3863
3968
  params.push(source);
3864
3969
  }
3865
3970
 
3866
- return await queryOne(this.db, sql, params);
3971
+ return shapePublication(await queryOne(this.db, sql, params));
3867
3972
  }
3868
3973
 
3869
3974
  /**
@@ -3925,7 +4030,7 @@ class CommentRepository {
3925
4030
  }
3926
4031
 
3927
4032
  /**
3928
- * Bulk delete all user comments for a review
4033
+ * Bulk dismiss only user comments not yet posted for a review
3929
4034
  * Also dismisses any AI suggestions that were parents of the deleted comments.
3930
4035
  * @param {number} reviewId - Review ID (from reviews table)
3931
4036
  * @returns {Promise<{deletedCount: number, dismissedSuggestionIds: number[]}>} Number of comments deleted and list of dismissed suggestion IDs
@@ -3943,16 +4048,16 @@ class CommentRepository {
3943
4048
 
3944
4049
  // First, find all user comments with parent_id (adopted from AI suggestions)
3945
4050
  const adoptedComments = await query(this.db, `
3946
- SELECT parent_id FROM comments
4051
+ SELECT parent_id FROM comments c
3947
4052
  WHERE review_id = ? AND source = 'user' AND parent_id IS NOT NULL
3948
- AND status IN ('active', 'submitted', 'draft')
4053
+ AND ${CommentRepository.pendingCommentSql()}
3949
4054
  `, [reviewId]);
3950
4055
 
3951
4056
  // Soft delete all user comments
3952
4057
  const result = await run(this.db, `
3953
- UPDATE comments
4058
+ UPDATE comments AS c
3954
4059
  SET status = 'inactive', updated_at = CURRENT_TIMESTAMP
3955
- WHERE review_id = ? AND source = 'user' AND status IN ('active', 'submitted', 'draft')
4060
+ WHERE review_id = ? AND source = 'user' AND ${CommentRepository.pendingCommentSql()}
3956
4061
  `, [reviewId]);
3957
4062
 
3958
4063
  // Dismiss all parent AI suggestions with a single UPDATE statement
@@ -3984,33 +4089,14 @@ class CommentRepository {
3984
4089
  */
3985
4090
  async getUserComments(reviewId, options = {}) {
3986
4091
  const { includeDismissed = false } = options;
3987
- const statusFilter = includeDismissed
3988
- ? "status IN ('active', 'submitted', 'draft', 'inactive')"
3989
- : "status IN ('active', 'submitted', 'draft')";
3990
-
3991
- return await query(this.db, `
3992
- SELECT
3993
- id,
3994
- source,
3995
- author,
3996
- file,
3997
- line_start,
3998
- line_end,
3999
- side,
4000
- diff_position,
4001
- type,
4002
- title,
4003
- body,
4004
- status,
4005
- parent_id,
4006
- is_file_level,
4007
- severity,
4008
- created_at,
4009
- updated_at
4010
- FROM comments
4011
- WHERE review_id = ? AND source = 'user' AND ${statusFilter}
4012
- ORDER BY file, line_start, created_at
4092
+ const statusFilter = includeDismissed ? "c.status IN ('active', 'inactive')" : "c.status = 'active'";
4093
+ const rows = await query(this.db, `
4094
+ SELECT c.*, ${PUBLICATION_COLUMNS}
4095
+ FROM comments c ${PUBLICATION_JOIN}
4096
+ WHERE c.review_id = ? AND c.source = 'user' AND ${statusFilter}
4097
+ ORDER BY c.file, c.line_start, c.created_at
4013
4098
  `, [reviewId]);
4099
+ return rows.map(shapePublication);
4014
4100
  }
4015
4101
 
4016
4102
  /**
@@ -4066,7 +4152,8 @@ class CommentRepository {
4066
4152
  }
4067
4153
 
4068
4154
  /**
4069
- * Restore a soft-deleted user comment (set status from 'inactive' back to 'active')
4155
+ * Restore a soft-deleted user comment. Postings survive dismissal, so
4156
+ * restoring a posted comment does not queue it for another submission.
4070
4157
  * @param {number} id - Comment ID
4071
4158
  * @returns {Promise<boolean>} True if restored successfully
4072
4159
  */
@@ -5654,6 +5741,33 @@ class AnalysisRunRepository {
5654
5741
  /**
5655
5742
  * GitHubReviewRepository class for managing GitHub review submission records
5656
5743
  */
5744
+ class CommentPostingRepository {
5745
+ constructor(db) { this.db = db; }
5746
+
5747
+ async insertMany(postings) {
5748
+ for (const p of postings) {
5749
+ await run(this.db, `INSERT INTO comment_postings
5750
+ (comment_id, github_review_row_id, comment_body, body, file, line_start,
5751
+ line_end, side, is_file_level, github_comment_id, github_comment_node_id,
5752
+ github_comment_url, backfilled, kind, posted_at)
5753
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE(?, CURRENT_TIMESTAMP))`,
5754
+ [p.comment_id, p.github_review_row_id ?? null, p.comment_body ?? null, p.body,
5755
+ p.file ?? null, p.line_start ?? null, p.line_end ?? null, p.side ?? null,
5756
+ p.is_file_level ? 1 : 0, p.github_comment_id == null ? null : String(p.github_comment_id),
5757
+ p.github_comment_node_id ?? null, p.github_comment_url ?? null,
5758
+ p.backfilled ? 1 : 0, p.kind ?? 'post', p.posted_at ?? null]);
5759
+ }
5760
+ }
5761
+
5762
+ async listByComment(commentId) {
5763
+ return query(this.db, 'SELECT * FROM comment_postings WHERE comment_id = ? ORDER BY id', [commentId]);
5764
+ }
5765
+
5766
+ async listByGitHubReviewRow(rowId) {
5767
+ return query(this.db, 'SELECT * FROM comment_postings WHERE github_review_row_id = ? ORDER BY id', [rowId]);
5768
+ }
5769
+ }
5770
+
5657
5771
  class GitHubReviewRepository {
5658
5772
  /**
5659
5773
  * Create a new GitHubReviewRepository instance
@@ -5676,6 +5790,30 @@ class GitHubReviewRepository {
5676
5790
  * @param {string} [data.github_url] - GitHub URL for the review
5677
5791
  * @returns {Promise<Object>} Created github_review record
5678
5792
  */
5793
+ async findByGitHubIdentity(reviewId, { nodeId = null, databaseId = null } = {}) {
5794
+ return queryOne(this.db, `SELECT * FROM github_reviews WHERE review_id = ?
5795
+ AND ((? IS NOT NULL AND github_node_id = ?) OR (? IS NOT NULL AND github_review_id = ?))
5796
+ ORDER BY id DESC LIMIT 1`, [reviewId, nodeId, nodeId, databaseId, databaseId]);
5797
+ }
5798
+
5799
+ async upsertForSubmission(reviewId, data) {
5800
+ const identity = { nodeId: data.github_node_id, databaseId: data.github_review_id };
5801
+ const existing = await this.findByGitHubIdentity(reviewId, identity);
5802
+ if (!existing) return this.create(reviewId, data);
5803
+ const update = { ...data };
5804
+ if (!update.github_url) delete update.github_url;
5805
+ if (data.state === 'pending') delete update.body; // GitHub ignores a replacement draft body.
5806
+ await this.update(existing.id, update);
5807
+ // Preserve legacy duplicate rows, but never leave their linked postings in
5808
+ // an obsolete pending state after the identity has been submitted.
5809
+ await run(this.db, `UPDATE github_reviews SET state = ?, submitted_at = ?, event = ?
5810
+ WHERE review_id = ? AND id != ? AND
5811
+ ((? IS NOT NULL AND github_node_id = ?) OR (? IS NOT NULL AND github_review_id = ?))`,
5812
+ [data.state, data.submitted_at ?? null, data.event ?? null, reviewId, existing.id,
5813
+ identity.nodeId ?? null, identity.nodeId ?? null, identity.databaseId ?? null, identity.databaseId ?? null]);
5814
+ return this.getById(existing.id);
5815
+ }
5816
+
5679
5817
  async create(reviewId, data = {}) {
5680
5818
  const {
5681
5819
  github_review_id = null,
@@ -6409,6 +6547,7 @@ module.exports = {
6409
6547
  getDatabaseStatus,
6410
6548
  getSchemaVersion,
6411
6549
  CURRENT_SCHEMA_VERSION,
6550
+ runVersionedMigrations,
6412
6551
  getDbPath,
6413
6552
  WorktreeRepository,
6414
6553
  WorktreePoolRepository,
@@ -6427,7 +6566,10 @@ module.exports = {
6427
6566
  TourRepository,
6428
6567
  generateWorktreeId,
6429
6568
  migrateExistingWorktrees,
6569
+ shapePublication,
6570
+ CommentPostingRepository,
6430
6571
  // Exported for testing only
6572
+ _setupSchema: setupSchema,
6431
6573
  _MIGRATIONS: MIGRATIONS,
6432
6574
  MIGRATIONS
6433
6575
  };