test(ai-review): 補使用量統計 NaN 安全測試並排除微優化誤報 #47

Merged
admin merged 15 commits from develop into master 2026-06-23 09:35:54 +00:00
5 changed files with 654 additions and 543 deletions
Showing only changes of commit 3763908b39 - Show all commits
+49 -1
View File
@@ -97,7 +97,7 @@
{ {
"role": "Leo", "role": "Leo",
"location": "app/llm.js", "location": "app/llm.js",
"suggestion": "Authorization 標頭已有 provider !== \u0027ollama\u0027 判斷,不會無條件加入,已正確處理" "suggestion": "Authorization 標頭已有 provider !== 'ollama' 判斷,不會無條件加入,已正確處理"
}, },
{ {
"role": "Rogue", "role": "Rogue",
@@ -518,5 +518,53 @@
"role": "Maya", "role": "Maya",
"original_finding": "parseBotReviewComment 的測試沒有驗證解析失敗時回傳 null 的行為。", "original_finding": "parseBotReviewComment 的測試沒有驗證解析失敗時回傳 null 的行為。",
"reason": "誤判,測試已存在。'returns null for free-form human comments' 已斷言自由格式留言、空字串、null 皆回傳 null。" "reason": "誤判,測試已存在。'returns null for free-form human comments' 已斷言自由格式留言、空字串、null 皆回傳 null。"
},
{
"location": "app/usage.js:146",
"role": "Rogue",
"original_finding": "在 recordRateLimit 中頻繁呼叫 lowerCaseKeys,對每個請求的 headers 複製與轉換,增加記憶體分配開銷;建議改用不分大小寫存取避免複製。",
"reason": "過度設計/非熱路徑。recordRateLimit 每次 LLM 回應只呼叫一次(一輪審查約 13 次),headers 物件小,複製成本可忽略;改用不分大小寫存取器反而增加複雜度,效益不成比例。"
},
{
"location": "app/usage.test.js:250",
"role": "Leo",
"original_finding": "測試案例直接引用 describe 外層定義的 `usage` 變數,若被其他測試修改會造成測試間隱性耦合;建議改用區域變數或工廠函式生成測試資料。",
"reason": "誤判。該 `const usage` 為不可變的測試夾具,所有測試只讀不寫、未曾被任何案例 mutate,不存在跨測試耦合;新增的邊界測試已各自使用區域 usage 物件。為一個唯讀 fixture 改工廠函式屬過度設計。"
},
{
"location": "app/usage.test.js:254",
"role": "Mage",
"original_finding": "測試情境雖處理了 NaN,但未涵蓋 limit 為 0 或負數等極端數值,可能輸出 Infinity 或負百分比。",
"reason": "誤判,測試已存在。`returns null percent for non-finite or non-positive quota limits` 與 `does not output Infinity/NaN/negative percent for invalid quota numbers` 已涵蓋 limit 為 0/負數/InfinityNaNundefined,皆驗證輸出「無法計算」、不含 NaN/Infinity/負百分比。"
},
{
"location": "app/usage.test.js:200",
"role": "Bard",
"original_finding": "邊界測試迴圈 [0, -5, Infinity, NaN, undefined] 混用不同型別,建議拆成多個測試以提升可讀性。",
"reason": "主觀風格/不採納。該迴圈已對每個值帶入 assert 訊息(如 `quota.limit=${limit} 應算不出百分比`),測試失敗時可直接定位是哪個輸入;用單一資料驅動迴圈反而精簡、不易遺漏案例。"
},
{
"location": "app/usage.js:216",
"role": "Maya",
"original_finding": "isFinitePositive 沒有對 null/undefined 做顯式檢查,未來可能被錯誤複用。",
"reason": "誤判。`isFinitePositive` 對 nullNumber(null)=0 → 0>0 為 false)與 undefinedNumber(undefined)=NaN → Number.isFinite 為 false)皆正確回傳 false,本就涵蓋這些輸入;函式名已表明只接受有限正數,無須再加冗餘檢查。"
},
{
"location": "app/usage.test.js:200",
"role": "Bard",
"original_finding": "建議將測試案例拆分,例如分開測試「非數字類型」、「負數」與「邊界值(Infinity/NaN)」,這樣在測試失敗時能更快速釐清是哪種輸入類型導致的問題。",
"reason": "AI 對話收斂判定為誤報(問題在最新程式碼中不成立或不適用)"
},
{
"location": "app/usage.js:213",
"role": "Rogue",
"original_finding": "建議將 `Number(n)` 的結果暫存起來,或在進入條件式後立即將值賦值給一個變數並重複使用,減少重複轉換的成本。",
"reason": "AI 對話收斂判定為誤報(問題在最新程式碼中不成立或不適用)"
},
{
"location": "app/usage.js:215",
"role": "Assassin",
"original_finding": "建議在 `isFinitePositive` 內部補上對 `null` 或 `undefined` 的顯式檢查,並直接在外部嚴格限制輸入類型或使用更明確的檢查(例如 `typeof n === 'number'`)取代隱式轉型,避免隱式轉型帶來的不可控副作用。",
"reason": "AI 對話收斂判定為誤報(問題在最新程式碼中不成立或不適用)"
} }
] ]
+4 -12
View File
@@ -1,18 +1,10 @@
[ [
{ {
"level": "warning", "level": "info",
"role": "Maya", "role": "Maya",
"location": "app/usage.test.js:240", "location": "app/usage.test.js:217",
"problem": "`formatUsageStatsLine` 測試案例中,僅驗證了單一平台的格式,缺失了當 `quota` 或 `rate` 資料缺失或包含無效數字(如 `NaN`)時的處理測試。", "problem": "測試案例 `returns null percent when rate.remaining is null/undefined` 僅驗證了 remaining 為 null/undefined,但未驗證當 rate.limit 為 null/undefined 時的情況。雖然這可能由 `calculatePercent` 內部處理,但針對 `resolveRemainingPercent` 這一層級的整合測試仍不完整。",
"suggestion": "補充針對 `quota` 或 `rate` 傳入異常資料(如 `limit: NaN`)的測試,驗證 `formatUsageStatsLine` 是否能產生安全的預設文字,而非輸出 `NaN` 或破壞版面。", "suggestion": "建議補上一個測試案例,明確測試當 `rate.limit` 為 null/undefined 時,`resolveRemainingPercent` 的行為是否符合預期。",
"is_new": true
},
{
"level": "warning",
"role": "Rogue",
"location": "app/usage.js:146",
"problem": "在 `recordRateLimit` 中頻繁呼叫 `lowerCaseKeys`,這會對每個請求的 headers 進行複製與轉換,增加記憶體分配開銷。",
"suggestion": "建議直接存取 headers 時改用不區分大小寫的存取函式,避免複製整個物件。",
"is_new": true "is_new": true
} }
] ]
+3 -1
View File
@@ -115,7 +115,9 @@ async function main() {
// Step7 過濾:套用排除規則 + 防守方 AI 誤報裁決 // Step7 過濾:套用排除規則 + 防守方 AI 誤報裁決
step('Step7', '排除規則與誤報過濾'); step('Step7', '排除規則與誤報過濾');
if (reconcile.excludedFindings.length > 0) { if (reconcile.excludedFindings.length > 0) {
appendExclusions(WORKSPACE, reconcile.excludedFindings, repoDir || WORKSPACE); // 以 repoDir 為主(即將提交回去的來源分支副本),WORKSPACE 為鏡像;
// 順序須與下方 loadExclusions 一致,否則會讀到空的 WORKSPACE 而把既有排除規則覆蓋掉。
appendExclusions(repoDir || WORKSPACE, reconcile.excludedFindings, WORKSPACE);
} }
const exclusions = loadExclusions(repoDir || WORKSPACE, repoState, WORKSPACE); const exclusions = loadExclusions(repoDir || WORKSPACE, repoState, WORKSPACE);
input(`待過濾 ${sorted.length} 筆;排除規則 ${exclusions.length}`); input(`待過濾 ${sorted.length} 筆;排除規則 ${exclusions.length}`);
+26 -9
View File
@@ -211,23 +211,40 @@ function round1(n) {
const RATE_KIND_LABEL = { tokens: 'token', requests: '次數' }; const RATE_KIND_LABEL = { tokens: 'token', requests: '次數' };
/**
* 計算「剩餘百分比」= remaining / limit × 100。
* limit 或 remaining 為 nullundefinedNaNInfinity,或 limit ≤ 0 時回 null
* 避免算出 Infinity%/NaN%/負百分比或除以零。
*/
function calculatePercent(remaining, limit) {
if (remaining == null || limit == null) return null;
const rem = Number(remaining);
const lim = Number(limit);
if (!Number.isFinite(rem) || !Number.isFinite(lim) || lim <= 0) return null;
return round1((rem / lim) * 100);
}
/** /**
* 計算「剩餘可用百分比」,依優先序擇一: * 計算「剩餘可用百分比」,依優先序擇一:
* 1. 帳號額度(quota 有上限)→ 剩餘 credits / 上限; * 1. 帳號額度(quota 有有效上限)→ 剩餘 credits / 上限;
* 2. 速率配額(rate limit header)→ 當前視窗剩餘 / 上限; * 2. 速率配額(rate limit header,有有效上限)→ 當前視窗剩餘 / 上限;
* 皆無法取得時回傳 { percent: null, reason }。 * 上限或剩餘為無效值(null/0/負數/NaN/Infinity)時跳過計算,落到 { percent: null, reason }。
*/ */
export function resolveRemainingPercent(quota, rate) { export function resolveRemainingPercent(quota, rate) {
if (quota?.available && quota.limit != null && Number(quota.limit) > 0) { if (quota?.available && quota.limit != null) {
const limit = Number(quota.limit); const limit = Number(quota.limit);
const remaining = quota.remaining == null ? limit - num(quota.used) : Number(quota.remaining); const remaining = quota.remaining == null ? limit - num(quota.used) : Number(quota.remaining);
return { percent: round1((remaining / limit) * 100), basis: '帳號額度', remaining, limit, unit: quota.currency || '' }; const percent = calculatePercent(remaining, limit);
if (percent != null) {
return { percent, basis: '帳號額度', remaining, limit, unit: quota.currency || '' };
} }
if (rate?.hasData && Number(rate.limit) > 0) { }
const limit = Number(rate.limit); if (rate?.hasData) {
const remaining = Number(rate.remaining); const percent = calculatePercent(rate.remaining, rate.limit);
if (percent != null) {
const kindLabel = RATE_KIND_LABEL[rate.kind] || rate.kind; const kindLabel = RATE_KIND_LABEL[rate.kind] || rate.kind;
return { percent: round1((remaining / limit) * 100), basis: `速率配額(當前視窗,${kindLabel}`, remaining, limit, unit: '' }; return { percent, basis: `速率配額(當前視窗,${kindLabel}`, remaining: Number(rate.remaining), limit: Number(rate.limit), unit: '' };
}
} }
let reason; let reason;
if (quota?.available && quota.limit == null) reason = '帳號額度無上限,無法計算百分比'; if (quota?.available && quota.limit == null) reason = '帳號額度無上限,無法計算百分比';
+52
View File
@@ -196,6 +196,32 @@ describe('resolveRemainingPercent', () => {
assert.match(pct.reason, /無上限/); assert.match(pct.reason, /無上限/);
}); });
it('returns null percent for non-finite or non-positive quota limits', () => {
for (const limit of [0, -5, Infinity, NaN, undefined]) {
const pct = resolveRemainingPercent({ available: true, used: 0, limit, remaining: limit, currency: 'USD' }, null);
assert.equal(pct.percent, null, `quota.limit=${limit} 應算不出百分比`);
}
});
it('returns null percent for non-finite or non-positive rate limits', () => {
for (const limit of [0, -1, Infinity, NaN]) {
const pct = resolveRemainingPercent({ available: false, reason: 'x' }, { hasData: true, remaining: limit, limit, kind: 'tokens' });
assert.equal(pct.percent, null, `rate.limit=${limit} 應算不出百分比`);
}
});
it('returns null percent when rate.remaining is null/undefined', () => {
for (const remaining of [null, undefined]) {
const pct = resolveRemainingPercent({ available: false, reason: 'x' }, { hasData: true, remaining, limit: 200000, kind: 'tokens' });
assert.equal(pct.percent, null, `rate.remaining=${remaining} 應算不出百分比`);
}
});
it('returns null percent when limit is finite but remaining is non-finite', () => {
const pct = resolveRemainingPercent({ available: true, used: 0, limit: 100, remaining: Infinity, currency: 'USD' }, null);
assert.equal(pct.percent, null);
});
it('does not divide by zero when quota.limit is 0', () => { it('does not divide by zero when quota.limit is 0', () => {
const pct = resolveRemainingPercent({ available: true, used: 5, limit: 0, currency: 'USD' }, { hasData: false }); const pct = resolveRemainingPercent({ available: true, used: 5, limit: 0, currency: 'USD' }, { hasData: false });
assert.equal(pct.percent, null); // limit > 0 守衛擋掉除以零 assert.equal(pct.percent, null); // limit > 0 守衛擋掉除以零
@@ -244,4 +270,30 @@ describe('formatUsageStatsLine', () => {
const line = formatUsageStatsLine('ollama', 'llama3', usage, { available: false, reason: '本地服務,無帳號額度概念' }, { hasData: false }); const line = formatUsageStatsLine('ollama', 'llama3', usage, { available: false, reason: '本地服務,無帳號額度概念' }, { hasData: false });
assert.match(line, /;剩餘可用: 無法計算(本地服務,無帳號額度概念)/); assert.match(line, /;剩餘可用: 無法計算(本地服務,無帳號額度概念)/);
}); });
it('produces safe text (no NaN) when quota/rate carry invalid numbers', () => {
// quota.limit 為 NaN、rate.limit 為 NaN → 不應算出百分比、不得輸出 NaN
const line = formatUsageStatsLine('openai', 'm', usage,
{ available: true, used: 5, limit: NaN, currency: 'USD' },
{ hasData: true, remaining: NaN, limit: NaN, kind: 'tokens' });
assert.doesNotMatch(line, /NaN/);
assert.match(line, /剩餘可用: 無法計算/);
});
it('does not output Infinity/NaN/negative percent for invalid quota numbers', () => {
const ownUsage = { calls: 1, promptTokens: 1, completionTokens: 1, totalTokens: 2 };
for (const limit of [Infinity, 0, -5, NaN]) {
const line = formatUsageStatsLine('openai', 'm', ownUsage, { available: true, used: 0, limit, remaining: limit, currency: 'USD' }, null);
assert.doesNotMatch(line, /NaN|Infinity|-\d+%/);
assert.match(line, /剩餘可用: 無法計算/);
}
});
it('falls back to a valid rate percent when only the quota limit is invalid', () => {
const ownUsage = { calls: 1, promptTokens: 1, completionTokens: 1, totalTokens: 2 };
const line = formatUsageStatsLine('openai', 'm', ownUsage,
{ available: true, used: 0, limit: Infinity, currency: 'USD' }, // quota 無效
{ hasData: true, remaining: 150000, limit: 200000, kind: 'tokens' }); // rate 有效 → 75%
assert.match(line, /剩餘可用: 75%(速率配額/);
});
}); });