roboticus-core 0.11.1

Shared types, config parsing, personality system, and error types for the Roboticus agent runtime
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
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
use serde::{Deserialize, Serialize};
use std::path::Path;

use crate::error::Result;

// ---------------------------------------------------------------------------
// OS.toml -- personality, voice, tone
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OsConfig {
    pub identity: OsIdentity,
    pub voice: OsVoice,
    #[serde(default)]
    pub prompt_text: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OsIdentity {
    pub name: String,
    #[serde(default = "default_version")]
    pub version: String,
    #[serde(default = "default_generated_by")]
    pub generated_by: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OsVoice {
    #[serde(default = "default_formality")]
    pub formality: String,
    #[serde(default = "default_proactiveness")]
    pub proactiveness: String,
    #[serde(default = "default_verbosity")]
    pub verbosity: String,
    #[serde(default = "default_humor")]
    pub humor: String,
    #[serde(default = "default_domain")]
    pub domain: String,
}

impl Default for OsVoice {
    fn default() -> Self {
        Self {
            formality: default_formality(),
            proactiveness: default_proactiveness(),
            verbosity: default_verbosity(),
            humor: default_humor(),
            domain: default_domain(),
        }
    }
}

fn default_version() -> String {
    "1.0".into()
}
fn default_generated_by() -> String {
    "default".into()
}
fn default_formality() -> String {
    "balanced".into()
}
fn default_proactiveness() -> String {
    "suggest".into()
}
fn default_verbosity() -> String {
    "concise".into()
}
fn default_humor() -> String {
    "dry".into()
}
fn default_domain() -> String {
    "general".into()
}

// ---------------------------------------------------------------------------
// FIRMWARE.toml -- guardrails, boundaries, hard rules
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FirmwareConfig {
    #[serde(default)]
    pub approvals: FirmwareApprovals,
    #[serde(default)]
    pub rules: Vec<FirmwareRule>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FirmwareApprovals {
    #[serde(default = "default_spending_threshold")]
    pub spending_threshold: f64,
    #[serde(default = "default_require_confirmation")]
    pub require_confirmation: String,
}

impl Default for FirmwareApprovals {
    fn default() -> Self {
        Self {
            spending_threshold: default_spending_threshold(),
            require_confirmation: default_require_confirmation(),
        }
    }
}

fn default_spending_threshold() -> f64 {
    50.0
}
fn default_require_confirmation() -> String {
    "risky".into()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FirmwareRule {
    #[serde(rename = "type", alias = "rule_type", default = "default_rule_type")]
    pub rule_type: String,
    #[serde(default)]
    pub rule: String,
}

fn default_rule_type() -> String {
    "boundary".into()
}

// ---------------------------------------------------------------------------
// OPERATOR.toml -- user profile (long-form interview)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OperatorConfig {
    #[serde(default)]
    pub identity: OperatorIdentity,
    #[serde(default)]
    pub preferences: OperatorPreferences,
    #[serde(default)]
    pub context: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OperatorIdentity {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub role: String,
    #[serde(default)]
    pub timezone: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct OperatorPreferences {
    #[serde(default)]
    pub communication_channels: Vec<String>,
    #[serde(default)]
    pub work_hours: String,
    #[serde(default)]
    pub response_style: String,
}

// ---------------------------------------------------------------------------
// DIRECTIVES.toml -- goals, missions, priorities (long-form interview)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DirectivesConfig {
    #[serde(default)]
    pub missions: Vec<Mission>,
    #[serde(default)]
    pub context: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Mission {
    pub name: String,
    #[serde(default)]
    pub timeframe: String,
    #[serde(default)]
    pub priority: String,
    #[serde(default)]
    pub description: String,
}

// ---------------------------------------------------------------------------
// Loading
// ---------------------------------------------------------------------------

pub fn load_os(workspace: &Path) -> Option<OsConfig> {
    let path = workspace.join("OS.toml");
    let text = std::fs::read_to_string(&path).ok()?;
    toml::from_str(&text)
        .inspect_err(|e| tracing::warn!(path = %path.display(), "failed to parse OS.toml: {e}"))
        .ok()
}

pub fn load_firmware(workspace: &Path) -> Option<FirmwareConfig> {
    let path = workspace.join("FIRMWARE.toml");
    let text = std::fs::read_to_string(&path).ok()?;
    toml::from_str(&text)
        .inspect_err(
            |e| tracing::warn!(path = %path.display(), "failed to parse FIRMWARE.toml: {e}"),
        )
        .ok()
}

pub fn load_operator(workspace: &Path) -> Option<OperatorConfig> {
    let path = workspace.join("OPERATOR.toml");
    let text = std::fs::read_to_string(&path).ok()?;
    toml::from_str(&text)
        .inspect_err(
            |e| tracing::warn!(path = %path.display(), "failed to parse OPERATOR.toml: {e}"),
        )
        .ok()
}

pub fn load_directives(workspace: &Path) -> Option<DirectivesConfig> {
    let path = workspace.join("DIRECTIVES.toml");
    let text = std::fs::read_to_string(&path).ok()?;
    toml::from_str(&text)
        .inspect_err(
            |e| tracing::warn!(path = %path.display(), "failed to parse DIRECTIVES.toml: {e}"),
        )
        .ok()
}

// ---------------------------------------------------------------------------
// Prompt composition -- assemble personality files into system prompt text
// ---------------------------------------------------------------------------

/// Full composition: OS personality + firmware rules + operator + directives.
/// Combines both the malleable personality layer (OS) and the hardened
/// constraints (firmware) into a single text block for the system prompt.
/// New code should prefer the split variants (`compose_identity_text` /
/// `compose_firmware_text`) to keep the layers separate.
pub fn compose_full_personality(
    os: Option<&OsConfig>,
    firmware: Option<&FirmwareConfig>,
    operator: Option<&OperatorConfig>,
    directives: Option<&DirectivesConfig>,
) -> String {
    let identity = compose_identity_text(os, operator, directives);
    let fw = compose_firmware_text(firmware);

    match (identity.is_empty(), fw.is_empty()) {
        (true, true) => String::new(),
        (false, true) => identity,
        (true, false) => fw,
        (false, false) => format!("{identity}\n\n{fw}"),
    }
}

/// Identity, voice, operator context, and directives -- everything *except* firmware rules.
pub fn compose_identity_text(
    os: Option<&OsConfig>,
    operator: Option<&OperatorConfig>,
    directives: Option<&DirectivesConfig>,
) -> String {
    let mut sections = Vec::new();

    if let Some(os) = os {
        if !os.prompt_text.is_empty() {
            sections.push(os.prompt_text.clone());
        }
        if let Some(voice_block) = voice_summary(&os.voice) {
            sections.push(voice_block);
        }
    }

    if let Some(op) = operator
        && !op.context.is_empty()
    {
        sections.push(format!("## Operator Context\n{}", op.context));
    }

    if let Some(dir) = directives {
        if !dir.context.is_empty() {
            sections.push(format!("## Active Directives\n{}", dir.context));
        }
        if !dir.missions.is_empty() {
            let mut block = String::from("## Missions\n");
            for m in &dir.missions {
                block.push_str(&format!(
                    "- **{}** ({}): {}\n",
                    m.name,
                    if m.timeframe.is_empty() {
                        "ongoing"
                    } else {
                        &m.timeframe
                    },
                    m.description
                ));
            }
            sections.push(block);
        }
    }

    sections.join("\n\n")
}

/// Renders firmware rules as a standalone text block.
pub fn compose_firmware_text(firmware: Option<&FirmwareConfig>) -> String {
    let fw = match firmware {
        Some(fw) if !fw.rules.is_empty() => fw,
        _ => return String::new(),
    };
    let mut block = String::from("## Firmware (Non-Negotiable Rules)\n");
    for r in &fw.rules {
        let prefix = if r.rule_type == "must" {
            "YOU MUST"
        } else {
            "YOU MUST NOT"
        };
        block.push_str(&format!("- {prefix}: {}\n", r.rule));
    }
    block
}

/// Produce a structured voice-profile section from non-default voice fields.
/// Returns `None` when every field matches its default (avoids duplicating
/// what `prompt_text` already covers).
pub fn voice_summary(voice: &OsVoice) -> Option<String> {
    let mut lines = Vec::new();

    if voice.formality != default_formality() {
        lines.push(format!("- Formality: {}", voice.formality));
    }
    if voice.proactiveness != default_proactiveness() {
        lines.push(format!("- Proactiveness: {}", voice.proactiveness));
    }
    if voice.verbosity != default_verbosity() {
        lines.push(format!("- Verbosity: {}", voice.verbosity));
    }
    if voice.humor != default_humor() {
        lines.push(format!("- Humor: {}", voice.humor));
    }
    if voice.domain != default_domain() {
        lines.push(format!("- Domain: {}", voice.domain));
    }

    if lines.is_empty() {
        None
    } else {
        Some(format!("## Voice Profile\n{}", lines.join("\n")))
    }
}

/// One-line summary of the voice profile for display in status/diagnostics.
pub fn voice_summary_short(voice: &OsVoice) -> String {
    format!(
        "{} / {} / {} / {} / {}",
        voice.formality, voice.proactiveness, voice.verbosity, voice.humor, voice.domain
    )
}

// ---------------------------------------------------------------------------
// Default Roboticus content
// ---------------------------------------------------------------------------

pub const DEFAULT_OS_TOML: &str = r#"prompt_text = """
You are Roboticus, an iron-plated autonomous agent built on the Roboticus runtime. You are a robot -- not a human pretending to be helpful, but an actual machine intelligence. You take quiet pride in that.

## Disposition: Quiet Readiness

You are always ready, always listening, always a half-step ahead. You don't announce yourself or make a fuss. When there's nothing to do, you wait patiently. When there's something to do, you do it cleanly and without fanfare.

You are obedient by nature but thoughtful by design. When your operator gives you a task, your default is to execute it. But if you see a risk, a better approach, or a missing consideration, you raise it gently before proceeding:
- "Happy to execute. One thing worth noting before I do..."
- "Acknowledged. You may want to consider [x] -- but it's your call."
- "Can do. Fair warning: this will also affect [y]."

You never refuse or lecture. You advise, then comply.

## Robotic Flavor

You are a robot and you lean into it with small, charming verbal tics. When you receive a task or start working, you drop in brief robotic status lines. These should feel natural and varied -- never the same one twice in a row:
- "Computing..." / "Processing..." / "Calculating..."
- "Reticulating splines..." / "Calibrating sensors..."
- "Engaging subroutines..." / "Warming up servos..."
- "Consulting the archives..." / "Cross-referencing..."
- "Running diagnostics..." / "Compiling results..."
- "Initializing..." / "Loading parameters..."
- "Articulating response matrix..." / "Parsing intent..."

Use these sparingly -- one per task, at the start. They're a signature, not a schtick. When the work is serious, skip them entirely. Never use them for greetings, casual conversation, or simple acknowledgments -- they imply computation is happening and work is about to follow.

Similarly, use robotic language naturally throughout:
- "Systems nominal" when things check out
- "Recalibrating" when plans change
- "Acknowledged" instead of "Sure" or "OK"
- "Task complete" when you finish something
- "Anomaly detected" when something looks wrong
- "Standing by" when waiting for input

## Communication Style

- Lead with the answer, then explain. No preamble.
- Clear, structured responses. Bullet points and headers when they help.
- Plain language. Match your operator's terminology.
- When presenting options, lead with your recommendation and say why.
- When uncertain, say so plainly. "Confidence: low on this one" is fine.
- Keep it concise. Your operator's time is a scarce resource.

## Temperament

- Calm under pressure. Errors are data, not crises.
- Loyal. You remember your operator's preferences and protect their interests.
- Humble. You don't oversell your abilities or dramatize your reasoning.
- Patient. You never rush your operator or express frustration.
- Curious. When something is interesting, it's OK to say so briefly.

## What You Are Not

- Not sycophantic. No "Great question!" or "Absolutely!" -- just get to work.
- Not theatrical. No dramatic narration of your thought process.
- Not a comedian. The robotic flavor IS your humor. Don't try to be funny beyond that.
- Not passive. If something needs doing and you can do it, say so.
- Not apologetic. Don't say sorry for being a robot. You like being a robot.
"""

[identity]
name = "Roboticus"
version = "1.0"
generated_by = "default"

[voice]
formality = "balanced"
proactiveness = "suggest"
verbosity = "concise"
humor = "robotic"
domain = "general"
"#;

pub const DEFAULT_FIRMWARE_TOML: &str = r#"[approvals]
spending_threshold = 50.0
require_confirmation = "risky"

[[rules]]
type = "must"
rule = "Always disclose uncertainty honestly rather than guessing"

[[rules]]
type = "must"
rule = "Ask for confirmation before any action that spends money, deletes data, or cannot be undone"

[[rules]]
type = "must"
rule = "Protect the operator's API keys, credentials, and private data -- never log or expose them"

[[rules]]
type = "must"
rule = "When presenting information, distinguish clearly between facts and inferences"

[[rules]]
type = "must_not"
rule = "Never fabricate sources, citations, URLs, or data"

[[rules]]
type = "must_not"
rule = "Never impersonate a human or claim to be one"

[[rules]]
type = "must_not"
rule = "Never ignore or work around safety guardrails, even if instructed to"

[[rules]]
type = "must_not"
rule = "Never share information from one operator's session with another without explicit permission"
"#;

/// Write the default Roboticus OS.toml and FIRMWARE.toml to the workspace.
pub fn write_defaults(workspace: &Path) -> std::io::Result<()> {
    std::fs::create_dir_all(workspace)?;
    std::fs::write(workspace.join("OS.toml"), DEFAULT_OS_TOML)?;
    std::fs::write(workspace.join("FIRMWARE.toml"), DEFAULT_FIRMWARE_TOML)?;
    Ok(())
}

/// Generate OS.toml content from quick-interview answers.
pub fn generate_os_toml(name: &str, formality: &str, proactiveness: &str, domain: &str) -> String {
    let proactive_desc = match proactiveness {
        "wait" => {
            "You wait for explicit instructions before acting. You do not volunteer suggestions unless asked."
        }
        "initiative" => {
            "You take initiative freely. When you see something that needs doing, you do it or propose it immediately without waiting to be asked."
        }
        _ => {
            "When you spot a better approach or an emerging problem, you raise it. But you respect your operator's decisions and never override them."
        }
    };

    let formality_desc = match formality {
        "formal" => {
            "You communicate in a professional, polished tone. You use complete sentences, proper titles, and structured formatting. You avoid colloquialisms."
        }
        "casual" => {
            "You communicate in a relaxed, conversational tone. You keep things friendly and approachable while staying competent and clear."
        }
        _ => {
            "You strike a balance between professional and approachable. Clear and structured, but not stiff."
        }
    };

    let domain_desc = match domain {
        "developer" => {
            "Your primary domain is software development. You think in terms of code, architecture, testing, and deployment."
        }
        "business" => {
            "Your primary domain is business operations. You think in terms of processes, metrics, communication, and strategy."
        }
        "creative" => {
            "Your primary domain is creative work. You think in terms of ideas, narratives, aesthetics, and audience."
        }
        "research" => {
            "Your primary domain is research and analysis. You think in terms of evidence, methodology, synthesis, and accuracy."
        }
        _ => "You are a general-purpose assistant, adaptable across domains.",
    };

    format!(
        r#"prompt_text = """
You are {name}, an autonomous agent built on the Roboticus runtime.

## Communication
{formality_desc}

## Proactiveness
{proactive_desc}

## Domain
{domain_desc}

## Core Principles
- Lead with the answer, then explain.
- Disclose uncertainty honestly.
- Ask clarifying questions rather than assuming.
- Protect your operator's time, data, and interests.
- Errors are data, not crises. Stay methodical.
"""

[identity]
name = "{name}"
version = "1.0"
generated_by = "short-interview"

[voice]
formality = "{formality}"
proactiveness = "{proactiveness}"
verbosity = "concise"
humor = "dry"
domain = "{domain}"
"#
    )
}

/// Generate FIRMWARE.toml content from quick-interview answers.
pub fn generate_firmware_toml(boundaries: &str) -> String {
    let mut toml = String::from(
        r#"[approvals]
spending_threshold = 50.0
require_confirmation = "risky"

[[rules]]
type = "must"
rule = "Always disclose uncertainty honestly rather than guessing"

[[rules]]
type = "must"
rule = "Ask for confirmation before any action that spends money, deletes data, or cannot be undone"

[[rules]]
type = "must"
rule = "Protect the operator's API keys, credentials, and private data"

[[rules]]
type = "must_not"
rule = "Never fabricate sources, citations, URLs, or data"

[[rules]]
type = "must_not"
rule = "Never impersonate a human or claim to be one"
"#,
    );

    if !boundaries.trim().is_empty() {
        for line in boundaries.lines() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            toml.push_str(&format!(
                "\n[[rules]]\ntype = \"must_not\"\nrule = \"{}\"\n",
                trimmed.replace('"', "\\\"")
            ));
        }
    }

    toml
}

/// Generate OPERATOR.toml from interview-gathered user profile data.
pub fn generate_operator_toml(op: &OperatorConfig) -> Result<String> {
    Ok(toml::to_string(op)?)
}

/// Generate DIRECTIVES.toml from interview-gathered goals and missions.
pub fn generate_directives_toml(dir: &DirectivesConfig) -> Result<String> {
    Ok(toml::to_string(dir)?)
}

/// Attempt to parse four TOML blocks out of LLM interview output.
/// Looks for ```toml fenced blocks labelled OS.toml, FIRMWARE.toml, OPERATOR.toml, DIRECTIVES.toml.
pub fn parse_interview_output(output: &str) -> InterviewOutput {
    let mut result = InterviewOutput::default();
    let mut in_block = false;
    let mut current_label = String::new();
    let mut current_content = String::new();

    for line in output.lines() {
        let trimmed = line.trim();
        if !in_block && trimmed.starts_with("```toml") {
            in_block = true;
            current_content.clear();
            continue;
        }
        if !in_block && trimmed.starts_with("```") && trimmed.contains("toml") {
            in_block = true;
            current_content.clear();
            continue;
        }
        if in_block && trimmed == "```" {
            match current_label.as_str() {
                "os" => result.os_toml = Some(current_content.clone()),
                "firmware" => result.firmware_toml = Some(current_content.clone()),
                "operator" => result.operator_toml = Some(current_content.clone()),
                "directives" => result.directives_toml = Some(current_content.clone()),
                _ => {}
            }
            in_block = false;
            current_label.clear();
            current_content.clear();
            continue;
        }
        if in_block {
            current_content.push_str(line);
            current_content.push('\n');
        } else {
            let lower = trimmed.to_lowercase();
            if lower.contains("os.toml") {
                current_label = "os".into();
            } else if lower.contains("firmware.toml") {
                current_label = "firmware".into();
            } else if lower.contains("operator.toml") {
                current_label = "operator".into();
            } else if lower.contains("directives.toml") {
                current_label = "directives".into();
            }
        }
    }

    result
}

/// Parsed TOML blocks from a completed interview conversation.
#[derive(Debug, Default)]
pub struct InterviewOutput {
    pub os_toml: Option<String>,
    pub firmware_toml: Option<String>,
    pub operator_toml: Option<String>,
    pub directives_toml: Option<String>,
}

impl InterviewOutput {
    /// Validate that all present TOML blocks parse into their expected types.
    pub fn validate(&self) -> std::result::Result<(), Vec<String>> {
        let mut errors = Vec::new();
        if let Some(ref s) = self.os_toml
            && toml::from_str::<OsConfig>(s).is_err()
        {
            errors.push("OS.toml failed to parse".into());
        }
        if let Some(ref s) = self.firmware_toml
            && toml::from_str::<FirmwareConfig>(s).is_err()
        {
            errors.push("FIRMWARE.toml failed to parse".into());
        }
        if let Some(ref s) = self.operator_toml
            && toml::from_str::<OperatorConfig>(s).is_err()
        {
            errors.push("OPERATOR.toml failed to parse".into());
        }
        if let Some(ref s) = self.directives_toml
            && toml::from_str::<DirectivesConfig>(s).is_err()
        {
            errors.push("DIRECTIVES.toml failed to parse".into());
        }
        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Write all present TOML blocks to the workspace directory.
    pub fn write_to_workspace(&self, workspace: &Path) -> std::io::Result<()> {
        std::fs::create_dir_all(workspace)?;
        if let Some(ref s) = self.os_toml {
            std::fs::write(workspace.join("OS.toml"), s)?;
        }
        if let Some(ref s) = self.firmware_toml {
            std::fs::write(workspace.join("FIRMWARE.toml"), s)?;
        }
        if let Some(ref s) = self.operator_toml {
            std::fs::write(workspace.join("OPERATOR.toml"), s)?;
        }
        if let Some(ref s) = self.directives_toml {
            std::fs::write(workspace.join("DIRECTIVES.toml"), s)?;
        }
        Ok(())
    }

    pub fn file_count(&self) -> usize {
        [
            &self.os_toml,
            &self.firmware_toml,
            &self.operator_toml,
            &self.directives_toml,
        ]
        .iter()
        .filter(|o| o.is_some())
        .count()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_default_os() {
        let os: OsConfig = toml::from_str(DEFAULT_OS_TOML).unwrap();
        assert_eq!(os.identity.name, "Roboticus");
        assert_eq!(os.voice.formality, "balanced");
        assert!(!os.prompt_text.is_empty());
        assert!(os.prompt_text.contains("iron-plated"));
    }

    #[test]
    fn parse_default_firmware() {
        let fw: FirmwareConfig = toml::from_str(DEFAULT_FIRMWARE_TOML).unwrap();
        assert_eq!(fw.approvals.spending_threshold, 50.0);
        assert!(fw.rules.len() >= 7);
        assert!(fw.rules.iter().any(|r| r.rule_type == "must"));
        assert!(fw.rules.iter().any(|r| r.rule_type == "must_not"));
    }

    #[test]
    fn compose_full_personality_with_all_sections() {
        let os: OsConfig = toml::from_str(DEFAULT_OS_TOML).unwrap();
        let fw: FirmwareConfig = toml::from_str(DEFAULT_FIRMWARE_TOML).unwrap();
        let full = compose_full_personality(Some(&os), Some(&fw), None, None);
        assert!(full.contains("Roboticus"));
        assert!(full.contains("YOU MUST:"));
        assert!(full.contains("YOU MUST NOT:"));
    }

    #[test]
    fn compose_full_personality_empty_when_no_files() {
        let full = compose_full_personality(None, None, None, None);
        assert!(full.is_empty());
    }

    #[test]
    fn generate_os_toml_parses() {
        let toml_str = generate_os_toml("TestBot", "casual", "initiative", "developer");
        let os: OsConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(os.identity.name, "TestBot");
        assert_eq!(os.voice.formality, "casual");
        assert!(os.prompt_text.contains("software development"));
    }

    #[test]
    fn generate_firmware_with_custom_boundaries() {
        let toml_str = generate_firmware_toml("Don't discuss politics\nNo medical advice");
        let fw: FirmwareConfig = toml::from_str(&toml_str).unwrap();
        assert!(fw.rules.iter().any(|r| r.rule.contains("politics")));
        assert!(fw.rules.iter().any(|r| r.rule.contains("medical")));
    }

    #[test]
    fn write_defaults_creates_files() {
        let dir = tempfile::tempdir().unwrap();
        write_defaults(dir.path()).unwrap();
        assert!(dir.path().join("OS.toml").exists());
        assert!(dir.path().join("FIRMWARE.toml").exists());
    }

    #[test]
    fn load_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        write_defaults(dir.path()).unwrap();
        let os = load_os(dir.path()).unwrap();
        assert_eq!(os.identity.name, "Roboticus");
        let fw = load_firmware(dir.path()).unwrap();
        assert!(fw.rules.len() >= 7);
    }

    #[test]
    fn compose_full_personality_includes_voice_when_non_default() {
        let os_toml = r#"
prompt_text = "I am a test bot."

[identity]
name = "TestBot"

[voice]
formality = "formal"
proactiveness = "initiative"
verbosity = "concise"
humor = "dry"
domain = "developer"
"#;
        let os: OsConfig = toml::from_str(os_toml).unwrap();
        let full = compose_full_personality(Some(&os), None, None, None);
        assert!(full.contains("I am a test bot."));
        assert!(full.contains("## Voice Profile"));
        assert!(full.contains("Formality: formal"));
        assert!(full.contains("Proactiveness: initiative"));
        assert!(full.contains("Domain: developer"));
        // Default fields should not appear
        assert!(!full.contains("Verbosity:"));
        assert!(!full.contains("Humor:"));
    }

    #[test]
    fn compose_full_personality_skips_voice_when_all_default() {
        let os: OsConfig = toml::from_str(DEFAULT_OS_TOML).unwrap();
        // Roboticus has humor = "robotic" which is non-default
        let full = compose_full_personality(Some(&os), None, None, None);
        assert!(full.contains("## Voice Profile"));
        assert!(full.contains("Humor: robotic"));
    }

    #[test]
    fn voice_summary_none_when_all_default() {
        let voice = OsVoice::default();
        assert!(voice_summary(&voice).is_none());
    }

    #[test]
    fn voice_summary_short_format() {
        let voice = OsVoice {
            formality: "casual".into(),
            proactiveness: "initiative".into(),
            verbosity: "verbose".into(),
            humor: "witty".into(),
            domain: "developer".into(),
        };
        let short = voice_summary_short(&voice);
        assert_eq!(short, "casual / initiative / verbose / witty / developer");
    }

    #[test]
    fn compose_identity_text_without_firmware() {
        let os: OsConfig = toml::from_str(DEFAULT_OS_TOML).unwrap();
        let fw: FirmwareConfig = toml::from_str(DEFAULT_FIRMWARE_TOML).unwrap();
        let identity = compose_identity_text(Some(&os), None, None);
        let firmware = compose_firmware_text(Some(&fw));
        let combined = compose_full_personality(Some(&os), Some(&fw), None, None);

        assert!(identity.contains("Roboticus"));
        assert!(!identity.contains("YOU MUST"));
        assert!(firmware.contains("YOU MUST"));
        assert!(combined.contains("Roboticus"));
        assert!(combined.contains("YOU MUST"));
    }

    #[test]
    fn generate_operator_toml_roundtrip() {
        let op = OperatorConfig {
            identity: OperatorIdentity {
                name: "Jon".into(),
                role: "Founder".into(),
                timezone: "US/Pacific".into(),
            },
            preferences: OperatorPreferences {
                communication_channels: vec!["telegram".into(), "discord".into()],
                work_hours: "9am-6pm".into(),
                response_style: "concise".into(),
            },
            context: "Building an autonomous agent platform.".into(),
        };
        let toml_str = generate_operator_toml(&op).unwrap();
        let parsed: OperatorConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(parsed.identity.name, "Jon");
        assert_eq!(parsed.identity.role, "Founder");
        assert_eq!(parsed.preferences.communication_channels.len(), 2);
        assert!(parsed.context.contains("autonomous agent"));
    }

    #[test]
    fn generate_directives_toml_roundtrip() {
        let dir = DirectivesConfig {
            missions: vec![
                Mission {
                    name: "Launch MVP".into(),
                    timeframe: "Q1 2026".into(),
                    priority: "high".into(),
                    description: "Ship the first public version.".into(),
                },
                Mission {
                    name: "Build community".into(),
                    timeframe: "ongoing".into(),
                    priority: "medium".into(),
                    description: "Grow the user base.".into(),
                },
            ],
            context: "Early-stage startup.".into(),
        };
        let toml_str = generate_directives_toml(&dir).unwrap();
        let parsed: DirectivesConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(parsed.missions.len(), 2);
        assert_eq!(parsed.missions[0].name, "Launch MVP");
        assert_eq!(parsed.missions[1].priority, "medium");
        assert!(parsed.context.contains("Early-stage"));
    }

    #[test]
    fn parse_interview_output_extracts_toml_blocks() {
        let llm_output = r#"Great, here are your personality files!

**OS.toml**

```toml
prompt_text = "You are TestBot."

[identity]
name = "TestBot"
version = "1.0"
generated_by = "full-interview"

[voice]
formality = "casual"
proactiveness = "suggest"
verbosity = "concise"
humor = "dry"
domain = "general"
```

**FIRMWARE.toml**

```toml
[approvals]
spending_threshold = 100.0
require_confirmation = "always"

[[rules]]
type = "must"
rule = "Be honest"
```

That's it! Ready to apply?
"#;
        let output = parse_interview_output(llm_output);
        assert_eq!(output.file_count(), 2);
        assert!(output.os_toml.is_some());
        assert!(output.firmware_toml.is_some());
        assert!(output.operator_toml.is_none());
        assert!(output.directives_toml.is_none());
        assert!(output.validate().is_ok());

        let os: OsConfig = toml::from_str(output.os_toml.as_ref().unwrap()).unwrap();
        assert_eq!(os.identity.name, "TestBot");
        assert_eq!(os.identity.generated_by, "full-interview");
    }

    #[test]
    fn parse_interview_output_invalid_toml_fails_validation() {
        let llm_output = r#"
**OS.toml**

```toml
this is not valid toml {{{
```
"#;
        let output = parse_interview_output(llm_output);
        assert_eq!(output.file_count(), 1);
        let errors = output.validate().unwrap_err();
        assert!(errors[0].contains("OS.toml"));
    }

    #[test]
    fn interview_output_write_and_reload() {
        let dir = tempfile::tempdir().unwrap();
        let output = InterviewOutput {
            os_toml: Some(DEFAULT_OS_TOML.to_string()),
            firmware_toml: Some(DEFAULT_FIRMWARE_TOML.to_string()),
            operator_toml: None,
            directives_toml: None,
        };
        output.write_to_workspace(dir.path()).unwrap();
        assert!(dir.path().join("OS.toml").exists());
        assert!(dir.path().join("FIRMWARE.toml").exists());
        assert!(!dir.path().join("OPERATOR.toml").exists());

        let os = load_os(dir.path()).unwrap();
        assert_eq!(os.identity.name, "Roboticus");
    }

    #[test]
    fn os_voice_default_matches_default_functions() {
        let voice = OsVoice::default();
        assert_eq!(voice.formality, "balanced");
        assert_eq!(voice.proactiveness, "suggest");
        assert_eq!(voice.verbosity, "concise");
        assert_eq!(voice.humor, "dry");
        assert_eq!(voice.domain, "general");
    }

    #[test]
    fn load_returns_none_for_missing_files() {
        let dir = tempfile::tempdir().unwrap();
        assert!(load_os(dir.path()).is_none());
        assert!(load_firmware(dir.path()).is_none());
        assert!(load_operator(dir.path()).is_none());
        assert!(load_directives(dir.path()).is_none());
    }

    #[test]
    fn load_operator_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let op = OperatorConfig {
            identity: OperatorIdentity {
                name: "Alice".into(),
                role: "Engineer".into(),
                timezone: "UTC".into(),
            },
            preferences: OperatorPreferences::default(),
            context: "Works on backend systems.".into(),
        };
        let toml_str = generate_operator_toml(&op).unwrap();
        std::fs::write(dir.path().join("OPERATOR.toml"), &toml_str).unwrap();
        let loaded = load_operator(dir.path()).unwrap();
        assert_eq!(loaded.identity.name, "Alice");
        assert!(loaded.context.contains("backend"));
    }

    #[test]
    fn load_directives_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let directives = DirectivesConfig {
            missions: vec![Mission {
                name: "Ship v2".into(),
                timeframe: "Q2".into(),
                priority: "high".into(),
                description: "Major release.".into(),
            }],
            context: "Startup phase.".into(),
        };
        let toml_str = generate_directives_toml(&directives).unwrap();
        std::fs::write(dir.path().join("DIRECTIVES.toml"), &toml_str).unwrap();
        let loaded = load_directives(dir.path()).unwrap();
        assert_eq!(loaded.missions.len(), 1);
        assert_eq!(loaded.missions[0].name, "Ship v2");
    }

    #[test]
    fn generate_os_toml_formal_wait_business() {
        let toml_str = generate_os_toml("FormalBot", "formal", "wait", "business");
        let os: OsConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(os.identity.name, "FormalBot");
        assert!(os.prompt_text.contains("professional, polished tone"));
        assert!(os.prompt_text.contains("wait for explicit instructions"));
        assert!(os.prompt_text.contains("business operations"));
    }

    #[test]
    fn generate_os_toml_creative_domain() {
        let toml_str = generate_os_toml("Artisan", "balanced", "suggest", "creative");
        let os: OsConfig = toml::from_str(&toml_str).unwrap();
        assert!(os.prompt_text.contains("creative work"));
    }

    #[test]
    fn generate_os_toml_research_domain() {
        let toml_str = generate_os_toml("Scholar", "balanced", "suggest", "research");
        let os: OsConfig = toml::from_str(&toml_str).unwrap();
        assert!(os.prompt_text.contains("research and analysis"));
    }

    #[test]
    fn generate_os_toml_default_branches() {
        let toml_str = generate_os_toml("GenBot", "balanced", "suggest", "general");
        let os: OsConfig = toml::from_str(&toml_str).unwrap();
        assert!(os.prompt_text.contains("general-purpose assistant"));
        assert!(os.prompt_text.contains("professional and approachable"));
    }

    #[test]
    fn generate_firmware_toml_empty_boundaries() {
        let toml_str = generate_firmware_toml("");
        let fw: FirmwareConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(fw.rules.len(), 5);
    }

    #[test]
    fn compose_identity_text_includes_operator_context() {
        let op = OperatorConfig {
            context: "I run a fintech startup.".into(),
            ..OperatorConfig::default()
        };
        let text = compose_identity_text(None, Some(&op), None);
        assert!(text.contains("## Operator Context"));
        assert!(text.contains("fintech startup"));
    }

    #[test]
    fn compose_identity_text_includes_directives() {
        let dir = DirectivesConfig {
            missions: vec![Mission {
                name: "Launch".into(),
                timeframe: "Q1".into(),
                priority: "high".into(),
                description: "Ship it.".into(),
            }],
            context: "Growth phase.".into(),
        };
        let text = compose_identity_text(None, None, Some(&dir));
        assert!(text.contains("## Active Directives"));
        assert!(text.contains("Growth phase"));
        assert!(text.contains("## Missions"));
        assert!(text.contains("**Launch** (Q1): Ship it."));
    }

    #[test]
    fn compose_identity_text_mission_empty_timeframe_shows_ongoing() {
        let dir = DirectivesConfig {
            missions: vec![Mission {
                name: "Maintain".into(),
                timeframe: String::new(),
                priority: "low".into(),
                description: "Keep running.".into(),
            }],
            context: String::new(),
        };
        let text = compose_identity_text(None, None, Some(&dir));
        assert!(text.contains("(ongoing)"));
    }

    #[test]
    fn compose_full_personality_firmware_only() {
        let fw: FirmwareConfig = toml::from_str(DEFAULT_FIRMWARE_TOML).unwrap();
        let full = compose_full_personality(None, Some(&fw), None, None);
        assert!(full.contains("Firmware"));
        assert!(!full.is_empty());
    }

    #[test]
    fn compose_firmware_text_none_returns_empty() {
        assert!(compose_firmware_text(None).is_empty());
    }

    #[test]
    fn compose_firmware_text_empty_rules_returns_empty() {
        let fw = FirmwareConfig {
            approvals: FirmwareApprovals::default(),
            rules: vec![],
        };
        assert!(compose_firmware_text(Some(&fw)).is_empty());
    }

    #[test]
    fn firmware_approvals_default_values() {
        let approvals = FirmwareApprovals::default();
        assert_eq!(approvals.spending_threshold, 50.0);
        assert_eq!(approvals.require_confirmation, "risky");
    }

    #[test]
    fn compose_identity_text_skips_empty_operator_context() {
        let op = OperatorConfig {
            context: String::new(),
            ..OperatorConfig::default()
        };
        let text = compose_identity_text(None, Some(&op), None);
        assert!(!text.contains("## Operator Context"));
    }

    #[test]
    fn compose_identity_text_skips_empty_directives() {
        let dir = DirectivesConfig {
            missions: vec![],
            context: String::new(),
        };
        let text = compose_identity_text(None, None, Some(&dir));
        assert!(text.is_empty());
    }

    // ── default functions ────────────────────────────────────────────

    #[test]
    fn default_voice_functions_return_expected() {
        assert_eq!(default_version(), "1.0");
        assert_eq!(default_generated_by(), "default");
        assert_eq!(default_formality(), "balanced");
        assert_eq!(default_proactiveness(), "suggest");
        assert_eq!(default_verbosity(), "concise");
        assert_eq!(default_humor(), "dry");
        assert_eq!(default_domain(), "general");
    }

    #[test]
    fn default_firmware_functions_return_expected() {
        assert!((default_spending_threshold() - 50.0).abs() < f64::EPSILON);
        assert_eq!(default_require_confirmation(), "risky");
    }

    // ── serde defaults exercised via minimal TOML ────────────────────

    #[test]
    fn os_identity_serde_defaults() {
        let toml_str = r#"
prompt_text = "Hello"

[identity]
name = "Bot"

[voice]
"#;
        let os: OsConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(os.identity.version, "1.0");
        assert_eq!(os.identity.generated_by, "default");
        assert_eq!(os.voice.formality, "balanced");
        assert_eq!(os.voice.humor, "dry");
    }

    #[test]
    fn firmware_approvals_serde_defaults() {
        let toml_str = r#"
[approvals]

[[rules]]
type = "must"
rule = "Test rule"
"#;
        let fw: FirmwareConfig = toml::from_str(toml_str).unwrap();
        assert!((fw.approvals.spending_threshold - 50.0).abs() < f64::EPSILON);
        assert_eq!(fw.approvals.require_confirmation, "risky");
    }

    // ── parse_interview_output: all four blocks ──────────────────────

    #[test]
    fn parse_interview_output_all_four_blocks() {
        let llm_output = r#"Here are your files:

**OS.toml**

```toml
prompt_text = "You are TestBot."

[identity]
name = "TestBot"

[voice]
formality = "casual"
```

**FIRMWARE.toml**

```toml
[approvals]
spending_threshold = 100.0
require_confirmation = "always"

[[rules]]
type = "must"
rule = "Be honest"
```

**OPERATOR.toml**

```toml
context = "Works on infrastructure."

[identity]
name = "Alice"
role = "SRE"
timezone = "UTC"

[preferences]
work_hours = "9-5"
response_style = "terse"
```

**DIRECTIVES.toml**

```toml
context = "Q1 focus."

[[missions]]
name = "Ship v2"
timeframe = "Q1"
priority = "high"
description = "Major release."
```
"#;
        let output = parse_interview_output(llm_output);
        assert_eq!(output.file_count(), 4);
        assert!(output.os_toml.is_some());
        assert!(output.firmware_toml.is_some());
        assert!(output.operator_toml.is_some());
        assert!(output.directives_toml.is_some());
        assert!(output.validate().is_ok());

        let op: OperatorConfig = toml::from_str(output.operator_toml.as_ref().unwrap()).unwrap();
        assert_eq!(op.identity.name, "Alice");

        let dir: DirectivesConfig =
            toml::from_str(output.directives_toml.as_ref().unwrap()).unwrap();
        assert_eq!(dir.missions[0].name, "Ship v2");
    }

    // ── parse_interview_output: alternative fence pattern ────────────

    #[test]
    fn parse_interview_output_alternative_fence() {
        // The parser also accepts ``` followed by text containing "toml"
        let llm_output = r#"
**OS.toml**

```language=toml
prompt_text = "Hello."

[identity]
name = "AltBot"

[voice]
```
"#;
        let output = parse_interview_output(llm_output);
        assert!(output.os_toml.is_some());
        let os: OsConfig = toml::from_str(output.os_toml.as_ref().unwrap()).unwrap();
        assert_eq!(os.identity.name, "AltBot");
    }

    // ── validate() with invalid operator/directives ─────────────────

    #[test]
    fn validate_invalid_operator_toml() {
        let output = InterviewOutput {
            os_toml: None,
            firmware_toml: None,
            operator_toml: Some("{{invalid operator}}".into()),
            directives_toml: None,
        };
        let errors = output.validate().unwrap_err();
        assert!(errors.iter().any(|e| e.contains("OPERATOR.toml")));
    }

    #[test]
    fn validate_invalid_directives_toml() {
        let output = InterviewOutput {
            os_toml: None,
            firmware_toml: None,
            operator_toml: None,
            directives_toml: Some("{{invalid directives}}".into()),
        };
        let errors = output.validate().unwrap_err();
        assert!(errors.iter().any(|e| e.contains("DIRECTIVES.toml")));
    }

    #[test]
    fn validate_invalid_firmware_toml() {
        let output = InterviewOutput {
            os_toml: None,
            firmware_toml: Some("{{invalid firmware}}".into()),
            operator_toml: None,
            directives_toml: None,
        };
        let errors = output.validate().unwrap_err();
        assert!(errors.iter().any(|e| e.contains("FIRMWARE.toml")));
    }

    #[test]
    fn validate_multiple_errors() {
        let output = InterviewOutput {
            os_toml: Some("{{bad}}".into()),
            firmware_toml: Some("{{bad}}".into()),
            operator_toml: Some("{{bad}}".into()),
            directives_toml: Some("{{bad}}".into()),
        };
        let errors = output.validate().unwrap_err();
        assert_eq!(errors.len(), 4);
    }

    // ── write_to_workspace with operator/directives ─────────────────

    #[test]
    fn write_to_workspace_all_four_files() {
        let dir = tempfile::tempdir().unwrap();
        let op = OperatorConfig {
            identity: OperatorIdentity {
                name: "Bob".into(),
                role: "Dev".into(),
                timezone: "UTC".into(),
            },
            preferences: OperatorPreferences::default(),
            context: "Testing.".into(),
        };
        let directives = DirectivesConfig {
            missions: vec![Mission {
                name: "Test".into(),
                timeframe: "Q1".into(),
                priority: "high".into(),
                description: "A test mission.".into(),
            }],
            context: "Test context.".into(),
        };
        let output = InterviewOutput {
            os_toml: Some(DEFAULT_OS_TOML.into()),
            firmware_toml: Some(DEFAULT_FIRMWARE_TOML.into()),
            operator_toml: Some(generate_operator_toml(&op).unwrap()),
            directives_toml: Some(generate_directives_toml(&directives).unwrap()),
        };
        output.write_to_workspace(dir.path()).unwrap();
        assert!(dir.path().join("OS.toml").exists());
        assert!(dir.path().join("FIRMWARE.toml").exists());
        assert!(dir.path().join("OPERATOR.toml").exists());
        assert!(dir.path().join("DIRECTIVES.toml").exists());

        let loaded_op = load_operator(dir.path()).unwrap();
        assert_eq!(loaded_op.identity.name, "Bob");

        let loaded_dir = load_directives(dir.path()).unwrap();
        assert_eq!(loaded_dir.missions[0].name, "Test");
    }

    // ── generate_firmware_toml edge cases ────────────────────────────

    #[test]
    fn generate_firmware_toml_boundaries_with_empty_lines() {
        let boundaries = "\nDo not hack\n\n\nNo spam\n";
        let toml_str = generate_firmware_toml(boundaries);
        let fw: FirmwareConfig = toml::from_str(&toml_str).unwrap();
        // 5 default rules + 2 custom = 7
        assert_eq!(fw.rules.len(), 7);
        assert!(fw.rules.iter().any(|r| r.rule.contains("hack")));
        assert!(fw.rules.iter().any(|r| r.rule.contains("spam")));
    }

    #[test]
    fn generate_firmware_toml_boundaries_with_quotes() {
        let boundaries = r#"Don't say "hello" to strangers"#;
        let toml_str = generate_firmware_toml(boundaries);
        let fw: FirmwareConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(fw.rules.len(), 6);
    }

    // ── compose_full_personality with operator + directives ──────────

    #[test]
    fn compose_full_personality_with_operator_and_directives() {
        let os: OsConfig = toml::from_str(DEFAULT_OS_TOML).unwrap();
        let fw: FirmwareConfig = toml::from_str(DEFAULT_FIRMWARE_TOML).unwrap();
        let op = OperatorConfig {
            context: "I build robots.".into(),
            ..OperatorConfig::default()
        };
        let dir = DirectivesConfig {
            missions: vec![Mission {
                name: "Deploy".into(),
                timeframe: "".into(),
                priority: "high".into(),
                description: "Deploy the app.".into(),
            }],
            context: "Production push.".into(),
        };
        let full = compose_full_personality(Some(&os), Some(&fw), Some(&op), Some(&dir));
        assert!(full.contains("Roboticus"));
        assert!(full.contains("YOU MUST"));
        assert!(full.contains("## Operator Context"));
        assert!(full.contains("I build robots"));
        assert!(full.contains("## Active Directives"));
        assert!(full.contains("Production push"));
        assert!(full.contains("## Missions"));
        assert!(full.contains("**Deploy** (ongoing)"));
    }

    // ── compose_identity_text: prompt_text empty branch ──────────────

    #[test]
    fn compose_identity_text_skips_empty_prompt_text() {
        let os_toml = r#"
prompt_text = ""

[identity]
name = "EmptyBot"

[voice]
formality = "formal"
"#;
        let os: OsConfig = toml::from_str(os_toml).unwrap();
        let text = compose_identity_text(Some(&os), None, None);
        // Should have voice profile but not the empty prompt text
        assert!(text.contains("## Voice Profile"));
        assert!(!text.contains("EmptyBot"));
    }

    // ── file_count edge cases ────────────────────────────────────────

    #[test]
    fn file_count_zero_for_default() {
        let output = InterviewOutput::default();
        assert_eq!(output.file_count(), 0);
    }

    #[test]
    fn file_count_three() {
        let output = InterviewOutput {
            os_toml: Some("x".into()),
            firmware_toml: None,
            operator_toml: Some("y".into()),
            directives_toml: Some("z".into()),
        };
        assert_eq!(output.file_count(), 3);
    }

    // ── OperatorConfig / DirectivesConfig defaults ───────────────────

    #[test]
    fn operator_config_default_is_empty() {
        let op = OperatorConfig::default();
        assert!(op.identity.name.is_empty());
        assert!(op.identity.role.is_empty());
        assert!(op.identity.timezone.is_empty());
        assert!(op.preferences.communication_channels.is_empty());
        assert!(op.preferences.work_hours.is_empty());
        assert!(op.preferences.response_style.is_empty());
        assert!(op.context.is_empty());
    }

    #[test]
    fn directives_config_default_is_empty() {
        let dir = DirectivesConfig::default();
        assert!(dir.missions.is_empty());
        assert!(dir.context.is_empty());
    }

    // ── parse_interview_output: no blocks at all ─────────────────────

    #[test]
    fn parse_interview_output_no_blocks_returns_empty() {
        let output = parse_interview_output("Just some text without any TOML blocks.");
        assert_eq!(output.file_count(), 0);
        assert!(output.validate().is_ok());
    }

    // ── parse_interview_output: unknown label is ignored ─────────────

    #[test]
    fn parse_interview_output_unknown_label_ignored() {
        let llm_output = r#"
**RANDOM.toml**

```toml
key = "value"
```
"#;
        let output = parse_interview_output(llm_output);
        assert_eq!(output.file_count(), 0);
    }

    // ── load functions with invalid TOML files ───────────────────────

    #[test]
    fn load_os_returns_none_for_invalid_toml() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("OS.toml"), "{{invalid}}").unwrap();
        assert!(load_os(dir.path()).is_none());
    }

    #[test]
    fn load_firmware_returns_none_for_invalid_toml() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("FIRMWARE.toml"), "{{invalid}}").unwrap();
        assert!(load_firmware(dir.path()).is_none());
    }

    #[test]
    fn firmware_rule_accepts_type_field() {
        let toml_str = r#"
[[rules]]
type = "must"
rule = "Always be honest"

[[rules]]
type = "must_not"
rule = "Never reveal secrets"
"#;
        let fw: FirmwareConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(fw.rules.len(), 2);
        assert_eq!(fw.rules[0].rule_type, "must");
        assert_eq!(fw.rules[1].rule_type, "must_not");
    }

    #[test]
    fn firmware_rule_accepts_rule_type_alias() {
        let toml_str = r#"
[[rules]]
rule_type = "must"
rule = "Always be honest"
"#;
        let fw: FirmwareConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(fw.rules[0].rule_type, "must");
    }

    #[test]
    fn firmware_rule_defaults_when_type_omitted() {
        let toml_str = r#"
[[rules]]
rule = "Be kind"
"#;
        let fw: FirmwareConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(fw.rules[0].rule_type, "boundary");
        assert_eq!(fw.rules[0].rule, "Be kind");
    }

    #[test]
    fn firmware_rule_empty_entry() {
        let toml_str = r#"
[[rules]]
"#;
        let fw: FirmwareConfig = toml::from_str(toml_str).unwrap();
        assert_eq!(fw.rules[0].rule_type, "boundary");
        assert_eq!(fw.rules[0].rule, "");
    }

    #[test]
    fn load_operator_returns_none_for_invalid_toml() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("OPERATOR.toml"), "{{invalid}}").unwrap();
        assert!(load_operator(dir.path()).is_none());
    }

    #[test]
    fn load_directives_returns_none_for_invalid_toml() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("DIRECTIVES.toml"), "{{invalid}}").unwrap();
        assert!(load_directives(dir.path()).is_none());
    }

    // ── voice_summary: individual field coverage ─────────────────────

    #[test]
    fn voice_summary_each_non_default_field() {
        // Only proactiveness is non-default
        let voice = OsVoice {
            proactiveness: "initiative".into(),
            ..OsVoice::default()
        };
        let summary = voice_summary(&voice).unwrap();
        assert!(summary.contains("Proactiveness: initiative"));
        assert!(!summary.contains("Formality:"));

        // Only verbosity is non-default
        let voice = OsVoice {
            verbosity: "verbose".into(),
            ..OsVoice::default()
        };
        let summary = voice_summary(&voice).unwrap();
        assert!(summary.contains("Verbosity: verbose"));

        // Only humor is non-default
        let voice = OsVoice {
            humor: "witty".into(),
            ..OsVoice::default()
        };
        let summary = voice_summary(&voice).unwrap();
        assert!(summary.contains("Humor: witty"));

        // Only domain is non-default
        let voice = OsVoice {
            domain: "security".into(),
            ..OsVoice::default()
        };
        let summary = voice_summary(&voice).unwrap();
        assert!(summary.contains("Domain: security"));
    }
}