agentguard-tool 1.0.5__py3-none-any.whl

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.
@@ -0,0 +1,328 @@
1
+ Metadata-Version: 2.4
2
+ Name: agentguard-tool
3
+ Version: 1.0.5
4
+ Summary: AI-native quality gate for agent-generated code — scan, audit, auto-fix, trend
5
+ Author: Hermes Labs
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Hermes Labs
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/weijinsheng123456/agentguard
29
+ Project-URL: Issues, https://github.com/weijinsheng123456/agentguard/issues
30
+ Classifier: Development Status :: 4 - Beta
31
+ Classifier: Intended Audience :: Developers
32
+ Classifier: Topic :: Software Development :: Quality Assurance
33
+ Classifier: Topic :: Software Development :: Testing
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Programming Language :: Python :: 3
36
+ Classifier: Programming Language :: Python :: 3.10
37
+ Classifier: Programming Language :: Python :: 3.11
38
+ Classifier: Programming Language :: Python :: 3.12
39
+ Requires-Python: >=3.10
40
+ Description-Content-Type: text/markdown
41
+ License-File: LICENSE
42
+ Requires-Dist: pyyaml>=6.0
43
+ Requires-Dist: ruff>=0.4.0
44
+ Dynamic: license-file
45
+
46
+ <p align="center">
47
+ <img src="https://img.shields.io/badge/AgentGuard-1.0.0-8A2BE2?style=for-the-badge&logo=python&logoColor=white" width="300" alt="AgentGuard">
48
+ </p>
49
+
50
+ <p align="center">
51
+ <em>AI-native quality gate for agent-generated code.</em>
52
+ <br>
53
+ Scan · Audit · Auto-fix · Track trends
54
+ </p>
55
+
56
+ <p align="center">
57
+ <a href="#features">Features</a> •
58
+ <a href="#quick-start">Quick Start</a> •
59
+ <a href="#commands">Commands</a> •
60
+ <a href="#demo">Demo</a> •
61
+ <a href="#rules">Rules</a> •
62
+ <a href="#write-custom-rules">Custom Rules</a>
63
+ </p>
64
+
65
+ <p align="center">
66
+ <a href="https://github.com/weijinsheng123456/agentguard/actions">
67
+ <img src="https://img.shields.io/github/actions/workflow/status/weijinsheng123456/agentguard/ci.yml?branch=main&label=CI&logo=github&color=success" alt="CI">
68
+ </a>
69
+ <a href="https://pypi.org/project/agentguard/">
70
+ <img src="https://img.shields.io/pypi/v/agentguard?label=PyPI&logo=pypi&color=blue" alt="PyPI">
71
+ </a>
72
+ <a href="LICENSE">
73
+ <img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License">
74
+ </a>
75
+ <a href="https://github.com/weijinsheng123456/agentguard/stargazers">
76
+ <img src="https://img.shields.io/github/stars/weijinsheng123456/agentguard?style=social" alt="Stars">
77
+ </a>
78
+ <a href="https://www.python.org/">
79
+ <img src="https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-blue?logo=python" alt="Python">
80
+ </a>
81
+ <a href="https://github.com/weijinsheng123456/agentguard/blob/main/CHANGELOG.md">
82
+ <img src="https://img.shields.io/badge/version-1.0.0-8A2BE2" alt="Version">
83
+ </a>
84
+ </p>
85
+
86
+ ---
87
+
88
+ # 🚀 Install in 3 seconds
89
+
90
+ ```bash
91
+ pip install agentguard-tool
92
+ cd your-project
93
+ gate run
94
+ ```
95
+
96
+ Done. AgentGuard scans your code, detects AI-specific issues (hardcoded secrets, unsafe APIs, hallucinations), auto-fixes what it can, and tracks quality trends over time.
97
+
98
+ ---
99
+
100
+ ## Why AgentGuard?
101
+
102
+ Traditional linters like SonarQube and CodeQL were built for **human-written code**. AI agents write code differently — they hallucinate module names, leave placeholder comments, generate giant functions, and introduce patterns that human linters miss.
103
+
104
+ **AgentGuard is built for the age of AI-generated code.**
105
+
106
+ It understands the patterns, pitfalls, and security risks specific to code written by LLMs. It runs as a CI-ready CLI, a pre-commit hook, or a daily cron.
107
+
108
+ ---
109
+
110
+ ## Features
111
+
112
+ ### 🔍 AI-Specific Detection (3 rules)
113
+ | Rule | Severity | What It Finds |
114
+ |------|----------|---------------|
115
+ | `unsafe_api` | 🚫 BLOCKER | `eval()`, `exec()`, `os.system()`, `subprocess(shell=True)`, `pickle.loads()` |
116
+ | `secret_leak` | 🚫 BLOCKER | Hardcoded API keys, tokens, passwords in source code |
117
+ | `ai_hallucination` | ℹ️ INFO | AI placeholder comments, suspicious module names, giant auto-generated functions |
118
+
119
+ ### 📊 Agent Behavior Audit
120
+ Reads agent trace data and produces a daily report:
121
+ - Tool call frequency & ranking
122
+ - Error rate & anomaly detection
123
+ - Token consumption & cost estimates
124
+ - Model usage distribution
125
+
126
+ ### 📈 Trend Dashboard
127
+ Text-based trend chart with zero external dependencies:
128
+ ```
129
+ ━━━ 📈 Quality Trend (Last 14 days) ━━━
130
+ 05/08 ████████████████ 0 issues ✅
131
+ 05/09 ████████████░░░░ 3 issues ⚠️
132
+ 05/10 ████████████████ 0 issues ✅
133
+ ```
134
+ Data persists in SQLite. View anytime with `gate trend`.
135
+
136
+ ### 🔧 Auto-Fix Pipeline
137
+ Automatically fixes what it can, backed up and git-committed:
138
+ - Unused imports (`F401`) and variables (`F841`)
139
+ - Bare `except:` → `except Exception:`
140
+ - Hardcoded paths → `$HOME` references
141
+ - Ruff-safe auto-fixes
142
+
143
+ ### 🧩 Plugin Rule Architecture
144
+ Rules are Python classes. Adding a new rule = one file, one class, one decorator.
145
+ ```python
146
+ @register_rule
147
+ class MyRule(Rule):
148
+ name = "my_rule"
149
+ severity = Severity.BLOCKER
150
+
151
+ def diagnose(self, filepath: str) -> list[Issue]:
152
+ # Your detection logic here
153
+ ...
154
+ ```
155
+
156
+ ---
157
+
158
+ ## Quick Start
159
+
160
+ ```bash
161
+ # 🎯 Scan your project
162
+ pip install agentguard-tool
163
+ cd your-project
164
+ gate run
165
+
166
+ # 🔍 Quick check (staged files only)
167
+ gate run --quick
168
+
169
+ # 📊 Agent behavior audit
170
+ gate audit
171
+
172
+ # 📈 View quality trends
173
+ gate trend
174
+
175
+ # 🔧 Install pre-commit hooks
176
+ gate install
177
+ ```
178
+
179
+ ### Run from source
180
+ ```bash
181
+ git clone https://github.com/weijinsheng123456/agentguard.git
182
+ cd agentguard
183
+ python gate.py run
184
+ ```
185
+
186
+ ---
187
+
188
+ ## Commands
189
+
190
+ | Command | Description |
191
+ |---------|-------------|
192
+ | `gate run` | Full scan + auto-fix + commit + audit |
193
+ | `gate run --quick` | Pre-commit check (staged files only) |
194
+ | `gate run --fixme` | Auto-fix staged files |
195
+ | `gate audit` | Agent behavior audit only |
196
+ | `gate trend [N]` | Show last N days of quality trends |
197
+ | `gate install` | Install pre-commit hooks & cron |
198
+ | `gate version` | Show version |
199
+
200
+ ---
201
+
202
+ ## Rules
203
+
204
+ ### Built-in Rules (8 total)
205
+
206
+ **Code Quality (ported from ruff):**
207
+ | Rule | Code | Severity | Auto-fix |
208
+ |------|------|----------|:--------:|
209
+ | `syntax_check` | `SYNTAX` | BLOCKER | ❌ |
210
+ | `ruff_blocker` | `F821`, `E999` | BLOCKER | ❌ |
211
+ | `ruff_fixable` | `F401`, `F841`, `E711`, `E712` | FIXABLE | ✅ |
212
+ | `bare_except` | `E722` | FIXABLE | ✅ |
213
+ | `hardcoded_paths` | `HARDCODE` | FIXABLE | ✅ |
214
+
215
+ **AI-Specific:**
216
+ | Rule | Code | Severity | Auto-fix |
217
+ |------|------|----------|:--------:|
218
+ | `unsafe_api` | `UNSAFE_*` | BLOCKER | ❌ |
219
+ | `secret_leak` | `LEAK_SECRET` | BLOCKER | ❌ |
220
+ | `ai_hallucination` | `AI_*` | INFO | ❌ |
221
+
222
+ ---
223
+
224
+ ## Write Custom Rules
225
+
226
+ Create a new `.py` file in `qg/rules/`:
227
+
228
+ ```python
229
+ from qg.models import Issue, Severity
230
+ from qg.rules.base import Rule, register_rule
231
+
232
+ @register_rule
233
+ class MyCustomRule(Rule):
234
+ name = "my_custom_rule"
235
+ severity = Severity.FIXABLE
236
+ description = "Detects something specific"
237
+
238
+ def should_check(self, filepath: str) -> bool:
239
+ return filepath.endswith(".py")
240
+
241
+ def diagnose(self, filepath: str) -> list[Issue]:
242
+ issues = []
243
+ # Your detection logic...
244
+ return issues
245
+
246
+ def fix(self, filepath: str, issue: Issue) -> bool:
247
+ # Your fix logic...
248
+ return True
249
+ ```
250
+
251
+ Rules support two modes:
252
+ - **`Rule`** — per-file scanning (for AST analysis, regex)
253
+ - **`BatchRule`** — directory-level scanning (for ruff, 10-50x faster)
254
+
255
+ ---
256
+
257
+ ## Configuration
258
+
259
+ ```yaml
260
+ scan_dirs:
261
+ - "~/my-project/src"
262
+ - "~/my-project/scripts"
263
+
264
+ ignore_patterns:
265
+ - "*__pycache__*"
266
+ - "*/tests/*"
267
+
268
+ severity:
269
+ blocker_codes: ["F821", "E999", "SYNTAX"]
270
+ auto_fix_codes: ["F401", "F841", "E711", "E712", "E722", "HARDCODE"]
271
+
272
+ report:
273
+ to_wechat: true
274
+ ```
275
+
276
+ ---
277
+
278
+ ## Architecture
279
+
280
+ ```
281
+ gate.py (CLI entry)
282
+ └── qg/
283
+ ├── scanner.py # File discovery
284
+ ├── engine.py # Diagnostic engine (rule dispatching)
285
+ ├── fixer.py # Auto-fix engine
286
+ ├── verifier.py # Post-fix verification
287
+ ├── committer.py # Git commit automation
288
+ ├── reporter.py # Report generation (console + log)
289
+ ├── auditor.py # Agent behavior audit
290
+ ├── dashboard.py # Trend tracking (SQLite)
291
+ ├── models.py # Data models
292
+ ├── config.py # Configuration loader
293
+ └── rules/ # Plugin rules (hot-pluggable)
294
+ ├── base.py # Rule + BatchRule base classes
295
+ ├── syntax_check.py
296
+ ├── ruff_blocker.py (BatchRule)
297
+ ├── ruff_fixable.py (BatchRule)
298
+ ├── bare_except.py
299
+ ├── hardcoded_paths.py
300
+ ├── unsafe_api.py
301
+ ├── secret_leak.py
302
+ └── ai_hallucination.py
303
+ ```
304
+
305
+ ---
306
+
307
+ ## Roadmap
308
+
309
+ - [x] Phase 1: Python rewrite + plugin architecture
310
+ - [x] Phase 2: AI-specific rules + agent audit + trend dashboard
311
+ - [x] Phase 3: Open-source release + CI integration
312
+ - [ ] Phase 4: Security rules (OWASP Top 10 for AI code)
313
+ - [ ] Phase 5: GitHub Actions native action
314
+ - [ ] Phase 6: VS Code extension
315
+
316
+ ---
317
+
318
+ ## License
319
+
320
+ MIT License — see [LICENSE](LICENSE)
321
+
322
+ ---
323
+
324
+ <p align="center">
325
+ Built for the age of AI-generated code.
326
+ <br>
327
+ <sub>Because code quality doesn't matter less when AI writes it — it matters more.</sub>
328
+ </p>
@@ -0,0 +1,29 @@
1
+ agentguard_tool-1.0.5.dist-info/licenses/LICENSE,sha256=GIXKl73dGwSbkQSE6HUMPgfwT0peiuDGFIrz4gfJ8xs,1068
2
+ qg/__init__.py,sha256=Oh2451fZ3-TKpURfnm-bTk16TqYM0j2XByVAIHaSFIY,77
3
+ qg/__main__.py,sha256=dvvjl6aIzE7EJt70fcQufvRFSxczegdOfqtLt7lqwAU,504
4
+ qg/auditor.py,sha256=Un0KXuywsdVxb0_G5CRweiqP75i-MNKaGnT0SHzo9K8,6766
5
+ qg/committer.py,sha256=JtbDjfGSAZcj92A2vduc_raOwGbTfoRT3e_b48Ci_9k,3263
6
+ qg/config.py,sha256=tMjzMa_rWlGLTM1Su-dfaSCrdN6BaBOIzlQhdve5-4o,4341
7
+ qg/dashboard.py,sha256=eY6BNnfNEdb0p9Ywe6nJf3sfamqRtHPs0UHztBKjXnQ,4444
8
+ qg/engine.py,sha256=vnbQTQ6ZVqUnC9nMO4iqYel6_eumyacI2HUNfq20Y_4,3764
9
+ qg/fixer.py,sha256=h7BqSZbF1NyMJaKCLCgtegp1jL6mGJQKubmEkHF47Q0,4056
10
+ qg/main.py,sha256=6ukGt0Y7MZ9LCnnxG1mV9hmaaKEuhqIAx0bh6mWq2ow,11790
11
+ qg/models.py,sha256=SY106zvZu2o1yeY8xWmpuXt4Hgf_-UjzVtkiCBm9D4g,2293
12
+ qg/reporter.py,sha256=SbXEVmz1y7AO89yCM5FZxeU5NQirVeEF4ILO60fDUaM,2735
13
+ qg/scanner.py,sha256=LubqmTfmw5Ue7usOV_g7660yqhRNrmpWw9588JkgzWc,3895
14
+ qg/verifier.py,sha256=iKZmDGv1UvR5XL2kHdNHqfztm9foF8Qomgpo9tvr_SE,1270
15
+ qg/rules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
+ qg/rules/ai_hallucination.py,sha256=pktdUUIFcvaTYtxHBqWYbJzasY231Bkn_kNRp8fAg_Y,4061
17
+ qg/rules/bare_except.py,sha256=I66hUtLgst-qAqOwGFrs_FzyPhSMLMJC8Ca6T_mJ2vI,1609
18
+ qg/rules/base.py,sha256=wiYKlyilJOEcAB0mEvTlBRtUHPcdoMTLN_xV9cfpm2I,2735
19
+ qg/rules/hardcoded_paths.py,sha256=oSKfwPVkQnBRFB--y2Hj2pLw6lNSc2bjlSDZOXr30tY,1918
20
+ qg/rules/ruff_blocker.py,sha256=194YmO9X5Obmscejtlui1yOR_rd8Ji3fsyVwK_oJDs8,1831
21
+ qg/rules/ruff_fixable.py,sha256=XMvtFksiEP3a8_s9RoM0HnZJFb2uoPIxVimcHYxjXuk,2065
22
+ qg/rules/secret_leak.py,sha256=6yf_AMXjEkL41op7jIpkMefD4UkGAiBjZwFGY3j4p7U,5129
23
+ qg/rules/syntax_check.py,sha256=t8HJ8r623OY3xnYCnEnX8zMrrw0e9mB96jaXv73GAhc,1067
24
+ qg/rules/unsafe_api.py,sha256=SQuIz509fqhDwd8j2l2BE1fnZdEFqPPogaUba9yHTNI,5280
25
+ agentguard_tool-1.0.5.dist-info/METADATA,sha256=zDVFy6jEVrVnZWDqEepg7w4ZBJlym2WGB2tEQJ99Je8,10407
26
+ agentguard_tool-1.0.5.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
27
+ agentguard_tool-1.0.5.dist-info/entry_points.txt,sha256=wnBggB3me9QkignAXJILXFLX_DIEk77n7hewzupIsas,72
28
+ agentguard_tool-1.0.5.dist-info/top_level.txt,sha256=EDXMbpxZjlpG6jxQHmoE9k_doivDOELRIHH8jo19gZk,3
29
+ agentguard_tool-1.0.5.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ agentguard = qg.__main__:main
3
+ gate = qg.__main__:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Hermes Labs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ qg
qg/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """qg 包 — AgentGuard: AI-native quality gate for agent-generated code"""
qg/__main__.py ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env python3
2
+ """AgentGuard CLI entry point.
3
+
4
+ Install: pip install agentguard
5
+ Usage: gate run gate audit gate trend
6
+ """
7
+
8
+ import sys
9
+ from pathlib import Path
10
+
11
+ # When installed via pip, this isn't needed. When run from source, add to path.
12
+ if not any(p.endswith("agentguard") for p in sys.path):
13
+ pkg_dir = Path(__file__).resolve().parent.parent
14
+ if (pkg_dir / "qg").exists():
15
+ sys.path.insert(0, str(pkg_dir))
16
+
17
+ from qg.main import main
18
+
19
+ if __name__ == "__main__":
20
+ main()
qg/auditor.py ADDED
@@ -0,0 +1,190 @@
1
+ """Agent行为审计模块。
2
+
3
+ 从 agent_traces 表读取数据,分析:
4
+ - 工具调用频率与分布
5
+ - 错误率与异常模式
6
+ - Token消耗与成本估算
7
+ - 异常行为检测(连续失败、循环调用)
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import logging
13
+ import os
14
+ import sqlite3
15
+ from datetime import datetime, timedelta
16
+ from pathlib import Path
17
+ from typing import Optional
18
+
19
+ logger = logging.getLogger(__name__)
20
+
21
+ DB_PATH = Path(
22
+ os.environ.get("QG_TRACE_DB", os.environ.get("HERMES_HOME", Path.home() / ".hermes"))
23
+ ) / "state.db"
24
+
25
+
26
+ def _get_conn() -> Optional[sqlite3.Connection]:
27
+ """获取数据库连接"""
28
+ if not DB_PATH.exists():
29
+ logger.warning(f"state.db 不存在: {DB_PATH}")
30
+ return None
31
+ try:
32
+ conn = sqlite3.connect(str(DB_PATH))
33
+ conn.row_factory = sqlite3.Row
34
+ return conn
35
+ except sqlite3.Error as e:
36
+ logger.warning(f"连接 state.db 失败: {e}")
37
+ return None
38
+
39
+
40
+ def _table_exists(conn: sqlite3.Connection, table: str) -> bool:
41
+ """检查表是否存在"""
42
+ try:
43
+ cursor = conn.execute(
44
+ "SELECT name FROM sqlite_master WHERE type='table' AND name=?",
45
+ (table,)
46
+ )
47
+ return cursor.fetchone() is not None
48
+ except sqlite3.Error:
49
+ return False
50
+
51
+
52
+ def audit_agent(days: int = 7) -> dict:
53
+ """执行Agent行为审计,返回结构化报告"""
54
+ conn = _get_conn()
55
+ if conn is None:
56
+ return {"error": "state.db 不可用"}
57
+
58
+ if not _table_exists(conn, "agent_traces"):
59
+ return {"error": "agent_traces 表不存在(未启用追踪)"}
60
+
61
+ report: dict = {}
62
+
63
+ # 1. 整体统计
64
+ cursor = conn.execute("SELECT COUNT(*) as cnt FROM agent_traces")
65
+ total = cursor.fetchone()["cnt"]
66
+ report["total_events"] = total
67
+
68
+ # 2. 按事件类型分布
69
+ cursor = conn.execute(
70
+ "SELECT event_type, COUNT(*) as cnt FROM agent_traces GROUP BY event_type ORDER BY cnt DESC"
71
+ )
72
+ report["event_distribution"] = {row["event_type"]: row["cnt"] for row in cursor.fetchall()}
73
+
74
+ # 3. 按模型分布(api_call)
75
+ cursor = conn.execute(
76
+ "SELECT model, COUNT(*) as cnt FROM agent_traces WHERE event_type='api_call' AND model IS NOT NULL GROUP BY model ORDER BY cnt DESC LIMIT 10"
77
+ )
78
+ report["model_usage"] = {row["model"]: row["cnt"] for row in cursor.fetchall()}
79
+
80
+ # 4. 工具调用排行
81
+ cursor = conn.execute(
82
+ "SELECT tool_name, COUNT(*) as cnt FROM agent_traces WHERE event_type='tool_call' AND tool_name IS NOT NULL GROUP BY tool_name ORDER BY cnt DESC LIMIT 15"
83
+ )
84
+ report["tool_ranking"] = {row["tool_name"]: row["cnt"] for row in cursor.fetchall()}
85
+
86
+ # 5. 错误统计
87
+ cursor = conn.execute(
88
+ "SELECT status, COUNT(*) as cnt FROM agent_traces WHERE status != 'success' AND status IS NOT NULL GROUP BY status"
89
+ )
90
+ errors = {row["status"]: row["cnt"] for row in cursor.fetchall()}
91
+ cursor = conn.execute("SELECT COUNT(*) as cnt FROM agent_traces WHERE status != 'success' OR status IS NULL")
92
+ total_errors = cursor.fetchone()["cnt"]
93
+ report["errors"] = errors
94
+ report["total_errors"] = total_errors
95
+ report["error_rate"] = round(total_errors / total * 100, 2) if total > 0 else 0
96
+
97
+ # 6. Token消耗汇总
98
+ cursor = conn.execute(
99
+ "SELECT model, SUM(token_count) as total_tokens FROM agent_traces WHERE event_type='api_call' AND token_count IS NOT NULL GROUP BY model ORDER BY total_tokens DESC"
100
+ )
101
+ token_data = {row["model"]: row["total_tokens"] for row in cursor.fetchall()}
102
+ report["token_consumption"] = token_data
103
+ report["total_tokens"] = sum(token_data.values()) if token_data else 0
104
+
105
+ # 7. 成本估算(粗略:按模型均价)
106
+ COST_PER_1K = {
107
+ "deepseek-v4-flash": 0.00015,
108
+ "deepseek-v4-pro": 0.002,
109
+ "gpt-4o": 0.0025,
110
+ "gpt-4o-mini": 0.00015,
111
+ "claude-sonnet-4": 0.003,
112
+ "claude-haiku-3.5": 0.00025,
113
+ }
114
+ estimated_cost = 0.0
115
+ for model, tokens in token_data.items():
116
+ rate = 0.0005 # 默认
117
+ for key, val in COST_PER_1K.items():
118
+ if key in model.lower():
119
+ rate = val
120
+ break
121
+ estimated_cost += (tokens / 1000) * rate
122
+ report["estimated_cost_usd"] = round(estimated_cost, 4)
123
+
124
+ # 8. 每日活跃度(近7天)
125
+ report["daily_activity"] = {}
126
+ for i in range(days - 1, -1, -1):
127
+ day = (datetime.now() - timedelta(days=i)).strftime("%m/%d")
128
+ cursor = conn.execute(
129
+ "SELECT COUNT(*) as cnt FROM agent_traces WHERE timestamp LIKE ?",
130
+ (f"{day}%",)
131
+ )
132
+ report["daily_activity"][day] = cursor.fetchone()["cnt"]
133
+
134
+ # 9. 异常检测:连续同工具失败
135
+ cursor = conn.execute(
136
+ "SELECT session_id, tool_name, COUNT(*) as cnt FROM agent_traces WHERE event_type='tool_call' AND status != 'success' AND tool_name IS NOT NULL GROUP BY session_id, tool_name HAVING cnt >= 3 ORDER BY cnt DESC LIMIT 5"
137
+ )
138
+ anomalies = cursor.fetchall()
139
+ report["anomalies"] = [dict(a) for a in anomalies] if anomalies else []
140
+
141
+ conn.close()
142
+ return report
143
+
144
+
145
+ def format_audit_report(report: dict) -> list[str]:
146
+ """将审计报告格式化为可读文本"""
147
+ if "error" in report:
148
+ return [f"⚠️ Agent审计不可用: {report['error']}"]
149
+
150
+ lines = []
151
+ lines.append("━━━ 📊 Agent行为审计 ━━━")
152
+
153
+ # 概览
154
+ lines.append(f"总事件: {report.get('total_events', 0)} 次")
155
+ dist = report.get("event_distribution", {})
156
+ parts = [f"{k}={v}" for k, v in sorted(dist.items(), key=lambda x: -x[1])]
157
+ lines.append(f"分布: {' | '.join(parts[:5])}")
158
+
159
+ # 错误率
160
+ err_rate = report.get("error_rate", 0)
161
+ err_total = report.get("total_errors", 0)
162
+ if err_rate > 0:
163
+ lines.append(f"错误: {err_total} 次 ({err_rate}%) ⚠️")
164
+ else:
165
+ lines.append("错误: 0 次 ✅")
166
+
167
+ # Token & 成本
168
+ tokens = report.get("total_tokens", 0)
169
+ cost = report.get("estimated_cost_usd", 0)
170
+ lines.append(f"Token: {tokens:,} | 成本: ~${cost:.4f}")
171
+
172
+ # 工具调用排行(Top 5)
173
+ tools = report.get("tool_ranking", {})
174
+ if tools:
175
+ top5 = list(tools.items())[:5]
176
+ lines.append(f"工具Top5: {' → '.join(f'{n}({c})' for n, c in top5)}")
177
+
178
+ # 模型使用
179
+ models = report.get("model_usage", {})
180
+ if models:
181
+ model_str = " | ".join(f"{m}: {c}" for m, c in list(models.items())[:3])
182
+ lines.append(f"模型: {model_str}")
183
+
184
+ # 异常
185
+ anomalies = report.get("anomalies", [])
186
+ if anomalies:
187
+ for a in anomalies:
188
+ lines.append(f"⚠️ 异常: session {a['session_id'][:12]}... {a['tool_name']} 失败 {a['cnt']} 次")
189
+
190
+ return lines