aidaemon 0.11.4

A personal AI agent that runs as a background daemon, accessible via Telegram, Slack, or Discord, with tool use, MCP integration, and persistent memory
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
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{json, Value};
use tracing::{info, warn};

use crate::config::{AppConfig, HttpAuthProfile, HttpAuthType};
use crate::oauth::SharedHttpProfiles;
use crate::tools::command_risk::{PermissionMode, RiskLevel};
use crate::tools::http_request::HttpRequestTool;
use crate::tools::terminal::ApprovalRequest;
use crate::tools::ApprovalBroker;
use crate::traits::{OAuthStore, Tool, ToolCapabilities};
use crate::types::ApprovalResponse;

const MAX_VERIFY_TIMEOUT_SECS: u64 = 60;
const DEFAULT_VERIFY_TIMEOUT_SECS: u64 = 20;

#[derive(Deserialize, Default)]
struct ManageHttpAuthArgs {
    action: String,
    profile: Option<String>,
    auth_type: Option<String>,
    allowed_domains: Option<Vec<String>>,
    header_name: Option<String>,
    username: Option<String>,
    user_id: Option<String>,
    url: Option<String>,
    method: Option<String>,
    timeout_secs: Option<u64>,
    #[serde(default)]
    _session_id: String,
}

#[derive(Clone)]
struct ManualProfileResolution {
    auth_type: HttpAuthType,
    allowed_domains: Vec<String>,
    live_profile: HttpAuthProfile,
    missing_direct_fields: Vec<&'static str>,
    missing_secret_fields: Vec<&'static str>,
    detected_secret_fields: Vec<&'static str>,
    configured_secret_fields: Vec<&'static str>,
}

impl ManualProfileResolution {
    fn status_label(&self) -> &'static str {
        if !self.missing_direct_fields.is_empty() || !self.missing_secret_fields.is_empty() {
            "incomplete"
        } else if !self.detected_secret_fields.is_empty() {
            "stored-secrets-detected"
        } else {
            "ready"
        }
    }
}

pub struct ManageHttpAuthTool {
    config_path: PathBuf,
    profiles: SharedHttpProfiles,
    approval_tx: ApprovalBroker,
    state_store: Arc<dyn OAuthStore>,
}

impl ManageHttpAuthTool {
    pub fn new(
        config_path: PathBuf,
        profiles: SharedHttpProfiles,
        approval_tx: ApprovalBroker,
        state_store: Arc<dyn OAuthStore>,
    ) -> Self {
        Self {
            config_path,
            profiles,
            approval_tx,
            state_store,
        }
    }

    fn validate_profile_name(raw: &str) -> anyhow::Result<String> {
        let trimmed = raw.trim();
        anyhow::ensure!(!trimmed.is_empty(), "Profile name must not be empty");
        anyhow::ensure!(
            trimmed
                .chars()
                .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-')),
            "Profile name '{}' is invalid. Use only letters, numbers, '_' or '-'.",
            trimmed
        );
        Ok(trimmed.to_ascii_lowercase())
    }

    fn parse_auth_type(raw: &str) -> anyhow::Result<HttpAuthType> {
        match raw.trim().to_ascii_lowercase().as_str() {
            "oauth1a" | "oauth_1a" | "oauth-1a" => Ok(HttpAuthType::Oauth1a),
            "bearer" => Ok(HttpAuthType::Bearer),
            "header" => Ok(HttpAuthType::Header),
            "basic" => Ok(HttpAuthType::Basic),
            other => anyhow::bail!(
                "Unknown auth_type '{}'. Use one of: oauth1a, bearer, header, basic.",
                other
            ),
        }
    }

    fn auth_type_name(auth_type: &HttpAuthType) -> &'static str {
        match auth_type {
            HttpAuthType::Oauth1a => "oauth1a",
            HttpAuthType::Bearer => "bearer",
            HttpAuthType::Header => "header",
            HttpAuthType::Basic => "basic",
        }
    }

    fn required_secret_fields(auth_type: &HttpAuthType) -> &'static [&'static str] {
        match auth_type {
            HttpAuthType::Oauth1a => &[
                "api_key",
                "api_secret",
                "access_token",
                "access_token_secret",
            ],
            HttpAuthType::Bearer => &["token"],
            HttpAuthType::Header => &["header_value"],
            HttpAuthType::Basic => &["password"],
        }
    }

    fn required_direct_fields(auth_type: &HttpAuthType) -> &'static [&'static str] {
        match auth_type {
            HttpAuthType::Oauth1a | HttpAuthType::Bearer => &[],
            HttpAuthType::Header => &["header_name"],
            HttpAuthType::Basic => &["username"],
        }
    }

    fn optional_direct_fields(auth_type: &HttpAuthType) -> &'static [&'static str] {
        match auth_type {
            HttpAuthType::Oauth1a => &["user_id"],
            _ => &[],
        }
    }

    fn is_direct_field_relevant(auth_type: &HttpAuthType, field: &str) -> bool {
        Self::required_direct_fields(auth_type).contains(&field)
            || Self::optional_direct_fields(auth_type).contains(&field)
    }

    fn keychain_field_name(profile: &str, field: &str) -> String {
        format!("http_auth_{}_{}", profile, field)
    }

    fn normalize_domains(domains: &[String]) -> anyhow::Result<Vec<String>> {
        let normalized: Vec<String> = domains
            .iter()
            .map(|domain| domain.trim().trim_start_matches('.').to_ascii_lowercase())
            .filter(|domain| !domain.is_empty())
            .map(|domain| {
                anyhow::ensure!(
                    !domain.contains("://")
                        && !domain.contains('/')
                        && !domain.contains(char::is_whitespace),
                    "Allowed domain '{}' must be a bare host/domain, not a URL or path.",
                    domain
                );
                Ok(domain)
            })
            .collect::<anyhow::Result<_>>()?;
        anyhow::ensure!(
            !normalized.is_empty(),
            "`allowed_domains` must contain at least one hostname."
        );
        Ok(normalized)
    }

    async fn load_config_doc(&self) -> anyhow::Result<toml::Table> {
        let content = tokio::fs::read_to_string(&self.config_path).await?;
        Ok(content.parse()?)
    }

    fn validate_config(content: &str) -> Result<(), String> {
        let _doc: toml::Table = content
            .parse()
            .map_err(|e| format!("Invalid TOML syntax: {}", e))?;
        let expanded = crate::config::expand_env_vars(content).map_err(|e| format!("{}", e))?;
        toml::from_str::<AppConfig>(&expanded)
            .map_err(|e| format!("Invalid config structure: {}", e))?;
        Ok(())
    }

    #[cfg(unix)]
    fn set_owner_only_permissions(path: &Path) {
        use std::os::unix::fs::PermissionsExt;

        if let Ok(metadata) = std::fs::metadata(path) {
            let mut perms = metadata.permissions();
            perms.set_mode(0o600);
            let _ = std::fs::set_permissions(path, perms);
        }
    }

    #[cfg(not(unix))]
    fn set_owner_only_permissions(_path: &Path) {}

    async fn create_backup(&self) -> anyhow::Result<()> {
        let bak = self.config_path.with_extension("toml.bak");
        let bak1 = self.config_path.with_extension("toml.bak.1");
        let bak2 = self.config_path.with_extension("toml.bak.2");

        if bak1.exists() {
            let _ = tokio::fs::rename(&bak1, &bak2).await;
        }
        if bak.exists() {
            let _ = tokio::fs::rename(&bak, &bak1).await;
        }
        tokio::fs::copy(&self.config_path, &bak).await?;
        Self::set_owner_only_permissions(&bak);
        Self::set_owner_only_permissions(&bak1);
        Self::set_owner_only_permissions(&bak2);
        Ok(())
    }

    async fn save_config_doc(&self, doc: &toml::Table) -> anyhow::Result<()> {
        let rendered = toml::to_string_pretty(&toml::Value::Table(doc.clone()))?;
        if let Err(err) = Self::validate_config(&rendered) {
            anyhow::bail!("Refused to save invalid config: {}", err);
        }
        if let Err(err) = self.create_backup().await {
            warn!(error = %err, "Failed to create config backup before manage_http_auth change");
        }
        tokio::fs::write(&self.config_path, &rendered).await?;
        Self::set_owner_only_permissions(&self.config_path);
        Ok(())
    }

    fn http_auth_table(doc: &toml::Table) -> Option<&toml::Table> {
        doc.get("http_auth").and_then(toml::Value::as_table)
    }

    fn http_auth_table_mut(doc: &mut toml::Table) -> anyhow::Result<&mut toml::Table> {
        doc.entry("http_auth")
            .or_insert_with(|| toml::Value::Table(toml::Table::new()))
            .as_table_mut()
            .ok_or_else(|| anyhow::anyhow!("`http_auth` is not a table"))
    }

    fn profile_table<'a>(doc: &'a toml::Table, profile: &str) -> Option<&'a toml::Table> {
        Self::http_auth_table(doc)?
            .get(profile)
            .and_then(toml::Value::as_table)
    }

    fn profile_table_mut<'a>(
        doc: &'a mut toml::Table,
        profile: &str,
    ) -> anyhow::Result<&'a mut toml::Table> {
        Self::http_auth_table_mut(doc)?
            .entry(profile.to_string())
            .or_insert_with(|| toml::Value::Table(toml::Table::new()))
            .as_table_mut()
            .ok_or_else(|| anyhow::anyhow!("`http_auth.{}` is not a table", profile))
    }

    fn redact_profile_error(error: &str, profile: &HttpAuthProfile) -> String {
        let mut redacted = error.to_string();
        for secret in profile.credential_values() {
            if secret.len() >= 4 {
                redacted = redacted.replace(secret, "[REDACTED]");
            }
        }
        redacted
    }

    fn read_string_field(table: &toml::Table, field: &str) -> Option<String> {
        table
            .get(field)
            .and_then(toml::Value::as_str)
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(ToString::to_string)
    }

    fn read_allowed_domains(table: &toml::Table) -> Vec<String> {
        table
            .get("allowed_domains")
            .and_then(toml::Value::as_array)
            .map(|values| {
                values
                    .iter()
                    .filter_map(|value| value.as_str())
                    .map(str::trim)
                    .filter(|value| !value.is_empty())
                    .map(|value| value.to_ascii_lowercase())
                    .collect()
            })
            .unwrap_or_default()
    }

    fn resolve_manual_profile(
        &self,
        profile_name: &str,
        table: &toml::Table,
    ) -> anyhow::Result<ManualProfileResolution> {
        let auth_type_raw = Self::read_string_field(table, "auth_type")
            .ok_or_else(|| anyhow::anyhow!("Profile '{}' is missing auth_type", profile_name))?;
        let auth_type = Self::parse_auth_type(&auth_type_raw)?;
        let allowed_domains = Self::read_allowed_domains(table);

        let mut missing_direct_fields = Vec::new();
        if allowed_domains.is_empty() {
            missing_direct_fields.push("allowed_domains");
        }
        for field in Self::required_direct_fields(&auth_type) {
            if Self::read_string_field(table, field).is_none() {
                missing_direct_fields.push(field);
            }
        }

        let mut live_profile = HttpAuthProfile {
            auth_type: auth_type.clone(),
            allowed_domains: allowed_domains.clone(),
            api_key: None,
            api_secret: None,
            access_token: None,
            access_token_secret: None,
            user_id: Self::read_string_field(table, "user_id"),
            token: None,
            header_name: Self::read_string_field(table, "header_name"),
            header_value: None,
            username: Self::read_string_field(table, "username"),
            password: None,
        };

        let mut missing_secret_fields = Vec::new();
        let mut detected_secret_fields = Vec::new();
        let mut configured_secret_fields = Vec::new();

        for field in Self::required_secret_fields(&auth_type) {
            let key_name = Self::keychain_field_name(profile_name, field);
            let inline_value = Self::read_string_field(table, field);
            if let Some(ref value) = inline_value {
                if value == "keychain" {
                    match crate::config::resolve_from_keychain(&key_name) {
                        Ok(secret) if !secret.is_empty() => {
                            configured_secret_fields.push(*field);
                            Self::set_secret_field(&mut live_profile, field, Some(secret));
                        }
                        _ => missing_secret_fields.push(*field),
                    }
                    continue;
                }

                configured_secret_fields.push(*field);
                Self::set_secret_field(&mut live_profile, field, Some(value.clone()));
                continue;
            }

            match crate::config::resolve_from_keychain(&key_name) {
                Ok(secret) if !secret.is_empty() => {
                    detected_secret_fields.push(*field);
                    Self::set_secret_field(&mut live_profile, field, Some(secret));
                }
                _ => missing_secret_fields.push(*field),
            }
        }

        Ok(ManualProfileResolution {
            auth_type,
            allowed_domains,
            live_profile,
            missing_direct_fields,
            missing_secret_fields,
            detected_secret_fields,
            configured_secret_fields,
        })
    }

    fn set_secret_field(profile: &mut HttpAuthProfile, field: &str, value: Option<String>) {
        match field {
            "api_key" => profile.api_key = value,
            "api_secret" => profile.api_secret = value,
            "access_token" => profile.access_token = value,
            "access_token_secret" => profile.access_token_secret = value,
            "token" => profile.token = value,
            "header_value" => profile.header_value = value,
            "password" => profile.password = value,
            _ => {}
        }
    }

    async fn sync_manual_profile(
        &self,
        doc: &mut toml::Table,
        profile_name: &str,
    ) -> anyhow::Result<ManualProfileResolution> {
        let Some(profile_table) = Self::profile_table(doc, profile_name) else {
            anyhow::bail!("Profile '{}' was not found after update", profile_name);
        };
        let mut resolution = self.resolve_manual_profile(profile_name, profile_table)?;
        if !resolution.detected_secret_fields.is_empty() {
            {
                let profile_table = Self::profile_table_mut(doc, profile_name)?;
                for field in &resolution.detected_secret_fields {
                    profile_table.insert(
                        (*field).to_string(),
                        toml::Value::String("keychain".to_string()),
                    );
                }
            }
            self.save_config_doc(doc).await?;
            let profile_table = Self::profile_table(doc, profile_name).ok_or_else(|| {
                anyhow::anyhow!("Profile '{}' disappeared during sync", profile_name)
            })?;
            resolution = self.resolve_manual_profile(profile_name, profile_table)?;
        }
        self.profiles
            .write()
            .await
            .insert(profile_name.to_string(), resolution.live_profile.clone());
        Ok(resolution)
    }

    async fn request_approval(
        &self,
        session_id: &str,
        description: &str,
        risk_level: RiskLevel,
        warnings: Vec<String>,
    ) -> anyhow::Result<ApprovalResponse> {
        let (response_tx, response_rx) = tokio::sync::oneshot::channel();
        self.approval_tx
            .send(ApprovalRequest {
                command: description.to_string(),
                session_id: session_id.to_string(),
                risk_level,
                warnings,
                permission_mode: PermissionMode::Default,
                response_tx,
                kind: Default::default(),
            })
            .await
            .map_err(|_| anyhow::anyhow!("Approval channel closed"))?;

        match tokio::time::timeout(Duration::from_secs(300), response_rx).await {
            Ok(Ok(response)) => Ok(response),
            Ok(Err(_)) => Ok(ApprovalResponse::Deny),
            Err(_) => Ok(ApprovalResponse::Deny),
        }
    }

    async fn is_oauth_managed_profile(&self, profile_name: &str) -> anyhow::Result<bool> {
        Ok(self
            .state_store
            .get_oauth_connection(profile_name)
            .await?
            .is_some())
    }

    async fn handle_list(&self) -> anyhow::Result<String> {
        let doc = self.load_config_doc().await?;
        let manual_names: Vec<String> = Self::http_auth_table(&doc)
            .map(|table| table.keys().cloned().collect())
            .unwrap_or_default();
        let oauth_connections = self.state_store.list_oauth_connections().await?;

        let mut lines = Vec::new();
        if manual_names.is_empty() {
            lines.push("No manual API auth profiles configured yet.".to_string());
        } else {
            lines.push("Manual API auth profiles:".to_string());
            for name in manual_names {
                let table = Self::profile_table(&doc, &name)
                    .ok_or_else(|| anyhow::anyhow!("Profile '{}' is not a table", name))?;
                match self.resolve_manual_profile(&name, table) {
                    Ok(resolution) => {
                        let domains = if resolution.allowed_domains.is_empty() {
                            "(none)".to_string()
                        } else {
                            resolution.allowed_domains.join(", ")
                        };
                        lines.push(format!(
                            "- {} [{}] domains: {} ({})",
                            name,
                            Self::auth_type_name(&resolution.auth_type),
                            domains,
                            resolution.status_label()
                        ));
                    }
                    Err(err) => {
                        lines.push(format!("- {} [invalid] {}", name, err));
                    }
                }
            }
        }

        if !oauth_connections.is_empty() {
            if !lines.is_empty() {
                lines.push(String::new());
            }
            lines.push("OAuth-managed profiles:".to_string());
            for connection in oauth_connections {
                lines.push(format!(
                    "- {} [{}] manage with OAuth flow tools",
                    connection.service, connection.auth_type
                ));
            }
        }

        if lines.is_empty() {
            lines.push("No API auth profiles are configured yet.".to_string());
        } else {
            lines.push(String::new());
            lines.push(
                "Use `upsert` to create/update a manual profile, `describe` to inspect it, and `verify` to bind stored secrets and refresh it before live API calls."
                    .to_string(),
            );
        }

        Ok(lines.join("\n"))
    }

    async fn handle_describe(&self, profile_name: &str) -> anyhow::Result<String> {
        let doc = self.load_config_doc().await?;
        let Some(profile_table) = Self::profile_table(&doc, profile_name) else {
            if self.is_oauth_managed_profile(profile_name).await? {
                return Ok(format!(
                    "Profile '{}' is managed by OAuth. Use the OAuth management flow for connection state; this tool only manages manual HTTP auth profiles.",
                    profile_name
                ));
            }
            return Ok(format!(
                "Manual auth profile '{}' was not found. Use action='upsert' to create it.",
                profile_name
            ));
        };
        let resolution = self.resolve_manual_profile(profile_name, profile_table)?;

        let mut lines = vec![
            format!("Manual API auth profile `{}`", profile_name),
            format!(
                "- auth_type: `{}`",
                Self::auth_type_name(&resolution.auth_type)
            ),
            format!(
                "- allowed_domains: {}",
                if resolution.allowed_domains.is_empty() {
                    "(none)".to_string()
                } else {
                    resolution.allowed_domains.join(", ")
                }
            ),
            format!("- status: {}", resolution.status_label()),
        ];

        if let Some(header_name) = resolution.live_profile.header_name.as_deref() {
            lines.push(format!("- header_name: `{}`", header_name));
        }
        if let Some(username) = resolution.live_profile.username.as_deref() {
            lines.push(format!("- username: `{}`", username));
        }
        if let Some(user_id) = resolution.live_profile.user_id.as_deref() {
            lines.push(format!("- user_id: `{}`", user_id));
        }

        if !resolution.configured_secret_fields.is_empty() {
            lines.push(format!(
                "- configured secret fields: {}",
                resolution.configured_secret_fields.join(", ")
            ));
        }
        if !resolution.detected_secret_fields.is_empty() {
            lines.push(format!(
                "- stored but not yet bound in config: {}",
                resolution.detected_secret_fields.join(", ")
            ));
        }
        if !resolution.missing_direct_fields.is_empty() {
            lines.push(format!(
                "- missing direct fields: {}",
                resolution.missing_direct_fields.join(", ")
            ));
        }
        if !resolution.missing_secret_fields.is_empty() {
            lines.push(format!(
                "- missing secure credentials: {}",
                resolution.missing_secret_fields.join(", ")
            ));
        }

        if !resolution.missing_secret_fields.is_empty() {
            lines.push(String::new());
            lines.push("Store missing credentials securely with:".to_string());
            for field in &resolution.missing_secret_fields {
                lines.push(format!(
                    "- `aidaemon keychain set {}`",
                    Self::keychain_field_name(profile_name, field)
                ));
            }
        }

        lines.push(String::new());
        lines.push(
            "After storing or rotating credentials, run `verify` for this profile before making live API calls."
                .to_string(),
        );

        Ok(lines.join("\n"))
    }

    async fn handle_upsert(
        &self,
        profile_name: &str,
        args: &ManageHttpAuthArgs,
    ) -> anyhow::Result<String> {
        if self.is_oauth_managed_profile(profile_name).await? {
            return Ok(format!(
                "Profile '{}' is already managed by OAuth. Use the OAuth flow for that service name, or choose a different manual profile name.",
                profile_name
            ));
        }

        let session_id = if args._session_id.is_empty() {
            "unknown"
        } else {
            args._session_id.as_str()
        };
        let mut doc = self.load_config_doc().await?;
        let existing_auth_type = Self::profile_table(&doc, profile_name)
            .and_then(|table| Self::read_string_field(table, "auth_type"));
        let auth_type = if let Some(ref raw) = args.auth_type {
            Self::parse_auth_type(raw)?
        } else if let Some(raw) = existing_auth_type {
            Self::parse_auth_type(&raw)?
        } else {
            anyhow::bail!("action='upsert' requires auth_type when creating a new profile.");
        };
        let existing_allowed_domains = Self::profile_table(&doc, profile_name)
            .map(Self::read_allowed_domains)
            .unwrap_or_default();
        let allowed_domains = if let Some(ref domains) = args.allowed_domains {
            Self::normalize_domains(domains)?
        } else if !existing_allowed_domains.is_empty() {
            existing_allowed_domains
        } else {
            anyhow::bail!("action='upsert' requires allowed_domains when creating a new profile.");
        };

        let approval_description = format!(
            "Update manual API auth profile '{}' (type={}, domains={})",
            profile_name,
            Self::auth_type_name(&auth_type),
            allowed_domains.join(", ")
        );
        match self
            .request_approval(
                session_id,
                &approval_description,
                RiskLevel::High,
                vec![
                    "Modifies API auth configuration".to_string(),
                    "Can change which domains receive credentials".to_string(),
                ],
            )
            .await?
        {
            ApprovalResponse::AllowOnce
            | ApprovalResponse::AllowSession
            | ApprovalResponse::AllowAlways => {}
            ApprovalResponse::Deny => {
                return Ok("Manual API auth profile update denied by user.".to_string());
            }
        }

        {
            let profile_table = Self::profile_table_mut(&mut doc, profile_name)?;
            profile_table.insert(
                "auth_type".to_string(),
                toml::Value::String(Self::auth_type_name(&auth_type).to_string()),
            );
            profile_table.insert(
                "allowed_domains".to_string(),
                toml::Value::Array(
                    allowed_domains
                        .iter()
                        .cloned()
                        .map(toml::Value::String)
                        .collect(),
                ),
            );

            for field in ["header_name", "username", "user_id"] {
                if !Self::is_direct_field_relevant(&auth_type, field) {
                    profile_table.remove(field);
                    continue;
                }
                let incoming = match field {
                    "header_name" => args.header_name.as_deref(),
                    "username" => args.username.as_deref(),
                    "user_id" => args.user_id.as_deref(),
                    _ => None,
                };
                if let Some(value) = incoming {
                    let trimmed = value.trim();
                    if trimmed.is_empty() {
                        profile_table.remove(field);
                    } else {
                        profile_table
                            .insert(field.to_string(), toml::Value::String(trimmed.to_string()));
                    }
                }
            }

            for field in [
                "api_key",
                "api_secret",
                "access_token",
                "access_token_secret",
                "token",
                "header_value",
                "password",
            ] {
                if !Self::required_secret_fields(&auth_type).contains(&field) {
                    profile_table.remove(field);
                }
            }
        }

        self.save_config_doc(&doc).await?;
        let resolution = self.sync_manual_profile(&mut doc, profile_name).await?;
        info!(
            profile = %profile_name,
            auth_type = Self::auth_type_name(&resolution.auth_type),
            "Saved manual API auth profile"
        );

        let mut lines = vec![
            format!("Saved manual API auth profile `{}`.", profile_name),
            format!(
                "- auth_type: `{}`",
                Self::auth_type_name(&resolution.auth_type)
            ),
            format!(
                "- allowed_domains: {}",
                resolution.allowed_domains.join(", ")
            ),
        ];

        if !resolution.detected_secret_fields.is_empty() {
            lines.push(format!(
                "- bound stored credentials from keychain/env: {}",
                resolution.detected_secret_fields.join(", ")
            ));
        }
        if !resolution.missing_direct_fields.is_empty() {
            lines.push(format!(
                "- missing direct fields: {}",
                resolution.missing_direct_fields.join(", ")
            ));
        }
        if !resolution.missing_secret_fields.is_empty() {
            lines.push(format!(
                "- missing secure credentials: {}",
                resolution.missing_secret_fields.join(", ")
            ));
            lines.push(String::new());
            lines.push("Store the missing credentials with:".to_string());
            for field in &resolution.missing_secret_fields {
                lines.push(format!(
                    "- `aidaemon keychain set {}`",
                    Self::keychain_field_name(profile_name, field)
                ));
            }
            lines.push(String::new());
            lines.push(
                "After storing them, run `verify` for this profile to bind the secrets and refresh the live auth profile."
                    .to_string(),
            );
        } else {
            lines.push("- runtime profile refreshed".to_string());
        }

        Ok(lines.join("\n"))
    }

    async fn handle_remove(&self, profile_name: &str, session_id: &str) -> anyhow::Result<String> {
        let mut doc = self.load_config_doc().await?;
        let Some(http_auth_table) = Self::http_auth_table_mut(&mut doc).ok() else {
            return Ok(format!(
                "Manual auth profile '{}' was not found.",
                profile_name
            ));
        };

        if !http_auth_table.contains_key(profile_name) {
            if self.is_oauth_managed_profile(profile_name).await? {
                return Ok(format!(
                    "Profile '{}' is OAuth-managed. Use the OAuth flow to disconnect it; this tool only removes manual profiles.",
                    profile_name
                ));
            }
            return Ok(format!(
                "Manual auth profile '{}' was not found.",
                profile_name
            ));
        }

        let approval_description = format!(
            "Remove manual API auth profile '{}' and delete its stored credentials",
            profile_name
        );
        match self
            .request_approval(
                session_id,
                &approval_description,
                RiskLevel::High,
                vec![
                    "Deletes API auth configuration".to_string(),
                    "Deletes stored credentials for this profile".to_string(),
                ],
            )
            .await?
        {
            ApprovalResponse::AllowOnce
            | ApprovalResponse::AllowSession
            | ApprovalResponse::AllowAlways => {}
            ApprovalResponse::Deny => {
                return Ok("Manual API auth profile removal denied by user.".to_string());
            }
        }

        http_auth_table.remove(profile_name);
        if http_auth_table.is_empty() {
            doc.remove("http_auth");
        }
        self.save_config_doc(&doc).await?;

        for field in [
            "api_key",
            "api_secret",
            "access_token",
            "access_token_secret",
            "token",
            "header_value",
            "password",
        ] {
            let key = Self::keychain_field_name(profile_name, field);
            if let Err(err) = crate::config::delete_from_keychain(&key) {
                warn!(key = %key, error = %err, "Failed to delete stored credential for removed HTTP auth profile");
            }
        }

        self.profiles.write().await.remove(profile_name);
        Ok(format!(
            "Removed manual API auth profile '{}' and purged its stored credentials.",
            profile_name
        ))
    }

    async fn handle_verify(
        &self,
        profile_name: &str,
        url: Option<&str>,
        method: Option<&str>,
        timeout_secs: Option<u64>,
        session_id: &str,
    ) -> anyhow::Result<String> {
        let mut doc = self.load_config_doc().await?;
        let Some(_) = Self::profile_table(&doc, profile_name) else {
            if self.is_oauth_managed_profile(profile_name).await? {
                return Ok(format!(
                    "Profile '{}' is OAuth-managed. Use the OAuth connection tools to inspect it; this tool verifies manual profiles only.",
                    profile_name
                ));
            }
            return Ok(format!(
                "Manual auth profile '{}' was not found.",
                profile_name
            ));
        };

        let resolution = self.sync_manual_profile(&mut doc, profile_name).await?;
        if !resolution.missing_direct_fields.is_empty()
            || !resolution.missing_secret_fields.is_empty()
        {
            let mut lines = vec![format!(
                "Profile '{}' is not ready for live API calls yet.",
                profile_name
            )];
            if !resolution.missing_direct_fields.is_empty() {
                lines.push(format!(
                    "- missing direct fields: {}",
                    resolution.missing_direct_fields.join(", ")
                ));
            }
            if !resolution.missing_secret_fields.is_empty() {
                lines.push(format!(
                    "- missing secure credentials: {}",
                    resolution.missing_secret_fields.join(", ")
                ));
                lines.push("Store them with:".to_string());
                for field in &resolution.missing_secret_fields {
                    lines.push(format!(
                        "- `aidaemon keychain set {}`",
                        Self::keychain_field_name(profile_name, field)
                    ));
                }
            }
            return Ok(lines.join("\n"));
        }

        let Some(url) = url.map(str::trim).filter(|value| !value.is_empty()) else {
            return Ok(format!(
                "Profile '{}' is ready. Runtime auth state refreshed for `{}` across domains: {}.",
                profile_name,
                Self::auth_type_name(&resolution.auth_type),
                resolution.allowed_domains.join(", ")
            ));
        };

        let verify_method = method.unwrap_or("GET").trim().to_ascii_uppercase();
        if !matches!(verify_method.as_str(), "GET" | "HEAD") {
            return Ok("verify currently supports only GET or HEAD probes. Use http_request for other methods.".to_string());
        }

        let parsed_url = reqwest::Url::parse(url)
            .map_err(|err| anyhow::anyhow!("Invalid verify URL: {}", err))?;
        if parsed_url.scheme() != "https" {
            return Ok("Verification probe blocked: only HTTPS URLs are allowed.".to_string());
        }
        let request_host = parsed_url.host_str().unwrap_or("");
        let domain_ok = resolution
            .allowed_domains
            .iter()
            .any(|domain| HttpRequestTool::domain_matches(request_host, domain));
        if !domain_ok {
            return Ok(format!(
                "Verification probe blocked: '{}' is not in the allowed domains for profile '{}'.",
                request_host, profile_name
            ));
        }

        let approval_description = format!(
            "Verify manual API auth profile '{}' with {} {}",
            profile_name, verify_method, url
        );
        match self
            .request_approval(
                session_id,
                &approval_description,
                RiskLevel::Medium,
                vec![
                    "Authenticated HTTP probe to external API".to_string(),
                    "Read-only verification request".to_string(),
                ],
            )
            .await?
        {
            ApprovalResponse::AllowOnce
            | ApprovalResponse::AllowSession
            | ApprovalResponse::AllowAlways => {}
            ApprovalResponse::Deny => {
                return Ok("Verification probe denied by user.".to_string());
            }
        }

        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(
                timeout_secs
                    .unwrap_or(DEFAULT_VERIFY_TIMEOUT_SECS)
                    .min(MAX_VERIFY_TIMEOUT_SECS),
            ))
            .build()
            .map_err(|err| anyhow::anyhow!("Failed to build HTTP client: {}", err))?;

        let builder = match verify_method.as_str() {
            "HEAD" => client.head(url),
            _ => client.get(url),
        };
        let builder = HttpRequestTool::apply_auth(
            builder,
            &resolution.live_profile,
            &verify_method,
            url,
            None,
            None,
        )
        .map_err(|err| {
            anyhow::anyhow!(
                "Failed to apply auth for profile '{}': {}",
                profile_name,
                Self::redact_profile_error(&err.to_string(), &resolution.live_profile)
            )
        })?;

        let response = builder.send().await.map_err(|err| {
            anyhow::anyhow!(
                "Verification request failed: {}",
                Self::redact_profile_error(&err.to_string(), &resolution.live_profile)
            )
        })?;
        let status = response.status();
        let content_type = response
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|value| value.to_str().ok())
            .unwrap_or("unknown")
            .to_string();

        if verify_method == "HEAD" {
            return Ok(format!(
                "Verification probe completed for '{}': {} {} (content-type: {}).",
                profile_name, verify_method, status, content_type
            ));
        }

        let body = response.text().await.unwrap_or_default();
        let snippet = if body.is_empty() {
            "(empty body)".to_string()
        } else {
            let boundary = crate::utils::floor_char_boundary(&body, 400);
            let truncated = body.len() > boundary;
            let mut snippet = body[..boundary].to_string();
            if truncated {
                snippet.push_str("...");
            }
            snippet
        };
        Ok(format!(
            "Verification probe completed for '{}': {} {} (content-type: {}).\nBody preview:\n{}",
            profile_name, verify_method, status, content_type, snippet
        ))
    }
}

fn manage_http_auth_schema() -> Value {
    json!({
        "name": "manage_http_auth",
        "description": "HTTP auth CRUD. No secrets in chat—keychain. verify after upsert/rotate refreshes runtime auth.",
        "parameters": {
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["list", "describe", "upsert", "remove", "verify"]
                },
                "profile": { "type": "string" },
                "auth_type": {
                    "type": "string",
                    "enum": ["oauth1a", "bearer", "header", "basic"]
                },
                "allowed_domains": {
                    "description": "Hosts allowed to receive this profile's credentials",
                    "type": "array",
                    "items": { "type": "string" }
                },
                "header_name": { "type": "string", "description": "header auth" },
                "username": { "type": "string", "description": "basic" },
                "user_id": { "type": "string" },
                "url": { "type": "string" },
                "method": { "type": "string", "enum": ["GET", "HEAD"] },
                "timeout_secs": { "type": "integer" }
            },
            "required": ["action"],
            "additionalProperties": false
        }
    })
}

#[async_trait]
impl Tool for ManageHttpAuthTool {
    fn name(&self) -> &str {
        "manage_http_auth"
    }

    fn description(&self) -> &str {
        "Create, inspect, verify, and remove generic API auth profiles for HTTP APIs"
    }

    fn schema(&self) -> Value {
        manage_http_auth_schema()
    }

    fn capabilities(&self) -> ToolCapabilities {
        ToolCapabilities {
            read_only: false,
            external_side_effect: true,
            needs_approval: true,
            idempotent: false,
            high_impact_write: true,
        }
    }

    async fn call(&self, arguments: &str) -> anyhow::Result<String> {
        let args: ManageHttpAuthArgs = serde_json::from_str(arguments)?;
        match args.action.as_str() {
            "list" => self.handle_list().await,
            "describe" => {
                let profile = Self::validate_profile_name(
                    args.profile
                        .as_deref()
                        .ok_or_else(|| anyhow::anyhow!("action='describe' requires 'profile'"))?,
                )?;
                self.handle_describe(&profile).await
            }
            "upsert" => {
                let profile = Self::validate_profile_name(
                    args.profile
                        .as_deref()
                        .ok_or_else(|| anyhow::anyhow!("action='upsert' requires 'profile'"))?,
                )?;
                self.handle_upsert(&profile, &args).await
            }
            "remove" => {
                let profile = Self::validate_profile_name(
                    args.profile
                        .as_deref()
                        .ok_or_else(|| anyhow::anyhow!("action='remove' requires 'profile'"))?,
                )?;
                let session_id = if args._session_id.is_empty() {
                    "unknown"
                } else {
                    args._session_id.as_str()
                };
                self.handle_remove(&profile, session_id).await
            }
            "verify" => {
                let profile = Self::validate_profile_name(
                    args.profile
                        .as_deref()
                        .ok_or_else(|| anyhow::anyhow!("action='verify' requires 'profile'"))?,
                )?;
                let session_id = if args._session_id.is_empty() {
                    "unknown"
                } else {
                    args._session_id.as_str()
                };
                self.handle_verify(
                    &profile,
                    args.url.as_deref(),
                    args.method.as_deref(),
                    args.timeout_secs,
                    session_id,
                )
                .await
            }
            other => Ok(format!(
                "Unknown action '{}'. Use: list, describe, upsert, remove, verify.",
                other
            )),
        }
    }
}

#[cfg(test)]
// These tests serialize global env-var mutation via a shared lock; the guard
// must intentionally span `.await` points to keep parallel tests isolated, so
// `clippy::await_holding_lock` is a false positive for this pattern.
#[allow(clippy::await_holding_lock)]
mod tests {
    use super::*;
    use once_cell::sync::Lazy;
    use tempfile::{NamedTempFile, TempDir};

    use crate::memory::embeddings::EmbeddingService;
    use crate::state::SqliteStateStore;

    static ENV_LOCK: Lazy<std::sync::Mutex<()>> = Lazy::new(|| std::sync::Mutex::new(()));

    fn restore_env_var(name: &str, old_value: Option<String>) {
        if let Some(value) = old_value {
            std::env::set_var(name, value);
        } else {
            std::env::remove_var(name);
        }
    }

    async fn test_tool(
        config_path: PathBuf,
        profiles: SharedHttpProfiles,
    ) -> anyhow::Result<ManageHttpAuthTool> {
        let db_file = NamedTempFile::new()?;
        let db_path = db_file.path().display().to_string();
        let embedding_service = Arc::new(EmbeddingService::new()?);
        let state = Arc::new(SqliteStateStore::new(&db_path, 32, None, embedding_service).await?);
        let (approval_tx, mut approval_rx) = tokio::sync::mpsc::channel::<ApprovalRequest>(4);
        let approval_tx = ApprovalBroker::new(approval_tx);
        tokio::spawn(async move {
            while let Some(request) = approval_rx.recv().await {
                let _ = request.response_tx.send(ApprovalResponse::AllowOnce);
            }
        });
        Ok(ManageHttpAuthTool::new(
            config_path,
            profiles,
            approval_tx,
            state as Arc<dyn OAuthStore>,
        ))
    }

    fn write_minimal_config(path: &Path, extra: &str) {
        std::fs::write(
            path,
            format!(
                "[provider]\napi_key = \"test-key\"\n{}\n",
                if extra.is_empty() {
                    String::new()
                } else {
                    format!("\n{}", extra)
                }
            ),
        )
        .unwrap();
    }

    #[tokio::test]
    async fn upsert_creates_profile_and_reports_missing_secret_commands() {
        let config_file = NamedTempFile::new().unwrap();
        write_minimal_config(config_file.path(), "");
        let profiles = Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new()));
        let tool = test_tool(config_file.path().to_path_buf(), profiles.clone())
            .await
            .unwrap();

        let result = tool
            .call(
                r#"{
                    "action": "upsert",
                    "profile": "stripe",
                    "auth_type": "bearer",
                    "allowed_domains": ["api.stripe.com"],
                    "_session_id": "test"
                }"#,
            )
            .await
            .unwrap();

        assert!(result.contains("Saved manual API auth profile `stripe`."));
        assert!(result.contains("aidaemon keychain set http_auth_stripe_token"));
        let saved = std::fs::read_to_string(config_file.path()).unwrap();
        assert!(saved.contains("[http_auth.stripe]"));
        assert!(saved.contains("auth_type = \"bearer\""));
        assert!(saved.contains("allowed_domains = [\"api.stripe.com\"]"));
        assert!(!saved.contains("token = \"keychain\""));

        let runtime = profiles.read().await;
        assert!(runtime.contains_key("stripe"));
        assert_eq!(runtime["stripe"].allowed_domains, vec!["api.stripe.com"]);
    }

    #[tokio::test]
    async fn verify_binds_detected_env_secret_and_refreshes_runtime_profile() {
        let _guard = ENV_LOCK.lock().unwrap();
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");
        let env_path = temp_dir.path().join(".env");
        write_minimal_config(
            &config_path,
            r#"
[http_auth.demo]
auth_type = "bearer"
allowed_domains = ["api.example.com"]
"#,
        );
        std::fs::write(&env_path, "HTTP_AUTH_DEMO_TOKEN=secret-demo-token\n").unwrap();

        let old_no_keychain = std::env::var("AIDAEMON_NO_KEYCHAIN").ok();
        let old_runtime_env = std::env::var(crate::RUNTIME_ENV_FILE_ENV_KEY).ok();
        std::env::set_var("AIDAEMON_NO_KEYCHAIN", "1");
        std::env::set_var(
            crate::RUNTIME_ENV_FILE_ENV_KEY,
            env_path.to_string_lossy().to_string(),
        );

        let profiles = Arc::new(tokio::sync::RwLock::new(std::collections::HashMap::new()));
        let tool = test_tool(config_path.clone(), profiles.clone())
            .await
            .unwrap();
        let result = tool
            .call(r#"{"action":"verify","profile":"demo"}"#)
            .await
            .unwrap();

        restore_env_var("AIDAEMON_NO_KEYCHAIN", old_no_keychain);
        restore_env_var(crate::RUNTIME_ENV_FILE_ENV_KEY, old_runtime_env);

        assert!(result.contains("Profile 'demo' is ready."));
        let saved = std::fs::read_to_string(&config_path).unwrap();
        assert!(saved.contains("token = \"keychain\""));

        let runtime = profiles.read().await;
        let profile = runtime.get("demo").expect("runtime profile inserted");
        assert_eq!(profile.token.as_deref(), Some("secret-demo-token"));
    }

    #[test]
    fn schema_fits_payload_budget() {
        // Pillar C of 2026-06-06-cross-turn-prefix-stability-design.md:
        // admin-tool schemas ride in EVERY provider call; this ceiling is the
        // per-tool payload budget. If you trip this assert by adding features,
        // compress the description text — do not raise the ceiling without
        // updating the Pillar C implementation plan.
        let bytes = serde_json::to_string(&manage_http_auth_schema())
            .unwrap()
            .len();
        assert!(
            bytes <= 800,
            "manage_http_auth schema is {bytes} bytes, budget is 800"
        );
    }
}