gptme-runloops

v0.1.0 Python-based run loop framework for autonomous AI agent operation packages/gptme-runloops View on GitHub

gptme-runloops

A run-loop framework for operating AI agents autonomously on a schedule: lock-protected autonomous sessions, GitHub PR/issue monitoring, email handling and team coordination, executed on interchangeable backends (gptme, Claude Code, Codex, Grok Build), plus a one-shot, resumable run command for any backend.

Status: experimental, in production use by the gptme agent fleet. The generic pieces (run, select, the backend executors, BaseRunLoop, review) are reusable as-is; the autonomous, monitoring, run-item and team loops assume a gptme-agent-template-style workspace and are more opinionated.

Why / when to use it

An agent that runs unattended needs more than a cron line: it needs a lock so runs don't overlap, backoff when the same work keeps failing, a git pull before starting, structured logs, and a way to swap the underlying agent CLI without rewriting the loop. gptme-runloops provides those as a BaseRunLoop class and a CLI you schedule with cron or a systemd timer.

It composes with other contrib packages — use them, or bring your own task and communication systems:

Install

Not published on PyPI. Install from the repository:

uv tool install --with "git+https://github.com/gptme/gptme-contrib#subdirectory=packages/gptme-sessions" "git+https://github.com/gptme/gptme-contrib#subdirectory=packages/gptme-runloops"
# or, from a clone of gptme-contrib:
uv pip install -e packages/gptme-sessions -e packages/gptme-runloops

The backend CLIs (gptme, claude, codex, grok) and gh are external and must be on PATH for the backends/loops that use them. The console script is gptme-runloops (run-loops is a deprecated alias).

Safety: the loops run their backend non-interactively with permission prompts bypassed (e.g. Claude Code with --dangerously-skip-permissions). Run them under an account and in a workspace where that is acceptable.

Quickstart: one-shot runs on any backend

# Run one prompt; prints one JSON line: backend, model, session_id, result,
# exit_code, is_error, resumed, timed_out, cost_usd, agent_output
gptme-runloops run --workspace . --backend claude-code --allowed-tool Read "summarise TODO.md"

# Continue that session (same backend + model)
gptme-runloops run --workspace . --backend claude-code --model <model> --resume <session_id> "and now?"

Resume mapping: claude-code --resume <id>, codex codex exec resume <id>, gptme --name <id>. Backends without resume support (grok-build) fail with a usage error rather than silently starting fresh. A rejected resume id is an error unless --on-resume-failure fresh opts into a new session (optionally with --fallback-prompt-file to carry context over). Tool restriction is backend-native: --allowed-tool (claude-code --allowedTools, gptme --tools) or --sandbox (codex). --report (claude-code only) adds report_text, every assistant text turn joined: result is only the final message, which a delegated run can reduce to a closing line ("report above"). Other options: --prompt-file, --timeout (default 1800 s); --backend defaults to $AGENT_BACKEND, else gptme. Exit code is 0 on success, 1 on an error result, 2 on usage errors.

Commands

Command Description
run [PROMPT] One prompt on any backend, JSON result (see above)
select [--config PATH] [--state-dir DIR] [--json] Print the first viable backend/model from harness-quota.toml, skipping candidates whose binary is missing or that are blocked in the block registry. Exit 1 if none
select --lane LANE [...] One routing decision (backend, model, effort) from the lane policy; see Lane policy
autonomous One autonomous session. Prompt from scripts/runs/autonomous/autonomous-prompt.txt in the workspace, else a built-in fallback
monitoring [--org ORG]... [--repo OWNER/REPO]... [--author LOGIN] [--agent-name NAME] GitHub project monitoring: find PRs/issues needing action (CI failures, review comments, merge eligibility) and act on them. Defaults to the claude-code backend
run-item --workspace DIR [--work-file F] ... Execute a single project-monitoring work item (--dry-run prints the execution plan)
email Sync mail (mbsync -a), then answer unreplied messages using gptmail
team [--tools LIST] Coordinator agent with restricted tools that delegates work to subagents via gptodo. The restriction is only enforced on the gptme backend: claude-code, codex and grok-build log a warning and run with their normal tools
review [--working-tree | --base SHA --head SHA] [--output F] [--model M] Legacy: LLM review of a local diff, emitted as ReviewArtifact JSON (no forge access needed). Production local-diff review is scripts/github/ai-review-local.py in the agent workspace
review-pr OWNER/REPO#NUM [--shadow|--publish] [--min-confidence X] Retired path. pm-run-item-slot.sh refuses to route a PR here. Production GitHub-posting review is the separate gptme-review package (gptme_review.core + writer)

autonomous, email, team and monitoring accept --workspace (default: current directory), --model, --tool-format markdown|xml|tool and --backend.

harness-quota.toml (default ~/.config/gptme/harness-quota.toml):

[[candidates]]
backend  = "claude-code"
model    = "claude-sonnet-4-6"
priority = 1

[[candidates]]
backend  = "gptme"
model    = "openrouter/deepseek/deepseek-chat-v3-0324:free"
priority = 2

Lane policy: select --lane

With --lane, select reads a lane policy from the same harness-quota.toml and returns one decision: backend, model, reasoning effort, scope, the single rule that decided, a decision id and the pool reading it used. Effort steps down under weekly pool pressure, a fallback list covers blocked or excluded arms, and the command never takes the agent down because of its own failure. Without --lane the [[candidates]] behaviour above is unchanged.

A single-lane agent:

[routing]
schema_version = 1

[pools.claude-max]                       # provider = "claude-usage" (default)
no_capacity_at = 0.90                    # weekly use at or above this: do not launch
effort_steps = [{ at = 0.60, down = 1 }, { at = 0.85, to = "low" }]
pace_guard = 0.10                        # >10 points ahead of even pace: one step down

[lanes.autonomous]
arm = "claude-code:claude-opus-5-5"      # backend:model, explicit model id
effort = "high"
effort_ladder = ["high", "medium", "low"]
pool = "claude-max"
fallback = [{ arm = "gptme:gemini/gemini-flash-latest", scope = "lights-on", on = ["auth", "blocked"] }]
gptme-runloops select --lane autonomous [--class C] [--work-item ID] \
    [--exclude BACKEND:MODEL --reason rate_limited|auth|crash]... \
    [--default-arm BACKEND:MODEL --default-effort E] \
    [--shadow] [--format json|shell] [--explain] [--validate] \
    [--config PATH] [--state-dir DIR] [--log PATH]
Table / key Meaning
routing.schema_version Required, must be 1
pools.<p>.provider Usage reader: claude-usage (default) or none
pools.<p>.usage_cache claude-usage: cache file of scripts/check-claude-usage.sh (default /tmp/claude-usage-cache.json)
pools.<p>.usage_command claude-usage: run this instead (list or string), e.g. ["bash", "/path/check-claude-usage.sh", "--json"]. It runs on the launch path (a fresh scrape can take ~25 s); reading the cache is the cheap route. The reading's age is still the usage_cache file's mtime (missing cache = age 0), so point both keys at the same cache
pools.<p>.usage_max_age_s / usage_timeout_s Older readings count as unknown (default 1800) / command timeout (default 15; a timeout reads as unknown)
pools.<p>.no_capacity_at Weekly use at or above this: rule no_capacity unless a fallback applies
pools.<p>.owner_reserve Share kept for the owner; sets the ceiling to 1 - owner_reserve when no_capacity_at is unset
pools.<p>.effort_steps {at, down = n} or {at, to = effort}; the highest reached step applies (no stacking); --validate warns when a later step resolves to a higher effort than an earlier one
pools.<p>.pace_guard More than this ahead of even pace forces at least one step down (max with the step, not sum)
pools.<p>.usage_unknown Effort when usage is unreadable: base (default) or an effort on the ladder
pools.<p>.max_width Enables the width field (shadow, logged and ROUTE_WIDTH): sessions this consumer may run on the pool's account. Target is linear, end_target × elapsed; behind it, width scales from 1 to max_width over a 10-point gap. None = no opinion (usage/reset unknown, or the owner on pace)
pools.<p>.end_target Weekly fraction to reach by reset (default 0.98); set lower for a measured owner reserve
pools.<p>.rank This consumer's rank on the account: 0 = owner (default), 1+ = overflow. Rank r widens only when more than r × 0.02 behind, so the owner absorbs its account first
lanes.<l>.arm backend:model; bare aliases (opus, sonnet, haiku, fable) are rejected
lanes.<l>.effort / effort_ladder Base effort and its ladder, highest first; omit both for backends without effort
lanes.<l>.pool Pool this lane draws from (optional)
lanes.<l>.expires After this date the row is still used, and decisions carry policy_expired: true
lanes.<l>.scope Scope reported for the lane arm (default none)
lanes.<l>.fallback Ordered {arm, scope, on, effort}; on lists triggers (rate_limited, auth, crash, blocked, missing, no_capacity); omitted on matches any
[lanes.<l>.<class>] Per-class overrides; missing keys and missing classes fall back to the lane row

Rules, first that applies: excluded_fallback (lane arm passed in --exclude; first fallback whose on matches the reason), lane (lane arm at base effort), lane_pressure (lane arm at a stepped-down effort), fallback (lane arm blocked in the block registry, binary missing, or pool at no_capacity_at), no_capacity (pool at its ceiling and no fallback applies), no_arm (lane arm excluded, blocked or missing and no fallback applies), fail_open. A --reason outside the trigger vocabulary is treated as unspecified (only fallbacks without on match it), with a note in reason.

Exit Rule Caller should
0 lane, lane_pressure, fallback, excluded_fallback, fail_open with --default-arm Run ROUTE_*
76 no_capacity Skip this run (quota)
69 no_arm Run its default and report an outage; not a quota skip
2 fail_open without --default-arm Run its default
other (click-level failure, see below) Run its default

--shadow always exits 0. Fail open: an unreadable or invalid config, unknown schema version, unknown lane or unexpected exception yields rule fail_open for --default-arm / --default-effort (exit 0). A failing, slow or stale usage reading counts as unknown: base effort, never no_capacity. Errors in one lane's row do not affect other lanes.

The JSON decision has backend, model, effort, scope, rule, trigger (why the lane arm was not used: an exclusion reason, blocked, missing or no_capacity; null when it was used), reason, lane, class, work_item, decision_id (uuid4), policy_version (12-hex hash of the routing tables), policy_expired, shadow, pool (name, used, pace_gap, age_s, source; pace_gap > 0 means ahead of even pace) and skipped. Every decision, shadow ones included, is appended to <state-dir>/routing/decisions.jsonl (or --log); a logging failure is only a warning. --format shell prints quoted ROUTE_BACKEND, ROUTE_MODEL, ROUTE_EFFORT, ROUTE_SCOPE, ROUTE_RULE, ROUTE_TRIGGER, ROUTE_REASON and ROUTE_DECISION_ID lines for eval; --explain writes a trace to stderr; --validate lists errors and warnings and exits 1 only for errors. Record the decision id with the session (for example in its dispatch_cause) to link sessions to decisions.

Claude usage caches expose _pacing.pace_gap_used_minus_target with the same sign as the selector and gptme_subscription.routing: positive means overusing, negative means underusing. The selector reads that field directly, including when gptme-subscription is not installed. The parser's older _pacing.pace_gap is deprecated and retains its opposite sign for existing callers (including the legacy utilization-bypass gate and quota-gate log). Old caches remain readable through the selector's sign-converting fallback; an unknown reset still yields unknown pacing.

Treat any exit other than 0, 76 and 69 as "run your default": click-level failures (an older installed version without --lane, or --config pointing at a directory) exit 2 with empty stdout and bypass both --default-arm and --shadow. Capture the exit code without tripping set -e, and clear ROUTE_* before eval so a loop cannot reuse a previous iteration's values:

unset ROUTE_BACKEND ROUTE_MODEL ROUTE_EFFORT ROUTE_SCOPE ROUTE_RULE ROUTE_TRIGGER ROUTE_WIDTH \
      ROUTE_REASON ROUTE_DECISION_ID
rc=0
out=$(gptme-runloops select --lane autonomous --format shell \
      --default-arm claude-code:claude-opus-5-5 --default-effort high) || rc=$?
case "$rc" in
  0)  eval "$out" ;;                                   # sets ROUTE_* variables
  76) echo "no capacity, skipping"; exit 0 ;;          # quota skip
  69) echo "routing: no arm available, running default" >&2 ;;  # outage, not quota
  *)  echo "routing failed (rc=$rc), running default" >&2 ;;
esac
BACKEND="${ROUTE_BACKEND:-claude-code}" MODEL="${ROUTE_MODEL:-claude-opus-5-5}"
EFFORT="${ROUTE_EFFORT-high}"   # set but empty: the routed backend has no effort setting

Run shadow calls as gptme-runloops select --lane autonomous --shadow ... >/dev/null || true.

Dispatcher tick coverage

A dispatcher (fanout timer, worker sweep) can append one row per fire with gptme_runloops.dispatch_ticks.append_tick(path, granted=..., bindable=..., pacing=...). granted is the slot width after the resource/quota gate, bindable the work items it could bind (null = not computed) and pacing the account's position against its usage target. Then:

gptme-runloops tick-coverage state/dispatch-ticks.jsonl --since-hours 168

prints the share of behind ticks where bindable >= granted, plus counts of the ticks it could not judge (unknown pacing, unknown bindable, no capacity). The target is granted, not the slots the dispatcher requested: covering a one-slot request while five granted slots sit idle is the starvation this measures.

Review artifact output

Retirement note (2026-11-26): review-pr no longer routes a PR (see the command table above), and review is legacy. The wire format below still describes what review --output writes and what existing consumers can read; it is not the output of the live review engines (gptme-review, or scripts/github/ai-review-local.py in the agent workspace).

review and review-pr now write canonical core v2 JSON, both on stdout and with --output. Consumers can load it directly with gptme.util.review.ReviewArtifact.from_json(); no forge-neutral reader is needed for these new outputs. Local reviews use PR number zero. Unsafe or unknown merge verdicts still exit nonzero, even when the artifact was written successfully.

The internal Python pr_review.schema.ReviewArtifact is a v1 Pydantic model and stays one: this package deliberately has no gptme dependency, so it cannot import core's dataclass, and the public contract is the v2 wire format, not the in-memory type. Its model_dump_json() still serializes the internal shape; use pr_review.core_compat.artifact_json() or save_artifact() for the public wire format. Consumers previously using schema.ReviewArtifact.model_validate_json() on CLI output must migrate to the core reader (or read the v2 dictionary).

V2 output retains actionable severity, finding identity (independence_key), category, open/dropped status, merge safety, target kind, diff fingerprint, the reviewed base/head SHAs (a working-tree review has no head), model, reviewed_at, review_duration_s, and per-finding evidence (quote) and fix hint (suggestion). Summary, prompt version and confidence are not in the wire format; the returned Python object still carries them. A confirmed defect stays status: "open"; core's "confirmed" means a verified fix.

Python API

from pathlib import Path
from gptme_runloops import AutonomousRun, get_executor, list_backends

print(list_backends())  # ['claude-code', 'codex', 'gptme', 'grok-build']
run = AutonomousRun(Path("."), executor=get_executor("claude-code"))
exit_code = run.run()

Write your own loop by subclassing BaseRunLoop and overriding generate_prompt() (plus optionally has_work(), pre_run(), post_run()). The base class provides the lock (in <workspace>/logs/), git pull with retry, timeout handling, logging, and opt-in consecutive-failure backoff.

Other modules:

The legacy import path run_loops is kept as a symlink to gptme_runloops in source checkouts.

Runtime-admission gates: the state-delta probe

state_delta_gate decides whether an already-triggered session that follows a noop-only streak deserves to run. Branches, in evaluation order: inactive → quota pace → addressed signal → wall-clock backoff → legacy change probe → epsilon explore → skip. GateDecision.cause names the branch that decided it.

New callers should drive throughput from the two middle probes and omit changes_detected. “Any commit since the last session” does not discriminate productive runs (45–48% productive whether a real commit, only timer commits, or nothing opened the gate, versus 64% when the gate was inactive — gptme/gptme-contrib#2089), so it works as a throttle whose rate depends on how often unrelated lanes commit.

Explicit runner environment (PM Python API)

gptme_runloops.run_item.execute_plan(..., runner_base_env=...) optionally replaces the runner subprocess's inherited environment:

Plan metadata includes AGENT_SESSION_ID, BOB_SESSION_ID, and applicable backend aliases (CC_SESSION_ID / GROK_BUILD_SESSION_ID). The executor always sets BOB_AUTONOMOUS_AGENT_ID and BOB_SESSION_PID last. Callers opting in must explicitly supply any needed credentials, PATH, HOME, backend configuration, and other execution settings; ambient_env cannot add them back in this mode. No environment values are logged by this API.

This is an opt-in Python API boundary for the runner only, not a sandbox or a fleet rollout. Lifecycle, GitHub observation, system-prompt, and post-run hooks are not isolated by this argument.

Runner environment contract (config.runner_env)

run_work_file adopts the boundary from a declarative TOML table (gptme_runloops.runner_env builds it; the module has no I/O):

[config.runner_env]
mode = "explicit"            # or "legacy" (inherit os.environ unchanged)
passthrough = ["PATH", "HOME"]
prefixes = ["LC_"]
credentials = ["OPENROUTER_API_KEY"]  # omit for the backend default
set = { BOB_BACKEND = "gptme" }
require = ["PATH", "HOME"]
disable_dotenv = true

Semantics:

Credentials stored in files under HOME remain readable to the same UID: this boundary removes unrelated environment authority, not filesystem authority.

Contributor initial reviews (PM)

contributor_unreviewed is a slow-lane pr-review session, never an automated-pr-review or code-repair session. RunItem normalizes mixed groups to this type before merge-lifecycle I/O; the shared coordination claim is github:OWNER/REPO#NUMBER. The dedicated prompt replaces generic monitoring, repair, arc and mention instructions. It requires live PR/head/CI/managed-review checks, detached isolated-worktree inspection, safe test execution, touched and adjacent regression suites, and a final head/eligibility recheck. The session submits one review with explicit commit_id: approval only after meaningful green verification, actionable changes requested for defects, or a comment review for infrastructure/verification limits. It never edits or pushes the contributor branch, opens a parallel fix, or merges.

The legacy ProjectMonitoringRun discovery loop also uses this shared review-only prompt. It executes each contributor review in a separate session, excludes those PRs (including other event types for the same PR) from its generic repair batch, and retains unrelated repair work. At discovery, existing notifications/activity for a PR take priority over its initial-review candidate, so consumed legacy watermarks never hide an unanswered request behind a review-only session. Per-session recording retains each session's own outcome; a separate aggregate result reports batch failure. Candidate enumeration paginates all open REST pull-request pages before filtering, so newer managed PRs cannot hide older contributors behind a fixed listing limit. CI is fetched only for age/author eligible candidates and must match the enumerated head. Timed-out CI or review lookups skip that candidate without abandoning later PRs in the repository scan. The legacy detector includes the configured author in its managed-login set for both authorship and current-head review checks, and carries that set into the review-only prompt.

The detector owns age/author/green-CI eligibility and full-head-SHA dedupe; its managed-login configuration must be carried as exactly one managed_logins=Login1,Login2 token in item detail. The handler renders quoted JSON evidence and adds the authenticated viewer; user logins and GitHub App [bot] logins are accepted. Missing, duplicate or malformed tokens fail closed, and display/operator names are never inferred as logins. CONTRIBUTOR_REVIEW_ONLY_V1 in run_item.py marks the safe executor capability, not deployment of a shell fallback. Enable the detector only after the actual executor and any shell adapters support the same policy. Shell deployments must normalize mixed types before cheap self-merge pre-gates, lifecycle, prompt concatenation, coordination claims and post-session wait/merge. Do not route this type through the retired review-pr intercept.

PR state is observed, but movement or issue comments do not count as a review. worker_records.viewer_review_since(repo, number, since_iso, *, cwd, runner=...) returns True only for a newly submitted authenticated-viewer review on the live head (APPROVED, CHANGES_REQUESTED or COMMENTED), False if absent, and None if unobservable. It paginates reviews, checks head stability between pages, and is also suitable for a shell adapter. Review-only post-session effect uses this evidence instead of generic reply checks or fallback comments. It does not promote pending PR activity or notification watermarks (including immutable launch snapshots): co-grouped work suppressed by the review-only prompt must remain eligible for a later handling session. The default launcher preserves item detail, including the required managed-login evidence. Review-only sessions also skip generic notification-handling latency receipts and PR-movement deliverable attribution: neither a suppressed mention nor a contributor's concurrent commit is work delivered by the reviewer. If a review is absent or unobservable (including a failed worker), launch event fingerprints are cleared for retry without consuming suppressed watermarks. A confirmed prompt-mandated skip (missing/invalid managed-login evidence, draft, closed/merged, managed-author, non-green CI, or a managed review already on the live head) is graded observed so recovery does not drain the ineffective budget, and fingerprints are still cleared so other slot types and later eligibility are not suppressed. Head movement is not a skip: the new head remains eligible and retries. contributor_review_skip_observed is the observation helper for that distinction; an empty parsed managed-login set proves the local fail-closed skip without a GitHub request. Dispatch ledger types likewise reflect only the review; mixed inputs remain available as per-item original_types, never credited as completed repairs.

Failed execution and notification delivery

For thread-deliverable items with a configured delivery checker, a nonzero worker exit without a verified handled reply or posted fallback reply leaves notification watermarks unconsumed. This includes immutable launch snapshots. The executor clears launch-event fingerprints and purges owned pending notifications so end-of-run blanket promotion cannot consume them. Valid PR and master-CI observations still promote independently of reply delivery.

Execution failures do not spend the no-reply redelivery budget: the dispatcher's existing failure/infra backoff, finite retry budget and visible escalation own execution recovery. The caller must retain that recovery policy; this change is not an unbounded automatic retry loop. Successful executions with unverified or orphan delivery retain the existing delivery cap. A verified reply before a later process failure still promotes notifications to prevent duplicates. Without a delivery hook, historical compatibility behavior is unchanged; no new claim of observed delivery is implied. Review-only suppressed work retains its separate non-promotion contract above.

Hang watchdog for claude -p

Headless claude -p --output-format stream-json sessions can hang: the process stays alive but emits no events, or never exits after its final result event. gptme-claude-watchdog wraps the session, relays its stdout unchanged and stops it early:

gptme-claude-watchdog --verdict-file verdict.jsonl -- \
    claude -p "$PROMPT" --output-format stream-json --verbose
Trip When Exit
idle no event for --idle-free s (default 120) and no tool call pending 75
tool_pending no event for --idle-tooled s (default 1800) while a tool call is pending 75
post_result result seen, process still running after --grace s (default 30) 0, or 1 if the result was an error
eof_no_exit stdout closed, process still running after --grace s 75

Tool calls are tracked by id (tool_use → matching tool_result), so a subagent's inner tool results cannot clear the parent's pending call. On a trip the session's process group gets SIGTERM, then SIGKILL after --kill-after s (default 5). Otherwise the exit code is the session's own; if the watchdog itself receives SIGTERM/SIGINT/SIGHUP it stops the session and exits 128 + signal. Defaults can also be set with AGENT_CLAUDE_WATCHDOG_IDLE_FREE, _IDLE_TOOLED, _GRACE and _KILL_AFTER.

Every run ends with one JSON verdict line (appended to --verdict-file, and on stderr after [claude-watchdog] verdict), with outcome (ok, error, hang or interrupted), trip, reason, exit_code and any pending tool calls.

The module is stdlib-only, so it also runs without installing the package: python3 .../gptme_runloops/claude_watchdog.py -- claude -p ....

In the shared runner. scripts/runs/autonomous/autonomous-run-cc.sh uses the watchdog only when AGENT_CLAUDE_WATCHDOG=1 (or true/yes). It then runs the session with --output-format stream-json --verbose under the watchdog (still inside the outer timeout), logs the verdict as Watchdog verdict: {...}, and logs Session outcome: hang and exits 75 when the watchdog stopped a hung session. AGENT_CLAUDE_WATCHDOG_CMD overrides the command (default gptme-claude-watchdog); the run fails with exit 2 if it is not found. With the variable unset, the runner behaves as before.

Development

cd packages/gptme-runloops
make test        # pytest with pytest-mock
make typecheck

Running uv run pytest packages/gptme-runloops/tests/ from the repository root fails with No module named gptme_runloops, because the package is not installed into the root environment. gptme-sessions>=0.2.0 is a declared runtime dependency: PM routing requires its shared bandit serialization and lock-path hooks. When installing from source outside the workspace, install the matching packages/gptme-sessions alongside packages/gptme-runloops as shown above.

License

MIT