feat(cli): add commit command to snapshot running bottle state
Adds `./cli.py commit [<slug>]` which runs `docker commit` on the active agent container and stores the resulting image tag in per-bottle state. The next `./cli.py resume <slug>` automatically boots from the committed snapshot instead of rebuilding from the Dockerfile, preserving all in-container state across restarts and migrations. - bottle_state: add write_committed_image / read_committed_image helpers - docker/util: add commit_container wrapper around `docker commit` - docker/launch: check for a committed image before the Dockerfile build step; fall back to normal build if the image is absent from the daemon - cli/commit: new command with interactive slug picker; errors clearly on non-Docker backends - 50 new unit tests covering all paths Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -277,5 +277,56 @@ class TestBottleMetadataBackend(_FakeHomeMixin, unittest.TestCase):
|
||||
self.assertEqual("", loaded.backend)
|
||||
|
||||
|
||||
class TestCommittedImage(_FakeHomeMixin, unittest.TestCase):
|
||||
"""write_committed_image / read_committed_image round-trip."""
|
||||
|
||||
def setUp(self):
|
||||
self._setup_fake_home()
|
||||
|
||||
def tearDown(self):
|
||||
self._teardown_fake_home()
|
||||
|
||||
def test_returns_none_when_absent(self):
|
||||
self.assertIsNone(bottle_state.read_committed_image("dev"))
|
||||
|
||||
def test_write_then_read_roundtrip(self):
|
||||
bottle_state.write_committed_image("dev", "bot-bottle-committed-dev:latest")
|
||||
self.assertEqual(
|
||||
"bot-bottle-committed-dev:latest",
|
||||
bottle_state.read_committed_image("dev"),
|
||||
)
|
||||
|
||||
def test_strips_trailing_newline_on_read(self):
|
||||
path = bottle_state.committed_image_path("dev")
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text("bot-bottle-committed-dev:latest\n\n")
|
||||
self.assertEqual(
|
||||
"bot-bottle-committed-dev:latest",
|
||||
bottle_state.read_committed_image("dev"),
|
||||
)
|
||||
|
||||
def test_isolated_per_slug(self):
|
||||
bottle_state.write_committed_image("dev", "bot-bottle-committed-dev:latest")
|
||||
bottle_state.write_committed_image("api", "bot-bottle-committed-api:latest")
|
||||
self.assertEqual(
|
||||
"bot-bottle-committed-dev:latest",
|
||||
bottle_state.read_committed_image("dev"),
|
||||
)
|
||||
self.assertEqual(
|
||||
"bot-bottle-committed-api:latest",
|
||||
bottle_state.read_committed_image("api"),
|
||||
)
|
||||
|
||||
def test_path_under_state_dir(self):
|
||||
path = bottle_state.committed_image_path("dev")
|
||||
self.assertTrue(str(path).endswith("/.bot-bottle/state/dev/committed-image"))
|
||||
|
||||
def test_empty_content_returns_none(self):
|
||||
path = bottle_state.committed_image_path("dev")
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(" \n")
|
||||
self.assertIsNone(bottle_state.read_committed_image("dev"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
"""Unit: cli.py commit command."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
from bot_bottle.cli.commit import cmd_commit, _committed_image_tag, _agent_container_name
|
||||
from bot_bottle import supervise
|
||||
from bot_bottle import bottle_state
|
||||
|
||||
|
||||
class _FakeHomeMixin:
|
||||
def _setup_fake_home(self):
|
||||
self._tmp = tempfile.TemporaryDirectory(prefix="cli-commit-test.")
|
||||
original = supervise.bot_bottle_root
|
||||
|
||||
def fake_root() -> Path:
|
||||
return Path(self._tmp.name) / ".bot-bottle"
|
||||
|
||||
supervise.bot_bottle_root = fake_root # type: ignore[assignment]
|
||||
self._restore = lambda: setattr(supervise, "bot_bottle_root", original)
|
||||
|
||||
def _teardown_fake_home(self):
|
||||
self._restore()
|
||||
self._tmp.cleanup()
|
||||
|
||||
|
||||
class TestCommitHelpers(unittest.TestCase):
|
||||
def test_committed_image_tag(self):
|
||||
self.assertEqual(
|
||||
"bot-bottle-committed-dev-abc12:latest",
|
||||
_committed_image_tag("dev-abc12"),
|
||||
)
|
||||
|
||||
def test_agent_container_name(self):
|
||||
self.assertEqual(
|
||||
"bot-bottle-dev-abc12",
|
||||
_agent_container_name("dev-abc12"),
|
||||
)
|
||||
|
||||
|
||||
class TestCmdCommitSlugArg(_FakeHomeMixin, unittest.TestCase):
|
||||
"""cmd_commit with an explicit slug bypasses the TUI picker."""
|
||||
|
||||
def setUp(self):
|
||||
self._setup_fake_home()
|
||||
|
||||
def tearDown(self):
|
||||
self._teardown_fake_home()
|
||||
|
||||
def test_commits_docker_bottle(self):
|
||||
slug = "dev-abc12"
|
||||
# Write metadata saying this is a docker bottle.
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="docker",
|
||||
))
|
||||
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.commit_container",
|
||||
) as mock_commit, patch(
|
||||
"bot_bottle.cli.commit.info",
|
||||
):
|
||||
rc = cmd_commit([slug])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
mock_commit.assert_called_once_with(
|
||||
f"bot-bottle-{slug}",
|
||||
f"bot-bottle-committed-{slug}:latest",
|
||||
)
|
||||
|
||||
def test_writes_committed_image_to_state(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="docker",
|
||||
))
|
||||
|
||||
with patch("bot_bottle.cli.commit.commit_container"), \
|
||||
patch("bot_bottle.cli.commit.info"):
|
||||
cmd_commit([slug])
|
||||
|
||||
self.assertEqual(
|
||||
f"bot-bottle-committed-{slug}:latest",
|
||||
bottle_state.read_committed_image(slug),
|
||||
)
|
||||
|
||||
def test_marks_bottle_preserved(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="docker",
|
||||
))
|
||||
|
||||
with patch("bot_bottle.cli.commit.commit_container"), \
|
||||
patch("bot_bottle.cli.commit.info"):
|
||||
cmd_commit([slug])
|
||||
|
||||
self.assertTrue(bottle_state.is_preserved(slug))
|
||||
|
||||
def test_empty_backend_treated_as_docker(self):
|
||||
"""Old state dirs without a backend field should be treated as docker."""
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="",
|
||||
))
|
||||
|
||||
with patch("bot_bottle.cli.commit.commit_container") as mock_commit, \
|
||||
patch("bot_bottle.cli.commit.info"):
|
||||
rc = cmd_commit([slug])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
mock_commit.assert_called_once()
|
||||
|
||||
|
||||
class TestCmdCommitNonDockerBackend(_FakeHomeMixin, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._setup_fake_home()
|
||||
|
||||
def tearDown(self):
|
||||
self._teardown_fake_home()
|
||||
|
||||
def test_dies_for_smolmachines_backend(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="smolmachines",
|
||||
))
|
||||
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.die", side_effect=SystemExit("die"),
|
||||
) as mock_die:
|
||||
with self.assertRaises(SystemExit):
|
||||
cmd_commit([slug])
|
||||
|
||||
mock_die.assert_called_once()
|
||||
self.assertIn("smolmachines", mock_die.call_args.args[0])
|
||||
|
||||
def test_dies_for_macos_container_backend(self):
|
||||
slug = "dev-abc12"
|
||||
bottle_state.write_metadata(bottle_state.BottleMetadata(
|
||||
identity=slug, agent_name="dev", cwd="", copy_cwd=False,
|
||||
started_at="t", backend="macos-container",
|
||||
))
|
||||
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.die", side_effect=SystemExit("die"),
|
||||
) as mock_die:
|
||||
with self.assertRaises(SystemExit):
|
||||
cmd_commit([slug])
|
||||
|
||||
mock_die.assert_called_once()
|
||||
self.assertIn("macos-container", mock_die.call_args.args[0])
|
||||
|
||||
|
||||
class TestCmdCommitNoActiveBottles(_FakeHomeMixin, unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._setup_fake_home()
|
||||
|
||||
def tearDown(self):
|
||||
self._teardown_fake_home()
|
||||
|
||||
def test_dies_when_no_active_bottles_and_no_slug(self):
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.enumerate_active_agents", return_value=[],
|
||||
), patch(
|
||||
"bot_bottle.cli.commit.die", side_effect=SystemExit("die"),
|
||||
) as mock_die:
|
||||
with self.assertRaises(SystemExit):
|
||||
cmd_commit([])
|
||||
|
||||
mock_die.assert_called_once()
|
||||
|
||||
def test_returns_zero_when_picker_cancelled(self):
|
||||
active = MagicMock()
|
||||
active.slug = "dev-abc12"
|
||||
with patch(
|
||||
"bot_bottle.cli.commit.enumerate_active_agents", return_value=[active],
|
||||
), patch(
|
||||
"bot_bottle.cli.commit.tui.filter_select", return_value=None,
|
||||
):
|
||||
rc = cmd_commit([])
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,200 @@
|
||||
"""Unit: Docker launch step uses committed image when available."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from bot_bottle.agent_provider import AgentProvisionPlan
|
||||
from bot_bottle.backend import BottleSpec
|
||||
from bot_bottle.backend.docker import launch as launch_mod
|
||||
from bot_bottle.backend.docker.bottle_plan import DockerBottlePlan
|
||||
from bot_bottle.egress import EgressPlan
|
||||
from bot_bottle.git_gate import GitGatePlan
|
||||
from bot_bottle.manifest import Manifest
|
||||
|
||||
|
||||
_SLUG = "dev-abc12"
|
||||
_COMMITTED_TAG = f"bot-bottle-committed-{_SLUG}:latest"
|
||||
_DEFAULT_IMAGE = "bot-bottle-claude:latest"
|
||||
|
||||
|
||||
def _manifest() -> Manifest:
|
||||
return Manifest.from_json_obj({
|
||||
"bottles": {"dev": {}},
|
||||
"agents": {"demo": {"skills": [], "prompt": "", "bottle": "dev"}},
|
||||
})
|
||||
|
||||
|
||||
def _plan(tmp: str) -> DockerBottlePlan:
|
||||
stage = Path(tmp)
|
||||
spec = BottleSpec(
|
||||
manifest=_manifest(),
|
||||
agent_name="demo",
|
||||
copy_cwd=False,
|
||||
user_cwd=tmp,
|
||||
identity=_SLUG,
|
||||
)
|
||||
return DockerBottlePlan(
|
||||
spec=spec,
|
||||
stage_dir=stage,
|
||||
git_gate_plan=GitGatePlan(
|
||||
slug=_SLUG,
|
||||
entrypoint_script=stage / "entrypoint.sh",
|
||||
hook_script=stage / "hook.sh",
|
||||
access_hook_script=stage / "access-hook.sh",
|
||||
upstreams=(),
|
||||
),
|
||||
egress_plan=EgressPlan(
|
||||
slug=_SLUG,
|
||||
routes_path=stage / "egress.yaml",
|
||||
routes=(),
|
||||
token_env_map={},
|
||||
),
|
||||
supervise_plan=None,
|
||||
agent_provision=AgentProvisionPlan(
|
||||
template="claude",
|
||||
command="claude",
|
||||
prompt_mode="append_file",
|
||||
image=_DEFAULT_IMAGE,
|
||||
dockerfile="",
|
||||
guest_home="/home/node",
|
||||
instance_name=f"bot-bottle-{_SLUG}",
|
||||
prompt_file=stage / "prompt.txt",
|
||||
guest_env={},
|
||||
),
|
||||
slug=_SLUG,
|
||||
forwarded_env={},
|
||||
use_runsc=False,
|
||||
)
|
||||
|
||||
|
||||
def _std_mocks(test, plan):
|
||||
"""Context manager providing the standard launch-step mocks needed to
|
||||
get through the non-image parts of `launch()` without real Docker."""
|
||||
return mock.patch.multiple(
|
||||
launch_mod,
|
||||
egress_tls_init=mock.DEFAULT,
|
||||
network_mod=mock.DEFAULT,
|
||||
bottle_plan_to_compose=mock.DEFAULT,
|
||||
write_compose_file=mock.DEFAULT,
|
||||
compose_up=mock.DEFAULT,
|
||||
compose_dump_logs=mock.DEFAULT,
|
||||
compose_down=mock.DEFAULT,
|
||||
)
|
||||
|
||||
|
||||
class TestLaunchCommittedImage(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._tmp = tempfile.mkdtemp(prefix="launch-committed-test.")
|
||||
|
||||
def tearDown(self):
|
||||
import shutil
|
||||
shutil.rmtree(self._tmp, ignore_errors=True)
|
||||
|
||||
def _run_launch(self, plan, *, committed_tag=None, image_present=True):
|
||||
"""Drive launch() through its full sequence with the committed-image
|
||||
behaviour controlled by the arguments. Returns the images that were
|
||||
passed to `build_image` (empty list if it was never called)."""
|
||||
built = []
|
||||
|
||||
def fake_build(image, ctx, *, dockerfile=""):
|
||||
built.append(image)
|
||||
|
||||
with mock.patch.object(
|
||||
launch_mod, "read_committed_image", return_value=committed_tag,
|
||||
), mock.patch.object(
|
||||
launch_mod.docker_mod, "image_exists", return_value=image_present,
|
||||
), mock.patch.object(
|
||||
launch_mod.docker_mod, "build_image", side_effect=fake_build,
|
||||
), mock.patch.object(
|
||||
launch_mod, "egress_tls_init",
|
||||
return_value=(Path("/egress_ca"), Path("/egress_cert")),
|
||||
), mock.patch.object(
|
||||
launch_mod.network_mod, "network_name_for_slug",
|
||||
return_value="bb-internal",
|
||||
), mock.patch.object(
|
||||
launch_mod.network_mod, "network_egress_name_for_slug",
|
||||
return_value="bb-egress",
|
||||
), mock.patch.object(
|
||||
launch_mod, "bottle_plan_to_compose",
|
||||
return_value={"services": {"agent": {}}},
|
||||
), mock.patch.object(
|
||||
launch_mod, "write_compose_file",
|
||||
return_value=Path("/tmp/compose.yml"),
|
||||
), mock.patch.object(launch_mod, "compose_up"), \
|
||||
mock.patch.object(launch_mod, "compose_dump_logs"), \
|
||||
mock.patch.object(launch_mod, "compose_down"), \
|
||||
contextlib.redirect_stderr(io.StringIO()):
|
||||
provision = mock.Mock(return_value=None)
|
||||
with launch_mod.launch(plan, provision=provision):
|
||||
pass
|
||||
|
||||
return built
|
||||
|
||||
def test_skips_build_when_committed_image_present(self):
|
||||
plan = _plan(self._tmp)
|
||||
built = self._run_launch(plan, committed_tag=_COMMITTED_TAG, image_present=True)
|
||||
self.assertEqual([], built, "build_image should not be called when committed image exists")
|
||||
|
||||
def test_uses_committed_image_in_compose_spec(self):
|
||||
"""The compose spec renderer receives the committed image tag via
|
||||
plan.image — captured here by checking what bottle_plan_to_compose
|
||||
was called with."""
|
||||
plan = _plan(self._tmp)
|
||||
captured_plans = []
|
||||
|
||||
def fake_compose(p):
|
||||
captured_plans.append(p)
|
||||
return {"services": {"agent": {}}}
|
||||
|
||||
with mock.patch.object(
|
||||
launch_mod, "read_committed_image", return_value=_COMMITTED_TAG,
|
||||
), mock.patch.object(
|
||||
launch_mod.docker_mod, "image_exists", return_value=True,
|
||||
), mock.patch.object(
|
||||
launch_mod.docker_mod, "build_image",
|
||||
), mock.patch.object(
|
||||
launch_mod, "egress_tls_init",
|
||||
return_value=(Path("/egress_ca"), Path("/egress_cert")),
|
||||
), mock.patch.object(
|
||||
launch_mod.network_mod, "network_name_for_slug",
|
||||
return_value="bb-internal",
|
||||
), mock.patch.object(
|
||||
launch_mod.network_mod, "network_egress_name_for_slug",
|
||||
return_value="bb-egress",
|
||||
), mock.patch.object(
|
||||
launch_mod, "bottle_plan_to_compose", side_effect=fake_compose,
|
||||
), mock.patch.object(
|
||||
launch_mod, "write_compose_file",
|
||||
return_value=Path("/tmp/compose.yml"),
|
||||
), mock.patch.object(launch_mod, "compose_up"), \
|
||||
mock.patch.object(launch_mod, "compose_dump_logs"), \
|
||||
mock.patch.object(launch_mod, "compose_down"), \
|
||||
contextlib.redirect_stderr(io.StringIO()):
|
||||
provision = mock.Mock(return_value=None)
|
||||
with launch_mod.launch(plan, provision=provision):
|
||||
pass
|
||||
|
||||
self.assertEqual(1, len(captured_plans))
|
||||
self.assertEqual(_COMMITTED_TAG, captured_plans[0].image)
|
||||
|
||||
def test_falls_back_to_build_when_no_committed_image(self):
|
||||
plan = _plan(self._tmp)
|
||||
built = self._run_launch(plan, committed_tag=None)
|
||||
self.assertEqual([_DEFAULT_IMAGE], built)
|
||||
|
||||
def test_falls_back_to_build_when_committed_image_missing_from_daemon(self):
|
||||
plan = _plan(self._tmp)
|
||||
built = self._run_launch(
|
||||
plan, committed_tag=_COMMITTED_TAG, image_present=False,
|
||||
)
|
||||
self.assertEqual([_DEFAULT_IMAGE], built)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -67,5 +67,46 @@ class TestSave(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
class TestCommitContainer(unittest.TestCase):
|
||||
def test_runs_docker_commit(self):
|
||||
with patch.object(
|
||||
docker_mod.subprocess, "run", return_value=_ok(),
|
||||
) as run, patch.object(docker_mod, "info"):
|
||||
docker_mod.commit_container(
|
||||
"bot-bottle-dev-abc12",
|
||||
"bot-bottle-committed-dev-abc12:latest",
|
||||
)
|
||||
argv = run.call_args.args[0]
|
||||
self.assertEqual(
|
||||
[
|
||||
"docker", "commit",
|
||||
"bot-bottle-dev-abc12",
|
||||
"bot-bottle-committed-dev-abc12:latest",
|
||||
],
|
||||
argv,
|
||||
)
|
||||
|
||||
def test_dies_on_docker_commit_failure(self):
|
||||
with patch.object(
|
||||
docker_mod.subprocess, "run", return_value=_fail("No such container"),
|
||||
), patch.object(
|
||||
docker_mod, "die", side_effect=SystemExit("die"),
|
||||
) as die:
|
||||
with self.assertRaises(SystemExit):
|
||||
docker_mod.commit_container("missing-container", "some:tag")
|
||||
die.assert_called_once()
|
||||
self.assertIn("missing-container", die.call_args.args[0])
|
||||
|
||||
def test_die_message_includes_image_tag(self):
|
||||
with patch.object(
|
||||
docker_mod.subprocess, "run", return_value=_fail("boom"),
|
||||
), patch.object(
|
||||
docker_mod, "die", side_effect=SystemExit("die"),
|
||||
) as die:
|
||||
with self.assertRaises(SystemExit):
|
||||
docker_mod.commit_container("ctr", "my-tag:v1")
|
||||
self.assertIn("my-tag:v1", die.call_args.args[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user