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
// terminal_channel.rs — Terminal-based ReviewChannel adapter.
//
// The default ReviewChannel implementation for v0.4.1.1. Renders interaction
// requests to stdout with formatting, collects responses from stdin.
// Supports mock I/O for testing.

use std::io::{BufRead, BufReader, Read, Write};
use std::sync::Mutex;

use crate::interaction::{
    ChannelCapabilities, Decision, InteractionKind, InteractionRequest, InteractionResponse,
    Notification, NotificationLevel,
};
use crate::review_channel::{ReviewChannel, ReviewChannelError};
use crate::session_channel::{HumanInput, SessionChannel, SessionChannelError, SessionEvent};

/// A ReviewChannel that uses stdin/stdout for human interaction.
///
/// Renders interaction requests as formatted text, prompts for input,
/// and parses responses into InteractionResponse values.
pub struct TerminalChannel {
    reader: Mutex<BufReader<Box<dyn Read + Send>>>,
    writer: Mutex<Box<dyn Write + Send>>,
    channel_id: String,
}

impl TerminalChannel {
    /// Create a TerminalChannel from raw reader/writer.
    /// Use `TerminalChannel::stdio()` for real terminal, or pass mock I/O for tests.
    pub fn new(
        reader: Box<dyn Read + Send>,
        writer: Box<dyn Write + Send>,
        channel_id: impl Into<String>,
    ) -> Self {
        Self {
            reader: Mutex::new(BufReader::new(reader)),
            writer: Mutex::new(writer),
            channel_id: channel_id.into(),
        }
    }

    /// Create a TerminalChannel that reads/writes to real stdin/stdout.
    pub fn stdio() -> Self {
        Self::new(
            Box::new(std::io::stdin()),
            Box::new(std::io::stdout()),
            "terminal:stdio",
        )
    }

    /// Render an interaction request as formatted text.
    fn render_request(&self, request: &InteractionRequest) -> String {
        let mut out = String::new();
        out.push('\n');
        out.push_str(&"=".repeat(60));
        out.push('\n');

        match &request.kind {
            InteractionKind::DraftReview => {
                out.push_str("  DRAFT REVIEW REQUIRED\n");
                out.push_str(&"-".repeat(60));
                out.push('\n');
                if let Some(summary) = request.context.get("summary").and_then(|v| v.as_str()) {
                    out.push_str(&format!("  Summary: {}\n", summary));
                }
                if let Some(count) = request
                    .context
                    .get("artifact_count")
                    .and_then(|v| v.as_u64())
                {
                    out.push_str(&format!("  Artifacts: {}\n", count));
                }
                if let Some(draft_id) = request.context.get("draft_id").and_then(|v| v.as_str()) {
                    out.push_str(&format!("  Draft ID: {}\n", draft_id));
                }
            }
            InteractionKind::PlanNegotiation => {
                out.push_str("  PLAN UPDATE PROPOSED\n");
                out.push_str(&"-".repeat(60));
                out.push('\n');
                if let Some(phase) = request.context.get("phase").and_then(|v| v.as_str()) {
                    out.push_str(&format!("  Phase: {}\n", phase));
                }
                if let Some(status) = request
                    .context
                    .get("proposed_status")
                    .and_then(|v| v.as_str())
                {
                    out.push_str(&format!("  Proposed status: {}\n", status));
                }
            }
            InteractionKind::ApprovalDiscussion => {
                out.push_str("  APPROVAL REQUIRED\n");
                out.push_str(&"-".repeat(60));
                out.push('\n');
                if let Some(msg) = request.context.as_str() {
                    out.push_str(&format!("  {}\n", msg));
                }
            }
            InteractionKind::Escalation => {
                out.push_str("  ESCALATION\n");
                out.push_str(&"-".repeat(60));
                out.push('\n');
                if let Some(reason) = request.context.get("reason").and_then(|v| v.as_str()) {
                    out.push_str(&format!("  Reason: {}\n", reason));
                }
            }
            InteractionKind::AgentQuestion => {
                out.push_str("  AGENT QUESTION\n");
                out.push_str(&"-".repeat(60));
                out.push('\n');
                if let Some(q) = request.context.get("question").and_then(|v| v.as_str()) {
                    out.push_str(&format!("  Question: {}\n", q));
                }
                if let Some(ctx) = request.context.get("context").and_then(|v| v.as_str()) {
                    out.push_str(&format!("  Context: {}\n", ctx));
                }
                if let Some(hint) = request
                    .context
                    .get("response_hint")
                    .and_then(|v| v.as_str())
                {
                    out.push_str(&format!("  Expected response: {}\n", hint));
                }
            }
            InteractionKind::Custom(name) => {
                out.push_str(&format!("  INTERACTION: {}\n", name.to_uppercase()));
                out.push_str(&"-".repeat(60));
                out.push('\n');
            }
        }

        out.push_str(&"-".repeat(60));
        out.push('\n');
        out.push_str("  [a]pprove  [r]eject  [d]iscuss  [s]kip\n");
        out.push_str(&"=".repeat(60));
        out.push_str("\n> ");
        out
    }

    /// Parse a user's text response into a Decision.
    fn parse_decision(input: &str) -> Result<Decision, ReviewChannelError> {
        let trimmed = input.trim().to_lowercase();
        match trimmed.as_str() {
            "a" | "approve" | "y" | "yes" => Ok(Decision::Approve),
            "d" | "discuss" => Ok(Decision::Discuss),
            "s" | "skip" => Ok(Decision::SkipForNow),
            _ if trimmed.starts_with("r") || trimmed.starts_with("n") => {
                // "r", "reject", "n", "no" — optionally followed by a reason
                let reason = if trimmed.len() > 1 {
                    // "reject: reason" or "r reason" or "r: reason"
                    let rest = trimmed
                        .trim_start_matches("reject")
                        .trim_start_matches("no")
                        .trim_start_matches('r')
                        .trim_start_matches('n')
                        .trim_start_matches(':')
                        .trim();
                    if rest.is_empty() {
                        "rejected by reviewer".to_string()
                    } else {
                        rest.to_string()
                    }
                } else {
                    "rejected by reviewer".to_string()
                };
                Ok(Decision::Reject { reason })
            }
            "" => Err(ReviewChannelError::InvalidResponse("empty response".into())),
            _ => Err(ReviewChannelError::InvalidResponse(format!(
                "unrecognized input: '{}'",
                trimmed
            ))),
        }
    }

    /// Render a notification as formatted text.
    fn render_notification(notification: &Notification) -> String {
        let prefix = match notification.level {
            NotificationLevel::Debug => "[DEBUG]",
            NotificationLevel::Info => "[INFO]",
            NotificationLevel::Warning => "[WARN]",
            NotificationLevel::Error => "[ERROR]",
        };
        format!("{} {}\n", prefix, notification.message)
    }
}

impl ReviewChannel for TerminalChannel {
    fn request_interaction(
        &self,
        request: &InteractionRequest,
    ) -> Result<InteractionResponse, ReviewChannelError> {
        let rendered = self.render_request(request);

        // Write the rendered request to output.
        {
            let mut writer = self
                .writer
                .lock()
                .map_err(|e| ReviewChannelError::Other(format!("writer lock poisoned: {}", e)))?;
            writer.write_all(rendered.as_bytes())?;
            writer.flush()?;
        }

        // Read the response from input.
        let mut line = String::new();
        {
            let mut reader = self
                .reader
                .lock()
                .map_err(|e| ReviewChannelError::Other(format!("reader lock poisoned: {}", e)))?;
            let bytes = reader.read_line(&mut line)?;
            if bytes == 0 {
                return Err(ReviewChannelError::ChannelClosed);
            }
        }

        let decision = Self::parse_decision(&line)?;

        Ok(InteractionResponse::new(request.interaction_id, decision)
            .with_responder(&self.channel_id))
    }

    fn notify(&self, notification: &Notification) -> Result<(), ReviewChannelError> {
        let rendered = Self::render_notification(notification);
        let mut writer = self
            .writer
            .lock()
            .map_err(|e| ReviewChannelError::Other(format!("writer lock poisoned: {}", e)))?;
        writer.write_all(rendered.as_bytes())?;
        writer.flush()?;
        Ok(())
    }

    fn capabilities(&self) -> ChannelCapabilities {
        ChannelCapabilities {
            supports_async: false,
            supports_rich_media: false,
            supports_threads: false,
        }
    }

    fn channel_id(&self) -> &str {
        &self.channel_id
    }
}

/// A no-op ReviewChannel that auto-approves all interactions.
/// Useful for non-interactive/batch mode and testing.
pub struct AutoApproveChannel {
    channel_id: String,
}

impl AutoApproveChannel {
    pub fn new() -> Self {
        Self {
            channel_id: "auto-approve".to_string(),
        }
    }
}

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

/// A SessionChannel implementation for terminal-based interaction (v0.7.0).
///
/// Emits session events to stdout and reads human input from stdin.
pub struct TerminalSessionChannel {
    channel_id: String,
}

impl TerminalSessionChannel {
    pub fn new() -> Self {
        Self {
            channel_id: "terminal:session".to_string(),
        }
    }
}

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

impl SessionChannel for TerminalSessionChannel {
    fn emit(&self, event: &SessionEvent) -> Result<(), SessionChannelError> {
        println!("{}", event);
        Ok(())
    }

    fn receive(
        &self,
        timeout: std::time::Duration,
    ) -> Result<Option<HumanInput>, SessionChannelError> {
        // Simple blocking stdin read with no real timeout (terminal limitation).
        // In practice, the agent process drives the session and the human types
        // when prompted. A full PTY-based implementation would use non-blocking I/O.
        let _ = timeout;
        let mut line = String::new();
        match std::io::stdin().read_line(&mut line) {
            Ok(0) => Ok(None), // EOF
            Ok(_) => {
                let trimmed = line.trim();
                if trimmed.is_empty() {
                    Ok(None)
                } else if trimmed.eq_ignore_ascii_case("abort") {
                    Ok(Some(HumanInput::Abort))
                } else {
                    Ok(Some(HumanInput::Message {
                        text: trimmed.to_string(),
                    }))
                }
            }
            Err(e) => Err(SessionChannelError::Io(e)),
        }
    }

    fn channel_id(&self) -> &str {
        &self.channel_id
    }
}

impl ReviewChannel for AutoApproveChannel {
    fn request_interaction(
        &self,
        request: &InteractionRequest,
    ) -> Result<InteractionResponse, ReviewChannelError> {
        Ok(
            InteractionResponse::new(request.interaction_id, Decision::Approve)
                .with_responder(&self.channel_id),
        )
    }

    fn notify(&self, _notification: &Notification) -> Result<(), ReviewChannelError> {
        Ok(())
    }

    fn capabilities(&self) -> ChannelCapabilities {
        ChannelCapabilities::default()
    }

    fn channel_id(&self) -> &str {
        &self.channel_id
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::interaction::Notification;
    use std::io::Cursor;
    use uuid::Uuid;

    fn mock_channel(input: &str) -> (TerminalChannel, std::sync::Arc<Mutex<Vec<u8>>>) {
        let output_buf = std::sync::Arc::new(Mutex::new(Vec::new()));
        let output_writer = output_buf.clone();

        struct SharedWriter(std::sync::Arc<Mutex<Vec<u8>>>);
        impl Write for SharedWriter {
            fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
                self.0.lock().unwrap().write(buf)
            }
            fn flush(&mut self) -> std::io::Result<()> {
                Ok(())
            }
        }

        let reader = Box::new(Cursor::new(input.as_bytes().to_vec()));
        let writer = Box::new(SharedWriter(output_writer));
        let channel = TerminalChannel::new(reader, writer, "test:mock");
        (channel, output_buf)
    }

    #[test]
    fn approve_draft_review() {
        let (channel, _output) = mock_channel("a\n");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Test draft", 3);
        let resp = channel.request_interaction(&req).unwrap();
        assert_eq!(resp.decision, Decision::Approve);
        assert_eq!(resp.interaction_id, req.interaction_id);
        assert_eq!(resp.responder_id.as_deref(), Some("test:mock"));
    }

    #[test]
    fn reject_with_reason() {
        let (channel, _output) = mock_channel("reject: needs more tests\n");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Draft", 1);
        let resp = channel.request_interaction(&req).unwrap();
        assert_eq!(
            resp.decision,
            Decision::Reject {
                reason: "needs more tests".into()
            }
        );
    }

    #[test]
    fn reject_shorthand() {
        let (channel, _output) = mock_channel("r\n");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Draft", 1);
        let resp = channel.request_interaction(&req).unwrap();
        assert!(matches!(resp.decision, Decision::Reject { .. }));
    }

    #[test]
    fn discuss_response() {
        let (channel, _output) = mock_channel("d\n");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Draft", 1);
        let resp = channel.request_interaction(&req).unwrap();
        assert_eq!(resp.decision, Decision::Discuss);
    }

    #[test]
    fn skip_response() {
        let (channel, _output) = mock_channel("s\n");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Draft", 1);
        let resp = channel.request_interaction(&req).unwrap();
        assert_eq!(resp.decision, Decision::SkipForNow);
    }

    #[test]
    fn yes_is_approve() {
        let (channel, _output) = mock_channel("yes\n");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Draft", 1);
        let resp = channel.request_interaction(&req).unwrap();
        assert_eq!(resp.decision, Decision::Approve);
    }

    #[test]
    fn empty_input_is_error() {
        let (channel, _output) = mock_channel("\n");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Draft", 1);
        let result = channel.request_interaction(&req);
        assert!(matches!(
            result,
            Err(ReviewChannelError::InvalidResponse(_))
        ));
    }

    #[test]
    fn eof_is_channel_closed() {
        let (channel, _output) = mock_channel("");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Draft", 1);
        let result = channel.request_interaction(&req);
        assert!(matches!(result, Err(ReviewChannelError::ChannelClosed)));
    }

    #[test]
    fn renders_draft_review_output() {
        let (channel, output) = mock_channel("a\n");
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Add auth module", 5);
        channel.request_interaction(&req).unwrap();

        let rendered = String::from_utf8(output.lock().unwrap().clone()).unwrap();
        assert!(rendered.contains("DRAFT REVIEW REQUIRED"));
        assert!(rendered.contains("Add auth module"));
        assert!(rendered.contains("Artifacts: 5"));
        assert!(rendered.contains("[a]pprove"));
    }

    #[test]
    fn renders_plan_negotiation() {
        let (channel, output) = mock_channel("a\n");
        let req = InteractionRequest::plan_negotiation("v0.4.2", "done");
        channel.request_interaction(&req).unwrap();

        let rendered = String::from_utf8(output.lock().unwrap().clone()).unwrap();
        assert!(rendered.contains("PLAN UPDATE PROPOSED"));
        assert!(rendered.contains("v0.4.2"));
    }

    #[test]
    fn notify_renders_to_output() {
        let (channel, output) = mock_channel("");
        let notif = Notification::info("Sub-goal 2 of 5 complete");
        channel.notify(&notif).unwrap();

        let rendered = String::from_utf8(output.lock().unwrap().clone()).unwrap();
        assert!(rendered.contains("[INFO]"));
        assert!(rendered.contains("Sub-goal 2 of 5 complete"));
    }

    #[test]
    fn notify_warning_prefix() {
        let (channel, output) = mock_channel("");
        let notif = Notification::warning("Agent approaching token limit");
        channel.notify(&notif).unwrap();

        let rendered = String::from_utf8(output.lock().unwrap().clone()).unwrap();
        assert!(rendered.contains("[WARN]"));
    }

    #[test]
    fn channel_capabilities() {
        let (channel, _) = mock_channel("");
        let caps = channel.capabilities();
        assert!(!caps.supports_async);
        assert!(!caps.supports_rich_media);
        assert!(!caps.supports_threads);
    }

    #[test]
    fn channel_id_returns_configured_id() {
        let (channel, _) = mock_channel("");
        assert_eq!(channel.channel_id(), "test:mock");
    }

    #[test]
    fn auto_approve_channel_approves_all() {
        let channel = AutoApproveChannel::new();
        let req = InteractionRequest::draft_review(Uuid::new_v4(), "Any draft", 10);
        let resp = channel.request_interaction(&req).unwrap();
        assert_eq!(resp.decision, Decision::Approve);
        assert_eq!(resp.responder_id.as_deref(), Some("auto-approve"));
    }

    #[test]
    fn auto_approve_channel_notify_is_noop() {
        let channel = AutoApproveChannel::new();
        let notif = Notification::info("test");
        assert!(channel.notify(&notif).is_ok());
    }

    #[test]
    fn parse_decision_variants() {
        assert_eq!(
            TerminalChannel::parse_decision("approve").unwrap(),
            Decision::Approve
        );
        assert_eq!(
            TerminalChannel::parse_decision("y").unwrap(),
            Decision::Approve
        );
        assert_eq!(
            TerminalChannel::parse_decision("discuss").unwrap(),
            Decision::Discuss
        );
        assert_eq!(
            TerminalChannel::parse_decision("skip").unwrap(),
            Decision::SkipForNow
        );
        assert!(TerminalChannel::parse_decision("unknown").is_err());
    }
}