fix(exec): yield long-running commands by default (NAN-328) (#6385)
* fix(exec): yield long-running commands by default (NAN-328) * test(exec): avoid waiting for already observed output * test(exec): observe hard-timeout cleanup before asserting completion
This commit is contained in:
1 parent
421bd6b593
commit
26e1f41a00
14 files changed
+164
-49
No files matched your search
@@ -38,6 +38,7 @@ from nanobot.agent.tools.file_state import FileStateStore, bind_file_states, res
|
||||
from nanobot.agent.tools.message import capture_message_deliveries
|
||||
from nanobot.agent.tools.registry import ToolRegistry
|
||||
from nanobot.agent.tools.runtime_control import AgentRuntimeControl
|
||||
from nanobot.agent.tools.shell import ExecTool
|
||||
from nanobot.agent.turn_delivery import (
|
||||
TurnDelivery,
|
||||
TurnDeliveryFactory,
|
||||
@@ -871,7 +872,7 @@ class AgentLoop:
|
||||
"""Execute one trusted user command with the active workspace policy."""
|
||||
metadata = dict(ctx.msg.metadata or {})
|
||||
tool = self.tools.get("exec")
|
||||
if tool is None:
|
||||
if not isinstance(tool, ExecTool):
|
||||
content = "Shell execution is disabled in this nanobot configuration."
|
||||
else:
|
||||
session = ctx.session or await self.sessions.state.get(ctx.key)
|
||||
@@ -899,7 +900,7 @@ class AgentLoop:
|
||||
try:
|
||||
for turn_scope in ctx.turn_scopes:
|
||||
turn_scope_stack.enter_context(turn_scope)
|
||||
result = await tool.execute(
|
||||
result = await tool.execute_to_completion(
|
||||
command=ctx.args.strip(),
|
||||
working_dir=str(scope.project_path),
|
||||
)
|
||||
|
||||
@@ -762,7 +762,7 @@ def format_session_poll(session_id: str, poll: _SessionPoll) -> str:
|
||||
nullable=True,
|
||||
),
|
||||
until_exit=BooleanSchema(
|
||||
description="Wait for the process to exit.",
|
||||
description="Wait for exit within timeout_ms; return the active session if the wait expires.",
|
||||
default=False,
|
||||
),
|
||||
timeout_ms=IntegerSchema(
|
||||
|
||||
@@ -135,7 +135,7 @@ def _exec_parameters(config: ExecToolConfig) -> dict[str, Any]:
|
||||
nullable=True,
|
||||
),
|
||||
yield_time_ms=IntegerSchema(
|
||||
description="Return after this many milliseconds if still running; omit to wait for exit.",
|
||||
description="Return after this many milliseconds if still running (default 1000, max 30000); execution continues.",
|
||||
minimum=0,
|
||||
maximum=MAX_YIELD_MS,
|
||||
nullable=True,
|
||||
@@ -317,11 +317,23 @@ class ExecTool(Tool):
|
||||
return prepared
|
||||
return await self._execute_session(prepared, yield_time_ms, max_output_chars)
|
||||
|
||||
async def execute_to_completion(self, command: str, working_dir: str) -> str:
|
||||
"""Return the complete result for a trusted user shell command, without an agent observer."""
|
||||
try:
|
||||
prepared = self._prepare_command(command, working_dir)
|
||||
except ValueError as exc:
|
||||
return ToolResult.error(f"Error: {exc}")
|
||||
if isinstance(prepared, str):
|
||||
return prepared
|
||||
return await self._execute_session(prepared, None, None, until_exit=True)
|
||||
|
||||
async def _execute_session(
|
||||
self,
|
||||
prepared: _PreparedCommand,
|
||||
yield_time_ms: int | None,
|
||||
max_output_chars: int | None,
|
||||
*,
|
||||
until_exit: bool = False,
|
||||
) -> str:
|
||||
try:
|
||||
session_id, poll = await self._session_manager.start(
|
||||
@@ -331,11 +343,10 @@ class ExecTool(Tool):
|
||||
timeout=prepared.timeout,
|
||||
shell_program=prepared.shell_program,
|
||||
login=prepared.login,
|
||||
yield_time_ms=(
|
||||
0 if yield_time_ms is None
|
||||
else clamp_session_int(yield_time_ms, DEFAULT_YIELD_MS, 0, MAX_YIELD_MS)
|
||||
yield_time_ms=0 if until_exit else clamp_session_int(
|
||||
yield_time_ms, DEFAULT_YIELD_MS, 0, MAX_YIELD_MS,
|
||||
),
|
||||
until_exit=yield_time_ms is None,
|
||||
until_exit=until_exit,
|
||||
stdin=(asyncio.subprocess.DEVNULL if yield_time_ms is None else asyncio.subprocess.PIPE),
|
||||
idle_timeout=self._exec_config.idle_timeout,
|
||||
idle_action=self._exec_config.idle_action,
|
||||
@@ -348,7 +359,7 @@ class ExecTool(Tool):
|
||||
),
|
||||
)
|
||||
result = format_session_poll(session_id, poll)
|
||||
if not poll.done and yield_time_ms is not None:
|
||||
if not poll.done:
|
||||
result += "\nInitial wait ended; session remains active."
|
||||
return ToolResult.error(result) if poll.failed else result
|
||||
except Exception as exc:
|
||||
|
||||
Reference in new issue
Block a user