roboticus-core 0.11.1

Shared types, config parsing, personality system, and error types for the Roboticus agent runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Claim-based RBAC authority resolution.
//!
//! Every message entry point calls into this module to derive the sender's
//! effective [`InputAuthority`] from all authentication layers.
//!
//! # Algorithm
//!
//! ```text
//! effective = min(max(positive_grants…), min(negative_ceilings…))
//! ```
//!
//! Positive grants **OR** across layers (any layer can grant authority).
//! Negative ceilings **AND** across layers (strictest restriction wins).

use crate::config::SecurityConfig;
use crate::types::{ClaimSource, InputAuthority, SecurityClaim};

/// Inputs describing what a channel adapter knows about the sender.
#[derive(Debug, Clone)]
pub struct ChannelContext<'a> {
    /// Sender's platform-specific ID (chat ID, phone number, etc.).
    pub sender_id: &'a str,
    /// Chat/group/guild ID, if distinct from sender (e.g., Telegram chat ID).
    pub chat_id: &'a str,
    /// Platform name (e.g., "telegram", "discord", "api").
    pub channel: &'a str,
    /// Whether the sender passed the adapter's allow-list check.
    pub sender_in_allowlist: bool,
    /// Whether the adapter's allow-list is non-empty (has entries).
    pub allowlist_configured: bool,
    /// Whether the threat scanner flagged this input as Caution-level.
    pub threat_is_caution: bool,
    /// The global `channels.trusted_sender_ids` list.
    pub trusted_sender_ids: &'a [String],
}

/// Resolve a [`SecurityClaim`] for a channel-originated message.
///
/// This is the single authority resolution path for Telegram, Discord,
/// WhatsApp, Signal, and Email messages.
pub fn resolve_channel_claim(ctx: &ChannelContext<'_>, sec: &SecurityConfig) -> SecurityClaim {
    let mut grants: Vec<InputAuthority> = Vec::new();
    let mut sources: Vec<ClaimSource> = Vec::new();
    let mut ceilings: Vec<InputAuthority> = Vec::new();

    // ── Positive grants ─────────────────────────────────────────────

    // Layer 1: Channel allow-list
    if ctx.allowlist_configured {
        // Non-empty allow-list — grant if sender passed it
        if ctx.sender_in_allowlist {
            grants.push(sec.allowlist_authority);
            sources.push(ClaimSource::ChannelAllowList);
        }
        // Sender NOT in a configured allow-list: no grant from this layer
    }
    // Empty allow-list: no grant (secure default)

    // Layer 2: trusted_sender_ids
    if !ctx.trusted_sender_ids.is_empty() {
        let is_trusted = ctx
            .trusted_sender_ids
            .iter()
            .any(|id| id == ctx.chat_id || id == ctx.sender_id);
        if is_trusted {
            grants.push(sec.trusted_authority);
            sources.push(ClaimSource::TrustedSenderId);
        }
    }

    // ── Negative ceilings ───────────────────────────────────────────

    if ctx.threat_is_caution {
        ceilings.push(sec.threat_caution_ceiling);
    }

    // ── Compose ─────────────────────────────────────────────────────

    compose_claim(grants, sources, ceilings, ctx.sender_id, ctx.channel)
}

/// Resolve a [`SecurityClaim`] for an HTTP API or WebSocket request.
///
/// API callers are authenticated by API key (currently implicit — the API
/// is only accessible on localhost). The threat scanner can still apply a
/// ceiling.
pub fn resolve_api_claim(
    threat_is_caution: bool,
    channel: &str,
    sec: &SecurityConfig,
) -> SecurityClaim {
    let grants = vec![sec.api_authority];
    let sources = vec![ClaimSource::ApiKey];
    let mut ceilings: Vec<InputAuthority> = Vec::new();

    if threat_is_caution {
        ceilings.push(sec.threat_caution_ceiling);
    }

    compose_claim(grants, sources, ceilings, "api", channel)
}

/// Resolve a [`SecurityClaim`] for an A2A (agent-to-agent) session.
///
/// A2A peers are authenticated via ECDH X25519 key exchange. They receive
/// `Peer` authority — never `Creator`. The threat scanner still applies.
pub fn resolve_a2a_claim(
    threat_is_caution: bool,
    sender_id: &str,
    sec: &SecurityConfig,
) -> SecurityClaim {
    let grants = vec![InputAuthority::Peer];
    let sources = vec![ClaimSource::A2aSession];
    let mut ceilings: Vec<InputAuthority> = Vec::new();

    if threat_is_caution {
        ceilings.push(sec.threat_caution_ceiling);
    }

    compose_claim(grants, sources, ceilings, sender_id, "a2a")
}

/// Compose grants and ceilings into a final [`SecurityClaim`].
///
/// ```text
/// effective = min(max(grants…), min(ceilings…))
/// ```
fn compose_claim(
    grants: Vec<InputAuthority>,
    mut sources: Vec<ClaimSource>,
    ceilings: Vec<InputAuthority>,
    sender_id: &str,
    channel: &str,
) -> SecurityClaim {
    // Best grant (OR — any layer can grant).
    // NOTE: The unwrap_or_else closure mutates `sources` to record Anonymous
    // when no grants were provided. This is safe because `sources` is owned
    // by this function and consumed into the returned SecurityClaim.
    let effective_grant = grants.iter().copied().max().unwrap_or_else(|| {
        sources.push(ClaimSource::Anonymous);
        InputAuthority::External
    });

    // Strictest ceiling (AND — all must allow)
    let effective_ceiling = ceilings
        .iter()
        .copied()
        .min()
        .unwrap_or(InputAuthority::Creator); // no restrictions

    let final_authority = effective_grant.min(effective_ceiling);
    let threat_downgraded = !ceilings.is_empty() && final_authority < effective_grant;

    SecurityClaim {
        authority: final_authority,
        sources,
        ceiling: effective_ceiling,
        threat_downgraded,
        sender_id: sender_id.to_string(),
        channel: channel.to_string(),
    }
}

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

    fn default_sec() -> SecurityConfig {
        SecurityConfig::default()
    }

    fn channel_ctx<'a>(
        sender_id: &'a str,
        chat_id: &'a str,
        channel: &'a str,
        sender_in_allowlist: bool,
        allowlist_configured: bool,
        threat_is_caution: bool,
        trusted: &'a [String],
    ) -> ChannelContext<'a> {
        ChannelContext {
            sender_id,
            chat_id,
            channel,
            sender_in_allowlist,
            allowlist_configured,
            threat_is_caution,
            trusted_sender_ids: trusted,
        }
    }

    // ── Grant composition ───────────────────────────────────────────

    #[test]
    fn no_grants_yields_external() {
        let sec = default_sec();
        let ctx = channel_ctx("u1", "c1", "telegram", false, true, false, &[]);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::External);
        assert!(claim.sources.contains(&ClaimSource::Anonymous));
    }

    #[test]
    fn allowlist_only_yields_peer() {
        let sec = default_sec();
        let ctx = channel_ctx("u1", "c1", "telegram", true, true, false, &[]);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::Peer);
        assert!(claim.sources.contains(&ClaimSource::ChannelAllowList));
    }

    #[test]
    fn trusted_only_yields_creator() {
        let sec = default_sec();
        let trusted = vec!["u1".to_string()];
        let ctx = channel_ctx("u1", "c1", "telegram", false, true, false, &trusted);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::Creator);
        assert!(claim.sources.contains(&ClaimSource::TrustedSenderId));
    }

    #[test]
    fn trusted_by_chat_id() {
        let sec = default_sec();
        let trusted = vec!["c1".to_string()];
        let ctx = channel_ctx("u1", "c1", "telegram", false, true, false, &trusted);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::Creator);
    }

    #[test]
    fn both_allowlist_and_trusted_yields_creator() {
        // OR: best grant wins
        let sec = default_sec();
        let trusted = vec!["u1".to_string()];
        let ctx = channel_ctx("u1", "c1", "telegram", true, true, false, &trusted);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::Creator);
        assert!(claim.sources.contains(&ClaimSource::ChannelAllowList));
        assert!(claim.sources.contains(&ClaimSource::TrustedSenderId));
    }

    // ── Ceiling composition ─────────────────────────────────────────

    #[test]
    fn threat_ceiling_downgrades_creator() {
        let sec = default_sec();
        let trusted = vec!["u1".to_string()];
        let ctx = channel_ctx("u1", "c1", "telegram", true, true, true, &trusted);
        let claim = resolve_channel_claim(&ctx, &sec);
        // Creator grant capped by External ceiling
        assert_eq!(claim.authority, InputAuthority::External);
        assert!(claim.threat_downgraded);
        assert_eq!(claim.ceiling, InputAuthority::External);
    }

    #[test]
    fn custom_threat_ceiling() {
        let mut sec = default_sec();
        sec.threat_caution_ceiling = InputAuthority::Peer;
        let trusted = vec!["u1".to_string()];
        let ctx = channel_ctx("u1", "c1", "telegram", true, true, true, &trusted);
        let claim = resolve_channel_claim(&ctx, &sec);
        // Creator grant capped by Peer ceiling
        assert_eq!(claim.authority, InputAuthority::Peer);
        assert!(claim.threat_downgraded);
    }

    #[test]
    fn no_threat_means_no_ceiling() {
        let sec = default_sec();
        let trusted = vec!["u1".to_string()];
        let ctx = channel_ctx("u1", "c1", "telegram", true, true, false, &trusted);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::Creator);
        assert!(!claim.threat_downgraded);
        assert_eq!(claim.ceiling, InputAuthority::Creator); // no restriction
    }

    // ── Empty allow-list behavior ───────────────────────────────────

    #[test]
    fn empty_allowlist_deny_on_empty_true_rejects() {
        let sec = default_sec(); // deny_on_empty_allowlist = true
        let ctx = channel_ctx("u1", "c1", "telegram", false, false, false, &[]);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::External);
        assert!(claim.sources.contains(&ClaimSource::Anonymous));
    }

    #[test]
    fn empty_allowlist_still_rejects_even_if_flag_is_false() {
        let mut sec = default_sec();
        // Runtime no longer supports permissive empty allow-lists. Repair/update
        // migrates this value back to true before persisted configs are reloaded.
        sec.deny_on_empty_allowlist = false;
        let ctx = channel_ctx("u1", "c1", "telegram", false, false, false, &[]);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::External);
        assert!(claim.sources.contains(&ClaimSource::Anonymous));
    }

    // ── API claims ──────────────────────────────────────────────────

    #[test]
    fn api_claim_default_creator() {
        let sec = default_sec();
        let claim = resolve_api_claim(false, "api", &sec);
        assert_eq!(claim.authority, InputAuthority::Creator);
        assert!(claim.sources.contains(&ClaimSource::ApiKey));
    }

    #[test]
    fn api_claim_threat_downgrade() {
        let sec = default_sec();
        let claim = resolve_api_claim(true, "api", &sec);
        assert_eq!(claim.authority, InputAuthority::External);
        assert!(claim.threat_downgraded);
    }

    // ── A2A claims ──────────────────────────────────────────────────

    #[test]
    fn a2a_claim_always_peer() {
        let sec = default_sec();
        let claim = resolve_a2a_claim(false, "peer-agent", &sec);
        assert_eq!(claim.authority, InputAuthority::Peer);
        assert!(claim.sources.contains(&ClaimSource::A2aSession));
    }

    #[test]
    fn a2a_claim_threat_downgrade() {
        let sec = default_sec();
        let claim = resolve_a2a_claim(true, "peer-agent", &sec);
        assert_eq!(claim.authority, InputAuthority::External);
        assert!(claim.threat_downgraded);
    }

    // ── Configurable authority levels ───────────────────────────────

    #[test]
    fn custom_allowlist_authority() {
        let mut sec = default_sec();
        sec.allowlist_authority = InputAuthority::Creator;
        let ctx = channel_ctx("u1", "c1", "telegram", true, true, false, &[]);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::Creator);
    }

    #[test]
    fn custom_api_authority_downgraded() {
        let mut sec = default_sec();
        sec.api_authority = InputAuthority::Peer;
        let claim = resolve_api_claim(false, "api", &sec);
        assert_eq!(claim.authority, InputAuthority::Peer);
    }

    // ── Monotonicity properties ─────────────────────────────────────

    #[test]
    fn adding_grant_never_decreases_authority() {
        let sec = default_sec();
        // Without trusted
        let ctx1 = channel_ctx("u1", "c1", "telegram", true, true, false, &[]);
        let claim1 = resolve_channel_claim(&ctx1, &sec);

        // With trusted (additional grant)
        let trusted = vec!["u1".to_string()];
        let ctx2 = channel_ctx("u1", "c1", "telegram", true, true, false, &trusted);
        let claim2 = resolve_channel_claim(&ctx2, &sec);

        assert!(claim2.authority >= claim1.authority);
    }

    #[test]
    fn adding_ceiling_never_increases_authority() {
        let sec = default_sec();
        let trusted = vec!["u1".to_string()];

        // Without threat
        let ctx1 = channel_ctx("u1", "c1", "telegram", true, true, false, &trusted);
        let claim1 = resolve_channel_claim(&ctx1, &sec);

        // With threat (additional ceiling)
        let ctx2 = channel_ctx("u1", "c1", "telegram", true, true, true, &trusted);
        let claim2 = resolve_channel_claim(&ctx2, &sec);

        assert!(claim2.authority <= claim1.authority);
    }

    // ── Edge cases: threat_downgraded correctness ───────────────────

    #[test]
    fn threat_present_but_not_binding_does_not_set_downgraded() {
        // External user with no grants + threat_is_caution = true.
        // Ceiling is External, grant is External → ceiling is not binding.
        let sec = default_sec();
        let ctx = channel_ctx("unknown", "c1", "telegram", false, true, true, &[]);
        let claim = resolve_channel_claim(&ctx, &sec);
        assert_eq!(claim.authority, InputAuthority::External);
        // Ceiling exists but didn't actually reduce authority
        assert!(!claim.threat_downgraded);
    }

    #[test]
    fn api_claim_with_custom_ceiling_and_threat() {
        // API with Peer authority + Peer ceiling → no downgrade (ceiling = grant)
        let mut sec = default_sec();
        sec.api_authority = InputAuthority::Peer;
        sec.threat_caution_ceiling = InputAuthority::Peer;
        let claim = resolve_api_claim(true, "api", &sec);
        assert_eq!(claim.authority, InputAuthority::Peer);
        assert!(!claim.threat_downgraded); // ceiling = grant, not binding

        // API with Creator authority + Peer ceiling → downgrade
        let mut sec2 = default_sec();
        sec2.threat_caution_ceiling = InputAuthority::Peer;
        let claim2 = resolve_api_claim(true, "api", &sec2);
        assert_eq!(claim2.authority, InputAuthority::Peer);
        assert!(claim2.threat_downgraded); // ceiling < grant, binding
    }
}