@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
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 = 51;
24
+ const CURRENT_SCHEMA_VERSION = 54;
25
25
 
26
26
  /**
27
27
  * Database schema SQL statements
@@ -90,6 +90,7 @@ const SCHEMA_SQL = {
90
90
  reasoning TEXT,
91
91
 
92
92
  status TEXT DEFAULT 'active' CHECK(status IN ('active', 'dismissed', 'adopted', 'submitted', 'draft', 'inactive')),
93
+ status_reason TEXT,
93
94
  adopted_as_id INTEGER,
94
95
  parent_id INTEGER,
95
96
  is_file_level INTEGER DEFAULT 0,
@@ -361,6 +362,29 @@ const SCHEMA_SQL = {
361
362
  FOREIGN KEY (review_id) REFERENCES reviews(id) ON DELETE CASCADE,
362
363
  FOREIGN KEY (parent_id) REFERENCES external_comments(id) ON DELETE SET NULL
363
364
  )
365
+ `,
366
+
367
+ global_settings: `
368
+ CREATE TABLE IF NOT EXISTS global_settings (
369
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
370
+ key TEXT NOT NULL UNIQUE,
371
+ value TEXT NOT NULL,
372
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
373
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
374
+ )
375
+ `,
376
+
377
+ // Global (user-level) reusable chat prompt snippets. No `repository` column
378
+ // now; repo-scoping later is a non-breaking ALTER TABLE ADD COLUMN + WHERE
379
+ // filter. MRU order comes from last_used_at (see ChatSnippetRepository).
380
+ chat_snippets: `
381
+ CREATE TABLE IF NOT EXISTS chat_snippets (
382
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
383
+ body TEXT NOT NULL,
384
+ last_used_at DATETIME,
385
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
386
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
387
+ )
364
388
  `
365
389
  };
366
390
 
@@ -415,7 +439,12 @@ const INDEX_SQL = [
415
439
  // External comments indexes (read-only mirror of GitHub/etc. PR review comments)
416
440
  'CREATE UNIQUE INDEX IF NOT EXISTS idx_external_comments_unique ON external_comments(review_id, source, external_id)',
417
441
  'CREATE INDEX IF NOT EXISTS idx_external_comments_anchor ON external_comments(review_id, file, line_end)',
418
- 'CREATE INDEX IF NOT EXISTS idx_external_comments_parent_lookup ON external_comments(review_id, source, in_reply_to_id)'
442
+ 'CREATE INDEX IF NOT EXISTS idx_external_comments_parent_lookup ON external_comments(review_id, source, in_reply_to_id)',
443
+ // Global settings (in-app overrides). key is already UNIQUE in the table, but
444
+ // the explicit index keeps parity with the test schema and lookup by key fast.
445
+ 'CREATE UNIQUE INDEX IF NOT EXISTS idx_global_settings_key ON global_settings(key)',
446
+ // Chat snippets MRU lookup by last_used_at.
447
+ 'CREATE INDEX IF NOT EXISTS idx_chat_snippets_last_used ON chat_snippets(last_used_at DESC)'
419
448
  ];
420
449
 
421
450
  /**
@@ -2237,6 +2266,60 @@ const MIGRATIONS = {
2237
2266
  console.log(' Table github_pr_cache absent; nothing to reindex');
2238
2267
  }
2239
2268
  console.log('Migration to schema version 51 complete');
2269
+ },
2270
+
2271
+ // Migration to version 52: add status_reason column to comments table.
2272
+ // Stores the human/agent-provided reason a suggestion was dismissed (and the
2273
+ // canonical cascade reason when an adopted comment is deleted). Nullable; the
2274
+ // invariant is that it is only ever populated on 'dismissed' suggestions.
2275
+ 52: (db) => {
2276
+ console.log('Running migration to schema version 52: add status_reason to comments...');
2277
+ if (!tableExists(db, 'comments')) {
2278
+ console.log(' comments table does not exist, skipping');
2279
+ console.log('Migration to schema version 52 complete');
2280
+ return;
2281
+ }
2282
+ if (!columnExists(db, 'comments', 'status_reason')) {
2283
+ db.prepare('ALTER TABLE comments ADD COLUMN status_reason TEXT').run();
2284
+ console.log(' Added status_reason column to comments');
2285
+ } else {
2286
+ console.log(' status_reason column already present; nothing to do');
2287
+ }
2288
+ console.log('Migration to schema version 52 complete');
2289
+ },
2290
+
2291
+ // Migration to version 53: add the global_settings table backing the
2292
+ // /settings page's in-app overrides. A single CREATE TABLE needs no
2293
+ // transaction; the tableExists guard keeps it idempotent if the migration
2294
+ // re-runs after a crash. The unique index is created separately (also
2295
+ // idempotent) so the schema matches SCHEMA_SQL + INDEX_SQL exactly.
2296
+ 53: (db) => {
2297
+ console.log('Running migration to schema version 53: Add global_settings table...');
2298
+ if (!tableExists(db, 'global_settings')) {
2299
+ db.exec(SCHEMA_SQL.global_settings);
2300
+ console.log(' Created global_settings table');
2301
+ } else {
2302
+ console.log(' Table global_settings already exists');
2303
+ }
2304
+ db.exec('CREATE UNIQUE INDEX IF NOT EXISTS idx_global_settings_key ON global_settings(key)');
2305
+ console.log('Migration to schema version 53 complete');
2306
+ },
2307
+
2308
+ // Migration to version 54: add the chat_snippets table backing the reusable
2309
+ // chat prompt snippets library. Fresh installs already create the table via
2310
+ // SCHEMA_SQL before migrations run, so the tableExists guard keeps this
2311
+ // idempotent. The index is created separately (also idempotent) so the schema
2312
+ // matches SCHEMA_SQL + INDEX_SQL exactly.
2313
+ 54: (db) => {
2314
+ console.log('Running migration to schema version 54: Add chat_snippets table...');
2315
+ if (!tableExists(db, 'chat_snippets')) {
2316
+ db.exec(SCHEMA_SQL.chat_snippets);
2317
+ console.log(' Created chat_snippets table');
2318
+ } else {
2319
+ console.log(' Table chat_snippets already exists');
2320
+ }
2321
+ db.exec('CREATE INDEX IF NOT EXISTS idx_chat_snippets_last_used ON chat_snippets(last_used_at DESC)');
2322
+ console.log('Migration to schema version 54 complete');
2240
2323
  }
2241
2324
  };
2242
2325
 
@@ -3156,6 +3239,83 @@ class WorktreePoolRepository {
3156
3239
  /**
3157
3240
  * RepoSettingsRepository class for managing per-repository AI settings
3158
3241
  */
3242
+ /**
3243
+ * Repository for global (non-repo) in-app setting overrides.
3244
+ *
3245
+ * Backs the /settings page. Values are JSON-encoded per row so booleans,
3246
+ * integers, and strings round-trip with their original type. Methods are
3247
+ * synchronous (better-sqlite3) because the GlobalSettingsService resolves
3248
+ * effective config synchronously at startup and per request.
3249
+ */
3250
+ class GlobalSettingsRepository {
3251
+ /**
3252
+ * @param {Database} db - Database instance
3253
+ */
3254
+ constructor(db) {
3255
+ this.db = db;
3256
+ }
3257
+
3258
+ /**
3259
+ * Get all overrides as a plain map of key -> parsed value. Rows whose value
3260
+ * is not valid JSON are skipped (defensive against manual DB edits).
3261
+ * @returns {Object}
3262
+ */
3263
+ getAll() {
3264
+ const rows = this.db.prepare('SELECT key, value FROM global_settings').all();
3265
+ const out = {};
3266
+ for (const row of rows) {
3267
+ try {
3268
+ out[row.key] = JSON.parse(row.value);
3269
+ } catch {
3270
+ // Skip malformed row rather than throw — resolution must not break.
3271
+ }
3272
+ }
3273
+ return out;
3274
+ }
3275
+
3276
+ /**
3277
+ * Get a single override's parsed value, or undefined if unset/malformed.
3278
+ * @param {string} key
3279
+ * @returns {*}
3280
+ */
3281
+ get(key) {
3282
+ const row = this.db.prepare('SELECT value FROM global_settings WHERE key = ?').get(key);
3283
+ if (!row) return undefined;
3284
+ try {
3285
+ return JSON.parse(row.value);
3286
+ } catch {
3287
+ return undefined;
3288
+ }
3289
+ }
3290
+
3291
+ /**
3292
+ * Upsert an override. Value is JSON-encoded.
3293
+ * @param {string} key
3294
+ * @param {*} value
3295
+ */
3296
+ set(key, value) {
3297
+ const now = new Date().toISOString();
3298
+ this.db.prepare(`
3299
+ INSERT INTO global_settings (key, value, created_at, updated_at)
3300
+ VALUES (?, ?, ?, ?)
3301
+ ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at
3302
+ `).run(key, JSON.stringify(value), now, now);
3303
+ }
3304
+
3305
+ /**
3306
+ * Delete a single override (no-op if absent).
3307
+ * @param {string} key
3308
+ */
3309
+ delete(key) {
3310
+ this.db.prepare('DELETE FROM global_settings WHERE key = ?').run(key);
3311
+ }
3312
+
3313
+ /** Delete all overrides. */
3314
+ deleteAll() {
3315
+ this.db.prepare('DELETE FROM global_settings').run();
3316
+ }
3317
+ }
3318
+
3159
3319
  class RepoSettingsRepository {
3160
3320
  /**
3161
3321
  * Create a new RepoSettingsRepository instance
@@ -3562,33 +3722,48 @@ class CommentRepository {
3562
3722
 
3563
3723
  /**
3564
3724
  * Update AI suggestion status and link to adopted comment
3725
+ *
3726
+ * status_reason invariant: a reason only ever lives on a 'dismissed'
3727
+ * suggestion. Restoring to 'active' and adopting both clear it.
3565
3728
  * @param {number} suggestionId - AI suggestion comment ID
3566
3729
  * @param {string} status - New status ('adopted', 'dismissed', 'active')
3567
3730
  * @param {number} [adoptedAsId] - ID of the user comment if adopted
3731
+ * @param {string|null} [reason] - Dismissal reason (only meaningful for 'dismissed'); trimmed empty → null
3568
3732
  * @returns {Promise<boolean>} True if updated successfully
3569
3733
  */
3570
- async updateSuggestionStatus(suggestionId, status, adoptedAsId = null) {
3734
+ async updateSuggestionStatus(suggestionId, status, adoptedAsId = null, reason = null) {
3571
3735
  const validStatuses = ['adopted', 'dismissed', 'active'];
3572
3736
  if (!validStatuses.includes(status)) {
3573
3737
  throw new Error('Invalid status. Must be "adopted", "dismissed", or "active"');
3574
3738
  }
3575
3739
 
3576
- // When restoring to active, clear adopted_as_id
3740
+ // When restoring to active, clear adopted_as_id and status_reason
3577
3741
  if (status === 'active') {
3578
3742
  const result = await run(this.db, `
3579
3743
  UPDATE comments
3580
- SET status = ?, adopted_as_id = NULL, updated_at = CURRENT_TIMESTAMP
3744
+ SET status = ?, adopted_as_id = NULL, status_reason = NULL, updated_at = CURRENT_TIMESTAMP
3581
3745
  WHERE id = ?
3582
3746
  `, [status, suggestionId]);
3583
3747
  return result.changes > 0;
3584
3748
  }
3585
3749
 
3586
- // For adopted/dismissed, optionally set adopted_as_id
3750
+ // Adopting clears any prior dismissal reason (invariant: reason only on 'dismissed').
3751
+ if (status === 'adopted') {
3752
+ const result = await run(this.db, `
3753
+ UPDATE comments
3754
+ SET status = ?, adopted_as_id = ?, status_reason = NULL, updated_at = CURRENT_TIMESTAMP
3755
+ WHERE id = ?
3756
+ `, [status, adoptedAsId, suggestionId]);
3757
+ return result.changes > 0;
3758
+ }
3759
+
3760
+ // Dismissing: persist the (already trimmed/normalized) reason, or null.
3761
+ const normalizedReason = typeof reason === 'string' && reason.trim() ? reason.trim() : null;
3587
3762
  const result = await run(this.db, `
3588
3763
  UPDATE comments
3589
- SET status = ?, adopted_as_id = ?, updated_at = CURRENT_TIMESTAMP
3764
+ SET status = ?, adopted_as_id = ?, status_reason = ?, updated_at = CURRENT_TIMESTAMP
3590
3765
  WHERE id = ?
3591
- `, [status, adoptedAsId, suggestionId]);
3766
+ `, [status, adoptedAsId, normalizedReason, suggestionId]);
3592
3767
 
3593
3768
  return result.changes > 0;
3594
3769
  }
@@ -3662,7 +3837,7 @@ class CommentRepository {
3662
3837
 
3663
3838
  // If this comment was adopted from an AI suggestion, dismiss the parent suggestion
3664
3839
  if (comment.parent_id) {
3665
- await this.updateSuggestionStatus(comment.parent_id, 'dismissed');
3840
+ await this.updateSuggestionStatus(comment.parent_id, 'dismissed', null, 'Adopted comment was deleted');
3666
3841
  dismissedSuggestionId = comment.parent_id;
3667
3842
  }
3668
3843
 
@@ -3711,7 +3886,8 @@ class CommentRepository {
3711
3886
  const placeholders = dismissedSuggestionIds.map(() => '?').join(',');
3712
3887
  await run(this.db, `
3713
3888
  UPDATE comments
3714
- SET status = 'dismissed', updated_at = CURRENT_TIMESTAMP
3889
+ SET status = 'dismissed', status_reason = 'Adopted comment was deleted',
3890
+ adopted_as_id = NULL, updated_at = CURRENT_TIMESTAMP
3715
3891
  WHERE id IN (${placeholders})
3716
3892
  `, dismissedSuggestionIds);
3717
3893
  }
@@ -3802,7 +3978,7 @@ class CommentRepository {
3802
3978
  SELECT
3803
3979
  id, ai_run_id, ai_level, ai_confidence,
3804
3980
  file, line_start, line_end, type, title, body,
3805
- reasoning, status, is_file_level, severity, created_at
3981
+ reasoning, status, status_reason, is_file_level, severity, created_at
3806
3982
  FROM comments
3807
3983
  WHERE ${conditions.join('\n AND ')}
3808
3984
  ORDER BY file, line_start
@@ -3937,6 +4113,7 @@ class ExternalCommentRepository {
3937
4113
  * @param {number} [mappedRow.diff_position]
3938
4114
  * @param {string} [mappedRow.commit_sha]
3939
4115
  * @param {boolean|number} [mappedRow.is_outdated]
4116
+ * @param {boolean|number} [mappedRow.is_file_level] - 1 for file-level (no line anchor)
3940
4117
  * @param {number} [mappedRow.original_line_start]
3941
4118
  * @param {number} [mappedRow.original_line_end]
3942
4119
  * @param {string} [mappedRow.original_commit_sha]
@@ -3964,6 +4141,7 @@ class ExternalCommentRepository {
3964
4141
  ? String(mappedRow.in_reply_to_id)
3965
4142
  : null;
3966
4143
  const isOutdated = mappedRow.is_outdated ? 1 : 0;
4144
+ const isFileLevel = mappedRow.is_file_level ? 1 : 0;
3967
4145
  const side = mappedRow.side === 'LEFT' ? 'LEFT' : (mappedRow.side === 'RIGHT' ? 'RIGHT' : null);
3968
4146
 
3969
4147
  // SQLite UPSERT. Update every column except id and parent_id; parent_id is
@@ -3974,9 +4152,9 @@ class ExternalCommentRepository {
3974
4152
  review_id, source, external_id, in_reply_to_id,
3975
4153
  external_url, author, author_url,
3976
4154
  file, side, line_start, line_end, diff_position, commit_sha,
3977
- is_outdated, original_line_start, original_line_end, original_commit_sha,
4155
+ is_outdated, is_file_level, original_line_start, original_line_end, original_commit_sha,
3978
4156
  body, external_created_at, synced_at
3979
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
4157
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
3980
4158
  ON CONFLICT(review_id, source, external_id) DO UPDATE SET
3981
4159
  in_reply_to_id = excluded.in_reply_to_id,
3982
4160
  external_url = excluded.external_url,
@@ -3989,6 +4167,7 @@ class ExternalCommentRepository {
3989
4167
  diff_position = excluded.diff_position,
3990
4168
  commit_sha = excluded.commit_sha,
3991
4169
  is_outdated = excluded.is_outdated,
4170
+ is_file_level = excluded.is_file_level,
3992
4171
  original_line_start = excluded.original_line_start,
3993
4172
  original_line_end = excluded.original_line_end,
3994
4173
  original_commit_sha = excluded.original_commit_sha,
@@ -4011,6 +4190,7 @@ class ExternalCommentRepository {
4011
4190
  mappedRow.diff_position ?? null,
4012
4191
  mappedRow.commit_sha ?? null,
4013
4192
  isOutdated,
4193
+ isFileLevel,
4014
4194
  mappedRow.original_line_start ?? null,
4015
4195
  mappedRow.original_line_end ?? null,
4016
4196
  mappedRow.original_commit_sha ?? null,
@@ -4410,7 +4590,8 @@ class ReviewRepository {
4410
4590
  const row = await queryOne(this.db, `
4411
4591
  SELECT id, pr_number, repository, status, review_id,
4412
4592
  created_at, updated_at, submitted_at, review_data, custom_instructions, summary,
4413
- review_type, local_path, local_head_sha
4593
+ review_type, local_path, local_head_sha,
4594
+ local_scope_start, local_scope_end, local_base_branch
4414
4595
  FROM reviews
4415
4596
  WHERE id = ?
4416
4597
  `, [id]);
@@ -5694,6 +5875,116 @@ class CouncilRepository {
5694
5875
  }
5695
5876
  }
5696
5877
 
5878
+ /**
5879
+ * ChatSnippetRepository class for managing reusable chat prompt snippets.
5880
+ *
5881
+ * Snippets are global (user-level) body-only prompts inserted into the chat
5882
+ * input. Modeled on CouncilRepository: MRU ordering via last_used_at and a
5883
+ * touchLastUsedAt() bump on every insert.
5884
+ */
5885
+ class ChatSnippetRepository {
5886
+ /**
5887
+ * Create a new ChatSnippetRepository instance
5888
+ * @param {Database} db - Database instance
5889
+ */
5890
+ constructor(db) {
5891
+ this.db = db;
5892
+ }
5893
+
5894
+ /**
5895
+ * Create a new snippet
5896
+ * @param {Object} snippetData - Snippet data
5897
+ * @param {string} snippetData.body - Snippet body text (non-empty)
5898
+ * @returns {Promise<Object>} Created snippet record
5899
+ */
5900
+ async create({ body }) {
5901
+ if (typeof body !== 'string' || !body.trim()) {
5902
+ throw new Error('body is required and must be a non-empty string');
5903
+ }
5904
+
5905
+ const result = await run(this.db, `
5906
+ INSERT INTO chat_snippets (body)
5907
+ VALUES (?)
5908
+ `, [body]);
5909
+
5910
+ return this.getById(result.lastID);
5911
+ }
5912
+
5913
+ /**
5914
+ * Get a snippet by ID
5915
+ * @param {number} id - Snippet ID
5916
+ * @returns {Promise<Object|null>} Snippet record or null
5917
+ */
5918
+ async getById(id) {
5919
+ const row = await queryOne(this.db, `
5920
+ SELECT id, body, last_used_at, created_at, updated_at
5921
+ FROM chat_snippets
5922
+ WHERE id = ?
5923
+ `, [id]);
5924
+
5925
+ return row || null;
5926
+ }
5927
+
5928
+ /**
5929
+ * List all snippets in MRU order (most recently used first)
5930
+ * @returns {Promise<Array<Object>>} Array of snippet records
5931
+ */
5932
+ async list() {
5933
+ return query(this.db, `
5934
+ SELECT id, body, last_used_at, created_at, updated_at
5935
+ FROM chat_snippets
5936
+ ORDER BY last_used_at DESC NULLS LAST, updated_at DESC
5937
+ `);
5938
+ }
5939
+
5940
+ /**
5941
+ * Update a snippet's body
5942
+ * @param {number} id - Snippet ID
5943
+ * @param {Object} updates - Fields to update
5944
+ * @param {string} updates.body - New body text (non-empty)
5945
+ * @returns {Promise<boolean>} True if record was updated (snippet exists)
5946
+ */
5947
+ async update(id, { body }) {
5948
+ if (typeof body !== 'string' || !body.trim()) {
5949
+ throw new Error('body is required and must be a non-empty string');
5950
+ }
5951
+
5952
+ const result = await run(this.db, `
5953
+ UPDATE chat_snippets
5954
+ SET body = ?, updated_at = CURRENT_TIMESTAMP
5955
+ WHERE id = ?
5956
+ `, [body, id]);
5957
+
5958
+ return result.changes > 0;
5959
+ }
5960
+
5961
+ /**
5962
+ * Update the last_used_at timestamp for a snippet (for MRU tracking)
5963
+ * @param {number} id - Snippet ID
5964
+ * @returns {Promise<boolean>} True if record was updated (snippet exists)
5965
+ */
5966
+ async touchLastUsedAt(id) {
5967
+ const result = await run(this.db, `
5968
+ UPDATE chat_snippets SET last_used_at = CURRENT_TIMESTAMP WHERE id = ?
5969
+ `, [id]);
5970
+
5971
+ return result.changes > 0;
5972
+ }
5973
+
5974
+ /**
5975
+ * Delete a snippet
5976
+ * @param {number} id - Snippet ID
5977
+ * @returns {Promise<boolean>} True if record was deleted (snippet existed)
5978
+ */
5979
+ async delete(id) {
5980
+ const result = await run(this.db, `
5981
+ DELETE FROM chat_snippets WHERE id = ?
5982
+ `, [id]);
5983
+
5984
+ return result.changes > 0;
5985
+ }
5986
+ }
5987
+
5697
5988
  /**
5698
5989
  * ContextFileRepository class for managing context file range records.
5699
5990
  * Context files allow pinning specific line ranges from non-diff files
@@ -6018,6 +6309,7 @@ module.exports = {
6018
6309
  WorktreeRepository,
6019
6310
  WorktreePoolRepository,
6020
6311
  RepoSettingsRepository,
6312
+ GlobalSettingsRepository,
6021
6313
  ReviewRepository,
6022
6314
  CommentRepository,
6023
6315
  ExternalCommentRepository,
@@ -6025,6 +6317,7 @@ module.exports = {
6025
6317
  AnalysisRunRepository,
6026
6318
  GitHubReviewRepository,
6027
6319
  CouncilRepository,
6320
+ ChatSnippetRepository,
6028
6321
  ContextFileRepository,
6029
6322
  HunkSummaryRepository,
6030
6323
  TourRepository,
@@ -144,7 +144,23 @@ async function fetchComments({ client, owner, repo, pull_number }) {
144
144
  /**
145
145
  * Map a raw GitHub review-comment API row to an `external_comments` row.
146
146
  *
147
- * Host-aware anchoring (the `options.isAltHost` switch):
147
+ * File-level comments (`subject_type === 'file'`): GitHub's REST "list review
148
+ * comments" returns comments attached to a whole file (not a line) with
149
+ * `subject_type: 'file'` but STILL anchored at `line: 1, position: 1`. Taking
150
+ * those coordinates at face value renders them as ordinary line-1 annotations
151
+ * (the bug this handles). So when `subject_type === 'file'` we flag the row
152
+ * `is_file_level = 1` and null EVERY line anchor (`line_start`, `line_end`,
153
+ * `diff_position`, `original_line_start`, `original_line_end`) — a file-level
154
+ * comment has no line to anchor to. `is_outdated` stays 0: with no line anchor
155
+ * there is nothing to outdate, and this deliberately bypasses the position/line
156
+ * `is_outdated` derivation below (whose null-position branch would otherwise
157
+ * mark it outdated). The frontend renders `is_file_level` rows in the per-file
158
+ * comments zone above the diff. Alt hosts may omit `subject_type` entirely —
159
+ * then `isFileLevel` is false and behaviour is unchanged (acceptable; those
160
+ * hosts don't distinguish file-level comments in this response shape).
161
+ *
162
+ * Host-aware anchoring (the `options.isAltHost` switch), applied only to
163
+ * NON-file-level rows:
148
164
  * - **github.com (default, `isAltHost` falsy)**: `position` is the signal
149
165
  * for "outdated". A null `position` means the comment no longer maps to
150
166
  * the current diff, so the current-anchor fields are nulled and
@@ -192,13 +208,31 @@ function mapComment(apiRow, options = {}) {
192
208
 
193
209
  const user = apiRow.user || null;
194
210
  const isAltHost = options.isAltHost === true;
211
+ // A file-level comment (GitHub sets subject_type='file') has no line anchor,
212
+ // even though GitHub still reports line:1/position:1. Alt hosts may omit
213
+ // subject_type — then this is false and the row is treated line-anchored,
214
+ // exactly as before.
215
+ const isFileLevel = apiRow.subject_type === 'file';
195
216
 
196
217
  let line_start;
197
218
  let line_end;
198
219
  let diff_position;
199
220
  let is_outdated;
221
+ // Preferred original anchor; nulled for file-level rows (no line at all).
222
+ let original_line_start = apiRow.original_start_line ?? apiRow.original_line ?? null;
223
+ let original_line_end = apiRow.original_line ?? null;
200
224
 
201
- if (isAltHost) {
225
+ if (isFileLevel) {
226
+ // No line anchor of any kind: strip current AND original coordinates so
227
+ // the frontend routes the row to the per-file comments zone instead of a
228
+ // diff line. is_outdated stays 0 — there is no line to have gone stale.
229
+ line_start = null;
230
+ line_end = null;
231
+ diff_position = null;
232
+ is_outdated = 0;
233
+ original_line_start = null;
234
+ original_line_end = null;
235
+ } else if (isAltHost) {
202
236
  // Alt-host: `line` is the authoritative signal. `position` is unreliable
203
237
  // (alt-hosts leave it null even for current comments), so we never null
204
238
  // out a good `line` based on it. `diff_position` is carried through —
@@ -238,9 +272,9 @@ function mapComment(apiRow, options = {}) {
238
272
  diff_position,
239
273
  commit_sha: apiRow.commit_id ?? null,
240
274
  is_outdated,
241
- original_line_start:
242
- apiRow.original_start_line ?? apiRow.original_line ?? null,
243
- original_line_end: apiRow.original_line ?? null,
275
+ is_file_level: isFileLevel ? 1 : 0,
276
+ original_line_start,
277
+ original_line_end,
244
278
  original_commit_sha: apiRow.original_commit_id ?? null,
245
279
  body: apiRow.body ?? '',
246
280
  external_created_at: apiRow.created_at ?? null,