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
@@ -11,10 +11,11 @@ from typing import TYPE_CHECKING, Any, cast
|
||||
from loguru import logger
|
||||
|
||||
from nanobot import __version__
|
||||
from nanobot.bus.events import INBOUND_META_USER_SHELL, OutboundMessage
|
||||
from nanobot.bus.events import OutboundMessage
|
||||
from nanobot.command.router import CommandContext, CommandRouter
|
||||
from nanobot.command.spec import BuiltinCommandSpec
|
||||
from nanobot.command.spec import CommandLifecycle as CommandLifecycle
|
||||
from nanobot.command.user_shell import cmd_user_shell
|
||||
from nanobot.providers.base import LLMUsage
|
||||
from nanobot.utils.cancellation import shield_and_drain
|
||||
from nanobot.utils.helpers import build_status_content
|
||||
@@ -924,30 +925,6 @@ async def cmd_help(ctx: CommandContext) -> OutboundMessage:
|
||||
)
|
||||
|
||||
|
||||
async def cmd_user_shell(ctx: CommandContext) -> OutboundMessage:
|
||||
"""Run a trusted local ``!command`` through nanobot's exec policy."""
|
||||
metadata = dict(ctx.msg.metadata or {})
|
||||
if (
|
||||
ctx.msg.channel != "websocket"
|
||||
or metadata.get("webui") is not True
|
||||
or metadata.get(INBOUND_META_USER_SHELL) is not True
|
||||
):
|
||||
return OutboundMessage(
|
||||
channel=ctx.msg.channel,
|
||||
chat_id=ctx.msg.chat_id,
|
||||
content="Shell commands are only available from a trusted local client.",
|
||||
metadata={**metadata, "render_as": "text"},
|
||||
)
|
||||
if not ctx.args.strip():
|
||||
return OutboundMessage(
|
||||
channel=ctx.msg.channel,
|
||||
chat_id=ctx.msg.chat_id,
|
||||
content="Type a command after `!`, for example `!pwd`.",
|
||||
metadata={**metadata, "render_as": "text"},
|
||||
)
|
||||
return await ctx.loop.execute_user_shell_command(ctx)
|
||||
|
||||
|
||||
def build_help_text(specs: tuple[BuiltinCommandSpec, ...] = CORE_COMMAND_SPECS) -> str:
|
||||
"""Build help text for the commands registered in one application."""
|
||||
lines = ["🐈 nanobot commands:"]
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Trusted user Shell commands, outside model-turn execution policy."""
|
||||
|
||||
from nanobot.agent.tools.context import RequestContext, bind_request_context, reset_request_context
|
||||
from nanobot.agent.tools.shell import ExecTool
|
||||
from nanobot.bus.events import INBOUND_META_USER_SHELL, OutboundMessage
|
||||
from nanobot.command.router import CommandContext
|
||||
from nanobot.security.workspace_access import bind_workspace_scope, reset_workspace_scope
|
||||
|
||||
|
||||
async def cmd_user_shell(ctx: CommandContext) -> OutboundMessage:
|
||||
"""Run a trusted local command to completion under its admitted policy."""
|
||||
metadata = dict(ctx.msg.metadata or {})
|
||||
command = ctx.args.strip()
|
||||
if (
|
||||
ctx.msg.channel != "websocket"
|
||||
or metadata.get("webui") is not True
|
||||
or metadata.get(INBOUND_META_USER_SHELL) is not True
|
||||
):
|
||||
content = "Shell commands are only available from a trusted local client."
|
||||
elif not command:
|
||||
content = "Type a command after `!`, for example `!pwd`."
|
||||
elif not isinstance(tool := ctx.loop.tools.get("exec"), ExecTool):
|
||||
content = "Shell execution is disabled in this nanobot configuration."
|
||||
else:
|
||||
session = ctx.session or await ctx.loop.sessions.state.get(ctx.key)
|
||||
scope = ctx.loop.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"!{command}",
|
||||
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:
|
||||
# A trusted user command owns the complete result. The model's
|
||||
# bounded observation/exec_session behavior remains in its tool.
|
||||
content = str(await tool.execute_to_completion(
|
||||
command=command, working_dir=str(scope.project_path),
|
||||
))
|
||||
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"},
|
||||
)
|
||||
Reference in new issue
Block a user