fix(my): report current workspace access in self-inspection

This commit is contained in:
Xubin Ren committed 2026-10-11 17:06:39 +08:00
1 parent 9261e731ce
commit e79a0099f6
4 files changed
+177 -5

No files matched your search

+16
View File
@@ -7,6 +7,8 @@ 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
if TYPE_CHECKING:
from nanobot.agent.tools.shell import ExecToolConfig
from nanobot.agent.tools.web import WebToolsConfig
@@ -25,6 +27,7 @@ RUNTIME_SNAPSHOT_KEYS = frozenset({
"max_iterations",
"context_window_tokens",
"workspace",
"workspace_sandbox",
"provider_retry_mode",
"max_tool_result_chars",
"tool_names",
@@ -59,6 +62,7 @@ class RuntimeSnapshot:
web_config: dict[str, object]
exec_config: dict[str, object]
scratchpad: dict[str, JsonValue]
workspace_sandbox: dict[str, object]
def as_mapping(self) -> Mapping[str, object]:
"""Return the fixed public names understood by ``MyTool``."""
@@ -69,6 +73,7 @@ class RuntimeSnapshot:
"max_iterations": self.max_iterations,
"context_window_tokens": self.context_window_tokens,
"workspace": self.workspace,
"workspace_sandbox": self.workspace_sandbox,
"provider_retry_mode": self.provider_retry_mode,
"max_tool_result_chars": self.max_tool_result_chars,
"tool_names": self.tool_names,
@@ -115,6 +120,7 @@ class _RuntimeControlTarget(Protocol):
max_tool_result_chars: int
web_config: WebToolsConfig
exec_config: ExecToolConfig
restrict_to_workspace: bool
@property
def model(self) -> str: ...
@@ -155,6 +161,12 @@ class AgentRuntimeControl:
def snapshot(self) -> RuntimeSnapshot:
target = self.__target
workspace = current_tool_workspace(
target.workspace,
restrict_to_workspace=target.restrict_to_workspace,
sandbox_restricts_workspace=bool(target.exec_config.sandbox),
)
assert workspace.project_path is not None
return RuntimeSnapshot(
model=target.model,
model_preset=target.model_preset,
@@ -172,6 +184,10 @@ 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(),
)
def set_model(self, model: str) -> LLMRuntime:
+8 -1
View File
@@ -142,6 +142,10 @@ class MyTool(Tool):
"Scratchpad keys persist across turns but not restarts.\n"
"Current routing metadata is available read-only via request.channel, "
"request.chat_id, and request.sender_id.\n"
"For path access, check workspace_sandbox for the current project and "
"workspace restriction. Check exec_config for shell sandbox settings and extra binds. "
"Shell binds do not grant file-tool access. OS permissions still apply.\n"
"workspace is the legacy agent-directory display, not the current project or an access grant.\n"
"Use model_preset for session-scoped model or context changes; direct "
"model/context_window_tokens writes are disabled during active sessions.\n"
"Note: web_config and exec_config are readable but read-only.\n"
@@ -206,7 +210,7 @@ class MyTool(Tool):
for part in parts:
if part in self._DENIED_ATTRS or part.startswith("__"):
return None, f"'{part}' is not accessible"
if part in self.BLOCKED:
if part in self.BLOCKED and path != "workspace_sandbox.restrict_to_workspace":
return None, f"'{part}' is not accessible"
if part.lower() in self._SENSITIVE_NAMES:
return None, f"'{part}' is not accessible"
@@ -238,6 +242,8 @@ class MyTool(Tool):
# Mapping — small: show content; large: show keys for dot-path navigation
if isinstance(val, Mapping):
value_mapping = cast(Mapping[object, object], val)
if key == "workspace_sandbox":
return f"{key}: {dict(value_mapping)!r}"
ks = list(value_mapping.keys())
if not ks:
return f"{key}: {{}}" if key else "{}"
@@ -344,6 +350,7 @@ class MyTool(Tool):
))
for k in (
"workspace",
"workspace_sandbox",
"provider_retry_mode",
"max_tool_result_chars",
"web_config",