Compare commits

..

17 Commits

Author SHA1 Message Date
jiantw83 db83b3ee11 Merge pull request 'feat: 加入階段四:排除問題過濾' (#73) from feat/refactor/kiro/fix-2 into feat/refactor/kiro/fix-1
Reviewed-on: jiantw83/code-review#73
2026-05-12 02:17:01 +00:00
AI Review Bot b17a8757b2 chore: update ai-review findings [skip ci] 2026-05-12 02:16:42 +00:00
jiantw83 58479c7c6d refactor: rename Step4 to AI 排除問題過濾 2026-05-12 02:15:47 +00:00
AI Review Bot 0de87d6629 chore: update ai-review findings [skip ci] 2026-05-12 02:14:49 +00:00
jiantw83 bc914c401c docs: update TODO stage4 description and fix findings filename typo 2026-05-12 02:13:38 +00:00
jiantw83 27e471d9e0 feat: add AI false positive filtering in Step4 2026-05-12 02:12:26 +00:00
jiantw83 f1c21beed5 fix: use includes matching for exclusions location and suggestion 2026-05-12 02:10:47 +00:00
AI Review Bot 3c3019d1ab chore: update ai-review findings [skip ci] 2026-05-12 02:07:34 +00:00
jiantw83 41a8fe100f fix: clone repo before Step3/4 to read findings and exclusions from head branch 2026-05-12 02:06:24 +00:00
AI Review Bot c1c00449af chore: update ai-review findings [skip ci] 2026-05-12 01:49:18 +00:00
jiantw83 ef3654b091 chore: add exclusions for Rex false positive on git.js token handling 2026-05-12 01:48:00 +00:00
jiantw83 5d0c9fd691 docs: mark all TODO stages complete 2026-05-12 01:47:52 +00:00
jiantw83 5860588588 fix: align flow with README, add Step4 exclusions filter, fix step numbers 2026-05-12 01:47:43 +00:00
jiantw83 53a7ec7a3e refactor: reorganize TODO stages for clarity and accuracy in workflow steps
Co-authored-by: Copilot <copilot@github.com>
2026-05-12 01:47:43 +00:00
jiantw83 42241c5000 refactor: update processing steps in README for clarity and accuracy 2026-05-12 01:47:43 +00:00
jiantw83 1a12ec4e2e refactor: remove outdated AI Code configurations for Kilo, Roo, Cline, Continue, and Kade 2026-05-12 01:47:43 +00:00
AI Review Bot a90886e924 chore: update ai-review findings [skip ci] 2026-05-12 01:13:53 +00:00
5 changed files with 101 additions and 66 deletions
+18 -53
View File
@@ -1,72 +1,37 @@
[
{
"level": "critical",
"role": "Leo",
"location": "app/config.js:7",
"suggestion": "請確保 EXCLUSIONS_PATH 的值不包含敏感資訊,並使用環境變數來管理敏感資料。",
"is_new": true
},
{
"level": "critical",
"role": "Maya",
"location": "app/git.js:1",
"suggestion": "缺少對 commitAndPush 函數的單元測試,應該為其添加測試以確保其正確性。",
"is_new": true
},
{
"level": "warning",
"role": "Zara",
"location": "app/findings.js:40",
"suggestion": "在 applyExclusions 函數中,使用 filter 和 some 方法的組合可能會導致效能問題,特別是當 findings 和 exclusions 的數量很大時。考慮使用更有效的資料結構(如 HashSet)來加速查詢。",
"is_new": true
},
{
"level": "warning",
"role": "Rex",
"location": "app/findings.js:40",
"suggestion": "在讀取排除問題檔案時,建議加入對檔案內容的驗證,以防止不正確的格式導致潛在的錯誤或漏洞。",
"location": "app/git.js:14",
"suggestion": "請避免將 GIT_TOKEN 直接寫入腳本中,應使用安全的秘密管理工具來管理這些敏感資訊.",
"is_new": true
},
{
"level": "info",
"level": "warning",
"role": "Leo",
"location": "app/findings.js:1",
"suggestion": "建議在檔案開頭添加檔案的功能描述,以提高可讀性。",
"location": "app/git.js:14",
"suggestion": "建議在 cloneRepo 函數中增加對於 GIT_TOKEN 的安全性處理,避免敏感資訊洩漏.",
"is_new": true
},
{
"level": "info",
"role": "Leo",
"location": "app/findings.js:40",
"suggestion": "建議為 loadExclusions 函式添加詳細的文件說明,以便未來的開發者能更快理解其功能。",
"is_new": true
},
{
"level": "info",
"level": "warning",
"role": "Leo",
"location": "app/findings.js:93",
"suggestion": "建議為 deduplicateWithAI 函式添加詳細的文件說明,以便未來的開發者能更快理解其功能。",
"is_new": true
"suggestion": "建議在 loadExclusions 函式中增加對於 JSON 格式的驗證,確保讀取的資料符合預期格式,避免潛在的錯誤.",
"is_new": false
},
{
"level": "warning",
"role": "Leo",
"location": "app/findings.js:40",
"suggestion": "在 applyExclusions 函式中,建議增加對於 findings 和 exclusions 參數的有效性檢查,以提高程式的健壯性.",
"is_new": false
},
{
"level": "info",
"role": "Aria",
"location": "README.md:10",
"suggestion": "建議在每個步驟後添加簡短的描述,以提高可讀性和理解性。",
"is_new": true
},
{
"level": "info",
"role": "Aria",
"location": "app/config.js:7",
"suggestion": "建議在常數命名中使用全大寫字母和底線分隔,以提高可讀性。",
"is_new": true
},
{
"level": "info",
"role": "Maya",
"location": "app/main.js:50",
"suggestion": "建議在發佈 comment 失敗時,記錄具體的錯誤原因,以便後續調試。",
"role": "Leo",
"location": "README.md",
"suggestion": "建議在 README 中增加對於新功能(如排除問題過濾)的詳細說明,以便未來的維護者能快速了解其功能.",
"is_new": true
}
]
+4 -4
View File
@@ -15,14 +15,14 @@
- 驗收:log 中能看到 deduplication/resolution confirmation 成功或失敗(如 402),降級時有「保留所有問題」等明確訊息。
- 完成
## 階段四:排除問題過濾
- 目標:讀取排除問題檔案,過濾 PR 問題表格中不需要處理的問題
- 驗收:log 中能看到排除問題檔案讀取成功或不存在的訊息,以及過濾後 findings 數量變化
## 階段四:AI 排除問題過濾
- 目標:讀取排除問題檔案exclusions.json)進行規則過濾,並呼叫 AI 判斷剩餘問題是否為誤報或不適用,兩層過濾後產生最終問題清單
- 驗收:log 中能看到排除問題檔案讀取成功或不存在的訊息、規則過濾數量變化,以及「AI 誤報過濾: N -> M 筆」或降級訊息
- 完成
## 階段五:findings 寫入與 comment 發布
- 目標:findings.jsonl 正確寫入,comment 發布順序正確(舊問題→非嚴重→嚴重),每步有 log。
- 驗收:log 中能看到 findings 寫入、comment sync 的詳細訊息與順序。
- 驗收:log 中能看到 findings.json 寫入、comment sync 的詳細訊息與順序。
- 完成
## 階段六:記憶區 commit/push 與錯誤處理
+36 -2
View File
@@ -124,9 +124,43 @@ export function applyExclusions(findings, exclusions) {
const before = findings.length;
const filtered = findings.filter(f => !exclusions.some(ex =>
(!ex.role || ex.role === f.role) &&
(!ex.location || ex.location === f.location) &&
(!ex.suggestion || String(f.suggestion).startsWith(String(ex.suggestion).slice(0, 50)))
(!ex.location || String(f.location).includes(ex.location)) &&
(!ex.suggestion || String(f.suggestion).includes(String(ex.suggestion).slice(0, 20)))
));
console.log(` 排除過濾: ${before} -> ${filtered.length} 筆(排除 ${before - filtered.length} 筆)`);
return filtered;
}
/**
* 呼叫 AI 判斷哪些問題是誤報或不需處理,回傳需保留的 findings
* 失敗時降級回傳原始 findings
*/
export async function filterFalsePositivesWithAI(findings) {
if (findings.length === 0) return findings;
const systemPrompt = `你是一位資深程式碼審查專家,負責判斷審查問題是否為誤報或不需處理。
給你一份問題清單(JSON 陣列),每筆包含 level、role、location、suggestion。
請移除以下類型的問題:
1. 誤報:問題描述與實際程式碼不符(例如:程式碼已正確使用環境變數或 secrets,卻被標記為硬編碼敏感資料)
2. 不適用:問題在此專案情境下不需處理(例如:CI/CD action 本來就需要透過環境變數傳遞 token)
只回傳需要保留的問題 JSON 陣列,不要有其他文字。`;
const userContent = `請判斷以下問題清單,移除誤報或不需處理的問題:\n\n${JSON.stringify(findings, null, 2)}`;
try {
const result = await chatJSON(systemPrompt, userContent);
if (Array.isArray(result)) {
console.log(` AI 誤報過濾: ${findings.length} -> ${result.length}`);
return result;
}
throw new Error('AI 回傳非陣列');
} catch (e) {
const status = e.response?.status;
if (status === 402 || status === 429) {
console.log(` ⚠️ AI 誤報過濾失敗(${status} 額度/限流),降級:保留所有問題`);
} else {
console.log(` ⚠️ AI 誤報過濾失敗(${e.message}),降級:保留所有問題`);
}
return findings;
}
}
+28
View File
@@ -14,6 +14,34 @@ function makeRunner(spawn) {
};
}
/**
* Clone PR head branch to workspace/repo (idempotent)
*/
export function cloneRepo(workspace, _spawnSync = spawnSync) {
const run = makeRunner(_spawnSync);
const baseUrl = GITEA_SERVER_URL.replace(/\/$/, '');
const remoteUrl = `${baseUrl}/${GITEA_REPOSITORY}.git`;
const repoDir = path.join(workspace, 'repo');
const askpassScript = path.join(workspace, '.git-askpass.sh');
fs.writeFileSync(askpassScript, '#!/bin/sh\necho "$GIT_TOKEN"\n', { mode: 0o700 });
const credEnv = { ...process.env, GIT_ASKPASS: askpassScript, GIT_USERNAME: 'x-token', GIT_TOKEN: GITEA_TOKEN };
try {
if (!fs.existsSync(repoDir)) {
run(['clone', '--depth=1', '--branch', PR_HEAD_BRANCH, remoteUrl, repoDir], workspace, credEnv);
console.log(` ✅ repo cloned to ${repoDir}`);
} else {
run(['fetch', 'origin', PR_HEAD_BRANCH], repoDir, credEnv);
run(['checkout', PR_HEAD_BRANCH], repoDir);
console.log(` ✅ repo already exists, fetched latest`);
}
} finally {
try { fs.unlinkSync(askpassScript); } catch {}
}
return repoDir;
}
export async function commitAndPush(workspace, _spawnSync = spawnSync) {
const run = makeRunner(_spawnSync);
+15 -7
View File
@@ -1,9 +1,9 @@
import { GITEA_REPOSITORY, PR_NUMBER, PR_HEAD_BRANCH, PR_BASE_BRANCH, getLLMConfig } from './config.js';
import { loadRoles, getRoleIntro } from './roles.js';
import { getPRDiff, postComment } from './gitea.js';
import { analyzeWithRole, loadOldFindings, mergeFindings, sortByLevel, deduplicateWithAI, loadExclusions, applyExclusions } from './findings.js';
import { analyzeWithRole, loadOldFindings, mergeFindings, sortByLevel, deduplicateWithAI, loadExclusions, applyExclusions, filterFalsePositivesWithAI } from './findings.js';
import { saveFindings, postOldFindingsComment, postNewNonCriticalComment, postNewCriticalComments } from './comments.js';
import { commitAndPush } from './git.js';
import { cloneRepo, commitAndPush } from './git.js';
const WORKSPACE = process.env.GITHUB_WORKSPACE || '/workspace';
@@ -65,7 +65,14 @@ async function main() {
// Step3: 讀取舊 findings,合併去重(含 AI 語意去重)
console.log('\n🔀 Step3: Findings 合併');
const oldFindings = loadOldFindings(WORKSPACE);
// Clone repo 以讀取舊 findings 與排除清單
let repoDir;
try {
repoDir = cloneRepo(WORKSPACE);
} catch (e) {
console.log(` ⚠️ clone repo 失敗(繼續執行): ${e.message}`);
}
const oldFindings = loadOldFindings(repoDir || WORKSPACE);
const mergedFindings = mergeFindings(oldFindings, newFindings);
console.log(` Step3 merged findings total=${mergedFindings.length}`);
@@ -74,10 +81,11 @@ async function main() {
const sorted = sortByLevel(deduped);
console.log(` Step3b dedup findings total=${sorted.length} (critical=${sorted.filter(f=>f.level==='critical').length} warning=${sorted.filter(f=>f.level==='warning').length} info=${sorted.filter(f=>f.level==='info').length})`);
// Step4: 讀取排除問題檔案,過濾 PR 問題表格
console.log('\n🚫 Step4: 排除問題過濾');
const exclusions = loadExclusions(WORKSPACE);
const filtered = applyExclusions(sorted, exclusions);
// Step4: 讀取排除問題檔案,過濾 PR 問題表格,並請 AI 判斷誤報
console.log('\n🚫 Step4: AI 排除問題過濾');
const exclusions = loadExclusions(repoDir || WORKSPACE);
const ruleFiltered = applyExclusions(sorted, exclusions);
const filtered = await filterFalsePositivesWithAI(ruleFiltered);
console.log(` Step4 完成: findings total=${filtered.length}`);
// Step5: 寫入 findings.json,依序發布 comment