zag-cli 0.13.0

A unified CLI for AI coding agents — Claude, Codex, Gemini, Copilot, and Ollama
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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
use anyhow::{Context, Result, bail};
use log::{debug, info};
use zag_agent::config::Config;
use zag_agent::factory::AgentFactory;
use zag_agent::{auto_selector, mcp, sandbox, session, skills, worktree};

use crate::cleanup::{
    print_resume_hint, print_session_resume_hint, prompt_sandbox_cleanup, prompt_worktree_cleanup,
};
use crate::cli::Commands;
use crate::json_mode::{augment_system_prompt_for_json, handle_json_output, wrap_prompt_for_json};
use crate::output::print_agent_output;
use crate::resume::{
    current_workspace, discover_provider_session_id, resolve_continue_target, resolve_resume_target,
};
use crate::session_setup::{
    SessionMetadata, save_session_mapping, setup_plain_session, setup_sandbox, setup_worktree,
    update_provider_session_id, update_session_log_metadata,
};

/// Exit code: agent reported failure (is_error = true in AgentOutput).
const EXIT_AGENT_FAILURE: i32 = 1;

/// Exit code: underlying provider process crashed or exited with non-zero status.
const EXIT_PROVIDER_ERROR: i32 = 2;

pub(crate) struct AgentActionParams {
    pub(crate) agent_name: String,
    pub(crate) provider: String,
    pub(crate) provider_explicit: bool,
    pub(crate) action: Commands,
    pub(crate) system_prompt: Option<String>,
    pub(crate) model: Option<String>,
    pub(crate) root: Option<String>,
    pub(crate) auto_approve: bool,
    pub(crate) add_dirs: Vec<String>,
    pub(crate) show_usage: bool,
    pub(crate) quiet: bool,
    pub(crate) verbose: bool,
    pub(crate) worktree: Option<Option<String>>,
    pub(crate) sandbox: Option<Option<String>>,
    pub(crate) size: Option<String>,
    pub(crate) json_mode: bool,
    pub(crate) json_schema: Option<serde_json::Value>,
    pub(crate) session: Option<String>,
    pub(crate) max_turns: Option<u32>,
    pub(crate) mcp_config: Option<String>,
    pub(crate) timeout: Option<String>,
    pub(crate) exit_on_failure: bool,
    pub(crate) context_session: Option<String>,
    pub(crate) plan_path: Option<String>,
    pub(crate) env_vars: Vec<(String, String)>,
    pub(crate) files: Vec<String>,
    pub(crate) session_metadata: SessionMetadata,
}

pub(crate) fn run_resume_id(action: &Commands) -> Option<&str> {
    match action {
        Commands::Run { resume, .. } | Commands::Exec { resume, .. } => resume.as_deref(),
        _ => None,
    }
}

fn run_continue_requested(action: &Commands) -> bool {
    matches!(
        action,
        Commands::Run {
            continue_session: true,
            ..
        } | Commands::Exec {
            continue_session: true,
            ..
        }
    )
}

pub(crate) fn is_resume_run(action: &Commands) -> bool {
    run_resume_id(action).is_some() || run_continue_requested(action)
}

fn run_prompt(action: &Commands) -> Option<&str> {
    match action {
        Commands::Run { prompt, .. } => prompt.as_deref(),
        Commands::Exec { prompt, .. } => Some(prompt.as_str()),
        _ => None,
    }
}

fn is_new_interactive_run(action: &Commands, json_mode: bool) -> bool {
    matches!(action, Commands::Run { .. })
        && !is_resume_run(action)
        && !(json_mode && run_prompt(action).is_some())
}

/// Resolve the effective provider by walking the fallback tier list.
///
/// When the user has not pinned a provider with `-p`, this walks
/// `AgentFactory::fallback_sequence(requested)` and picks the first
/// provider whose binary is present in PATH and whose startup probe
/// succeeds. Each downgrade is logged via `log::warn!` so the user can
/// see which provider actually ended up running and why.
///
/// When `provider_explicit` is true, this is a no-op that just returns
/// the requested provider — explicit pinning must not be downgraded.
async fn resolve_effective_provider(
    provider: &str,
    provider_explicit: bool,
    root: Option<&str>,
) -> Result<String> {
    if provider_explicit {
        return Ok(provider.to_string());
    }
    // "auto" is already resolved by resolve_auto_selection; if we still
    // see it here something is off — leave it alone so the normal error
    // path catches it.
    if provider == "auto" {
        return Ok(provider.to_string());
    }

    let mut on_downgrade = |from: &str, to: &str, reason: &str| {
        log::warn!("Downgrading provider: {from} → {to} ({reason})");
    };
    let (_agent, effective) = AgentFactory::create_with_fallback(
        provider,
        false,
        None,
        None,
        root.map(String::from),
        false,
        Vec::new(),
        &mut on_downgrade,
    )
    .await?;
    Ok(effective)
}

/// Handle auto provider/model selection, mutating params in place.
async fn resolve_auto_selection(params: &mut AgentActionParams) -> Result<()> {
    let is_auto_provider = params.provider == "auto";
    let is_auto_model = params.model.as_deref() == Some("auto");

    if !is_auto_provider && !is_auto_model {
        return Ok(());
    }

    let task_prompt = run_prompt(&params.action);

    let task_prompt = task_prompt
        .ok_or_else(|| anyhow::anyhow!("auto provider/model requires a prompt to analyze"))?;

    let config = Config::load(params.root.as_deref()).unwrap_or_default();
    let current_provider = if !is_auto_provider {
        Some(params.provider.as_str())
    } else {
        None
    };

    let result = auto_selector::resolve(
        task_prompt,
        is_auto_provider,
        is_auto_model,
        current_provider,
        &config,
        params.root.as_deref(),
    )
    .await?;

    if let Some(p) = result.provider {
        params.provider = p;
    }
    if let Some(m) = result.model {
        params.model = Some(m);
    } else if is_auto_provider {
        params.model = None;
    }

    params.agent_name = crate::capitalize(&params.provider);

    let is_exec_action = matches!(params.action, Commands::Exec { .. });
    let show_wrapper = !params.quiet && (!is_exec_action || params.verbose);
    if show_wrapper {
        let model_info = params
            .model
            .as_deref()
            .map(|m| format!(" with model {m}"))
            .unwrap_or_default();
        println!(
            "\x1b[32m✓\x1b[0m Auto-selected: {}{}",
            params.agent_name, model_info
        );
    }

    Ok(())
}

/// Parameters for creating and configuring an agent.
struct AgentSetupParams {
    provider: String,
    agent_name: String,
    system_prompt: Option<String>,
    model: Option<String>,
    effective_root: Option<String>,
    session_id: Option<String>,
    auto_approve: bool,
    add_dirs: Vec<String>,
    output_format: Option<String>,
    input_format: Option<String>,
    replay_user_messages: bool,
    include_partial_messages: bool,
    verbose: bool,
    json_mode: bool,
    max_turns: Option<u32>,
    mcp_config: Option<String>,
    env_vars: Vec<(String, String)>,
}

/// Create and configure the agent with all settings.
fn create_and_configure_agent(
    p: AgentSetupParams,
    json_schema: &Option<serde_json::Value>,
    show_wrapper: bool,
) -> Result<(Box<dyn crate::agent::Agent + Send + Sync>, Option<String>)> {
    let spinner = if show_wrapper {
        crate::logging::spinner(format!("Initializing {} agent", p.agent_name))
    } else {
        let pb = indicatif::ProgressBar::new_spinner();
        pb.set_draw_target(indicatif::ProgressDrawTarget::hidden());
        pb
    };

    let mut agent = AgentFactory::create(
        &p.provider,
        p.system_prompt,
        p.model,
        p.effective_root,
        p.auto_approve,
        p.add_dirs,
    )?;

    let output_fmt_clone = p.output_format.clone();
    agent.set_output_format(p.output_format);

    if let Some(turns) = p.max_turns {
        agent.set_max_turns(turns);
    }

    if !p.env_vars.is_empty() {
        agent.set_env_vars(p.env_vars);
    }

    // Configure Claude-specific options in a single downcast
    if p.provider == "claude"
        && let Some(claude_agent) = agent.as_any_mut().downcast_mut::<crate::claude::Claude>()
    {
        claude_agent.set_verbose(p.verbose);
        if let Some(session_id) = p.session_id {
            claude_agent.set_session_id(session_id);
        }
        if let Some(input_fmt) = p.input_format {
            claude_agent.set_input_format(Some(input_fmt));
        }
        if p.replay_user_messages {
            claude_agent.set_replay_user_messages(true);
        }
        if p.include_partial_messages {
            claude_agent.set_include_partial_messages(true);
        }
        if p.json_mode
            && let Some(schema) = json_schema
        {
            let schema_str = serde_json::to_string(schema).unwrap_or_default();
            claude_agent.set_json_schema(Some(schema_str));
        }
        if p.mcp_config.is_some() {
            claude_agent.set_mcp_config(p.mcp_config);
        }

        // Set up event handler for streaming output (text or stream-json modes)
        let is_stream_json = output_fmt_clone.as_deref() == Some("stream-json");
        claude_agent.set_event_handler(Box::new(move |event, verbose| {
            use crate::output::{ContentBlock, Event};
            if is_stream_json {
                // Output as unified NDJSON
                if let Ok(json) = serde_json::to_string(event) {
                    println!("{json}");
                }
            } else {
                match event {
                    Event::Result { .. } => {
                        // End of stream — flush
                        if !verbose {
                            use std::io::Write;
                            println!();
                            let _ = std::io::stdout().flush();
                        }
                    }
                    _ => {
                        if verbose {
                            if let Some(formatted) = crate::output::format_event_as_text(event) {
                                println!("{formatted}");
                            }
                        } else if let Event::AssistantMessage { content, .. } = event {
                            for block in content {
                                if let ContentBlock::Text { text } = block {
                                    print!("{text}");
                                }
                            }
                        }
                    }
                }
            }
        }));
    }

    // Force output capture when JSON mode is active
    let user_output_format = output_fmt_clone.clone();
    if p.json_mode && user_output_format.is_none() {
        agent.set_output_format(Some("json".to_string()));
        if p.provider != "claude" {
            agent.set_capture_output(true);
        }
    }

    crate::logging::finish_spinner_quiet(&spinner);
    debug!("Agent configuration complete");

    Ok((agent, output_fmt_clone))
}

/// Context for executing an action.
struct ExecutionContext<'a> {
    provider: &'a str,
    json_mode: bool,
    json_schema: &'a Option<serde_json::Value>,
    output_fmt: Option<&'a str>,
    show_usage: bool,
    verbose: bool,
}

/// Execute the requested action.
/// Returns `Ok(true)` if agent reported success (or no output), `Ok(false)` if agent reported an error.
async fn execute_action(
    action: Commands,
    agent: &mut (dyn crate::agent::Agent + Send + Sync),
    ctx: &ExecutionContext<'_>,
    log_writer: Option<&crate::session_log::SessionLogWriter>,
) -> Result<bool> {
    match action {
        Commands::Run {
            prompt,
            resume,
            continue_session,
            ..
        } => {
            if resume.is_some() || continue_session {
                if let Some(ref session_id) = resume {
                    info!("Resuming session {session_id}");
                } else {
                    info!("Resuming latest session");
                }

                agent
                    .run_resume(resume.as_deref(), continue_session)
                    .await?;
            } else if ctx.json_mode && prompt.is_some() {
                info!("Starting non-interactive session (JSON mode)");
                let wrapped = if ctx.provider != "claude" {
                    let w = prompt.as_deref().map(wrap_prompt_for_json);
                    if let Some(ref wp) = w {
                        debug!("JSON-wrapped run prompt: {wp}");
                    }
                    w
                } else {
                    debug!("Run prompt (JSON mode, Claude): {prompt:?}");
                    None
                };
                let run_prompt = wrapped.as_deref().or(prompt.as_deref());
                let agent_output = agent.run(run_prompt).await?;
                if let (Some(writer), Some(agent_output)) = (log_writer, agent_output.as_ref()) {
                    crate::session_log::record_agent_output(writer, agent_output)?;
                }
                handle_json_output(
                    agent_output,
                    agent,
                    ctx.json_schema,
                    ctx.show_usage,
                    ctx.verbose,
                )
                .await?;
            } else {
                info!("Starting interactive session");
                agent.run_interactive(prompt.as_deref()).await?;
            }
        }
        Commands::Exec { prompt, resume, .. } => {
            let run_prompt = if ctx.json_mode && ctx.provider != "claude" {
                let wrapped = wrap_prompt_for_json(&prompt);
                debug!("JSON-wrapped prompt: {wrapped}");
                wrapped
            } else {
                debug!("Exec prompt: {prompt}");
                prompt.clone()
            };

            let agent_output = if let Some(ref session_id) = resume {
                info!("Resuming session {session_id} with prompt");
                agent
                    .run_resume_with_prompt(session_id, &run_prompt)
                    .await?
            } else {
                info!("Starting non-interactive session");
                agent.run(Some(&run_prompt)).await?
            };

            if let (Some(writer), Some(agent_output)) = (log_writer, agent_output.as_ref()) {
                crate::session_log::record_agent_output(writer, agent_output)?;
            }

            let agent_success = agent_output.as_ref().map(|o| !o.is_error).unwrap_or(true);

            if ctx.json_mode {
                handle_json_output(
                    agent_output,
                    agent,
                    ctx.json_schema,
                    ctx.show_usage,
                    ctx.verbose,
                )
                .await?;
            } else if let Some(agent_out) = agent_output {
                print_agent_output(&agent_out, ctx.output_fmt, ctx.show_usage, ctx.verbose)?;
            }

            return Ok(agent_success);
        }
        _ => unreachable!(),
    }

    Ok(true)
}

/// Log configuration details at debug level.
fn log_config_details(params: &AgentActionParams) {
    if let Some(ref m) = params.model {
        debug!("Model specified: {m}");
    }
    if let Some(ref r) = params.root {
        debug!("Root directory: {r}");
    }
    if params.auto_approve {
        debug!("Auto-approve enabled");
    }
    if let Some(ref sp) = params.system_prompt {
        debug!("System prompt: {sp}");
    }
    if !params.add_dirs.is_empty() {
        debug!("Additional directories: {:?}", params.add_dirs);
    }
    if params.worktree.is_some() {
        debug!("Worktree mode enabled");
    }
    if params.sandbox.is_some() {
        debug!("Sandbox mode enabled");
    }
    if params.json_mode {
        debug!("JSON output mode enabled");
    }
}

fn command_name(action: &Commands) -> &'static str {
    match action {
        Commands::Run { .. } => "run",
        Commands::Exec { .. } => "exec",
        Commands::Review { .. } => "review",
        Commands::Config { .. } => "config",
        Commands::Session { .. } => "session",
        Commands::Capability { .. } => "capability",
        Commands::Discover { .. } => "discover",
        Commands::Listen { .. } => "listen",
        Commands::Man { .. } => "man",
        Commands::Skills { .. } => "skills",
        Commands::Mcp { .. } => "mcp",
        Commands::Ps { .. } => "ps",
        Commands::Search { .. } => "search",
        Commands::Input { .. } => "input",
        Commands::Broadcast { .. } => "broadcast",
        Commands::Whoami { .. } => "whoami",
        Commands::Env { .. } => "env",
        Commands::Collect { .. } => "collect",
        Commands::Status { .. } => "status",
        Commands::Wait { .. } => "wait",
        Commands::Spawn { .. } => "spawn",
        Commands::Pipe { .. } => "pipe",
        Commands::Events { .. } => "events",
        Commands::Cancel { .. } => "cancel",
        Commands::Summary { .. } => "summary",
        Commands::Watch { .. } => "watch",
        Commands::Subscribe { .. } => "subscribe",
        Commands::Log { .. } => "log",
        Commands::Output { .. } => "output",
        Commands::Retry { .. } => "retry",
        Commands::Gc { .. } => "gc",
        Commands::Serve { .. } => "serve",
        Commands::Connect { .. } => "connect",
        Commands::Disconnect => "disconnect",
        Commands::Relay { .. } => "relay",
        Commands::User { .. } => "user",
        Commands::Plan { .. } => "plan",
    }
}

fn action_prompt(action: &Commands) -> Option<&str> {
    match action {
        Commands::Run { prompt, .. } => prompt.as_deref(),
        Commands::Exec { prompt, .. } => Some(prompt.as_str()),
        _ => None,
    }
}

fn should_enable_live_session_logs(action: &Commands, json_mode: bool) -> bool {
    matches!(action, Commands::Run { .. }) && !json_mode
}

pub(crate) async fn run_agent_action(mut params: AgentActionParams) -> Result<()> {
    resolve_auto_selection(&mut params).await?;

    // If the user did not pin a provider with `-p`, and we're not resuming
    // an existing session, walk the fallback tier list and pick the first
    // provider that actually works. This downgrades past missing binaries
    // and startup probe failures, logging each downgrade so the user can
    // see what happened.
    if !is_resume_run(&params.action) {
        let effective = resolve_effective_provider(
            &params.provider,
            params.provider_explicit,
            params.root.as_deref(),
        )
        .await?;
        if effective != params.provider {
            params.provider = effective;
            params.agent_name = crate::capitalize(&params.provider);
        }
    }

    log_config_details(&params);

    let session_metadata = std::mem::take(&mut params.session_metadata);
    let AgentActionParams {
        agent_name: _,
        mut provider,
        provider_explicit,
        mut action,
        system_prompt,
        mut model,
        root,
        auto_approve,
        add_dirs,
        show_usage,
        quiet,
        verbose,
        worktree: worktree_flag,
        sandbox: sandbox_flag,
        size,
        json_mode,
        json_schema,
        session,
        max_turns,
        mcp_config,
        timeout,
        exit_on_failure,
        context_session,
        plan_path,
        env_vars,
        files,
        session_metadata: _,
    } = params;

    // Apply config fallbacks for max_turns and system_prompt
    let config = Config::load(root.as_deref()).unwrap_or_default();
    let max_turns = max_turns.or(config.max_turns());
    let system_prompt = system_prompt.or_else(|| config.system_prompt().map(String::from));

    let is_exec = matches!(action, Commands::Exec { .. });
    let show_wrapper = !quiet && (!is_exec || verbose);

    let mut system_prompt =
        augment_system_prompt_for_json(system_prompt, json_mode, &provider, &json_schema);

    if let Err(e) = skills::setup_skills(&provider, &mut system_prompt) {
        log::warn!("Failed to set up skills: {e}");
    }

    if let Err(e) = mcp::setup_mcp(&provider, root.as_deref()) {
        log::warn!("Failed to set up MCP servers: {e}");
    }

    if let Some(ref sp) = system_prompt {
        debug!("Effective system prompt: {sp}");
    }

    let resume_target = if let Some(session_id) = run_resume_id(&action) {
        resolve_resume_target(session_id, root.as_deref())
    } else if run_continue_requested(&action) {
        resolve_continue_target(root.as_deref())
    } else {
        None
    };

    if is_resume_run(&action) && resume_target.is_none() {
        bail!("No matching session found to resume");
    }

    if let Some(target) = &resume_target {
        if provider_explicit && provider != target.entry.provider {
            bail!(
                "Requested provider '{}' does not match the stored session provider '{}'",
                provider,
                target.entry.provider
            );
        }
        provider = target.entry.provider.clone();
        if !target.entry.model.is_empty() {
            debug!(
                "Restored model from session entry: '{}'",
                target.entry.model
            );
            model = Some(target.entry.model.clone());
        } else {
            debug!("Session entry has empty model, will fall back to config/default");
        }
    }

    if let Some(target) = &resume_target {
        let native_id = target
            .entry
            .provider_session_id
            .clone()
            .unwrap_or_else(|| target.entry.session_id.clone());
        match &mut action {
            Commands::Run {
                resume,
                continue_session,
                ..
            }
            | Commands::Exec {
                resume,
                continue_session,
                ..
            } => {
                *resume = Some(native_id);
                *continue_session = false;
            }
            _ => {}
        }
    }

    let is_resume = is_resume_run(&action);
    let plain = setup_plain_session(is_new_interactive_run(&action, json_mode), &root, &session);
    let wrapper_session_id = plain.session_id.clone();
    let log_session_id = wrapper_session_id
        .clone()
        .or_else(|| {
            resume_target
                .as_ref()
                .map(|target| target.entry.session_id.clone())
        })
        .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

    let wt = setup_worktree(
        &worktree_flag,
        is_resume,
        &root,
        show_wrapper,
        wrapper_session_id.clone(),
    )?;
    let sb = setup_sandbox(&sandbox_flag, is_resume, &root, wrapper_session_id.clone())?;

    let effective_root = if let Some(target) = &resume_target {
        if target.entry.is_worktree {
            let wt_path = std::path::Path::new(&target.entry.worktree_path);
            if !wt_path.exists() && target.matched_by_wrapper_id {
                log::warn!(
                    "Worktree no longer exists at {}, resuming without it",
                    target.entry.worktree_path
                );
                let mut store = session::SessionStore::load(root.as_deref()).unwrap_or_default();
                store.remove(&target.entry.session_id);
                let _ = store.save(root.as_deref());
                Some(current_workspace(root.as_deref()))
            } else {
                Some(target.entry.worktree_path.clone())
            }
        } else {
            Some(target.entry.worktree_path.clone())
        }
    } else {
        wt.effective_root
            .clone()
            .or_else(|| plain.workspace_path.clone())
    };

    // Extract output/input format and streaming flags from exec action
    let (output_format, input_format, replay_user_messages, include_partial_messages) =
        match &action {
            Commands::Exec {
                output,
                input_format,
                replay_user_messages,
                include_partial_messages,
                ..
            } => (
                output.clone(),
                input_format.clone(),
                *replay_user_messages,
                *include_partial_messages,
            ),
            _ => (None, None, false, false),
        };

    if let Some(ref o) = output_format {
        debug!("Output format: {o}");
    }
    if let Some(ref i) = input_format {
        debug!("Input format: {i}");
    }

    let (mut agent, output_fmt_clone) = create_and_configure_agent(
        AgentSetupParams {
            provider: provider.clone(),
            agent_name: crate::capitalize(&provider),
            system_prompt,
            model,
            effective_root: effective_root.clone(),
            session_id: wrapper_session_id.clone(),
            auto_approve,
            add_dirs,
            output_format,
            input_format,
            replay_user_messages,
            include_partial_messages,
            verbose,
            json_mode,
            max_turns,
            mcp_config,
            env_vars,
        },
        &json_schema,
        show_wrapper,
    )?;

    // Configure sandbox if active
    if sb.is_sandbox_session
        && let (Some(name), Some(workspace)) = (&sb.sandbox_name, &sb.workspace)
    {
        let config = sandbox::SandboxConfig {
            name: name.clone(),
            template: sandbox::template_for_provider(&provider).to_string(),
            workspace: workspace.clone(),
        };
        agent.set_sandbox(config);
        if show_wrapper {
            println!("\x1b[32m✓\x1b[0m Sandbox configured: {name}");
        }
    }
    if let Some(target) = &resume_target
        && let Some(name) = &target.entry.sandbox_name
    {
        let config = sandbox::SandboxConfig {
            name: name.clone(),
            template: sandbox::template_for_provider(&provider).to_string(),
            workspace: target.entry.worktree_path.clone(),
        };
        agent.set_sandbox(config);
        if show_wrapper {
            println!("\x1b[32m✓\x1b[0m Sandbox configured: {name}");
        }
    }

    // Configure Ollama-specific options (model + size from config, --size flag)
    if provider == "ollama" {
        let config = Config::load(root.as_deref()).unwrap_or_default();

        // If --model was a size alias (small/medium/large), the factory resolved it
        // to a size string (e.g., "2b") via model_for_size — treat that as a --size instead.
        let current_model = agent.get_model().to_string();
        let is_size_value = crate::ollama::AVAILABLE_SIZES.contains(&current_model.as_str());
        if is_size_value {
            // --model was a size alias — revert model to config default, use resolved value as size
            agent.set_model(config.ollama_model().to_string());
        } else if current_model == crate::ollama::DEFAULT_MODEL {
            // No --model flag (or it matched default) — use config model
            agent.set_model(config.ollama_model().to_string());
        }
        // else: --model was an explicit model name — keep it

        if let Some(ollama_agent) = agent.as_any_mut().downcast_mut::<crate::ollama::Ollama>() {
            // Resolve size: --size flag > size-from-alias > ollama.size config > default
            if let Some(ref s) = size {
                let resolved = config.ollama_size_for(s).to_string();
                ollama_agent.set_size(resolved);
            } else if is_size_value {
                ollama_agent.set_size(current_model);
            } else {
                ollama_agent.set_size(config.ollama_size().to_string());
            }
        }
    }

    // Display initialization message
    let model_display = if provider == "ollama" {
        // Show full model:size tag for ollama
        if let Some(ollama_agent) = agent.as_any_mut().downcast_mut::<crate::ollama::Ollama>() {
            ollama_agent.display_model()
        } else {
            agent.get_model().to_string()
        }
    } else {
        agent.get_model().to_string()
    };
    let persisted_model = agent.get_model().to_string();
    let auto_approve_suffix = if auto_approve { " (auto approve)" } else { "" };
    if show_wrapper {
        println!(
            "\x1b[32m✓\x1b[0m {} initialized with model {}{}",
            crate::capitalize(&provider),
            model_display,
            auto_approve_suffix
        );
    }

    // Save session-worktree mapping before execution (so it survives Ctrl+C)
    save_session_mapping(
        &plain,
        &wt,
        &sb,
        &provider,
        &persisted_model,
        root.as_deref(),
        &session_metadata,
    );

    // Register process entry before execution so `zag ps` can see it while running.
    let proc_id = uuid::Uuid::new_v4().to_string();
    let proc_session_id = wt
        .session_id
        .clone()
        .or_else(|| sb.session_id.clone())
        .or_else(|| plain.session_id.clone());
    let proc_prompt = action_prompt(&action).map(|p| p.chars().take(100).collect::<String>());
    let proc_cmd = command_name(&action).to_string();
    // Read parent process/session info from env vars (set by a parent zag process, if nested)
    let parent_process_id = std::env::var("ZAG_PROCESS_ID").ok();
    let parent_session_id = std::env::var("ZAG_SESSION_ID").ok();

    if let Ok(mut pstore) = zag_agent::process_store::ProcessStore::load() {
        pstore.add(zag_agent::process_store::ProcessEntry {
            id: proc_id.clone(),
            pid: std::process::id(),
            session_id: proc_session_id.clone(),
            provider: provider.clone(),
            model: persisted_model.clone(),
            command: proc_cmd,
            prompt: proc_prompt,
            started_at: chrono::Utc::now().to_rfc3339(),
            status: "running".to_string(),
            exit_code: None,
            exited_at: None,
            root: root.clone(),
            parent_process_id,
            parent_session_id,
        });
        let _ = pstore.save();
    }

    // Set ZAG_* env vars so child processes (agent CLIs) can discover their identity.
    // These are inherited by spawned subprocesses automatically.
    // SAFETY: zag is single-threaded at this point (agent subprocess has not yet been spawned).
    unsafe {
        if let Some(ref sid) = proc_session_id {
            std::env::set_var("ZAG_SESSION_ID", sid);
        }
        std::env::set_var("ZAG_PROCESS_ID", &proc_id);
        std::env::set_var("ZAG_PROVIDER", &provider);
        std::env::set_var("ZAG_MODEL", &persisted_model);
        if let Some(ref r) = root {
            std::env::set_var("ZAG_ROOT", r);
        }
        if let Some(ref name) = session_metadata.name {
            std::env::set_var("ZAG_SESSION_NAME", name);
        }
    }

    // Write lifecycle started marker and prune old markers
    let lifecycle_session_id = wt
        .session_id
        .as_deref()
        .or(sb.session_id.as_deref())
        .or(plain.session_id.as_deref())
        .unwrap_or(&log_session_id)
        .to_string();
    zag_orch::lifecycle::write_started_marker(&lifecycle_session_id);
    zag_orch::lifecycle::prune_old_markers();

    // Echo session ID for `agent listen` usage
    if show_wrapper {
        let display_session_id = wt
            .session_id
            .as_deref()
            .or(sb.session_id.as_deref())
            .or(plain.session_id.as_deref())
            .unwrap_or(&log_session_id);
        println!("\x1b[33m>\x1b[0m Session: {display_session_id}");
        println!("\x1b[33m>\x1b[0m Listen:  agent listen {display_session_id}");
    }

    let initial_provider_session_id = if provider == "claude" {
        wrapper_session_id.clone()
    } else {
        resume_target
            .as_ref()
            .and_then(|target| target.entry.provider_session_id.clone())
    };
    let log_metadata = crate::session_log::SessionLogMetadata {
        provider: provider.clone(),
        wrapper_session_id: log_session_id.clone(),
        provider_session_id: initial_provider_session_id,
        workspace_path: effective_root
            .clone()
            .or_else(|| plain.workspace_path.clone())
            .or_else(|| wt.worktree_path.clone())
            .or_else(|| sb.workspace.clone()),
        command: command_name(&action).to_string(),
        model: Some(persisted_model.clone()),
        resumed: is_resume_run(&action),
        backfilled: false,
    };
    let live_ctx = crate::session_log::LiveLogContext {
        root: root.clone(),
        provider_session_id: log_metadata.provider_session_id.clone(),
        workspace_path: log_metadata.workspace_path.clone(),
        started_at: chrono::Utc::now(),
        is_worktree: wt.is_worktree_session,
    };
    let live_adapter = crate::session_log::live_adapter_for_provider(
        &provider,
        live_ctx,
        should_enable_live_session_logs(&action, json_mode),
    );
    let log_coordinator = crate::session_log::SessionLogCoordinator::start(
        &crate::session_log::logs_dir(root.as_deref()),
        log_metadata,
        live_adapter,
    )?;
    let _ = log_coordinator
        .writer()
        .set_global_index_dir(Config::global_base_dir());
    crate::session_log::record_prompt(log_coordinator.writer(), action_prompt(&action))?;
    if let Ok(log_path) = log_coordinator.writer().log_path() {
        update_session_log_metadata(
            wrapper_session_id
                .as_deref()
                .or(wt.session_id.as_deref())
                .or(sb.session_id.as_deref()),
            Some(log_path.to_string_lossy().to_string()),
            "partial",
            root.as_deref(),
        );
    }

    // Resolve --context: prepend another session's result to the prompt
    if let Some(ref ctx_session_id) = context_session {
        let context_text =
            zag_orch::collect::extract_last_assistant_message(ctx_session_id, root.as_deref());
        if let Some(context_text) = context_text {
            let prefix = format!(
                "Context from previous session ({ctx_session_id}):\n\n{context_text}\n\n---\n\n"
            );
            match &mut action {
                Commands::Exec { prompt, .. } => {
                    *prompt = format!("{prefix}{prompt}");
                }
                Commands::Run {
                    prompt: Some(p), ..
                } => {
                    *p = format!("{prefix}{p}");
                }
                _ => {}
            }
        }
    }

    // Resolve --plan: prepend a plan file's content to the prompt
    if let Some(ref plan_file) = plan_path {
        let plan_content = std::fs::read_to_string(plan_file)
            .with_context(|| format!("Failed to read plan file: {plan_file}"))?;
        let prefix =
            format!("Implementation plan:\n\n{plan_content}\n\n---\n\nFollow the plan above.\n\n");
        match &mut action {
            Commands::Exec { prompt, .. } => {
                *prompt = format!("{prefix}{prompt}");
            }
            Commands::Run {
                prompt: Some(p), ..
            } => {
                *p = format!("{prefix}{p}");
            }
            Commands::Run { prompt, .. } => {
                *prompt = Some(prefix);
            }
            _ => {}
        }
    }

    // Resolve --file: prepend file attachments to the prompt
    if !files.is_empty() {
        let attachments = files
            .iter()
            .map(|f| zag_agent::attachment::Attachment::from_path(std::path::Path::new(f)))
            .collect::<Result<Vec<_>>>()?;
        let prefix = zag_agent::attachment::format_attachments_prefix(&attachments);
        match &mut action {
            Commands::Exec { prompt, .. } => {
                *prompt = format!("{prefix}{prompt}");
            }
            Commands::Run {
                prompt: Some(p), ..
            } => {
                *p = format!("{prefix}{p}");
            }
            Commands::Run { prompt, .. } => {
                *prompt = Some(prefix);
            }
            _ => {}
        }
    }

    let is_worktree_session = wt.is_worktree_session;
    let is_interactive_worktree = wt.is_worktree_session && matches!(action, Commands::Run { .. });
    let is_interactive_sandbox = sb.is_sandbox_session && matches!(action, Commands::Run { .. });
    let is_interactive_run = matches!(action, Commands::Run { .. });

    let exec_ctx = ExecutionContext {
        provider: &provider,
        json_mode,
        json_schema: &json_schema,
        output_fmt: output_fmt_clone.as_deref(),
        show_usage,
        verbose,
    };
    let action_future = execute_action(
        action,
        &mut *agent,
        &exec_ctx,
        Some(log_coordinator.writer()),
    );
    let action_result = if let Some(ref timeout_str) = timeout {
        let duration = zag_orch::duration::parse_duration(timeout_str)?;
        match tokio::time::timeout(duration, action_future).await {
            Ok(r) => r,
            Err(_) => Err(anyhow::anyhow!("Agent timed out after {timeout_str}")),
        }
    } else {
        action_future.await
    };

    // Always run agent cleanup regardless of action result to prevent resource leaks.
    debug!("Cleaning up agent resources");
    if let Err(cleanup_err) = agent.cleanup().await {
        log::warn!("Agent cleanup failed: {cleanup_err}");
    }

    let agent_success = match &action_result {
        Err(err) => {
            // Extract structured error info if available
            let process_err = err.downcast_ref::<zag_agent::process::ProcessError>();
            let exit_code = process_err.and_then(|pe| pe.exit_code).unwrap_or(1);

            if let Ok(mut pstore) = zag_agent::process_store::ProcessStore::load() {
                pstore.update_status(&proc_id, "killed", Some(exit_code));
                let _ = pstore.save();
            }
            // Use ok() to avoid masking the original error if log finishing also fails
            if let Err(log_err) = log_coordinator.finish(false, Some(err.to_string())).await {
                log::warn!("Failed to finish session log: {log_err}");
            }
            zag_orch::lifecycle::write_ended_marker(&lifecycle_session_id, false, Some(exit_code));

            // Show the error to the user on stderr
            eprintln!("\x1b[31merror\x1b[0m: {err}");
            eprintln!("\x1b[2mRun with --debug for full details\x1b[0m");

            // Exit with structured code: 2 for provider process crash
            if process_err.is_some() {
                std::process::exit(EXIT_PROVIDER_ERROR);
            }
            return Err(anyhow::anyhow!(err.to_string()));
        }
        Ok(success) => *success,
    };

    let wrapper_session_id = wt
        .session_id
        .as_deref()
        .or(sb.session_id.as_deref())
        .or(plain.session_id.as_deref());
    // Prefer the provider session ID discovered by the live log adapter during the session.
    // Fall back to post-session discovery only if the live adapter didn't find one
    // (or found one identical to the wrapper UUID, which is not a real native ID).
    let live_discovered_id = log_coordinator.writer().get_provider_session_id();
    // Use the effective workspace path (worktree/sandbox path if applicable) for provider
    // session discovery, not plain.workspace_path which is always the original repo root.
    let discovery_workspace = effective_root
        .as_deref()
        .or(plain.workspace_path.as_deref());
    let native_session_id = live_discovered_id
        .filter(|id| wrapper_session_id.is_none_or(|wid| id != wid))
        .or_else(|| {
            discover_provider_session_id(
                &provider,
                wrapper_session_id,
                root.as_deref(),
                discovery_workspace,
            )
        });
    if let Some(ref native_id) = native_session_id {
        log_coordinator
            .writer()
            .set_provider_session_id(Some(native_id.clone()))?;
    }
    update_provider_session_id(wrapper_session_id, native_session_id, root.as_deref());
    update_session_log_metadata(
        wrapper_session_id,
        log_coordinator
            .writer()
            .log_path()
            .ok()
            .map(|path| path.to_string_lossy().to_string()),
        "partial",
        root.as_deref(),
    );
    let final_exit_code = if agent_success { 0 } else { EXIT_AGENT_FAILURE };
    log_coordinator.finish(agent_success, None).await?;
    zag_orch::lifecycle::write_ended_marker(
        &lifecycle_session_id,
        agent_success,
        Some(final_exit_code),
    );

    if let Ok(mut pstore) = zag_agent::process_store::ProcessStore::load() {
        pstore.update_status(&proc_id, "exited", Some(final_exit_code));
        let _ = pstore.save();
    }

    if !agent_success {
        eprintln!("\x1b[31merror\x1b[0m: agent exited with failure");
        eprintln!("\x1b[2mRun with --debug for full details\x1b[0m");
    }

    if show_wrapper {
        println!("\x1b[32m✓\x1b[0m Session terminated");
    }

    // Sandbox cleanup prompt
    if is_interactive_sandbox {
        if let Some(ref name) = sb.sandbox_name {
            prompt_sandbox_cleanup(
                sb.session_id.as_deref().unwrap_or(""),
                name,
                root.as_deref(),
            )?;
        }
    } else if let Some(target) = &resume_target
        && let Some(ref sandbox_name) = target.entry.sandbox_name
        && target.matched_by_wrapper_id
    {
        let sid = target.entry.session_id.as_str();
        prompt_sandbox_cleanup(sid, sandbox_name, root.as_deref())?;
    }

    // Worktree cleanup
    // For interactive sessions: auto-delete if no changes, prompt if changes exist
    // For exec sessions: auto-delete if no changes, keep if changes exist
    let cleanup_info = if is_worktree_session {
        wt.session_id
            .as_ref()
            .zip(wt.worktree_path.as_ref())
            .map(|(sid, wtp)| (sid.clone(), wtp.clone()))
    } else if let Some(target) = &resume_target {
        if target.entry.is_worktree && target.matched_by_wrapper_id {
            Some((
                target.entry.session_id.clone(),
                target.entry.worktree_path.clone(),
            ))
        } else {
            None
        }
    } else {
        None
    };

    if let Some((sid, wtp)) = cleanup_info {
        let wt_path = std::path::Path::new(&wtp);
        let has_changes = wt_path.exists()
            && (worktree::has_changes(wt_path).unwrap_or(true)
                || worktree::has_unpushed_commits(wt_path).unwrap_or(true));

        if !has_changes {
            // Auto-remove worktree with no changes
            if wt_path.exists() {
                match worktree::remove_worktree(wt_path) {
                    Ok(()) => {
                        if show_wrapper {
                            println!("\x1b[32m✓\x1b[0m Worktree removed (no changes)");
                        }
                    }
                    Err(e) => {
                        log::warn!("Failed to remove worktree: {e}");
                    }
                }
            }
            let mut store = session::SessionStore::load(root.as_deref()).unwrap_or_default();
            store.remove(&sid);
            let _ = store.save(root.as_deref());
        } else if is_interactive_worktree {
            prompt_worktree_cleanup(&sid, &wtp, root.as_deref())?;
        } else {
            // Exec with changes: keep and print resume command
            if show_wrapper {
                let store = session::SessionStore::load(root.as_deref()).unwrap_or_default();
                let provider_session_id = store
                    .find_by_session_id(&sid)
                    .and_then(|entry| entry.provider_session_id.as_deref());
                print_resume_hint(&sid, provider_session_id, "Workspace");
            }
        }
    } else if let Some(wrapper_session_id) = wrapper_session_id {
        // Plain interactive session (no worktree/sandbox): print resume hint
        if is_interactive_run && show_wrapper {
            let store = session::SessionStore::load(root.as_deref()).unwrap_or_default();
            let provider_session_id = store
                .find_by_session_id(wrapper_session_id)
                .and_then(|entry| entry.provider_session_id.clone());
            print_session_resume_hint(wrapper_session_id, provider_session_id.as_deref());
        }
    } else if is_interactive_run
        && show_wrapper
        && let Some(target) = &resume_target
        && !target.entry.is_worktree
        && target.entry.sandbox_name.is_none()
    {
        // Resumed plain session (no worktree/sandbox): print resume hint again
        let sid = &target.entry.session_id;
        let store = session::SessionStore::load(root.as_deref()).unwrap_or_default();
        let provider_session_id = store
            .find_by_session_id(sid)
            .and_then(|entry| entry.provider_session_id.clone());
        print_session_resume_hint(sid, provider_session_id.as_deref());
    }

    // Exit with code 1 if agent reported failure and --exit-on-failure is set
    if exit_on_failure && !agent_success {
        std::process::exit(EXIT_AGENT_FAILURE);
    }

    Ok(())
}