-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathworkspace_manager.py
More file actions
299 lines (231 loc) · 8.53 KB
/
workspace_manager.py
File metadata and controls
299 lines (231 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python3
"""
workspace_manager.py - File operations and bash execution for NeoFish agent.
Provides safe file operations within a workspace directory with:
- Path safety (strict mode for sandboxing, non-strict for flexibility)
- Dangerous command interception
- Async bash execution
"""
import os
import asyncio
import subprocess
from pathlib import Path
from typing import Optional
# Default workspace directory
WORKDIR = Path(os.getenv("WORKDIR", "./workspace")).resolve()
class WorkspaceManager:
"""Manages file operations and bash execution within a workspace."""
def __init__(self, workdir: Path = None, strict: bool = False):
"""
Initialize workspace manager.
Args:
workdir: Base directory for file operations. Defaults to WORKDIR env or ./workspace
strict: If True, block operations outside workdir. If False, allow with warning.
"""
self.workdir = (workdir or WORKDIR).resolve()
self.strict = strict
self.workdir.mkdir(parents=True, exist_ok=True)
def safe_path(self, p: str, strict: bool = None) -> Path:
"""
Resolve and validate a path.
Args:
p: Path string (relative or absolute)
strict: Override default strict mode for this call
Returns:
Resolved Path object
Raises:
ValueError: If path escapes workspace in strict mode
"""
use_strict = strict if strict is not None else self.strict
path = Path(p)
# If relative, resolve against workdir
if not path.is_absolute():
path = (self.workdir / path).resolve()
else:
path = path.resolve()
# Check if path is within workspace
if use_strict and not path.is_relative_to(self.workdir):
raise ValueError(f"Path escapes workspace: {p}")
return path
async def run_bash(self, command: str, timeout: int = 120) -> str:
"""
Execute a shell command with safety checks.
Args:
command: Shell command to execute
timeout: Timeout in seconds (default 120)
Returns:
Command output or error message
"""
# Dangerous command patterns to block
dangerous_patterns = [
"rm -rf /",
"rm -rf /*",
"sudo ",
"shutdown",
"reboot",
"init 0",
"mkfs",
"dd if=",
"> /dev/sd",
":(){ :|:& };:", # Fork bomb
]
for pattern in dangerous_patterns:
if pattern in command:
return f"Error: Dangerous command blocked (matched: {pattern.strip()})"
try:
process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=str(self.workdir)
)
try:
stdout, stderr = await asyncio.wait_for(
process.communicate(),
timeout=timeout
)
except asyncio.TimeoutError:
process.kill()
await process.wait()
return f"Error: Timeout ({timeout}s)"
output = (stdout.decode() + stderr.decode()).strip()
# Truncate large outputs
return output[:50000] if output else "(no output)"
except Exception as e:
return f"Error: {str(e)}"
async def read_file(self, path: str, limit: Optional[int] = None) -> str:
"""
Read file contents.
Args:
path: File path (relative to workdir or absolute)
limit: Maximum number of lines to read
Returns:
File contents or error message
"""
try:
fp = self.safe_path(path)
if not fp.exists():
return f"Error: File not found: {path}"
if not fp.is_file():
return f"Error: Not a file: {path}"
lines = fp.read_text(encoding='utf-8', errors='replace').splitlines()
if limit and limit < len(lines):
lines = lines[:limit] + [f"... ({len(lines) - limit} more lines)"]
content = "\n".join(lines)
return content[:50000] if content else "(empty file)"
except ValueError as e:
return f"Error: {str(e)}"
except Exception as e:
return f"Error reading file: {str(e)}"
async def write_file(self, path: str, content: str) -> str:
"""
Write content to a file.
Args:
path: File path (relative to workdir or absolute)
content: Content to write
Returns:
Success message or error
"""
try:
fp = self.safe_path(path)
# Create parent directories if needed
fp.parent.mkdir(parents=True, exist_ok=True)
fp.write_text(content, encoding='utf-8')
return f"Wrote {len(content)} characters to {path}"
except ValueError as e:
return f"Error: {str(e)}"
except Exception as e:
return f"Error writing file: {str(e)}"
async def edit_file(self, path: str, old_text: str, new_text: str) -> str:
"""
Replace exact text in a file.
Args:
path: File path
old_text: Text to find and replace
new_text: Replacement text
Returns:
Success message or error
"""
try:
fp = self.safe_path(path)
if not fp.exists():
return f"Error: File not found: {path}"
content = fp.read_text(encoding='utf-8')
if old_text not in content:
return f"Error: Text not found in {path}"
# Replace only the first occurrence
new_content = content.replace(old_text, new_text, 1)
fp.write_text(new_content, encoding='utf-8')
return f"Edited {path}: replaced {len(old_text)} chars with {len(new_text)} chars"
except ValueError as e:
return f"Error: {str(e)}"
except Exception as e:
return f"Error editing file: {str(e)}"
async def list_dir(self, path: str = ".") -> str:
"""
List directory contents.
Args:
path: Directory path
Returns:
Directory listing or error
"""
try:
fp = self.safe_path(path)
if not fp.exists():
return f"Error: Directory not found: {path}"
if not fp.is_dir():
return f"Error: Not a directory: {path}"
items = list(fp.iterdir())
lines = []
for item in sorted(items, key=lambda x: (not x.is_dir(), x.name.lower())):
prefix = "📁 " if item.is_dir() else "📄 "
size = ""
if item.is_file():
try:
size = f" ({item.stat().st_size} bytes)"
except:
pass
lines.append(f"{prefix}{item.name}{size}")
return "\n".join(lines) if lines else "(empty directory)"
except ValueError as e:
return f"Error: {str(e)}"
except Exception as e:
return f"Error listing directory: {str(e)}"
async def delete_file(self, path: str) -> str:
"""
Delete a file.
Args:
path: File path to delete
Returns:
Success message or error
"""
try:
fp = self.safe_path(path)
if not fp.exists():
return f"Error: File not found: {path}"
if fp.is_dir():
return f"Error: Is a directory, not a file: {path}"
fp.unlink()
return f"Deleted: {path}"
except ValueError as e:
return f"Error: {str(e)}"
except Exception as e:
return f"Error deleting file: {str(e)}"
async def create_dir(self, path: str) -> str:
"""
Create a directory.
Args:
path: Directory path to create
Returns:
Success message or error
"""
try:
fp = self.safe_path(path)
fp.mkdir(parents=True, exist_ok=True)
return f"Created directory: {path}"
except ValueError as e:
return f"Error: {str(e)}"
except Exception as e:
return f"Error creating directory: {str(e)}"
# Default instance
workspace = WorkspaceManager()