File size: 5,943 Bytes
846b6be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# RMI Development Standards β€” AI-Forward + Web3 Best Practices

> **v3 (Jun 21 2026) β€” Crisis Ops Rebuild**: These standards reflect the
> rebuilt architecture. The legacy 8,475-line `_legacy_main.py` is frozen;
> do not edit. New code goes into `app/api/v1/` via the aggregator in
> `app/api/v1/__init__.py`. See `docs/adr/0001-why-fastapi.md` and
> `docs/adr/0003-strangler-fig-not-rewrite.md` for the architectural decisions.

## v3 Hard Rules

1. **No mass regex.** Each transformation explicit, one-line-per-replace.
2. **New files only** β€” never modify the 14 frozen files without explicit un-freeze.
3. **Strangler-fig migration**: legacy routes stay mounted in `main.py` until
   explicitly removed. New v1 routes mount at the same path β€” first match wins.
4. **One-line main.py edits only.** Even when adding routes, prefer the
   `app/api/v1/__init__.py` aggregator + `v1_modules` list pattern.
5. **No `_legacy_main` import.** The new main.py does not import legacy.
6. **Real data flows only.** A v1 route that returns 404 / placeholder is not
   done. Acceptance = a real route serves real data end-to-end.
7. **Prometheus metrics required.** Every new router emits `rmi_requests_total`
   counter (auto via CostTrackingMiddleware).
8. **Telegram alerts via @rmialerts only.** No personal chat routing.
   See `app/api/v1/admin/alerts_webhook.py`.

## v3 Modern Builder Bar (14 points)

See `~/.hermes/skills/rmi/modern-builder-compliance/SKILL.md` for the full bar.
Highlights:
- Async/await for all I/O  Β·  Pydantic v2 everywhere  Β·  Structured logging
- Type hints on all public functions  Β·  No bare except:  Β·  Frozen file respect
- Real data acceptance test  Β·  Documented in DESIGN.md or relevant ADR
- One concern per file  Β·  Tests before merge  Β·  No silent failures
- Idempotent endpoints  Β·  Pagination on list endpoints  Β·  Rate-limited public routes

---

## ⚠️ FIRST: Read /root/DEVELOPERS.md for canonical paths.

---

## BACKEND DEVELOPMENT

### Pre-commit checklist (run before every commit):
```bash
bash /root/backend/scripts/pre-commit.sh
```
Checks: Python syntax, hardcoded secrets, env var consistency, stale path references.

### Environment variables:
```bash
# Auto-generate from Hermes config:
python3 /root/backend/generate_env.py --force
# Then fill in missing values:
nano /root/backend/.env
```

### Live development (no rebuild needed):
```bash
# Volume mount means code changes are instant:
docker restart rmi-backend
# Verify:
curl http://localhost:8000/health
```

### Adding new env vars:
1. Add to code: `os.getenv("MY_VAR")`
2. Add to `/root/backend/.env.example` with comment
3. Run `python3 /root/backend/generate_env.py --force`
4. Add to `/srv/rugmuncher-backend/docker-compose.yml` if container needs it

---

## AI AGENT DEVELOPMENT WORKFLOW

This system is designed for AI-assisted development. Here's the stack:

```
hermes-agent (CLI)
    β”‚
    β”œβ”€β”€ Terminal tool β†’ docker exec, git, curl, python
    β”œβ”€β”€ Web tool β†’ API testing, research
    β”œβ”€β”€ File tool β†’ Edit /root/backend/ directly
    β”œβ”€β”€ Delegate β†’ Spawn sub-agents for parallel work
    └── Cron jobs β†’ Automated tasks
```

### How Hermes develops the backend:
1. **Discover**: Reads AGENTS.md in /root/backend/
2. **Edit**: Patches files directly (volume mount = live)
3. **Test**: `curl localhost:8000/health` after changes
4. **Rebuild**: `docker compose build && docker compose up -d`
5. **Verify**: Checks logs, API responses

### n8n workflow development:
- UI: http://localhost:5678 (admin / RugMuncher2024)
- Direct DB: `sqlite3 /root/n8n-data/database.sqlite`
- Import: Copy workflow JSONs into `/root/n8n-workflows/`
- Test: Check execution history in UI

### Orchestrator swarm:
- API: http://localhost:8081
- Health: `curl http://localhost:8081/health`
- Bots: `curl http://localhost:8081/orchestrator/bots`
- Create task: `POST /orchestrator/task`

---

## WEB3 SECURITY BEST PRACTICES

### Secrets management:
- **NO hardcoded secrets** in any `.py` file
- All secrets in `/root/.secrets/` or `/root/.hermes/.env`
- App passwords preferred over account passwords
- Rotate API keys quarterly

### Key scanning:
```bash
# Run before any commit:
grep -rn '0x[0-9a-fA-F]\{64\}\|sk-[a-zA-Z0-9]\{20,\}' /root/backend/app/ --include='*.py'
```

### RPC security:
- Use dedicated RPC URLs, never public endpoints in production
- Rate limit all on-chain queries
- Cache blockchain data aggressively (Redis)

---

## CODE QUALITY

### Python:
- Type hints on all public functions
- Docstrings for modules and classes
- Async/await for all I/O operations
- Use Pydantic for data models

### TypeScript (Frontend):
- Components in `/srv/rugmuncher-backend/rmi-frontend/src/components/`
- Services in `/srv/rugmuncher-backend/rmi-frontend/src/services/`
- Types shared via `/srv/rugmuncher-backend/rmi-frontend/src/types.ts`

---

## MONITORING

### Health checks:
```bash
# All services:
curl http://localhost:8000/health      # Backend
curl http://localhost:8081/health      # Orchestrator
curl http://localhost:5678/healthz     # n8n
curl http://localhost:9001/api/health  # Listmonk
```

### Logs:
```bash
docker logs rmi-backend --tail 50
docker logs rmi-n8n --tail 50
journalctl -u hermes -n 50
```

### Cron jobs:
```bash
# List all:
cronjob action='list'
# Check status of specific job:
cronjob action='list'  # look for last_status
```

---

## DEPLOYMENT

### Full stack restart:
```bash
cd /srv/rugmuncher-backend
docker compose down
docker compose up -d
```

### Rebuild with cache clear:
```bash
docker compose build --no-cache backend worker orchestrator
docker compose up -d
```

### Rollback (if something breaks):
```bash
# Restore backup:
cp /root/backups/n8n/$(date +%Y-%m)/database.sqlite /root/n8n-data/
docker restart rmi-n8n

# Rebuild from known-good commit:
cd /root/backend && git checkout <commit-hash>
docker restart rmi-backend
```