caretta 0.11.6

caretta agent
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
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
#![allow(dead_code, non_snake_case, unused_imports)]

extern crate self as agent_common;
extern crate self as agent_runtime;
extern crate self as claude;
extern crate self as cli_common;
extern crate self as cline;
extern crate self as codex;
extern crate self as copilot;
extern crate self as gemini;
extern crate self as grok;
extern crate self as junie;
extern crate self as xai;

#[path = "agent_common.rs"]
mod agent_common_impl;
use agent_common_impl::*;

#[path = "cli_common.rs"]
mod cli_common_impl;
use cli_common_impl::*;

#[path = "claude/lib.rs"]
mod claude_impl;
use claude_impl::*;

#[path = "cline/lib.rs"]
mod cline_impl;
use cline_impl::*;

#[path = "codex/lib.rs"]
mod codex_impl;
use codex_impl::*;

#[path = "copilot/lib.rs"]
mod copilot_impl;
use copilot_impl::*;

#[path = "gemini/lib.rs"]
mod gemini_impl;
use gemini_impl::*;

#[path = "grok/lib.rs"]
mod grok_impl;
use grok_impl::*;

#[path = "junie/lib.rs"]
mod junie_impl;
use junie_impl::*;

#[path = "xai/lib.rs"]
mod xai_impl;
use xai_impl::*;

#[path = "agent_runtime/lib.rs"]
mod agent_runtime_impl;
use agent_runtime_impl::*;

pub mod agent;
pub mod custom_themes;
pub mod ui;

pub use agent::types::{Agent, Config, SkillPaths};

use agent::actions::{ActionContext, lookup_action};
use agent::auto_merge::{run_auto_merge_stack, run_automerge_queue, run_branch_sync};
use agent::config_store::{
    clear_bot_private_key_pem, clear_bot_token, clear_local_inference_api_key,
    store_bot_private_key_pem, store_bot_token, store_local_inference_api_key,
};
use agent::conflicts::run_pr_conflict_fix;
use agent::shell::{
    clear_stop_request, list_all_files, parse_args, preflight, record_agent_response, request_stop,
    reset_chat_history, run_chat_send, run_code_review, run_interview_draft, run_interview_respond,
    run_loop, run_pr_review_fix, run_refresh_agents, run_refresh_docs, run_security_code_review,
    run_single_issue, run_tracker_matrix, run_workflow_draft, try_approve_pr,
};
use agent::tracker::{
    DEFAULT_REVIEW_BOT_LOGIN, PendingIssue, PrSummary, TrackerInfo, current_branch_pr,
    enable_auto_merge, fetch_unresolved_thread_counts, find_tracker, get_tracker_body,
    is_auto_merge_enabled, list_open_prs, open_pr_map_from, parse_pending,
};
use agent::types::{
    AgentEvent, BotAuthMode, ChangedFile, ClaudeEvent, ContentBlock, EVENT_SENDER, FileChangeKind,
    InterviewTurn, Workflow, save_dev_config,
};
use agent::workflow::{list_presets, load_sidebar_entries, load_workflows};
use clap::{Parser, Subcommand};
use custom_themes::Theme;
use dioxus::prelude::*;
use std::collections::HashMap;
use tokio::sync::mpsc;
use tracing::info;
use ui::components::BASE_CSS;
use ui::security::{SecurityFinding, run_security_scan};
use ui::{Editor, Sidebar, Statusbar};

#[cfg(target_arch = "wasm32")]
#[derive(serde::Deserialize)]
struct WorkflowPresetsResponse {
    presets: Vec<String>,
}

#[cfg(target_arch = "wasm32")]
#[derive(serde::Deserialize)]
struct WorkflowEntriesResponse {
    workflows: Vec<crate::agent::workflow::WorkflowEntry>,
}

#[derive(Parser)]
#[command(
    name = "caretta",
    about = "Distributed application runtime agent",
    long_about = "caretta runs agent-powered project workflows from the command line or launches the desktop UI when no subcommand is given.",
    after_help = "Examples:\n  caretta\n  caretta --agent codex code-review\n  caretta --dry-run refresh-docs\n  caretta --preset software-factory run backlog-curation\n  caretta tracker-matrix 51 --json\n  caretta models\n  caretta --agent codex models --plain\n  caretta serve --port 3000",
    version
)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,

    /// Agent CLI adapter to use when running workflows
    #[arg(long, default_value = "claude")]
    agent: agent::types::Agent,

    /// Pass adapter-specific flags that reduce permission prompts
    #[arg(long)]
    auto: bool,

    /// Print planned prompts and actions without making supported changes
    #[arg(long)]
    dry_run: bool,

    /// Workflow preset to use (overrides `workflow_preset` in caretta.toml).
    /// See `caretta presets` for the list of available presets.
    #[arg(long, value_name = "NAME")]
    preset: Option<String>,

    /// Agent model for this invocation (overrides persisted caretta dev config).
    /// `CARETTA_MODEL` applies the same override when set.
    /// Hint: `caretta --agent … models` lists bundled IDs from `assets/available-models.json`.
    #[arg(long, value_name = "MODEL")]
    model: Option<String>,

    /// Write the bundled label taxonomy to .github/labels.yml and exit
    #[arg(long)]
    create_labels: bool,
}

#[derive(Subcommand)]
enum Commands {
    /// Launch the GUI (default)
    Gui,
    /// Address review threads on a PR
    FixPr {
        /// Pull request number to inspect and update
        #[arg(value_name = "PR")]
        pr: u32,
    },
    /// Resolve merge conflicts on a PR branch after a caretta branch-sync marker
    FixConflicts {
        /// Pull request number to inspect and update
        #[arg(value_name = "PR")]
        pr: u32,
    },
    /// Approve a PR if all bot-authored review threads are resolved
    /// and the current reviewDecision is CHANGES_REQUESTED.
    ApprovePr {
        /// Pull request number to evaluate
        #[arg(value_name = "PR")]
        pr: u32,
    },
    /// Squash-merge approved `agent/issue-*` PRs respecting tracker/stack lineage (`auto_merge` workflow)
    #[command(name = "auto-merge")]
    AutoMerge {
        /// Tracker issue supplying deterministic execution order (`pending_issues_execution_order`)
        #[arg(long, value_name = "NUMBER")]
        tracker: Option<u32>,
        /// Merge each approved PR's base into its branch, then enable squash auto-merge (GitHub `--auto`) instead of merging immediately.
        #[arg(long, conflicts_with = "sync_branches")]
        automerge_queue: bool,
        /// Align bases and update all open non-draft agent/issue-* PR branches without merging.
        #[arg(long, conflicts_with = "automerge_queue")]
        sync_branches: bool,
    },
    /// Run ideation draft
    Ideation,
    /// Run UXR synthesis draft
    UxrSynth,
    /// Run strategic review draft
    StrategicReview,
    /// Run roadmapper draft
    Roadmapper,
    /// Run sprint planning draft
    SprintPlanning,
    /// Run retrospective draft
    Retrospective,
    /// Run housekeeping draft
    Housekeeping,
    /// Run user interview
    Interview,
    /// Run code review on every open PR, or a single PR when a number is given
    CodeReview {
        /// Pull request number (omit to review all open PRs)
        #[arg(value_name = "PR")]
        pr: Option<u32>,
    },
    /// Run security code review
    SecurityReview,
    /// Refresh agent files
    RefreshAgents,
    /// Refresh project documentation
    RefreshDocs,
    /// Run a single issue
    Issue {
        /// Tracker issue whose body lists this work item (for blocker → branch chaining)
        #[arg(long, value_name = "TRACKER_ISSUE")]
        tracker: Option<u32>,
        /// Issue number to run
        #[arg(value_name = "NUMBER")]
        number: u32,
    },
    /// Run the main loop for a tracker
    Loop {
        /// Tracker issue number to process
        #[arg(value_name = "TRACKER")]
        tracker: u32,
    },
    /// Print pending issue numbers for a tracker (for CI matrix generation)
    TrackerMatrix {
        /// Tracker issue number to read
        #[arg(value_name = "TRACKER")]
        tracker: u32,
        /// Emit a JSON array of issue numbers on stdout
        #[arg(long)]
        json: bool,
    },
    /// Serve the web UI via a local HTTP server
    Serve {
        /// Port for the local HTTP server
        #[arg(long, default_value = "8080")]
        port: u16,
    },
    /// List available workflow presets, or the workflows inside one preset
    Presets {
        /// If given, list the workflows inside this preset instead of all presets
        #[arg(value_name = "NAME")]
        name: Option<String>,
    },
    /// Run any workflow from the active preset by ID (accepts hyphen or underscore form)
    Run {
        /// Workflow ID, e.g. `backlog-curation` or `backlog_curation`.
        /// See `caretta presets <NAME>` for available IDs.
        #[arg(value_name = "WORKFLOW")]
        workflow: String,
    },
    /// List bundled model IDs and labels for `--model` / `CARETTA_MODEL`
    ///
    /// Data comes from assets/available-models.json (regenerate: cargo build -p caretta-agent-runtime).
    Models {
        /// Print only model IDs, one per line (for shell completion).
        #[arg(long)]
        plain: bool,
        /// List every adapter; with `--plain`, deduplicate IDs across adapters.
        #[arg(long)]
        all: bool,
    },
}

/// Standalone entry point — equivalent to `run_with_overrides(|_| {})`.
/// Used by the `caretta` binary.
pub fn run() {
    run_with_overrides(|_| {});
}

/// Library entry point for consumers that want to inject custom `Config`
/// fields (e.g. a project-specific skill layout) before the agent runs.
///
/// The closure receives a mutable `Config` populated from CLI args, env vars,
/// and `caretta.toml`. Mutate it however you need; caretta then dispatches to
/// either the GUI or the requested CLI subcommand.
pub fn run_with_overrides<F>(overrides: F)
where
    F: FnOnce(&mut Config),
{
    #[cfg(target_arch = "wasm32")]
    {
        let mut config = parse_args();
        overrides(&mut config);
        CONFIG_OVERRIDE
            .set(config)
            .expect("failed to set CONFIG_OVERRIDE in wasm32");
        dioxus::launch(App);
    }

    #[cfg(not(target_arch = "wasm32"))]
    {
        let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
            .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
        tracing_subscriber::fmt().with_env_filter(env_filter).init();

        let cli = Cli::parse();

        if cli.create_labels {
            let config = parse_args();
            let content =
                agent::assets::LABELS_YML.replace("{{project_name}}", &config.project_name);
            let dir = std::path::Path::new(".github");
            let _ = std::fs::create_dir_all(dir);
            let dest = dir.join("labels.yml");
            std::fs::write(&dest, content).expect("failed to write .github/labels.yml");
            println!("wrote {}", dest.display());
            return;
        }

        let mut config = parse_args();
        config.agent = cli.agent;
        // Load persisted model for the selected agent.
        let dev_cfg = agent::types::load_dev_config(&config.root);
        config.model = dev_cfg
            .agent_models
            .get(&config.agent.to_string())
            .cloned()
            .unwrap_or_default();
        config.auto_mode = cli.auto;
        config.dry_run = cli.dry_run;
        overrides(&mut config);

        apply_caretta_model_env_and_cli(&mut config, cli.model.as_deref());

        // CLI `--preset` wins over caretta.toml and library overrides — fail fast
        // with the available list if the name doesn't match a real preset dir.
        if let Some(preset) = &cli.preset {
            let available = list_presets(&config.root);
            if !available.iter().any(|p| p == preset) {
                eprintln!(
                    "unknown preset: {preset}\navailable presets: {}",
                    available.join(", ")
                );
                std::process::exit(2);
            }
            config.workflow_preset = preset.clone();
        }

        // Eagerly populate the issue-comment trigger cache from the (possibly
        // overridden) skill path so the sidebar reminder is non-empty before the
        // first render. Done after `overrides` so consumers' custom paths win.
        ui::sidebar::init_issue_comment_triggers(std::path::Path::new(
            &config.skill_paths.issue_tracking,
        ));

        match cli.command {
            Some(Commands::FixPr { pr }) => {
                run_pr_review_fix(&config, pr);
            }
            Some(Commands::FixConflicts { pr }) => {
                run_pr_conflict_fix(&config, pr);
            }
            Some(Commands::ApprovePr { pr }) => {
                try_approve_pr(&config, pr);
            }
            Some(Commands::AutoMerge {
                tracker,
                automerge_queue,
                sync_branches,
            }) => {
                if sync_branches {
                    run_branch_sync(&config, tracker);
                } else if automerge_queue {
                    run_automerge_queue(&config, tracker);
                } else {
                    run_auto_merge_stack(&config, tracker);
                }
            }
            Some(Commands::Ideation) => run_workflow_draft(&config, "ideation"),
            Some(Commands::UxrSynth) => run_workflow_draft(&config, "report_research"),
            Some(Commands::StrategicReview) => run_workflow_draft(&config, "strategic_review"),
            Some(Commands::Roadmapper) => run_workflow_draft(&config, "roadmapper"),
            Some(Commands::SprintPlanning) => run_workflow_draft(&config, "sprint_planning"),
            Some(Commands::Retrospective) => run_workflow_draft(&config, "retrospective"),
            Some(Commands::Housekeeping) => run_workflow_draft(&config, "housekeeping"),
            Some(Commands::Interview) => run_interview_draft(&config),
            Some(Commands::CodeReview { pr }) => run_code_review(&config, pr),
            Some(Commands::SecurityReview) => run_security_code_review(&config),
            Some(Commands::RefreshAgents) => run_refresh_agents(&config),
            Some(Commands::RefreshDocs) => run_refresh_docs(&config),
            Some(Commands::Issue { number, tracker }) => {
                let tracker_num = tracker
                    .or_else(|| find_tracker().first().map(|t| t.number))
                    .unwrap_or(0);
                let blockers = if tracker_num != 0 {
                    let body = get_tracker_body(tracker_num);
                    parse_pending(&body)
                        .into_iter()
                        .find(|p| p.number == number)
                        .map(|p| p.blockers)
                        .unwrap_or_default()
                } else {
                    Vec::new()
                };
                run_single_issue(&config, tracker_num, number, &blockers);
            }
            Some(Commands::Loop { tracker }) => run_loop(&config, tracker),
            Some(Commands::TrackerMatrix { tracker, json }) => {
                run_tracker_matrix(&config, tracker, json);
            }
            Some(Commands::Run { workflow }) => {
                let workflows = load_workflows(&config.root, &config.workflow_preset);
                let normalized = workflow.replace('-', "_");
                let resolved = workflows
                    .get(workflow.as_str())
                    .or_else(|| workflows.get(normalized.as_str()));
                match resolved {
                    Some(wf) => {
                        let id = wf.id.clone();
                        let norm_id = id.replace('-', "_");
                        if let Some(action) = lookup_action(norm_id.as_str()) {
                            let mut ctx = ActionContext::new(norm_id.as_str());
                            match action(&config, &mut ctx) {
                                Ok(()) => {}
                                Err(e) => {
                                    eprintln!("workflow action '{id}' failed: {e}");
                                    std::process::exit(1);
                                }
                            }
                        } else {
                            run_workflow_draft(&config, &id);
                        }
                    }
                    None => {
                        let mut ids: Vec<&str> =
                            workflows.values().map(|w| w.id.as_str()).collect();
                        ids.sort();
                        eprintln!(
                            "unknown workflow: {workflow}\npreset: {}\navailable workflows: {}",
                            config.workflow_preset,
                            ids.join(", ")
                        );
                        std::process::exit(2);
                    }
                }
            }
            Some(Commands::Models { plain, all }) => {
                agent::models_catalog::run_models_list(cli.agent, plain, all);
            }
            Some(Commands::Presets { name }) => match name {
                None => {
                    let active = &config.workflow_preset;
                    for preset in list_presets(&config.root) {
                        if &preset == active {
                            println!("{preset} (active)");
                        } else {
                            println!("{preset}");
                        }
                    }
                }
                Some(preset) => {
                    let available = list_presets(&config.root);
                    if !available.iter().any(|p| p == &preset) {
                        eprintln!(
                            "unknown preset: {preset}\navailable presets: {}",
                            available.join(", ")
                        );
                        std::process::exit(2);
                    }
                    let workflows = load_workflows(&config.root, &preset);
                    if workflows.is_empty() {
                        eprintln!("preset {preset} has no workflows");
                        std::process::exit(1);
                    }
                    let mut entries: Vec<_> = workflows.values().collect();
                    entries.sort_by(|a, b| {
                        a.ui.category
                            .cmp(&b.ui.category)
                            .then_with(|| a.id.cmp(&b.id))
                    });
                    let id_w = entries.iter().map(|w| w.id.len()).max().unwrap_or(0);
                    let cat_w = entries
                        .iter()
                        .map(|w| w.ui.category.len())
                        .max()
                        .unwrap_or(0);
                    for wf in entries {
                        let hidden = if wf.ui.visible { "" } else { " (hidden)" };
                        let desc = if wf.description.is_empty() {
                            wf.name.as_str()
                        } else {
                            wf.description.as_str()
                        };
                        println!(
                            "{:id_w$}  {:cat_w$}  {desc}{hidden}",
                            wf.id,
                            wf.ui.category,
                            id_w = id_w,
                            cat_w = cat_w,
                        );
                    }
                }
            },
            Some(Commands::Serve { port }) => {
                info!(
                    "Launching API/web server for root={} with requested_port={}",
                    config.root, port
                );
                let rt = tokio::runtime::Runtime::new().unwrap();
                rt.block_on(async {
                    if let Err(e) = ui::server::serve(config.root.clone(), port).await {
                        eprintln!("Error: {}", e);
                        std::process::exit(1);
                    }
                });
            }
            Some(Commands::Gui) | None => {
                // Stash the finalised Config so the Dioxus App component can pick
                // it up via `parse_args` (which already loads from caretta.toml). The
                // App's own use of `parse_args()` would otherwise lose the
                // overrides — but since the overrides are also persisted via the
                // explicit init_issue_comment_triggers call above, the only place
                // overrides matter inside the GUI is the next `parse_args` call,
                // which already reads `caretta.toml`. Library consumers who need to
                // inject overrides that aren't expressible in `caretta.toml` should
                // use the CLI subcommands instead of the GUI.
                CONFIG_OVERRIDE
                    .set(config)
                    .expect("CONFIG_OVERRIDE set twice");
                dioxus::launch(App);
            }
        }
    }
}

/// Process-wide handoff for `run_with_overrides` → `App`. The Dioxus App
/// component reads from this on first render so library consumers' overrides
/// (e.g. custom `skill_paths`) survive into the GUI rather than being
/// re-derived from `caretta.toml` alone.
static CONFIG_OVERRIDE: std::sync::OnceLock<Config> = std::sync::OnceLock::new();

fn apply_nonempty_model_trim(into: &mut String, candidate: Option<&str>) {
    if let Some(m) = candidate {
        let m = m.trim();
        if !m.is_empty() {
            *into = m.to_string();
        }
    }
}

fn apply_caretta_model_env_and_cli(config: &mut Config, cli_model: Option<&str>) {
    apply_nonempty_model_trim(
        &mut config.model,
        std::env::var("CARETTA_MODEL").ok().as_deref(),
    );
    apply_nonempty_model_trim(&mut config.model, cli_model);
}

fn ensure_default_workflow_preset_first(mut presets: Vec<String>) -> Vec<String> {
    if !presets.iter().any(|preset| preset == "default") {
        presets.push("default".to_string());
    }
    if let Some(default_pos) = presets.iter().position(|preset| preset == "default") {
        presets.remove(default_pos);
    }
    presets.insert(0, "default".to_string());
    presets.dedup();
    presets
}

#[component]
fn App() -> Element {
    let mut config = use_signal(|| {
        // Prefer the override stashed by `run_with_overrides` so any custom
        // `skill_paths` / `bootstrap_agent_files` survive into the GUI.
        CONFIG_OVERRIDE.get().cloned().unwrap_or_else(parse_args)
    });
    let mut tracker_ids = use_signal(Vec::<TrackerInfo>::new);
    let mut issues = use_signal(Vec::<PendingIssue>::new);
    let mut is_working = use_signal(|| false);
    let mut awaiting_feedback = use_signal(|| None::<Workflow>);
    let mut feedback_text = use_signal(String::new);
    let mut events = use_signal(Vec::<AgentEvent>::new);
    let mut changed_files = use_signal(Vec::<ChangedFile>::new);
    let mut pr_map_sig = use_signal(HashMap::<u32, u32>::new);
    let mut pull_requests = use_signal(Vec::<PrSummary>::new);
    let mut security_findings = use_signal(Vec::<SecurityFinding>::new);
    let mut interview_turns = use_signal(Vec::<InterviewTurn>::new);
    let mut interview_active = use_signal(|| false);
    let mut interview_done = use_signal(|| false);
    let mut interview_agent_buf = use_signal(String::new);
    let mut chat_turns = use_signal(Vec::<InterviewTurn>::new);
    let mut chat_active = use_signal(|| false);
    let mut chat_agent_buf = use_signal(String::new);
    let mut settings_status = use_signal(|| None::<String>);
    let root_sig = use_signal(|| config.read().root.clone());
    let persona_skill_path_sig = use_signal(|| config.read().skill_paths.user_personas.clone());
    let mut all_files = use_signal(Vec::<String>::new);

    #[cfg(not(target_arch = "wasm32"))]
    let _ = use_resource(move || async move {
        let r = root_sig.read().clone();
        let files = tokio::task::spawn_blocking(move || list_all_files(&r))
            .await
            .unwrap_or_default();
        all_files.set(files);
    });

    let mut auto_merge_enabled = use_signal(|| false);
    let expand_all = use_signal(|| false);
    let follow_mode = use_signal(|| true);
    let bottom_el = use_signal(|| None::<std::rc::Rc<MountedData>>);
    let mut theme = use_signal(Theme::tokyo_night);
    let mut presets = use_signal(|| vec!["default".to_string()]);
    let mut workflow_entries = use_signal(Vec::<crate::agent::workflow::WorkflowEntry>::new);

    use_effect(move || {
        #[cfg(not(target_arch = "wasm32"))]
        {
            preflight(&config.read());
        }

        // Initialize channel if not already done
        if EVENT_SENDER.get().is_none() {
            let (tx, mut rx) = mpsc::unbounded_channel::<AgentEvent>();
            let _ = EVENT_SENDER.set(tx);

            #[cfg(not(target_arch = "wasm32"))]
            {
                // Initialize auto-merge state from actual PR (desktop only)
                spawn(async move {
                    let enabled = tokio::task::spawn_blocking(|| {
                        current_branch_pr()
                            .map(|pr| is_auto_merge_enabled(pr.number))
                            .unwrap_or(false)
                    })
                    .await
                    .unwrap_or(false);
                    auto_merge_enabled.set(enabled);
                });
            }

            // Spawn task to listen for events, update UI, and auto-scroll
            spawn(async move {
                while let Some(ev) = rx.recv().await {
                    match &ev {
                        AgentEvent::Done => {
                            // Flush interview agent buffer if active.
                            if *interview_active.peek() {
                                let buf = interview_agent_buf.peek().clone();
                                if !buf.trim().is_empty() {
                                    interview_turns.write().push(InterviewTurn {
                                        is_agent: true,
                                        content: buf,
                                    });
                                }
                                interview_agent_buf.set(String::new());
                                interview_done.set(true);
                                interview_active.set(false);
                            }
                            // Flush chat agent buffer if active.
                            if *chat_active.peek() {
                                let buf = chat_agent_buf.peek().clone();
                                if !buf.trim().is_empty() {
                                    record_agent_response(&buf);
                                    chat_turns.write().push(InterviewTurn {
                                        is_agent: true,
                                        content: buf,
                                    });
                                }
                                chat_agent_buf.set(String::new());
                                chat_active.set(false);
                            }
                            is_working.set(false);
                            awaiting_feedback.set(None);
                            clear_stop_request();
                            continue;
                        }
                        AgentEvent::AwaitingFeedback(wf) => {
                            // Flush interview agent buffer as a dialog turn.
                            if *wf == Workflow::Interview {
                                let buf = interview_agent_buf.peek().clone();
                                if !buf.trim().is_empty() {
                                    interview_turns.write().push(InterviewTurn {
                                        is_agent: true,
                                        content: buf,
                                    });
                                }
                                interview_agent_buf.set(String::new());
                            }
                            // Flush chat agent buffer as a dialog turn.
                            if *wf == Workflow::Chat {
                                let buf = chat_agent_buf.peek().clone();
                                if !buf.trim().is_empty() {
                                    record_agent_response(&buf);
                                    chat_turns.write().push(InterviewTurn {
                                        is_agent: true,
                                        content: buf,
                                    });
                                }
                                chat_agent_buf.set(String::new());
                            }
                            is_working.set(false);
                            awaiting_feedback.set(Some(*wf));
                            feedback_text.set(String::new());
                            clear_stop_request();
                            continue;
                        }
                        AgentEvent::TrackerUpdate(pending) => {
                            issues.set(pending.clone());
                            continue;
                        }
                        _ => {}
                    }
                    // Accumulate agent text into the interview buffer.
                    if *interview_active.peek()
                        && let AgentEvent::Claude(ClaudeEvent::Assistant { ref message }) = ev
                    {
                        for block in &message.content {
                            if let ContentBlock::Text { text } = block {
                                let mut buf = interview_agent_buf.write();
                                if !buf.is_empty() {
                                    buf.push('\n');
                                }
                                buf.push_str(text);
                            }
                        }
                    }
                    // Accumulate agent text into the chat buffer.
                    if *chat_active.peek()
                        && let AgentEvent::Claude(ClaudeEvent::Assistant { ref message }) = ev
                    {
                        for block in &message.content {
                            if let ContentBlock::Text { text } = block {
                                let mut buf = chat_agent_buf.write();
                                if !buf.is_empty() {
                                    buf.push('\n');
                                }
                                buf.push_str(text);
                            }
                        }
                    }
                    // Extract file changes from tool use events
                    if let AgentEvent::Claude(ClaudeEvent::Assistant { ref message }) = ev {
                        for block in &message.content {
                            if let ContentBlock::ToolUse { name, input, .. } = block {
                                let (path, kind) = match name.as_str() {
                                    "Read" => (
                                        input.get("file_path").and_then(|v| v.as_str()),
                                        FileChangeKind::Read,
                                    ),
                                    "Write" => (
                                        input.get("file_path").and_then(|v| v.as_str()),
                                        FileChangeKind::Created,
                                    ),
                                    "Edit" => (
                                        input.get("file_path").and_then(|v| v.as_str()),
                                        FileChangeKind::Modified,
                                    ),
                                    _ => (None, FileChangeKind::Read),
                                };
                                if let Some(p) = path {
                                    let mut files = changed_files.write();
                                    // Update existing entry or add new
                                    if let Some(existing) = files.iter_mut().find(|f| f.path == p) {
                                        // Upgrade: Read -> Modified/Created
                                        if kind != FileChangeKind::Read {
                                            existing.kind = kind;
                                        }
                                    } else {
                                        files.push(ChangedFile {
                                            path: p.to_string(),
                                            kind,
                                        });
                                    }
                                }
                            }
                        }
                    }
                    events.write().push(ev);
                    if *follow_mode.peek()
                        && let Some(el) = bottom_el.peek().as_ref()
                    {
                        let _ = el.scroll_to(ScrollBehavior::Instant).await;
                    }
                }
            });
        }
    });

    // Load presets and workflows on app start
    use_effect(move || {
        #[cfg(target_arch = "wasm32")]
        {
            let mut config_signal = config;
            let mut preset_signal = presets;
            // Fetch presets from API
            spawn(async move {
                if let Ok(response) = gloo_net::http::Request::get("/api/workflows/presets")
                    .send()
                    .await
                {
                    if let Ok(text) = response.text().await {
                        if let Ok(mut json) = serde_json::from_str::<WorkflowPresetsResponse>(&text)
                        {
                            let mut values = std::mem::take(&mut json.presets);
                            let presets = if values.is_empty() {
                                vec!["default".to_string()]
                            } else {
                                ensure_default_workflow_preset_first(values)
                            };
                            let default_preset = presets
                                .first()
                                .cloned()
                                .unwrap_or_else(|| "default".to_string());
                            {
                                let current = config_signal.read().workflow_preset.clone();
                                if !presets.iter().any(|p| p == &current) {
                                    config_signal.write().workflow_preset = default_preset.clone();
                                }
                            }
                            preset_signal.set(presets);
                        }
                    }
                }
            });
        }

        #[cfg(not(target_arch = "wasm32"))]
        {
            let cfg = config.read().clone();
            let preset_list = ensure_default_workflow_preset_first(list_presets(&cfg.root));
            let default_preset = preset_list
                .first()
                .cloned()
                .unwrap_or_else(|| "default".to_string());
            if !preset_list.iter().any(|p| p == &cfg.workflow_preset) {
                config.write().workflow_preset = default_preset;
            }
            presets.set(preset_list);
        }
    });

    // Fetch workflows from API in web mode, or from filesystem in desktop mode
    use_effect(move || {
        let preset = config.read().workflow_preset.clone();

        #[cfg(target_arch = "wasm32")]
        {
            spawn(async move {
                if let Ok(response) =
                    gloo_net::http::Request::get(&format!("/api/workflows/{}", preset))
                        .send()
                        .await
                {
                    if let Ok(text) = response.text().await {
                        if let Ok(json) = serde_json::from_str::<WorkflowEntriesResponse>(&text) {
                            workflow_entries.set(json.workflows);
                        }
                    }
                }
            });
        }

        #[cfg(not(target_arch = "wasm32"))]
        {
            let cfg = config.read().clone();
            let entries = load_sidebar_entries(&cfg.root, &preset);
            workflow_entries.set(entries);
        }
    });

    #[cfg(not(target_arch = "wasm32"))]
    let refresh_tracker = move |_: MouseEvent| {
        info!("Refreshing trackers...");
        let infos = find_tracker();
        tracker_ids.set(infos.clone());
        let mut all_pending = Vec::new();
        for info in &infos {
            let body = get_tracker_body(info.number);
            all_pending.extend(parse_pending(&body));
        }
        all_pending.sort_by_key(|i| i.number);
        all_pending.dedup_by_key(|i| i.number);
        let mut prs = list_open_prs();
        let counts = fetch_unresolved_thread_counts(DEFAULT_REVIEW_BOT_LOGIN);
        for pr in &mut prs {
            pr.unresolved_thread_count = counts.get(&pr.number).copied().unwrap_or(0);
        }
        let pr_map = open_pr_map_from(&prs);
        pr_map_sig.set(pr_map);
        pull_requests.set(prs);
        issues.set(all_pending);
    };

    #[cfg(target_arch = "wasm32")]
    let refresh_tracker = move |_: MouseEvent| {
        info!("Tracker refresh not available in web mode");
    };

    #[cfg(not(target_arch = "wasm32"))]
    let start_work = move |tracker_num: u32| {
        clear_stop_request();
        is_working.set(true);
        changed_files.write().clear();
        let cfg = config.read().clone();
        tokio::spawn(async move {
            run_loop(&cfg, tracker_num);
        });
    };

    #[cfg(target_arch = "wasm32")]
    let start_work = move |_tracker_num: u32| {
        info!("Tracker work not available in web mode");
    };

    #[cfg(not(target_arch = "wasm32"))]
    let start_single_issue = move |issue_num: u32| {
        clear_stop_request();
        is_working.set(true);
        changed_files.write().clear();
        let cfg = config.read().clone();
        tokio::spawn(async move {
            let trackers = find_tracker();
            let tracker_num = trackers.first().map(|t| t.number).unwrap_or(0);
            let blockers = if tracker_num != 0 {
                let body = get_tracker_body(tracker_num);
                parse_pending(&body)
                    .into_iter()
                    .find(|p| p.number == issue_num)
                    .map(|p| p.blockers)
                    .unwrap_or_default()
            } else {
                Vec::new()
            };
            run_single_issue(&cfg, tracker_num, issue_num, &blockers);
        });
    };

    #[cfg(target_arch = "wasm32")]
    let start_single_issue = move |_issue_num: u32| {
        info!("Single issue work not available in web mode");
    };

    #[cfg(not(target_arch = "wasm32"))]
    let start_pr_fix = move |pr_num: u32| {
        clear_stop_request();
        is_working.set(true);
        changed_files.write().clear();
        let cfg = config.read().clone();
        tokio::spawn(async move {
            run_pr_review_fix(&cfg, pr_num);
        });
    };

    #[cfg(target_arch = "wasm32")]
    let start_pr_fix = move |_pr_num: u32| {
        info!("PR fix work not available in web mode");
    };

    #[cfg(not(target_arch = "wasm32"))]
    let on_preset_change = move |preset: String| {
        config.write().workflow_preset = preset.clone();
        workflow_entries.set(load_sidebar_entries(&config.read().root, &preset));
    };

    #[cfg(target_arch = "wasm32")]
    let on_preset_change = move |preset: String| {
        config.write().workflow_preset = preset.clone();
        // Re-fetch workflows from API with new preset
        let preset_clone = preset.clone();
        spawn(async move {
            if let Ok(response) =
                gloo_net::http::Request::get(&format!("/api/workflows/{}", preset_clone))
                    .send()
                    .await
            {
                if let Ok(text) = response.text().await {
                    if let Ok(json) = serde_json::from_str::<WorkflowEntriesResponse>(&text) {
                        workflow_entries.set(json.workflows);
                    }
                }
            }
        });
    };

    #[cfg(not(target_arch = "wasm32"))]
    let on_start_workflow = move |workflow_id: String| {
        clear_stop_request();
        let cfg = config.read().clone();

        // Chat mode: free-form conversation, no workflow commitment.
        if workflow_id == "chat" {
            // Reset if starting fresh (not continuing).
            if *awaiting_feedback.read() != Some(Workflow::Chat) {
                reset_chat_history();
                chat_turns.write().clear();
                chat_agent_buf.set(String::new());
            }
            chat_active.set(true);
            // Signal the UI to show the chat tab with the input ready.
            awaiting_feedback.set(Some(Workflow::Chat));
            feedback_text.set(String::new());
            return;
        }

        // Interview has special state management.
        if workflow_id == "interview" {
            is_working.set(true);
            interview_turns.write().clear();
            interview_agent_buf.set(String::new());
            interview_active.set(true);
            interview_done.set(false);
            tokio::spawn(async move {
                run_interview_draft(&cfg);
            });
            return;
        }

        // Security scan (local, non-agent).
        if workflow_id == "security_scan" {
            let root = cfg.root.clone();
            let targets = cfg.scan_targets.clone();
            info!("Running security review scan...");
            spawn(async move {
                let findings =
                    tokio::task::spawn_blocking(move || run_security_scan(&root, &targets))
                        .await
                        .unwrap_or_default();
                info!("Security scan complete: {} findings", findings.len());
                security_findings.set(findings);
            });
            return;
        }

        // Auto merge (lineage-aware; same runner as CLI `caretta auto-merge`).
        if workflow_id == "auto_merge" || workflow_id == "auto-merge" {
            info!("Running lineage-aware auto-merge pass…");
            spawn(async move {
                let cfg_inner = cfg.clone();
                if let Err(e) =
                    tokio::task::spawn_blocking(move || run_auto_merge_stack(&cfg_inner, None))
                        .await
                {
                    info!("Auto-merge lineage task failed: {e}");
                } else {
                    info!("Auto-merge lineage task finished.");
                }
            });
            return;
        }

        is_working.set(true);

        // Look up a registered action runner, otherwise use the generic YAML runner.
        let norm_workflow_id = workflow_id.replace('-', "_");
        if let Some(action) = lookup_action(norm_workflow_id.as_str()) {
            let action = *action;
            tokio::spawn(async move {
                let mut ctx = ActionContext::new(norm_workflow_id.as_str());
                if let Err(e) = action(&cfg, &mut ctx) {
                    agent::shell::log(&format!("Workflow '{}' failed: {e}", ctx.workflow_id));
                }
            });
        } else {
            tokio::spawn(async move { run_workflow_draft(&cfg, &workflow_id) });
        }
    };

    #[cfg(target_arch = "wasm32")]
    let on_start_workflow = move |workflow_id: String| {
        info!(
            "Workflow execution not available in web mode: {}",
            workflow_id
        );
    };

    #[cfg(not(target_arch = "wasm32"))]
    let save_settings = move |_: MouseEvent| {
        let cfg = config.read().clone();
        settings_status.set(Some("Saving configuration...".into()));
        spawn(async move {
            let root = cfg.root.clone();
            let result = tokio::task::spawn_blocking(move || {
                save_dev_config(&root, &cfg)?;

                match cfg.bot_settings.mode {
                    BotAuthMode::Token => {
                        let token = cfg.bot_settings.token.trim();
                        if token.is_empty() {
                            clear_bot_token(&root).map_err(|e| e.to_string())?;
                        } else {
                            store_bot_token(&root, token).map_err(|e| e.to_string())?;
                        }
                        clear_bot_private_key_pem(&root).map_err(|e| e.to_string())?;
                    }
                    BotAuthMode::GitHubApp => {
                        clear_bot_token(&root).map_err(|e| e.to_string())?;
                        let pem = cfg.bot_settings.private_key_pem.trim();
                        if pem.is_empty() {
                            clear_bot_private_key_pem(&root).map_err(|e| e.to_string())?;
                        } else {
                            store_bot_private_key_pem(&root, pem).map_err(|e| e.to_string())?;
                        }
                    }
                    BotAuthMode::Disabled => {
                        clear_bot_token(&root).map_err(|e| e.to_string())?;
                        clear_bot_private_key_pem(&root).map_err(|e| e.to_string())?;
                    }
                }

                let api_key = cfg.local_inference.api_key.trim();
                if api_key.is_empty() {
                    clear_local_inference_api_key(&root).map_err(|e| e.to_string())?;
                } else {
                    store_local_inference_api_key(&root, api_key).map_err(|e| e.to_string())?;
                }

                Ok::<(), String>(())
            })
            .await
            .map_err(|e| e.to_string())
            .and_then(|r| r);

            match result {
                Ok(()) => settings_status.set(Some(
                    "Configuration saved. Secrets use the OS credential vault.".into(),
                )),
                Err(err) => {
                    settings_status.set(Some(format!("Failed to save configuration: {err}")))
                }
            }
        });
    };

    #[cfg(target_arch = "wasm32")]
    let save_settings = move |_: MouseEvent| {
        info!("Configuration saving not available in web mode");
    };

    #[cfg(not(target_arch = "wasm32"))]
    let submit_feedback = move |_: MouseEvent| {
        let fb = feedback_text.read().clone();
        if fb.trim().is_empty() {
            return;
        }
        clear_stop_request();
        let wf = *awaiting_feedback.read();
        awaiting_feedback.set(None);
        is_working.set(true);

        // Record user answer as interview turn.
        if wf == Some(Workflow::Interview) {
            interview_turns.write().push(InterviewTurn {
                is_agent: false,
                content: fb.clone(),
            });
        }

        // Record user message as chat turn.
        if wf == Some(Workflow::Chat) {
            chat_turns.write().push(InterviewTurn {
                is_agent: false,
                content: fb.clone(),
            });
        }

        let cfg = config.read().clone();
        tokio::spawn(async move {
            match wf {
                Some(Workflow::Interview) => run_interview_respond(&cfg, &fb),
                Some(Workflow::Chat) => run_chat_send(&cfg, &fb),
                Some(w) => {
                    use agent::shell::run_workflow_finalize;
                    run_workflow_finalize(&cfg, w.to_id(), &fb);
                }
                None => {}
            }
        });
    };

    #[cfg(target_arch = "wasm32")]
    let submit_feedback = move |_: MouseEvent| {
        info!("Feedback submission not available in web mode");
    };

    let stop_work = move |_: MouseEvent| {
        request_stop();
        is_working.set(false);
    };

    #[cfg(not(target_arch = "wasm32"))]
    let on_auto_merge = move |_: MouseEvent| {
        auto_merge_enabled.set(true); // Optimistic guard against double-click
        spawn(async move {
            let result = tokio::task::spawn_blocking(move || {
                if let Some(pr) = current_branch_pr() {
                    if is_auto_merge_enabled(pr.number) {
                        return true;
                    }
                    return enable_auto_merge(pr.number);
                }
                false
            })
            .await;
            match result {
                Ok(true) => auto_merge_enabled.set(true),
                Ok(false) => {
                    info!("Failed to enable auto-merge");
                    auto_merge_enabled.set(false);
                }
                Err(e) => {
                    info!("Auto-merge task failed: {e}");
                    auto_merge_enabled.set(false);
                }
            }
        });
    };

    #[cfg(target_arch = "wasm32")]
    let on_auto_merge = move |_: MouseEvent| {
        info!("Auto-merge not available in web mode");
    };

    let css = format!("{vars}\n{BASE_CSS}", vars = theme.read().to_css_vars());

    rsx! {
        style { "{css}" }

        div { class: "ide",
            // ── Title bar ──
            div { class: "titlebar",
                div { class: "titlebar-left",
                    span { class: "titlebar-icon", ">" }
                    span { class: "titlebar-name", "{config.read().project_name} Dev Agent" }
                }
                div { class: "titlebar-right",
                    select {
                        class: "titlebar-select",
                        value: "{theme.read().name}",
                        onchange: move |evt| {
                            if let Some(t) = Theme::by_name(&evt.value()) {
                                theme.set(t);
                            }
                        },
                        for t in Theme::all() {
                            option { value: "{t.name}", "{t.name}" }
                        }
                    }
                }
            }

            // ── Main body: sidebar + editor ──
            div { class: "ide-body",
                Sidebar {
                    config,
                    tracker_ids,
                    issues,
                    pull_requests,
                    pr_map: pr_map_sig,
                    is_working,
                    awaiting_feedback,
                    feedback_text,
                    auto_merge_enabled,
                    settings_status,
                    refresh_tracker,
                    start_work,
                    start_single_issue,
                    start_pr_fix,
                    workflow_entries,
                    presets,
                    on_preset_change,
                    on_start_workflow,
                    save_settings,
                    stop_work,
                    submit_feedback,
                    on_auto_merge,
                }

                Editor {
                    events,
                    changed_files,
                    all_files,
                    security_findings,
                    interview_turns,
                    interview_active,
                    interview_done,
                    chat_turns,
                    chat_active,
                    awaiting_feedback,
                    is_working,
                    feedback_text,
                    submit_feedback,
                    root: root_sig,
                    persona_skill_path: persona_skill_path_sig,
                    follow_mode,
                    expand_all,
                    usage_model: config.read().pricing_model_key(),
                    pricing: config.read().pricing.clone(),
                    bottom_el,
                }
            }

            // ── Status bar ──
            Statusbar {
                config,
                tracker_ids,
                issues,
                events,
                is_working,
                theme_name: theme.read().name.to_string(),
            }
        }
    }
}

#[cfg(test)]
mod model_trim_tests {
    use super::apply_nonempty_model_trim;

    #[test]
    fn successive_nonempty_candidates_override() {
        let mut m = "first".into();
        apply_nonempty_model_trim(&mut m, Some("  second  "));
        apply_nonempty_model_trim(&mut m, Some("third"));
        assert_eq!(m, "third");
    }

    #[test]
    fn skips_empty_or_whitespace_only() {
        let mut m = "keep".into();
        apply_nonempty_model_trim(&mut m, Some("   "));
        apply_nonempty_model_trim(&mut m, Some(""));
        apply_nonempty_model_trim(&mut m, None);
        assert_eq!(m, "keep");
    }
}