deepagents 0.3.8__py3-none-any.whl → 0.3.9__py3-none-any.whl

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.
@@ -46,12 +46,32 @@ for m in matches:
46
46
  print(json.dumps(result))
47
47
  " 2>/dev/null"""
48
48
 
49
+ # Use heredoc to pass content via stdin to avoid ARG_MAX limits on large files.
50
+ # ARG_MAX limits the total size of command-line arguments.
51
+ # Previously, base64-encoded content was interpolated directly into the command
52
+ # string, which would fail for files larger than ~100KB after base64 expansion.
53
+ # Heredocs bypass this by passing data through stdin rather than as arguments.
54
+ # Stdin format: first line is base64-encoded file path, second line is base64-encoded content.
49
55
  _WRITE_COMMAND_TEMPLATE = """python3 -c "
50
56
  import os
51
57
  import sys
52
58
  import base64
59
+ import json
53
60
 
54
- file_path = '{file_path}'
61
+ # Read JSON payload from stdin containing file_path and content (both base64-encoded)
62
+ payload_b64 = sys.stdin.read().strip()
63
+ if not payload_b64:
64
+ print('Error: No payload received for write operation', file=sys.stderr)
65
+ sys.exit(1)
66
+
67
+ try:
68
+ payload = base64.b64decode(payload_b64).decode('utf-8')
69
+ data = json.loads(payload)
70
+ file_path = data['path']
71
+ content = base64.b64decode(data['content']).decode('utf-8')
72
+ except Exception as e:
73
+ print(f'Error: Failed to decode write payload: {e}', file=sys.stderr)
74
+ sys.exit(1)
55
75
 
56
76
  # Check if file already exists (atomic with write)
57
77
  if os.path.exists(file_path):
@@ -62,24 +82,46 @@ if os.path.exists(file_path):
62
82
  parent_dir = os.path.dirname(file_path) or '.'
63
83
  os.makedirs(parent_dir, exist_ok=True)
64
84
 
65
- # Decode and write content
66
- content = base64.b64decode('{content_b64}').decode('utf-8')
67
85
  with open(file_path, 'w') as f:
68
86
  f.write(content)
69
- " 2>&1"""
70
-
87
+ " <<'__DEEPAGENTS_EOF__'
88
+ {payload_b64}
89
+ __DEEPAGENTS_EOF__"""
90
+
91
+ # Use heredoc to pass edit parameters via stdin to avoid ARG_MAX limits.
92
+ # Stdin format: base64-encoded JSON with {"path": str, "old": str, "new": str}.
93
+ # JSON bundles all parameters; base64 ensures safe transport of arbitrary content
94
+ # (special chars, newlines, etc.) through the heredoc without escaping issues.
71
95
  _EDIT_COMMAND_TEMPLATE = """python3 -c "
72
96
  import sys
73
97
  import base64
98
+ import json
99
+ import os
100
+
101
+ # Read and decode JSON payload from stdin
102
+ payload_b64 = sys.stdin.read().strip()
103
+ if not payload_b64:
104
+ print('Error: No payload received for edit operation', file=sys.stderr)
105
+ sys.exit(4)
106
+
107
+ try:
108
+ payload = base64.b64decode(payload_b64).decode('utf-8')
109
+ data = json.loads(payload)
110
+ file_path = data['path']
111
+ old = data['old']
112
+ new = data['new']
113
+ except Exception as e:
114
+ print(f'Error: Failed to decode edit payload: {e}', file=sys.stderr)
115
+ sys.exit(4)
116
+
117
+ # Check if file exists
118
+ if not os.path.isfile(file_path):
119
+ sys.exit(3) # File not found
74
120
 
75
121
  # Read file content
76
- with open('{file_path}', 'r') as f:
122
+ with open(file_path, 'r') as f:
77
123
  text = f.read()
78
124
 
79
- # Decode base64-encoded strings
80
- old = base64.b64decode('{old_b64}').decode('utf-8')
81
- new = base64.b64decode('{new_b64}').decode('utf-8')
82
-
83
125
  # Count occurrences
84
126
  count = text.count(old)
85
127
 
@@ -96,11 +138,13 @@ else:
96
138
  result = text.replace(old, new, 1)
97
139
 
98
140
  # Write back to file
99
- with open('{file_path}', 'w') as f:
141
+ with open(file_path, 'w') as f:
100
142
  f.write(result)
101
143
 
102
144
  print(count)
103
- " 2>&1"""
145
+ " <<'__DEEPAGENTS_EOF__'
146
+ {payload_b64}
147
+ __DEEPAGENTS_EOF__"""
104
148
 
105
149
  _READ_COMMAND_TEMPLATE = """python3 -c "
106
150
  import os
@@ -221,11 +265,14 @@ except PermissionError:
221
265
  content: str,
222
266
  ) -> WriteResult:
223
267
  """Create a new file. Returns WriteResult; error populated on failure."""
224
- # Encode content as base64 to avoid any escaping issues
268
+ # Create JSON payload with file path and base64-encoded content
269
+ # This avoids shell injection via file_path and ARG_MAX limits on content
225
270
  content_b64 = base64.b64encode(content.encode("utf-8")).decode("ascii")
271
+ payload = json.dumps({"path": file_path, "content": content_b64})
272
+ payload_b64 = base64.b64encode(payload.encode("utf-8")).decode("ascii")
226
273
 
227
274
  # Single atomic check + write command
228
- cmd = _WRITE_COMMAND_TEMPLATE.format(file_path=file_path, content_b64=content_b64)
275
+ cmd = _WRITE_COMMAND_TEMPLATE.format(payload_b64=payload_b64)
229
276
  result = self.execute(cmd)
230
277
 
231
278
  # Check for errors (exit code or error message in output)
@@ -244,23 +291,29 @@ except PermissionError:
244
291
  replace_all: bool = False,
245
292
  ) -> EditResult:
246
293
  """Edit a file by replacing string occurrences. Returns EditResult."""
247
- # Encode strings as base64 to avoid any escaping issues
248
- old_b64 = base64.b64encode(old_string.encode("utf-8")).decode("ascii")
249
- new_b64 = base64.b64encode(new_string.encode("utf-8")).decode("ascii")
294
+ # Create JSON payload with file path, old string, and new string
295
+ # This avoids shell injection via file_path and ARG_MAX limits on strings
296
+ payload = json.dumps({"path": file_path, "old": old_string, "new": new_string})
297
+ payload_b64 = base64.b64encode(payload.encode("utf-8")).decode("ascii")
250
298
 
251
299
  # Use template for string replacement
252
- cmd = _EDIT_COMMAND_TEMPLATE.format(file_path=file_path, old_b64=old_b64, new_b64=new_b64, replace_all=replace_all)
300
+ cmd = _EDIT_COMMAND_TEMPLATE.format(payload_b64=payload_b64, replace_all=replace_all)
253
301
  result = self.execute(cmd)
254
302
 
255
303
  exit_code = result.exit_code
256
304
  output = result.output.strip()
257
305
 
258
- if exit_code == 1:
259
- return EditResult(error=f"Error: String not found in file: '{old_string}'")
260
- if exit_code == 2:
261
- return EditResult(error=f"Error: String '{old_string}' appears multiple times. Use replace_all=True to replace all occurrences.")
306
+ # Map exit codes to error messages
307
+ error_messages = {
308
+ 1: f"Error: String not found in file: '{old_string}'",
309
+ 2: f"Error: String '{old_string}' appears multiple times. Use replace_all=True to replace all occurrences.",
310
+ 3: f"Error: File '{file_path}' not found",
311
+ 4: f"Error: Failed to decode edit payload: {output}",
312
+ }
313
+ if exit_code in error_messages:
314
+ return EditResult(error=error_messages[exit_code])
262
315
  if exit_code != 0:
263
- return EditResult(error=f"Error: File '{file_path}' not found")
316
+ return EditResult(error=f"Error editing file (exit code {exit_code}): {output or 'Unknown error'}")
264
317
 
265
318
  count = int(output)
266
319
  # External storage - no files_update needed