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
// interaction.rs — Interaction request/response model for ReviewChannel.
//
// These types define the protocol for bidirectional human-agent communication.
// An InteractionRequest is sent when TA needs human input (draft review, plan
// approval, escalation), and InteractionResponse carries the human's decision.
//
// This is the core protocol for v0.4.1.1 (Runtime Channel Architecture).

use std::collections::HashMap;
use std::fmt;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// What kind of interaction is being requested.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum InteractionKind {
    /// A draft is ready for review — human should approve, reject, or discuss.
    DraftReview,
    /// General approval question (e.g., "proceed with this approach?").
    ApprovalDiscussion,
    /// Agent proposes a plan change — human should accept or reject.
    PlanNegotiation,
    /// Agent is escalating an issue that exceeds its authority.
    Escalation,
    /// Agent is asking the human a question mid-execution.
    AgentQuestion,
    /// Extension point for future interaction types.
    Custom(String),
}

impl fmt::Display for InteractionKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            InteractionKind::DraftReview => write!(f, "draft_review"),
            InteractionKind::ApprovalDiscussion => write!(f, "approval_discussion"),
            InteractionKind::PlanNegotiation => write!(f, "plan_negotiation"),
            InteractionKind::Escalation => write!(f, "escalation"),
            InteractionKind::AgentQuestion => write!(f, "agent_question"),
            InteractionKind::Custom(name) => write!(f, "custom:{}", name),
        }
    }
}

/// How urgent is this interaction?
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Urgency {
    /// Agent blocks until human responds.
    Blocking,
    /// Agent can continue, but human should respond eventually.
    Advisory,
    /// Informational only — no response expected.
    Informational,
}

impl fmt::Display for Urgency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Urgency::Blocking => write!(f, "blocking"),
            Urgency::Advisory => write!(f, "advisory"),
            Urgency::Informational => write!(f, "informational"),
        }
    }
}

/// A request from TA to the human, delivered via a ReviewChannel.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InteractionRequest {
    /// Unique identifier for this interaction (for correlation with response).
    pub interaction_id: Uuid,

    /// What kind of interaction this is.
    pub kind: InteractionKind,

    /// Structured payload — contents depend on `kind`.
    /// For DraftReview: { "draft_id": "...", "summary": "...", "artifact_count": N }
    /// For PlanNegotiation: { "phase": "...", "proposed_status": "..." }
    pub context: serde_json::Value,

    /// How urgent is this interaction?
    pub urgency: Urgency,

    /// Arbitrary key-value pairs for channel-specific rendering hints.
    /// e.g., { "color": "yellow", "thread_id": "..." }
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, String>,

    /// When the request was created.
    pub created_at: DateTime<Utc>,

    /// Optional goal ID for context.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub goal_id: Option<Uuid>,
}

impl InteractionRequest {
    /// Create a new interaction request.
    pub fn new(kind: InteractionKind, context: serde_json::Value, urgency: Urgency) -> Self {
        Self {
            interaction_id: Uuid::new_v4(),
            kind,
            context,
            urgency,
            metadata: HashMap::new(),
            created_at: Utc::now(),
            goal_id: None,
        }
    }

    /// Set the goal ID for this interaction.
    pub fn with_goal_id(mut self, goal_id: Uuid) -> Self {
        self.goal_id = Some(goal_id);
        self
    }

    /// Add a metadata key-value pair.
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Create a DraftReview interaction request.
    pub fn draft_review(draft_id: Uuid, summary: &str, artifact_count: usize) -> Self {
        Self::new(
            InteractionKind::DraftReview,
            serde_json::json!({
                "draft_id": draft_id.to_string(),
                "summary": summary,
                "artifact_count": artifact_count,
            }),
            Urgency::Blocking,
        )
    }

    /// Create a PlanNegotiation interaction request.
    pub fn plan_negotiation(phase: &str, proposed_status: &str) -> Self {
        Self::new(
            InteractionKind::PlanNegotiation,
            serde_json::json!({
                "phase": phase,
                "proposed_status": proposed_status,
            }),
            Urgency::Blocking,
        )
    }

    /// Create an Escalation interaction request.
    pub fn escalation(reason: &str, details: serde_json::Value) -> Self {
        Self::new(
            InteractionKind::Escalation,
            serde_json::json!({
                "reason": reason,
                "details": details,
            }),
            Urgency::Blocking,
        )
    }
}

impl fmt::Display for InteractionRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{}] {} (urgency: {})",
            self.interaction_id, self.kind, self.urgency
        )
    }
}

/// The human's decision in response to an InteractionRequest.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case", tag = "decision")]
pub enum Decision {
    /// Approved — proceed as proposed.
    Approve,
    /// Rejected — do not proceed, with explanation.
    Reject { reason: String },
    /// Human wants to discuss further before deciding.
    Discuss,
    /// Skip this interaction for now (non-blocking interactions only).
    SkipForNow,
}

impl fmt::Display for Decision {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Decision::Approve => write!(f, "approved"),
            Decision::Reject { reason } => write!(f, "rejected: {}", reason),
            Decision::Discuss => write!(f, "discuss"),
            Decision::SkipForNow => write!(f, "skipped"),
        }
    }
}

/// The human's response to an InteractionRequest.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InteractionResponse {
    /// Correlation ID — must match the InteractionRequest.interaction_id.
    pub interaction_id: Uuid,

    /// The human's decision.
    pub decision: Decision,

    /// Optional free-text reasoning or feedback.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<String>,

    /// When the response was created.
    pub responded_at: DateTime<Utc>,

    /// Who responded (channel identity, e.g., "cli:tty0", "slack:U12345").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub responder_id: Option<String>,
}

impl InteractionResponse {
    /// Create a new response for a given interaction.
    pub fn new(interaction_id: Uuid, decision: Decision) -> Self {
        Self {
            interaction_id,
            decision,
            reasoning: None,
            responded_at: Utc::now(),
            responder_id: None,
        }
    }

    /// Set reasoning text.
    pub fn with_reasoning(mut self, reasoning: impl Into<String>) -> Self {
        self.reasoning = Some(reasoning.into());
        self
    }

    /// Set responder identity.
    pub fn with_responder(mut self, responder_id: impl Into<String>) -> Self {
        self.responder_id = Some(responder_id.into());
        self
    }
}

impl fmt::Display for InteractionResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}] {}", self.interaction_id, self.decision)
    }
}

/// A non-blocking notification from TA to the human.
/// Unlike InteractionRequest, no response is expected.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Notification {
    /// Unique notification ID.
    pub notification_id: Uuid,

    /// Human-readable message.
    pub message: String,

    /// Severity level for rendering.
    pub level: NotificationLevel,

    /// When the notification was created.
    pub created_at: DateTime<Utc>,

    /// Optional goal ID for context.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub goal_id: Option<Uuid>,
}

impl Notification {
    /// Create a new notification.
    pub fn new(message: impl Into<String>, level: NotificationLevel) -> Self {
        Self {
            notification_id: Uuid::new_v4(),
            message: message.into(),
            level,
            created_at: Utc::now(),
            goal_id: None,
        }
    }

    /// Set the goal ID.
    pub fn with_goal_id(mut self, goal_id: Uuid) -> Self {
        self.goal_id = Some(goal_id);
        self
    }

    /// Create an info notification.
    pub fn info(message: impl Into<String>) -> Self {
        Self::new(message, NotificationLevel::Info)
    }

    /// Create a warning notification.
    pub fn warning(message: impl Into<String>) -> Self {
        Self::new(message, NotificationLevel::Warning)
    }
}

/// Notification severity level.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NotificationLevel {
    Debug,
    Info,
    Warning,
    Error,
}

impl fmt::Display for NotificationLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            NotificationLevel::Debug => write!(f, "debug"),
            NotificationLevel::Info => write!(f, "info"),
            NotificationLevel::Warning => write!(f, "warning"),
            NotificationLevel::Error => write!(f, "error"),
        }
    }
}

/// Describes what a ReviewChannel implementation supports.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ChannelCapabilities {
    /// Whether the channel supports async responses (human responds later, not inline).
    pub supports_async: bool,

    /// Whether the channel supports rich media (images, formatted diffs, etc.).
    pub supports_rich_media: bool,

    /// Whether the channel supports threaded discussions.
    pub supports_threads: bool,
}

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

    #[test]
    fn interaction_request_creation() {
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Test draft", 3);
        assert_eq!(req.kind, InteractionKind::DraftReview);
        assert_eq!(req.urgency, Urgency::Blocking);
        assert_eq!(req.context["artifact_count"], 3);
        assert_eq!(req.context["summary"], "Test draft");
    }

    #[test]
    fn interaction_request_with_metadata() {
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Test", 1)
            .with_metadata("color", "yellow")
            .with_goal_id(Uuid::new_v4());
        assert_eq!(req.metadata.get("color").unwrap(), "yellow");
        assert!(req.goal_id.is_some());
    }

    #[test]
    fn plan_negotiation_request() {
        let req = InteractionRequest::plan_negotiation("v0.4.2", "done");
        assert_eq!(req.kind, InteractionKind::PlanNegotiation);
        assert_eq!(req.context["phase"], "v0.4.2");
        assert_eq!(req.context["proposed_status"], "done");
    }

    #[test]
    fn escalation_request() {
        let req = InteractionRequest::escalation(
            "exceeded token budget",
            serde_json::json!({"budget": 10000, "used": 15000}),
        );
        assert_eq!(req.kind, InteractionKind::Escalation);
        assert_eq!(req.context["reason"], "exceeded token budget");
    }

    #[test]
    fn interaction_response_creation() {
        let id = Uuid::new_v4();
        let resp = InteractionResponse::new(id, Decision::Approve)
            .with_reasoning("looks good")
            .with_responder("cli:tty0");
        assert_eq!(resp.interaction_id, id);
        assert_eq!(resp.decision, Decision::Approve);
        assert_eq!(resp.reasoning.as_deref(), Some("looks good"));
        assert_eq!(resp.responder_id.as_deref(), Some("cli:tty0"));
    }

    #[test]
    fn decision_display() {
        assert_eq!(format!("{}", Decision::Approve), "approved");
        assert_eq!(
            format!(
                "{}",
                Decision::Reject {
                    reason: "missing tests".into()
                }
            ),
            "rejected: missing tests"
        );
        assert_eq!(format!("{}", Decision::Discuss), "discuss");
        assert_eq!(format!("{}", Decision::SkipForNow), "skipped");
    }

    #[test]
    fn notification_creation() {
        let goal_id = Uuid::new_v4();
        let notif = Notification::info("Sub-goal 2 of 5 started").with_goal_id(goal_id);
        assert_eq!(notif.level, NotificationLevel::Info);
        assert_eq!(notif.goal_id, Some(goal_id));
    }

    #[test]
    fn interaction_request_serialization_round_trip() {
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Test", 2)
            .with_metadata("thread_id", "T123");
        let json = serde_json::to_string(&req).unwrap();
        let restored: InteractionRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.interaction_id, req.interaction_id);
        assert_eq!(restored.kind, InteractionKind::DraftReview);
        assert_eq!(restored.metadata.get("thread_id").unwrap(), "T123");
    }

    #[test]
    fn interaction_response_serialization_round_trip() {
        let resp = InteractionResponse::new(
            Uuid::new_v4(),
            Decision::Reject {
                reason: "needs refactor".into(),
            },
        )
        .with_reasoning("too complex");
        let json = serde_json::to_string(&resp).unwrap();
        let restored: InteractionResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.decision, resp.decision);
        assert_eq!(restored.reasoning.as_deref(), Some("too complex"));
    }

    #[test]
    fn notification_serialization_round_trip() {
        let notif = Notification::warning("Drift detected");
        let json = serde_json::to_string(&notif).unwrap();
        let restored: Notification = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.message, "Drift detected");
        assert_eq!(restored.level, NotificationLevel::Warning);
    }

    #[test]
    fn channel_capabilities_defaults() {
        let caps = ChannelCapabilities::default();
        assert!(!caps.supports_async);
        assert!(!caps.supports_rich_media);
        assert!(!caps.supports_threads);
    }

    #[test]
    fn interaction_kind_custom() {
        let kind = InteractionKind::Custom("webhook_alert".into());
        assert_eq!(format!("{}", kind), "custom:webhook_alert");

        let json = serde_json::to_string(&kind).unwrap();
        let restored: InteractionKind = serde_json::from_str(&json).unwrap();
        assert_eq!(restored, kind);
    }

    #[test]
    fn interaction_request_display() {
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Test", 1);
        let display = format!("{}", req);
        assert!(display.contains("draft_review"));
        assert!(display.contains("blocking"));
    }
}