trusty-mpm 1.7.0

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
Documentation
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# Environment Troubleshooting Guide

> **Part of**: [env-manager]../SKILL.md
> **Category**: infrastructure
> **Reading Level**: Intermediate

## Purpose

Solutions to common environment variable issues: "works locally, not in production", missing variables, framework-specific problems, and platform quirks.

## Common Issues

### Issue 1: Works Locally, Not in Production

**Symptoms:**
- Application works fine locally
- Fails or behaves incorrectly in production
- Error messages about missing or undefined variables

**Root Causes:**

**A. Variables Not Synced to Platform**
```bash
# Check local vs platform
python scripts/validate_env.py .env --compare-platform vercel

# Common issue: forgot to sync
python scripts/sync_secrets.py --platform vercel --sync --dry-run
```

**B. Wrong Environment File Loaded**
```bash
# Next.js example
# Local: .env.local (gitignored, has all secrets)
# Production: Platform env vars (might be missing some)

# Solution: Ensure all vars from .env.local are in platform
```

**C. Build-Time vs Runtime Variables**
```bash
# Vite/Next.js: VITE_* and NEXT_PUBLIC_* are build-time
# If you change them in production, you must rebuild

# Vercel: Redeploy after changing NEXT_PUBLIC_ vars
vercel --prod
```

### Issue 2: Missing Variables

**Debug Workflow:**

```python
# 1. Check if variable is defined
def check_variable(var_name: str, env_file: Path):
    """Check if variable exists and has value."""
    vars_dict = parse_env_file(env_file)

    if var_name not in vars_dict:
        print(f"❌ {var_name} not defined in {env_file}")
        return False

    if not vars_dict[var_name]:
        print(f"⚠️  {var_name} defined but empty")
        return False

    print(f"✅ {var_name}={mask_secret(vars_dict[var_name])}")
    return True

# 2. Check file precedence (Next.js)
def check_nextjs_precedence(project_dir: Path, var_name: str):
    """Check which file defines variable in Next.js."""
    env_files = [
        '.env.production.local',
        '.env.local',
        '.env.production',
        '.env'
    ]

    for env_file in env_files:
        file_path = project_dir / env_file
        if file_path.exists():
            vars_dict = parse_env_file(file_path)
            if var_name in vars_dict:
                print(f"Found in: {env_file}")
                print(f"Value: {mask_secret(vars_dict[var_name])}")
                return

    print(f"❌ {var_name} not found in any .env file")
```

**Common Mistakes:**
```bash
# ❌ Wrong: Variable name typo
DATABASE_URL=postgres://...
# Code accessing: DATABASE_ULR (typo)

# ❌ Wrong: Not loaded in .env file
# Missing from .env entirely

# ❌ Wrong: Empty value
DATABASE_URL=

# ✅ Correct:
DATABASE_URL=postgres://localhost:5432/mydb
```

### Issue 3: Framework-Specific Issues

**Next.js Issues:**

**A. NEXT_PUBLIC_ Variable Not Available Client-Side**
```bash
# Issue: Variable defined but undefined in browser
# Cause: Missing NEXT_PUBLIC_ prefix

# ❌ Wrong:
API_URL=https://api.example.com

# ✅ Correct:
NEXT_PUBLIC_API_URL=https://api.example.com
```

**B. File Precedence Confusion**
```bash
# If .env.local defines DATABASE_URL=local
# But .env defines DATABASE_URL=remote
# .env.local wins (higher precedence)

# Solution: Check all .env files
ls -la .env*
```

**Express/Node.js Issues:**

**A. process.env.VAR Undefined**
```javascript
// Issue: Variable not loaded
// Cause: Forgot to load dotenv

// ❌ Wrong: No dotenv
const dbUrl = process.env.DATABASE_URL;  // undefined

// ✅ Correct: Load dotenv first
require('dotenv').config();
const dbUrl = process.env.DATABASE_URL;
```

**Flask Issues:**

**A. Variable Not Available in Flask App**
```python
# Issue: os.getenv() returns None
# Cause: dotenv not loaded before app initialization

# ❌ Wrong: Config before load_dotenv
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')  # None
load_dotenv()

# ✅ Correct: load_dotenv first
from dotenv import load_dotenv
load_dotenv()
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
```

### Issue 4: Platform-Specific Issues

**Vercel Issues:**

**A. Environment Variable Not Applied**
```bash
# Issue: Changed env var in Vercel UI, but app still uses old value
# Cause: Vercel caches NEXT_PUBLIC_ vars at build time

# Solution: Redeploy
vercel --prod

# Or: Use non-NEXT_PUBLIC_ var and read server-side
```

**B. Variable Shows in Preview, Not Production**
```bash
# Issue: Variable works in preview deployments
# Cause: Variable only set for "Preview" environment in Vercel

# Solution: Set for "Production" environment too
# Vercel UI: Settings → Environment Variables → Production
```

**Railway Issues:**

**A. Variable Not Found After Deployment**
```bash
# Issue: App can't find variable
# Cause: Railway uses exact syntax, no dotenv loading

# Solution: Set variables in Railway dashboard or CLI
railway variables set DATABASE_URL=postgres://...
```

**Heroku Issues:**

**A. Config Vars Not Applied**
```bash
# Check current config
heroku config --app myapp

# Set missing var
heroku config:set DATABASE_URL=postgres://... --app myapp

# Restart app (sometimes needed)
heroku restart --app myapp
```

### Issue 5: Secret Exposure

**If Secrets Are Committed to Git:**

```bash
# Immediate actions:
# 1. Revoke/rotate exposed secrets immediately
# 2. Remove from git history
# 3. Update all deployments

# Remove from git history (DESTRUCTIVE)
git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch .env" \
  --prune-empty --tag-name-filter cat -- --all

# Or use BFG Repo-Cleaner (recommended)
bfg --delete-files .env
git reflog expire --expire=now --all
git gc --prune=now --aggressive

# Force push (careful!)
git push origin --force --all
```

**If Secrets Are in Public Logs:**

```bash
# 1. Clear deployment logs (if possible)
# 2. Rotate all exposed secrets
# 3. Add logging safeguards

# Update code to never log secrets
# ❌ Wrong:
logger.info(f"Connecting with {db_password}")

# ✅ Correct:
logger.info(f"Connecting to database (credentials masked)")
```

## Debugging Checklist

### Local Development

```bash
# 1. Check .env file exists
ls -la .env*

# 2. Validate structure
python scripts/validate_env.py .env

# 3. Check for duplicates
python scripts/validate_env.py .env --check-duplicates

# 4. Verify framework detection
python scripts/validate_env.py .env --detect-framework

# 5. Test variable loading
node -e "require('dotenv').config(); console.log(process.env.DATABASE_URL)"
```

### Production Debugging

```bash
# 1. Compare local vs production
python scripts/sync_secrets.py --platform vercel --compare

# 2. Check platform variables
vercel env ls

# 3. Check build logs for errors
vercel logs --follow

# 4. Verify deployment used correct branch
vercel inspect <deployment-url>
```

## Quick Fixes

### Fix 1: Sync Local to Platform

```bash
# 1. Validate local .env
python scripts/validate_env.py .env

# 2. Compare with platform
python scripts/sync_secrets.py --platform vercel --compare

# 3. Sync (dry-run first)
python scripts/sync_secrets.py --platform vercel --sync --dry-run

# 4. Actually sync
python scripts/sync_secrets.py --platform vercel --sync --confirm
```

### Fix 2: Regenerate .env.example

```bash
# Generate from current .env
python scripts/validate_env.py .env --generate-example

# Review and commit
git add .env.example
git commit -m "docs: update .env.example"
```

### Fix 3: Check All .env Files (Next.js)

```bash
# List all .env files
find . -name ".env*" -not -path "*/node_modules/*"

# Check each file
for file in .env*; do
  echo "=== $file ==="
  python scripts/validate_env.py $file
done
```

## Prevention Tips

### Tip 1: Use .env.example

```bash
# Always maintain .env.example with structure
# Never include actual values

# .env.example
DATABASE_URL=postgres://localhost:5432/mydb
JWT_SECRET=your-secret-here
API_KEY=your-api-key-here
```

### Tip 2: Validate Before Deploy

```bash
# Add to CI/CD pipeline
# .github/workflows/validate.yml
- name: Validate Environment
  run: python scripts/validate_env.py .env.example
```

### Tip 3: Document Platform-Specific Setup

```markdown
# README.md

## Environment Setup

### Local Development
1. Copy `.env.example` to `.env.local`
2. Fill in actual values
3. Run `python scripts/validate_env.py .env.local`

### Vercel Deployment
1. Go to Settings → Environment Variables
2. Add variables from `.env.example`
3. Set for Production environment
4. Deploy
```

## Summary

**Common Issues**:
- Works locally, not in production → Check platform sync
- Missing variables → Check file precedence
- Framework-specific → Check prefix requirements
- Platform quirks → Check platform documentation

**Debugging Workflow**:
1. Validate local .env structure
2. Check file precedence (Next.js)
3. Compare local vs platform
4. Check build logs
5. Verify variable access in code

**Quick Fixes**:
- Sync to platform with dry-run
- Regenerate .env.example
- Check all .env files
- Rotate exposed secrets

## Related References

- [Validation]validation.md: Environment validation workflows
- [Security]security.md: Secret exposure recovery
- [Synchronization]synchronization.md: Platform sync procedures
- [Frameworks]frameworks.md: Framework-specific patterns

---
**Lines**: 245 ✓ 180-250 range