# Autonomous Run Completion Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use /executing-plans. Steps use checkbox syntax.

**Goal:** Make failed run plans recover autonomously and land every task.

**Architecture:** Persist attempt-scoped lifecycle in existing SQLite journal. Convert Cursor terminal stream records into transport completion while retaining evidence gates. Execute resolver policy for recoverable task failures.

**Tech Stack:** Node.js, Bash, SQLite, node:test.

---

## Wave Plan

| Wave | Tasks | Files touched | Safe to parallelize? |
|---|---|---|---|
| 1 | Task 1 | `src/engine/scheduler.js`, `src/daemon.js`, lifecycle tests | single task |
| 2 | Task 2 | `src/runner.js`, runner/ship tests | single task |
| 3 | Task 3 | `src/engine/repair.js`, resolver/repair tests | single task |
| 4 | Task 4 | status/audit code and integration tests | single task |
| 5 | Task 5 | runtime build and foreground plan executions | single task |

### Task 1: Canonical Attempt Lifecycle

**Wave:** 1  
**Blocks:** Tasks 4, 5  
**Blocked by:** —

**Files:**
- Modify: `src/engine/scheduler.js`
- Modify: `src/daemon.js`
- Test: `src/test/engine-scheduler.test.js`
- Test: `src/daemon.test.js`

**Contract:** Append attempt-scoped `run.start` and `run.done` to SQLite. Derive terminal/recovery state from latest attempt, never entire history.

**Acceptance:** `node --test src/test/engine-scheduler.test.js src/daemon.test.js`

- [ ] Write failing lifecycle tests.
- [ ] Run tests and confirm expected failures.
- [ ] Implement minimal lifecycle changes.
- [ ] Run acceptance.

### Task 2: Cursor Terminal Transport Completion

**Wave:** 2  
**Blocks:** Task 5  
**Blocked by:** —

**Files:**
- Modify: `src/runner.js`
- Test: `src/test/runner-transcript.test.js`
- Test: `src/test/engine-ship.test.js`

**Contract:** Parsed Cursor terminal result closes transport after grace and reaps lingering child. Repository evidence rules remain unchanged.

**Acceptance:** `node --test src/test/runner-transcript.test.js src/test/engine-ship.test.js`

- [ ] Write hanging-terminal and zero-diff regression tests.
- [ ] Run tests and confirm expected failures.
- [ ] Implement terminal-aware process handling.
- [ ] Run acceptance.

### Task 3: Resolver Recovery Execution

**Wave:** 3  
**Blocks:** Task 5  
**Blocked by:** —

**Files:**
- Modify: `src/engine/repair.js`
- Test: `src/engine/repair.test.js`

**Contract:** `agent-idle` executes `resume`, then clean `redo-task`, then quarantine; record each repair action.

**Acceptance:** `node --test src/engine/repair.test.js src/resolver.test.js`

- [ ] Write failing recovery ladder tests.
- [ ] Run tests and confirm expected failures.
- [ ] Implement minimal resolver action execution.
- [ ] Run acceptance.

### Task 4: Accurate Status and Lifecycle Audit

**Wave:** 4  
**Blocks:** Task 5  
**Blocked by:** Task 1

**Files:**
- Modify: `src/daemon.js`
- Test: `src/daemon.test.js`

**Contract:** Report precise run outcome from lifecycle events persisted in existing SQLite run journal.

**Acceptance:** `node --test src/daemon.test.js`

- [ ] Write failing status/audit tests.
- [ ] Run tests and confirm expected failures.
- [ ] Implement status/audit behavior.
- [ ] Run acceptance.

### Task 5: Runtime Acceptance

**Wave:** 5  
**Blocks:** —  
**Blocked by:** Tasks 1–4

**Files:**
- Modify only files required by failures found in Tasks 1–4.

**Contract:** Actual launcher uses repaired code. Both failed plans run foreground with exit 0, every task committed/landed, zero quarantine/skip, no manual restart.

**Acceptance:** `make test`, then foreground `bin/runplan` executions for both plans.

- [ ] Run lint, typecheck, full tests, and production build.
- [ ] Land via `.claude/scripts/ship.sh land`.
- [ ] Run `delivery-controller-receipts` foreground to all-task success.
- [ ] Run `throughput-seams` foreground to all-task success.
