refactor(supervise): make Supervisor a service class the orchestrator calls
tracker-policy-pr / check-pr (pull_request) Successful in 12s
test / integration-docker (pull_request) Successful in 19s
lint / lint (push) Failing after 55s
test / unit (pull_request) Successful in 2m8s
test / integration-firecracker (pull_request) Successful in 3m25s
test / coverage (pull_request) Successful in 39s
test / publish-infra (pull_request) Has been skipped

Turn the loose queue/audit functions in orchestrator/supervisor/queue.py into
methods on a concrete Supervisor service. The Orchestrator now owns an
injectable `self._supervisor` and calls `self._supervisor.write_proposal(...)`
etc., instead of module-level free functions — the supervise dependency is
explicit and mockable, and a Supervisor can be scoped to a `db_path` (defaults
to the host DB) so tests can point it at a temp database.

  - Supervisor (in the package __init__) drops the ABC and gains write_proposal,
    read_proposal, list_pending_proposals, list_all_pending_proposals,
    write_response, read_response, archive_all_proposals, write_audit_entry,
    read_audit_entries, plus the launch-time prepare().
  - render_diff / sha256_hex are pure and stateless, so they move to
    orchestrator/supervisor/util.py (re-exported from the facade) rather than
    becoming methods.
  - queue.py is deleted; service.py + the supervise tests call through a
    Supervisor instance.

Full unit suite green (2243).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-24 14:52:17 -04:00
parent 27a122e24b
commit 8abccf7ffe
9 changed files with 183 additions and 210 deletions
+72 -33
View File
@@ -1,52 +1,91 @@
"""The orchestrator's view of the supervise plane.
"""The orchestrator's supervise service.
Bundles the orchestrator-owned supervise surface: the queue/audit I/O and diff
rendering (`queue`), and the `Supervisor` lifecycle (host-side database
staging). For convenience it re-exports the neutral vocabulary
(`bot_bottle.supervisor`'s types + `SupervisePlan`) so orchestrator-side
callers — service, CLI, tests — import from one place.
`Supervisor` is the service object the `Orchestrator` owns and calls: it wraps
the supervise queue + audit stores (queue proposals, record operator responses,
write audit entries, stage the DB at launch). Binding those operations to one
object — optionally scoped to a `db_path` — makes the orchestrator's supervise
dependency explicit and injectable in tests.
The data plane must NOT import this package; it imports `bot_bottle.supervisor`
(neutral) directly and reaches the queue over the control-plane RPC.
For convenience this package also re-exports the neutral vocabulary
(`bot_bottle.supervisor`'s types + `SupervisePlan`) and the pure `render_diff`
/ `sha256_hex` helpers (`util`), so orchestrator-side callers import from one
place. The data plane must NOT import this package — it imports
`bot_bottle.supervisor` (neutral) directly and reaches the queue over the
control-plane RPC.
"""
from __future__ import annotations
from abc import ABC
from pathlib import Path
from ...supervisor.types import * # noqa: F401,F403 — re-export the neutral vocabulary
from ...supervisor.plan import SupervisePlan
from ..store.store_manager import StoreManager
from .queue import (
archive_all_proposals,
list_all_pending_proposals,
list_pending_proposals,
read_audit_entries,
read_proposal,
read_response,
render_diff,
sha256_hex,
write_audit_entry,
write_proposal,
write_response,
)
from ..store.queue_store import QueueStore
from ...store.audit_store import AuditStore
from ...supervisor.types import AuditEntry, Proposal, Response
from .util import render_diff, sha256_hex # noqa: F401 — re-exported for callers
class Supervisor(ABC):
"""Per-bottle supervise lifecycle. Encapsulates host-side database staging;
the gateway's start/stop lifecycle is backend-specific.
class Supervisor:
"""Host-side supervise service: the queue + audit I/O and launch-time DB
staging the orchestrator drives. Stateless apart from an optional `db_path`
(None → the host DB, resolved per store as before), so a test can point a
`Supervisor` at a temp database."""
`prepare` migrates the orchestrator's `bot-bottle.db` and returns the
neutral `SupervisePlan`. It touches the orchestrator store manager, so it
lives here rather than in the neutral `bot_bottle.supervisor` package; the
backend (which drives launch) may import it — backend → orchestrator is an
allowed direction, unlike gateway → orchestrator."""
def __init__(self, db_path: Path | None = None) -> None:
self._db_path = db_path
def _queue(self, queue_key: str) -> QueueStore:
return QueueStore(queue_key, self._db_path)
def _audit(self) -> AuditStore:
return AuditStore(self._db_path)
# --- Queue I/O ---------------------------------------------------------
def write_proposal(self, proposal: Proposal) -> Path:
"""Persist `proposal` in the queue database, mode 0o600."""
return self._queue(proposal.bottle_slug).write_proposal(proposal)
def read_proposal(self, bottle_slug: str, proposal_id: str) -> Proposal:
return self._queue(bottle_slug).read_proposal(proposal_id)
def list_pending_proposals(self, bottle_slug: str) -> list[Proposal]:
"""A single bottle's undecided proposals, FIFO by arrival."""
return self._queue(bottle_slug).list_pending_proposals()
def list_all_pending_proposals(self) -> list[Proposal]:
"""All pending proposals across bottles, sorted FIFO."""
return self._queue("").list_all_pending_proposals()
def write_response(self, bottle_slug: str, response: Response) -> Path:
return self._queue(bottle_slug).write_response(response)
def read_response(self, bottle_slug: str, proposal_id: str) -> Response:
return self._queue(bottle_slug).read_response(proposal_id)
def archive_all_proposals(self, bottle_slug: str) -> None:
"""Archive every proposal + response for `bottle_slug` (bottle teardown
/ reconcile cleanup). Idempotent."""
self._queue(bottle_slug).archive_all()
# --- Audit log ---------------------------------------------------------
def write_audit_entry(self, entry: AuditEntry) -> Path:
"""Append `entry` to the host supervise audit table."""
return self._audit().write_audit_entry(entry)
def read_audit_entries(self, component: str, slug: str) -> list[AuditEntry]:
"""Load all audit entries for the given component+slug."""
return self._audit().read_audit_entries(component, slug)
# --- Launch-time staging ----------------------------------------------
def prepare(self, slug: str, stage_dir: Path) -> SupervisePlan:
"""Stage the host database. Returns the plan; `internal_network` must be
set by the launch step before .start runs."""
"""Stage the host database and return the plan. Called by the backend at
launch; `internal_network` is set by the launch step before `.start`."""
del stage_dir
mgr = StoreManager.instance()
mgr = StoreManager(self._db_path) if self._db_path else StoreManager.instance()
mgr.migrate()
return SupervisePlan(slug=slug, db_path=mgr.db_path)
@@ -1,93 +0,0 @@
"""Orchestrator-side supervise queue + audit I/O and diff rendering.
The host-side library the orchestrator uses to drive the supervise flow: read /
write proposals and responses against the queue store, append audit entries,
and render operator diffs. Only the orchestrator opens `bot-bottle.db` (PRD
0070), so this lives under `orchestrator/` — the data plane reaches it over the
control-plane RPC, never by importing this module.
"""
from __future__ import annotations
import difflib
import hashlib
from pathlib import Path
from ...supervisor.types import AuditEntry, Proposal, Response
from ..store.queue_store import QueueStore
from ...store.audit_store import AuditStore
# --- Queue I/O -------------------------------------------------------------
def write_proposal(proposal: Proposal) -> Path:
"""Persist `proposal` in the queue database, mode 0o600.
Directory is created if missing."""
return QueueStore(proposal.bottle_slug).write_proposal(proposal)
def read_proposal(bottle_slug: str, proposal_id: str) -> Proposal:
return QueueStore(bottle_slug).read_proposal(proposal_id)
def list_pending_proposals(bottle_slug: str) -> list[Proposal]:
"""All proposals for `bottle_slug` that do not yet have a matching
response. Sorted by `arrival_timestamp` so the operator sees the queue
FIFO."""
return QueueStore(bottle_slug).list_pending_proposals()
def list_all_pending_proposals() -> list[Proposal]:
"""All pending proposals across bottles, sorted FIFO."""
return QueueStore("").list_all_pending_proposals()
def write_response(bottle_slug: str, response: Response) -> Path:
return QueueStore(bottle_slug).write_response(response)
def read_response(bottle_slug: str, proposal_id: str) -> Response:
return QueueStore(bottle_slug).read_response(proposal_id)
def archive_all_proposals(bottle_slug: str) -> None:
"""Archive every proposal + response for `bottle_slug` (bottle teardown /
reconcile cleanup). Idempotent."""
QueueStore(bottle_slug).archive_all()
# --- Audit log -------------------------------------------------------------
def write_audit_entry(entry: AuditEntry) -> Path:
"""Append `entry` to the host supervise audit table."""
return AuditStore().write_audit_entry(entry)
def read_audit_entries(component: str, slug: str) -> list[AuditEntry]:
"""Load all audit entries for the given component+slug."""
return AuditStore().read_audit_entries(component, slug)
# --- Diff rendering --------------------------------------------------------
def render_diff(before: str, after: str, *, label: str = "config") -> str:
"""Unified diff suitable for the audit log + TUI. Empty diff (no changes)
renders as the empty string."""
diff = difflib.unified_diff(
before.splitlines(keepends=True),
after.splitlines(keepends=True),
fromfile=f"{label} (current)",
tofile=f"{label} (proposed)",
lineterm="",
)
parts = list(diff)
if not parts:
return ""
return "".join(p if p.endswith("\n") else p + "\n" for p in parts).rstrip("\n")
def sha256_hex(content: str) -> str:
return hashlib.sha256(content.encode("utf-8")).hexdigest()
@@ -0,0 +1,31 @@
"""Pure supervise helpers — diff rendering and content hashing.
Stateless utilities used alongside the `Supervisor` service. Kept as plain
functions (not methods) because they carry no state and are also handy to
callers that only need to hash or diff.
"""
from __future__ import annotations
import difflib
import hashlib
def render_diff(before: str, after: str, *, label: str = "config") -> str:
"""Unified diff suitable for the audit log + TUI. Empty diff (no changes)
renders as the empty string."""
diff = difflib.unified_diff(
before.splitlines(keepends=True),
after.splitlines(keepends=True),
fromfile=f"{label} (current)",
tofile=f"{label} (proposed)",
lineterm="",
)
parts = list(diff)
if not parts:
return ""
return "".join(p if p.endswith("\n") else p + "\n" for p in parts).rstrip("\n")
def sha256_hex(content: str) -> str:
return hashlib.sha256(content.encode("utf-8")).hexdigest()