heartbit-core 2026.613.1

The Rust agentic framework — agents, tools, LLM providers, memory, evaluation.
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
use serde::Deserialize;

use crate::Error;

/// Daemon mode configuration for runtime execution.
///
/// When `kafka` is absent, the daemon runs in HTTP-only mode: it serves
/// `/v1/tasks/execute` for cloud-delegated execution but does not consume
/// Kafka commands or produce events. This is the recommended mode for the
/// 3-tier architecture where Kafka lives in the gateway, not the runtime.
///
/// Input-source fields (schedules, sensors, ws, telegram, heartbit_pulse,
/// owner_emails, mcp_server) have been moved to the gateway crate as part
/// of the 3-tier architecture refactoring. The type definitions are kept
/// here so they can be re-used by the gateway.
#[derive(Debug, Clone, Default, Deserialize)]
#[non_exhaustive]
pub struct DaemonConfig {
    /// Kafka transport configuration. Omit for in-process channel mode.
    #[serde(default)]
    pub kafka: Option<KafkaConfig>,
    /// HTTP bind address (e.g. `0.0.0.0:8080`).
    #[serde(default = "default_daemon_bind")]
    pub bind: String,
    /// Maximum concurrently-executing tasks across all consumers.
    #[serde(default = "default_max_concurrent")]
    pub max_concurrent_tasks: usize,
    /// Prometheus metrics configuration. Metrics are enabled by default.
    #[serde(default)]
    pub metrics: Option<MetricsConfig>,
    /// PostgreSQL URL for durable task persistence. When absent, tasks are
    /// stored in-memory (lost on restart).
    #[serde(default)]
    pub database_url: Option<String>,
    /// HTTP API authentication configuration.
    pub auth: Option<AuthConfig>,
    /// Memory access control configuration.
    #[serde(default)]
    pub memory: DaemonMemoryConfig,
    /// Audit log retention configuration.
    #[serde(default)]
    pub audit: DaemonAuditConfig,
    /// Idempotency-key TTL sweep configuration.
    #[serde(default)]
    pub idempotency: IdempotencyConfig,
    /// Per-persona X/Twitter mention-poll configurations.
    /// Each entry launches a `MentionPollScheduler` that fires
    /// `DaemonCommand::MentionPoll` on its configured interval.
    #[cfg(feature = "ghost-domain-config")]
    #[serde(default)]
    pub persona_mentions: Vec<PersonaMentionsConfig>,
    /// Per-persona proactive-posting configuration. One entry per
    /// persona that has proactive posting enabled.
    #[cfg(feature = "ghost-domain-config")]
    #[serde(default)]
    pub persona_posts: Vec<PersonaPostsConfig>,
    /// Per-persona quote-tweet configurations. Each entry launches a
    /// `PersonaQuoteScheduler`.
    #[cfg(feature = "ghost-domain-config")]
    #[serde(default)]
    pub persona_quotes: Vec<PersonaQuotesConfig>,
    /// Personal blog configuration. Single block (one blog per daemon).
    /// When absent, the daemon does not spawn a blog scheduler.
    #[cfg(feature = "ghost-domain-config")]
    #[serde(default)]
    pub persona_blog: Option<PersonaBlogConfig>,
}

/// MCP server configuration for the daemon.
///
/// When present, the daemon exposes an MCP-compatible endpoint at `/mcp`
/// so external MCP clients can discover and call heartbit tools/resources.
#[derive(Debug, Clone, Deserialize)]
pub struct DaemonMcpServerConfig {
    /// Server name reported in the `initialize` response. Defaults to `"heartbit"`.
    #[serde(default = "default_mcp_server_name")]
    pub name: String,
    /// Whether to expose heartbit tools via MCP. Default: `true`.
    #[serde(default = "super::default_true")]
    pub expose_tools: bool,
    /// Whether to expose resources (tasks, memory, knowledge) via MCP. Default: `true`.
    #[serde(default = "super::default_true")]
    pub expose_resources: bool,
    /// Whether to expose prompts via MCP. Default: `false`.
    #[serde(default)]
    pub expose_prompts: bool,
}

fn default_mcp_server_name() -> String {
    "heartbit".into()
}

/// Audit log retention configuration for the daemon.
///
/// Controls automatic pruning of old audit log entries.
/// When `retain_days` is set and a Postgres store is attached, the daemon
/// spawns a background task that deletes entries older than `retain_days`.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct DaemonAuditConfig {
    /// Number of days to retain audit log entries. Entries older than this
    /// are deleted by the background prune task. `None` disables pruning.
    #[serde(default)]
    pub retain_days: Option<u32>,
    /// Interval in minutes between prune runs. Defaults to 60 (hourly).
    #[serde(default)]
    pub prune_interval_minutes: Option<u64>,
}

/// Idempotency-key sweep settings.
///
/// When `ttl_hours` is `Some`, the daemon runs a background task that nulls
/// out idempotency keys older than the TTL. The row itself is retained so
/// existing primary-key lookups still work; only the dedup contract expires.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct IdempotencyConfig {
    /// Hours to retain idempotency keys before the sweep nulls them out.
    /// Default `None` disables the sweep.
    #[serde(default)]
    pub ttl_hours: Option<u32>,
    /// How often the sweep runs, in minutes. Default 60.
    #[serde(default)]
    pub sweep_interval_minutes: Option<u32>,
}

/// Per-persona X/Twitter mention-poll configuration.
///
/// Each entry in `[[daemon.persona_mentions]]` configures one persona's
/// periodic mention polling loop. When `enabled = true`, the daemon spawns
/// a `MentionPollScheduler` for each entry that fires a
/// `DaemonCommand::MentionPoll` on the configured interval.
#[cfg(feature = "ghost-domain-config")]
#[derive(Debug, Clone, Deserialize)]
pub struct PersonaMentionsConfig {
    /// Persona slug (must match a loaded persona file, e.g. `"heartbit-ghost"`).
    pub persona: String,
    /// Enable mention polling for this persona. Defaults to `true`.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
    /// Seconds between mention polls. Defaults to 300 (5 minutes, X API rate-limit-safe).
    #[serde(default = "default_poll_interval_seconds")]
    pub poll_interval_seconds: u64,
    /// X/Twitter user-id to poll mentions for (e.g. `"100"`).
    pub user_id: String,
    /// Maximum candidates to generate replies for per poll cycle. Defaults to 5.
    #[serde(default = "default_candidates_per_reply")]
    pub candidates_per_reply: usize,
    /// Which mention store backend to use: `"in_memory"` or `"jsonl"`.
    /// Defaults to `"in_memory"`.
    #[serde(default = "default_mention_store")]
    pub mention_store: String,
    /// File path for the JSONL mention store (only used when
    /// `mention_store = "jsonl"`). When absent, a per-persona default path is
    /// derived from the persona slug.
    #[serde(default)]
    pub mention_store_path: Option<String>,

    // ── P1.7 loop-protection guards ─────────────────────────────────────────
    /// Enable the thread-depth guard (skips mentions whose parent tweet was
    /// already replied to by this persona). Defaults to `true`.
    #[serde(default = "super::default_true")]
    pub enable_thread_depth_guard: bool,
    /// Enable the bot-heuristic guard. Defaults to `true`.
    #[serde(default = "super::default_true")]
    pub enable_bot_heuristic_guard: bool,
    /// Extra substrings that flag a handle as suspicious (supplements
    /// built-in defaults when non-empty). Defaults to `[]` (use built-ins).
    #[serde(default)]
    pub suspicious_handle_patterns: Vec<String>,
    /// Minimum follower/following ratio; below this is one bot signal.
    /// Defaults to 0.05.
    #[serde(default = "default_min_follower_following_ratio")]
    pub min_follower_following_ratio: f32,
    /// Minimum account age in days; younger accounts trigger one bot signal.
    /// Defaults to 7.
    #[serde(default = "default_min_account_age_days")]
    pub min_account_age_days: i64,
    /// Number of bot signals required to skip a mention. Defaults to 2.
    #[serde(default = "default_bot_heuristic_threshold")]
    pub bot_heuristic_threshold: usize,
    /// Maximum replies per unique conversation. Defaults to 2.
    #[serde(default = "default_per_conversation_max_replies")]
    pub per_conversation_max_replies: usize,
    /// Daily LLM token budget per persona. `null` (default) disables the cap.
    #[serde(default)]
    pub daily_token_budget: Option<u64>,
    /// Budget tracker backend: `"in_memory"` or `"jsonl"`. Defaults to
    /// `"in_memory"`.
    #[serde(default = "default_p1_7_budget_store")]
    pub budget_store: String,
    /// File path for the JSONL budget store (only used when
    /// `budget_store = "jsonl"`). Required when `budget_store = "jsonl"`.
    #[serde(default)]
    pub budget_path: Option<String>,
}

#[cfg(feature = "ghost-domain-config")]
fn default_poll_interval_seconds() -> u64 {
    300
}

#[cfg(feature = "ghost-domain-config")]
fn default_candidates_per_reply() -> usize {
    2
}

#[cfg(feature = "ghost-domain-config")]
fn default_mention_store() -> String {
    "in_memory".into()
}

#[cfg(feature = "ghost-domain-config")]
fn default_min_follower_following_ratio() -> f32 {
    0.05
}

#[cfg(feature = "ghost-domain-config")]
fn default_min_account_age_days() -> i64 {
    7
}

#[cfg(feature = "ghost-domain-config")]
fn default_bot_heuristic_threshold() -> usize {
    2
}

#[cfg(feature = "ghost-domain-config")]
fn default_per_conversation_max_replies() -> usize {
    2
}

#[cfg(feature = "ghost-domain-config")]
fn default_p1_7_budget_store() -> String {
    "in_memory".into()
}

/// Memory access control configuration for the daemon.
///
/// Controls which users can write to shared institutional memory.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct DaemonMemoryConfig {
    /// Roles that are allowed to write to shared institutional memory.
    ///
    /// When empty (the default), all users can write — backward compatible.
    /// When non-empty, only users with at least one of these roles can write.
    ///
    /// Example: `["admin", "knowledge_manager"]`
    #[serde(default)]
    pub shared_write_roles: Vec<String>,
}

/// HTTP API authentication configuration for the daemon.
#[derive(Debug, Clone, Deserialize)]
pub struct AuthConfig {
    /// Bearer tokens that grant API access. Multiple tokens support key rotation.
    #[serde(default)]
    pub bearer_tokens: Vec<String>,
    /// JWKS endpoint URL for JWT signature verification
    /// (e.g. `"https://idp.example.com/.well-known/jwks.json"`).
    pub jwks_url: Option<String>,
    /// Expected JWT issuer (`iss` claim). Validated when present.
    pub issuer: Option<String>,
    /// Expected JWT audience (`aud` claim). Validated when present.
    pub audience: Option<String>,
    /// JWT claim to extract user ID from. Defaults to `"sub"`.
    pub user_id_claim: Option<String>,
    /// JWT claim to extract tenant ID from. Defaults to `"tid"`.
    pub tenant_id_claim: Option<String>,
    /// JWT claim to extract roles from. Defaults to `"roles"`.
    pub roles_claim: Option<String>,
    /// RFC 8693 Token Exchange configuration for per-user MCP auth delegation.
    /// When configured, the daemon exchanges user JWTs for MCP-scoped delegated tokens.
    pub token_exchange: Option<TokenExchangeConfig>,
}

/// RFC 8693 Token Exchange configuration for per-user MCP auth delegation.
///
/// When configured, each task submitted with a JWT gets a user-scoped delegated
/// token injected into MCP requests. The daemon acts as the agent (actor) and
/// exchanges the user's subject token for a scoped access token.
#[derive(Debug, Clone, Deserialize)]
pub struct TokenExchangeConfig {
    /// Token exchange endpoint URL (e.g. `"https://idp.example.com/oauth/token"`).
    pub exchange_url: String,
    /// OAuth client ID for the daemon/agent.
    pub client_id: String,
    /// OAuth client secret for the daemon/agent.
    pub client_secret: String,
    /// NHI tenant ID — used for `X-Tenant-ID` header in `client_credentials` grant.
    /// When set, `agent_token` is fetched and cached automatically; no static token needed.
    pub tenant_id: Option<String>,
    /// Static fallback agent token (`actor_token` in RFC 8693).
    /// Used only when `tenant_id` is absent (backward-compat).
    #[serde(default)]
    pub agent_token: String,
    /// OAuth scopes to request for the delegated token. Defaults to empty.
    #[serde(default)]
    pub scopes: Vec<String>,
}

/// Heartbit pulse configuration for autonomous periodic awareness.
///
/// When enabled, the daemon periodically reviews its persistent todo list
/// and decides what to work on next — a cognitive pulse loop.
#[derive(Debug, Clone, Deserialize)]
pub struct HeartbitPulseConfig {
    /// Enable the heartbit pulse. Defaults to `false`.
    #[serde(default)]
    pub enabled: bool,
    /// Interval in seconds between heartbit pulse ticks. Defaults to 1800 (30 min).
    #[serde(default = "default_pulse_interval")]
    pub interval_seconds: u64,
    /// Active hours window. When set, the pulse only fires within this window.
    pub active_hours: Option<ActiveHoursConfig>,
    /// Custom prompt override for the heartbit pulse. When absent, the
    /// default built-in prompt is used.
    pub prompt: Option<String>,
    /// Number of consecutive HEARTBIT_OK responses before doubling the
    /// interval (idle backoff). Defaults to 6 (3h at 30min interval).
    #[serde(default = "default_idle_backoff_threshold")]
    pub idle_backoff_threshold: u32,
}

fn default_pulse_interval() -> u64 {
    1800
}

fn default_idle_backoff_threshold() -> u32 {
    6
}

/// Active hours window for the heartbit pulse.
#[derive(Debug, Clone, Deserialize)]
pub struct ActiveHoursConfig {
    /// Start time in "HH:MM" format (24-hour).
    pub start: String,
    /// End time in "HH:MM" format (24-hour).
    pub end: String,
}

impl ActiveHoursConfig {
    /// Parse the start hour and minute. Returns `(hour, minute)`.
    pub fn parse_start(&self) -> Result<(u32, u32), Error> {
        parse_hhmm(&self.start)
    }

    /// Parse the end hour and minute. Returns `(hour, minute)`.
    pub fn parse_end(&self) -> Result<(u32, u32), Error> {
        parse_hhmm(&self.end)
    }
}

fn parse_hhmm(s: &str) -> Result<(u32, u32), Error> {
    let parts: Vec<&str> = s.split(':').collect();
    if parts.len() != 2 {
        return Err(Error::Config(format!(
            "invalid time format '{}': expected HH:MM",
            s
        )));
    }
    let hour: u32 = parts[0]
        .parse()
        .map_err(|_| Error::Config(format!("invalid hour in '{s}'")))?;
    let minute: u32 = parts[1]
        .parse()
        .map_err(|_| Error::Config(format!("invalid minute in '{s}'")))?;
    if hour > 23 || minute > 59 {
        return Err(Error::Config(format!(
            "time '{}' out of range (00:00-23:59)",
            s
        )));
    }
    Ok((hour, minute))
}

/// How the X-post pipeline produces the optional head-tweet image.
#[cfg(feature = "ghost-domain-config")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum ImageSource {
    /// Search Openverse for a CC0/public-domain image matching the post.
    #[default]
    Online,
    /// Generate an image with the AI image tool.
    Ai,
    /// No image — text-only posts.
    None,
}

/// Per-persona proactive-posting configuration.
///
/// When present, the daemon registers a `PersonaPostScheduler` that
/// fires `DaemonCommand::PersonaPost` on the configured cadence
/// (gated by `active_hours`). The handler runs the topic generator,
/// drafts candidate threads via the existing review pipeline, sends
/// them to Telegram for review, posts the chosen draft.
///
/// Configured under `[[daemon.persona_posts]]` blocks.
#[cfg(feature = "ghost-domain-config")]
#[derive(Debug, Clone, Deserialize)]
pub struct PersonaPostsConfig {
    /// Persona registry name (e.g. `"heartbit-ghost:x"`).
    pub persona: String,
    /// Whether this poster is enabled.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
    /// Posting interval, in seconds. Default 14400 (4 hours).
    /// Recommended ≥60. NOTE: this is NOT enforced at config load — the value
    /// is consumed as-is by the (cross-crate) scheduler loop.
    #[serde(default = "default_post_interval_seconds")]
    pub post_interval_seconds: u64,
    /// Randomness applied to each `post_interval_seconds` tick, as a
    /// percentage (`0`–`50`). Default `25` = ±25%, so a base 4h interval
    /// fires somewhere in [3h, 5h]. Prevents the daemon from posting
    /// on a perfectly predictable cadence — that's a textbook bot
    /// signature on X. `0` disables jitter (use only for tests).
    #[serde(default = "default_post_interval_jitter_pct")]
    pub interval_jitter_pct: u32,
    /// Optional `"HH:MM-HH:MM"` window during which posts are allowed.
    /// Outside this window, the scheduler tick is a no-op. When absent,
    /// posting is allowed 24/7.
    #[serde(default)]
    pub active_hours: Option<ActiveHoursConfig>,
    /// Number of candidate threads to draft per tick (1..=10).
    /// Default 3.
    #[serde(default = "default_post_candidates")]
    pub candidates_per_draft: usize,
    /// Backend for the post history store: `"in_memory"` or `"jsonl"`.
    #[serde(default = "default_post_history_store")]
    pub post_history_store: String,
    /// Path to the JSONL store file (only used when
    /// `post_history_store == "jsonl"`). Tilde expansion happens at
    /// store-construction time.
    #[serde(default)]
    pub post_history_path: Option<String>,
    /// How far back to check for topic duplicates. Default 30 days.
    #[serde(default = "default_post_history_lookback_days")]
    pub post_history_lookback_days: i64,
    /// Optional fallback brief used when the persona declares no
    /// topic-context provider, or appended to the provider's output.
    #[serde(default)]
    pub topic_brief: Option<String>,

    // --- Engagement feedback (P2.0) ---
    /// Engagement-collector tick interval, in seconds. Default 21600 = 6h.
    /// Each tick batch-refreshes every eligible Posted tweet's metrics
    /// from the X API and writes a fresh [`EngagementSnapshot`].
    #[serde(default = "default_engagement_refresh_seconds")]
    pub engagement_refresh_seconds: u64,
    /// How many top-engaged posts to inject as writer few-shot exemplars.
    /// Default 5. Set to `0` to disable injection — the writer then runs
    /// without exemplars, matching pre-P2.0 behavior.
    #[serde(default = "default_engagement_top_n")]
    pub engagement_top_n: usize,
    /// Ignore tweets older than this many days when refreshing engagement.
    /// Default 30 — older tweets rarely accrue new engagement.
    #[serde(default = "default_engagement_max_age_days")]
    pub engagement_max_age_days: i64,
    /// Ignore tweets younger than this many hours when refreshing
    /// engagement. Default 24 — gives the algorithm time to fan out.
    #[serde(default = "default_engagement_min_age_hours")]
    pub engagement_min_age_hours: i64,

    /// Override LLM provider for the writer + style-critic stages of the
    /// proactive-post pipeline. When set, these two "engagement voice"
    /// agents use this provider; the researcher and fact-check stages
    /// keep using the global `[provider]`. Use this to point the
    /// engagement voice at a different model (e.g. Grok via OpenRouter)
    /// without affecting verification or research quality.
    ///
    /// Configured under `[daemon.persona_posts.writer_provider]`. When
    /// absent, the writer and critic share the global provider.
    #[serde(default)]
    pub writer_provider: Option<super::agent::AgentProviderConfig>,

    /// How to produce the optional head-tweet image. Default `online`
    /// (Openverse CC0/public-domain search). `ai` uses the image
    /// generator; `none` disables images.
    #[serde(default)]
    pub image_source: ImageSource,
}

#[cfg(feature = "ghost-domain-config")]
fn default_post_interval_seconds() -> u64 {
    14400
}

#[cfg(feature = "ghost-domain-config")]
fn default_post_interval_jitter_pct() -> u32 {
    25
}

#[cfg(feature = "ghost-domain-config")]
fn default_post_candidates() -> usize {
    3
}

#[cfg(feature = "ghost-domain-config")]
fn default_post_history_store() -> String {
    "in_memory".into()
}

#[cfg(feature = "ghost-domain-config")]
fn default_post_history_lookback_days() -> i64 {
    30
}

#[cfg(feature = "ghost-domain-config")]
fn default_engagement_refresh_seconds() -> u64 {
    21600
}

#[cfg(feature = "ghost-domain-config")]
fn default_engagement_top_n() -> usize {
    5
}

#[cfg(feature = "ghost-domain-config")]
fn default_engagement_max_age_days() -> i64 {
    30
}

#[cfg(feature = "ghost-domain-config")]
fn default_engagement_min_age_hours() -> i64 {
    24
}

/// Per-persona quote-tweet configuration.
///
/// When present, the daemon registers a `PersonaQuoteScheduler` that
/// fires `DaemonCommand::PersonaQuote` on the configured cadence. The
/// handler polls each `source_user_ids` entry via X v2
/// `/2/users/{id}/tweets`, filters by `max_age_hours` + not-yet-quoted,
/// drafts an opinionated-but-charitable quote-tweet via the
/// `quote_writer` agent, sends to Telegram for review, posts the
/// chosen draft via `POST /2/tweets` with `quote_tweet_id`.
///
/// Configured under `[[daemon.persona_quotes]]` blocks.
#[cfg(feature = "ghost-domain-config")]
#[derive(Debug, Clone, Deserialize)]
pub struct PersonaQuotesConfig {
    /// Persona registry name (e.g. `"heartbit-ghost:x"`).
    pub persona: String,
    /// Whether this quoter is enabled.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
    /// Polling interval, in seconds. Default 5400 (90 min — medium per
    /// brainstorm).
    /// Recommended ≥60. NOTE: this is NOT enforced at config load — the value
    /// is consumed as-is by the (cross-crate) scheduler loop.
    #[serde(default = "default_quote_poll_interval_seconds")]
    pub poll_interval_seconds: u64,
    /// Randomness applied to each `poll_interval_seconds` tick, as a
    /// percentage (`0`–`50`). Default `25` = ±25%. Same anti-pattern
    /// rationale as `interval_jitter_pct` on `persona_posts`.
    #[serde(default = "default_quote_interval_jitter_pct")]
    pub interval_jitter_pct: u32,
    /// Optional `"HH:MM-HH:MM"` window during which quote-polls run.
    /// Outside this window, the scheduler tick is a no-op.
    #[serde(default)]
    pub active_hours: Option<ActiveHoursConfig>,
    /// X user IDs (numeric strings) of accounts to poll. Curated list.
    /// Required — must contain at least one entry.
    pub source_user_ids: Vec<String>,
    /// Number of candidate quote-tweets to draft per chosen source
    /// tweet (1..=3). Default 3. Bounded tighter than the proactive
    /// post pipeline (1..=10) because quote-tweets are higher-stakes
    /// (you're commenting on someone else's content) and 3 candidates
    /// hits the cost/quality knee.
    #[serde(default = "default_quote_candidates_per_draft")]
    pub candidates_per_draft: usize,
    /// Backend for the "already quoted" dedup store: `"in_memory"` or
    /// `"jsonl"`. Default `"in_memory"`.
    #[serde(default = "default_quote_seen_store")]
    pub seen_store: String,
    /// Path to the JSONL store file (only used when
    /// `seen_store == "jsonl"`). Tilde expansion at construction time.
    #[serde(default)]
    pub seen_store_path: Option<String>,
    /// Maximum age in hours of a source tweet for it to be quote-able.
    /// Default 12 — beyond that the discourse has moved on. Set to 0
    /// to disable the age filter.
    #[serde(default = "default_quote_max_age_hours")]
    pub max_age_hours: i64,
    /// Maximum number of quote-tweets to draft+review per scheduler
    /// tick. Default 1 — pick the best candidate from sources and
    /// stop. Set higher to draft multiple quote-tweets per tick
    /// (one Telegram review per draft).
    #[serde(default = "default_quote_max_candidates_per_tick")]
    pub max_candidates_per_tick: usize,
    /// Optional override LLM provider for the quote_writer + critic.
    /// `None` falls back to the global `[provider]`. Same shape as
    /// `persona_posts.writer_provider`.
    #[serde(default)]
    pub writer_provider: Option<super::agent::AgentProviderConfig>,
}

#[cfg(feature = "ghost-domain-config")]
fn default_quote_poll_interval_seconds() -> u64 {
    5400 // 90 minutes
}

#[cfg(feature = "ghost-domain-config")]
fn default_quote_interval_jitter_pct() -> u32 {
    25
}

#[cfg(feature = "ghost-domain-config")]
fn default_quote_candidates_per_draft() -> usize {
    3
}

#[cfg(feature = "ghost-domain-config")]
fn default_quote_seen_store() -> String {
    "in_memory".into()
}

#[cfg(feature = "ghost-domain-config")]
fn default_quote_max_age_hours() -> i64 {
    12
}

#[cfg(feature = "ghost-domain-config")]
fn default_quote_max_candidates_per_tick() -> usize {
    1
}

/// Personal-blog publishing configuration.
///
/// When present, the daemon registers a `PersonaBlogScheduler` that
/// fires `DaemonCommand::PersonaBlog` on a weekly cadence (with jitter).
/// The handler picks the highest-engagement X post from the prior
/// `seed_lookback_days` (default 7) as the topic seed, drafts a
/// long-form essay via the `blog_writer` agent, routes through
/// Telegram for review, writes the picked draft as Markdown to
/// `posts_dir`, and re-renders the static site into `out_dir`.
///
/// Configured under `[daemon.persona_blog]` (single block, not a
/// list — one blog per daemon).
#[cfg(feature = "ghost-domain-config")]
#[derive(Debug, Clone, Deserialize)]
pub struct PersonaBlogConfig {
    /// Persona registry name (e.g. `"heartbit-ghost:x"`). Must match an
    /// existing persona whose post history + engagement store are
    /// already configured under `[[daemon.persona_posts]]` for the same
    /// slug — the blog reuses those stores for seed selection.
    pub persona: String,
    /// Whether the blog scheduler is enabled.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
    /// Polling interval in seconds. Default 604_800 (7 days = weekly).
    /// Validation: must be ≥3600 (1 hour) — anything tighter is almost
    /// certainly a misconfig and produces thin posts.
    #[serde(default = "default_blog_poll_interval_seconds")]
    pub poll_interval_seconds: u64,
    /// Jitter percentage applied to the cadence. Default 10 — tighter
    /// than X posts because weekly is already coarse and operators
    /// usually want a predictable day-of-week.
    #[serde(default = "default_blog_interval_jitter_pct")]
    pub interval_jitter_pct: u32,
    /// Optional active-hours window for the scheduler.
    #[serde(default)]
    pub active_hours: Option<ActiveHoursConfig>,
    /// Directory holding the generated Markdown post files. Relative to
    /// the daemon's CWD; tilde-expanded.
    /// Default: `"blog-site/posts"`.
    #[serde(default = "default_blog_posts_dir")]
    pub posts_dir: String,
    /// Directory where the rendered static site is written. Relative to
    /// CWD; tilde-expanded. This is what gets deployed to Cloudflare
    /// Pages (commit + push triggers deploy).
    /// Default: `"blog-site/public"`.
    #[serde(default = "default_blog_out_dir")]
    pub out_dir: String,
    /// How many days back to look for the highest-engagement X post
    /// used as the topic seed. Default 7. Set to 0 to disable
    /// X-derived seeding (forces the operator to set `topic_brief`).
    #[serde(default = "default_blog_seed_lookback_days")]
    pub seed_lookback_days: i64,
    /// Number of candidate essay drafts per tick. Default 2 — long-form
    /// drafts are expensive (~3-5k tokens each); 2 is enough for
    /// meaningful comparison without blowing the budget.
    #[serde(default = "default_blog_candidates_per_draft")]
    pub candidates_per_draft: usize,
    /// Public site URL (required) — used to build canonical URLs,
    /// OpenGraph tags, the sitemap, and the RSS feed.
    pub site_url: String,
    /// Site title rendered in `<title>` and the index page header.
    /// Default: `"pascal.heartbit.ai"`.
    #[serde(default = "default_blog_site_title")]
    pub site_title: String,
    /// Optional override LLM provider for the blog_writer + critic.
    /// `None` falls back to the global `[provider]`. Same shape as
    /// `persona_posts.writer_provider`.
    #[serde(default)]
    pub writer_provider: Option<super::agent::AgentProviderConfig>,
    /// Optional shell command run after a successful `BlogOutcome::Posted`.
    /// Runs from the daemon CWD; env is inherited (use this with
    /// `CLOUDFLARE_API_TOKEN` exported in the daemon shell to invoke
    /// `wrangler pages deploy …`). Empty/`None` skips the hook.
    #[serde(default)]
    pub deploy_command: Option<String>,
    /// Optional X self-amplification — when `Some(cfg)` and `cfg.enabled`,
    /// each successful blog publish (after deploy_command succeeds or is
    /// absent) enqueues a `DaemonCommand::BlogAnnounceX` that drafts an
    /// announcement thread through the existing X persona pipeline +
    /// Telegram review.
    #[serde(default)]
    pub x_announce: Option<XAnnounceConfig>,
    /// Optional GitHub Profile README auto-update — when `Some(cfg)` and
    /// `cfg.enabled`, each successful blog publish re-renders the
    /// operator's profile README to feature the 3 most recent essays
    /// and pushes the change to GitHub via the local repo clone.
    #[serde(default)]
    pub github_readme: Option<GithubReadmeConfig>,
}

#[cfg(feature = "ghost-domain-config")]
fn default_blog_poll_interval_seconds() -> u64 {
    604_800 // 7 days
}

#[cfg(feature = "ghost-domain-config")]
fn default_blog_interval_jitter_pct() -> u32 {
    10
}

#[cfg(feature = "ghost-domain-config")]
fn default_blog_posts_dir() -> String {
    "blog-site/posts".into()
}

#[cfg(feature = "ghost-domain-config")]
fn default_blog_out_dir() -> String {
    "blog-site/public".into()
}

#[cfg(feature = "ghost-domain-config")]
fn default_blog_seed_lookback_days() -> i64 {
    7
}

#[cfg(feature = "ghost-domain-config")]
fn default_blog_candidates_per_draft() -> usize {
    2
}

#[cfg(feature = "ghost-domain-config")]
fn default_blog_site_title() -> String {
    "pascal.heartbit.ai".into()
}

/// X self-amplification settings (sub-block of `[daemon.persona_blog]`).
#[cfg(feature = "ghost-domain-config")]
#[derive(Debug, Clone, Deserialize)]
pub struct XAnnounceConfig {
    /// Whether X self-amplification is enabled.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
}

/// GitHub Profile README auto-update settings.
#[cfg(feature = "ghost-domain-config")]
#[derive(Debug, Clone, Deserialize)]
pub struct GithubReadmeConfig {
    /// Whether the GitHub README update is enabled.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
    /// Absolute path to the local clone of the profile repo
    /// (e.g. `/home/pleclech/projects/100-tokens-profile`). Must already
    /// be cloned with `origin` configured for `git push` (HTTPS token or
    /// SSH key).
    pub local_repo_path: String,
    /// Path (absolute or relative to `local_repo_path`) to the operator-
    /// authored bio template inserted at the top of the README. If the
    /// file is missing, a minimal default is used.
    #[serde(default = "default_bio_template_path")]
    pub bio_template_path: String,
    /// Git commit author name.
    pub git_author_name: String,
    /// Git commit author email.
    pub git_author_email: String,
}

#[cfg(feature = "ghost-domain-config")]
fn default_bio_template_path() -> String {
    "bio.md".into()
}

/// WebSocket configuration for bidirectional user↔agent communication.
#[derive(Debug, Clone, Deserialize)]
pub struct WsConfig {
    /// Whether WebSocket endpoint is enabled. Defaults to `true`.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
    /// Timeout in seconds for blocking interactions (approval, input, question).
    /// Defaults to 120 seconds.
    #[serde(default = "default_interaction_timeout")]
    pub interaction_timeout_seconds: u64,
    /// Maximum concurrent WebSocket connections. Defaults to 100.
    #[serde(default = "default_max_ws_connections")]
    pub max_connections: usize,
    /// PostgreSQL URL for durable session persistence. When absent, sessions
    /// are stored in-memory (lost on restart).
    #[serde(default)]
    pub database_url: Option<String>,
}

fn default_interaction_timeout() -> u64 {
    120
}

fn default_max_ws_connections() -> usize {
    100
}

/// Kafka broker connection settings.
#[derive(Debug, Clone, Deserialize)]
pub struct KafkaConfig {
    /// Comma-separated bootstrap broker list (e.g. `localhost:9092`).
    pub brokers: String,
    /// Consumer group ID for the daemon's command consumer.
    #[serde(default = "default_consumer_group")]
    pub consumer_group: String,
    /// Topic the daemon consumes `DaemonCommand` messages from.
    #[serde(default = "default_commands_topic")]
    pub commands_topic: String,
    /// Topic the daemon emits `AgentEvent` records to.
    #[serde(default = "default_events_topic")]
    pub events_topic: String,
    /// Topic for events that failed triage processing.
    #[serde(default = "default_dead_letter_topic")]
    pub dead_letter_topic: String,
}

/// A scheduled task entry for the cron scheduler.
#[derive(Debug, Clone, Deserialize)]
pub struct ScheduleEntry {
    /// Human-readable schedule name (used in logs/metrics).
    pub name: String,
    /// Cron expression (5- or 6-field, depending on parser).
    pub cron: String,
    /// Task body to submit when the schedule fires.
    pub task: String,
    /// Set to `false` to keep the entry declared but inactive.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
}

/// Prometheus metrics configuration for daemon mode.
#[derive(Debug, Clone, Deserialize)]
pub struct MetricsConfig {
    /// Whether Prometheus metrics are enabled. Defaults to `true`.
    #[serde(default = "super::default_true")]
    pub enabled: bool,
}

fn default_daemon_bind() -> String {
    "127.0.0.1:3000".into()
}

fn default_max_concurrent() -> usize {
    4
}

fn default_consumer_group() -> String {
    "heartbit-daemon".into()
}

fn default_commands_topic() -> String {
    "heartbit.commands".into()
}

fn default_events_topic() -> String {
    "heartbit.events".into()
}

fn default_dead_letter_topic() -> String {
    "heartbit.dead-letter".into()
}

#[cfg(all(test, feature = "ghost-domain-config"))]
mod tests {
    use super::*;

    #[test]
    fn persona_posts_config_parses_with_defaults() {
        let toml = r#"
[[persona_posts]]
persona = "heartbit-ghost:x"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_posts: Vec<PersonaPostsConfig>,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        assert_eq!(cfg.persona_posts.len(), 1);
        let p = &cfg.persona_posts[0];
        assert_eq!(p.persona, "heartbit-ghost:x");
        assert!(p.enabled);
        assert_eq!(p.post_interval_seconds, 14400);
        assert_eq!(p.candidates_per_draft, 3);
        assert_eq!(p.post_history_store, "in_memory");
        assert_eq!(p.post_history_lookback_days, 30);
        assert!(p.active_hours.is_none());
        assert!(p.topic_brief.is_none());
        assert!(p.post_history_path.is_none());
        // Engagement defaults (P2.0).
        assert_eq!(p.engagement_refresh_seconds, 21600);
        assert_eq!(p.engagement_top_n, 5);
        assert_eq!(p.engagement_max_age_days, 30);
        assert_eq!(p.engagement_min_age_hours, 24);
        // Writer provider override defaults to None.
        assert!(p.writer_provider.is_none());
    }

    #[test]
    fn persona_posts_config_parses_writer_provider_override() {
        let toml = r#"
[[persona_posts]]
persona = "heartbit-ghost:x"

[persona_posts.writer_provider]
name = "openrouter"
model = "x-ai/grok-4"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_posts: Vec<PersonaPostsConfig>,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        let p = &cfg.persona_posts[0];
        let wp = p.writer_provider.as_ref().expect("writer_provider present");
        assert_eq!(wp.name, "openrouter");
        assert_eq!(wp.model, "x-ai/grok-4");
    }

    #[test]
    fn persona_posts_config_parses_engagement_overrides() {
        let toml = r#"
[[persona_posts]]
persona = "heartbit-ghost:x"
engagement_refresh_seconds = 3600
engagement_top_n = 0
engagement_max_age_days = 7
engagement_min_age_hours = 6
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_posts: Vec<PersonaPostsConfig>,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        let p = &cfg.persona_posts[0];
        assert_eq!(p.engagement_refresh_seconds, 3600);
        assert_eq!(p.engagement_top_n, 0);
        assert_eq!(p.engagement_max_age_days, 7);
        assert_eq!(p.engagement_min_age_hours, 6);
    }

    #[test]
    fn persona_posts_image_source_defaults_to_online() {
        let toml = r#"
[[persona_posts]]
persona = "heartbit-ghost:x"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_posts: Vec<PersonaPostsConfig>,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        assert_eq!(cfg.persona_posts[0].image_source, ImageSource::Online);
    }

    #[test]
    fn persona_posts_image_source_parses_explicit() {
        let toml = r#"
[[persona_posts]]
persona = "heartbit-ghost:x"
image_source = "none"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_posts: Vec<PersonaPostsConfig>,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        assert_eq!(cfg.persona_posts[0].image_source, ImageSource::None);
    }

    #[test]
    fn persona_posts_image_source_parses_ai() {
        let toml = r#"
[[persona_posts]]
persona = "heartbit-ghost:x"
image_source = "ai"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_posts: Vec<PersonaPostsConfig>,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        assert_eq!(cfg.persona_posts[0].image_source, ImageSource::Ai);
    }

    #[test]
    fn persona_quotes_config_parses_with_defaults() {
        let toml = r#"
[[persona_quotes]]
persona = "heartbit-ghost:x"
source_user_ids = ["44196397", "16884623"]
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_quotes: Vec<PersonaQuotesConfig>,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        assert_eq!(cfg.persona_quotes.len(), 1);
        let q = &cfg.persona_quotes[0];
        assert_eq!(q.persona, "heartbit-ghost:x");
        assert!(q.enabled);
        assert_eq!(q.poll_interval_seconds, 5400); // default 90 min
        assert_eq!(q.interval_jitter_pct, 25);
        assert!(q.active_hours.is_none());
        assert_eq!(q.candidates_per_draft, 3);
        assert_eq!(q.seen_store, "in_memory");
        assert!(q.seen_store_path.is_none());
        assert_eq!(q.max_age_hours, 12);
        assert_eq!(q.max_candidates_per_tick, 1);
        assert_eq!(q.source_user_ids, vec!["44196397", "16884623"]);
        assert!(q.writer_provider.is_none());
    }

    #[test]
    fn persona_quotes_config_rejects_missing_required_fields() {
        let toml = r#"
[[persona_quotes]]
persona = "heartbit-ghost:x"
"#;
        // source_user_ids is required (no default). Parse must fail.
        #[derive(Debug, Deserialize)]
        struct Shim {
            #[allow(dead_code)]
            persona_quotes: Vec<PersonaQuotesConfig>,
        }
        let err = toml::from_str::<Shim>(toml).unwrap_err();
        assert!(
            err.to_string().contains("source_user_ids"),
            "expected missing-field error for source_user_ids; got: {err}"
        );
    }

    #[test]
    fn persona_blog_config_parses_with_defaults() {
        let toml = r#"
[persona_blog]
persona = "heartbit-ghost:x"
site_url = "https://pascal.heartbit.ai"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_blog: PersonaBlogConfig,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        let b = &cfg.persona_blog;
        assert_eq!(b.persona, "heartbit-ghost:x");
        assert!(b.enabled);
        assert_eq!(b.poll_interval_seconds, 604_800); // 7 days
        assert_eq!(b.interval_jitter_pct, 10);
        assert_eq!(b.posts_dir, "blog-site/posts");
        assert_eq!(b.out_dir, "blog-site/public");
        assert_eq!(b.seed_lookback_days, 7);
        assert_eq!(b.candidates_per_draft, 2);
        assert_eq!(b.site_url, "https://pascal.heartbit.ai");
        assert_eq!(b.site_title, "pascal.heartbit.ai");
        assert!(b.writer_provider.is_none());
        assert!(b.deploy_command.is_none());
    }

    #[test]
    fn persona_blog_config_parses_deploy_command() {
        let toml = r#"
[persona_blog]
persona = "heartbit-ghost:x"
site_url = "https://pascal.heartbit.ai"
deploy_command = "npx wrangler pages deploy blog-site/public --project-name=pascal-heartbit-ai --commit-dirty=true --branch=main"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_blog: PersonaBlogConfig,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        let cmd = cfg.persona_blog.deploy_command.as_deref().unwrap();
        assert!(cmd.contains("wrangler pages deploy"));
        assert!(cmd.contains("pascal-heartbit-ai"));
    }

    #[test]
    fn persona_blog_config_parses_x_announce_block() {
        let toml = r#"
[persona_blog]
persona = "heartbit-ghost:x"
site_url = "https://pascal.heartbit.ai"

[persona_blog.x_announce]
enabled = true
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_blog: PersonaBlogConfig,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        let x = cfg.persona_blog.x_announce.as_ref().unwrap();
        assert!(x.enabled);
    }

    #[test]
    fn persona_blog_config_parses_github_readme_block() {
        let toml = r#"
[persona_blog]
persona = "heartbit-ghost:x"
site_url = "https://pascal.heartbit.ai"

[persona_blog.github_readme]
enabled = true
local_repo_path = "/home/pleclech/projects/100-tokens-profile"
git_author_name = "Pascal Le Clech"
git_author_email = "pascal@heartbit.ai"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_blog: PersonaBlogConfig,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        let gh = cfg.persona_blog.github_readme.as_ref().unwrap();
        assert!(gh.enabled);
        assert_eq!(
            gh.local_repo_path,
            "/home/pleclech/projects/100-tokens-profile"
        );
        assert_eq!(gh.bio_template_path, "bio.md"); // default
        assert_eq!(gh.git_author_name, "Pascal Le Clech");
    }

    #[test]
    fn persona_blog_config_amp_blocks_default_to_none() {
        let toml = r#"
[persona_blog]
persona = "heartbit-ghost:x"
site_url = "https://pascal.heartbit.ai"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_blog: PersonaBlogConfig,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        assert!(cfg.persona_blog.x_announce.is_none());
        assert!(cfg.persona_blog.github_readme.is_none());
    }

    #[test]
    fn persona_blog_config_rejects_missing_required_fields() {
        let toml = r#"
[persona_blog]
persona = "heartbit-ghost:x"
"#;
        // site_url is required (no default). Parse must fail.
        #[derive(Deserialize, Debug)]
        struct Shim {
            #[allow(dead_code)]
            persona_blog: PersonaBlogConfig,
        }
        let err = toml::from_str::<Shim>(toml).unwrap_err();
        assert!(
            err.to_string().contains("site_url"),
            "expected missing-field error for site_url; got: {err}"
        );
    }

    #[test]
    fn persona_posts_config_parses_full() {
        let toml = r#"
[[persona_posts]]
persona = "heartbit-ghost:x"
enabled = true
post_interval_seconds = 7200
candidates_per_draft = 5
post_history_store = "jsonl"
post_history_path = "~/.heartbit/posts.jsonl"
post_history_lookback_days = 14
topic_brief = "agents, Rust, LLMs"

[persona_posts.active_hours]
start = "09:00"
end = "22:00"
"#;
        #[derive(Deserialize)]
        struct Shim {
            persona_posts: Vec<PersonaPostsConfig>,
        }
        let cfg: Shim = toml::from_str(toml).unwrap();
        let p = &cfg.persona_posts[0];
        assert_eq!(p.persona, "heartbit-ghost:x");
        assert!(p.enabled);
        assert_eq!(p.post_interval_seconds, 7200);
        assert_eq!(p.candidates_per_draft, 5);
        assert_eq!(p.post_history_store, "jsonl");
        assert_eq!(
            p.post_history_path.as_deref(),
            Some("~/.heartbit/posts.jsonl")
        );
        assert_eq!(p.post_history_lookback_days, 14);
        assert_eq!(p.topic_brief.as_deref(), Some("agents, Rust, LLMs"));
        assert!(p.active_hours.is_some());
        let h = p.active_hours.as_ref().unwrap();
        assert_eq!(h.start, "09:00");
        assert_eq!(h.end, "22:00");
    }
}