winx-code-agent 0.2.301

High-performance Rust implementation of WCGW for LLM code agents
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
#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]
use anyhow::{anyhow, Context as AnyhowContext, Result};
use glob;
use lazy_static::lazy_static;
use rand::RngExt;
use regex::Regex;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::io::{BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tracing::{debug, info, warn};

use crate::state::persistence::{
    delete_bash_state as delete_state_file, load_bash_state as load_state_file,
    save_bash_state as save_state_file, BashStateSnapshot,
};
use crate::state::pty::PtyShell;
use crate::state::terminal::{
    incremental_text, TerminalEmulator, TerminalOutputDiff, DEFAULT_MAX_SCREEN_LINES,
    MAX_OUTPUT_SIZE as TERMINAL_MAX_OUTPUT_SIZE,
};
use crate::types::{
    AllowedCommands, AllowedGlobs, BashCommandMode, BashMode, FileEditMode, Modes, WriteIfEmptyMode,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileWhitelistData {
    pub file_hash: String,
    pub line_ranges_read: Vec<(usize, usize)>,
    pub total_lines: usize,
}

impl FileWhitelistData {
    pub fn new(
        file_hash: String,
        line_ranges_read: Vec<(usize, usize)>,
        total_lines: usize,
    ) -> Self {
        Self { file_hash, line_ranges_read, total_lines }
    }

    pub fn is_read_enough(&self) -> bool {
        self.get_percentage_read() >= 99.0
    }

    pub fn get_percentage_read(&self) -> f64 {
        if self.total_lines == 0 {
            return 100.0;
        }
        let mut lines_read = std::collections::HashSet::new();
        for (start, end) in &self.line_ranges_read {
            for line in *start..=*end {
                lines_read.insert(line);
            }
        }
        (lines_read.len() as f64 / self.total_lines as f64) * 100.0
    }

    pub fn get_unread_ranges(&self) -> Vec<(usize, usize)> {
        if self.total_lines == 0 {
            return vec![];
        }
        let mut lines_read = std::collections::HashSet::new();
        for (start, end) in &self.line_ranges_read {
            for line in *start..=*end {
                lines_read.insert(line);
            }
        }
        let mut unread = vec![];
        let mut start_range = None;
        for i in 1..=self.total_lines {
            if !lines_read.contains(&i) {
                if start_range.is_none() {
                    start_range = Some(i);
                }
            } else if let Some(start) = start_range {
                unread.push((start, i - 1));
                start_range = None;
            }
        }
        if let Some(start) = start_range {
            unread.push((start, self.total_lines));
        }
        unread
    }

    pub fn add_range(&mut self, start: usize, end: usize) {
        self.line_ranges_read.push((start, end));
    }

    pub fn get_read_error_message(&self, file_path: &Path) -> String {
        format!(
            "File {} needs more reading. Coverage: {:.1}%",
            file_path.display(),
            self.get_percentage_read()
        )
    }

    pub fn needs_more_reading(&self) -> bool {
        !self.is_read_enough()
    }
}

#[derive(Debug, Clone)]
pub struct TerminalState {
    pub last_command: String,
    pub last_pending_output: String,
    pub command_running: bool,
    pub terminal_emulator: Arc<Mutex<TerminalEmulator>>,
    pub diff_detector: Option<TerminalOutputDiff>,
    pub limit_buffer: bool,
    pub max_buffer_lines: usize,
}

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

impl TerminalState {
    pub fn new() -> Self {
        Self {
            last_command: String::new(),
            last_pending_output: String::new(),
            command_running: false,
            terminal_emulator: Arc::new(Mutex::new(TerminalEmulator::new(160))),
            diff_detector: Some(TerminalOutputDiff::new()),
            limit_buffer: false,
            max_buffer_lines: DEFAULT_MAX_SCREEN_LINES,
        }
    }

    pub fn process_output(&mut self, output: &str) -> String {
        self.last_pending_output = output.to_string();
        if let Ok(mut emulator) = self.terminal_emulator.lock() {
            emulator.process(output);
            emulator.display().join("\n")
        } else {
            output.to_string()
        }
    }

    pub fn get_incremental_output(&mut self, output: &str) -> String {
        let result = incremental_text(output, &self.last_pending_output);
        self.last_pending_output = output.to_string();
        result
    }

    pub fn smart_truncate(&mut self, max_size: usize) {
        if let Ok(screen) = self.terminal_emulator.lock() {
            if let Ok(mut screen_guard) = screen.get_screen().lock() {
                screen_guard.smart_truncate(max_size);
            }
        }
    }
}

const WCGW_PROMPT_PATTERN: &str = r"◉ ([^\n]*)──➤";
const WCGW_PROMPT_COMMAND: &str = r#"printf '◉ "$(pwd)"──➤ '"#;
const BASH_PROMPT_STATEMENT: &str =
    r#"export GIT_PAGER=cat PAGER=cat PROMPT_COMMAND='printf \"◉ $(pwd)──➤ \"'"#;

lazy_static! {
    static ref PROMPT_REGEX: Regex = Regex::new(WCGW_PROMPT_PATTERN).expect("Invalid prompt regex");
}

fn contains_wcgw_prompt(text: &str) -> bool {
    PROMPT_REGEX.is_match(text)
}

const MAX_OUTPUT_SIZE: usize = 1_000_000;
const MAX_COMMAND_TIMEOUT: f32 = 60.0;
const DEFAULT_BUFFER_SIZE: usize = 8192;

#[derive(Debug, Clone, PartialEq)]
pub enum CommandState {
    Idle,
    Running { start_time: std::time::SystemTime, command: String },
}

#[derive(Debug, Clone)]
pub struct BashState {
    pub cwd: PathBuf,
    pub workspace_root: PathBuf,
    pub current_thread_id: String,
    pub mode: Modes,
    pub bash_command_mode: BashCommandMode,
    pub file_edit_mode: FileEditMode,
    pub write_if_empty_mode: WriteIfEmptyMode,
    pub whitelist_for_overwrite: HashMap<String, FileWhitelistData>,
    pub terminal_state: TerminalState,
    pub interactive_bash: Arc<Mutex<Option<InteractiveBash>>>,
    pub pty_shell: Arc<Mutex<Option<PtyShell>>>,
    pub initialized: bool,
}

#[derive(Debug)]
pub struct InteractiveBash {
    pub process: Child,
    pub last_command: String,
    pub last_output: String,
    pub output_buffer: String,
    pub command_state: CommandState,
    pub max_output_size: usize,
    pub output_truncated: bool,
    pub output_chunks: Vec<String>,
    initial_dir: PathBuf,
    restricted_mode: bool,
}

impl InteractiveBash {
    pub fn is_alive(&mut self) -> bool {
        matches!(self.process.try_wait(), Ok(None))
    }

    pub fn reinit(&mut self) -> Result<()> {
        let mut cmd = Command::new("bash");
        cmd.arg("-i");
        if self.restricted_mode {
            cmd.arg("-r");
        }
        let mut process = cmd
            .env("PAGER", "cat")
            .env("GIT_PAGER", "cat")
            .env("PROMPT_COMMAND", WCGW_PROMPT_COMMAND)
            .env("TERM", "xterm-256color")
            .current_dir(&self.initial_dir)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()?;
        let mut stdin = process.stdin.take().ok_or_else(|| anyhow!("No stdin"))?;
        writeln!(stdin, "{BASH_PROMPT_STATEMENT}")?;
        stdin.flush()?;
        process.stdin = Some(stdin);
        self.process = process;
        self.command_state = CommandState::Idle;
        Ok(())
    }

    pub fn ensure_alive(&mut self) -> Result<()> {
        if !self.is_alive() {
            self.reinit()?;
        }
        Ok(())
    }

    pub fn new(initial_dir: &Path, restricted_mode: bool) -> Result<Self> {
        let mut cmd = Command::new("bash");
        cmd.arg("-i");
        if restricted_mode {
            cmd.arg("-r");
        }
        let mut process = cmd
            .env("PAGER", "cat")
            .env("GIT_PAGER", "cat")
            .env("PROMPT_COMMAND", WCGW_PROMPT_COMMAND)
            .env("TERM", "xterm-256color")
            .current_dir(initial_dir)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()?;
        let mut stdin = process.stdin.take().ok_or_else(|| anyhow!("No stdin"))?;
        writeln!(stdin, "{BASH_PROMPT_STATEMENT}")?;
        stdin.flush()?;
        process.stdin = Some(stdin);
        Ok(Self {
            process,
            last_command: String::new(),
            last_output: String::new(),
            output_buffer: String::new(),
            command_state: CommandState::Idle,
            max_output_size: MAX_OUTPUT_SIZE,
            output_truncated: false,
            output_chunks: Vec::new(),
            initial_dir: initial_dir.to_path_buf(),
            restricted_mode,
        })
    }

    pub fn send_command(&mut self, command: &str) -> Result<()> {
        self.ensure_alive()?;
        let mut stdin = self.process.stdin.take().ok_or_else(|| anyhow!("No stdin"))?;
        writeln!(stdin, "{command}")?;
        stdin.flush()?;
        self.process.stdin = Some(stdin);
        self.last_command = command.to_string();
        self.command_state = CommandState::Running {
            start_time: std::time::SystemTime::now(),
            command: command.to_string(),
        };
        Ok(())
    }

    pub fn read_output(&mut self, timeout_secs: f32) -> Result<(String, bool)> {
        let timeout = Duration::from_secs_f32(timeout_secs.clamp(0.1, MAX_COMMAND_TIMEOUT));
        let start = Instant::now();
        let mut new_output = String::new();
        let mut complete = false;
        let mut full_output = self.last_output.clone();

        while start.elapsed() < timeout {
            let mut buf = vec![0; DEFAULT_BUFFER_SIZE];
            if let Some(stdout) = self.process.stdout.as_mut() {
                if let Ok(n) = stdout.read(&mut buf) {
                    if n > 0 {
                        let chunk = String::from_utf8_lossy(&buf[..n]);
                        full_output.push_str(&chunk);
                        new_output.push_str(&chunk);
                        if contains_wcgw_prompt(&full_output) {
                            complete = true;
                            break;
                        }
                    }
                }
            }
            std::thread::sleep(Duration::from_millis(10));
        }

        if complete {
            self.command_state = CommandState::Idle;
        }
        self.last_output.clone_from(&full_output);
        Ok((full_output, complete))
    }

    pub fn send_interrupt(&mut self) -> Result<()> {
        #[cfg(unix)]
        {
            let pid = self.process.id() as i32;
            unsafe {
                libc::kill(pid, libc::SIGINT);
            }
        }
        Ok(())
    }
}

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

impl BashState {
    pub fn new() -> Self {
        let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/tmp"));
        Self {
            cwd: cwd.clone(),
            workspace_root: cwd,
            current_thread_id: generate_thread_id(),
            mode: Modes::Wcgw,
            bash_command_mode: BashCommandMode {
                bash_mode: BashMode::NormalMode,
                allowed_commands: AllowedCommands::All("all".to_string()),
            },
            file_edit_mode: FileEditMode { allowed_globs: AllowedGlobs::All("all".to_string()) },
            write_if_empty_mode: WriteIfEmptyMode {
                allowed_globs: AllowedGlobs::All("all".to_string()),
            },
            whitelist_for_overwrite: HashMap::new(),
            terminal_state: TerminalState::new(),
            interactive_bash: Arc::new(Mutex::new(None)),
            pty_shell: Arc::new(Mutex::new(None)),
            initialized: false,
        }
    }

    pub fn init_interactive_bash(&mut self) -> Result<()> {
        let bash = InteractiveBash::new(
            &self.cwd,
            self.bash_command_mode.bash_mode == BashMode::RestrictedMode,
        )?;
        *self.interactive_bash.lock().unwrap() = Some(bash);
        Ok(())
    }

    pub fn update_cwd(&mut self, path: &Path) -> Result<()> {
        self.cwd = path.to_path_buf();
        Ok(())
    }

    pub fn update_workspace_root(&mut self, path: &Path) -> Result<()> {
        self.workspace_root = path.to_path_buf();
        Ok(())
    }

    pub fn is_command_allowed(&self, command: &str) -> bool {
        self.bash_command_mode.allowed_commands.is_allowed(command)
    }

    pub fn is_file_edit_allowed(&self, path: &str) -> bool {
        self.file_edit_mode.allowed_globs.is_allowed(path)
    }

    pub fn is_file_write_allowed(&self, path: &str) -> bool {
        self.write_if_empty_mode.allowed_globs.is_allowed(path)
    }
    pub fn get_mode_violation_message(&self, op: &str, _target: &str) -> String {
        format!("Operation {op} not allowed")
    }

    pub fn save_state_to_disk(&self) -> Result<()> {
        let snapshot = BashStateSnapshot::from_state(
            &self.cwd.to_string_lossy(),
            &self.workspace_root.to_string_lossy(),
            &self.mode,
            &self.bash_command_mode,
            &self.file_edit_mode,
            &self.write_if_empty_mode,
            &self.whitelist_for_overwrite,
            &self.current_thread_id,
        );
        save_state_file(&self.current_thread_id, &snapshot)?;
        Ok(())
    }

    pub fn load_state_from_disk(&mut self, thread_id: &str) -> Result<bool> {
        if let Some(snapshot) = load_state_file(thread_id)? {
            let (cwd, root, mode, bmode, emode, wmode, whitelist, tid) =
                snapshot.to_state_components();

            self.cwd = PathBuf::from(cwd);

            self.workspace_root = PathBuf::from(root);

            self.mode = mode;

            self.bash_command_mode = bmode;

            self.file_edit_mode = emode;

            self.write_if_empty_mode = wmode;

            self.whitelist_for_overwrite = whitelist;

            self.current_thread_id = tid;

            self.initialized = true;

            Ok(true)
        } else {
            Ok(false)
        }
    }

    pub fn new_with_thread_id(thread_id: Option<&str>) -> Self {
        let mut state = Self::new();

        if let Some(tid) = thread_id {
            if !tid.is_empty() {
                if let Ok(true) = state.load_state_from_disk(tid) {
                    info!("Loaded state for thread_id '{}'", tid);
                } else {
                    state.current_thread_id = tid.to_string();
                }
            }
        }

        state
    }
}

pub fn generate_thread_id() -> String {
    let mut rng = rand::rng();
    format!("tid_{:x}", rng.random::<u64>())
}