Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: add additional e2b debug logging (#4772)
cthomas00292363c496
diff --git a/letta/otel/tracing.py b/letta/otel/tracing.pyindex db22d0135..a351962cf 100644--- a/letta/otel/tracing.py+++ b/letta/otel/tracing.py@@ -1,6 +1,8 @@+import asyncioimport inspectimport reimport time+import tracebackfrom functools import wrapsfrom typing import Any, Dict, List, Optional@@ -236,9 +238,31 @@ def trace_method(func):with tracer.start_as_current_span(_get_span_name(func, args)) as span:_add_parameters_to_span(span, func, args, kwargs)- result = await func(*args, **kwargs)- span.set_status(Status(StatusCode.OK))- return result+ try:+ result = await func(*args, **kwargs)+ span.set_status(Status(StatusCode.OK))+ return result+ except asyncio.CancelledError as e:+ # Get current task info+ current_task = asyncio.current_task()+ task_name = current_task.get_name() if current_task else "unknown"++ # Log detailed information+ logger.error(f"Task {task_name} cancelled in {func.__module__}.{func.__name__}")++ # Add to span+ span.set_status(Status(StatusCode.ERROR))+ span.record_exception(+ e,+ attributes={+ "exception.type": "asyncio.CancelledError",+ "task.name": task_name,+ "function.name": func.__name__,+ "function.module": func.__module__,+ "cancellation.timestamp": time.time_ns(),+ },+ )+ raise@wraps(func)def sync_wrapper(*args, **kwargs):diff --git a/letta/services/tool_sandbox/base.py b/letta/services/tool_sandbox/base.pyindex e504cdfee..abb1a2a11 100644--- a/letta/services/tool_sandbox/base.py+++ b/letta/services/tool_sandbox/base.py@@ -5,6 +5,7 @@ from abc import ABC, abstractmethodfrom typing import Any, Dict, Optionalfrom letta.functions.helpers import generate_model_from_args_json_schema+from letta.otel.tracing import trace_methodfrom letta.schemas.agent import AgentStatefrom letta.schemas.sandbox_config import SandboxConfigfrom letta.schemas.tool import Tool@@ -75,6 +76,7 @@ class AsyncToolSandboxBase(ABC):"""raise NotImplementedError+ @trace_methodasync def generate_execution_script(self, agent_state: Optional[AgentState], wrap_print_with_markers: bool = False) -> str:"""Generate code to run inside of execution sandbox. Serialize the agent state and arguments, call the tool,diff --git a/letta/services/tool_sandbox/e2b_sandbox.py b/letta/services/tool_sandbox/e2b_sandbox.pyindex d345ead9e..c8774de65 100644--- a/letta/services/tool_sandbox/e2b_sandbox.py+++ b/letta/services/tool_sandbox/e2b_sandbox.py@@ -1,3 +1,4 @@+import asynciofrom typing import TYPE_CHECKING, Any, Dict, Optionalfrom e2b.sandbox.commands.command_handle import CommandExitException@@ -65,14 +66,24 @@ class AsyncToolSandboxE2B(AsyncToolSandboxBase):code = await self.generate_execution_script(agent_state=agent_state)try:+ logger.info(f"E2B execution started for ID {e2b_sandbox.sandbox_id}: {self.tool_name}")log_event("e2b_execution_started",{"tool": self.tool_name, "sandbox_id": e2b_sandbox.sandbox_id, "code": code, "env_vars": envs},)- execution = await e2b_sandbox.run_code(code, envs=envs)+ try:+ execution = await e2b_sandbox.run_code(code, envs=envs)+ except asyncio.CancelledError:+ logger.info(f"E2B execution cancelled for ID {e2b_sandbox.sandbox_id}: {self.tool_name}")+ log_event(+ "e2b_execution_cancelled",+ {"tool": self.tool_name, "sandbox_id": e2b_sandbox.sandbox_id},+ )+ raise Exception("Execution cancelled. Transient failure, please retry.")if execution.results:func_return, agent_state = parse_stdout_best_effort(execution.results[0].text)+ logger.info(f"E2B execution succeeded for ID {e2b_sandbox.sandbox_id}: {self.tool_name}")log_event("e2b_execution_succeeded",{@@ -90,6 +101,7 @@ class AsyncToolSandboxE2B(AsyncToolSandboxBase):function_name=self.tool_name, exception_name=execution.error.name, exception_message=execution.error.value)execution.logs.stderr.append(execution.error.traceback)+ logger.info(f"E2B execution failed for ID {e2b_sandbox.sandbox_id}: {self.tool_name}")log_event("e2b_execution_failed",{@@ -101,6 +113,7 @@ class AsyncToolSandboxE2B(AsyncToolSandboxBase):},)else:+ logger.info(f"E2B execution empty for ID {e2b_sandbox.sandbox_id}: {self.tool_name}")log_event("e2b_execution_empty",{@@ -120,6 +133,7 @@ class AsyncToolSandboxE2B(AsyncToolSandboxBase):sandbox_config_fingerprint=sbx_config.fingerprint(),)finally:+ logger.info(f"E2B sandbox {e2b_sandbox.sandbox_id} killed")await e2b_sandbox.kill()@staticmethod