-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit.go
More file actions
315 lines (272 loc) · 8.72 KB
/
Copy pathaudit.go
File metadata and controls
315 lines (272 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package cmd
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/GrayCodeAI/hawk/internal/hooks/audit"
"github.com/GrayCodeAI/hawk/internal/storage"
"github.com/spf13/cobra"
)
var (
auditDays int
auditFormat string
auditLimit int
auditProject string
auditJSON bool
)
var auditCmd = &cobra.Command{
Use: "audit",
Short: "Analyze past sessions for wasteful patterns",
Long: `Scan past agent transcripts to detect wasteful behaviors like
redundant cd commands, unnecessary cat/head usage, long sleep loops,
and other patterns that waste tokens and wall-clock time.
Reports what hawk would have caught with current policies enabled,
plus audit-only detectors that identify optimization opportunities.`,
RunE: runAudit,
}
func init() {
auditCmd.Flags().IntVar(&auditDays, "days", 7, "number of days to scan")
auditCmd.Flags().StringVar(&auditFormat, "format", "text", "output format: text or json")
auditCmd.Flags().IntVar(&auditLimit, "limit", 20, "max rows in table output")
auditCmd.Flags().StringVar(&auditProject, "project", "", "restrict to specific project path")
auditCmd.Flags().BoolVar(&auditJSON, "json", false, "output as JSON (alias for --format json)")
rootCmd.AddCommand(auditCmd)
}
// AuditCount represents aggregated hits for one detector.
type AuditCount struct {
Name string `json:"name"`
Category string `json:"category"`
Severity string `json:"severity"`
Hits int `json:"hits"`
Projects int `json:"projects"`
Examples []string `json:"examples"`
FirstSeen string `json:"first_seen,omitempty"`
LastSeen string `json:"last_seen,omitempty"`
}
// AuditResult is the top-level audit output.
type AuditResult struct {
Version int `json:"version"`
ScannedAt string `json:"scanned_at"`
Days int `json:"days"`
Sessions int `json:"sessions_scanned"`
TotalHits int `json:"total_hits"`
Detectors []AuditCount `json:"detectors"`
}
func runAudit(cmd *cobra.Command, args []string) error {
if auditJSON {
auditFormat = "json"
}
// Discover session transcripts
sessions, err := discoverSessions(auditDays, auditProject)
if err != nil {
return fmt.Errorf("discovering sessions: %w", err)
}
if len(sessions) == 0 {
cmd.Println("No session transcripts found for the specified time period.")
return nil
}
// Run audit detectors on each session
detectors := audit.AllDetectors()
counts := make(map[string]*AuditCount)
totalHits := 0
for _, sess := range sessions {
events, err := loadSessionEvents(sess.Path)
if err != nil {
continue
}
state := make(audit.DetectorSessionState)
for _, event := range events {
for _, d := range detectors {
hit := d.Detect(event, state)
if hit == nil {
continue
}
totalHits++
key := hit.DetectorName
if counts[key] == nil {
counts[key] = &AuditCount{
Name: hit.DetectorName,
Category: hit.Category,
Severity: hit.Severity,
}
}
c := counts[key]
c.Hits++
if len(c.Examples) < 3 {
c.Examples = append(c.Examples, truncateStr(hit.Example, 80))
}
// Track timestamps
ts := event.Timestamp.Format(time.RFC3339)
if c.FirstSeen == "" || ts < c.FirstSeen {
c.FirstSeen = ts
}
if c.LastSeen == "" || ts > c.LastSeen {
c.LastSeen = ts
}
}
}
}
// Count unique projects per detector
for _, c := range counts {
c.Projects = 1 // simplified — could track unique cwds
}
// Sort by hits descending
var sorted []AuditCount
for _, c := range counts {
sorted = append(sorted, *c)
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Hits > sorted[j].Hits
})
// Apply limit
if auditLimit > 0 && len(sorted) > auditLimit {
sorted = sorted[:auditLimit]
}
result := AuditResult{
Version: 1,
ScannedAt: time.Now().Format(time.RFC3339),
Days: auditDays,
Sessions: len(sessions),
TotalHits: totalHits,
Detectors: sorted,
}
if auditFormat == "json" {
data, err := json.MarshalIndent(result, "", " ")
if err != nil {
return fmt.Errorf("marshaling JSON: %w", err)
}
cmd.Println(string(data))
return nil
}
printAuditText(cmd, result)
return nil
}
// SessionInfo represents a discovered session transcript.
type SessionInfo struct {
Path string
CWD string
StartTime time.Time
}
func discoverSessions(days int, projectFilter string) ([]SessionInfo, error) {
cutoff := time.Now().AddDate(0, 0, -days)
var sessions []SessionInfo
// Scan hawk sessions directory
hawkDir := storage.SessionsDir()
entries, err := os.ReadDir(hawkDir)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".jsonl") {
continue
}
path := filepath.Join(hawkDir, e.Name())
info, err := os.Stat(path)
if err != nil {
continue
}
if info.ModTime().Before(cutoff) {
continue
}
sessions = append(sessions, SessionInfo{
Path: path,
StartTime: info.ModTime(),
})
}
return sessions, nil
}
func loadSessionEvents(path string) ([]audit.ToolEvent, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var events []audit.ToolEvent
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
var entry struct {
Type string `json:"type"`
Timestamp string `json:"timestamp"`
Tool string `json:"tool"`
Input map[string]interface{} `json:"input"`
CWD string `json:"cwd"`
}
if err := json.Unmarshal([]byte(line), &entry); err != nil {
continue
}
// Only process tool_use events
if entry.Type != "tool_use" && entry.Tool == "" {
continue
}
ts, _ := time.Parse(time.RFC3339, entry.Timestamp)
events = append(events, audit.ToolEvent{
ToolName: entry.Tool,
ToolInput: entry.Input,
CWD: entry.CWD,
Timestamp: ts,
})
}
return events, nil
}
func printAuditText(cmd *cobra.Command, result AuditResult) {
w := cmd.OutOrStdout()
_, _ = fmt.Fprintf(w, "\n")
_, _ = fmt.Fprintf(w, "═══════════════════════════════════════════════════════════════\n")
_, _ = fmt.Fprintf(w, " Hawk Audit Report\n")
_, _ = fmt.Fprintf(w, "═══════════════════════════════════════════════════════════════\n")
_, _ = fmt.Fprintf(w, "\n")
_, _ = fmt.Fprintf(w, " Scanned: %d sessions (last %d days)\n", result.Sessions, result.Days)
_, _ = fmt.Fprintf(w, " Total hits: %d\n", result.TotalHits)
_, _ = fmt.Fprintf(w, " Scanned at: %s\n", result.ScannedAt)
if len(result.Detectors) == 0 {
_, _ = fmt.Fprintf(w, "\n No wasteful patterns detected. Great job!\n\n")
return
}
_, _ = fmt.Fprintf(w, "\n")
_, _ = fmt.Fprintf(w, "─── Detected Patterns ───\n\n")
_, _ = fmt.Fprintf(w, " %-30s %6s %8s %s\n", "DETECTOR", "HITS", "SEVERITY", "EXAMPLE")
_, _ = fmt.Fprintf(w, " %-30s %6s %8s %s\n", strings.Repeat("─", 30), strings.Repeat("─", 6), strings.Repeat("─", 8), strings.Repeat("─", 30))
for _, d := range result.Detectors {
example := ""
if len(d.Examples) > 0 {
example = d.Examples[0]
}
_, _ = fmt.Fprintf(w, " %-30s %6d %8s %s\n", d.Name, d.Hits, d.Severity, example)
}
_, _ = fmt.Fprintf(w, "\n")
_, _ = fmt.Fprintf(w, "─── Remediation Tips ───\n\n")
for _, d := range result.Detectors {
switch d.Name {
case "redundant-cd-cwd":
_, _ = fmt.Fprintf(w, " • Remove `cd <cwd> &&` prefixes — commands already run in cwd\n")
case "prefer-edit-over-read-cat":
_, _ = fmt.Fprintf(w, " • Use the Read tool instead of `cat`/`head`/`tail` on source files\n")
case "prefer-edit-over-sed-awk":
_, _ = fmt.Fprintf(w, " • Use the Edit tool instead of `sed`/`awk` on source files\n")
case "prefer-write-over-heredoc":
_, _ = fmt.Fprintf(w, " • Use the Write tool instead of `cat << EOF > file`\n")
case "sleep-polling-loop":
_, _ = fmt.Fprintf(w, " • Replace long sleep/polling with explicit wait signals\n")
case "find-from-root":
_, _ = fmt.Fprintf(w, " • Scope `find` to the project directory, not `/` or `/home`\n")
case "git-commit-no-verify":
_, _ = fmt.Fprintf(w, " • Remove `--no-verify` to let pre-commit hooks run\n")
case "reread-after-edit":
_, _ = fmt.Fprintf(w, " • Don't re-read files immediately after editing them\n")
}
}
_, _ = fmt.Fprintf(w, "\n")
}
func truncateStr(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen-3] + "..."
}