ta-changeset 0.15.15-alpha.3

ChangeSet and PR Package data model for Trusted Autonomy
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
// channel_registry.rs — Pluggable IO channel system (v0.7.0).
//
// All channels (CLI, web, Slack, Discord, email) are equal: they share the
// same ChannelFactory trait and register in the ChannelRegistry at startup.
// Channel routing config (`.ta/config.yaml`) determines which channel handles
// which concern (review, notify, session, escalation).

use std::collections::HashMap;
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::review_channel::{ReviewChannel, ReviewChannelError};
use crate::session_channel::{SessionChannel, SessionChannelError};

/// What a channel implementation can do.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelCapabilitySet {
    /// Can this channel deliver review interactions (approve/deny)?
    pub supports_review: bool,
    /// Can this channel stream session events (agent output, etc.)?
    pub supports_session: bool,
    /// Can this channel deliver notifications?
    pub supports_notify: bool,
    /// Does this channel support rich media (images, code blocks, buttons)?
    pub supports_rich_media: bool,
    /// Does this channel support threaded conversations?
    pub supports_threads: bool,
}

impl Default for ChannelCapabilitySet {
    fn default() -> Self {
        Self {
            supports_review: true,
            supports_session: true,
            supports_notify: true,
            supports_rich_media: false,
            supports_threads: false,
        }
    }
}

/// Factory trait for creating channel instances.
///
/// Each channel plugin (terminal, Slack, Discord, email, webhook) implements
/// this trait. The registry holds factories, and the routing config decides
/// which factory handles which concern.
pub trait ChannelFactory: Send + Sync {
    /// Channel type name (e.g., "terminal", "slack", "discord", "email", "webhook").
    fn channel_type(&self) -> &str;

    /// Create a ReviewChannel for human review interactions.
    fn build_review(
        &self,
        config: &serde_json::Value,
    ) -> Result<Box<dyn ReviewChannel>, ReviewChannelError>;

    /// Create a SessionChannel for agent-human streaming.
    fn build_session(
        &self,
        config: &serde_json::Value,
    ) -> Result<Box<dyn SessionChannel>, SessionChannelError>;

    /// What this channel type supports.
    fn capabilities(&self) -> ChannelCapabilitySet;
}

/// Registry of channel factories, keyed by channel type name.
pub struct ChannelRegistry {
    factories: HashMap<String, Box<dyn ChannelFactory>>,
}

impl ChannelRegistry {
    /// Create a new empty registry.
    pub fn new() -> Self {
        Self {
            factories: HashMap::new(),
        }
    }

    /// Register a channel factory.
    pub fn register(&mut self, factory: Box<dyn ChannelFactory>) {
        let name = factory.channel_type().to_string();
        self.factories.insert(name, factory);
    }

    /// Get a factory by channel type.
    pub fn get(&self, channel_type: &str) -> Option<&dyn ChannelFactory> {
        self.factories.get(channel_type).map(|f| f.as_ref())
    }

    /// List all registered channel types.
    pub fn channel_types(&self) -> Vec<&str> {
        self.factories.keys().map(|k| k.as_str()).collect()
    }

    /// Check if a channel type is registered.
    pub fn has_channel(&self, channel_type: &str) -> bool {
        self.factories.contains_key(channel_type)
    }

    /// Number of registered channel factories.
    pub fn len(&self) -> usize {
        self.factories.len()
    }

    /// Whether the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.factories.is_empty()
    }

    /// Build a ReviewChannel from routing config.
    pub fn build_review_from_config(
        &self,
        route: &ChannelRouteConfig,
    ) -> Result<Box<dyn ReviewChannel>, ReviewChannelError> {
        let factory = self.get(&route.channel_type).ok_or_else(|| {
            ReviewChannelError::Other(format!(
                "unknown channel type: '{}'. Registered: {:?}",
                route.channel_type,
                self.channel_types()
            ))
        })?;
        factory.build_review(&route.config)
    }

    /// Build a ReviewChannel from a ReviewRouteConfig (single or multi).
    ///
    /// If the config specifies multiple channels, returns a `MultiReviewChannel`
    /// wrapping all of them. If single, returns the channel directly.
    pub fn build_review_from_route(
        &self,
        route: &ReviewRouteConfig,
        strategy: &crate::multi_channel::MultiChannelStrategy,
    ) -> Result<Box<dyn ReviewChannel>, ReviewChannelError> {
        let configs = route.configs();
        if configs.len() == 1 {
            return self.build_review_from_config(configs[0]);
        }
        let mut channels: Vec<Box<dyn ReviewChannel>> = Vec::with_capacity(configs.len());
        for config in configs {
            channels.push(self.build_review_from_config(config)?);
        }
        Ok(Box::new(crate::multi_channel::MultiReviewChannel::new(
            channels,
            strategy.clone(),
        )))
    }

    /// Build a SessionChannel from routing config.
    pub fn build_session_from_config(
        &self,
        route: &ChannelRouteConfig,
    ) -> Result<Box<dyn SessionChannel>, SessionChannelError> {
        let factory = self.get(&route.channel_type).ok_or_else(|| {
            SessionChannelError::Other(format!(
                "unknown channel type: '{}'. Registered: {:?}",
                route.channel_type,
                self.channel_types()
            ))
        })?;
        factory.build_session(&route.config)
    }
}

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

/// A single channel routing entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelRouteConfig {
    /// Channel type (must match a registered ChannelFactory).
    #[serde(rename = "type")]
    pub channel_type: String,
    /// Channel-specific config (Slack channel, email address, etc.).
    #[serde(flatten)]
    pub config: serde_json::Value,
}

impl Default for ChannelRouteConfig {
    fn default() -> Self {
        Self {
            channel_type: "terminal".to_string(),
            config: serde_json::Value::Object(serde_json::Map::new()),
        }
    }
}

/// Notification routing entry (supports multiple targets).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotifyRouteConfig {
    /// Channel type.
    #[serde(rename = "type")]
    pub channel_type: String,
    /// Minimum notification level to deliver: "debug", "info", "warning", "error".
    #[serde(default = "default_notify_level")]
    pub level: String,
    /// Channel-specific config.
    #[serde(flatten)]
    pub config: serde_json::Value,
}

fn default_notify_level() -> String {
    "info".to_string()
}

/// One-or-many channel route config (v0.10.0 multi-channel routing).
///
/// Accepts either a single channel object or an array of channels:
/// ```yaml
/// review: { type: terminal }          # single
/// review:                              # multiple
///   - { type: terminal }
///   - { type: webhook, endpoint: /tmp/review }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ReviewRouteConfig {
    /// A single channel (backward-compatible default).
    Single(ChannelRouteConfig),
    /// Multiple channels — dispatched via MultiReviewChannel.
    Multiple(Vec<ChannelRouteConfig>),
}

impl Default for ReviewRouteConfig {
    fn default() -> Self {
        Self::Single(ChannelRouteConfig::default())
    }
}

impl ReviewRouteConfig {
    /// Return the list of channel configs (always at least one).
    pub fn configs(&self) -> Vec<&ChannelRouteConfig> {
        match self {
            Self::Single(c) => vec![c],
            Self::Multiple(cs) => cs.iter().collect(),
        }
    }

    /// True if this specifies more than one channel.
    pub fn is_multi(&self) -> bool {
        matches!(self, Self::Multiple(cs) if cs.len() > 1)
    }
}

/// One-or-many escalation route config (v0.10.0).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EscalationRouteConfig {
    /// A single escalation channel.
    Single(ChannelRouteConfig),
    /// Multiple escalation channels.
    Multiple(Vec<ChannelRouteConfig>),
}

impl EscalationRouteConfig {
    /// Return the list of channel configs.
    pub fn configs(&self) -> Vec<&ChannelRouteConfig> {
        match self {
            Self::Single(c) => vec![c],
            Self::Multiple(cs) => cs.iter().collect(),
        }
    }
}

/// Top-level channel routing configuration.
///
/// Loaded from `.ta/config.yaml`:
/// ```yaml
/// channels:
///   review: { type: terminal }                  # single channel
///   review:                                      # multi-channel (v0.10.0)
///     - { type: terminal }
///     - { type: webhook, endpoint: /tmp/review }
///   notify:
///     - { type: terminal }
///     - { type: slack, channel: "#reviews", level: warning }
///   session: { type: terminal }
///   escalation: { type: email, to: "mgr@co.com" }
///   strategy: first_response                     # or "quorum"
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ChannelRoutingConfig {
    /// Channel(s) for review interactions (draft approve/deny).
    /// Supports a single channel or array of channels (v0.10.0).
    #[serde(default)]
    pub review: ReviewRouteConfig,
    /// Channels for notifications (can be multiple).
    #[serde(default)]
    pub notify: Vec<NotifyRouteConfig>,
    /// Channel for interactive sessions.
    #[serde(default)]
    pub session: ChannelRouteConfig,
    /// Channel(s) for escalation (high-priority or supervisor review).
    /// Supports a single channel or array of channels (v0.10.0).
    #[serde(default)]
    pub escalation: Option<EscalationRouteConfig>,
    /// Default agent to assign when requests come in through a channel.
    #[serde(default)]
    pub default_agent: Option<String>,
    /// Default workflow to use for channel-initiated goals.
    #[serde(default)]
    pub default_workflow: Option<String>,
    /// Multi-channel dispatch strategy (v0.10.0): "first_response" (default) or "quorum".
    #[serde(default)]
    pub strategy: Option<String>,
}

// ChannelRoutingConfig derives Default since all fields have Default implementations.

/// Wrapper for `.ta/config.yaml` — the channels section lives here.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TaConfig {
    /// Channel routing configuration.
    #[serde(default)]
    pub channels: ChannelRoutingConfig,
}

/// Load `.ta/config.yaml` from project root.
pub fn load_config(project_root: &Path) -> TaConfig {
    let config_path = project_root.join(".ta").join("config.yaml");
    if !config_path.exists() {
        return TaConfig::default();
    }
    match std::fs::read_to_string(&config_path) {
        Ok(content) => serde_yaml::from_str(&content).unwrap_or_default(),
        Err(_) => TaConfig::default(),
    }
}

/// Built-in terminal channel factory.
///
/// Always available — provides CLI-based review and session channels.
pub struct TerminalChannelFactory;

impl ChannelFactory for TerminalChannelFactory {
    fn channel_type(&self) -> &str {
        "terminal"
    }

    fn build_review(
        &self,
        _config: &serde_json::Value,
    ) -> Result<Box<dyn ReviewChannel>, ReviewChannelError> {
        Ok(Box::new(crate::terminal_channel::TerminalChannel::stdio()))
    }

    fn build_session(
        &self,
        _config: &serde_json::Value,
    ) -> Result<Box<dyn SessionChannel>, SessionChannelError> {
        Ok(Box::new(
            crate::terminal_channel::TerminalSessionChannel::new(),
        ))
    }

    fn capabilities(&self) -> ChannelCapabilitySet {
        ChannelCapabilitySet {
            supports_review: true,
            supports_session: true,
            supports_notify: true,
            supports_rich_media: false,
            supports_threads: false,
        }
    }
}

/// Built-in auto-approve channel factory (for testing/CI).
pub struct AutoApproveChannelFactory;

impl ChannelFactory for AutoApproveChannelFactory {
    fn channel_type(&self) -> &str {
        "auto-approve"
    }

    fn build_review(
        &self,
        _config: &serde_json::Value,
    ) -> Result<Box<dyn ReviewChannel>, ReviewChannelError> {
        Ok(Box::new(crate::terminal_channel::AutoApproveChannel::new()))
    }

    fn build_session(
        &self,
        _config: &serde_json::Value,
    ) -> Result<Box<dyn SessionChannel>, SessionChannelError> {
        // Auto-approve doesn't have meaningful session interaction.
        Ok(Box::new(
            crate::terminal_channel::TerminalSessionChannel::new(),
        ))
    }

    fn capabilities(&self) -> ChannelCapabilitySet {
        ChannelCapabilitySet {
            supports_review: true,
            supports_session: false,
            supports_notify: false,
            supports_rich_media: false,
            supports_threads: false,
        }
    }
}

/// Built-in webhook channel factory.
pub struct WebhookChannelFactory;

impl ChannelFactory for WebhookChannelFactory {
    fn channel_type(&self) -> &str {
        "webhook"
    }

    fn build_review(
        &self,
        config: &serde_json::Value,
    ) -> Result<Box<dyn ReviewChannel>, ReviewChannelError> {
        let endpoint = config
            .get("endpoint")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                ReviewChannelError::Other("webhook requires 'endpoint' in config".into())
            })?;
        Ok(Box::new(crate::webhook_channel::WebhookChannel::new(
            endpoint,
        )))
    }

    fn build_session(
        &self,
        _config: &serde_json::Value,
    ) -> Result<Box<dyn SessionChannel>, SessionChannelError> {
        // Webhook doesn't support bidirectional sessions.
        Err(SessionChannelError::Other(
            "webhook does not support interactive sessions".into(),
        ))
    }

    fn capabilities(&self) -> ChannelCapabilitySet {
        ChannelCapabilitySet {
            supports_review: true,
            supports_session: false,
            supports_notify: true,
            supports_rich_media: false,
            supports_threads: false,
        }
    }
}

/// Create a ChannelRegistry pre-loaded with all built-in channel factories.
pub fn default_registry() -> ChannelRegistry {
    let mut registry = ChannelRegistry::new();
    registry.register(Box::new(TerminalChannelFactory));
    registry.register(Box::new(AutoApproveChannelFactory));
    registry.register(Box::new(WebhookChannelFactory));
    registry
}

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

    #[test]
    fn default_registry_has_builtins() {
        let registry = default_registry();
        assert!(registry.has_channel("terminal"));
        assert!(registry.has_channel("auto-approve"));
        assert!(registry.has_channel("webhook"));
        assert!(!registry.has_channel("slack"));
        assert_eq!(registry.len(), 3);
    }

    #[test]
    fn build_review_from_config() {
        let registry = default_registry();
        let route = ChannelRouteConfig {
            channel_type: "terminal".into(),
            config: serde_json::json!({}),
        };
        let channel = registry.build_review_from_config(&route);
        assert!(channel.is_ok());
    }

    #[test]
    fn build_review_unknown_type_errors() {
        let registry = default_registry();
        let route = ChannelRouteConfig {
            channel_type: "slack".into(),
            config: serde_json::json!({}),
        };
        let result = registry.build_review_from_config(&route);
        assert!(result.is_err());
    }

    #[test]
    fn channel_routing_config_single_review() {
        let yaml = r#"
review:
  type: terminal
notify:
  - type: terminal
  - type: webhook
    endpoint: "/tmp/notify"
    level: warning
session:
  type: terminal
escalation:
  type: webhook
  endpoint: "/tmp/escalate"
default_agent: claude-code
"#;
        let config: ChannelRoutingConfig = serde_yaml::from_str(yaml).unwrap();
        let review_configs = config.review.configs();
        assert_eq!(review_configs.len(), 1);
        assert_eq!(review_configs[0].channel_type, "terminal");
        assert!(!config.review.is_multi());
        assert_eq!(config.notify.len(), 2);
        assert_eq!(config.notify[1].channel_type, "webhook");
        assert_eq!(config.notify[1].level, "warning");
        assert!(config.escalation.is_some());
        assert_eq!(config.default_agent.as_deref(), Some("claude-code"));
    }

    #[test]
    fn channel_routing_config_multi_review() {
        let yaml = r#"
review:
  - type: terminal
  - type: webhook
    endpoint: "/tmp/review"
session:
  type: terminal
strategy: first_response
"#;
        let config: ChannelRoutingConfig = serde_yaml::from_str(yaml).unwrap();
        let review_configs = config.review.configs();
        assert_eq!(review_configs.len(), 2);
        assert_eq!(review_configs[0].channel_type, "terminal");
        assert_eq!(review_configs[1].channel_type, "webhook");
        assert!(config.review.is_multi());
        assert_eq!(config.strategy.as_deref(), Some("first_response"));
    }

    #[test]
    fn channel_routing_config_multi_escalation() {
        let yaml = r#"
review:
  type: terminal
session:
  type: terminal
escalation:
  - type: webhook
    endpoint: "/tmp/esc1"
  - type: webhook
    endpoint: "/tmp/esc2"
"#;
        let config: ChannelRoutingConfig = serde_yaml::from_str(yaml).unwrap();
        let esc = config.escalation.unwrap();
        assert_eq!(esc.configs().len(), 2);
    }

    #[test]
    fn ta_config_deserialization() {
        let yaml = r#"
channels:
  review:
    type: terminal
  session:
    type: terminal
"#;
        let config: TaConfig = serde_yaml::from_str(yaml).unwrap();
        let review_configs = config.channels.review.configs();
        assert_eq!(review_configs[0].channel_type, "terminal");
    }

    #[test]
    fn default_ta_config() {
        let config = TaConfig::default();
        let review_configs = config.channels.review.configs();
        assert_eq!(review_configs[0].channel_type, "terminal");
        assert!(config.channels.notify.is_empty());
    }

    #[test]
    fn build_multi_review_from_route_single() {
        let registry = default_registry();
        let route = ReviewRouteConfig::Single(ChannelRouteConfig {
            channel_type: "terminal".into(),
            config: serde_json::json!({}),
        });
        let strategy = crate::multi_channel::MultiChannelStrategy::FirstResponse;
        let channel = registry.build_review_from_route(&route, &strategy);
        assert!(channel.is_ok());
    }

    #[test]
    fn build_multi_review_from_route_multiple() {
        let registry = default_registry();
        let route = ReviewRouteConfig::Multiple(vec![
            ChannelRouteConfig {
                channel_type: "auto-approve".into(),
                config: serde_json::json!({}),
            },
            ChannelRouteConfig {
                channel_type: "auto-approve".into(),
                config: serde_json::json!({}),
            },
        ]);
        let strategy = crate::multi_channel::MultiChannelStrategy::FirstResponse;
        let channel = registry.build_review_from_route(&route, &strategy);
        assert!(channel.is_ok());
    }

    #[test]
    fn channel_capability_set_defaults() {
        let caps = ChannelCapabilitySet::default();
        assert!(caps.supports_review);
        assert!(caps.supports_session);
        assert!(caps.supports_notify);
        assert!(!caps.supports_rich_media);
        assert!(!caps.supports_threads);
    }

    #[test]
    fn register_custom_factory() {
        struct MockFactory;
        impl ChannelFactory for MockFactory {
            fn channel_type(&self) -> &str {
                "mock"
            }
            fn build_review(
                &self,
                _config: &serde_json::Value,
            ) -> Result<Box<dyn ReviewChannel>, ReviewChannelError> {
                Ok(Box::new(crate::terminal_channel::AutoApproveChannel::new()))
            }
            fn build_session(
                &self,
                _config: &serde_json::Value,
            ) -> Result<Box<dyn SessionChannel>, SessionChannelError> {
                Err(SessionChannelError::Other("mock".into()))
            }
            fn capabilities(&self) -> ChannelCapabilitySet {
                ChannelCapabilitySet::default()
            }
        }

        let mut registry = default_registry();
        registry.register(Box::new(MockFactory));
        assert!(registry.has_channel("mock"));
        assert_eq!(registry.len(), 4);
    }

    #[test]
    fn load_config_missing_file() {
        let dir = tempfile::TempDir::new().unwrap();
        let config = load_config(dir.path());
        assert_eq!(config.channels.review.configs()[0].channel_type, "terminal");
    }

    #[test]
    fn webhook_factory_requires_endpoint() {
        let registry = default_registry();
        let route = ChannelRouteConfig {
            channel_type: "webhook".into(),
            config: serde_json::json!({}),
        };
        let result = registry.build_review_from_config(&route);
        assert!(result.is_err());
    }
}