-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscover.py
More file actions
268 lines (228 loc) · 10.4 KB
/
Copy pathdiscover.py
File metadata and controls
268 lines (228 loc) · 10.4 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
import os
import glob
import json
import re
import shlex
from collections import Counter
from typing import List
from filters.engine import engine
def extract_antigravity_commands() -> List[str]:
home = os.path.expanduser("~")
# Matches ~/.gemini/antigravity-ide/brain/*/.system_generated/logs/transcript.jsonl
pattern = os.path.join(home, ".gemini", "antigravity-ide", "brain", "*", ".system_generated", "logs", "transcript.jsonl")
commands = []
for filepath in glob.glob(pattern):
try:
with open(filepath, 'r', encoding='utf-8') as f:
for line in f:
if '"run_command"' in line:
try:
data = json.loads(line)
tool_calls = data.get("tool_calls", [])
if tool_calls is None:
continue
for call in tool_calls:
if call.get("name") == "run_command":
args = call.get("args", {})
cmd = args.get("CommandLine")
if cmd:
cmd = cmd.strip('"')
commands.append(cmd)
except json.JSONDecodeError:
continue
except Exception:
pass
return commands
def extract_claude_commands() -> List[str]:
home = os.path.expanduser("~")
projects_dir = os.path.join(home, ".claude", "projects")
commands = []
if not os.path.exists(projects_dir):
return commands
for root, dirs, files in os.walk(projects_dir):
for file in files:
if file.endswith(".jsonl"):
filepath = os.path.join(root, file)
try:
with open(filepath, 'r', encoding='utf-8') as f:
for line in f:
if '"Bash"' in line and '"tool_use"' in line:
try:
data = json.loads(line)
msg = data.get("message", {})
content = msg.get("content", [])
if isinstance(content, list):
for block in content:
if block.get("type") == "tool_use" and block.get("name") == "Bash":
cmd = block.get("input", {}).get("command")
if cmd:
commands.append(cmd)
except json.JSONDecodeError:
continue
except Exception:
pass
return commands
def extract_hermes_commands() -> List[str]:
home = os.path.expanduser("~")
sessions_dir = os.path.join(home, "AppData", "Local", "Hermes", "sessions")
if os.name != 'nt':
sessions_dir = os.path.join(home, ".config", "hermes", "sessions")
commands = []
if not os.path.exists(sessions_dir):
return commands
for root, _, files in os.walk(sessions_dir):
for file in files:
if file.endswith(".json") or file.endswith(".jsonl"):
filepath = os.path.join(root, file)
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
matches = re.findall(r'"(?:command|CommandLine|cmd)"\s*:\s*"([^"\\]*(?:\\.[^"\\]*)*)"', content)
for m in matches:
cmd = m.encode('utf-8').decode('unicode_escape')
commands.append(cmd)
except Exception:
pass
return commands
def extract_openclaw_commands() -> List[str]:
home = os.path.expanduser("~")
openclaw_dir = os.path.join(home, ".openclaw")
commands = []
if not os.path.exists(openclaw_dir):
return commands
for root, _, files in os.walk(openclaw_dir):
for file in files:
filepath = os.path.join(root, file)
try:
if file.endswith(".sqlite") or file.endswith(".db"):
with open(filepath, 'rb') as f:
content = f.read().decode('utf-8', errors='ignore')
matches = re.findall(r'"(?:command|CommandLine|cmd)"\s*:\s*"([^"\\]*(?:\\.[^"\\]*)*)"', content)
for m in matches:
cmd = m.encode('utf-8').decode('unicode_escape')
commands.append(cmd)
elif file.endswith(".json") or file.endswith(".jsonl"):
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
matches = re.findall(r'"(?:command|CommandLine|cmd)"\s*:\s*"([^"\\]*(?:\\.[^"\\]*)*)"', content)
for m in matches:
cmd = m.encode('utf-8').decode('unicode_escape')
commands.append(cmd)
except Exception:
pass
return commands
def extract_craft_agents_commands() -> List[str]:
home = os.path.expanduser("~")
craft_dir = os.path.join(home, ".craft-agent")
commands = []
if not os.path.exists(craft_dir):
return commands
for root, _, files in os.walk(craft_dir):
for file in files:
if file.endswith(".json") or file.endswith(".jsonl"):
filepath = os.path.join(root, file)
try:
with open(filepath, 'r', encoding='utf-8') as f:
for line in f:
if '"command"' in line or '"CommandLine"' in line or '"cmd"' in line:
matches = re.findall(r'"(?:command|CommandLine|cmd)"\s*:\s*"([^"\\]*(?:\\.[^"\\]*)*)"', line)
for m in matches:
cmd = m.encode('utf-8').decode('unicode_escape')
commands.append(cmd)
except Exception:
pass
return commands
def split_chained_commands(cmd: str) -> List[str]:
"""Split a chained command string into individual commands.
Handles: && , || , ; , and | (pipe) separators.
For pipes, extracts each command in the pipeline individually.
"""
try:
tokens = shlex.split(cmd)
except Exception:
# Fallback for unbalanced quotes
parts = re.split(r'\s*(?:&&|\|\||;|\|)\s*', cmd)
return [p.strip() for p in parts if p.strip()]
individual = []
current = []
for token in tokens:
if token in ("&&", "||", ";", "|"):
if current:
individual.append(" ".join(current))
current = []
else:
current.append(token)
if current:
individual.append(" ".join(current))
return individual
def simplify_command(cmd: str) -> str:
"""Extracts the base executable name from a command string.
Strips environment variable prefixes, sudo, and resolves
the first real command token.
"""
cmd = cmd.strip()
# Strip leading env var assignments (e.g., FOO=bar command)
cmd = re.sub(r'^(?:[A-Z_]+=[^\s]+\s+)+', '', cmd)
# Strip sudo
cmd = re.sub(r'^sudo\s+', '', cmd)
# Strip cd ... && prefix (very common in agent commands)
cmd = re.sub(r'^cd\s+[^\s;&]+\s*(?:&&\s*)?', '', cmd)
parts = cmd.split()
if not parts:
return ""
base = parts[0]
# Strip path prefixes (e.g., /usr/bin/git -> git, ./gradlew -> gradlew)
base = os.path.basename(base)
# Ignore invalid executable names that might be parsing artifacts
if not re.match(r'^[a-zA-Z0-9_.-]+$', base):
return ""
return base
def run_discover():
print("Discovering local AI agent transcripts...")
ag_cmds = extract_antigravity_commands()
claude_cmds = extract_claude_commands()
hermes_cmds = extract_hermes_commands()
openclaw_cmds = extract_openclaw_commands()
craft_cmds = extract_craft_agents_commands()
all_cmds = ag_cmds + claude_cmds + hermes_cmds + openclaw_cmds + craft_cmds
print(f"Found {len(ag_cmds)} commands from Antigravity IDE")
print(f"Found {len(claude_cmds)} commands from Claude Code")
print(f"Found {len(hermes_cmds)} commands from Hermes")
print(f"Found {len(openclaw_cmds)} commands from OpenClaw")
print(f"Found {len(craft_cmds)} commands from Craft Agents")
print("-" * 50)
if not all_cmds:
print("No commands found in logs. Start using your agents to gather data!")
return
filtered_count = 0
unfiltered_counts = Counter()
for raw_cmd in all_cmds:
# Split chained commands so each part is evaluated independently
individual_cmds = split_chained_commands(raw_cmd)
for cmd in individual_cmds:
base_cmd = simplify_command(cmd)
if not base_cmd:
continue
matched = False
for f in engine.filters:
# Skip fallback — we want to know explicit tool coverage
if f["id"] == "fallback":
continue
if f["match_command"].search(cmd) or f["match_command"].search(base_cmd):
matched = True
break
if matched:
filtered_count += 1
else:
unfiltered_counts[base_cmd] += 1
total_individual = filtered_count + sum(unfiltered_counts.values())
coverage = (filtered_count / total_individual) * 100 if total_individual > 0 else 0
print(f"Total Commands Analyzed: {total_individual} (from {len(all_cmds)} raw entries)")
print(f"Commands Protected by DeNoiser: {filtered_count} ({coverage:.1f}% Coverage)\n")
print("Top 10 Unfiltered Targets (Write TOML rules for these!):")
if not unfiltered_counts:
print("Amazing! 100% of your commands are explicitly covered by DeNoiser filters!")
else:
for cmd, count in unfiltered_counts.most_common(10):
print(f" - `{cmd}` ({count} times)")
print("-" * 50)