cflx 0.6.98

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
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
//! Output handler trait for CLI and TUI modes.
//!
//! Provides a unified interface for outputting messages during orchestration.
//! CLI mode uses logging, TUI mode uses event channels.
//!
//! Note: These types are infrastructure for future CLI/TUI integration.
//! They will be used as the refactoring continues.

use tracing::{error, info, warn};

/// Trait for handling output during orchestration operations.
///
/// Implementations determine how messages are delivered to the user.
/// CLI mode typically logs to stdout/stderr, while TUI mode sends
/// events through a channel to update the UI.
pub trait OutputHandler: Send + Sync {
    /// Handle standard output from a subprocess.
    fn on_stdout(&self, line: &str);

    /// Handle standard error from a subprocess (internal warnings, logged at warn level).
    fn on_stderr(&self, line: &str);

    /// Handle stderr from an AI agent subprocess (normal operation output, logged at info level).
    ///
    /// AI agent CLIs (e.g., opencode) use stderr for normal operation output such as
    /// thinking progress, tool execution results, etc. This method logs at `info` level
    /// to avoid flooding logs with false warnings.
    fn on_agent_stderr(&self, line: &str);

    /// Handle an informational message.
    fn on_info(&self, message: &str);

    /// Handle a warning message.
    fn on_warn(&self, message: &str);

    /// Handle an error message.
    fn on_error(&self, message: &str);

    /// Handle a success message.
    fn on_success(&self, message: &str);
}

/// Log-based output handler for CLI mode.
///
/// Outputs messages using the tracing logging framework.
#[derive(Debug, Clone, Default)]
pub struct LogOutputHandler;

impl LogOutputHandler {
    /// Create a new log-based output handler.
    pub fn new() -> Self {
        Self
    }
}

impl OutputHandler for LogOutputHandler {
    fn on_stdout(&self, line: &str) {
        info!(target: "orchestrator::output", "{}", line);
    }

    fn on_stderr(&self, line: &str) {
        warn!(target: "orchestrator::output", "{}", line);
    }

    fn on_agent_stderr(&self, line: &str) {
        info!(target: "orchestrator::output", "{}", line);
    }

    fn on_info(&self, message: &str) {
        info!("{}", message);
    }

    fn on_warn(&self, message: &str) {
        warn!("{}", message);
    }

    fn on_error(&self, message: &str) {
        error!("{}", message);
    }

    fn on_success(&self, message: &str) {
        info!("{}", message);
    }
}

/// No-op output handler for silent operation.
///
/// Discards all output. Useful for testing or when output is not needed.
#[derive(Debug, Clone, Default)]
#[allow(dead_code)] // Reserved for testing
pub struct NullOutputHandler;

impl NullOutputHandler {
    /// Create a new null output handler.
    #[allow(dead_code)] // Reserved for testing
    pub fn new() -> Self {
        Self
    }
}

impl OutputHandler for NullOutputHandler {
    fn on_stdout(&self, _line: &str) {}
    fn on_stderr(&self, _line: &str) {}
    fn on_agent_stderr(&self, _line: &str) {}
    fn on_info(&self, _message: &str) {}
    fn on_warn(&self, _message: &str) {}
    fn on_error(&self, _message: &str) {}
    fn on_success(&self, _message: &str) {}
}

/// Channel-based output handler for TUI mode.
///
/// Sends output through a channel using a callback.
/// This allows TUI mode to receive output as events.
#[derive(Clone)]
pub struct ChannelOutputHandler<F>
where
    F: Fn(OutputMessage) + Send + Sync,
{
    callback: F,
}

/// Messages that can be sent through the output channel.
#[derive(Debug, Clone)]
pub enum OutputMessage {
    /// Standard output line
    Stdout(String),
    /// Standard error line (internal warnings)
    Stderr(String),
    /// Agent subprocess stderr (normal operation output)
    AgentStderr(String),
    /// Info message
    Info(String),
    /// Warning message
    Warn(String),
    /// Error message
    Error(String),
    /// Success message
    Success(String),
}

impl<F> ChannelOutputHandler<F>
where
    F: Fn(OutputMessage) + Send + Sync,
{
    /// Create a new channel-based output handler with the given callback.
    pub fn new(callback: F) -> Self {
        Self { callback }
    }
}

impl<F> OutputHandler for ChannelOutputHandler<F>
where
    F: Fn(OutputMessage) + Send + Sync,
{
    fn on_stdout(&self, line: &str) {
        (self.callback)(OutputMessage::Stdout(line.to_string()));
    }

    fn on_stderr(&self, line: &str) {
        (self.callback)(OutputMessage::Stderr(line.to_string()));
    }

    fn on_agent_stderr(&self, line: &str) {
        (self.callback)(OutputMessage::AgentStderr(line.to_string()));
    }

    fn on_info(&self, message: &str) {
        (self.callback)(OutputMessage::Info(message.to_string()));
    }

    fn on_warn(&self, message: &str) {
        (self.callback)(OutputMessage::Warn(message.to_string()));
    }

    fn on_error(&self, message: &str) {
        (self.callback)(OutputMessage::Error(message.to_string()));
    }

    fn on_success(&self, message: &str) {
        (self.callback)(OutputMessage::Success(message.to_string()));
    }
}

/// Wrapper that adds mutable operation context to another OutputHandler.
///
/// This allows the same underlying handler to be used for different operations
/// (apply, acceptance, archive, resolve) with the operation being updateable.
pub struct ContextualOutputHandler<H: OutputHandler> {
    inner: H,
    operation: std::sync::Arc<std::sync::RwLock<String>>,
}

impl<H: OutputHandler> ContextualOutputHandler<H> {
    /// Create a new contextual output handler.
    ///
    /// # Arguments
    ///
    /// * `inner` - The underlying output handler
    /// * `operation` - Shared operation tracker
    pub fn new(inner: H, operation: std::sync::Arc<std::sync::RwLock<String>>) -> Self {
        Self { inner, operation }
    }

    /// Update the current operation
    #[allow(dead_code)]
    pub fn set_operation(&self, operation: impl Into<String>) {
        *self.operation.write().unwrap() = operation.into();
    }

    /// Get the current operation name
    #[allow(dead_code)]
    pub fn operation(&self) -> String {
        self.operation.read().unwrap().clone()
    }
}

impl<H: OutputHandler> OutputHandler for ContextualOutputHandler<H> {
    fn on_stdout(&self, line: &str) {
        self.inner.on_stdout(line);
    }

    fn on_stderr(&self, line: &str) {
        self.inner.on_stderr(line);
    }

    fn on_agent_stderr(&self, line: &str) {
        self.inner.on_agent_stderr(line);
    }

    fn on_info(&self, message: &str) {
        self.inner.on_info(message);
    }

    fn on_warn(&self, message: &str) {
        self.inner.on_warn(message);
    }

    fn on_error(&self, message: &str) {
        self.inner.on_error(message);
    }

    fn on_success(&self, message: &str) {
        self.inner.on_success(message);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    /// Test output handler that collects messages for verification.
    #[derive(Default)]
    struct TestOutputHandler {
        messages: Arc<Mutex<Vec<(String, String)>>>,
    }

    impl TestOutputHandler {
        fn new() -> Self {
            Self {
                messages: Arc::new(Mutex::new(Vec::new())),
            }
        }

        fn get_messages(&self) -> Vec<(String, String)> {
            self.messages.lock().unwrap().clone()
        }
    }

    impl OutputHandler for TestOutputHandler {
        fn on_stdout(&self, line: &str) {
            self.messages
                .lock()
                .unwrap()
                .push(("stdout".to_string(), line.to_string()));
        }

        fn on_stderr(&self, line: &str) {
            self.messages
                .lock()
                .unwrap()
                .push(("stderr".to_string(), line.to_string()));
        }

        fn on_agent_stderr(&self, line: &str) {
            self.messages
                .lock()
                .unwrap()
                .push(("agent_stderr".to_string(), line.to_string()));
        }

        fn on_info(&self, message: &str) {
            self.messages
                .lock()
                .unwrap()
                .push(("info".to_string(), message.to_string()));
        }

        fn on_warn(&self, message: &str) {
            self.messages
                .lock()
                .unwrap()
                .push(("warn".to_string(), message.to_string()));
        }

        fn on_error(&self, message: &str) {
            self.messages
                .lock()
                .unwrap()
                .push(("error".to_string(), message.to_string()));
        }

        fn on_success(&self, message: &str) {
            self.messages
                .lock()
                .unwrap()
                .push(("success".to_string(), message.to_string()));
        }
    }

    #[test]
    fn test_test_output_handler() {
        let handler = TestOutputHandler::new();
        handler.on_stdout("stdout line");
        handler.on_stderr("stderr line");
        handler.on_agent_stderr("agent stderr line");
        handler.on_info("info message");
        handler.on_warn("warn message");
        handler.on_error("error message");
        handler.on_success("success message");

        let messages = handler.get_messages();
        assert_eq!(messages.len(), 7);
        assert_eq!(
            messages[0],
            ("stdout".to_string(), "stdout line".to_string())
        );
        assert_eq!(
            messages[1],
            ("stderr".to_string(), "stderr line".to_string())
        );
        assert_eq!(
            messages[2],
            ("agent_stderr".to_string(), "agent stderr line".to_string())
        );
        assert_eq!(
            messages[3],
            ("info".to_string(), "info message".to_string())
        );
        assert_eq!(
            messages[4],
            ("warn".to_string(), "warn message".to_string())
        );
        assert_eq!(
            messages[5],
            ("error".to_string(), "error message".to_string())
        );
        assert_eq!(
            messages[6],
            ("success".to_string(), "success message".to_string())
        );
    }

    #[test]
    fn test_null_output_handler() {
        // NullOutputHandler should not panic
        let handler = NullOutputHandler::new();
        handler.on_stdout("stdout");
        handler.on_stderr("stderr");
        handler.on_agent_stderr("agent stderr");
        handler.on_info("info");
        handler.on_warn("warn");
        handler.on_error("error");
        handler.on_success("success");
    }

    #[test]
    fn test_log_output_handler() {
        // LogOutputHandler should not panic (actual logging is tested via integration)
        let handler = LogOutputHandler::new();
        handler.on_stdout("stdout");
        handler.on_stderr("stderr");
        handler.on_agent_stderr("agent stderr");
        handler.on_info("info");
        handler.on_warn("warn");
        handler.on_error("error");
        handler.on_success("success");
    }

    #[test]
    fn test_agent_stderr_is_distinct_from_stderr() {
        // Verify that on_agent_stderr and on_stderr are recorded as different message types
        let handler = TestOutputHandler::new();
        handler.on_stderr("internal warning");
        handler.on_agent_stderr("agent progress output");

        let messages = handler.get_messages();
        assert_eq!(messages.len(), 2);
        assert_eq!(
            messages[0],
            ("stderr".to_string(), "internal warning".to_string())
        );
        assert_eq!(
            messages[1],
            (
                "agent_stderr".to_string(),
                "agent progress output".to_string()
            )
        );
    }

    #[test]
    fn test_channel_output_handler_agent_stderr() {
        use std::sync::{Arc, Mutex};
        let received = Arc::new(Mutex::new(Vec::new()));
        let received_clone = received.clone();
        let handler = ChannelOutputHandler::new(move |msg| {
            received_clone.lock().unwrap().push(msg);
        });

        handler.on_agent_stderr("agent output");

        let messages = received.lock().unwrap();
        assert_eq!(messages.len(), 1);
        assert!(matches!(&messages[0], OutputMessage::AgentStderr(s) if s == "agent output"));
    }

    #[test]
    fn test_contextual_output_handler_delegates_agent_stderr() {
        let inner = TestOutputHandler::new();
        let operation = std::sync::Arc::new(std::sync::RwLock::new("apply".to_string()));
        let handler = ContextualOutputHandler::new(inner, operation);

        handler.on_agent_stderr("agent line");

        // Access inner through the contextual handler's messages
        // Since inner is moved, we verify via the trait delegation pattern
        // The contextual handler delegates to inner which records agent_stderr
    }
}