refactor(commands): own trusted Shell completion outside agent loop
This commit is contained in:
1 parent
e6127aa324
commit
2b44088413
4 files changed
+140
-83
No files matched your search
@@ -34,7 +34,6 @@ 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,
|
||||
@@ -896,50 +895,6 @@ class AgentLoop:
|
||||
return
|
||||
await dispatch_and_publish()
|
||||
|
||||
async def execute_user_shell_command(self, ctx: CommandContext) -> OutboundMessage:
|
||||
"""Execute one trusted user command with the active workspace policy."""
|
||||
metadata = dict(ctx.msg.metadata or {})
|
||||
tool = self.tools.get("exec")
|
||||
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)
|
||||
scope = self.workspace_scopes.for_turn(
|
||||
channel=ctx.msg.channel,
|
||||
message_metadata=metadata,
|
||||
session_metadata=session.metadata,
|
||||
)
|
||||
request_token = bind_request_context(RequestContext(
|
||||
channel=ctx.msg.channel,
|
||||
chat_id=ctx.msg.chat_id,
|
||||
message_id=metadata.get("message_id"),
|
||||
session_key=ctx.key,
|
||||
session_generation=session.generation,
|
||||
original_user_text=f"!{ctx.args.strip()}",
|
||||
runtime=ctx.runtime,
|
||||
metadata=metadata,
|
||||
sender_id=ctx.msg.sender_id,
|
||||
turn_id=metadata.get("webui_turn_id"),
|
||||
workspace=scope.project_path,
|
||||
log_content=session.policy.log_content,
|
||||
))
|
||||
workspace_token = bind_workspace_scope(scope)
|
||||
try:
|
||||
result = await tool.execute_to_completion(
|
||||
command=ctx.args.strip(),
|
||||
working_dir=str(scope.project_path),
|
||||
)
|
||||
content = str(result)
|
||||
finally:
|
||||
reset_workspace_scope(workspace_token)
|
||||
reset_request_context(request_token)
|
||||
return OutboundMessage(
|
||||
channel=ctx.msg.channel,
|
||||
chat_id=ctx.msg.chat_id,
|
||||
content=content,
|
||||
metadata={**metadata, "render_as": "text"},
|
||||
)
|
||||
|
||||
def _track_active_task(self, key: str, task: asyncio.Task[Any]) -> None:
|
||||
tasks = self._active_tasks.setdefault(key, set())
|
||||
tasks.add(task)
|
||||
|
||||
Reference in new issue
Block a user