gitarsenal-cli 1.0.2 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2354 @@
1
+ import os
2
+ import sys
3
+ import modal
4
+ import time
5
+ import subprocess
6
+ import json
7
+ import re
8
+ import datetime
9
+ import getpass
10
+ import requests
11
+
12
+ def handle_interactive_input(prompt, is_password=False):
13
+ """Handle interactive input from the user with optional password masking"""
14
+ print("\n" + "="*60)
15
+ print(f"{prompt}")
16
+ print("="*60)
17
+
18
+ try:
19
+ if is_password:
20
+ user_input = getpass.getpass("Input (hidden): ").strip()
21
+ else:
22
+ user_input = input("Input: ").strip()
23
+
24
+ if not user_input:
25
+ print("❌ No input provided.")
26
+ return None
27
+ print("✅ Input received successfully!")
28
+ return user_input
29
+ except KeyboardInterrupt:
30
+ print("\n❌ Input cancelled by user.")
31
+ return None
32
+ except Exception as e:
33
+ print(f"❌ Error getting input: {e}")
34
+ return None
35
+
36
+ def handle_wandb_login(sandbox, current_dir):
37
+ """Handle Weights & Biases login with proper API key input"""
38
+ # Define _to_str function locally to avoid NameError
39
+ def _to_str(maybe_bytes):
40
+ try:
41
+ return (maybe_bytes.decode('utf-8') if isinstance(maybe_bytes, (bytes, bytearray)) else maybe_bytes)
42
+ except UnicodeDecodeError:
43
+ # Handle non-UTF-8 bytes by replacing invalid characters
44
+ if isinstance(maybe_bytes, (bytes, bytearray)):
45
+ return maybe_bytes.decode('utf-8', errors='replace')
46
+ else:
47
+ return str(maybe_bytes)
48
+ except Exception:
49
+ # Last resort fallback
50
+ return str(maybe_bytes)
51
+
52
+ print("\n🔑 WEIGHTS & BIASES LOGIN")
53
+ print("="*60)
54
+ print("Setting up Weights & Biases credentials")
55
+ print("You can get your API key from: https://wandb.ai/authorize")
56
+
57
+ # Get API key from user
58
+ api_key = handle_interactive_input(
59
+ "🔑 WEIGHTS & BIASES API KEY REQUIRED\n" +
60
+ "Please paste your W&B API key below:\n" +
61
+ "(Your API key should be 40 characters long)",
62
+ is_password=True
63
+ )
64
+
65
+ if not api_key:
66
+ print("❌ No API key provided. Cannot continue with W&B login.")
67
+ return False, "", "No W&B API key provided"
68
+
69
+ # Validate API key length
70
+ if len(api_key) != 40:
71
+ print(f"⚠️ Warning: API key should be 40 characters long, yours was {len(api_key)}")
72
+ confirm = handle_interactive_input("Continue anyway? (yes/no)")
73
+ if not confirm or confirm.lower() not in ["yes", "y"]:
74
+ print("❌ W&B login cancelled.")
75
+ return False, "", "W&B login cancelled"
76
+
77
+ # Use non-interactive login
78
+ cmd = f"wandb login {api_key}"
79
+ print(f"🔄 Running non-interactive login command")
80
+
81
+ # Execute the command
82
+ result = sandbox.exec("bash", "-c", f"cd {current_dir} && {cmd}")
83
+
84
+ # Collect output
85
+ stdout_lines = []
86
+ stderr_lines = []
87
+
88
+ for line in result.stdout:
89
+ line_str = _to_str(line)
90
+ stdout_lines.append(line_str)
91
+ sys.stdout.write(line_str)
92
+ sys.stdout.flush()
93
+
94
+ for line in result.stderr:
95
+ line_str = _to_str(line)
96
+ stderr_lines.append(line_str)
97
+ sys.stderr.write(line_str)
98
+ sys.stderr.flush()
99
+
100
+ result.wait()
101
+ exit_code = result.returncode
102
+
103
+ stdout_buffer = ''.join(stdout_lines)
104
+ stderr_buffer = ''.join(stderr_lines)
105
+
106
+ if exit_code == 0:
107
+ print("✅ Weights & Biases login successful")
108
+ # Also set the environment variable for this session
109
+ os.environ["WANDB_API_KEY"] = api_key
110
+ print("✅ WANDB_API_KEY environment variable set")
111
+ else:
112
+ print(f"❌ Weights & Biases login failed with exit code {exit_code}")
113
+ if stderr_buffer:
114
+ print(f"Error: {stderr_buffer}")
115
+
116
+ return exit_code == 0, stdout_buffer, stderr_buffer
117
+
118
+ def handle_huggingface_login(sandbox, current_dir):
119
+ """Handle Hugging Face login with proper token input"""
120
+ # Define _to_str function locally to avoid NameError
121
+ def _to_str(maybe_bytes):
122
+ try:
123
+ return (maybe_bytes.decode('utf-8') if isinstance(maybe_bytes, (bytes, bytearray)) else maybe_bytes)
124
+ except UnicodeDecodeError:
125
+ # Handle non-UTF-8 bytes by replacing invalid characters
126
+ if isinstance(maybe_bytes, (bytes, bytearray)):
127
+ return maybe_bytes.decode('utf-8', errors='replace')
128
+ else:
129
+ return str(maybe_bytes)
130
+ except Exception:
131
+ # Last resort fallback
132
+ return str(maybe_bytes)
133
+
134
+ print("\n🔑 HUGGING FACE LOGIN")
135
+ print("="*60)
136
+ print("Setting up Hugging Face credentials")
137
+
138
+ # Get token from user
139
+ token = prompt_for_hf_token()
140
+ if not token:
141
+ print("❌ No token provided. Cannot continue with Hugging Face login.")
142
+ return False, "", "No Hugging Face token provided"
143
+
144
+ # Use non-interactive login
145
+ cmd = f"huggingface-cli login --token {token} --add-to-git-credential"
146
+ print(f"🔄 Running non-interactive login command")
147
+
148
+ # Execute the command
149
+ result = sandbox.exec("bash", "-c", f"cd {current_dir} && {cmd}")
150
+
151
+ # Collect output
152
+ stdout_lines = []
153
+ stderr_lines = []
154
+
155
+ for line in result.stdout:
156
+ line_str = _to_str(line)
157
+ stdout_lines.append(line_str)
158
+ sys.stdout.write(line_str)
159
+ sys.stdout.flush()
160
+
161
+ for line in result.stderr:
162
+ line_str = _to_str(line)
163
+ stderr_lines.append(line_str)
164
+ sys.stderr.write(line_str)
165
+ sys.stderr.flush()
166
+
167
+ result.wait()
168
+ exit_code = result.returncode
169
+
170
+ stdout_buffer = ''.join(stdout_lines)
171
+ stderr_buffer = ''.join(stderr_lines)
172
+
173
+ if exit_code == 0:
174
+ print("✅ Hugging Face login successful")
175
+ # Also set the environment variable for this session
176
+ os.environ["HF_TOKEN"] = token
177
+ print("✅ HF_TOKEN environment variable set")
178
+ else:
179
+ print(f"❌ Hugging Face login failed with exit code {exit_code}")
180
+ if stderr_buffer:
181
+ print(f"Error: {stderr_buffer}")
182
+
183
+ return exit_code == 0, stdout_buffer, stderr_buffer
184
+
185
+ def handle_interactive_command(cmd, sandbox, current_dir):
186
+ """Handle interactive commands by prompting the user for input"""
187
+ print(f"⚠️ Interactive command detected: {cmd}")
188
+ print("⚠️ Some prompts may not be visible. If the command appears stuck, it may be waiting for input.")
189
+
190
+ # This is a placeholder for more sophisticated interactive command handling
191
+ # In a real implementation, you would need to handle specific interactive commands differently
192
+ return None
193
+
194
+ def call_openai_for_debug(command, error_output, api_key=None, current_dir=None, sandbox=None):
195
+ """Call OpenAI to debug a failed command and suggest a fix"""
196
+ # Define _to_str function locally to avoid NameError
197
+ def _to_str(maybe_bytes):
198
+ try:
199
+ return (maybe_bytes.decode('utf-8') if isinstance(maybe_bytes, (bytes, bytearray)) else maybe_bytes)
200
+ except UnicodeDecodeError:
201
+ # Handle non-UTF-8 bytes by replacing invalid characters
202
+ if isinstance(maybe_bytes, (bytes, bytearray)):
203
+ return maybe_bytes.decode('utf-8', errors='replace')
204
+ else:
205
+ return str(maybe_bytes)
206
+ except Exception:
207
+ # Last resort fallback
208
+ return str(maybe_bytes)
209
+
210
+ # Skip debugging for certain commands that commonly return non-zero exit codes
211
+ # but aren't actually errors (like test commands)
212
+ if command.strip().startswith("test "):
213
+ print("🔍 Skipping debugging for test command - non-zero exit code is expected behavior")
214
+ return None
215
+
216
+ # Validate error_output - if it's empty, we can't debug effectively
217
+ if not error_output or not error_output.strip():
218
+ print("⚠️ Error output is empty. Cannot effectively debug the command.")
219
+ print("⚠️ Skipping OpenAI debugging due to lack of error information.")
220
+ return None
221
+
222
+ if not api_key:
223
+ # Try to get API key from environment
224
+ api_key = os.environ.get("OPENAI_API_KEY")
225
+
226
+ if not api_key:
227
+ print("\n" + "="*60)
228
+ print("🔑 OPENAI API KEY REQUIRED FOR DEBUGGING")
229
+ print("="*60)
230
+ print("To debug failed commands, an OpenAI API key is needed.")
231
+ print("📝 Please paste your OpenAI API key below:")
232
+ print(" (Your input will be hidden for security)")
233
+ print("-" * 60)
234
+
235
+ try:
236
+ api_key = getpass.getpass("OpenAI API Key: ").strip()
237
+ if not api_key:
238
+ print("❌ No API key provided. Skipping debugging.")
239
+ return None
240
+ print("✅ API key received successfully!")
241
+ except KeyboardInterrupt:
242
+ print("\n❌ API key input cancelled by user.")
243
+ return None
244
+ except Exception as e:
245
+ print(f"❌ Error getting API key: {e}")
246
+ return None
247
+
248
+ # Get current directory context
249
+ directory_context = ""
250
+ system_info = ""
251
+
252
+ if sandbox:
253
+ try:
254
+ print("🔍 Getting system information for better debugging...")
255
+
256
+ # Get OS information
257
+ os_info_cmd = """
258
+ echo "OS Information:"
259
+ cat /etc/os-release 2>/dev/null || echo "OS release info not available"
260
+ echo -e "\nKernel Information:"
261
+ uname -a
262
+ echo -e "\nPython Information:"
263
+ python --version
264
+ echo -e "\nPackage Manager:"
265
+ which apt 2>/dev/null && echo "apt available" || echo "apt not available"
266
+ which yum 2>/dev/null && echo "yum available" || echo "yum not available"
267
+ which dnf 2>/dev/null && echo "dnf available" || echo "dnf not available"
268
+ which apk 2>/dev/null && echo "apk available" || echo "apk not available"
269
+ echo -e "\nEnvironment Variables:"
270
+ env | grep -E "^(PATH|PYTHON|VIRTUAL_ENV|HOME|USER|SHELL|LANG)" || echo "No relevant env vars found"
271
+ """
272
+
273
+ os_result = sandbox.exec("bash", "-c", os_info_cmd)
274
+ os_output = ""
275
+ for line in os_result.stdout:
276
+ os_output += _to_str(line)
277
+ os_result.wait()
278
+
279
+ system_info = f"""
280
+ System Information:
281
+ {os_output}
282
+ """
283
+ print("✅ System information gathered successfully")
284
+ except Exception as e:
285
+ print(f"⚠️ Error getting system information: {e}")
286
+ system_info = "System information not available\n"
287
+
288
+ if current_dir and sandbox:
289
+ try:
290
+ print("🔍 Getting directory context for better debugging...")
291
+
292
+ # Get current directory contents
293
+ ls_result = sandbox.exec("bash", "-c", f"cd {current_dir} && ls -la")
294
+ ls_output = ""
295
+ for line in ls_result.stdout:
296
+ ls_output += _to_str(line)
297
+ ls_result.wait()
298
+
299
+ # Get parent directory contents if this isn't root
300
+ parent_context = ""
301
+ if current_dir != "/" and "/" in current_dir:
302
+ parent_dir = os.path.dirname(current_dir)
303
+ parent_result = sandbox.exec("bash", "-c", f"cd {parent_dir} && ls -la")
304
+ parent_ls = ""
305
+ for line in parent_result.stdout:
306
+ parent_ls += _to_str(line)
307
+ parent_result.wait()
308
+ parent_context = f"\nParent directory ({parent_dir}) contents:\n{parent_ls}"
309
+
310
+ directory_context = f"""
311
+ Current directory: {current_dir}
312
+
313
+ Directory contents:
314
+ {ls_output}
315
+ {parent_context}
316
+ """
317
+ print("✅ Directory context gathered successfully")
318
+ except Exception as e:
319
+ print(f"⚠️ Error getting directory context: {e}")
320
+ directory_context = f"\nCurrent directory: {current_dir}\n"
321
+
322
+ # Prepare the API request
323
+ headers = {
324
+ "Content-Type": "application/json",
325
+ "Authorization": f"Bearer {api_key}"
326
+ }
327
+
328
+ # Create a prompt for the LLM
329
+ print("\n" + "="*60)
330
+ print("DEBUG: ERROR_OUTPUT SENT TO LLM:")
331
+ print("="*60)
332
+ print(f"{error_output}")
333
+ print("="*60 + "\n")
334
+
335
+ prompt = f"""
336
+ I'm trying to run the following command in a Linux environment:
337
+
338
+ ```
339
+ {command}
340
+ ```
341
+
342
+ But it failed with this error:
343
+
344
+ ```
345
+ {error_output}
346
+ ```
347
+ {system_info}
348
+ {directory_context}
349
+ Please analyze the error and provide ONLY a single terminal command that would fix the issue.
350
+ Consider the current directory, system information, and directory contents carefully before suggesting a solution.
351
+
352
+ IMPORTANT: For any commands that might ask for yes/no confirmation, use the appropriate non-interactive flag:
353
+ - For apt/apt-get: use -y or --yes
354
+ - For pip: use --no-input
355
+ - For rm: use -f or --force
356
+ - For other commands: check their documentation for the appropriate non-interactive flag
357
+
358
+ Do not provide any explanations, just the exact command to run.
359
+ """
360
+
361
+ # Prepare the API request payload
362
+ payload = {
363
+ "model": "gpt-4.1",
364
+ "messages": [
365
+ {"role": "system", "content": "You are a debugging assistant. Provide only the terminal command to fix the issue, analyze the issue first understand why its happening and then provide the command to fix the issue. If you see missing pytest errors, suggest 'pip install pytest'. For wandb login issues, suggest 'wandb login YOUR_API_KEY' and the system will handle prompting for the actual key."},
366
+ {"role": "user", "content": prompt}
367
+ ],
368
+ "temperature": 0.2,
369
+ "max_tokens": 300
370
+ }
371
+
372
+ try:
373
+ print("🤖 Calling OpenAI to debug the failed command...")
374
+ response = requests.post(
375
+ "https://api.openai.com/v1/chat/completions",
376
+ headers=headers,
377
+ json=payload,
378
+ timeout=30
379
+ )
380
+
381
+ if response.status_code == 200:
382
+ result = response.json()
383
+ fix_command = result["choices"][0]["message"]["content"].strip()
384
+
385
+ # Extract just the command if it's wrapped in backticks or explanation
386
+ if "```" in fix_command:
387
+ # Extract content between backticks
388
+ import re
389
+ code_blocks = re.findall(r'```(?:bash|sh)?\s*(.*?)\s*```', fix_command, re.DOTALL)
390
+ if code_blocks:
391
+ fix_command = code_blocks[0].strip()
392
+
393
+ # If the response still has explanatory text, try to extract just the command
394
+ if len(fix_command.split('\n')) > 1:
395
+ # Take the shortest non-empty line as it's likely the command
396
+ lines = [line.strip() for line in fix_command.split('\n') if line.strip()]
397
+ if lines:
398
+ fix_command = min(lines, key=len)
399
+
400
+ print(f"🔧 Suggested fix: {fix_command}")
401
+ return fix_command
402
+ else:
403
+ print(f"❌ OpenAI API error: {response.status_code} - {response.text}")
404
+ return None
405
+ except Exception as e:
406
+ print(f"❌ Error calling OpenAI API: {e}")
407
+ return None
408
+
409
+ def prompt_for_hf_token():
410
+ """Prompt user for Hugging Face token when needed"""
411
+ print("\n" + "="*60)
412
+ print("🔑 HUGGING FACE TOKEN REQUIRED")
413
+ print("="*60)
414
+ print("The training script requires a valid Hugging Face token.")
415
+ print("You can get your token from: https://huggingface.co/settings/tokens")
416
+ print("📝 Please paste your Hugging Face token below:")
417
+ print(" (Your input will be hidden for security)")
418
+ print("-" * 60)
419
+
420
+ try:
421
+ token = getpass.getpass("HF Token: ").strip()
422
+ if not token:
423
+ print("❌ No token provided.")
424
+ return None
425
+ print("✅ Token received successfully!")
426
+ return token
427
+ except KeyboardInterrupt:
428
+ print("\n❌ Token input cancelled by user.")
429
+ return None
430
+ except Exception as e:
431
+ print(f"❌ Error getting token: {e}")
432
+ return None
433
+
434
+ def create_modal_sandbox(gpu_type, repo_url=None, repo_name=None, setup_commands=None, volume_name=None):
435
+ # Execution history for tracking all commands and their results in this session
436
+ execution_history = []
437
+
438
+ # Track session start time
439
+ session_start = datetime.datetime.now().isoformat()
440
+
441
+ # Track previous errors to detect repeated failures
442
+ previous_errors = {}
443
+
444
+ # Track Python version management
445
+ conda_installed = False
446
+ python_version_switched = False
447
+ current_python_version = None
448
+
449
+ # Generate a unique app name with timestamp to avoid conflicts
450
+ timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
451
+ app_name = f"sandbox-{timestamp}"
452
+
453
+ gpu_configs = {
454
+ 'A10G': {'gpu': 'A10G', 'memory': 24},
455
+ 'A100': {'gpu': 'A100-SXM4-40GB', 'memory': 40},
456
+ 'H100': {'gpu': 'H100', 'memory': 80},
457
+ 'T4': {'gpu': 'T4', 'memory': 16},
458
+ 'V100': {'gpu': 'V100-SXM2-16GB', 'memory': 16}
459
+ }
460
+
461
+ if gpu_type not in gpu_configs:
462
+ print(f"⚠️ Unknown GPU type: {gpu_type}. Using A10G as default.")
463
+ gpu_type = 'A10G'
464
+
465
+ gpu_spec = gpu_configs[gpu_type]
466
+ print(f"🚀 Creating Modal sandbox with {gpu_spec['gpu']} GPU ({gpu_spec['memory']}GB VRAM)")
467
+
468
+ # Initialize uv_path variable
469
+ uv_path = ""
470
+
471
+ # Setup volume if specified
472
+ volume = None
473
+ volume_mount_path = "/persistent"
474
+
475
+ if volume_name:
476
+ print(f"📦 Setting up volume: {volume_name}")
477
+ try:
478
+ # Try to get existing volume or create new one
479
+ volume = modal.Volume.from_name(volume_name, create_if_missing=True)
480
+ print(f"✅ Volume '{volume_name}' ready for use")
481
+ except Exception as e:
482
+ print(f"⚠️ Could not setup volume '{volume_name}': {e}")
483
+ print("⚠️ Continuing without persistent volume")
484
+ volume = None
485
+ else:
486
+ # Create a default volume for this session
487
+ default_volume_name = f"sandbox-vol-{timestamp}"
488
+ print(f"📦 Creating default volume: {default_volume_name}")
489
+ try:
490
+ volume = modal.Volume.from_name(default_volume_name, create_if_missing=True)
491
+ volume_name = default_volume_name
492
+ print(f"✅ Default volume '{default_volume_name}' created")
493
+ except Exception as e:
494
+ print(f"⚠️ Could not create default volume: {e}")
495
+ print("⚠️ Continuing without persistent volume")
496
+ volume = None
497
+
498
+ # Enable output for image building
499
+ with modal.enable_output():
500
+ # Create a Modal app and sandbox
501
+ print(f"🚀 Creating Modal sandbox with GPU: {gpu_type.lower()} (App: {app_name})...")
502
+ # Always use lookup with create_if_missing=True to properly initialize the app
503
+ app = modal.App.lookup(app_name, create_if_missing=True)
504
+ print(f"Created app: {app_name}")
505
+
506
+ # Create the sandbox with increased timeout for long-running operations
507
+ print("⏱️ Setting 30-minute timeout for long-running installations...")
508
+
509
+ # Setup volume mount if available
510
+ volumes = {}
511
+ if volume:
512
+ volumes[volume_mount_path] = volume
513
+ print(f"📦 Mounting volume '{volume_name}' at {volume_mount_path}")
514
+
515
+ cuda_image = modal.Image.from_registry("nvidia/cuda:12.8.1-devel-ubuntu24.04", add_python="3.12")
516
+
517
+ sandbox = modal.Sandbox.create(
518
+ "sleep", "infinity",
519
+ app=app,
520
+ gpu=gpu_type.lower(),
521
+ image=cuda_image,
522
+ timeout=3600, # 40 minutes instead of 15 minutes
523
+ volumes=volumes if volumes else None
524
+ )
525
+
526
+ # Get the sandbox ID for reference
527
+ sandbox_id = sandbox.object_id
528
+ print(f"📋 Sandbox ID: {sandbox_id}")
529
+
530
+ # Wait a moment for the container to be registered
531
+ print("⏳ Waiting for container to be registered...")
532
+ time.sleep(5) # Increased wait time
533
+
534
+ # Function to extract container ID from text output
535
+ def extract_container_id_from_text(output):
536
+ print("Extracting container ID from text output...")
537
+
538
+ # First, try to find lines with the app name
539
+ lines = output.split('\n')
540
+ app_lines = [line for line in lines if app_name in line]
541
+
542
+ if app_lines:
543
+ # Get the first line with the app name
544
+ app_line = app_lines[0]
545
+ print(f"Found line with app name: {app_line}")
546
+
547
+ # Try to extract the container ID
548
+ if '│' in app_line:
549
+ parts = app_line.split('│')
550
+ if len(parts) >= 2:
551
+ container_id_part = parts[1].strip()
552
+ if container_id_part.startswith('ta-'):
553
+ return container_id_part
554
+
555
+ # If that didn't work, try regex pattern matching
556
+ container_matches = re.findall(r'ta-[A-Z0-9]+', output)
557
+ if container_matches:
558
+ return container_matches[0]
559
+
560
+ return None
561
+
562
+ # Get the container ID using multiple approaches
563
+ print("📋 Getting container ID...")
564
+ container_id = None
565
+
566
+ # Approach 1: Use modal container list --json
567
+ try:
568
+ print("Trying JSON approach...")
569
+ result = subprocess.run(["modal", "container", "list", "--json"], capture_output=True, text=True)
570
+ output = result.stdout
571
+ print(f"JSON output: {output}")
572
+
573
+ import json
574
+ try:
575
+ containers = json.loads(output)
576
+ print(f"Parsed JSON: {containers}")
577
+ if containers and isinstance(containers, list) and len(containers) > 0:
578
+ # The container ID is in the "Container ID" field, not "id"
579
+ container_id = containers[0].get("Container ID")
580
+ if container_id:
581
+ print(f"📋 Found container ID from JSON: {container_id}")
582
+ else:
583
+ # Try lowercase keys as a fallback
584
+ container_id = containers[0].get("container_id") or containers[0].get("container id")
585
+ if container_id:
586
+ print(f"📋 Found container ID from JSON with lowercase keys: {container_id}")
587
+ except json.JSONDecodeError as json_err:
588
+ print(f"JSON parse error: {json_err}")
589
+ except Exception as e:
590
+ print(f"Error with JSON approach: {e}")
591
+
592
+ # Approach 2: Use modal container list with text parsing
593
+ if not container_id:
594
+ try:
595
+ print("Trying text output approach...")
596
+ result = subprocess.run(["modal", "container", "list"], capture_output=True, text=True)
597
+ output = result.stdout
598
+ print("Modal container list output:")
599
+ print(output)
600
+
601
+ container_id = extract_container_id_from_text(output)
602
+ if container_id:
603
+ print(f"📋 Found container ID from text: {container_id}")
604
+ except Exception as e:
605
+ print(f"Error with text approach: {e}")
606
+
607
+ # Approach 3: Use shell command to get first container
608
+ if not container_id:
609
+ try:
610
+ print("Trying shell command approach...")
611
+ cmd = "modal container list | grep -v Container | grep -v '─' | head -1 | awk '{print $1}'"
612
+ result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
613
+ output = result.stdout.strip()
614
+ print(f"Shell command output: {output}")
615
+
616
+ if output and output.startswith('ta-'):
617
+ container_id = output
618
+ print(f"📋 Found container ID from shell command: {container_id}")
619
+ except Exception as e:
620
+ print(f"Error with shell command approach: {e}")
621
+
622
+ # Approach 4: Get all containers and find the one with our app
623
+ if not container_id:
624
+ try:
625
+ print("Trying app matching approach...")
626
+ cmd = "modal container list"
627
+ result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
628
+ output = result.stdout
629
+
630
+ # Look for our app name in the output
631
+ if app_name in output:
632
+ print(f"Found {app_name} in container list")
633
+ # Try to get the container ID from the same line
634
+ lines = output.split('\n')
635
+ for line in lines:
636
+ if app_name in line:
637
+ print(f"Found line: {line}")
638
+ # Try to extract the first column
639
+ if '│' in line:
640
+ container_id_part = line.split('│')[1].strip()
641
+ if container_id_part.startswith('ta-'):
642
+ container_id = container_id_part
643
+ print(f"📋 Found container ID from app matching: {container_id}")
644
+ break
645
+ except Exception as e:
646
+ print(f"Error with app matching approach: {e}")
647
+
648
+ # Final fallback: Use sandbox ID to create a container ID
649
+ if not container_id:
650
+ print("⚠️ All approaches failed to find container ID")
651
+ # Use sandbox ID as container prefix
652
+ short_id = sandbox_id.split('-')[1][:8] if '-' in sandbox_id else sandbox_id[:8]
653
+ container_id = f"ta-{short_id.upper()}"
654
+ print(f"📋 Using derived container ID: {container_id}")
655
+
656
+ # Ensure we have a non-None container ID
657
+ if not container_id:
658
+ print("⚠️ Critical error: Failed to determine container ID")
659
+ print("⚠️ Using a placeholder container ID")
660
+ container_id = "ta-UNKNOWN"
661
+
662
+ # Try to verify the container ID exists
663
+ print("🔍 Verifying container ID...")
664
+ verify_cmd = f"modal container logs {container_id} --tail 1 2>/dev/null || echo 'Container not found'"
665
+ verify_result = subprocess.run(verify_cmd, shell=True, capture_output=True, text=True)
666
+ if "Container not found" in verify_result.stdout:
667
+ print(f"⚠️ Container ID verification failed: {container_id}")
668
+
669
+ # Last resort: Try to find any valid container
670
+ print("🔍 Looking for any valid container as last resort...")
671
+ list_cmd = "modal container list | grep -v Container | grep -v '─' | grep -v '┏' | grep -v '┃' | head -1"
672
+ list_result = subprocess.run(list_cmd, shell=True, capture_output=True, text=True)
673
+ if list_result.stdout.strip():
674
+ print(f"Found container line: {list_result.stdout.strip()}")
675
+ # Try to extract the ID from the first column
676
+ container_line = list_result.stdout.strip()
677
+ if '│' in container_line:
678
+ possible_id = container_line.split('│')[1].strip()
679
+ if possible_id.startswith('ta-'):
680
+ container_id = possible_id
681
+ print(f"📋 Using container ID from list as last resort: {container_id}")
682
+
683
+ # Verify this container
684
+ verify_cmd = f"modal container logs {container_id} --tail 1 2>/dev/null || echo 'Container not found'"
685
+ verify_result = subprocess.run(verify_cmd, shell=True, capture_output=True, text=True)
686
+ if "Container not found" not in verify_result.stdout:
687
+ print(f"✅ Last resort container ID verified: {container_id}")
688
+ else:
689
+ print("⚠️ Last resort container ID also failed verification")
690
+
691
+ print("⚠️ Container connection may fail. You may need to connect manually.")
692
+ else:
693
+ print(f"✅ Container ID verified: {container_id}")
694
+
695
+ # Function to convert bytes to string
696
+ def _to_str(maybe_bytes):
697
+ try:
698
+ return (maybe_bytes.decode('utf-8') if isinstance(maybe_bytes, (bytes, bytearray)) else maybe_bytes)
699
+ except UnicodeDecodeError:
700
+ # Handle non-UTF-8 bytes by replacing invalid characters
701
+ if isinstance(maybe_bytes, (bytes, bytearray)):
702
+ return maybe_bytes.decode('utf-8', errors='replace')
703
+ else:
704
+ return str(maybe_bytes)
705
+ except Exception:
706
+ # Last resort fallback
707
+ return str(maybe_bytes)
708
+
709
+ # Skip the persistent shell approach for now due to async stream complexity
710
+ print("🔍 Modal's async streams require complex async handling")
711
+ print("🔄 Switching to individual command execution approach for reliability...")
712
+
713
+ # Initialize state tracking variables
714
+ current_dir = "/"
715
+ execution_history = []
716
+
717
+ # Function to run commands using individual sandbox.exec calls
718
+ def run_command(cmd, show_output=True, retry_count=0, max_retries=3, debug_with_llm=True, timeout=600):
719
+ """
720
+ Execute a command in the sandbox with error handling and automatic retries.
721
+
722
+ When a command fails and is fixed by the LLM debugging system, the retry count
723
+ is reset to 0, so successful fixes don't count against the maximum retry limit.
724
+ This ensures that a command that's been fixed gets a fresh set of retry attempts.
725
+ """
726
+ # Use the outer scope variables
727
+ nonlocal current_dir, execution_history, sandbox, previous_errors
728
+ nonlocal conda_installed, python_version_switched, current_python_version
729
+
730
+ # Record command start time
731
+ command_start_time = datetime.datetime.now().isoformat()
732
+ start_time = time.time()
733
+
734
+ # Prevent infinite retry loops
735
+ if retry_count >= max_retries:
736
+ print(f"⚠️ Maximum retry count ({max_retries}) reached. Stopping retries.")
737
+ return False, "", f"Maximum retry count ({max_retries}) reached"
738
+
739
+ # Special handling for cd commands to prevent common navigation errors
740
+ if cmd.strip().startswith("cd "):
741
+ # Extract the target directory from the cd command
742
+ cd_parts = cmd.split(None, 1)
743
+ if len(cd_parts) >= 2:
744
+ target_dir = cd_parts[1].strip().strip('"\'')
745
+
746
+ # Check if this is a repo name that matches the end of current_dir
747
+ # This prevents errors like "cd repo-name" when already in "/root/repo-name"
748
+ if (target_dir != "/" and target_dir != "." and target_dir != ".." and
749
+ not target_dir.startswith("/") and not target_dir.startswith("./") and
750
+ not target_dir.startswith("../") and current_dir.endswith("/" + target_dir)):
751
+ print(f"⚠️ Detected redundant directory navigation: {cmd}")
752
+ print(f"📂 Already in the correct directory: {current_dir}")
753
+ print(f"✅ Skipping unnecessary navigation command")
754
+ return True, f"Already in directory {current_dir}", ""
755
+
756
+ # Remove any parenthetical text that could cause syntax errors in bash
757
+ if '(' in cmd:
758
+ original_cmd = cmd
759
+ cmd = re.sub(r'\([^)]*\)', '', cmd).strip()
760
+ print(f"🔄 Removing parenthetical text:")
761
+ print(f" Original: {original_cmd}")
762
+ print(f" Cleaned: {cmd}")
763
+
764
+ # Convert pip install commands to use uv for faster installation
765
+ original_cmd = cmd
766
+ if 'uv_path' in globals() and uv_path and ('pip install' in cmd or 'pip3 install' in cmd) and not cmd.startswith(uv_path):
767
+ # Replace pip/pip3 install with uv pip install, but only if not already using uv
768
+ cmd = cmd.replace('pip install', f'{uv_path} pip install')
769
+ cmd = cmd.replace('pip3 install', f'{uv_path} pip install')
770
+ print(f"🚀 Converting to uv for faster installation:")
771
+ print(f" Original: {original_cmd}")
772
+ print(f" Converted: {cmd}")
773
+
774
+ print(f"\n▶ {cmd}\n")
775
+
776
+ # Check if this is a potentially long-running command
777
+ long_running_patterns = [
778
+ 'pip install', 'apt install', 'yum install',
779
+ 'wget', 'curl', 'git clone', 'npm install', 'yarn install',
780
+ 'cmake', 'make', 'gcc', 'g++', 'python setup.py'
781
+ ]
782
+
783
+ is_long_running = any(pattern in cmd.lower() for pattern in long_running_patterns)
784
+ if is_long_running:
785
+ print(f"⏱️ Detected potentially long-running command. This may take several minutes...")
786
+ print(f"📦 Large packages (like PyTorch) can take 5-10 minutes to download and install.")
787
+ print(f"🔄 The container has a 30-minute timeout to accommodate this.")
788
+
789
+ # Use the original command without modification for interactivity
790
+ cmd_to_execute = cmd
791
+
792
+ # Special handling for huggingface-cli login command
793
+ if "huggingface-cli login" in cmd_to_execute:
794
+ print("🔍 Detected huggingface-cli login command")
795
+ print("🔄 Using non-interactive login approach with token instead")
796
+
797
+ # Check if the command already has a token
798
+ if "--token" in cmd_to_execute:
799
+ print("✅ Command already includes token parameter")
800
+ else:
801
+ # Prompt for HF token
802
+ hf_token = prompt_for_hf_token()
803
+ if hf_token:
804
+ # Replace with non-interactive command
805
+ cmd_to_execute = f"huggingface-cli login --token {hf_token} --add-to-git-credential"
806
+ print(f"🔄 Using non-interactive command: {cmd_to_execute}")
807
+ else:
808
+ print("❌ No token provided. Cannot continue with Hugging Face login.")
809
+ return False, "", "No Hugging Face token provided"
810
+
811
+ # Special handling for wandb login command
812
+ elif "wandb login" in cmd_to_execute and "YOUR_API_KEY" not in cmd_to_execute:
813
+ print("🔍 Detected Weights & Biases login command")
814
+ print("🔄 Using API key approach for non-interactive login")
815
+
816
+ # Check if the command already includes an API key
817
+ has_api_key = False
818
+ cmd_parts = cmd_to_execute.split()
819
+ for part in cmd_parts:
820
+ if part != "wandb" and part != "login" and not part.startswith("-"):
821
+ has_api_key = True
822
+ break
823
+
824
+ if not has_api_key:
825
+ # Prompt for W&B API key
826
+ print("\n" + "="*60)
827
+ print("🔑 WEIGHTS & BIASES API KEY REQUIRED")
828
+ print("="*60)
829
+ print("You can get your API key from: https://wandb.ai/authorize")
830
+ print("📝 Please paste your W&B API key below:")
831
+ print(" (Your input will be hidden for security)")
832
+ print("-" * 60)
833
+
834
+ try:
835
+ api_key = getpass.getpass("W&B API Key: ").strip()
836
+ if not api_key:
837
+ print("❌ No API key provided. Cannot continue with W&B login.")
838
+ return False, "", "No W&B API key provided"
839
+
840
+ # Validate API key length (typically 40 characters)
841
+ if len(api_key) != 40:
842
+ print(f"⚠️ Warning: API key should be 40 characters long, yours was {len(api_key)}")
843
+ confirm = input("Continue anyway? (yes/no): ").strip().lower()
844
+ if confirm not in ["yes", "y"]:
845
+ print("❌ W&B login cancelled.")
846
+ return False, "", "W&B login cancelled"
847
+
848
+ print("✅ API key received successfully!")
849
+
850
+ # Replace with non-interactive command
851
+ cmd_to_execute = f"wandb login {api_key}"
852
+ print(f"🔄 Using non-interactive command: wandb login [API_KEY_HIDDEN]")
853
+ except KeyboardInterrupt:
854
+ print("\n❌ API key input cancelled by user.")
855
+ return False, "", "W&B API key input cancelled"
856
+ except Exception as e:
857
+ print(f"❌ Error getting API key: {e}")
858
+ return False, "", f"Error getting W&B API key: {e}"
859
+
860
+ # Validate the command before execution
861
+ if not cmd_to_execute or cmd_to_execute.strip() == "":
862
+ print("⚠️ Empty command detected, skipping execution")
863
+ return False, "", "Empty command"
864
+
865
+ # Sanitize command to prevent issues with special characters
866
+ # Remove any null bytes or other problematic characters
867
+ cmd_to_execute = cmd_to_execute.replace('\x00', '').strip()
868
+
869
+ if len(cmd_to_execute) > 10000: # Prevent extremely long commands
870
+ print("⚠️ Command too long, truncating")
871
+ cmd_to_execute = cmd_to_execute[:10000]
872
+
873
+ # Prepare the command with environment variables and error handling
874
+ full_command = f"""
875
+ # Change to current directory
876
+ cd "{current_dir}"
877
+
878
+ # Execute the command
879
+ {cmd_to_execute}
880
+ """
881
+
882
+ # Execute the command using sandbox.exec
883
+ try:
884
+ print(f"🔄 Executing command in directory: {current_dir}")
885
+
886
+ # Use sandbox.exec for individual command execution
887
+ result = sandbox.exec("bash", "-c", full_command.strip())
888
+
889
+ # Collect output in real-time - Modal streams are already set up for line-by-line streaming
890
+ stdout_lines = []
891
+ stderr_lines = []
892
+
893
+ # Process output streams in real-time - Modal handles this natively
894
+ # We don't need to use threading here as Modal's streams are designed to be consumed directly
895
+ if show_output:
896
+ print("\n--- Command Output ---")
897
+
898
+ # Track if we've shown timeout warnings
899
+ timeout_warnings = set()
900
+ last_output_time = time.time()
901
+
902
+ # Read stdout in real-time
903
+ for line in result.stdout:
904
+ # Check for timeout
905
+ current_time = time.time()
906
+ elapsed = current_time - start_time
907
+ time_since_output = current_time - last_output_time
908
+
909
+ # Show timeout warning every 30 seconds if no output for 30+ seconds
910
+ if time_since_output > 30 and int(time_since_output) // 30 not in timeout_warnings:
911
+ warning_time = int(time_since_output) // 30 * 30
912
+ timeout_warnings.add(int(time_since_output) // 30)
913
+ print(f"Still running after {int(elapsed)} seconds...")
914
+
915
+ # If total time exceeds timeout, break
916
+ if elapsed > timeout:
917
+ print(f"⚠️ Command timed out after {timeout} seconds")
918
+ # Force terminate the command
919
+ try:
920
+ result.terminate()
921
+ except:
922
+ pass
923
+ return False, "Command timed out", f"Command execution exceeded timeout of {timeout} seconds"
924
+
925
+ # Process the line
926
+ line_str = _to_str(line)
927
+ stdout_lines.append(line_str)
928
+ if show_output:
929
+ # Print immediately with flush to ensure real-time display
930
+ print(line_str, end="", flush=True)
931
+
932
+ # Update last output time
933
+ last_output_time = time.time()
934
+
935
+ # Read stderr in real-time
936
+ for line in result.stderr:
937
+ # Check for timeout
938
+ current_time = time.time()
939
+ elapsed = current_time - start_time
940
+ time_since_output = current_time - last_output_time
941
+
942
+ # Show timeout warning every 30 seconds if no output for 30+ seconds
943
+ if time_since_output > 30 and int(time_since_output) // 30 not in timeout_warnings:
944
+ warning_time = int(time_since_output) // 30 * 30
945
+ timeout_warnings.add(int(time_since_output) // 30)
946
+ print(f"Still running after {int(elapsed)} seconds...")
947
+
948
+ # If total time exceeds timeout, break
949
+ if elapsed > timeout:
950
+ print(f"⚠️ Command timed out after {timeout} seconds")
951
+ # Force terminate the command
952
+ try:
953
+ result.terminate()
954
+ except:
955
+ pass
956
+ return False, "Command timed out", f"Command execution exceeded timeout of {timeout} seconds"
957
+
958
+ # Process the line
959
+ line_str = _to_str(line)
960
+ stderr_lines.append(line_str)
961
+ if show_output:
962
+ # Print immediately with flush to ensure real-time display
963
+ print(line_str, end="", file=sys.stderr, flush=True)
964
+
965
+ # Update last output time
966
+ last_output_time = time.time()
967
+
968
+ if show_output:
969
+ print("--- End Output ---\n")
970
+
971
+ stdout_buffer = ''.join(stdout_lines)
972
+ stderr_buffer = ''.join(stderr_lines)
973
+
974
+ # Wait for the process to complete before accessing returncode
975
+ result.wait()
976
+ exit_code = result.returncode
977
+
978
+ except Exception as e:
979
+ print(f"❌ Error executing command: {e}")
980
+ return False, "", str(e)
981
+
982
+ # Record command completion time
983
+ command_end_time = datetime.datetime.now().isoformat()
984
+
985
+ # Calculate duration in seconds
986
+ start_dt = datetime.datetime.fromisoformat(command_start_time)
987
+ end_dt = datetime.datetime.fromisoformat(command_end_time)
988
+ duration = (end_dt - start_dt).total_seconds()
989
+
990
+ # Record this command execution in history
991
+ execution_record = {
992
+ "command": cmd_to_execute,
993
+ "original_command": cmd if cmd != cmd_to_execute else None,
994
+ "start_time": command_start_time,
995
+ "end_time": command_end_time,
996
+ "duration_seconds": duration,
997
+ "exit_code": exit_code,
998
+ "stdout": stdout_buffer,
999
+ "stderr": stderr_buffer,
1000
+ "directory": current_dir
1001
+ }
1002
+ execution_history.append(execution_record)
1003
+
1004
+ # Update current directory if this was a cd command and it succeeded
1005
+ if cmd_to_execute.strip().startswith("cd ") and exit_code == 0:
1006
+ # Extract the target directory from the cd command
1007
+ cd_parts = cmd_to_execute.split(None, 1)
1008
+ if len(cd_parts) >= 2:
1009
+ target_dir = cd_parts[1].strip('"\'')
1010
+
1011
+ # Store the previous directory for logging
1012
+ previous_dir = current_dir
1013
+
1014
+ # Handle different types of paths
1015
+ if target_dir.startswith('/'):
1016
+ # Absolute path
1017
+ current_dir = target_dir
1018
+ elif target_dir == '..':
1019
+ # Parent directory
1020
+ current_dir = '/'.join(current_dir.rstrip('/').split('/')[:-1]) or '/'
1021
+ elif target_dir == '.':
1022
+ # Current directory - no change
1023
+ pass
1024
+ else:
1025
+ # Relative path - handle special case where target is already at the end of current_dir
1026
+ if current_dir.endswith('/' + target_dir):
1027
+ print(f"📂 Already in directory {current_dir}, no change needed")
1028
+ else:
1029
+ current_dir = f"{current_dir.rstrip('/')}/{target_dir}"
1030
+
1031
+ print(f"📂 Updated current directory: {previous_dir} -> {current_dir}")
1032
+ execution_record["new_current_dir"] = current_dir
1033
+
1034
+ # Verify the directory actually exists
1035
+ verify_cmd = f"test -d \"{current_dir}\""
1036
+ verify_result = sandbox.exec("bash", "-c", verify_cmd)
1037
+ verify_result.wait()
1038
+
1039
+ if verify_result.returncode != 0:
1040
+ print(f"⚠️ Warning: Directory {current_dir} does not exist")
1041
+ print(f"⚠️ Reverting to previous directory: {previous_dir}")
1042
+ current_dir = previous_dir
1043
+ execution_record["new_current_dir"] = current_dir
1044
+
1045
+ # Check for errors and handle Hugging Face token issues
1046
+ if exit_code != 0:
1047
+ # Check for specific Hugging Face token errors
1048
+ hf_token_error_patterns = [
1049
+ "Token is required",
1050
+ "LocalTokenNotFoundError",
1051
+ "Invalid user token",
1052
+ "401 Client Error: Unauthorized",
1053
+ "Invalid credentials in Authorization header",
1054
+ "HF_TOKEN environment variable is invalid"
1055
+ ]
1056
+
1057
+ is_hf_token_error = any(pattern in stderr_buffer for pattern in hf_token_error_patterns)
1058
+
1059
+ if is_hf_token_error:
1060
+ print(f"🔑 Detected Hugging Face token authentication error!")
1061
+ print(f"🔍 Error details: {stderr_buffer}")
1062
+
1063
+ # Prompt for the real token
1064
+ real_token = prompt_for_hf_token()
1065
+
1066
+ if real_token:
1067
+ print(f"🔄 Setting HF_TOKEN and retrying command...")
1068
+
1069
+ # Retry with the token set
1070
+ token_command = f"export HF_TOKEN='{real_token}'; {cmd_to_execute}"
1071
+ return run_command(token_command, show_output, retry_count + 1, max_retries)
1072
+ else:
1073
+ print("❌ No token provided. Cannot continue with Hugging Face operations.")
1074
+ return False, stdout_buffer, "No Hugging Face token provided"
1075
+
1076
+ # Check for "No such file or directory" errors with cd commands
1077
+ if "cd " in cmd_to_execute and "No such file or directory" in stderr_buffer:
1078
+ print("⚠️ Directory navigation error detected")
1079
+
1080
+ # Extract the target directory from the cd command
1081
+ cd_parts = cmd_to_execute.split(None, 1)
1082
+ if len(cd_parts) >= 2:
1083
+ target_dir = cd_parts[1].strip('"\'')
1084
+
1085
+ # Check if this might be a repository name that's already in the path
1086
+ if not target_dir.startswith('/') and '/' + target_dir in current_dir:
1087
+ print(f"🔍 The directory '{target_dir}' appears to be part of the current path: {current_dir}")
1088
+ print(f"⚠️ This is likely a redundant navigation attempt")
1089
+
1090
+ # If we're already in a directory that ends with the target, consider it a success
1091
+ if current_dir.endswith('/' + target_dir):
1092
+ print(f"✅ Already in the correct directory: {current_dir}")
1093
+ return True, f"Already in directory {current_dir}", ""
1094
+
1095
+ print(f"⚠️ Command failed with exit code {exit_code}")
1096
+ if stderr_buffer.strip():
1097
+ print(f"Error output: {stderr_buffer}")
1098
+
1099
+ # If command failed and we're debugging with LLM
1100
+ if debug_with_llm:
1101
+ print("🔍 Attempting to debug the failed command with OpenAI...")
1102
+
1103
+ # Check if the command is a hanging huggingface-cli login
1104
+ if "huggingface-cli login" in cmd_to_execute and not stderr_buffer.strip():
1105
+ print("🔍 Detected hanging huggingface-cli login command")
1106
+ print("🔄 Using non-interactive login approach with HF_TOKEN instead")
1107
+
1108
+ # Prompt for HF token
1109
+ hf_token = prompt_for_hf_token()
1110
+ if hf_token:
1111
+ # Set the token as environment variable and create .huggingface folder
1112
+ print("✅ Token received, setting up non-interactive authentication")
1113
+ setup_commands = [
1114
+ "mkdir -p ~/.huggingface",
1115
+ f"echo '{hf_token}' > ~/.huggingface/token",
1116
+ f"export HF_TOKEN='{hf_token}'",
1117
+ "echo 'HF_TOKEN and token file have been set up'"
1118
+ ]
1119
+
1120
+ for setup_cmd in setup_commands:
1121
+ setup_success, setup_stdout, _ = run_command(setup_cmd, show_output=True, debug_with_llm=False)
1122
+ if not setup_success:
1123
+ print(f"⚠️ Setup command failed: {setup_cmd}")
1124
+
1125
+ print("✅ Hugging Face authentication set up non-interactively")
1126
+ return True, "Hugging Face authentication set up successfully", ""
1127
+ else:
1128
+ print("❌ No token provided. Cannot set up Hugging Face authentication.")
1129
+ return False, "", "No Hugging Face token provided"
1130
+
1131
+ # Check if the error is related to missing pytest
1132
+ if "ModuleNotFoundError: No module named 'pytest'" in stderr_buffer or "ImportError: No module named pytest" in stderr_buffer:
1133
+ print("🔍 Detected missing pytest module, installing it automatically...")
1134
+ pytest_install_success, _, _ = run_command("pip install pytest", show_output=True, debug_with_llm=False)
1135
+ if pytest_install_success:
1136
+ print("✅ Successfully installed pytest, retrying original command...")
1137
+ return run_command(cmd, show_output, retry_count + 1, max_retries)
1138
+
1139
+ # Check for Python version-specific errors
1140
+ python_version_errors = [
1141
+ # Python 3.13 distutils issue
1142
+ ("ModuleNotFoundError: No module named 'distutils'", "3.13"),
1143
+ # Add more version-specific error patterns here
1144
+ ("ImportError: cannot import name 'soft_unicode' from 'markupsafe'", None),
1145
+ ("AttributeError: module 'setuptools.dist' has no attribute 'check_specifier'", None)
1146
+ ]
1147
+
1148
+ # Check if any of the error patterns match
1149
+ for error_pattern, problematic_version in python_version_errors:
1150
+ if error_pattern in stderr_buffer:
1151
+ print(f"🔍 Detected Python version-specific error: {error_pattern}")
1152
+
1153
+ # Get current Python version if not already known
1154
+ if not current_python_version:
1155
+ version_cmd = "python --version"
1156
+ version_success, version_stdout, _ = run_command(version_cmd, show_output=False, debug_with_llm=False)
1157
+ if version_success:
1158
+ current_python_version = version_stdout.strip()
1159
+ print(f"🐍 Current Python version: {current_python_version}")
1160
+
1161
+ # Check if we've already tried switching Python versions
1162
+ if python_version_switched:
1163
+ print("⚠️ Already attempted to switch Python versions once, not trying again")
1164
+ break
1165
+
1166
+ print("🔄 Attempting to fix by switching Python version...")
1167
+
1168
+ # Install conda if not already installed
1169
+ if not conda_installed:
1170
+ print("📦 Installing Miniconda to manage Python versions...")
1171
+ conda_install_cmds = [
1172
+ "apt-get update -y",
1173
+ "apt-get install -y wget bzip2",
1174
+ "wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /tmp/miniconda.sh",
1175
+ "bash /tmp/miniconda.sh -b -p /opt/conda",
1176
+ "rm /tmp/miniconda.sh",
1177
+ "echo 'export PATH=/opt/conda/bin:$PATH' >> ~/.bashrc",
1178
+ "export PATH=/opt/conda/bin:$PATH",
1179
+ "conda init bash",
1180
+ "source ~/.bashrc",
1181
+ "conda activate base"
1182
+ ]
1183
+
1184
+ for conda_cmd in conda_install_cmds:
1185
+ print(f"🔄 Running: {conda_cmd}")
1186
+ conda_success, _, _ = run_command(conda_cmd, show_output=True, debug_with_llm=False)
1187
+ if not conda_success:
1188
+ print("⚠️ Failed to install conda, continuing with system Python")
1189
+ break
1190
+
1191
+ # Check if conda was successfully installed
1192
+ conda_check_cmd = "conda --version"
1193
+ conda_check_success, conda_check_stdout, _ = run_command(conda_check_cmd, show_output=True, debug_with_llm=False)
1194
+ conda_installed = conda_check_success
1195
+
1196
+ if conda_installed:
1197
+ print(f"✅ Successfully installed conda: {conda_check_stdout.strip()}")
1198
+ else:
1199
+ print("⚠️ Failed to verify conda installation")
1200
+ break
1201
+
1202
+ # Determine target Python version
1203
+ target_version = "3.10" # Default to a stable version
1204
+ if problematic_version == "3.13":
1205
+ # If we're on 3.13 and having issues, go to 3.10
1206
+ target_version = "3.10"
1207
+ elif "3.13" in str(current_python_version):
1208
+ # If we're on 3.13 for any other error, try 3.10
1209
+ target_version = "3.10"
1210
+ elif "3.10" in str(current_python_version):
1211
+ # If we're on 3.10 and having issues, try 3.9
1212
+ target_version = "3.9"
1213
+
1214
+ print(f"🐍 Switching from {current_python_version} to Python {target_version}...")
1215
+
1216
+ # Create and activate a conda environment with the target Python version
1217
+ conda_cmds = [
1218
+ f"conda create -y -n py{target_version} python={target_version}",
1219
+ f"echo 'conda activate py{target_version}' >> ~/.bashrc",
1220
+ f"conda init bash",
1221
+ f"source ~/.bashrc",
1222
+ f"conda activate py{target_version}"
1223
+ ]
1224
+
1225
+ for conda_cmd in conda_cmds:
1226
+ print(f"🔄 Running: {conda_cmd}")
1227
+ conda_success, _, _ = run_command(conda_cmd, show_output=True, debug_with_llm=False)
1228
+ if not conda_success:
1229
+ print(f"⚠️ Failed to run conda command: {conda_cmd}")
1230
+
1231
+ # Verify Python version changed
1232
+ verify_cmd = "python --version"
1233
+ verify_success, verify_stdout, _ = run_command(verify_cmd, show_output=True, debug_with_llm=False)
1234
+
1235
+ if verify_success and target_version in verify_stdout:
1236
+ print(f"✅ Successfully switched to Python {verify_stdout.strip()}")
1237
+ python_version_switched = True
1238
+ current_python_version = verify_stdout.strip()
1239
+
1240
+ # Reinstall pip and setuptools in the new environment
1241
+ print("📦 Installing pip and setuptools in new environment...")
1242
+ run_command("pip install --upgrade pip setuptools wheel", show_output=True, debug_with_llm=False)
1243
+
1244
+ # Retry the original command with the new Python version
1245
+ print(f"🔄 Retrying original command with Python {target_version}...")
1246
+ # Reset the retry counter since we've made a significant change
1247
+ return run_command(cmd, show_output, 0, max_retries)
1248
+ else:
1249
+ print("⚠️ Failed to switch Python version, continuing with current version")
1250
+
1251
+ break
1252
+
1253
+ # Check if stderr is empty, try to use stdout as fallback
1254
+ debug_output = stderr_buffer
1255
+ if not debug_output or not debug_output.strip():
1256
+ print("⚠️ stderr is empty, checking if stdout contains error information...")
1257
+ if stdout_buffer and stdout_buffer.strip():
1258
+ print("✅ Using stdout for debugging as stderr is empty")
1259
+ debug_output = stdout_buffer
1260
+ else:
1261
+ print("⚠️ Both stderr and stdout are empty. Limited debugging information available.")
1262
+ debug_output = f"Command failed with exit code {exit_code}, but no error output was captured."
1263
+
1264
+ # Print debug output for verification
1265
+ print(f"🔍 Debug output to be sent to OpenAI ({len(debug_output)} chars):")
1266
+ print("="*60)
1267
+ print(debug_output if debug_output else "[EMPTY]")
1268
+ print("="*60)
1269
+
1270
+ fix_command = call_openai_for_debug(cmd_to_execute, debug_output, current_dir=current_dir, sandbox=sandbox)
1271
+
1272
+ if fix_command:
1273
+ print(f"🔧 OpenAI suggested fix command: {fix_command}")
1274
+
1275
+ # Check if the suggested command is "wandb login YOUR_API_KEY" or similar
1276
+ if "wandb login" in fix_command and ("YOUR_API_KEY" in fix_command or "[your_api_key]" in fix_command):
1277
+ print("🔍 Detected placeholder API key in suggested command")
1278
+ print("🔄 Prompting for actual W&B API key instead")
1279
+
1280
+ # Prompt for W&B API key
1281
+ print("\n" + "="*60)
1282
+ print("🔑 WEIGHTS & BIASES API KEY REQUIRED")
1283
+ print("="*60)
1284
+ print("You can get your API key from: https://wandb.ai/authorize")
1285
+ print("📝 Please paste your W&B API key below:")
1286
+ print(" (Your input will be hidden for security)")
1287
+ print("-" * 60)
1288
+
1289
+ try:
1290
+ api_key = getpass.getpass("W&B API Key: ").strip()
1291
+ if api_key:
1292
+ # Replace placeholder with actual API key
1293
+ fix_command = f"wandb login {api_key}"
1294
+ print(f"🔄 Using actual API key: wandb login [API_KEY_HIDDEN]")
1295
+ else:
1296
+ print("❌ No API key provided. Cannot continue with W&B login.")
1297
+ return False, stdout_buffer, stderr_buffer
1298
+ except Exception as e:
1299
+ print(f"❌ Error getting API key: {e}")
1300
+ return False, stdout_buffer, stderr_buffer
1301
+
1302
+ # Special handling for cd commands to prevent directory navigation loops
1303
+ if fix_command.strip().startswith("cd "):
1304
+ # Extract the target directory from the cd command
1305
+ cd_parts = fix_command.split(None, 1)
1306
+ if len(cd_parts) >= 2:
1307
+ target_dir = cd_parts[1].strip('"\'')
1308
+
1309
+ # Check if this is trying to navigate to a directory we're already in
1310
+ if target_dir.endswith(current_dir.split('/')[-1]) or current_dir.endswith('/' + target_dir):
1311
+ print(f"⚠️ Detected potential directory navigation loop")
1312
+ print(f"🔍 Current directory: {current_dir}")
1313
+ print(f"🔍 Suggested navigation: {target_dir}")
1314
+
1315
+ # Check if we're already in the target directory or a directory that contains it
1316
+ if current_dir.endswith('/' + target_dir) or ('/' + target_dir + '/' in current_dir):
1317
+ print(f"✅ Already in or past the target directory")
1318
+ print(f"🔄 Skipping redundant navigation and retrying the original command")
1319
+ return run_command(cmd, show_output, retry_count + 1, max_retries)
1320
+
1321
+ # Automatically run the fix command without asking for permission
1322
+ print(f"🔄 Running suggested fix command: {fix_command}")
1323
+ # Run the fix command with debugging disabled to prevent infinite loop
1324
+ fix_success, fix_stdout, fix_stderr = run_command(fix_command, show_output=True, debug_with_llm=False)
1325
+
1326
+ if fix_success:
1327
+ print("✅ Fix command succeeded!")
1328
+ # Retry the original command with reset retry count
1329
+ print(f"🔄 Retrying original command: {cmd}")
1330
+
1331
+ # Create a key for tracking this error
1332
+ error_key = f"{cmd}:{stderr_buffer[:100]}"
1333
+
1334
+ # Check if we've seen this error before
1335
+ if error_key in previous_errors:
1336
+ # We've seen this error before, don't reset the retry count
1337
+ previous_errors[error_key] += 1
1338
+ print(f"⚠️ Same error encountered {previous_errors[error_key]} times. Not resetting retry count.")
1339
+ return run_command(cmd, show_output, retry_count + 1, max_retries)
1340
+ else:
1341
+ # First time seeing this error, track it and reset retry count
1342
+ previous_errors[error_key] = 1
1343
+ print(f"🔄 Resetting retry count to 0 after successful fix")
1344
+ return run_command(cmd, show_output, 0, max_retries) # Reset retry count to 0
1345
+ else:
1346
+ print("❌ Fix command failed.")
1347
+ return False, stdout_buffer, stderr_buffer
1348
+
1349
+ return exit_code == 0, stdout_buffer, stderr_buffer
1350
+
1351
+ # Initialize the environment with basic commands
1352
+ print("🔄 Initializing environment...")
1353
+ init_commands = [
1354
+ "export PS1='$ '", # Set a simple prompt
1355
+ "export TERM=xterm-256color", # Set terminal type
1356
+ "source ~/.bashrc 2>/dev/null || true" # Source bashrc if available
1357
+ ]
1358
+
1359
+ # Add volume-specific initialization if volume is available
1360
+ if volume:
1361
+ volume_commands = [
1362
+ f"mkdir -p {volume_mount_path}/venvs", # Create virtual environments directory
1363
+ f"mkdir -p {volume_mount_path}/cache", # Create cache directory
1364
+ f"export PIP_CACHE_DIR={volume_mount_path}/cache/pip", # Pip cache
1365
+ f"export UV_CACHE_DIR={volume_mount_path}/cache/uv", # UV cache
1366
+ ]
1367
+ init_commands.extend(volume_commands)
1368
+ print(f"📦 Setting up persistent storage directories in {volume_mount_path}")
1369
+
1370
+ # Run initialization commands
1371
+ for i, init_cmd in enumerate(init_commands, 1):
1372
+ print(f"📋 Running init command {i}/{len(init_commands)}: {init_cmd}")
1373
+ success, stdout, stderr = run_command(init_cmd, show_output=False)
1374
+ if not success:
1375
+ print(f"⚠️ Init command failed: {stderr}")
1376
+
1377
+ print("✅ Environment initialization completed")
1378
+
1379
+ print("📦 Installing basic tools...")
1380
+ run_command("apt-get update && apt-get install -y git curl wget")
1381
+
1382
+ print("📦 Installing uv with pip...")
1383
+ run_command("pip install uv")
1384
+
1385
+ # Set uv path to system installation
1386
+ uv_path = "uv"
1387
+
1388
+ # Test if uv is available and working
1389
+ test_uv_cmd = f"{uv_path} --version || echo 'uv not found'"
1390
+ test_success, test_stdout, test_stderr = run_command(test_uv_cmd)
1391
+ if not test_success or 'uv not found' in test_stdout:
1392
+ print("⚠️ uv installation not found in system path, trying alternative installation...")
1393
+ # Try alternative installation method
1394
+ print("📦 Installing uv using the official installer...")
1395
+ run_command("curl -LsSf https://astral.sh/uv/install.sh | sh")
1396
+ run_command("source $HOME/.local/bin/env")
1397
+ run_command('export PATH="$HOME/.local/bin:$PATH"')
1398
+
1399
+ # Update path to the local installation
1400
+ uv_path = "$HOME/.local/bin/uv"
1401
+
1402
+ # Test again
1403
+ test_uv_cmd = f"{uv_path} --version || echo 'uv not found'"
1404
+ test_success, test_stdout, test_stderr = run_command(test_uv_cmd)
1405
+ if not test_success or 'uv not found' in test_stdout:
1406
+ print("⚠️ uv installation still failed, using standard pip")
1407
+ uv_path = ""
1408
+ else:
1409
+ print(f"✅ uv installed successfully via alternative method: {test_stdout.strip()}")
1410
+ else:
1411
+ print(f"✅ uv installed successfully via pip: {test_stdout.strip()}")
1412
+
1413
+ # Initialize repo_clone_dir for use throughout the function
1414
+ repo_clone_dir = "/root" # Always use home directory for repositories
1415
+
1416
+ # Clone repository if URL is provided
1417
+ if repo_url:
1418
+ try:
1419
+ # Extract repo name from URL
1420
+ repo_name_from_url = repo_name or repo_url.split('/')[-1].replace('.git', '')
1421
+
1422
+ print(f"📥 Cloning repository in Modal container: {repo_url}")
1423
+
1424
+ # Determine the best location for the repository
1425
+ repo_clone_dir = "/root" # Always use home directory for repositories
1426
+ print(f"📦 Using home directory for repository: {repo_clone_dir}")
1427
+
1428
+ # Ensure we're in the home directory and update current directory tracking
1429
+ cd_success, cd_stdout, cd_stderr = run_command(f"cd {repo_clone_dir}", show_output=False)
1430
+ if cd_success:
1431
+ current_dir = repo_clone_dir
1432
+ print(f"📂 Successfully changed to: {repo_clone_dir}")
1433
+ else:
1434
+ print(f"⚠️ Failed to change to {repo_clone_dir}: {cd_stderr}")
1435
+ current_dir = "/"
1436
+
1437
+ # First, list current directory contents for debugging
1438
+ print("📂 Current directory contents before cloning:")
1439
+ run_command("pwd && ls -la", show_output=True)
1440
+
1441
+ # Check if repository already exists in current location
1442
+ print(f"🔍 Checking if {repo_name_from_url} directory exists...")
1443
+
1444
+ # First ensure we're in the right directory and check with absolute path
1445
+ check_cmd = f"cd {repo_clone_dir} && test -d {repo_name_from_url}"
1446
+ success, stdout, stderr = run_command(check_cmd, show_output=False, retry_count=0, max_retries=0)
1447
+
1448
+ # The directory exists if the test command succeeds (exit code 0)
1449
+ repo_exists = success
1450
+ print(f"📂 Repository check result: exists={repo_exists} (exit code: {0 if success else 1})")
1451
+ print(f"📂 Checking in directory: {repo_clone_dir}/{repo_name_from_url}")
1452
+
1453
+ if repo_exists:
1454
+ print(f"📂 Repository directory already exists: {repo_name_from_url}")
1455
+ # Check if it's actually a git repository - disable retries to avoid bad debugging
1456
+ git_check_cmd = f"cd {repo_clone_dir}/{repo_name_from_url} && git status"
1457
+ git_check_success, git_stdout, git_stderr = run_command(git_check_cmd, show_output=False, retry_count=0, max_retries=0)
1458
+ if git_check_success:
1459
+ print(f"✅ Valid git repository found, using existing: {repo_name_from_url}")
1460
+ else:
1461
+ print(f"⚠️ Directory exists but is not a valid git repository, removing and re-cloning...")
1462
+ remove_cmd = f"cd {repo_clone_dir} && rm -rf {repo_name_from_url}"
1463
+ run_command(remove_cmd, show_output=False)
1464
+ repo_exists = False
1465
+
1466
+ if not repo_exists:
1467
+ print(f"📥 Repository does not exist, proceeding with clone...")
1468
+ print(f"📥 Cloning repository: {repo_url}")
1469
+ print(f"📥 Repository name will be: {repo_name_from_url}")
1470
+ print(f"📥 Clone location: {repo_clone_dir}")
1471
+
1472
+ # Ensure we're in the right directory before cloning
1473
+ run_command(f"cd {repo_clone_dir}", show_output=False)
1474
+
1475
+ # Execute the git clone command with verbose output - use absolute path, disable retries
1476
+ clone_cmd = f"cd {repo_clone_dir} && git clone {repo_url}"
1477
+ clone_success, clone_stdout, clone_stderr = run_command(clone_cmd, show_output=True, retry_count=0, max_retries=0)
1478
+
1479
+ print(f"📥 Clone command completed. Success: {clone_success}")
1480
+ if clone_stdout.strip():
1481
+ print(f"📥 Clone stdout: {clone_stdout.strip()}")
1482
+ if clone_stderr.strip():
1483
+ print(f"📥 Clone stderr: {clone_stderr.strip()}")
1484
+
1485
+ if not clone_success:
1486
+ print(f"❌ Failed to clone repository: {clone_stderr}")
1487
+ print("🔄 Trying alternative clone methods...")
1488
+
1489
+ # Try with different git options - use absolute path, disable retries
1490
+ print("🔄 Attempting shallow clone...")
1491
+ shallow_clone_cmd = f"cd {repo_clone_dir} && git clone --depth 1 {repo_url}"
1492
+ clone_success, clone_stdout, clone_stderr = run_command(shallow_clone_cmd, show_output=True, retry_count=0, max_retries=0)
1493
+
1494
+ print(f"📥 Shallow clone command completed. Success: {clone_success}")
1495
+ if clone_stdout.strip():
1496
+ print(f"📥 Shallow clone stdout: {clone_stdout.strip()}")
1497
+ if clone_stderr.strip():
1498
+ print(f"📥 Shallow clone stderr: {clone_stderr.strip()}")
1499
+
1500
+ if not clone_success:
1501
+ print(f"❌ Alternative clone also failed: {clone_stderr}")
1502
+ print("⚠️ Continuing without repository...")
1503
+ repo_name_from_url = None
1504
+ else:
1505
+ print(f"✅ Repository cloned successfully with shallow clone")
1506
+ else:
1507
+ print(f"✅ Repository cloned successfully")
1508
+ else:
1509
+ print(f"📂 Repository already exists, skipping clone")
1510
+
1511
+ # Verify repository directory exists and change to it
1512
+ if repo_name_from_url:
1513
+ print("📂 Verifying repository directory...")
1514
+
1515
+ # List available directories for debugging
1516
+ print("📂 Available directories after cloning:")
1517
+ run_command("ls -la", show_output=True)
1518
+
1519
+ # Check if the repository directory exists using simple test
1520
+ check_success, _, _ = run_command(f"test -d {repo_name_from_url}", show_output=False)
1521
+
1522
+ if check_success:
1523
+ print(f"📂 Repository directory confirmed: {repo_name_from_url}")
1524
+ # Change to the repository directory
1525
+ cd_success, cd_stdout, cd_stderr = run_command(f"cd {repo_name_from_url}")
1526
+ if cd_success:
1527
+ print(f"📂 Successfully changed to repository directory: {repo_name_from_url}")
1528
+ repo_dir_name = f"{repo_clone_dir}/{repo_name_from_url}" if repo_clone_dir != "/" else repo_name_from_url
1529
+ else:
1530
+ print(f"⚠️ Failed to change to repository directory: {cd_stderr}")
1531
+ repo_dir_name = repo_clone_dir
1532
+ else:
1533
+ print(f"⚠️ Repository directory not found after cloning: {repo_name_from_url}")
1534
+ print("🔍 Looking for alternative directories...")
1535
+
1536
+ # Look for any git repositories
1537
+ search_success, search_stdout, search_stderr = run_command("find . -maxdepth 1 -type d -name '.git' -exec dirname {} \\;", show_output=False)
1538
+
1539
+ if search_success and search_stdout.strip():
1540
+ found_dirs = [d.replace('./', '') for d in search_stdout.strip().split('\n') if d.strip() and d != '.']
1541
+ if found_dirs:
1542
+ repo_dir_name = f"{repo_clone_dir}/{found_dirs[0]}" if repo_clone_dir != "/" else found_dirs[0]
1543
+ print(f"📂 Found git repository: {repo_dir_name}")
1544
+ run_command(f"cd {found_dirs[0]}")
1545
+ else:
1546
+ repo_dir_name = repo_clone_dir
1547
+ print("📂 Using current directory")
1548
+ else:
1549
+ repo_dir_name = repo_clone_dir
1550
+ print("📂 Using current directory")
1551
+ else:
1552
+ repo_dir_name = repo_clone_dir
1553
+ print("📂 No valid repository, using current directory")
1554
+
1555
+ # Show final directory status
1556
+ print("📂 Final directory status:")
1557
+ run_command("pwd && ls -la", show_output=True)
1558
+
1559
+ except Exception as e:
1560
+ print(f"❌ Error during repository cloning: {e}")
1561
+ print(f"❌ Exception type: {type(e).__name__}")
1562
+ print("⚠️ Continuing without repository...")
1563
+ repo_dir_name = repo_clone_dir
1564
+ run_command("pwd && ls -la", show_output=True)
1565
+ else:
1566
+ repo_dir_name = repo_clone_dir
1567
+ print("📂 No repository URL provided, using current directory")
1568
+ run_command("pwd && ls -la", show_output=True)
1569
+
1570
+ # Run setup commands if provided - now we're already in the repository directory
1571
+ if setup_commands:
1572
+ print("⚙️ Running user setup commands in Modal container...")
1573
+
1574
+ # Check if git clone is already in the setup commands
1575
+ has_git_clone = any('git clone' in cmd for cmd in setup_commands)
1576
+
1577
+ # Only add git clone if:
1578
+ # 1. No git clone in setup commands AND
1579
+ # 2. We have a repo URL AND
1580
+ # 3. Repository was NOT already cloned successfully
1581
+ if not has_git_clone and repo_url and not repo_exists:
1582
+ print("📥 Git clone not found in setup commands and repository not yet cloned, adding it...")
1583
+ clone_cmd = f"git clone {repo_url}"
1584
+ setup_commands = [clone_cmd] + setup_commands
1585
+ print(f"📥 Added git clone command: {clone_cmd}")
1586
+ elif has_git_clone and repo_exists:
1587
+ print("⚠️ Repository already cloned successfully, removing duplicate git clone from setup commands...")
1588
+ # Remove git clone commands since repository is already cloned
1589
+ setup_commands = [cmd for cmd in setup_commands if 'git clone' not in cmd]
1590
+ print(f"📥 Removed duplicate git clone commands")
1591
+ elif repo_exists:
1592
+ print("📂 Repository already cloned successfully, skipping git clone in setup commands")
1593
+
1594
+ # Print all commands that will be executed
1595
+ print("📋 Setup commands to execute in container:")
1596
+ for i, cmd in enumerate(setup_commands, 1):
1597
+ print(f" {i}. {cmd}")
1598
+
1599
+ print(f"\n🚀 Executing commands in container directory: {repo_dir_name}")
1600
+
1601
+ # Ensure we start in the /root directory and reset current_dir
1602
+ current_dir = "/root"
1603
+ print(f"📂 Resetting working directory to: {current_dir}")
1604
+
1605
+ # Verify we can access /root directory
1606
+ verify_success, verify_output, _ = run_command("pwd", show_output=True)
1607
+ if verify_success:
1608
+ print(f"✅ Current directory verified: {verify_output.strip()}")
1609
+
1610
+ # Execute each command individually in the repository directory within the container
1611
+ for i, cmd in enumerate(setup_commands, 1):
1612
+ print(f"\n📋 Executing command {i}/{len(setup_commands)} in container: {cmd}")
1613
+
1614
+ # If this is a cd command, just run it directly
1615
+ if cmd.strip().startswith('cd '):
1616
+ # Execute the command directly (we're already in the right directory)
1617
+ success, stdout, stderr = run_command(cmd)
1618
+ continue
1619
+
1620
+ # For git clone commands, handle as before
1621
+ if 'git clone' in cmd:
1622
+ # Execute the command directly
1623
+ success, stdout, stderr = run_command(cmd)
1624
+
1625
+ if success:
1626
+ print(f"✅ Command executed successfully in container: {cmd}")
1627
+ if stdout.strip():
1628
+ print(f"📄 Output: {stdout.strip()}")
1629
+
1630
+ # Handle repository directory change as before
1631
+ print("📂 Git clone detected, attempting to change to repository directory...")
1632
+ # Extract repository name from the clone command
1633
+ parts = cmd.split()
1634
+ if len(parts) >= 3:
1635
+ clone_url = parts[2] # git clone <url>
1636
+ target_dir = clone_url.split('/')[-1].replace('.git', '')
1637
+
1638
+ # Check if we're already in the target directory
1639
+ if current_dir.endswith(f"/{target_dir}") or current_dir == f"/{target_dir}":
1640
+ print(f"📂 Already in target directory: {current_dir}")
1641
+ else:
1642
+ # The repository should now be at current_dir/target_dir
1643
+ repo_full_path = f"{current_dir.rstrip('/')}/{target_dir}"
1644
+
1645
+ # Check if directory exists using absolute path
1646
+ dir_check_success, _, _ = run_command(f"test -d '{repo_full_path}'", show_output=False)
1647
+ if dir_check_success:
1648
+ current_dir = repo_full_path
1649
+ print(f"📂 Successfully changed current directory to: {current_dir}")
1650
+ # Verify the change worked
1651
+ verify_success, verify_output, _ = run_command("pwd", show_output=True)
1652
+ if verify_success:
1653
+ print(f"✅ Directory change verified: {verify_output.strip()}")
1654
+ # List contents to confirm we're in the right place
1655
+ run_command("ls -la", show_output=True)
1656
+
1657
+ # Initialize git submodules if they exist
1658
+ print("📦 Checking for git submodules...")
1659
+ submodule_check_success, _, _ = run_command("test -f .gitmodules", show_output=False)
1660
+ if submodule_check_success:
1661
+ print("📦 Git submodules found, initializing...")
1662
+ run_command("git submodule update --init --recursive", show_output=True)
1663
+ print("✅ Git submodules initialized")
1664
+ else:
1665
+ print("📦 No git submodules found")
1666
+ else:
1667
+ print("⚠️ Directory change verification failed")
1668
+ else:
1669
+ print(f"⚠️ Repository directory {repo_full_path} not found after clone")
1670
+ print("🔍 Checking what was actually created:")
1671
+ run_command("find . -maxdepth 2 -name '*.git' -type d", show_output=True)
1672
+ run_command("ls -la", show_output=True)
1673
+ else:
1674
+ # For Python commands, make sure we're in the correct directory first
1675
+ if cmd.startswith('python '):
1676
+ # Fix the directory path issue - ensure we're in the correct repository directory
1677
+ # Check if we're in a nested directory that matches the repo name
1678
+ repo_dir_parts = current_dir.split('/')
1679
+ if len(repo_dir_parts) >= 2 and repo_dir_parts[-1] == repo_dir_parts[-2]:
1680
+ # We're in a nested directory like /root/nanoGPT/nanoGPT
1681
+ # Move up one level to /root/nanoGPT
1682
+ print(f"⚠️ Detected nested directory structure: {current_dir}")
1683
+ parent_dir = '/'.join(repo_dir_parts[:-1])
1684
+ print(f"🔄 Moving to parent directory: {parent_dir}")
1685
+ cd_success, _, _ = run_command(f"cd {parent_dir}", show_output=False)
1686
+ if cd_success:
1687
+ current_dir = parent_dir
1688
+ print(f"📂 Updated current directory to: {current_dir}")
1689
+
1690
+ # Execute the command directly (we're already in the right directory)
1691
+ success, stdout, stderr = run_command(cmd)
1692
+
1693
+ if success:
1694
+ print(f"✅ Command executed successfully in container: {cmd}")
1695
+ if stdout.strip():
1696
+ print(f"📄 Output: {stdout.strip()}")
1697
+ else:
1698
+ print(f"❌ Command failed in container: {cmd}")
1699
+ print(f"❌ Error: {stderr}")
1700
+ # Continue with next command even if this one failed
1701
+
1702
+ # Show final status of the repository directory in container
1703
+ print(f"\n📂 Final directory contents in container ({repo_dir_name}):")
1704
+ run_command("pwd && ls -la")
1705
+
1706
+ else:
1707
+ print("⚠️ No setup commands provided.")
1708
+
1709
+ # If no setup commands but we have a repo URL, at least try to clone it
1710
+ if repo_url and not repo_exists:
1711
+ print("📥 No setup commands provided, but cloning repository anyway...")
1712
+ clone_success, _, _ = run_command(f"git clone {repo_url}", show_output=True)
1713
+ if clone_success:
1714
+ print(f"✅ Repository cloned successfully")
1715
+ # Try to change to the repository directory
1716
+ if repo_name_from_url:
1717
+ run_command(f"cd {repo_name_from_url}")
1718
+ print("📂 Final directory status after clone:")
1719
+ run_command("pwd && ls -la", show_output=True)
1720
+
1721
+ # Write container ID to file for future reference
1722
+ with open(os.path.expanduser("~/.modal_last_container_id"), "w") as f:
1723
+ f.write(container_id)
1724
+
1725
+ # Print connection instructions
1726
+ print(f"✅ Modal sandbox created successfully!")
1727
+ print(f"📋 Sandbox ID: {sandbox_id}")
1728
+ print(f"📋 Container ID: {container_id}")
1729
+ if volume:
1730
+ print(f"📦 Volume: {volume_name} (mounted at {volume_mount_path})")
1731
+ print(f"💾 Persistent storage available for pip and uv caches")
1732
+ print(f"📂 Repositories will be cloned in home directory (/root) for faster access")
1733
+ print("🔗 To connect to this container, run:")
1734
+ print(f"modal container exec --pty {container_id} bash")
1735
+ print("⏳ Sandbox will remain running until you terminate it with:")
1736
+ print(f"modal sandbox terminate {sandbox_id}")
1737
+
1738
+ # Try to open a new terminal window and connect to the container
1739
+ if container_id:
1740
+ print("🖥️ Attempting to open new terminal window...")
1741
+ # Use osascript to open a new terminal with the modal shell command
1742
+ terminal_script = f'''
1743
+ tell application "Terminal"
1744
+ do script "modal shell {container_id}"
1745
+ activate
1746
+ end tell
1747
+ '''
1748
+
1749
+ try:
1750
+ result = subprocess.run(['osascript', '-e', terminal_script],
1751
+ capture_output=True, text=True, timeout=30)
1752
+ if result.returncode == 0:
1753
+ print("✅ New terminal window opened successfully")
1754
+ else:
1755
+ print(f"⚠️ Failed to open terminal window: {result.stderr}")
1756
+
1757
+ # Try alternative approach with iTerm2 if Terminal failed
1758
+ print("🔄 Trying with iTerm2 instead...")
1759
+ iterm_script = f'''
1760
+ tell application "iTerm"
1761
+ create window with default profile
1762
+ tell current session of current window
1763
+ write text "modal shell {container_id}"
1764
+ end tell
1765
+ end tell
1766
+ '''
1767
+
1768
+ try:
1769
+ iterm_result = subprocess.run(['osascript', '-e', iterm_script],
1770
+ capture_output=True, text=True, timeout=30)
1771
+ if iterm_result.returncode == 0:
1772
+ print("✅ New iTerm2 window opened successfully")
1773
+ else:
1774
+ print(f"⚠️ Failed to open iTerm2 window: {iterm_result.stderr}")
1775
+ print("📝 You can manually connect using:")
1776
+ print(f" modal shell {container_id}")
1777
+ except Exception as e:
1778
+ print(f"⚠️ Error opening iTerm2: {e}")
1779
+ print("📝 You can manually connect using:")
1780
+ print(f" modal shell {container_id}")
1781
+ except subprocess.TimeoutExpired:
1782
+ print("⚠️ Terminal opening timed out")
1783
+ except Exception as e:
1784
+ print(f"⚠️ Error opening terminal: {e}")
1785
+ print("📝 You can manually connect using:")
1786
+ print(f" modal shell {container_id}")
1787
+
1788
+ # Also provide manual connection instructions
1789
+ print("\n" + "="*60)
1790
+ print("🚀 SANDBOX READY!")
1791
+ print("="*60)
1792
+ print(f"📋 Sandbox ID: {sandbox_id}")
1793
+ print(f"🆔 Container ID: {container_id}")
1794
+ if volume:
1795
+ print(f"💾 Volume: {volume_name} mounted at {volume_mount_path}")
1796
+ print("📁 Persistent storage available for caches and repositories")
1797
+ print("\n🔗 To connect to your container, run:")
1798
+ print(f" modal shell {container_id}")
1799
+ print("="*60)
1800
+ else:
1801
+ print("❌ No container ID available for connection")
1802
+
1803
+ return {
1804
+ "run_command": run_command,
1805
+ "current_dir": current_dir,
1806
+ "execution_history": execution_history,
1807
+ "container_id": container_id,
1808
+ "sandbox_id": sandbox_id
1809
+ }
1810
+
1811
+ def fetch_setup_commands_from_api(repo_url):
1812
+ """Fetch setup commands from the GitIngest API using real repository analysis."""
1813
+ import tempfile
1814
+ import subprocess
1815
+ import os
1816
+ import shutil
1817
+ import json
1818
+
1819
+ api_url = "http://localhost:3000/api/analyze-with-gitingest"
1820
+
1821
+ print(f"🔍 Fetching setup commands from API for repository: {repo_url}")
1822
+
1823
+ # Check if gitingest command line tool is available - try multiple possible command names
1824
+ has_gitingest_cli = False
1825
+ gitingest_cmd_name = None
1826
+
1827
+ # Try the standard command name first
1828
+ try:
1829
+ print(f"🔍 Checking for GitIngest CLI tool...")
1830
+ result = subprocess.run(["gitingest", "--help"], check=True, capture_output=True, text=True)
1831
+ has_gitingest_cli = True
1832
+ gitingest_cmd_name = "gitingest"
1833
+ print(f"✅ GitIngest CLI tool found")
1834
+ except (subprocess.SubprocessError, FileNotFoundError) as e:
1835
+ print(f" - GitIngest command not found: {str(e)}")
1836
+
1837
+ # Create a temporary directory for output
1838
+ temp_dir = tempfile.mkdtemp(prefix="repo_analysis_")
1839
+ output_file = os.path.join(temp_dir, "digest.json")
1840
+
1841
+ try:
1842
+ if has_gitingest_cli:
1843
+ # Use gitingest CLI tool to analyze the repository directly from URL
1844
+ print(f"🔎 Running GitIngest analysis on {repo_url}...")
1845
+
1846
+ # Based on the help output, the correct format is:
1847
+ # gitingest [OPTIONS] [SOURCE]
1848
+ # With options:
1849
+ # -o, --output TEXT Output file path
1850
+ # --format TEXT Output format (json)
1851
+
1852
+ # Run gitingest command with proper parameters
1853
+ gitingest_run_cmd = [
1854
+ gitingest_cmd_name,
1855
+ repo_url,
1856
+ "-o", output_file, # Use -o for output file
1857
+ ]
1858
+
1859
+ print(f"🔄 Executing: {' '.join(gitingest_run_cmd)}")
1860
+
1861
+ result = subprocess.run(gitingest_run_cmd, capture_output=True, text=True)
1862
+
1863
+ if result.returncode != 0:
1864
+ print(f"⚠️ GitIngest CLI failed with exit code {result.returncode}")
1865
+ print(f"⚠️ Error output: {result.stderr}")
1866
+ print("Falling back to basic analysis")
1867
+ gitingest_data = generate_basic_repo_analysis_from_url(repo_url)
1868
+ else:
1869
+ print(f"✅ GitIngest analysis completed successfully")
1870
+
1871
+ # Read the output file - note that the default format might not be JSON
1872
+ try:
1873
+ # First try to parse as JSON
1874
+ try:
1875
+ with open(output_file, 'r', encoding='utf-8') as f:
1876
+ content = f.read()
1877
+ try:
1878
+ gitingest_data = json.loads(content)
1879
+ print(f"✅ GitIngest data loaded as JSON from {output_file}")
1880
+ except json.JSONDecodeError:
1881
+ # If not JSON, convert the text output to a basic structure
1882
+ print(f"⚠️ GitIngest output is not in JSON format, converting text to structure")
1883
+ gitingest_data = {
1884
+ "system_info": {
1885
+ "detected_language": "Unknown",
1886
+ "detected_technologies": [],
1887
+ },
1888
+ "repository_analysis": {
1889
+ "summary": content[:5000], # First 5000 chars as summary
1890
+ "content_preview": content[:10000] # First 10000 chars as preview
1891
+ },
1892
+ "success": True
1893
+ }
1894
+ except FileNotFoundError:
1895
+ print(f"⚠️ Output file not found at {output_file}")
1896
+ gitingest_data = generate_basic_repo_analysis_from_url(repo_url)
1897
+ except Exception as e:
1898
+ print(f"⚠️ Error reading GitIngest output: {e}")
1899
+ gitingest_data = generate_basic_repo_analysis_from_url(repo_url)
1900
+ else:
1901
+ # Fall back to basic analysis if gitingest CLI is not available
1902
+ gitingest_data = generate_basic_repo_analysis_from_url(repo_url)
1903
+
1904
+ # Prepare the request payload with GitIngest data
1905
+ payload = {
1906
+ "repoUrl": repo_url,
1907
+ "gitingestData": gitingest_data,
1908
+ "userRequest": "Setup and run the repository"
1909
+ }
1910
+
1911
+ print(f"📤 API Request payload prepared (GitIngest data size: {len(json.dumps(gitingest_data))} bytes)")
1912
+
1913
+ # Make the API request
1914
+ print(f"🌐 Making POST request to: {api_url}")
1915
+ response = requests.post(api_url, json=payload, timeout=60)
1916
+
1917
+ print(f"📥 API Response status code: {response.status_code}")
1918
+
1919
+ if response.status_code == 200:
1920
+ try:
1921
+ data = response.json()
1922
+ print(f"📄 API Response data received")
1923
+
1924
+ # Extract setup commands from the response
1925
+ if "setupInstructions" in data and "commands" in data["setupInstructions"]:
1926
+ commands = data["setupInstructions"]["commands"]
1927
+ print(f"✅ Successfully fetched {len(commands)} setup commands from API")
1928
+
1929
+ # Print the commands for reference
1930
+ for i, cmd in enumerate(commands, 1):
1931
+ print(f" {i}. {cmd}")
1932
+
1933
+ return commands
1934
+ else:
1935
+ print("⚠️ API response did not contain setupInstructions.commands field")
1936
+ print("📋 Available fields in response:")
1937
+ for key in data.keys():
1938
+ print(f" - {key}")
1939
+ return []
1940
+ except json.JSONDecodeError as e:
1941
+ print(f"❌ Failed to parse API response as JSON: {e}")
1942
+ print(f"Raw response: {response.text[:500]}...")
1943
+ return []
1944
+ else:
1945
+ print(f"❌ API request failed with status code: {response.status_code}")
1946
+ print(f"Error response: {response.text[:500]}...")
1947
+ return []
1948
+ except requests.exceptions.ConnectionError:
1949
+ print(f"❌ Connection error: Could not connect to {api_url}")
1950
+ print("⚠️ Make sure the API server is running at localhost:3000")
1951
+ return []
1952
+ except Exception as e:
1953
+ print(f"❌ Error fetching setup commands from API: {e}")
1954
+ import traceback
1955
+ traceback.print_exc()
1956
+ return []
1957
+ finally:
1958
+ # Clean up the temporary directory
1959
+ print(f"🧹 Cleaning up temporary directory...")
1960
+ shutil.rmtree(temp_dir, ignore_errors=True)
1961
+
1962
+ def generate_basic_repo_analysis_from_url(repo_url):
1963
+ """Generate basic repository analysis data from a repository URL."""
1964
+ import tempfile
1965
+ import subprocess
1966
+ import os
1967
+ import shutil
1968
+
1969
+ # Create a temporary directory for cloning
1970
+ temp_dir = tempfile.mkdtemp(prefix="repo_basic_analysis_")
1971
+
1972
+ try:
1973
+ print(f"📥 Cloning repository to {temp_dir} for basic analysis...")
1974
+ clone_result = subprocess.run(
1975
+ ["git", "clone", "--depth", "1", repo_url, temp_dir],
1976
+ capture_output=True,
1977
+ text=True
1978
+ )
1979
+
1980
+ if clone_result.returncode != 0:
1981
+ print(f"❌ Failed to clone repository: {clone_result.stderr}")
1982
+ return {
1983
+ "system_info": {
1984
+ "platform": "linux",
1985
+ "python_version": "3.10",
1986
+ "detected_language": "Unknown",
1987
+ "detected_technologies": [],
1988
+ "file_count": 0,
1989
+ "repo_stars": 0,
1990
+ "repo_forks": 0,
1991
+ "primary_package_manager": "Unknown",
1992
+ "complexity_level": "low"
1993
+ },
1994
+ "repository_analysis": {
1995
+ "summary": f"Repository analysis for {repo_url}",
1996
+ "tree": "Failed to clone repository",
1997
+ "content_preview": "No content available"
1998
+ },
1999
+ "success": False
2000
+ }
2001
+
2002
+ print(f"✅ Repository cloned successfully for basic analysis")
2003
+
2004
+ # Use the existing generate_basic_repo_analysis function
2005
+ return generate_basic_repo_analysis(temp_dir)
2006
+ finally:
2007
+ # Clean up the temporary directory
2008
+ print(f"🧹 Cleaning up temporary directory for basic analysis...")
2009
+ shutil.rmtree(temp_dir, ignore_errors=True)
2010
+
2011
+ def generate_basic_repo_analysis(repo_dir):
2012
+ """Generate basic repository analysis when GitIngest is not available."""
2013
+ import os
2014
+ import subprocess
2015
+
2016
+ # Detect language and technologies based on file extensions
2017
+ file_extensions = {}
2018
+ file_count = 0
2019
+
2020
+ for root, _, files in os.walk(repo_dir):
2021
+ for file in files:
2022
+ file_count += 1
2023
+ ext = os.path.splitext(file)[1].lower()
2024
+ if ext:
2025
+ file_extensions[ext] = file_extensions.get(ext, 0) + 1
2026
+
2027
+ # Determine primary language
2028
+ language_map = {
2029
+ '.py': 'Python',
2030
+ '.js': 'JavaScript',
2031
+ '.ts': 'TypeScript',
2032
+ '.jsx': 'JavaScript',
2033
+ '.tsx': 'TypeScript',
2034
+ '.java': 'Java',
2035
+ '.cpp': 'C++',
2036
+ '.c': 'C',
2037
+ '.go': 'Go',
2038
+ '.rs': 'Rust',
2039
+ '.rb': 'Ruby',
2040
+ '.php': 'PHP',
2041
+ '.swift': 'Swift',
2042
+ '.kt': 'Kotlin',
2043
+ '.cs': 'C#'
2044
+ }
2045
+
2046
+ # Count files by language
2047
+ language_counts = {}
2048
+ for ext, count in file_extensions.items():
2049
+ if ext in language_map:
2050
+ lang = language_map[ext]
2051
+ language_counts[lang] = language_counts.get(lang, 0) + count
2052
+
2053
+ # Determine primary language
2054
+ primary_language = max(language_counts.items(), key=lambda x: x[1])[0] if language_counts else "Unknown"
2055
+
2056
+ # Detect package managers
2057
+ package_managers = []
2058
+ package_files = {
2059
+ 'requirements.txt': 'pip',
2060
+ 'setup.py': 'pip',
2061
+ 'pyproject.toml': 'pip',
2062
+ 'package.json': 'npm',
2063
+ 'yarn.lock': 'yarn',
2064
+ 'pnpm-lock.yaml': 'pnpm',
2065
+ 'Cargo.toml': 'cargo',
2066
+ 'go.mod': 'go',
2067
+ 'Gemfile': 'bundler',
2068
+ 'pom.xml': 'maven',
2069
+ 'build.gradle': 'gradle',
2070
+ 'composer.json': 'composer'
2071
+ }
2072
+
2073
+ for file, manager in package_files.items():
2074
+ if os.path.exists(os.path.join(repo_dir, file)):
2075
+ package_managers.append(manager)
2076
+
2077
+ primary_package_manager = package_managers[0] if package_managers else "Unknown"
2078
+
2079
+ # Get README content
2080
+ readme_content = ""
2081
+ for readme_name in ['README.md', 'README', 'README.txt', 'readme.md']:
2082
+ readme_path = os.path.join(repo_dir, readme_name)
2083
+ if os.path.exists(readme_path):
2084
+ with open(readme_path, 'r', encoding='utf-8', errors='ignore') as f:
2085
+ readme_content = f.read()
2086
+ break
2087
+
2088
+ # Try to get repository info
2089
+ repo_info = {}
2090
+ try:
2091
+ # Get remote origin URL
2092
+ cmd = ["git", "config", "--get", "remote.origin.url"]
2093
+ result = subprocess.run(cmd, cwd=repo_dir, capture_output=True, text=True)
2094
+ if result.returncode == 0:
2095
+ repo_info["url"] = result.stdout.strip()
2096
+
2097
+ # Get commit count as a proxy for activity
2098
+ cmd = ["git", "rev-list", "--count", "HEAD"]
2099
+ result = subprocess.run(cmd, cwd=repo_dir, capture_output=True, text=True)
2100
+ if result.returncode == 0:
2101
+ repo_info["commit_count"] = int(result.stdout.strip())
2102
+ except Exception:
2103
+ pass
2104
+
2105
+ # Build the analysis data
2106
+ return {
2107
+ "system_info": {
2108
+ "platform": "linux", # Assuming Linux for container environment
2109
+ "python_version": "3.10", # Common Python version
2110
+ "detected_language": primary_language,
2111
+ "detected_technologies": list(language_counts.keys()),
2112
+ "file_count": file_count,
2113
+ "repo_stars": repo_info.get("stars", 0),
2114
+ "repo_forks": repo_info.get("forks", 0),
2115
+ "primary_package_manager": primary_package_manager,
2116
+ "complexity_level": "medium" # Default assumption
2117
+ },
2118
+ "repository_analysis": {
2119
+ "summary": f"Repository analysis for {repo_dir}",
2120
+ "readme_content": readme_content[:5000] if readme_content else "No README found",
2121
+ "package_managers": package_managers,
2122
+ "file_extensions": list(file_extensions.keys())
2123
+ },
2124
+ "success": True
2125
+ }
2126
+
2127
+ def get_setup_commands_from_local_api(repo_url, gitingest_data):
2128
+ """Try to get setup commands from the local API."""
2129
+ api_url = "http://localhost:3000/api/analyze-with-gitingest"
2130
+
2131
+ # Prepare the request payload
2132
+ payload = {
2133
+ "repoUrl": repo_url,
2134
+ "gitingestData": gitingest_data,
2135
+ "userRequest": "Setup and run the repository"
2136
+ }
2137
+
2138
+ try:
2139
+ # Make the API request
2140
+ print(f"🌐 Making POST request to local API: {api_url}")
2141
+ response = requests.post(api_url, json=payload, timeout=60)
2142
+
2143
+ if response.status_code == 200:
2144
+ data = response.json()
2145
+ if "setupInstructions" in data and "commands" in data["setupInstructions"]:
2146
+ commands = data["setupInstructions"]["commands"]
2147
+ print(f"✅ Successfully fetched {len(commands)} setup commands from local API")
2148
+ for i, cmd in enumerate(commands, 1):
2149
+ print(f" {i}. {cmd}")
2150
+ return commands
2151
+ except Exception as e:
2152
+ print(f"❌ Error connecting to local API: {e}")
2153
+
2154
+ return None
2155
+
2156
+ if __name__ == "__main__":
2157
+ # Parse command line arguments when script is run directly
2158
+ import argparse
2159
+
2160
+ parser = argparse.ArgumentParser(description='Create a Modal sandbox with GPU')
2161
+ parser.add_argument('--gpu', type=str, default='A10G', help='GPU type (default: A10G)')
2162
+ parser.add_argument('--repo-url', type=str, help='Repository URL to clone')
2163
+ parser.add_argument('--repo-name', type=str, help='Repository name override')
2164
+ parser.add_argument('--setup-commands', type=str, nargs='+', help='Setup commands to run (deprecated)')
2165
+ parser.add_argument('--setup-commands-json', type=str, help='Setup commands as JSON array')
2166
+ parser.add_argument('--commands-file', type=str, help='Path to file containing setup commands (one per line)')
2167
+ parser.add_argument('--setup-script', type=str, help='Path to bash script containing setup commands')
2168
+ parser.add_argument('--working-dir', type=str, help='Working directory for the setup script')
2169
+ parser.add_argument('--volume-name', type=str, help='Name of the Modal volume for persistent storage')
2170
+ parser.add_argument('--use-api', action='store_true', help='Fetch setup commands from API')
2171
+
2172
+ args = parser.parse_args()
2173
+
2174
+ # Get setup commands from file if specified
2175
+ setup_commands = args.setup_commands or []
2176
+
2177
+ # If --use-api flag is set and repo_url is provided, fetch setup commands from API
2178
+ if args.use_api and args.repo_url:
2179
+ print("🔄 Using API to fetch setup commands")
2180
+ api_commands = fetch_setup_commands_from_api(args.repo_url)
2181
+ if api_commands:
2182
+ setup_commands = api_commands
2183
+ print(f"📋 Using {len(setup_commands)} commands from API")
2184
+ else:
2185
+ print("⚠️ Failed to get commands from API, no fallback commands will be used")
2186
+ # Do not fall back to basic setup commands
2187
+ setup_commands = []
2188
+
2189
+ # Parse setup commands from JSON if provided
2190
+ if args.setup_commands_json:
2191
+ try:
2192
+ json_commands = json.loads(args.setup_commands_json)
2193
+ if isinstance(json_commands, list):
2194
+ setup_commands = json_commands
2195
+ print(f"📋 Parsed {len(setup_commands)} commands from JSON:")
2196
+ for i, cmd in enumerate(setup_commands, 1):
2197
+ print(f" {i}. {cmd}")
2198
+ else:
2199
+ print(f"⚠️ Invalid JSON format for setup commands: not a list")
2200
+ except json.JSONDecodeError as e:
2201
+ print(f"⚠️ Error parsing JSON setup commands: {e}")
2202
+ print(f"Received JSON string: {args.setup_commands_json}")
2203
+
2204
+ # Print received setup commands for debugging
2205
+ if setup_commands:
2206
+ print(f"📋 Using {len(setup_commands)} setup commands:")
2207
+ for i, cmd in enumerate(setup_commands, 1):
2208
+ print(f" {i}. {cmd}")
2209
+
2210
+ if args.commands_file and os.path.exists(args.commands_file):
2211
+ try:
2212
+ with open(args.commands_file, 'r') as f:
2213
+ # Check if the file contains JSON or line-by-line commands
2214
+ content = f.read().strip()
2215
+
2216
+ if content.startswith('[') and content.endswith(']'):
2217
+ # JSON format
2218
+ try:
2219
+ json_commands = json.loads(content)
2220
+ if isinstance(json_commands, list):
2221
+ setup_commands.extend(json_commands)
2222
+ print(f"📋 Loaded {len(json_commands)} commands from JSON file {args.commands_file}")
2223
+ else:
2224
+ print(f"⚠️ Invalid JSON format in commands file: not a list")
2225
+ except json.JSONDecodeError as json_err:
2226
+ print(f"⚠️ Error parsing JSON commands file: {json_err}")
2227
+ # Fall back to line-by-line parsing
2228
+ file_commands = [line.strip() for line in content.split('\n') if line.strip()]
2229
+ setup_commands.extend(file_commands)
2230
+ print(f"📋 Loaded {len(file_commands)} commands from file (line-by-line fallback)")
2231
+ else:
2232
+ # Line-by-line format
2233
+ file_commands = [line.strip() for line in content.split('\n') if line.strip()]
2234
+ setup_commands.extend(file_commands)
2235
+ print(f"📋 Loaded {len(file_commands)} commands from file (line-by-line format)")
2236
+
2237
+ except Exception as e:
2238
+ print(f"⚠️ Error reading commands file: {e}")
2239
+
2240
+ # Execute setup script if provided
2241
+ if args.setup_script:
2242
+ print(f"📜 Setup script path: {args.setup_script}")
2243
+
2244
+ # Verify script exists
2245
+ if os.path.exists(args.setup_script):
2246
+ print(f"✅ Script exists at: {args.setup_script}")
2247
+
2248
+ # Check if script is executable
2249
+ if not os.access(args.setup_script, os.X_OK):
2250
+ print(f"⚠️ Script is not executable, setting permissions...")
2251
+ try:
2252
+ os.chmod(args.setup_script, 0o755)
2253
+ print(f"✅ Set executable permissions on script")
2254
+ except Exception as e:
2255
+ print(f"❌ Failed to set permissions: {e}")
2256
+
2257
+ working_dir = args.working_dir or os.getcwd()
2258
+ print(f"📂 Using working directory: {working_dir}")
2259
+
2260
+ # Execute the script directly instead of through sandbox
2261
+ try:
2262
+ print(f"🔄 Executing script directly: bash {args.setup_script} {working_dir}")
2263
+ result = subprocess.run(['bash', args.setup_script, working_dir],
2264
+ capture_output=True, text=True)
2265
+
2266
+ print(f"📋 Script output:")
2267
+ print(result.stdout)
2268
+
2269
+ if result.returncode != 0:
2270
+ print(f"❌ Script execution failed with error code {result.returncode}")
2271
+ print(f"Error output: {result.stderr}")
2272
+ else:
2273
+ print(f"✅ Script executed successfully")
2274
+
2275
+ # Skip the regular setup commands since we executed the script directly
2276
+ setup_commands = []
2277
+ except Exception as e:
2278
+ print(f"❌ Failed to execute script: {e}")
2279
+ # Fall back to running the script through sandbox
2280
+ setup_commands = [f"bash {args.setup_script} {working_dir}"]
2281
+ print("🔄 Falling back to running script through sandbox")
2282
+ else:
2283
+ print(f"❌ Script not found at: {args.setup_script}")
2284
+ # Try to find the script in common locations
2285
+ possible_paths = [
2286
+ os.path.join(os.path.expanduser('~'), os.path.basename(args.setup_script)),
2287
+ os.path.join('/tmp', os.path.basename(args.setup_script)),
2288
+ os.path.join('/var/tmp', os.path.basename(args.setup_script))
2289
+ ]
2290
+
2291
+ found = False
2292
+ for test_path in possible_paths:
2293
+ if os.path.exists(test_path):
2294
+ print(f"🔍 Found script at alternative location: {test_path}")
2295
+ setup_commands = [f"bash {test_path} {args.working_dir or os.getcwd()}"]
2296
+ found = True
2297
+ break
2298
+
2299
+ if not found:
2300
+ print("❌ Could not find script in any location")
2301
+ setup_commands = []
2302
+
2303
+ try:
2304
+ result = create_modal_sandbox(
2305
+ args.gpu,
2306
+ args.repo_url,
2307
+ args.repo_name,
2308
+ setup_commands,
2309
+ getattr(args, 'volume_name', None)
2310
+ )
2311
+
2312
+ print("\n⏳ Keeping the sandbox alive. Press Ctrl+C to exit (sandbox will continue running)...")
2313
+ while True:
2314
+ time.sleep(90)
2315
+ print(".", end="", flush=True)
2316
+ except KeyboardInterrupt:
2317
+ print("\n👋 Script exited. The sandbox will continue running.")
2318
+ if 'result' in locals() and result:
2319
+ container_id = None
2320
+ # Try to get container ID from the result dictionary
2321
+ if isinstance(result, dict):
2322
+ # The container ID might be stored in execution_history or elsewhere
2323
+ # Let's try to find it in the current_dir which might contain it
2324
+ current_dir = result.get('current_dir', '')
2325
+ if 'container_id' in result:
2326
+ container_id = result['container_id']
2327
+ elif hasattr(result, 'container_id'):
2328
+ container_id = result.container_id
2329
+
2330
+ # If we still don't have the container ID, try to read it from the file
2331
+ if not container_id:
2332
+ try:
2333
+ with open(os.path.expanduser("~/.modal_last_container_id"), "r") as f:
2334
+ container_id = f.read().strip()
2335
+ print(f"📋 Retrieved container ID from file: {container_id}")
2336
+ except Exception as e:
2337
+ print(f"⚠️ Could not read container ID from file: {e}")
2338
+
2339
+ if container_id:
2340
+ print(f"🚀 Starting shell in container: {container_id}")
2341
+ try:
2342
+ # Execute the modal shell command directly
2343
+ shell_cmd = f"modal shell {container_id}"
2344
+ print(f"🔄 Executing: {shell_cmd}")
2345
+ subprocess.run(shell_cmd, shell=True)
2346
+ except Exception as e:
2347
+ print(f"❌ Error starting shell: {e}")
2348
+ print(f"📝 You can manually connect using:")
2349
+ print(f" modal shell {container_id}")
2350
+ else:
2351
+ print("⚠️ Could not determine container ID")
2352
+ print("📝 You can manually connect using:")
2353
+ print(" modal container list")
2354
+ print(" modal shell <CONTAINER_ID>")