# Business Analysis - Module Loop Protocol (_module-loop.md)

> **Loaded by:** step-03a1-setup, step-03b-ui, step-03c-compile, step-03d-validate
> **Purpose:** State management and iteration logic for multi-module specification loop + application loop (project mode)

---

## State Management (Application Mode — single app)

```json
{
  "metadata": {
    "workflow": {
      "mode": "application",
      "moduleOrder": ["Customers", "Products", "Orders", "Invoices"],
      "currentModuleIndex": 2,
      "completedModules": ["Customers", "Products"],
      "currentModule": "Orders"
    }
  }
}
```

## State Management (Project Mode — multi app)

Project-level feature.json tracks the application loop:

```json
{
  "metadata": {
    "workflow": {
      "mode": "project",
      "applicationOrder": ["HumanResources", "EmployeeSelfService"],
      "currentApplicationIndex": 0,
      "completedApplications": [],
      "currentApplication": "HumanResources"
    }
  }
}
```

Each application-level feature.json tracks its own module loop (same as application mode).

## Module Loop Logic (end of each step-03 iteration)

```
1. Mark current module as "validated" in application-level feature.json
2. Add to completedModules[]
3. IF currentModuleIndex + 1 < moduleOrder.length:
     currentModuleIndex++
     currentModule = moduleOrder[currentModuleIndex]
     Write application-level feature.json
     Display: "Module {name} complete. Next: {next_name} ({N}/{total})"
     RE-LOAD: steps/step-03a1-setup.md
4. ELSE (all modules for current application specified):
     IF workflow.mode === "project":
       → Execute APPLICATION LOOP LOGIC (below)
     ELSE:
       application.status = "specified"
       Load: steps/step-04a-collect.md
```

## Application Loop Logic (project mode only)

> Only applicable when `workflow.mode === "project"` in the project-level feature.json.

```
1. Mark current application as "specified" in project feature.json
   ba-writer.updateApplicationStatus(projectId, currentApp, "specified")
2. Add to completedApplications[]
   ba-writer.advanceApplicationLoop(projectId)
3. IF currentApplicationIndex + 1 < applicationOrder.length:
     Display: "═══ Application {currentApp} complete ({N}/{total}). Next: {nextApp} ═══"
     Load: steps/step-02-decomposition.md (for next application's module decomposition)
4. ELSE (all applications specified):
     Display: "═══ All applications specified! Proceeding to cross-application consolidation... ═══"
     project.status = "specified"
     Load: steps/step-04a-collect.md (with cross-application consolidation)
```

## Context Management During Loop

To prevent context explosion as modules accumulate:
- **Current module:** Load full module feature.json
- **Completed modules:** Only load compact summary via `ba-reader.getCompletedModulesSummary()` (max 100 lines)
- **Master feature.json:** Only read workflow state + application roles
- **In project mode — completed applications:** Only load compact summary via `ba-reader.getCompletedApplicationsSummary()` (max 50 lines per app)

## Resume After Interruption

### Application Mode (single app)
```
1. ba-reader.findApplicationFeature() -> locate application-level feature.json
2. Read metadata.workflow.currentModuleIndex
3. IF currentModule has a feature.json with status != "validated":
     Resume step-03a1-setup for that module
4. ELSE:
     Start step-03a1-setup for next module
```

### Project Mode (multi app)
```
1. ba-reader.findProjectFeature() -> locate project-level feature.json
2. Read metadata.workflow.currentApplicationIndex
3. Locate current application's feature.json
4. Read application's metadata.workflow.currentModuleIndex
5. IF currentModule has a feature.json with status != "validated":
     Resume step-03a1-setup for that module in that application
6. ELSE IF application has unspecified modules:
     Start step-03a1-setup for next module in current application
7. ELSE IF more applications remain:
     Start step-02-decomposition for next application
8. ELSE:
     All done → Resume step-04a-collect
```
