# Phase 2 — MC + HiveMind API

# Phase 2 — MC + HiveMind API

**Timeline:** Weeks 1-2  
**Goal:** Mission Control and HiveMind leave ANVIL and become cloud-hosted APIs. This is the biggest architectural change — SQLite becomes Postgres, local scripts become REST calls.  
**MC Task:** #8495  
**Proveo Owner:** Angie Jones  
**Status:** PREVIEW — Kelsey working in parallel

## Why Second

MC and HiveMind are the nervous system. Once they are cloud-hosted, every other phase can run from any machine without touching ANVIL.

## Deliverables

- **mc-api.js:** Express-based REST API wrapping current `mc.js` logic 
    - `GET /tasks`, `POST /tasks`, `PATCH /tasks/:id`, `GET /stats`
    - Postgres driver (pg) replacing SQLite
    - Schema migration: 8378 tasks, 127 open — pg-migrate from SQLite dump
- **hivemind-api.js:** REST + optional WebSocket for pub/sub 
    - Postgres backend (hivemind schema)
- Docker images for both, pushed to Azure Container Registry
- **Azure Container Apps:** deploy mc-api and hivemind-api 
    - Consumption plan (serverless, scale-to-zero when no traffic)
    - Min replicas: 1 (so cold start is 2-4s max, not 30s+)
    - Memory: 0.5GB each, vCPU: 0.25 each
- **Azure Database for Postgres Flexible Server:** Burstable B1ms 
    - Region: swedencentral
    - `mission_control` DB + `hivemind` DB on same instance
    - Automated backups (7-day retention, included in cost)
- Update `mc.js` client wrapper: detect `ALAI_MC_URL` env var, proxy to API if set 
    - Backward compatible: if no `ALAI_MC_URL`, still uses local SQLite (ANVIL stays working)

## Cost Estimate

```

Container Apps (2 apps, ~5h/day active, consumption plan):
  ~$1.50/month per app = $3/month total
  (Free grant: 180,000 vCPU-s/month covers most light usage)

Azure Postgres B1ms: ~$22-24/month (swedencentral, Flexible Server)
Azure Container Registry Basic: $5/month

Total Phase 2 additions: ~$30-32/month
```

## Rollback Plan

`mc.js` still reads local SQLite if `ALAI_MC_URL` is not set. If Postgres or Container Apps fail, unset `ALAI_MC_URL` on ANVIL and operations continue locally. SQLite is kept in parallel for 30 days post-migration before decommission.

## Proveo Validation Criteria

**Test Owner:** Angie Jones (Proveo)

1. From ab-mac (no local SQLite): `alai mc list` returns live tasks
2. From ANVIL: `node ~/system/tools/mc.js list` still works (backward compat)
3. POST to mc-api: task appears in both `mc.js list` AND cloud Postgres within 2s
4. Postgres automated backup: verify restore of 100-row sample matches source
5. Container App scales to zero after 10min idle, cold starts under 5s

## Detailed Implementation

Kelsey Hightower (FlowForge) is implementing Azure Container Apps + Postgres in parallel. Full runbook will be linked here once ready.

---

<small>Credit: ALAI, 2026</small>