Skip to content

Latest commit

 

History

History
341 lines (259 loc) · 13.1 KB

batch_runner.md

File metadata and controls

341 lines (259 loc) · 13.1 KB

Batch Runner

orchestrate-batch is a sequential queue runner for the existing orchestrate binary. By default, it performs one task at a time using a two-workflow pipeline:

plan -> implement -> remove and commit PLAN.md -> done

With --implement-only (alias --impl), it uses:

implement -> done

The batch runner does not call the orchestrator FSM as a library. Every phase starts a fresh orchestrate process through Cargo and evaluates only that process's exit status and final JSON report.

Run

From the multi-agent-orchestrator-rs repository root:

cargo run --package orchestrate-batch -- \
  --workspace-root /home/user/projects/myapp \
  --executor-thread-name "batch executor" \
  --reviewer-thread-name "batch reviewer" \
  --notion-policy optional \
  --remote-network-policy forbidden \
  --executor-provider claude \
  --reviewer-provider codex

--tasks-dir has the visible alias --td. When omitted, it defaults to the canonical <workspace-root>/tasks path regardless of the batch runner's current working directory. An explicit relative --tasks-dir override is resolved from the current working directory. All other arguments are the same common arguments accepted by orchestrate:

  • --workspace-root (--wr)
  • --executor-thread-name (--etn)
  • --reviewer-thread-name (--rtn)
  • --notion-policy (--np)
  • --remote-network-policy (--rnp)
  • --executor-provider (--ep)
  • --reviewer-provider (--rp)
  • --executor-model (--em)
  • --reviewer-model (--rm)

--implement-only has the visible alias --impl. It sends each task directly to the implementation workflow, skips planning and PLAN.md cleanup, and requires PLAN.md to remain absent.

The batch runner owns --workflow and --prompt; callers cannot supply either value. Model overrides are forwarded unchanged to the selected Claude, OpenCode, or Codex provider.

Cargo Child Process

The batch runner deliberately does not depend on an existing orchestrate executable in PATH or under target/. Each phase is equivalent to:

cargo run --quiet \
  --manifest-path /absolute/path/to/multi-agent-orchestrator-rs/Cargo.toml \
  --package orchestrate \
  --bin orchestrate \
  -- \
  <orchestrator arguments>

Cargo therefore rebuilds changed orchestrator sources before the phase and reuses its build cache when the sources are unchanged. The active task prompt is passed as one process argument; no shell, command substitution, or heredoc is involved. The exact task-file text is embedded unchanged between batch_task_prompt_begin and batch_task_prompt_end inside the mode-specific wrapper.

Cargo and orchestrator logs remain on stderr and are also persisted in <workspace-root>/orchestrate-logs/. The batch creates one --batch--...log file; every child orchestrator run creates its own --orchestrate--b...--taskNNN--<workflow>--...log file. The child orchestrator's stdout is captured and must contain exactly one valid final JSON report.

Task Directory

The runner creates the task root and done/ directory when they do not exist:

tasks/
├── README.md
├── 001.txt
├── 002.txt
├── done/
└── .batch.lock

Pending tasks are only the direct children of the task root. The scan is non-recursive and never reads done/.

Task input rules:

  • A task must be a regular, non-symlink UTF-8 file.
  • Its name must consist of a decimal numeric id followed by .txt.
  • Ordering uses the parsed numeric id, not lexicographic filename order.
  • Equivalent ids such as 1.txt and 001.txt are rejected as duplicates.
  • Empty or whitespace-only prompts are rejected.
  • A regular root README.md is allowed for queue documentation and is ignored by task discovery.
  • The agent-facing guide for investigating work, decomposing it, and populating the queue is docs/task_queue_preparation.md.
  • Any other unknown file or directory in the task root is a preflight error; only numeric task files, README.md, done/, and .batch.lock are accepted.
  • When a task starts, a same-named previous copy in done/ is deleted before the prompt is read.
  • After reading the planning prompt, the runner does not monitor later task-file edits. If another process changes, removes, or moves the file, the batch continues.

The runner itself keeps the task file in the root for the entire pipeline. There is no in-progress/ directory and no pre-success move.

Local Git Exclusion and Lock

The task directory is local runtime state and must not become part of a repository worktree. If it is inside a Git repository, the runner:

  1. Resolves that repository's real .git/info/exclude, including Git worktrees whose .git is a file.
  2. Refuses to continue if the task directory already contains tracked files.
  3. Adds an anchored repository-relative directory pattern when it is absent.
  4. Creates .batch.lock and verifies with git check-ignore that the lock file is ignored.

For a root-level default directory the pattern is:

/tasks/

For a custom directory such as <repo>/local/batch, the pattern is:

/local/batch/

The leading slash anchors the pattern to the detected Git top-level. Nothing is added when the task directory is outside a Git worktree.

The runner holds an exclusive filesystem lock on tasks/.batch.lock for the complete batch. A second runner using the same task directory fails during preflight. The lock file may remain on disk after exit; the operating-system lock is released when the process exits.

Per-Task Pipelines

The runner rescans the task root, selects the smallest numeric id, reads the prompt, and performs the following steps without moving the task file.

Default: Planning

The planning child receives:

workflow: plan
prompt:
  planning and feasibility guidance
  batch_task_prompt_begin
  <exact UTF-8 contents of the task file>
  batch_task_prompt_end
  explicit PLAN.md and outbox reporting requirements

The wrapper requires the planner to investigate feasibility, preserve explicit requirements, and report any objectively impossible or disproportionately large part in PLAN.md and executor outbox with evidence, impact, and a recommended alternative or task split. Difficulty by itself is not sufficient reason to discard a requirement.

The batch runner waits for the complete orchestrator run, including its reviewer revise loop.

Default: Implementation

Implementation starts only after accepted planning and receives:

workflow: implement
prompt: Implement PLAN.md.

The same workspace, provider, thread, policy, and optional model arguments are forwarded to both phases.

Implement-Only

With --implement-only or --impl, the planning child is not started. The implementation child receives:

workflow: implement
prompt:
  direct implementation and feasibility guidance
  batch_task_prompt_begin
  <exact UTF-8 contents of the task file>
  batch_task_prompt_end
  explicit executor outbox reporting requirements

The wrapper requires direct implementation rather than a plan-only response and explicitly forbids creating or changing PLAN.md. Objectively impossible or disproportionately large requirements must be reported with evidence, impact, and a recommended alternative or task split. Difficulty by itself is not sufficient reason to discard a requirement. The orchestrator still owns its complete executor/reviewer revise loop.

Implement-only requires <workspace-root>/PLAN.md to be absent during batch preflight, immediately before each task starts, and after accepted implementation, regardless of whether the path is tracked. The runner does not delete or replace an existing plan because the implement reviewer would otherwise treat stale PLAN.md content as part of the review contract.

Child Success Contract

A phase succeeds only when all of these facts agree:

  • The cargo run process exits with status 0.
  • stdout is one valid orchestrator JSON document.
  • workflow_type matches the requested phase.
  • workspace_root matches the canonical target workspace.
  • state is run_done.
  • reason_code is done_quality_reached.
  • reviewer_decision is accept.

A non-zero child exit with a valid JSON report is a normal orchestration failure and stops the batch. A missing or malformed JSON report, a successful exit paired with non-success fields, or a workflow/workspace mismatch is a protocol failure and also stops the batch.

The runner does not read .agent-io/outbox.txt, compare outbox timestamps, or persist child reports to files. A rejected child report is emitted to stderr for diagnosis, and the terminal batch report contains a concise failure reason.

PLAN.md Cleanup

This stage applies only to the default pipeline. Implement-only mode skips it entirely.

After accepted implementation, the task is still not complete. The runner validates the target workspace again. If PLAN.md is present, it creates its own cleanup commit:

  • The workspace is the Git top-level and has a valid HEAD.
  • HEAD is attached to the same non-protected named branch reported by implementation.
  • No merge, rebase, cherry-pick, revert, sequencer, bisect, conflicted index, or Git lock is present.
  • The worktree and index are clean.
  • PLAN.md exists as a regular tracked file.

The runner then performs the safe equivalent of the local rmvpl shell alias:

git rm -- PLAN.md
git commit -m "removed plan"

Before committing, the staged diff must contain only D PLAN.md. After committing, the runner verifies that HEAD advanced, the subject is exactly removed plan, the commit contains only that deletion, and the worktree is clean. It does not amend, reset, rebase, or automatically roll back a failed cleanup.

If PLAN.md is already absent, the runner still validates the expected branch and a clean worktree, but skips git rm and does not create an additional cleanup commit. This permits a previous agent to have removed and committed the plan without making the batch fail.

When a task starts, the runner first removes a same-named previous copy from done/. Immediately before completion it removes that destination again in case another process recreated it, then performs one same-filesystem rename when the source task file still exists:

tasks/001.txt -> tasks/done/001.txt

Failure and Restart Semantics

The batch layer has no automatic retry or recovery. The orchestrator retains its own internal infrastructure retries and reviewer revise loop, but the first terminal planning, implementation, cleanup, completion, process, or protocol failure stops the batch. A reviewer revise is not a terminal batch result and does not stop the batch by itself.

On failure:

  • The current task file remains in the task root unless another process has moved or removed it.
  • No later task starts.
  • If the file still exists, a new explicit batch invocation selects the same smallest numeric task again and restarts it from the first stage of the selected mode.

The target workspace may still require manual attention. For example, an orchestrator failure may leave workspace changes, or cleanup may fail after git rm has staged deletion of PLAN.md. The batch runner intentionally performs no destructive rollback; the next orchestrator invocation will normally stop at its clean-worktree gate until the operator resolves the state.

In default mode, there is a narrow crash window after the cleanup commit succeeds but before the task rename. In that case the task remains pending even though PLAN.md has already been removed. Inspect the Git history before manually deciding whether to move the task to done/ or prepare it for another run.

Because all tasks use the same executor and reviewer thread names, long batches also accumulate context in those persistent provider sessions. Create and select appropriate sessions before starting a batch.

Batch Output and Exit Codes

Logs are written to stderr and persisted in <workspace-root>/orchestrate-logs/. On normal batch termination, stdout contains one JSON document.

Successful or empty queue:

{
  "state": "batch_done",
  "tasks_directory": "/absolute/path/to/tasks",
  "workspace_root": "/absolute/path/to/workspace",
  "completed_tasks": [
    {
      "task_id": 1,
      "source_file": "001.txt",
      "cleanup_commit": "0123456789abcdef"
    }
  ]
}

cleanup_commit is omitted when no cleanup commit was needed: always in implement-only mode and also in default mode when PLAN.md was already absent after implementation.

Failure:

{
  "state": "batch_failed",
  "tasks_directory": "/absolute/path/to/tasks",
  "workspace_root": "/absolute/path/to/workspace",
  "completed_tasks": [],
  "failed_task": "001.txt",
  "failure_stage": "implement",
  "failure_reason": "orchestrator process failed: ..."
}

failure_stage is one of preflight, plan, implement, cleanup, or completion.

Exit codes:

  • 0: all pending tasks completed, or the queue was empty.
  • 1: batch preflight, task, subprocess, protocol, cleanup, or completion failure.
  • 2: CLI argument parsing error emitted by Clap before the batch report is created.

The runner does not create timestamps, per-attempt state files, or per-attempt report files.