deer-flow/backend/tests/test_warm_pool_lifecycle.py
Vanzeren 67c8ade375
feat(sandbox): warm pool for boxlite (#3951)
* feat: add boxlite SDK as harness dependency

* test: add fake SimpleBox fixtures for BoxLite warm pool tests

* feat: add deterministic sandbox_id and warm pool fields to BoxliteProvider

* feat: pass deterministic sandbox_id to SimpleBox name

* feat: warm pool lifecycle — park on release, reclaim on acquire

- release(): parks VMs in _warm_pool with timestamp instead of closing
- _reclaim_warm_pool(): health checks warm boxes via echo ok
- acquire(): tries warm pool reclaim before creating new boxes
- Deterministic sandbox_id ensures thread isolation

* feat(boxlite): idle reaper, replica enforcement, warm-pool shutdown/reset

- Task 6: idle reaper daemon thread destroys expired warm-pool boxes
- Task 7: replica enforcement evicts oldest warm-pool box when at capacity
- Task 8: shutdown() stops idle checker first, destroys all boxes (active+warm);
  reset() clears warm pool

Tests: 17 passed (4 new: idle reaper, replica enforcement, shutdown, reset)

* fix(boxlite): harden warm pool lifecycle races

* docs(boxlite): document warm pool configuration

* fix(sandbox): log warning when evicting oldest warm box is failed

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix(boxlite): stop provider lifecycle on reset

* fix(boxlite): make runtime an optional dependency

* test(boxlite): remove unused warm pool lookup

* docs(boxlite): clarify optional runtime support

* refactor: extract shared WarmPoolLifecycleMixin for sandbox warm-pool lifecycle

Introduce deerflow.community.warm_pool_lifecycle.WarmPoolLifecycleMixin
owning idle-checker loop, warm-pool expiry, oldest-warm eviction,
replica counting, and soft-cap logging. Move AioSandboxProvider and
BoxliteProvider onto the mixin; keep AIO active-idle cleanup local and
delegate only warm-pool expiry to the shared helper.

BoxliteProvider also gains:
- Prefixed box names (deer-flow-boxlite-*) for startup orphan reconciliation
- _reconcile_orphans() adopting surviving boxes from a prior process
- Pinned timeout forwarding: command timeout now bounds both BoxLite
  SDK exec(timeout=...) and the loop bridge .result(timeout)
- reset() reworked as lightweight registry clear (boxes -> warm pool,
  no close, no idle-reaper stop, no loop close) so reset_sandbox_provider()
  config switches are safe; shutdown() remains the teardown path

Backward-compatible: AIO DEFAULT_IDLE_TIMEOUT/DEFAULT_REPLICAS/
IDLE_CHECK_INTERVAL stay importable; Boxlite IDLE_CHECK_INTERVAL
stays monkeypatchable.

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-07-06 07:21:12 +08:00

95 lines
3.4 KiB
Python

"""Unit tests for shared warm-pool lifecycle mechanics."""
from __future__ import annotations
import threading
import time
from typing import Any
from deerflow.community.warm_pool_lifecycle import DEFAULT_IDLE_TIMEOUT, DEFAULT_REPLICAS, WarmPoolLifecycleMixin
class _Provider(WarmPoolLifecycleMixin[str]):
_idle_checker_thread_name = "test-warm-pool-reaper"
def __init__(self, *, replicas: int = DEFAULT_REPLICAS, idle_timeout: float = DEFAULT_IDLE_TIMEOUT, active_count: int = 0) -> None:
self._lock = threading.Lock()
self._warm_pool: dict[str, tuple[str, float]] = {}
self._config: dict[str, Any] = {"replicas": replicas, "idle_timeout": idle_timeout}
self._idle_checker_stop = threading.Event()
self._idle_checker_thread: threading.Thread | None = None
self.active_count = active_count
self.destroyed: list[tuple[str, str, str]] = []
def _active_count_locked(self) -> int:
return self.active_count
def _destroy_warm_entry(self, sandbox_id: str, entry: str, *, reason: str) -> None:
self.destroyed.append((sandbox_id, entry, reason))
def test_replica_count_includes_active_and_warm_entries() -> None:
provider = _Provider(replicas=2, active_count=1)
provider._warm_pool["warm-1"] = ("entry-1", time.time())
assert provider._replica_count() == (2, 2)
def test_evict_oldest_warm_removes_and_destroys_oldest_entry() -> None:
provider = _Provider()
provider._warm_pool["new"] = ("entry-new", 200.0)
provider._warm_pool["old"] = ("entry-old", 100.0)
evicted = provider._evict_oldest_warm()
assert evicted == "old"
assert "old" not in provider._warm_pool
assert "new" in provider._warm_pool
assert provider.destroyed == [("old", "entry-old", "replica_enforcement")]
def test_evict_oldest_warm_returns_none_when_pool_empty() -> None:
provider = _Provider()
assert provider._evict_oldest_warm() is None
assert provider.destroyed == []
def test_reap_expired_warm_destroys_only_expired_entries() -> None:
provider = _Provider()
now = time.time()
provider._warm_pool["expired"] = ("entry-expired", now - 100)
provider._warm_pool["fresh"] = ("entry-fresh", now)
provider._reap_expired_warm(idle_timeout=10)
assert "expired" not in provider._warm_pool
assert "fresh" in provider._warm_pool
assert provider.destroyed == [("expired", "entry-expired", "idle_timeout")]
def test_reap_expired_warm_noops_when_timeout_disabled() -> None:
provider = _Provider(idle_timeout=0)
provider._warm_pool["expired"] = ("entry-expired", time.time() - 100)
provider._reap_expired_warm(idle_timeout=0)
assert "expired" in provider._warm_pool
assert provider.destroyed == []
def test_start_idle_checker_uses_monkeypatchable_interval(monkeypatch) -> None:
provider = _Provider(idle_timeout=0.01)
monkeypatch.setattr(_Provider, "IDLE_CHECK_INTERVAL", 0.01)
provider._warm_pool["expired"] = ("entry-expired", time.time() - 10)
provider._start_idle_checker()
deadline = time.time() + 1
while "expired" in provider._warm_pool and time.time() < deadline:
time.sleep(0.01)
provider._stop_idle_checker()
assert "expired" not in provider._warm_pool
assert provider.destroyed == [("expired", "entry-expired", "idle_timeout")]
assert provider._idle_checker_thread is not None
assert not provider._idle_checker_thread.is_alive()