mecha10-cli 0.8.0

Mecha10 CLI tool
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
//! Config command handlers
//!
//! Handles pushing, pulling, and listing robot configurations.
//! Uses shared sync logic from mecha10-robot-config crate.

use crate::commands::config::{ConfigListArgs, ConfigPullArgs, ConfigPushArgs, ConfigValidateArgs};
use crate::context::CliContext;
use crate::paths;
use crate::services::credentials::CredentialsService;
use crate::ui::style;
use anyhow::{Context, Result};
use colored_json::to_colored_json_auto;
use mecha10_robot_config::{collect_config_files, write_config_value, RobotConfigClient};
use mecha10_templates::services::assets_download::copy_dir_recursive;
use std::path::Path;

pub(crate) const ROBOT_CONFIG_PATH: &str = "/api/robots";

/// Handle the config push command
///
/// Walks the whole `configs/` directory (plus `mecha10.json`) and pushes each file as its own
/// config key, one PUT per key. No client-side diffing: every push sends every key, every time.
pub async fn handle_config_push(ctx: &mut CliContext, args: &ConfigPushArgs) -> Result<()> {
    println!();
    println!("Pushing configurations...");
    println!();

    mecha10_core::relay_topics::regenerate_websocket_bridge_relay_config(&ctx.working_dir)
        .context("Failed to regenerate websocket-bridge relay topics")?;

    let robot_id = get_robot_id(ctx, args.robot_id.as_deref()).await?;

    let configs_dir = match &args.path {
        Some(path) => path.clone(),
        None => ctx.working_dir.join("configs"),
    };

    if !configs_dir.exists() {
        println!("No configs/ directory found");
        println!();
        println!("Expected:");
        println!("  - configs/ directory with node/behavior/simulation/rl configs");
        println!("  - mecha10.json at project root");
        return Ok(());
    }

    println!("Robot ID:     {}", robot_id);
    println!("Configs dir:  {}", configs_dir.display());
    println!();

    println!("Validating configs...");
    let assets_dir = if crate::config_validation::needs_environment_schema(&ctx.working_dir) {
        Some(
            ctx.assets_download()
                .fetch_and_extract()
                .await
                .context("Failed to download mecha10-assets for config validation")?,
        )
    } else {
        None
    };
    let schemas_dir = assets_dir
        .as_ref()
        .map(|dir| dir.path().join("configs/simulation/schemas"));
    match crate::config_validation::validate_configs(
        &ctx.working_dir,
        crate::config_validation::ConfigKind::ALL,
        schemas_dir.as_deref(),
    ) {
        Ok(()) => println!("{}", style::success("✅ All configs are valid")),
        Err(e) => {
            println!("{}", style::error("❌ Config validation failed:"));
            println!();
            println!("{:#}", e);
            println!();
            return Err(e.context("Refusing to push: fix the invalid config(s) above and try again"));
        }
    }
    println!();

    let mecha10_json = ctx.working_dir.join("mecha10.json");
    let configs = collect_config_files(&configs_dir, Some(&mecha10_json))
        .await
        .map_err(|e| anyhow::anyhow!("Failed to collect configs: {}", e))?;

    if configs.is_empty() {
        println!("No config files found");
        println!();
        return Ok(());
    }

    println!("Found {} config(s):", configs.len());
    for (key, _) in &configs {
        println!("  - {}", key);
    }
    println!();

    if args.dry_run {
        println!("DRY RUN - No changes will be made");
        return Ok(());
    }

    let client = create_config_client(ctx).await?;
    for (key, value) in &configs {
        client
            .upsert_config(&robot_id, key, value.clone())
            .await
            .map_err(|e| anyhow::anyhow!("Failed to push config '{}': {}", key, e))?;
    }

    println!("Pushed {} config(s)", configs.len());

    println!();
    Ok(())
}

/// Handle the config pull command
///
/// Fetches every config key for the robot and writes each back to disk individually.
/// `mecha10.json` always lands at the project root; `--output` only relocates `configs/`.
pub async fn handle_config_pull(ctx: &mut CliContext, args: &ConfigPullArgs) -> Result<()> {
    println!();
    println!("Pulling configurations...");
    println!();

    let robot_id = get_robot_id(ctx, args.robot_id.as_deref()).await?;

    let output_dir = args.output.clone().unwrap_or_else(|| ctx.working_dir.join("configs"));

    println!("Robot ID:    {}", robot_id);
    println!("Output dir:  {}", output_dir.display());
    println!();

    if output_dir.exists() && output_dir.read_dir().is_ok_and(|mut d| d.next().is_some()) && !args.force {
        return Err(anyhow::anyhow!(
            "{} already exists and is not empty - pass --force to overwrite",
            output_dir.display()
        ));
    }

    let client = create_config_client(ctx).await?;

    let configs = client
        .get_configs(&robot_id)
        .await
        .map_err(|e| anyhow::anyhow!("Failed to fetch configs: {}", e))?;

    if configs.is_empty() {
        println!("No configs found for robot {}", robot_id);
        return Ok(());
    }

    let mecha10_json_dest = ctx.working_dir.join("mecha10.json");
    let mut wrote_mecha10_json = false;
    for config in &configs {
        write_config_value(
            &output_dir,
            Some(&mecha10_json_dest),
            &config.config_key,
            &config.config_value,
        )
        .await
        .map_err(|e| anyhow::anyhow!("Failed to write config '{}': {}", config.config_key, e))?;
        if config.config_key == "mecha10.json" {
            wrote_mecha10_json = true;
        }
    }

    println!(
        "Pulled {} config(s){}",
        configs.len(),
        if wrote_mecha10_json {
            " (incl. mecha10.json)"
        } else {
            ""
        }
    );
    println!();

    Ok(())
}

/// Handle the config list command
///
/// Lists all configs for a robot.
pub async fn handle_config_list(ctx: &mut CliContext, args: &ConfigListArgs) -> Result<()> {
    println!();

    let robot_id = get_robot_id(ctx, args.robot_id.as_deref()).await?;

    println!("Configs for robot: {}", robot_id);
    println!();

    let client = create_config_client(ctx).await?;

    let configs = match client.get_configs(&robot_id).await {
        Ok(configs) => configs,
        Err(e) => {
            return Err(anyhow::anyhow!("Failed to fetch configs: {}", e));
        }
    };

    if configs.is_empty() {
        println!("No configs found");
        println!();
        return Ok(());
    }

    for config in configs {
        println!("{}:", config.config_key);
        println!("  Updated: {}", config.updated_at.format("%Y-%m-%d %H:%M:%S UTC"));
        println!("  Created by: {}", config.created_by);

        if args.verbose {
            let value_str = to_colored_json_auto(&config.config_value).unwrap_or_else(|_| "{}".to_string());
            for line in value_str.lines() {
                println!("    {}", line);
            }
        }
        println!();
    }

    Ok(())
}

/// Push every config file from the project's `configs/` directory (plus `mecha10.json`) that
/// differs from what's currently stored for the robot, one key per file. Used by build, upload,
/// and train handlers to auto-push configs after a successful operation.
pub async fn push_configs(ctx: &mut CliContext) -> Result<()> {
    mecha10_core::relay_topics::regenerate_websocket_bridge_relay_config(&ctx.working_dir)
        .context("Failed to regenerate websocket-bridge relay topics")?;

    let configs_dir = ctx.working_dir.join("configs");

    if !configs_dir.exists() {
        println!("  No configs/ directory found, skipping config push");
        return Ok(());
    }

    let robot_id = get_robot_id(ctx, None).await?;

    let mecha10_json = ctx.working_dir.join("mecha10.json");
    let configs = collect_config_files(&configs_dir, Some(&mecha10_json))
        .await
        .map_err(|e| anyhow::anyhow!("Failed to collect configs: {}", e))?;

    if configs.is_empty() {
        println!("  No configs found in configs/ directory");
        return Ok(());
    }

    let client = create_config_client(ctx).await?;

    let remote = client
        .get_configs(&robot_id)
        .await
        .map_err(|e| anyhow::anyhow!("Failed to fetch current configs for robot {}: {}", robot_id, e))?;
    let remote_by_key: std::collections::HashMap<&str, &mecha10_robot_config::ConfigValue> = remote
        .iter()
        .map(|c| (c.config_key.as_str(), &c.config_value))
        .collect();

    let to_push: Vec<&(String, mecha10_robot_config::ConfigValue)> = configs
        .iter()
        .filter(|(key, value)| remote_by_key.get(key.as_str()) != Some(&value))
        .collect();

    if to_push.is_empty() {
        println!("  Configs unchanged, skipping push");
        return Ok(());
    }

    println!(
        "  Pushing {} changed config(s) for robot {}...",
        to_push.len(),
        robot_id
    );

    for (key, value) in &to_push {
        client
            .upsert_config(&robot_id, key, value.clone())
            .await
            .map_err(|e| anyhow::anyhow!("Failed to push config '{}': {}", key, e))?;
    }

    println!("  {}", style::success(format!("✅ Pushed {} config(s)", to_push.len())));

    Ok(())
}

/// Handle the config show command
///
/// Prints the resolved project identity (name, robot ID, control plane URL, login status,
/// and local configs/ directory path).
pub async fn handle_config_show(ctx: &mut CliContext) -> Result<()> {
    println!();

    if !ctx.is_project_initialized() {
        println!("Not inside a mecha10 project (no mecha10.json found).");
        println!();
        println!("Run `mecha10 init` to create a new project.");
        println!();
        return Ok(());
    }

    let project_config = ctx.load_project_config().await?;
    let env_name = project_config.environments.current_env_name();
    let control_plane_url = project_config.environments.control_plane_url();
    let auth_status = mecha10_auth::commands::auth_status_line()?;
    let configs_dir = ctx.working_dir.join("configs");

    println!("Project name:      {}", project_config.name);
    println!("Robot ID:          {}", project_config.robot.id);
    println!("Environment:       {}", env_name);
    println!("Control plane URL: {}", control_plane_url);
    println!("Auth status:       {}", auth_status);
    println!("Configs dir:       {}", configs_dir.display());
    println!();

    Ok(())
}

/// Handle the config validate command
///
/// Validates `mecha10.json` plus every node config it references, or -- with `--path` -- a
/// single arbitrary config file. Delegates to [`crate::config_validation::validate_config`].
pub async fn handle_config_validate(ctx: &mut CliContext, args: &ConfigValidateArgs) -> Result<()> {
    println!();
    println!("{}", style::heading("🔎 Config Validation"));
    println!("{}", style::separator(47));
    println!();

    if args.path.is_none() && !ctx.is_project_initialized() {
        println!(
            "{}",
            style::error("❌ Not in a Mecha10 project directory (no mecha10.json found).")
        );
        println!();
        println!("Run `mecha10 init` to create a new project, or pass --path to validate a single file.");
        println!();
        return Err(anyhow::anyhow!("Not in a Mecha10 project directory"));
    }

    let needs_schemas = args.path.is_none() && crate::config_validation::needs_environment_schema(&ctx.working_dir);
    let assets_dir = if needs_schemas {
        Some(
            ctx.assets_download()
                .fetch_and_extract()
                .await
                .context("Failed to download mecha10-assets for config validation")?,
        )
    } else {
        None
    };
    let schemas_dir = assets_dir
        .as_ref()
        .map(|dir| dir.path().join("configs/simulation/schemas"));

    match crate::config_validation::validate_config(args.path.as_deref(), schemas_dir.as_deref()) {
        Ok(target) => {
            println!("{}", style::success(format!("✅ {} is valid", target.display())));
            println!();
            Ok(())
        }
        Err(e) => {
            println!("{}", style::error("❌ Config validation failed:"));
            println!();
            println!("{:#}", e);
            println!();
            Err(e)
        }
    }
}

/// Handle the config edit command
///
/// Shows a scrollable picker of every editable config this project knows about and opens the
/// selected one in `$VISUAL`/`$EDITOR` (falling back to `vi`).
pub async fn handle_config_edit(ctx: &mut CliContext) -> Result<()> {
    println!();

    if !ctx.is_project_initialized() {
        println!("Not inside a mecha10 project (no mecha10.json found).");
        println!();
        println!("Run `mecha10 init` to create a new project.");
        println!();
        return Ok(());
    }

    let entries = crate::ui::discover_editable_configs(&ctx.working_dir);

    if entries.is_empty() {
        println!("No editable configs found under configs/, mecha10.json, or simulation/.");
        println!();
        println!("Run `mecha10 init` to scaffold a project, or `mecha10 config pull` to fetch existing configs.");
        println!();
        return Ok(());
    }

    let selected_path = run_config_edit_tui(entries)?;

    let Some(path) = selected_path else {
        return Ok(());
    };

    let editor_command = std::env::var("VISUAL")
        .or_else(|_| std::env::var("EDITOR"))
        .unwrap_or_else(|_| "vi".to_string());

    let mut parts = editor_command.split_whitespace();
    let editor = parts
        .next()
        .ok_or_else(|| anyhow::anyhow!("$VISUAL/$EDITOR is set but empty"))?;

    let status = std::process::Command::new(editor)
        .args(parts)
        .arg(&path)
        .status()
        .with_context(|| format!("Failed to launch editor '{editor_command}'"))?;

    if !status.success() {
        return Err(anyhow::anyhow!(
            "Editor '{editor}' exited with {}",
            status
                .code()
                .map_or_else(|| "a signal".to_string(), |code| code.to_string())
        ));
    }

    Ok(())
}

/// Run the config edit picker TUI to completion, restoring the terminal before returning.
/// Returns the selected file path, or `None` if the user cancelled (Esc/q).
fn run_config_edit_tui(entries: Vec<crate::ui::EditableConfigEntry>) -> Result<Option<std::path::PathBuf>> {
    use crate::ui::{ConfigEditOutcome, ConfigEditTui};
    use crossterm::{
        event::{self, Event},
        execute,
        terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
    };
    use ratatui::{backend::CrosstermBackend, Terminal};
    use std::io::stdout;

    enable_raw_mode()?;
    let mut stdout_handle = stdout();
    execute!(stdout_handle, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout_handle);
    let mut terminal = Terminal::new(backend)?;

    let mut tui = ConfigEditTui::new(entries);

    let result: Result<Option<ConfigEditOutcome>> = loop {
        if let Err(e) = terminal.draw(|f| tui.draw(f)) {
            break Err(e.into());
        }

        match event::poll(std::time::Duration::from_millis(100)) {
            Ok(true) => match event::read() {
                Ok(Event::Key(key_event)) => tui.handle_key(key_event),
                Ok(_) => {}
                Err(e) => break Err(e.into()),
            },
            Ok(false) => {}
            Err(e) => break Err(e.into()),
        }

        if tui.should_quit() {
            break Ok(tui.take_outcome());
        }
    };

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;

    let outcome = result?;

    Ok(match outcome {
        Some(ConfigEditOutcome::Selected(path)) => Some(path),
        Some(ConfigEditOutcome::Cancelled) | None => None,
    })
}

/// Framework-tier (`@mecha10/`) bundled behavior templates, relative to a freshly-extracted
/// `mecha10-assets` root - see `BehaviorGeneratorService`'s module doc for the bundled set.
const FRAMEWORK_BEHAVIORS_ASSETS_DIR: &str = "configs/behaviors/@mecha10";

/// Handle the config sync command
///
/// Backfills any bundled `@mecha10/` framework behavior missing from `configs/behaviors/`
/// (e.g. in a project scaffolded before that behavior existed), without touching project-tier
/// custom behaviors or overwriting anything already on disk.
pub async fn handle_config_sync(ctx: &mut CliContext) -> Result<()> {
    println!();
    println!("Syncing framework behaviors...");
    println!();

    if !ctx.is_project_initialized() {
        anyhow::bail!("Not in a Mecha10 project directory");
    }

    let project_config = ctx.load_project_config().await?;
    let platform = project_config.robot.platform.as_deref().unwrap_or("rover").to_string();
    let working_dir = ctx.working_dir.clone();

    let assets_dir = ctx
        .assets_download()
        .fetch_and_extract()
        .await
        .context("Failed to fetch mecha10-assets")?;
    let framework_behaviors_root = assets_dir.path().join(FRAMEWORK_BEHAVIORS_ASSETS_DIR);

    let added = sync_framework_behaviors(&framework_behaviors_root, &working_dir)?;

    println!("Platform: {}", platform);
    println!();

    if added.is_empty() {
        println!("✅ Framework behaviors are already up to date.");
    } else {
        println!("Added {} new framework behavior(s):", added.len());
        for name in &added {
            println!("  - @mecha10/{}", name);
        }
    }
    println!();

    Ok(())
}

/// Copy in any `@mecha10/<name>` framework behavior that exists under `framework_behaviors_root`
/// (the freshly-fetched `mecha10-assets` extraction's `configs/behaviors/@mecha10/`) but is
/// missing on disk under `configs/behaviors/@mecha10/` in `project_root`. Never touches an
/// existing directory. Returns the names of behaviors newly copied in.
fn sync_framework_behaviors(framework_behaviors_root: &Path, project_root: &Path) -> Result<Vec<String>> {
    if !framework_behaviors_root.exists() {
        return Ok(Vec::new());
    }

    let entries = std::fs::read_dir(framework_behaviors_root).context("Failed to list bundled framework behaviors")?;

    let mut behavior_names: Vec<String> = entries
        .filter_map(|entry| entry.ok())
        .filter(|entry| entry.path().is_dir())
        .filter_map(|entry| entry.file_name().to_str().map(str::to_string))
        .collect();
    behavior_names.sort();

    let mut added = Vec::new();
    for name in behavior_names {
        let dest = project_root.join(paths::config::framework_behavior_dir(&name));
        if dest.exists() {
            continue;
        }

        copy_dir_recursive(&framework_behaviors_root.join(&name), &dest)
            .with_context(|| format!("Failed to copy bundled behavior '{}'", name))?;
        added.push(name);
    }

    Ok(added)
}

/// Get robot ID from args or project config
pub(crate) async fn get_robot_id(ctx: &mut CliContext, arg_robot_id: Option<&str>) -> Result<String> {
    if let Some(id) = arg_robot_id {
        return Ok(id.to_string());
    }

    if ctx.is_project_initialized() {
        let project_config = ctx.load_project_config().await?;
        return Ok(project_config.robot.id);
    }

    Err(anyhow::anyhow!(
        "No robot ID specified. Use --robot-id or run from a mecha10 project directory."
    ))
}

/// Create a RobotConfigClient with credentials
pub(crate) async fn create_config_client(ctx: &mut CliContext) -> Result<RobotConfigClient> {
    let credentials_service = CredentialsService::new();
    let credentials = if ctx.is_ci() {
        credentials_service
            .load()?
            .ok_or_else(|| anyhow::anyhow!("Not logged in. Run `mecha10 auth login` first."))?
    } else {
        mecha10_auth::ensure_authenticated_interactive(&credentials_service).await?
    };

    let project_config = ctx.load_project_config().await?;
    let control_plane_url = project_config.environments.control_plane_url();
    let config_url = format!("{}{}", control_plane_url, ROBOT_CONFIG_PATH);

    Ok(RobotConfigClient::with_user_id(
        config_url,
        credentials.api_key,
        Some(credentials.user_id),
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::CliContextBuilder;
    use std::io::{Read, Write};
    use tempfile::{tempdir, TempDir};

    /// Build a gzip-tar byte stream mirroring the real `mecha10-assets` publish layout, just
    /// `configs/simulation/schemas/*.schema.json` (what `needs_environment_schema`-gated config
    /// validation actually reads), sourced from this crate's vendored fixture copies.
    fn build_fake_assets_tarball() -> Vec<u8> {
        let fixtures_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/schemas");

        let mut tar_bytes = Vec::new();
        {
            let mut builder = tar::Builder::new(&mut tar_bytes);
            for file_name in ["environment-config.schema.json", "preset-config.schema.json"] {
                let content = std::fs::read(fixtures_dir.join(file_name)).unwrap();
                let mut header = tar::Header::new_gnu();
                header
                    .set_path(format!("configs/simulation/schemas/{file_name}"))
                    .unwrap();
                header.set_size(content.len() as u64);
                header.set_mode(0o644);
                header.set_cksum();
                builder.append(&header, content.as_slice()).unwrap();
            }
            builder.finish().unwrap();
        }

        let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
        gz.write_all(&tar_bytes).unwrap();
        gz.finish().unwrap()
    }

    /// Points `MECHA10_API_URL` at a freshly spawned fake `mecha10-assets` download server
    /// (serving [`build_fake_assets_tarball`]'s content) for the lifetime of the returned guard,
    /// removing the env var on drop (even on panic) - mirrors `add.rs`'s
    /// `FakeNodeListServer`/`start_fake_node_list_server` pattern for the node-catalog endpoint.
    struct FakeAssetsServer;

    impl Drop for FakeAssetsServer {
        fn drop(&mut self) {
            std::env::remove_var("MECHA10_API_URL");
        }
    }

    fn start_fake_assets_server() -> FakeAssetsServer {
        let body = build_fake_assets_tarball();
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();

        std::thread::spawn(move || {
            for stream in listener.incoming() {
                let mut stream = match stream {
                    Ok(s) => s,
                    Err(_) => continue,
                };
                let mut buf = [0u8; 4096];
                let _ = stream.read(&mut buf);
                let header = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: application/gzip\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    body.len()
                );
                let _ = stream.write_all(header.as_bytes());
                let _ = stream.write_all(&body);
                let _ = stream.flush();
            }
        });

        std::env::set_var("MECHA10_API_URL", format!("http://{}/api", addr));
        FakeAssetsServer
    }

    fn write_project_config(dir: &std::path::Path, robot_id: &str) -> std::path::PathBuf {
        let config_path = dir.join("mecha10.json");
        std::fs::write(
            &config_path,
            serde_json::json!({
                "name": "proj",
                "version": "0.1.0",
                "robot_id": robot_id,
            })
            .to_string(),
        )
        .unwrap();
        config_path
    }

    #[tokio::test]
    async fn get_robot_id_prefers_explicit_arg_over_project_config() {
        let dir = tempdir().unwrap();
        let config_path = write_project_config(dir.path(), "from-config");

        let mut ctx = CliContextBuilder::new()
            .working_dir(dir.path().to_path_buf())
            .config_path(Some(config_path))
            .build()
            .unwrap();

        let robot_id = get_robot_id(&mut ctx, Some("from-arg")).await.unwrap();
        assert_eq!(robot_id, "from-arg");
    }

    #[tokio::test]
    async fn get_robot_id_falls_back_to_project_config() {
        let dir = tempdir().unwrap();
        let config_path = write_project_config(dir.path(), "from-config");

        let mut ctx = CliContextBuilder::new()
            .working_dir(dir.path().to_path_buf())
            .config_path(Some(config_path))
            .build()
            .unwrap();

        let robot_id = get_robot_id(&mut ctx, None).await.unwrap();
        assert_eq!(robot_id, "from-config");
    }

    #[tokio::test]
    async fn get_robot_id_errors_when_no_arg_and_no_project() {
        let dir = tempdir().unwrap();
        let config_path = dir.path().join("mecha10.json");

        let mut ctx = CliContextBuilder::new()
            .working_dir(dir.path().to_path_buf())
            .config_path(Some(config_path))
            .build()
            .unwrap();

        let err = get_robot_id(&mut ctx, None).await.unwrap_err();
        assert!(err.to_string().contains("No robot ID specified"));
    }

    /// `mecha10 config push` must refuse to push when any config fails validation.
    #[tokio::test]
    async fn config_push_fails_cleanly_and_does_not_push_when_a_config_is_invalid() {
        let _assets_server = start_fake_assets_server();
        let dir = tempdir().unwrap();
        std::fs::write(
            dir.path().join("mecha10.json"),
            serde_json::json!({"name": "proj", "version": "1.0.0"}).to_string(),
        )
        .unwrap();
        std::fs::create_dir_all(dir.path().join("configs/simulation/environments/broken_env")).unwrap();
        std::fs::write(
            dir.path()
                .join("configs/simulation/environments/broken_env/environment.json"),
            serde_json::json!({}).to_string(),
        )
        .unwrap();

        let mut ctx = CliContextBuilder::new()
            .working_dir(dir.path().to_path_buf())
            .config_path(Some(dir.path().join("mecha10.json")))
            .build()
            .unwrap();

        let args = ConfigPushArgs {
            path: None,
            robot_id: Some("test-robot".to_string()),
            dry_run: true,
        };

        let err = handle_config_push(&mut ctx, &args).await.unwrap_err();
        assert!(
            err.to_string().contains("Refusing to push"),
            "expected the push to be refused, got: {}",
            err
        );
    }

    /// A project whose configs all pass validation must sail through the gate.
    #[tokio::test]
    async fn config_push_proceeds_past_validation_when_every_config_is_valid() {
        let _assets_server = start_fake_assets_server();
        let dir = tempdir().unwrap();
        std::fs::write(
            dir.path().join("mecha10.json"),
            serde_json::json!({"name": "proj", "version": "1.0.0"}).to_string(),
        )
        .unwrap();
        std::fs::create_dir_all(dir.path().join("configs/simulation/environments/basic_arena")).unwrap();
        std::fs::write(
            dir.path()
                .join("configs/simulation/environments/basic_arena/environment.json"),
            serde_json::json!({
                "id": "basic_arena",
                "version": "1.0.0",
                "name": "Basic Arena",
                "description": "A test arena",
                "scene_path": "res://basic_arena.tscn",
                "dimensions": {"width": 20.0, "length": 20.0, "height": 3.0, "unit": "meters"},
                "features": {}
            })
            .to_string(),
        )
        .unwrap();

        let mut ctx = CliContextBuilder::new()
            .working_dir(dir.path().to_path_buf())
            .config_path(Some(dir.path().join("mecha10.json")))
            .build()
            .unwrap();

        let args = ConfigPushArgs {
            path: None,
            robot_id: Some("test-robot".to_string()),
            dry_run: true,
        };

        let result = handle_config_push(&mut ctx, &args).await;
        assert!(result.is_ok(), "{:?}", result.err());
    }

    #[tokio::test]
    async fn per_key_push_round_trips_nodes_and_simulation_images_together() {
        let dir = tempdir().unwrap();
        let configs_dir = dir.path().join("configs");

        std::fs::create_dir_all(configs_dir.join("nodes/@mecha10/motor")).unwrap();
        std::fs::write(configs_dir.join("nodes/@mecha10/motor/config.json"), r#"{"speed": 1}"#).unwrap();

        std::fs::create_dir_all(configs_dir.join("simulation/images")).unwrap();
        std::fs::write(configs_dir.join("simulation/images/aiko.jpg"), [0xFF, 0xD8, 0xFF, 0x00]).unwrap();

        let collected = collect_config_files(&configs_dir, None).await.unwrap();
        assert_eq!(collected.len(), 2);

        let extract_dir = tempdir().unwrap();
        for (key, value) in &collected {
            write_config_value(&extract_dir.path().join("configs"), None, key, value)
                .await
                .unwrap();
        }

        assert_eq!(
            std::fs::read_to_string(extract_dir.path().join("configs/nodes/@mecha10/motor/config.json")).unwrap(),
            "{\n  \"speed\": 1\n}"
        );
        assert_eq!(
            std::fs::read(extract_dir.path().join("configs/simulation/images/aiko.jpg")).unwrap(),
            vec![0xFF, 0xD8, 0xFF, 0x00]
        );
    }

    /// Stage a fake extracted `mecha10-assets` root containing one `@mecha10/<name>/config.json`
    /// per entry in `behaviors`, returning the root's `configs/behaviors/@mecha10` subdirectory
    /// (what `sync_framework_behaviors` is handed in production).
    fn stage_bundled_behaviors(behaviors: &[(&str, &str)]) -> TempDir {
        let assets_root = tempdir().unwrap();
        let framework_behaviors_dir = assets_root.path().join(FRAMEWORK_BEHAVIORS_ASSETS_DIR);

        for (name, config_json) in behaviors {
            let dir = framework_behaviors_dir.join(name);
            std::fs::create_dir_all(&dir).unwrap();
            std::fs::write(dir.join("config.json"), config_json).unwrap();
        }

        assets_root
    }

    /// `sync` adds a missing bundled behavior, leaving an already-present one alone.
    #[test]
    fn sync_framework_behaviors_adds_a_missing_bundled_behavior() {
        let assets_root = stage_bundled_behaviors(&[
            ("idle-wander", r#"{"name": "idle-wander"}"#),
            ("instance-track", r#"{"name": "instance-track"}"#),
        ]);
        let framework_behaviors_dir = assets_root.path().join(FRAMEWORK_BEHAVIORS_ASSETS_DIR);

        let project = tempdir().unwrap();
        std::fs::create_dir_all(project.path().join("configs/behaviors/@mecha10/idle-wander")).unwrap();
        std::fs::write(
            project
                .path()
                .join("configs/behaviors/@mecha10/idle-wander/config.json"),
            r#"{"name": "idle-wander"}"#,
        )
        .unwrap();

        let added = sync_framework_behaviors(&framework_behaviors_dir, project.path()).unwrap();

        assert_eq!(added, vec!["instance-track".to_string()]);
        assert_eq!(
            std::fs::read_to_string(
                project
                    .path()
                    .join("configs/behaviors/@mecha10/instance-track/config.json")
            )
            .unwrap(),
            r#"{"name": "instance-track"}"#
        );
    }

    /// A project that already has every bundled behavior on disk gets nothing added.
    #[test]
    fn sync_framework_behaviors_reports_nothing_to_do_when_already_up_to_date() {
        let assets_root = stage_bundled_behaviors(&[("idle-wander", r#"{"name": "idle-wander"}"#)]);
        let framework_behaviors_dir = assets_root.path().join(FRAMEWORK_BEHAVIORS_ASSETS_DIR);

        let project = tempdir().unwrap();
        std::fs::create_dir_all(project.path().join("configs/behaviors/@mecha10/idle-wander")).unwrap();
        std::fs::write(
            project
                .path()
                .join("configs/behaviors/@mecha10/idle-wander/config.json"),
            r#"{"name": "idle-wander"}"#,
        )
        .unwrap();

        let added = sync_framework_behaviors(&framework_behaviors_dir, project.path()).unwrap();

        assert!(added.is_empty(), "expected nothing to sync, got {:?}", added);
    }

    /// An `@mecha10/<name>` directory already on disk must never be overwritten.
    #[test]
    fn sync_framework_behaviors_never_overwrites_an_existing_customized_behavior() {
        let assets_root = stage_bundled_behaviors(&[("idle-wander", r#"{"name": "idle-wander-bundled"}"#)]);
        let framework_behaviors_dir = assets_root.path().join(FRAMEWORK_BEHAVIORS_ASSETS_DIR);

        let project = tempdir().unwrap();
        std::fs::create_dir_all(project.path().join("configs/behaviors/@mecha10/idle-wander")).unwrap();
        std::fs::write(
            project
                .path()
                .join("configs/behaviors/@mecha10/idle-wander/config.json"),
            r#"{"name": "idle-wander-customized"}"#,
        )
        .unwrap();

        let added = sync_framework_behaviors(&framework_behaviors_dir, project.path()).unwrap();

        assert!(added.is_empty());
        assert_eq!(
            std::fs::read_to_string(
                project
                    .path()
                    .join("configs/behaviors/@mecha10/idle-wander/config.json")
            )
            .unwrap(),
            r#"{"name": "idle-wander-customized"}"#,
            "an existing @mecha10/ behavior must never be overwritten by sync"
        );
    }

    /// Project-tier custom behaviors (no `@mecha10/` prefix) must never be touched by sync.
    #[test]
    fn sync_framework_behaviors_leaves_project_tier_custom_behaviors_untouched() {
        let assets_root = stage_bundled_behaviors(&[("instance-track", r#"{"name": "instance-track"}"#)]);
        let framework_behaviors_dir = assets_root.path().join(FRAMEWORK_BEHAVIORS_ASSETS_DIR);

        let project = tempdir().unwrap();
        std::fs::create_dir_all(project.path().join("configs/behaviors/my-custom-patrol")).unwrap();
        std::fs::write(
            project.path().join("configs/behaviors/my-custom-patrol/config.json"),
            r#"{"name": "my-custom-patrol"}"#,
        )
        .unwrap();

        let added = sync_framework_behaviors(&framework_behaviors_dir, project.path()).unwrap();

        assert_eq!(added, vec!["instance-track".to_string()]);
        assert_eq!(
            std::fs::read_to_string(project.path().join("configs/behaviors/my-custom-patrol/config.json")).unwrap(),
            r#"{"name": "my-custom-patrol"}"#,
            "project-tier custom behaviors must be left untouched by sync"
        );
    }
}