Files
bot-bottle/bot_bottle/backend/macos_container/freezer.py
T
didericis-claude 8ab24726fe
lint / lint (push) Failing after 1m38s
test / unit (pull_request) Successful in 31s
test / integration (pull_request) Successful in 15s
refactor(commit): introduce Freezer class hierarchy across backends
Adds a Freezer ABC (backend/freeze.py) that encapsulates the
stop-commit-mark-preserved flow for all backends, following the same
pattern as BottleBackend. Each backend gets its own Freezer subclass:

  DockerFreezer           — docker commit
  MacosContainerFreezer   — container export + image rebuild; prompts
                            to stop if the container is running
  SmolmachinesFreezer     — smolvm pack create --from-vm

The base class owns write_committed_image, mark_preserved, and the
resume hint. Subclasses implement _freeze() and optionally override
_export_hint() for migration instructions.

Freezer.commit(agent, bottle) is the primary entry point for use
within a live launch context. Freezer.commit_slug(slug) is a
convenience wrapper for cmd_commit, which no longer branches on
backend names itself.

get_freezer(backend_name) is the factory, analogous to
get_bottle_backend(). CommitCancelled is raised by MacosContainerFreezer
when the user declines the stop prompt; cmd_commit catches it and
returns 0.
2026-06-23 07:41:48 +00:00

51 lines
1.7 KiB
Python

"""MacosContainerFreezer — snapshot a macOS container bottle.
Apple's `container export` requires the container to be stopped first.
When the container is running the freezer prompts the user to confirm
the stop before proceeding."""
from __future__ import annotations
import sys
from .. import ActiveAgent, Bottle
from ..freeze import CommitCancelled, Freezer
from .util import commit_container, container_is_running, stop_container
from ...log import info
class MacosContainerFreezer(Freezer):
"""Freezes a macOS-container bottle via `container export` + image rebuild."""
backend_name = "macos-container"
def _freeze(self, agent: ActiveAgent, bottle: Bottle) -> str:
image_tag = f"bot-bottle-committed-{agent.slug}:latest"
if container_is_running(bottle.name):
sys.stderr.write(
f"bot-bottle: bottle {agent.slug!r} is running; "
"commit will stop it. Continue? [y/N] "
)
sys.stderr.flush()
reply = _read_tty_line().strip().lower()
if reply not in ("y", "yes"):
raise CommitCancelled
stop_container(bottle.name)
commit_container(bottle.name, image_tag)
return image_tag
def _export_hint(self, slug: str, image_ref: str) -> None:
info(
f"to export for migration: "
f"container image save {image_ref} -o {slug}.tar"
)
def _read_tty_line() -> str:
"""Read one line from /dev/tty, falling back to stdin."""
try:
with open("/dev/tty", "r", encoding="utf-8") as tty:
return tty.readline().rstrip("\n")
except OSError:
return sys.stdin.readline().rstrip("\n")