Skip to content

Commit 2b5068f

Browse files
committed
fix(agents): ShellToolMiddleware session lost on HIL resume
1 parent 5917732 commit 2b5068f

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

libs/langchain_v1/langchain/agents/middleware/shell_tool.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,13 @@ async def aafter_agent(self, state: ShellToolState, runtime: Runtime) -> None:
521521
"""Async counterpart to `after_agent`."""
522522
return self.after_agent(state, runtime)
523523

524-
def _ensure_resources(self, state: ShellToolState) -> _SessionResources:
525-
resources = state.get("shell_session_resources")
526-
if resources is not None and not isinstance(resources, _SessionResources):
527-
resources = None
528-
if resources is None:
529-
return self._get_or_create_resources()
530-
return resources
524+
def _ensure_resources(self, state : ShellToolState) -> _SessionResources: # noqa: ARG002
525+
"""Always return live resources from middleware cache.
526+
527+
State is ignored — session is managed internally to support restart and HIL resume.
528+
"""
529+
return self._get_or_create_resources()
530+
531531
def _get_or_create_resources(self) -> _SessionResources:
532532
if self._session is None:
533533
self._ensure_session()
@@ -677,8 +677,10 @@ def _run_shell_tool(
677677
if payload.get("restart"):
678678
LOGGER.info("Restarting shell session on request.")
679679
try:
680-
session.restart()
680+
session.stop(self._execution_policy.termination_timeout)
681+
session.start()
681682
self._run_startup_commands(session)
683+
self._session = session
682684
except BaseException as err:
683685
LOGGER.exception("Restarting shell session failed; session remains unavailable.")
684686
msg = "Failed to restart shell session."

0 commit comments

Comments
 (0)