wgm CLI reference: loop.sh

August 20, 2026 · View on GitHub

scripts/loop.sh runs the Ralph outer loop: it invokes your coding agent once per iteration, each time with a fresh context and a short prompt telling it to follow the wgm protocol in a given mode and advance exactly one task. IMPLEMENTATION_PLAN.md is the shared state between otherwise disposable iterations.

Note: The loop is optional. wgm is a portable SKILL.md and works in-session without any script. Reach for loop.sh when you want genuinely fresh context per iteration — see Choose a loop mode.

Syntax

scripts/loop.sh [MODE] [MAX_ITERATIONS|only] [FLAGS] [-- AGENT_ARGV...]

Run it from your project's root, using the path to your installed copy:

~/.agents/skills/wgm/scripts/loop.sh build --agent 'copilot -p --allow-all-tools'

Positional arguments

ArgumentValuesDefaultDescription
MODEgrill, analyze, plan, preflight, build, loop, review, extractbuildWhich phase to run. loop is an alias of build.
MAX_ITERATIONSInteger; 0 = unlimited0 for build, 1 for single-phase modesIteration cap.
onlyLiteral keywordRun a single iteration or phase, then stop. Equivalent to MAX_ITERATIONS of 1.

Choosing the agent

The script does not know your agent's CLI. Supply it one of three ways, listed in order of increasing safety:

MethodExampleNotes
$WGM_AGENTexport WGM_AGENT='copilot -p --allow-all-tools'Shell-evaluated. Set only to a command you trust; the permission flag is required for unattended tool use.
--agent "CMD"--agent 'claude --dangerously-skip-permissions -p'Shell-evaluated. Overrides $WGM_AGENT.
-- passthrough-- claude -pSafest. Everything after -- is argv, invoked without eval.

The prompt is appended as the final argument. If your agent reads its prompt from stdin instead, set WGM_PROMPT_STDIN=1.

Caution: $WGM_AGENT and --agent are evaluated by the shell. Never point them at a command string assembled from untrusted input.

Flags

Agent and scope

FlagDefaultDescription
--agent "CMD"$WGM_AGENTAgent command, shell-evaluated.
--frugal-agent "CMD"$WGM_FRUGAL_AGENTCheaper agent for routine iterations; escalates to --agent on a stall. Requires --agent for escalation to engage.
--request "TXT"noneRequest or scope injected into the prompt. Most useful with plan and build.
--plan FILEautoExplicit plan path. Without it, .wgm/IMPLEMENTATION_PLAN.md wins when both root and .wgm/ plans exist.
--source DIRnoneExemplar codebase for extract (gene transfusion).

Scenario validation

FlagDefaultDescription
--threshold N95Satisfaction target (0–100) the build converges to.
--scenarios DIRscenarios/ or .wgm/scenarios/Where holdout scenarios live.
--stratifiedoffValidate scenarios by ascending tier (1 → 2 → 3), so easy passes cannot mask a broken tier 3.
--container ENGINEautoSelects available podman, then docker; explicit unavailable engines fail clearly.

Stopping the loop

FlagDefaultDescription
--max-runtime-seconds N0 (unlimited)Hard wall-clock cap for the whole loop.
--idle-timeout N0 (disabled)Stop if the plan makes no progress for N seconds.
--agent-timeout-seconds N0 (disabled)Terminate the active agent process group after N seconds with GNU timeout/gtimeout; otherwise report the cooperative fallback.
--max-no-progress-iterations N3Fail after N successful build iterations leave the plan unchanged; 0 disables this guard. With frugal/main escalation, the default leaves one iteration for escalation before the circuit breaker.
--max-consecutive-failures N3Circuit breaker: stop after N iterations that fail every retry. 0 never trips.
--max-cost N0 (unlimited)Stop once cumulative cost from --cost-cmd reaches N. Requires --cost-cmd.

Tip: For fail-fast on the first error, combine --max-retries 0 --max-consecutive-failures 1.

Resilience

FlagDefaultDescription
--max-retries N2Retry a failed agent invocation N times per iteration, with exponential backoff and full jitter. 0 disables retry.
--retry-base-delay N5Base seconds for the backoff. 0 waits not at all.
--retry-max-delay N60Cap for any single backoff wait, in seconds.
--escalate-after N2Consecutive no-progress iterations before escalating to the main agent.
--downgrade-after N5Consecutive progressing iterations before dropping back to the frugal agent.

Telemetry and cost

FlagDefaultDescription
--metrics FILE.wgm/metrics.tsvPer-iteration TSV ledger. Pass --metrics off to disable.
--cost-cmd "CMD"noneRun after each iteration to print a token or cost figure for the cost column. Best-effort; its failure never breaks the loop.

The ledger's columns are start_timestamp, end_timestamp, iter, mode, agent, duration_s, plan_changed, result (ok, fail, or stall), cost, parent. $WGM_PARENT_TASK populates parent, which swarm.sh sets per lane. See Telemetry for what the numbers mean and, importantly, what they do not.

Project gates

FlagDefaultDescription
--gates FILEauto-detect wgm.yml or .wgm/gates.ymlA YAML file with a gates: list of commands executed by the host after each build iteration.

The gate file accepts a block list (gates: followed by - command) or a simple inline list (gates: [command-a, command-b]). Use the block form when a command contains a comma or needs shell quoting; commands are trusted shell text and execute in the target project directory.

Execution

FlagDefaultDescription
--commitoffCommit after each build iteration. Requires a clean baseline, exclusive worktree ownership, and an iteration path manifest.
--checkpoint-interval N0 (off)Commit every N build iterations, independent of --commit.
--devcontaineroffRun the entire invocation inside wgm's shared local sandbox. A no-op with --dry-run.
--devcontainer-mount HOST[:CONTAINER]noneRepeatable credential/config bind mount forwarded to the sandbox; requires --devcontainer.
--notify "CMD"noneRun CMD on lifecycle events with $WGM_EVENT (start, complete, error, retry) and $WGM_ITER set. Best-effort.
--dry-runoffPrint the prompt and the command that would run; invoke nothing, including the capability probe.
-h, --helpShow usage.

Commit checkpoints use the same clean-baseline and ownership-manifest rules as --commit; a missing manifest or undeclared path fails closed instead of being swept into the checkpoint.

Environment variables

VariableUsed byDescription
WGM_AGENTloop.sh, swarm.shDefault agent command (shell-evaluated).
WGM_FRUGAL_AGENTloop.shDefault frugal agent command.
WGM_PROMPT_STDINloop.shSet to 1 if the agent reads its prompt from stdin.
WGM_PARENT_TASKloop.shPopulates the ledger's parent column. Set per lane by swarm.sh.
WGM_CAPABILITY_PROBE_FILEagent during probeUnique disposable path the agent must create; unset outside the probe. The probe is mandatory for non-dry-run build and plan.
WGM_CAPABILITY_PROBE_CONTENTagent during probeExact marker content expected in the probe file.
WGM_OWNERSHIP_MANIFESTagent in commit modePath where the agent declares intentionally changed repository-relative files, one per line.
WGM_EVENT, WGM_ITER--notify CMDLifecycle event name and current iteration number.
WGM_IN_DEVCONTAINERdevcontainer.shSet inside the sandbox; prevents recursive re-exec.

Exit codes

CodeMeaning
0The loop finished: iterations exhausted, a stop condition fired, or the stop sentinel appeared.
1A capability probe, phase-artifact, project-gate, timeout, no-progress, ownership, single-phase, or circuit-breaker check failed; or build/review/preflight ran with no IMPLEMENTATION_PLAN.md.
2Misconfiguration: unknown flag, non-numeric knob value, malformed/unreadable gate file, unavailable explicit container engine, missing --gates file, or no agent configured.

Stopping a run

To stop a running loop, use whichever is convenient:

  • Press Ctrl+C.
  • Create the stop sentinel: touch .wgm/STOP (or ./STOP when there is no .wgm/ directory). The loop ends after the current iteration finishes.

Note: In build mode the agent is instructed to create that sentinel itself once no must-have task remains, so a healthy loop self-terminates without your intervention.

Safety

  • Non-destructive by default: no commits and no pushes unless you pass --commit.
  • The agent still edits files during a non-dry run. Run this only in a workspace you are willing to let an agent operate in autonomously.
  • With --commit, do not edit the worktree while the loop runs. Use another worktree for concurrent human edits; undeclared paths make the commit fail closed.
  • build, review, and preflight refuse to start without an IMPLEMENTATION_PLAN.md in the project root or .wgm/.

What to do next