feat(cleanup): walk every backend, reap smolmachines orphans too
`./cli.py cleanup` previously called only the env-var-selected backend's `prepare_cleanup` / `cleanup` — so a leftover smolvm machine + bundle container + bundle network from a crashed smolmachines bottle would survive a default `docker`-mode cleanup indefinitely. Smolmachines now has a real `cleanup` module (alongside `enumerate.py` from issue #77) that walks: - smolvm machines named `claude-bottle-*` (via `smolvm machine ls --json`) - bundle containers `claude-bottle-sidecars-*` - bundle networks `claude-bottle-bundle-*` Cleanup runs stop+delete on the machines, force-rm on the containers, network rm on the networks. Each step is best-effort so a failed rm doesn't block the rest. `cli.py cleanup` walks every backend in `known_backend_names()` and runs each backend's `cleanup` after a single y/N prompt that shows a combined plan. State dirs (`~/.claude-bottle/state/<slug>/`) are shared layout with the docker backend, which still owns the orphan-state-dir bucket. It now consults `enumerate_active_bottles()` for the cross-backend live identity set so a running smolmachines bottle's state dir isn't reaped during a cleanup. Tests: smolmachines cleanup (prepare + cleanup ordering + failure handling); cross-backend orphan protection on the docker state-dir check; CLI cmd_cleanup walks both backends, short- circuits on all-empty, aborts on N. 617 unit tests pass. End-to-end verified on this host: $ smolvm machine ls --json | jq '.[].name' "claude-bottle-researcher-m3hxd" $ ./cli.py cleanup --- smolmachines backend --- smolvm machine: claude-bottle-researcher-m3hxd remove all of the above? [y/N] Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit was merged in pull request #79.
This commit is contained in:
@@ -44,7 +44,7 @@ class TestOrphanStateDirs(_FakeHomeMixin, unittest.TestCase):
|
||||
self._teardown_fake_home()
|
||||
|
||||
def test_no_state_root_returns_empty(self):
|
||||
self.assertEqual([], _list_orphan_state_dirs(set()))
|
||||
self.assertEqual([], _list_orphan_state_dirs(set(), set()))
|
||||
|
||||
def test_state_dir_with_no_live_no_preserve_is_orphan(self):
|
||||
# Just touch the dir; no metadata, no preserve marker — the
|
||||
@@ -52,7 +52,7 @@ class TestOrphanStateDirs(_FakeHomeMixin, unittest.TestCase):
|
||||
bottle_state.write_per_bottle_dockerfile("solo-aaa", "FROM x\n")
|
||||
self.assertEqual(
|
||||
["solo-aaa"],
|
||||
_list_orphan_state_dirs(set()),
|
||||
_list_orphan_state_dirs(set(), set()),
|
||||
)
|
||||
|
||||
def test_live_project_skips_dir(self):
|
||||
@@ -61,7 +61,7 @@ class TestOrphanStateDirs(_FakeHomeMixin, unittest.TestCase):
|
||||
bottle_state.write_per_bottle_dockerfile("live-bbb", "FROM x\n")
|
||||
self.assertEqual(
|
||||
[],
|
||||
_list_orphan_state_dirs({"claude-bottle-live-bbb"}),
|
||||
_list_orphan_state_dirs({"claude-bottle-live-bbb"}, set()),
|
||||
)
|
||||
|
||||
def test_preserve_marker_skips_dir(self):
|
||||
@@ -71,14 +71,14 @@ class TestOrphanStateDirs(_FakeHomeMixin, unittest.TestCase):
|
||||
bottle_state.mark_preserved("kept-ccc")
|
||||
self.assertEqual(
|
||||
[],
|
||||
_list_orphan_state_dirs(set()),
|
||||
_list_orphan_state_dirs(set(), set()),
|
||||
)
|
||||
|
||||
def test_preserve_overrides_no_live_project(self):
|
||||
# Even without a live project, a preserve marker keeps it.
|
||||
bottle_state.write_per_bottle_dockerfile("kept-ddd", "FROM x\n")
|
||||
bottle_state.mark_preserved("kept-ddd")
|
||||
self.assertEqual([], _list_orphan_state_dirs(set()))
|
||||
self.assertEqual([], _list_orphan_state_dirs(set(), set()))
|
||||
|
||||
def test_mixed_set_categorized_correctly(self):
|
||||
bottle_state.write_per_bottle_dockerfile("orphan-eee", "FROM x\n")
|
||||
@@ -86,7 +86,7 @@ class TestOrphanStateDirs(_FakeHomeMixin, unittest.TestCase):
|
||||
bottle_state.write_per_bottle_dockerfile("kept-ggg", "FROM z\n")
|
||||
bottle_state.mark_preserved("kept-ggg")
|
||||
|
||||
result = _list_orphan_state_dirs({"claude-bottle-live-fff"})
|
||||
result = _list_orphan_state_dirs({"claude-bottle-live-fff"}, set())
|
||||
self.assertEqual(["orphan-eee"], result)
|
||||
|
||||
def test_sorted_output(self):
|
||||
@@ -94,7 +94,31 @@ class TestOrphanStateDirs(_FakeHomeMixin, unittest.TestCase):
|
||||
bottle_state.write_per_bottle_dockerfile(name, "FROM x\n")
|
||||
self.assertEqual(
|
||||
["aaa-1", "mmm-1", "zzz-1"],
|
||||
_list_orphan_state_dirs(set()),
|
||||
_list_orphan_state_dirs(set(), set()),
|
||||
)
|
||||
|
||||
def test_protected_identity_skips_dir(self):
|
||||
# `protected_identities` carries slugs that are live in
|
||||
# any backend (smolmachines included). docker's orphan
|
||||
# detection respects them so a running smolmachines
|
||||
# bottle's state dir isn't reaped while the VM is up.
|
||||
bottle_state.write_per_bottle_dockerfile("smol-hhh", "FROM x\n")
|
||||
self.assertEqual(
|
||||
[],
|
||||
_list_orphan_state_dirs(set(), {"smol-hhh"}),
|
||||
)
|
||||
|
||||
def test_protected_overrides_no_live_project(self):
|
||||
# A smolmachines bottle has no docker compose project but
|
||||
# IS in the protected set; the absence of a project
|
||||
# shouldn't cause a reap.
|
||||
bottle_state.write_per_bottle_dockerfile("smol-iii", "FROM x\n")
|
||||
self.assertEqual(
|
||||
[],
|
||||
_list_orphan_state_dirs(
|
||||
{"claude-bottle-something-else"}, # different project up
|
||||
{"smol-iii"}, # but smol-iii is live
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user