fix(exec): filter terminal control sequences from shell output
This commit is contained in:
1 parent
32f5cc76a4
commit
4fa125e134
4 files changed
+167
-7
No files matched your search
@@ -27,6 +27,7 @@ from nanobot.agent.tools.schema import (
|
||||
tool_parameters_schema,
|
||||
)
|
||||
from nanobot.agent.tools.shell_config import ExecToolConfig
|
||||
from nanobot.agent.tools.shell_output import ShellOutputFilter
|
||||
|
||||
DEFAULT_YIELD_MS = 1000
|
||||
MAX_YIELD_MS = 30_000
|
||||
@@ -175,9 +176,10 @@ class _ExecSession:
|
||||
if stream is None:
|
||||
return
|
||||
decoder = codecs.getincrementaldecoder("utf-8")(errors="replace")
|
||||
output_filter = ShellOutputFilter()
|
||||
while True:
|
||||
chunk = await stream.read(4096)
|
||||
text = decoder.decode(chunk, final=not chunk)
|
||||
text = output_filter.feed(decoder.decode(chunk, final=not chunk))
|
||||
async with self._lock:
|
||||
buffer.append(text)
|
||||
if not chunk:
|
||||
|
||||
@@ -38,6 +38,7 @@ from nanobot.agent.tools.schema import (
|
||||
tool_parameters_schema,
|
||||
)
|
||||
from nanobot.agent.tools.shell_config import ExecToolConfig
|
||||
from nanobot.agent.tools.shell_output import ShellOutputFilter
|
||||
from nanobot.config.paths import get_media_dir
|
||||
from nanobot.security.workspace_access import current_scope_allows_loopback, current_tool_workspace
|
||||
from nanobot.security.workspace_policy import is_path_within
|
||||
@@ -309,13 +310,13 @@ class ExecTool(Tool):
|
||||
|
||||
output_parts: list[str] = []
|
||||
|
||||
if stdout:
|
||||
output_parts.append(stdout.decode("utf-8", errors="replace"))
|
||||
stdout_text = ShellOutputFilter().feed(stdout.decode("utf-8", errors="replace"))
|
||||
if stdout_text:
|
||||
output_parts.append(stdout_text)
|
||||
|
||||
if stderr:
|
||||
stderr_text = stderr.decode("utf-8", errors="replace")
|
||||
if stderr_text.strip():
|
||||
output_parts.append(f"STDERR:\n{stderr_text}")
|
||||
stderr_text = ShellOutputFilter().feed(stderr.decode("utf-8", errors="replace"))
|
||||
if stderr_text.strip():
|
||||
output_parts.append(f"STDERR:\n{stderr_text}")
|
||||
|
||||
output_parts.append(f"\nExit code: {process.returncode}")
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Terminal control filtering for shell tool output."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class ShellOutputFilter:
|
||||
"""Strip terminal escapes, preserving visible text and whitespace.
|
||||
|
||||
Use one instance per decoded stdout/stderr stream so escape sequences can
|
||||
span reads. Only parser state is retained, never control-string payloads.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._state = "text"
|
||||
self._string_escape = False
|
||||
|
||||
def feed(self, text: str) -> str:
|
||||
if self._state == "text" and "\x1b" not in text:
|
||||
return text
|
||||
|
||||
output: list[str] = []
|
||||
for char in text:
|
||||
if self._state in ("osc", "string"):
|
||||
if (self._string_escape and char == "\\") or (
|
||||
self._state == "osc" and char == "\x07"
|
||||
):
|
||||
self._state = "text"
|
||||
self._string_escape = char == "\x1b"
|
||||
elif char == "\x1b":
|
||||
self._state = "escape"
|
||||
elif self._state in ("escape", "intermediate"):
|
||||
if self._state == "escape" and char == "[":
|
||||
self._state = "csi"
|
||||
elif self._state == "escape" and char in "]PX^_":
|
||||
self._state = "osc" if char == "]" else "string"
|
||||
self._string_escape = False
|
||||
elif " " <= char <= "/":
|
||||
self._state = "intermediate"
|
||||
else:
|
||||
self._state = "text"
|
||||
if not "0" <= char <= "~":
|
||||
output.append(char)
|
||||
elif self._state == "csi":
|
||||
if "@" <= char <= "~":
|
||||
self._state = "text"
|
||||
elif not " " <= char <= "?":
|
||||
self._state = "text"
|
||||
output.append(char)
|
||||
else:
|
||||
output.append(char)
|
||||
return "".join(output)
|
||||
Reference in new issue
Block a user