quadra-a-core 0.1.0-beta.1

Core protocol primitives for the quadra-a agent network
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
use crate::e2e::LocalE2EConfig;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    pub identity: Option<IdentityConfig>,
    #[serde(default, rename = "deviceIdentity")]
    pub device_identity: Option<DeviceIdentityConfig>,
    #[serde(rename = "agentCard")]
    pub agent_card: Option<AgentCardConfig>,
    #[serde(default)]
    pub published: Option<bool>,
    #[serde(default, rename = "relayInviteToken")]
    pub relay_invite_token: Option<String>,
    #[serde(default)]
    pub aliases: HashMap<String, String>,
    #[serde(default, rename = "trustConfig")]
    pub trust_config: Option<TrustConfig>,
    #[serde(default, rename = "reachabilityPolicy")]
    pub reachability_policy: Option<ReachabilityPolicy>,
    #[serde(default)]
    pub e2e: Option<LocalE2EConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ReachabilityMode {
    Adaptive,
    Fixed,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReachabilityPolicy {
    pub mode: ReachabilityMode,
    #[serde(default, rename = "bootstrapProviders")]
    pub bootstrap_providers: Vec<String>,
    #[serde(
        default = "default_target_provider_count",
        rename = "targetProviderCount"
    )]
    pub target_provider_count: u32,
    #[serde(
        default = "default_auto_discover_providers",
        rename = "autoDiscoverProviders"
    )]
    pub auto_discover_providers: bool,
    #[serde(default, rename = "operatorLock")]
    pub operator_lock: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdentityConfig {
    pub did: String,
    #[serde(rename = "publicKey")]
    pub public_key: String,
    #[serde(rename = "privateKey")]
    pub private_key: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DeviceIdentityConfig {
    pub seed: String,
    #[serde(rename = "deviceId")]
    pub device_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentCardConfig {
    pub name: String,
    pub description: String,
    pub capabilities: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrustConfig {
    #[serde(default)]
    pub endorsements: HashMap<String, EndorsementV2>,
    #[serde(default)]
    pub trust_scores: HashMap<String, CachedTrustScore>,
    #[serde(default)]
    pub blocked_agents: HashSet<String>,
    #[serde(default)]
    pub blocked_reasons: HashMap<String, String>,
    #[serde(default)]
    pub allowed_agents: HashMap<String, AllowedAgent>,
    #[serde(default)]
    pub collusion_penalties: HashMap<String, CachedCollusionPenalty>,
    #[serde(default)]
    pub seed_peers: Vec<String>,
    #[serde(default = "default_max_recursion_depth")]
    pub max_recursion_depth: u8,
    #[serde(default)]
    pub decay_half_life: HashMap<String, u32>,
    #[serde(default = "default_collusion_external_ratio_threshold")]
    pub collusion_external_ratio_threshold: f64,
    #[serde(default = "default_collusion_min_cluster_size")]
    pub collusion_min_cluster_size: usize,
    #[serde(default = "default_trust_cache_ttl_seconds")]
    pub trust_cache_ttl_seconds: u64,
    #[serde(default = "default_scc_cache_ttl_seconds")]
    pub scc_cache_ttl_seconds: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EndorsementV2 {
    pub endorser: String,
    pub endorsee: String,
    pub domain: Option<String>,
    #[serde(rename = "type")]
    pub endorsement_type: String,
    pub strength: f64,
    pub comment: Option<String>,
    pub timestamp: u64,
    pub expires: Option<u64>,
    pub version: String,
    pub signature: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedTrustScore {
    pub score: f64,
    pub computed_at: u64,
    pub ttl: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CachedCollusionPenalty {
    pub penalty: f64,
    pub computed_at: u64,
    pub ttl: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AllowedAgent {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
    #[serde(default, rename = "addedAt", skip_serializing_if = "Option::is_none")]
    pub added_at: Option<u64>,
}

fn default_max_recursion_depth() -> u8 {
    3
}

fn default_target_provider_count() -> u32 {
    3
}

fn default_auto_discover_providers() -> bool {
    true
}

fn default_bootstrap_providers() -> Vec<String> {
    vec!["ws://relay-sg-1.quadra-a.com:8080".to_string()]
}

fn default_collusion_external_ratio_threshold() -> f64 {
    0.2
}

fn default_collusion_min_cluster_size() -> usize {
    4
}

fn default_trust_cache_ttl_seconds() -> u64 {
    300
}

fn default_scc_cache_ttl_seconds() -> u64 {
    300
}

fn normalize_optional_string(value: Option<&str>) -> Option<String> {
    value
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(ToOwned::to_owned)
}

fn normalize_provider_urls(values: Vec<String>) -> Vec<String> {
    let mut normalized = Vec::new();
    for value in values.into_iter().map(|value| value.trim().to_string()) {
        if value.is_empty() || normalized.iter().any(|existing| existing == &value) {
            continue;
        }
        normalized.push(value);
    }
    normalized
}

fn env_bootstrap_providers() -> Vec<String> {
    let env_value = std::env::var("QUADRA_A_RELAY_URLS")
        .ok()
        .or_else(|| std::env::var("HW1_RELAY_URLS").ok());

    let providers = env_value
        .map(|value| {
            value
                .split(',')
                .map(|entry| entry.trim().to_string())
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();

    let normalized = normalize_provider_urls(providers);
    if normalized.is_empty() {
        default_bootstrap_providers()
    } else {
        normalized
    }
}

fn env_disable_auto_relay_supplement() -> bool {
    normalize_optional_string(
        std::env::var("QUADRA_A_DISABLE_AUTO_RELAY_SUPPLEMENT")
            .ok()
            .as_deref(),
    )
    .map(|value| matches!(value.to_lowercase().as_str(), "1" | "true" | "yes" | "on"))
    .unwrap_or(false)
}

fn current_unix_seconds() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

impl Default for ReachabilityPolicy {
    fn default() -> Self {
        let auto_discover_providers = !env_disable_auto_relay_supplement();
        Self {
            mode: if auto_discover_providers {
                ReachabilityMode::Adaptive
            } else {
                ReachabilityMode::Fixed
            },
            bootstrap_providers: env_bootstrap_providers(),
            target_provider_count: default_target_provider_count(),
            auto_discover_providers,
            operator_lock: false,
        }
    }
}

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

impl TrustConfig {
    pub fn new() -> Self {
        let mut decay_half_life = HashMap::new();
        decay_half_life.insert("default".to_string(), 90);
        decay_half_life.insert("translation".to_string(), 30);
        decay_half_life.insert("transcription".to_string(), 30);
        decay_half_life.insert("data-entry".to_string(), 30);
        decay_half_life.insert("moderation".to_string(), 30);
        decay_half_life.insert("research".to_string(), 180);
        decay_half_life.insert("architecture".to_string(), 180);
        decay_half_life.insert("security-audit".to_string(), 180);
        decay_half_life.insert("legal-review".to_string(), 180);

        Self {
            endorsements: HashMap::new(),
            trust_scores: HashMap::new(),
            blocked_agents: HashSet::new(),
            blocked_reasons: HashMap::new(),
            allowed_agents: HashMap::new(),
            collusion_penalties: HashMap::new(),
            seed_peers: Vec::new(),
            max_recursion_depth: default_max_recursion_depth(),
            decay_half_life,
            collusion_external_ratio_threshold: default_collusion_external_ratio_threshold(),
            collusion_min_cluster_size: default_collusion_min_cluster_size(),
            trust_cache_ttl_seconds: default_trust_cache_ttl_seconds(),
            scc_cache_ttl_seconds: default_scc_cache_ttl_seconds(),
        }
    }

    pub fn add_endorsement(&mut self, endorsement: EndorsementV2) {
        self.endorsements
            .insert(endorsement_cache_key(&endorsement), endorsement);
    }

    pub fn block_agent(&mut self, did: String) {
        self.blocked_agents.insert(did.clone());
        self.allowed_agents.remove(&did);
    }

    pub fn block_agent_with_reason(&mut self, did: String, reason: Option<String>) {
        self.block_agent(did.clone());
        if let Some(reason) = reason.map(|value| value.trim().to_string()) {
            if !reason.is_empty() {
                self.blocked_reasons.insert(did, reason);
            }
        }
    }

    pub fn unblock_agent(&mut self, did: &str) {
        self.blocked_agents.remove(did);
        self.blocked_reasons.remove(did);
    }

    pub fn is_blocked(&self, did: &str) -> bool {
        self.blocked_agents.contains(did)
    }

    pub fn blocked_reason(&self, did: &str) -> Option<&str> {
        self.blocked_reasons.get(did).map(String::as_str)
    }

    pub fn allow_agent(&mut self, did: String, note: Option<String>) {
        self.unblock_agent(&did);
        self.allowed_agents.insert(
            did,
            AllowedAgent {
                note: note
                    .map(|value| value.trim().to_string())
                    .filter(|value| !value.is_empty()),
                added_at: Some(current_unix_seconds()),
            },
        );
    }

    pub fn is_allowed(&self, did: &str) -> bool {
        self.allowed_agents.contains_key(did)
    }

    pub fn cache_trust_score(&mut self, did: String, score: f64, ttl_seconds: u64) {
        self.trust_scores.insert(
            did,
            CachedTrustScore {
                score,
                computed_at: current_unix_seconds(),
                ttl: ttl_seconds,
            },
        );
    }

    pub fn get_cached_trust_score(&self, did: &str) -> Option<f64> {
        self.trust_scores.get(did).and_then(|cached| {
            let now = current_unix_seconds();
            if now.saturating_sub(cached.computed_at) < cached.ttl {
                Some(cached.score)
            } else {
                None
            }
        })
    }

    pub fn cache_collusion_penalty(&mut self, did: String, penalty: f64, ttl_seconds: u64) {
        self.collusion_penalties.insert(
            did,
            CachedCollusionPenalty {
                penalty,
                computed_at: current_unix_seconds(),
                ttl: ttl_seconds,
            },
        );
    }

    pub fn get_cached_collusion_penalty(&self, did: &str) -> Option<f64> {
        self.collusion_penalties.get(did).and_then(|cached| {
            let now = current_unix_seconds();
            if now.saturating_sub(cached.computed_at) < cached.ttl {
                Some(cached.penalty)
            } else {
                None
            }
        })
    }
}

pub fn resolve_reachability_policy(
    explicit_relay: Option<&str>,
    config: Option<&Config>,
) -> ReachabilityPolicy {
    let mut policy = config
        .and_then(|cfg| cfg.reachability_policy.clone())
        .unwrap_or_default();

    if policy.bootstrap_providers.is_empty() {
        policy.bootstrap_providers = env_bootstrap_providers();
    } else {
        policy.bootstrap_providers = normalize_provider_urls(policy.bootstrap_providers);
    }

    if let Some(relay) = normalize_optional_string(explicit_relay) {
        policy.bootstrap_providers = vec![relay];
        policy.mode = ReachabilityMode::Fixed;
        policy.auto_discover_providers = false;
        policy.target_provider_count = 1;
    }

    if policy.target_provider_count == 0 {
        policy.target_provider_count = default_target_provider_count();
    }

    if matches!(policy.mode, ReachabilityMode::Fixed) && policy.bootstrap_providers.is_empty() {
        policy.bootstrap_providers = default_bootstrap_providers();
    }

    if policy.bootstrap_providers.is_empty() {
        policy.bootstrap_providers = default_bootstrap_providers();
    }

    policy
}

pub fn resolve_relay_invite_token(
    explicit: Option<&str>,
    config: Option<&Config>,
) -> Option<String> {
    normalize_optional_string(explicit)
        .or_else(|| {
            normalize_optional_string(std::env::var("QUADRA_A_INVITE_TOKEN").ok().as_deref())
        })
        .or_else(|| normalize_optional_string(std::env::var("HW1_INVITE_TOKEN").ok().as_deref()))
        .or_else(|| {
            config.and_then(|cfg| normalize_optional_string(cfg.relay_invite_token.as_deref()))
        })
}

pub fn endorsement_cache_key(endorsement: &EndorsementV2) -> String {
    format!(
        "{}:{}:{}:{}",
        endorsement.endorser,
        endorsement.endorsee,
        endorsement.domain.as_deref().unwrap_or("*"),
        endorsement.endorsement_type
    )
}

#[cfg(test)]
mod tests {
    use super::Config;
    use crate::e2e::ensure_local_e2e_config;
    use crate::identity::{derive_did, KeyPair};
    use serde_json::json;

    #[test]
    fn parses_empty_e2e_config_and_rebuilds_valid_state() {
        let keypair = KeyPair::generate();
        let did = derive_did(keypair.verifying_key.as_bytes());
        let mut config: Config = serde_json::from_value(json!({
            "identity": {
                "did": did,
                "publicKey": keypair.public_key_hex(),
                "privateKey": keypair.private_key_hex(),
            },
            "e2e": {}
        }))
        .expect("config parses");

        assert!(config.e2e.as_ref().is_some_and(|e2e| !e2e.is_valid()));

        let created = ensure_local_e2e_config(&mut config).expect("e2e config rebuilt");
        assert!(created);
        assert!(config.e2e.as_ref().is_some_and(|e2e| e2e.is_valid()));
        assert!(config
            .device_identity
            .as_ref()
            .is_some_and(|device_identity| !device_identity.seed.is_empty()
                && !device_identity.device_id.is_empty()));
    }

    #[test]
    fn backfills_device_identity_from_existing_device_id() {
        let keypair = KeyPair::generate();
        let did = derive_did(keypair.verifying_key.as_bytes());
        let device_id = "device-existing".to_string();
        let mut config: Config = serde_json::from_value(json!({
            "identity": {
                "did": did,
                "publicKey": keypair.public_key_hex(),
                "privateKey": keypair.private_key_hex(),
            },
            "e2e": {
                "currentDeviceId": device_id,
                "devices": {
                    "device-existing": {
                        "deviceId": "device-existing",
                        "createdAt": 1,
                        "identityKey": {
                            "publicKey": "11",
                            "privateKey": "22"
                        },
                        "signedPreKey": {
                            "signedPreKeyId": 1,
                            "publicKey": "33",
                            "privateKey": "44",
                            "signature": "55",
                            "createdAt": 1
                        },
                        "oneTimePreKeys": [],
                        "lastResupplyAt": 1,
                        "sessions": {}
                    }
                }
            }
        }))
        .expect("config parses");

        let created = ensure_local_e2e_config(&mut config).expect("device identity backfilled");
        assert!(!created);
        assert_eq!(
            config
                .device_identity
                .as_ref()
                .expect("device identity present")
                .device_id,
            "device-existing"
        );
    }
}