refactor(backend): fold BottleProvisioner back into BottleBackend
test / run tests/run_tests.py (pull_request) Successful in 14s
test / run tests/run_tests.py (pull_request) Successful in 14s
BottleProvisioner had no independent identity — no state, only one
caller, never selected, never crossed a method boundary as data. It
was a method dressed up as a class. Reverting that turn:
- BottleBackend gains an abstract provision(plan, target).
- DockerBottleBackend.provision absorbs the body that lived on
DockerBottleProvisioner.
- backend/docker/provisioner.py deleted.
- BottleProvisioner ABC removed from backend/__init__.py.
- launch now calls self.provision(plan, container) directly.
Net: -1 file, -1 class, -1 ABC. Same behavior; tests pass.
This commit is contained in:
@@ -104,20 +104,6 @@ class Bottle(ABC):
|
|||||||
def close(self) -> None: ...
|
def close(self) -> None: ...
|
||||||
|
|
||||||
|
|
||||||
class BottleProvisioner(ABC):
|
|
||||||
"""Copies host-side files (prompt, skills, SSH keys, .git) into a
|
|
||||||
running bottle after the container/machine is up. Owned by a
|
|
||||||
BottleBackend; called from its launch step before yielding the
|
|
||||||
Bottle handle."""
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def provision(self, plan: BottlePlan, target: str) -> str | None:
|
|
||||||
"""Provision the running bottle described by `plan`. `target`
|
|
||||||
identifies the running instance in backend-specific terms
|
|
||||||
(Docker: resolved container name; fly: machine id). Returns the
|
|
||||||
in-container prompt path if a prompt was provisioned, else
|
|
||||||
None — the Bottle handle uses it to decide whether to add
|
|
||||||
--append-system-prompt-file to claude's argv."""
|
|
||||||
|
|
||||||
|
|
||||||
class BottleBackend(ABC):
|
class BottleBackend(ABC):
|
||||||
@@ -136,6 +122,17 @@ class BottleBackend(ABC):
|
|||||||
def launch(self, plan: BottlePlan) -> AbstractContextManager[Bottle]:
|
def launch(self, plan: BottlePlan) -> AbstractContextManager[Bottle]:
|
||||||
"""Build/run the bottle and yield a handle; tear down on exit."""
|
"""Build/run the bottle and yield a handle; tear down on exit."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def provision(self, plan: BottlePlan, target: str) -> str | None:
|
||||||
|
"""Copy host-side files (prompt, skills, SSH keys, .git) into
|
||||||
|
the running bottle. Called from `launch` after the container/
|
||||||
|
machine is up. `target` identifies the running instance in
|
||||||
|
backend-specific terms (Docker: resolved container name; fly:
|
||||||
|
machine id). Returns the in-container prompt path if a prompt
|
||||||
|
was provisioned, else None — the Bottle handle uses it to
|
||||||
|
decide whether to add --append-system-prompt-file to claude's
|
||||||
|
argv."""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def prepare_cleanup(self) -> BottleCleanupPlan:
|
def prepare_cleanup(self) -> BottleCleanupPlan:
|
||||||
"""Enumerate orphaned resources from previous bottles. No side
|
"""Enumerate orphaned resources from previous bottles. No side
|
||||||
@@ -178,7 +175,6 @@ __all__ = [
|
|||||||
"BottleBackend",
|
"BottleBackend",
|
||||||
"BottleCleanupPlan",
|
"BottleCleanupPlan",
|
||||||
"BottlePlan",
|
"BottlePlan",
|
||||||
"BottleProvisioner",
|
|
||||||
"BottleSpec",
|
"BottleSpec",
|
||||||
"get_bottle_backend",
|
"get_bottle_backend",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ The bulk of the implementation lives in sibling modules:
|
|||||||
- bottle_plan: DockerBottlePlan
|
- bottle_plan: DockerBottlePlan
|
||||||
- bottle_cleanup_plan: DockerBottleCleanupPlan
|
- bottle_cleanup_plan: DockerBottleCleanupPlan
|
||||||
- bottle: DockerBottle handle
|
- bottle: DockerBottle handle
|
||||||
- provisioner: DockerBottleProvisioner
|
|
||||||
- backend: DockerBottleBackend
|
- backend: DockerBottleBackend
|
||||||
|
|
||||||
This file only re-exports the public names so
|
This file only re-exports the public names so
|
||||||
@@ -21,12 +20,10 @@ from .backend import DockerBottleBackend
|
|||||||
from .bottle import DockerBottle
|
from .bottle import DockerBottle
|
||||||
from .bottle_cleanup_plan import DockerBottleCleanupPlan
|
from .bottle_cleanup_plan import DockerBottleCleanupPlan
|
||||||
from .bottle_plan import DockerBottlePlan
|
from .bottle_plan import DockerBottlePlan
|
||||||
from .provisioner import DockerBottleProvisioner
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"DockerBottle",
|
"DockerBottle",
|
||||||
"DockerBottleBackend",
|
"DockerBottleBackend",
|
||||||
"DockerBottleCleanupPlan",
|
"DockerBottleCleanupPlan",
|
||||||
"DockerBottlePlan",
|
"DockerBottlePlan",
|
||||||
"DockerBottleProvisioner",
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ from . import util as docker_mod
|
|||||||
from .bottle import DockerBottle
|
from .bottle import DockerBottle
|
||||||
from .bottle_cleanup_plan import DockerBottleCleanupPlan
|
from .bottle_cleanup_plan import DockerBottleCleanupPlan
|
||||||
from .bottle_plan import DockerBottlePlan
|
from .bottle_plan import DockerBottlePlan
|
||||||
from .provisioner import DockerBottleProvisioner
|
|
||||||
|
|
||||||
|
|
||||||
# Where the repo root lives, for `docker build` context. Computed once.
|
# Where the repo root lives, for `docker build` context. Computed once.
|
||||||
@@ -40,7 +39,6 @@ class DockerBottleBackend(BottleBackend):
|
|||||||
(default)."""
|
(default)."""
|
||||||
|
|
||||||
name = "docker"
|
name = "docker"
|
||||||
_provisioner: DockerBottleProvisioner = DockerBottleProvisioner()
|
|
||||||
|
|
||||||
def prepare(self, spec: BottleSpec, *, stage_dir: Path) -> DockerBottlePlan:
|
def prepare(self, spec: BottleSpec, *, stage_dir: Path) -> DockerBottlePlan:
|
||||||
"""Resolve names, validate, write scratch files. No Docker
|
"""Resolve names, validate, write scratch files. No Docker
|
||||||
@@ -187,7 +185,7 @@ class DockerBottleBackend(BottleBackend):
|
|||||||
container = self._run_agent_container(plan, state["internal_network"])
|
container = self._run_agent_container(plan, state["internal_network"])
|
||||||
state["container"] = container
|
state["container"] = container
|
||||||
|
|
||||||
prompt_path = self._provisioner.provision(plan, container)
|
prompt_path = self.provision(plan, container)
|
||||||
|
|
||||||
bottle = DockerBottle(container, teardown, prompt_path)
|
bottle = DockerBottle(container, teardown, prompt_path)
|
||||||
yield bottle
|
yield bottle
|
||||||
@@ -260,6 +258,65 @@ class DockerBottleBackend(BottleBackend):
|
|||||||
docker_args[name_idx] = container
|
docker_args[name_idx] = container
|
||||||
info(f"name conflict; retrying as {container}")
|
info(f"name conflict; retrying as {container}")
|
||||||
|
|
||||||
|
def provision(self, plan: BottlePlan, target: str) -> str | None:
|
||||||
|
"""Copy prompt, skills, ssh keys, and (optionally) .git into
|
||||||
|
the running container. `target` is the resolved container
|
||||||
|
name. Returns the in-container prompt path if a prompt was
|
||||||
|
provisioned, else None — the Bottle handle uses it to decide
|
||||||
|
whether to add --append-system-prompt-file to claude's argv."""
|
||||||
|
assert isinstance(plan, DockerBottlePlan), (
|
||||||
|
f"DockerBottleBackend.provision expects DockerBottlePlan, "
|
||||||
|
f"got {type(plan).__name__}"
|
||||||
|
)
|
||||||
|
container = target
|
||||||
|
container_home = os.environ.get("CLAUDE_BOTTLE_CONTAINER_HOME", "/home/node")
|
||||||
|
in_container_prompt_path = f"{container_home}/.claude-bottle-prompt.txt"
|
||||||
|
|
||||||
|
subprocess.run(
|
||||||
|
["docker", "cp", str(plan.prompt_file), f"{container}:{in_container_prompt_path}"],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
# `docker cp` preserves host UID; re-own/mode as root so node
|
||||||
|
# can read its own mode-600 prompt regardless of host UID.
|
||||||
|
subprocess.run(
|
||||||
|
["docker", "exec", "-u", "0", container, "chown", "node:node", in_container_prompt_path],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
subprocess.run(
|
||||||
|
["docker", "exec", "-u", "0", container, "chmod", "600", in_container_prompt_path],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
agent = plan.spec.manifest.agents[plan.spec.agent_name]
|
||||||
|
if agent.skills:
|
||||||
|
skills_mod.skills_copy_into(container, list(agent.skills))
|
||||||
|
|
||||||
|
bottle = plan.spec.manifest.bottle_for(plan.spec.agent_name)
|
||||||
|
if bottle.ssh:
|
||||||
|
proxy_host_port = pipelock.pipelock_proxy_host_port(plan.slug)
|
||||||
|
ssh_mod.ssh_setup(container, plan.stage_dir, proxy_host_port, bottle.ssh)
|
||||||
|
|
||||||
|
if plan.spec.copy_cwd and Path(plan.spec.user_cwd, ".git").is_dir():
|
||||||
|
info(f"copying {plan.spec.user_cwd}/.git -> {container}:/home/node/workspace/.git")
|
||||||
|
subprocess.run(
|
||||||
|
["docker", "cp", f"{plan.spec.user_cwd}/.git", f"{container}:/home/node/workspace/.git"],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
"docker", "exec", "-u", "0", container,
|
||||||
|
"chown", "-R", "node:node", "/home/node/workspace/.git",
|
||||||
|
],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
return in_container_prompt_path if agent.prompt else None
|
||||||
|
|
||||||
# --- Cleanup ---
|
# --- Cleanup ---
|
||||||
|
|
||||||
def prepare_cleanup(self) -> DockerBottleCleanupPlan:
|
def prepare_cleanup(self) -> DockerBottleCleanupPlan:
|
||||||
|
|||||||
@@ -1,79 +0,0 @@
|
|||||||
"""DockerBottleProvisioner — copies prompt, skills, SSH keys, and
|
|
||||||
.git into a running Docker container.
|
|
||||||
|
|
||||||
Called by DockerBottleBackend.launch after the agent container is up
|
|
||||||
but before the DockerBottle handle is yielded. The returned in-
|
|
||||||
container prompt path tells the handle whether to add
|
|
||||||
--append-system-prompt-file to claude's argv.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import os
|
|
||||||
import subprocess
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from ... import pipelock
|
|
||||||
from ... import skills as skills_mod
|
|
||||||
from ... import ssh as ssh_mod
|
|
||||||
from ...log import info
|
|
||||||
from .. import BottlePlan, BottleProvisioner
|
|
||||||
from .bottle_plan import DockerBottlePlan
|
|
||||||
|
|
||||||
|
|
||||||
class DockerBottleProvisioner(BottleProvisioner):
|
|
||||||
"""Docker implementation of BottleProvisioner."""
|
|
||||||
|
|
||||||
def provision(self, plan: BottlePlan, target: str) -> str | None:
|
|
||||||
assert isinstance(plan, DockerBottlePlan), (
|
|
||||||
f"DockerBottleProvisioner.provision expects DockerBottlePlan, "
|
|
||||||
f"got {type(plan).__name__}"
|
|
||||||
)
|
|
||||||
container = target
|
|
||||||
container_home = os.environ.get("CLAUDE_BOTTLE_CONTAINER_HOME", "/home/node")
|
|
||||||
in_container_prompt_path = f"{container_home}/.claude-bottle-prompt.txt"
|
|
||||||
|
|
||||||
subprocess.run(
|
|
||||||
["docker", "cp", str(plan.prompt_file), f"{container}:{in_container_prompt_path}"],
|
|
||||||
stdout=subprocess.DEVNULL,
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
# `docker cp` preserves host UID; re-own/mode as root so node
|
|
||||||
# can read its own mode-600 prompt regardless of host UID.
|
|
||||||
subprocess.run(
|
|
||||||
["docker", "exec", "-u", "0", container, "chown", "node:node", in_container_prompt_path],
|
|
||||||
stdout=subprocess.DEVNULL,
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
subprocess.run(
|
|
||||||
["docker", "exec", "-u", "0", container, "chmod", "600", in_container_prompt_path],
|
|
||||||
stdout=subprocess.DEVNULL,
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
agent = plan.spec.manifest.agents[plan.spec.agent_name]
|
|
||||||
if agent.skills:
|
|
||||||
skills_mod.skills_copy_into(container, list(agent.skills))
|
|
||||||
|
|
||||||
bottle = plan.spec.manifest.bottle_for(plan.spec.agent_name)
|
|
||||||
if bottle.ssh:
|
|
||||||
proxy_host_port = pipelock.pipelock_proxy_host_port(plan.slug)
|
|
||||||
ssh_mod.ssh_setup(container, plan.stage_dir, proxy_host_port, bottle.ssh)
|
|
||||||
|
|
||||||
if plan.spec.copy_cwd and Path(plan.spec.user_cwd, ".git").is_dir():
|
|
||||||
info(f"copying {plan.spec.user_cwd}/.git -> {container}:/home/node/workspace/.git")
|
|
||||||
subprocess.run(
|
|
||||||
["docker", "cp", f"{plan.spec.user_cwd}/.git", f"{container}:/home/node/workspace/.git"],
|
|
||||||
stdout=subprocess.DEVNULL,
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
subprocess.run(
|
|
||||||
[
|
|
||||||
"docker", "exec", "-u", "0", container,
|
|
||||||
"chown", "-R", "node:node", "/home/node/workspace/.git",
|
|
||||||
],
|
|
||||||
stdout=subprocess.DEVNULL,
|
|
||||||
check=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
return in_container_prompt_path if agent.prompt else None
|
|
||||||
Reference in New Issue
Block a user