mielin-cli 0.1.0-rc.1

Command-line interface and control plane for MielinOS distributed agent mesh
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# MielinOS CLI - Troubleshooting Guide

Solutions to common problems and debugging techniques.

## Table of Contents

- [Installation Issues]#installation-issues
- [Connection Problems]#connection-problems
- [Agent Issues]#agent-issues
- [Cluster Problems]#cluster-problems
- [Performance Issues]#performance-issues
- [Configuration Problems]#configuration-problems
- [Platform-Specific Issues]#platform-specific-issues
- [Getting Help]#getting-help

## Installation Issues

### Problem: `cargo install` fails

**Symptoms:**
```
error: failed to compile mielin-cli
```

**Solutions:**

1. **Update Rust toolchain:**
```bash
rustup update stable
```

2. **Check Rust version (requires 1.70+):**
```bash
rustc --version
```

3. **Install with specific features:**
```bash
cargo install mielin-cli --no-default-features
```

4. **Clean and retry:**
```bash
cargo clean
cargo install --path . --locked --force
```

### Problem: Binary not found after installation

**Symptoms:**
```
command not found: mielinctl
```

**Solutions:**

1. **Add cargo bin to PATH:**
```bash
# Add to ~/.bashrc or ~/.zshrc
export PATH="$HOME/.cargo/bin:$PATH"

# Reload shell
source ~/.bashrc
```

2. **Verify installation location:**
```bash
ls -la ~/.cargo/bin/mielinctl
```

3. **Install to custom location:**
```bash
cargo install --path . --root /usr/local
```

### Problem: Permission denied on Linux/macOS

**Symptoms:**
```
Permission denied (os error 13)
```

**Solutions:**

1. **Install to user directory (recommended):**
```bash
cargo install --path . --root ~/.local
export PATH="$HOME/.local/bin:$PATH"
```

2. **Or use sudo (not recommended for Cargo):**
```bash
sudo mv ~/.cargo/bin/mielinctl /usr/local/bin/
```

## Connection Problems

### Problem: Cannot connect to daemon

**Symptoms:**
```
Error: Connection refused
Could not connect to the MielinOS daemon
```

**Solutions:**

1. **Check if daemon is running:**
```bash
ps aux | grep mielinctl
```

2. **Start the daemon:**
```bash
mielinctl daemon --role core &
```

3. **Check listen address:**
```bash
# View current configuration
mielinctl node config node.listen_address

# Set correct address
mielinctl node config node.listen_address 0.0.0.0:7000
```

4. **Check firewall:**
```bash
# Linux (UFW)
sudo ufw allow 7000/tcp

# macOS
# System Preferences > Security & Privacy > Firewall

# Windows
netsh advfirewall firewall add rule name="MielinOS" dir=in action=allow protocol=TCP localport=7000
```

### Problem: Timeout errors

**Symptoms:**
```
Error: Operation timed out
```

**Solutions:**

1. **Increase timeout:**
```bash
# Via environment variable
export MIELIN_CLI_COMMAND_TIMEOUT_SECS=120

# Via configuration
mielinctl node config cli.command_timeout_secs 120
```

2. **Check network latency:**
```bash
ping -c 5 <node-address>
```

3. **Check if node is responsive:**
```bash
mielinctl mesh status
mielinctl node info <node-id>
```

### Problem: Cannot join cluster

**Symptoms:**
```
Error: Failed to join cluster
Bootstrap node not reachable
```

**Solutions:**

1. **Verify bootstrap node is running:**
```bash
# On bootstrap node
mielinctl node info <bootstrap-node-id>
```

2. **Check bootstrap address:**
```bash
# Use IP instead of hostname
mielinctl node join --bootstrap tcp://192.168.1.100:7000

# Check DNS resolution
nslookup bootstrap-node
```

3. **Verify network connectivity:**
```bash
telnet bootstrap-node 7000
# or
nc -zv bootstrap-node 7000
```

4. **Check bootstrap node logs:**
```bash
# View daemon logs
journalctl -u mielinctl -f
```

## Agent Issues

### Problem: Agent creation fails

**Symptoms:**
```
Error: Failed to create agent
WASM module validation failed
```

**Solutions:**

1. **Validate WASM module:**
```bash
mielinctl wasm validate agent.wasm
```

2. **Check file exists and is readable:**
```bash
ls -lh agent.wasm
file agent.wasm
```

3. **Verify WASM is correct format:**
```bash
# Should show "WebAssembly"
file agent.wasm

# Rebuild if needed
cargo build --target wasm32-wasi --release
```

4. **Check memory/CPU limits:**
```bash
# Increase limits if too restrictive
mielinctl agent create \
  --name my-agent \
  --wasm-file agent.wasm \
  --memory 512 \
  --cpu-shares 2048
```

### Problem: Agent stuck in "Paused" state

**Symptoms:**
```
State: Paused
Expected: Running
```

**Solutions:**

1. **Check agent logs:**
```bash
mielinctl agent logs <agent-id> --tail 100
```

2. **Inspect agent details:**
```bash
mielinctl agent inspect <agent-id> --output json
```

3. **Check node resources:**
```bash
mielinctl node info <node-id>
```

4. **Restart agent:**
```bash
mielinctl agent stop <agent-id>
sleep 5
mielinctl agent create --name <name> --wasm-file <file>
```

### Problem: Agent logs not showing

**Symptoms:**
```
No logs available
```

**Solutions:**

1. **Check if agent is running:**
```bash
mielinctl agent inspect <agent-id>
```

2. **Ensure agent is writing to stdout/stderr:**
```rust
// In your WASM code
println!("Log message");
eprintln!("Error message");
```

3. **Follow logs in real-time:**
```bash
mielinctl agent logs <agent-id> --follow
```

4. **Check log retention settings:**
```bash
mielinctl node config daemon.log_retention_days
```

### Problem: Agent migration fails

**Symptoms:**
```
Error: Migration failed
Agent state inconsistent
```

**Solutions:**

1. **Check migration status:**
```bash
mielinctl migrate status --agent <agent-id>
```

2. **Cancel stuck migration:**
```bash
mielinctl migrate cancel <migration-id>
```

3. **Try cold migration instead:**
```bash
mielinctl agent migrate <agent-id> --to <node-id> --strategy cold
```

4. **Check destination node has resources:**
```bash
mielinctl node info <destination-node>
```

5. **View migration history:**
```bash
mielinctl migrate history --limit 20
```

## Cluster Problems

### Problem: Cluster health check fails

**Symptoms:**
```
Cluster status: Unhealthy
Some nodes are not responding
```

**Solutions:**

1. **Identify unhealthy nodes:**
```bash
mielinctl node list --output json | jq -r '.[] | select(.state != "Running") | .id'
```

2. **Check individual node status:**
```bash
for node in $(mielinctl node list --quiet); do
  echo "Checking $node..."
  mielinctl node info $node
done
```

3. **Check gossip protocol:**
```bash
mielinctl gossip status
mielinctl gossip members --output json | jq -r '.[] | "\(.id): \(.state)"'
```

4. **Force gossip sync:**
```bash
mielinctl gossip sync
```

5. **Check for network partitions:**
```bash
mielinctl mesh status
mielinctl mesh peers
```

### Problem: Nodes not discovering each other

**Symptoms:**
```
Connected peers: 0
Cluster members: 1
```

**Solutions:**

1. **Check if mDNS is enabled:**
```bash
mielinctl node config daemon.enable_mdns
```

2. **Enable mDNS:**
```bash
export MIELIN_DAEMON_ENABLE_MDNS=true
mielinctl daemon --role core
```

3. **Use explicit bootstrap:**
```bash
mielinctl daemon \
  --role edge \
  --bootstrap tcp://192.168.1.100:7000,tcp://192.168.1.101:7001
```

4. **Check multicast firewall rules:**
```bash
# Linux
sudo iptables -A INPUT -p udp --dport 5353 -j ACCEPT

# macOS - ensure firewall allows mDNS
```

### Problem: Gossip protocol issues

**Symptoms:**
```
Gossip round: 0
Members: Suspect or Dead
```

**Solutions:**

1. **Check gossip status:**
```bash
mielinctl gossip status --output json
```

2. **Force synchronization:**
```bash
mielinctl gossip sync
```

3. **Restart gossip protocol:**
```bash
# Restart daemon with gossip enabled
mielinctl daemon --role core
```

4. **Check for dead members:**
```bash
DEAD=$(mielinctl gossip members --output json | jq -r '.[] | select(.state=="Dead") | .id')
if [ -n "$DEAD" ]; then
  echo "Dead members: $DEAD"
  mielinctl gossip sync
fi
```

## Performance Issues

### Problem: High memory usage

**Symptoms:**
```
Memory usage: 95%
Agents being evicted
```

**Solutions:**

1. **Identify high-memory agents:**
```bash
mielinctl agent list --output json | \
  jq -r 'sort_by(.memory_mb) | reverse | .[] | "\(.id): \(.memory_mb)MB"' | \
  head -10
```

2. **Profile memory usage:**
```bash
mielinctl debug profile <agent-id> --mem
```

3. **Reduce agent memory limits:**
```bash
# When creating new agents
mielinctl agent create \
  --name my-agent \
  --wasm-file agent.wasm \
  --memory 128
```

4. **Distribute load across nodes:**
```bash
# Migrate some agents to other nodes
mielinctl agent migrate <agent-id> --to <less-loaded-node>
```

5. **Add more nodes:**
```bash
mielinctl daemon --role edge --bootstrap tcp://core:7000
```

### Problem: Slow command execution

**Symptoms:**
```
Commands taking > 10 seconds
Frequent timeouts
```

**Solutions:**

1. **Check cluster load:**
```bash
mielinctl cluster status
mielinctl node list --output json | jq -r '.[] | "\(.id): \(.agents) agents"'
```

2. **Profile cluster performance:**
```bash
time mielinctl node list
time mielinctl agent list
```

3. **Check network latency:**
```bash
ping -c 10 <node-address>
```

4. **Reduce agent count per node:**
```bash
# Current distribution
mielinctl node list --output json | jq -r '.[] | "\(.id): \(.agents)"'

# Rebalance if needed
```

5. **Optimize WASM modules:**
```bash
mielinctl wasm optimize \
  --input agent.wasm \
  --output agent-opt.wasm \
  --level 3
```

### Problem: High CPU usage

**Symptoms:**
```
CPU usage: 90%+
System slow/unresponsive
```

**Solutions:**

1. **Identify CPU-intensive agents:**
```bash
for agent in $(mielinctl agent list --quiet); do
  mielinctl debug profile $agent --cpu
done
```

2. **Trace execution:**
```bash
mielinctl debug trace <agent-id> --duration 60 --file trace.json
```

3. **Reduce CPU shares:**
```bash
# Limit CPU for specific agents
# (requires recreating agent)
```

4. **Distribute load:**
```bash
# Add more edge nodes
mielinctl daemon --role edge --bootstrap tcp://core:7000 &
```

## Configuration Problems

### Problem: Configuration not persisting

**Symptoms:**
```
Settings reset after restart
Configuration changes not saved
```

**Solutions:**

1. **Check config file location:**
```bash
ls -la ~/.config/mielin/config.toml
```

2. **Create config directory if missing:**
```bash
mkdir -p ~/.config/mielin
```

3. **Verify config file permissions:**
```bash
chmod 644 ~/.config/mielin/config.toml
```

4. **Save configuration manually:**
```bash
mielinctl node config node.id my-node
# Verify
mielinctl node config node.id
```

5. **Check for environment variable overrides:**
```bash
env | grep MIELIN_
```

### Problem: Environment variables not working

**Symptoms:**
```
MIELIN_* variables ignored
Settings not applied
```

**Solutions:**

1. **Check variable names are correct:**
```bash
# Correct
export MIELIN_NODE_ID="my-node"

# Incorrect
export MIELIN_NODEID="my-node"
```

2. **Verify variables are exported:**
```bash
export MIELIN_NODE_ID="my-node"
env | grep MIELIN_NODE_ID
```

3. **Restart daemon after setting variables:**
```bash
export MIELIN_NODE_ROLE="core"
mielinctl daemon
```

4. **Use config file instead:**
```bash
mielinctl node config node.role core
```

## Platform-Specific Issues

### Linux Issues

**Problem: Systemd service won't start**

```bash
# Check service status
sudo systemctl status mielinctl

# View logs
sudo journalctl -u mielinctl -n 50

# Verify service file
cat /etc/systemd/system/mielinctl.service

# Reload systemd
sudo systemctl daemon-reload
sudo systemctl restart mielinctl
```

**Problem: SELinux blocking connections**

```bash
# Check if SELinux is enforcing
getenforce

# Temporarily disable (for testing)
sudo setenforce 0

# Add permanent rule
sudo setsebool -P nis_enabled 1
```

### macOS Issues

**Problem: "mielinctl" cannot be opened**

```bash
# Remove quarantine attribute
xattr -d com.apple.quarantine /usr/local/bin/mielinctl

# Or allow in System Preferences
# System Preferences > Security & Privacy > Allow
```

**Problem: Port binding fails**

```bash
# Check if port is in use
lsof -i :7000

# Kill process using port
kill -9 $(lsof -t -i :7000)
```

### Windows Issues

**Problem: PowerShell execution policy**

```powershell
# Check current policy
Get-ExecutionPolicy

# Allow scripts
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
```

**Problem: Path not updated**

```powershell
# Add to PATH permanently
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\Program Files\MielinOS", [EnvironmentVariableTarget]::Machine)

# Restart PowerShell
```

**Problem: Firewall blocking**

```powershell
# Add firewall rule
New-NetFirewallRule -DisplayName "MielinOS" -Direction Inbound -Protocol TCP -LocalPort 7000 -Action Allow
```

## Diagnostic Commands

### Collect diagnostic information

```bash
#!/bin/bash
# diagnose.sh - Collect diagnostic information

echo "=== System Information ==="
uname -a
cat /etc/os-release

echo "=== MielinCTL Version ==="
mielinctl --version

echo "=== Configuration ==="
mielinctl node config --all

echo "=== Cluster Status ==="
mielinctl cluster status

echo "=== Node List ==="
mielinctl node list --output json

echo "=== Agent List ==="
mielinctl agent list --output json

echo "=== Mesh Status ==="
mielinctl mesh status --output json

echo "=== Gossip Status ==="
mielinctl gossip status --output json

echo "=== Network Connectivity ==="
netstat -an | grep 7000

echo "=== Process Information ==="
ps aux | grep mielinctl

echo "=== Recent Logs ==="
# Platform specific
```

### Enable debug logging

```bash
# Set log level
export RUST_LOG=debug

# Run with debug output
mielinctl --verbose daemon --role core

# Or for specific modules
export RUST_LOG=mielin_cli=debug,mielin_mesh=info
```

## Getting Help

### Community Resources

- **GitHub Issues**: https://github.com/mielinos/mielin/issues
- **Documentation**: https://docs.mielinos.org
- **Discord**: https://discord.gg/mielinos
- **Stack Overflow**: Tag `mielinos`

### Reporting Bugs

When reporting issues, include:

1. **Environment Information:**
```bash
mielinctl --version
uname -a
```

2. **Configuration:**
```bash
mielinctl node config --all
```

3. **Error Messages:**
```bash
# Full error output
mielinctl <command> 2>&1
```

4. **Diagnostic Output:**
```bash
# Run diagnostic script
./diagnose.sh > diagnostic.txt
```

5. **Steps to Reproduce:**
- List exact commands run
- Include any error output
- Note expected vs actual behavior

### Quick Checklist

Before asking for help, verify:

- [ ] Using latest version: `mielinctl --version`
- [ ] Checked this troubleshooting guide
- [ ] Daemon is running: `ps aux | grep mielinctl`
- [ ] Network connectivity: `mielinctl mesh status`
- [ ] Firewall allows connections
- [ ] Configuration is correct: `mielinctl node config --all`
- [ ] Logs reviewed: `mielinctl agent logs <id>`
- [ ] Tried restarting daemon

### Advanced Debugging

For complex issues:

1. **Enable trace logging:**
```bash
export RUST_LOG=trace
export RUST_BACKTRACE=full
mielinctl daemon
```

2. **Use debugger:**
```bash
rust-lldb -- mielinctl daemon --role core
```

3. **Profile performance:**
```bash
cargo flamegraph -- mielinctl daemon
```

4. **Check for memory leaks:**
```bash
valgrind --leak-check=full mielinctl daemon
```

## See Also

- [Quickstart Guide]./QUICKSTART.md
- [Command Reference]./COMMAND_REFERENCE.md
- [Common Workflows]./WORKFLOWS.md
- [Examples]../examples/