Skip to content

run

run

Run command for Marianne CLI.

This module implements the mzt run command which routes scores through a running conductor (mzt start). Direct execution is not supported — a running conductor is required (like docker requires dockerd).

The only exception is --dry-run, which validates and displays the execution plan without executing anything (no conductor needed).

Classes

SubmitStatus

Bases: str, Enum

How a submission ended (#561).

What the conductor did with it, or why it never got there. submit_score returns one of these; the commands render.

SubmitResult dataclass

SubmitResult(status, job_id=None, message='', error='', response=None, pid=None, cause=None, fresh=False)

A submission outcome as a value: no printing, no typer.Exit.

Attributes:

Name Type Description
status SubmitStatus

Which :class:SubmitStatus outcome this is.

job_id str | None

The conductor-assigned job id, when known.

message str

The conductor's message, or the refusal reason.

error str

The error_message of an early failure.

response dict[str, Any] | None

The conductor's raw reply dict, when it sent one: the job.submit reply, or on EARLY_FAILED the early-failure poll's reply. None when nothing reached the conductor.

pid int | None

On NOT_ROUTED with a refused socket, the PID of a conductor that is still alive (busy, GH #455); None when it is absent.

cause Exception | None

The exception behind a ROUTE_ERROR.

fresh bool

Whether the submission asked for a fresh run; selects the "already running" hint, so a renderer needs no extra argument.

Functions:

run

run(config_file=Argument(..., help='Path to YAML score configuration file', exists=True, readable=True), dry_run=Option(False, '--dry-run', '-n', help='Show what would be executed without running'), start_sheet=Option(None, '--start-sheet', '-s', help='Override starting sheet number'), workspace=Option(None, '--workspace', '-w', hidden=True, help="Expert override of the workspace directory. By default the conductor auto-manages workspaces under ~/workspaces/<score-name> (or the score's explicit workspace:), so this is rarely needed. Takes precedence over both. Creates the directory if absent."), json_output=Option(False, '--json', '-j', help='Output result as JSON for machine parsing'), escalation=Option(False, '--escalation', '-e', help='Pause sheets in FERMATA on retry exhaustion for a composer decision (resolve via mzt status instructions). Works without --self-healing.'), self_healing=Option(False, '--self-healing', '-H', help='Enable automatic diagnosis and remediation when retries are exhausted'), yes=Option(False, '--yes', '-y', help='Auto-confirm suggested fixes when using --self-healing'), fresh=Option(False, '--fresh', help="Start over from scratch: re-register the score (all sheets re-run) and, when the score sets workspace_lifecycle.archive_on_fresh, archive non-preserved workspace files first so stale artifacts can't satisfy this run's validations. Use for self-chaining scores or re-running a completed score. (Editing the score file also triggers this automatically for completed scores.)"), require_lock=Option(None, '--require-lock', help="Admission gate: refuse unless this composition lock (written by the composing skill's release gate, check_score_release.py --write-lock) attests the exact score bytes. A missing, stale or malformed lock rejects the job with a specific reason. Omit for ordinary unlocked submission."), job_id=Option(None, '--job-id', help="Name this run. By default the job ID is the score's filename stem; --job-id lets one shipped score serve several commissions side by side without copying it. When the score sets no workspace:, the auto-managed workspace follows the job ID too (~/workspaces/<job-id>). Use the same ID with status, pause, resume and cancel."), var=Option(None, '--var', help="Per-invocation template variable as key=value (repeatable). Merges into the score's prompt.variables, overriding YAML on collision — so one generic score can be pointed at different inputs without editing the file. Values are strings; typed variables belong in YAML. Persisted across resume."))

Run a score from a YAML configuration file.

Source code in src/marianne/cli/commands/run.py
def run(
    config_file: Path = typer.Argument(
        ...,
        help="Path to YAML score configuration file",
        exists=True,
        readable=True,
    ),
    dry_run: bool = typer.Option(
        False,
        "--dry-run",
        "-n",
        help="Show what would be executed without running",
    ),
    start_sheet: int | None = typer.Option(
        None,
        "--start-sheet",
        "-s",
        help="Override starting sheet number",
    ),
    workspace: Path | None = typer.Option(
        None,
        "--workspace",
        "-w",
        hidden=True,
        help="Expert override of the workspace directory. By default the "
        "conductor auto-manages workspaces under ~/workspaces/<score-name> "
        "(or the score's explicit workspace:), so this is rarely needed. "
        "Takes precedence over both. Creates the directory if absent.",
    ),
    json_output: bool = typer.Option(
        False,
        "--json",
        "-j",
        help="Output result as JSON for machine parsing",
    ),
    escalation: bool = typer.Option(
        False,
        "--escalation",
        "-e",
        help="Pause sheets in FERMATA on retry exhaustion for a composer "
        "decision (resolve via mzt status instructions). Works without "
        "--self-healing.",
    ),
    self_healing: bool = typer.Option(
        False,
        "--self-healing",
        "-H",
        help="Enable automatic diagnosis and remediation when retries are exhausted",
    ),
    yes: bool = typer.Option(
        False,
        "--yes",
        "-y",
        help="Auto-confirm suggested fixes when using --self-healing",
    ),
    fresh: bool = typer.Option(
        False,
        "--fresh",
        help="Start over from scratch: re-register the score (all sheets "
        "re-run) and, when the score sets workspace_lifecycle."
        "archive_on_fresh, archive non-preserved workspace files first so "
        "stale artifacts can't satisfy this run's validations. Use for "
        "self-chaining scores or re-running a completed score. (Editing "
        "the score file also triggers this automatically for completed "
        "scores.)",
    ),
    require_lock: Path | None = typer.Option(
        None,
        "--require-lock",
        help="Admission gate: refuse unless this composition lock (written by "
        "the composing skill's release gate, check_score_release.py "
        "--write-lock) attests the exact score bytes. A missing, stale or "
        "malformed lock rejects the job with a specific reason. Omit for "
        "ordinary unlocked submission.",
    ),
    job_id: str | None = typer.Option(
        None,
        "--job-id",
        help="Name this run. By default the job ID is the score's filename "
        "stem; --job-id lets one shipped score serve several commissions "
        "side by side without copying it. When the score sets no "
        "workspace:, the auto-managed workspace follows the job ID too "
        "(~/workspaces/<job-id>). Use the same ID with status, pause, "
        "resume and cancel.",
    ),
    var: list[str] | None = typer.Option(
        None,
        "--var",
        help="Per-invocation template variable as key=value (repeatable). "
        "Merges into the score's prompt.variables, overriding YAML on "
        "collision — so one generic score can be pointed at different "
        "inputs without editing the file. Values are strings; typed "
        "variables belong in YAML. Persisted across resume.",
    ),
) -> None:
    """Run a score from a YAML configuration file."""
    from marianne.core.config import JobConfig
    from marianne.daemon.fleet import is_fleet_config

    # A direct call of run() (tests, embedding) leaves typer's OptionInfo
    # default in place; only a real string names the run.
    if not isinstance(job_id, str):
        job_id = None
    if job_id is not None:
        from ._shared import validate_job_id

        job_id = validate_job_id(job_id)

    try:
        runtime_vars = _parse_runtime_vars(var)
    except ValueError as exc:
        if json_output:
            output_json({"error": str(exc)})
        else:
            output_error(
                str(exc),
                hints=["Use --var key=value (the value may contain '=')."],
            )
        raise typer.Exit(1) from None

    # Fleet configs are submitted through the same command, but they are
    # not JobConfig files. Route them before score parsing so the documented
    # `mzt run fleet.yaml` path actually reaches the conductor fleet manager.
    if is_fleet_config(config_file):
        _run_fleet(
            config_file=config_file,
            dry_run=dry_run,
            start_sheet=start_sheet,
            workspace=workspace,
            json_output=json_output,
            fresh=fresh,
            self_healing=self_healing,
            yes=yes,
            escalation=escalation,
            runtime_vars=runtime_vars,
            job_id=job_id,
        )
        return

    try:
        config = JobConfig.from_yaml(config_file)
    except yaml.YAMLError as e:
        if json_output:
            output_json({"error": f"YAML syntax error: {e}"})
        else:
            output_error(
                f"YAML syntax error: {e}",
                hints=[
                    "Check for indentation issues or invalid YAML characters.",
                    f"Validate with: mzt validate {config_file}",
                ],
            )
        raise typer.Exit(1) from None
    except Exception as e:  # noqa: BLE001 — CLI boundary reports the error and exits unsuccessfully
        if json_output:
            output_json({"error": str(e)})
        else:
            output_error(
                str(e),
                hints=[f"Validate with: mzt validate {config_file}"],
            )
        raise typer.Exit(1) from None

    if config.unknown_fields:
        names = ", ".join(
            f"{field.path + '.' if field.path else ''}{field.key}"
            for field in config.unknown_fields
        )
        typer.echo(f"Warning: ignored unknown score field(s): {names}", err=True)

    # Validate start_sheet (must be positive if provided)
    start_sheet = validate_start_sheet(start_sheet)

    # Override workspace from CLI if provided
    if workspace is not None:
        config.workspace = Path(workspace).resolve()

    # In quiet mode, skip the config panel
    if not is_quiet() and not json_output:
        instrument_display = config.effective_instrument_name
        recurring_label = " \\[recurring]" if config.schedule is not None else ""
        console.print(
            Panel(
                f"[bold]{config.name}[/bold]{recurring_label}\n"
                f"{config.description or 'No description'}\n\n"
                f"Instrument: {instrument_display}\n"
                f"Sheets: {config.sheet.total_sheets} "
                f"({config.sheet.size} items each)\n"
                f"Workspace: {config.workspace}",
                title="Score Configuration",
            )
        )

    # Cost warning — alert users when cost tracking is disabled
    if not is_quiet() and not json_output and not config.cost_limits.enabled:
        console.print(
            "\n[yellow]Note:[/yellow] Cost tracking is disabled for this score. "
            "API calls will not be monitored or limited."
        )
        console.print(
            "  [dim]To enable, add to your score:[/dim] "
            "cost_limits: {enabled: true, max_cost_per_job: 10.00}"
        )

    if dry_run:
        # The preview renders the score the conductor would run: --var merges
        # through the same seam the daemon applies at submit (#359).
        from marianne.core.config.job import merge_runtime_variables

        config = merge_runtime_variables(config, runtime_vars or {})
        if not json_output:
            console.print("\n[yellow]Dry run - not executing[/yellow]")
            _show_dry_run(config, config_file)
        else:
            output_json(
                {
                    "dry_run": True,
                    "job_name": config.name,
                    "total_sheets": config.sheet.total_sheets,
                    "workspace": str(config.workspace),
                }
            )
        return

    # Route through daemon (required)
    routed = asyncio.run(
        _try_daemon_submit(
            config_file,
            workspace,
            fresh,
            self_healing,
            yes,
            json_output,
            start_sheet=start_sheet,
            escalation=escalation,
            runtime_vars=runtime_vars,
            require_lock=require_lock,
            job_id=job_id,
        ),
    )
    if routed:
        return

    # Daemon not available or submission failed
    _report_conductor_unreachable(json_output)

submit_score async

submit_score(config_file, *, workspace=None, fresh=False, self_healing=False, auto_confirm=False, start_sheet=None, escalation=False, runtime_vars=None, require_lock=None, job_id=None)

Validate, submit the score to the conductor, return the outcome.

The one submit path mzt run and mzt agents run both call (#561). It validates the job id and the score, probes the conductor, routes job.submit, polls briefly for an early failure, and returns a typed value. It never prints and never raises typer.Exit; every refusal — including an invalid job id or an unloadable score — is a returned SubmitResult, and neither reaches the conductor.

The score check is conservative (:func:_score_load_error refuses only what it can prove), and neither refusal is new for mzt run: run refuses a bad --job-id and an invalid score in its own command body, before it calls the adapter, so here those branches serve mzt agents run, which calls this function directly.

Source code in src/marianne/cli/commands/run.py
async def submit_score(
    config_file: Path,
    *,
    workspace: Path | None = None,
    fresh: bool = False,
    self_healing: bool = False,
    auto_confirm: bool = False,
    start_sheet: int | None = None,
    escalation: bool = False,
    runtime_vars: dict[str, str] | None = None,
    require_lock: Path | None = None,
    job_id: str | None = None,
) -> SubmitResult:
    """Validate, submit the score to the conductor, return the outcome.

    The one submit path ``mzt run`` and ``mzt agents run`` both call (#561).
    It validates the job id and the score, probes the conductor, routes
    ``job.submit``, polls briefly for an early failure, and returns a typed
    value. It never prints and never raises ``typer.Exit``; every refusal —
    including an invalid job id or an unloadable score — is a returned
    ``SubmitResult``, and neither reaches the conductor.

    The score check is conservative (:func:`_score_load_error` refuses only
    what it can prove), and neither refusal is new for ``mzt run``: ``run``
    refuses a bad ``--job-id`` and an invalid score in its own command body,
    before it calls the adapter, so here those branches serve
    ``mzt agents run``, which calls this function directly.
    """
    from marianne.daemon.exceptions import (
        DaemonAccessDeniedError,
        DaemonError,
        DaemonProtocolError,
        DaemonUnresponsiveError,
    )

    def not_routed() -> SubmitResult:
        # A refused socket with a live PID is a busy conductor, never an absent
        # one (GH #455): carry the evidence, and let the renderer word it.
        from marianne.cli.helpers import conductor_socket_present, live_conductor_pid

        pid = live_conductor_pid() if conductor_socket_present() else None
        return SubmitResult(status=SubmitStatus.NOT_ROUTED, pid=pid, fresh=fresh)

    # Job id first, as a value (#561 D4): an invalid id must not escape as a
    # click.UsageError and must not touch the conductor.
    if job_id is not None:
        from ._shared import validate_job_id

        try:
            validate_job_id(job_id)
        except typer.BadParameter as exc:
            return SubmitResult(
                status=SubmitStatus.BAD_JOB_ID, message=str(exc.message), fresh=fresh
            )

    # Then the score, for the same reason: a syntax or schema error is known
    # here and must not depend on the conductor being up to be reported.
    score_error = _score_load_error(config_file)
    if score_error is not None:
        return SubmitResult(
            status=SubmitStatus.INVALID_SCORE, message=score_error, fresh=fresh
        )

    try:
        from marianne.daemon.detect import is_daemon_available, try_daemon_route

        if not await is_daemon_available():
            return not_routed()

        params: dict[str, Any] = {
            "config_path": str(config_file.resolve()),
            "fresh": fresh,
            "self_healing": self_healing,
            "self_healing_auto_confirm": auto_confirm,
            "escalation": escalation,  # #361: decoupled from self_healing
            "client_cwd": str(Path.cwd()),
        }
        if workspace is not None:
            params["workspace"] = str(Path(workspace).resolve())
        if start_sheet is not None:
            params["start_sheet"] = start_sheet
        if runtime_vars:
            params["runtime_variables"] = runtime_vars
        if require_lock is not None:
            params["required_lock"] = str(Path(require_lock).resolve())
        if job_id is not None:
            params["job_id"] = job_id

        routed, result = await try_daemon_route("job.submit", params)
        if not routed:
            return not_routed()

        if not isinstance(result, dict):
            # D6: a non-dict reply is a protocol problem, not a rejection —
            # there is no reply to print honestly.
            return SubmitResult(
                status=SubmitStatus.ROUTE_ERROR,
                cause=DaemonProtocolError("job.submit returned a non-dict reply"),
                fresh=fresh,
            )

        status = result.get("status", "unknown")
        got_id = str(result.get("job_id") or "")
        msg = str(result.get("message") or "")

        if status == "pending":
            return SubmitResult(
                status=SubmitStatus.PENDING,
                job_id=got_id,
                message=msg,
                response=result,
                fresh=fresh,
            )
        if status != "accepted":
            return SubmitResult(
                status=SubmitStatus.REJECTED,
                job_id=got_id,
                message=msg,
                response=result,
                fresh=fresh,
            )

        # Poll briefly to catch early failures (e.g. template errors).
        # Skip when --fresh: old state may still be transitioning and
        # would produce false failure reports from the previous run (#139).
        early: dict[str, Any] | None = None
        if not fresh:
            early = await await_early_failure(got_id)
        early_status = early.get("status", "") if isinstance(early, dict) else ""
        if early_status in ("failed", "cancelled"):
            return SubmitResult(
                status=SubmitStatus.EARLY_FAILED,
                job_id=got_id,
                error=str(early.get("error_message") or "") if isinstance(early, dict) else "",
                response=early if isinstance(early, dict) else None,
                fresh=fresh,
            )
        return SubmitResult(
            status=SubmitStatus.ACCEPTED,
            job_id=got_id,
            message=msg,
            response=result,
            fresh=fresh,
        )
    except DaemonAccessDeniedError as exc:
        return SubmitResult(status=SubmitStatus.ROUTE_ERROR, cause=exc, fresh=fresh)
    except (DaemonUnresponsiveError, DaemonProtocolError) as exc:
        return SubmitResult(status=SubmitStatus.ROUTE_ERROR, cause=exc, fresh=fresh)
    except (OSError, ConnectionError, TimeoutError) as exc:
        _logger.warning("daemon_submit_failed", error=str(exc), exc_info=True)
        return not_routed()
    except Exception as exc:
        # F-450: MethodNotFoundError and other DaemonError subclasses
        # re-raised by try_daemon_route are route errors, not crashes.
        if isinstance(exc, DaemonError):
            return SubmitResult(status=SubmitStatus.ROUTE_ERROR, cause=exc, fresh=fresh)
        raise

render_submit_result

render_submit_result(result, *, json_output, rejection_followup=None)

Print a SubmitResult the way mzt run has always printed one.

Raises typer.Exit(1) for every outcome a caller cannot continue past. Today the _try_daemon_submit adapter is its only caller: run and _run_fleet both submit through it. The renderer stays synchronous, so rejection_followup is how the adapter gets the rate-limit panel into the output: it awaits the query (:func:_fetch_rate_limits_on_rejection) before rendering, then hands the sync print (:func:_print_rate_limits) in here, to run in text mode after the error and before the exit, which is where mzt run has always shown it. It is called only for REJECTED.

NOT_ROUTED is not rendered here: the adapter returns False for it and the caller renders it through _report_conductor_unreachable.

Source code in src/marianne/cli/commands/run.py
def render_submit_result(
    result: SubmitResult,
    *,
    json_output: bool,
    rejection_followup: Callable[[], None] | None = None,
) -> None:
    """Print a ``SubmitResult`` the way ``mzt run`` has always printed one.

    Raises ``typer.Exit(1)`` for every outcome a caller cannot continue past.
    Today the ``_try_daemon_submit`` adapter is its only caller: ``run`` and
    ``_run_fleet`` both submit through it. The renderer stays synchronous, so
    ``rejection_followup`` is how the adapter gets the rate-limit panel into
    the output: it awaits the query (:func:`_fetch_rate_limits_on_rejection`)
    before rendering, then hands the sync print (:func:`_print_rate_limits`)
    in here, to run in text mode after the error and before the exit, which
    is where ``mzt run`` has always shown it. It is called only for
    ``REJECTED``.

    ``NOT_ROUTED`` is not rendered here: the adapter returns ``False`` for it
    and the caller renders it through ``_report_conductor_unreachable``.
    """
    from marianne.cli.helpers import report_daemon_access_denied, report_daemon_route_error

    if result.status == SubmitStatus.ACCEPTED:
        if json_output:
            output_json(result.response or {})
        else:
            job_id = result.job_id or "?"
            console.print(f"[green]Score submitted to conductor:[/green] {job_id}")
            if result.message:
                console.print(f"  {result.message}")
            console.print(f"\n[dim]Monitor with:[/dim] mzt status {job_id} --watch")
        return

    if result.status == SubmitStatus.PENDING:
        _handle_pending_response(
            job_id=result.job_id or "?", message=result.message, json_output=json_output
        )
        return

    if result.status == SubmitStatus.REJECTED:
        if json_output:
            output_json(result.response or {"error": result.message})
        else:
            rejection = (
                f"Conductor rejected score: {result.message}"
                if result.message
                else "Conductor rejected score."
            )
            # The result carries `fresh`, so the "already running" hint is the
            # one this submission earned, with no extra argument to pass.
            output_error(rejection, hints=_rejection_hints(result.message, fresh=result.fresh))
            if rejection_followup is not None:
                rejection_followup()
        raise typer.Exit(1)

    if result.status == SubmitStatus.EARLY_FAILED:
        job_id = result.job_id or "?"
        if json_output:
            output_json(result.response or {"error": result.error})
        else:
            hints = [f"Run: mzt diagnose {job_id}"]
            if result.error:
                hints.insert(0, result.error)
            output_error(f"Score failed: {job_id}", hints=hints)
        raise typer.Exit(1)

    if result.status in (SubmitStatus.BAD_JOB_ID, SubmitStatus.INVALID_SCORE):
        # Reached only when a caller passes an unvalidated id or an unchecked
        # score straight to the adapter; `run` refuses both at its own entry.
        if json_output:
            output_json({"error": result.message})
        else:
            output_error(result.message, hints=_refusal_hints(result.status))
        raise typer.Exit(1)

    if result.status == SubmitStatus.ROUTE_ERROR:
        cause = result.cause
        if cause is None:
            raise ValueError("ROUTE_ERROR without a cause")
        from marianne.daemon.exceptions import DaemonAccessDeniedError

        if isinstance(cause, DaemonAccessDeniedError):
            report_daemon_access_denied(cause, json_output=json_output)
        report_daemon_route_error(cause, json_output=json_output)
        return  # both reporters raise; this keeps the function honest if one stops

    raise ValueError(f"unhandled SubmitStatus: {result.status!r}")