job51-gitlab-cr-node-jt-1 2.9.1 → 2.9.3

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.
@@ -1,9 +1,9 @@
1
1
  # GitLab Code Review AI Tool 技术文档
2
2
 
3
- **项目名称**: job51-gitlab-cr-node
4
- **当前版本**: 2.8.7
5
- **作者**: tao.jing
6
- **最后更新**: 2026-04-28
3
+ **项目名称**: job51-gitlab-cr-node
4
+ **当前版本**: 2.8.8
5
+ **作者**: tao.jing
6
+ **最后更新**: 2026-05-09
7
7
  **项目地址**: https://gitdev.51job.com/51jobweb/ai-agent
8
8
 
9
9
  ---
@@ -27,6 +27,56 @@
27
27
 
28
28
  ## 版本历史
29
29
 
30
+ ### v2.8.8 (2026-05-09)
31
+
32
+ **当前版本**: 幻觉检测增强 - 部分幻觉问题过滤机制
33
+
34
+ **新增功能**:
35
+ - **部分幻觉问题过滤机制**:在汇总报告中只显示正常发布的问题,不包含幻觉问题内容
36
+ - 问题现象:当一个diff块有多个问题时,部分问题被检测为幻觉(如文件路径不匹配),但汇总报告中仍包含所有问题的完整内容,包括幻觉问题的内容
37
+ - 根本原因:
38
+ - 幻觉检测逻辑只判断是否跳过评论发布
39
+ - 但审查结果的 `reportContent` 包含所有问题的完整报告
40
+ - 汇总报告直接使用原始 `reportContent`,导致幻觉问题内容也被显示
41
+ - 影响分析:
42
+ - 用户在汇总报告中看到幻觉问题的内容,但这些问题的评论并未发布到GitLab
43
+ - 产生信息不一致,降低汇总报告的可信度
44
+ - 解决方案:
45
+ - **统计机制**:记录每个问题的发布状态(幻觉/正常)
46
+ - **过滤机制**:根据实际发布的问题索引重新生成过滤后的报告内容
47
+ - **传递机制**:通过返回值将过滤后的内容传递给汇总报告生成流程
48
+ - 实现细节:
49
+ - 新增 `publishedProblemIndexes` 数组:记录正常发布的问题索引(从1开始)
50
+ - 新增 `generateFilteredReport` 方法:根据问题索引生成过滤后的报告内容
51
+ - 修改 `postSingleCommentToGitLab` 方法:
52
+ - 统计幻觉问题数量和正常发布数量
53
+ - 根据统计结果返回不同状态:
54
+ - 所有问题都是幻觉:返回 `{ hallucination_detected: true, filtered_report_content: '' }`
55
+ - 部分问题正常:返回 `{ hallucination_detected: false, filtered_report_content: '过滤后的报告' }`
56
+ - 所有问题正常:返回 `{ hallucination_detected: false, filtered_report_content: '完整报告' }`
57
+ - 修改 `processBlock` 方法:接收过滤后的报告内容并更新 `blockObj.review_result.reportContent`
58
+ - 修改 `collectAllReviewReports` 方法:过滤掉完全幻觉的结果
59
+ - 处理场景:
60
+ - **场景1:所有问题都是幻觉**
61
+ - 示例:问题1、2、3文件路径都不匹配
62
+ - 行为:标记为幻觉,过滤掉整个结果,不出现在汇总报告中
63
+ - **场景2:部分问题正常**
64
+ - 示例:问题1幻觉(文件路径不匹配),问题2、3正常发布
65
+ - 行为:不标记为幻觉,但汇总报告只显示问题2、3的内容(过滤掉问题1)
66
+ - 实现:调用 `generateFilteredReport` 提取问题2、3内容
67
+ - **场景3:所有问题都正常**
68
+ - 示例:问题1、2、3都正常发布
69
+ - 行为:汇总报告显示完整内容
70
+ - 效果:
71
+ - 汇总报告与GitLab发布的评论保持一致
72
+ - 用户看到的汇总报告内容都是实际发布的评论内容
73
+ - 提高汇总报告的准确性和可信度
74
+ - 修复文件:
75
+ - `index.js:863-966`:统计机制和返回值增强
76
+ - `index.js:125-147`:接收过滤后的内容并更新报告
77
+ - `index.js:201-223`:汇总报告过滤逻辑
78
+ - `index.js:775-807`:新增 `generateFilteredReport` 方法
79
+
30
80
  ### v2.8.7 (2026-04-29)
31
81
 
32
82
  **当前版本**: 审查规则增强 - 文件路径格式保留与上下文行限制
@@ -1300,7 +1350,9 @@ POST /projects/{projectId}/merge_requests/{iid}/notes
1300
1350
 
1301
1351
  ## 10. 错误处理与容错机制
1302
1352
 
1303
- ### 10.1 AI 幻觉检测与过滤 (v2.8.4)
1353
+ ### 10.1 AI 幻觉检测与过滤
1354
+
1355
+ #### 10.1.1 文件路径幻觉检测 (v2.8.4)
1304
1356
 
1305
1357
  **幻觉问题表现**:
1306
1358
  - AI 在生成审查报告时可能编造错误的文件路径
@@ -1317,18 +1369,97 @@ if (problemInfo.new_path && diff_info.new_path &&
1317
1369
  }
1318
1370
  ```
1319
1371
 
1320
- **处理策略**:
1321
- | 检测到幻觉时 | 处理动作 | 结果 |
1322
- |------------|----------|------|
1323
- | 文件路径不匹配 | 输出警告日志 + `continue`跳过 | **不发布评论**,避免误导 |
1324
- | 行号超出范围 | 降级为一般讨论 | 发布到 MR(无行号定位) |
1325
- | GitLab API失败 | 降级为一般讨论 | 确保评论可见 |
1326
-
1327
- **效果**:
1328
- - ✅ 防止错误路径的评论发布
1329
- - ✅ 提高审查准确性
1330
- - ✅ 减少无效评论
1331
- - ✅ 提升用户信任度
1372
+ #### 10.1.2 部分幻觉过滤机制 (v2.8.8)
1373
+
1374
+ **新增问题**:
1375
+ - 一个diff块有多个问题时,部分问题被检测为幻觉,但汇总报告仍包含所有问题的完整内容
1376
+ - 用户在汇总报告中看到幻觉问题的内容,但这些评论并未发布到GitLab
1377
+ - 信息不一致,降低汇总报告的可信度
1378
+
1379
+ **解决方案**:
1380
+
1381
+ 1. **统计机制**:记录每个问题的发布状态
1382
+ ```javascript
1383
+ // 统计幻觉问题数量和正常发布数量
1384
+ let hallucinationCount = 0;
1385
+ let publishedCount = 0;
1386
+ const publishedProblemIndexes = []; // 记录正常发布的问题索引(从1开始)
1387
+
1388
+ for (let i = 0; i < allLineInfo.length; i++) {
1389
+ if (文件路径不匹配) {
1390
+ hallucinationCount++;
1391
+ continue; // 跳过发布
1392
+ }
1393
+ // 正常发布
1394
+ publishedProblemIndexes.push(i + 1);
1395
+ publishedCount++;
1396
+ }
1397
+ ```
1398
+
1399
+ 2. **过滤机制**:根据问题索引生成过滤后的报告
1400
+ ```javascript
1401
+ // generateFilteredReport 方法:根据问题索引生成过滤后的报告
1402
+ generateFilteredReport(fullReport, publishedProblemIndexes) {
1403
+ // 提取报告标题(如 "## 🤖 AI 代码审查结果")
1404
+ // 提取指定索引的问题内容
1405
+ // 组合成过滤后的报告
1406
+ }
1407
+ ```
1408
+
1409
+ 3. **传递机制**:返回过滤后的内容
1410
+ ```javascript
1411
+ // 只有问题都被检测为幻觉时,才标记整个结果为幻觉
1412
+ const isAllHallucination = hallucinationCount > 0 && hallucinationCount === allLineInfo.length;
1413
+
1414
+ if (isAllHallucination) {
1415
+ return { hallucination_detected: true, filtered_report_content: '' };
1416
+ }
1417
+
1418
+ // 如果部分问题正常发布,生成过滤后的报告内容
1419
+ let filteredReportContent = fullReportContent;
1420
+ if (publishedCount > 0 && publishedCount < allLineInfo.length) {
1421
+ filteredReportContent = this.generateFilteredReport(fullReportContent, publishedProblemIndexes);
1422
+ }
1423
+
1424
+ return {
1425
+ hallucination_detected: false,
1426
+ filtered_report_content: filteredReportContent
1427
+ };
1428
+ ```
1429
+
1430
+ **处理策略** (v2.8.8更新):
1431
+ | 检测场景 | 处理动作 | 汇总报告显示 |
1432
+ |---------|----------|------------|
1433
+ | 所有问题都是幻觉 | 标记 `hallucination_detected=true` | **完全过滤**,不显示 |
1434
+ | 部分问题正常 | 生成过滤后的报告内容 | 只显示正常问题内容 |
1435
+ | 所有问题正常 | 返回完整报告内容 | 显示完整内容 |
1436
+
1437
+ **效果对比**:
1438
+
1439
+ | 版本 | 行为 | 效果 |
1440
+ |------|------|------|
1441
+ | v2.8.4-v2.8.7 | 所有问题都标记为幻觉 → 过滤整个结果 | ❌ 如果有正常问题,正常问题也被过滤 |
1442
+ | v2.8.8 | 只过滤幻觉问题内容,保留正常问题内容 | ✅ 汇总报告与GitLab评论一致 |
1443
+
1444
+ **示例**:
1445
+
1446
+ 假设审查结果有3个问题:
1447
+ - 问题1:文件路径不匹配(幻觉)
1448
+ - 问题2:正常发布
1449
+ - 问题3:正常发布
1450
+
1451
+ **v2.8.7处理**:
1452
+ ```
1453
+ hallucination_detected = true
1454
+ 汇总报告:不显示该diff块
1455
+ ```
1456
+
1457
+ **v2.8.8处理**:
1458
+ ```
1459
+ hallucination_detected = false
1460
+ filtered_report_content = 只包含问题2和问题3的内容
1461
+ 汇总报告:显示问题2和问题3
1462
+ ```
1332
1463
 
1333
1464
  ### 10.2 AI 审查失败处理
1334
1465
 
package/index.js CHANGED
@@ -119,14 +119,27 @@ class GitLabCodeReviewer {
119
119
  const filePath = diffObject.new_path || diffObject.old_path || '';
120
120
  this.metrics.recordBlockReviewed(reviewTime, problemsCount, hasSeriousProblems, diffSize, filePath);
121
121
 
122
+ // 初始化幻觉标记
123
+ let hallucinationDetected = false;
124
+
122
125
  // 检查审查结果中是否包含严重问题,只有包含严重问题才发布评论
123
126
  if (blockObj.review_result && blockObj.review_result.reportContent && blockObj.review_result.reportContent.includes('严重问题')) {
124
127
  // 立即发布评论
125
- await this.postSingleCommentToGitLab(projectId, mergeRequestIid, {
128
+ const commentResult = await this.postSingleCommentToGitLab(projectId, mergeRequestIid, {
126
129
  diff_info: blockObj,
127
130
  block_index: blockObj.block_index,
128
131
  review_result: blockObj.review_result,
129
132
  });
133
+ // 如果发布过程中检测到幻觉,记录标记和过滤后的报告
134
+ if (commentResult) {
135
+ if (commentResult.hallucination_detected) {
136
+ hallucinationDetected = true;
137
+ }
138
+ // 如果有过滤后的报告内容,使用它替代原始报告内容
139
+ if (commentResult.filtered_report_content) {
140
+ blockObj.review_result.reportContent = commentResult.filtered_report_content;
141
+ }
142
+ }
130
143
  } else {
131
144
  debugLog(`该块不包含严重问题,跳过评论发布: ${blockObj.new_path || blockObj.old_path}#${blockObj.block_index}`);
132
145
  }
@@ -136,6 +149,7 @@ class GitLabCodeReviewer {
136
149
  block_index: blockObj.block_index,
137
150
  review_result: blockObj.review_result,
138
151
  temp_file_path: tmpFileName,
152
+ hallucination_detected: hallucinationDetected,
139
153
  };
140
154
  } catch (error) {
141
155
  throw error;
@@ -763,6 +777,50 @@ ${allReportsText}
763
777
  return fullReport;
764
778
  }
765
779
 
780
+ /**
781
+ * 根据实际发布的问题索引生成过滤后的报告内容
782
+ * @param {string} fullReport 完整的 REPORT 内容
783
+ * @param {Array<number>} publishedProblemIndexes 正常发布的问题索引数组(从 1 开始)
784
+ * @returns {string} 过滤后的报告内容,只包含正常发布的问题
785
+ */
786
+ generateFilteredReport(fullReport, publishedProblemIndexes) {
787
+ // 提取报告的标题部分(如 "## 🤖 AI 代码审查结果" 和 "### 🔴 严重问题")
788
+ const lines = fullReport.split('\n');
789
+ const headerLines = [];
790
+ let foundFirstProblem = false;
791
+
792
+ for (const line of lines) {
793
+ // 提取标题行(如 "## 🤖 AI 代码审查结果"、"### 🔴 严重问题" 等)
794
+ if (line.startsWith('##') || line.startsWith('###')) {
795
+ if (!line.includes('问题')) {
796
+ headerLines.push(line);
797
+ }
798
+ }
799
+ // 遇到第一个问题块时停止提取标题
800
+ if (line.match(/\*\*问题\s*\d+\*\*/)) {
801
+ foundFirstProblem = true;
802
+ break;
803
+ }
804
+ }
805
+
806
+ // 提取每个正常发布的问题内容
807
+ const filteredProblemBlocks = [];
808
+ for (const problemIndex of publishedProblemIndexes) {
809
+ const problemContent = this.extractSingleProblemReport(fullReport, problemIndex);
810
+ if (problemContent) {
811
+ filteredProblemBlocks.push(problemContent);
812
+ }
813
+ }
814
+
815
+ // 组合过滤后的报告:标题 + 问题内容
816
+ let filteredReport = headerLines.join('\n');
817
+ if (filteredProblemBlocks.length > 0) {
818
+ filteredReport += '\n\n' + filteredProblemBlocks.join('\n\n');
819
+ }
820
+
821
+ return filteredReport;
822
+ }
823
+
766
824
  /**
767
825
  * 获取合并请求的最新版本信息
768
826
  * @param {number} projectId GitLab项目ID
@@ -849,12 +907,19 @@ ${allReportsText}
849
907
  if (allLineInfo.length === 0) {
850
908
  await this.createGeneralDiscussion(projectId, mergeRequestIid, file_path_with_line, fullReportContent);
851
909
  debugLog(`评论已发布到文件 ${file_path_with_line} (无法解析行号)`);
852
- return;
910
+ return { hallucination_detected: false };
853
911
  }
912
+
913
+ // 统计幻觉问题数量和正常发布数量
914
+ let hallucinationCount = 0;
915
+ let publishedCount = 0;
916
+ const publishedProblemIndexes = []; // 记录正常发布的问题索引(从1开始)
917
+
854
918
  for (let i = 0; i < allLineInfo.length; i++) {
855
919
  const problemInfo = allLineInfo[i];
856
920
  debugLog(`处理第 ${i + 1}/${allLineInfo.length} 个问题:文件=${problemInfo.new_path}, 行号=${problemInfo.new_line}`);
857
- const singleProblemContent = this.extractSingleProblemReport(fullReportContent, i + 1);
921
+ const problemIndex = i + 1; // 问题索引从1开始
922
+ const singleProblemContent = this.extractSingleProblemReport(fullReportContent, problemIndex);
858
923
 
859
924
  // 构建目标行号,并验证行号是否在 diff 块范围内
860
925
  let targetLine = null;
@@ -863,9 +928,8 @@ ${allReportsText}
863
928
  // 验证文件路径是否匹配当前 diff 块(检测AI幻觉)
864
929
  if (problemInfo.new_path && diff_info.new_path &&
865
930
  problemInfo.new_path !== diff_info.new_path) {
866
- console.warn(`⚠️ 检测到AI幻觉:第 ${i + 1} 个问题的文件路径 ${problemInfo.new_path} 与当前 diff 块文件 ${diff_info.new_path} 不匹配,跳过该问题的评论发布`);
867
- // 标记该结果存在幻觉问题,后续生成汇总报告时会过滤掉
868
- result.hallucination_detected = true;
931
+ console.warn(`⚠️ 检测到AI幻觉:第 ${problemIndex} 个问题的文件路径 ${problemInfo.new_path} 与当前 diff 块文件 ${diff_info.new_path} 不匹配,跳过该问题的评论发布`);
932
+ hallucinationCount++;
869
933
  continue; // 直接跳过,不发布评论
870
934
  }
871
935
 
@@ -910,8 +974,8 @@ ${allReportsText}
910
974
 
911
975
  if (!targetLine) {
912
976
  // 无法解析行号或行号超出范围,标记为幻觉问题,跳过发布
913
- console.warn(`⚠️ 检测到AI幻觉:第 ${i + 1} 个问题 ${skipReason || '无法解析行号'},该问题可能报告在上下文行或删除行,跳过发布`);
914
- result.hallucination_detected = true;
977
+ console.warn(`⚠️ 检测到AI幻觉:第 ${problemIndex} 个问题 ${skipReason || '无法解析行号'},该问题可能报告在上下文行或删除行,跳过发布`);
978
+ hallucinationCount++;
915
979
  continue;
916
980
  }
917
981
 
@@ -928,17 +992,41 @@ ${allReportsText}
928
992
  };
929
993
  try {
930
994
  await this.createDiffDiscussion(projectId, mergeRequestIid, payload);
931
- debugLog(`第 ${i + 1} 个问题的评论已发布到 ${problemInfo.new_path}#${problemInfo.new_line}`);
995
+ debugLog(`第 ${problemIndex} 个问题的评论已发布到 ${problemInfo.new_path}#${problemInfo.new_line}`);
932
996
  this.metrics.recordCommentPublished();
997
+ publishedCount++;
998
+ publishedProblemIndexes.push(problemIndex); // 记录正常发布的问题索引
933
999
  } catch (error) {
934
1000
  debugLog(`GitLab API 错误详情:${JSON.stringify(error.response?.data || error.message)}`);
935
- console.error(`发布第 ${i + 1} 个问题的评论到 ${problemInfo.new_path}#${problemInfo.new_line} 失败,改用一般讨论:`, error.message);
1001
+ console.error(`发布第 ${problemIndex} 个问题的评论到 ${problemInfo.new_path}#${problemInfo.new_line} 失败,改用一般讨论:`, error.message);
936
1002
  await this.createGeneralDiscussion(projectId, mergeRequestIid, file_path_with_line, singleProblemContent);
937
- debugLog(`第 ${i + 1} 个问题的评论已发布 (作为一般讨论)`);
1003
+ debugLog(`第 ${problemIndex} 个问题的评论已发布 (作为一般讨论)`);
938
1004
  this.metrics.recordCommentPublished();
1005
+ publishedCount++;
1006
+ publishedProblemIndexes.push(problemIndex); // 记录正常发布的问题索引
939
1007
  }
940
1008
  }
941
- debugLog(`所有 ${allLineInfo.length} 个问题的评论已发布完成`);
1009
+ debugLog(`所有 ${allLineInfo.length} 个问题的评论已发布完成,其中幻觉问题 ${hallucinationCount} 个,正常发布 ${publishedCount} 个`);
1010
+
1011
+ // 只有所有问题都被检测为幻觉时,才标记整个结果为幻觉
1012
+ const isAllHallucination = hallucinationCount > 0 && hallucinationCount === allLineInfo.length;
1013
+ if (isAllHallucination) {
1014
+ console.warn(`⚠️ 该 diff 块的所有 ${allLineInfo.length} 个问题都被检测为AI幻觉,将过滤该结果`);
1015
+ return { hallucination_detected: true, filtered_report_content: '' };
1016
+ }
1017
+
1018
+ // 如果部分问题正常发布,生成过滤后的报告内容(只包含正常发布的问题)
1019
+ let filteredReportContent = fullReportContent;
1020
+ if (publishedCount > 0 && publishedCount < allLineInfo.length) {
1021
+ console.warn(`⚠️ 该 diff 块有 ${hallucinationCount} 个问题被检测为AI幻觉,汇总报告中只保留 ${publishedCount} 个正常发布的问题`);
1022
+ filteredReportContent = this.generateFilteredReport(fullReportContent, publishedProblemIndexes);
1023
+ }
1024
+
1025
+ // 返回幻觉标记和过滤后的报告内容
1026
+ return {
1027
+ hallucination_detected: false,
1028
+ filtered_report_content: filteredReportContent
1029
+ };
942
1030
  } catch (error) {
943
1031
  console.error('发布单个评论到 GitLab 失败:', error.message);
944
1032
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "job51-gitlab-cr-node-jt-1",
3
- "version": "2.9.1",
3
+ "version": "2.9.3",
4
4
  "description": "GitLab merge request code review tool with AI-powered analysis and project context support",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -1,10 +0,0 @@
1
- {
2
- "env": {
3
- "ANTHROPIC_AUTH_TOKEN": "sk-436f005eeece4cf7b339bd18162c8a76",
4
- "ANTHROPIC_BASE_URL": "https://dashscope.aliyuncs.com/apps/anthropic",
5
- "API_TIMEOUT_MS": "3000000",
6
- "ANTHROPIC_MODEL": "qwen3.5-plus",
7
- "ANTHROPIC_SMALL_FAST_MODEL":"qwen3.5-plus",
8
- "SLASH_COMMAND_TOOL_CHAR_BUDGET": "50000"
9
- }
10
- }