# Initialize Resume Logic & Error Handling

> **Purpose:** Handle interruptions, resume previous workflows, and route based on status.
> Executed at the end of initialization to check if a workflow can be resumed.

## Error Handling Matrix

| Error | Action |
|-------|--------|
| Config missing | Create fresh with defaults, continue |
| Directory creation fails | Check permissions, display error, EXIT |
| index.json creation fails | Validate ba-writer, retry or fallback |
| Feature ID not unique | Generate new ID, retry |

## Resume After Interruption

If initialization was interrupted:

1. Check `.business-analyse/config.json` for currentFeature or currentProject
2. If project ID exists, search for project index.json in `docs/*/v*/` (or legacy `docs/business-analyse/`)
   - If found with scope = "project": resume project mode (see Project Resume below)
3. If feature ID exists, search for index.json in `docs/`
4. If found, check status and `metadata.workflow.lastCompletedStep`

## Status-Based Resume Routing (check in this order)

### If status = `"handed-off"` or `"consolidated"`

→ Display: "BA workflow complete. Use /business-analyse-html to generate HTML, /business-analyse-review for corrections."
→ **STOP**

### If status = `"specified"` AND `metadata.workflow.allModulesSpecified === true`

→ Resume at `step-04-consolidate.md`
→ Display: "All modules specified — resuming at consolidation..."

### If status = `"specified"` AND `metadata.workflow.completedModules.length > 0` AND `metadata.workflow.completedModules.length < metadata.workflow.moduleOrder.length`

→ Resume at `step-03-specify.md` (continue specifying remaining modules)
→ Display: "Resuming module specification ({completedCount}/{totalCount})..."

### Otherwise

→ Offer to resume from last completed step

### If not found

→ Create fresh index.json

## Project Resume (project mode)

If a project-level index.json is found (scope = "project"):

### If status = `"draft"` or `"framed"`

→ Resume at `step-01-cadrage.md`

### If status = `"decomposed"`

→ Check `metadata.workflow.currentApplicationIndex` and `completedApplications[]`
→ Resume module specification for current application (step-02 or step-03)

### If status = `"specified"`

→ All applications specified → Resume at `step-04-consolidate.md`

### If status = `"consolidated"`

→ Display: "BA workflow complete. Use /business-analyse-html to generate HTML."
→ **STOP**
