优化: AI治理与性能(tool_failure_stats失败率统计命令 + git status仓库级缓存治cmd闪烁N²) + 销账
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
//! 依赖 audit/utils.rs 的 `truncate_chars` 做参数/结果截断,通过 `super::truncate_chars` 引用。
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use tauri::State;
|
||||
|
||||
use df_ai::ai_tools::RiskLevel;
|
||||
@@ -223,3 +224,206 @@ pub async fn list_tool_executions(
|
||||
has_more,
|
||||
})
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// AC-5 按工具失败率统计(诊断 IPC,替代 ad-hoc 查库)
|
||||
// ============================================================
|
||||
|
||||
/// 按工具失败率统计查询入参(前端透传,空值=全量)。
|
||||
///
|
||||
/// `from`:可选时间下限(millis,`requested_at >= from`),None = 全量。
|
||||
/// AC-5 诊断期 ad-hoc 查库无运行时统计机制,本命令提供聚合统计供审计面板/诊断查询。
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
pub struct ToolFailureStatsQuery {
|
||||
pub from: Option<i64>,
|
||||
}
|
||||
|
||||
/// 单工具执行统计(AC-5 失败率画像)。
|
||||
///
|
||||
/// `failed_rate` = failed / (completed + failed)——仅"真正执行过"的算成功率分母;
|
||||
/// rejected/skipped_retry 是用户/AI 决策非执行失败,不计分母,但单独计数展示。
|
||||
/// completed+failed=0(从未真正执行,如纯决策挂起)时 failed_rate=0。
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ToolFailureStat {
|
||||
pub tool_name: String,
|
||||
pub total: i64,
|
||||
pub completed: i64,
|
||||
pub failed: i64,
|
||||
pub rejected: i64,
|
||||
pub interrupted: i64,
|
||||
pub skipped_retry: i64,
|
||||
pub failed_rate: f64,
|
||||
}
|
||||
|
||||
/// 失败率统计聚合结果(跨工具汇总)。
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ToolFailureStats {
|
||||
/// 按 total 降序(使用最多的工具在前,面板聚焦高流量工具)
|
||||
pub stats: Vec<ToolFailureStat>,
|
||||
pub total_executions: i64,
|
||||
pub total_failed: i64,
|
||||
}
|
||||
|
||||
/// 把 `stats_by_tool` 的 (tool_name, status, count) 三元组聚合为 DTO(纯函数,命令 + 单测共用)。
|
||||
///
|
||||
/// 内存聚合(单条 GROUP BY 已按 tool,status 分组):HashMap 二次归并 → 每工具 status 分布,
|
||||
/// 计算 failed_rate(分母 completed+failed,四舍五入到 4 位小数防浮点长尾)。排序按 total 降序、
|
||||
/// tool_name 升序破平。`total_executions` = 全工具 total 之和,`total_failed` = failed 之和。
|
||||
fn aggregate_tool_stats(rows: Vec<(String, String, i64)>) -> ToolFailureStats {
|
||||
let mut by_tool: HashMap<String, HashMap<String, i64>> = HashMap::new();
|
||||
for (tool, status, cnt) in rows {
|
||||
*by_tool.entry(tool).or_default().entry(status).or_insert(0) += cnt;
|
||||
}
|
||||
let mut total_executions = 0i64;
|
||||
let mut total_failed = 0i64;
|
||||
let mut stats: Vec<ToolFailureStat> = by_tool
|
||||
.into_iter()
|
||||
.map(|(tool_name, m)| {
|
||||
let completed = m.get("completed").copied().unwrap_or(0);
|
||||
let failed = m.get("failed").copied().unwrap_or(0);
|
||||
let rejected = m.get("rejected").copied().unwrap_or(0);
|
||||
let interrupted = m.get("interrupted").copied().unwrap_or(0);
|
||||
let skipped_retry = m.get("skipped_retry").copied().unwrap_or(0);
|
||||
let total: i64 = m.values().sum();
|
||||
total_executions += total;
|
||||
total_failed += failed;
|
||||
let failed_rate = if completed + failed > 0 {
|
||||
((failed as f64 / (completed + failed) as f64) * 10_000.0).round() / 10_000.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
ToolFailureStat {
|
||||
tool_name,
|
||||
total,
|
||||
completed,
|
||||
failed,
|
||||
rejected,
|
||||
interrupted,
|
||||
skipped_retry,
|
||||
failed_rate,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
stats.sort_by(|a, b| b.total.cmp(&a.total).then_with(|| a.tool_name.cmp(&b.tool_name)));
|
||||
ToolFailureStats { stats, total_executions, total_failed }
|
||||
}
|
||||
|
||||
/// AC-5 运行时失败率统计:按工具聚合 ai_tool_executions 的 status 分布与失败率。
|
||||
///
|
||||
/// 只读诊断 IPC:`query.from` 可选时间下限(millis),默认全量。数据源 ai_tool_executions 表
|
||||
/// (GUI audit 模块写,已完成记录落盘)。口径:failed_rate = failed / (completed + failed);
|
||||
/// rejected/skipped_retry/interrupted 单独计数展示(不计失败率分母,属用户/AI 决策非执行失败)。
|
||||
#[tauri::command]
|
||||
pub async fn tool_failure_stats(
|
||||
state: State<'_, AppState>,
|
||||
query: Option<ToolFailureStatsQuery>,
|
||||
) -> Result<ToolFailureStats, String> {
|
||||
let from = query.and_then(|q| q.from);
|
||||
let rows = state
|
||||
.ai_tool_executions
|
||||
.stats_by_tool(from)
|
||||
.await
|
||||
.map_err(err_str)?;
|
||||
Ok(aggregate_tool_stats(rows))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use df_storage::db::Database;
|
||||
|
||||
/// 插入一条指定 tool/status/requested_at 的审计记录(测试构造数据用)。
|
||||
/// requested_at 传毫秒(与生产 `audit_tool_call` 落 now_millis() 同口径)。
|
||||
async fn insert_record(repo: &AiToolExecutionRepo, tool: &str, status: &str, t: i64) {
|
||||
repo.insert(AiToolExecutionRecord {
|
||||
id: new_id(),
|
||||
conversation_id: None,
|
||||
message_id: None,
|
||||
tool_call_id: new_id(),
|
||||
tool_name: tool.to_string(),
|
||||
arguments: "{}".to_string(),
|
||||
result: None,
|
||||
status: status.to_string(),
|
||||
risk_level: "low".to_string(),
|
||||
requested_at: t.to_string(),
|
||||
executed_at: None,
|
||||
decided_by: None,
|
||||
})
|
||||
.await
|
||||
.expect("测试数据插入应成功");
|
||||
}
|
||||
|
||||
/// failed_rate 分母口径:rejected/skipped_retry/interrupted 不计分母,单独计数。
|
||||
///
|
||||
/// read_file: 8 completed + 2 failed + 3 rejected → rate=2/10=0.2,rejected=3
|
||||
/// patch_file: 1 completed + 4 failed → rate=4/5=0.8
|
||||
/// run_command: 2 skipped_retry + 1 rejected + 1 interrupted(无 completed/failed)→ rate=0
|
||||
#[tokio::test]
|
||||
async fn tool_failure_stats_denominator_and_counts() {
|
||||
let db = Database::open_in_memory().await.expect("in-memory db 初始化失败");
|
||||
let repo = AiToolExecutionRepo::new(&db);
|
||||
let t = 2_000_000_000_000i64;
|
||||
for _ in 0..8 { insert_record(&repo, "read_file", "completed", t).await; }
|
||||
for _ in 0..2 { insert_record(&repo, "read_file", "failed", t).await; }
|
||||
for _ in 0..3 { insert_record(&repo, "read_file", "rejected", t).await; }
|
||||
insert_record(&repo, "patch_file", "completed", t).await;
|
||||
for _ in 0..4 { insert_record(&repo, "patch_file", "failed", t).await; }
|
||||
for _ in 0..2 { insert_record(&repo, "run_command", "skipped_retry", t).await; }
|
||||
insert_record(&repo, "run_command", "rejected", t).await;
|
||||
insert_record(&repo, "run_command", "interrupted", t).await;
|
||||
|
||||
let out = aggregate_tool_stats(repo.stats_by_tool(None).await.unwrap());
|
||||
assert_eq!(out.total_executions, 8 + 2 + 3 + 1 + 4 + 2 + 1 + 1);
|
||||
assert_eq!(out.total_failed, 6);
|
||||
|
||||
let rf = out.stats.iter().find(|s| s.tool_name == "read_file").unwrap();
|
||||
assert_eq!(rf.total, 13);
|
||||
assert_eq!(rf.completed, 8);
|
||||
assert_eq!(rf.failed, 2);
|
||||
assert_eq!(rf.rejected, 3);
|
||||
assert_eq!(rf.failed_rate, 0.2, "rejected 不计分母,failed/(completed+failed)=2/10");
|
||||
|
||||
let pf = out.stats.iter().find(|s| s.tool_name == "patch_file").unwrap();
|
||||
assert_eq!(pf.failed_rate, 0.8, "4/(1+4)=0.8");
|
||||
|
||||
let rc = out.stats.iter().find(|s| s.tool_name == "run_command").unwrap();
|
||||
assert_eq!(rc.completed + rc.failed, 0, "无真正执行记录");
|
||||
assert_eq!(rc.failed_rate, 0.0, "分母为 0 应归零");
|
||||
assert_eq!(rc.skipped_retry, 2);
|
||||
assert_eq!(rc.interrupted, 1);
|
||||
|
||||
// 排序:total 降序 → read_file(13) 应在 patch_file(5) 之前
|
||||
assert_eq!(out.stats[0].tool_name, "read_file");
|
||||
}
|
||||
|
||||
/// from 时间过滤:仅统计 requested_at >= from 的记录。
|
||||
///
|
||||
/// patch_file 追加一条更早(early)的 completed → from=late 时其不计入,
|
||||
/// completed 由 2 降为 1,failed_rate 由 4/6≈0.6667 变为 4/5=0.8。
|
||||
#[tokio::test]
|
||||
async fn tool_failure_stats_from_time_filter() {
|
||||
let db = Database::open_in_memory().await.expect("in-memory db 初始化失败");
|
||||
let repo = AiToolExecutionRepo::new(&db);
|
||||
let t_late = 2_000_000_000_000i64;
|
||||
let t_early = 1_000_000_000_000i64;
|
||||
|
||||
for _ in 0..8 { insert_record(&repo, "read_file", "completed", t_late).await; }
|
||||
for _ in 0..2 { insert_record(&repo, "read_file", "failed", t_late).await; }
|
||||
insert_record(&repo, "patch_file", "completed", t_late).await;
|
||||
for _ in 0..4 { insert_record(&repo, "patch_file", "failed", t_late).await; }
|
||||
insert_record(&repo, "patch_file", "completed", t_early).await;
|
||||
|
||||
// 全量:patch_file completed=2(early+late)
|
||||
let all = aggregate_tool_stats(repo.stats_by_tool(None).await.unwrap());
|
||||
let pf_all = all.stats.iter().find(|s| s.tool_name == "patch_file").unwrap();
|
||||
assert_eq!(pf_all.completed, 2);
|
||||
assert_eq!(pf_all.failed_rate, 0.6667, "4/(2+4)≈0.6667");
|
||||
|
||||
// from=t_late:early 不计,patch_file completed=1
|
||||
let late = aggregate_tool_stats(repo.stats_by_tool(Some(t_late)).await.unwrap());
|
||||
let pf_late = late.stats.iter().find(|s| s.tool_name == "patch_file").unwrap();
|
||||
assert_eq!(pf_late.completed, 1, "early 记录应被 from 过滤");
|
||||
assert_eq!(pf_late.failed_rate, 0.8, "4/(1+4)=0.8");
|
||||
assert_eq!(late.total_executions, 8 + 2 + 1 + 4, "不含 early 记录");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user