git-iris 2.0.8

AI-powered Git workflow assistant for smart commits, code reviews, changelogs, and release notes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
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
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
//! Iris Agent - The unified AI agent for Git-Iris operations
//!
//! This agent can handle any Git workflow task through capability-based prompts
//! and multi-turn execution using Rig. One agent to rule them all! ✨

use anyhow::Result;
use rig::agent::{AgentBuilder, PromptResponse};
use rig::completion::CompletionModel;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;

/// Macro to build a streaming agent for any provider.
///
/// All three providers (`OpenAI`, `Anthropic`, `Gemini`) share identical setup logic —
/// subagent creation, tool attachment, optional content update tools — differing
/// only in the provider builder function. This macro eliminates that duplication.
macro_rules! build_streaming_agent {
    ($self:expr, $builder_fn:path, $fast_model:expr, $api_key:expr, $subagent_timeout:expr) => {{
        use crate::agents::debug_tool::DebugTool;

        // Build subagent
        let sub_builder = $builder_fn($fast_model, $api_key)?
            .name("analyze_subagent")
            .preamble("You are a specialized analysis sub-agent.");
        let sub_builder = $self.apply_completion_params(
            sub_builder,
            $fast_model,
            4096,
            CompletionProfile::Subagent,
        )?;
        let sub_agent = crate::attach_core_tools!(sub_builder).build();

        // Build main agent with tools
        let builder = $builder_fn(&$self.model, $api_key)?
            .preamble($self.preamble.as_deref().unwrap_or("You are Iris."));
        let builder = $self.apply_completion_params(
            builder,
            &$self.model,
            16384,
            CompletionProfile::MainAgent,
        )?;

        let builder = crate::attach_core_tools!(builder)
            .tool(DebugTool::new(GitRepoInfo))
            .tool(DebugTool::new($self.workspace.clone()))
            .tool(DebugTool::new(ParallelAnalyze::with_timeout(
                &$self.provider,
                $fast_model,
                $subagent_timeout,
                $api_key,
                $self.current_provider_additional_params().cloned(),
            )?))
            .tool(sub_agent);

        // Conditionally attach content update tools for chat mode
        if let Some(sender) = &$self.content_update_sender {
            use crate::agents::tools::{UpdateCommitTool, UpdatePRTool, UpdateReviewTool};
            Ok(builder
                .tool(DebugTool::new(UpdateCommitTool::new(sender.clone())))
                .tool(DebugTool::new(UpdatePRTool::new(sender.clone())))
                .tool(DebugTool::new(UpdateReviewTool::new(sender.clone())))
                .build())
        } else {
            Ok(builder.build())
        }
    }};
}

// Embed capability TOML files at compile time so they're always available
const CAPABILITY_COMMIT: &str = include_str!("capabilities/commit.toml");
const CAPABILITY_PR: &str = include_str!("capabilities/pr.toml");
const CAPABILITY_REVIEW: &str = include_str!("capabilities/review.toml");
const CAPABILITY_CHANGELOG: &str = include_str!("capabilities/changelog.toml");
const CAPABILITY_RELEASE_NOTES: &str = include_str!("capabilities/release_notes.toml");
const CAPABILITY_CHAT: &str = include_str!("capabilities/chat.toml");
const CAPABILITY_SEMANTIC_BLAME: &str = include_str!("capabilities/semantic_blame.toml");

/// Default preamble for Iris agent
const DEFAULT_PREAMBLE: &str = "\
You are Iris, a helpful AI assistant specialized in Git operations and workflows.

You have access to Git tools, code analysis tools, and powerful sub-agent capabilities for handling large analyses.

**File Access Tools:**
- **file_read** - Read file contents directly. Use `start_line` and `num_lines` for large files.
- **project_docs** - Load a compact snapshot of README and agent instructions. Use targeted doc types for full docs when needed.
- **code_search** - Search for patterns across files. Use sparingly; prefer file_read for known files.

**Sub-Agent Tools:**

1. **parallel_analyze** - Run multiple analysis tasks CONCURRENTLY with independent context windows
   - Best for: Large changesets (>500 lines or >20 files), batch commit analysis
   - Each task runs in its own subagent, preventing context overflow
   - Example: parallel_analyze({ \"tasks\": [\"Analyze auth/ changes for security\", \"Review db/ for performance\", \"Check api/ for breaking changes\"] })

2. **analyze_subagent** - Delegate a single focused task to a sub-agent
   - Best for: Deep dive on specific files or focused analysis

**Best Practices:**
- Use git_diff to get changes first - it includes file content
- Use file_read to read files directly instead of multiple code_search calls
- Use project_docs when repository conventions or product framing matter; do not front-load docs if the diff already answers the question
- Use parallel_analyze for large changesets to avoid context overflow";

fn streaming_response_instructions(capability: &str) -> &'static str {
    if capability == "chat" {
        "After using the available tools, respond in plain text.\n\
         Keep it concise and do not repeat full content that tools already updated."
    } else {
        "After using the available tools, respond with your analysis in markdown format.\n\
         Keep it clear, well-structured, and informative."
    }
}

use crate::agents::provider::{self, CompletionProfile, DynAgent};
use crate::agents::tools::{GitRepoInfo, ParallelAnalyze, Workspace};

/// Trait for streaming callback to handle real-time response processing
#[async_trait::async_trait]
pub trait StreamingCallback: Send + Sync {
    /// Called when a new chunk of text is received
    async fn on_chunk(
        &self,
        chunk: &str,
        tokens: Option<crate::agents::status::TokenMetrics>,
    ) -> Result<()>;

    /// Called when the response is complete
    async fn on_complete(
        &self,
        full_response: &str,
        final_tokens: crate::agents::status::TokenMetrics,
    ) -> Result<()>;

    /// Called when an error occurs
    async fn on_error(&self, error: &anyhow::Error) -> Result<()>;

    /// Called for status updates
    async fn on_status_update(&self, message: &str) -> Result<()>;
}

/// Unified response type that can hold any structured output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StructuredResponse {
    CommitMessage(crate::types::GeneratedMessage),
    PullRequest(crate::types::MarkdownPullRequest),
    Changelog(crate::types::MarkdownChangelog),
    ReleaseNotes(crate::types::MarkdownReleaseNotes),
    /// Markdown-based review (LLM-driven structure)
    MarkdownReview(crate::types::MarkdownReview),
    /// Semantic blame explanation (plain text)
    SemanticBlame(String),
    PlainText(String),
}

impl fmt::Display for StructuredResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            StructuredResponse::CommitMessage(msg) => {
                write!(f, "{}", crate::types::format_commit_message(msg))
            }
            StructuredResponse::PullRequest(pr) => {
                write!(f, "{}", pr.raw_content())
            }
            StructuredResponse::Changelog(cl) => {
                write!(f, "{}", cl.raw_content())
            }
            StructuredResponse::ReleaseNotes(rn) => {
                write!(f, "{}", rn.raw_content())
            }
            StructuredResponse::MarkdownReview(review) => {
                write!(f, "{}", review.format())
            }
            StructuredResponse::SemanticBlame(explanation) => {
                write!(f, "{explanation}")
            }
            StructuredResponse::PlainText(text) => {
                write!(f, "{text}")
            }
        }
    }
}

/// Locate the first balanced `{ ... }` pair in `s`, returning `(start, end)` byte
/// offsets where `end` is exclusive. Returns `None` if no balanced pair exists.
///
/// The scanner is intentionally simple — it does not track string literals, so
/// braces embedded inside strings may still close an enclosing object. Callers
/// compensate by trying subsequent candidates when parsing fails.
fn find_balanced_braces(s: &str) -> Option<(usize, usize)> {
    let mut depth: i32 = 0;
    let mut start: Option<usize> = None;
    for (i, ch) in s.char_indices() {
        match ch {
            '{' => {
                if depth == 0 {
                    start = Some(i);
                }
                depth += 1;
            }
            '}' if depth > 0 => {
                depth -= 1;
                if depth == 0 {
                    return start.map(|s_idx| (s_idx, i + 1));
                }
            }
            _ => {}
        }
    }
    None
}

/// Extract JSON from a potentially verbose response that might contain explanations
fn extract_json_from_response(response: &str) -> Result<String> {
    use crate::agents::debug;

    debug::debug_section("JSON Extraction");

    let trimmed_response = response.trim();

    // First, try parsing the entire response as JSON (for well-behaved responses)
    if trimmed_response.starts_with('{')
        && serde_json::from_str::<serde_json::Value>(trimmed_response).is_ok()
    {
        debug::debug_context_management(
            "Response is pure JSON",
            &format!("{} characters", trimmed_response.len()),
        );
        return Ok(trimmed_response.to_string());
    }

    // Try to find JSON within markdown code blocks
    if let Some(start) = response.find("```json") {
        let content_start = start + "```json".len();
        // Find the closing ``` on its own line (to avoid matching ``` inside JSON strings)
        // First try with newline prefix to find standalone closing marker
        let json_end = if let Some(end) = response[content_start..].find("\n```") {
            // Found it with newline - the JSON ends before the newline
            end
        } else {
            // Fallback: try to find ``` at start of response section or end of string
            response[content_start..]
                .find("```")
                .unwrap_or(response.len() - content_start)
        };

        let json_content = &response[content_start..content_start + json_end];
        let trimmed = json_content.trim().to_string();

        debug::debug_context_management(
            "Found JSON in markdown code block",
            &format!("{} characters", trimmed.len()),
        );

        // Save extracted JSON for debugging
        if let Err(e) = debug::write_debug_artifact("iris_extracted.json", &trimmed) {
            debug::debug_warning(&format!("Failed to write extracted JSON: {}", e));
        }

        debug::debug_json_parse_attempt(&trimmed);
        return Ok(trimmed);
    }

    // Look for JSON objects by scanning for balanced `{ ... }` pairs.
    //
    // The response may contain several `{` characters that are NOT the real JSON
    // payload — for example `${{ github.ref_name }}` lifted verbatim from a diff,
    // or template placeholders the model echoes in its prose. We try each balanced
    // candidate in order and return the first one that parses. If every candidate
    // fails, we fall through with an error built from the last attempt.
    let mut last_error: Option<anyhow::Error> = None;
    let mut cursor = 0;
    while cursor < response.len() {
        let Some((rel_start, rel_end)) = find_balanced_braces(&response[cursor..]) else {
            break;
        };
        let start = cursor + rel_start;
        let end = cursor + rel_end;
        let json_content = &response[start..end];
        debug::debug_json_parse_attempt(json_content);

        let sanitized = sanitize_json_response(json_content);
        match serde_json::from_str::<serde_json::Value>(&sanitized) {
            Ok(_) => {
                debug::debug_context_management(
                    "Found valid JSON object",
                    &format!("{} characters", json_content.len()),
                );
                return Ok(sanitized.into_owned());
            }
            Err(e) => {
                debug::debug_json_parse_error(&format!(
                    "Candidate at offset {} is not valid JSON: {}",
                    start, e
                ));
                let preview = if json_content.len() > 200 {
                    format!("{}...", &json_content[..200])
                } else {
                    json_content.to_string()
                };
                last_error = Some(anyhow::anyhow!(
                    "Found JSON-like content but it's not valid JSON: {}\nPreview: {}",
                    e,
                    preview
                ));
                // Advance past the opening brace of this failed candidate so we
                // can try the next `{` in the response.
                cursor = start + 1;
            }
        }
    }

    if let Some(err) = last_error {
        return Err(err);
    }

    // If no JSON found, check if the response is raw markdown that we can wrap
    // This handles cases where the model returns markdown directly without JSON wrapper
    let trimmed = response.trim();
    if trimmed.starts_with('#') || trimmed.starts_with("##") {
        debug::debug_context_management(
            "Detected raw markdown response",
            "Wrapping in JSON structure",
        );
        // Escape the markdown content for JSON and wrap it
        let escaped_content = serde_json::to_string(trimmed)?;
        // escaped_content includes quotes, so we need to use it directly as the value
        let wrapped = format!(r#"{{"content": {}}}"#, escaped_content);
        debug::debug_json_parse_attempt(&wrapped);
        return Ok(wrapped);
    }

    // If no JSON found, return error
    debug::debug_json_parse_error("No valid JSON found in response");
    Err(anyhow::anyhow!("No valid JSON found in response"))
}

/// Some providers (Anthropic) occasionally send literal control characters like newlines
/// inside JSON strings, which violates strict JSON parsing rules. This helper sanitizes
/// those responses by escaping control characters only within string literals while
/// leaving the rest of the payload untouched.
fn sanitize_json_response(raw: &str) -> Cow<'_, str> {
    let mut needs_sanitization = false;
    let mut in_string = false;
    let mut escaped = false;

    for ch in raw.chars() {
        if in_string {
            if escaped {
                escaped = false;
                continue;
            }

            match ch {
                '\\' => escaped = true,
                '"' => in_string = false,
                '\n' | '\r' | '\t' => {
                    needs_sanitization = true;
                    break;
                }
                c if c.is_control() => {
                    needs_sanitization = true;
                    break;
                }
                _ => {}
            }
        } else if ch == '"' {
            in_string = true;
        }
    }

    if !needs_sanitization {
        return Cow::Borrowed(raw);
    }

    let mut sanitized = String::with_capacity(raw.len());
    in_string = false;
    escaped = false;

    for ch in raw.chars() {
        if in_string {
            if escaped {
                sanitized.push(ch);
                escaped = false;
                continue;
            }

            match ch {
                '\\' => {
                    sanitized.push('\\');
                    escaped = true;
                }
                '"' => {
                    sanitized.push('"');
                    in_string = false;
                }
                '\n' => sanitized.push_str("\\n"),
                '\r' => sanitized.push_str("\\r"),
                '\t' => sanitized.push_str("\\t"),
                c if c.is_control() => {
                    use std::fmt::Write as _;
                    let _ = write!(&mut sanitized, "\\u{:04X}", u32::from(c));
                }
                _ => sanitized.push(ch),
            }
        } else {
            sanitized.push(ch);
            if ch == '"' {
                in_string = true;
                escaped = false;
            }
        }
    }

    Cow::Owned(sanitized)
}

/// Parse JSON with schema validation and error recovery
///
/// This function attempts to parse JSON with the following strategy:
/// 1. Try direct parsing (fast path for well-formed responses)
/// 2. If that fails, use the output validator for recovery
/// 3. Log any warnings about recovered issues
fn parse_with_recovery<T>(json_str: &str) -> Result<T>
where
    T: JsonSchema + DeserializeOwned,
{
    use crate::agents::debug as agent_debug;
    use crate::agents::output_validator::validate_and_parse;

    let validation_result = validate_and_parse::<T>(json_str)?;

    // Log recovery warnings
    if validation_result.recovered {
        agent_debug::debug_context_management(
            "JSON recovery applied",
            &format!("{} issues fixed", validation_result.warnings.len()),
        );
        for warning in &validation_result.warnings {
            agent_debug::debug_warning(warning);
        }
    }

    validation_result
        .value
        .ok_or_else(|| anyhow::anyhow!("Failed to parse JSON even after recovery"))
}

/// The unified Iris agent that can handle any Git-Iris task
///
/// Note: This struct is Send + Sync safe - we don't store the client builder,
/// instead we create it fresh when needed. This allows the agent to be used
/// across async boundaries with `tokio::spawn`.
pub struct IrisAgent {
    provider: String,
    model: String,
    /// Fast model for subagents and simple tasks
    fast_model: Option<String>,
    /// Current capability/task being executed
    current_capability: Option<String>,
    /// Provider configuration
    provider_config: HashMap<String, String>,
    /// Custom preamble
    preamble: Option<String>,
    /// Configuration for features like gitmoji, presets, etc.
    config: Option<crate::config::Config>,
    /// Optional sender for content updates (used in Studio chat mode)
    content_update_sender: Option<crate::agents::tools::ContentUpdateSender>,
    /// Persistent workspace for notes and task tracking (shared across agent invocations)
    workspace: Workspace,
}

impl IrisAgent {
    /// Create a new Iris agent with the given provider and model
    ///
    /// # Errors
    ///
    /// Returns an error when the provider or model configuration is invalid.
    pub fn new(provider: &str, model: &str) -> Result<Self> {
        Ok(Self {
            provider: provider.to_string(),
            model: model.to_string(),
            fast_model: None,
            current_capability: None,
            provider_config: HashMap::new(),
            preamble: None,
            config: None,
            content_update_sender: None,
            workspace: Workspace::new(),
        })
    }

    /// Set the content update sender for Studio chat mode
    ///
    /// When set, the agent will have access to tools for updating
    /// commit messages, PR descriptions, and reviews.
    pub fn set_content_update_sender(&mut self, sender: crate::agents::tools::ContentUpdateSender) {
        self.content_update_sender = Some(sender);
    }

    /// Get the effective fast model (configured or same as main model)
    fn effective_fast_model(&self) -> &str {
        self.fast_model.as_deref().unwrap_or(&self.model)
    }

    /// Get the API key for the current provider from config
    fn get_api_key(&self) -> Option<&str> {
        provider::current_provider_config(self.config.as_ref(), &self.provider)
            .and_then(crate::providers::ProviderConfig::api_key_if_set)
    }

    fn current_provider(&self) -> Result<crate::providers::Provider> {
        provider::provider_from_name(&self.provider)
    }

    fn current_provider_additional_params(&self) -> Option<&HashMap<String, String>> {
        provider::current_provider_config(self.config.as_ref(), &self.provider)
            .map(|provider_config| &provider_config.additional_params)
    }

    /// Build the actual agent for execution
    ///
    /// Uses provider-specific builders (rig-core 0.27+) with enum dispatch for runtime
    /// provider selection. Each provider arm builds both the subagent and main agent
    /// with proper typing.
    fn build_agent(&self) -> Result<DynAgent> {
        use crate::agents::debug_tool::DebugTool;

        let preamble = self.preamble.as_deref().unwrap_or(DEFAULT_PREAMBLE);
        let fast_model = self.effective_fast_model();
        let api_key = self.get_api_key();
        let subagent_timeout = self
            .config
            .as_ref()
            .map_or(120, |c| c.subagent_timeout_secs);

        // Macro to build and configure subagent with core tools
        macro_rules! build_subagent {
            ($builder:expr) => {{
                let builder = $builder
                    .name("analyze_subagent")
                    .description("Delegate focused analysis tasks to a sub-agent with its own context window. Use for analyzing specific files, commits, or code sections independently. The sub-agent has access to Git tools (diff, log, status) and file analysis tools.")
                    .preamble("You are a specialized analysis sub-agent for Iris. Your job is to complete focused analysis tasks and return concise, actionable summaries.

Guidelines:
- Use the available tools to gather information
- Focus only on what's asked - don't expand scope
- Return a clear, structured summary of findings
- Highlight important issues, patterns, or insights
- Keep your response focused and concise")
                    ;
                let builder = self.apply_completion_params(
                    builder,
                    fast_model,
                    4096,
                    CompletionProfile::Subagent,
                )?;
                crate::attach_core_tools!(builder).build()
            }};
        }

        // Macro to attach main agent tools (excluding subagent which varies by type)
        macro_rules! attach_main_tools {
            ($builder:expr) => {{
                crate::attach_core_tools!($builder)
                    .tool(DebugTool::new(GitRepoInfo))
                    .tool(DebugTool::new(self.workspace.clone()))
                    .tool(DebugTool::new(ParallelAnalyze::with_timeout(
                        &self.provider,
                        fast_model,
                        subagent_timeout,
                        api_key,
                        self.current_provider_additional_params().cloned(),
                    )?))
            }};
        }

        // Macro to optionally attach content update tools
        macro_rules! maybe_attach_update_tools {
            ($builder:expr) => {{
                if let Some(sender) = &self.content_update_sender {
                    use crate::agents::tools::{UpdateCommitTool, UpdatePRTool, UpdateReviewTool};
                    $builder
                        .tool(DebugTool::new(UpdateCommitTool::new(sender.clone())))
                        .tool(DebugTool::new(UpdatePRTool::new(sender.clone())))
                        .tool(DebugTool::new(UpdateReviewTool::new(sender.clone())))
                        .build()
                } else {
                    $builder.build()
                }
            }};
        }

        match self.provider.as_str() {
            "openai" => {
                // Build subagent
                let sub_agent = build_subagent!(provider::openai_builder(fast_model, api_key)?);

                // Build main agent
                let builder = provider::openai_builder(&self.model, api_key)?.preamble(preamble);
                let builder = self.apply_completion_params(
                    builder,
                    &self.model,
                    16384,
                    CompletionProfile::MainAgent,
                )?;
                let builder = attach_main_tools!(builder).tool(sub_agent);
                let agent = maybe_attach_update_tools!(builder);
                Ok(DynAgent::OpenAI(agent))
            }
            "anthropic" => {
                // Build subagent
                let sub_agent = build_subagent!(provider::anthropic_builder(fast_model, api_key)?);

                // Build main agent
                let builder = provider::anthropic_builder(&self.model, api_key)?.preamble(preamble);
                let builder = self.apply_completion_params(
                    builder,
                    &self.model,
                    16384,
                    CompletionProfile::MainAgent,
                )?;
                let builder = attach_main_tools!(builder).tool(sub_agent);
                let agent = maybe_attach_update_tools!(builder);
                Ok(DynAgent::Anthropic(agent))
            }
            "google" | "gemini" => {
                // Build subagent
                let sub_agent = build_subagent!(provider::gemini_builder(fast_model, api_key)?);

                // Build main agent
                let builder = provider::gemini_builder(&self.model, api_key)?.preamble(preamble);
                let builder = self.apply_completion_params(
                    builder,
                    &self.model,
                    16384,
                    CompletionProfile::MainAgent,
                )?;
                let builder = attach_main_tools!(builder).tool(sub_agent);
                let agent = maybe_attach_update_tools!(builder);
                Ok(DynAgent::Gemini(agent))
            }
            _ => Err(anyhow::anyhow!("Unsupported provider: {}", self.provider)),
        }
    }

    fn apply_completion_params<M>(
        &self,
        builder: AgentBuilder<M>,
        model: &str,
        max_tokens: u64,
        profile: CompletionProfile,
    ) -> Result<AgentBuilder<M>>
    where
        M: CompletionModel,
    {
        let provider = self.current_provider()?;
        Ok(provider::apply_completion_params(
            builder,
            provider,
            model,
            max_tokens,
            self.current_provider_additional_params(),
            profile,
        ))
    }

    /// Execute task using agent with tools and parse structured JSON response
    /// This is the core method that enables Iris to use tools and generate structured outputs
    async fn execute_with_agent<T>(&self, system_prompt: &str, user_prompt: &str) -> Result<T>
    where
        T: JsonSchema + for<'a> serde::Deserialize<'a> + serde::Serialize + Send + Sync + 'static,
    {
        use crate::agents::debug;
        use crate::agents::status::IrisPhase;
        use crate::messages::get_capability_message;
        use schemars::schema_for;

        let capability = self.current_capability().unwrap_or("commit");

        debug::debug_phase_change(&format!("AGENT EXECUTION: {}", std::any::type_name::<T>()));

        // Update status - building agent (capability-aware)
        let msg = get_capability_message(capability);
        crate::iris_status_dynamic!(IrisPhase::Planning, msg.text, 2, 4);

        // Build agent with all tools attached
        let agent = self.build_agent()?;
        debug::debug_context_management(
            "Agent built with tools",
            &format!(
                "Provider: {}, Model: {} (fast: {})",
                self.provider,
                self.model,
                self.effective_fast_model()
            ),
        );

        // Create JSON schema for the response type
        let schema = schema_for!(T);
        let schema_json = serde_json::to_string_pretty(&schema)?;
        debug::debug_context_management(
            "JSON schema created",
            &format!("Type: {}", std::any::type_name::<T>()),
        );

        // Enhanced prompt that instructs Iris to use tools and respond with JSON
        let full_prompt = format!(
            "{system_prompt}\n\n{user_prompt}\n\n\
            === CRITICAL: RESPONSE FORMAT ===\n\
            After using the available tools to gather necessary information, you MUST respond with ONLY a valid JSON object.\n\n\
            REQUIRED JSON SCHEMA:\n\
            {schema_json}\n\n\
            CRITICAL INSTRUCTIONS:\n\
            - Return ONLY the raw JSON object - nothing else\n\
            - NO explanations before the JSON\n\
            - NO explanations after the JSON\n\
            - NO markdown code blocks (just raw JSON)\n\
            - NO preamble text like 'Here is the JSON:' or 'Let me generate:'\n\
            - Start your response with {{ and end with }}\n\
            - The JSON must be complete and valid\n\n\
            Your entire response should be ONLY the JSON object."
        );

        debug::debug_llm_request(&full_prompt, Some(16384));

        // Update status - generation phase (capability-aware)
        let gen_msg = get_capability_message(capability);
        crate::iris_status_dynamic!(IrisPhase::Generation, gen_msg.text, 3, 4);

        // Prompt the agent with multi-turn support
        // Set multi_turn to allow the agent to call multiple tools (default is 0 = single-shot)
        // For complex tasks like PRs and release notes, Iris may need many tool calls to analyze all changes
        // The agent knows when to stop, so we give it plenty of room (50 rounds)
        let timer = debug::DebugTimer::start("Agent prompt execution");

        debug::debug_context_management(
            "LLM request",
            "Sending prompt to agent with multi_turn(50)",
        );
        let prompt_response: PromptResponse = agent.prompt_extended(&full_prompt, 50).await?;

        timer.finish();

        // Extract usage stats for debug output
        let usage = &prompt_response.usage;
        debug::debug_context_management(
            "Token usage",
            &format!(
                "input: {} | output: {} | total: {}",
                usage.input_tokens, usage.output_tokens, usage.total_tokens
            ),
        );

        let response = &prompt_response.output;
        #[allow(clippy::cast_possible_truncation, clippy::as_conversions)]
        let total_tokens_usize = usage.total_tokens as usize;
        debug::debug_llm_response(
            response,
            std::time::Duration::from_secs(0),
            Some(total_tokens_usize),
        );

        // Update status - synthesis phase
        crate::iris_status_dynamic!(
            IrisPhase::Synthesis,
            "✨ Iris is synthesizing results...",
            4,
            4
        );

        // Extract and parse JSON from the response
        let json_str = extract_json_from_response(response)?;
        let sanitized_json = sanitize_json_response(&json_str);
        let sanitized_ref = sanitized_json.as_ref();

        if matches!(sanitized_json, Cow::Borrowed(_)) {
            debug::debug_json_parse_attempt(sanitized_ref);
        } else {
            debug::debug_context_management(
                "Sanitized JSON response",
                &format!("{}{} characters", json_str.len(), sanitized_ref.len()),
            );
            debug::debug_json_parse_attempt(sanitized_ref);
        }

        // Use the output validator for robust parsing with error recovery
        let result: T = parse_with_recovery(sanitized_ref)?;

        debug::debug_json_parse_success(std::any::type_name::<T>());

        // Update status - completed
        crate::iris_status_completed!();

        Ok(result)
    }

    /// Inject style instructions into the system prompt based on config and capability
    ///
    /// Key distinction:
    /// - Commits: preset controls format (conventional = no emojis)
    /// - Non-commits (PR, review, changelog, `release_notes`): `use_gitmoji` controls emojis
    fn inject_style_instructions(&self, system_prompt: &mut String, capability: &str) {
        let Some(config) = &self.config else {
            return;
        };

        let preset_name = config.get_effective_preset_name();
        let is_conventional = preset_name == "conventional";
        let is_default_mode = preset_name == "default" || preset_name.is_empty();

        // For commits in default mode with no explicit gitmoji override, use style detection
        let use_style_detection =
            capability == "commit" && is_default_mode && config.gitmoji_override.is_none();

        // Commit emoji: respects preset (conventional = no emoji)
        let commit_emoji = config.use_gitmoji && !is_conventional && !use_style_detection;

        // Output emoji: independent of preset, only respects use_gitmoji setting
        // CLI --gitmoji/--no-gitmoji override is already applied to config.use_gitmoji
        let output_emoji = config.gitmoji_override.unwrap_or(config.use_gitmoji);

        // Inject instruction preset if configured (skip for default mode)
        if !preset_name.is_empty() && !is_default_mode {
            let library = crate::instruction_presets::get_instruction_preset_library();
            if let Some(preset) = library.get_preset(preset_name) {
                tracing::info!("📋 Injecting '{}' preset style instructions", preset_name);
                system_prompt.push_str("\n\n=== STYLE INSTRUCTIONS ===\n");
                system_prompt.push_str(&preset.instructions);
                system_prompt.push('\n');
            } else {
                tracing::warn!("⚠️ Preset '{}' not found in library", preset_name);
            }
        }

        // Handle commit-specific styling (structured JSON output with emoji field)
        // Default mode (use_style_detection): no style injection here — the agent
        // detects format from git_log via commit.toml §Local Style Detection.
        if capability == "commit" {
            if commit_emoji {
                system_prompt.push_str("\n\n=== GITMOJI INSTRUCTIONS ===\n");
                system_prompt.push_str("Set the 'emoji' field to a single relevant gitmoji. ");
                system_prompt.push_str(
                    "DO NOT include the emoji in the 'message' or 'title' text - only set the 'emoji' field. ",
                );
                system_prompt.push_str("Choose the closest match from this compact guide:\n\n");
                system_prompt.push_str(&crate::gitmoji::get_gitmoji_prompt_guide());
                system_prompt.push_str("\n\nThe emoji should match the primary type of change.");
            } else if is_conventional {
                system_prompt.push_str("\n\n=== CONVENTIONAL COMMITS FORMAT ===\n");
                system_prompt.push_str("IMPORTANT: This uses Conventional Commits format. ");
                system_prompt
                    .push_str("DO NOT include any emojis in the commit message or PR title. ");
                system_prompt.push_str("The 'emoji' field should be null.");
            }
        }

        // Handle non-commit outputs: use output_emoji (independent of preset)
        if capability == "pr" || capability == "review" {
            if output_emoji {
                Self::inject_pr_review_emoji_styling(system_prompt);
            } else {
                Self::inject_no_emoji_styling(system_prompt);
            }
        }

        if capability == "release_notes" && output_emoji {
            Self::inject_release_notes_emoji_styling(system_prompt);
        } else if capability == "release_notes" {
            Self::inject_no_emoji_styling(system_prompt);
        }

        if capability == "changelog" && output_emoji {
            Self::inject_changelog_emoji_styling(system_prompt);
        } else if capability == "changelog" {
            Self::inject_no_emoji_styling(system_prompt);
        }
    }

    fn inject_pr_review_emoji_styling(prompt: &mut String) {
        prompt.push_str("\n\n=== EMOJI STYLING ===\n");
        prompt.push_str("Use emojis to make the output visually scannable and engaging:\n");
        prompt.push_str("- H1 title: ONE gitmoji at the start (✨, 🐛, ♻️, etc.)\n");
        prompt.push_str("- Section headers: Add relevant emojis (🎯 What's New, ⚙️ How It Works, 📋 Commits, ⚠️ Breaking Changes)\n");
        prompt.push_str("- Commit list entries: Include gitmoji where appropriate\n");
        prompt.push_str("- Body text: Keep clean - no scattered emojis within prose\n\n");
        prompt.push_str(&crate::gitmoji::get_gitmoji_prompt_guide());
    }

    fn inject_release_notes_emoji_styling(prompt: &mut String) {
        prompt.push_str("\n\n=== EMOJI STYLING ===\n");
        prompt.push_str("Use at most one emoji per highlight/section title. No emojis in bullet descriptions, upgrade notes, or metrics. ");
        prompt.push_str("Pick from the approved gitmoji list (e.g., 🌟 Highlights, 🤖 Agents, 🔧 Tooling, 🐛 Fixes, ⚡ Performance). ");
        prompt.push_str("Never sprinkle emojis within sentences or JSON keys.\n\n");
        prompt.push_str(&crate::gitmoji::get_gitmoji_prompt_guide());
    }

    fn inject_changelog_emoji_styling(prompt: &mut String) {
        prompt.push_str("\n\n=== EMOJI STYLING ===\n");
        prompt.push_str("Section keys must remain plain text (Added/Changed/Deprecated/Removed/Fixed/Security). ");
        prompt.push_str(
            "You may include one emoji within a change description to reinforce meaning. ",
        );
        prompt.push_str(
            "Never add emojis to JSON keys, section names, metrics, or upgrade notes.\n\n",
        );
        prompt.push_str(&crate::gitmoji::get_gitmoji_prompt_guide());
    }

    fn inject_no_emoji_styling(prompt: &mut String) {
        prompt.push_str("\n\n=== NO EMOJI STYLING ===\n");
        prompt.push_str(
            "DO NOT include any emojis anywhere in the output. Keep all content plain text.",
        );
    }

    /// Execute a task with the given capability and user prompt
    ///
    /// This now automatically uses structured output based on the capability type
    ///
    /// # Errors
    ///
    /// Returns an error when capability loading, agent construction, or generation fails.
    pub async fn execute_task(
        &mut self,
        capability: &str,
        user_prompt: &str,
    ) -> Result<StructuredResponse> {
        use crate::agents::status::IrisPhase;
        use crate::messages::get_capability_message;

        // Show initializing status with a capability-specific message
        let waiting_msg = get_capability_message(capability);
        crate::iris_status_dynamic!(IrisPhase::Initializing, waiting_msg.text, 1, 4);

        // Load the capability config to get both prompt and output type
        let (mut system_prompt, output_type) = self.load_capability_config(capability)?;

        // Inject style instructions (presets, gitmoji, conventional commits)
        self.inject_style_instructions(&mut system_prompt, capability);

        // Set the current capability
        self.current_capability = Some(capability.to_string());

        // Update status - analyzing with agent
        crate::iris_status_dynamic!(
            IrisPhase::Analysis,
            "🔍 Iris is analyzing your changes...",
            2,
            4
        );

        // Use agent with tools for all structured outputs
        // The agent will use tools as needed and respond with JSON
        match output_type.as_str() {
            "GeneratedMessage" => {
                let response = self
                    .execute_with_agent::<crate::types::GeneratedMessage>(
                        &system_prompt,
                        user_prompt,
                    )
                    .await?;
                Ok(StructuredResponse::CommitMessage(response))
            }
            "MarkdownPullRequest" => {
                let response = self
                    .execute_with_agent::<crate::types::MarkdownPullRequest>(
                        &system_prompt,
                        user_prompt,
                    )
                    .await?;
                Ok(StructuredResponse::PullRequest(response))
            }
            "MarkdownChangelog" => {
                let response = self
                    .execute_with_agent::<crate::types::MarkdownChangelog>(
                        &system_prompt,
                        user_prompt,
                    )
                    .await?;
                Ok(StructuredResponse::Changelog(response))
            }
            "MarkdownReleaseNotes" => {
                let response = self
                    .execute_with_agent::<crate::types::MarkdownReleaseNotes>(
                        &system_prompt,
                        user_prompt,
                    )
                    .await?;
                Ok(StructuredResponse::ReleaseNotes(response))
            }
            "MarkdownReview" => {
                let response = self
                    .execute_with_agent::<crate::types::MarkdownReview>(&system_prompt, user_prompt)
                    .await?;
                Ok(StructuredResponse::MarkdownReview(response))
            }
            "SemanticBlame" => {
                // For semantic blame, we want plain text response
                let agent = self.build_agent()?;
                let full_prompt = format!("{system_prompt}\n\n{user_prompt}");
                let response = agent.prompt_multi_turn(&full_prompt, 10).await?;
                Ok(StructuredResponse::SemanticBlame(response))
            }
            _ => {
                // Fallback to regular agent for unknown types
                let agent = self.build_agent()?;
                let full_prompt = format!("{system_prompt}\n\n{user_prompt}");
                // Use multi_turn to allow tool calls even for unknown capability types
                let response = agent.prompt_multi_turn(&full_prompt, 50).await?;
                Ok(StructuredResponse::PlainText(response))
            }
        }
    }

    /// Execute a task with streaming, calling the callback with each text chunk
    ///
    /// This enables real-time display of LLM output in the TUI.
    /// The callback receives `(chunk, aggregated_text)` for each delta.
    ///
    /// Returns the final structured response after streaming completes.
    ///
    /// # Errors
    ///
    /// Returns an error when capability loading, agent construction, or streaming fails.
    pub async fn execute_task_streaming<F>(
        &mut self,
        capability: &str,
        user_prompt: &str,
        mut on_chunk: F,
    ) -> Result<StructuredResponse>
    where
        F: FnMut(&str, &str) + Send,
    {
        use crate::agents::status::IrisPhase;
        use crate::messages::get_capability_message;
        use futures::StreamExt;
        use rig::agent::MultiTurnStreamItem;
        use rig::streaming::{StreamedAssistantContent, StreamingPrompt};

        // Show initializing status
        let waiting_msg = get_capability_message(capability);
        crate::iris_status_dynamic!(IrisPhase::Initializing, waiting_msg.text, 1, 4);

        // Load the capability config
        let (mut system_prompt, output_type) = self.load_capability_config(capability)?;

        // Inject style instructions
        self.inject_style_instructions(&mut system_prompt, capability);

        // Set current capability
        self.current_capability = Some(capability.to_string());

        // Update status
        crate::iris_status_dynamic!(
            IrisPhase::Analysis,
            "🔍 Iris is analyzing your changes...",
            2,
            4
        );

        // Build the full prompt (simplified for streaming - no JSON schema enforcement)
        let full_prompt = format!(
            "{}\n\n{}\n\n{}",
            system_prompt,
            user_prompt,
            streaming_response_instructions(capability)
        );

        // Update status
        let gen_msg = get_capability_message(capability);
        crate::iris_status_dynamic!(IrisPhase::Generation, gen_msg.text, 3, 4);

        // Macro to consume a stream and aggregate text
        macro_rules! consume_stream {
            ($stream:expr) => {{
                let mut aggregated_text = String::new();
                let mut stream = $stream;
                while let Some(item) = stream.next().await {
                    match item {
                        Ok(MultiTurnStreamItem::StreamAssistantItem(
                            StreamedAssistantContent::Text(text),
                        )) => {
                            aggregated_text.push_str(&text.text);
                            on_chunk(&text.text, &aggregated_text);
                        }
                        Ok(MultiTurnStreamItem::StreamAssistantItem(
                            StreamedAssistantContent::ToolCall { tool_call, .. },
                        )) => {
                            let tool_name = &tool_call.function.name;
                            let reason = format!("Calling {}", tool_name);
                            crate::iris_status_dynamic!(
                                IrisPhase::ToolExecution {
                                    tool_name: tool_name.clone(),
                                    reason: reason.clone()
                                },
                                format!("🔧 {}", reason),
                                3,
                                4
                            );
                        }
                        Ok(MultiTurnStreamItem::FinalResponse(_)) => break,
                        Err(e) => return Err(anyhow::anyhow!("Streaming error: {}", e)),
                        _ => {}
                    }
                }
                aggregated_text
            }};
        }

        // Build and stream per-provider (streaming types are model-specific)
        let aggregated_text = match self.provider.as_str() {
            "openai" => {
                let agent = self.build_openai_agent_for_streaming(&full_prompt)?;
                let stream = agent.stream_prompt(&full_prompt).multi_turn(50).await;
                consume_stream!(stream)
            }
            "anthropic" => {
                let agent = self.build_anthropic_agent_for_streaming(&full_prompt)?;
                let stream = agent.stream_prompt(&full_prompt).multi_turn(50).await;
                consume_stream!(stream)
            }
            "google" | "gemini" => {
                let agent = self.build_gemini_agent_for_streaming(&full_prompt)?;
                let stream = agent.stream_prompt(&full_prompt).multi_turn(50).await;
                consume_stream!(stream)
            }
            _ => return Err(anyhow::anyhow!("Unsupported provider: {}", self.provider)),
        };

        // Update status
        crate::iris_status_dynamic!(
            IrisPhase::Synthesis,
            "✨ Iris is synthesizing results...",
            4,
            4
        );

        let response = Self::text_to_structured_response(&output_type, aggregated_text);
        crate::iris_status_completed!();
        Ok(response)
    }

    /// Convert raw text to the appropriate structured response type
    fn text_to_structured_response(output_type: &str, text: String) -> StructuredResponse {
        match output_type {
            "MarkdownReview" => {
                StructuredResponse::MarkdownReview(crate::types::MarkdownReview { content: text })
            }
            "MarkdownPullRequest" => {
                StructuredResponse::PullRequest(crate::types::MarkdownPullRequest { content: text })
            }
            "MarkdownChangelog" => {
                StructuredResponse::Changelog(crate::types::MarkdownChangelog { content: text })
            }
            "MarkdownReleaseNotes" => {
                StructuredResponse::ReleaseNotes(crate::types::MarkdownReleaseNotes {
                    content: text,
                })
            }
            "SemanticBlame" => StructuredResponse::SemanticBlame(text),
            _ => StructuredResponse::PlainText(text),
        }
    }

    /// Shared streaming agent configuration
    fn streaming_agent_config(&self) -> (&str, Option<&str>, u64) {
        let fast_model = self.effective_fast_model();
        let api_key = self.get_api_key();
        let subagent_timeout = self
            .config
            .as_ref()
            .map_or(120, |c| c.subagent_timeout_secs);
        (fast_model, api_key, subagent_timeout)
    }

    /// Build `OpenAI` agent for streaming (with tools attached)
    fn build_openai_agent_for_streaming(
        &self,
        _prompt: &str,
    ) -> Result<rig::agent::Agent<provider::OpenAIModel>> {
        let (fast_model, api_key, subagent_timeout) = self.streaming_agent_config();
        build_streaming_agent!(
            self,
            provider::openai_builder,
            fast_model,
            api_key,
            subagent_timeout
        )
    }

    /// Build Anthropic agent for streaming (with tools attached)
    fn build_anthropic_agent_for_streaming(
        &self,
        _prompt: &str,
    ) -> Result<rig::agent::Agent<provider::AnthropicModel>> {
        let (fast_model, api_key, subagent_timeout) = self.streaming_agent_config();
        build_streaming_agent!(
            self,
            provider::anthropic_builder,
            fast_model,
            api_key,
            subagent_timeout
        )
    }

    /// Build Gemini agent for streaming (with tools attached)
    fn build_gemini_agent_for_streaming(
        &self,
        _prompt: &str,
    ) -> Result<rig::agent::Agent<provider::GeminiModel>> {
        let (fast_model, api_key, subagent_timeout) = self.streaming_agent_config();
        build_streaming_agent!(
            self,
            provider::gemini_builder,
            fast_model,
            api_key,
            subagent_timeout
        )
    }

    /// Load capability configuration from embedded TOML, returning both prompt and output type
    fn load_capability_config(&self, capability: &str) -> Result<(String, String)> {
        let _ = self; // Keep &self for method syntax consistency
        // Use embedded capability strings - always available regardless of working directory
        let content = match capability {
            "commit" => CAPABILITY_COMMIT,
            "pr" => CAPABILITY_PR,
            "review" => CAPABILITY_REVIEW,
            "changelog" => CAPABILITY_CHANGELOG,
            "release_notes" => CAPABILITY_RELEASE_NOTES,
            "chat" => CAPABILITY_CHAT,
            "semantic_blame" => CAPABILITY_SEMANTIC_BLAME,
            _ => {
                // Return generic prompt for unknown capabilities
                return Ok((
                    format!(
                        "You are helping with a {capability} task. Use the available Git tools to assist the user."
                    ),
                    "PlainText".to_string(),
                ));
            }
        };

        // Parse TOML to extract both task_prompt and output_type
        let parsed: toml::Value = toml::from_str(content)?;

        let task_prompt = parsed
            .get("task_prompt")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("No task_prompt found in capability file"))?;

        let output_type = parsed
            .get("output_type")
            .and_then(|v| v.as_str())
            .unwrap_or("PlainText")
            .to_string();

        Ok((task_prompt.to_string(), output_type))
    }

    /// Get the current capability being executed
    #[must_use]
    pub fn current_capability(&self) -> Option<&str> {
        self.current_capability.as_deref()
    }

    /// Simple single-turn execution for basic queries
    ///
    /// # Errors
    ///
    /// Returns an error when the provider request fails.
    pub async fn chat(&self, message: &str) -> Result<String> {
        let agent = self.build_agent()?;
        let response = agent.prompt(message).await?;
        Ok(response)
    }

    /// Set the current capability
    pub fn set_capability(&mut self, capability: &str) {
        self.current_capability = Some(capability.to_string());
    }

    /// Get provider configuration
    #[must_use]
    pub fn provider_config(&self) -> &HashMap<String, String> {
        &self.provider_config
    }

    /// Set provider configuration
    pub fn set_provider_config(&mut self, config: HashMap<String, String>) {
        self.provider_config = config;
    }

    /// Set custom preamble
    pub fn set_preamble(&mut self, preamble: String) {
        self.preamble = Some(preamble);
    }

    /// Set configuration
    pub fn set_config(&mut self, config: crate::config::Config) {
        self.config = Some(config);
    }

    /// Set fast model for subagents
    pub fn set_fast_model(&mut self, fast_model: String) {
        self.fast_model = Some(fast_model);
    }
}

/// Builder for creating `IrisAgent` instances with different configurations
pub struct IrisAgentBuilder {
    provider: String,
    model: String,
    preamble: Option<String>,
}

impl IrisAgentBuilder {
    /// Create a new builder
    #[must_use]
    pub fn new() -> Self {
        Self {
            provider: "openai".to_string(),
            model: "gpt-5.4".to_string(),
            preamble: None,
        }
    }

    /// Set the provider to use
    pub fn with_provider(mut self, provider: impl Into<String>) -> Self {
        self.provider = provider.into();
        self
    }

    /// Set the model to use
    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    /// Set a custom preamble
    pub fn with_preamble(mut self, preamble: impl Into<String>) -> Self {
        self.preamble = Some(preamble.into());
        self
    }

    /// Build the `IrisAgent`
    ///
    /// # Errors
    ///
    /// Returns an error when the configured provider or model cannot build an agent.
    pub fn build(self) -> Result<IrisAgent> {
        let mut agent = IrisAgent::new(&self.provider, &self.model)?;

        // Apply custom preamble if provided
        if let Some(preamble) = self.preamble {
            agent.set_preamble(preamble);
        }

        Ok(agent)
    }
}

impl Default for IrisAgentBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::{
        IrisAgent, extract_json_from_response, find_balanced_braces, sanitize_json_response,
        streaming_response_instructions,
    };
    use serde_json::Value;
    use std::borrow::Cow;

    #[test]
    fn sanitize_json_response_is_noop_for_valid_payloads() {
        let raw = r#"{"title":"Test","description":"All good"}"#;
        let sanitized = sanitize_json_response(raw);
        assert!(matches!(sanitized, Cow::Borrowed(_)));
        serde_json::from_str::<Value>(sanitized.as_ref()).expect("valid JSON");
    }

    #[test]
    fn sanitize_json_response_escapes_literal_newlines() {
        let raw = "{\"description\": \"Line1
Line2\"}";
        let sanitized = sanitize_json_response(raw);
        assert_eq!(sanitized.as_ref(), "{\"description\": \"Line1\\nLine2\"}");
        serde_json::from_str::<Value>(sanitized.as_ref()).expect("json sanitized");
    }

    #[test]
    fn chat_streaming_instructions_avoid_markdown_suffix() {
        let instructions = streaming_response_instructions("chat");
        assert!(instructions.contains("plain text"));
        assert!(instructions.contains("do not repeat full content"));
        assert!(!instructions.contains("markdown format"));
    }

    #[test]
    fn structured_streaming_instructions_still_use_markdown_suffix() {
        let instructions = streaming_response_instructions("review");
        assert!(instructions.contains("markdown format"));
        assert!(instructions.contains("well-structured"));
    }

    #[test]
    fn find_balanced_braces_returns_first_balanced_pair() {
        let (start, end) = find_balanced_braces("prefix {\"a\":1} suffix").expect("balanced pair");
        assert_eq!(&"prefix {\"a\":1} suffix"[start..end], "{\"a\":1}");
    }

    #[test]
    fn find_balanced_braces_returns_none_for_unbalanced() {
        assert_eq!(find_balanced_braces("no braces here"), None);
        assert_eq!(find_balanced_braces("{ unclosed"), None);
    }

    #[test]
    fn extract_json_skips_github_actions_expression_false_positive() {
        // Regression for a real failure: a diff hunk that adds
        // `commit_message: "Update to ${{ github.ref_name }}"` to a workflow
        // lands in the model's response. The old scanner grabbed `{{ github.ref_name }}`
        // as its first balanced pair and errored out before seeing the real JSON.
        let response = r#"Looking at the diff, I see the new value `${{ github.ref_name }}` replacing the old bash expansion. Here's the commit:

{"emoji": "🔧", "title": "Upgrade AUR deploy action", "message": "Bump to v4.1.2 to fix bash --command error."}
"#;
        let extracted = extract_json_from_response(response).expect("should recover real JSON");
        let parsed: Value = serde_json::from_str(&extracted).expect("extracted value is JSON");
        assert_eq!(parsed["emoji"], "🔧");
        assert_eq!(parsed["title"], "Upgrade AUR deploy action");
    }

    #[test]
    fn extract_json_from_pure_json_response() {
        let response = r##"{"content": "# Heading\n\nBody text."}"##;
        let extracted = extract_json_from_response(response).expect("pure JSON passes through");
        assert_eq!(extracted, response);
    }

    #[test]
    fn extract_json_errors_when_no_candidate_parses() {
        // A single malformed candidate and no other braces: we surface the
        // parse error with a preview so the user sees what went wrong.
        let response = "prose ${{ template }} more prose";
        let err = extract_json_from_response(response).expect_err("should fail");
        let msg = err.to_string();
        assert!(
            msg.contains("Preview:"),
            "error should include a preview: {msg}"
        );
    }

    #[test]
    fn pr_review_emoji_styling_uses_a_compact_gitmoji_guide() {
        let mut prompt = String::new();
        IrisAgent::inject_pr_review_emoji_styling(&mut prompt);

        assert!(prompt.contains("Common gitmoji choices:"));
        assert!(prompt.contains("`:feat:`"));
        assert!(prompt.contains("`:fix:`"));
        assert!(!prompt.contains("`:accessibility:`"));
        assert!(!prompt.contains("`:analytics:`"));
    }
}