trusty-mpm 1.0.2

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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# Environment Synchronization Patterns

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

## Purpose

Platform synchronization workflows: comparing local vs deployed environments, generating platform configs, safe sync patterns, and secret manager integration.

## Synchronization Principles

### Safety First: Dry-Run Default

**Always default to dry-run for safety:**

```python
def sync_to_platform(env_file: Path, platform: str, dry_run: bool = True):
    """Sync environment to platform. Defaults to dry-run."""
    changes = calculate_changes(env_file, platform)

    if dry_run:
        print("šŸ” DRY-RUN MODE: No changes will be applied")
        print("\nProposed changes:")
        display_changes(changes)
        print("\nTo apply: add --confirm flag")
        return

    # Actual sync only if dry_run=False
    apply_changes(changes, platform)
```

### Three-Way Comparison

Compare across local, platform, and secret manager:

```
Local (.env) ←→ Platform (Vercel/Railway) ←→ Secret Manager (1Password/AWS)
```

## Platform Comparison Workflows

### Compare Local vs Platform

```python
from typing import Dict, Set
from pathlib import Path

def compare_environments(
    local_env: Path,
    platform_env: Dict[str, str]
) -> Dict:
    """Compare local .env against platform environment."""

    local_vars = parse_env_file(local_env)

    return {
        'only_local': set(local_vars.keys()) - set(platform_env.keys()),
        'only_platform': set(platform_env.keys()) - set(local_vars.keys()),
        'different_values': {
            key for key in local_vars.keys() & platform_env.keys()
            if local_vars[key] != platform_env[key]
        },
        'identical': {
            key for key in local_vars.keys() & platform_env.keys()
            if local_vars[key] == platform_env[key]
        }
    }
```

### Display Comparison Results

```python
def display_comparison(comparison: Dict):
    """Display comparison in readable format."""

    print("\nšŸ“Š Environment Comparison\n")

    if comparison['only_local']:
        print("āš ļø  Variables only in LOCAL:")
        for var in sorted(comparison['only_local']):
            print(f"  + {var}")

    if comparison['only_platform']:
        print("\nāš ļø  Variables only in PLATFORM:")
        for var in sorted(comparison['only_platform']):
            print(f"  - {var}")

    if comparison['different_values']:
        print("\nšŸ”„ Variables with DIFFERENT values:")
        for var in sorted(comparison['different_values']):
            print(f"  ≠ {var}")

    if comparison['identical']:
        print(f"\nāœ… {len(comparison['identical'])} variables identical")
```

## Platform-Specific Integration

### Vercel

**Fetch Current Environment:**
```python
import requests

def fetch_vercel_env(project_id: str, token: str) -> Dict[str, str]:
    """Fetch environment variables from Vercel."""
    url = f"https://api.vercel.com/v9/projects/{project_id}/env"
    headers = {"Authorization": f"Bearer {token}"}

    response = requests.get(url, headers=headers)
    response.raise_for_status()

    # Vercel returns array of {key, value, target, type}
    env_vars = {}
    for var in response.json()['envs']:
        if 'production' in var.get('target', []):
            env_vars[var['key']] = var['value']

    return env_vars
```

**Generate Vercel Config:**
```python
def generate_vercel_config(env_file: Path) -> str:
    """Generate vercel.json env configuration."""
    vars_dict = parse_env_file(env_file)

    # Separate public vs private
    public_vars = {k: v for k, v in vars_dict.items() if k.startswith('NEXT_PUBLIC_')}
    private_vars = {k: v for k, v in vars_dict.items() if not k.startswith('NEXT_PUBLIC_')}

    config = {
        "env": {k: v for k, v in public_vars.items()},
        "build": {
            "env": {k: v for k, v in private_vars.items() if not is_secret(k)}
        }
    }

    return json.dumps(config, indent=2)
```

**Sync to Vercel:**
```python
def sync_to_vercel(env_file: Path, project_id: str, token: str, dry_run: bool = True):
    """Sync environment variables to Vercel."""
    local_vars = parse_env_file(env_file)
    remote_vars = fetch_vercel_env(project_id, token)

    changes = compare_environments(env_file, remote_vars)

    if dry_run:
        display_comparison(changes)
        return

    # Apply changes
    url = f"https://api.vercel.com/v10/projects/{project_id}/env"
    headers = {"Authorization": f"Bearer {token}"}

    for key in changes['only_local']:
        data = {
            "key": key,
            "value": local_vars[key],
            "target": ["production"],
            "type": "encrypted"
        }
        requests.post(url, headers=headers, json=data)

    print("āœ… Synced to Vercel")
```

### Railway

**Fetch Railway Environment:**
```python
def fetch_railway_env(project_id: str, token: str) -> Dict[str, str]:
    """Fetch environment variables from Railway."""
    # Railway uses GraphQL API
    query = """
    query($projectId: String!) {
        project(id: $projectId) {
            environments {
                variables {
                    name
                    value
                }
            }
        }
    }
    """

    response = requests.post(
        "https://backboard.railway.app/graphql/v2",
        headers={"Authorization": f"Bearer {token}"},
        json={"query": query, "variables": {"projectId": project_id}}
    )

    data = response.json()
    env_vars = {}
    for var in data['data']['project']['environments'][0]['variables']:
        env_vars[var['name']] = var['value']

    return env_vars
```

**Sync to Railway:**
```python
def sync_to_railway(env_file: Path, project_id: str, token: str, dry_run: bool = True):
    """Sync environment variables to Railway."""
    local_vars = parse_env_file(env_file)

    if dry_run:
        print("šŸ” DRY-RUN: Would sync to Railway:")
        for key in local_vars:
            print(f"  {key}={mask_secret(local_vars[key])}")
        return

    # Use Railway CLI (more reliable than API)
    import subprocess
    for key, value in local_vars.items():
        subprocess.run(
            ["railway", "variables", "set", f"{key}={value}"],
            check=True
        )

    print("āœ… Synced to Railway")
```

### Heroku

**Sync to Heroku:**
```python
def sync_to_heroku(env_file: Path, app_name: str, dry_run: bool = True):
    """Sync environment variables to Heroku."""
    import subprocess

    local_vars = parse_env_file(env_file)

    # Fetch current Heroku config
    result = subprocess.run(
        ["heroku", "config", "--app", app_name, "--json"],
        capture_output=True,
        text=True
    )
    remote_vars = json.loads(result.stdout)

    changes = compare_environments(env_file, remote_vars)

    if dry_run:
        display_comparison(changes)
        return

    # Apply changes using Heroku CLI
    for key, value in local_vars.items():
        subprocess.run(
            ["heroku", "config:set", f"{key}={value}", "--app", app_name],
            check=True
        )

    print("āœ… Synced to Heroku")
```

## Secret Manager Integration

### 1Password

**Fetch from 1Password:**
```python
def fetch_from_1password(vault: str, item: str) -> Dict[str, str]:
    """Fetch secrets from 1Password CLI."""
    import subprocess
    import json

    result = subprocess.run(
        ["op", "item", "get", item, "--vault", vault, "--format", "json"],
        capture_output=True,
        text=True,
        check=True
    )

    data = json.loads(result.stdout)
    env_vars = {}

    for field in data['fields']:
        if field['purpose'] == 'NOTES':
            # Parse env format from notes
            for line in field['value'].split('\n'):
                if '=' in line:
                    key, value = line.split('=', 1)
                    env_vars[key.strip()] = value.strip()
        else:
            # Individual fields
            env_vars[field['label'].upper()] = field['value']

    return env_vars
```

**Push to 1Password:**
```python
def push_to_1password(env_file: Path, vault: str, item: str):
    """Push environment to 1Password."""
    import subprocess

    vars_dict = parse_env_file(env_file)

    # Create notes field with all env vars
    notes = '\n'.join([f"{k}={v}" for k, v in vars_dict.items()])

    subprocess.run(
        ["op", "item", "edit", item, "--vault", vault, f"notes={notes}"],
        check=True
    )

    print(f"āœ… Pushed to 1Password vault '{vault}'")
```

### AWS Secrets Manager

**Fetch from AWS:**
```python
def fetch_from_aws_secrets(secret_name: str, region: str = 'us-east-1') -> Dict[str, str]:
    """Fetch secrets from AWS Secrets Manager."""
    import boto3
    import json

    client = boto3.client('secretsmanager', region_name=region)
    response = client.get_secret_value(SecretId=secret_name)

    # Secrets stored as JSON
    return json.loads(response['SecretString'])
```

**Push to AWS:**
```python
def push_to_aws_secrets(env_file: Path, secret_name: str, region: str = 'us-east-1'):
    """Push environment to AWS Secrets Manager."""
    import boto3
    import json

    vars_dict = parse_env_file(env_file)
    client = boto3.client('secretsmanager', region_name=region)

    try:
        # Update existing secret
        client.update_secret(
            SecretId=secret_name,
            SecretString=json.dumps(vars_dict)
        )
    except client.exceptions.ResourceNotFoundException:
        # Create new secret
        client.create_secret(
            Name=secret_name,
            SecretString=json.dumps(vars_dict)
        )

    print(f"āœ… Pushed to AWS Secrets Manager: {secret_name}")
```

## Safe Sync Patterns

### Three-Step Sync Process

```python
def safe_sync_workflow(env_file: Path, platform: str):
    """Safe sync workflow with validation."""

    # Step 1: Validate local environment
    print("1ļøāƒ£ Validating local environment...")
    validation = validate_structure(env_file)
    if validation['errors']:
        print("āŒ Validation failed. Fix errors first.")
        return

    # Step 2: Dry-run comparison
    print("\n2ļøāƒ£ Comparing with platform...")
    comparison = compare_with_platform(env_file, platform)
    display_comparison(comparison)

    # Step 3: Confirm and sync
    print("\n3ļøāƒ£ Ready to sync")
    confirm = input("Apply changes? (yes/no): ")
    if confirm.lower() != 'yes':
        print("āŒ Sync cancelled")
        return

    sync_to_platform(env_file, platform, dry_run=False)
    print("āœ… Sync complete")
```

### Rollback Procedures

```python
def create_backup(platform: str, project_id: str) -> str:
    """Create backup before sync."""
    import json
    from datetime import datetime

    timestamp = datetime.now().isoformat()
    current_env = fetch_platform_env(platform, project_id)

    backup_file = f".env.backup.{platform}.{timestamp}.json"
    with open(backup_file, 'w') as f:
        json.dump(current_env, f, indent=2)

    return backup_file

def rollback(backup_file: str, platform: str, project_id: str):
    """Rollback to previous environment."""
    import json

    with open(backup_file) as f:
        backup_env = json.load(f)

    sync_dict_to_platform(backup_env, platform, project_id, dry_run=False)
    print(f"āœ… Rolled back to {backup_file}")
```

## Summary

**Synchronization Workflow**:
1. **Validate**: Check local .env structure
2. **Compare**: Dry-run comparison with platform
3. **Review**: Display proposed changes
4. **Backup**: Save current platform state
5. **Sync**: Apply changes with confirmation
6. **Verify**: Check platform reflects changes

**Key Patterns**:
- āœ… Always dry-run first
- āœ… Three-way comparison (local/platform/secret manager)
- āœ… Never auto-apply changes
- āœ… Create backups before sync
- āœ… Rollback capability
- āœ… Platform-specific handling

**Supported Platforms**:
- Vercel (API + CLI)
- Railway (GraphQL API + CLI)
- Heroku (CLI)

**Supported Secret Managers**:
- 1Password (CLI)
- AWS Secrets Manager (boto3)

## Related References

- [Validation]validation.md: Environment validation
- [Security]security.md: Secret protection during sync
- [Troubleshooting]troubleshooting.md: Sync issue resolution

---
**Lines**: 277 āœ“ 200-280 range