|
| 1 | +"""Runtime auto-detection and lifecycle management for the `aasm` sidecar (F115 / AAASM-1205). |
| 2 | +
|
| 3 | +The `init_assembly()` exported here is intentionally NOT re-exported from |
| 4 | +`agent_assembly` at the top level: the existing gateway-based |
| 5 | +`agent_assembly.init_assembly` keeps its meaning. Opt in to the runtime-managed |
| 6 | +flow with ``from agent_assembly.runtime import init_assembly``. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import os |
| 12 | +import shutil |
| 13 | +import socket |
| 14 | +import subprocess |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +__all__ = [ |
| 18 | + "BINARY_NAME", |
| 19 | + "DEFAULT_PORT", |
| 20 | + "DEFAULT_RUNTIME_HOST", |
| 21 | + "INSTALL_HINT", |
| 22 | + "find_aasm_binary", |
| 23 | + "init_assembly", |
| 24 | + "is_running", |
| 25 | + "start_runtime", |
| 26 | +] |
| 27 | + |
| 28 | +BINARY_NAME = "aasm" |
| 29 | +DEFAULT_PORT = 7878 |
| 30 | +DEFAULT_RUNTIME_HOST = "127.0.0.1" |
| 31 | + |
| 32 | +USER_LOCAL_BIN = Path.home() / ".local" / "bin" |
| 33 | +WHEEL_BUNDLED_BIN = Path(__file__).resolve().parent / "bin" |
| 34 | +DOCKER_BASE_BIN = Path("/usr/local/bin") |
| 35 | + |
| 36 | +RUNTIME_LOG_FILENAME = ".aasm-runtime.log" |
| 37 | + |
| 38 | +INSTALL_HINT = ( |
| 39 | + "agent-assembly runtime not found.\n" |
| 40 | + " Install with: pip install agent-assembly-python[runtime]\n" |
| 41 | + " Or manually: brew install agent-assembly/tap/aasm\n" |
| 42 | + " curl -fsSL https://get.agent-assembly.io | sh" |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +def find_aasm_binary() -> Path | None: |
| 47 | + """Locate the `aasm` binary across the 5 supported install paths. |
| 48 | +
|
| 49 | + Search order: ``$PATH`` (covers Homebrew and ``cargo install``) → |
| 50 | + ``~/.local/bin/aasm`` (curl installer default) → |
| 51 | + ``agent_assembly/bin/aasm`` (wheel-bundled with the ``[runtime]`` extra) → |
| 52 | + ``/usr/local/bin/aasm`` (Docker base image). Returns the first executable |
| 53 | + match, or ``None`` when no candidate exists. |
| 54 | + """ |
| 55 | + path_hit = shutil.which(BINARY_NAME) |
| 56 | + if path_hit: |
| 57 | + return Path(path_hit) |
| 58 | + for candidate in ( |
| 59 | + USER_LOCAL_BIN / BINARY_NAME, |
| 60 | + WHEEL_BUNDLED_BIN / BINARY_NAME, |
| 61 | + DOCKER_BASE_BIN / BINARY_NAME, |
| 62 | + ): |
| 63 | + if candidate.is_file() and os.access(candidate, os.X_OK): |
| 64 | + return candidate |
| 65 | + return None |
| 66 | + |
| 67 | + |
| 68 | +def is_running(port: int = DEFAULT_PORT, *, host: str = DEFAULT_RUNTIME_HOST) -> bool: |
| 69 | + """Return True iff a local TCP listener accepts a connect on ``host:port``. |
| 70 | +
|
| 71 | + A 100 ms connect window keeps the probe cheap on the common idle path; any |
| 72 | + socket error (refused, timeout, unreachable) is treated as no-sidecar. |
| 73 | + """ |
| 74 | + try: |
| 75 | + with socket.create_connection((host, port), timeout=0.1): |
| 76 | + return True |
| 77 | + except OSError: |
| 78 | + return False |
| 79 | + |
| 80 | + |
| 81 | +def start_runtime( |
| 82 | + binary: Path, |
| 83 | + *, |
| 84 | + port: int = DEFAULT_PORT, |
| 85 | + log_dir: Path | None = None, |
| 86 | +) -> subprocess.Popen[bytes]: |
| 87 | + """Spawn ``aasm serve --port <port>`` as a detached background subprocess. |
| 88 | +
|
| 89 | + Stdout and stderr are appended to ``<log_dir>/.aasm-runtime.log`` (default |
| 90 | + log directory is the current working directory) so the sidecar outlives |
| 91 | + the parent. ``start_new_session=True`` detaches the child from this |
| 92 | + process's controlling terminal. |
| 93 | + """ |
| 94 | + target_dir = log_dir if log_dir is not None else Path.cwd() |
| 95 | + log_path = target_dir / RUNTIME_LOG_FILENAME |
| 96 | + log_file = log_path.open("ab") |
| 97 | + return subprocess.Popen( |
| 98 | + [str(binary), "serve", "--port", str(port)], |
| 99 | + stdout=log_file, |
| 100 | + stderr=log_file, |
| 101 | + stdin=subprocess.DEVNULL, |
| 102 | + start_new_session=True, |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def init_assembly( |
| 107 | + agent_id: str | None = None, |
| 108 | + *, |
| 109 | + port: int = DEFAULT_PORT, |
| 110 | +) -> None: |
| 111 | + """Ensure the local ``aasm`` sidecar is running, starting it if necessary. |
| 112 | +
|
| 113 | + Lifecycle per F115 / AAASM-1205: |
| 114 | +
|
| 115 | + 1. Probe ``host:port`` via :func:`is_running`; return early if the sidecar |
| 116 | + is already up (idempotent re-init). |
| 117 | + 2. Resolve the binary via :func:`find_aasm_binary`. |
| 118 | + 3. Spawn the sidecar via :func:`start_runtime`. |
| 119 | +
|
| 120 | + ``agent_id`` is accepted to keep the ticket-specified signature stable; |
| 121 | + actual register-and-connect is performed by the existing gateway-aware |
| 122 | + ``agent_assembly.init_assembly`` once the sidecar is reachable. |
| 123 | +
|
| 124 | + Raises: |
| 125 | + RuntimeError: when no ``aasm`` binary is found on disk. The message |
| 126 | + contains copy-paste install commands for all supported channels. |
| 127 | + """ |
| 128 | + del agent_id # not consumed at the lifecycle layer; see docstring |
| 129 | + if is_running(port): |
| 130 | + return |
| 131 | + binary = find_aasm_binary() |
| 132 | + if binary is None: |
| 133 | + raise RuntimeError(INSTALL_HINT) |
| 134 | + start_runtime(binary, port=port) |
0 commit comments