{#- Context variables:
   - company: str (e.g., "ACME Bank")
   - industry: str (e.g., "fsi")
   - catalog: str (e.g., "acme_fsi_demo")
   - bundle_name: str (e.g., "acme-bank-fsi-demo")
   - app_name: str (e.g., "acme-bank-fsi-app")
   - warehouse_id: str
   - workspace_host: str
   - databricks_profile: str | None
   - lakebase_url: str | None
   - notification_email: str
   - demo_title: str (e.g., "Acme Bank FSI Demo" or "Financial Services Demo")
-#}
# /build-next — {{ demo_title }} Builder

**This command builds the next wave.** It requires a plan to exist first.

---

## FIRST: Determine Current State

1. Read `conf/demo_manifest.json` — what's the current wave status?
2. Read `docs/IMPLEMENTATION_PLAN.md` — does a plan exist?
3. Read `docs/PROGRESS.md` — what waves are done?

**If no plan exists (IMPLEMENTATION_PLAN.md is still a placeholder)** → Tell the user: "No plan found. Run `/plan` first to create an implementation plan."
**If a wave is `in_progress`** → Resume that wave.
**If all waves are `completed`** → Tell the user: "All waves complete! Run `/deploy` to deploy."
**Otherwise** → Start the next `pending` wave.

---

## Build-Validate Loop

### CRITICAL: Build ONE piece at a time. No exceptions.

For each wave, execute this loop:

```
1. BUILD one increment (one entity, one layer, one component) — write code files
2. REVIEW — check against skill patterns and plan
3. DEPLOY — `databricks bundle validate && databricks bundle deploy -t dev`
4. RUN — `databricks bundle run -t dev {job_or_pipeline}`
5. VALIDATE — use MCP `execute_sql` to verify tables/data exist
6. Only THEN move to next increment
```

**Everything is code. Everything deploys via `databricks bundle deploy`.** MCP is only for validation/debugging.

**Max 3 attempts per increment. Escalate to user after 3 failures.**

---

## Wave Guides

### Wave 1: Data Foundation — Agent: lakehouse-dev
- Unity Catalog setup (`src/data_generation/setup_catalog.py`)
- Structured data generation (one entity at a time in `src/data_generation/structured/`)
- Unstructured data generation (`src/data_generation/unstructured/`)
- Update `resources/jobs/data_generation_job.yml` with tasks
- **Done when**: All data exists in volumes with expected row counts.

### Wave 2: DLT/SDP Pipelines — Agent: lakehouse-dev
- Bronze layer (`src/pipelines/bronze/`) — STREAMING TABLE + read_files
- Silver layer (`src/pipelines/silver/`) — STREAMING TABLE + STREAM + constraints
- Gold layer (`src/pipelines/gold/`) — LIVE TABLE + aggregations
- **Strict order: Bronze -> Silver -> Gold. Never skip ahead.**
- **Done when**: Full medallion architecture running, all tables have data.

### Wave 3: ML + Analytics — Agents: lakehouse-dev + dashboard-dev
- Feature engineering (`src/ml/feature_engineering.py`)
- Model training (`src/ml/train_model.py`)
- Dashboards (`src/dashboards/`) — follow `.claude/skills/databricks-aibi-dashboards/`
- Genie space setup
- **Done when**: ML model serving, dashboards live, Genie responding.

### Wave 4: Showcase App — Agent: app-dev
- Backend API (FastAPI 3-layer: api/services/repositories)
- Frontend (React + TypeScript + shadcn/ui)
- Integration (ML model inference, agent chat)
- Deploy to Databricks Apps
- **Done when**: App deployed, end-to-end functional.

### Wave 5: Agents + Polish — Agent: lakehouse-dev
- Agent Framework code (`src/agents/`)
- Talk track (`/talk-track`)
- Final end-to-end validation
- **Done when**: Presentation-ready demo with talk track.

---

## Error Recovery

| Error | Auto-Fix |
|-------|----------|
| `Table not found` in DLT | Add `LIVE.` prefix or fully qualify with `{{ catalog }}.{schema}.` |
| `Streaming tables cannot use batch queries` | Change to `LIVE TABLE` or add `STREAM()` wrapper |
| `GROUP BY position N not in select list` | Replace positional with explicit column names |
| `Module not found` in job | Update `requirements.txt` or environment dependencies |
| Dashboard column mismatch | Run `inspect_schemas.py`, fix column references |
| Notebook path has `.py` | Remove `.py` extension from `notebook_task` path |
| Authentication error | Check profile `{{ databricks_profile }}` in `~/.databrickscfg` |

**Escalate to user ONLY when:**
1. Max 3 attempts exceeded
2. Permission/authentication errors
3. Infrastructure issues (warehouse down)
4. Ambiguous requirements needing human judgment

---

## Configuration Reference

| Setting | Value |
|---------|-------|
| Catalog | `{{ catalog }}` |
| Warehouse | `{{ warehouse_id }}` |
| Workspace | `{{ workspace_host }}` |
{% if databricks_profile %}| Profile | `{{ databricks_profile }}` |
{% endif %}{% if lakebase_url %}| Lakebase | `{{ lakebase_url }}` |
{% endif %}| Bundle | `{{ bundle_name }}` |
| App | `{{ app_name }}` |
| Email | `{{ notification_email }}` |
