Skip to content

helpers

helpers

Shared utilities for Marianne CLI commands.

This module contains helpers used across multiple CLI command modules: - Logger setup and configuration - Backend creation helpers - Config loading utilities - Console/Rich setup - State backend discovery - Common utility functions

Attributes

Classes

ErrorMessages

Constants for CLI error messages.

Centralizing error messages ensures consistent user-facing output and simplifies potential localization.

OutputLevel

Bases: str, Enum

Output verbosity level.

CliLoggingConfig dataclass

CliLoggingConfig(level='WARNING', file=None, format='console', configured=False)

Centralized CLI logging configuration state.

Replaces 4 module-level variables with a single typed dataclass. All getter/setter functions below delegate to this instance.

Functions:

live_conductor_pid

live_conductor_pid()

The PID the active conductor's PID file names, if that process is alive.

Clone-aware. This is the evidence that separates "busy" from "absent" (GH #455): a socket that does not answer while this PID is alive is a conductor blocked on disk, never one to start or restart. Reads only; never asserts or unlinks a PID file.

Source code in src/marianne/cli/helpers.py
def live_conductor_pid() -> int | None:
    """The PID the active conductor's PID file names, if that process is alive.

    Clone-aware. This is the evidence that separates "busy" from "absent"
    (GH #455): a socket that does not answer while this PID is alive is a
    conductor blocked on disk, never one to start or restart. Reads only;
    never asserts or unlinks a PID file.
    """
    from marianne.daemon.clone import get_clone_name, resolve_clone_paths
    from marianne.daemon.config import DaemonConfig
    from marianne.daemon.process import _pid_alive, _read_pid, _resolve_live_pid_file

    clone_name = get_clone_name()
    pid_file = (
        resolve_clone_paths(clone_name).pid_file if clone_name is not None
        else _resolve_live_pid_file(DaemonConfig().pid_file)
    )
    pid = _read_pid(pid_file)
    if pid is None or not _pid_alive(pid):
        return None
    return pid

conductor_socket_present

conductor_socket_present()

Whether the socket the CLI would route through exists (clone-aware).

Source code in src/marianne/cli/helpers.py
def conductor_socket_present() -> bool:
    """Whether the socket the CLI would route through exists (clone-aware)."""
    from marianne.daemon.detect import _resolve_socket_path
    from marianne.daemon.ipc.client import _socket_exists

    try:
        return _socket_exists(_resolve_socket_path(None))
    except OSError:
        return False

busy_conductor_message

busy_conductor_message(pid, detail='')

The one sentence every CLI path uses for a live conductor that did not answer.

Source code in src/marianne/cli/helpers.py
def busy_conductor_message(pid: int, detail: str = "") -> str:
    """The one sentence every CLI path uses for a live conductor that did not answer."""
    tail = f": {detail}" if detail else ""
    return (
        f"Marianne conductor busy or unresponsive (PID {pid} alive); "
        f"running state is unknown{tail}"
    )

report_daemon_route_error

report_daemon_route_error(exc, *, json_output)

Report an IPC outcome once, without changing its running-state meaning.

An absent verdict needs two proofs: the connect was refused or the socket is missing, AND no live PID is held. With a live PID the same failure is reported busy (GH #455), and no path here ever suggests mzt start, mzt restart or mzt stop --force for a conductor that may be alive.

Source code in src/marianne/cli/helpers.py
def report_daemon_route_error(exc: Exception, *, json_output: bool) -> NoReturn:
    """Report an IPC outcome once, without changing its running-state meaning.

    An absent verdict needs two proofs: the connect was refused or the socket
    is missing, AND no live PID is held. With a live PID the same failure is
    reported busy (GH #455), and no path here ever suggests ``mzt start``,
    ``mzt restart`` or ``mzt stop --force`` for a conductor that may be alive.
    """
    from marianne.cli.output import output_error
    from marianne.daemon.exceptions import (
        DaemonAccessDeniedError,
        DaemonNotRunningError,
        DaemonProtocolError,
        DaemonUnresponsiveError,
        MethodNotFoundError,
    )

    pid = None
    if isinstance(exc, DaemonUnresponsiveError):
        pid = live_conductor_pid()
    elif isinstance(exc, DaemonNotRunningError) and conductor_socket_present():
        # A missing socket proves absence on its own; a refused connect on a
        # socket that still exists does so only without a live PID.
        pid = live_conductor_pid()
    if isinstance(exc, DaemonAccessDeniedError):
        message = str(exc)
        hints = ["Check whether your current user is permitted to access the conductor socket."]
        running_state = "unknown"
    elif isinstance(exc, DaemonUnresponsiveError) and pid is not None:
        message = busy_conductor_message(pid, str(exc))
        hints = list(BUSY_HINTS)
        running_state = "unknown"
    elif isinstance(exc, DaemonUnresponsiveError):
        message = f"Marianne conductor did not respond; running state is unknown: {exc}"
        hints = ["Check the conductor logs and retry a read-only status check."]
        running_state = "unknown"
    elif isinstance(exc, DaemonProtocolError):
        message = f"Marianne conductor returned an invalid response: {exc}"
        hints = ["Check the conductor logs and CLI protocol version."]
        running_state = "unknown"
    elif isinstance(exc, DaemonNotRunningError) and pid is not None:
        # Refused or vanished socket, but the PID file names a live process:
        # that is a conductor between bind and listen, or one blocked before
        # it could answer. Absence is not proven.
        message = busy_conductor_message(pid, "the socket did not accept a connection")
        hints = list(BUSY_HINTS)
        running_state = "unknown"
    elif isinstance(exc, DaemonNotRunningError):
        message = "Marianne conductor is not running."
        hints = ["Start it with: mzt start"]
        running_state = "absent"
    elif isinstance(exc, MethodNotFoundError):
        # The conductor answered and does not know the method: the CLI is
        # newer than the running conductor. This is the one conductor error
        # a restart actually fixes.
        message = str(exc)
        hints = ["Pause or finish active scores before restarting: mzt restart"]
        running_state = "unknown"
    else:
        # A DaemonError raised after the conductor answered its health probe
        # (connection lost mid-request, admission refused). Restarting fixes
        # none of these; the outcome of a write is unknown.
        message = str(exc)
        hints = ["Check conductor status: mzt conductor-status"]
        running_state = "unknown"

    if pid is not None and running_state == "unknown":
        output_error(
            message,
            hints=hints,
            json_output=json_output,
            error_type=type(exc).__name__,
            running_state=running_state,
            conductor_pid=pid,
        )
    else:
        output_error(
            message,
            hints=hints,
            json_output=json_output,
            error_type=type(exc).__name__,
            running_state=running_state,
        )
    raise typer.Exit(1) from None

report_daemon_access_denied

report_daemon_access_denied(exc, *, json_output)

Preserve the established denial reporting entry point.

Source code in src/marianne/cli/helpers.py
def report_daemon_access_denied(exc: Exception, *, json_output: bool) -> NoReturn:
    """Preserve the established denial reporting entry point."""
    report_daemon_route_error(exc, json_output=json_output)

set_output_level

set_output_level(level)

Set the output level.

Parameters:

Name Type Description Default
level OutputLevel

The new output level.

required
Source code in src/marianne/cli/helpers.py
def set_output_level(level: OutputLevel) -> None:
    """Set the output level.

    Args:
        level: The new output level.
    """
    global _output_level
    _output_level = level

is_verbose

is_verbose()

Check if verbose output is enabled.

Source code in src/marianne/cli/helpers.py
def is_verbose() -> bool:
    """Check if verbose output is enabled."""
    return _output_level == OutputLevel.VERBOSE

is_quiet

is_quiet()

Check if quiet output is enabled.

Source code in src/marianne/cli/helpers.py
def is_quiet() -> bool:
    """Check if quiet output is enabled."""
    return _output_level == OutputLevel.QUIET

set_log_level

set_log_level(level)

Set the log level.

Parameters:

Name Type Description Default
level str

Log level string (DEBUG, INFO, WARNING, ERROR).

required
Source code in src/marianne/cli/helpers.py
def set_log_level(level: str) -> None:
    """Set the log level.

    Args:
        level: Log level string (DEBUG, INFO, WARNING, ERROR).
    """
    _log_config.level = level  # type: ignore[assignment]

set_log_file

set_log_file(path)

Set the log file path.

When a log file is specified, logs are written to the file in console format. Rich CLI output (progress bars, status tables) is separate from structured logging and still displays on console.

Parameters:

Name Type Description Default
path Path | None

Path for log file output, or None to disable file logging.

required
Source code in src/marianne/cli/helpers.py
def set_log_file(path: Path | None) -> None:
    """Set the log file path.

    When a log file is specified, logs are written to the file in
    console format. Rich CLI output (progress bars, status tables)
    is separate from structured logging and still displays on console.

    Args:
        path: Path for log file output, or None to disable file logging.
    """
    _log_config.file = path
    if path:
        _log_config.format = "console"

set_log_format

set_log_format(fmt)

Set the log format.

Parameters:

Name Type Description Default
fmt str

Log format string (json, console, both).

required
Source code in src/marianne/cli/helpers.py
def set_log_format(fmt: str) -> None:
    """Set the log format.

    Args:
        fmt: Log format string (json, console, both).
    """
    _log_config.format = fmt  # type: ignore[assignment]

configure_global_logging

configure_global_logging(console)

Configure logging based on global CLI options.

This is called after all callbacks have processed their options. Only configures once per session.

Parameters:

Name Type Description Default
console Console

Rich console for error output.

required

Raises:

Type Description
Exit

If logging configuration fails.

Source code in src/marianne/cli/helpers.py
def configure_global_logging(console: Console) -> None:
    """Configure logging based on global CLI options.

    This is called after all callbacks have processed their options.
    Only configures once per session.

    Args:
        console: Rich console for error output.

    Raises:
        typer.Exit: If logging configuration fails.
    """
    if _log_config.configured:
        return

    try:
        configure_logging(
            level=_log_config.level,
            format=_log_config.format,
            file_path=_log_config.file,
        )
        _log_config.configured = True
        # Note: Intentionally not logging here to avoid polluting --json output
        # If debugging is needed, use --log-file to redirect logs
    except ValueError as e:
        # Handle configuration errors (e.g., format="both" without file_path)
        from .output import output_error

        output_error(
            f"Logging configuration error: {e}",
            hints=[
                "Check --log-format and --log-file options.",
                "format='both' requires --log-file to be set.",
            ],
        )
        raise typer.Exit(1) from None

create_notifiers_from_config

create_notifiers_from_config(notification_configs)

Create Notifier instances from notification configuration.

Parameters:

Name Type Description Default
notification_configs list[NotificationConfig]

List of NotificationConfig from job config.

required

Returns:

Type Description
list[Notifier]

List of configured Notifier instances.

Source code in src/marianne/notifications/factory.py
def create_notifiers_from_config(
    notification_configs: list[NotificationConfig],
) -> list[Notifier]:
    """Create Notifier instances from notification configuration.

    Args:
        notification_configs: List of NotificationConfig from job config.

    Returns:
        List of configured Notifier instances.
    """
    from marianne.notifications.desktop import DesktopNotifier
    from marianne.notifications.slack import SlackNotifier
    from marianne.notifications.webhook import WebhookNotifier

    notifiers: list[Notifier] = []

    for config in notification_configs:
        notifier: Notifier | None = None
        # Cast Literal list to str list for from_config methods
        events: list[str] = list(config.on_events)

        if config.type == "desktop":
            notifier = DesktopNotifier.from_config(
                on_events=events,
                config=config.config,
            )
        elif config.type == "slack":
            notifier = SlackNotifier.from_config(
                on_events=events,
                config=config.config,
            )
        elif config.type == "webhook":
            notifier = WebhookNotifier.from_config(
                on_events=events,
                config=config.config,
            )
        else:
            _logger.warning("unknown_notification_type", type=config.type)
            continue

        if notifier:
            notifiers.append(notifier)

    return notifiers

require_conductor

require_conductor(routed, *, json_output=False)

Exit with a clear error when the conductor is not running.

Used by CLI commands that route through the conductor IPC. If the conductor was not reachable, this prints "not running" with the hint "Start it with: mzt start" only when no live PID is held; a live PID makes the same outcome "busy or unresponsive" (GH #455).

Parameters:

Name Type Description Default
routed bool

Whether try_daemon_route succeeded.

required
json_output bool

If True, output error as JSON instead of Rich markup.

False

Raises:

Type Description
Exit(1)

If the conductor is not running.

Source code in src/marianne/cli/helpers.py
def require_conductor(
    routed: bool,
    *,
    json_output: bool = False,
) -> None:
    """Exit with a clear error when the conductor is not running.

    Used by CLI commands that route through the conductor IPC. If the
    conductor was not reachable, this prints "not running" with the hint
    "Start it with: mzt start" only when no live PID is held; a live PID
    makes the same outcome "busy or unresponsive" (GH #455).

    Args:
        routed: Whether ``try_daemon_route`` succeeded.
        json_output: If True, output error as JSON instead of Rich markup.

    Raises:
        typer.Exit(1): If the conductor is not running.
    """
    if routed:
        return

    from marianne.daemon.exceptions import DaemonNotRunningError

    report_daemon_route_error(DaemonNotRunningError(), json_output=json_output)

await_early_failure async

await_early_failure(job_id, *, timeout=1.5, poll_interval=0.2)

Poll job status briefly to detect early failures after submission.

After a job is submitted and accepted, template rendering errors or other immediate failures happen within milliseconds. This function polls the daemon for up to timeout seconds so the CLI can report the failure inline instead of printing a cheerful "Job queued".

Fail-open: any exception returns None so this never blocks the CLI.

Source code in src/marianne/cli/helpers.py
async def await_early_failure(
    job_id: str,
    *,
    timeout: float = 1.5,
    poll_interval: float = 0.2,
) -> dict[str, Any] | None:
    """Poll job status briefly to detect early failures after submission.

    After a job is submitted and accepted, template rendering errors or
    other immediate failures happen within milliseconds.  This function
    polls the daemon for up to ``timeout`` seconds so the CLI can report
    the failure inline instead of printing a cheerful "Job queued".

    Fail-open: any exception returns ``None`` so this never blocks the CLI.
    """
    client = None
    try:
        from marianne.daemon.detect import _resolve_socket_path
        from marianne.daemon.ipc.client import DaemonClient

        socket_path = _resolve_socket_path(None)
        client = DaemonClient(socket_path)

        _terminal_states = {"failed", "cancelled"}
        _active_states = {"running", "queued"}

        # The deadline includes each RPC, not just sleeps between polls.
        async with asyncio.timeout(timeout):
            while True:
                await asyncio.sleep(poll_interval)
                result = await client.call("job.status", {"job_id": job_id})
                if not isinstance(result, dict):
                    continue

                status = result.get("status", "")
                if status in _terminal_states or status == "completed":
                    return result
                if status in _active_states:
                    continue

        return None
    except Exception:  # noqa: BLE001 — submission must survive failure of the advisory status probe
        _logger.debug("cli.early_failure_probe_failed", exc_info=True)
        return None
    finally:
        if client is not None:
            await client.close()

query_rate_limits async

query_rate_limits()

Query the conductor for active rate limit information.

Returns the backends dict from the daemon.rate_limits IPC response, e.g. {"claude-cli": {"seconds_remaining": 120.0}}.

Returns None if the conductor is unreachable or an error occurs. Fail-open: this never raises — callers can safely display extra info when available and skip it when not.

Source code in src/marianne/cli/helpers.py
async def query_rate_limits() -> dict[str, dict[str, float]] | None:
    """Query the conductor for active rate limit information.

    Returns the ``backends`` dict from the ``daemon.rate_limits`` IPC
    response, e.g. ``{"claude-cli": {"seconds_remaining": 120.0}}``.

    Returns ``None`` if the conductor is unreachable or an error occurs.
    Fail-open: this never raises — callers can safely display extra info
    when available and skip it when not.
    """
    try:
        from marianne.daemon.detect import try_daemon_route

        routed, result = await try_daemon_route("daemon.rate_limits", {})
        if not routed or not isinstance(result, dict):
            return None
        backends = result.get("backends")
        if isinstance(backends, dict):
            return backends
        return None
    except Exception:  # noqa: BLE001 — optional rate limit display must not block the CLI
        _logger.debug("cli.rate_limits_unavailable", exc_info=True)
        return None

check_pid_alive

check_pid_alive(pid)

Check if a process with the given PID is running.

Uses os.kill(pid, 0) — signal 0 checks existence without sending a signal.

Returns:

Type Description
bool

True if the process exists (even if owned by another user).

bool

False if the process does not exist.

Source code in src/marianne/cli/helpers.py
def check_pid_alive(pid: int) -> bool:
    """Check if a process with the given PID is running.

    Uses ``os.kill(pid, 0)`` — signal 0 checks existence without sending
    a signal.

    Returns:
        ``True`` if the process exists (even if owned by another user).
        ``False`` if the process does not exist.
    """
    import os

    try:
        os.kill(pid, 0)
        return True
    except PermissionError:
        # Process exists but owned by another user
        return True
    except OSError:
        # Process does not exist
        return False

get_last_activity_time

get_last_activity_time(job)

Get the most recent activity timestamp from the job.

Checks sheet last_activity_at fields and updated_at.

Parameters:

Name Type Description Default
job CheckpointState

CheckpointState to check.

required

Returns:

Type Description
datetime | None

datetime of last activity, or None if not available.

Source code in src/marianne/cli/helpers.py
def get_last_activity_time(job: CheckpointState) -> datetime | None:
    """Get the most recent activity timestamp from the job.

    Checks sheet last_activity_at fields and updated_at.

    Args:
        job: CheckpointState to check.

    Returns:
        datetime of last activity, or None if not available.
    """
    candidates = [
        ts for ts in (
            job.updated_at,
            *(sheet.last_activity_at for sheet in job.sheets.values()),
        )
        if ts is not None
    ]
    return max(candidates) if candidates else None