Bash Runner Modules¶
This document describes the bash runner system that orchestrates GitTinkerer. It covers execution flow, module responsibilities, environment variables, payload parsing, and failure artifacts.
Flow of Modules¶
graph TD
A[CLI args
lib/args.sh] --> B[Env + secrets
lib/env.sh]
B --> C[Payload parse
lib/payload.sh]
C --> D[Artifacts init
lib/artifacts.sh]
D --> E[Workspace prep
lib/workspace.sh]
E --> F[Codex run
lib/codex.sh]
F --> G[Diff/commit/push
lib/git.sh]
F --> H[Metrics
lib/metrics.sh]
G --> H
F --> I[Reply selector]
G --> I
I --> J[PR reply
lib/reply/github.sh]
I --> K[Web reply
lib/reply/web.sh]
B --> L[Logging
lib/log.sh]
B --> M[Sentry
lib/sentry.sh]
L -. errors .-> D
M -. optional alerts .-> D
Run Lifecycle (sequence)¶
sequenceDiagram
participant CLI as bin/gittinkerer
participant Args as args.sh
participant Env as env.sh
participant Payload as payload.sh
participant Art as artifacts.sh
participant WS as workspace.sh
participant Codex as codex.sh
participant Git as git.sh
participant Reply as reply (github/web)
participant Metrics as metrics.sh
participant Sentry as sentry.sh
participant Log as log.sh
CLI->>Args: parse_args()
Args->>Env: load_env()
Env-->>Log: logging configured
Env-->>Sentry: DSN (optional)
Env->>Payload: parse_payload()
Payload-->>Metrics: PAYLOAD_RUN_ID
Payload->>Art: init_artifacts()
Art->>WS: workspace_dir prepared
WS->>Codex: run_codex_operations()
Codex->>Metrics: record_token/seconds
Codex->>Git: run_git_operations()
Git->>Reply: publish reply (PR/web)
Reply-->>Metrics: flush_metrics()
Git-->>Sentry: on push/commit failures
Note over Art,Reply: write_failure_artifacts on any stage error
Module Reference¶
- lib/args.sh: CLI entry; enforces
runcommand, required--payload-file, optional--workspaces-dir,--artifacts-dir,--timestamp,--dry-run. - lib/env.sh: Loads
.env, setsREPO_ROOT, default git identity, normalizesARTIFACTS_DIR/WORKSPACES_DIR, requiresGITHUB_TOKEN. - lib/payload.sh: Validates JSON schema (
sourcein {pr,web}); extracts repo/PR/web/run fields; exportsPAYLOAD_*variables using node helpers. - lib/artifacts.sh: Creates run folder at
ARTIFACTS_DIR/<timestamp>/agent-run; seedsprompt.txt,pr_comment.txt,diff.patch,files_changed.json,commit_sha.txt,summary.md. - lib/workspace.sh: Validates clone URL, clones or fetches repo into
WORKSPACES_DIR/<safe_name>, checks out/pulls target ref (PAYLOAD_PR_HEAD_REFormain). - lib/codex.sh: Builds prompt from comment + repo + contract; runs
codex exec --sandbox danger-full-access; writessummary.md; records prompt tokens and completion seconds. - lib/git.sh: Generates
diff.patchandfiles_changed.json, computesDIFF_LOC, commits with truncated comment-derived message, pushes toPAYLOAD_PR_HEAD_REFormainunlessDRY_RUN. - lib/metrics.sh: Estimates prompt tokens, calculates diff LOC, queues metrics and POSTs to
$SERVICE_URL/api/runs/$PAYLOAD_RUN_ID/metricswithRUNNER_METRICS_TOKEN. - lib/sentry.sh: Builds Sentry store URL from
SENTRY_DSN; sends events tagged with run/repo/PR/source/stage. - lib/log.sh: Sanitizes secrets; emits timestamped INFO/WARN/ERROR/DEBUG; forwards ERRORs to Sentry when configured.
- lib/reply/github.sh: Posts
CODEX_RESPONSEas PR comment (skips onDRY_RUN); targetsissues/<PR>/commentsendpoint. - lib/reply/web.sh: Sends
CODEX_RESPONSEto$SERVICE_URL/api/runs/$PAYLOAD_RUN_ID/reply(skips onDRY_RUN).
Environment Variables (concise)¶
| Module | Inputs (expected) | Outputs/Exports |
|---|---|---|
| args | CLI flags populate PAYLOAD_FILE, WORKSPACES_DIR_ARG, ARTIFACTS_DIR_ARG, TIMESTAMP_ARG, DRY_RUN |
same exported |
| env | GITHUB_TOKEN, optional .env, ARTIFACTS_DIR_ARG, WORKSPACES_DIR_ARG |
REPO_ROOT, ARTIFACTS_DIR, WORKSPACES_DIR, git identity vars, GITHUB_TOKEN |
| payload | PAYLOAD_FILE, node |
PAYLOAD_SOURCE, PAYLOAD_REPO_FULL_NAME, PAYLOAD_REPO_CLONE_URL, PAYLOAD_COMMENT_BODY, PAYLOAD_COMMENT_RAW_BODY, PR fields, PAYLOAD_WEB_CONVERSATION_ID, PAYLOAD_RUN_ID |
| artifacts | ARTIFACTS_DIR, TIMESTAMP_ARG, PAYLOAD_COMMENT_BODY, PAYLOAD_COMMENT_RAW_BODY |
ARTIFACTS_RUN_DIR; seeded artifacts files |
| workspace | PAYLOAD_REPO_CLONE_URL, PAYLOAD_REPO_FULL_NAME, PAYLOAD_PR_HEAD_REF, WORKSPACES_DIR |
WORKSPACE_DIR |
| codex | WORKSPACE_DIR, ARTIFACTS_RUN_DIR, PAYLOAD_COMMENT_BODY, PAYLOAD_REPO_FULL_NAME |
CODEX_PROMPT, CODEX_RESPONSE, CODEX_COMPLETION_SECONDS |
| git | WORKSPACE_DIR, ARTIFACTS_RUN_DIR, PAYLOAD_COMMENT_BODY, PAYLOAD_PR_HEAD_REF, git identity vars, DRY_RUN |
DIFF_PATCH, FILES_CHANGED_JSON, DIFF_LOC, COMMIT_SHA |
| metrics | PAYLOAD_RUN_ID, RUNNER_METRICS_TOKEN, SERVICE_URL, ARTIFACTS_RUN_DIR |
publishes metrics; clears queue |
| sentry | SENTRY_DSN, optional PAYLOAD_*, USER |
sends events (no exported vars) |
| log | optional DEBUG, optional Sentry function |
sanitized log output |
| reply/github | PAYLOAD_SOURCE=pr, GITHUB_TOKEN, PAYLOAD_PR_NUMBER, PAYLOAD_REPO_FULL_NAME, CODEX_RESPONSE, DRY_RUN |
PR comment (id logged) |
| reply/web | PAYLOAD_SOURCE=web, PAYLOAD_RUN_ID, CODEX_RESPONSE, SERVICE_URL, DRY_RUN |
web reply confirmation |
Payload Parsing Examples¶
- Schema enforcement:
validate_payload_schemarequiressourcein {pr,web},repo.full_name,repo.clone_url,comment_body, pluspr.number/head_ref/head_shafor PRs orweb.web_conversation_idfor web runs (see lib/payload.sh). - Extraction pattern:
PAYLOAD_SOURCE="$(json_get "$PAYLOAD_FILE" "source")" PAYLOAD_REPO_FULL_NAME="$(json_get "$PAYLOAD_FILE" "repo.full_name")" PAYLOAD_PR_NUMBER="$(json_get "$PAYLOAD_FILE" "pr.number" 2>/dev/null || true)" PAYLOAD_WEB_CONVERSATION_ID="$(json_get "$PAYLOAD_FILE" "web.web_conversation_id" 2>/dev/null || true)" - Guard rails: exits with code 2 on missing fields/invalid JSON; exits with code 1 if node is absent; rejects empty payload files; logs validation errors.
Failure Artifacts and Error Handling¶
- Initialization:
init_artifactscreatesARTIFACTS_RUN_DIRand seedsprompt.txt,pr_comment.txt,diff.patch,files_changed.json,commit_sha.txt,summary.mdwithStatus: running(see lib/artifacts.sh). - Failure path:
write_failure_artifacts <stage> <message>sanitizes secrets, ensures core files exist, writessummary.mdwithStatus: failedandFailed stage: <stage>, then forwards the sanitized message to Sentry when configured. - Common callers: workspace validation/clone/pull, Codex invocation, diff/commit/push, replies, and any missing prerequisites call
write_failure_artifactsbefore exiting non-zero. - Logging:
log_errorredacts tokens/secrets/passwords and sends Sentry events on ERROR when DSN is set.
Notes on Replies and Metrics¶
- Reply routing:
publish_github_replyhandles PR sources;publish_web_replyhandles web sources; both honorDRY_RUNand fail via artifacts on API errors. - Metrics:
record_metricqueues numeric values;flush_metricsPOSTs them to the service usingRUNNER_METRICS_TOKEN; token or run-id missing causes a logged skip, not a hard failure.