-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
223 lines (147 loc) · 4.8 KB
/
Copy pathcli.py
File metadata and controls
223 lines (147 loc) · 4.8 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
from __future__ import annotations
import sys
import os
from typing import List
from .runner import run_command
from .context import build_context
from .ai import analyze_failure
from .safety import filter_safe_fixes
from .ui import (
print_failure_header,
print_analysis,
prompt_choice,
print_safety_messages,
)
# Only auto-test safe commands
SAFE_TEST_PREFIXES = ["pip", "npm", "python", "cargo", "go"]
# -------------------------------------------------
# Safe fix verification
# -------------------------------------------------
def verify_fix(command: str) -> bool:
"""
Optionally test a fix command before suggesting it.
Only runs for safe command prefixes.
"""
try:
prefix = command.split()[0]
if prefix not in SAFE_TEST_PREFIXES:
return True
print("🔎 Verifying fix...")
result = run_command(command.split())
if result.exit_code == 0:
print("✔ Fix verified")
return True
print("✖ Fix failed during verification")
return False
except Exception:
return False
# -------------------------------------------------
# Stats command
# -------------------------------------------------
def print_stats() -> None:
print("ShellPilot Stats")
print("----------------")
try:
import boto3
table_name = os.getenv("SHELLPILOT_MEMORY_TABLE", "shellpilot-memory")
dynamodb = boto3.resource("dynamodb", region_name="ap-south-1")
table = dynamodb.Table(table_name)
items = []
resp = table.scan()
items.extend(resp.get("Items", []))
while "LastEvaluatedKey" in resp:
resp = table.scan(ExclusiveStartKey=resp["LastEvaluatedKey"])
items.extend(resp.get("Items", []))
print(f"Errors learned: {len(items)}")
fix_counts = {}
for item in items:
fix = item.get("fix")
if fix:
fix_counts[fix] = fix_counts.get(fix, 0) + 1
if fix_counts:
print("")
print("Top learned fixes:")
top = sorted(
fix_counts.items(),
key=lambda x: x[1],
reverse=True
)[:3]
for fix, count in top:
print(f"• {fix} ({count} times)")
except Exception as e:
print("Unable to fetch stats.")
print(str(e))
# -------------------------------------------------
# Main CLI
# -------------------------------------------------
def main() -> None:
argv: List[str] = sys.argv[1:]
if not argv:
print("Usage:")
print(" ai-run <command>")
print(" ai-run stats")
sys.exit(1)
# -----------------------------
# Stats command
# -----------------------------
if len(argv) == 1 and argv[0] == "stats":
print_stats()
return
# -----------------------------
# Run the command
# -----------------------------
result = run_command(argv)
if result.exit_code == 0:
return
print_failure_header(result.command, result.exit_code)
context = build_context(result)
learning_mode = os.getenv("SHELLPILOT_MODE") == "aws"
analysis = analyze_failure(context, learning_mode=learning_mode)
if not analysis:
print("ShellPilot could not analyze this failure.")
return
filtered, safety_msgs = filter_safe_fixes(analysis)
if safety_msgs:
print_safety_messages(safety_msgs)
print_analysis(filtered, learning_mode=learning_mode)
if not filtered.fixes:
return
# -----------------------------
# Optional verification
# -----------------------------
verified_fixes = []
for fix in filtered.fixes:
if not fix.commands:
continue
if verify_fix(fix.commands[0]):
verified_fixes.append(fix)
if verified_fixes:
from dataclasses import replace
filtered = replace(filtered, fixes=verified_fixes)
choice = prompt_choice(len(filtered.fixes))
if choice is None:
return
fix = filtered.fixes[choice - 1]
print("")
print(f"Executing fix #{choice}:")
for cmd in fix.commands:
print(f" $ {cmd}")
cp = run_command(cmd.split())
if cp.exit_code != 0:
print("❌ Fix failed")
return
print("")
print("✅ Fix completed successfully")
print("")
print("🧠 ShellPilot learned this error pattern for faster fixes next time.")
# -----------------------------
# Optional rerun
# -----------------------------
try:
rerun = input("Re-run original command now? (y/n): ").strip().lower()
if rerun in {"y", "yes"}:
run_command(argv)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()