# Codex Agent Prompt: Deploy ByteLyst on this Azure VM

> **Goal:** Execute `setup.sh` on this fresh Azure VM to deploy the entire ByteLyst ecosystem (31 services, 11 products). Then validate the deployment and fix any issues that arise.
>
> **You are running on:** A fresh Ubuntu 24.04 LTS Azure VM (Standard_D8s_v5: 8 vCPU, 32 GB RAM, 128 GB disk).
> **Nothing is pre-installed** except SSH. The script handles everything.

---

## Your Tasks (execute in order)

### Task 1: Run the bootstrap script

```bash
chmod +x setup.sh
sudo ./setup.sh
```

This will take ~15-25 minutes. It runs 8 phases:

1. Install system dependencies (Docker, Node.js 22, pnpm 10.6.5, Ollama, git, jq)
2. Start Gitea (local npm registry on :3300) + CI runner
3. Clone 12 repos from GitHub (`saravanakumardb1` org, public repos)
4. Build all `@bytelyst/*` packages (`pnpm install && pnpm -r build`)
5. Publish packages to Gitea npm registry
6. Generate `.env.ecosystem` with Cosmos emulator keys, JWT secret, etc.
7. Build + deploy 31 Docker Compose services (per-service, with fallback)
8. Health-check all endpoints + create `/opt/bytelyst/check-health.sh`

**Monitor the output.** The script logs everything to `/opt/bytelyst/setup.log`.

### Task 2: Handle failures (if any)

If the script fails mid-way:

- **Check which phase failed:** `sudo ./setup.sh --status`
- **Resume from where it stopped:** `sudo ./setup.sh --resume`
- **Retry a single phase:** `sudo ./setup.sh --phase=N` (e.g., `--phase=7` for Docker builds)
- **Check build logs:** `ls /opt/bytelyst/.setup-state/builds/` (per-service logs)
- **Check the main log:** `tail -100 /opt/bytelyst/setup.log`

If a Docker build fails for a specific service:

1. Read the build log: `cat /opt/bytelyst/.setup-state/builds/<service>.log`
2. Diagnose the issue (missing dependency, Dockerfile error, OOM, etc.)
3. If the fix is in a Dockerfile or source file, edit it in `/opt/bytelyst/<repo>/`
4. Retry: `sudo ./setup.sh --phase=7`

**Common issues and fixes:**

- **OOM during Docker builds:** Phase 7 already stops Ollama to free ~3 GB. If still OOM, run `docker system prune -f` and retry.
- **Gitea package not found:** Run `sudo ./setup.sh --phase=5` to re-publish, then `--phase=7`.
- **Cosmos emulator slow to start:** Just wait. Health checks have timeouts. Retry with `--phase=8`.
- **Build cache fills disk:** Phase 7 prunes after builds. If disk is full: `docker builder prune -af`.

### Task 3: Validate the deployment

After the script completes successfully, run the validation:

```bash
# Quick health check (generated by Phase 8)
/opt/bytelyst/check-health.sh

# Dry-run validation (checks all prerequisites)
sudo ./setup.sh --dry-run
```

Then verify these specific endpoints:

```bash
# Platform service
curl -sf http://localhost:4003/health | jq .
# Expect: {"status":"ok","service":"platform-service",...}

# All 10 product backends
for port in 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019; do
  echo -n "  :${port} -> "
  curl -sf http://localhost:${port}/health | jq -r .status 2>/dev/null || echo "FAIL"
done
# Expect: all "ok"

# All 10 product web apps (9 product + 1 LLM Lab)
for port in 3002 3030 3035 3040 3045 3050 3055 3060 3070 3075; do
  echo -n "  :${port} -> HTTP "
  curl -so /dev/null -w '%{http_code}' http://localhost:${port}/
  echo ""
done
# Expect: all HTTP 200

# LocalMemGPT can see Ollama models
curl -sf http://localhost:4019/api/models | jq '.[0].name'
# Expect: model name (e.g., "llama3.2:3b")

# LLM Lab dashboard proxies to Ollama
curl -sf http://localhost:3075/api/ollama/tags | jq '.models[0].name'
# Expect: model name

# Gitea has packages
curl -sf https://gitea.bytelyst.com/api/packages/ByteLyst/npm/ | jq '.[0].name'
# Expect: @bytelyst package names

# Grafana
curl -sf -u admin:bytelyst http://localhost:3000/api/org | jq .name
# Expect: "Main Org."

# Mailpit
curl -sf http://localhost:8025/api/v1/messages | jq .total
# Expect: 0
```

### Task 4: Report results

After validation, produce a summary:

1. **Phase completion status** — output of `sudo ./setup.sh --status`
2. **Health check results** — output of `/opt/bytelyst/check-health.sh`
3. **Any services that failed** — which ones and why
4. **Any manual fixes applied** — what you changed and where
5. **Total deployment time** — from the script's completion banner

---

## Important Context

### What the script deploys (31 services)

| Category          | Count | Services                                                                 |
| ----------------- | ----- | ------------------------------------------------------------------------ |
| Infrastructure    | 6     | cosmos-emulator, azurite, mailpit, loki, grafana, traefik                |
| Platform Services | 3     | platform-service (:4003), extraction-service (:4005), mcp-server (:4007) |
| Dashboards        | 2     | admin-web (:3001), tracker-web (:3003)                                   |
| Product Backends  | 10    | peakpulse thru localmemgpt (:4010-:4019)                                 |
| Product Web Apps  | 9     | lysnrai-dashboard (:3002), chronomind thru localmemgpt-web               |
| Standalone        | 1     | llmlab-dashboard (:3075)                                                 |

### Repo list (cloned to `/opt/bytelyst/`)

```
learning_ai_common_plat          # Shared platform: packages, services, dashboards, compose
learning_voice_ai_agent          # LysnrAI
learning_multimodal_memory_agents # MindLyst (web is at mindlyst-native/web/)
learning_ai_clock                # ChronoMind
learning_ai_jarvis_jr            # JarvisJr
learning_ai_fastgap              # NomGap
learning_ai_peakpulse            # PeakPulse
learning_ai_flowmonk             # FlowMonk
learning_ai_notes                # NoteLett
learning_ai_trails               # ActionTrail
learning_ai_local_memory_gpt     # LocalMemGPT
learning_ai_local_llms           # Local LLM Lab (dashboard only, no backend)
```

GitHub org: `saravanakumardb1` (repos are public).

### How Docker builds reach the Gitea npm registry

Product Dockerfiles use BuildKit secret mount for the npm token:

```dockerfile
RUN --mount=type=secret,id=gitea_npm_token \
    cp .npmrc.docker .npmrc && \
    GITEA_NPM_TOKEN=$(cat /run/secrets/gitea_npm_token) \
    pnpm install
```

`GITEA_NPM_HOST` is passed as a build arg (overridden to `172.17.0.1` on Linux VMs by the script).

### Script features (already built in)

- **Resume/retry:** `--resume`, `--resume-from=N`, `--phase=N`, `--reset`, `--status`, `--dry-run`
- **Phase markers:** `/opt/bytelyst/.setup-state/phaseN.done`
- **Per-service fallback:** Failed Docker builds skip, remaining services still start
- **Build logs:** Per-service at `/opt/bytelyst/.setup-state/builds/<service>.log`
- **Idempotent:** Safe to run twice
- **SSH protection:** All output tee'd to `/opt/bytelyst/setup.log`

### 30+ bugs already fixed in the script

The script has been extensively tested and hardened. 30+ bugs were found and fixed during development, including: Docker apt issues, Gitea token format changes, corporate proxy cleanup, OOM handling, build cache management, CORS and NODE_ENV configuration, healthchecks for all web services, and more. See the git log for details. **Do NOT modify `setup.sh` unless you encounter an actual runtime failure on this VM.**

---

## Constraints

- **DO NOT** modify `setup.sh` unless you encounter an actual failure that requires a fix
- **DO NOT** modify `docker-compose.ecosystem.yml` or any Dockerfile unless you are fixing a real build failure
- **DO NOT** hardcode secrets or API keys
- If you must edit a file to fix a build failure, **document what you changed and why** in your report
- The script is designed for Ubuntu 24.04 LTS on x86_64 (Cosmos emulator requires x86)

## Definition of Done

- [ ] `setup.sh` completed all 8 phases (or you resolved failures and re-ran successfully)
- [ ] `/opt/bytelyst/check-health.sh` shows ALL 31 services green
- [ ] All 10 product backends respond to `/health` with `{"status":"ok",...}`
- [ ] All 10 product web apps return HTTP 200 (9 product + 1 LLM Lab)
- [ ] LocalMemGPT can reach Ollama (`curl http://localhost:4019/api/models` returns models)
- [ ] LLM Lab dashboard (:3075) loads and proxies to Ollama
- [ ] Gitea UI at :3300 shows all `@bytelyst/*` packages
- [ ] Grafana at :3000 is accessible (admin / bytelyst)
- [ ] Mailpit at :8025 is accessible
- [ ] You produced a results summary (Task 4)

## CLI Quick Reference

```bash
sudo ./setup.sh                    # Fresh install (all 8 phases)
sudo ./setup.sh --phase=7          # Retry just the deploy phase
sudo ./setup.sh --resume           # Auto-resume after SSH disconnect
sudo ./setup.sh --resume-from=7    # Jump to deploy after manual fix
sudo ./setup.sh --status           # Check what's done
sudo ./setup.sh --dry-run          # Validate prerequisites (no changes)
sudo ./setup.sh --reset            # Start completely over
sudo ./setup.sh --help             # Show usage
```
