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 (callsbackend.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
¶
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
Methods:¶
acquire
async
¶
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
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
release
async
¶
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 |
required |
backend
|
Backend
|
The Backend instance to release. |
required |
Source code in src/marianne/daemon/baton/backend_pool.py
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
in_flight_count
¶
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
total_in_flight
¶
create_one_off
¶
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
close_all
async
¶
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
Functions:¶
create_backend_for_instrument
¶
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. |