ReMe/reme/reme.py
jinliyl 2e87b7a52e
Some checks are pending
Pre-commit / run (ubuntu-latest) (push) Waiting to run
Tests ReMe / Unit Tests - py3.11 (push) Waiting to run
Tests ReMe / Unit Tests - py3.12 (push) Waiting to run
Tests ReMe / Unit Tests - py3.13 (push) Waiting to run
Windows Smoke / CLI smoke - py3.11 (push) Waiting to run
feat(core): add shell execution and runtime memory status (#344)
* feat(core): add shell command execution and memory status reporting

- Introduce ShellStep for executing shell commands with timeout support
- Add StatusStep to report memory estimates for stateful data components
- Register shell and status commands in default configuration
- Update documentation with new reme status and shell command capabilities
- Implement comprehensive unit tests for both new step types
- Add support for asynchronous command execution with proper error handling

* feat(config): add log_config option to suppress config loading logs

- Add log_config parameter to resolve_app_config function with default True
- Conditionally log config loading messages based on log_config flag
- Update reme.py and service_utils.py to use log_config=False for client calls
- Suppress config logging in user-facing contexts to avoid output pollution

refactor(shell): rename command parameter to cmd for clarity

- Change 'command' to 'cmd' in default.yaml configuration schema
- Rename 'timeout' to 'shell_timeout' to avoid parameter name collisions
- Update ShellStep to accept both legacy and new parameter names
- Maintain backward compatibility with existing command/timeout usage

test(shell): add comprehensive tests for shell step parameter handling

- Add test cases for new cmd and shell_timeout parameter names
- Verify legacy command and timeout parameters still work
- Test blank command rejection message updated to use cmd
- Create integration test for shell parameter payload passing

* fix(shell): ensure proper environment loading and process timeout handling

- Move load_env() call to execute before parse_args() in main function
- Add proper process group killing for timeout scenarios on POSIX systems
- Implement recursive child process termination on Windows for proper cleanup
- Change parameter name from 'timeout' to 'shell_timeout' in shell execution
- Remove support for legacy 'command' and 'timeout' parameter names
- Update test cases to verify new timeout behavior and parameter requirements
- Add comments explaining component size tracking implementation details
2026-07-14 16:31:41 +08:00

78 lines
3.2 KiB
Python

"""ReMe memory management application entry point."""
import asyncio
import sys
from .application import Application
from .components import R
from .components.service.cli_service import prepare_start_config, should_precheck_start
from .config import parse_args, resolve_app_config
from .enumeration import ComponentEnum
from .utils import cli_find_reme, load_env, precheck_start, running_service_config
_CLIENT_KWARGS = {"host", "port", "timeout", "transport", "command", "args", "show_metadata"}
class ReMe(Application):
"""ReMe memory management application."""
async def call_server(action: str, **kwargs):
"""Call the running server with a client matching its *actual* service config.
The client backend and its transport/host/port are taken from the running
``reme start`` process — its start args replayed through the same
``resolve_app_config`` the server used — so a bare ``reme <action>`` reaches
the server however it was actually started (``http`` REST or ``mcp``
streamable-http / sse / stdio), including ``service.*`` overrides that never
touched the on-disk config file. Falls back to local config resolution when
no server is detected. Explicit ``backend=`` / ``transport=`` / ``host=`` /
``port=`` kwargs still win and never leak into the tool payload.
"""
# config-selecting keys steer client construction; they are not tool args.
resolve_kwargs = {}
if isinstance(kwargs.get("config"), str):
resolve_kwargs["config"] = kwargs.pop("config")
if isinstance(kwargs.get("service"), dict):
resolve_kwargs["service"] = kwargs.pop("service")
# Prefer the running server's real config; fall back to the local config file.
service = running_service_config()
if service is None:
service = resolve_app_config(log_config=False, **resolve_kwargs).get("service")
service = service if isinstance(service, dict) else {}
backend: str = kwargs.pop("backend", None) or service.get("backend", "http")
# Seed client kwargs from the service config only when we are actually using
# that service's backend — transport/host/port are backend-specific, so an
# explicit backend override must not inherit the other backend's settings.
seed = service if backend == service.get("backend") else {}
client_kwargs = {k: seed[k] for k in _CLIENT_KWARGS if k in seed}
client_kwargs.update({key: kwargs.pop(key) for key in list(kwargs) if key in _CLIENT_KWARGS})
client_cls = R.get(ComponentEnum.CLIENT, backend)
if client_cls is None:
raise ValueError(f"Unknown client backend: {backend!r}")
async with client_cls(**client_kwargs) as client:
async for chunk in client(action=action, **kwargs):
print(chunk, end="", flush=True)
print()
def main():
"""Parse CLI arguments and launch the appropriate mode."""
load_env()
action, kwargs = parse_args(*sys.argv[1:])
if action == "start":
kwargs = prepare_start_config(kwargs)
if should_precheck_start(kwargs) and not precheck_start(kwargs.get("service")):
return
ReMe(**kwargs).run_app()
elif action == "find_reme":
cli_find_reme()
else:
asyncio.run(call_server(action, **kwargs))
if __name__ == "__main__":
main()