fix(agent): provide current workspace access before model requests

This commit is contained in:
Xubin Ren committed 2026-10-11 17:06:39 +08:00
1 parent d7d4b97df3
commit 1d69f630d7
17 files changed
+265 -44

No files matched your search

+7 -1
View File
@@ -620,6 +620,8 @@ class AgentLoop:
from nanobot.agent.tools.context import ToolContext
from nanobot.agent.tools.loader import ToolLoader
runtime_control = AgentRuntimeControl(self)
self.register_runtime_context_provider(runtime_control.workspace_context)
ctx = ToolContext(
config=self.tools_config,
workspace=str(self.workspace),
@@ -632,7 +634,7 @@ class AgentLoop:
image_generation_provider_configs=self._image_generation_provider_configs,
timezone=self.context.timezone or "UTC",
workspace_sandbox=self.workspace_scopes.sandbox_status,
runtime_control=AgentRuntimeControl(self),
runtime_control=runtime_control,
)
loader = ToolLoader()
registered = loader.load(ctx, self.tools)
@@ -741,6 +743,7 @@ class AgentLoop:
sender_id=ctx.msg.sender_id,
turn_id=ctx.delivery.route.turn_id or ctx.turn_id,
workspace=scope.project_path,
workspace_scope=scope,
log_content=ctx.session.policy.log_content and not ctx.ephemeral,
persist_session=ctx.session.policy.persist and not ctx.ephemeral,
can_receive_background_results=self._running,
@@ -1085,6 +1088,8 @@ class AgentLoop:
sender_id=pending_msg.sender_id,
turn_id=request_ctx.turn_id,
workspace=scope.project_path,
# Injected input continues the active run's tool policy.
workspace_scope=effective_scope,
log_content=request_ctx.log_content,
)
blocks = await self._resolve_runtime_context_for_request(
@@ -1197,6 +1202,7 @@ class AgentLoop:
)
request_ctx = dataclasses.replace(
request_ctx,
workspace_scope=effective_scope,
log_content=(
request_ctx.log_content and not ephemeral
and (session is None or session.policy.log_content)
+3 -1
View File
@@ -16,7 +16,7 @@ if TYPE_CHECKING:
from nanobot.config.schema import ProviderConfig, ToolsConfig
from nanobot.cron.service import CronService
from nanobot.providers.factory import ProviderSnapshot
from nanobot.security.workspace_access import WorkspaceSandboxStatus
from nanobot.security.workspace_access import WorkspaceSandboxStatus, WorkspaceScope
from nanobot.session.manager import SessionManager
from nanobot.utils.llm_runtime import LLMRuntime
@@ -44,6 +44,8 @@ class RequestContext:
# The host can consume completion messages after this request returns.
can_receive_background_results: bool = True
persist_session: bool = True
# Effective tool scope; injected inputs retain the active run's scope.
workspace_scope: WorkspaceScope | None = None
@runtime_checkable
+40 -6
View File
@@ -2,14 +2,22 @@
from __future__ import annotations
import json
from collections.abc import Mapping
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Protocol, TypeAlias, runtime_checkable
from nanobot.security.workspace_access import current_tool_workspace, workspace_sandbox_status
from nanobot.runtime_context import RuntimeContextBlock, wrap_runtime_context_lines
from nanobot.security.workspace_access import (
WorkspaceSandboxStatus,
WorkspaceScope,
current_tool_workspace,
workspace_sandbox_status,
)
if TYPE_CHECKING:
from nanobot.agent.tools.context import RequestContext
from nanobot.agent.tools.shell import ExecToolConfig
from nanobot.agent.tools.web import WebToolsConfig
from nanobot.config.schema import ModelPresetConfig
@@ -159,14 +167,43 @@ class AgentRuntimeControl:
self.__scratchpad: dict[str, JsonValue] = {}
self.__workspace_display: str | None = None
def snapshot(self) -> RuntimeSnapshot:
def _workspace_sandbox(self, scope: WorkspaceScope | None = None) -> WorkspaceSandboxStatus:
target = self.__target
workspace = current_tool_workspace(
target.workspace,
restrict_to_workspace=target.restrict_to_workspace,
sandbox_restricts_workspace=bool(target.exec_config.sandbox),
scope=scope,
)
assert workspace.project_path is not None
return workspace_sandbox_status(
workspace=workspace.project_path,
restrict_to_workspace=workspace.restrict_to_workspace,
)
async def workspace_context(self, request: RequestContext) -> RuntimeContextBlock:
"""Append current access facts without changing the reusable prompt prefix."""
status = self._workspace_sandbox(request.workspace_scope)
# A valid directory can contain newlines or prompt delimiters. Keep it data.
root = json.dumps(status.workspace_root, ensure_ascii=False)
root = root.replace("[", "\\u005b").replace("]", "\\u005d")
lines = [
f"Current project (JSON path): {root}",
(
"File access: limited to this project, plus tool-specific allowed paths."
if status.restrict_to_workspace
else "File access: not restricted to this project."
),
"OS permissions and tool-specific rules still apply.",
]
if self.__target.exec_config.sandbox:
lines.append(
"A shell sandbox is configured. Extra shell binds do not grant file-tool access."
)
return RuntimeContextBlock(source="workspace_access", content=wrap_runtime_context_lines(lines))
def snapshot(self) -> RuntimeSnapshot:
target = self.__target
return RuntimeSnapshot(
model=target.model,
model_preset=target.model_preset,
@@ -184,10 +221,7 @@ class AgentRuntimeControl:
web_config=_snapshot_web_config(target.web_config),
exec_config=_snapshot_exec_config(target.exec_config),
scratchpad=_snapshot_json_mapping(self.__scratchpad),
workspace_sandbox=workspace_sandbox_status(
workspace=workspace.project_path,
restrict_to_workspace=workspace.restrict_to_workspace,
).as_dict(),
workspace_sandbox=self._workspace_sandbox().as_dict(),
)
def set_model(self, model: str) -> LLMRuntime:
+3 -2
View File
@@ -348,10 +348,11 @@ def current_tool_workspace(
*,
restrict_to_workspace: bool = False,
sandbox_restricts_workspace: bool = False,
scope: WorkspaceScope | None = None,
) -> ToolWorkspace:
"""Return the workspace/access policy for the current tool call."""
"""Resolve tool policy from an explicit request scope or the bound call scope."""
scope = current_workspace_scope()
scope = scope if scope is not None else current_workspace_scope()
project_path = (
scope.project_path
if scope is not None