1---2name: resumable-execution3description: Execute multi-stage plans with durable state, crash-safe resume, and idempotent closeout.4---56## What I do78- Provide a reusable workflow for long-running or high-risk tasks that may span multiple sessions.9- Enforce a transaction-like model: immutable contract, stage gates, atomic state persistence, resume reconciliation, and idempotent finalization.10- Reduce drift during multi-phase execution by requiring explicit completion checks and artifact capture.1112## When to use me1314- Multi-stage plans where interruption is likely.15- Tasks with strict stop conditions and non-trivial verification gates.16- Any run where progress must be recoverable without redoing completed steps.1718## Run layout1920- Create one run root per execution:21 - `$OPENCODE_CONFIG_DIR/reports/<task>-<run-id>/`22- Keep durable files inside the run root:23 - `state.env` (machine state)24 - `checklist.state.env` (required child checks)25 - `run.log` (human-readable timeline)26 - `artifacts/` (reports, diffs, evidence)27- Keep temporary files under `$OPENCODE_CONFIG_DIR/`, never in the repo.2829## Starter templates3031- Bootstrap run files from:32 - `$OPENCODE_CONFIG_DIR/skills/resumable-execution/run-state.template.env`33 - `$OPENCODE_CONFIG_DIR/skills/resumable-execution/checklist-state.template.env`34- Fill immutable contract keys before any mutating action.35- Add task-specific `CB_<STAGE>_<ITEM>` keys before executing each stage.36- Keep keys explicit (`0` or `1`); never infer completion from missing keys.3738## Quick bootstrap (3 commands)3940```bash41TASK=<task>42RUN_ID=$(date -u +%Y%m%dT%H%M%SZ)43RUN_ROOT="$OPENCODE_CONFIG_DIR/reports/${TASK}-${RUN_ID}"44mkdir -p "$RUN_ROOT/artifacts" && cp "$OPENCODE_CONFIG_DIR/skills/resumable-execution/run-state.template.env" "$RUN_ROOT/state.env" && cp "$OPENCODE_CONFIG_DIR/skills/resumable-execution/checklist-state.template.env" "$RUN_ROOT/checklist.state.env"45```4647- In `state.env`, set at minimum: `RUN_ID`, `TASK_NAME`, `RUN_ROOT`, `REPO_ROOT`, `PLAN_PATH`, `ARTIFACT_ROOT`, and `LOCK_FILE`.48- In `checklist.state.env`, add required task-specific gates for each stage.4950## Single-writer discipline5152- Use a lock file with `pid`, `host`, `user`, and timestamp metadata.53- If lock is active and valid, stop immediately.54- If lock is stale, append lock contents to `run.log`, then replace lock.55- Remove lock on normal and failure exit.5657## Immutable contract first5859- Persist immutable intent before stage mutations:60 - repo root, branch, base pin, expected old SHA, plan path, and report targets61- Validate immutable contract on resume.62- If immutable values drift, stop and start a new run.6364## Atomic state persistence6566- Never edit state files in place.67- Write to `*.tmp`, parse-check, then atomically rename.68- Track both stage state and checklist state.69- Use explicit schema keys:70 - `STATE_SCHEMA_VERSION`71 - `CHECKLIST_SCHEMA_VERSION`7273## Stage and checklist gates7475- Maintain one stage pointer (`CURRENT_STAGE`) and per-stage done flags (`S*_DONE=1`).76- Mirror required child checks with explicit keys (`CB_<STAGE>_<ITEM>=1`).77- A stage is complete only when all required child checks are complete.78- Forbid setting `S*_DONE=1` when required checklist keys are incomplete.7980## Crash-resume reconciliation8182- Define resume windows before execution starts:83 - before mutation84 - after mutation, before verification85 - push attempted with unknown outcome86 - push confirmed, finalization incomplete87- Reconcile by observing real state (HEAD, remote SHA, artifacts), then continue from the earliest incomplete safe stage.88- Never rerun completed destructive stages unless reconciliation proves it is necessary.8990## Idempotent finalization9192- Finalization updates are post-confirmation only.93- Append summary blocks with a unique run marker (`RUN_ID`) and skip duplicates on resume.94- Track finalization flags explicitly:95 - `CANONICAL_STATE_UPDATED=1`96 - `REPORT_APPENDED=1`97 - `RUN_SUMMARY_UPDATED=1`9899## Mandatory stop conditions100101- Dirty worktree at a mutation boundary.102- Remote drift from expected lease target.103- Missing or unparsable state/checklist files.104- Stage marked done while required checklist keys are incomplete.105- Missing required artifacts for the current stage gate.106107## Recommended execution flow1081091. Bootstrap run root and acquire lock.1102. Initialize or resume durable state.1113. Validate immutable contract and stop conditions.1124. Execute stage-by-stage with checklist gates.1135. Verify outputs and capture artifacts.1146. Push with explicit lease where required.1157. Finalize idempotently and release lock.