1---2name: fls-resumable-execution3description: Execute multi-stage, high-risk plans with durable state, crash-safe resume, and rollback readiness.4---56## What I do78- Provide a reusable way of work for long-running or high-risk operations that may span multiple sessions.9- Encode a transaction-like execution model: immutable contract, stage gates, atomic state persistence, crash-resume reconciliation, and idempotent finalization.10- Reduce unsafe improvisation during rewrites, restacks, migrations, or multi-step verification runs.1112## When to use me1314- Branch rewrites, restacks, or other history-mutating operations.15- Multi-stage plans with strict invariants and explicit stop conditions.16- Any task where a session may be interrupted and must resume safely.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, manifests)27- Keep temporary files under `$OPENCODE_CONFIG_DIR/`, never in the repo.2829## Starter templates3031- Bootstrap run files from:32 - `skills/fls-resumable-execution/run-state.template.env`33 - `skills/fls-resumable-execution/checklist-state.template.env`34- Fill immutable contract keys before any mutating action.35- Add task-specific `CB_<STAGE>_<ITEM>` keys before running each stage.36- Keep keys explicit (`0` or `1`); do not infer completion from missing keys.3738## Quick bootstrap (3 commands)3940```bash41TASK=<task>; RUN_ID=$(date -u +%Y%m%dT%H%M%SZ); RUN_ROOT="$OPENCODE_CONFIG_DIR/reports/${TASK}-${RUN_ID}"42mkdir -p "$RUN_ROOT/artifacts" && cp "$OPENCODE_CONFIG_DIR/skills/fls-resumable-execution/run-state.template.env" "$RUN_ROOT/state.env" && cp "$OPENCODE_CONFIG_DIR/skills/fls-resumable-execution/checklist-state.template.env" "$RUN_ROOT/checklist.state.env"43${EDITOR:-vi} "$RUN_ROOT/state.env" "$RUN_ROOT/checklist.state.env"44```4546- In `state.env`, set at minimum: `RUN_ID`, `TASK_NAME`, `RUN_ROOT`, `REPO_ROOT`, `TARGET_BRANCH`, `BASE_PIN_SHA`, `EXPECTED_OLD_REMOTE_SHA`, `LOCK_FILE`, and `ARTIFACT_ROOT`.47- In `checklist.state.env`, add task-specific `CB_<STAGE>_<ITEM>` keys before stage execution begins.4849## Single-writer discipline5051- Use a lock file with `pid`, `host`, `user`, and timestamp metadata.52- If lock is active and valid, stop immediately.53- If lock is stale, archive lock contents to `run.log`, then replace lock.54- Remove lock on normal exit and failure exit.5556## Immutable contract first5758- Persist immutable intent before mutation starts:59 - repo root, target branch, remote, base pin, expected old SHA, required commit subjects, report paths60- Validate immutable contract at resume time.61- If immutable values drift, stop and open a new run.6263## Atomic state persistence6465- Never edit state files in place.66- Write to `*.tmp`, parse-check, then atomically rename.67- Track both stage state and checklist state.68- Use explicit schema version keys to support safe backfills:69 - `STATE_SCHEMA_VERSION`70 - `CHECKLIST_SCHEMA_VERSION`7172## Stage and checklist gates7374- Maintain one stage pointer (`CURRENT_STAGE`) and per-stage done flags (`S*_DONE=1`).75- Mirror required child checks with explicit keys (`CB_<STAGE>_<ITEM>=1`).76- A stage is complete only when all required child checks are complete.77- Forbid setting `S*_DONE=1` when required checklist keys are incomplete.7879## Mutation safety pattern8081- Before destructive steps:82 - verify clean tree and no in-progress git operations83 - fetch remotes and re-validate expected remote SHA84 - create and push rollback refs (backup branch + tag)85 - record rollback commands in `run.log`86- For rewrite pushes, use explicit lease with expected old SHA.8788## Crash-resume reconciliation8990- Define resume windows before execution starts:91 - before mutation92 - after mutation, before push93 - push attempted with unknown outcome94 - push confirmed, finalization incomplete95- Reconcile by observing real world state (local HEAD, remote SHA, artifacts), then continue from the earliest incomplete safe stage.96- Never rerun completed destructive stages unless reconciliation proves it is necessary.9798## Idempotent finalization99100- Finalization updates are post-confirmation only (for example, after push is confirmed).101- Append summary blocks with a unique run marker (`run_id`) and skip duplicates on resume.102- Mark finalization keys separately, for example:103 - `CANONICAL_STATE_UPDATED=1`104 - `REPORT_APPENDED=1`105 - `RUN_SUMMARY_UPDATED=1`106107## Mandatory stop conditions108109- Dirty worktree at a mutation boundary.110- Remote SHA drift from expected lease target.111- PR head owner/branch/SHA mismatch (when operating on a PR head).112- Missing or unparsable state/checklist files.113- Stage marked done without required checklist completion.114- Missing required artifacts for the current gate.115116## Recommended execution flow1171181. Bootstrap tools/env and acquire lock.1192. Initialize or resume durable run state.1203. Validate immutable contract and stop conditions.1214. Execute stage-by-stage with checklist gates.1225. Perform destructive step only after backup refs exist.1236. Verify outputs and capture artifacts.1247. Push with explicit lease where required.1258. Finalize idempotently and release lock.126127## References128129- `AGENTS.md`130- `skills/fls-resumable-execution/run-state.template.env`131- `skills/fls-resumable-execution/checklist-state.template.env`132- `$OPENCODE_CONFIG_DIR/plans/`133- `$OPENCODE_CONFIG_DIR/reports/`