mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-29 00:38:33 +00:00
Some checks failed
CI and Release / Docker / Build and test / amd64 (push) Has been cancelled
CI and Release / Docker / Build and test / arm64 (push) Has been cancelled
Security / CodeQL / Analyze python (push) Has been cancelled
CI / Python quality / Pre-commit (push) Has been cancelled
CI / Python tests / Unit Tests - py3.12 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.13 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.14 (push) Has been cancelled
CI / Python packages / Build and verify distributions (push) Has been cancelled
CI / Python quality / GitHub Actions (push) Has been cancelled
CI / Python tests / Hermes embedded - ubuntu-latest (push) Has been cancelled
CI / Python tests / Hermes embedded - windows-latest (push) Has been cancelled
CI / Python tests / Unit Tests - py3.11 (push) Has been cancelled
CI / ReMe Studio / Studio checks (push) Has been cancelled
Deploy / Documentation / Build documentation (push) Has been cancelled
CI / Windows / CLI smoke - py3.11 (push) Has been cancelled
Security / CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI and Release / Docker / Publish multi-platform tags (push) Has been cancelled
Deploy / Documentation / deploy (push) Has been cancelled
* refactor(core): reorganize the ReMe runtime and shared helpers * test(status): import the base component from its canonical module * test: remove redundant unit checks * refactor(core): use canonical runtime imports and shared CLI discovery Restore evolve behavior to the base branch and update its imports. Use Application as the runtime entry point and encode SSE in the HTTP service. * fix(cli): honor caller config when service replay is incomplete Use partial startup overrides for port probes and resolve named configs before requesting the service process working directory.
70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
"""Execute a shell command and return its stdout."""
|
|
|
|
import math
|
|
import os
|
|
from typing import Any
|
|
|
|
from ..base_step import BaseStep
|
|
from ...core.registry import R
|
|
from ...utils.local_backend import LocalProcessBackend, ProcessTimeout
|
|
|
|
DEFAULT_TIMEOUT = 60.0 * 60 * 24
|
|
|
|
|
|
@R.register("shell_step")
|
|
class ShellStep(BaseStep):
|
|
"""Run shell commands with bounded output and lifecycle-owned child processes."""
|
|
|
|
async def execute(self):
|
|
assert self.context is not None
|
|
|
|
command = self.context.get("cmd", "")
|
|
timeout, timeout_error = self._parse_timeout(self.context.get("shell_timeout", DEFAULT_TIMEOUT))
|
|
if not isinstance(command, str) or not command.strip():
|
|
self.context.response.success = False
|
|
self.context.response.answer = "cmd is required"
|
|
return self.context.response
|
|
if timeout_error:
|
|
self.context.response.success = False
|
|
self.context.response.answer = timeout_error
|
|
return self.context.response
|
|
|
|
argv = (
|
|
["/bin/sh", "-c", command] if os.name == "posix" else [os.environ.get("COMSPEC", "cmd.exe"), "/c", command]
|
|
)
|
|
timed_out = False
|
|
try:
|
|
result = await LocalProcessBackend().exec_shell(
|
|
argv,
|
|
cwd=str(self.workspace_path),
|
|
timeout=timeout,
|
|
raise_errors=True,
|
|
)
|
|
except ProcessTimeout as exc:
|
|
result, timed_out = exc.result, True
|
|
stdout, stderr = result.stdout.decode(errors="replace"), result.stderr.decode(errors="replace")
|
|
if timed_out:
|
|
self.context.response.success = False
|
|
self.context.response.answer = f"Shell command timed out after {timeout:g}s"
|
|
else:
|
|
self.context.response.success = result.exit_code == 0
|
|
self.context.response.answer = stdout if stdout or result.exit_code == 0 else stderr
|
|
|
|
self.context.response.metadata.update(
|
|
{
|
|
"returncode": result.exit_code,
|
|
"stderr": stderr,
|
|
"shell_timeout": timeout,
|
|
},
|
|
)
|
|
return self.context.response
|
|
|
|
@staticmethod
|
|
def _parse_timeout(raw: Any) -> tuple[float, str]:
|
|
try:
|
|
timeout = float(raw)
|
|
except (TypeError, ValueError, OverflowError):
|
|
return DEFAULT_TIMEOUT, "shell_timeout must be a positive number"
|
|
if isinstance(raw, bool) or not math.isfinite(timeout) or timeout <= 0:
|
|
return DEFAULT_TIMEOUT, "shell_timeout must be a positive number"
|
|
return timeout, ""
|