Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -57,10 +57,10 @@ class MemoryMiddleware(AgentMiddleware[MemoryMiddlewareState]):
|
||||
if not config.enabled:
|
||||
return None
|
||||
|
||||
# Get thread ID from runtime context
|
||||
# Resolve thread ID from the runtime or configured fallback sources
|
||||
thread_id = get_thread_id(runtime)
|
||||
if not thread_id:
|
||||
logger.debug("No thread_id in context, skipping memory update")
|
||||
logger.debug("No thread_id could be resolved from runtime/config, skipping memory update")
|
||||
return None
|
||||
|
||||
# Get messages from state
|
||||
|
||||
@@ -854,7 +854,9 @@ def ensure_sandbox_initialized(runtime: ToolRuntime[ContextT, ThreadState] | Non
|
||||
# Lazy acquisition: get thread_id and acquire sandbox
|
||||
thread_id = get_thread_id(runtime)
|
||||
if thread_id is None:
|
||||
raise SandboxRuntimeError("Thread ID not available in runtime context")
|
||||
raise SandboxRuntimeError(
|
||||
"Thread ID not available in runtime context, runtime config, or LangGraph config"
|
||||
)
|
||||
|
||||
provider = get_sandbox_provider()
|
||||
sandbox_id = provider.acquire(thread_id)
|
||||
|
||||
@@ -36,7 +36,9 @@ def _normalize_presented_filepath(
|
||||
|
||||
thread_id = get_thread_id(runtime)
|
||||
if not thread_id:
|
||||
raise ValueError("Thread ID is not available in runtime context or runtime config")
|
||||
raise ValueError(
|
||||
"Thread ID is not available in runtime context, runtime config, or LangGraph thread-local config"
|
||||
)
|
||||
|
||||
thread_data = runtime.state.get("thread_data") or {}
|
||||
outputs_path = thread_data.get("outputs_path")
|
||||
|
||||
@@ -83,6 +83,8 @@ def get_thread_id(runtime: Any | None) -> str | None:
|
||||
if thread_id:
|
||||
return thread_id
|
||||
except RuntimeError:
|
||||
# Expected when not running inside a LangGraph runnable context (e.g., unit tests).
|
||||
# In that case, thread_id cannot be resolved from thread-local config, so fall through.
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
@@ -45,7 +45,8 @@ class TestGetThreadId:
|
||||
|
||||
def test_returns_none_when_get_config_raises_runtime_error(self):
|
||||
runtime = SimpleNamespace(context=None, config={})
|
||||
assert get_thread_id(runtime) is None
|
||||
with patch("langgraph.config.get_config", side_effect=RuntimeError):
|
||||
assert get_thread_id(runtime) is None
|
||||
|
||||
def test_handles_object_without_context_or_config(self):
|
||||
runtime = SimpleNamespace()
|
||||
|
||||
Reference in New Issue
Block a user