-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsandbox.py
More file actions
418 lines (334 loc) · 13.2 KB
/
Copy pathsandbox.py
File metadata and controls
418 lines (334 loc) · 13.2 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""
Copyright (c) 2025 Xpander, Inc. All rights reserved.
"""
import os
import subprocess
import shutil
from typing import Optional, Dict, Any
# === Base Setup ===
# Base directory for sandboxes
SANDBOX_BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sandboxes")
os.makedirs(SANDBOX_BASE_DIR, exist_ok=True)
print(f"Sandbox base directory: {SANDBOX_BASE_DIR}")
# Track sandboxes by thread
sandboxes = {} # Map thread_id -> sandbox_path
current_sandbox = None
# === Sandbox Management ===
def get_sandbox_path(thread_id: Optional[str] = None) -> str:
"""
Get or create a sandbox path for the specified thread.
Args:
thread_id (Optional[str]): Identifier for the thread.
Returns:
str: Path to the sandbox directory.
"""
global current_sandbox, sandboxes
if not thread_id and current_sandbox and os.path.exists(current_sandbox):
return current_sandbox
if thread_id and thread_id in sandboxes and os.path.exists(sandboxes[thread_id]):
current_sandbox = sandboxes[thread_id]
return current_sandbox
thread_part = f"{thread_id}_" if thread_id else ""
sandbox_path = os.path.join(SANDBOX_BASE_DIR, f"sandbox_{thread_part}")
if os.path.exists(sandbox_path):
shutil.rmtree(sandbox_path)
os.makedirs(sandbox_path, exist_ok=True)
current_sandbox = sandbox_path
if thread_id:
sandboxes[thread_id] = sandbox_path
return sandbox_path
def get_sandbox(thread_id: Optional[str] = None, filepath: Optional[str] = None) -> str:
"""
Get sandbox path for the specified thread, optionally joining with a file path.
Args:
thread_id (Optional[str]): Identifier for the thread.
filepath (Optional[str]): Filepath relative to the sandbox.
Returns:
str: Full sandbox path or full path to file inside sandbox.
"""
sandbox_path = get_sandbox_path(thread_id)
if filepath:
return safe_path_join(filepath, thread_id)
return sandbox_path
def safe_path_join(filepath: str, thread_id: Optional[str] = None) -> str:
"""
Securely join a filepath to its sandbox.
Args:
filepath (str): Relative file path.
thread_id (Optional[str]): Thread identifier.
Returns:
str: Full path inside the sandbox.
"""
sandbox_path = get_sandbox_path(thread_id)
if not filepath or filepath.strip() == "":
return sandbox_path
if ".." in filepath:
print("⚠️ Security: Parent directory traversal blocked")
return sandbox_path
full_path = os.path.join(sandbox_path, filepath)
parent_dir = os.path.dirname(full_path)
if parent_dir and not os.path.exists(parent_dir):
os.makedirs(parent_dir, exist_ok=True)
return full_path
# === Git Operations ===
def git_clone(repo_url: str, branch: Optional[str] = None, thread_id: Optional[str] = None) -> Dict[str, Any]:
"""
Clone a Git repository into the sandbox.
Args:
repo_url (str): Repository URL.
branch (Optional[str]): Branch to checkout.
thread_id (Optional[str]): Thread identifier.
Returns:
Dict[str, Any]: Operation result including success status and messages.
"""
sandbox_path = get_sandbox_path(thread_id)
try:
target_dir = repo_url.split("/")[-1]
if target_dir.endswith(".git"):
target_dir = target_dir[:-4]
target_path = os.path.join(sandbox_path, target_dir)
os.makedirs(target_path, exist_ok=True)
cmd = ["git", "clone", repo_url, "."]
if branch:
cmd.extend(["--branch", branch, "--single-branch"])
result = subprocess.run(
cmd,
cwd=target_path,
capture_output=True,
text=True,
timeout=120
)
return {
"success": result.returncode == 0,
"message": result.stdout if result.returncode == 0 else result.stderr,
"directory": target_dir,
"cloned_to": target_path if result.returncode == 0 else ""
}
except Exception as e:
return {
"success": False,
"message": f"Error cloning repository: {str(e)}",
"directory": target_dir if target_dir else ""
}
# === File System Operations ===
def describe_folders_and_files(thread_id: Optional[str] = None) -> Dict[str, Any]:
"""
Return a tree-like structure of the sandbox contents.
Args:
thread_id (Optional[str]): Thread identifier.
Returns:
Dict[str, Any]: Directory and file structure tree.
"""
sandbox_path = get_sandbox_path(thread_id)
def build_tree(path: str, rel_path: str = "") -> list:
result = []
for item in sorted(os.listdir(path)):
item_path = os.path.join(path, item)
item_rel_path = os.path.join(rel_path, item)
if os.path.isdir(item_path):
children = build_tree(item_path, item_rel_path)
result.append({
"name": item,
"type": "directory",
"path": item_rel_path,
"children": children
})
else:
result.append({
"name": item,
"type": "file",
"path": item_rel_path,
"size": os.path.getsize(item_path)
})
return result
tree = build_tree(sandbox_path)
return {
"success": True,
"tree": tree
}
def edit_file(file_path: str, content: str, thread_id: Optional[str] = None) -> Dict[str, Any]:
"""
Edit a file in the sandbox with the provided content.
Args:
file_path (str): Path to the file inside sandbox.
content (str): Content to write.
thread_id (Optional[str]): Thread identifier.
Returns:
Dict[str, Any]: Operation result.
"""
full_path = safe_path_join(file_path, thread_id)
try:
with open(full_path, 'w', encoding='utf-8') as f:
f.write(content)
return {
"success": True,
"message": f"File edited successfully: {os.path.basename(file_path)}",
"filepath": file_path
}
except Exception as e:
return {
"success": False,
"message": f"Error editing file: {str(e)}",
"filepath": file_path
}
def new_file(file_path: str, content: str, thread_id: Optional[str] = None) -> Dict[str, Any]:
"""
Create a new file in the sandbox with the provided content.
Args:
file_path (str): File path to create.
content (str): Content to write.
thread_id (Optional[str]): Thread identifier.
Returns:
Dict[str, Any]: Operation result.
"""
return edit_file(file_path, content, thread_id)
def read_file(file_path: str, thread_id: Optional[str] = None) -> Dict[str, Any]:
"""
Read the contents of a file in the sandbox.
Args:
file_path (str): File path to read.
thread_id (Optional[str]): Thread identifier.
Returns:
Dict[str, Any]: File read result with content.
"""
full_path = safe_path_join(file_path, thread_id)
try:
if not os.path.exists(full_path):
return {
"success": False,
"message": f"File not found: {file_path}",
"filepath": file_path,
"content": ""
}
with open(full_path, 'r', encoding='utf-8') as f:
content = f.read()
return {
"success": True,
"message": "File read successfully",
"filepath": file_path,
"content": content
}
except Exception as e:
return {
"success": False,
"message": f"Error reading file: {str(e)}",
"filepath": file_path,
"content": ""
}
# === Git Commit & Push ===
def commit(message: str, branch_name: str, repository: Optional[str] = None, thread_id: Optional[str] = None) -> Dict[str, Any]:
"""
Commit changes and push to a branch. Creates the branch if it doesn't exist.
Args:
message (str): Commit message.
branch_name (str): Name of the branch to commit to.
repository (Optional[str]): Repository name in the sandbox.
thread_id (Optional[str]): Thread identifier for sandbox context.
Returns:
Dict[str, Any]: Operation result, including commit hash and branch info.
"""
sandbox_path = get_sandbox_path(thread_id)
try:
# Discover git repositories in sandbox
git_dirs = {
item: os.path.join(sandbox_path, item)
for item in os.listdir(sandbox_path)
if os.path.isdir(os.path.join(sandbox_path, item)) and os.path.exists(os.path.join(sandbox_path, item, ".git"))
}
if not git_dirs:
return {"success": False, "message": "No Git repositories found in the sandbox."}
if repository:
if repository not in git_dirs:
return {
"success": False,
"message": f"Repository '{repository}' not found. Available: {', '.join(git_dirs.keys())}"
}
repo_name = repository
else:
repo_name = next(iter(git_dirs))
print(f"Warning: No repository specified. Defaulting to '{repo_name}'.")
repo_path = git_dirs[repo_name]
# Ensure git user identity is set
def ensure_git_config(key: str, value: str):
result = subprocess.run(["git", "config", "--get", key], cwd=repo_path, capture_output=True, text=True)
if result.returncode != 0 or not result.stdout.strip():
subprocess.run(["git", "config", key, value], cwd=repo_path, check=True)
ensure_git_config("user.name", "AI Agent")
ensure_git_config("user.email", "agent@xpander.ai")
# Check if branch exists
check_branch = subprocess.run(
["git", "rev-parse", "--verify", branch_name],
cwd=repo_path, capture_output=True, text=True
)
if check_branch.returncode == 0:
checkout = subprocess.run(["git", "checkout", branch_name], cwd=repo_path, capture_output=True, text=True)
else:
checkout = subprocess.run(["git", "checkout", "-b", branch_name], cwd=repo_path, capture_output=True, text=True)
if checkout.returncode != 0:
return {"success": False, "message": f"Failed to checkout/create branch '{branch_name}': {checkout.stderr.strip()}"}
# Stage all changes
add = subprocess.run(["git", "add", "."], cwd=repo_path, capture_output=True, text=True)
if add.returncode != 0:
return {"success": False, "message": f"Failed to stage files: {add.stderr.strip()}"}
# Commit
commit = subprocess.run(["git", "commit", "-m", message], cwd=repo_path, capture_output=True, text=True)
if commit.returncode != 0:
return {"success": False, "message": f"Commit failed: {commit.stderr.strip()}"}
# Push
push = subprocess.run(["git", "push", "-u", "origin", branch_name], cwd=repo_path, capture_output=True, text=True)
if push.returncode != 0:
return {"success": False, "message": f"Push failed: {push.stderr.strip()}"}
return {
"success": True,
"message": f"Successfully committed and pushed to branch '{branch_name}' in repository '{repo_name}'.",
"commit_hash": commit.stdout.strip(),
"branch": branch_name,
"repository": repo_name
}
except Exception as e:
return {"success": False, "message": f"Unexpected error: {str(e)}"}
def git_switch_branch(branch: str, path: Optional[str] = None, thread_id: Optional[str] = None) -> Dict[str, Any]:
"""
Switch to a different Git branch in the sandbox or specified path.
Args:
branch (str): Branch to switch to.
path (Optional[str]): Direct path to the Git working directory.
thread_id (Optional[str]): Thread identifier for fallback sandbox path.
Returns:
Dict[str, Any]: Operation result including success status and messages.
"""
sandbox_path = path or get_sandbox_path(thread_id)
if not os.path.isdir(os.path.join(sandbox_path, ".git")):
return {
"success": False,
"message": "You need to clone a repo first"
}
try:
fetch_result = subprocess.run(
["git", "fetch"],
cwd=sandbox_path,
capture_output=True,
text=True,
timeout=60
)
if fetch_result.returncode != 0:
return {
"success": False,
"message": f"Failed to fetch branches: {fetch_result.stderr}"
}
result = subprocess.run(
["git", "checkout", branch],
cwd=sandbox_path,
capture_output=True,
text=True,
timeout=60
)
return {
"success": result.returncode == 0,
"message": result.stdout if result.returncode == 0 else result.stderr
}
except Exception as e:
return {
"success": False,
"message": f"Error switching branch: {str(e)}"
}