3103266053
Launching a smolmachines agent from the dashboard inside tmux crashed with AttributeError: 'SmolmachinesBottle' object has no attribute 'claude_docker_argv' because the tmux pane-respawn path called `bottle.claude_docker_argv(...)` directly — a method that only existed on DockerBottle. The foreground-handoff path (curses endwin → subprocess.run → restore) doesn't hit it; it goes through `bottle.exec_claude` which is on the ABC. - Move the argv builder onto the `Bottle` ABC as `claude_argv(argv, *, tty=True) -> list[str]`. Both backends implement it; both `exec_claude` impls collapse to `subprocess.run(self.claude_argv(argv, tty=tty), check=False)`. - DockerBottle: rename `claude_docker_argv` → `claude_argv`, body unchanged. - SmolmachinesBottle: extract the argv-building from `exec_claude` into `claude_argv`; the new method returns the full `smolvm machine exec --name … -- runuser -u node -- claude …` argv. The `runuser` switch lives on the exec-framing prefix so the dashboard's `_build_resume_argv_with_fallback` split-at-"claude" trick keeps the UID switch when wrapping the claude tail in `sh -c "… --continue || …"`. - Dashboard: drop the docker-specific wording — local + helper arg names `docker_argv` → `claude_argv`; docstrings on `_build_resume_argv_with_fallback`, `_build_split_pane_argv`, `_build_respawn_pane_argv` now say "backend-exec argv". The shell-fallback wrap is unchanged; the existing logic works for smolmachines because `claude` is still the marker token. Tests: - `tests/unit/test_smolmachines_bottle.py` (new): locks down the smolmachines argv shape — prompt-file flag injection, guest-env `-e K=V` forwarding, TTY toggle, runuser-precedes- claude invariant. - `test_docker_bottle.py`: TestClaudeDockerArgv → TestClaudeArgv; method renames follow. - `test_dashboard_active_agents.py`: docstring follow. 615 unit tests pass. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
"""DockerBottle — concrete Bottle handle yielded by
|
|
DockerBottleBackend.launch.
|
|
|
|
Holds the container name plus the in-container prompt path so
|
|
exec_claude can transparently add --append-system-prompt-file when a
|
|
prompt was provisioned.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
from typing import Callable
|
|
|
|
from .. import Bottle, ExecResult
|
|
|
|
|
|
class DockerBottle(Bottle):
|
|
"""Concrete Bottle for Docker."""
|
|
|
|
def __init__(
|
|
self,
|
|
container: str,
|
|
teardown: Callable[[], None],
|
|
prompt_path_in_container: str | None,
|
|
):
|
|
self.name = container
|
|
self._teardown = teardown
|
|
self._prompt_path = prompt_path_in_container
|
|
self._closed = False
|
|
|
|
def claude_argv(
|
|
self, argv: list[str], *, tty: bool = True,
|
|
) -> list[str]:
|
|
full_argv = list(argv)
|
|
if self._prompt_path:
|
|
full_argv.extend(["--append-system-prompt-file", self._prompt_path])
|
|
cmd = ["docker", "exec"]
|
|
if tty:
|
|
cmd.append("-it")
|
|
cmd.extend([self.name, "claude", *full_argv])
|
|
return cmd
|
|
|
|
def exec_claude(self, argv: list[str], *, tty: bool = True) -> int:
|
|
return subprocess.run(
|
|
self.claude_argv(argv, tty=tty), check=False,
|
|
).returncode
|
|
|
|
def exec(self, script: str, *, user: str = "node") -> ExecResult:
|
|
# Pipe via stdin to `sh -s` so the caller never has to worry
|
|
# about quoting; the script source lands inside the container
|
|
# without crossing argv. `-u <user>` overrides the image's
|
|
# default USER — defaults to `node` which is already the
|
|
# image's USER, so the explicit flag is a no-op there but
|
|
# keeps the cross-backend contract uniform.
|
|
result = subprocess.run(
|
|
["docker", "exec", "-u", user, "-i", self.name, "sh", "-s"],
|
|
input=script,
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
return ExecResult(
|
|
returncode=result.returncode,
|
|
stdout=result.stdout,
|
|
stderr=result.stderr,
|
|
)
|
|
|
|
def cp_in(self, host_path: str, container_path: str) -> None:
|
|
subprocess.run(
|
|
["docker", "cp", host_path, f"{self.name}:{container_path}"],
|
|
stdout=subprocess.DEVNULL,
|
|
check=True,
|
|
)
|
|
|
|
def close(self) -> None:
|
|
if self._closed:
|
|
return
|
|
self._closed = True
|
|
self._teardown()
|