e22a96e511
test / run tests/run_tests.py (pull_request) Successful in 18s
Mirror the BottlePlan -> DockerBottlePlan hierarchy at the platform layer. BottlePlatform is now an abstract base with abstract `prepare` and `launch` methods; DockerBottlePlatform lives alongside the rest of the Docker code in bottles/docker.py and supplies the concrete impls. The registry in bottles/__init__.py now holds an instance of each concrete platform class. Future per-platform state (region, api token, cleanup primitives) has a natural home on the subclass rather than being stitched onto a dataclass struct. No behavior change. Tests pass; dry-run output unchanged.
122 lines
3.8 KiB
Python
122 lines
3.8 KiB
Python
"""Per-platform bottle factories.
|
|
|
|
A bottle is a running, isolated environment with claude inside. Each
|
|
platform exposes two functions:
|
|
|
|
prepare(spec, stage_dir=...) -> BottlePlan
|
|
Resolves names, validates host-side prerequisites, and writes
|
|
scratch files. No remote/runtime resources are created yet.
|
|
Safe to call before the y/N preflight.
|
|
|
|
launch(plan) -> ContextManager[Bottle]
|
|
Brings up the container (or VM, or remote machine), provisions
|
|
it, yields a Bottle handle, and tears everything down on exit.
|
|
|
|
Selection is driven by CLAUDE_BOTTLE_PLATFORM (default "docker"). Per
|
|
PRD 0003 the manifest does not carry a platform field; the host
|
|
environment picks.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from abc import ABC, abstractmethod
|
|
from contextlib import AbstractContextManager
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import Protocol
|
|
|
|
from ..log import die
|
|
from ..manifest import Manifest
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BottleSpec:
|
|
"""CLI-supplied intent. Platform-agnostic — each platform's prepare
|
|
step consumes it and produces its own platform-specific plan.
|
|
Resolved values (image names, container name, scratch paths, runsc
|
|
availability) live on the plan, not the spec."""
|
|
|
|
manifest: Manifest
|
|
agent_name: str
|
|
copy_cwd: bool
|
|
user_cwd: str
|
|
forward_oauth_token: bool
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BottlePlan(ABC):
|
|
"""Base output of a platform's prepare step. Concrete subclasses
|
|
(e.g. DockerBottlePlan) add platform-specific resolved fields and
|
|
implement `print`."""
|
|
|
|
spec: BottleSpec
|
|
stage_dir: Path
|
|
|
|
@abstractmethod
|
|
def print(self, *, remote_control: bool) -> None:
|
|
"""Render the y/N preflight summary to stderr."""
|
|
|
|
|
|
class Bottle(Protocol):
|
|
"""Handle to a running bottle. Yielded by a platform's launch step.
|
|
|
|
`exec_claude` runs `claude` inside the bottle and blocks until the
|
|
session ends. `cp_in` copies a host path into the bottle. `close`
|
|
is an idempotent alias for context-manager teardown.
|
|
"""
|
|
|
|
name: str
|
|
|
|
def exec_claude(self, argv: list[str], *, tty: bool = True) -> int: ...
|
|
def cp_in(self, host_path: str, container_path: str) -> None: ...
|
|
def close(self) -> None: ...
|
|
|
|
|
|
class BottlePlatform(ABC):
|
|
"""Abstract base for selectable bottle platforms. Concrete subclasses
|
|
(e.g. DockerBottlePlatform) own their own prepare/launch impls.
|
|
Symmetric with the BottlePlan → DockerBottlePlan hierarchy."""
|
|
|
|
name: str
|
|
|
|
@abstractmethod
|
|
def prepare(self, spec: BottleSpec, *, stage_dir: Path) -> BottlePlan:
|
|
"""Resolve names, validate host-side prerequisites, write
|
|
scratch files. No remote/runtime resources created yet."""
|
|
|
|
@abstractmethod
|
|
def launch(self, plan: BottlePlan) -> AbstractContextManager[Bottle]:
|
|
"""Build/run the bottle and yield a handle; tear down on exit."""
|
|
|
|
|
|
# Import concrete platform classes AFTER the base types are defined, so
|
|
# each platform module can pull BottleSpec / BottlePlan / BottlePlatform
|
|
# via `from . import ...` without hitting a partially-initialized module.
|
|
from .docker import DockerBottlePlatform # noqa: E402
|
|
|
|
|
|
_PLATFORMS: dict[str, BottlePlatform] = {
|
|
"docker": DockerBottlePlatform(),
|
|
}
|
|
|
|
|
|
def get_bottle_platform() -> BottlePlatform:
|
|
"""Resolve the bottle platform for the active environment. Dies with
|
|
a pointer at the known platforms if CLAUDE_BOTTLE_PLATFORM names an
|
|
unimplemented one."""
|
|
name = os.environ.get("CLAUDE_BOTTLE_PLATFORM", "docker")
|
|
if name not in _PLATFORMS:
|
|
known = ", ".join(sorted(_PLATFORMS))
|
|
die(f"unknown CLAUDE_BOTTLE_PLATFORM={name!r}; known platforms: {known}")
|
|
return _PLATFORMS[name]
|
|
|
|
|
|
__all__ = [
|
|
"Bottle",
|
|
"BottlePlan",
|
|
"BottlePlatform",
|
|
"BottleSpec",
|
|
"get_bottle_platform",
|
|
]
|