|
| 1 | +#!/usr/bin/env bash |
| 2 | +# VibeGuard Hook Health Snapshot |
| 3 | +# 读取 events.jsonl,输出最近 N 小时的健康快照。 |
| 4 | +# |
| 5 | +# 用法: |
| 6 | +# bash scripts/hook-health.sh # 最近 24 小时 |
| 7 | +# bash scripts/hook-health.sh 72 # 最近 72 小时 |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +HOURS="${1:-24}" |
| 12 | +LOG_FILE="${VIBEGUARD_LOG_DIR:-${HOME}/.vibeguard}/events.jsonl" |
| 13 | + |
| 14 | +if ! [[ "${HOURS}" =~ ^[0-9]+$ ]] || [[ "${HOURS}" -le 0 ]]; then |
| 15 | + echo "参数必须是正整数小时数,例如: 24" |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +if [[ ! -f "${LOG_FILE}" ]]; then |
| 20 | + echo "没有日志数据。hooks 触发后会自动记录到 ${LOG_FILE}" |
| 21 | + exit 0 |
| 22 | +fi |
| 23 | + |
| 24 | +VG_HOURS="${HOURS}" VG_LOG_FILE="${LOG_FILE}" python3 - <<'PY' |
| 25 | +import json |
| 26 | +import os |
| 27 | +import sys |
| 28 | +from collections import Counter |
| 29 | +from datetime import datetime, timedelta, timezone |
| 30 | +
|
| 31 | +
|
| 32 | +def parse_ts(ts: str): |
| 33 | + if not ts: |
| 34 | + return None |
| 35 | + try: |
| 36 | + return datetime.fromisoformat(ts.replace("Z", "+00:00")) |
| 37 | + except ValueError: |
| 38 | + return None |
| 39 | +
|
| 40 | +
|
| 41 | +hours = int(os.environ["VG_HOURS"]) |
| 42 | +log_file = os.environ["VG_LOG_FILE"] |
| 43 | +now = datetime.now(timezone.utc) |
| 44 | +cutoff = now - timedelta(hours=hours) |
| 45 | +
|
| 46 | +events = [] |
| 47 | +with open(log_file, "r", encoding="utf-8") as f: |
| 48 | + for line in f: |
| 49 | + line = line.strip() |
| 50 | + if not line: |
| 51 | + continue |
| 52 | + try: |
| 53 | + event = json.loads(line) |
| 54 | + except json.JSONDecodeError: |
| 55 | + continue |
| 56 | + event_ts = parse_ts(event.get("ts", "")) |
| 57 | + if event_ts is None: |
| 58 | + continue |
| 59 | + if event_ts >= cutoff: |
| 60 | + event["_parsed_ts"] = event_ts |
| 61 | + events.append(event) |
| 62 | +
|
| 63 | +if not events: |
| 64 | + print(f"最近 {hours} 小时没有日志数据。") |
| 65 | + sys.exit(0) |
| 66 | +
|
| 67 | +events.sort(key=lambda e: e["_parsed_ts"]) |
| 68 | +total = len(events) |
| 69 | +by_decision = Counter(e.get("decision", "unknown") for e in events) |
| 70 | +pass_count = by_decision.get("pass", 0) |
| 71 | +risk_count = total - pass_count |
| 72 | +risk_rate = (risk_count / total * 100) if total else 0.0 |
| 73 | +
|
| 74 | +first_ts = events[0].get("ts", "?") |
| 75 | +last_ts = events[-1].get("ts", "?") |
| 76 | +
|
| 77 | +print(f"VibeGuard Hook Health (最近 {hours} 小时)") |
| 78 | +print("=" * 44) |
| 79 | +print(f"时间范围: {first_ts} ~ {last_ts}") |
| 80 | +print(f"总触发: {total}") |
| 81 | +print(f"通过(pass): {pass_count}") |
| 82 | +print(f"风险(非 pass): {risk_count}") |
| 83 | +print(f"风险率: {risk_rate:.1f}%") |
| 84 | +print(f" block: {by_decision.get('block', 0)}") |
| 85 | +print(f" gate: {by_decision.get('gate', 0)}") |
| 86 | +print(f" warn: {by_decision.get('warn', 0)}") |
| 87 | +print(f" escalate: {by_decision.get('escalate', 0)}") |
| 88 | +print(f" correction: {by_decision.get('correction', 0)}") |
| 89 | +
|
| 90 | +non_pass_events = [e for e in events if e.get("decision") != "pass"] |
| 91 | +if non_pass_events: |
| 92 | + top_non_pass_hooks = Counter(e.get("hook", "unknown") for e in non_pass_events) |
| 93 | + print("\n风险 Hook Top 5:") |
| 94 | + for hook, count in top_non_pass_hooks.most_common(5): |
| 95 | + print(f" {hook}: {count}") |
| 96 | +
|
| 97 | + print("\n最近风险事件 Top 10:") |
| 98 | + for i, event in enumerate(reversed(non_pass_events[-10:]), start=1): |
| 99 | + ts = event.get("ts", "?") |
| 100 | + session = event.get("session", "?") |
| 101 | + hook = event.get("hook", "unknown") |
| 102 | + decision = event.get("decision", "unknown") |
| 103 | + reason = (event.get("reason") or "").replace("\n", " ").strip() |
| 104 | + detail = (event.get("detail") or "").replace("\n", " ").strip() |
| 105 | + if len(reason) > 100: |
| 106 | + reason = reason[:97] + "..." |
| 107 | + if len(detail) > 100: |
| 108 | + detail = detail[:97] + "..." |
| 109 | + print(f" {i}. {ts} | {hook} | {decision} | session={session}") |
| 110 | + if reason: |
| 111 | + print(f" reason: {reason}") |
| 112 | + if detail: |
| 113 | + print(f" detail: {detail}") |
| 114 | +
|
| 115 | +print() |
| 116 | +PY |
0 commit comments