ModelNudgePolicy(backend_factory, instrument_name, timeout_seconds=30.0, fallback_message=DEFAULT_NUDGE_MESSAGE)
Ask a one-off headless instrument what to type into an idle session.
The caller supplies the factory so service calls do not consume a sheet
dispatch slot. Failure leaves the existing static continuation intact.
Source code in src/marianne/execution/instruments/interactive/model_nudge.py
| def __init__(
self,
backend_factory: BackendFactory,
instrument_name: str,
timeout_seconds: float = 30.0,
fallback_message: str = DEFAULT_NUDGE_MESSAGE,
) -> None:
if not instrument_name.strip():
raise ValueError("nudge instrument must be non-empty")
if not math.isfinite(timeout_seconds) or timeout_seconds <= 0:
raise ValueError("nudge timeout must be finite and > 0")
self._backend_factory = backend_factory
self._instrument_name = instrument_name
self._timeout_seconds = timeout_seconds
self._fallback = StaticNudgePolicy(fallback_message)
|
Methods:
next_nudge
async
Produce bounded input; propagate cancellation after backend cleanup.
Source code in src/marianne/execution/instruments/interactive/model_nudge.py
| async def next_nudge(self, context: ContinuationContext) -> NudgeDecision:
"""Produce bounded input; propagate cancellation after backend cleanup."""
backend: Backend | None = None
try:
prompt = self._build_prompt(context)
async with asyncio.timeout(self._timeout_seconds):
backend = self._backend_factory(self._instrument_name)
result = await backend.execute(prompt)
if result.success:
decision = self._parse(result.stdout or "")
if decision is not None:
return decision
except Exception: # noqa: BLE001 — model failure must not fail the attempt
_logger.warning("interactive_model_nudge_failed_open", exc_info=True)
finally:
if backend is not None:
close = getattr(backend, "close", None)
if close is not None:
try:
async with asyncio.timeout(min(self._timeout_seconds, 1.0)):
await close()
except Exception: # noqa: BLE001 — cleanup must not mask the decision
_logger.debug("interactive_nudge_backend_close_failed", exc_info=True)
return await self._fallback.next_nudge(context)
|