Spaces:
Running
Running
Sync Bash output handling
Browse filesFiles changed (3)
11v0.0.47v0.0.481323 unmodified lines1324132513261327132813271328132913301331133213331329133013311332133313341335133613371338133913401323 unmodified lines )
def execute_tool_item(item: dict[str, Any]) -> tuple[dict[str, Any], Any]: tool_name = str(item["tool_name"]) try: result = self.custom_call_tool( str(item["tool_name"]), item["tool_arguments"], workspace_root=resolved_workspace_root, runtime_deadline=runtime_deadline, model_name=self.model, ) result = self.custom_call_tool( tool_name, item["tool_arguments"], workspace_root=resolved_workspace_root, runtime_deadline=runtime_deadline, model_name=self.model, ) except Exception as exc: result = f"[{tool_name}] Tool execution error: {type(exc).__name__}: {exc}" return item, result
for batch_indexes in tool_execution_batches([str(item["tool_name"]) for item in tool_call_items]):30 unmodified lines3132333435363763 unmodified lines10110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315472 unmodified lines2272282291812302312322335 unmodified lines23924024119319424224324424524630 unmodified linesDEFAULT_YIELD_MS = 200REPEAT_COLLAPSE_THRESHOLD = 3
def _default_shell() -> str: return shutil.which("bash") or "/bin/bash"
63 unmodified lines return compressed[:keep] + suffix
def _looks_binary_output(data: bytes) -> bool: if not data: return False sample = data[:4096] if b"\x00" in sample: return True allowed_controls = {9, 10, 12, 13} control_count = sum(1 for byte in sample if byte < 32 and byte not in allowed_controls) return control_count / max(len(sample), 1) > 0.05
def _truncate_output_bytes(data: bytes, *, max_output_chars: int) -> tuple[bytes, bool, int]: max_output_bytes = max(1, int(max_output_chars)) if len(data) <= max_output_bytes: return data, False, 0 omitted = len(data) - max_output_bytes suffix = f"\n[output truncated before decoding: omitted {omitted} bytes]\n".encode("utf-8") keep = max(0, max_output_bytes - len(suffix)) return data[:keep] + suffix, True, omitted
def _safe_process_output(data: Union[str, bytes, None], *, max_output_chars: int = DEFAULT_OUTPUT_CHARS) -> str: if data is None or data == "": return "" if isinstance(data, str): return _bounded_output(data, max_output_chars=max_output_chars)
if not data: return "" if _looks_binary_output(data): note = ( f"[binary output omitted: {len(data)} bytes. " "Use file or a format-aware reader such as openpyxl/scipy.io.loadmat for binary files.]" ) if len(data) > max_output_chars: note += f"\n[output truncated before decoding: omitted {len(data) - max_output_chars} bytes]" return note
bounded, _, _ = _truncate_output_bytes(data, max_output_chars=max_output_chars) try: decoded = bounded.decode("utf-8") prefix = "" except UnicodeDecodeError: decoded = bounded.decode("utf-8", errors="replace") prefix = "[non-UTF-8 bytes decoded with replacement characters]\n" return _bounded_output(prefix + decoded, max_output_chars=max_output_chars)
class Bash(ToolBase): name = "Bash" description = (72 unmodified lines command, shell=True, capture_output=True, text=True, text=False, timeout=effective_timeout, cwd=str(cwd), env=sanitized_subprocess_env(base_root=base_root),5 unmodified lines return f"[Bash] Error executing command: {exc}"
parts = [f"exit_code: {proc.returncode}"] stdout = _bounded_output(proc.stdout, max_output_chars=max_output_chars) stderr = _bounded_output(proc.stderr, max_output_chars=max_output_chars) stdout = _safe_process_output(proc.stdout, max_output_chars=max_output_chars) stderr = _safe_process_output(proc.stderr, max_output_chars=max_output_chars) if stdout: parts.append(f"stdout:\n{stdout}") if stderr:VERSION+1-1
agent_base/react_agent.py+11-7
agent_base/tools/tool_runtime.py+52-3