Skip to content

backend_pool

backend_pool

Backend pool — per-instrument backend instance management.

The baton dispatches sheets to execution, each requiring a Backend instance for its assigned instrument. The BackendPool manages these instances:

  • CLI instruments get one Backend per concurrent sheet (subprocess isolation — each sheet runs in its own process).
  • HTTP instruments share a singleton Backend (httpx handles connection pooling and concurrency internally).

The pool tracks how many instances are in-flight per instrument, which the baton uses to enforce per-instrument concurrency limits.

Design decisions:

  • Lazy creation — Backend instances are created on first acquire, not upfront. This avoids spawning processes for instruments that aren't used by any sheet in the current job.
  • CLI reuse — Released CLI backends go back into a free list. The next acquire for the same instrument reuses an existing instance rather than creating a new one. This avoids repeated subprocess setup for sequential sheets on the same instrument.
  • Lock-free acquire for HTTP — HTTP singletons are created once and returned on every acquire. No release needed (the pool tracks them but doesn't recycle them).
  • Graceful close — close_all() closes every Backend instance (calls backend.close()). Called by the baton at job completion or cancellation.

See: docs/plans/2026-03-26-baton-design.md — BackendPool section.

Classes

InstrumentNotRegisteredError

Bases: ValueError

A named profile disappeared from the live registry before dispatch.

BackendPool

BackendPool(registry, pgroup=None, keyring=None)

Manages Backend instances for per-sheet execution.

The baton acquires a backend before dispatching a sheet and releases it after the sheet completes (or fails). The pool enforces per-instrument concurrency by tracking in-flight instances.

Usage::

pool = BackendPool(registry)

# Dispatch a sheet
backend = await pool.acquire("claude-code", working_directory=ws)
try:
    result = await backend.execute(prompt)
finally:
    await pool.release("claude-code", backend)

# Job done
await pool.close_all()
Source code in src/marianne/daemon/baton/backend_pool.py
def __init__(
    self,
    registry: InstrumentRegistry,
    pgroup: ProcessGroupManager | None = None,
    keyring: ApiKeyKeyring | None = None,
) -> None:
    self._registry = registry
    self._pgroup = pgroup
    self._keyring = keyring

    # CLI instruments: free list per instrument name
    self._cli_free: dict[str, list[Backend]] = {}

    # HTTP instruments: singleton per instrument name
    self._http_singletons: dict[str, Backend] = {}

    # Tracking: how many backends are currently in-flight (acquired
    # but not yet released) per instrument.
    self._in_flight: dict[str, int] = {}

    # All backends ever created (for close_all cleanup)
    self._all_backends: list[Backend] = []

    # #171 hot-reload generation. invalidate() bumps it and clears the
    # free lists; backends are tagged with the generation they were
    # built in, so an in-flight backend built before a reload is
    # dropped (not reused stale) when released.
    self._generation: int = 0

    # Protect concurrent acquire/release to avoid race conditions
    # on the free lists
    self._lock = asyncio.Lock()

    self._closed = False
Methods:
acquire async
acquire(instrument_name, *, model=None, working_directory=None, interactive=None)

Acquire a Backend instance for an instrument.

For CLI instruments: returns a free instance if available, otherwise creates a new one. For HTTP instruments: returns the shared singleton (creating it on first call).

Parameters:

Name Type Description Default
instrument_name str

Name of the instrument (from registry).

required
model str | None

Optional model override for this execution.

None
working_directory Path | None

Working directory for the backend.

None
interactive bool | None

Tri-state execution-mode request. None (default) = use the profile's default — interactive when the profile carries verified interactive support, headless otherwise. True = force interactive (error if unsupported). False = force headless. Interactive instances live in their own free list — they never cross-pollinate with headless ones.

None

Returns:

Type Description
Backend

A Backend instance ready for execution.

Raises:

Type Description
ValueError

If the instrument is not registered, or interactive was explicitly requested for an instrument without verified interactive support.

RuntimeError

If the pool has been closed.

Source code in src/marianne/daemon/baton/backend_pool.py
async def acquire(
    self,
    instrument_name: str,
    *,
    model: str | None = None,
    working_directory: Path | None = None,
    interactive: bool | None = None,
) -> Backend:
    """Acquire a Backend instance for an instrument.

    For CLI instruments: returns a free instance if available,
    otherwise creates a new one. For HTTP instruments: returns
    the shared singleton (creating it on first call).

    Args:
        instrument_name: Name of the instrument (from registry).
        model: Optional model override for this execution.
        working_directory: Working directory for the backend.
        interactive: Tri-state execution-mode request. None (default)
            = use the profile's default — interactive when the profile
            carries verified interactive support, headless otherwise.
            True = force interactive (error if unsupported). False =
            force headless. Interactive instances live in their own
            free list — they never cross-pollinate with headless ones.

    Returns:
        A Backend instance ready for execution.

    Raises:
        ValueError: If the instrument is not registered, or interactive
            was explicitly requested for an instrument without verified
            interactive support.
        RuntimeError: If the pool has been closed.
    """
    if self._closed:
        msg = "BackendPool is closed — cannot acquire new backends"
        raise RuntimeError(msg)

    profile = self._registry.get(instrument_name)
    if profile is None:
        msg = (
            f"Instrument '{instrument_name}' not found in registry. "
            f"Available: {', '.join(p.name for p in self._registry.list_all())}"
        )
        raise InstrumentNotRegisteredError(msg)

    # Resolve API key from keyring for HTTP instruments before acquiring lock.
    # Key is loaded from disk, used to configure the backend, then not stored.
    api_key: str | None = None
    if profile.kind == "http" and self._keyring is not None:
        if self._keyring.has_keys(instrument_name):
            try:
                api_key = await self._keyring.select_key(instrument_name)
            except (KeyError, FileNotFoundError, ValueError):
                _logger.warning(
                    "backend_pool.keyring_select_failed",
                    extra={"instrument": instrument_name},
                    exc_info=True,
                )

    # Resolve the tri-state: score silence → the profile's default.
    resolved_interactive = (
        self._profile_default_interactive(profile)
        if interactive is None
        else interactive
    )

    async with self._lock:
        backend = self._acquire_locked(
            profile,
            model=model,
            working_directory=working_directory,
            api_key=api_key,
            interactive=resolved_interactive,
        )

    _logger.debug(
        "backend_pool.acquired",
        extra={
            "instrument": instrument_name,
            "in_flight": self._in_flight.get(instrument_name, 0),
            "model": model,
            "interactive": resolved_interactive,
            "interactive_requested": interactive,
        },
    )
    return backend
release async
release(instrument_name, backend)

Release a Backend instance back to the pool.

For CLI instruments: the backend goes back to the free list for reuse. For HTTP instruments: no-op (the singleton stays active).

Parameters:

Name Type Description Default
instrument_name str

The instrument name used in acquire().

required
backend Backend

The Backend instance to release.

required
Source code in src/marianne/daemon/baton/backend_pool.py
async def release(
    self,
    instrument_name: str,
    backend: Backend,
) -> None:
    """Release a Backend instance back to the pool.

    For CLI instruments: the backend goes back to the free list for
    reuse. For HTTP instruments: no-op (the singleton stays active).

    Args:
        instrument_name: The instrument name used in ``acquire()``.
        backend: The Backend instance to release.
    """
    # Clear any per-sheet overrides (model, etc.) before returning
    # the backend to the free list. Without this, a model override from
    # sheet N would silently carry over to sheet N+1 that reuses the
    # same backend instance. This was F-150's secondary bug.
    # For interactive backends this also resets the per-sheet driver
    # knobs and the attempt identity, so free-list reuse is clean.
    backend.clear_overrides()

    async with self._lock:
        count = self._in_flight.get(instrument_name, 0)
        self._in_flight[instrument_name] = max(0, count - 1)

        profile = self._registry.get(instrument_name)
        stale = (
            getattr(backend, "_pool_generation", self._generation)
            != self._generation
        )
        if profile is not None and profile.kind == "cli" and not stale:
            # Return CLI backend to free list for reuse. Interactive
            # instances live under their own key — a headless acquire
            # must never receive a session-driving backend or vice versa.
            free_key = self._free_list_key(instrument_name, backend)
            if free_key not in self._cli_free:
                self._cli_free[free_key] = []
            self._cli_free[free_key].append(backend)
        # A stale (pre-reload) backend is dropped, not re-listed (#171);
        # it remains in _all_backends for close_all on shutdown.

        # HTTP singletons are never "released" — they stay active

    _logger.debug(
        "backend_pool.released",
        extra={
            "instrument": instrument_name,
            "in_flight": self._in_flight.get(instrument_name, 0),
        },
    )
invalidate
invalidate()

Drop cached backends so the next acquire rebuilds from the current registry profiles (#171 hot-reload).

Idle backends (free lists, HTTP singletons) are dropped now; in-flight backends finish on their loaded profile and are dropped on release via the generation check. Synchronous + lock-free: it only bumps a counter and clears dicts, so it is safe to call from the SIGHUP handler.

Source code in src/marianne/daemon/baton/backend_pool.py
def invalidate(self) -> None:
    """Drop cached backends so the next acquire rebuilds from the
    current registry profiles (#171 hot-reload).

    Idle backends (free lists, HTTP singletons) are dropped now;
    in-flight backends finish on their loaded profile and are dropped
    on release via the generation check. Synchronous + lock-free: it
    only bumps a counter and clears dicts, so it is safe to call from
    the SIGHUP handler.
    """
    self._generation += 1
    self._cli_free.clear()
    self._http_singletons.clear()
    _logger.info(
        "backend_pool.invalidated",
        extra={"generation": self._generation},
    )
in_flight_count
in_flight_count(instrument_name)

How many backends are currently acquired for this instrument.

Used by the baton's dispatch logic to enforce per-instrument concurrency limits.

Source code in src/marianne/daemon/baton/backend_pool.py
def in_flight_count(self, instrument_name: str) -> int:
    """How many backends are currently acquired for this instrument.

    Used by the baton's dispatch logic to enforce per-instrument
    concurrency limits.
    """
    return self._in_flight.get(instrument_name, 0)
total_in_flight
total_in_flight()

Total backends in-flight across all instruments.

Source code in src/marianne/daemon/baton/backend_pool.py
def total_in_flight(self) -> int:
    """Total backends in-flight across all instruments."""
    return sum(self._in_flight.values())
create_one_off
create_one_off(instrument_name, *, model=None, working_directory=None)

Create a one-off Backend outside the dispatch-pool lifecycle.

Public seam for daemon-side services (interactive model nudges) that need an LLM backend without acquiring a dispatch slot. Delegates to create_backend_for_instrument — the single creation path — so service backends and musician backends can never drift. The caller owns closing the returned backend.

Source code in src/marianne/daemon/baton/backend_pool.py
def create_one_off(
    self,
    instrument_name: str,
    *,
    model: str | None = None,
    working_directory: Path | None = None,
) -> Backend:
    """Create a one-off Backend outside the dispatch-pool lifecycle.

    Public seam for daemon-side services (interactive model nudges)
    that need an LLM backend without acquiring a dispatch slot.
    Delegates to ``create_backend_for_instrument`` — the single
    creation path — so service backends and musician backends can
    never drift. The caller owns closing the returned backend.
    """
    return create_backend_for_instrument(
        self._registry,
        instrument_name,
        model=model,
        working_directory=working_directory,
    )
close_all async
close_all()

Close all Backend instances and mark the pool as closed.

Called at job completion, cancellation, or conductor shutdown. After this call, acquire() raises RuntimeError.

Source code in src/marianne/daemon/baton/backend_pool.py
async def close_all(self) -> None:
    """Close all Backend instances and mark the pool as closed.

    Called at job completion, cancellation, or conductor shutdown.
    After this call, ``acquire()`` raises RuntimeError.
    """
    self._closed = True

    async with self._lock:
        for backend in self._all_backends:
            try:
                await backend.close()
            except Exception:  # noqa: BLE001 — structured logger reports backend_pool.close_failed
                _logger.warning(
                    "backend_pool.close_failed",
                    extra={"backend": backend.name},
                    exc_info=True,
                )

        self._cli_free.clear()
        self._http_singletons.clear()
        self._in_flight.clear()

    _logger.debug(
        "backend_pool.closed",
        extra={"total_backends": len(self._all_backends)},
    )

Functions:

create_backend_for_instrument

create_backend_for_instrument(registry, instrument_name, *, model=None, working_directory=None)

Create a one-off Backend for a registered instrument by name.

The single creation path for daemon services that need an LLM backend outside the sheet-dispatch pool (semantic analyzer, judgment client). Routes through the same profile dispatch as the BackendPool, so service backends and musician backends can never drift.

Service calls are always HEADLESS: their callers parse structured output from result.stdout, which an interactive session cannot provide (its stdout is a rendered screen).

Raises:

Type Description
ValueError

If the instrument is not registered.

Source code in src/marianne/daemon/baton/backend_pool.py
def create_backend_for_instrument(
    registry: InstrumentRegistry,
    instrument_name: str,
    *,
    model: str | None = None,
    working_directory: Path | None = None,
) -> Backend:
    """Create a one-off Backend for a registered instrument by name.

    The single creation path for daemon services that need an LLM backend
    outside the sheet-dispatch pool (semantic analyzer, judgment client).
    Routes through the same profile dispatch as the BackendPool, so service
    backends and musician backends can never drift.

    Service calls are always HEADLESS: their callers parse structured
    output from ``result.stdout``, which an interactive session cannot
    provide (its stdout is a rendered screen).

    Raises:
        ValueError: If the instrument is not registered.
    """
    profile = registry.get(instrument_name)
    if profile is None:
        raise ValueError(
            f"Instrument '{instrument_name}' not found in registry. "
            f"Available: {', '.join(p.name for p in registry.list_all())}"
        )
    return _create_backend_for_profile(
        profile,
        working_directory=working_directory,
        model=model,
        interactive=False,
    )